ox-extra.el 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 (backend)
  37. (when (org-export-derived-backend-p backend 'latex)
  38. (let ((positions
  39. (org-element-map (org-element-parse-buffer 'greater-element nil) 'export-block
  40. (lambda (block)
  41. (when (and (string= (org-element-property :type block) "LATEX")
  42. (string= (org-export-read-attribute
  43. :header block :header)
  44. "yes"))
  45. (list (org-element-property :begin block)
  46. (org-element-property :end block)
  47. (org-element-property :post-affiliated block)))))))
  48. (mapc (lambda (pos)
  49. (goto-char (nth 2 pos))
  50. (destructuring-bind
  51. (beg end &rest ignore)
  52. (org-edit-src-find-region-and-lang)
  53. (let ((contents-lines (split-string
  54. (buffer-substring-no-properties beg end)
  55. "\n")))
  56. (delete-region (nth 0 pos) (nth 1 pos))
  57. (dolist (line contents-lines)
  58. (insert (concat "#+latex_header: "
  59. (replace-regexp-in-string "\\` *" "" line)
  60. "\n"))))))
  61. ;; go in reverse, to avoid wrecking the numeric positions
  62. ;; earlier in the file
  63. (reverse positions)))))
  64. (defconst ox-extras
  65. '((latex-header-blocks org-latex-header-blocks-filter org-export-before-parsing-hook))
  66. "A list of org export extras that can be enabled.
  67. Should be a list of items of the form (NAME FN HOOK). NAME is a
  68. symbol, which can be passed to `ox-extras-activate'. FN is a
  69. function which will be added to HOOK.")
  70. (defun ox-extras-activate (extras)
  71. "Activate certain org export extras.
  72. EXTRAS should be a list of extras (defined in `ox-extras') which
  73. should be activated."
  74. (dolist (extra extras)
  75. (let* ((lst (assq extra ox-extras))
  76. (fn (nth 1 lst))
  77. (hook (nth 2 lst)))
  78. (when (and fn hook)
  79. (add-hook hook fn)))))
  80. (defun ox-extras-deactivate (extras)
  81. "Deactivate certain org export extras.
  82. This function is the opposite of `ox-extras-activate'. EXTRAS
  83. should be a list of extras (defined in `ox-extras') which should
  84. be activated."
  85. (dolist (extra extras)
  86. (let* ((lst (assq extra ox-extras))
  87. (fn (nth 1 lst))
  88. (hook (nth 2 lst)))
  89. (when (and fn hook)
  90. (remove-hook hook fn)))))
  91. (provide 'ox-extra)
  92. ;;; ox-extra.el ends here