Org-DoccoThe
The way this works is an Org-mode document with embedded code blocks
is exported to html using the standard Org-mode export functions.
This file defines a new function named
A pure source code file can be extracted (or "tangled") from the
Org-mode document using the normal Disclaimer: this currently only works on very simple Org-mode files which have no headings but rather are just a collection of alternating text and code blocks. It wouldn't be difficult to generalize the following code so that it could be run in particular sub-trees but I simply don't have the time to do so myself, and this version perfectly satisfies my own limit needs. I make no promises to support this code moving forward. Caveat Emptor |
;;; org-docco.el --- docco type html generation from Org-mode ;; Copyright (C) 2012 Eric Schulte ;; Author: Eric Schulte ;; Keywords: org-mode, literate programming, html ;; Homepage: http://orgmode.org/worg/org-contrib/org-mime.php ;; Version: 0.01 ;; This file is not part of GNU Emacs. ;;; License: ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation; either version 3, or (at your option) ;; any later version. ;; ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; ;; You should have received a copy of the GNU General Public License ;; along with GNU Emacs; see the file COPYING. If not, write to the ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, ;; Boston, MA 02110-1301, USA. ;;; Commentary: ;; <- look over there |
---|---|
The |
;;; Code: (require 'cl) |
This is a function which returns the buffer positions of matching regular expressions. It has two special features…
|
(defun org-docco-balanced-re (beg-re end-re) "Return the beginning and of a balanced regexp." (save-excursion (save-match-data (let ((both-re (concat "\\(" beg-re "\\|" end-re "\\)")) (beg-count 0) (end-count 0) beg end) (when (re-search-forward beg-re nil t) (goto-char (match-beginning 0)) (setq beg (point-marker)) (incf beg-count) (goto-char (match-end 0)) (while (and (not end) (re-search-forward both-re nil t)) (goto-char (match-beginning 0)) (cond ((looking-at beg-re) (incf beg-count)) ((looking-at end-re) (incf end-count)) (:otherwise (error "miss-matched"))) (goto-char (match-end 0)) (when (= beg-count end-count) (setq end (point-marker)))) (when end (cons beg end))))))) |
This ugly large function does the actual conversion. It wraps the
entire main content
|
(defun org-docco-buffer () "Call from within an HTML buffer to doccoize it." (interactive) (let ((table-start "<table>\n") (doc-row-start "<tr><th class=\"docs\">\n") (doc-row-end "</th>\n") (code-row-start " <td class=\"code\">\n") (code-row-end "</td></tr>\n") (table-end "</table>" ) pair transition-points next) (save-excursion (save-match-data (goto-char (point-min)) (when (re-search-forward "<div id=\"content\">" nil t) (goto-char (match-end 0)) (push (point-marker) transition-points) (goto-char (match-beginning 0)) (setq pair (org-docco-balanced-re "<div" "</div>")) (while (setq next (org-docco-balanced-re "<pre class=\"src" "</pre>")) (goto-char (cdr next)) (push (car next) transition-points) (push (cdr next) transition-points)) (goto-char (cdr pair)) (push (and (re-search-backward "</div>" nil t) (point-marker)) transition-points) ;; collected transitions, so build the table (setq transition-points (nreverse transition-points)) (goto-char (pop transition-points)) (insert table-start doc-row-start) (while (> (length transition-points) 1) (goto-char (pop transition-points)) (insert doc-row-end code-row-start) (goto-char (pop transition-points)) (insert code-row-end doc-row-start)) (goto-char (pop transition-points)) (insert code-row-end table-end) (unless (null transition-points) (error "leftover points"))))))) |
We'll use Emacs File Local Variables and the
|
(defvar org-docco-doccoize-me nil "File local variable controlling if html export should be doccoized.") (make-local-variable 'org-docco-doccoize-me) |
A simple function will conditionally process HTML output based on the value of this variable. |
(defun org-docco-buffer-maybe () (when org-docco-doccoize-me (org-docco-buffer))) |
Finally this function is added to the |
(add-hook 'org-export-html-final-hook #'org-docco-buffer-maybe) |
That's it. To use this simply;
|
(provide 'org-docco) ;;; org-docco.el ends here |