ox-org.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. ;;; ox-org.el --- Org Back-End for Org Export Engine -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2013-2019 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 <https://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-timestamp)
  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. :filters-alist '((:filter-parse-tree . org-org--add-missing-sections)))
  100. (defun org-org--add-missing-sections (tree _backend _info)
  101. "Ensure each headline has an associated section.
  102. TREE is the parse tree being exported.
  103. Footnotes relative to the headline are inserted in the section,
  104. using `org-org-section'. However, this function is not called if
  105. the headline doesn't contain any section in the first place, so
  106. we make sure it is always called."
  107. (org-element-map tree 'headline
  108. (lambda (h)
  109. (let ((first-child (car (org-element-contents h)))
  110. (new-section (org-element-create 'section)))
  111. (pcase (org-element-type first-child)
  112. (`section nil)
  113. (`nil (org-element-adopt-elements h new-section))
  114. (_ (org-element-insert-before new-section first-child))))))
  115. tree)
  116. (defun org-org-export-block (export-block _contents _info)
  117. "Transcode a EXPORT-BLOCK element from Org to LaTeX.
  118. CONTENTS and INFO are ignored."
  119. (and (equal (org-element-property :type export-block) "ORG")
  120. (org-element-property :value export-block)))
  121. (defun org-org-identity (blob contents _info)
  122. "Transcode BLOB element or object back into Org syntax.
  123. CONTENTS is its contents, as a string or nil. INFO is ignored."
  124. (let ((case-fold-search t))
  125. (replace-regexp-in-string
  126. "^[ \t]*#\\+ATTR_[-_A-Za-z0-9]+:\\(?: .*\\)?\n" ""
  127. (org-export-expand blob contents t))))
  128. (defun org-org-headline (headline contents info)
  129. "Transcode HEADLINE element back into Org syntax.
  130. CONTENTS is its contents, as a string or nil. INFO is ignored."
  131. (unless (org-element-property :footnote-section-p headline)
  132. (unless (plist-get info :with-todo-keywords)
  133. (org-element-put-property headline :todo-keyword nil))
  134. (unless (plist-get info :with-tags)
  135. (org-element-put-property headline :tags nil))
  136. (unless (plist-get info :with-priority)
  137. (org-element-put-property headline :priority nil))
  138. (org-element-put-property headline :level
  139. (org-export-get-relative-level headline info))
  140. (org-element-headline-interpreter headline contents)))
  141. (defun org-org-keyword (keyword _contents _info)
  142. "Transcode KEYWORD element back into Org syntax.
  143. CONTENTS is nil. INFO is ignored."
  144. (let ((key (org-element-property :key keyword)))
  145. (unless (member key
  146. '("AUTHOR" "CREATOR" "DATE" "EMAIL" "OPTIONS" "TITLE"))
  147. (org-element-keyword-interpreter keyword nil))))
  148. (defun org-org-link (link contents _info)
  149. "Transcode LINK object back into Org syntax.
  150. CONTENTS is the description of the link, as a string, or nil.
  151. INFO is a plist containing current export state."
  152. (or (org-export-custom-protocol-maybe link contents 'org)
  153. (org-element-link-interpreter link contents)))
  154. (defun org-org-template (contents info)
  155. "Return Org document template with document keywords.
  156. CONTENTS is the transcoded contents string. INFO is a plist used
  157. as a communication channel."
  158. (concat
  159. (and (plist-get info :time-stamp-file)
  160. (format-time-string "# Created %Y-%m-%d %a %H:%M\n"))
  161. (org-element-normalize-string
  162. (mapconcat #'identity
  163. (org-element-map (plist-get info :parse-tree) 'keyword
  164. (lambda (k)
  165. (and (string-equal (org-element-property :key k) "OPTIONS")
  166. (concat "#+OPTIONS: "
  167. (org-element-property :value k)))))
  168. "\n"))
  169. (and (plist-get info :with-title)
  170. (format "#+TITLE: %s\n" (org-export-data (plist-get info :title) info)))
  171. (and (plist-get info :with-date)
  172. (let ((date (org-export-data (org-export-get-date info) info)))
  173. (and (org-string-nw-p date)
  174. (format "#+DATE: %s\n" date))))
  175. (and (plist-get info :with-author)
  176. (let ((author (org-export-data (plist-get info :author) info)))
  177. (and (org-string-nw-p author)
  178. (format "#+AUTHOR: %s\n" author))))
  179. (and (plist-get info :with-email)
  180. (let ((email (org-export-data (plist-get info :email) info)))
  181. (and (org-string-nw-p email)
  182. (format "#+EMAIL: %s\n" email))))
  183. (and (plist-get info :with-creator)
  184. (org-string-nw-p (plist-get info :creator))
  185. (format "#+CREATOR: %s\n" (plist-get info :creator)))
  186. contents))
  187. (defun org-org-timestamp (timestamp _contents _info)
  188. "Transcode a TIMESTAMP object to custom format or back into Org syntax."
  189. (org-timestamp-translate timestamp))
  190. (defun org-org-section (section contents info)
  191. "Transcode SECTION element back into Org syntax.
  192. CONTENTS is the contents of the section. INFO is a plist used as
  193. a communication channel."
  194. (concat
  195. (org-element-normalize-string contents)
  196. ;; Insert footnote definitions appearing for the first time in this
  197. ;; section, or in the relative headline title. Indeed, some of
  198. ;; them may not be available to narrowing so we make sure all of
  199. ;; them are included in the result.
  200. (let ((footnotes
  201. (org-element-map
  202. (list (org-export-get-parent-headline section) section)
  203. 'footnote-reference
  204. (lambda (fn)
  205. (and (eq (org-element-property :type fn) 'standard)
  206. (org-export-footnote-first-reference-p fn info)
  207. (org-element-normalize-string
  208. (format "[fn:%s] %s"
  209. (org-element-property :label fn)
  210. (org-export-data
  211. (org-export-get-footnote-definition fn info)
  212. info)))))
  213. info nil 'headline t)))
  214. (and footnotes (concat "\n" (mapconcat #'identity footnotes "\n"))))))
  215. ;;;###autoload
  216. (defun org-org-export-as-org
  217. (&optional async subtreep visible-only body-only ext-plist)
  218. "Export current buffer to an Org buffer.
  219. If narrowing is active in the current buffer, only export its
  220. narrowed part.
  221. If a region is active, export that region.
  222. A non-nil optional argument ASYNC means the process should happen
  223. asynchronously. The resulting buffer should be accessible
  224. through the `org-export-stack' interface.
  225. When optional argument SUBTREEP is non-nil, export the sub-tree
  226. at point, extracting information from the headline properties
  227. first.
  228. When optional argument VISIBLE-ONLY is non-nil, don't export
  229. contents of hidden elements.
  230. When optional argument BODY-ONLY is non-nil, strip document
  231. keywords from output.
  232. EXT-PLIST, when provided, is a property list with external
  233. parameters overriding Org default settings, but still inferior to
  234. file-local settings.
  235. Export is done in a buffer named \"*Org ORG Export*\", which will
  236. be displayed when `org-export-show-temporary-export-buffer' is
  237. non-nil."
  238. (interactive)
  239. (org-export-to-buffer 'org "*Org ORG Export*"
  240. async subtreep visible-only body-only ext-plist (lambda () (org-mode))))
  241. ;;;###autoload
  242. (defun org-org-export-to-org
  243. (&optional async subtreep visible-only body-only ext-plist)
  244. "Export current buffer to an Org file.
  245. If narrowing is active in the current buffer, only export its
  246. narrowed part.
  247. If a region is active, export that region.
  248. A non-nil optional argument ASYNC means the process should happen
  249. asynchronously. The resulting file should be accessible through
  250. the `org-export-stack' interface.
  251. When optional argument SUBTREEP is non-nil, export the sub-tree
  252. at point, extracting information from the headline properties
  253. first.
  254. When optional argument VISIBLE-ONLY is non-nil, don't export
  255. contents of hidden elements.
  256. When optional argument BODY-ONLY is non-nil, strip document
  257. keywords from output.
  258. EXT-PLIST, when provided, is a property list with external
  259. parameters overriding Org default settings, but still inferior to
  260. file-local settings.
  261. Return output file name."
  262. (interactive)
  263. (let ((outfile (org-export-output-file-name ".org" subtreep)))
  264. (org-export-to-file 'org outfile
  265. async subtreep visible-only body-only ext-plist)))
  266. ;;;###autoload
  267. (defun org-org-publish-to-org (plist filename pub-dir)
  268. "Publish an Org file to Org.
  269. FILENAME is the filename of the Org file to be published. PLIST
  270. is the property list for the given project. PUB-DIR is the
  271. publishing directory.
  272. Return output file name."
  273. (org-publish-org-to 'org filename ".org" plist pub-dir)
  274. (when (plist-get plist :htmlized-source)
  275. (or (require 'htmlize nil t)
  276. (error "Please install htmlize from https://github.com/hniksic/emacs-htmlize"))
  277. (require 'ox-html)
  278. (let* ((org-inhibit-startup t)
  279. (htmlize-output-type 'css)
  280. (html-ext (concat "." (or (plist-get plist :html-extension)
  281. org-html-extension "html")))
  282. (visitingp (find-buffer-visiting filename))
  283. (work-buffer (or visitingp (find-file-noselect filename)))
  284. newbuf)
  285. (with-current-buffer work-buffer
  286. (org-font-lock-ensure)
  287. (org-show-all)
  288. (setq newbuf (htmlize-buffer)))
  289. (with-current-buffer newbuf
  290. (when org-org-htmlized-css-url
  291. (goto-char (point-min))
  292. (and (re-search-forward
  293. "<style type=\"text/css\">[^\000]*?\n[ \t]*</style>.*" nil t)
  294. (replace-match
  295. (format
  296. "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">"
  297. org-org-htmlized-css-url)
  298. t t)))
  299. (write-file (concat pub-dir (file-name-nondirectory filename) html-ext)))
  300. (kill-buffer newbuf)
  301. (unless visitingp (kill-buffer work-buffer)))
  302. ;; FIXME: Why? Which buffer is this supposed to apply to?
  303. (set-buffer-modified-p nil)))
  304. (provide 'ox-org)
  305. ;; Local variables:
  306. ;; generated-autoload-file: "org-loaddefs.el"
  307. ;; End:
  308. ;;; ox-org.el ends here