org.el (org-insert-heading): Fix behavior
[org-mode.git] / lisp / ox-org.el
blob3b2e55e2d459a62f469a2d3d5882786b96f600c8
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 ;;; 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 (comment . (lambda (&rest args) ""))
61 (comment-block . (lambda (&rest args) ""))
62 (diary-sexp . org-org-identity)
63 (drawer . org-org-identity)
64 (dynamic-block . org-org-identity)
65 (entity . org-org-identity)
66 (example-block . org-org-identity)
67 (fixed-width . org-org-identity)
68 (footnote-definition . org-org-identity)
69 (footnote-reference . org-org-identity)
70 (headline . org-org-headline)
71 (horizontal-rule . org-org-identity)
72 (inline-babel-call . org-org-identity)
73 (inline-src-block . org-org-identity)
74 (inlinetask . org-org-identity)
75 (italic . org-org-identity)
76 (item . org-org-identity)
77 (keyword . org-org-keyword)
78 (latex-environment . org-org-identity)
79 (latex-fragment . org-org-identity)
80 (line-break . org-org-identity)
81 (link . org-org-identity)
82 (node-property . org-org-identity)
83 (paragraph . org-org-identity)
84 (plain-list . org-org-identity)
85 (planning . org-org-identity)
86 (property-drawer . org-org-identity)
87 (quote-block . org-org-identity)
88 (quote-section . org-org-identity)
89 (radio-target . org-org-identity)
90 (section . org-org-identity)
91 (special-block . org-org-identity)
92 (src-block . org-org-identity)
93 (statistics-cookie . org-org-identity)
94 (strike-through . org-org-identity)
95 (subscript . org-org-identity)
96 (superscript . org-org-identity)
97 (table . org-org-identity)
98 (table-cell . org-org-identity)
99 (table-row . org-org-identity)
100 (target . org-org-identity)
101 (timestamp . org-org-identity)
102 (underline . org-org-identity)
103 (verbatim . org-org-identity)
104 (verse-block . org-org-identity))
105 :menu-entry
106 '(?O "Export to Org"
107 ((?O "As Org buffer" org-org-export-as-org)
108 (?o "As Org file" org-org-export-to-org)
109 (?v "As Org file and open"
110 (lambda (a s v b)
111 (if a (org-org-export-to-org t s v b)
112 (org-open-file (org-org-export-to-org nil s v b))))))))
114 (defun org-org-identity (blob contents info)
115 "Transcode BLOB element or object back into Org syntax.
116 CONTENTS is its contents, as a string or nil. INFO is ignored."
117 (let ((case-fold-search t))
118 (replace-regexp-in-string
119 "^[ \t]*#\\+ATTR_[-_A-Za-z0-9]+:\\(?: .*\\)?\n" ""
120 (org-export-expand blob contents t))))
122 (defun org-org-headline (headline contents info)
123 "Transcode HEADLINE element back into Org syntax.
124 CONTENTS is its contents, as a string or nil. INFO is ignored."
125 (unless (plist-get info :with-todo-keywords)
126 (org-element-put-property headline :todo-keyword nil))
127 (unless (plist-get info :with-tags)
128 (org-element-put-property headline :tags nil))
129 (unless (plist-get info :with-priority)
130 (org-element-put-property headline :priority nil))
131 (org-element-put-property headline :level
132 (org-export-get-relative-level headline info))
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 (org-export-to-buffer 'org "*Org ORG Export*"
176 async subtreep visible-only nil ext-plist (lambda () (org-mode))))
178 ;;;###autoload
179 (defun org-org-export-to-org (&optional async subtreep visible-only ext-plist)
180 "Export current buffer to an org file.
182 If narrowing is active in the current buffer, only export its
183 narrowed part.
185 If a region is active, export that region.
187 A non-nil optional argument ASYNC means the process should happen
188 asynchronously. The resulting file should be accessible through
189 the `org-export-stack' interface.
191 When optional argument SUBTREEP is non-nil, export the sub-tree
192 at point, extracting information from the headline properties
193 first.
195 When optional argument VISIBLE-ONLY is non-nil, don't export
196 contents of hidden elements.
198 EXT-PLIST, when provided, is a property list with external
199 parameters overriding Org default settings, but still inferior to
200 file-local settings.
202 Return output file name."
203 (interactive)
204 (let ((outfile (org-export-output-file-name ".org" subtreep)))
205 (org-export-to-file 'org outfile
206 async subtreep visible-only nil ext-plist)))
208 ;;;###autoload
209 (defun org-org-publish-to-org (plist filename pub-dir)
210 "Publish an org file to org.
212 FILENAME is the filename of the Org file to be published. PLIST
213 is the property list for the given project. PUB-DIR is the
214 publishing directory.
216 Return output file name."
217 (org-publish-org-to 'org filename ".org" plist pub-dir)
218 (when (plist-get plist :htmlized-source)
219 (require 'htmlize)
220 (require 'ox-html)
221 (let* ((org-inhibit-startup t)
222 (htmlize-output-type 'css)
223 (html-ext (concat "." (or (plist-get plist :html-extension)
224 org-html-extension "html")))
225 (visitingp (find-buffer-visiting filename))
226 (work-buffer (or visitingp (find-file filename)))
227 newbuf)
228 (font-lock-fontify-buffer)
229 (show-all)
230 (org-show-block-all)
231 (setq newbuf (htmlize-buffer))
232 (with-current-buffer newbuf
233 (when org-org-htmlized-css-url
234 (goto-char (point-min))
235 (and (re-search-forward
236 "<style type=\"text/css\">[^\000]*?\n[ \t]*</style>.*" nil t)
237 (replace-match
238 (format
239 "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">"
240 org-org-htmlized-css-url) t t)))
241 (write-file (concat pub-dir (file-name-nondirectory filename) html-ext)))
242 (kill-buffer newbuf)
243 (unless visitingp (kill-buffer work-buffer)))
244 (set-buffer-modified-p nil)))
247 (provide 'ox-org)
249 ;; Local variables:
250 ;; generated-autoload-file: "org-loaddefs.el"
251 ;; End:
253 ;;; ox-org.el ends here