ox: Tiny fix
[org-mode.git] / lisp / ox-org.el
blob658983ea540c577668267fe79accfbdfc716764b
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 (diary-sexp . org-org-identity)
61 (drawer . org-org-identity)
62 (dynamic-block . org-org-identity)
63 (entity . org-org-identity)
64 (example-block . org-org-identity)
65 (fixed-width . org-org-identity)
66 (footnote-definition . ignore)
67 (footnote-reference . org-org-identity)
68 (headline . org-org-headline)
69 (horizontal-rule . org-org-identity)
70 (inline-babel-call . org-org-identity)
71 (inline-src-block . org-org-identity)
72 (inlinetask . org-org-identity)
73 (italic . org-org-identity)
74 (item . org-org-identity)
75 (keyword . org-org-keyword)
76 (latex-environment . org-org-identity)
77 (latex-fragment . org-org-identity)
78 (line-break . org-org-identity)
79 (link . org-org-link)
80 (node-property . org-org-identity)
81 (template . org-org-template)
82 (paragraph . org-org-identity)
83 (plain-list . org-org-identity)
84 (planning . org-org-identity)
85 (property-drawer . org-org-identity)
86 (quote-block . org-org-identity)
87 (radio-target . org-org-identity)
88 (section . org-org-section)
89 (special-block . org-org-identity)
90 (src-block . org-org-identity)
91 (statistics-cookie . org-org-identity)
92 (strike-through . org-org-identity)
93 (subscript . org-org-identity)
94 (superscript . org-org-identity)
95 (table . org-org-identity)
96 (table-cell . org-org-identity)
97 (table-row . org-org-identity)
98 (target . org-org-identity)
99 (timestamp . org-org-identity)
100 (underline . org-org-identity)
101 (verbatim . org-org-identity)
102 (verse-block . org-org-identity))
103 :menu-entry
104 '(?O "Export to Org"
105 ((?O "As Org buffer" org-org-export-as-org)
106 (?o "As Org file" org-org-export-to-org)
107 (?v "As Org file and open"
108 (lambda (a s v b)
109 (if a (org-org-export-to-org t s v b)
110 (org-open-file (org-org-export-to-org nil s v b))))))))
112 (defun org-org-identity (blob contents info)
113 "Transcode BLOB element or object back into Org syntax.
114 CONTENTS is its contents, as a string or nil. INFO is ignored."
115 (let ((case-fold-search t))
116 (replace-regexp-in-string
117 "^[ \t]*#\\+ATTR_[-_A-Za-z0-9]+:\\(?: .*\\)?\n" ""
118 (org-export-expand blob contents t))))
120 (defun org-org-headline (headline contents info)
121 "Transcode HEADLINE element back into Org syntax.
122 CONTENTS is its contents, as a string or nil. INFO is ignored."
123 (unless (org-element-property :footnote-section-p headline)
124 (unless (plist-get info :with-todo-keywords)
125 (org-element-put-property headline :todo-keyword nil))
126 (unless (plist-get info :with-tags)
127 (org-element-put-property headline :tags nil))
128 (unless (plist-get info :with-priority)
129 (org-element-put-property headline :priority nil))
130 (org-element-put-property headline :level
131 (org-export-get-relative-level headline info))
132 (org-element-headline-interpreter headline contents)))
134 (defun org-org-keyword (keyword contents info)
135 "Transcode KEYWORD element back into Org syntax.
136 CONTENTS is nil. INFO is ignored."
137 (let ((key (org-element-property :key keyword)))
138 (unless (member key
139 '("AUTHOR" "CREATOR" "DATE" "DESCRIPTION" "EMAIL" "KEYWORDS"
140 "OPTIONS" "TITLE"))
141 (org-element-keyword-interpreter keyword nil))))
143 (defun org-org-link (link contents info)
144 "Transcode LINK object back into Org syntax.
145 CONTENTS is the description of the link, as a string, or nil.
146 INFO is a plist containing current export state."
147 (or (org-export-custom-protocol-maybe link contents 'org)
148 (org-element-link-interpreter link contents)))
150 (defun org-org-template (contents info)
151 "Return Org document template with document keywords.
152 CONTENTS is the transcoded contents string. INFO is a plist used
153 as a communication channel."
154 (concat
155 (and (plist-get info :time-stamp-file)
156 (format-time-string "# Created %Y-%m-%d %a %H:%M\n"))
157 (org-element-normalize-string
158 (mapconcat #'identity
159 (org-element-map (plist-get info :parse-tree) 'keyword
160 (lambda (k)
161 (and (string-equal (org-element-property :key k) "OPTIONS")
162 (concat "#+OPTIONS: "
163 (org-element-property :value k)))))
164 "\n"))
165 (and (plist-get info :with-title)
166 (format "#+TITLE: %s\n" (org-export-data (plist-get info :title) info)))
167 (and (plist-get info :with-date)
168 (let ((date (org-export-data (org-export-get-date info) info)))
169 (and (org-string-nw-p date)
170 (format "#+DATE: %s\n" date))))
171 (and (plist-get info :with-author)
172 (let ((author (org-export-data (plist-get info :author) info)))
173 (and (org-string-nw-p author)
174 (format "#+AUTHOR: %s\n" author))))
175 (and (plist-get info :with-email)
176 (let ((email (org-export-data (plist-get info :email) info)))
177 (and (org-string-nw-p email)
178 (format "#+EMAIL: %s\n" email))))
179 (and (plist-get info :with-creator)
180 (org-string-nw-p (plist-get info :creator))
181 (format "#+CREATOR: %s\n" (plist-get info :creator)))
182 (and (org-string-nw-p (plist-get info :keywords))
183 (format "#+KEYWORDS: %s\n" (plist-get info :keywords)))
184 (and (org-string-nw-p (plist-get info :description))
185 (format "#+DESCRIPTION: %s\n" (plist-get info :description)))
186 contents))
188 (defun org-org-section (section contents info)
189 "Transcode SECTION element back into Org syntax.
190 CONTENTS is the contents of the section. INFO is a plist used as
191 a communication channel."
192 (concat
193 (org-element-normalize-string contents)
194 ;; Insert footnote definitions appearing for the first time in this
195 ;; section. Indeed, some of them may not be available to narrowing
196 ;; so we make sure all of them are included in the result.
197 (let ((footnotes-alist
198 (org-element-map section 'footnote-reference
199 (lambda (fn)
200 (and (eq (org-element-property :type fn) 'standard)
201 (org-export-footnote-first-reference-p fn info)
202 (cons (org-element-property :label fn)
203 (org-export-get-footnote-definition fn info))))
204 info)))
205 (and footnotes-alist
206 (concat "\n"
207 (mapconcat
208 (lambda (d)
209 (org-element-normalize-string
210 (concat (format "[%s] "(car d))
211 (org-export-data (cdr d) info))))
212 footnotes-alist "\n"))))
213 (make-string (or (org-element-property :post-blank section) 0) ?\n)))
215 ;;;###autoload
216 (defun org-org-export-as-org
217 (&optional async subtreep visible-only body-only ext-plist)
218 "Export current buffer to an Org buffer.
220 If narrowing is active in the current buffer, only export its
221 narrowed part.
223 If a region is active, export that region.
225 A non-nil optional argument ASYNC means the process should happen
226 asynchronously. The resulting buffer should be accessible
227 through the `org-export-stack' interface.
229 When optional argument SUBTREEP is non-nil, export the sub-tree
230 at point, extracting information from the headline properties
231 first.
233 When optional argument VISIBLE-ONLY is non-nil, don't export
234 contents of hidden elements.
236 When optional argument BODY-ONLY is non-nil, strip document
237 keywords from output.
239 EXT-PLIST, when provided, is a property list with external
240 parameters overriding Org default settings, but still inferior to
241 file-local settings.
243 Export is done in a buffer named \"*Org ORG Export*\", which will
244 be displayed when `org-export-show-temporary-export-buffer' is
245 non-nil."
246 (interactive)
247 (org-export-to-buffer 'org "*Org ORG Export*"
248 async subtreep visible-only body-only ext-plist (lambda () (org-mode))))
250 ;;;###autoload
251 (defun org-org-export-to-org
252 (&optional async subtreep visible-only body-only ext-plist)
253 "Export current buffer to an org file.
255 If narrowing is active in the current buffer, only export its
256 narrowed part.
258 If a region is active, export that region.
260 A non-nil optional argument ASYNC means the process should happen
261 asynchronously. The resulting file should be accessible through
262 the `org-export-stack' interface.
264 When optional argument SUBTREEP is non-nil, export the sub-tree
265 at point, extracting information from the headline properties
266 first.
268 When optional argument VISIBLE-ONLY is non-nil, don't export
269 contents of hidden elements.
271 When optional argument BODY-ONLY is non-nil, strip document
272 keywords from output.
274 EXT-PLIST, when provided, is a property list with external
275 parameters overriding Org default settings, but still inferior to
276 file-local settings.
278 Return output file name."
279 (interactive)
280 (let ((outfile (org-export-output-file-name ".org" subtreep)))
281 (org-export-to-file 'org outfile
282 async subtreep visible-only body-only ext-plist)))
284 ;;;###autoload
285 (defun org-org-publish-to-org (plist filename pub-dir)
286 "Publish an org file to org.
288 FILENAME is the filename of the Org file to be published. PLIST
289 is the property list for the given project. PUB-DIR is the
290 publishing directory.
292 Return output file name."
293 (org-publish-org-to 'org filename ".org" plist pub-dir)
294 (when (plist-get plist :htmlized-source)
295 (require 'htmlize)
296 (require 'ox-html)
297 (let* ((org-inhibit-startup t)
298 (htmlize-output-type 'css)
299 (html-ext (concat "." (or (plist-get plist :html-extension)
300 org-html-extension "html")))
301 (visitingp (find-buffer-visiting filename))
302 (work-buffer (or visitingp (find-file filename)))
303 newbuf)
304 (font-lock-ensure)
305 (show-all)
306 (org-show-block-all)
307 (setq newbuf (htmlize-buffer))
308 (with-current-buffer newbuf
309 (when org-org-htmlized-css-url
310 (goto-char (point-min))
311 (and (re-search-forward
312 "<style type=\"text/css\">[^\000]*?\n[ \t]*</style>.*" nil t)
313 (replace-match
314 (format
315 "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">"
316 org-org-htmlized-css-url) t t)))
317 (write-file (concat pub-dir (file-name-nondirectory filename) html-ext)))
318 (kill-buffer newbuf)
319 (unless visitingp (kill-buffer work-buffer)))
320 (set-buffer-modified-p nil)))
323 (provide 'ox-org)
325 ;; Local variables:
326 ;; generated-autoload-file: "org-loaddefs.el"
327 ;; End:
329 ;;; ox-org.el ends here