Commit | Line | Data |
---|---|---|
8223b1d2 | 1 | ;;; org-exp.el --- Export internals for Org-mode |
20908596 | 2 | |
ab422c4d | 3 | ;; Copyright (C) 2004-2013 Free Software Foundation, Inc. |
20908596 CD |
4 | |
5 | ;; Author: Carsten Dominik <carsten at orgmode dot org> | |
6 | ;; Keywords: outlines, hypermedia, calendar, wp | |
7 | ;; Homepage: http://orgmode.org | |
20908596 CD |
8 | ;; |
9 | ;; This file is part of GNU Emacs. | |
10 | ;; | |
b1fc2b50 | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
20908596 | 12 | ;; it under the terms of the GNU General Public License as published by |
b1fc2b50 GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
20908596 CD |
15 | |
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
b1fc2b50 | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
20908596 CD |
23 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
24 | ;; | |
25 | ;;; Commentary: | |
26 | ||
86fbb8ca CD |
27 | ;;; Code: |
28 | ||
20908596 | 29 | (require 'org) |
8d642074 | 30 | (require 'org-macs) |
20908596 | 31 | (require 'org-agenda) |
c8d0cf5c | 32 | (require 'org-exp-blocks) |
86fbb8ca | 33 | (require 'ob-exp) |
8bfe682a | 34 | (require 'org-src) |
86fbb8ca | 35 | |
54a0dee5 | 36 | (eval-when-compile |
20908596 CD |
37 | (require 'cl)) |
38 | ||
c8d0cf5c CD |
39 | (declare-function org-export-latex-preprocess "org-latex" (parameters)) |
40 | (declare-function org-export-ascii-preprocess "org-ascii" (parameters)) | |
41 | (declare-function org-export-html-preprocess "org-html" (parameters)) | |
42 | (declare-function org-export-docbook-preprocess "org-docbook" (parameters)) | |
1371348a | 43 | (declare-function org-infojs-options-inbuffer-template "org-jsinfo" ()) |
c8d0cf5c | 44 | (declare-function org-export-htmlize-region-for-paste "org-html" (beg end)) |
9d459fc5 | 45 | (declare-function htmlize-buffer "ext:htmlize" (&optional buffer)) |
8bfe682a | 46 | (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ()) |
86fbb8ca CD |
47 | (declare-function org-table-cookie-line-p "org-table" (line)) |
48 | (declare-function org-table-colgroup-line-p "org-table" (line)) | |
e66ba1df BG |
49 | (declare-function org-pop-to-buffer-same-window "org-compat" |
50 | (&optional buffer-or-name norecord label)) | |
bdebdb64 | 51 | (declare-function org-unescape-code-in-region "org-src" (beg end)) |
3ab2c837 | 52 | |
c8d0cf5c | 53 | (autoload 'org-export-generic "org-export-generic" "Export using the generic exporter" t) |
3ab2c837 BG |
54 | |
55 | (autoload 'org-export-as-odt "org-odt" | |
e66ba1df | 56 | "Export the outline to a OpenDocument Text file." t) |
3ab2c837 | 57 | (autoload 'org-export-as-odt-and-open "org-odt" |
e66ba1df | 58 | "Export the outline to a OpenDocument Text file and open it." t) |
3ab2c837 | 59 | |
20908596 CD |
60 | (defgroup org-export nil |
61 | "Options for exporting org-listings." | |
62 | :tag "Org Export" | |
63 | :group 'org) | |
64 | ||
65 | (defgroup org-export-general nil | |
66 | "General options for exporting Org-mode files." | |
67 | :tag "Org Export General" | |
68 | :group 'org-export) | |
69 | ||
c8d0cf5c | 70 | (defcustom org-export-allow-BIND 'confirm |
ed21c5c8 | 71 | "Non-nil means allow #+BIND to define local variable values for export. |
c8d0cf5c CD |
72 | This is a potential security risk, which is why the user must confirm the |
73 | use of these lines." | |
74 | :group 'org-export-general | |
75 | :type '(choice | |
76 | (const :tag "Never" nil) | |
77 | (const :tag "Always" t) | |
78 | (const :tag "Make the user confirm for each file" confirm))) | |
79 | ||
20908596 CD |
80 | ;; FIXME |
81 | (defvar org-export-publishing-directory nil) | |
82 | ||
c8d0cf5c | 83 | (defcustom org-export-show-temporary-export-buffer t |
ed21c5c8 | 84 | "Non-nil means show buffer after exporting to temp buffer. |
c8d0cf5c CD |
85 | When Org exports to a file, the buffer visiting that file is ever |
86 | shown, but remains buried. However, when exporting to a temporary | |
87 | buffer, that buffer is popped up in a second window. When this variable | |
88 | is nil, the buffer remains buried also in these cases." | |
89 | :group 'org-export-general | |
90 | :type 'boolean) | |
91 | ||
92 | (defcustom org-export-copy-to-kill-ring t | |
ed21c5c8 CD |
93 | "Non-nil means exported stuff will also be pushed onto the kill ring." |
94 | :group 'org-export-general | |
95 | :type 'boolean) | |
96 | ||
97 | (defcustom org-export-kill-product-buffer-when-displayed nil | |
98 | "Non-nil means kill the product buffer if it is displayed immediately. | |
3ab2c837 | 99 | This applied to the commands `org-export-as-html-and-open' and |
ed21c5c8 | 100 | `org-export-as-pdf-and-open'." |
c8d0cf5c | 101 | :group 'org-export-general |
372d7b21 | 102 | :version "24.1" |
c8d0cf5c CD |
103 | :type 'boolean) |
104 | ||
20908596 CD |
105 | (defcustom org-export-run-in-background nil |
106 | "Non-nil means export and publishing commands will run in background. | |
107 | This works by starting up a separate Emacs process visiting the same file | |
108 | and doing the export from there. | |
109 | Not all export commands are affected by this - only the ones which | |
110 | actually write to a file, and that do not depend on the buffer state. | |
86fbb8ca | 111 | \\<org-mode-map> |
20908596 | 112 | If this option is nil, you can still get background export by calling |
86fbb8ca CD |
113 | `org-export' with a double prefix arg: \ |
114 | \\[universal-argument] \\[universal-argument] \\[org-export]. | |
20908596 CD |
115 | |
116 | If this option is t, the double prefix can be used to exceptionally | |
117 | force an export command into the current process." | |
118 | :group 'org-export-general | |
119 | :type 'boolean) | |
120 | ||
3ab2c837 BG |
121 | (defcustom org-export-initial-scope 'buffer |
122 | "The initial scope when exporting with `org-export'. | |
123 | This variable can be either set to 'buffer or 'subtree." | |
124 | :group 'org-export-general | |
372d7b21 | 125 | :version "24.1" |
3ab2c837 BG |
126 | :type '(choice |
127 | (const :tag "Export current buffer" 'buffer) | |
128 | (const :tag "Export current subtree" 'subtree))) | |
129 | ||
621f83e4 CD |
130 | (defcustom org-export-select-tags '("export") |
131 | "Tags that select a tree for export. | |
132 | If any such tag is found in a buffer, all trees that do not carry one | |
133 | of these tags will be deleted before export. | |
134 | Inside trees that are selected like this, you can still deselect a | |
93b62de8 | 135 | subtree by tagging it with one of the `org-export-exclude-tags'." |
621f83e4 CD |
136 | :group 'org-export-general |
137 | :type '(repeat (string :tag "Tag"))) | |
138 | ||
139 | (defcustom org-export-exclude-tags '("noexport") | |
140 | "Tags that exclude a tree from export. | |
141 | All trees carrying any of these tags will be excluded from export. | |
33306645 | 142 | This is without condition, so even subtrees inside that carry one of the |
621f83e4 CD |
143 | `org-export-select-tags' will be removed." |
144 | :group 'org-export-general | |
145 | :type '(repeat (string :tag "Tag"))) | |
146 | ||
c8d0cf5c CD |
147 | ;; FIXME: rename, this is a general variable |
148 | (defcustom org-export-html-expand t | |
ed21c5c8 | 149 | "Non-nil means for HTML export, treat @<...> as HTML tag. |
c8d0cf5c CD |
150 | When nil, these tags will be exported as plain text and therefore |
151 | not be interpreted by a browser. | |
152 | ||
153 | This option can also be set with the +OPTIONS line, e.g. \"@:nil\"." | |
154 | :group 'org-export-html | |
155 | :group 'org-export-general | |
156 | :type 'boolean) | |
157 | ||
20908596 | 158 | (defcustom org-export-with-special-strings t |
ed21c5c8 | 159 | "Non-nil means interpret \"\-\", \"--\" and \"---\" for export. |
20908596 CD |
160 | When this option is turned on, these strings will be exported as: |
161 | ||
162 | Org HTML LaTeX | |
163 | -----+----------+-------- | |
164 | \\- ­ \\- | |
165 | -- – -- | |
166 | --- — --- | |
167 | ... … \ldots | |
168 | ||
169 | This option can also be set with the +OPTIONS line, e.g. \"-:nil\"." | |
170 | :group 'org-export-translation | |
171 | :type 'boolean) | |
172 | ||
c8d0cf5c CD |
173 | (defcustom org-export-html-link-up "" |
174 | "Where should the \"UP\" link of exported HTML pages lead?" | |
175 | :group 'org-export-html | |
176 | :group 'org-export-general | |
177 | :type '(string :tag "File or URL")) | |
178 | ||
179 | (defcustom org-export-html-link-home "" | |
180 | "Where should the \"HOME\" link of exported HTML pages lead?" | |
181 | :group 'org-export-html | |
182 | :group 'org-export-general | |
183 | :type '(string :tag "File or URL")) | |
184 | ||
20908596 | 185 | (defcustom org-export-language-setup |
93b62de8 CD |
186 | '(("en" "Author" "Date" "Table of Contents" "Footnotes") |
187 | ("ca" "Autor" "Data" "Índex" "Peus de pàgina") | |
188 | ("cs" "Autor" "Datum" "Obsah" "Pozn\xe1mky pod carou") | |
189 | ("da" "Ophavsmand" "Dato" "Indhold" "Fodnoter") | |
190 | ("de" "Autor" "Datum" "Inhaltsverzeichnis" "Fußnoten") | |
191 | ("eo" "Aŭtoro" "Dato" "Enhavo" "Piednotoj") | |
192 | ("es" "Autor" "Fecha" "Índice" "Pies de página") | |
193 | ("fi" "Tekijä" "Päivämäärä" "Sisällysluettelo" "Alaviitteet") | |
8223b1d2 | 194 | ("fr" "Auteur" "Date" "Sommaire" "Notes de bas de page") |
93b62de8 CD |
195 | ("hu" "Szerzõ" "Dátum" "Tartalomjegyzék" "Lábjegyzet") |
196 | ("is" "Höfundur" "Dagsetning" "Efnisyfirlit" "Aftanmálsgreinar") | |
197 | ("it" "Autore" "Data" "Indice" "Note a piè di pagina") | |
8223b1d2 BG |
198 | ;; Use numeric character entities for proper rendering of non-UTF8 documents |
199 | ;; ("ja" "著者" "日付" "目次" "脚注") | |
200 | ("ja" "著者" "日付" "目次" "脚注") | |
93b62de8 CD |
201 | ("nl" "Auteur" "Datum" "Inhoudsopgave" "Voetnoten") |
202 | ("no" "Forfatter" "Dato" "Innhold" "Fotnoter") | |
203 | ("nb" "Forfatter" "Dato" "Innhold" "Fotnoter") ;; nb = Norsk (bokm.l) | |
204 | ("nn" "Forfattar" "Dato" "Innhald" "Fotnotar") ;; nn = Norsk (nynorsk) | |
afe98dfa | 205 | ("pl" "Autor" "Data" "Spis treści" "Przypis") |
8223b1d2 BG |
206 | ;; Use numeric character entities for proper rendering of non-UTF8 documents |
207 | ;; ("ru" "Автор" "Дата" "Содержание" "Сноски") | |
208 | ("ru" "Автор" "Дата" "Содержание" "Сноски") | |
209 | ("sv" "Författare" "Datum" "Innehåll" "Fotnoter") | |
210 | ;; Use numeric character entities for proper rendering of non-UTF8 documents | |
211 | ;; ("uk" "Автор" "Дата" "Зміст" "Примітки") | |
212 | ("uk" "Автор" "Дата" "Зміст" "Примітки") | |
213 | ;; Use numeric character entities for proper rendering of non-UTF8 documents | |
214 | ;; ("zh-CN" "作者" "日期" "目录" "脚注") | |
215 | ("zh-CN" "作者" "日期" "目录" "脚注") | |
216 | ;; Use numeric character entities for proper rendering of non-UTF8 documents | |
217 | ;; ("zh-TW" "作者" "日期" "目錄" "腳註") | |
218 | ("zh-TW" "作者" "日期" "目錄" "腳註")) | |
20908596 CD |
219 | "Terms used in export text, translated to different languages. |
220 | Use the variable `org-export-default-language' to set the language, | |
221 | or use the +OPTION lines for a per-file setting." | |
222 | :group 'org-export-general | |
223 | :type '(repeat | |
224 | (list | |
225 | (string :tag "HTML language tag") | |
226 | (string :tag "Author") | |
227 | (string :tag "Date") | |
93b62de8 CD |
228 | (string :tag "Table of Contents") |
229 | (string :tag "Footnotes")))) | |
20908596 CD |
230 | |
231 | (defcustom org-export-default-language "en" | |
3ab2c837 BG |
232 | "The default language for export and clocktable translations, as a string. |
233 | This should have an association in `org-export-language-setup' | |
234 | and in `org-clock-clocktable-language-setup'." | |
20908596 CD |
235 | :group 'org-export-general |
236 | :type 'string) | |
237 | ||
e66ba1df BG |
238 | (defcustom org-export-date-timestamp-format "%Y-%m-%d" |
239 | "Time string format for Org timestamps in the #+DATE option." | |
240 | :group 'org-export-general | |
372d7b21 | 241 | :version "24.1" |
e66ba1df BG |
242 | :type 'string) |
243 | ||
c8d0cf5c CD |
244 | (defvar org-export-page-description "" |
245 | "The page description, for the XHTML meta tag. | |
246 | This is best set with the #+DESCRIPTION line in a file, it does not make | |
247 | sense to set this globally.") | |
248 | ||
249 | (defvar org-export-page-keywords "" | |
250 | "The page description, for the XHTML meta tag. | |
251 | This is best set with the #+KEYWORDS line in a file, it does not make | |
252 | sense to set this globally.") | |
253 | ||
b349f79f | 254 | (defcustom org-export-skip-text-before-1st-heading nil |
ed21c5c8 | 255 | "Non-nil means skip all text before the first headline when exporting. |
20908596 CD |
256 | When nil, that text is exported as well." |
257 | :group 'org-export-general | |
258 | :type 'boolean) | |
259 | ||
260 | (defcustom org-export-headline-levels 3 | |
261 | "The last level which is still exported as a headline. | |
262 | Inferior levels will produce itemize lists when exported. | |
263 | Note that a numeric prefix argument to an exporter function overrides | |
264 | this setting. | |
265 | ||
266 | This option can also be set with the +OPTIONS line, e.g. \"H:2\"." | |
267 | :group 'org-export-general | |
c8d0cf5c | 268 | :type 'integer) |
20908596 CD |
269 | |
270 | (defcustom org-export-with-section-numbers t | |
ed21c5c8 | 271 | "Non-nil means add section numbers to headlines when exporting. |
20908596 CD |
272 | |
273 | This option can also be set with the +OPTIONS line, e.g. \"num:t\"." | |
274 | :group 'org-export-general | |
275 | :type 'boolean) | |
276 | ||
b349f79f CD |
277 | (defcustom org-export-section-number-format '((("1" ".")) . "") |
278 | "Format of section numbers for export. | |
279 | The variable has two components. | |
280 | 1. A list of lists, each indicating a counter type and a separator. | |
86fbb8ca | 281 | The counter type can be any of \"1\", \"A\", \"a\", \"I\", or \"i\". |
b349f79f CD |
282 | It causes causes numeric, alphabetic, or roman counters, respectively. |
283 | The separator is only used if another counter for a subsection is being | |
284 | added. | |
285 | If there are more numbered section levels than entries in this lists, | |
286 | then the last entry will be reused. | |
287 | 2. A terminator string that will be added after the entire | |
288 | section number." | |
289 | :group 'org-export-general | |
290 | :type '(cons | |
291 | (repeat | |
292 | (list | |
293 | (string :tag "Counter Type") | |
294 | (string :tag "Separator "))) | |
295 | (string :tag "Terminator"))) | |
296 | ||
20908596 | 297 | (defcustom org-export-with-toc t |
ed21c5c8 | 298 | "Non-nil means create a table of contents in exported files. |
20908596 CD |
299 | The TOC contains headlines with levels up to`org-export-headline-levels'. |
300 | When an integer, include levels up to N in the toc, this may then be | |
301 | different from `org-export-headline-levels', but it will not be allowed | |
302 | to be larger than the number of headline levels. | |
303 | When nil, no table of contents is made. | |
304 | ||
305 | Headlines which contain any TODO items will be marked with \"(*)\" in | |
306 | ASCII export, and with red color in HTML output, if the option | |
307 | `org-export-mark-todo-in-toc' is set. | |
308 | ||
309 | In HTML output, the TOC will be clickable. | |
310 | ||
311 | This option can also be set with the +OPTIONS line, e.g. \"toc:nil\" | |
312 | or \"toc:3\"." | |
313 | :group 'org-export-general | |
314 | :type '(choice | |
315 | (const :tag "No Table of Contents" nil) | |
316 | (const :tag "Full Table of Contents" t) | |
317 | (integer :tag "TOC to level"))) | |
318 | ||
319 | (defcustom org-export-mark-todo-in-toc nil | |
ed21c5c8 | 320 | "Non-nil means mark TOC lines that contain any open TODO items." |
20908596 CD |
321 | :group 'org-export-general |
322 | :type 'boolean) | |
323 | ||
ff4be292 | 324 | (defcustom org-export-with-todo-keywords t |
ed21c5c8 | 325 | "Non-nil means include TODO keywords in export. |
ff4be292 CD |
326 | When nil, remove all these keywords from the export." |
327 | :group 'org-export-general | |
328 | :type 'boolean) | |
329 | ||
3ab2c837 BG |
330 | (defcustom org-export-with-tasks t |
331 | "Non-nil means include TODO items for export. | |
332 | This may have the following values: | |
333 | t include tasks independent of state. | |
334 | todo include only tasks that are not yet done. | |
335 | done include only tasks that are already done. | |
336 | nil remove all tasks before export | |
337 | list of TODO kwds keep only tasks with these keywords" | |
338 | :group 'org-export-general | |
372d7b21 | 339 | :version "24.1" |
3ab2c837 BG |
340 | :type '(choice |
341 | (const :tag "All tasks" t) | |
342 | (const :tag "No tasks" nil) | |
343 | (const :tag "Not-done tasks" todo) | |
344 | (const :tag "Only done tasks" done) | |
345 | (repeat :tag "Specific TODO keywords" | |
346 | (string :tag "Keyword")))) | |
347 | ||
ff4be292 | 348 | (defcustom org-export-with-priority nil |
ed21c5c8 | 349 | "Non-nil means include priority cookies in export. |
ff4be292 CD |
350 | When nil, remove priority cookies for export." |
351 | :group 'org-export-general | |
352 | :type 'boolean) | |
353 | ||
20908596 | 354 | (defcustom org-export-preserve-breaks nil |
ed21c5c8 | 355 | "Non-nil means preserve all line breaks when exporting. |
20908596 CD |
356 | Normally, in HTML output paragraphs will be reformatted. In ASCII |
357 | export, line breaks will always be preserved, regardless of this variable. | |
358 | ||
359 | This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"." | |
360 | :group 'org-export-general | |
361 | :type 'boolean) | |
362 | ||
363 | (defcustom org-export-with-archived-trees 'headline | |
364 | "Whether subtrees with the ARCHIVE tag should be exported. | |
365 | This can have three different values | |
366 | nil Do not export, pretend this tree is not present | |
367 | t Do export the entire tree | |
368 | headline Only export the headline, but skip the tree below it." | |
369 | :group 'org-export-general | |
370 | :group 'org-archive | |
371 | :type '(choice | |
372 | (const :tag "not at all" nil) | |
373 | (const :tag "headline only" 'headline) | |
374 | (const :tag "entirely" t))) | |
375 | ||
376 | (defcustom org-export-author-info t | |
ed21c5c8 CD |
377 | "Non-nil means insert author name and email into the exported file. |
378 | ||
379 | This option can also be set with the +OPTIONS line, | |
380 | e.g. \"author:nil\"." | |
381 | :group 'org-export-general | |
382 | :type 'boolean) | |
383 | ||
384 | (defcustom org-export-email-info nil | |
385 | "Non-nil means insert author name and email into the exported file. | |
20908596 CD |
386 | |
387 | This option can also be set with the +OPTIONS line, | |
ed21c5c8 | 388 | e.g. \"email:t\"." |
20908596 | 389 | :group 'org-export-general |
372d7b21 | 390 | :version "24.1" |
20908596 CD |
391 | :type 'boolean) |
392 | ||
2c3ad40d | 393 | (defcustom org-export-creator-info t |
ed21c5c8 | 394 | "Non-nil means the postamble should contain a creator sentence. |
2c3ad40d CD |
395 | This sentence is \"HTML generated by org-mode XX in emacs XXX\"." |
396 | :group 'org-export-general | |
397 | :type 'boolean) | |
398 | ||
20908596 | 399 | (defcustom org-export-time-stamp-file t |
ed21c5c8 | 400 | "Non-nil means insert a time stamp into the exported file. |
20908596 CD |
401 | The time stamp shows when the file was created. |
402 | ||
403 | This option can also be set with the +OPTIONS line, | |
404 | e.g. \"timestamp:nil\"." | |
405 | :group 'org-export-general | |
406 | :type 'boolean) | |
407 | ||
408 | (defcustom org-export-with-timestamps t | |
409 | "If nil, do not export time stamps and associated keywords." | |
410 | :group 'org-export-general | |
411 | :type 'boolean) | |
412 | ||
413 | (defcustom org-export-remove-timestamps-from-toc t | |
c8d0cf5c | 414 | "If t, remove timestamps from the table of contents entries." |
20908596 CD |
415 | :group 'org-export-general |
416 | :type 'boolean) | |
417 | ||
418 | (defcustom org-export-with-tags 'not-in-toc | |
419 | "If nil, do not export tags, just remove them from headlines. | |
420 | If this is the symbol `not-in-toc', tags will be removed from table of | |
421 | contents entries, but still be shown in the headlines of the document. | |
422 | ||
423 | This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"." | |
424 | :group 'org-export-general | |
425 | :type '(choice | |
426 | (const :tag "Off" nil) | |
427 | (const :tag "Not in TOC" not-in-toc) | |
428 | (const :tag "On" t))) | |
429 | ||
430 | (defcustom org-export-with-drawers nil | |
ed21c5c8 | 431 | "Non-nil means export with drawers like the property drawer. |
20908596 CD |
432 | When t, all drawers are exported. This may also be a list of |
433 | drawer names to export." | |
434 | :group 'org-export-general | |
435 | :type '(choice | |
436 | (const :tag "All drawers" t) | |
437 | (const :tag "None" nil) | |
438 | (repeat :tag "Selected drawers" | |
439 | (string :tag "Drawer name")))) | |
440 | ||
ed21c5c8 CD |
441 | (defvar org-export-first-hook nil |
442 | "Hook called as the first thing in each exporter. | |
443 | Point will be still in the original buffer. | |
444 | Good for general initialization") | |
445 | ||
b349f79f CD |
446 | (defvar org-export-preprocess-hook nil |
447 | "Hook for preprocessing an export buffer. | |
ed21c5c8 CD |
448 | Pretty much the first thing when exporting is running this hook. |
449 | Point will be in a temporary buffer that contains a copy of | |
3ab2c837 | 450 | the original buffer, or of the section that is being exported. |
ed21c5c8 CD |
451 | All the other hooks in the org-export-preprocess... category |
452 | also work in that temporary buffer, already modified by various | |
453 | stages of the processing.") | |
b349f79f | 454 | |
0bd48b37 CD |
455 | (defvar org-export-preprocess-after-include-files-hook nil |
456 | "Hook for preprocessing an export buffer. | |
457 | This is run after the contents of included files have been inserted.") | |
458 | ||
459 | (defvar org-export-preprocess-after-tree-selection-hook nil | |
460 | "Hook for preprocessing an export buffer. | |
461 | This is run after selection of trees to be exported has happened. | |
462 | This selection includes tags-based selection, as well as removal | |
463 | of commented and archived trees.") | |
464 | ||
ed21c5c8 CD |
465 | (defvar org-export-preprocess-after-headline-targets-hook nil |
466 | "Hook for preprocessing export buffer. | |
467 | This is run just after the headline targets have been defined and | |
468 | the target-alist has been set up.") | |
469 | ||
470 | (defvar org-export-preprocess-before-selecting-backend-code-hook nil | |
471 | "Hook for preprocessing an export buffer. | |
472 | This is run just before backend-specific blocks get selected.") | |
473 | ||
c8d0cf5c CD |
474 | (defvar org-export-preprocess-after-blockquote-hook nil |
475 | "Hook for preprocessing an export buffer. | |
476 | This is run after blockquote/quote/verse/center have been marked | |
477 | with cookies.") | |
478 | ||
afe98dfa CD |
479 | (defvar org-export-preprocess-after-radio-targets-hook nil |
480 | "Hook for preprocessing an export buffer. | |
481 | This is run after radio target processing.") | |
482 | ||
ed21c5c8 CD |
483 | (defvar org-export-preprocess-before-normalizing-links-hook nil |
484 | "Hook for preprocessing an export buffer. | |
485 | This hook is run before links are normalized.") | |
486 | ||
0bd48b37 CD |
487 | (defvar org-export-preprocess-before-backend-specifics-hook nil |
488 | "Hook run before backend-specific functions are called during preprocessing.") | |
489 | ||
490 | (defvar org-export-preprocess-final-hook nil | |
491 | "Hook for preprocessing an export buffer. | |
492 | This is run as the last thing in the preprocessing buffer, just before | |
493 | returning the buffer string to the backend.") | |
494 | ||
20908596 CD |
495 | (defgroup org-export-translation nil |
496 | "Options for translating special ascii sequences for the export backends." | |
497 | :tag "Org Export Translation" | |
498 | :group 'org-export) | |
499 | ||
500 | (defcustom org-export-with-emphasize t | |
ed21c5c8 | 501 | "Non-nil means interpret *word*, /word/, and _word_ as emphasized text. |
20908596 CD |
502 | If the export target supports emphasizing text, the word will be |
503 | typeset in bold, italic, or underlined, respectively. Works only for | |
504 | single words, but you can say: I *really* *mean* *this*. | |
505 | Not all export backends support this. | |
506 | ||
507 | This option can also be set with the +OPTIONS line, e.g. \"*:nil\"." | |
508 | :group 'org-export-translation | |
509 | :type 'boolean) | |
510 | ||
511 | (defcustom org-export-with-footnotes t | |
512 | "If nil, export [1] as a footnote marker. | |
513 | Lines starting with [1] will be formatted as footnotes. | |
514 | ||
515 | This option can also be set with the +OPTIONS line, e.g. \"f:nil\"." | |
516 | :group 'org-export-translation | |
517 | :type 'boolean) | |
518 | ||
20908596 | 519 | (defcustom org-export-with-TeX-macros t |
ed21c5c8 | 520 | "Non-nil means interpret simple TeX-like macros when exporting. |
20908596 | 521 | For example, HTML export converts \\alpha to α and \\AA to Å. |
0bd48b37 | 522 | Not only real TeX macros will work here, but the standard HTML entities |
20908596 | 523 | for math can be used as macro names as well. For a list of supported |
ed21c5c8 CD |
524 | names in HTML export, see the constant `org-entities' and the user option |
525 | `org-entities-user'. | |
20908596 CD |
526 | Not all export backends support this. |
527 | ||
528 | This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"." | |
529 | :group 'org-export-translation | |
530 | :group 'org-export-latex | |
531 | :type 'boolean) | |
532 | ||
afe98dfa CD |
533 | (defcustom org-export-with-LaTeX-fragments t |
534 | "Non-nil means process LaTeX math fragments for HTML display. | |
535 | When set, the exporter will find and process LaTeX environments if the | |
536 | \\begin line is the first non-white thing on a line. It will also find | |
3ab2c837 BG |
537 | and process the math delimiters like $a=b$ and \\( a=b \\) for inline math, |
538 | $$a=b$$ and \\=\\[ a=b \\] for display math. | |
afe98dfa CD |
539 | |
540 | This option can also be set with the +OPTIONS line, e.g. \"LaTeX:mathjax\". | |
20908596 | 541 | |
afe98dfa CD |
542 | Allowed values are: |
543 | ||
8223b1d2 BG |
544 | nil Don't do anything. |
545 | verbatim Keep everything in verbatim | |
546 | dvipng Process the LaTeX fragments to images. | |
547 | This will also include processing of non-math environments. | |
548 | imagemagick Convert the LaTeX fragments to pdf files and use imagemagick | |
549 | to convert pdf files to png files. | |
550 | t Do MathJax preprocessing if there is at least on math snippet, | |
551 | and arrange for MathJax.js to be loaded. | |
c8d0cf5c CD |
552 | |
553 | The default is nil, because this option needs the `dvipng' program which | |
554 | is not available on all systems." | |
20908596 CD |
555 | :group 'org-export-translation |
556 | :group 'org-export-latex | |
afe98dfa CD |
557 | :type '(choice |
558 | (const :tag "Do not process math in any way" nil) | |
559 | (const :tag "Obsolete, use dvipng setting" t) | |
560 | (const :tag "Use dvipng to make images" dvipng) | |
8223b1d2 | 561 | (const :tag "Use imagemagick to make images" imagemagick) |
afe98dfa CD |
562 | (const :tag "Use MathJax to display math" mathjax) |
563 | (const :tag "Leave math verbatim" verbatim))) | |
20908596 CD |
564 | |
565 | (defcustom org-export-with-fixed-width t | |
ed21c5c8 | 566 | "Non-nil means lines starting with \":\" will be in fixed width font. |
20908596 CD |
567 | This can be used to have pre-formatted text, fragments of code etc. For |
568 | example: | |
569 | : ;; Some Lisp examples | |
570 | : (while (defc cnt) | |
571 | : (ding)) | |
572 | will be looking just like this in also HTML. See also the QUOTE keyword. | |
573 | Not all export backends support this. | |
574 | ||
575 | This option can also be set with the +OPTIONS line, e.g. \"::nil\"." | |
576 | :group 'org-export-translation | |
577 | :type 'boolean) | |
578 | ||
20908596 CD |
579 | (defgroup org-export-tables nil |
580 | "Options for exporting tables in Org-mode." | |
581 | :tag "Org Export Tables" | |
582 | :group 'org-export) | |
583 | ||
584 | (defcustom org-export-with-tables t | |
585 | "If non-nil, lines starting with \"|\" define a table. | |
586 | For example: | |
587 | ||
588 | | Name | Address | Birthday | | |
589 | |-------------+----------+-----------| | |
590 | | Arthur Dent | England | 29.2.2100 | | |
591 | ||
592 | Not all export backends support this. | |
593 | ||
594 | This option can also be set with the +OPTIONS line, e.g. \"|:nil\"." | |
595 | :group 'org-export-tables | |
596 | :type 'boolean) | |
597 | ||
598 | (defcustom org-export-highlight-first-table-line t | |
ed21c5c8 | 599 | "Non-nil means highlight the first table line. |
20908596 CD |
600 | In HTML export, this means use <th> instead of <td>. |
601 | In tables created with table.el, this applies to the first table line. | |
602 | In Org-mode tables, all lines before the first horizontal separator | |
603 | line will be formatted with <th> tags." | |
604 | :group 'org-export-tables | |
605 | :type 'boolean) | |
606 | ||
607 | (defcustom org-export-table-remove-special-lines t | |
608 | "Remove special lines and marking characters in calculating tables. | |
609 | This removes the special marking character column from tables that are set | |
610 | up for spreadsheet calculations. It also removes the entire lines | |
611 | marked with `!', `_', or `^'. The lines with `$' are kept, because | |
612 | the values of constants may be useful to have." | |
613 | :group 'org-export-tables | |
614 | :type 'boolean) | |
615 | ||
3ab2c837 BG |
616 | (defcustom org-export-table-remove-empty-lines t |
617 | "Remove empty lines when exporting tables. | |
618 | This is the global equivalent of the :remove-nil-lines option | |
619 | when locally sending a table with #+ORGTBL." | |
620 | :group 'org-export-tables | |
372d7b21 | 621 | :version "24.1" |
3ab2c837 BG |
622 | :type 'boolean) |
623 | ||
20908596 | 624 | (defcustom org-export-prefer-native-exporter-for-tables nil |
ed21c5c8 CD |
625 | "Non-nil means always export tables created with table.el natively. |
626 | Natively means use the HTML code generator in table.el. | |
20908596 CD |
627 | When nil, Org-mode's own HTML generator is used when possible (i.e. if |
628 | the table does not use row- or column-spanning). This has the | |
629 | advantage, that the automatic HTML conversions for math symbols and | |
630 | sub/superscripts can be applied. Org-mode's HTML generator is also | |
ed21c5c8 CD |
631 | much faster. The LaTeX exporter always use the native exporter for |
632 | table.el tables." | |
20908596 CD |
633 | :group 'org-export-tables |
634 | :type 'boolean) | |
635 | ||
20908596 CD |
636 | ;;;; Exporting |
637 | ||
638 | ;;; Variables, constants, and parameter plists | |
639 | ||
640 | (defconst org-level-max 20) | |
641 | ||
3ab2c837 BG |
642 | (defvar org-export-current-backend nil |
643 | "During export, this will be bound to a symbol such as 'html, | |
644 | 'latex, 'docbook, 'ascii, etc, indicating which of the export | |
8223b1d2 | 645 | backends is in use. Otherwise it has the value nil. Users |
3ab2c837 BG |
646 | should not attempt to change the value of this variable |
647 | directly, but it can be used in code to test whether export is | |
648 | in progress, and if so, what the backend is.") | |
649 | ||
20908596 CD |
650 | (defvar org-current-export-file nil) ; dynamically scoped parameter |
651 | (defvar org-current-export-dir nil) ; dynamically scoped parameter | |
c8d0cf5c CD |
652 | (defvar org-export-opt-plist nil |
653 | "Contains the current option plist.") | |
654 | (defvar org-last-level nil) ; dynamically scoped variable | |
655 | (defvar org-min-level nil) ; dynamically scoped variable | |
656 | (defvar org-levels-open nil) ; dynamically scoped parameter | |
3ab2c837 BG |
657 | (defvar org-export-footnotes-data nil |
658 | "Alist of labels used in buffers, along with their definition.") | |
659 | (defvar org-export-footnotes-seen nil | |
660 | "Alist of labels encountered so far by the exporter, along with their definition.") | |
661 | ||
20908596 CD |
662 | |
663 | (defconst org-export-plist-vars | |
33306645 CD |
664 | '((:link-up nil org-export-html-link-up) |
665 | (:link-home nil org-export-html-link-home) | |
666 | (:language nil org-export-default-language) | |
c8d0cf5c CD |
667 | (:keywords nil org-export-page-keywords) |
668 | (:description nil org-export-page-description) | |
33306645 CD |
669 | (:customtime nil org-display-custom-times) |
670 | (:headline-levels "H" org-export-headline-levels) | |
671 | (:section-numbers "num" org-export-with-section-numbers) | |
672 | (:section-number-format nil org-export-section-number-format) | |
673 | (:table-of-contents "toc" org-export-with-toc) | |
674 | (:preserve-breaks "\\n" org-export-preserve-breaks) | |
675 | (:archived-trees nil org-export-with-archived-trees) | |
676 | (:emphasize "*" org-export-with-emphasize) | |
677 | (:sub-superscript "^" org-export-with-sub-superscripts) | |
678 | (:special-strings "-" org-export-with-special-strings) | |
679 | (:footnotes "f" org-export-with-footnotes) | |
680 | (:drawers "d" org-export-with-drawers) | |
681 | (:tags "tags" org-export-with-tags) | |
682 | (:todo-keywords "todo" org-export-with-todo-keywords) | |
3ab2c837 | 683 | (:tasks "tasks" org-export-with-tasks) |
33306645 CD |
684 | (:priority "pri" org-export-with-priority) |
685 | (:TeX-macros "TeX" org-export-with-TeX-macros) | |
686 | (:LaTeX-fragments "LaTeX" org-export-with-LaTeX-fragments) | |
54a0dee5 | 687 | (:latex-listings nil org-export-latex-listings) |
33306645 CD |
688 | (:skip-before-1st-heading "skip" org-export-skip-text-before-1st-heading) |
689 | (:fixed-width ":" org-export-with-fixed-width) | |
690 | (:timestamps "<" org-export-with-timestamps) | |
3ab2c837 BG |
691 | (:author nil user-full-name) |
692 | (:email nil user-mail-address) | |
33306645 | 693 | (:author-info "author" org-export-author-info) |
ed21c5c8 | 694 | (:email-info "email" org-export-email-info) |
33306645 CD |
695 | (:creator-info "creator" org-export-creator-info) |
696 | (:time-stamp-file "timestamp" org-export-time-stamp-file) | |
697 | (:tables "|" org-export-with-tables) | |
698 | (:table-auto-headline nil org-export-highlight-first-table-line) | |
699 | (:style-include-default nil org-export-html-style-include-default) | |
c8d0cf5c | 700 | (:style-include-scripts nil org-export-html-style-include-scripts) |
33306645 CD |
701 | (:style nil org-export-html-style) |
702 | (:style-extra nil org-export-html-style-extra) | |
703 | (:agenda-style nil org-agenda-export-html-style) | |
704 | (:convert-org-links nil org-export-html-link-org-files-as-html) | |
705 | (:inline-images nil org-export-html-inline-images) | |
706 | (:html-extension nil org-export-html-extension) | |
3ab2c837 BG |
707 | (:html-preamble nil org-export-html-preamble) |
708 | (:html-postamble nil org-export-html-postamble) | |
c8d0cf5c | 709 | (:xml-declaration nil org-export-html-xml-declaration) |
33306645 CD |
710 | (:html-table-tag nil org-export-html-table-tag) |
711 | (:expand-quoted-html "@" org-export-html-expand) | |
712 | (:timestamp nil org-export-html-with-timestamp) | |
713 | (:publishing-directory nil org-export-publishing-directory) | |
33306645 | 714 | (:select-tags nil org-export-select-tags) |
c8d0cf5c CD |
715 | (:exclude-tags nil org-export-exclude-tags) |
716 | ||
717 | (:latex-image-options nil org-export-latex-image-default-option)) | |
ff4be292 CD |
718 | "List of properties that represent export/publishing variables. |
719 | Each element is a list of 3 items: | |
720 | 1. The property that is used internally, and also for org-publish-project-alist | |
721 | 2. The string that can be used in the OPTION lines to set this option, | |
722 | or nil if this option cannot be changed in this way | |
723 | 3. The customization variable that sets the default for this option." | |
8223b1d2 | 724 | ) |
20908596 CD |
725 | |
726 | (defun org-default-export-plist () | |
727 | "Return the property list with default settings for the export variables." | |
c8d0cf5c CD |
728 | (let* ((infile (org-infile-export-plist)) |
729 | (letbind (plist-get infile :let-bind)) | |
730 | (l org-export-plist-vars) rtn e s v) | |
20908596 | 731 | (while (setq e (pop l)) |
c8d0cf5c CD |
732 | (setq s (nth 2 e) |
733 | v (cond | |
734 | ((assq s letbind) (nth 1 (assq s letbind))) | |
8223b1d2 | 735 | ((boundp s) (symbol-value s))) |
c8d0cf5c | 736 | rtn (cons (car e) (cons v rtn)))) |
20908596 CD |
737 | rtn)) |
738 | ||
739 | (defvar org-export-inbuffer-options-extra nil | |
740 | "List of additional in-buffer options that should be detected. | |
741 | Just before export, the buffer is scanned for options like #+TITLE, #+EMAIL, | |
742 | etc. Extensions can add to this list to get their options detected, and they | |
743 | can then add a function to `org-export-options-filters' to process these | |
744 | options. | |
745 | Each element in this list must be a list, with the in-buffer keyword as car, | |
33306645 | 746 | and a property (a symbol) as the next element. All occurrences of the |
20908596 CD |
747 | keyword will be found, the values concatenated with a space character |
748 | in between, and the result stored in the export options property list.") | |
749 | ||
750 | (defvar org-export-options-filters nil | |
751 | "Functions to be called to finalize the export/publishing options. | |
752 | All these options are stored in a property list, and each of the functions | |
753 | in this hook gets a chance to modify this property list. Each function | |
754 | must accept the property list as an argument, and must return the (possibly | |
755 | modified) list.") | |
756 | ||
71d35b24 | 757 | ;; FIXME: should we fold case here? |
e66ba1df | 758 | |
20908596 CD |
759 | (defun org-infile-export-plist () |
760 | "Return the property list with file-local settings for export." | |
761 | (save-excursion | |
762 | (save-restriction | |
763 | (widen) | |
b349f79f | 764 | (goto-char (point-min)) |
20908596 CD |
765 | (let ((re (org-make-options-regexp |
766 | (append | |
767 | '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE" | |
afe98dfa | 768 | "MATHJAX" |
8d642074 | 769 | "LINK_UP" "LINK_HOME" "SETUPFILE" "STYLE" |
e66ba1df | 770 | "LATEX_HEADER" "LATEX_CLASS" "LATEX_CLASS_OPTIONS" |
c8d0cf5c | 771 | "EXPORT_SELECT_TAGS" "EXPORT_EXCLUDE_TAGS" |
86fbb8ca | 772 | "KEYWORDS" "DESCRIPTION" "MACRO" "BIND" "XSLT") |
20908596 | 773 | (mapcar 'car org-export-inbuffer-options-extra)))) |
afe98dfa CD |
774 | (case-fold-search t) |
775 | p key val text options mathjax a pr style | |
e66ba1df | 776 | latex-header latex-class latex-class-options macros letbind |
3ab2c837 | 777 | ext-setup-or-nil setup-file setup-dir setup-contents (start 0)) |
b349f79f CD |
778 | (while (or (and ext-setup-or-nil |
779 | (string-match re ext-setup-or-nil start) | |
780 | (setq start (match-end 0))) | |
781 | (and (setq ext-setup-or-nil nil start 0) | |
782 | (re-search-forward re nil t))) | |
783 | (setq key (upcase (org-match-string-no-properties 1 ext-setup-or-nil)) | |
784 | val (org-match-string-no-properties 2 ext-setup-or-nil)) | |
20908596 CD |
785 | (cond |
786 | ((setq a (assoc key org-export-inbuffer-options-extra)) | |
787 | (setq pr (nth 1 a)) | |
788 | (setq p (plist-put p pr (concat (plist-get p pr) " " val)))) | |
789 | ((string-equal key "TITLE") (setq p (plist-put p :title val))) | |
790 | ((string-equal key "AUTHOR")(setq p (plist-put p :author val))) | |
791 | ((string-equal key "EMAIL") (setq p (plist-put p :email val))) | |
e66ba1df BG |
792 | ((string-equal key "DATE") |
793 | ;; If date is an Org timestamp, convert it to a time | |
794 | ;; string using `org-export-date-timestamp-format' | |
795 | (when (string-match org-ts-regexp3 val) | |
796 | (setq val (format-time-string | |
797 | org-export-date-timestamp-format | |
798 | (apply 'encode-time (org-parse-time-string | |
799 | (match-string 0 val)))))) | |
800 | (setq p (plist-put p :date val))) | |
c8d0cf5c CD |
801 | ((string-equal key "KEYWORDS") (setq p (plist-put p :keywords val))) |
802 | ((string-equal key "DESCRIPTION") | |
803 | (setq p (plist-put p :description val))) | |
20908596 | 804 | ((string-equal key "LANGUAGE") (setq p (plist-put p :language val))) |
621f83e4 CD |
805 | ((string-equal key "STYLE") |
806 | (setq style (concat style "\n" val))) | |
807 | ((string-equal key "LATEX_HEADER") | |
808 | (setq latex-header (concat latex-header "\n" val))) | |
8d642074 CD |
809 | ((string-equal key "LATEX_CLASS") |
810 | (setq latex-class val)) | |
e66ba1df BG |
811 | ((string-equal key "LATEX_CLASS_OPTIONS") |
812 | (setq latex-class-options val)) | |
20908596 CD |
813 | ((string-equal key "TEXT") |
814 | (setq text (if text (concat text "\n" val) val))) | |
815 | ((string-equal key "OPTIONS") | |
b349f79f | 816 | (setq options (concat val " " options))) |
afe98dfa CD |
817 | ((string-equal key "MATHJAX") |
818 | (setq mathjax (concat val " " mathjax))) | |
c8d0cf5c CD |
819 | ((string-equal key "BIND") |
820 | (push (read (concat "(" val ")")) letbind)) | |
86fbb8ca CD |
821 | ((string-equal key "XSLT") |
822 | (setq p (plist-put p :xslt val))) | |
20908596 CD |
823 | ((string-equal key "LINK_UP") |
824 | (setq p (plist-put p :link-up val))) | |
825 | ((string-equal key "LINK_HOME") | |
b349f79f | 826 | (setq p (plist-put p :link-home val))) |
621f83e4 CD |
827 | ((string-equal key "EXPORT_SELECT_TAGS") |
828 | (setq p (plist-put p :select-tags (org-split-string val)))) | |
829 | ((string-equal key "EXPORT_EXCLUDE_TAGS") | |
830 | (setq p (plist-put p :exclude-tags (org-split-string val)))) | |
c8d0cf5c CD |
831 | ((string-equal key "MACRO") |
832 | (push val macros)) | |
b349f79f | 833 | ((equal key "SETUPFILE") |
3ab2c837 BG |
834 | (setq setup-file (org-remove-double-quotes (org-trim val)) |
835 | ;; take care of recursive inclusion of setupfiles | |
836 | setup-file (if (or (file-name-absolute-p val) (not setup-dir)) | |
837 | (expand-file-name setup-file) | |
838 | (let ((default-directory setup-dir)) | |
839 | (expand-file-name setup-file)))) | |
840 | (setq setup-dir (file-name-directory setup-file)) | |
841 | (setq setup-contents (org-file-contents setup-file 'noerror)) | |
b349f79f CD |
842 | (if (not ext-setup-or-nil) |
843 | (setq ext-setup-or-nil setup-contents start 0) | |
844 | (setq ext-setup-or-nil | |
845 | (concat (substring ext-setup-or-nil 0 start) | |
846 | "\n" setup-contents "\n" | |
847 | (substring ext-setup-or-nil start))))))) | |
20908596 | 848 | (setq p (plist-put p :text text)) |
c8d0cf5c CD |
849 | (when (and letbind (org-export-confirm-letbind)) |
850 | (setq p (plist-put p :let-bind letbind))) | |
621f83e4 CD |
851 | (when style (setq p (plist-put p :style-extra style))) |
852 | (when latex-header | |
853 | (setq p (plist-put p :latex-header-extra (substring latex-header 1)))) | |
8d642074 CD |
854 | (when latex-class |
855 | (setq p (plist-put p :latex-class latex-class))) | |
e66ba1df BG |
856 | (when latex-class-options |
857 | (setq p (plist-put p :latex-class-options latex-class-options))) | |
20908596 | 858 | (when options |
b349f79f | 859 | (setq p (org-export-add-options-to-plist p options))) |
afe98dfa CD |
860 | (when mathjax |
861 | (setq p (plist-put p :mathjax mathjax))) | |
c8d0cf5c CD |
862 | ;; Add macro definitions |
863 | (setq p (plist-put p :macro-date "(eval (format-time-string \"$1\"))")) | |
864 | (setq p (plist-put p :macro-time "(eval (format-time-string \"$1\"))")) | |
afe98dfa | 865 | (setq p (plist-put p :macro-property "(eval (org-entry-get nil \"$1\" 'selective))")) |
c8d0cf5c CD |
866 | (setq p (plist-put |
867 | p :macro-modification-time | |
868 | (and (buffer-file-name) | |
869 | (file-exists-p (buffer-file-name)) | |
870 | (concat | |
871 | "(eval (format-time-string \"$1\" '" | |
872 | (prin1-to-string (nth 5 (file-attributes | |
873 | (buffer-file-name)))) | |
874 | "))")))) | |
875 | (setq p (plist-put p :macro-input-file (and (buffer-file-name) | |
876 | (file-name-nondirectory | |
877 | (buffer-file-name))))) | |
878 | (while (setq val (pop macros)) | |
879 | (when (string-match "^\\([-a-zA-Z0-9_]+\\)[ \t]+\\(.*?[ \t]*$\\)" val) | |
880 | (setq p (plist-put | |
881 | p (intern | |
882 | (concat ":macro-" (downcase (match-string 1 val)))) | |
54a0dee5 | 883 | (org-export-interpolate-newlines (match-string 2 val)))))) |
20908596 CD |
884 | p)))) |
885 | ||
54a0dee5 CD |
886 | (defun org-export-interpolate-newlines (s) |
887 | (while (string-match "\\\\n" s) | |
888 | (setq s (replace-match "\n" t t s))) | |
889 | s) | |
890 | ||
c8d0cf5c CD |
891 | (defvar org-export-allow-BIND-local nil) |
892 | (defun org-export-confirm-letbind () | |
893 | "Can we use #+BIND values during export? | |
3ab2c837 | 894 | By default this will ask for confirmation by the user, to divert possible |
c8d0cf5c CD |
895 | security risks." |
896 | (cond | |
897 | ((not org-export-allow-BIND) nil) | |
898 | ((eq org-export-allow-BIND t) t) | |
899 | ((local-variable-p 'org-export-allow-BIND-local (current-buffer)) | |
900 | org-export-allow-BIND-local) | |
901 | (t (org-set-local 'org-export-allow-BIND-local | |
902 | (yes-or-no-p "Allow BIND values in this buffer? "))))) | |
903 | ||
904 | (defun org-install-letbind () | |
905 | "Install the values from #+BIND lines as local variables." | |
ed21c5c8 CD |
906 | (let ((letbind (plist-get org-export-opt-plist :let-bind)) |
907 | pair) | |
908 | (while (setq pair (pop letbind)) | |
909 | (org-set-local (car pair) (nth 1 pair))))) | |
c8d0cf5c | 910 | |
b349f79f | 911 | (defun org-export-add-options-to-plist (p options) |
33306645 | 912 | "Parse an OPTIONS line and set values in the property list P." |
b349f79f CD |
913 | (let (o) |
914 | (when options | |
65c439fd | 915 | (let ((op org-export-plist-vars)) |
b349f79f | 916 | (while (setq o (pop op)) |
ff4be292 | 917 | (if (and (nth 1 o) |
3ab2c837 BG |
918 | (string-match (concat "\\(\\`\\|[ \t]\\)" |
919 | (regexp-quote (nth 1 o)) | |
920 | ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)") | |
ff4be292 CD |
921 | options)) |
922 | (setq p (plist-put p (car o) | |
b349f79f | 923 | (car (read-from-string |
3ab2c837 | 924 | (match-string 2 options)))))))))) |
b349f79f | 925 | p) |
ff4be292 | 926 | |
b349f79f CD |
927 | (defun org-export-add-subtree-options (p pos) |
928 | "Add options in subtree at position POS to property list P." | |
929 | (save-excursion | |
930 | (goto-char pos) | |
931 | (when (org-at-heading-p) | |
932 | (let (a) | |
933 | ;; This is actually read in `org-export-get-title-from-subtree' | |
934 | ;; (when (setq a (org-entry-get pos "EXPORT_TITLE")) | |
935 | ;; (setq p (plist-put p :title a))) | |
936 | (when (setq a (org-entry-get pos "EXPORT_TEXT")) | |
937 | (setq p (plist-put p :text a))) | |
c8d0cf5c CD |
938 | (when (setq a (org-entry-get pos "EXPORT_AUTHOR")) |
939 | (setq p (plist-put p :author a))) | |
940 | (when (setq a (org-entry-get pos "EXPORT_DATE")) | |
941 | (setq p (plist-put p :date a))) | |
b349f79f CD |
942 | (when (setq a (org-entry-get pos "EXPORT_OPTIONS")) |
943 | (setq p (org-export-add-options-to-plist p a))))) | |
944 | p)) | |
945 | ||
20908596 CD |
946 | (defun org-export-directory (type plist) |
947 | (let* ((val (plist-get plist :publishing-directory)) | |
948 | (dir (if (listp val) | |
949 | (or (cdr (assoc type val)) ".") | |
950 | val))) | |
951 | dir)) | |
952 | ||
b349f79f CD |
953 | (defun org-export-process-option-filters (plist) |
954 | (let ((functions org-export-options-filters) f) | |
955 | (while (setq f (pop functions)) | |
956 | (setq plist (funcall f plist)))) | |
957 | plist) | |
958 | ||
20908596 CD |
959 | ;;;###autoload |
960 | (defun org-export (&optional arg) | |
961 | "Export dispatcher for Org-mode. | |
962 | When `org-export-run-in-background' is non-nil, try to run the command | |
963 | in the background. This will be done only for commands that write | |
964 | to a file. For details see the docstring of `org-export-run-in-background'. | |
965 | ||
966 | The prefix argument ARG will be passed to the exporter. However, if | |
86fbb8ca CD |
967 | ARG is a double universal prefix \\[universal-argument] \\[universal-argument], \ |
968 | that means to inverse the | |
3ab2c837 BG |
969 | value of `org-export-run-in-background'. |
970 | ||
971 | If `org-export-initial-scope' is set to 'subtree, try to export | |
972 | the current subtree, otherwise try to export the whole buffer. | |
973 | Pressing `1' will switch between these two options." | |
20908596 CD |
974 | (interactive "P") |
975 | (let* ((bg (org-xor (equal arg '(16)) org-export-run-in-background)) | |
3ab2c837 BG |
976 | (subtree-p (or (org-region-active-p) |
977 | (eq org-export-initial-scope 'subtree))) | |
8223b1d2 BG |
978 | (regb (and (org-region-active-p) (region-beginning))) |
979 | (rege (and (org-region-active-p) (region-end))) | |
20908596 CD |
980 | (help "[t] insert the export option template |
981 | \[v] limit export to visible part of outline tree | |
3ab2c837 | 982 | \[1] switch buffer/subtree export |
ed21c5c8 | 983 | \[SPC] publish enclosing subtree (with LaTeX_CLASS or EXPORT_FILE_NAME prop) |
20908596 | 984 | |
86fbb8ca | 985 | \[a/n/u] export as ASCII/Latin-1/UTF-8 [A/N/U] to temporary buffer |
20908596 | 986 | |
ed21c5c8 | 987 | \[h] export as HTML [H] to temporary buffer [R] export region |
c8d0cf5c | 988 | \[b] export as HTML and open in browser |
20908596 | 989 | |
ed21c5c8 CD |
990 | \[l] export as LaTeX [L] to temporary buffer |
991 | \[p] export as LaTeX and process to PDF [d] ... and open PDF file | |
c8d0cf5c | 992 | |
ed21c5c8 | 993 | \[D] export as DocBook [V] export as DocBook, process to PDF, and open |
c8d0cf5c | 994 | |
e66ba1df | 995 | \[o] export as OpenDocument Text [O] ... and open |
3ab2c837 | 996 | |
86fbb8ca CD |
997 | \[j] export as TaskJuggler [J] ... and open |
998 | ||
8bfe682a | 999 | \[m] export as Freemind mind map |
c8d0cf5c CD |
1000 | \[x] export as XOXO |
1001 | \[g] export using Wes Hardaker's generic exporter | |
20908596 CD |
1002 | |
1003 | \[i] export current file as iCalendar file | |
ed21c5c8 | 1004 | \[I] export all agenda files as iCalendar files [c] ...as one combined file |
20908596 | 1005 | |
c8d0cf5c CD |
1006 | \[F] publish current file [P] publish current project |
1007 | \[X] publish a project... [E] publish every projects") | |
20908596 CD |
1008 | (cmds |
1009 | '((?t org-insert-export-options-template nil) | |
1010 | (?v org-export-visible nil) | |
1011 | (?a org-export-as-ascii t) | |
c8d0cf5c | 1012 | (?A org-export-as-ascii-to-buffer t) |
ed21c5c8 CD |
1013 | (?n org-export-as-latin1 t) |
1014 | (?N org-export-as-latin1-to-buffer t) | |
1015 | (?u org-export-as-utf8 t) | |
1016 | (?U org-export-as-utf8-to-buffer t) | |
20908596 CD |
1017 | (?h org-export-as-html t) |
1018 | (?b org-export-as-html-and-open t) | |
1019 | (?H org-export-as-html-to-buffer nil) | |
1020 | (?R org-export-region-as-html nil) | |
1021 | (?x org-export-as-xoxo t) | |
c8d0cf5c CD |
1022 | (?g org-export-generic t) |
1023 | (?D org-export-as-docbook t) | |
1024 | (?V org-export-as-docbook-pdf-and-open t) | |
3ab2c837 BG |
1025 | (?o org-export-as-odt t) |
1026 | (?O org-export-as-odt-and-open t) | |
86fbb8ca CD |
1027 | (?j org-export-as-taskjuggler t) |
1028 | (?J org-export-as-taskjuggler-and-open t) | |
8bfe682a | 1029 | (?m org-export-as-freemind t) |
20908596 | 1030 | (?l org-export-as-latex t) |
71d35b24 CD |
1031 | (?p org-export-as-pdf t) |
1032 | (?d org-export-as-pdf-and-open t) | |
20908596 CD |
1033 | (?L org-export-as-latex-to-buffer nil) |
1034 | (?i org-export-icalendar-this-file t) | |
1035 | (?I org-export-icalendar-all-agenda-files t) | |
1036 | (?c org-export-icalendar-combine-agenda-files t) | |
1037 | (?F org-publish-current-file t) | |
1038 | (?P org-publish-current-project t) | |
1039 | (?X org-publish t) | |
c8d0cf5c | 1040 | (?E org-publish-all t))) |
ed21c5c8 CD |
1041 | r1 r2 ass |
1042 | (cpos (point)) (cbuf (current-buffer)) bpos) | |
c8d0cf5c CD |
1043 | (save-excursion |
1044 | (save-window-excursion | |
3ab2c837 BG |
1045 | (if subtree-p |
1046 | (message "Export subtree: ") | |
1047 | (message "Export buffer: ")) | |
c8d0cf5c CD |
1048 | (delete-other-windows) |
1049 | (with-output-to-temp-buffer "*Org Export/Publishing Help*" | |
1050 | (princ help)) | |
1051 | (org-fit-window-to-buffer (get-buffer-window | |
1052 | "*Org Export/Publishing Help*")) | |
3ab2c837 BG |
1053 | (while (eq (setq r1 (read-char-exclusive)) ?1) |
1054 | (cond (subtree-p | |
1055 | (setq subtree-p nil) | |
1056 | (message "Export buffer: ")) | |
1057 | ((not subtree-p) | |
1058 | (setq subtree-p t) | |
e66ba1df | 1059 | (setq bpos (point)) |
8223b1d2 BG |
1060 | (org-mark-subtree) |
1061 | (org-activate-mark) | |
1062 | (setq regb (and (org-region-active-p) (region-beginning))) | |
1063 | (setq rege (and (org-region-active-p) (region-end))) | |
3ab2c837 | 1064 | (message "Export subtree: ")))) |
ed21c5c8 | 1065 | (when (eq r1 ?\ ) |
3ab2c837 BG |
1066 | (let ((case-fold-search t) |
1067 | (end (save-excursion (while (org-up-heading-safe)) (point)))) | |
1068 | (outline-next-heading) | |
ed21c5c8 | 1069 | (if (re-search-backward |
3ab2c837 BG |
1070 | "^[ \t]+\\(:latex_class:\\|:export_title:\\|:export_file_name:\\)[ \t]+\\S-" |
1071 | end t) | |
ed21c5c8 CD |
1072 | (progn |
1073 | (org-back-to-heading t) | |
1074 | (setq subtree-p t) | |
1075 | (setq bpos (point)) | |
1076 | (message "Select command (for subtree): ") | |
1077 | (setq r1 (read-char-exclusive))) | |
3ab2c837 | 1078 | (error "No enclosing node with LaTeX_CLASS or EXPORT_TITLE or EXPORT_FILE_NAME") |
ed21c5c8 | 1079 | ))))) |
e66ba1df | 1080 | (if (fboundp 'redisplay) (redisplay)) ;; XEmacs does not have/need (redisplay) |
ed21c5c8 | 1081 | (and bpos (goto-char bpos)) |
20908596 CD |
1082 | (setq r2 (if (< r1 27) (+ r1 96) r1)) |
1083 | (unless (setq ass (assq r2 cmds)) | |
1084 | (error "No command associated with key %c" r1)) | |
71d35b24 CD |
1085 | (if (and bg (nth 2 ass) |
1086 | (not (buffer-base-buffer)) | |
1087 | (not (org-region-active-p))) | |
20908596 CD |
1088 | ;; execute in background |
1089 | (let ((p (start-process | |
1090 | (concat "Exporting " (file-name-nondirectory (buffer-file-name))) | |
1091 | "*Org Processes*" | |
1092 | (expand-file-name invocation-name invocation-directory) | |
1093 | "-batch" | |
1094 | "-l" user-init-file | |
1095 | "--eval" "(require 'org-exp)" | |
1096 | "--eval" "(setq org-wait .2)" | |
1097 | (buffer-file-name) | |
1098 | "-f" (symbol-name (nth 1 ass))))) | |
1099 | (set-process-sentinel p 'org-export-process-sentinel) | |
1100 | (message "Background process \"%s\": started" p)) | |
8223b1d2 BG |
1101 | ;; set the mark correctly when exporting a subtree |
1102 | (if subtree-p (let (deactivate-mark) (push-mark rege t t) (goto-char regb))) | |
1103 | ||
ed21c5c8 CD |
1104 | (call-interactively (nth 1 ass)) |
1105 | (when (and bpos (get-buffer-window cbuf)) | |
1106 | (let ((cw (selected-window))) | |
1107 | (select-window (get-buffer-window cbuf)) | |
1108 | (goto-char cpos) | |
1109 | (deactivate-mark) | |
1110 | (select-window cw)))))) | |
20908596 CD |
1111 | |
1112 | (defun org-export-process-sentinel (process status) | |
1113 | (if (string-match "\n+\\'" status) | |
1114 | (setq status (substring status 0 -1))) | |
1115 | (message "Background process \"%s\": %s" process status)) | |
1116 | ||
20908596 CD |
1117 | ;;; General functions for all backends |
1118 | ||
ce4fdcb9 CD |
1119 | (defvar org-export-target-aliases nil |
1120 | "Alist of targets with invisible aliases.") | |
c8d0cf5c CD |
1121 | (defvar org-export-preferred-target-alist nil |
1122 | "Alist of section id's with preferred aliases.") | |
ed21c5c8 CD |
1123 | (defvar org-export-id-target-alist nil |
1124 | "Alist of section id's with preferred aliases.") | |
0bd48b37 | 1125 | (defvar org-export-code-refs nil |
86fbb8ca | 1126 | "Alist of code references and line numbers.") |
ce4fdcb9 | 1127 | |
20908596 | 1128 | (defun org-export-preprocess-string (string &rest parameters) |
3ab2c837 | 1129 | "Cleanup STRING so that the true exported has a more consistent source. |
20908596 CD |
1130 | This function takes STRING, which should be a buffer-string of an org-file |
1131 | to export. It then creates a temporary buffer where it does its job. | |
1132 | The result is then again returned as a string, and the exporter works | |
1133 | on this string to produce the exported version." | |
1134 | (interactive) | |
3ab2c837 BG |
1135 | (let* ((org-export-current-backend (or (plist-get parameters :for-backend) |
1136 | org-export-current-backend)) | |
20908596 CD |
1137 | (archived-trees (plist-get parameters :archived-trees)) |
1138 | (inhibit-read-only t) | |
1139 | (drawers org-drawers) | |
3ab2c837 | 1140 | (source-buffer (current-buffer)) |
b349f79f | 1141 | target-alist rtn) |
20908596 | 1142 | |
ed21c5c8 CD |
1143 | (setq org-export-target-aliases nil |
1144 | org-export-preferred-target-alist nil | |
1145 | org-export-id-target-alist nil | |
1146 | org-export-code-refs nil) | |
ce4fdcb9 | 1147 | |
3ab2c837 | 1148 | (with-temp-buffer |
20908596 CD |
1149 | (erase-buffer) |
1150 | (insert string) | |
b349f79f | 1151 | (setq case-fold-search t) |
b349f79f | 1152 | |
ed21c5c8 CD |
1153 | (let ((inhibit-read-only t)) |
1154 | (remove-text-properties (point-min) (point-max) | |
1155 | '(read-only t))) | |
1156 | ||
20908596 | 1157 | ;; Remove license-to-kill stuff |
33306645 | 1158 | ;; The caller marks some stuff for killing, stuff that has been |
20908596 | 1159 | ;; used to create the page title, for example. |
b349f79f | 1160 | (org-export-kill-licensed-text) |
ff4be292 | 1161 | |
20908596 | 1162 | (let ((org-inhibit-startup t)) (org-mode)) |
b349f79f | 1163 | (setq case-fold-search t) |
3ab2c837 | 1164 | (org-clone-local-variables source-buffer "^\\(org-\\|orgtbl-\\)") |
c8d0cf5c | 1165 | (org-install-letbind) |
0bd48b37 CD |
1166 | |
1167 | ;; Call the hook | |
1168 | (run-hooks 'org-export-preprocess-hook) | |
1169 | ||
20908596 | 1170 | (untabify (point-min) (point-max)) |
ff4be292 | 1171 | |
0bd48b37 | 1172 | ;; Handle include files, and call a hook |
86fbb8ca | 1173 | (org-export-handle-include-files-recurse) |
0bd48b37 | 1174 | (run-hooks 'org-export-preprocess-after-include-files-hook) |
ff4be292 | 1175 | |
0bd48b37 CD |
1176 | ;; Get rid of archived trees |
1177 | (org-export-remove-archived-trees archived-trees) | |
1178 | ||
1179 | ;; Remove comment environment and comment subtrees | |
1180 | (org-export-remove-comment-blocks-and-subtrees) | |
1181 | ||
1182 | ;; Get rid of excluded trees, and call a hook | |
621f83e4 CD |
1183 | (org-export-handle-export-tags (plist-get parameters :select-tags) |
1184 | (plist-get parameters :exclude-tags)) | |
0bd48b37 | 1185 | (run-hooks 'org-export-preprocess-after-tree-selection-hook) |
621f83e4 | 1186 | |
3ab2c837 BG |
1187 | ;; Get rid of tasks, depending on configuration |
1188 | (org-export-remove-tasks (plist-get parameters :tasks)) | |
1189 | ||
1190 | ;; Prepare footnotes for export. During that process, footnotes | |
1191 | ;; actually included in the exported part of the buffer go | |
1192 | ;; though some transformations: | |
1193 | ||
1194 | ;; 1. They have their label normalized (like "[N]"); | |
1195 | ||
1196 | ;; 2. They get moved at the same place in the buffer (usually at | |
1197 | ;; its end, but backends may define another place via | |
1198 | ;; `org-footnote-insert-pos-for-preprocessor'); | |
1199 | ||
1200 | ;; 3. The are stored in `org-export-footnotes-seen', while | |
1201 | ;; `org-export-preprocess-string' is applied to their | |
1202 | ;; definition. | |
1203 | ||
1204 | ;; Line-wise exporters ignore `org-export-footnotes-seen', as | |
1205 | ;; they interpret footnotes at the moment they see them in the | |
1206 | ;; buffer. Context-wise exporters grab all the info needed in | |
1207 | ;; that variable and delete moved definitions (as described in | |
1208 | ;; 2nd step). | |
1209 | (when (plist-get parameters :footnotes) | |
1210 | (org-footnote-normalize nil parameters)) | |
1211 | ||
8223b1d2 | 1212 | ;; Change lists ending. Other parts of export may insert blank |
3ab2c837 BG |
1213 | ;; lines and lists' structure could be altered. |
1214 | (org-export-mark-list-end) | |
1215 | ||
1216 | ;; Process the macros | |
1217 | (org-export-preprocess-apply-macros) | |
1218 | (run-hooks 'org-export-preprocess-after-macros-hook) | |
afe98dfa | 1219 | |
acedf35c CD |
1220 | ;; Export code blocks |
1221 | (org-export-blocks-preprocess) | |
1222 | ||
3ab2c837 BG |
1223 | ;; Mark lists with properties |
1224 | (org-export-mark-list-properties) | |
1225 | ||
b349f79f | 1226 | ;; Handle source code snippets |
3ab2c837 | 1227 | (org-export-replace-src-segments-and-examples) |
0bd48b37 CD |
1228 | |
1229 | ;; Protect short examples marked by a leading colon | |
1230 | (org-export-protect-colon-examples) | |
1231 | ||
acedf35c | 1232 | ;; Protected spaces |
3ab2c837 | 1233 | (org-export-convert-protected-spaces) |
33306645 | 1234 | |
db55f368 CD |
1235 | ;; Find all headings and compute the targets for them |
1236 | (setq target-alist (org-export-define-heading-targets target-alist)) | |
ff4be292 | 1237 | |
ed21c5c8 CD |
1238 | (run-hooks 'org-export-preprocess-after-headline-targets-hook) |
1239 | ||
1240 | ;; Find HTML special classes for headlines | |
1241 | (org-export-remember-html-container-classes) | |
1242 | ||
20908596 | 1243 | ;; Get rid of drawers |
8bfe682a | 1244 | (org-export-remove-or-extract-drawers |
3ab2c837 | 1245 | drawers (plist-get parameters :drawers)) |
ff4be292 | 1246 | |
20908596 CD |
1247 | ;; Get the correct stuff before the first headline |
1248 | (when (plist-get parameters :skip-before-1st-heading) | |
1249 | (goto-char (point-min)) | |
ee9d4ebe | 1250 | (when (re-search-forward "^\\(#.*\n\\)?\\*+[ \t]" nil t) |
20908596 CD |
1251 | (delete-region (point-min) (match-beginning 0)) |
1252 | (goto-char (point-min)) | |
1253 | (insert "\n"))) | |
1254 | (when (plist-get parameters :add-text) | |
1255 | (goto-char (point-min)) | |
1256 | (insert (plist-get parameters :add-text) "\n")) | |
ff4be292 | 1257 | |
ff4be292 CD |
1258 | ;; Remove todo-keywords before exporting, if the user has requested so |
1259 | (org-export-remove-headline-metadata parameters) | |
20908596 CD |
1260 | |
1261 | ;; Find targets in comments and move them out of comments, | |
1262 | ;; but mark them as targets that should be invisible | |
b349f79f CD |
1263 | (setq target-alist (org-export-handle-invisible-targets target-alist)) |
1264 | ||
0bd48b37 CD |
1265 | ;; Select and protect backend specific stuff, throw away stuff |
1266 | ;; that is specific for other backends | |
ed21c5c8 | 1267 | (run-hooks 'org-export-preprocess-before-selecting-backend-code-hook) |
3ab2c837 | 1268 | (org-export-select-backend-specific-text) |
20908596 CD |
1269 | |
1270 | ;; Protect quoted subtrees | |
b349f79f | 1271 | (org-export-protect-quoted-subtrees) |
20908596 | 1272 | |
c8d0cf5c CD |
1273 | ;; Remove clock lines |
1274 | (org-export-remove-clock-lines) | |
1275 | ||
20908596 | 1276 | ;; Protect verbatim elements |
b349f79f | 1277 | (org-export-protect-verbatim) |
20908596 | 1278 | |
c8d0cf5c CD |
1279 | ;; Blockquotes, verse, and center |
1280 | (org-export-mark-blockquote-verse-center) | |
1281 | (run-hooks 'org-export-preprocess-after-blockquote-hook) | |
b349f79f | 1282 | |
0bd48b37 | 1283 | ;; Remove timestamps, if the user has requested so |
0bd48b37 CD |
1284 | (unless (plist-get parameters :timestamps) |
1285 | (org-export-remove-timestamps)) | |
1286 | ||
33306645 | 1287 | ;; Attach captions to the correct object |
3ab2c837 | 1288 | (setq target-alist (org-export-attach-captions-and-attributes target-alist)) |
db55f368 | 1289 | |
621f83e4 CD |
1290 | ;; Find matches for radio targets and turn them into internal links |
1291 | (org-export-mark-radio-links) | |
afe98dfa | 1292 | (run-hooks 'org-export-preprocess-after-radio-targets-hook) |
621f83e4 CD |
1293 | |
1294 | ;; Find all links that contain a newline and put them into a single line | |
1295 | (org-export-concatenate-multiline-links) | |
1296 | ||
1297 | ;; Normalize links: Convert angle and plain links into bracket links | |
1298 | ;; and expand link abbreviations | |
ed21c5c8 | 1299 | (run-hooks 'org-export-preprocess-before-normalizing-links-hook) |
621f83e4 CD |
1300 | (org-export-normalize-links) |
1301 | ||
1302 | ;; Find all internal links. If they have a fuzzy match (i.e. not | |
1303 | ;; a *dedicated* target match, let the link point to the | |
1304 | ;; corresponding section. | |
1305 | (org-export-target-internal-links target-alist) | |
1306 | ||
1307 | ;; Find multiline emphasis and put them into single line | |
1308 | (when (plist-get parameters :emph-multiline) | |
1309 | (org-export-concatenate-multiline-emphasis)) | |
1310 | ||
afe98dfa CD |
1311 | ;; Remove special table lines, and store alignment information |
1312 | (org-store-forced-table-alignment) | |
20908596 | 1313 | (when org-export-table-remove-special-lines |
b349f79f | 1314 | (org-export-remove-special-table-lines)) |
20908596 | 1315 | |
0bd48b37 CD |
1316 | ;; Another hook |
1317 | (run-hooks 'org-export-preprocess-before-backend-specifics-hook) | |
1318 | ||
3ab2c837 BG |
1319 | ;; Backend-specific preprocessing |
1320 | (let* ((backend-name (symbol-name org-export-current-backend)) | |
1321 | (f (intern (format "org-export-%s-preprocess" backend-name)))) | |
1322 | (require (intern (concat "org-" backend-name)) nil) | |
1323 | (funcall f parameters)) | |
c8d0cf5c | 1324 | |
20908596 | 1325 | ;; Remove or replace comments |
b349f79f | 1326 | (org-export-handle-comments (plist-get parameters :comments)) |
20908596 | 1327 | |
8223b1d2 BG |
1328 | ;; Remove #+TBLFM #+TBLNAME #+NAME #+RESULTS lines |
1329 | (org-export-handle-metalines) | |
e66ba1df | 1330 | |
0bd48b37 CD |
1331 | ;; Run the final hook |
1332 | (run-hooks 'org-export-preprocess-final-hook) | |
1333 | ||
20908596 | 1334 | (setq rtn (buffer-string))) |
20908596 CD |
1335 | rtn)) |
1336 | ||
b349f79f CD |
1337 | (defun org-export-kill-licensed-text () |
1338 | "Remove all text that is marked with a :org-license-to-kill property." | |
65c439fd | 1339 | (let (p) |
b349f79f CD |
1340 | (while (setq p (text-property-any (point-min) (point-max) |
1341 | :org-license-to-kill t)) | |
2c3ad40d CD |
1342 | (delete-region |
1343 | p (or (next-single-property-change p :org-license-to-kill) | |
1344 | (point-max)))))) | |
b349f79f | 1345 | |
afe98dfa CD |
1346 | (defvar org-export-define-heading-targets-headline-hook nil |
1347 | "Hook that is run when a headline was matched during target search. | |
1348 | This is part of the preprocessing for export.") | |
1349 | ||
b349f79f CD |
1350 | (defun org-export-define-heading-targets (target-alist) |
1351 | "Find all headings and define the targets for them. | |
86fbb8ca CD |
1352 | The new targets are added to TARGET-ALIST, which is also returned. |
1353 | Also find all ID and CUSTOM_ID properties and store them." | |
b349f79f CD |
1354 | (goto-char (point-min)) |
1355 | (org-init-section-numbers) | |
db55f368 | 1356 | (let ((re (concat "^" org-outline-regexp |
86fbb8ca CD |
1357 | "\\|" |
1358 | "^[ \t]*:\\(ID\\|CUSTOM_ID\\):[ \t]*\\([^ \t\r\n]+\\)")) | |
c8d0cf5c | 1359 | level target last-section-target a id) |
b349f79f | 1360 | (while (re-search-forward re nil t) |
86fbb8ca CD |
1361 | (org-if-unprotected-at (match-beginning 0) |
1362 | (if (match-end 2) | |
1363 | (progn | |
1364 | (setq id (org-match-string-no-properties 2)) | |
1365 | (push (cons id target) target-alist) | |
1366 | (setq a (or (assoc last-section-target org-export-target-aliases) | |
1367 | (progn | |
1368 | (push (list last-section-target) | |
1369 | org-export-target-aliases) | |
1370 | (car org-export-target-aliases)))) | |
1371 | (push (caar target-alist) (cdr a)) | |
1372 | (when (equal (match-string 1) "CUSTOM_ID") | |
1373 | (if (not (assoc last-section-target | |
1374 | org-export-preferred-target-alist)) | |
1375 | (push (cons last-section-target id) | |
1376 | org-export-preferred-target-alist))) | |
1377 | (when (equal (match-string 1) "ID") | |
1378 | (if (not (assoc last-section-target | |
1379 | org-export-id-target-alist)) | |
1380 | (push (cons last-section-target (concat "ID-" id)) | |
1381 | org-export-id-target-alist)))) | |
1382 | (setq level (org-reduced-level | |
1383 | (save-excursion (goto-char (point-at-bol)) | |
1384 | (org-outline-level)))) | |
1385 | (setq target (org-solidify-link-text | |
1386 | (format "sec-%s" (replace-regexp-in-string | |
3ab2c837 | 1387 | "\\." "-" |
86fbb8ca CD |
1388 | (org-section-number level))))) |
1389 | (setq last-section-target target) | |
1390 | (push (cons target target) target-alist) | |
1391 | (add-text-properties | |
1392 | (point-at-bol) (point-at-eol) | |
afe98dfa CD |
1393 | (list 'target target)) |
1394 | (run-hooks 'org-export-define-heading-targets-headline-hook))))) | |
b349f79f CD |
1395 | target-alist) |
1396 | ||
1397 | (defun org-export-handle-invisible-targets (target-alist) | |
1398 | "Find targets in comments and move them out of comments. | |
1399 | Mark them as invisible targets." | |
ce4fdcb9 | 1400 | (let (target tmp a) |
b349f79f CD |
1401 | (goto-char (point-min)) |
1402 | (while (re-search-forward "^#.*?\\(<<<?\\([^>\r\n]+\\)>>>?\\).*" nil t) | |
1403 | ;; Check if the line before or after is a headline with a target | |
1404 | (if (setq target (or (get-text-property (point-at-bol 0) 'target) | |
1405 | (get-text-property (point-at-bol 2) 'target))) | |
1406 | (progn | |
1407 | ;; use the existing target in a neighboring line | |
1408 | (setq tmp (match-string 2)) | |
1409 | (replace-match "") | |
1410 | (and (looking-at "\n") (delete-char 1)) | |
ce4fdcb9 CD |
1411 | (push (cons (setq tmp (org-solidify-link-text tmp)) target) |
1412 | target-alist) | |
1413 | (setq a (or (assoc target org-export-target-aliases) | |
1414 | (progn | |
1415 | (push (list target) org-export-target-aliases) | |
1416 | (car org-export-target-aliases)))) | |
1417 | (push tmp (cdr a))) | |
b349f79f CD |
1418 | ;; Make an invisible target |
1419 | (replace-match "\\1(INVISIBLE)")))) | |
1420 | target-alist) | |
1421 | ||
1422 | (defun org-export-target-internal-links (target-alist) | |
db55f368 | 1423 | "Find all internal links and assign targets to them. |
b349f79f | 1424 | If a link has a fuzzy match (i.e. not a *dedicated* target match), |
db55f368 CD |
1425 | let the link point to the corresponding section. |
1426 | This function also handles the id links, if they have a match in | |
1427 | the current file." | |
b349f79f CD |
1428 | (goto-char (point-min)) |
1429 | (while (re-search-forward org-bracket-link-regexp nil t) | |
ed21c5c8 | 1430 | (org-if-unprotected-at (1+ (match-beginning 0)) |
8223b1d2 BG |
1431 | (let* ((org-link-search-must-match-exact-headline t) |
1432 | (md (match-data)) | |
1433 | (desc (match-end 2)) | |
1434 | (link (org-link-unescape (match-string 1))) | |
1435 | (slink (org-solidify-link-text link)) | |
1436 | found props pos cref | |
1437 | (target | |
1438 | (cond | |
1439 | ((= (string-to-char link) ?#) | |
1440 | ;; user wants exactly this link | |
1441 | link) | |
1442 | ((cdr (assoc slink target-alist)) | |
1443 | (or (cdr (assoc (assoc slink target-alist) | |
1444 | org-export-preferred-target-alist)) | |
1445 | (cdr (assoc slink target-alist)))) | |
1446 | ((and (string-match "^id:" link) | |
1447 | (cdr (assoc (substring link 3) target-alist)))) | |
1448 | ((string-match "^(\\(.*\\))$" link) | |
1449 | (setq cref (match-string 1 link)) | |
1450 | (concat "coderef:" cref)) | |
1451 | ((string-match org-link-types-re link) nil) | |
1452 | ((or (file-name-absolute-p link) | |
1453 | (string-match "^\\." link)) | |
1454 | nil) | |
1455 | (t | |
1456 | (let ((org-link-search-inhibit-query t)) | |
1457 | (save-excursion | |
1458 | (setq found (condition-case nil (org-link-search link) | |
1459 | (error nil))) | |
1460 | (when (and found | |
1461 | (or (org-at-heading-p) | |
1462 | (not (eq found 'dedicated)))) | |
1463 | (or (get-text-property (point) 'target) | |
1464 | (get-text-property | |
1465 | (max (point-min) | |
1466 | (1- (or (previous-single-property-change | |
1467 | (point) 'target) 0))) | |
1468 | 'target))))))))) | |
1469 | (when target | |
1470 | (set-match-data md) | |
1471 | (goto-char (match-beginning 1)) | |
1472 | (setq props (text-properties-at (point))) | |
1473 | (delete-region (match-beginning 1) (match-end 1)) | |
1474 | (setq pos (point)) | |
1475 | (insert target) | |
1476 | (unless desc (insert "][" link)) | |
1477 | (add-text-properties pos (point) props)))))) | |
b349f79f | 1478 | |
ed21c5c8 CD |
1479 | (defun org-export-remember-html-container-classes () |
1480 | "Store the HTML_CONTAINER_CLASS properties in a text property." | |
1481 | (goto-char (point-min)) | |
1482 | (let (class) | |
1483 | (while (re-search-forward | |
afe98dfa | 1484 | "^[ \t]*:HTML_CONTAINER_CLASS:[ \t]+\\(.+\\)$" nil t) |
ed21c5c8 CD |
1485 | (setq class (match-string 1)) |
1486 | (save-excursion | |
8223b1d2 BG |
1487 | (when (re-search-backward "^\\*" (point-min) t) |
1488 | (org-back-to-heading t) | |
1489 | (put-text-property (point-at-bol) (point-at-eol) | |
1490 | 'html-container-class class)))))) | |
ed21c5c8 | 1491 | |
8bfe682a CD |
1492 | (defvar org-export-format-drawer-function nil |
1493 | "Function to be called to format the contents of a drawer. | |
e66ba1df | 1494 | The function must accept two parameters: |
8bfe682a CD |
1495 | NAME the drawer name, like \"PROPERTIES\" |
1496 | CONTENT the content of the drawer. | |
3ab2c837 | 1497 | You can check the export backend through `org-export-current-backend'. |
8bfe682a CD |
1498 | The function should return the text to be inserted into the buffer. |
1499 | If this is nil, `org-export-format-drawer' is used as a default.") | |
1500 | ||
3ab2c837 | 1501 | (defun org-export-remove-or-extract-drawers (all-drawers exp-drawers) |
8bfe682a | 1502 | "Remove drawers, or extract and format the content. |
b349f79f CD |
1503 | ALL-DRAWERS is a list of all drawer names valid in the current buffer. |
1504 | EXP-DRAWERS can be t to keep all drawer contents, or a list of drawers | |
8bfe682a | 1505 | whose content to keep. Any drawers that are in ALL-DRAWERS but not in |
3ab2c837 | 1506 | EXP-DRAWERS will be removed." |
8bfe682a CD |
1507 | (goto-char (point-min)) |
1508 | (let ((re (concat "^[ \t]*:\\(" | |
1509 | (mapconcat 'identity all-drawers "\\|") | |
1510 | "\\):[ \t]*$")) | |
1511 | name beg beg-content eol content) | |
1512 | (while (re-search-forward re nil t) | |
1513 | (org-if-unprotected | |
1514 | (setq name (match-string 1)) | |
1515 | (setq beg (match-beginning 0) | |
1516 | beg-content (1+ (point-at-eol)) | |
1517 | eol (point-at-eol)) | |
1518 | (if (not (and (re-search-forward | |
1519 | "^\\([ \t]*:END:[ \t]*\n?\\)\\|^\\*+[ \t]" nil t) | |
1520 | (match-end 1))) | |
1521 | (goto-char eol) | |
1522 | (goto-char (match-beginning 0)) | |
1523 | (and (looking-at ".*\n?") (replace-match "")) | |
1524 | (setq content (buffer-substring beg-content (point))) | |
1525 | (delete-region beg (point)) | |
1526 | (when (or (eq exp-drawers t) | |
1527 | (member name exp-drawers)) | |
1528 | (setq content (funcall (or org-export-format-drawer-function | |
1529 | 'org-export-format-drawer) | |
3ab2c837 | 1530 | name content)) |
8bfe682a CD |
1531 | (insert content))))))) |
1532 | ||
3ab2c837 | 1533 | (defun org-export-format-drawer (name content) |
8bfe682a CD |
1534 | "Format the content of a drawer as a colon example." |
1535 | (if (string-match "[ \t]+\\'" content) | |
1536 | (setq content (substring content (match-beginning 0)))) | |
1537 | (while (string-match "\\`[ \t]*\n" content) | |
1538 | (setq content (substring content (match-end 0)))) | |
1539 | (setq content (org-remove-indentation content)) | |
1540 | (setq content (concat ": " (mapconcat 'identity | |
1541 | (org-split-string content "\n") | |
1542 | "\n: ") | |
1543 | "\n")) | |
1544 | (setq content (concat " : " (upcase name) "\n" content)) | |
1545 | (org-add-props content nil 'org-protected t)) | |
b349f79f | 1546 | |
621f83e4 CD |
1547 | (defun org-export-handle-export-tags (select-tags exclude-tags) |
1548 | "Modify the buffer, honoring SELECT-TAGS and EXCLUDE-TAGS. | |
1549 | Both arguments are lists of tags. | |
1550 | If any of SELECT-TAGS is found, all trees not marked by a SELECT-TAG | |
1551 | will be removed. | |
1552 | After that, all subtrees that are marked by EXCLUDE-TAGS will be | |
1553 | removed as well." | |
1554 | (remove-text-properties (point-min) (point-max) '(:org-delete t)) | |
1555 | (let* ((re-sel (concat ":\\(" (mapconcat 'regexp-quote | |
1556 | select-tags "\\|") | |
1557 | "\\):")) | |
1558 | (re-excl (concat ":\\(" (mapconcat 'regexp-quote | |
8223b1d2 BG |
1559 | exclude-tags "\\|") |
1560 | "\\):")) | |
621f83e4 CD |
1561 | beg end cont) |
1562 | (goto-char (point-min)) | |
1563 | (when (and select-tags | |
1564 | (re-search-forward | |
1565 | (concat "^\\*+[ \t].*" re-sel "[^ \t\n]*[ \t]*$") nil t)) | |
1566 | ;; At least one tree is marked for export, this means | |
1567 | ;; all the unmarked stuff needs to go. | |
1568 | ;; Dig out the trees that should be exported | |
1569 | (goto-char (point-min)) | |
1570 | (outline-next-heading) | |
1571 | (setq beg (point)) | |
1572 | (put-text-property beg (point-max) :org-delete t) | |
1573 | (while (re-search-forward re-sel nil t) | |
e66ba1df | 1574 | (when (org-at-heading-p) |
621f83e4 CD |
1575 | (org-back-to-heading) |
1576 | (remove-text-properties | |
1577 | (max (1- (point)) (point-min)) | |
1578 | (setq cont (save-excursion (org-end-of-subtree t t))) | |
1579 | '(:org-delete t)) | |
1580 | (while (and (org-up-heading-safe) | |
1581 | (get-text-property (point) :org-delete)) | |
1582 | (remove-text-properties (max (1- (point)) (point-min)) | |
1583 | (point-at-eol) '(:org-delete t))) | |
1584 | (goto-char cont)))) | |
1585 | ;; Remove the trees explicitly marked for noexport | |
1586 | (when exclude-tags | |
1587 | (goto-char (point-min)) | |
1588 | (while (re-search-forward re-excl nil t) | |
1589 | (when (org-at-heading-p) | |
1590 | (org-back-to-heading t) | |
1591 | (setq beg (point)) | |
8bfe682a CD |
1592 | (org-end-of-subtree t t) |
1593 | (delete-region beg (point)) | |
1594 | (when (featurep 'org-inlinetask) | |
1595 | (org-inlinetask-remove-END-maybe))))) | |
621f83e4 CD |
1596 | ;; Remove everything that is now still marked for deletion |
1597 | (goto-char (point-min)) | |
1598 | (while (setq beg (text-property-any (point-min) (point-max) :org-delete t)) | |
1599 | (setq end (or (next-single-property-change beg :org-delete) | |
1600 | (point-max))) | |
1601 | (delete-region beg end)))) | |
1602 | ||
3ab2c837 BG |
1603 | (defun org-export-remove-tasks (keep) |
1604 | "Remove tasks depending on configuration. | |
1605 | When KEEP is nil, remove all tasks. | |
1606 | When KEEP is `todo', remove the tasks that are DONE. | |
1607 | When KEEP is `done', remove the tasks that are not yet done. | |
1608 | When it is a list of strings, keep only tasks with these TODO keywords." | |
1609 | (when (or (listp keep) (memq keep '(todo done nil))) | |
1610 | (let ((re (concat "^\\*+[ \t]+\\(" | |
1611 | (mapconcat | |
1612 | 'regexp-quote | |
1613 | (cond ((not keep) org-todo-keywords-1) | |
1614 | ((eq keep 'todo) org-done-keywords) | |
1615 | ((eq keep 'done) org-not-done-keywords) | |
1616 | ((listp keep) | |
1617 | (org-delete-all keep (copy-sequence | |
1618 | org-todo-keywords-1)))) | |
1619 | "\\|") | |
1620 | "\\)\\($\\|[ \t]\\)")) | |
8223b1d2 BG |
1621 | (case-fold-search nil) |
1622 | beg) | |
3ab2c837 BG |
1623 | (goto-char (point-min)) |
1624 | (while (re-search-forward re nil t) | |
1625 | (org-if-unprotected | |
1626 | (setq beg (match-beginning 0)) | |
1627 | (org-end-of-subtree t t) | |
1628 | (if (looking-at "^\\*+[ \t]+END[ \t]*$") | |
1629 | ;; Kill the END line of the inline task | |
1630 | (goto-char (min (point-max) (1+ (match-end 0))))) | |
1631 | (delete-region beg (point))))))) | |
1632 | ||
b349f79f CD |
1633 | (defun org-export-remove-archived-trees (export-archived-trees) |
1634 | "Remove archived trees. | |
1635 | When EXPORT-ARCHIVED-TREES is `headline;, only the headline will be exported. | |
1636 | When it is t, the entire archived tree will be exported. | |
1637 | When it is nil the entire tree including the headline will be removed | |
1638 | from the buffer." | |
1639 | (let ((re-archive (concat ":" org-archive-tag ":")) | |
1640 | a b) | |
1641 | (when (not (eq export-archived-trees t)) | |
1642 | (goto-char (point-min)) | |
1643 | (while (re-search-forward re-archive nil t) | |
e66ba1df | 1644 | (if (not (org-at-heading-p t)) |
afe98dfa | 1645 | (goto-char (point-at-eol)) |
b349f79f CD |
1646 | (beginning-of-line 1) |
1647 | (setq a (if export-archived-trees | |
1648 | (1+ (point-at-eol)) (point)) | |
1649 | b (org-end-of-subtree t)) | |
1650 | (if (> b a) (delete-region a b))))))) | |
1651 | ||
ff4be292 CD |
1652 | (defun org-export-remove-headline-metadata (opts) |
1653 | "Remove meta data from the headline, according to user options." | |
1654 | (let ((re org-complex-heading-regexp) | |
1655 | (todo (plist-get opts :todo-keywords)) | |
1656 | (tags (plist-get opts :tags)) | |
1657 | (pri (plist-get opts :priority)) | |
db55f368 | 1658 | (elts '(1 2 3 4 5)) |
3ab2c837 | 1659 | (case-fold-search nil) |
65c439fd | 1660 | rpl) |
db55f368 | 1661 | (setq elts (delq nil (list 1 (if todo 2) (if pri 3) 4 (if tags 5)))) |
ff4be292 | 1662 | (when (or (not todo) (not tags) (not pri)) |
ff4be292 CD |
1663 | (goto-char (point-min)) |
1664 | (while (re-search-forward re nil t) | |
0bd48b37 CD |
1665 | (org-if-unprotected |
1666 | (setq rpl (mapconcat (lambda (i) (if (match-end i) (match-string i) "")) | |
1667 | elts " ")) | |
1668 | (replace-match rpl t t)))))) | |
1669 | ||
1670 | (defun org-export-remove-timestamps () | |
1671 | "Remove timestamps and keywords for export." | |
c8d0cf5c | 1672 | (goto-char (point-min)) |
0bd48b37 CD |
1673 | (while (re-search-forward org-maybe-keyword-time-regexp nil t) |
1674 | (backward-char 1) | |
1675 | (org-if-unprotected | |
1e4f816a CD |
1676 | (unless (save-match-data (org-at-table-p)) |
1677 | (replace-match "") | |
1678 | (beginning-of-line 1) | |
1679 | (if (looking-at "[- \t]*\\(=>[- \t0-9:]*\\)?[ \t]*\n") | |
1680 | (replace-match "")))))) | |
0bd48b37 CD |
1681 | |
1682 | (defun org-export-remove-clock-lines () | |
c8d0cf5c CD |
1683 | "Remove clock lines for export." |
1684 | (goto-char (point-min)) | |
0bd48b37 CD |
1685 | (let ((re (concat "^[ \t]*" org-clock-string ".*\n?"))) |
1686 | (while (re-search-forward re nil t) | |
1687 | (org-if-unprotected | |
1688 | (replace-match ""))))) | |
ff4be292 | 1689 | |
e66ba1df | 1690 | (defvar org-heading-keyword-regexp-format) ; defined in org.el |
b349f79f CD |
1691 | (defun org-export-protect-quoted-subtrees () |
1692 | "Mark quoted subtrees with the protection property." | |
e66ba1df BG |
1693 | (let ((org-re-quote (format org-heading-keyword-regexp-format |
1694 | org-quote-string))) | |
b349f79f | 1695 | (goto-char (point-min)) |
3ab2c837 | 1696 | (while (re-search-forward org-re-quote nil t) |
b349f79f CD |
1697 | (goto-char (match-beginning 0)) |
1698 | (end-of-line 1) | |
1699 | (add-text-properties (point) (org-end-of-subtree t) | |
1700 | '(org-protected t))))) | |
1701 | ||
3ab2c837 | 1702 | (defun org-export-convert-protected-spaces () |
acedf35c CD |
1703 | "Convert strings like \\____ to protected spaces in all backends." |
1704 | (goto-char (point-min)) | |
1705 | (while (re-search-forward "\\\\__+" nil t) | |
1706 | (org-if-unprotected-1 | |
1707 | (replace-match | |
1708 | (org-add-props | |
1709 | (cond | |
3ab2c837 | 1710 | ((eq org-export-current-backend 'latex) |
acedf35c | 1711 | (format "\\hspace{%dex}" (- (match-end 0) (match-beginning 0)))) |
3ab2c837 | 1712 | ((eq org-export-current-backend 'html) |
acedf35c CD |
1713 | (org-add-props (match-string 0) nil |
1714 | 'org-whitespace (- (match-end 0) (match-beginning 0)))) | |
3ab2c837 BG |
1715 | ;; ((eq org-export-current-backend 'docbook)) |
1716 | ((eq org-export-current-backend 'ascii) | |
acedf35c CD |
1717 | (org-add-props (match-string 0) '(org-whitespace t))) |
1718 | (t (make-string (- (match-end 0) (match-beginning 0)) ?\ ))) | |
1719 | '(org-protected t)) | |
1720 | t t)))) | |
1721 | ||
b349f79f CD |
1722 | (defun org-export-protect-verbatim () |
1723 | "Mark verbatim snippets with the protection property." | |
1724 | (goto-char (point-min)) | |
1725 | (while (re-search-forward org-verbatim-re nil t) | |
8bfe682a CD |
1726 | (org-if-unprotected |
1727 | (add-text-properties (match-beginning 4) (match-end 4) | |
1728 | '(org-protected t org-verbatim-emph t)) | |
1729 | (goto-char (1+ (match-end 4)))))) | |
b349f79f | 1730 | |
0bd48b37 CD |
1731 | (defun org-export-protect-colon-examples () |
1732 | "Protect lines starting with a colon." | |
b349f79f | 1733 | (goto-char (point-min)) |
65c439fd | 1734 | (let ((re "^[ \t]*:\\([ \t]\\|$\\)") beg) |
0bd48b37 CD |
1735 | (while (re-search-forward re nil t) |
1736 | (beginning-of-line 1) | |
1737 | (setq beg (point)) | |
1738 | (while (looking-at re) | |
1739 | (end-of-line 1) | |
1740 | (or (eobp) (forward-char 1))) | |
1741 | (add-text-properties beg (if (bolp) (1- (point)) (point)) | |
1742 | '(org-protected t))))) | |
b349f79f | 1743 | |
3ab2c837 BG |
1744 | (defvar org-export-backends |
1745 | '(docbook html beamer ascii latex) | |
1746 | "List of Org supported export backends.") | |
1747 | ||
1748 | (defun org-export-select-backend-specific-text () | |
1749 | (let ((formatters org-export-backends) | |
0bd48b37 | 1750 | (case-fold-search t) |
3ab2c837 | 1751 | backend backend-name beg beg-content end end-content ind) |
b349f79f CD |
1752 | |
1753 | (while formatters | |
3ab2c837 BG |
1754 | (setq backend (pop formatters) |
1755 | backend-name (symbol-name backend)) | |
1756 | ||
1757 | ;; Handle #+BACKEND: stuff | |
86fbb8ca | 1758 | (goto-char (point-min)) |
3ab2c837 | 1759 | (while (re-search-forward (concat "^\\([ \t]*\\)#\\+" backend-name |
86fbb8ca | 1760 | ":[ \t]*\\(.*\\)") nil t) |
3ab2c837 | 1761 | (if (not (eq backend org-export-current-backend)) |
86fbb8ca | 1762 | (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max))) |
153ae947 BG |
1763 | (let ((ind (get-text-property (point-at-bol) 'original-indentation))) |
1764 | (replace-match "\\1\\2" t) | |
1765 | (add-text-properties | |
1766 | (point-at-bol) (min (1+ (point-at-eol)) (point-max)) | |
1767 | `(org-protected t original-indentation ,ind org-native-text t))))) | |
8223b1d2 | 1768 | ;; Delete #+ATTR_BACKEND: stuff of another backend. Those |
86fbb8ca CD |
1769 | ;; matching the current backend will be taken care of by |
1770 | ;; `org-export-attach-captions-and-attributes' | |
1771 | (goto-char (point-min)) | |
3ab2c837 | 1772 | (while (re-search-forward (concat "^\\([ \t]*\\)#\\+ATTR_" backend-name |
86fbb8ca | 1773 | ":[ \t]*\\(.*\\)") nil t) |
3ab2c837 BG |
1774 | (setq ind (org-get-indentation)) |
1775 | (when (not (eq backend org-export-current-backend)) | |
86fbb8ca | 1776 | (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max))))) |
3ab2c837 | 1777 | ;; Handle #+BEGIN_BACKEND and #+END_BACKEND stuff |
b349f79f | 1778 | (goto-char (point-min)) |
3ab2c837 | 1779 | (while (re-search-forward (concat "^[ \t]*#\\+BEGIN_" backend-name "\\>.*\n?") |
54a0dee5 CD |
1780 | nil t) |
1781 | (setq beg (match-beginning 0) beg-content (match-end 0)) | |
153ae947 BG |
1782 | (setq ind (or (get-text-property beg 'original-indentation) |
1783 | (save-excursion (goto-char beg) (org-get-indentation)))) | |
3ab2c837 | 1784 | (when (re-search-forward (concat "^[ \t]*#\\+END_" backend-name "\\>.*\n?") |
54a0dee5 CD |
1785 | nil t) |
1786 | (setq end (match-end 0) end-content (match-beginning 0)) | |
3ab2c837 | 1787 | (if (eq backend org-export-current-backend) |
54a0dee5 | 1788 | ;; yes, keep this |
8bfe682a | 1789 | (progn |
3ab2c837 BG |
1790 | (add-text-properties |
1791 | beg-content end-content | |
1792 | `(org-protected t original-indentation ,ind org-native-text t)) | |
1793 | ;; strip protective commas | |
bdebdb64 | 1794 | (org-unescape-code-in-region beg-content end-content) |
8bfe682a CD |
1795 | (delete-region (match-beginning 0) (match-end 0)) |
1796 | (save-excursion | |
1797 | (goto-char beg) | |
1798 | (delete-region (point) (1+ (point-at-eol))))) | |
54a0dee5 CD |
1799 | ;; No, this is for a different backend, kill it |
1800 | (delete-region beg end))))))) | |
b349f79f | 1801 | |
c8d0cf5c | 1802 | (defun org-export-mark-blockquote-verse-center () |
b349f79f CD |
1803 | "Mark block quote and verse environments with special cookies. |
1804 | These special cookies will later be interpreted by the backend." | |
1805 | ;; Blockquotes | |
c8d0cf5c CD |
1806 | (let (type t1 ind beg end beg1 end1 content) |
1807 | (goto-char (point-min)) | |
1808 | (while (re-search-forward | |
1809 | "^\\([ \t]*\\)#\\+\\(begin_\\(\\(block\\)?quote\\|verse\\|center\\)\\>.*\\)" | |
1810 | nil t) | |
1811 | (setq ind (length (match-string 1)) | |
1812 | type (downcase (match-string 3)) | |
1813 | t1 (if (equal type "quote") "blockquote" type)) | |
1814 | (setq beg (match-beginning 0) | |
1815 | beg1 (1+ (match-end 0))) | |
1816 | (when (re-search-forward (concat "^[ \t]*#\\+end_" type "\\>.*") nil t) | |
86fbb8ca CD |
1817 | (setq end1 (1- (match-beginning 0)) |
1818 | end (+ (point-at-eol) (if (looking-at "\n$") 1 0))) | |
c8d0cf5c CD |
1819 | (setq content (org-remove-indentation (buffer-substring beg1 end1))) |
1820 | (setq content (concat "ORG-" (upcase t1) "-START\n" | |
1821 | content "\n" | |
1822 | "ORG-" (upcase t1) "-END\n")) | |
1823 | (delete-region beg end) | |
1824 | (insert (org-add-props content nil 'original-indentation ind)))))) | |
b349f79f | 1825 | |
3ab2c837 BG |
1826 | (defun org-export-mark-list-end () |
1827 | "Mark all list endings with a special string." | |
1828 | (unless (eq org-export-current-backend 'ascii) | |
1829 | (mapc | |
1830 | (lambda (e) | |
1831 | ;; For each type allowing list export, find every list, remove | |
1832 | ;; ending regexp if needed, and insert org-list-end. | |
1833 | (goto-char (point-min)) | |
1834 | (while (re-search-forward (org-item-beginning-re) nil t) | |
1835 | (when (eq (nth 2 (org-list-context)) e) | |
1836 | (let* ((struct (org-list-struct)) | |
1837 | (bottom (org-list-get-bottom-point struct)) | |
1838 | (top (point-at-bol)) | |
1839 | (top-ind (org-list-get-ind top struct))) | |
1840 | (goto-char bottom) | |
153ae947 | 1841 | (when (and (not (looking-at "[ \t]*$")) |
3ab2c837 BG |
1842 | (looking-at org-list-end-re)) |
1843 | (replace-match "")) | |
1844 | (unless (bolp) (insert "\n")) | |
1845 | ;; As org-list-end is inserted at column 0, it would end | |
8223b1d2 | 1846 | ;; by indentation any list. It can be problematic when |
3ab2c837 | 1847 | ;; there are lists within lists: the inner list end would |
8223b1d2 | 1848 | ;; also become the outer list end. To avoid this, text |
3ab2c837 BG |
1849 | ;; property `original-indentation' is added, as |
1850 | ;; `org-list-struct' pays attention to it when reading a | |
1851 | ;; list. | |
1852 | (insert (org-add-props | |
1853 | "ORG-LIST-END-MARKER\n" | |
1854 | (list 'original-indentation top-ind))))))) | |
1855 | (cons nil org-list-export-context)))) | |
1856 | ||
1857 | (defun org-export-mark-list-properties () | |
1858 | "Mark list with special properties. | |
1859 | These special properties will later be interpreted by the backend." | |
1860 | (let ((mark-list | |
1861 | (function | |
1862 | ;; Mark a list with 3 properties: `list-item' which is | |
1863 | ;; position at beginning of line, `list-struct' which is | |
1864 | ;; list structure, and `list-prevs' which is the alist of | |
8223b1d2 | 1865 | ;; item and its predecessor. Leave point at list ending. |
3ab2c837 BG |
1866 | (lambda (ctxt) |
1867 | (let* ((struct (org-list-struct)) | |
1868 | (top (org-list-get-top-point struct)) | |
1869 | (bottom (org-list-get-bottom-point struct)) | |
1870 | (prevs (org-list-prevs-alist struct)) | |
1871 | poi) | |
1872 | ;; Get every item and ending position, without dups and | |
1873 | ;; without bottom point of list. | |
1874 | (mapc (lambda (e) | |
1875 | (let ((pos (car e)) | |
1876 | (end (nth 6 e))) | |
1877 | (unless (memq pos poi) | |
1878 | (push pos poi)) | |
1879 | (unless (or (= end bottom) (memq end poi)) | |
1880 | (push end poi)))) | |
1881 | struct) | |
1882 | (setq poi (sort poi '<)) | |
1883 | ;; For every point of interest, mark the whole line with | |
1884 | ;; its position in list. | |
1885 | (mapc | |
1886 | (lambda (e) | |
1887 | (goto-char e) | |
1888 | (add-text-properties (point-at-bol) (point-at-eol) | |
1889 | (list 'list-item (point-at-bol) | |
1890 | 'list-struct struct | |
1891 | 'list-prevs prevs))) | |
1892 | poi) | |
8223b1d2 | 1893 | ;; Take care of bottom point. As babel may have inserted |
3ab2c837 | 1894 | ;; a new list in buffer, list ending isn't always |
8223b1d2 | 1895 | ;; marked. Now mark every list ending and add properties |
3ab2c837 BG |
1896 | ;; useful to line processing exporters. |
1897 | (goto-char bottom) | |
1898 | (when (or (looking-at "^ORG-LIST-END-MARKER\n") | |
153ae947 | 1899 | (and (not (looking-at "[ \t]*$")) |
3ab2c837 BG |
1900 | (looking-at org-list-end-re))) |
1901 | (replace-match "")) | |
1902 | (unless (bolp) (insert "\n")) | |
1903 | (insert | |
1904 | (org-add-props "ORG-LIST-END-MARKER\n" (list 'list-item bottom | |
8223b1d2 BG |
1905 | 'list-struct struct |
1906 | 'list-prevs prevs))) | |
3ab2c837 BG |
1907 | ;; Following property is used by LaTeX exporter. |
1908 | (add-text-properties top (point) (list 'list-context ctxt))))))) | |
1909 | ;; Mark lists except for backends not interpreting them. | |
1910 | (unless (eq org-export-current-backend 'ascii) | |
1911 | (let ((org-list-end-re "^ORG-LIST-END-MARKER\n")) | |
1912 | (mapc | |
1913 | (lambda (e) | |
1914 | (goto-char (point-min)) | |
1915 | (while (re-search-forward (org-item-beginning-re) nil t) | |
1916 | (let ((context (nth 2 (org-list-context)))) | |
1917 | (if (eq context e) | |
1918 | (funcall mark-list e) | |
1919 | (put-text-property (point-at-bol) (point-at-eol) | |
1920 | 'list-context context))))) | |
1921 | (cons nil org-list-export-context)))))) | |
1922 | ||
1923 | (defun org-export-attach-captions-and-attributes (target-alist) | |
db55f368 CD |
1924 | "Move #+CAPTION, #+ATTR_BACKEND, and #+LABEL text into text properties. |
1925 | If the next thing following is a table, add the text properties to the first | |
1926 | table line. If it is a link, add it to the line containing the link." | |
1927 | (goto-char (point-min)) | |
1928 | (remove-text-properties (point-min) (point-max) | |
1929 | '(org-caption nil org-attributes nil)) | |
1930 | (let ((case-fold-search t) | |
c8d0cf5c | 1931 | (re (concat "^[ \t]*#\\+caption:[ \t]+\\(.*\\)" |
db55f368 | 1932 | "\\|" |
3ab2c837 | 1933 | "^[ \t]*#\\+attr_" (symbol-name org-export-current-backend) ":[ \t]+\\(.*\\)" |
db55f368 | 1934 | "\\|" |
c8d0cf5c | 1935 | "^[ \t]*#\\+label:[ \t]+\\(.*\\)" |
db55f368 | 1936 | "\\|" |
ed21c5c8 | 1937 | "^[ \t]*\\(|[^-]\\)" |
db55f368 CD |
1938 | "\\|" |
1939 | "^[ \t]*\\[\\[.*\\]\\][ \t]*$")) | |
86fbb8ca | 1940 | cap shortn attr label end) |
db55f368 CD |
1941 | (while (re-search-forward re nil t) |
1942 | (cond | |
3ab2c837 | 1943 | ;; there is a caption |
db55f368 | 1944 | ((match-end 1) |
86fbb8ca CD |
1945 | (progn |
1946 | (setq cap (concat cap (if cap " " "") (org-trim (match-string 1)))) | |
1947 | (when (string-match "\\[\\(.*\\)\\]{\\(.*\\)}" cap) | |
1948 | (setq shortn (match-string 1 cap) | |
1949 | cap (match-string 2 cap))) | |
1950 | (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max))))) | |
3ab2c837 | 1951 | ;; there is an attribute |
db55f368 | 1952 | ((match-end 2) |
86fbb8ca CD |
1953 | (progn |
1954 | (setq attr (concat attr (if attr " " "") (org-trim (match-string 2)))) | |
1955 | (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max))))) | |
3ab2c837 | 1956 | ;; there is a label |
db55f368 | 1957 | ((match-end 3) |
86fbb8ca CD |
1958 | (progn |
1959 | (setq label (org-trim (match-string 3))) | |
1960 | (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max))))) | |
db55f368 | 1961 | (t |
ed21c5c8 CD |
1962 | (setq end (if (match-end 4) |
1963 | (let ((ee (org-table-end))) | |
1964 | (prog1 (1- (marker-position ee)) (move-marker ee nil))) | |
1965 | (point-at-eol))) | |
1966 | (add-text-properties (point-at-bol) end | |
db55f368 | 1967 | (list 'org-caption cap |
86fbb8ca | 1968 | 'org-caption-shortn shortn |
db55f368 CD |
1969 | 'org-attributes attr |
1970 | 'org-label label)) | |
1971 | (if label (push (cons label label) target-alist)) | |
ed21c5c8 | 1972 | (goto-char end) |
3ab2c837 | 1973 | (setq cap nil shortn nil attr nil label nil))))) |
db55f368 CD |
1974 | target-alist) |
1975 | ||
b349f79f CD |
1976 | (defun org-export-remove-comment-blocks-and-subtrees () |
1977 | "Remove the comment environment, and also commented subtrees." | |
e66ba1df BG |
1978 | (let ((re-commented (format org-heading-keyword-regexp-format |
1979 | org-comment-string)) | |
ed21c5c8 | 1980 | case-fold-search) |
b349f79f CD |
1981 | ;; Remove comment environment |
1982 | (goto-char (point-min)) | |
ed21c5c8 | 1983 | (setq case-fold-search t) |
b349f79f | 1984 | (while (re-search-forward |
3ab2c837 | 1985 | "^#\\+begin_comment[ \t]*\n[^\000]*?\n#\\+end_comment\\>.*" nil t) |
b349f79f CD |
1986 | (replace-match "" t t)) |
1987 | ;; Remove subtrees that are commented | |
1988 | (goto-char (point-min)) | |
ed21c5c8 | 1989 | (setq case-fold-search nil) |
b349f79f CD |
1990 | (while (re-search-forward re-commented nil t) |
1991 | (goto-char (match-beginning 0)) | |
1992 | (delete-region (point) (org-end-of-subtree t))))) | |
1993 | ||
3ab2c837 | 1994 | (defun org-export-handle-comments (org-commentsp) |
b349f79f | 1995 | "Remove comments, or convert to backend-specific format. |
3ab2c837 | 1996 | ORG-COMMENTSP can be a format string for publishing comments. |
b349f79f | 1997 | When it is nil, all comments will be removed." |
8223b1d2 | 1998 | (let ((re "^[ \t]*#\\( \\|$\\)")) |
86fbb8ca | 1999 | (goto-char (point-min)) |
8223b1d2 BG |
2000 | (while (re-search-forward re nil t) |
2001 | (let ((pos (match-beginning 0)) | |
2002 | (end (progn (forward-line) (point)))) | |
2003 | (if (get-text-property pos 'org-protected) | |
2004 | (forward-line) | |
2005 | (if (not org-commentsp) (delete-region pos end) | |
2006 | (add-text-properties pos end '(org-protected t)) | |
2007 | (replace-match | |
2008 | (org-add-props | |
2009 | (format org-commentsp (buffer-substring (match-end 0) end)) | |
2010 | nil 'org-protected t) | |
2011 | t t))))) | |
2012 | ;; Hack attack: previous implementation also removed keywords at | |
2013 | ;; column 0. Brainlessly do it again. | |
2014 | (goto-char (point-min)) | |
2015 | (while (re-search-forward "^#\\+" nil t) | |
2016 | (unless (get-text-property (point-at-bol) 'org-protected) | |
2017 | (delete-region (point-at-bol) (progn (forward-line) (point))))))) | |
86fbb8ca | 2018 | |
8223b1d2 BG |
2019 | (defun org-export-handle-metalines () |
2020 | "Remove tables and source blocks metalines. | |
2021 | This function should only be called after all block processing | |
2022 | has taken place." | |
2023 | (let ((re "^[ \t]*#\\+\\(tbl\\(?:name\\|fm\\)\\|results\\(?:\\[[a-z0-9]+\\]\\)?\\|name\\):\\(.*\n?\\)") | |
2024 | (case-fold-search t) | |
b349f79f CD |
2025 | pos) |
2026 | (goto-char (point-min)) | |
2027 | (while (or (looking-at re) | |
2028 | (re-search-forward re nil t)) | |
2029 | (setq pos (match-beginning 0)) | |
afe98dfa | 2030 | (if (get-text-property (match-beginning 1) 'org-protected) |
86fbb8ca | 2031 | (goto-char (1+ pos)) |
b349f79f | 2032 | (goto-char (1+ pos)) |
86fbb8ca CD |
2033 | (replace-match "") |
2034 | (goto-char (max (point-min) (1- pos))))))) | |
b349f79f CD |
2035 | |
2036 | (defun org-export-mark-radio-links () | |
2037 | "Find all matches for radio targets and turn them into internal links." | |
2038 | (let ((re-radio (and org-target-link-regexp | |
2039 | (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))) | |
2040 | (goto-char (point-min)) | |
2041 | (when re-radio | |
2042 | (while (re-search-forward re-radio nil t) | |
c8d0cf5c CD |
2043 | (unless |
2044 | (save-match-data | |
2045 | (or (org-in-regexp org-bracket-link-regexp) | |
ed21c5c8 CD |
2046 | (org-in-regexp org-plain-link-re) |
2047 | (org-in-regexp "<<[^<>]+>>"))) | |
c8d0cf5c CD |
2048 | (org-if-unprotected |
2049 | (replace-match "\\1[[\\2]]"))))))) | |
b349f79f | 2050 | |
afe98dfa CD |
2051 | (defun org-store-forced-table-alignment () |
2052 | "Find table lines which force alignment, store the results in properties." | |
e66ba1df | 2053 | (let (line cnt cookies) |
afe98dfa | 2054 | (goto-char (point-min)) |
e66ba1df BG |
2055 | (while (re-search-forward "|[ \t]*<\\([lrc]?[0-9]+\\|[lrc]\\)>[ \t]*|" |
2056 | nil t) | |
afe98dfa CD |
2057 | ;; OK, this looks like a table line with an alignment cookie |
2058 | (org-if-unprotected | |
2059 | (setq line (buffer-substring (point-at-bol) (point-at-eol))) | |
2060 | (when (and (org-at-table-p) | |
2061 | (org-table-cookie-line-p line)) | |
e66ba1df | 2062 | (setq cnt 0 cookies nil) |
afe98dfa CD |
2063 | (mapc |
2064 | (lambda (x) | |
2065 | (setq cnt (1+ cnt)) | |
e66ba1df BG |
2066 | (when (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'" x) |
2067 | (let ((align (and (match-end 1) | |
2068 | (downcase (match-string 1 x)))) | |
2069 | (width (and (match-end 2) | |
2070 | (string-to-number (match-string 2 x))))) | |
2071 | (push (cons cnt (list align width)) cookies)))) | |
afe98dfa CD |
2072 | (org-split-string line "[ \t]*|[ \t]*")) |
2073 | (add-text-properties (org-table-begin) (org-table-end) | |
e66ba1df | 2074 | (list 'org-col-cookies cookies)))) |
afe98dfa CD |
2075 | (goto-char (point-at-eol))))) |
2076 | ||
b349f79f | 2077 | (defun org-export-remove-special-table-lines () |
afe98dfa | 2078 | "Remove tables lines that are used for internal purposes. |
3ab2c837 | 2079 | Also, store forced alignment information found in such lines." |
b349f79f CD |
2080 | (goto-char (point-min)) |
2081 | (while (re-search-forward "^[ \t]*|" nil t) | |
86fbb8ca CD |
2082 | (org-if-unprotected-at (1- (point)) |
2083 | (beginning-of-line 1) | |
2084 | (if (or (looking-at "[ \t]*| *[!_^] *|") | |
2085 | (not | |
2086 | (memq | |
2087 | nil | |
2088 | (mapcar | |
2089 | (lambda (f) | |
3ab2c837 | 2090 | (or (and org-export-table-remove-empty-lines (= (length f) 0)) |
86fbb8ca | 2091 | (string-match |
afe98dfa | 2092 | "\\`<\\([0-9]\\|[lrc]\\|[lrc][0-9]+\\)>\\'" f))) |
86fbb8ca CD |
2093 | (org-split-string ;; FIXME, can't we do without splitting??? |
2094 | (buffer-substring (point-at-bol) (point-at-eol)) | |
2095 | "[ \t]*|[ \t]*"))))) | |
2096 | (delete-region (max (point-min) (1- (point-at-bol))) | |
2097 | (point-at-eol)) | |
2098 | (end-of-line 1))))) | |
b349f79f | 2099 | |
54a0dee5 CD |
2100 | (defun org-export-protect-sub-super (s) |
2101 | (save-match-data | |
2102 | (while (string-match "\\([^\\\\]\\)\\([_^]\\)" s) | |
2103 | (setq s (replace-match "\\1\\\\\\2" nil nil s))) | |
2104 | s)) | |
2105 | ||
b349f79f CD |
2106 | (defun org-export-normalize-links () |
2107 | "Convert all links to bracket links, and expand link abbreviations." | |
2108 | (let ((re-plain-link (concat "\\([^[<]\\)" org-plain-link-re)) | |
c8d0cf5c CD |
2109 | (re-angle-link (concat "\\([^[]\\)" org-angle-link-re)) |
2110 | nodesc) | |
b349f79f | 2111 | (goto-char (point-min)) |
e66ba1df BG |
2112 | (while (re-search-forward org-bracket-link-regexp nil t) |
2113 | (put-text-property (match-beginning 0) (match-end 0) 'org-normalized-link t)) | |
2114 | (goto-char (point-min)) | |
b349f79f | 2115 | (while (re-search-forward re-plain-link nil t) |
f186a0b5 | 2116 | (unless (get-text-property (match-beginning 0) 'org-normalized-link) |
afe98dfa CD |
2117 | (goto-char (1- (match-end 0))) |
2118 | (org-if-unprotected-at (1+ (match-beginning 0)) | |
2119 | (let* ((s (concat (match-string 1) | |
2120 | "[[" (match-string 2) ":" (match-string 3) | |
2121 | "][" (match-string 2) ":" (org-export-protect-sub-super | |
2122 | (match-string 3)) | |
2123 | "]]"))) | |
2124 | ;; added 'org-link face to links | |
2125 | (put-text-property 0 (length s) 'face 'org-link s) | |
2126 | (replace-match s t t))))) | |
b349f79f CD |
2127 | (goto-char (point-min)) |
2128 | (while (re-search-forward re-angle-link nil t) | |
2129 | (goto-char (1- (match-end 0))) | |
2130 | (org-if-unprotected | |
54a0dee5 CD |
2131 | (let* ((s (concat (match-string 1) |
2132 | "[[" (match-string 2) ":" (match-string 3) | |
2133 | "][" (match-string 2) ":" (org-export-protect-sub-super | |
2134 | (match-string 3)) | |
2135 | "]]"))) | |
b349f79f CD |
2136 | (put-text-property 0 (length s) 'face 'org-link s) |
2137 | (replace-match s t t)))) | |
2138 | (goto-char (point-min)) | |
2139 | (while (re-search-forward org-bracket-link-regexp nil t) | |
2c3ad40d | 2140 | (goto-char (1- (match-end 0))) |
c8d0cf5c | 2141 | (setq nodesc (not (match-end 3))) |
b349f79f CD |
2142 | (org-if-unprotected |
2143 | (let* ((xx (save-match-data | |
ce4fdcb9 CD |
2144 | (org-translate-link |
2145 | (org-link-expand-abbrev (match-string 1))))) | |
b349f79f | 2146 | (s (concat |
0bd48b37 | 2147 | "[[" (org-add-props (copy-sequence xx) |
c8d0cf5c | 2148 | nil 'org-protected t 'org-no-description nodesc) |
0bd48b37 | 2149 | "]" |
b349f79f CD |
2150 | (if (match-end 3) |
2151 | (match-string 2) | |
c8d0cf5c | 2152 | (concat "[" (copy-sequence xx) |
0bd48b37 | 2153 | "]")) |
b349f79f CD |
2154 | "]"))) |
2155 | (put-text-property 0 (length s) 'face 'org-link s) | |
2156 | (replace-match s t t)))))) | |
ff4be292 | 2157 | |
b349f79f CD |
2158 | (defun org-export-concatenate-multiline-links () |
2159 | "Find multi-line links and put it all into a single line. | |
2160 | This is to make sure that the line-processing export backends | |
2161 | can work correctly." | |
2162 | (goto-char (point-min)) | |
2163 | (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t) | |
ed21c5c8 | 2164 | (org-if-unprotected-at (match-beginning 1) |
8223b1d2 BG |
2165 | (replace-match "\\1 \\3") |
2166 | (goto-char (match-beginning 0))))) | |
b349f79f CD |
2167 | |
2168 | (defun org-export-concatenate-multiline-emphasis () | |
2169 | "Find multi-line emphasis and put it all into a single line. | |
2170 | This is to make sure that the line-processing export backends | |
2171 | can work correctly." | |
2172 | (goto-char (point-min)) | |
2173 | (while (re-search-forward org-emph-re nil t) | |
0bd48b37 CD |
2174 | (if (and (not (= (char-after (match-beginning 3)) |
2175 | (char-after (match-beginning 4)))) | |
2176 | (save-excursion (goto-char (match-beginning 0)) | |
afe98dfa CD |
2177 | (save-match-data |
2178 | (and (not (org-at-table-p)) | |
2179 | (not (org-at-heading-p)))))) | |
b349f79f CD |
2180 | (org-if-unprotected |
2181 | (subst-char-in-region (match-beginning 0) (match-end 0) | |
2182 | ?\n ?\ t) | |
2183 | (goto-char (1- (match-end 0)))) | |
2184 | (goto-char (1+ (match-beginning 0)))))) | |
2185 | ||
20908596 CD |
2186 | (defun org-export-grab-title-from-buffer () |
2187 | "Get a title for the current document, from looking at the buffer." | |
2188 | (let ((inhibit-read-only t)) | |
2189 | (save-excursion | |
2190 | (goto-char (point-min)) | |
621f83e4 CD |
2191 | (let ((end (if (looking-at org-outline-regexp) |
2192 | (point) | |
2193 | (save-excursion (outline-next-heading) (point))))) | |
20908596 CD |
2194 | (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t) |
2195 | ;; Mark the line so that it will not be exported as normal text. | |
e66ba1df BG |
2196 | (unless (org-in-block-p org-list-forbidden-blocks) |
2197 | (org-unmodified | |
2198 | (add-text-properties (match-beginning 0) (match-end 0) | |
2199 | (list :org-license-to-kill t)))) | |
20908596 CD |
2200 | ;; Return the title string |
2201 | (org-trim (match-string 0))))))) | |
2202 | ||
2203 | (defun org-export-get-title-from-subtree () | |
2204 | "Return subtree title and exclude it from export." | |
3ab2c837 | 2205 | (let ((rbeg (region-beginning)) (rend (region-end)) |
e66ba1df BG |
2206 | (inhibit-read-only t) |
2207 | (tags (plist-get (org-infile-export-plist) :tags)) | |
2208 | title) | |
20908596 | 2209 | (save-excursion |
b349f79f | 2210 | (goto-char rbeg) |
20908596 | 2211 | (when (and (org-at-heading-p) |
b349f79f | 2212 | (>= (org-end-of-subtree t t) rend)) |
e66ba1df BG |
2213 | (when (plist-member org-export-opt-plist :tags) |
2214 | (setq tags (or (plist-get org-export-opt-plist :tags) tags))) | |
20908596 | 2215 | ;; This is a subtree, we take the title from the first heading |
b349f79f | 2216 | (goto-char rbeg) |
e66ba1df | 2217 | (looking-at org-todo-line-tags-regexp) |
153ae947 | 2218 | (setq title (if (and (eq tags t) (match-string 4)) |
e66ba1df BG |
2219 | (format "%s\t%s" (match-string 3) (match-string 4)) |
2220 | (match-string 3))) | |
20908596 CD |
2221 | (org-unmodified |
2222 | (add-text-properties (point) (1+ (point-at-eol)) | |
b349f79f CD |
2223 | (list :org-license-to-kill t))) |
2224 | (setq title (or (org-entry-get nil "EXPORT_TITLE") title)))) | |
20908596 CD |
2225 | title)) |
2226 | ||
2227 | (defun org-solidify-link-text (s &optional alist) | |
2228 | "Take link text and make a safe target out of it." | |
2229 | (save-match-data | |
2230 | (let* ((rtn | |
2231 | (mapconcat | |
2232 | 'identity | |
3ab2c837 | 2233 | (org-split-string s "[^a-zA-Z0-9_\\.-]+") "-")) |
20908596 CD |
2234 | (a (assoc rtn alist))) |
2235 | (or (cdr a) rtn)))) | |
2236 | ||
c8d0cf5c | 2237 | (defun org-get-min-level (lines &optional offset) |
20908596 | 2238 | "Get the minimum level in LINES." |
65c439fd | 2239 | (let ((re "^\\(\\*+\\) ") l) |
20908596 CD |
2240 | (catch 'exit |
2241 | (while (setq l (pop lines)) | |
2242 | (if (string-match re l) | |
c8d0cf5c CD |
2243 | (throw 'exit (org-tr-level (- (length (match-string 1 l)) |
2244 | (or offset 0)))))) | |
20908596 CD |
2245 | 1))) |
2246 | ||
2247 | ;; Variable holding the vector with section numbers | |
2248 | (defvar org-section-numbers (make-vector org-level-max 0)) | |
2249 | ||
2250 | (defun org-init-section-numbers () | |
2251 | "Initialize the vector for the section numbers." | |
2252 | (let* ((level -1) | |
2253 | (numbers (nreverse (org-split-string "" "\\."))) | |
2254 | (depth (1- (length org-section-numbers))) | |
2255 | (i depth) number-string) | |
2256 | (while (>= i 0) | |
2257 | (if (> i level) | |
2258 | (aset org-section-numbers i 0) | |
2259 | (setq number-string (or (car numbers) "0")) | |
2260 | (if (string-match "\\`[A-Z]\\'" number-string) | |
2261 | (aset org-section-numbers i | |
2262 | (- (string-to-char number-string) ?A -1)) | |
b349f79f | 2263 | (aset org-section-numbers i (string-to-number number-string))) |
20908596 CD |
2264 | (pop numbers)) |
2265 | (setq i (1- i))))) | |
2266 | ||
2267 | (defun org-section-number (&optional level) | |
2268 | "Return a string with the current section number. | |
2269 | When LEVEL is non-nil, increase section numbers on that level." | |
b349f79f CD |
2270 | (let* ((depth (1- (length org-section-numbers))) |
2271 | (string "") | |
2272 | (fmts (car org-export-section-number-format)) | |
2273 | (term (cdr org-export-section-number-format)) | |
2274 | (sep "") | |
2275 | ctype fmt idx n) | |
20908596 CD |
2276 | (when level |
2277 | (when (> level -1) | |
2278 | (aset org-section-numbers | |
2279 | level (1+ (aref org-section-numbers level)))) | |
2280 | (setq idx (1+ level)) | |
2281 | (while (<= idx depth) | |
2282 | (if (not (= idx 1)) | |
2283 | (aset org-section-numbers idx 0)) | |
2284 | (setq idx (1+ idx)))) | |
2285 | (setq idx 0) | |
2286 | (while (<= idx depth) | |
b349f79f CD |
2287 | (when (> (aref org-section-numbers idx) 0) |
2288 | (setq fmt (or (pop fmts) fmt) | |
2289 | ctype (car fmt) | |
2290 | n (aref org-section-numbers idx) | |
2291 | string (if (> n 0) | |
2292 | (concat string sep (org-number-to-counter n ctype)) | |
2293 | (concat string ".0")) | |
2294 | sep (nth 1 fmt))) | |
20908596 CD |
2295 | (setq idx (1+ idx))) |
2296 | (save-match-data | |
2297 | (if (string-match "\\`\\([@0]\\.\\)+" string) | |
2298 | (setq string (replace-match "" t nil string))) | |
2299 | (if (string-match "\\(\\.0\\)+\\'" string) | |
2300 | (setq string (replace-match "" t nil string)))) | |
b349f79f CD |
2301 | (concat string term))) |
2302 | ||
2303 | (defun org-number-to-counter (n type) | |
2304 | "Concert number N to a string counter, according to TYPE. | |
2305 | TYPE must be a string, any of: | |
2306 | 1 number | |
2307 | A A,B,.... | |
2308 | a a,b,.... | |
33306645 | 2309 | I upper case roman numeral |
b349f79f CD |
2310 | i lower case roman numeral" |
2311 | (cond | |
2312 | ((equal type "1") (number-to-string n)) | |
2313 | ((equal type "A") (char-to-string (+ ?A n -1))) | |
2314 | ((equal type "a") (char-to-string (+ ?a n -1))) | |
2315 | ((equal type "I") (org-number-to-roman n)) | |
2316 | ((equal type "i") (downcase (org-number-to-roman n))) | |
2317 | (t (error "Invalid counter type `%s'" type)))) | |
2318 | ||
2319 | (defun org-number-to-roman (n) | |
2320 | "Convert integer N into a roman numeral." | |
2321 | (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD") | |
2322 | ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL") | |
2323 | ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV") | |
2324 | ( 1 . "I"))) | |
2325 | (res "")) | |
2326 | (if (<= n 0) | |
2327 | (number-to-string n) | |
2328 | (while roman | |
2329 | (if (>= n (caar roman)) | |
2330 | (setq n (- n (caar roman)) | |
2331 | res (concat res (cdar roman))) | |
2332 | (pop roman))) | |
2333 | res))) | |
2334 | ||
c8d0cf5c | 2335 | ;;; Macros |
ff4be292 | 2336 | |
c8d0cf5c CD |
2337 | (defun org-export-preprocess-apply-macros () |
2338 | "Replace macro references." | |
2339 | (goto-char (point-min)) | |
3ab2c837 | 2340 | (let (sy val key args args2 ind-str s n) |
c8d0cf5c | 2341 | (while (re-search-forward |
54a0dee5 | 2342 | "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}" |
c8d0cf5c | 2343 | nil t) |
3ab2c837 BG |
2344 | (unless (save-match-data (save-excursion |
2345 | (goto-char (point-at-bol)) | |
2346 | (looking-at "[ \t]*#\\+macro"))) | |
2347 | ;; Get macro name (KEY), arguments (ARGS), and indentation of | |
2348 | ;; current line (IND-STR) as strings. | |
8d642074 | 2349 | (setq key (downcase (match-string 1)) |
3ab2c837 BG |
2350 | args (match-string 3) |
2351 | ind-str (save-match-data (save-excursion | |
2352 | (beginning-of-line) | |
2353 | (looking-at "^\\([ \t]*\\).*") | |
2354 | (match-string 1)))) | |
2355 | ;; When macro is defined, retrieve replacement text in VAL, | |
2356 | ;; and proceed with expansion. | |
8d642074 CD |
2357 | (when (setq val (or (plist-get org-export-opt-plist |
2358 | (intern (concat ":macro-" key))) | |
2359 | (plist-get org-export-opt-plist | |
2360 | (intern (concat ":" key))))) | |
2361 | (save-match-data | |
9858f6c3 | 2362 | ;; If arguments are provided, first retrieve them properly |
3ab2c837 | 2363 | ;; (in ARGS, as a list), then replace them in VAL. |
8d642074 | 2364 | (when args |
afe98dfa | 2365 | (setq args (org-split-string args ",") args2 nil) |
8d642074 CD |
2366 | (while args |
2367 | (while (string-match "\\\\\\'" (car args)) | |
3ab2c837 | 2368 | ;; Repair bad splits. |
8d642074 | 2369 | (setcar (cdr args) (concat (substring (car args) 0 -1) |
afe98dfa | 2370 | "," (nth 1 args))) |
8d642074 CD |
2371 | (pop args)) |
2372 | (push (pop args) args2)) | |
afe98dfa | 2373 | (setq args (mapcar 'org-trim (nreverse args2))) |
8d642074 CD |
2374 | (setq s 0) |
2375 | (while (string-match "\\$\\([0-9]+\\)" val s) | |
2376 | (setq s (1+ (match-beginning 0)) | |
2377 | n (string-to-number (match-string 1 val))) | |
2378 | (and (>= (length args) n) | |
2379 | (setq val (replace-match (nth (1- n) args) t t val))))) | |
3ab2c837 | 2380 | ;; VAL starts with "(eval": it is a sexp, `eval' it. |
8d642074 CD |
2381 | (when (string-match "\\`(eval\\>" val) |
2382 | (setq val (eval (read val)))) | |
3ab2c837 BG |
2383 | ;; Ensure VAL is a string (or nil) and that each new line |
2384 | ;; is indented as the first one. | |
2385 | (setq val (and val | |
2386 | (mapconcat 'identity | |
2387 | (org-split-string | |
2388 | (if (stringp val) val (format "%s" val)) | |
2389 | "\n") | |
2390 | (concat "\n" ind-str))))) | |
8223b1d2 | 2391 | ;; Eventually do the replacement, if VAL isn't nil. Move |
3ab2c837 BG |
2392 | ;; point at beginning of macro for recursive expansions. |
2393 | (when val | |
2394 | (replace-match val t t) | |
2395 | (goto-char (match-beginning 0)))))))) | |
c8d0cf5c CD |
2396 | |
2397 | (defun org-export-apply-macros-in-string (s) | |
2398 | "Apply the macros in string S." | |
2399 | (when s | |
2400 | (with-temp-buffer | |
2401 | (insert s) | |
2402 | (org-export-preprocess-apply-macros) | |
2403 | (buffer-string)))) | |
b349f79f CD |
2404 | |
2405 | ;;; Include files | |
2406 | ||
2407 | (defun org-export-handle-include-files () | |
2408 | "Include the contents of include files, with proper formatting." | |
2409 | (let ((case-fold-search t) | |
8223b1d2 | 2410 | params file markup lang start end prefix prefix1 switches all minlevel currentlevel addlevel lines) |
b349f79f | 2411 | (goto-char (point-min)) |
8223b1d2 | 2412 | (while (re-search-forward "^#\\+include:[ \t]+\\(.*\\)" nil t) |
b349f79f | 2413 | (setq params (read (concat "(" (match-string 1) ")")) |
2c3ad40d CD |
2414 | prefix (org-get-and-remove-property 'params :prefix) |
2415 | prefix1 (org-get-and-remove-property 'params :prefix1) | |
acedf35c | 2416 | minlevel (org-get-and-remove-property 'params :minlevel) |
8223b1d2 | 2417 | addlevel (org-get-and-remove-property 'params :addlevel) |
3ab2c837 | 2418 | lines (org-get-and-remove-property 'params :lines) |
b349f79f CD |
2419 | file (org-symname-or-string (pop params)) |
2420 | markup (org-symname-or-string (pop params)) | |
0bd48b37 CD |
2421 | lang (and (member markup '("src" "SRC")) |
2422 | (org-symname-or-string (pop params))) | |
3ab2c837 | 2423 | switches (mapconcat #'(lambda (x) (format "%s" x)) params " ") |
54a0dee5 | 2424 | start nil end nil) |
b349f79f | 2425 | (delete-region (match-beginning 0) (match-end 0)) |
8223b1d2 | 2426 | (setq currentlevel (or (org-current-level) 0)) |
b349f79f CD |
2427 | (if (or (not file) |
2428 | (not (file-exists-p file)) | |
2429 | (not (file-readable-p file))) | |
2430 | (insert (format "CANNOT INCLUDE FILE %s" file)) | |
86fbb8ca | 2431 | (setq all (cons file all)) |
b349f79f CD |
2432 | (when markup |
2433 | (if (equal (downcase markup) "src") | |
0bd48b37 CD |
2434 | (setq start (format "#+begin_src %s %s\n" |
2435 | (or lang "fundamental") | |
2436 | (or switches "")) | |
b349f79f | 2437 | end "#+end_src") |
0bd48b37 | 2438 | (setq start (format "#+begin_%s %s\n" markup switches) |
b349f79f CD |
2439 | end (format "#+end_%s" markup)))) |
2440 | (insert (or start "")) | |
54a0dee5 | 2441 | (insert (org-get-file-contents (expand-file-name file) |
8223b1d2 | 2442 | prefix prefix1 markup currentlevel minlevel addlevel lines)) |
b349f79f | 2443 | (or (bolp) (newline)) |
86fbb8ca CD |
2444 | (insert (or end "")))) |
2445 | all)) | |
2446 | ||
2447 | (defun org-export-handle-include-files-recurse () | |
2448 | "Recursively include files aborting on circular inclusion." | |
2449 | (let ((now (list org-current-export-file)) all) | |
2450 | (while now | |
2451 | (setq all (append now all)) | |
2452 | (setq now (org-export-handle-include-files)) | |
2453 | (let ((intersection | |
2454 | (delq nil | |
2455 | (mapcar (lambda (el) (when (member el all) el)) now)))) | |
2456 | (when intersection | |
2457 | (error "Recursive #+INCLUDE: %S" intersection)))))) | |
b349f79f | 2458 | |
8223b1d2 | 2459 | (defun org-get-file-contents (file &optional prefix prefix1 markup minlevel parentlevel addlevel lines) |
2c3ad40d CD |
2460 | "Get the contents of FILE and return them as a string. |
2461 | If PREFIX is a string, prepend it to each line. If PREFIX1 | |
c8d0cf5c CD |
2462 | is a string, prepend it to the first line instead of PREFIX. |
2463 | If MARKUP, don't protect org-like lines, the exporter will | |
8223b1d2 BG |
2464 | take care of the block they are in. If ADDLEVEL is a number, |
2465 | demote included file to current heading level+ADDLEVEL. | |
2466 | If LINES is a string specifying a range of lines, | |
2467 | include only those lines." | |
54a0dee5 | 2468 | (if (stringp markup) (setq markup (downcase markup))) |
2c3ad40d CD |
2469 | (with-temp-buffer |
2470 | (insert-file-contents file) | |
3ab2c837 BG |
2471 | (when lines |
2472 | (let* ((lines (split-string lines "-")) | |
2473 | (lbeg (string-to-number (car lines))) | |
2474 | (lend (string-to-number (cadr lines))) | |
2475 | (beg (if (zerop lbeg) (point-min) | |
2476 | (goto-char (point-min)) | |
2477 | (forward-line (1- lbeg)) | |
2478 | (point))) | |
2479 | (end (if (zerop lend) (point-max) | |
2480 | (goto-char (point-min)) | |
2481 | (forward-line (1- lend)) | |
2482 | (point)))) | |
2483 | (narrow-to-region beg end))) | |
2c3ad40d CD |
2484 | (when (or prefix prefix1) |
2485 | (goto-char (point-min)) | |
2486 | (while (not (eobp)) | |
2487 | (insert (or prefix1 prefix)) | |
86fbb8ca | 2488 | (setq prefix1 "") |
2c3ad40d | 2489 | (beginning-of-line 2))) |
c8d0cf5c | 2490 | (buffer-string) |
54a0dee5 | 2491 | (when (member markup '("src" "example")) |
c8d0cf5c | 2492 | (goto-char (point-min)) |
54a0dee5 | 2493 | (while (re-search-forward "^\\([*#]\\|[ \t]*#\\+\\)" nil t) |
c8d0cf5c CD |
2494 | (goto-char (match-beginning 0)) |
2495 | (insert ",") | |
2496 | (end-of-line 1))) | |
acedf35c CD |
2497 | (when minlevel |
2498 | (dotimes (lvl minlevel) | |
2499 | (org-map-region 'org-demote (point-min) (point-max)))) | |
8223b1d2 BG |
2500 | (when addlevel |
2501 | (let ((inclevel (or (if (org-before-first-heading-p) | |
2502 | (1- (and (outline-next-heading) | |
2503 | (org-current-level))) | |
2504 | (1- (org-current-level))) | |
2505 | 0))) | |
2506 | (dotimes (level (- (+ parentlevel addlevel) inclevel)) | |
2507 | (org-map-region 'org-demote (point-min) (point-max))))) | |
2c3ad40d CD |
2508 | (buffer-string))) |
2509 | ||
2510 | (defun org-get-and-remove-property (listvar prop) | |
2511 | "Check if the value of LISTVAR contains PROP as a property. | |
2512 | If yes, return the value of that property (i.e. the element following | |
2513 | in the list) and remove property and value from the list in LISTVAR." | |
2514 | (let ((list (symbol-value listvar)) m v) | |
2515 | (when (setq m (member prop list)) | |
2516 | (setq v (nth 1 m)) | |
2517 | (if (equal (car list) prop) | |
2518 | (set listvar (cddr list)) | |
2519 | (setcdr (nthcdr (- (length list) (length m) 1) list) | |
2520 | (cddr m)) | |
2521 | (set listvar list))) | |
2522 | v)) | |
2523 | ||
b349f79f CD |
2524 | (defun org-symname-or-string (s) |
2525 | (if (symbolp s) | |
2526 | (if s (symbol-name s) s) | |
2527 | s)) | |
2528 | ||
0bd48b37 CD |
2529 | ;;; Fontification and line numbers for code examples |
2530 | ||
2531 | (defvar org-export-last-code-line-counter-value 0) | |
2532 | ||
3ab2c837 | 2533 | (defun org-export-replace-src-segments-and-examples () |
b349f79f | 2534 | "Replace source code segments with special code for export." |
0bd48b37 | 2535 | (setq org-export-last-code-line-counter-value 0) |
b349f79f | 2536 | (let ((case-fold-search t) |
ed21c5c8 | 2537 | lang code trans opts indent caption) |
b349f79f CD |
2538 | (goto-char (point-min)) |
2539 | (while (re-search-forward | |
86fbb8ca | 2540 | "\\(^\\([ \t]*\\)#\\+BEGIN_SRC:?\\([ \t]+\\([^ \t\n]+\\)\\)?\\(.*\\)\n\\([^\000]+?\n\\)[ \t]*#\\+END_SRC.*\n?\\)\\|\\(^\\([ \t]*\\)#\\+BEGIN_EXAMPLE:?\\(?:[ \t]+\\(.*\\)\\)?\n\\([^\000]+?\n\\)[ \t]*#\\+END_EXAMPLE.*\n?\\)" |
b349f79f | 2541 | nil t) |
0bd48b37 | 2542 | (if (match-end 1) |
86fbb8ca CD |
2543 | (if (not (match-string 4)) |
2544 | (error "Source block missing language specification: %s" | |
2545 | (let* ((body (match-string 6)) | |
2546 | (nothing (message "body:%s" body)) | |
2547 | (preview (or (and (string-match | |
2548 | "^[ \t]*\\([^\n\r]*\\)" body) | |
2549 | (match-string 1 body)) body))) | |
2550 | (if (> (length preview) 35) | |
2551 | (concat (substring preview 0 32) "...") | |
2552 | preview))) | |
2553 | ;; src segments | |
2554 | (setq lang (match-string 4) | |
2555 | opts (match-string 5) | |
2556 | code (match-string 6) | |
2557 | indent (length (match-string 2)) | |
2558 | caption (get-text-property 0 'org-caption (match-string 0)))) | |
0bd48b37 | 2559 | (setq lang nil |
86fbb8ca CD |
2560 | opts (match-string 9) |
2561 | code (match-string 10) | |
2562 | indent (length (match-string 8)) | |
ed21c5c8 | 2563 | caption (get-text-property 0 'org-caption (match-string 0)))) |
0bd48b37 CD |
2564 | |
2565 | (setq trans (org-export-format-source-code-or-example | |
3ab2c837 | 2566 | lang code opts indent caption)) |
b349f79f CD |
2567 | (replace-match trans t t)))) |
2568 | ||
c8d0cf5c | 2569 | (defvar org-export-latex-verbatim-wrap) ;; defined in org-latex.el |
54a0dee5 CD |
2570 | (defvar org-export-latex-listings) ;; defined in org-latex.el |
2571 | (defvar org-export-latex-listings-langs) ;; defined in org-latex.el | |
afe98dfa CD |
2572 | (defvar org-export-latex-listings-w-names) ;; defined in org-latex.el |
2573 | (defvar org-export-latex-minted-langs) ;; defined in org-latex.el | |
3ab2c837 BG |
2574 | (defvar org-export-latex-custom-lang-environments) ;; defined in org-latex.el |
2575 | (defvar org-export-latex-listings-options) ;; defined in org-latex.el | |
2576 | (defvar org-export-latex-minted-options) ;; defined in org-latex.el | |
2577 | ||
2578 | (defun org-remove-formatting-on-newlines-in-region (beg end) | |
8223b1d2 | 2579 | "Remove formatting on newline characters." |
3ab2c837 BG |
2580 | (interactive "r") |
2581 | (save-excursion | |
2582 | (goto-char beg) | |
2583 | (while (progn (end-of-line) (< (point) end)) | |
2584 | (put-text-property (point) (1+ (point)) 'face nil) | |
2585 | (forward-char 1)))) | |
2586 | ||
c8d0cf5c | 2587 | (defun org-export-format-source-code-or-example |
3ab2c837 | 2588 | (lang code &optional opts indent caption) |
b349f79f | 2589 | "Format CODE from language LANG and return it formatted for export. |
3ab2c837 BG |
2590 | The CODE is marked up in `org-export-current-backend' format. |
2591 | ||
2592 | Check if a function by name | |
8223b1d2 BG |
2593 | \"org-<backend>-format-source-code-or-example\" is bound. If yes, |
2594 | use it as the custom formatter. Otherwise, use the default | |
2595 | formatter. Default formatters are provided for docbook, html, | |
2596 | latex and ascii backends. For example, use | |
3ab2c837 BG |
2597 | `org-html-format-source-code-or-example' to provide a custom |
2598 | formatter for export to \"html\". | |
2599 | ||
0bd48b37 | 2600 | If LANG is nil, do not add any fontification. |
8bfe682a CD |
2601 | OPTS contains formatting options, like `-n' for triggering numbering lines, |
2602 | and `+n' for continuing previous numbering. | |
0bd48b37 | 2603 | Code formatting according to language currently only works for HTML. |
c8d0cf5c CD |
2604 | Numbering lines works for all three major backends (html, latex, and ascii). |
2605 | INDENT was the original indentation of the block." | |
b349f79f | 2606 | (save-match-data |
3ab2c837 BG |
2607 | (let* ((backend-name (symbol-name org-export-current-backend)) |
2608 | (backend-formatter | |
2609 | (intern (format "org-%s-format-source-code-or-example" | |
2610 | backend-name))) | |
2611 | (backend-feature (intern (concat "org-" backend-name))) | |
2612 | (backend-formatter | |
2613 | (and (require (intern (concat "org-" backend-name)) nil) | |
2614 | (fboundp backend-formatter) backend-formatter)) | |
2615 | num cont rtn rpllbl keepp textareap preserve-indentp cols rows fmt) | |
0bd48b37 CD |
2616 | (setq opts (or opts "") |
2617 | num (string-match "[-+]n\\>" opts) | |
2618 | cont (string-match "\\+n\\>" opts) | |
2619 | rpllbl (string-match "-r\\>" opts) | |
2620 | keepp (string-match "-k\\>" opts) | |
2621 | textareap (string-match "-t\\>" opts) | |
8bfe682a CD |
2622 | preserve-indentp (or org-src-preserve-indentation |
2623 | (string-match "-i\\>" opts)) | |
0bd48b37 CD |
2624 | cols (if (string-match "-w[ \t]+\\([0-9]+\\)" opts) |
2625 | (string-to-number (match-string 1 opts)) | |
2626 | 80) | |
2627 | rows (if (string-match "-h[ \t]+\\([0-9]+\\)" opts) | |
2628 | (string-to-number (match-string 1 opts)) | |
2629 | (org-count-lines code)) | |
2630 | fmt (if (string-match "-l[ \t]+\"\\([^\"\n]+\\)\"" opts) | |
2631 | (match-string 1 opts))) | |
3ab2c837 | 2632 | (when (and textareap (eq org-export-current-backend 'html)) |
0bd48b37 CD |
2633 | ;; we cannot use numbering or highlighting. |
2634 | (setq num nil cont nil lang nil)) | |
2635 | (if keepp (setq rpllbl 'keep)) | |
8bfe682a | 2636 | (setq rtn (if preserve-indentp code (org-remove-indentation code))) |
c8d0cf5c | 2637 | (when (string-match "^," rtn) |
0bd48b37 CD |
2638 | (setq rtn (with-temp-buffer |
2639 | (insert rtn) | |
2640 | ;; Free up the protected lines | |
2641 | (goto-char (point-min)) | |
2642 | (while (re-search-forward "^," nil t) | |
c8d0cf5c CD |
2643 | (if (or (equal lang "org") |
2644 | (save-match-data | |
2645 | (looking-at "\\([*#]\\|[ \t]*#\\+\\)"))) | |
2646 | (replace-match "")) | |
0bd48b37 CD |
2647 | (end-of-line 1)) |
2648 | (buffer-string)))) | |
2649 | ;; Now backend-specific coding | |
c8d0cf5c CD |
2650 | (setq rtn |
2651 | (cond | |
3ab2c837 BG |
2652 | (backend-formatter |
2653 | (funcall backend-formatter rtn lang caption textareap cols rows num | |
2654 | cont rpllbl fmt)) | |
2655 | ((eq org-export-current-backend 'docbook) | |
2656 | (setq rtn (org-export-number-lines rtn 0 0 num cont rpllbl fmt)) | |
2657 | (concat "<programlisting><![CDATA[" | |
2658 | rtn | |
2659 | "]]></programlisting>\n")) | |
2660 | ((eq org-export-current-backend 'html) | |
c8d0cf5c CD |
2661 | ;; We are exporting to HTML |
2662 | (when lang | |
86fbb8ca CD |
2663 | (if (featurep 'xemacs) |
2664 | (require 'htmlize) | |
2665 | (require 'htmlize nil t)) | |
c8d0cf5c CD |
2666 | (when (not (fboundp 'htmlize-region-for-paste)) |
2667 | ;; we do not have htmlize.el, or an old version of it | |
2668 | (setq lang nil) | |
2669 | (message | |
2670 | "htmlize.el 1.34 or later is needed for source code formatting"))) | |
2671 | ||
2672 | (if lang | |
54a0dee5 CD |
2673 | (let* ((lang-m (when lang |
2674 | (or (cdr (assoc lang org-src-lang-modes)) | |
2675 | lang))) | |
2676 | (mode (and lang-m (intern | |
2677 | (concat | |
2678 | (if (symbolp lang-m) | |
2679 | (symbol-name lang-m) | |
2680 | lang-m) | |
2681 | "-mode")))) | |
c8d0cf5c CD |
2682 | (org-inhibit-startup t) |
2683 | (org-startup-folded nil)) | |
2684 | (setq rtn | |
2685 | (with-temp-buffer | |
2686 | (insert rtn) | |
2687 | (if (functionp mode) | |
2688 | (funcall mode) | |
2689 | (fundamental-mode)) | |
2690 | (font-lock-fontify-buffer) | |
3ab2c837 BG |
2691 | ;; markup each line separately |
2692 | (org-remove-formatting-on-newlines-in-region (point-min) (point-max)) | |
c8d0cf5c CD |
2693 | (org-src-mode) |
2694 | (set-buffer-modified-p nil) | |
2695 | (org-export-htmlize-region-for-paste | |
2696 | (point-min) (point-max)))) | |
2697 | (if (string-match "<pre\\([^>]*\\)>\n*" rtn) | |
ed21c5c8 CD |
2698 | (setq rtn |
2699 | (concat | |
2700 | (if caption | |
2701 | (concat | |
2702 | "<div class=\"org-src-container\">" | |
2703 | (format | |
2704 | "<label class=\"org-src-name\">%s</label>" | |
2705 | caption)) | |
2706 | "") | |
2707 | (replace-match | |
2708 | (format "<pre class=\"src src-%s\">\n" lang) | |
2709 | t t rtn) | |
2710 | (if caption "</div>" ""))))) | |
c8d0cf5c CD |
2711 | (if textareap |
2712 | (setq rtn (concat | |
ed21c5c8 | 2713 | (format "<p>\n<textarea cols=\"%d\" rows=\"%d\">" |
c8d0cf5c CD |
2714 | cols rows) |
2715 | rtn "</textarea>\n</p>\n")) | |
2716 | (with-temp-buffer | |
2717 | (insert rtn) | |
2718 | (goto-char (point-min)) | |
2719 | (while (re-search-forward "[<>&]" nil t) | |
2720 | (replace-match (cdr (assq (char-before) | |
2721 | '((?&."&")(?<."<")(?>.">")))) | |
2722 | t t)) | |
2723 | (setq rtn (buffer-string))) | |
2724 | (setq rtn (concat "<pre class=\"example\">\n" rtn "</pre>\n")))) | |
2725 | (unless textareap | |
3ab2c837 | 2726 | (setq rtn (org-export-number-lines rtn 1 1 num cont rpllbl fmt))) |
c8d0cf5c CD |
2727 | (if (string-match "\\(\\`<[^>]*>\\)\n" rtn) |
2728 | (setq rtn (replace-match "\\1" t nil rtn))) | |
3ab2c837 BG |
2729 | rtn) |
2730 | ((eq org-export-current-backend 'latex) | |
2731 | (setq rtn (org-export-number-lines rtn 0 0 num cont rpllbl fmt)) | |
2732 | (cond | |
2733 | ((and lang org-export-latex-listings) | |
8223b1d2 BG |
2734 | (let* ((make-option-string |
2735 | (lambda (pair) | |
2736 | (concat (first pair) | |
2737 | (if (> (length (second pair)) 0) | |
2738 | (concat "=" (second pair)))))) | |
2739 | (lang-sym (intern lang)) | |
2740 | (minted-p (eq org-export-latex-listings 'minted)) | |
2741 | (listings-p (not minted-p)) | |
2742 | (backend-lang | |
2743 | (or (cadr | |
2744 | (assq | |
2745 | lang-sym | |
2746 | (cond | |
2747 | (minted-p org-export-latex-minted-langs) | |
2748 | (listings-p org-export-latex-listings-langs)))) | |
2749 | lang)) | |
2750 | (custom-environment | |
2751 | (cadr | |
2752 | (assq | |
2753 | lang-sym | |
2754 | org-export-latex-custom-lang-environments)))) | |
2755 | (concat | |
2756 | (when (and listings-p (not custom-environment)) | |
2757 | (format | |
2758 | "\\lstset{%s}\n" | |
2759 | (mapconcat | |
2760 | make-option-string | |
2761 | (append org-export-latex-listings-options | |
2762 | `(("language" ,backend-lang))) ","))) | |
2763 | (when (and caption org-export-latex-listings-w-names) | |
2764 | (format | |
2765 | "\n%s $\\equiv$ \n" | |
2766 | (replace-regexp-in-string "_" "\\\\_" caption))) | |
2767 | (cond | |
2768 | (custom-environment | |
2769 | (format "\\begin{%s}\n%s\\end{%s}\n" | |
2770 | custom-environment rtn custom-environment)) | |
2771 | (listings-p | |
2772 | (format "\\begin{%s}\n%s\\end{%s}" | |
2773 | "lstlisting" rtn "lstlisting")) | |
2774 | (minted-p | |
2775 | (format | |
2776 | "\\begin{minted}[%s]{%s}\n%s\\end{minted}" | |
2777 | (mapconcat make-option-string | |
2778 | org-export-latex-minted-options ",") | |
2779 | backend-lang rtn)))))) | |
3ab2c837 BG |
2780 | (t (concat (car org-export-latex-verbatim-wrap) |
2781 | rtn (cdr org-export-latex-verbatim-wrap))))) | |
8223b1d2 BG |
2782 | ((eq org-export-current-backend 'ascii) |
2783 | ;; This is not HTML or LaTeX, so just make it an example. | |
2784 | (setq rtn (org-export-number-lines rtn 0 0 num cont rpllbl fmt)) | |
2785 | (concat caption "\n" | |
3ab2c837 BG |
2786 | (concat |
2787 | (mapconcat | |
2788 | (lambda (l) (concat " " l)) | |
2789 | (org-split-string rtn "\n") | |
2790 | "\n") | |
8223b1d2 | 2791 | "\n"))) |
3ab2c837 BG |
2792 | (t |
2793 | (error "Don't know how to markup source or example block in %s" | |
2794 | (upcase backend-name))))) | |
2795 | (setq rtn | |
2796 | (concat | |
2797 | "\n#+BEGIN_" backend-name "\n" | |
2798 | (org-add-props rtn | |
2799 | '(org-protected t org-example t org-native-text t)) | |
e66ba1df | 2800 | "\n#+END_" backend-name "\n")) |
c8d0cf5c | 2801 | (org-add-props rtn nil 'original-indentation indent)))) |
0bd48b37 | 2802 | |
3ab2c837 | 2803 | (defun org-export-number-lines (text &optional skip1 skip2 number cont |
e66ba1df BG |
2804 | replace-labels label-format preprocess) |
2805 | "Apply line numbers to literal examples and handle code references. | |
2806 | Handle user-specified options under info node `(org)Literal | |
2807 | examples' and return the modified source block. | |
2808 | ||
2809 | TEXT contains the source or example block. | |
2810 | ||
2811 | SKIP1 and SKIP2 are the number of lines that are to be skipped at | |
2812 | the beginning and end of TEXT. Use these to skip over | |
2813 | backend-specific lines pre-pended or appended to the original | |
2814 | source block. | |
2815 | ||
2816 | NUMBER is non-nil if the literal example specifies \"+n\" or | |
8223b1d2 | 2817 | \"-n\" switch. If NUMBER is non-nil add line numbers. |
e66ba1df BG |
2818 | |
2819 | CONT is non-nil if the literal example specifies \"+n\" switch. | |
2820 | If CONT is nil, start numbering this block from 1. Otherwise | |
2821 | continue numbering from the last numbered block. | |
2822 | ||
2823 | REPLACE-LABELS is dual-purpose. | |
2824 | 1. It controls the retention of labels in the exported block. | |
2825 | 2. It specifies in what manner the links (or references) to a | |
27e428e7 | 2826 | labeled line be formatted. |
e66ba1df BG |
2827 | |
2828 | REPLACE-LABELS is the symbol `keep' if the literal example | |
2829 | specifies \"-k\" option, is numeric if the literal example | |
2830 | specifies \"-r\" option and is nil otherwise. | |
2831 | ||
2832 | Handle REPLACE-LABELS as below: | |
2833 | - If nil, retain labels in the exported block and use | |
27e428e7 | 2834 | user-provided labels for referencing the labeled lines. |
e66ba1df | 2835 | - If it is a number, remove labels in the exported block and use |
27e428e7 | 2836 | one of line numbers or labels for referencing labeled lines based |
e66ba1df BG |
2837 | on NUMBER option. |
2838 | - If it is a keep, retain labels in the exported block and use | |
27e428e7 | 2839 | one of line numbers or labels for referencing labeled lines |
e66ba1df BG |
2840 | based on NUMBER option. |
2841 | ||
2842 | LABEL-FORMAT is the value of \"-l\" switch associated with | |
2843 | literal example. See `org-coderef-label-format'. | |
2844 | ||
2845 | PREPROCESS is intended for backend-agnostic handling of source | |
2846 | block numbering. When non-nil do the following: | |
2847 | - do not number the lines | |
2848 | - always strip the labels from exported block | |
27e428e7 PE |
2849 | - do not make the labeled line a target of an incoming link. |
2850 | Instead mark the labeled line with `org-coderef' property and | |
e66ba1df | 2851 | store the label in it." |
0bd48b37 | 2852 | (setq skip1 (or skip1 0) skip2 (or skip2 0)) |
e66ba1df | 2853 | (if (and number (not cont)) (setq org-export-last-code-line-counter-value 0)) |
0bd48b37 CD |
2854 | (with-temp-buffer |
2855 | (insert text) | |
2856 | (goto-char (point-max)) | |
2857 | (skip-chars-backward " \t\n\r") | |
2858 | (delete-region (point) (point-max)) | |
2859 | (beginning-of-line (- 1 skip2)) | |
2860 | (let* ((last (org-current-line)) | |
2861 | (n org-export-last-code-line-counter-value) | |
2862 | (nmax (+ n (- last skip1))) | |
2863 | (fmt (format "%%%dd: " (length (number-to-string nmax)))) | |
2864 | (fm | |
2865 | (cond | |
3ab2c837 | 2866 | ((eq org-export-current-backend 'html) (format "<span class=\"linenr\">%s</span>" |
8223b1d2 | 2867 | fmt)) |
3ab2c837 BG |
2868 | ((eq org-export-current-backend 'ascii) fmt) |
2869 | ((eq org-export-current-backend 'latex) fmt) | |
2870 | ((eq org-export-current-backend 'docbook) fmt) | |
0bd48b37 CD |
2871 | (t ""))) |
2872 | (label-format (or label-format org-coderef-label-format)) | |
2873 | (label-pre (if (string-match "%s" label-format) | |
2874 | (substring label-format 0 (match-beginning 0)) | |
2875 | label-format)) | |
2876 | (label-post (if (string-match "%s" label-format) | |
2877 | (substring label-format (match-end 0)) | |
2878 | "")) | |
2879 | (lbl-re | |
c8d0cf5c | 2880 | (concat |
0bd48b37 CD |
2881 | ".*?\\S-.*?\\([ \t]*\\(" |
2882 | (regexp-quote label-pre) | |
c8d0cf5c | 2883 | "\\([-a-zA-Z0-9_ ]+\\)" |
0bd48b37 CD |
2884 | (regexp-quote label-post) |
2885 | "\\)\\)")) | |
2886 | ref) | |
2887 | ||
54a0dee5 | 2888 | (org-goto-line (1+ skip1)) |
0bd48b37 | 2889 | (while (and (re-search-forward "^" nil t) (not (eobp)) (< n nmax)) |
e66ba1df BG |
2890 | (when number (incf n)) |
2891 | (if (or preprocess (not number)) | |
2892 | (forward-char 1) | |
2893 | (insert (format fm n))) | |
c8d0cf5c | 2894 | (when (looking-at lbl-re) |
0bd48b37 | 2895 | (setq ref (match-string 3)) |
c8d0cf5c CD |
2896 | (cond ((numberp replace-labels) |
2897 | ;; remove labels; use numbers for references when lines | |
2898 | ;; are numbered, use labels otherwise | |
2899 | (delete-region (match-beginning 1) (match-end 1)) | |
2900 | (push (cons ref (if (> n 0) n ref)) org-export-code-refs)) | |
2901 | ((eq replace-labels 'keep) | |
2902 | ;; don't remove labels; use numbers for references when | |
2903 | ;; lines are numbered, use labels otherwise | |
2904 | (goto-char (match-beginning 2)) | |
2905 | (delete-region (match-beginning 2) (match-end 2)) | |
e66ba1df BG |
2906 | (unless preprocess |
2907 | (insert "(" ref ")")) | |
c8d0cf5c CD |
2908 | (push (cons ref (if (> n 0) n (concat "(" ref ")"))) |
2909 | org-export-code-refs)) | |
2910 | (t | |
2911 | ;; don't remove labels and don't use numbers for | |
2912 | ;; references | |
2913 | (goto-char (match-beginning 2)) | |
2914 | (delete-region (match-beginning 2) (match-end 2)) | |
e66ba1df BG |
2915 | (unless preprocess |
2916 | (insert "(" ref ")")) | |
c8d0cf5c | 2917 | (push (cons ref (concat "(" ref ")")) org-export-code-refs))) |
e66ba1df | 2918 | (when (and (eq org-export-current-backend 'html) (not preprocess)) |
0bd48b37 CD |
2919 | (save-excursion |
2920 | (beginning-of-line 1) | |
2921 | (insert (format "<span id=\"coderef-%s\" class=\"coderef-off\">" | |
2922 | ref)) | |
2923 | (end-of-line 1) | |
e66ba1df BG |
2924 | (insert "</span>"))) |
2925 | (when preprocess | |
2926 | (add-text-properties | |
2927 | (point-at-bol) (point-at-eol) (list 'org-coderef ref))))) | |
0bd48b37 CD |
2928 | (setq org-export-last-code-line-counter-value n) |
2929 | (goto-char (point-max)) | |
2930 | (newline) | |
2931 | (buffer-string)))) | |
20908596 | 2932 | |
20908596 CD |
2933 | (defun org-search-todo-below (line lines level) |
2934 | "Search the subtree below LINE for any TODO entries." | |
2935 | (let ((rest (cdr (memq line lines))) | |
2936 | (re org-todo-line-regexp) | |
2937 | line lv todo) | |
2938 | (catch 'exit | |
2939 | (while (setq line (pop rest)) | |
2940 | (if (string-match re line) | |
2941 | (progn | |
2942 | (setq lv (- (match-end 1) (match-beginning 1)) | |
2943 | todo (and (match-beginning 2) | |
2944 | (not (member (match-string 2 line) | |
8223b1d2 | 2945 | org-done-keywords)))) |
20908596 CD |
2946 | ; TODO, not DONE |
2947 | (if (<= lv level) (throw 'exit nil)) | |
2948 | (if todo (throw 'exit t)))))))) | |
2949 | ||
20908596 CD |
2950 | ;;;###autoload |
2951 | (defun org-export-visible (type arg) | |
2952 | "Create a copy of the visible part of the current buffer, and export it. | |
2953 | The copy is created in a temporary buffer and removed after use. | |
86fbb8ca CD |
2954 | TYPE is the final key (as a string) that also selects the export command in |
2955 | the \\<org-mode-map>\\[org-export] export dispatcher. | |
20908596 CD |
2956 | As a special case, if the you type SPC at the prompt, the temporary |
2957 | org-mode file will not be removed but presented to you so that you can | |
2958 | continue to use it. The prefix arg ARG is passed through to the exporting | |
2959 | command." | |
2960 | (interactive | |
2961 | (list (progn | |
ed21c5c8 | 2962 | (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]buffer with HTML [D]ocBook [l]atex [p]df [d]view pdf [L]atex buffer [x]OXO [ ]keep buffer") |
20908596 CD |
2963 | (read-char-exclusive)) |
2964 | current-prefix-arg)) | |
3ab2c837 | 2965 | (if (not (member type '(?a ?n ?u ?\C-a ?b ?\C-b ?h ?D ?x ?\ ?l ?p ?d ?L ?H ?R))) |
20908596 CD |
2966 | (error "Invalid export key")) |
2967 | (let* ((binding (cdr (assoc type | |
ed21c5c8 CD |
2968 | '( |
2969 | (?a . org-export-as-ascii) | |
c8d0cf5c | 2970 | (?A . org-export-as-ascii-to-buffer) |
ed21c5c8 CD |
2971 | (?n . org-export-as-latin1) |
2972 | (?N . org-export-as-latin1-to-buffer) | |
2973 | (?u . org-export-as-utf8) | |
2974 | (?U . org-export-as-utf8-to-buffer) | |
20908596 CD |
2975 | (?\C-a . org-export-as-ascii) |
2976 | (?b . org-export-as-html-and-open) | |
2977 | (?\C-b . org-export-as-html-and-open) | |
2978 | (?h . org-export-as-html) | |
2979 | (?H . org-export-as-html-to-buffer) | |
2980 | (?R . org-export-region-as-html) | |
c8d0cf5c | 2981 | (?D . org-export-as-docbook) |
ed21c5c8 CD |
2982 | |
2983 | (?l . org-export-as-latex) | |
2984 | (?p . org-export-as-pdf) | |
2985 | (?d . org-export-as-pdf-and-open) | |
2986 | (?L . org-export-as-latex-to-buffer) | |
2987 | ||
20908596 CD |
2988 | (?x . org-export-as-xoxo))))) |
2989 | (keepp (equal type ?\ )) | |
2990 | (file buffer-file-name) | |
2991 | (buffer (get-buffer-create "*Org Export Visible*")) | |
2992 | s e) | |
2993 | ;; Need to hack the drawers here. | |
2994 | (save-excursion | |
2995 | (goto-char (point-min)) | |
2996 | (while (re-search-forward org-drawer-regexp nil t) | |
2997 | (goto-char (match-beginning 1)) | |
3ab2c837 | 2998 | (or (outline-invisible-p) (org-flag-drawer nil)))) |
20908596 CD |
2999 | (with-current-buffer buffer (erase-buffer)) |
3000 | (save-excursion | |
3001 | (setq s (goto-char (point-min))) | |
3002 | (while (not (= (point) (point-max))) | |
3003 | (goto-char (org-find-invisible)) | |
3004 | (append-to-buffer buffer s (point)) | |
3005 | (setq s (goto-char (org-find-visible)))) | |
3006 | (org-cycle-hide-drawers 'all) | |
3007 | (goto-char (point-min)) | |
3008 | (unless keepp | |
3009 | ;; Copy all comment lines to the end, to make sure #+ settings are | |
3010 | ;; still available for the second export step. Kind of a hack, but | |
3011 | ;; does do the trick. | |
3012 | (if (looking-at "#[^\r\n]*") | |
3013 | (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0)))) | |
acedf35c CD |
3014 | (when (re-search-forward "^\\*+[ \t]+" nil t) |
3015 | (while (re-search-backward "[\n\r]#[^\n\r]*" nil t) | |
3016 | (append-to-buffer buffer (1+ (match-beginning 0)) | |
3017 | (min (point-max) (1+ (match-end 0))))))) | |
20908596 CD |
3018 | (set-buffer buffer) |
3019 | (let ((buffer-file-name file) | |
3020 | (org-inhibit-startup t)) | |
3021 | (org-mode) | |
3022 | (show-all) | |
3023 | (unless keepp (funcall binding arg)))) | |
3024 | (if (not keepp) | |
3025 | (kill-buffer buffer) | |
3026 | (switch-to-buffer-other-window buffer) | |
3027 | (goto-char (point-min))))) | |
3028 | ||
c8d0cf5c CD |
3029 | (defvar org-export-htmlized-org-css-url) ;; defined in org-html.el |
3030 | ||
afe98dfa CD |
3031 | (defun org-export-string (string fmt &optional dir) |
3032 | "Export STRING to FMT using existing export facilities. | |
3033 | During export STRING is saved to a temporary file whose location | |
3034 | could vary. Optional argument DIR can be used to force the | |
3035 | directory in which the temporary file is created during export | |
3036 | which can be useful for resolving relative paths. Dir defaults | |
3037 | to the value of `temporary-file-directory'." | |
3038 | (let ((temporary-file-directory (or dir temporary-file-directory)) | |
3039 | (tmp-file (make-temp-file "org-"))) | |
3040 | (unwind-protect | |
3041 | (with-temp-buffer | |
3042 | (insert string) | |
3043 | (write-file tmp-file) | |
3044 | (org-load-modules-maybe) | |
3045 | (unless org-local-vars | |
3046 | (setq org-local-vars (org-get-local-variables))) | |
27e428e7 | 3047 | (eval ;; convert to fmt -- mimicking `org-run-like-in-org-mode' |
afe98dfa | 3048 | (list 'let org-local-vars |
3ab2c837 | 3049 | (list (intern (format "org-export-as-%s" fmt)) |
c7cf0ebc | 3050 | nil nil ''string t dir)))) |
afe98dfa CD |
3051 | (delete-file tmp-file)))) |
3052 | ||
c8d0cf5c | 3053 | ;;;###autoload |
c7cf0ebc | 3054 | (defun org-export-as-org (arg &optional ext-plist to-buffer body-only pub-dir) |
c8d0cf5c CD |
3055 | "Make a copy with not-exporting stuff removed. |
3056 | The purpose of this function is to provide a way to export the source | |
3057 | Org file of a webpage in Org format, but with sensitive and/or irrelevant | |
3058 | stuff removed. This command will remove the following: | |
3059 | ||
3060 | - archived trees (if the variable `org-export-with-archived-trees' is nil) | |
3061 | - comment blocks and trees starting with the COMMENT keyword | |
3062 | - only trees that are consistent with `org-export-select-tags' | |
3063 | and `org-export-exclude-tags'. | |
3064 | ||
3065 | The only arguments that will be used are EXT-PLIST and PUB-DIR, | |
3066 | all the others will be ignored (but are present so that the general | |
3067 | mechanism to call publishing functions will work). | |
3068 | ||
3069 | EXT-PLIST is a property list with external parameters overriding | |
3070 | org-mode's default settings, but still inferior to file-local | |
3071 | settings. When PUB-DIR is set, use this as the publishing | |
3072 | directory." | |
3073 | (interactive "P") | |
3074 | (let* ((opt-plist (org-combine-plists (org-default-export-plist) | |
3075 | ext-plist | |
3076 | (org-infile-export-plist))) | |
3077 | (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer)))) | |
3078 | (filename (concat (file-name-as-directory | |
3079 | (or pub-dir | |
3080 | (org-export-directory :org opt-plist))) | |
3081 | (file-name-sans-extension | |
3082 | (file-name-nondirectory bfname)) | |
3083 | ".org")) | |
3084 | (filename (and filename | |
3085 | (if (equal (file-truename filename) | |
3086 | (file-truename bfname)) | |
3087 | (concat (file-name-sans-extension filename) | |
3088 | "-source." | |
3089 | (file-name-extension filename)) | |
3090 | filename))) | |
3091 | (backup-inhibited t) | |
3092 | (buffer (find-file-noselect filename)) | |
86fbb8ca CD |
3093 | (region (buffer-string)) |
3094 | str-ret) | |
c8d0cf5c | 3095 | (save-excursion |
e66ba1df | 3096 | (org-pop-to-buffer-same-window buffer) |
c8d0cf5c CD |
3097 | (erase-buffer) |
3098 | (insert region) | |
3099 | (let ((org-inhibit-startup t)) (org-mode)) | |
3100 | (org-install-letbind) | |
3101 | ||
3102 | ;; Get rid of archived trees | |
3103 | (org-export-remove-archived-trees (plist-get opt-plist :archived-trees)) | |
3104 | ||
3105 | ;; Remove comment environment and comment subtrees | |
3106 | (org-export-remove-comment-blocks-and-subtrees) | |
3107 | ||
3108 | ;; Get rid of excluded trees | |
3109 | (org-export-handle-export-tags (plist-get opt-plist :select-tags) | |
3110 | (plist-get opt-plist :exclude-tags)) | |
3111 | ||
3112 | (when (or (plist-get opt-plist :plain-source) | |
3113 | (not (or (plist-get opt-plist :plain-source) | |
3114 | (plist-get opt-plist :htmlized-source)))) | |
3115 | ;; Either nothing special is requested (default call) | |
3116 | ;; or the plain source is explicitly requested | |
3117 | ;; so: save it | |
3118 | (save-buffer)) | |
3119 | (when (plist-get opt-plist :htmlized-source) | |
3120 | ;; Make the htmlized version | |
3121 | (require 'htmlize) | |
3122 | (require 'org-html) | |
3123 | (font-lock-fontify-buffer) | |
3124 | (let* ((htmlize-output-type 'css) | |
3125 | (newbuf (htmlize-buffer))) | |
3126 | (with-current-buffer newbuf | |
3127 | (when org-export-htmlized-org-css-url | |
3128 | (goto-char (point-min)) | |
3129 | (and (re-search-forward | |
3130 | "<style type=\"text/css\">[^\000]*?\n[ \t]*</style>.*" | |
3131 | nil t) | |
3132 | (replace-match | |
3133 | (format | |
3134 | "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">" | |
3135 | org-export-htmlized-org-css-url) | |
3136 | t t))) | |
3137 | (write-file (concat filename ".html"))) | |
3138 | (kill-buffer newbuf))) | |
3139 | (set-buffer-modified-p nil) | |
86fbb8ca CD |
3140 | (if (equal to-buffer 'string) |
3141 | (progn (setq str-ret (buffer-string)) | |
3142 | (kill-buffer (current-buffer)) | |
3143 | str-ret) | |
3144 | (kill-buffer (current-buffer)))))) | |
20908596 | 3145 | |
33306645 | 3146 | (defvar org-archive-location) ;; gets loaded with the org-archive require. |
20908596 CD |
3147 | (defun org-get-current-options () |
3148 | "Return a string with current options as keyword options. | |
3149 | Does include HTML export options as well as TODO and CATEGORY stuff." | |
3150 | (require 'org-archive) | |
3151 | (format | |
3152 | "#+TITLE: %s | |
3153 | #+AUTHOR: %s | |
3154 | #+EMAIL: %s | |
3155 | #+DATE: %s | |
3ab2c837 BG |
3156 | #+DESCRIPTION: |
3157 | #+KEYWORDS: | |
20908596 | 3158 | #+LANGUAGE: %s |
0bd48b37 CD |
3159 | #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s -:%s f:%s *:%s <:%s |
3160 | #+OPTIONS: TeX:%s LaTeX:%s skip:%s d:%s todo:%s pri:%s tags:%s | |
20908596 | 3161 | %s |
621f83e4 | 3162 | #+EXPORT_SELECT_TAGS: %s |
93b62de8 | 3163 | #+EXPORT_EXCLUDE_TAGS: %s |
20908596 CD |
3164 | #+LINK_UP: %s |
3165 | #+LINK_HOME: %s | |
3ab2c837 | 3166 | #+XSLT: |
20908596 CD |
3167 | #+CATEGORY: %s |
3168 | #+SEQ_TODO: %s | |
3169 | #+TYP_TODO: %s | |
3170 | #+PRIORITIES: %c %c %c | |
3171 | #+DRAWERS: %s | |
3172 | #+STARTUP: %s %s %s %s %s | |
3173 | #+TAGS: %s | |
b349f79f | 3174 | #+FILETAGS: %s |
20908596 CD |
3175 | #+ARCHIVE: %s |
3176 | #+LINK: %s | |
3177 | " | |
3178 | (buffer-name) (user-full-name) user-mail-address | |
621f83e4 | 3179 | (format-time-string (substring (car org-time-stamp-formats) 1 -1)) |
20908596 CD |
3180 | org-export-default-language |
3181 | org-export-headline-levels | |
3182 | org-export-with-section-numbers | |
3183 | org-export-with-toc | |
3184 | org-export-preserve-breaks | |
3185 | org-export-html-expand | |
3186 | org-export-with-fixed-width | |
3187 | org-export-with-tables | |
3188 | org-export-with-sub-superscripts | |
3189 | org-export-with-special-strings | |
3190 | org-export-with-footnotes | |
3191 | org-export-with-emphasize | |
0bd48b37 | 3192 | org-export-with-timestamps |
20908596 CD |
3193 | org-export-with-TeX-macros |
3194 | org-export-with-LaTeX-fragments | |
3195 | org-export-skip-text-before-1st-heading | |
3196 | org-export-with-drawers | |
ff4be292 CD |
3197 | org-export-with-todo-keywords |
3198 | org-export-with-priority | |
20908596 | 3199 | org-export-with-tags |
b349f79f | 3200 | (if (featurep 'org-jsinfo) (org-infojs-options-inbuffer-template) "") |
621f83e4 CD |
3201 | (mapconcat 'identity org-export-select-tags " ") |
3202 | (mapconcat 'identity org-export-exclude-tags " ") | |
20908596 CD |
3203 | org-export-html-link-up |
3204 | org-export-html-link-home | |
d6685abc CD |
3205 | (or (ignore-errors |
3206 | (file-name-sans-extension | |
3207 | (file-name-nondirectory (buffer-file-name (buffer-base-buffer))))) | |
3208 | "NOFILENAME") | |
20908596 CD |
3209 | "TODO FEEDBACK VERIFY DONE" |
3210 | "Me Jason Marie DONE" | |
3211 | org-highest-priority org-lowest-priority org-default-priority | |
3212 | (mapconcat 'identity org-drawers " ") | |
3213 | (cdr (assoc org-startup-folded | |
3214 | '((nil . "showall") (t . "overview") (content . "content")))) | |
3215 | (if org-odd-levels-only "odd" "oddeven") | |
3216 | (if org-hide-leading-stars "hidestars" "showstars") | |
3217 | (if org-startup-align-all-tables "align" "noalign") | |
3218 | (cond ((eq org-log-done t) "logdone") | |
3219 | ((equal org-log-done 'note) "lognotedone") | |
3220 | ((not org-log-done) "nologdone")) | |
3221 | (or (mapconcat (lambda (x) | |
3222 | (cond | |
8bfe682a CD |
3223 | ((equal :startgroup (car x)) "{") |
3224 | ((equal :endgroup (car x)) "}") | |
3225 | ((equal :newline (car x)) "") | |
20908596 CD |
3226 | ((cdr x) (format "%s(%c)" (car x) (cdr x))) |
3227 | (t (car x)))) | |
3228 | (or org-tag-alist (org-get-buffer-tags)) " ") "") | |
b349f79f | 3229 | (mapconcat 'identity org-file-tags " ") |
20908596 | 3230 | org-archive-location |
8223b1d2 | 3231 | "org file:~/org/%s.org")) |
20908596 | 3232 | |
20908596 CD |
3233 | (defun org-insert-export-options-template () |
3234 | "Insert into the buffer a template with information for exporting." | |
3235 | (interactive) | |
3236 | (if (not (bolp)) (newline)) | |
3237 | (let ((s (org-get-current-options))) | |
3238 | (and (string-match "#\\+CATEGORY" s) | |
3239 | (setq s (substring s 0 (match-beginning 0)))) | |
3240 | (insert s))) | |
3241 | ||
20908596 | 3242 | (defvar org-table-colgroup-info nil) |
20908596 | 3243 | |
0bd48b37 | 3244 | (defun org-table-clean-before-export (lines &optional maybe-quoted) |
20908596 CD |
3245 | "Check if the table has a marking column. |
3246 | If yes remove the column and the special lines." | |
3247 | (setq org-table-colgroup-info nil) | |
3248 | (if (memq nil | |
3249 | (mapcar | |
3250 | (lambda (x) (or (string-match "^[ \t]*|-" x) | |
0bd48b37 CD |
3251 | (string-match |
3252 | (if maybe-quoted | |
3253 | "^[ \t]*| *\\\\?\\([\#!$*_^ /]\\) *|" | |
3254 | "^[ \t]*| *\\([\#!$*_^ /]\\) *|") | |
3255 | x))) | |
20908596 | 3256 | lines)) |
86fbb8ca | 3257 | ;; No special marking column |
20908596 CD |
3258 | (progn |
3259 | (setq org-table-clean-did-remove-column nil) | |
3260 | (delq nil | |
3261 | (mapcar | |
3262 | (lambda (x) | |
3263 | (cond | |
86fbb8ca CD |
3264 | ((org-table-colgroup-line-p x) |
3265 | ;; This line contains colgroup info, extract it | |
3266 | ;; and then discard the line | |
20908596 CD |
3267 | (setq org-table-colgroup-info |
3268 | (mapcar (lambda (x) | |
3269 | (cond ((member x '("<" "<")) :start) | |
3270 | ((member x '(">" ">")) :end) | |
8223b1d2 | 3271 | ((member x '("<>" "<>")) :startend))) |
20908596 CD |
3272 | (org-split-string x "[ \t]*|[ \t]*"))) |
3273 | nil) | |
86fbb8ca CD |
3274 | ((org-table-cookie-line-p x) |
3275 | ;; This line contains formatting cookies, discard it | |
3276 | nil) | |
20908596 CD |
3277 | (t x))) |
3278 | lines))) | |
86fbb8ca | 3279 | ;; there is a special marking column |
20908596 CD |
3280 | (setq org-table-clean-did-remove-column t) |
3281 | (delq nil | |
3282 | (mapcar | |
3283 | (lambda (x) | |
3284 | (cond | |
86fbb8ca CD |
3285 | ((org-table-colgroup-line-p x) |
3286 | ;; This line contains colgroup info, extract it | |
3287 | ;; and then discard the line | |
20908596 CD |
3288 | (setq org-table-colgroup-info |
3289 | (mapcar (lambda (x) | |
3290 | (cond ((member x '("<" "<")) :start) | |
3291 | ((member x '(">" ">")) :end) | |
8223b1d2 | 3292 | ((member x '("<>" "<>")) :startend))) |
20908596 CD |
3293 | (cdr (org-split-string x "[ \t]*|[ \t]*")))) |
3294 | nil) | |
86fbb8ca CD |
3295 | ((org-table-cookie-line-p x) |
3296 | ;; This line contains formatting cookies, discard it | |
3297 | nil) | |
153ae947 | 3298 | ((string-match "^[ \t]*| *\\([!_^/$]\\|\\\\\\$\\) *|" x) |
86fbb8ca CD |
3299 | ;; ignore this line |
3300 | nil) | |
20908596 CD |
3301 | ((or (string-match "^\\([ \t]*\\)|-+\\+" x) |
3302 | (string-match "^\\([ \t]*\\)|[^|]*|" x)) | |
3303 | ;; remove the first column | |
3304 | (replace-match "\\1|" t nil x)))) | |
3305 | lines)))) | |
3306 | ||
20908596 | 3307 | (defun org-export-cleanup-toc-line (s) |
33306645 | 3308 | "Remove tags and timestamps from lines going into the toc." |
8223b1d2 BG |
3309 | (if (not s) |
3310 | "" ; Return a string when argument is nil | |
3311 | (when (memq org-export-with-tags '(not-in-toc nil)) | |
3312 | (if (string-match (org-re " +:[[:alnum:]_@#%:]+: *$") s) | |
3313 | (setq s (replace-match "" t t s)))) | |
3314 | (when org-export-remove-timestamps-from-toc | |
3315 | (while (string-match org-maybe-keyword-time-regexp s) | |
20908596 | 3316 | (setq s (replace-match "" t t s)))) |
8223b1d2 BG |
3317 | (while (string-match org-bracket-link-regexp s) |
3318 | (setq s (replace-match (match-string (if (match-end 3) 3 1) s) | |
3319 | t t s))) | |
3320 | (while (string-match "\\[\\([0-9]\\|fn:[^]]*\\)\\]" s) | |
3321 | (setq s (replace-match "" t t s))) | |
3322 | s)) | |
20908596 | 3323 | |
20908596 CD |
3324 | |
3325 | (defun org-get-text-property-any (pos prop &optional object) | |
3326 | (or (get-text-property pos prop object) | |
3327 | (and (setq pos (next-single-property-change pos prop object)) | |
3328 | (get-text-property pos prop object)))) | |
3329 | ||
c8d0cf5c CD |
3330 | (defun org-export-get-coderef-format (path desc) |
3331 | (save-match-data | |
3332 | (if (and desc (string-match | |
3333 | (regexp-quote (concat "(" path ")")) | |
3334 | desc)) | |
3335 | (replace-match "%s" t t desc) | |
3336 | (or desc "%s")))) | |
3337 | ||
3338 | (defun org-export-push-to-kill-ring (format) | |
3339 | "Push buffer content to kill ring. | |
e66ba1df | 3340 | The depends on the variable `org-export-copy-to-kill-ring'." |
c8d0cf5c CD |
3341 | (when org-export-copy-to-kill-ring |
3342 | (org-kill-new (buffer-string)) | |
3343 | (when (fboundp 'x-set-selection) | |
3344 | (ignore-errors (x-set-selection 'PRIMARY (buffer-string))) | |
3345 | (ignore-errors (x-set-selection 'CLIPBOARD (buffer-string)))) | |
3346 | (message "%s export done, pushed to kill ring and clipboard" format))) | |
20908596 CD |
3347 | |
3348 | (provide 'org-exp) | |
3349 | ||
bdebdb64 BG |
3350 | ;; Local variables: |
3351 | ;; generated-autoload-file: "org-loaddefs.el" | |
3352 | ;; End: | |
3353 | ||
b349f79f | 3354 | ;;; org-exp.el ends here |