I have a directory ~/foo that contains several org files. I'd like to use htmlize to convert each org file into an html file. Ideally this would be done with a shell script. Is there a way to make a shell script use htmlize to accomplish this?


You can use the following emacs lisp code saved in htmlize-script.el:

(add-to-list 'load-path "/path/to/htmlize/")
(require 'htmlize)

(defun htmlize-files-in-dir (dir)
  (htmlize-many-files (directory-files dir t ".org$")))

And then use the following command from a script:

emacs -Q -nw --eval "(progn (load-file \"/path/to/htmlize-script.el\") (htmlize-files-in-dir \"$1\"))" --kill

Call the script with scriptname /path/to/org/files. This will shortly show up the textual frame as was pointed out in the comments of the answer of unhammer, if you want to avoid that you can run emacs as a server and use the following command:

emacsclient --eval "(progn (load-file \"/path/to/htmlize-script.el\") (htmlize-files-in-dir \"$1\"))"
  • This answer has really helped me. Thanks! Out of curiosity, how could I modify this to have a only operate on a single file? I think htmlize-many-files would be changed to htmlize-file. Would any other changes need to be made? – Brian Fitzpatrick Aug 27 '15 at 7:26
  • Using htmlize-file should work. I think you can just replace the (htmlize-files-in-dir \"$1\") with (htmlize-file \"$1\") in the script command and call the script with the filename. – clemera Aug 27 '15 at 9:08

You can run emacs --batch f htmlize-my-org --kill where htmlize-my-org is a function you've written that runs htmlize on those files.

You can also pass files on the command line, e.g. emacs --batch --insert ~/foo/file1.org -f htmlize-my-org --kill

As a more complete example, here's a way to run M-x delete-trailing-whitespace on all files in a dir:

$ for file in ~/fixme/*; do 
      emacs -Q --batch --insert "${file}" -f delete-trailing-whitespace \
        --eval "(write-file \"${file}.fixed\")" --kill

See man emacs (search for "batch") for more info.

  • 2
    Note that running htmlize from batch mode will result in less colorful output because it's reusing font-lock information and font-lock is disabled in batch mode... – wasamasa Aug 22 '15 at 7:34
  • Can you turn on font-lock in your htmlize-my-org function? – unhammer Aug 22 '15 at 7:37
  • Well, not really. If you look at the sources of font-lock-mode, you'll see it deactivates the variable controlling whether it is turned on. If you just use the function being run depending on that variable, it still doesn't seem to work, probably because it's using the "JIT" variant by default which only starts fontifying when used interactively and if you just define noninteractive, you get loads of other unrelated errors. So, to conclude, IME Emacs sucks for batch processing stuff. – wasamasa Aug 22 '15 at 7:50
  • 1
    That should work, the only annoyance is that Emacs would briefly flash a graphical or textual frame before quitting. Another option would be using the daemon and client which avoids the flash. – wasamasa Aug 22 '15 at 8:08
  • 1
    If you run it without --batch the flickering becomes less of a problem when you don't start an emacs instance for every file so it will only flicker one time. See my answer for details. – clemera Aug 22 '15 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.