org-special-blocks.el 3.7 KB

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