ox-org: Preserve affiliated keywords
[org-mode/org-kjn.git] / lisp / ox-org.el
blob75393171398ebbb0880d9ded966137f0d538fc0f
1 ;;; ox-org.el --- Org Back-End for Org Export Engine
3 ;; Copyright (C) 2013 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou@gmail.com>
6 ;; Keywords: org, wp
8 ;; GNU Emacs is free software: you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Commentary:
23 ;; This library implements an Org back-end for Org exporter.
25 ;; It introduces two interactive functions, `org-org-export-as-org'
26 ;; and `org-org-export-to-org', which export, respectively, to
27 ;; a temporary buffer and to a file.
29 ;; A publishing function is also provided: `org-org-publish-to-org'.
31 ;;; Code:
32 (require 'ox)
33 (declare-function htmlize-buffer "htmlize" (&optional buffer))
35 (defgroup org-export-org nil
36 "Options for exporting Org mode files to Org."
37 :tag "Org Export Org"
38 :group 'org-export
39 :version "24.4"
40 :package-version '(Org . "8.0"))
42 (define-obsolete-variable-alias
43 'org-export-htmlized-org-css-url 'org-org-htmlized-css-url "24.4")
44 (defcustom org-org-htmlized-css-url nil
45 "URL pointing to the CSS defining colors for htmlized Emacs buffers.
46 Normally when creating an htmlized version of an Org buffer,
47 htmlize will create the CSS to define the font colors. However,
48 this does not work when converting in batch mode, and it also can
49 look bad if different people with different fontification setup
50 work on the same website. When this variable is non-nil,
51 creating an htmlized version of an Org buffer using
52 `org-org-export-as-org' will include a link to this URL if the
53 setting of `org-html-htmlize-output-type' is 'css."
54 :group 'org-export-org
55 :type '(choice
56 (const :tag "Don't include external stylesheet link" nil)
57 (string :tag "URL or local href")))
59 (org-export-define-backend 'org
60 '((babel-call . org-org-identity)
61 (bold . org-org-identity)
62 (center-block . org-org-identity)
63 (clock . org-org-identity)
64 (code . org-org-identity)
65 (comment . (lambda (&rest args) ""))
66 (comment-block . (lambda (&rest args) ""))
67 (diary-sexp . org-org-identity)
68 (drawer . org-org-identity)
69 (dynamic-block . org-org-identity)
70 (entity . org-org-identity)
71 (example-block . org-org-identity)
72 (fixed-width . org-org-identity)
73 (footnote-definition . org-org-identity)
74 (footnote-reference . org-org-identity)
75 (headline . org-org-headline)
76 (horizontal-rule . org-org-identity)
77 (inline-babel-call . org-org-identity)
78 (inline-src-block . org-org-identity)
79 (inlinetask . org-org-identity)
80 (italic . org-org-identity)
81 (item . org-org-identity)
82 (keyword . org-org-keyword)
83 (latex-environment . org-org-identity)
84 (latex-fragment . org-org-identity)
85 (line-break . org-org-identity)
86 (link . org-org-identity)
87 (node-property . org-org-identity)
88 (paragraph . org-org-identity)
89 (plain-list . org-org-identity)
90 (planning . org-org-identity)
91 (property-drawer . org-org-identity)
92 (quote-block . org-org-identity)
93 (quote-section . org-org-identity)
94 (radio-target . org-org-identity)
95 (section . org-org-identity)
96 (special-block . org-org-identity)
97 (src-block . org-org-identity)
98 (statistics-cookie . org-org-identity)
99 (strike-through . org-org-identity)
100 (subscript . org-org-identity)
101 (superscript . org-org-identity)
102 (table . org-org-identity)
103 (table-cell . org-org-identity)
104 (table-row . org-org-identity)
105 (target . org-org-identity)
106 (timestamp . org-org-identity)
107 (underline . org-org-identity)
108 (verbatim . org-org-identity)
109 (verse-block . org-org-identity))
110 :menu-entry
111 '(?O "Export to Org"
112 ((?O "As Org buffer" org-org-export-as-org)
113 (?o "As Org file" org-org-export-to-org)
114 (?v "As Org file and open"
115 (lambda (a s v b)
116 (if a (org-org-export-to-org t s v b)
117 (org-open-file (org-org-export-to-org nil s v b))))))))
119 (defun org-org-identity (blob contents info)
120 "Transcode BLOB element or object back into Org syntax.
121 CONTENTS is its contents, as a string or nil. INFO is ignored."
122 (org-export-expand blob contents t))
124 (defun org-org-headline (headline contents info)
125 "Transcode HEADLINE element back into Org syntax.
126 CONTENTS is its contents, as a string or nil. INFO is ignored."
127 (unless (plist-get info :with-todo-keywords)
128 (org-element-put-property headline :todo-keyword nil))
129 (unless (plist-get info :with-tags)
130 (org-element-put-property headline :tags nil))
131 (unless (plist-get info :with-priority)
132 (org-element-put-property headline :priority nil))
133 (org-element-headline-interpreter headline contents))
135 (defun org-org-keyword (keyword contents info)
136 "Transcode KEYWORD element back into Org syntax.
137 CONTENTS is nil. INFO is ignored. This function ignores
138 keywords targeted at other export back-ends."
139 (unless (member (org-element-property :key keyword)
140 (mapcar
141 (lambda (block-cons)
142 (and (eq (cdr block-cons) 'org-element-export-block-parser)
143 (car block-cons)))
144 org-element-block-name-alist))
145 (org-element-keyword-interpreter keyword nil)))
147 ;;;###autoload
148 (defun org-org-export-as-org (&optional async subtreep visible-only ext-plist)
149 "Export current buffer to an Org buffer.
151 If narrowing is active in the current buffer, only export its
152 narrowed part.
154 If a region is active, export that region.
156 A non-nil optional argument ASYNC means the process should happen
157 asynchronously. The resulting buffer should be accessible
158 through the `org-export-stack' interface.
160 When optional argument SUBTREEP is non-nil, export the sub-tree
161 at point, extracting information from the headline properties
162 first.
164 When optional argument VISIBLE-ONLY is non-nil, don't export
165 contents of hidden elements.
167 EXT-PLIST, when provided, is a property list with external
168 parameters overriding Org default settings, but still inferior to
169 file-local settings.
171 Export is done in a buffer named \"*Org ORG Export*\", which will
172 be displayed when `org-export-show-temporary-export-buffer' is
173 non-nil."
174 (interactive)
175 (if async
176 (org-export-async-start
177 (lambda (output)
178 (with-current-buffer (get-buffer-create "*Org ORG Export*")
179 (erase-buffer)
180 (insert output)
181 (goto-char (point-min))
182 (org-mode)
183 (org-export-add-to-stack (current-buffer) 'org)))
184 `(org-export-as 'org ,subtreep ,visible-only nil ',ext-plist))
185 (let ((outbuf
186 (org-export-to-buffer
187 'org "*Org ORG Export*" subtreep visible-only nil ext-plist)))
188 (with-current-buffer outbuf (org-mode))
189 (when org-export-show-temporary-export-buffer
190 (switch-to-buffer-other-window outbuf)))))
192 ;;;###autoload
193 (defun org-org-export-to-org (&optional async subtreep visible-only ext-plist)
194 "Export current buffer to an org file.
196 If narrowing is active in the current buffer, only export its
197 narrowed part.
199 If a region is active, export that region.
201 A non-nil optional argument ASYNC means the process should happen
202 asynchronously. The resulting file should be accessible through
203 the `org-export-stack' interface.
205 When optional argument SUBTREEP is non-nil, export the sub-tree
206 at point, extracting information from the headline properties
207 first.
209 When optional argument VISIBLE-ONLY is non-nil, don't export
210 contents of hidden elements.
212 EXT-PLIST, when provided, is a property list with external
213 parameters overriding Org default settings, but still inferior to
214 file-local settings.
216 Return output file name."
217 (interactive)
218 (let ((outfile (org-export-output-file-name ".org" subtreep)))
219 (if async
220 (org-export-async-start
221 (lambda (f) (org-export-add-to-stack f 'org))
222 `(expand-file-name
223 (org-export-to-file
224 'org ,outfile ,subtreep ,visible-only nil ',ext-plist)))
225 (org-export-to-file 'org outfile subtreep visible-only nil ext-plist))))
227 ;;;###autoload
228 (defun org-org-publish-to-org (plist filename pub-dir)
229 "Publish an org file to org.
231 FILENAME is the filename of the Org file to be published. PLIST
232 is the property list for the given project. PUB-DIR is the
233 publishing directory.
235 Return output file name."
236 (org-publish-org-to 'org filename ".org" plist pub-dir)
237 (when (plist-get plist :htmlized-source)
238 (require 'htmlize)
239 (require 'ox-html)
240 (let* ((org-inhibit-startup t)
241 (htmlize-output-type 'css)
242 (html-ext (concat "." (or (plist-get plist :html-extension)
243 org-html-extension "html")))
244 (visitingp (find-buffer-visiting filename))
245 (work-buffer (or visitingp (find-file filename)))
246 newbuf)
247 (font-lock-fontify-buffer)
248 (setq newbuf (htmlize-buffer))
249 (with-current-buffer newbuf
250 (when org-org-htmlized-css-url
251 (goto-char (point-min))
252 (and (re-search-forward
253 "<style type=\"text/css\">[^\000]*?\n[ \t]*</style>.*" nil t)
254 (replace-match
255 (format
256 "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">"
257 org-org-htmlized-css-url) t t)))
258 (write-file (concat pub-dir (file-name-nondirectory filename) html-ext)))
259 (kill-buffer newbuf)
260 (unless visitingp (kill-buffer work-buffer)))
261 (set-buffer-modified-p nil)))
264 (provide 'ox-org)
266 ;; Local variables:
267 ;; generated-autoload-file: "org-loaddefs.el"
268 ;; End:
270 ;;; ox-org.el ends here