ox: Small refactoring
[org-mode.git] / lisp / ox-org.el
blob734b8d221f8f493630bc6a165b8e0d3456766eb1
1 ;;; ox-org.el --- Org Back-End for Org Export Engine
3 ;; Copyright (C) 2013-2015 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou@gmail.com>
6 ;; Keywords: org, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;;; Code:
27 (require 'ox)
28 (declare-function htmlize-buffer "htmlize" (&optional buffer))
30 (defgroup org-export-org nil
31 "Options for exporting Org mode files to Org."
32 :tag "Org Export Org"
33 :group 'org-export
34 :version "24.4"
35 :package-version '(Org . "8.0"))
37 (define-obsolete-variable-alias
38 'org-export-htmlized-org-css-url 'org-org-htmlized-css-url "24.4")
39 (defcustom org-org-htmlized-css-url nil
40 "URL pointing to the CSS defining colors for htmlized Emacs buffers.
41 Normally when creating an htmlized version of an Org buffer,
42 htmlize will create the CSS to define the font colors. However,
43 this does not work when converting in batch mode, and it also can
44 look bad if different people with different fontification setup
45 work on the same website. When this variable is non-nil,
46 creating an htmlized version of an Org buffer using
47 `org-org-export-as-org' will include a link to this URL if the
48 setting of `org-html-htmlize-output-type' is 'css."
49 :group 'org-export-org
50 :type '(choice
51 (const :tag "Don't include external stylesheet link" nil)
52 (string :tag "URL or local href")))
54 (org-export-define-backend 'org
55 '((babel-call . org-org-identity)
56 (bold . org-org-identity)
57 (center-block . org-org-identity)
58 (clock . org-org-identity)
59 (code . org-org-identity)
60 (comment . (lambda (&rest args) ""))
61 (comment-block . (lambda (&rest args) ""))
62 (diary-sexp . org-org-identity)
63 (drawer . org-org-identity)
64 (dynamic-block . org-org-identity)
65 (entity . org-org-identity)
66 (example-block . org-org-identity)
67 (fixed-width . org-org-identity)
68 (footnote-definition . ignore)
69 (footnote-reference . org-org-identity)
70 (headline . org-org-headline)
71 (horizontal-rule . org-org-identity)
72 (inline-babel-call . org-org-identity)
73 (inline-src-block . org-org-identity)
74 (inlinetask . org-org-identity)
75 (italic . org-org-identity)
76 (item . org-org-identity)
77 (keyword . org-org-keyword)
78 (latex-environment . org-org-identity)
79 (latex-fragment . org-org-identity)
80 (line-break . org-org-identity)
81 (link . org-org-link)
82 (node-property . org-org-identity)
83 (template . org-org-template)
84 (paragraph . org-org-identity)
85 (plain-list . org-org-identity)
86 (planning . org-org-identity)
87 (property-drawer . org-org-identity)
88 (quote-block . org-org-identity)
89 (radio-target . org-org-identity)
90 (section . org-org-section)
91 (special-block . org-org-identity)
92 (src-block . org-org-identity)
93 (statistics-cookie . org-org-identity)
94 (strike-through . org-org-identity)
95 (subscript . org-org-identity)
96 (superscript . org-org-identity)
97 (table . org-org-identity)
98 (table-cell . org-org-identity)
99 (table-row . org-org-identity)
100 (target . org-org-identity)
101 (timestamp . org-org-identity)
102 (underline . org-org-identity)
103 (verbatim . org-org-identity)
104 (verse-block . org-org-identity))
105 :menu-entry
106 '(?O "Export to Org"
107 ((?O "As Org buffer" org-org-export-as-org)
108 (?o "As Org file" org-org-export-to-org)
109 (?v "As Org file and open"
110 (lambda (a s v b)
111 (if a (org-org-export-to-org t s v b)
112 (org-open-file (org-org-export-to-org nil s v b))))))))
114 (defun org-org-identity (blob contents info)
115 "Transcode BLOB element or object back into Org syntax.
116 CONTENTS is its contents, as a string or nil. INFO is ignored."
117 (let ((case-fold-search t))
118 (replace-regexp-in-string
119 "^[ \t]*#\\+ATTR_[-_A-Za-z0-9]+:\\(?: .*\\)?\n" ""
120 (org-export-expand blob contents t))))
122 (defun org-org-headline (headline contents info)
123 "Transcode HEADLINE element back into Org syntax.
124 CONTENTS is its contents, as a string or nil. INFO is ignored."
125 (unless (org-element-property :footnote-section-p headline)
126 (unless (plist-get info :with-todo-keywords)
127 (org-element-put-property headline :todo-keyword nil))
128 (unless (plist-get info :with-tags)
129 (org-element-put-property headline :tags nil))
130 (unless (plist-get info :with-priority)
131 (org-element-put-property headline :priority nil))
132 (org-element-put-property headline :level
133 (org-export-get-relative-level headline info))
134 (org-element-headline-interpreter headline contents)))
136 (defun org-org-keyword (keyword contents info)
137 "Transcode KEYWORD element back into Org syntax.
138 CONTENTS is nil. INFO is ignored."
139 (let ((key (org-element-property :key keyword)))
140 (unless (member key
141 '("AUTHOR" "CREATOR" "DATE" "DESCRIPTION" "EMAIL" "KEYWORDS"
142 "OPTIONS" "TITLE"))
143 (org-element-keyword-interpreter keyword nil))))
145 (defun org-org-link (link contents info)
146 "Transcode LINK object back into Org syntax.
147 CONTENTS is the description of the link, as a string, or nil.
148 INFO is a plist containing current export state."
149 (or (org-export-custom-protocol-maybe link contents 'org)
150 (org-element-link-interpreter link contents)))
152 (defun org-org-template (contents info)
153 "Return Org document template with document keywords.
154 CONTENTS is the transcoded contents string. INFO is a plist used
155 as a communication channel."
156 (concat
157 (and (plist-get info :time-stamp-file)
158 (format-time-string "# Created %Y-%m-%d %a %H:%M\n"))
159 (org-element-normalize-string
160 (mapconcat #'identity
161 (org-element-map (plist-get info :parse-tree) 'keyword
162 (lambda (k)
163 (and (string-equal (org-element-property :key k) "OPTIONS")
164 (concat "#+OPTIONS: "
165 (org-element-property :value k)))))
166 "\n"))
167 (and (plist-get info :with-title)
168 (format "#+TITLE: %s\n" (org-export-data (plist-get info :title) info)))
169 (and (plist-get info :with-date)
170 (let ((date (org-export-data (org-export-get-date info) info)))
171 (and (org-string-nw-p date)
172 (format "#+DATE: %s\n" date))))
173 (and (plist-get info :with-author)
174 (let ((author (org-export-data (plist-get info :author) info)))
175 (and (org-string-nw-p author)
176 (format "#+AUTHOR: %s\n" author))))
177 (and (plist-get info :with-email)
178 (let ((email (org-export-data (plist-get info :email) info)))
179 (and (org-string-nw-p email)
180 (format "#+EMAIL: %s\n" email))))
181 (and (eq (plist-get info :with-creator) t)
182 (org-string-nw-p (plist-get info :creator))
183 (format "#+CREATOR: %s\n" (plist-get info :creator)))
184 (and (org-string-nw-p (plist-get info :keywords))
185 (format "#+KEYWORDS: %s\n" (plist-get info :keywords)))
186 (and (org-string-nw-p (plist-get info :description))
187 (format "#+DESCRIPTION: %s\n" (plist-get info :description)))
188 contents
189 (and (eq (plist-get info :with-creator) 'comment)
190 (org-string-nw-p (plist-get info :creator))
191 (format "\n# %s\n" (plist-get info :creator)))))
193 (defun org-org-section (section contents info)
194 "Transcode SECTION element back into Org syntax.
195 CONTENTS is the contents of the section. INFO is a plist used as
196 a communication channel."
197 (concat
198 (org-element-normalize-string contents)
199 ;; Insert footnote definitions appearing for the first time in this
200 ;; section. Indeed, some of them may not be available to narrowing
201 ;; so we make sure all of them are included in the result.
202 (let ((footnotes-alist
203 (org-element-map section 'footnote-reference
204 (lambda (fn)
205 (and (eq (org-element-property :type fn) 'standard)
206 (org-export-footnote-first-reference-p fn info)
207 (cons (org-element-property :label fn)
208 (org-export-get-footnote-definition fn info))))
209 info)))
210 (and footnotes-alist
211 (concat "\n"
212 (mapconcat
213 (lambda (d)
214 (org-element-normalize-string
215 (concat (format "[%s] "(car d))
216 (org-export-data (cdr d) info))))
217 footnotes-alist "\n"))))
218 (make-string (or (org-element-property :post-blank section) 0) ?\n)))
220 ;;;###autoload
221 (defun org-org-export-as-org
222 (&optional async subtreep visible-only body-only ext-plist)
223 "Export current buffer to an Org buffer.
225 If narrowing is active in the current buffer, only export its
226 narrowed part.
228 If a region is active, export that region.
230 A non-nil optional argument ASYNC means the process should happen
231 asynchronously. The resulting buffer should be accessible
232 through 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.
238 When optional argument VISIBLE-ONLY is non-nil, don't export
239 contents of hidden elements.
241 When optional argument BODY-ONLY is non-nil, strip document
242 keywords from output.
244 EXT-PLIST, when provided, is a property list with external
245 parameters overriding Org default settings, but still inferior to
246 file-local settings.
248 Export is done in a buffer named \"*Org ORG Export*\", which will
249 be displayed when `org-export-show-temporary-export-buffer' is
250 non-nil."
251 (interactive)
252 (org-export-to-buffer 'org "*Org ORG Export*"
253 async subtreep visible-only body-only ext-plist (lambda () (org-mode))))
255 ;;;###autoload
256 (defun org-org-export-to-org
257 (&optional async subtreep visible-only body-only ext-plist)
258 "Export current buffer to an org file.
260 If narrowing is active in the current buffer, only export its
261 narrowed part.
263 If a region is active, export that region.
265 A non-nil optional argument ASYNC means the process should happen
266 asynchronously. The resulting file should be accessible through
267 the `org-export-stack' interface.
269 When optional argument SUBTREEP is non-nil, export the sub-tree
270 at point, extracting information from the headline properties
271 first.
273 When optional argument VISIBLE-ONLY is non-nil, don't export
274 contents of hidden elements.
276 When optional argument BODY-ONLY is non-nil, strip document
277 keywords from output.
279 EXT-PLIST, when provided, is a property list with external
280 parameters overriding Org default settings, but still inferior to
281 file-local settings.
283 Return output file name."
284 (interactive)
285 (let ((outfile (org-export-output-file-name ".org" subtreep)))
286 (org-export-to-file 'org outfile
287 async subtreep visible-only body-only ext-plist)))
289 ;;;###autoload
290 (defun org-org-publish-to-org (plist filename pub-dir)
291 "Publish an org file to org.
293 FILENAME is the filename of the Org file to be published. PLIST
294 is the property list for the given project. PUB-DIR is the
295 publishing directory.
297 Return output file name."
298 (org-publish-org-to 'org filename ".org" plist pub-dir)
299 (when (plist-get plist :htmlized-source)
300 (require 'htmlize)
301 (require 'ox-html)
302 (let* ((org-inhibit-startup t)
303 (htmlize-output-type 'css)
304 (html-ext (concat "." (or (plist-get plist :html-extension)
305 org-html-extension "html")))
306 (visitingp (find-buffer-visiting filename))
307 (work-buffer (or visitingp (find-file filename)))
308 newbuf)
309 (font-lock-ensure)
310 (show-all)
311 (org-show-block-all)
312 (setq newbuf (htmlize-buffer))
313 (with-current-buffer newbuf
314 (when org-org-htmlized-css-url
315 (goto-char (point-min))
316 (and (re-search-forward
317 "<style type=\"text/css\">[^\000]*?\n[ \t]*</style>.*" nil t)
318 (replace-match
319 (format
320 "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">"
321 org-org-htmlized-css-url) t t)))
322 (write-file (concat pub-dir (file-name-nondirectory filename) html-ext)))
323 (kill-buffer newbuf)
324 (unless visitingp (kill-buffer work-buffer)))
325 (set-buffer-modified-p nil)))
328 (provide 'ox-org)
330 ;; Local variables:
331 ;; generated-autoload-file: "org-loaddefs.el"
332 ;; End:
334 ;;; ox-org.el ends here