org-special-blocks.el 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. ;;; org-special-blocks.el --- Turn blocks into LaTeX envs and HTML divs
  2. ;; Copyright (C) 2009 Chris Gray
  3. ;; Author: Chris Gray <chrismgray@gmail.com>
  4. ;; This file is part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or
  6. ;; modify it under the terms of the GNU General Public License as
  7. ;; published by the Free Software Foundation; either version 2, or (at
  8. ;; your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful, but
  10. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program ; see the file COPYING. If not, write to
  15. ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. ;; Boston, MA 02111-1307, USA.
  17. ;;; Commentary:
  18. ;;
  19. ;; This package generalizes the #+begin_foo and #+end_foo tokens.
  20. ;; To use, put the following in your init file:
  21. ;;
  22. ;; (require 'org-special-blocks)
  23. ;; The tokens #+begin_center, #+begin_verse, etc. existed previously.
  24. ;; This package generalizes them (at least for the LaTeX and html
  25. ;; exporters). When a #+begin_foo token is encountered by the LaTeX
  26. ;; exporter, it is expanded into \begin{foo}. The text inside the
  27. ;; environment is not protected, as text inside environments generally
  28. ;; is. When #+begin_foo is encountered by the html exporter, a div
  29. ;; with class foo is inserted into the HTML file. It is up to the
  30. ;; user to add this class to his or her stylesheet if this div is to
  31. ;; mean anything.
  32. (require 'org-compat)
  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. ; (org-close-par-maybe)
  70. (message "%s" (match-string 1))
  71. (if (equal (match-string 2 line) "START")
  72. (insert "<div class=\"" (match-string 1 line) "\">\n")
  73. (insert "</div>\n"))
  74. (throw 'nextline nil)))
  75. (add-hook 'org-export-html-after-blockquotes-hook
  76. 'org-special-blocks-convert-html-special-cookies)
  77. (provide 'org-special-blocks)
  78. ;;; org-special-blocks.el ends here