Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / org / ox-org.el
blobd35d1c05f2989d1f264b111e56f5944654e064e7
1 ;;; ox-org.el --- Org Back-End for Org Export Engine
3 ;; Copyright (C) 2013-2014 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 ;; This library implements an Org back-end for Org exporter.
27 ;; It introduces two interactive functions, `org-org-export-as-org'
28 ;; and `org-org-export-to-org', which export, respectively, to
29 ;; a temporary buffer and to a file.
31 ;; A publishing function is also provided: `org-org-publish-to-org'.
33 ;;; Code:
34 (require 'ox)
35 (declare-function htmlize-buffer "htmlize" (&optional buffer))
37 (defgroup org-export-org nil
38 "Options for exporting Org mode files to Org."
39 :tag "Org Export Org"
40 :group 'org-export
41 :version "24.4"
42 :package-version '(Org . "8.0"))
44 (define-obsolete-variable-alias
45 'org-export-htmlized-org-css-url 'org-org-htmlized-css-url "24.4")
46 (defcustom org-org-htmlized-css-url nil
47 "URL pointing to the CSS defining colors for htmlized Emacs buffers.
48 Normally when creating an htmlized version of an Org buffer,
49 htmlize will create the CSS to define the font colors. However,
50 this does not work when converting in batch mode, and it also can
51 look bad if different people with different fontification setup
52 work on the same website. When this variable is non-nil,
53 creating an htmlized version of an Org buffer using
54 `org-org-export-as-org' will include a link to this URL if the
55 setting of `org-html-htmlize-output-type' is 'css."
56 :group 'org-export-org
57 :type '(choice
58 (const :tag "Don't include external stylesheet link" nil)
59 (string :tag "URL or local href")))
61 (org-export-define-backend 'org
62 '((babel-call . org-org-identity)
63 (bold . org-org-identity)
64 (center-block . org-org-identity)
65 (clock . org-org-identity)
66 (code . org-org-identity)
67 (comment . (lambda (&rest args) ""))
68 (comment-block . (lambda (&rest args) ""))
69 (diary-sexp . org-org-identity)
70 (drawer . org-org-identity)
71 (dynamic-block . org-org-identity)
72 (entity . org-org-identity)
73 (example-block . org-org-identity)
74 (fixed-width . org-org-identity)
75 (footnote-definition . org-org-identity)
76 (footnote-reference . org-org-identity)
77 (headline . org-org-headline)
78 (horizontal-rule . org-org-identity)
79 (inline-babel-call . org-org-identity)
80 (inline-src-block . org-org-identity)
81 (inlinetask . org-org-identity)
82 (italic . org-org-identity)
83 (item . org-org-identity)
84 (keyword . org-org-keyword)
85 (latex-environment . org-org-identity)
86 (latex-fragment . org-org-identity)
87 (line-break . org-org-identity)
88 (link . org-org-identity)
89 (node-property . org-org-identity)
90 (paragraph . org-org-identity)
91 (plain-list . org-org-identity)
92 (planning . org-org-identity)
93 (property-drawer . org-org-identity)
94 (quote-block . org-org-identity)
95 (quote-section . org-org-identity)
96 (radio-target . org-org-identity)
97 (section . org-org-identity)
98 (special-block . org-org-identity)
99 (src-block . org-org-identity)
100 (statistics-cookie . org-org-identity)
101 (strike-through . org-org-identity)
102 (subscript . org-org-identity)
103 (superscript . org-org-identity)
104 (table . org-org-identity)
105 (table-cell . org-org-identity)
106 (table-row . org-org-identity)
107 (target . org-org-identity)
108 (timestamp . org-org-identity)
109 (underline . org-org-identity)
110 (verbatim . org-org-identity)
111 (verse-block . org-org-identity))
112 :menu-entry
113 '(?O "Export to Org"
114 ((?O "As Org buffer" org-org-export-as-org)
115 (?o "As Org file" org-org-export-to-org)
116 (?v "As Org file and open"
117 (lambda (a s v b)
118 (if a (org-org-export-to-org t s v b)
119 (org-open-file (org-org-export-to-org nil s v b))))))))
121 (defun org-org-identity (blob contents info)
122 "Transcode BLOB element or object back into Org syntax.
123 CONTENTS is its contents, as a string or nil. INFO is ignored."
124 (org-export-expand blob contents t))
126 (defun org-org-headline (headline contents info)
127 "Transcode HEADLINE element back into Org syntax.
128 CONTENTS is its contents, as a string or nil. INFO is ignored."
129 (unless (plist-get info :with-todo-keywords)
130 (org-element-put-property headline :todo-keyword nil))
131 (unless (plist-get info :with-tags)
132 (org-element-put-property headline :tags nil))
133 (unless (plist-get info :with-priority)
134 (org-element-put-property headline :priority nil))
135 (org-element-put-property headline :level
136 (org-export-get-relative-level headline info))
137 (org-element-headline-interpreter headline contents))
139 (defun org-org-keyword (keyword contents info)
140 "Transcode KEYWORD element back into Org syntax.
141 CONTENTS is nil. INFO is ignored. This function ignores
142 keywords targeted at other export back-ends."
143 (unless (member (org-element-property :key keyword)
144 (mapcar
145 (lambda (block-cons)
146 (and (eq (cdr block-cons) 'org-element-export-block-parser)
147 (car block-cons)))
148 org-element-block-name-alist))
149 (org-element-keyword-interpreter keyword nil)))
151 ;;;###autoload
152 (defun org-org-export-as-org (&optional async subtreep visible-only ext-plist)
153 "Export current buffer to an Org buffer.
155 If narrowing is active in the current buffer, only export its
156 narrowed part.
158 If a region is active, export that region.
160 A non-nil optional argument ASYNC means the process should happen
161 asynchronously. The resulting buffer should be accessible
162 through the `org-export-stack' interface.
164 When optional argument SUBTREEP is non-nil, export the sub-tree
165 at point, extracting information from the headline properties
166 first.
168 When optional argument VISIBLE-ONLY is non-nil, don't export
169 contents of hidden elements.
171 EXT-PLIST, when provided, is a property list with external
172 parameters overriding Org default settings, but still inferior to
173 file-local settings.
175 Export is done in a buffer named \"*Org ORG Export*\", which will
176 be displayed when `org-export-show-temporary-export-buffer' is
177 non-nil."
178 (interactive)
179 (org-export-to-buffer 'org "*Org ORG Export*"
180 async subtreep visible-only nil ext-plist (lambda () (org-mode))))
182 ;;;###autoload
183 (defun org-org-export-to-org (&optional async subtreep visible-only ext-plist)
184 "Export current buffer to an org file.
186 If narrowing is active in the current buffer, only export its
187 narrowed part.
189 If a region is active, export that region.
191 A non-nil optional argument ASYNC means the process should happen
192 asynchronously. The resulting file should be accessible through
193 the `org-export-stack' interface.
195 When optional argument SUBTREEP is non-nil, export the sub-tree
196 at point, extracting information from the headline properties
197 first.
199 When optional argument VISIBLE-ONLY is non-nil, don't export
200 contents of hidden elements.
202 EXT-PLIST, when provided, is a property list with external
203 parameters overriding Org default settings, but still inferior to
204 file-local settings.
206 Return output file name."
207 (interactive)
208 (let ((outfile (org-export-output-file-name ".org" subtreep)))
209 (org-export-to-file 'org outfile
210 async subtreep visible-only nil ext-plist)))
212 ;;;###autoload
213 (defun org-org-publish-to-org (plist filename pub-dir)
214 "Publish an org file to org.
216 FILENAME is the filename of the Org file to be published. PLIST
217 is the property list for the given project. PUB-DIR is the
218 publishing directory.
220 Return output file name."
221 (org-publish-org-to 'org filename ".org" plist pub-dir)
222 (when (plist-get plist :htmlized-source)
223 (require 'htmlize)
224 (require 'ox-html)
225 (let* ((org-inhibit-startup t)
226 (htmlize-output-type 'css)
227 (html-ext (concat "." (or (plist-get plist :html-extension)
228 org-html-extension "html")))
229 (visitingp (find-buffer-visiting filename))
230 (work-buffer (or visitingp (find-file filename)))
231 newbuf)
232 (font-lock-fontify-buffer)
233 (show-all)
234 (org-show-block-all)
235 (setq newbuf (htmlize-buffer))
236 (with-current-buffer newbuf
237 (when org-org-htmlized-css-url
238 (goto-char (point-min))
239 (and (re-search-forward
240 "<style type=\"text/css\">[^\000]*?\n[ \t]*</style>.*" nil t)
241 (replace-match
242 (format
243 "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">"
244 org-org-htmlized-css-url) t t)))
245 (write-file (concat pub-dir (file-name-nondirectory filename) html-ext)))
246 (kill-buffer newbuf)
247 (unless visitingp (kill-buffer work-buffer)))
248 (set-buffer-modified-p nil)))
251 (provide 'ox-org)
253 ;; Local variables:
254 ;; generated-autoload-file: "org-loaddefs.el"
255 ;; End:
257 ;;; ox-org.el ends here