ox-org.el 11 KB

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