ox-org.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. ;;; ox-org.el --- Org Back-End for Org Export Engine
  2. ;; Copyright (C) 2013-2014 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. (comment . (lambda (&rest args) ""))
  49. (comment-block . (lambda (&rest args) ""))
  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. (fixed-width . org-org-identity)
  56. (footnote-definition . org-org-identity)
  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-identity)
  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-identity)
  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-identity (blob contents info)
  102. "Transcode BLOB element or object back into Org syntax.
  103. CONTENTS is its contents, as a string or nil. INFO is ignored."
  104. (let ((case-fold-search t))
  105. (replace-regexp-in-string
  106. "^[ \t]*#\\+ATTR_[-_A-Za-z0-9]+:\\(?: .*\\)?\n" ""
  107. (org-export-expand blob contents t))))
  108. (defun org-org-headline (headline contents info)
  109. "Transcode HEADLINE element back into Org syntax.
  110. CONTENTS is its contents, as a string or nil. INFO is ignored."
  111. (unless (plist-get info :with-todo-keywords)
  112. (org-element-put-property headline :todo-keyword nil))
  113. (unless (plist-get info :with-tags)
  114. (org-element-put-property headline :tags nil))
  115. (unless (plist-get info :with-priority)
  116. (org-element-put-property headline :priority nil))
  117. (org-element-put-property headline :level
  118. (org-export-get-relative-level headline info))
  119. (org-element-headline-interpreter headline contents))
  120. (defun org-org-keyword (keyword contents info)
  121. "Transcode KEYWORD element back into Org syntax.
  122. CONTENTS is nil. INFO is ignored. This function ignores
  123. keywords targeted at other export back-ends."
  124. (let ((key (org-element-property :key keyword)))
  125. (unless (or (member key
  126. (mapcar
  127. (lambda (block-cons)
  128. (and (eq (cdr block-cons)
  129. 'org-element-export-block-parser)
  130. (car block-cons)))
  131. org-element-block-name-alist))
  132. (member key
  133. '("AUTHOR" "CREATOR" "DATE" "DESCRIPTION" "EMAIL"
  134. "KEYWORDS" "TITLE")))
  135. (org-element-keyword-interpreter keyword nil))))
  136. (defun org-org-template (contents info)
  137. "Return Org document template with document keywords.
  138. CONTENTS is the transcoded contents string. INFO is a plist used
  139. as a communication channel."
  140. (concat
  141. (and (plist-get info :time-stamp-file)
  142. (format-time-string "# Created %Y-%m-%d %a %H:%M\n"))
  143. (format "#+TITLE: %s\n" (org-export-data (plist-get info :title) info))
  144. (and (plist-get info :with-date)
  145. (let ((date (org-export-data (org-export-get-date info) info)))
  146. (and (org-string-nw-p date)
  147. (format "#+DATE: %s\n" date))))
  148. (and (plist-get info :with-author)
  149. (let ((author (org-export-data (plist-get info :author) info)))
  150. (and (org-string-nw-p author)
  151. (format "#+AUTHOR: %s\n" author))))
  152. (and (plist-get info :with-email)
  153. (let ((email (org-export-data (plist-get info :email) info)))
  154. (and (org-string-nw-p email)
  155. (format "#+EMAIL: %s\n" email))))
  156. (and (eq (plist-get info :with-creator) t)
  157. (org-string-nw-p (plist-get info :creator))
  158. (format "#+CREATOR: %s\n" (plist-get info :creator)))
  159. (and (org-string-nw-p (plist-get info :keywords))
  160. (format "#+KEYWORDS: %s\n" (plist-get info :keywords)))
  161. (and (org-string-nw-p (plist-get info :description))
  162. (format "#+DESCRIPTION: %s\n" (plist-get info :description)))
  163. contents
  164. (and (eq (plist-get info :with-creator) 'comment)
  165. (org-string-nw-p (plist-get info :creator))
  166. (format "\n# %s\n" (plist-get info :creator)))))
  167. ;;;###autoload
  168. (defun org-org-export-as-org
  169. (&optional async subtreep visible-only body-only ext-plist)
  170. "Export current buffer to an Org buffer.
  171. If narrowing is active in the current buffer, only export its
  172. narrowed part.
  173. If a region is active, export that region.
  174. A non-nil optional argument ASYNC means the process should happen
  175. asynchronously. The resulting buffer should be accessible
  176. through the `org-export-stack' interface.
  177. When optional argument SUBTREEP is non-nil, export the sub-tree
  178. at point, extracting information from the headline properties
  179. first.
  180. When optional argument VISIBLE-ONLY is non-nil, don't export
  181. contents of hidden elements.
  182. When optional argument BODY-ONLY is non-nil, strip document
  183. keywords from output.
  184. EXT-PLIST, when provided, is a property list with external
  185. parameters overriding Org default settings, but still inferior to
  186. file-local settings.
  187. Export is done in a buffer named \"*Org ORG Export*\", which will
  188. be displayed when `org-export-show-temporary-export-buffer' is
  189. non-nil."
  190. (interactive)
  191. (org-export-to-buffer 'org "*Org ORG Export*"
  192. async subtreep visible-only body-only ext-plist (lambda () (org-mode))))
  193. ;;;###autoload
  194. (defun org-org-export-to-org
  195. (&optional async subtreep visible-only body-only ext-plist)
  196. "Export current buffer to an org file.
  197. If narrowing is active in the current buffer, only export its
  198. narrowed part.
  199. If a region is active, export that region.
  200. A non-nil optional argument ASYNC means the process should happen
  201. asynchronously. The resulting file should be accessible through
  202. the `org-export-stack' interface.
  203. When optional argument SUBTREEP is non-nil, export the sub-tree
  204. at point, extracting information from the headline properties
  205. first.
  206. When optional argument VISIBLE-ONLY is non-nil, don't export
  207. contents of hidden elements.
  208. When optional argument BODY-ONLY is non-nil, strip document
  209. keywords from output.
  210. EXT-PLIST, when provided, is a property list with external
  211. parameters overriding Org default settings, but still inferior to
  212. file-local settings.
  213. Return output file name."
  214. (interactive)
  215. (let ((outfile (org-export-output-file-name ".org" subtreep)))
  216. (org-export-to-file 'org outfile
  217. async subtreep visible-only body-only ext-plist)))
  218. ;;;###autoload
  219. (defun org-org-publish-to-org (plist filename pub-dir)
  220. "Publish an org file to org.
  221. FILENAME is the filename of the Org file to be published. PLIST
  222. is the property list for the given project. PUB-DIR is the
  223. publishing directory.
  224. Return output file name."
  225. (org-publish-org-to 'org filename ".org" plist pub-dir)
  226. (when (plist-get plist :htmlized-source)
  227. (require 'htmlize)
  228. (require 'ox-html)
  229. (let* ((org-inhibit-startup t)
  230. (htmlize-output-type 'css)
  231. (html-ext (concat "." (or (plist-get plist :html-extension)
  232. org-html-extension "html")))
  233. (visitingp (find-buffer-visiting filename))
  234. (work-buffer (or visitingp (find-file filename)))
  235. newbuf)
  236. (font-lock-fontify-buffer)
  237. (show-all)
  238. (org-show-block-all)
  239. (setq newbuf (htmlize-buffer))
  240. (with-current-buffer newbuf
  241. (when org-org-htmlized-css-url
  242. (goto-char (point-min))
  243. (and (re-search-forward
  244. "<style type=\"text/css\">[^\000]*?\n[ \t]*</style>.*" nil t)
  245. (replace-match
  246. (format
  247. "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">"
  248. org-org-htmlized-css-url) t t)))
  249. (write-file (concat pub-dir (file-name-nondirectory filename) html-ext)))
  250. (kill-buffer newbuf)
  251. (unless visitingp (kill-buffer work-buffer)))
  252. (set-buffer-modified-p nil)))
  253. (provide 'ox-org)
  254. ;; Local variables:
  255. ;; generated-autoload-file: "org-loaddefs.el"
  256. ;; End:
  257. ;;; ox-org.el ends here