ox-extra.el 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. ;;; ox-extra.el --- Convenience functions for org export
  2. ;; Copyright (C) 2014 Aaron Ecay
  3. ;; Author: Aaron Ecay <aaronecay@gmail.com>
  4. ;; This program 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. ;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
  14. ;;; Commentary:
  15. ;; This file contains some convenience functions for org export, which
  16. ;; are not part of org's core. Call `ox-extras-activate' passing a
  17. ;; list of symbols naming extras, which will be installed globally in
  18. ;; your org session.
  19. ;; Currently available extras:
  20. ;; - `latex-header-blocks' -- allow the use of latex blocks, the
  21. ;; contents of which which will be interpreted as #+latex_header lines
  22. ;; for export. These blocks should be tagged with #+header: :header
  23. ;; yes. For example:
  24. ;; #+header: :header yes
  25. ;; #+begin_latex
  26. ;; ...
  27. ;; #+end_latex
  28. ;; TODO:
  29. ;; - add a function to org-mode-hook that looks for a ox-extras local
  30. ;; variable and activates the specified extras buffer-locally
  31. ;; - allow specification of desired extras to be activated via
  32. ;; customize
  33. ;;; Code:
  34. (require 'ox)
  35. (eval-when-compile (require 'cl))
  36. (defun org-latex-header-blocks-filter ()
  37. (let ((positions
  38. (org-element-map (org-element-parse-buffer 'greater-element nil) 'export-block
  39. (lambda (block)
  40. (when (and (string= (org-element-property :type block) "LATEX")
  41. (string= (org-export-read-attribute
  42. :header block :header)
  43. "yes"))
  44. (list (org-element-property :begin block)
  45. (org-element-property :end block)
  46. (org-element-property :post-affiliated block)))))))
  47. (mapc (lambda (pos)
  48. (goto-char (nth 2 pos))
  49. (destructuring-bind
  50. (beg end &rest ignore)
  51. (org-edit-src-find-region-and-lang)
  52. (let ((contents-lines (split-string
  53. (buffer-substring-no-properties beg end)
  54. "\n")))
  55. (delete-region (nth 0 pos) (nth 1 pos))
  56. (dolist (line contents-lines)
  57. (insert (concat "#+latex_header: "
  58. (replace-regexp-in-string "\\` *" "" line)
  59. "\n"))))))
  60. ;; go in reverse, to avoid wrecking the numeric positions
  61. ;; earlier in the file
  62. (reverse positions))))
  63. (defconst ox-extras
  64. '((latex-header-blocks org-latex-latex-header-blocks-filter org-export-before-parsing-hook))
  65. "A list of org export extras that can be enabled.
  66. Should be a list of items of the form (NAME FN HOOK). NAME is a
  67. symbol, which can be passed to `ox-extras-activate'. FN is a
  68. function which will be added to HOOK.")
  69. (defun ox-extras-activate (extras)
  70. "Activate certain org export extras.
  71. EXTRAS should be a list of extras (defined in `ox-extras') which
  72. should be activated."
  73. (dolist (extra extras)
  74. (let* ((lst (assq extra ox-extras))
  75. (fn (nth 1 lst))
  76. (hook (nth 2 lst)))
  77. (when (and fn hook)
  78. (add-hook hook fn)))))
  79. (defun ox-extras-deactivate (extras)
  80. "Deactivate certain org export extras.
  81. This function is the opposite of `ox-extras-activate'. EXTRAS
  82. should be a list of extras (defined in `ox-extras') which should
  83. be activated."
  84. (dolist (extra extras)
  85. (let* ((lst (assq extra ox-extras))
  86. (fn (nth 1 lst))
  87. (hook (nth 2 lst)))
  88. (when (and fn hook)
  89. (remove-hook hook fn)))))
  90. (provide 'ox-extra)
  91. ;;; ox-extra.el ends here