ox-org.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. ;;; ox-org.el --- Org Back-End for Org Export Engine -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2013-2017 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. ;;; Code:
  18. (require 'ox)
  19. (declare-function htmlize-buffer "ext:htmlize" (&optional buffer))
  20. (defvar htmlize-output-type)
  21. (defgroup org-export-org nil
  22. "Options for exporting Org mode files to Org."
  23. :tag "Org Export Org"
  24. :group 'org-export
  25. :version "24.4"
  26. :package-version '(Org . "8.0"))
  27. (defcustom org-org-htmlized-css-url nil
  28. "URL pointing to the CSS defining colors for htmlized Emacs buffers.
  29. Normally when creating an htmlized version of an Org buffer,
  30. htmlize will create the CSS to define the font colors. However,
  31. this does not work when converting in batch mode, and it also can
  32. look bad if different people with different fontification setup
  33. work on the same website. When this variable is non-nil,
  34. creating an htmlized version of an Org buffer using
  35. `org-org-export-as-org' will include a link to this URL if the
  36. setting of `org-html-htmlize-output-type' is `css'."
  37. :group 'org-export-org
  38. :type '(choice
  39. (const :tag "Don't include external stylesheet link" nil)
  40. (string :tag "URL or local href")))
  41. (org-export-define-backend 'org
  42. '((babel-call . org-org-identity)
  43. (bold . org-org-identity)
  44. (center-block . org-org-identity)
  45. (clock . org-org-identity)
  46. (code . org-org-identity)
  47. (diary-sexp . org-org-identity)
  48. (drawer . org-org-identity)
  49. (dynamic-block . org-org-identity)
  50. (entity . org-org-identity)
  51. (example-block . org-org-identity)
  52. (export-block . org-org-export-block)
  53. (fixed-width . org-org-identity)
  54. (footnote-definition . ignore)
  55. (footnote-reference . org-org-identity)
  56. (headline . org-org-headline)
  57. (horizontal-rule . org-org-identity)
  58. (inline-babel-call . org-org-identity)
  59. (inline-src-block . org-org-identity)
  60. (inlinetask . org-org-identity)
  61. (italic . org-org-identity)
  62. (item . org-org-identity)
  63. (keyword . org-org-keyword)
  64. (latex-environment . org-org-identity)
  65. (latex-fragment . org-org-identity)
  66. (line-break . org-org-identity)
  67. (link . org-org-link)
  68. (node-property . org-org-identity)
  69. (template . org-org-template)
  70. (paragraph . org-org-identity)
  71. (plain-list . org-org-identity)
  72. (planning . org-org-identity)
  73. (property-drawer . org-org-identity)
  74. (quote-block . org-org-identity)
  75. (radio-target . org-org-identity)
  76. (section . org-org-section)
  77. (special-block . org-org-identity)
  78. (src-block . org-org-identity)
  79. (statistics-cookie . org-org-identity)
  80. (strike-through . org-org-identity)
  81. (subscript . org-org-identity)
  82. (superscript . org-org-identity)
  83. (table . org-org-identity)
  84. (table-cell . org-org-identity)
  85. (table-row . org-org-identity)
  86. (target . org-org-identity)
  87. (timestamp . org-org-identity)
  88. (underline . org-org-identity)
  89. (verbatim . org-org-identity)
  90. (verse-block . org-org-identity))
  91. :menu-entry
  92. '(?O "Export to Org"
  93. ((?O "As Org buffer" org-org-export-as-org)
  94. (?o "As Org file" org-org-export-to-org)
  95. (?v "As Org file and open"
  96. (lambda (a s v b)
  97. (if a (org-org-export-to-org t s v b)
  98. (org-open-file (org-org-export-to-org nil s v b))))))))
  99. (defun org-org-export-block (export-block _contents _info)
  100. "Transcode a EXPORT-BLOCK element from Org to LaTeX.
  101. CONTENTS and INFO are ignored."
  102. (and (equal (org-element-property :type export-block) "ORG")
  103. (org-element-property :value export-block)))
  104. (defun org-org-identity (blob contents _info)
  105. "Transcode BLOB element or object back into Org syntax.
  106. CONTENTS is its contents, as a string or nil. INFO is ignored."
  107. (let ((case-fold-search t))
  108. (replace-regexp-in-string
  109. "^[ \t]*#\\+ATTR_[-_A-Za-z0-9]+:\\(?: .*\\)?\n" ""
  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 (org-element-property :footnote-section-p headline)
  115. (unless (plist-get info :with-todo-keywords)
  116. (org-element-put-property headline :todo-keyword nil))
  117. (unless (plist-get info :with-tags)
  118. (org-element-put-property headline :tags nil))
  119. (unless (plist-get info :with-priority)
  120. (org-element-put-property headline :priority nil))
  121. (org-element-put-property headline :level
  122. (org-export-get-relative-level headline info))
  123. (org-element-headline-interpreter headline contents)))
  124. (defun org-org-keyword (keyword _contents _info)
  125. "Transcode KEYWORD element back into Org syntax.
  126. CONTENTS is nil. INFO is ignored."
  127. (let ((key (org-element-property :key keyword)))
  128. (unless (member key
  129. '("AUTHOR" "CREATOR" "DATE" "EMAIL" "OPTIONS" "TITLE"))
  130. (org-element-keyword-interpreter keyword nil))))
  131. (defun org-org-link (link contents _info)
  132. "Transcode LINK object back into Org syntax.
  133. CONTENTS is the description of the link, as a string, or nil.
  134. INFO is a plist containing current export state."
  135. (or (org-export-custom-protocol-maybe link contents 'org)
  136. (org-element-link-interpreter link contents)))
  137. (defun org-org-template (contents info)
  138. "Return Org document template with document keywords.
  139. CONTENTS is the transcoded contents string. INFO is a plist used
  140. as a communication channel."
  141. (concat
  142. (and (plist-get info :time-stamp-file)
  143. (format-time-string "# Created %Y-%m-%d %a %H:%M\n"))
  144. (org-element-normalize-string
  145. (mapconcat #'identity
  146. (org-element-map (plist-get info :parse-tree) 'keyword
  147. (lambda (k)
  148. (and (string-equal (org-element-property :key k) "OPTIONS")
  149. (concat "#+OPTIONS: "
  150. (org-element-property :value k)))))
  151. "\n"))
  152. (and (plist-get info :with-title)
  153. (format "#+TITLE: %s\n" (org-export-data (plist-get info :title) info)))
  154. (and (plist-get info :with-date)
  155. (let ((date (org-export-data (org-export-get-date info) info)))
  156. (and (org-string-nw-p date)
  157. (format "#+DATE: %s\n" date))))
  158. (and (plist-get info :with-author)
  159. (let ((author (org-export-data (plist-get info :author) info)))
  160. (and (org-string-nw-p author)
  161. (format "#+AUTHOR: %s\n" author))))
  162. (and (plist-get info :with-email)
  163. (let ((email (org-export-data (plist-get info :email) info)))
  164. (and (org-string-nw-p email)
  165. (format "#+EMAIL: %s\n" email))))
  166. (and (plist-get info :with-creator)
  167. (org-string-nw-p (plist-get info :creator))
  168. (format "#+CREATOR: %s\n" (plist-get info :creator)))
  169. contents))
  170. (defun org-org-section (section contents info)
  171. "Transcode SECTION element back into Org syntax.
  172. CONTENTS is the contents of the section. INFO is a plist used as
  173. a communication channel."
  174. (concat
  175. (org-element-normalize-string contents)
  176. ;; Insert footnote definitions appearing for the first time in this
  177. ;; section. Indeed, some of them may not be available to narrowing
  178. ;; so we make sure all of them are included in the result.
  179. (let ((footnotes-alist
  180. (org-element-map section 'footnote-reference
  181. (lambda (fn)
  182. (and (eq (org-element-property :type fn) 'standard)
  183. (org-export-footnote-first-reference-p fn info)
  184. (cons (org-element-property :label fn)
  185. (org-export-get-footnote-definition fn info))))
  186. info)))
  187. (and footnotes-alist
  188. (concat "\n"
  189. (mapconcat
  190. (lambda (d)
  191. (org-element-normalize-string
  192. (concat (format "[fn:%s] "(car d))
  193. (org-export-data (cdr d) info))))
  194. footnotes-alist "\n"))))
  195. (make-string (or (org-element-property :post-blank section) 0) ?\n)))
  196. ;;;###autoload
  197. (defun org-org-export-as-org
  198. (&optional async subtreep visible-only body-only ext-plist)
  199. "Export current buffer to an Org buffer.
  200. If narrowing is active in the current buffer, only export its
  201. narrowed part.
  202. If a region is active, export that region.
  203. A non-nil optional argument ASYNC means the process should happen
  204. asynchronously. The resulting buffer should be accessible
  205. through the `org-export-stack' interface.
  206. When optional argument SUBTREEP is non-nil, export the sub-tree
  207. at point, extracting information from the headline properties
  208. first.
  209. When optional argument VISIBLE-ONLY is non-nil, don't export
  210. contents of hidden elements.
  211. When optional argument BODY-ONLY is non-nil, strip document
  212. keywords from output.
  213. EXT-PLIST, when provided, is a property list with external
  214. parameters overriding Org default settings, but still inferior to
  215. file-local settings.
  216. Export is done in a buffer named \"*Org ORG Export*\", which will
  217. be displayed when `org-export-show-temporary-export-buffer' is
  218. non-nil."
  219. (interactive)
  220. (org-export-to-buffer 'org "*Org ORG Export*"
  221. async subtreep visible-only body-only ext-plist (lambda () (org-mode))))
  222. ;;;###autoload
  223. (defun org-org-export-to-org
  224. (&optional async subtreep visible-only body-only ext-plist)
  225. "Export current buffer to an org file.
  226. If narrowing is active in the current buffer, only export its
  227. narrowed part.
  228. If a region is active, export that region.
  229. A non-nil optional argument ASYNC means the process should happen
  230. asynchronously. The resulting file should be accessible through
  231. the `org-export-stack' interface.
  232. When optional argument SUBTREEP is non-nil, export the sub-tree
  233. at point, extracting information from the headline properties
  234. first.
  235. When optional argument VISIBLE-ONLY is non-nil, don't export
  236. contents of hidden elements.
  237. When optional argument BODY-ONLY is non-nil, strip document
  238. keywords from output.
  239. EXT-PLIST, when provided, is a property list with external
  240. parameters overriding Org default settings, but still inferior to
  241. file-local settings.
  242. Return output file name."
  243. (interactive)
  244. (let ((outfile (org-export-output-file-name ".org" subtreep)))
  245. (org-export-to-file 'org outfile
  246. async subtreep visible-only body-only ext-plist)))
  247. ;;;###autoload
  248. (defun org-org-publish-to-org (plist filename pub-dir)
  249. "Publish an org file to org.
  250. FILENAME is the filename of the Org file to be published. PLIST
  251. is the property list for the given project. PUB-DIR is the
  252. publishing directory.
  253. Return output file name."
  254. (org-publish-org-to 'org filename ".org" plist pub-dir)
  255. (when (plist-get plist :htmlized-source)
  256. (require 'htmlize)
  257. (require 'ox-html)
  258. (let* ((org-inhibit-startup t)
  259. (htmlize-output-type 'css)
  260. (html-ext (concat "." (or (plist-get plist :html-extension)
  261. org-html-extension "html")))
  262. (visitingp (find-buffer-visiting filename))
  263. (work-buffer (or visitingp (find-file-noselect filename)))
  264. newbuf)
  265. (with-current-buffer work-buffer
  266. (org-font-lock-ensure)
  267. (outline-show-all)
  268. (org-show-block-all)
  269. (setq newbuf (htmlize-buffer)))
  270. (with-current-buffer newbuf
  271. (when org-org-htmlized-css-url
  272. (goto-char (point-min))
  273. (and (re-search-forward
  274. "<style type=\"text/css\">[^\000]*?\n[ \t]*</style>.*" nil t)
  275. (replace-match
  276. (format
  277. "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">"
  278. org-org-htmlized-css-url)
  279. t t)))
  280. (write-file (concat pub-dir (file-name-nondirectory filename) html-ext)))
  281. (kill-buffer newbuf)
  282. (unless visitingp (kill-buffer work-buffer)))
  283. ;; FIXME: Why? Which buffer is this supposed to apply to?
  284. (set-buffer-modified-p nil)))
  285. (provide 'ox-org)
  286. ;; Local variables:
  287. ;; generated-autoload-file: "org-loaddefs.el"
  288. ;; End:
  289. ;;; ox-org.el ends here