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