ox-org.el 13 KB

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