Fix and standardize some copyright and license notices
[bpt/emacs.git] / lisp / org / ox-org.el
CommitLineData
271672fa
BG
1;;; ox-org.el --- Org Back-End for Org Export Engine
2
439d6b1c 3;; Copyright (C) 2013 Free Software Foundation, Inc.
271672fa
BG
4
5;; Author: Nicolas Goaziou <n.goaziou@gmail.com>
6;; Keywords: org, wp
7
439d6b1c
GM
8;; This file is part of GNU Emacs.
9
271672fa
BG
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.
14
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.
19
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/>.
22
23;;; Commentary:
24
25;; This library implements an Org back-end for Org exporter.
26;;
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.
30;;
31;; A publishing function is also provided: `org-org-publish-to-org'.
32
33;;; Code:
34(require 'ox)
35(declare-function htmlize-buffer "htmlize" (&optional buffer))
36
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"))
43
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.
48Normally when creating an htmlized version of an Org buffer,
49htmlize will create the CSS to define the font colors. However,
50this does not work when converting in batch mode, and it also can
51look bad if different people with different fontification setup
52work on the same website. When this variable is non-nil,
53creating an htmlized version of an Org buffer using
54`org-org-export-as-org' will include a link to this URL if the
55setting 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")))
60
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))))))))
120
121(defun org-org-identity (blob contents info)
122 "Transcode BLOB element or object back into Org syntax.
123CONTENTS is its contents, as a string or nil. INFO is ignored."
124 (org-export-expand blob contents t))
125
126(defun org-org-headline (headline contents info)
127 "Transcode HEADLINE element back into Org syntax.
128CONTENTS 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))
138
139(defun org-org-keyword (keyword contents info)
140 "Transcode KEYWORD element back into Org syntax.
141CONTENTS is nil. INFO is ignored. This function ignores
142keywords 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)))
150
151;;;###autoload
152(defun org-org-export-as-org (&optional async subtreep visible-only ext-plist)
153 "Export current buffer to an Org buffer.
154
155If narrowing is active in the current buffer, only export its
156narrowed part.
157
158If a region is active, export that region.
159
160A non-nil optional argument ASYNC means the process should happen
161asynchronously. The resulting buffer should be accessible
162through the `org-export-stack' interface.
163
164When optional argument SUBTREEP is non-nil, export the sub-tree
165at point, extracting information from the headline properties
166first.
167
168When optional argument VISIBLE-ONLY is non-nil, don't export
169contents of hidden elements.
170
171EXT-PLIST, when provided, is a property list with external
172parameters overriding Org default settings, but still inferior to
173file-local settings.
174
175Export is done in a buffer named \"*Org ORG Export*\", which will
176be displayed when `org-export-show-temporary-export-buffer' is
177non-nil."
178 (interactive)
179 (org-export-to-buffer 'org "*Org ORG Export*"
180 async subtreep visible-only nil ext-plist (lambda () (org-mode))))
181
182;;;###autoload
183(defun org-org-export-to-org (&optional async subtreep visible-only ext-plist)
184 "Export current buffer to an org file.
185
186If narrowing is active in the current buffer, only export its
187narrowed part.
188
189If a region is active, export that region.
190
191A non-nil optional argument ASYNC means the process should happen
192asynchronously. The resulting file should be accessible through
193the `org-export-stack' interface.
194
195When optional argument SUBTREEP is non-nil, export the sub-tree
196at point, extracting information from the headline properties
197first.
198
199When optional argument VISIBLE-ONLY is non-nil, don't export
200contents of hidden elements.
201
202EXT-PLIST, when provided, is a property list with external
203parameters overriding Org default settings, but still inferior to
204file-local settings.
205
206Return 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)))
211
212;;;###autoload
213(defun org-org-publish-to-org (plist filename pub-dir)
214 "Publish an org file to org.
215
216FILENAME is the filename of the Org file to be published. PLIST
217is the property list for the given project. PUB-DIR is the
218publishing directory.
219
220Return 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)))
249
250
251(provide 'ox-org)
252
253;; Local variables:
254;; generated-autoload-file: "org-loaddefs.el"
255;; End:
256
257;;; ox-org.el ends here