ox-org.el 13 KB

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