ox-org.el 12 KB

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