ox-org.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. ;;; ox-org.el --- Org Back-End for Org Export Engine -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2013-2018 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-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. :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-section (section contents info)
  188. "Transcode SECTION element back into Org syntax.
  189. CONTENTS is the contents of the section. INFO is a plist used as
  190. a communication channel."
  191. (concat
  192. (org-element-normalize-string contents)
  193. ;; Insert footnote definitions appearing for the first time in this
  194. ;; section, or in the relative headline title. Indeed, some of
  195. ;; them may not be available to narrowing so we make sure all of
  196. ;; them are included in the result.
  197. (let ((footnotes
  198. (org-element-map
  199. (list (org-export-get-parent-headline section) section)
  200. 'footnote-reference
  201. (lambda (fn)
  202. (and (eq (org-element-property :type fn) 'standard)
  203. (org-export-footnote-first-reference-p fn info)
  204. (org-element-normalize-string
  205. (format "[fn:%s] %s"
  206. (org-element-property :label fn)
  207. (org-export-data
  208. (org-export-get-footnote-definition fn info)
  209. info)))))
  210. info nil 'headline t)))
  211. (and footnotes (concat "\n" (mapconcat #'identity footnotes "\n"))))))
  212. ;;;###autoload
  213. (defun org-org-export-as-org
  214. (&optional async subtreep visible-only body-only ext-plist)
  215. "Export current buffer to an Org buffer.
  216. If narrowing is active in the current buffer, only export its
  217. narrowed part.
  218. If a region is active, export that region.
  219. A non-nil optional argument ASYNC means the process should happen
  220. asynchronously. The resulting buffer should be accessible
  221. through the `org-export-stack' interface.
  222. When optional argument SUBTREEP is non-nil, export the sub-tree
  223. at point, extracting information from the headline properties
  224. first.
  225. When optional argument VISIBLE-ONLY is non-nil, don't export
  226. contents of hidden elements.
  227. When optional argument BODY-ONLY is non-nil, strip document
  228. keywords from output.
  229. EXT-PLIST, when provided, is a property list with external
  230. parameters overriding Org default settings, but still inferior to
  231. file-local settings.
  232. Export is done in a buffer named \"*Org ORG Export*\", which will
  233. be displayed when `org-export-show-temporary-export-buffer' is
  234. non-nil."
  235. (interactive)
  236. (org-export-to-buffer 'org "*Org ORG Export*"
  237. async subtreep visible-only body-only ext-plist (lambda () (org-mode))))
  238. ;;;###autoload
  239. (defun org-org-export-to-org
  240. (&optional async subtreep visible-only body-only ext-plist)
  241. "Export current buffer to an org file.
  242. If narrowing is active in the current buffer, only export its
  243. narrowed part.
  244. If a region is active, export that region.
  245. A non-nil optional argument ASYNC means the process should happen
  246. asynchronously. The resulting file should be accessible through
  247. the `org-export-stack' interface.
  248. When optional argument SUBTREEP is non-nil, export the sub-tree
  249. at point, extracting information from the headline properties
  250. first.
  251. When optional argument VISIBLE-ONLY is non-nil, don't export
  252. contents of hidden elements.
  253. When optional argument BODY-ONLY is non-nil, strip document
  254. keywords from output.
  255. EXT-PLIST, when provided, is a property list with external
  256. parameters overriding Org default settings, but still inferior to
  257. file-local settings.
  258. Return output file name."
  259. (interactive)
  260. (let ((outfile (org-export-output-file-name ".org" subtreep)))
  261. (org-export-to-file 'org outfile
  262. async subtreep visible-only body-only ext-plist)))
  263. ;;;###autoload
  264. (defun org-org-publish-to-org (plist filename pub-dir)
  265. "Publish an org file to org.
  266. FILENAME is the filename of the Org file to be published. PLIST
  267. is the property list for the given project. PUB-DIR is the
  268. publishing directory.
  269. Return output file name."
  270. (org-publish-org-to 'org filename ".org" plist pub-dir)
  271. (when (plist-get plist :htmlized-source)
  272. (or (require 'htmlize nil t)
  273. (error "Please install htmlize from https://github.com/hniksic/emacs-htmlize"))
  274. (require 'ox-html)
  275. (let* ((org-inhibit-startup t)
  276. (htmlize-output-type 'css)
  277. (html-ext (concat "." (or (plist-get plist :html-extension)
  278. org-html-extension "html")))
  279. (visitingp (find-buffer-visiting filename))
  280. (work-buffer (or visitingp (find-file-noselect filename)))
  281. newbuf)
  282. (with-current-buffer work-buffer
  283. (org-font-lock-ensure)
  284. (outline-show-all)
  285. (org-show-block-all)
  286. (setq newbuf (htmlize-buffer)))
  287. (with-current-buffer newbuf
  288. (when org-org-htmlized-css-url
  289. (goto-char (point-min))
  290. (and (re-search-forward
  291. "<style type=\"text/css\">[^\000]*?\n[ \t]*</style>.*" nil t)
  292. (replace-match
  293. (format
  294. "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">"
  295. org-org-htmlized-css-url)
  296. t t)))
  297. (write-file (concat pub-dir (file-name-nondirectory filename) html-ext)))
  298. (kill-buffer newbuf)
  299. (unless visitingp (kill-buffer work-buffer)))
  300. ;; FIXME: Why? Which buffer is this supposed to apply to?
  301. (set-buffer-modified-p nil)))
  302. (provide 'ox-org)
  303. ;; Local variables:
  304. ;; generated-autoload-file: "org-loaddefs.el"
  305. ;; End:
  306. ;;; ox-org.el ends here