org-special-blocks.el 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. ;;; org-special-blocks.el --- handle Org special blocks
  2. ;; Copyright (C) 2009-2013 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-html)
  31. (require 'org-compat)
  32. (declare-function org-open-par "org-html" ())
  33. (declare-function org-close-par-maybe "org-html" ())
  34. (defvar org-special-blocks-ignore-regexp "^\\(LaTeX\\|HTML\\)$"
  35. "A regexp indicating the names of blocks that should be ignored
  36. by org-special-blocks. These blocks will presumably be
  37. interpreted by other mechanisms.")
  38. (defvar org-export-current-backend) ; dynamically bound in org-exp.el
  39. (defun org-special-blocks-make-special-cookies ()
  40. "Adds special cookies when #+begin_foo and #+end_foo tokens are
  41. seen. This is run after a few special cases are taken care of."
  42. (when (or (eq org-export-current-backend 'html)
  43. (eq org-export-current-backend 'latex))
  44. (goto-char (point-min))
  45. (while (re-search-forward "^[ \t]*#\\+\\(begin\\|end\\)_\\(.*\\)$" nil t)
  46. (unless (org-string-match-p org-special-blocks-ignore-regexp (match-string 2))
  47. (replace-match
  48. (if (equal (downcase (match-string 1)) "begin")
  49. (concat "ORG-" (match-string 2) "-START")
  50. (concat "ORG-" (match-string 2) "-END"))
  51. t t)))))
  52. (add-hook 'org-export-preprocess-after-blockquote-hook
  53. 'org-special-blocks-make-special-cookies)
  54. (defun org-special-blocks-convert-latex-special-cookies ()
  55. "Converts the special cookies into LaTeX blocks."
  56. (goto-char (point-min))
  57. (while (re-search-forward "^ORG-\\([^ \t\n]*\\)[ \t]*\\(.*\\)-\\(START\\|END\\)$" nil t)
  58. (replace-match
  59. (if (equal (match-string 3) "START")
  60. (concat "\\begin{" (match-string 1) "}" (match-string 2))
  61. (concat "\\end{" (match-string 1) "}"))
  62. t t)))
  63. (add-hook 'org-export-latex-after-blockquotes-hook
  64. 'org-special-blocks-convert-latex-special-cookies)
  65. (defvar org-line)
  66. (defun org-special-blocks-convert-html-special-cookies ()
  67. "Converts the special cookies into div blocks."
  68. ;; Uses the dynamically-bound variable `org-line'.
  69. (when (and org-line (string-match "^ORG-\\(.*\\)-\\(START\\|END\\)$" org-line))
  70. (message "%s" (match-string 1))
  71. (when (equal (match-string 2 org-line) "START")
  72. (org-close-par-maybe)
  73. (insert "\n<div class=\"" (match-string 1 org-line) "\">")
  74. (org-open-par))
  75. (when (equal (match-string 2 org-line) "END")
  76. (org-close-par-maybe)
  77. (insert "\n</div>")
  78. (org-open-par))
  79. (throw 'nextline nil)))
  80. (add-hook 'org-export-html-after-blockquotes-hook
  81. 'org-special-blocks-convert-html-special-cookies)
  82. (provide 'org-special-blocks)
  83. ;;; org-special-blocks.el ends here