testing: allow to select tests
[org-mode/org-tableheadings.git] / lisp / ox-org.el
blob32262e2b68e57291cda091a3b3c0328ac1fbe1d9
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 ;; 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 (template . org-org-template)
91 (paragraph . org-org-identity)
92 (plain-list . org-org-identity)
93 (planning . org-org-identity)
94 (property-drawer . org-org-identity)
95 (quote-block . org-org-identity)
96 (quote-section . org-org-identity)
97 (radio-target . org-org-identity)
98 (section . org-org-identity)
99 (special-block . org-org-identity)
100 (src-block . org-org-identity)
101 (statistics-cookie . org-org-identity)
102 (strike-through . org-org-identity)
103 (subscript . org-org-identity)
104 (superscript . org-org-identity)
105 (table . org-org-identity)
106 (table-cell . org-org-identity)
107 (table-row . org-org-identity)
108 (target . org-org-identity)
109 (timestamp . org-org-identity)
110 (underline . org-org-identity)
111 (verbatim . org-org-identity)
112 (verse-block . org-org-identity))
113 :menu-entry
114 '(?O "Export to Org"
115 ((?O "As Org buffer" org-org-export-as-org)
116 (?o "As Org file" org-org-export-to-org)
117 (?v "As Org file and open"
118 (lambda (a s v b)
119 (if a (org-org-export-to-org t s v b)
120 (org-open-file (org-org-export-to-org nil s v b))))))))
122 (defun org-org-identity (blob contents info)
123 "Transcode BLOB element or object back into Org syntax.
124 CONTENTS is its contents, as a string or nil. INFO is ignored."
125 (org-export-expand blob contents t))
127 (defun org-org-headline (headline contents info)
128 "Transcode HEADLINE element back into Org syntax.
129 CONTENTS is its contents, as a string or nil. INFO is ignored."
130 (unless (plist-get info :with-todo-keywords)
131 (org-element-put-property headline :todo-keyword nil))
132 (unless (plist-get info :with-tags)
133 (org-element-put-property headline :tags nil))
134 (unless (plist-get info :with-priority)
135 (org-element-put-property headline :priority nil))
136 (org-element-put-property headline :level
137 (org-export-get-relative-level headline info))
138 (org-element-headline-interpreter headline contents))
140 (defun org-org-keyword (keyword contents info)
141 "Transcode KEYWORD element back into Org syntax.
142 CONTENTS is nil. INFO is ignored. This function ignores
143 keywords targeted at other export back-ends."
144 (let ((key (org-element-property :key keyword)))
145 (unless (or (member key
146 (mapcar
147 (lambda (block-cons)
148 (and (eq (cdr block-cons)
149 'org-element-export-block-parser)
150 (car block-cons)))
151 org-element-block-name-alist))
152 (member key
153 '("AUTHOR" "CREATOR" "DATE" "DESCRIPTION" "EMAIL"
154 "KEYWORDS" "TITLE")))
155 (org-element-keyword-interpreter keyword nil))))
157 (defun org-org-template (contents info)
158 "Return Org document template with document keywords.
159 CONTENTS is the transcoded contents string. INFO is a plist used
160 as a communication channel."
161 (concat
162 (and (plist-get info :time-stamp-file)
163 (format-time-string "# Created %Y-%m-%d %a %H:%M\n"))
164 (format "#+TITLE: %s\n" (org-export-data (plist-get info :title) info))
165 (and (plist-get info :with-date)
166 (let ((date (org-export-data (org-export-get-date info) info)))
167 (and (org-string-nw-p date)
168 (format "#+DATE: %s\n" date))))
169 (and (plist-get info :with-author)
170 (let ((author (org-export-data (plist-get info :author) info)))
171 (and (org-string-nw-p author)
172 (format "#+AUTHOR: %s\n" author))))
173 (and (plist-get info :with-email)
174 (let ((email (org-export-data (plist-get info :email) info)))
175 (and (org-string-nw-p email)
176 (format "#+EMAIL: %s\n" email))))
177 (and (eq (plist-get info :with-creator) t)
178 (org-string-nw-p (plist-get info :creator))
179 (format "#+CREATOR: %s\n" (plist-get info :creator)))
180 (and (org-string-nw-p (plist-get info :keywords))
181 (format "#+KEYWORDS: %s\n" (plist-get info :keywords)))
182 (and (org-string-nw-p (plist-get info :description))
183 (format "#+DESCRIPTION: %s\n" (plist-get info :description)))
184 contents
185 (and (eq (plist-get info :with-creator) 'comment)
186 (org-string-nw-p (plist-get info :creator))
187 (format "\n# %s\n" (plist-get info :creator)))))
189 ;;;###autoload
190 (defun org-org-export-as-org
191 (&optional async subtreep visible-only body-only ext-plist)
192 "Export current buffer to an Org buffer.
194 If narrowing is active in the current buffer, only export its
195 narrowed part.
197 If a region is active, export that region.
199 A non-nil optional argument ASYNC means the process should happen
200 asynchronously. The resulting buffer should be accessible
201 through 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.
207 When optional argument VISIBLE-ONLY is non-nil, don't export
208 contents of hidden elements.
210 When optional argument BODY-ONLY is non-nil, strip document
211 keywords from output.
213 EXT-PLIST, when provided, is a property list with external
214 parameters overriding Org default settings, but still inferior to
215 file-local settings.
217 Export is done in a buffer named \"*Org ORG Export*\", which will
218 be displayed when `org-export-show-temporary-export-buffer' is
219 non-nil."
220 (interactive)
221 (org-export-to-buffer 'org "*Org ORG Export*"
222 async subtreep visible-only body-only ext-plist (lambda () (org-mode))))
224 ;;;###autoload
225 (defun org-org-export-to-org
226 (&optional async subtreep visible-only body-only ext-plist)
227 "Export current buffer to an org file.
229 If narrowing is active in the current buffer, only export its
230 narrowed part.
232 If a region is active, export that region.
234 A non-nil optional argument ASYNC means the process should happen
235 asynchronously. The resulting file should be accessible through
236 the `org-export-stack' interface.
238 When optional argument SUBTREEP is non-nil, export the sub-tree
239 at point, extracting information from the headline properties
240 first.
242 When optional argument VISIBLE-ONLY is non-nil, don't export
243 contents of hidden elements.
245 When optional argument BODY-ONLY is non-nil, strip document
246 keywords from output.
248 EXT-PLIST, when provided, is a property list with external
249 parameters overriding Org default settings, but still inferior to
250 file-local settings.
252 Return output file name."
253 (interactive)
254 (let ((outfile (org-export-output-file-name ".org" subtreep)))
255 (org-export-to-file 'org outfile
256 async subtreep visible-only body-only ext-plist)))
258 ;;;###autoload
259 (defun org-org-publish-to-org (plist filename pub-dir)
260 "Publish an org file to org.
262 FILENAME is the filename of the Org file to be published. PLIST
263 is the property list for the given project. PUB-DIR is the
264 publishing directory.
266 Return output file name."
267 (org-publish-org-to 'org filename ".org" plist pub-dir)
268 (when (plist-get plist :htmlized-source)
269 (require 'htmlize)
270 (require 'ox-html)
271 (let* ((org-inhibit-startup t)
272 (htmlize-output-type 'css)
273 (html-ext (concat "." (or (plist-get plist :html-extension)
274 org-html-extension "html")))
275 (visitingp (find-buffer-visiting filename))
276 (work-buffer (or visitingp (find-file filename)))
277 newbuf)
278 (font-lock-fontify-buffer)
279 (show-all)
280 (org-show-block-all)
281 (setq newbuf (htmlize-buffer))
282 (with-current-buffer newbuf
283 (when org-org-htmlized-css-url
284 (goto-char (point-min))
285 (and (re-search-forward
286 "<style type=\"text/css\">[^\000]*?\n[ \t]*</style>.*" nil t)
287 (replace-match
288 (format
289 "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">"
290 org-org-htmlized-css-url) t t)))
291 (write-file (concat pub-dir (file-name-nondirectory filename) html-ext)))
292 (kill-buffer newbuf)
293 (unless visitingp (kill-buffer work-buffer)))
294 (set-buffer-modified-p nil)))
297 (provide 'ox-org)
299 ;; Local variables:
300 ;; generated-autoload-file: "org-loaddefs.el"
301 ;; End:
303 ;;; ox-org.el ends here