org-export.el 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. ;;; org-export.el --- Export engine for Org
  2. ;;
  3. ;; Copyright 2008 2010 Bastien Guerry
  4. ;;
  5. ;; Emacs Lisp Archive Entry
  6. ;; Filename: org-export.el
  7. ;; Version: 0.3
  8. ;; Author: Bastien <bzg AT altern DOT org>
  9. ;; Maintainer: Bastien <bzg AT altern DOT org>
  10. ;; Keywords:
  11. ;; Description:
  12. ;; URL: [Not distributed yet]
  13. ;;
  14. ;; This program is free software; you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation; either version 3, or (at your option)
  17. ;; any later version.
  18. ;;
  19. ;; This program is distributed in the hope that it will be useful,
  20. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. ;; GNU General Public License for more details.
  23. ;;
  24. ;; You should have received a copy of the GNU General Public License
  25. ;; along with this program; if not, write to the Free Software
  26. ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  27. ;;
  28. ;;; Commentary:
  29. ;;
  30. ;; org-export.el implements a new experimental export engine for Org.
  31. ;;
  32. ;; Put this file into your load-path and the following into your ~/.emacs:
  33. ;; (require 'org-export)
  34. ;;
  35. ;;; Todo:
  36. ;;
  37. ;;; Code:
  38. (eval-when-compile
  39. (require 'cl))
  40. ;;; Preparation functions:
  41. ;; Currently needed for `org-export-preprocess-string'
  42. (require 'org-exp)
  43. (defvar org-export-structure nil)
  44. (defvar org-export-content nil)
  45. (defvar org-export-properties nil)
  46. (defun org-export-set-backend (suffix)
  47. "Set the backend functions names from SUFFIX."
  48. (setq org-export-structure
  49. `((header ,(intern (concat "org-" suffix "-export-header")))
  50. (first-lines ,(intern (concat "org-" suffix "-export-first-lines")))
  51. (section-beginning ,(intern (concat "org-" suffix "-export-section-beginning")))
  52. (heading ,(intern (concat "org-" suffix "-export-heading")))
  53. (section-end ,(intern (concat "org-" suffix "-export-section-end")))
  54. (footer ,(intern (concat "org-" suffix "-export-footer")))))
  55. (setq org-export-content
  56. `((fonts ,(intern (concat "org-" suffix "-export-fonts")))
  57. (links ,(intern (concat "org-" suffix "-export-links")))
  58. (lists ,(intern (concat "org-" suffix "-export-lists")))
  59. (envs ,(intern (concat "org-" suffix "-export-quote-verse-center")))
  60. (tables ,(intern (concat "org-" suffix "-export-tables"))))))
  61. ;;; Parsing functions:
  62. (defun org-export-parse (&optional level)
  63. "Recursively parse the current buffer.
  64. If LEVEL is set, do the parsing at that level of sectioning.
  65. Return a nested list containing the structure of the parsed
  66. buffer and information about each section, including its
  67. content."
  68. (let (output eos)
  69. (save-excursion
  70. (goto-char (point-min))
  71. (while (re-search-forward org-complex-heading-regexp nil t)
  72. (let ((heading (match-string 4))
  73. (properties (org-entry-properties)))
  74. (save-restriction
  75. (narrow-to-region (if (looking-at "\n") (1+ (point)) (point))
  76. (save-excursion
  77. (setq eos (org-end-of-subtree t t))))
  78. (setq output
  79. (append output
  80. (list
  81. (list :level (or level 1)
  82. :heading heading
  83. :properties properties
  84. :content (org-export-get-entry-content)
  85. :subtree (org-export-parse
  86. (if level (1+ level) 2)))))))
  87. (goto-char (1- eos)))))
  88. output))
  89. (defun org-export-get-entry-content ()
  90. "Extract the content of an entry.
  91. The content of a entry is the part before its first subtree or
  92. the end of the entry."
  93. (save-excursion
  94. (goto-char (point-min))
  95. ;; FIXME The following shouldn't be necessary since we are cleaning
  96. ;; up the buffer ith org-export-preprocess-string
  97. (while (or (looking-at org-property-drawer-re)
  98. (looking-at org-clock-drawer-re)
  99. (looking-at org-keyword-time-regexp))
  100. (move-beginning-of-line 1))
  101. (buffer-substring
  102. (point)
  103. (if (re-search-forward org-complex-heading-regexp nil t)
  104. (match-beginning 0) (point-max)))))
  105. ;;; Rendering functions:
  106. (defun org-export-render (&optional filter)
  107. "Render the current Org buffer and export it.
  108. First parse the buffer and return it as a nested list. If FILTER
  109. is set, use it to filter this list (see `org-export-filter') then
  110. export the (filtered) list with `org-export-render-structure'."
  111. (setq org-export-properties
  112. (org-combine-plists (org-default-export-plist)
  113. (org-infile-export-plist)))
  114. (let* (first-lines
  115. (bstring (buffer-string))
  116. (parsed-buffer
  117. (with-temp-buffer
  118. (org-mode)
  119. (insert (apply 'org-export-preprocess-string
  120. bstring org-export-properties))
  121. (goto-char (point-min))
  122. (setq first-lines (org-export-get-entry-content))
  123. (org-export-parse))))
  124. (switch-to-buffer (get-buffer-create "*Org export*"))
  125. (erase-buffer)
  126. (funcall (cadr (assoc 'header org-export-structure)))
  127. (funcall (cadr (assoc 'first-lines org-export-structure)) first-lines)
  128. (org-export-render-structure parsed-buffer filter)
  129. (funcall (cadr (assoc 'footer org-export-structure)))))
  130. (defun org-export-render-structure (parsed-buffer &optional filter)
  131. "Render PARSED-BUFFER.
  132. An optional argument FILTER specifies a filter to pass to the
  133. rendering engine."
  134. (mapc (lambda(s)
  135. (funcall (cadr (assoc 'section-beginning org-export-structure)) s)
  136. (funcall (cadr (assoc 'heading org-export-structure)) s)
  137. (insert (org-export-render-content s) "\n\n")
  138. (org-export-render-structure (plist-get s :subtree) filter)
  139. (funcall (cadr (assoc 'section-end org-export-structure)) s))
  140. (org-export-filter parsed-buffer filter)))
  141. (defun org-export-render-content (section)
  142. "Render SECTION.
  143. SECTION is either a string or a property list containing
  144. informations (including content) for a section."
  145. (with-temp-buffer
  146. (insert (if (listp section) (plist-get section :content) section))
  147. (mapc (lambda(e)
  148. (goto-char (point-min))
  149. (funcall (cadr (assoc e org-export-content))))
  150. '(fonts tables lists envs links))
  151. (buffer-string)))
  152. (defun org-export-filter (parsed-buffer filter)
  153. "Filter out PARSED-BUFFER with FILTER.
  154. PARSED-BUFFER is a nested list of sections and subsections, as
  155. produced by `org-export-parse'. FILTER is an alist of rules to
  156. apply to PARSED-BUFFER. For the syntax of a filter, please check
  157. the docstring of `org-export-latex-filter'."
  158. ;; FIXME where is org-export-latex-filter
  159. (delete
  160. nil
  161. (mapcar
  162. (lambda(s)
  163. (if (delete
  164. nil
  165. (mapcar
  166. (lambda(f)
  167. (let ((cnd (car f)) (re (cadr f)) prop-cnd)
  168. (or (and (eq cnd 'heading)
  169. (string-match re (plist-get s :heading)))
  170. (and (eq cnd 'content)
  171. (string-match re (plist-get s :content)))
  172. (and (setq prop-cnd
  173. (assoc cnd (plist-get s :properties)))
  174. (string-match re (cadr prop-cnd))))))
  175. filter))
  176. nil ;; return nil if the section is filtered out
  177. (progn (plist-put s :subtree
  178. (org-export-filter (plist-get s :subtree) filter))
  179. s))) ;; return the section if it isn't filtered out
  180. parsed-buffer)))
  181. (provide 'org-export)
  182. ;;; User Options, Variables
  183. ;;; org-export.el ends here