Emacs Lisp: emacs-org-to-html.el on command line, convert your Org files on command line to HTML output

Emacs Lisp: emacs-org-to-html.el on command line, convert your Org files on command line to HTML output

This is one way of converting the Org files on command line to HTML output. This is script that shall be made executable with chmod +x and it runs on command line. It may be used in website revision systems to feed Org input and get the HTML output. It could redirect to HTML file quickly, without launching full GNU Emacs. You may see inside that one function is to output full HTML and other only the body of the HTML. This is for those people who need to use templates.

It uses peculiar way to read standard input by using GNU Emacs Lisp programming language. It uses read-from-minibuffer to read standard input, concatenates the lines and emits html out.

#!/usr/local/bin/emacs --script

;;;; Good idea from https://joelmccracken.github.io/entries/reading-writing-data-in-emacs-batch-via-stdin-stdout/

(defun org-stdin-to-html-full ()
  "Reads org text body from STDIN and export full HTML"
  (let ((org-document-content "")
    this-read)
    (while (setq this-read (ignore-errors
                             (read-from-minibuffer "")))
      (setq org-document-content (concat org-document-content "\n" this-read)))

    (with-temp-buffer
      (org-mode)
      (insert org-document-content)
      (org-html-export-as-html)
      (princ (buffer-string)))))


(defun org-stdin-to-html-body-only ()
  "Reads org text body from STDIN and export full only body HTML"
  (let ((org-document-content "")
    this-read)
    (while (setq this-read (ignore-errors
                             (read-from-minibuffer "")))
      (setq org-document-content (concat org-document-content "\n" this-read)))

    (with-temp-buffer
      (org-mode)
      (insert org-document-content)
      (org-html-export-as-html nil nil nil t)
      (princ (buffer-string)))))

(org-stdin-to-html-body-only)

Then on the command line, you would do something like following:

$ emacs-org-to-html.el

#+TITLE: My title herre

* Heading

** Second heading
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org6e055e7">1. Heading</a>
<ul>
<li><a href="#orgb48b32d">1.1. Second heading</a></li>
</ul>
</li>
</ul>
</div>
</div>

<div id="outline-container-org6e055e7" class="outline-2">
<h2 id="org6e055e7"><span class="section-number-2">1</span> Heading</h2>
<div class="outline-text-2" id="text-1">
</div>
<div id="outline-container-orgb48b32d" class="outline-3">
<h3 id="orgb48b32d"><span class="section-number-3">1.1</span> Second heading</h3>
</div>
</div>

Of course it is intended to feed Org files programmatically into such script.

It will also work in following manner:

$ emacs-org-to-html.el < new.org

and also like this with the redirection to HTML file:

$ emacs-org-to-html.el < new.org  > new.html

If you have any improvements, let me know.

Leave Your Comment or Contact GNU.Support

Contact GNU.Support now. There is a simple rule at GNU.Support: if we can help you, we do, whenever and wherever necessary, and it's the way we've been doing business since 2002, and the only way we know


Full name:


E-mail:


Message: