ox-extra.el 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. ;;
  20. ;; For example, you could include the following in your .emacs file:
  21. ;;
  22. ;; (require 'ox-extra)
  23. ;; (ox-extras-activate '(latex-header-blocks ignore-headlines))
  24. ;;
  25. ;; Currently available extras:
  26. ;; - `latex-header-blocks' -- allow the use of latex blocks, the
  27. ;; contents of which which will be interpreted as #+latex_header lines
  28. ;; for export. These blocks should be tagged with #+header: :header
  29. ;; yes. For example:
  30. ;; #+header: :header yes
  31. ;; #+begin_export latex
  32. ;; ...
  33. ;; #+end_export
  34. ;; - `ignore-headlines' -- allow a headline (but not its children) to
  35. ;; be ignored. Any headline tagged with the 'ignore' tag will be
  36. ;; ignored (i.e. will not be included in the export), but any child
  37. ;; headlines will not be ignored (unless explicitly tagged to be
  38. ;; ignored), and will instead have their levels promoted by one.
  39. ;; TODO:
  40. ;; - add a function to org-mode-hook that looks for a ox-extras local
  41. ;; variable and activates the specified extras buffer-locally
  42. ;; - allow specification of desired extras to be activated via
  43. ;; customize
  44. ;;; Code:
  45. (require 'ox)
  46. (eval-when-compile (require 'cl))
  47. (defun org-latex-header-blocks-filter (backend)
  48. (when (org-export-derived-backend-p backend 'latex)
  49. (let ((positions
  50. (org-element-map (org-element-parse-buffer 'greater-element nil) 'export-block
  51. (lambda (block)
  52. (when (and (string= (org-element-property :type block) "LATEX")
  53. (string= (org-export-read-attribute
  54. :header block :header)
  55. "yes"))
  56. (list (org-element-property :begin block)
  57. (org-element-property :end block)
  58. (org-element-property :post-affiliated block)))))))
  59. (mapc (lambda (pos)
  60. (goto-char (nth 2 pos))
  61. (destructuring-bind
  62. (beg end &rest ignore)
  63. (org-edit-src-find-region-and-lang)
  64. (let ((contents-lines (split-string
  65. (buffer-substring-no-properties beg end)
  66. "\n")))
  67. (delete-region (nth 0 pos) (nth 1 pos))
  68. (dolist (line contents-lines)
  69. (insert (concat "#+latex_header: "
  70. (replace-regexp-in-string "\\` *" "" line)
  71. "\n"))))))
  72. ;; go in reverse, to avoid wrecking the numeric positions
  73. ;; earlier in the file
  74. (reverse positions)))))
  75. ;; During export headlines which have the "ignore" tag are removed
  76. ;; from the parse tree. Their contents are retained (leading to a
  77. ;; possibly invalid parse tree, which nevertheless appears to function
  78. ;; correctly with most export backends) all children headlines are
  79. ;; retained and are promoted to the level of the ignored parent
  80. ;; headline.
  81. ;;
  82. ;; This makes it possible to add structure to the original Org-mode
  83. ;; document which does not effect the exported version, such as in the
  84. ;; following examples.
  85. ;;
  86. ;; Wrapping an abstract in a headline
  87. ;;
  88. ;; * Abstract :ignore:
  89. ;; #+LaTeX: \begin{abstract}
  90. ;; #+HTML: <div id="abstract">
  91. ;;
  92. ;; ...
  93. ;;
  94. ;; #+HTML: </div>
  95. ;; #+LaTeX: \end{abstract}
  96. ;;
  97. ;; Placing References under a headline (using ox-bibtex in contrib)
  98. ;;
  99. ;; * References :ignore:
  100. ;; #+BIBLIOGRAPHY: dissertation plain
  101. ;;
  102. ;; Inserting an appendix for LaTeX using the appendix package.
  103. ;;
  104. ;; * Appendix :ignore:
  105. ;; #+LaTeX: \begin{appendices}
  106. ;; ** Reproduction
  107. ;; ...
  108. ;; ** Definitions
  109. ;; #+LaTeX: \end{appendices}
  110. ;;
  111. (defun org-export-ignore-headlines (data backend info)
  112. "Remove headlines tagged \"ignore\" retaining contents and promoting children.
  113. Each headline tagged \"ignore\" will be removed retaining its
  114. contents and promoting any children headlines to the level of the
  115. parent."
  116. (org-element-map data 'headline
  117. (lambda (object)
  118. (when (member "ignore" (org-element-property :tags object))
  119. (let ((level-top (org-element-property :level object))
  120. level-diff)
  121. (mapc (lambda (el)
  122. ;; recursively promote all nested headlines
  123. (org-element-map el 'headline
  124. (lambda (el)
  125. (when (equal 'headline (org-element-type el))
  126. (unless level-diff
  127. (setq level-diff (- (org-element-property :level el)
  128. level-top)))
  129. (org-element-put-property el
  130. :level (- (org-element-property :level el)
  131. level-diff)))))
  132. ;; insert back into parse tree
  133. (org-element-insert-before el object))
  134. (org-element-contents object)))
  135. (org-element-extract-element object)))
  136. info nil)
  137. data)
  138. (defconst ox-extras
  139. '((latex-header-blocks org-latex-header-blocks-filter org-export-before-parsing-hook)
  140. (ignore-headlines org-export-ignore-headlines org-export-filter-parse-tree-functions))
  141. "A list of org export extras that can be enabled.
  142. Should be a list of items of the form (NAME FN HOOK). NAME is a
  143. symbol, which can be passed to `ox-extras-activate'. FN is a
  144. function which will be added to HOOK.")
  145. (defun ox-extras-activate (extras)
  146. "Activate certain org export extras.
  147. EXTRAS should be a list of extras (defined in `ox-extras') which
  148. should be activated."
  149. (dolist (extra extras)
  150. (let* ((lst (assq extra ox-extras))
  151. (fn (nth 1 lst))
  152. (hook (nth 2 lst)))
  153. (when (and fn hook)
  154. (add-hook hook fn)))))
  155. (defun ox-extras-deactivate (extras)
  156. "Deactivate certain org export extras.
  157. This function is the opposite of `ox-extras-activate'. EXTRAS
  158. should be a list of extras (defined in `ox-extras') which should
  159. be activated."
  160. (dolist (extra extras)
  161. (let* ((lst (assq extra ox-extras))
  162. (fn (nth 1 lst))
  163. (hook (nth 2 lst)))
  164. (when (and fn hook)
  165. (remove-hook hook fn)))))
  166. (provide 'ox-extra)
  167. ;;; ox-extra.el ends here