1 ;; Copyright (C) 2009-2011 Free Software Foundation, Inc.
3 ;; Author: Chris Gray <chrismgray@gmail.com>
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software: you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;; This package generalizes the #+begin_foo and #+end_foo tokens.
25 ;; To use, put the following in your init file:
27 ;; (require 'org-special-blocks)
29 ;; The tokens #+begin_center, #+begin_verse, etc. existed previously.
30 ;; This package generalizes them (at least for the LaTeX and html
31 ;; exporters). When a #+begin_foo token is encountered by the LaTeX
32 ;; exporter, it is expanded into \begin{foo}. The text inside the
33 ;; environment is not protected, as text inside environments generally
34 ;; is. When #+begin_foo is encountered by the html exporter, a div
35 ;; with class foo is inserted into the HTML file. It is up to the
36 ;; user to add this class to his or her stylesheet if this div is to
42 (declare-function org-open-par
"org-html" ())
43 (declare-function org-close-par-maybe
"org-html" ())
45 (defvar org-special-blocks-ignore-regexp
"^\\(LaTeX\\|HTML\\)$"
46 "A regexp indicating the names of blocks that should be ignored
47 by org-special-blocks. These blocks will presumably be
48 interpreted by other mechanisms.")
50 (defvar org-export-current-backend
) ; dynamically bound in org-exp.el
51 (defun org-special-blocks-make-special-cookies ()
52 "Adds special cookies when #+begin_foo and #+end_foo tokens are
53 seen. This is run after a few special cases are taken care of."
54 (when (or (eq org-export-current-backend
'html
)
55 (eq org-export-current-backend
'latex
))
56 (goto-char (point-min))
57 (while (re-search-forward "^[ \t]*#\\+\\(begin\\|end\\)_\\(.*\\)$" nil t
)
58 (unless (org-string-match-p org-special-blocks-ignore-regexp
(match-string 2))
60 (if (equal (downcase (match-string 1)) "begin")
61 (concat "ORG-" (match-string 2) "-START")
62 (concat "ORG-" (match-string 2) "-END"))
65 (add-hook 'org-export-preprocess-after-blockquote-hook
66 'org-special-blocks-make-special-cookies
)
68 (defun org-special-blocks-convert-latex-special-cookies ()
69 "Converts the special cookies into LaTeX blocks."
70 (goto-char (point-min))
71 (while (re-search-forward "^ORG-\\([^ \t\n]*\\)[ \t]*\\(.*\\)-\\(START\\|END\\)$" nil t
)
73 (if (equal (match-string 3) "START")
74 (concat "\\begin{" (match-string 1) "}" (match-string 2))
75 (concat "\\end{" (match-string 1) "}"))
79 (add-hook 'org-export-latex-after-blockquotes-hook
80 'org-special-blocks-convert-latex-special-cookies
)
83 (defun org-special-blocks-convert-html-special-cookies ()
84 "Converts the special cookies into div blocks."
85 ;; Uses the dynamically-bound variable `line'.
86 (when (string-match "^ORG-\\(.*\\)-\\(START\\|END\\)$" line
)
87 (message "%s" (match-string 1))
88 (when (equal (match-string 2 line
) "START")
90 (insert "\n<div class=\"" (match-string 1 line
) "\">")
92 (when (equal (match-string 2 line
) "END")
96 (throw 'nextline nil
)))
98 (add-hook 'org-export-html-after-blockquotes-hook
99 'org-special-blocks-convert-html-special-cookies
)
101 (provide 'org-special-blocks
)
103 ;;; org-special-blocks.el ends here