org-special-blocks.el 3.9 KB

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