ox-org.el 12 KB

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