ox-org.el 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. ;;; ox-org.el --- Org Back-End for Org Export Engine
  2. ;; Copyright (C) 2013 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou@gmail.com>
  4. ;; Keywords: org, wp
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; This library implements an Org back-end for Org exporter.
  17. ;;
  18. ;; It introduces two interactive functions, `org-org-export-as-org'
  19. ;; and `org-org-export-to-org', which export, respectively, to
  20. ;; a temporary buffer and to a file.
  21. ;;
  22. ;; A publishing function is also provided: `org-org-publish-to-org'.
  23. ;;; Code:
  24. (require 'ox)
  25. (declare-function htmlize-buffer "htmlize" (&optional buffer))
  26. (defgroup org-export-org nil
  27. "Options for exporting Org mode files to Org."
  28. :tag "Org Export Org"
  29. :group 'org-export
  30. :version "24.4"
  31. :package-version '(Org . "8.0"))
  32. (define-obsolete-variable-alias
  33. 'org-export-htmlized-org-css-url 'org-org-htmlized-css-url "24.4")
  34. (defcustom org-org-htmlized-css-url nil
  35. "URL pointing to the CSS defining colors for htmlized Emacs buffers.
  36. Normally when creating an htmlized version of an Org buffer,
  37. htmlize will create the CSS to define the font colors. However,
  38. this does not work when converting in batch mode, and it also can
  39. look bad if different people with different fontification setup
  40. work on the same website. When this variable is non-nil,
  41. creating an htmlized version of an Org buffer using
  42. `org-org-export-as-org' will include a link to this URL if the
  43. setting of `org-html-htmlize-output-type' is 'css."
  44. :group 'org-export-org
  45. :type '(choice
  46. (const :tag "Don't include external stylesheet link" nil)
  47. (string :tag "URL or local href")))
  48. (org-export-define-backend 'org
  49. '((babel-call . org-org-identity)
  50. (bold . org-org-identity)
  51. (center-block . org-org-identity)
  52. (clock . org-org-identity)
  53. (code . org-org-identity)
  54. (comment . (lambda (&rest args) ""))
  55. (comment-block . (lambda (&rest args) ""))
  56. (diary-sexp . org-org-identity)
  57. (drawer . org-org-identity)
  58. (dynamic-block . org-org-identity)
  59. (entity . org-org-identity)
  60. (example-block . org-org-identity)
  61. (fixed-width . org-org-identity)
  62. (footnote-definition . org-org-identity)
  63. (footnote-reference . org-org-identity)
  64. (headline . org-org-headline)
  65. (horizontal-rule . org-org-identity)
  66. (inline-babel-call . org-org-identity)
  67. (inline-src-block . org-org-identity)
  68. (inlinetask . org-org-identity)
  69. (italic . org-org-identity)
  70. (item . org-org-identity)
  71. (keyword . org-org-keyword)
  72. (latex-environment . org-org-identity)
  73. (latex-fragment . org-org-identity)
  74. (line-break . org-org-identity)
  75. (link . org-org-identity)
  76. (node-property . org-org-identity)
  77. (paragraph . org-org-identity)
  78. (plain-list . org-org-identity)
  79. (planning . org-org-identity)
  80. (property-drawer . org-org-identity)
  81. (quote-block . org-org-identity)
  82. (quote-section . org-org-identity)
  83. (radio-target . org-org-identity)
  84. (section . org-org-identity)
  85. (special-block . org-org-identity)
  86. (src-block . org-org-identity)
  87. (statistics-cookie . org-org-identity)
  88. (strike-through . org-org-identity)
  89. (subscript . org-org-identity)
  90. (superscript . org-org-identity)
  91. (table . org-org-identity)
  92. (table-cell . org-org-identity)
  93. (table-row . org-org-identity)
  94. (target . org-org-identity)
  95. (timestamp . org-org-identity)
  96. (underline . org-org-identity)
  97. (verbatim . org-org-identity)
  98. (verse-block . org-org-identity))
  99. :menu-entry
  100. '(?O "Export to Org"
  101. ((?O "As Org buffer" org-org-export-as-org)
  102. (?o "As Org file" org-org-export-to-org)
  103. (?v "As Org file and open"
  104. (lambda (a s v b)
  105. (if a (org-org-export-to-org t s v b)
  106. (org-open-file (org-org-export-to-org nil s v b))))))))
  107. (defun org-org-identity (blob contents info)
  108. "Transcode BLOB element or object back into Org syntax.
  109. CONTENTS is its contents, as a string or nil. INFO is ignored."
  110. (org-export-expand blob contents t))
  111. (defun org-org-headline (headline contents info)
  112. "Transcode HEADLINE element back into Org syntax.
  113. CONTENTS is its contents, as a string or nil. INFO is ignored."
  114. (unless (plist-get info :with-todo-keywords)
  115. (org-element-put-property headline :todo-keyword nil))
  116. (unless (plist-get info :with-tags)
  117. (org-element-put-property headline :tags nil))
  118. (unless (plist-get info :with-priority)
  119. (org-element-put-property headline :priority nil))
  120. (org-element-headline-interpreter headline contents))
  121. (defun org-org-keyword (keyword contents info)
  122. "Transcode KEYWORD element back into Org syntax.
  123. CONTENTS is nil. INFO is ignored. This function ignores
  124. keywords targeted at other export back-ends."
  125. (unless (member (org-element-property :key keyword)
  126. (mapcar
  127. (lambda (block-cons)
  128. (and (eq (cdr block-cons) 'org-element-export-block-parser)
  129. (car block-cons)))
  130. org-element-block-name-alist))
  131. (org-element-keyword-interpreter keyword nil)))
  132. ;;;###autoload
  133. (defun org-org-export-as-org (&optional async subtreep visible-only ext-plist)
  134. "Export current buffer to an Org buffer.
  135. If narrowing is active in the current buffer, only export its
  136. narrowed part.
  137. If a region is active, export that region.
  138. A non-nil optional argument ASYNC means the process should happen
  139. asynchronously. The resulting buffer should be accessible
  140. through the `org-export-stack' interface.
  141. When optional argument SUBTREEP is non-nil, export the sub-tree
  142. at point, extracting information from the headline properties
  143. first.
  144. When optional argument VISIBLE-ONLY is non-nil, don't export
  145. contents of hidden elements.
  146. EXT-PLIST, when provided, is a property list with external
  147. parameters overriding Org default settings, but still inferior to
  148. file-local settings.
  149. Export is done in a buffer named \"*Org ORG Export*\", which will
  150. be displayed when `org-export-show-temporary-export-buffer' is
  151. non-nil."
  152. (interactive)
  153. (org-export-to-buffer 'org "*Org ORG Export*"
  154. async subtreep visible-only nil ext-plist (lambda () (org-mode))))
  155. ;;;###autoload
  156. (defun org-org-export-to-org (&optional async subtreep visible-only ext-plist)
  157. "Export current buffer to an org file.
  158. If narrowing is active in the current buffer, only export its
  159. narrowed part.
  160. If a region is active, export that region.
  161. A non-nil optional argument ASYNC means the process should happen
  162. asynchronously. The resulting file should be accessible through
  163. the `org-export-stack' interface.
  164. When optional argument SUBTREEP is non-nil, export the sub-tree
  165. at point, extracting information from the headline properties
  166. first.
  167. When optional argument VISIBLE-ONLY is non-nil, don't export
  168. contents of hidden elements.
  169. EXT-PLIST, when provided, is a property list with external
  170. parameters overriding Org default settings, but still inferior to
  171. file-local settings.
  172. Return output file name."
  173. (interactive)
  174. (let ((outfile (org-export-output-file-name ".org" subtreep)))
  175. (org-export-to-file 'org outfile
  176. async subtreep visible-only nil ext-plist)))
  177. ;;;###autoload
  178. (defun org-org-publish-to-org (plist filename pub-dir)
  179. "Publish an org file to org.
  180. FILENAME is the filename of the Org file to be published. PLIST
  181. is the property list for the given project. PUB-DIR is the
  182. publishing directory.
  183. Return output file name."
  184. (org-publish-org-to 'org filename ".org" plist pub-dir)
  185. (when (plist-get plist :htmlized-source)
  186. (require 'htmlize)
  187. (require 'ox-html)
  188. (let* ((org-inhibit-startup t)
  189. (htmlize-output-type 'css)
  190. (html-ext (concat "." (or (plist-get plist :html-extension)
  191. org-html-extension "html")))
  192. (visitingp (find-buffer-visiting filename))
  193. (work-buffer (or visitingp (find-file filename)))
  194. newbuf)
  195. (font-lock-fontify-buffer)
  196. (show-all)
  197. (org-show-block-all)
  198. (setq newbuf (htmlize-buffer))
  199. (with-current-buffer newbuf
  200. (when org-org-htmlized-css-url
  201. (goto-char (point-min))
  202. (and (re-search-forward
  203. "<style type=\"text/css\">[^\000]*?\n[ \t]*</style>.*" nil t)
  204. (replace-match
  205. (format
  206. "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">"
  207. org-org-htmlized-css-url) t t)))
  208. (write-file (concat pub-dir (file-name-nondirectory filename) html-ext)))
  209. (kill-buffer newbuf)
  210. (unless visitingp (kill-buffer work-buffer)))
  211. (set-buffer-modified-p nil)))
  212. (provide 'ox-org)
  213. ;; Local variables:
  214. ;; generated-autoload-file: "org-loaddefs.el"
  215. ;; End:
  216. ;;; ox-org.el ends here