Fix some issues with org-xhtml export
[org-mode/org-jambu.git] / contrib / lisp / org-lparse.el
blobfd0488b3126ce6c839f53982e66b078e01cbe441
1 ;;; org-lparse.el --- Line-oriented parser-exporter for Org-mode
3 ;; Copyright (C) 2010-2011 Jambunathan <kjambunathan at gmail dot com>
5 ;; Author: Jambunathan K <kjambunathan at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;; Version: 0.8
10 ;; This file is not (yet) part of GNU Emacs.
11 ;; However, it is distributed under the same license.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
29 ;; `org-lparse' is the entry point for the generic line-oriented
30 ;; exporter. `org-do-lparse' is the genericized version of the
31 ;; original `org-export-as-html' routine.
33 ;; `org-lparse-native-backends' is a good starting point for
34 ;; exploring the generic exporter.
36 ;; Following new interactive commands are provided by this library.
37 ;; `org-lparse', `org-lparse-and-open', `org-lparse-to-buffer'
38 ;; `org-replace-region-by', `org-lparse-region'.
40 ;; Note that the above routines correspond to the following routines
41 ;; in the html exporter `org-export-as-html',
42 ;; `org-export-as-html-and-open', `org-export-as-html-to-buffer',
43 ;; `org-replace-region-by-html' and `org-export-region-as-html'.
45 ;; The new interactive command `org-lparse-convert' can be used to
46 ;; convert documents between various formats. Use this to command,
47 ;; for example, to convert odt file to doc or pdf format.
49 ;; See README.org file that comes with this library for answers to
50 ;; FAQs and more information on using this library.
52 ;;; Code:
54 (require 'org-exp)
55 (require 'org-list)
57 ;;;###autoload
58 (defun org-lparse-and-open (target-backend native-backend arg)
59 "Export outline to TARGET-BACKEND via NATIVE-BACKEND and open exported file.
60 If there is an active region, export only the region. The prefix
61 ARG specifies how many levels of the outline should become
62 headlines. The default is 3. Lower levels will become bulleted
63 lists."
64 ;; (interactive "Mbackend: \nP")
65 (interactive
66 (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
67 (all-backends (org-lparse-all-backends))
68 (target-backend
69 (funcall input "Export to: " all-backends nil t nil))
70 (native-backend
71 (or
72 ;; (and (org-lparse-backend-is-native-p target-backend)
73 ;; target-backend)
74 (funcall input "Use Native backend: "
75 (cdr (assoc target-backend all-backends)) nil t nil))))
76 (list target-backend native-backend current-prefix-arg)))
77 (let (f (file-or-buf (org-lparse target-backend native-backend
78 arg 'hidden)))
79 (when file-or-buf
80 (setq f (cond
81 ((bufferp file-or-buf) buffer-file-name)
82 ((file-exists-p file-or-buf) file-or-buf)
83 (t (error "org-lparse-and-open: This shouldn't happen"))))
84 (message "Opening file %s" f)
85 (org-open-file f)
86 (when org-export-kill-product-buffer-when-displayed
87 (kill-buffer (current-buffer))))))
89 ;;;###autoload
90 (defun org-lparse-batch (target-backend &optional native-backend)
91 "Call the function `org-lparse'.
92 This function can be used in batch processing as:
93 emacs --batch
94 --load=$HOME/lib/emacs/org.el
95 --eval \"(setq org-export-headline-levels 2)\"
96 --visit=MyFile --funcall org-lparse-batch"
97 (setq native-backend (or native-backend target-backend))
98 (org-lparse target-backend native-backend
99 org-export-headline-levels 'hidden))
101 ;;;###autoload
102 (defun org-lparse-to-buffer (backend arg)
103 "Call `org-lparse' with output to a temporary buffer.
104 No file is created. The prefix ARG is passed through to
105 `org-lparse'."
106 (interactive "Mbackend: \nP")
107 (let ((tempbuf (format "*Org %s Export*" (upcase backend))))
108 (org-lparse backend backend arg nil nil tempbuf)
109 (when org-export-show-temporary-export-buffer
110 (switch-to-buffer-other-window tempbuf))))
112 ;;;###autoload
113 (defun org-replace-region-by (backend beg end)
114 "Assume the current region has org-mode syntax, and convert it to HTML.
115 This can be used in any buffer. For example, you could write an
116 itemized list in org-mode syntax in an HTML buffer and then use
117 this command to convert it."
118 (interactive "Mbackend: \nr")
119 (let (reg backend-string buf pop-up-frames)
120 (save-window-excursion
121 (if (org-mode-p)
122 (setq backend-string (org-lparse-region backend beg end t 'string))
123 (setq reg (buffer-substring beg end)
124 buf (get-buffer-create "*Org tmp*"))
125 (with-current-buffer buf
126 (erase-buffer)
127 (insert reg)
128 (org-mode)
129 (setq backend-string (org-lparse-region backend (point-min)
130 (point-max) t 'string)))
131 (kill-buffer buf)))
132 (delete-region beg end)
133 (insert backend-string)))
135 ;;;###autoload
136 (defun org-lparse-region (backend beg end &optional body-only buffer)
137 "Convert region from BEG to END in org-mode buffer to HTML.
138 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
139 contents, and only produce the region of converted text, useful for
140 cut-and-paste operations.
141 If BUFFER is a buffer or a string, use/create that buffer as a target
142 of the converted HTML. If BUFFER is the symbol `string', return the
143 produced HTML as a string and leave not buffer behind. For example,
144 a Lisp program could call this function in the following way:
146 (setq html (org-lparse-region \"html\" beg end t 'string))
148 When called interactively, the output buffer is selected, and shown
149 in a window. A non-interactive call will only return the buffer."
150 (interactive "Mbackend: \nr\nP")
151 (when (org-called-interactively-p 'any)
152 (setq buffer (format "*Org %s Export*" (upcase backend))))
153 (let ((transient-mark-mode t) (zmacs-regions t)
154 ext-plist rtn)
155 (setq ext-plist (plist-put ext-plist :ignore-subtree-p t))
156 (goto-char end)
157 (set-mark (point)) ;; to activate the region
158 (goto-char beg)
159 (setq rtn (org-lparse backend backend nil nil ext-plist buffer body-only))
160 (if (fboundp 'deactivate-mark) (deactivate-mark))
161 (if (and (org-called-interactively-p 'any) (bufferp rtn))
162 (switch-to-buffer-other-window rtn)
163 rtn)))
165 (defvar org-lparse-par-open nil)
167 (defun org-lparse-should-inline-p (filename descp)
168 "Return non-nil if link FILENAME should be inlined.
169 The decision to inline the FILENAME link is based on the current
170 settings. DESCP is the boolean of whether there was a link
171 description. See variables `org-export-html-inline-images' and
172 `org-export-html-inline-image-extensions'."
173 (let ((inline-images (org-lparse-get 'INLINE-IMAGES))
174 (inline-image-extensions
175 (org-lparse-get 'INLINE-IMAGE-EXTENSIONS)))
176 (and (or (eq t inline-images) (and inline-images (not descp)))
177 (org-file-image-p filename inline-image-extensions))))
179 (defun org-lparse-format-org-link (line opt-plist)
180 "Return LINE with markup of Org mode links.
181 OPT-PLIST is the export options list."
182 (let ((start 0)
183 (current-dir (if buffer-file-name
184 (file-name-directory buffer-file-name)
185 default-directory))
186 (link-validate (plist-get opt-plist :link-validation-function))
187 type id-file fnc
188 rpl path attr desc descp desc1 desc2 link
189 org-lparse-link-description-is-image)
190 (while (string-match org-bracket-link-analytic-regexp++ line start)
191 (setq org-lparse-link-description-is-image nil)
192 (setq start (match-beginning 0))
193 (setq path (save-match-data (org-link-unescape
194 (match-string 3 line))))
195 (setq type (cond
196 ((match-end 2) (match-string 2 line))
197 ((save-match-data
198 (or (file-name-absolute-p path)
199 (string-match "^\\.\\.?/" path)))
200 "file")
201 (t "internal")))
202 (setq path (org-extract-attributes (org-link-unescape path)))
203 (setq attr (get-text-property 0 'org-attributes path))
204 (setq desc1 (if (match-end 5) (match-string 5 line))
205 desc2 (if (match-end 2) (concat type ":" path) path)
206 descp (and desc1 (not (equal desc1 desc2)))
207 desc (or desc1 desc2))
208 ;; Make an image out of the description if that is so wanted
209 (when (and descp (org-file-image-p
210 desc (org-lparse-get 'INLINE-IMAGE-EXTENSIONS)))
211 (setq org-lparse-link-description-is-image t)
212 (save-match-data
213 (if (string-match "^file:" desc)
214 (setq desc (substring desc (match-end 0)))))
215 (save-match-data
216 (setq desc (org-add-props
217 (org-lparse-format 'INLINE-IMAGE desc)
218 '(org-protected t)))))
219 (cond
220 ((equal type "internal")
221 (let
222 ((frag-0
223 (if (= (string-to-char path) ?#)
224 (substring path 1)
225 path)))
226 (setq rpl
227 (org-lparse-format
228 'ORG-LINK opt-plist "" "" (org-solidify-link-text
229 (save-match-data
230 (org-link-unescape frag-0))
231 nil) desc attr descp))))
232 ((and (equal type "id")
233 (setq id-file (org-id-find-id-file path)))
234 ;; This is an id: link to another file (if it was the same file,
235 ;; it would have become an internal link...)
236 (save-match-data
237 (setq id-file (file-relative-name
238 id-file
239 (file-name-directory org-current-export-file)))
240 (setq rpl
241 (org-lparse-format
242 'ORG-LINK opt-plist type id-file
243 (concat (if (org-uuidgen-p path) "ID-") path)
244 desc attr descp))))
245 ((member type '("http" "https"))
246 ;; standard URL, can inline as image
247 (setq rpl
248 (org-lparse-format
249 'ORG-LINK opt-plist type path nil desc attr descp)))
250 ((member type '("ftp" "mailto" "news"))
251 ;; standard URL, can't inline as image
252 (setq rpl
253 (org-lparse-format
254 'ORG-LINK opt-plist type path nil desc attr descp)))
256 ((string= type "coderef")
257 (setq rpl
258 (org-lparse-format
259 'ORG-LINK opt-plist type "" (format "coderef-%s" path)
260 (format
261 (org-export-get-coderef-format
262 path
263 (and descp desc))
264 (cdr (assoc path org-export-code-refs))) nil descp)))
266 ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
267 ;; The link protocol has a function for format the link
268 (setq rpl
269 (save-match-data
270 (funcall fnc (org-link-unescape path) desc1 'html))))
272 ((string= type "file")
273 ;; FILE link
274 (save-match-data
275 (let*
276 ((components
278 (string-match "::\\(.*\\)" path)
279 (list
280 (replace-match "" t nil path)
281 (match-string 1 path))
282 (list path nil)))
284 ;;The proper path, without a fragment
285 (path-1
286 (first components))
288 ;;The raw fragment
289 (fragment-0
290 (second components))
292 ;;Check the fragment. If it can't be used as
293 ;;target fragment we'll pass nil instead.
294 (fragment-1
296 (and fragment-0
297 (not (string-match "^[0-9]*$" fragment-0))
298 (not (string-match "^\\*" fragment-0))
299 (not (string-match "^/.*/$" fragment-0)))
300 (org-solidify-link-text
301 (org-link-unescape fragment-0))
302 nil))
303 (desc-2
304 ;;Description minus "file:" and ".org"
305 (if (string-match "^file:" desc)
306 (let
307 ((desc-1 (replace-match "" t t desc)))
308 (if (string-match "\\.org$" desc-1)
309 (replace-match "" t t desc-1)
310 desc-1))
311 desc)))
313 (setq rpl
315 (and
316 (functionp link-validate)
317 (not (funcall link-validate path-1 current-dir)))
318 desc
319 (org-lparse-format
320 'ORG-LINK opt-plist "file" path-1 fragment-1
321 desc-2 attr descp))))))
324 ;; just publish the path, as default
325 (setq rpl (concat "<i>&lt;" type ":"
326 (save-match-data (org-link-unescape path))
327 "&gt;</i>"))))
328 (setq line (replace-match rpl t t line)
329 start (+ start (length rpl))))
330 line))
332 (defmacro with-org-lparse-preserve-paragraph-state (&rest body)
333 `(let ((org-lparse-do-open-par org-lparse-par-open))
334 (org-lparse-end-paragraph)
335 ,@body
336 (when org-lparse-do-open-par
337 (org-lparse-begin-paragraph))))
339 (defvar org-lparse-native-backends nil
340 "List of native backends registered with `org-lparse'.
341 A backend can use `org-lparse-register-backend' to add itself to
342 this list.
344 All native backends must implement a get routine and a mandatory
345 set of callback routines.
347 The get routine must be named as org-<backend>-get where backend
348 is the name of the backend. The exporter uses `org-lparse-get'
349 and retrieves the backend-specific callback by querying for
350 ENTITY-CONTROL and ENTITY-FORMAT variables.
352 For the sake of illustration, the html backend implements
353 `org-xhtml-get'. It returns
354 `org-xhtml-entity-control-callbacks-alist' and
355 `org-xhtml-entity-format-callbacks-alist' as the values of
356 ENTITY-CONTROL and ENTITY-FORMAT settings.")
358 (defun org-lparse-register-backend (backend)
359 "Make BACKEND known to `org-lparse' library.
360 Add BACKEND to `org-lparse-native-backends'."
361 (when backend
362 (setq backend (cond
363 ((symbolp backend) (symbol-name backend))
364 ((stringp backend) backend)
365 (t (error "Error while registering backend: %S" backend))))
366 (add-to-list 'org-lparse-native-backends backend)))
368 (defun org-lparse-unregister-backend (backend)
369 (setq org-lparse-native-backends
370 (remove (cond
371 ((symbolp backend) (symbol-name backend))
372 ((stringp backend) backend))
373 org-lparse-native-backends))
374 (message "Unregistered backend %S" backend))
376 (defun org-lparse-get-other-backends (in-fmt)
377 "Return OUTPUT-FMT-ALIST corresponding to IN-FMT.
378 See `org-lparse-convert-capabilities' for definition of
379 OUTPUT-FMT-ALIST."
380 (when (org-lparse-get-converter in-fmt)
381 (or (ignore-errors (org-lparse-backend-get in-fmt 'OTHER-BACKENDS) )
382 (catch 'out-fmts
383 (dolist (c org-lparse-convert-capabilities)
384 (when (member in-fmt (nth 1 c))
385 (throw 'out-fmts (nth 2 c))))))))
387 (defun org-lparse-get-converter (in-fmt)
388 "Return converter associated with IN-FMT.
389 See `org-lparse-convert-capabilities' for further information."
390 (or (ignore-errors (org-lparse-backend-get in-fmt 'CONVERT-METHOD))
391 org-lparse-convert-process))
393 (defun org-lparse-all-backends ()
394 "Return all formats to which `org-lparse' could export to.
395 The return value is an alist of the form (TARGET-BACKEND
396 NATIVE-BACKEND-1 NATIVE-BACKEND-2 ...) with the condition that
397 the org file can be exported to TARGET-BACKEND via any one of
398 NATIVE-BACKEND-1, NATIVE-BACKEND-2 etc.
400 For example, an entry of the form \"(\"pdf\" \"odt\" \"xhtml\")\"
401 would mean that the org file could be exported to \"pdf\" format
402 by exporting natively either to \"xhtml\" or \"odt\" backends."
403 (let (all-backends)
404 (flet ((add (other native)
405 (let ((val (assoc-string other all-backends t)))
406 (if val (setcdr val (nconc (list native) (cdr val)))
407 (push (cons other (list native)) all-backends)))))
408 (loop for backend in org-lparse-native-backends
409 do (loop for other in
410 (mapcar #'car (org-lparse-get-other-backends backend))
411 do (add other backend))))
412 all-backends))
414 (defun org-lparse-backend-is-native-p (backend)
415 (member backend org-lparse-native-backends))
417 (defun org-lparse (target-backend native-backend arg
418 &optional hidden ext-plist
419 to-buffer body-only pub-dir)
420 "Export the outline to various formats.
421 If there is an active region, export only the region. The
422 outline is first exported to NATIVE-BACKEND and optionally
423 converted to TARGET-BACKEND. See `org-lparse-native-backends'
424 for list of known native backends. Each native backend can
425 specify a converter and list of target backends it exports to
426 using the CONVERT-PROCESS and OTHER-BACKENDS settings of it's get
427 method. See `org-xhtml-get' for an illustrative example.
429 ARG is a prefix argument that specifies how many levels of
430 outline should become headlines. The default is 3. Lower levels
431 will become bulleted lists.
433 HIDDEN is obsolete and does nothing.
435 EXT-PLIST is a property list that controls various aspects of
436 export. The settings here override org-mode's default settings
437 and but are inferior to file-local settings.
439 TO-BUFFER dumps the exported lines to a buffer or a string
440 instead of a file. If TO-BUFFER is the symbol `string' return the
441 exported lines as a string. If TO-BUFFER is non-nil, create a
442 buffer with that name and export to that buffer.
444 BODY-ONLY controls the presence of header and footer lines in
445 exported text. If BODY-ONLY is non-nil, don't produce the file
446 header and footer, simply return the content of <body>...</body>,
447 without even the body tags themselves.
449 PUB-DIR specifies the publishing directory."
450 (interactive
451 (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
452 (all-backends (org-lparse-all-backends))
453 (target-backend
454 (and all-backends
455 (funcall input "Export to: " all-backends nil t nil)))
456 (native-backend
457 (let ((choices (if target-backend
458 (cdr (assoc target-backend all-backends))
459 (or org-lparse-native-backends
460 (error "No registered backends")))))
461 (funcall input "Use Native backend: " choices nil t nil))))
462 (list target-backend native-backend current-prefix-arg)))
463 (let* ((org-lparse-backend (intern native-backend))
464 (org-lparse-other-backend (and target-backend
465 (intern target-backend))))
466 (unless (org-lparse-backend-is-native-p native-backend)
467 (error "Don't know how to export natively to backend %s" native-backend))
468 (unless (or (not target-backend)
469 (equal target-backend native-backend)
470 (assoc target-backend (org-lparse-get-other-backends
471 native-backend)))
472 (error "Don't know how to export to backend %s %s" target-backend
473 (format "via %s" native-backend)))
474 (run-hooks 'org-export-first-hook)
475 (org-do-lparse arg hidden ext-plist to-buffer body-only pub-dir)))
477 (defcustom org-lparse-convert-processes
478 '(("BasicODConverter"
479 ("soffice" "-norestore" "-invisible" "-headless"
480 "\"macro:///BasicODConverter.Main.Convert(%I,%f,%O)\""))
481 ("unoconv"
482 ("unoconv" "-f" "%f" "-o" "%d" "%i")))
483 "Specify a list of document converters and their usage.
484 The converters in this list are offered as choices while
485 customizing `org-lparse-convert-process'.
487 This variable is an alist where each element is of the
488 form (CONVERTER-NAME (CONVERTER-PROGRAM ARG1 ARG2 ...)).
489 CONVERTER-NAME is name of the converter. CONVERTER-PROGRAM is
490 the name of the executable. ARG1, ARG2 etc are command line
491 options that are passed to CONVERTER-PROGRAM. Format specifiers
492 can be used in the ARGs and they are interpreted as below:
494 %i input file name in full
495 %I input file name as a URL
496 %f format of the output file
497 %o output file name in full
498 %O output file name as a URL
499 %d output dir in full
500 %D output dir as a URL."
501 :group 'org-lparse
502 :type
503 '(choice
504 (const :tag "None" nil)
505 (alist :tag "Converters"
506 :key-type (string :tag "Converter Name")
507 :value-type (group (cons (string :tag "Executable")
508 (repeat (string :tag "Command line args")))))))
509 (defcustom org-lparse-convert-process nil
510 "Command to convert from an Org exported format to other formats.
511 During customization, the list of choices are populated from
512 `org-lparse-convert-processes'. Refer afore-mentioned variable
513 for further information."
514 :group 'org-lparse
515 :type '(choice :convert-widget
516 (lambda (w)
517 (apply 'widget-convert (widget-type w)
518 (eval (car (widget-get w :args)))))
519 `((const :tag "None" nil)
520 ,@(mapcar (lambda (c)
521 `(const :tag ,(car c) ,(cadr c)))
522 org-lparse-convert-processes))))
524 (defcustom org-lparse-convert-capabilities
525 '(("Text"
526 ("odt" "ott" "doc" "rtf")
527 (("pdf" "pdf") ("odt" "odt") ("xhtml" "html") ("rtf" "rtf")
528 ("ott" "ott") ("doc" "doc") ("ooxml" "xml") ("html" "html")))
529 ("Web"
530 ("html" "xhtml") (("pdf" "pdf") ("odt" "txt") ("html" "html")))
531 ("Spreadsheet"
532 ("ods" "ots" "xls" "csv")
533 (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv")
534 ("ods" "ods") ("xls" "xls") ("xhtml" "xhtml") ("ooxml" "xml")))
535 ("Presentation"
536 ("odp" "otp" "ppt")
537 (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("xhtml" "xml")
538 ("otp" "otp") ("ppt" "ppt") ("odg" "odg") ("html" "html"))))
539 "Specify input and output formats of `org-lparse-convert-process'.
540 More correctly, specify the set of input and output formats that
541 the user is actually interested in.
543 This variable is an alist where each element is of the
544 form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
545 INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
546 alist where each element is of the form (OUTPUT-FMT
547 OUTPUT-FILE-EXTENSION).
549 The variable is interpreted as follows:
550 `org-lparse-convert-process' can take any document that is in
551 INPUT-FMT-LIST and produce any document that is in the
552 OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
553 OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
554 serves dual purposes:
555 - It is used for populating completion candidates during
556 `org-lparse' and `org-lparse-convert' commands.
558 - It is used as the value of \"%f\" specifier in
559 `org-lparse-convert-process'.
561 DOCUMENT-CLASS is used to group a set of file formats in
562 INPUT-FMT-LIST in to a single class.
564 Note that this variable inherently captures how LibreOffice based
565 converters work. LibreOffice maps documents of various formats
566 to classes like Text, Web, Spreadsheet, Presentation etc and
567 allow document of a given class (irrespective of it's source
568 format) to be converted to any of the export formats associated
569 with that class.
571 See default setting of this variable for an typical
572 configuration."
573 :group 'org-lparse
574 :type
575 '(choice
576 (const :tag "None" nil)
577 (alist :key-type (string :tag "Document Class")
578 :value-type
579 (group (repeat :tag "Input formats" (string :tag "Input format"))
580 (alist :tag "Output formats"
581 :key-type (string :tag "Output format")
582 :value-type
583 (group (string :tag "Output file extension")))))))
585 (defcustom org-lparse-use-flashy-warning nil
586 "Control flashing of messages logged with `org-lparse-warn'.
587 When non-nil, messages are fontified with warning face and the
588 exporter lingers for a while to catch user's attention."
589 :type 'boolean
590 :group 'org-lparse)
592 (defun org-lparse-convert (&optional in-file out-fmt prefix-arg)
593 "Convert IN-FILE to format OUT-FMT using a command line converter.
594 IN-FILE is the file to be converted. If unspecified, it defaults
595 to variable `buffer-file-name'. OUT-FMT is the desired output
596 format. If the backend has registered a CONVERT-METHOD as part
597 of it's get function then that converter is used. Otherwise
598 `org-lparse-convert-process' is used. If PREFIX-ARG is non-nil
599 then the newly converted file is opened using `org-open-file'."
600 (interactive
601 (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
602 (in-file (read-file-name "File to be converted: "
603 nil buffer-file-name t))
604 (in-fmt (file-name-extension in-file))
605 (out-fmt-choices (org-lparse-get-other-backends in-fmt))
606 (out-fmt
607 (or (and out-fmt-choices
608 (funcall input "Output format: "
609 out-fmt-choices nil nil nil))
610 (error
611 "No known converter or no known output formats for %s files"
612 in-fmt))))
613 (list in-file out-fmt current-prefix-arg)))
614 (require 'browse-url)
615 (let* ((in-file (expand-file-name (or in-file buffer-file-name)))
616 (dummy (or (file-readable-p in-file)
617 (error "Cannot read %s" in-file)))
618 (in-fmt (file-name-extension in-file))
619 (out-fmt (or out-fmt (error "Output format unspecified")))
620 (convert-process (org-lparse-get-converter in-fmt))
621 (program (car convert-process))
622 (dummy (and (or program (error "Converter not configured"))
623 (or (executable-find program)
624 (error "Cannot find converter %s" program))))
625 (out-fmt-alist
626 (or (assoc out-fmt (org-lparse-get-other-backends in-fmt))
627 (error "Cannot convert from %s to %s format" in-fmt out-fmt)))
628 (out-file (concat (file-name-sans-extension in-file) "."
629 (nth 1 out-fmt-alist)))
630 (out-dir (file-name-directory in-file))
631 (arglist (mapcar (lambda (arg)
632 (format-spec
633 arg `((?i . ,in-file)
634 (?I . ,(browse-url-file-url in-file))
635 (?f . ,out-fmt)
636 (?o . ,out-file)
637 (?O . ,(browse-url-file-url out-file))
638 (?d . ,out-dir)
639 (?D . ,(browse-url-file-url out-dir)))))
640 (cdr convert-process))))
641 (when (file-exists-p out-file)
642 (delete-file out-file))
644 (message "Executing %s %s" program (mapconcat 'identity arglist " "))
645 (apply 'call-process program nil nil nil arglist)
646 (cond
647 ((file-exists-p out-file)
648 (message "Exported to %s using %s" out-file program)
649 (when prefix-arg
650 (message "Opening %s..." out-file)
651 (org-open-file out-file))
652 out-file
653 ;; (set-buffer (find-file-noselect out-file))
656 (message "Export to %s failed" out-file)
657 nil))))
659 (defvar org-lparse-insert-tag-with-newlines 'both)
661 ;; Following variables are let-bound during `org-lparse'
662 (defvar org-lparse-dyn-first-heading-pos)
663 (defvar org-lparse-toc)
664 (defvar org-lparse-entity-control-callbacks-alist)
665 (defvar org-lparse-entity-format-callbacks-alist)
666 (defvar org-lparse-backend nil
667 "The native backend to which the document is currently exported.
668 This variable is let bound during `org-lparse'. Valid values are
669 one of the symbols corresponding to `org-lparse-native-backends'.
671 Compare this variable with `org-export-current-backend' which is
672 bound only during `org-export-preprocess-string' stage of the
673 export process.
675 See also `org-lparse-other-backend'.")
677 (defvar org-lparse-other-backend nil
678 "The target backend to which the document is currently exported.
679 This variable is let bound during `org-lparse'. This variable is
680 set to either `org-lparse-backend' or one of the symbols
681 corresponding to OTHER-BACKENDS specification of the
682 org-lparse-backend.
684 For example, if a document is exported to \"odt\" then both
685 org-lparse-backend and org-lparse-other-backend are bound to
686 'odt. On the other hand, if a document is exported to \"odt\"
687 and then converted to \"doc\" then org-lparse-backend is set to
688 'odt and org-lparse-other-backend is set to 'doc.")
690 (defvar org-lparse-body-only nil
691 "Bind this to BODY-ONLY arg of `org-lparse'.")
693 (defvar org-lparse-to-buffer nil
694 "Bind this to TO-BUFFER arg of `org-lparse'.")
696 (defun org-do-lparse (arg &optional hidden ext-plist
697 to-buffer body-only pub-dir)
698 "Export the outline to various formats.
699 See `org-lparse' for more information. This function is a
700 html-agnostic version of the `org-export-as-html' function in 7.5
701 version."
702 ;; Make sure we have a file name when we need it.
703 (when (and (not (or to-buffer body-only))
704 (not buffer-file-name))
705 (if (buffer-base-buffer)
706 (org-set-local 'buffer-file-name
707 (with-current-buffer (buffer-base-buffer)
708 buffer-file-name))
709 (error "Need a file name to be able to export")))
711 (org-lparse-warn
712 (format "Exporting to %s using org-lparse..."
713 (upcase (symbol-name
714 (or org-lparse-backend org-lparse-other-backend)))))
716 (setq-default org-todo-line-regexp org-todo-line-regexp)
717 (setq-default org-deadline-line-regexp org-deadline-line-regexp)
718 (setq-default org-done-keywords org-done-keywords)
719 (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
720 (let* (hfy-user-sheet-assoc ; let `htmlfontify' know that
721 ; we are interested in
722 ; collecting styles
723 org-lparse-encode-pending
724 org-lparse-par-open
725 (org-lparse-list-level 0) ; list level starts at 1. A
726 ; value of 0 implies we are
727 ; outside of any list
728 org-lparse-outline-text-open
729 (org-lparse-latex-fragment-fallback ; currently used only by
730 ; odt exporter
731 (or (ignore-errors (org-lparse-get 'LATEX-FRAGMENT-FALLBACK))
732 (if (and (org-check-external-command "latex" "" t)
733 (org-check-external-command "dvipng" "" t))
734 'dvipng
735 'verbatim)))
736 (org-lparse-insert-tag-with-newlines 'both)
737 (org-lparse-to-buffer to-buffer)
738 (org-lparse-body-only body-only)
739 (org-lparse-entity-control-callbacks-alist
740 (org-lparse-get 'ENTITY-CONTROL))
741 (org-lparse-entity-format-callbacks-alist
742 (org-lparse-get 'ENTITY-FORMAT))
743 (opt-plist
744 (org-export-process-option-filters
745 (org-combine-plists (org-default-export-plist)
746 ext-plist
747 (org-infile-export-plist))))
748 (body-only (or body-only (plist-get opt-plist :body-only)))
749 valid org-lparse-dyn-first-heading-pos
750 (odd org-odd-levels-only)
751 (region-p (org-region-active-p))
752 (rbeg (and region-p (region-beginning)))
753 (rend (and region-p (region-end)))
754 (subtree-p
755 (if (plist-get opt-plist :ignore-subtree-p)
757 (when region-p
758 (save-excursion
759 (goto-char rbeg)
760 (and (org-at-heading-p)
761 (>= (org-end-of-subtree t t) rend))))))
762 (level-offset (if subtree-p
763 (save-excursion
764 (goto-char rbeg)
765 (+ (funcall outline-level)
766 (if org-odd-levels-only 1 0)))
768 (opt-plist (setq org-export-opt-plist
769 (if subtree-p
770 (org-export-add-subtree-options opt-plist rbeg)
771 opt-plist)))
772 ;; The following two are dynamically scoped into other
773 ;; routines below.
774 (org-current-export-dir
775 (or pub-dir (org-lparse-get 'EXPORT-DIR opt-plist)))
776 (org-current-export-file buffer-file-name)
777 (level 0) (line "") (origline "") txt todo
778 (umax nil)
779 (umax-toc nil)
780 (filename (if to-buffer nil
781 (expand-file-name
782 (concat
783 (file-name-sans-extension
784 (or (and subtree-p
785 (org-entry-get (region-beginning)
786 "EXPORT_FILE_NAME" t))
787 (file-name-nondirectory buffer-file-name)))
788 "." (org-lparse-get 'FILE-NAME-EXTENSION opt-plist))
789 (file-name-as-directory
790 (or pub-dir (org-lparse-get 'EXPORT-DIR opt-plist))))))
791 (current-dir (if buffer-file-name
792 (file-name-directory buffer-file-name)
793 default-directory))
794 (buffer (if to-buffer
795 (cond
796 ((eq to-buffer 'string)
797 (get-buffer-create (org-lparse-get 'EXPORT-BUFFER-NAME)))
798 (t (get-buffer-create to-buffer)))
799 (find-file-noselect
800 (or (let ((f (org-lparse-get 'INIT-METHOD)))
801 (and f (functionp f) (funcall f filename)))
802 filename))))
803 (org-levels-open (make-vector org-level-max nil))
804 (date (plist-get opt-plist :date))
805 (date (cond
806 ((and date (string-match "%" date))
807 (format-time-string date))
808 (date date)
809 (t (format-time-string "%Y-%m-%d %T %Z"))))
810 (dummy (setq opt-plist (plist-put opt-plist :effective-date date)))
811 (title (org-xml-encode-org-text-skip-links
812 (or (and subtree-p (org-export-get-title-from-subtree))
813 (plist-get opt-plist :title)
814 (and (not body-only)
815 (not
816 (plist-get opt-plist :skip-before-1st-heading))
817 (org-export-grab-title-from-buffer))
818 (and buffer-file-name
819 (file-name-sans-extension
820 (file-name-nondirectory buffer-file-name)))
821 "UNTITLED")))
822 (dummy (setq opt-plist (plist-put opt-plist :title title)))
823 (html-table-tag (plist-get opt-plist :html-table-tag))
824 (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
825 (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
826 (org-lparse-dyn-current-environment nil)
827 ;; Get the language-dependent settings
828 (lang-words (or (assoc (plist-get opt-plist :language)
829 org-export-language-setup)
830 (assoc "en" org-export-language-setup)))
831 (dummy (setq opt-plist (plist-put opt-plist :lang-words lang-words)))
832 (head-count 0) cnt
833 (start 0)
834 (coding-system-for-write
835 (or (ignore-errors (org-lparse-get 'CODING-SYSTEM-FOR-WRITE))
836 (and (boundp 'buffer-file-coding-system)
837 buffer-file-coding-system)))
838 (save-buffer-coding-system
839 (or (ignore-errors (org-lparse-get 'CODING-SYSTEM-FOR-SAVE))
840 (and (boundp 'buffer-file-coding-system)
841 buffer-file-coding-system)))
842 (region
843 (buffer-substring
844 (if region-p (region-beginning) (point-min))
845 (if region-p (region-end) (point-max))))
846 (org-export-have-math nil)
847 (org-export-footnotes-seen nil)
848 (org-export-footnotes-data (org-footnote-all-labels 'with-defs))
849 (org-footnote-insert-pos-for-preprocessor 'point-min)
850 (org-lparse-opt-plist opt-plist)
851 (lines
852 (org-split-string
853 (org-export-preprocess-string
854 region
855 :emph-multiline t
856 :for-backend (if (equal org-lparse-backend 'xhtml) ; hack
857 'html
858 org-lparse-backend)
859 :skip-before-1st-heading
860 (plist-get opt-plist :skip-before-1st-heading)
861 :drawers (plist-get opt-plist :drawers)
862 :todo-keywords (plist-get opt-plist :todo-keywords)
863 :tasks (plist-get opt-plist :tasks)
864 :tags (plist-get opt-plist :tags)
865 :priority (plist-get opt-plist :priority)
866 :footnotes (plist-get opt-plist :footnotes)
867 :timestamps (plist-get opt-plist :timestamps)
868 :archived-trees
869 (plist-get opt-plist :archived-trees)
870 :select-tags (plist-get opt-plist :select-tags)
871 :exclude-tags (plist-get opt-plist :exclude-tags)
872 :add-text
873 (plist-get opt-plist :text)
874 :LaTeX-fragments
875 (plist-get opt-plist :LaTeX-fragments))
876 "[\r\n]"))
877 table-open
878 table-buffer table-orig-buffer
880 rpl path attr desc descp desc1 desc2 link
881 snumber fnc
882 footnotes footref-seen
883 org-lparse-output-buffer
884 org-lparse-footnote-definitions
885 org-lparse-footnote-number
886 org-lparse-footnote-buffer
887 org-lparse-toc
888 href
891 (let ((inhibit-read-only t))
892 (org-unmodified
893 (remove-text-properties (point-min) (point-max)
894 '(:org-license-to-kill t))))
896 (message "Exporting...")
897 (org-init-section-numbers)
899 ;; Switch to the output buffer
900 (setq org-lparse-output-buffer buffer)
901 (set-buffer org-lparse-output-buffer)
902 (let ((inhibit-read-only t)) (erase-buffer))
903 (fundamental-mode)
904 (org-install-letbind)
906 (and (fboundp 'set-buffer-file-coding-system)
907 (set-buffer-file-coding-system coding-system-for-write))
909 (let ((case-fold-search nil)
910 (org-odd-levels-only odd))
911 ;; create local variables for all options, to make sure all called
912 ;; functions get the correct information
913 (mapc (lambda (x)
914 (set (make-local-variable (nth 2 x))
915 (plist-get opt-plist (car x))))
916 org-export-plist-vars)
917 (setq umax (if arg (prefix-numeric-value arg)
918 org-export-headline-levels))
919 (setq umax-toc (if (integerp org-export-with-toc)
920 (min org-export-with-toc umax)
921 umax))
923 (when (and org-export-with-toc (not body-only))
924 (setq lines (org-lparse-prepare-toc
925 lines level-offset opt-plist umax-toc)))
927 (unless body-only
928 (org-lparse-begin 'DOCUMENT-CONTENT opt-plist)
929 (org-lparse-begin 'DOCUMENT-BODY opt-plist))
931 (setq head-count 0)
932 (org-init-section-numbers)
934 (org-lparse-begin-paragraph)
936 (while (setq line (pop lines) origline line)
937 (catch 'nextline
938 (when (and (org-lparse-current-environment-p 'quote)
939 (string-match org-outline-regexp-bol line))
940 (org-lparse-end-environment 'quote))
942 (when (org-lparse-current-environment-p 'quote)
943 (org-lparse-insert 'LINE line)
944 (throw 'nextline nil))
946 ;; Fixed-width, verbatim lines (examples)
947 (when (and org-export-with-fixed-width
948 (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line))
949 (when (not (org-lparse-current-environment-p 'fixedwidth))
950 (org-lparse-begin-environment 'fixedwidth))
951 (org-lparse-insert 'LINE (match-string 3 line))
952 (when (or (not lines)
953 (not (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)"
954 (car lines))))
955 (org-lparse-end-environment 'fixedwidth))
956 (throw 'nextline nil))
958 ;; Notes: The baseline version of org-html.el (git commit
959 ;; 3d802e), while encoutering a *line-long* protected text,
960 ;; does one of the following two things based on the state
961 ;; of the export buffer.
963 ;; 1. If a paragraph element has just been opened and
964 ;; contains only whitespace as content, insert the
965 ;; protected text as part of the previous paragraph.
967 ;; 2. If the paragraph element has already been opened and
968 ;; contains some valid content insert the protected text
969 ;; as part of the current paragraph.
971 ;; I think --->
973 ;; Scenario 1 mentioned above kicks in when a block of
974 ;; protected text has to be inserted enbloc. For example,
975 ;; this happens, when inserting an source or example block
976 ;; or preformatted content enclosed in #+backend,
977 ;; #+begin_bakend ... #+end_backend)
979 ;; Scenario 2 mentioned above kicks in when the protected
980 ;; text is part of a running sentence. For example this
981 ;; happens in the case of an *multiline* LaTeX equation that
982 ;; needs to be inserted verbatim.
984 ;; org-html.el in the master branch seems to do some
985 ;; jugglery by moving paragraphs around. Inorder to make
986 ;; these changes backend-agnostic introduce a new text
987 ;; property org-native-text and impose the added semantics
988 ;; that these protected blocks appear outside of a
989 ;; conventional paragraph element.
991 ;; Extra Note: Check whether org-example and org-native-text
992 ;; are entirely equivalent.
994 ;; Fixes bug reported by Christian Moe concerning verbatim
995 ;; LaTeX fragments.
996 ;; on git commit 533ba3f90250a1f25f494c390d639ea6274f235c
997 ;; http://repo.or.cz/w/org-mode/org-jambu.git/shortlog/refs/heads/staging
998 ;; See http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01379.html
1000 ;; Native Text
1001 (when (and (get-text-property 0 'org-native-text line)
1002 ;; Make sure it is the entire line that is protected
1003 (not (< (or (next-single-property-change
1004 0 'org-native-text line) 10000)
1005 (length line))))
1006 (let ((ind (get-text-property 0 'original-indentation line)))
1007 (org-lparse-begin-environment 'native)
1008 (org-lparse-insert 'LINE line)
1009 (while (and lines
1010 (or (= (length (car lines)) 0)
1011 (not ind)
1012 (equal ind (get-text-property
1013 0 'original-indentation (car lines))))
1014 (or (= (length (car lines)) 0)
1015 (get-text-property 0 'org-native-text (car lines))))
1016 (org-lparse-insert 'LINE (pop lines)))
1017 (org-lparse-end-environment 'native))
1018 (throw 'nextline nil))
1020 ;; Protected HTML
1021 (when (and (get-text-property 0 'org-protected line)
1022 ;; Make sure it is the entire line that is protected
1023 (not (< (or (next-single-property-change
1024 0 'org-protected line) 10000)
1025 (length line))))
1026 (let ((ind (get-text-property 0 'original-indentation line)))
1027 (org-lparse-insert 'LINE line)
1028 (while (and lines
1029 (or (= (length (car lines)) 0)
1030 (not ind)
1031 (equal ind (get-text-property
1032 0 'original-indentation (car lines))))
1033 (or (= (length (car lines)) 0)
1034 (get-text-property 0 'org-protected (car lines))))
1035 (org-lparse-insert 'LINE (pop lines))))
1036 (throw 'nextline nil))
1038 ;; Blockquotes, verse, and center
1039 (when (string-match "^ORG-\\(.+\\)-\\(START\\|END\\)$" line)
1040 (let* ((style (intern (downcase (match-string 1 line))))
1041 (f (cdr (assoc (match-string 2 line)
1042 '(("START" . org-lparse-begin-environment)
1043 ("END" . org-lparse-end-environment))))))
1044 (when (memq style '(blockquote verse center))
1045 (funcall f style)
1046 (throw 'nextline nil))))
1048 (run-hooks 'org-export-html-after-blockquotes-hook)
1049 (when (org-lparse-current-environment-p 'verse)
1050 (let ((i (org-get-string-indentation line)))
1051 (if (> i 0)
1052 (setq line (concat
1053 (let ((org-lparse-encode-pending t))
1054 (org-lparse-format 'SPACES (* 2 i)))
1055 " " (org-trim line))))
1056 (unless (string-match "\\\\\\\\[ \t]*$" line)
1057 (setq line (concat line "\\\\")))))
1059 ;; make targets to anchors
1060 (setq start 0)
1061 (while (string-match
1062 "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line start)
1063 (cond
1064 ((get-text-property (match-beginning 1) 'org-protected line)
1065 (setq start (match-end 1)))
1066 ((match-end 2)
1067 (setq line (replace-match
1068 (let ((org-lparse-encode-pending t))
1069 (org-lparse-format
1070 'ANCHOR "" (org-solidify-link-text
1071 (match-string 1 line))))
1072 t t line)))
1073 ((and org-export-with-toc (equal (string-to-char line) ?*))
1074 ;; FIXME: NOT DEPENDENT on TOC?????????????????????
1075 (setq line (replace-match
1076 (let ((org-lparse-encode-pending t))
1077 (org-lparse-format
1078 'FONTIFY (match-string 1 line) "target"))
1079 ;; (concat "@<i>" (match-string 1 line) "@</i> ")
1080 t t line)))
1082 (setq line (replace-match
1083 (concat
1084 (let ((org-lparse-encode-pending t))
1085 (org-lparse-format
1086 'ANCHOR (match-string 1 line)
1087 (org-solidify-link-text (match-string 1 line))
1088 "target")) " ")
1089 t t line)))))
1091 (let ((org-lparse-encode-pending t))
1092 (setq line (org-lparse-handle-time-stamps line)))
1094 ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
1095 ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
1096 ;; Also handle sub_superscripts and checkboxes
1097 (or (string-match org-table-hline-regexp line)
1098 (string-match "^[ \t]*\\([+]-\\||[ ]\\)[-+ |]*[+|][ \t]*$" line)
1099 (setq line (org-xml-encode-org-text-skip-links line)))
1101 (setq line (org-lparse-format-org-link line opt-plist))
1103 ;; TODO items
1104 (if (and (string-match org-todo-line-regexp line)
1105 (match-beginning 2))
1106 (setq line (concat
1107 (substring line 0 (match-beginning 2))
1108 (org-lparse-format 'TODO (match-string 2 line))
1109 (substring line (match-end 2)))))
1111 ;; Does this contain a reference to a footnote?
1112 (when org-export-with-footnotes
1113 (setq start 0)
1114 (while (string-match "\\([^* \t].*?\\)[ \t]*\\[\\([0-9]+\\)\\]" line start)
1115 ;; Discard protected matches not clearly identified as
1116 ;; footnote markers.
1117 (if (or (get-text-property (match-beginning 2) 'org-protected line)
1118 (not (get-text-property (match-beginning 2) 'org-footnote line)))
1119 (setq start (match-end 2))
1120 (let ((n (match-string 2 line)) refcnt a)
1121 (if (setq a (assoc n footref-seen))
1122 (progn
1123 (setcdr a (1+ (cdr a)))
1124 (setq refcnt (cdr a)))
1125 (setq refcnt 1)
1126 (push (cons n 1) footref-seen))
1127 (setq line
1128 (replace-match
1129 (concat
1130 (or (match-string 1 line) "")
1131 (org-lparse-format
1132 'FOOTNOTE-REFERENCE
1133 n (cdr (assoc n org-lparse-footnote-definitions))
1134 refcnt)
1135 ;; If another footnote is following the
1136 ;; current one, add a separator.
1137 (if (save-match-data
1138 (string-match "\\`\\[[0-9]+\\]"
1139 (substring line (match-end 0))))
1140 (ignore-errors
1141 (org-lparse-get 'FOOTNOTE-SEPARATOR))
1142 ""))
1143 t t line))))))
1145 (cond
1146 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
1147 ;; This is a headline
1148 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
1149 level-offset))
1150 txt (match-string 2 line))
1151 (if (string-match quote-re0 txt)
1152 (setq txt (replace-match "" t t txt)))
1153 (if (<= level (max umax umax-toc))
1154 (setq head-count (+ head-count 1)))
1155 (unless org-lparse-dyn-first-heading-pos
1156 (setq org-lparse-dyn-first-heading-pos (point)))
1157 (org-lparse-begin-level level txt umax head-count)
1159 ;; QUOTES
1160 (when (string-match quote-re line)
1161 (org-lparse-begin-environment 'quote)))
1163 ((and org-export-with-tables
1164 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
1165 (when (not table-open)
1166 ;; New table starts
1167 (setq table-open t table-buffer nil table-orig-buffer nil))
1169 ;; Accumulate lines
1170 (setq table-buffer (cons line table-buffer)
1171 table-orig-buffer (cons origline table-orig-buffer))
1172 (when (or (not lines)
1173 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
1174 (car lines))))
1175 (setq table-open nil
1176 table-buffer (nreverse table-buffer)
1177 table-orig-buffer (nreverse table-orig-buffer))
1178 (org-lparse-end-paragraph)
1179 (org-lparse-insert 'TABLE table-buffer table-orig-buffer)))
1181 ;; Normal lines
1184 ;; This line either is list item or end a list.
1185 (when (get-text-property 0 'list-item line)
1186 (setq line (org-lparse-export-list-line
1187 line
1188 (get-text-property 0 'list-item line)
1189 (get-text-property 0 'list-struct line)
1190 (get-text-property 0 'list-prevs line))))
1192 ;; Horizontal line
1193 (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
1194 (with-org-lparse-preserve-paragraph-state
1195 (org-lparse-insert 'HORIZONTAL-LINE))
1196 (throw 'nextline nil))
1198 ;; Empty lines start a new paragraph. If hand-formatted lists
1199 ;; are not fully interpreted, lines starting with "-", "+", "*"
1200 ;; also start a new paragraph.
1201 (when (string-match "^ [-+*]-\\|^[ \t]*$" line)
1202 (when org-lparse-footnote-number
1203 (org-lparse-end-footnote-definition org-lparse-footnote-number)
1204 (setq org-lparse-footnote-number nil))
1205 (org-lparse-begin-paragraph))
1207 ;; Is this the start of a footnote?
1208 (when org-export-with-footnotes
1209 (when (and (boundp 'footnote-section-tag-regexp)
1210 (string-match (concat "^" footnote-section-tag-regexp)
1211 line))
1212 ;; ignore this line
1213 (throw 'nextline nil))
1214 (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
1215 (org-lparse-end-paragraph)
1216 (setq org-lparse-footnote-number (match-string 1 line))
1217 (setq line (replace-match "" t t line))
1218 (org-lparse-begin-footnote-definition org-lparse-footnote-number)))
1219 ;; Check if the line break needs to be conserved
1220 (cond
1221 ((string-match "\\\\\\\\[ \t]*$" line)
1222 (setq line (replace-match
1223 (org-lparse-format 'LINE-BREAK)
1224 t t line)))
1225 (org-export-preserve-breaks
1226 (setq line (concat line (org-lparse-format 'LINE-BREAK)))))
1228 ;; Check if a paragraph should be started
1229 (let ((start 0))
1230 (while (and org-lparse-par-open
1231 (string-match "\\\\par\\>" line start))
1232 (error "FIXME")
1233 ;; Leave a space in the </p> so that the footnote matcher
1234 ;; does not see this.
1235 (if (not (get-text-property (match-beginning 0)
1236 'org-protected line))
1237 (setq line (replace-match "</p ><p >" t t line)))
1238 (setq start (match-end 0))))
1240 (org-lparse-insert 'LINE line)))))
1242 ;; Properly close all local lists and other lists
1243 (when (org-lparse-current-environment-p 'quote)
1244 (org-lparse-end-environment 'quote))
1246 (org-lparse-end-level 1 umax)
1248 ;; the </div> to close the last text-... div.
1249 (when (and (> umax 0) org-lparse-dyn-first-heading-pos)
1250 (org-lparse-end-outline-text-or-outline))
1252 (org-lparse-end 'DOCUMENT-BODY opt-plist)
1253 (unless body-only
1254 (org-lparse-end 'DOCUMENT-CONTENT))
1256 (unless (plist-get opt-plist :buffer-will-be-killed)
1257 (set-auto-mode t))
1259 (org-lparse-end 'EXPORT)
1261 (goto-char (point-min))
1262 (or (org-export-push-to-kill-ring
1263 (upcase (symbol-name org-lparse-backend)))
1264 (message "Exporting... done"))
1266 (cond
1267 ((not to-buffer)
1268 (let ((f (org-lparse-get 'SAVE-METHOD)))
1269 (or (and f (functionp f) (funcall f filename opt-plist))
1270 (save-buffer)))
1271 (or (when (and (boundp 'org-lparse-other-backend)
1272 org-lparse-other-backend
1273 (not (equal org-lparse-backend org-lparse-other-backend)))
1274 (let ((org-lparse-convert-process
1275 (org-lparse-get-converter org-lparse-backend)))
1276 (when org-lparse-convert-process
1277 (org-lparse-convert buffer-file-name
1278 (symbol-name org-lparse-other-backend)))))
1279 (current-buffer)))
1280 ((eq to-buffer 'string)
1281 (prog1 (buffer-substring (point-min) (point-max))
1282 (kill-buffer (current-buffer))))
1283 (t (current-buffer))))))
1285 (defun org-lparse-format-table (lines olines)
1286 "Retuns backend-specific code for org-type and table-type tables."
1287 (if (stringp lines)
1288 (setq lines (org-split-string lines "\n")))
1289 (if (string-match "^[ \t]*|" (car lines))
1290 ;; A normal org table
1291 (org-lparse-format-org-table lines nil)
1292 ;; Table made by table.el
1293 (or (org-lparse-format-table-table-using-table-generate-source
1294 ;; FIXME: Need to take care of this during merge
1295 (if (eq org-lparse-backend 'xhtml) 'html org-lparse-backend)
1296 olines
1297 (not org-export-prefer-native-exporter-for-tables))
1298 ;; We are here only when table.el table has NO col or row
1299 ;; spanning and the user prefers using org's own converter for
1300 ;; exporting of such simple table.el tables.
1301 (org-lparse-format-table-table lines))))
1303 (defun org-lparse-table-get-colalign-info (lines)
1304 (let ((forced-aligns (org-find-text-property-in-string
1305 'org-forced-aligns (car lines))))
1306 (when (and forced-aligns org-table-clean-did-remove-column)
1307 (setq forced-aligns
1308 (mapcar (lambda (x) (cons (1- (car x)) (cdr x))) forced-aligns)))
1310 forced-aligns))
1312 (defvar org-lparse-table-style)
1313 (defvar org-lparse-table-ncols)
1314 (defvar org-lparse-table-rownum)
1315 (defvar org-lparse-table-is-styled)
1316 (defvar org-lparse-table-begin-marker)
1317 (defvar org-lparse-table-num-numeric-items-per-column)
1318 (defvar org-lparse-table-colalign-info)
1319 (defvar org-lparse-table-colalign-vector)
1321 ;; Following variables are defined in org-table.el
1322 (defvar org-table-number-fraction)
1323 (defvar org-table-number-regexp)
1325 (defun org-lparse-do-format-org-table (lines &optional splice)
1326 "Format a org-type table into backend-specific code.
1327 LINES is a list of lines. Optional argument SPLICE means, do not
1328 insert header and surrounding <table> tags, just format the lines.
1329 Optional argument NO-CSS means use XHTML attributes instead of CSS
1330 for formatting. This is required for the DocBook exporter."
1331 (require 'org-table)
1332 ;; Get rid of hlines at beginning and end
1333 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
1334 (setq lines (nreverse lines))
1335 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
1336 (setq lines (nreverse lines))
1337 (when org-export-table-remove-special-lines
1338 ;; Check if the table has a marking column. If yes remove the
1339 ;; column and the special lines
1340 (setq lines (org-table-clean-before-export lines)))
1342 (let* ((caption (org-find-text-property-in-string 'org-caption (car lines)))
1343 (caption (and caption (org-xml-encode-org-text caption)))
1344 (label (org-find-text-property-in-string 'org-label (car lines)))
1345 (org-lparse-table-colalign-info (org-lparse-table-get-colalign-info lines))
1346 (attributes (org-find-text-property-in-string 'org-attributes
1347 (car lines)))
1348 (head (and org-export-highlight-first-table-line
1349 (delq nil (mapcar
1350 (lambda (x) (string-match "^[ \t]*|-" x))
1351 (cdr lines)))))
1352 (org-lparse-table-rownum -1) org-lparse-table-ncols i (cnt 0)
1353 tbopen line fields
1354 org-lparse-table-cur-rowgrp-is-hdr
1355 org-lparse-table-rowgrp-open
1356 org-lparse-table-num-numeric-items-per-column
1357 org-lparse-table-colalign-vector n
1358 org-lparse-table-rowgrp-info
1359 org-lparse-table-begin-marker
1360 (org-lparse-table-style 'org-table)
1361 org-lparse-table-is-styled)
1362 (cond
1363 (splice
1364 (setq org-lparse-table-is-styled nil)
1365 (while (setq line (pop lines))
1366 (unless (string-match "^[ \t]*|-" line)
1367 (insert
1368 (org-lparse-format-table-row
1369 (org-split-string line "[ \t]*|[ \t]*")) "\n"))))
1371 (setq org-lparse-table-is-styled t)
1372 (org-lparse-begin 'TABLE caption label attributes)
1373 (setq org-lparse-table-begin-marker (point))
1374 (org-lparse-begin-table-rowgroup head)
1375 (while (setq line (pop lines))
1376 (cond
1377 ((string-match "^[ \t]*|-" line)
1378 (when lines (org-lparse-begin-table-rowgroup)))
1380 (insert
1381 (org-lparse-format-table-row
1382 (org-split-string line "[ \t]*|[ \t]*")) "\n"))))
1383 (org-lparse-end 'TABLE-ROWGROUP)
1384 (org-lparse-end-table)))))
1386 (defun org-lparse-format-org-table (lines &optional splice)
1387 (with-temp-buffer
1388 (org-lparse-do-format-org-table lines splice)
1389 (buffer-substring-no-properties (point-min) (point-max))))
1391 (defun org-lparse-do-format-table-table (lines)
1392 "Format a table generated by table.el into backend-specific code.
1393 This conversion does *not* use `table-generate-source' from table.el.
1394 This has the advantage that Org-mode's HTML conversions can be used.
1395 But it has the disadvantage, that no cell- or row-spanning is allowed."
1396 (let (line field-buffer
1397 (org-lparse-table-cur-rowgrp-is-hdr
1398 org-export-highlight-first-table-line)
1399 (caption nil)
1400 (attributes nil)
1401 (label nil)
1402 (org-lparse-table-style 'table-table)
1403 (org-lparse-table-is-styled nil)
1404 fields org-lparse-table-ncols i (org-lparse-table-rownum -1)
1405 (empty (org-lparse-format 'SPACES 1)))
1406 (org-lparse-begin 'TABLE caption label attributes)
1407 (while (setq line (pop lines))
1408 (cond
1409 ((string-match "^[ \t]*\\+-" line)
1410 (when field-buffer
1411 (let ((org-export-table-row-tags '("<tr>" . "</tr>"))
1412 ;; (org-export-html-table-use-header-tags-for-first-column nil)
1414 (insert (org-lparse-format-table-row field-buffer empty)))
1415 (setq org-lparse-table-cur-rowgrp-is-hdr nil)
1416 (setq field-buffer nil)))
1418 ;; Break the line into fields and store the fields
1419 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
1420 (if field-buffer
1421 (setq field-buffer (mapcar
1422 (lambda (x)
1423 (concat x (org-lparse-format 'LINE-BREAK)
1424 (pop fields)))
1425 field-buffer))
1426 (setq field-buffer fields)))))
1427 (org-lparse-end-table)))
1429 (defun org-lparse-format-table-table (lines)
1430 (with-temp-buffer
1431 (org-lparse-do-format-table-table lines)
1432 (buffer-substring-no-properties (point-min) (point-max))))
1434 (defvar table-source-languages) ; defined in table.el
1435 (defun org-lparse-format-table-table-using-table-generate-source (backend
1436 lines
1437 &optional
1438 spanned-only)
1439 "Format a table into BACKEND, using `table-generate-source' from table.el.
1440 Use SPANNED-ONLY to suppress exporting of simple table.el tables.
1442 When SPANNED-ONLY is nil, all table.el tables are exported. When
1443 SPANNED-ONLY is non-nil, only tables with either row or column
1444 spans are exported.
1446 This routine returns the generated source or nil as appropriate.
1448 Refer docstring of `org-export-prefer-native-exporter-for-tables'
1449 for further information."
1450 (require 'table)
1451 (with-current-buffer (get-buffer-create " org-tmp1 ")
1452 (erase-buffer)
1453 (insert (mapconcat 'identity lines "\n"))
1454 (goto-char (point-min))
1455 (if (not (re-search-forward "|[^+]" nil t))
1456 (error "Error processing table"))
1457 (table-recognize-table)
1458 (when (or (not spanned-only)
1459 (let* ((dim (table-query-dimension))
1460 (c (nth 4 dim)) (r (nth 5 dim)) (cells (nth 6 dim)))
1461 (not (= (* c r) cells))))
1462 (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
1463 (cond
1464 ((member backend table-source-languages)
1465 (table-generate-source backend " org-tmp2 ")
1466 (set-buffer " org-tmp2 ")
1467 (buffer-substring (point-min) (point-max)))
1469 ;; table.el doesn't support the given backend. Currently this
1470 ;; happens in case of odt export. Strip the table from the
1471 ;; generated document. A better alternative would be to embed
1472 ;; the table as ascii text in the output document.
1473 (org-lparse-warn
1474 (concat
1475 "Found table.el-type table in the source org file. "
1476 (format "table.el doesn't support %s backend. "
1477 (upcase (symbol-name backend)))
1478 "Skipping ahead ..."))
1479 "")))))
1481 (defun org-lparse-handle-time-stamps (s)
1482 "Format time stamps in string S, or remove them."
1483 (catch 'exit
1484 (let (r b)
1485 (while (string-match org-maybe-keyword-time-regexp s)
1486 (or b (setq b (substring s 0 (match-beginning 0))))
1487 (setq r (concat
1488 r (substring s 0 (match-beginning 0)) " "
1489 (org-lparse-format
1490 'FONTIFY
1491 (concat
1492 (if (match-end 1)
1493 (org-lparse-format
1494 'FONTIFY
1495 (match-string 1 s) "timestamp-kwd"))
1497 (org-lparse-format
1498 'FONTIFY
1499 (substring (org-translate-time (match-string 3 s)) 1 -1)
1500 "timestamp"))
1501 "timestamp-wrapper"))
1502 s (substring s (match-end 0))))
1503 ;; Line break if line started and ended with time stamp stuff
1504 (if (not r)
1506 (setq r (concat r s))
1507 (unless (string-match "\\S-" (concat b s))
1508 (setq r (concat r (org-lparse-format 'LINE-BREAK))))
1509 r))))
1511 (defun org-xml-encode-plain-text (s)
1512 "Convert plain text characters to HTML equivalent.
1513 Possible conversions are set in `org-export-html-protect-char-alist'."
1514 (let ((cl (org-lparse-get 'PLAIN-TEXT-MAP)) c)
1515 (while (setq c (pop cl))
1516 (let ((start 0))
1517 (while (string-match (car c) s start)
1518 (setq s (replace-match (cdr c) t t s)
1519 start (1+ (match-beginning 0))))))
1522 (defun org-xml-encode-org-text-skip-links (string)
1523 "Prepare STRING for HTML export. Apply all active conversions.
1524 If there are links in the string, don't modify these."
1525 (let* ((re (concat org-bracket-link-regexp "\\|"
1526 (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")))
1527 m s l res)
1528 (while (setq m (string-match re string))
1529 (setq s (substring string 0 m)
1530 l (match-string 0 string)
1531 string (substring string (match-end 0)))
1532 (push (org-xml-encode-org-text s) res)
1533 (push l res))
1534 (push (org-xml-encode-org-text string) res)
1535 (apply 'concat (nreverse res))))
1537 (defun org-xml-encode-org-text (s)
1538 "Apply all active conversions to translate special ASCII to HTML."
1539 (setq s (org-xml-encode-plain-text s))
1540 (if org-export-html-expand
1541 (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
1542 (setq s (replace-match "<\\1>" t nil s))))
1543 (if org-export-with-emphasize
1544 (setq s (org-lparse-apply-char-styles s)))
1545 (if org-export-with-special-strings
1546 (setq s (org-lparse-convert-special-strings s)))
1547 (if org-export-with-sub-superscripts
1548 (setq s (org-lparse-apply-sub-superscript-styles s)))
1549 (if org-export-with-TeX-macros
1550 (let ((start 0) wd rep)
1551 (while (setq start (string-match "\\\\\\([a-zA-Z]+[0-9]*\\)\\({}\\)?"
1552 s start))
1553 (if (get-text-property (match-beginning 0) 'org-protected s)
1554 (setq start (match-end 0))
1555 (setq wd (match-string 1 s))
1556 (if (setq rep (org-lparse-format 'ORG-ENTITY wd))
1557 (setq s (replace-match rep t t s))
1558 (setq start (+ start (length wd))))))))
1561 (defun org-lparse-convert-special-strings (string)
1562 "Convert special characters in STRING to HTML."
1563 (let ((all (org-lparse-get 'SPECIAL-STRING-REGEXPS))
1564 e a re rpl start)
1565 (while (setq a (pop all))
1566 (setq re (car a) rpl (cdr a) start 0)
1567 (while (string-match re string start)
1568 (if (get-text-property (match-beginning 0) 'org-protected string)
1569 (setq start (match-end 0))
1570 (setq string (replace-match rpl t nil string)))))
1571 string))
1573 (defun org-lparse-apply-sub-superscript-styles (string)
1574 "Apply subscript and superscript styles to STRING.
1575 Use `org-export-with-sub-superscripts' to control application of
1576 sub and superscript styles."
1577 (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
1578 (while (string-match org-match-substring-regexp string s)
1579 (cond
1580 ((and requireb (match-end 8)) (setq s (match-end 2)))
1581 ((get-text-property (match-beginning 2) 'org-protected string)
1582 (setq s (match-end 2)))
1584 (setq s (match-end 1)
1585 key (if (string= (match-string 2 string) "_")
1586 'subscript 'superscript)
1587 c (or (match-string 8 string)
1588 (match-string 6 string)
1589 (match-string 5 string))
1590 string (replace-match
1591 (concat (match-string 1 string)
1592 (org-lparse-format 'FONTIFY c key))
1593 t t string)))))
1594 (while (string-match "\\\\\\([_^]\\)" string)
1595 (setq string (replace-match (match-string 1 string) t t string)))
1596 string))
1598 (defvar org-lparse-char-styles
1599 `(("*" bold)
1600 ("/" emphasis)
1601 ("_" underline)
1602 ("=" code)
1603 ("~" verbatim)
1604 ("+" strike))
1605 "Map Org emphasis markers to char styles.
1606 This is an alist where each element is of the
1607 form (ORG-EMPHASIS-CHAR . CHAR-STYLE).")
1609 (defun org-lparse-apply-char-styles (string)
1610 "Apply char styles to STRING.
1611 The variable `org-lparse-char-styles' controls how the Org
1612 emphasis markers are interpreted."
1613 (let ((s 0) rpl)
1614 (while (string-match org-emph-re string s)
1615 (if (not (equal
1616 (substring string (match-beginning 3) (1+ (match-beginning 3)))
1617 (substring string (match-beginning 4) (1+ (match-beginning 4)))))
1618 (setq s (match-beginning 0)
1620 (concat
1621 (match-string 1 string)
1622 (org-lparse-format
1623 'FONTIFY (match-string 4 string)
1624 (nth 1 (assoc (match-string 3 string)
1625 org-lparse-char-styles)))
1626 (match-string 5 string))
1627 string (replace-match rpl t t string)
1628 s (+ s (- (length rpl) 2)))
1629 (setq s (1+ s))))
1630 string))
1632 (defun org-lparse-export-list-line (line pos struct prevs)
1633 "Insert list syntax in export buffer. Return LINE, maybe modified.
1635 POS is the item position or line position the line had before
1636 modifications to buffer. STRUCT is the list structure. PREVS is
1637 the alist of previous items."
1638 (let* ((get-type
1639 (function
1640 ;; Translate type of list containing POS to "d", "o" or
1641 ;; "u".
1642 (lambda (pos struct prevs)
1643 (let ((type (org-list-get-list-type pos struct prevs)))
1644 (cond
1645 ((eq 'ordered type) "o")
1646 ((eq 'descriptive type) "d")
1647 (t "u"))))))
1648 (get-closings
1649 (function
1650 ;; Return list of all items and sublists ending at POS, in
1651 ;; reverse order.
1652 (lambda (pos)
1653 (let (out)
1654 (catch 'exit
1655 (mapc (lambda (e)
1656 (let ((end (nth 6 e))
1657 (item (car e)))
1658 (cond
1659 ((= end pos) (push item out))
1660 ((>= item pos) (throw 'exit nil)))))
1661 struct))
1662 out)))))
1663 ;; First close any previous item, or list, ending at POS.
1664 (mapc (lambda (e)
1665 (let* ((lastp (= (org-list-get-last-item e struct prevs) e))
1666 (first-item (org-list-get-list-begin e struct prevs))
1667 (type (funcall get-type first-item struct prevs)))
1668 (org-lparse-end-paragraph)
1669 ;; Ending for every item
1670 (org-lparse-end-list-item type)
1671 ;; We're ending last item of the list: end list.
1672 (when lastp
1673 (org-lparse-end-list type)
1674 (org-lparse-begin-paragraph))))
1675 (funcall get-closings pos))
1676 (cond
1677 ;; At an item: insert appropriate tags in export buffer.
1678 ((assq pos struct)
1679 (string-match
1680 (concat "[ \t]*\\(\\S-+[ \t]*\\)"
1681 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
1682 "\\(?:\\(\\[[ X-]\\]\\)[ \t]+\\)?"
1683 "\\(?:\\(.*\\)[ \t]+::\\(?:[ \t]+\\|$\\)\\)?"
1684 "\\(.*\\)") line)
1685 (let* ((checkbox (match-string 3 line))
1686 (desc-tag (or (match-string 4 line) "???"))
1687 (body (or (match-string 5 line) ""))
1688 (list-beg (org-list-get-list-begin pos struct prevs))
1689 (firstp (= list-beg pos))
1690 ;; Always refer to first item to determine list type, in
1691 ;; case list is ill-formed.
1692 (type (funcall get-type list-beg struct prevs))
1693 (counter (let ((count-tmp (org-list-get-counter pos struct)))
1694 (cond
1695 ((not count-tmp) nil)
1696 ((string-match "[A-Za-z]" count-tmp)
1697 (- (string-to-char (upcase count-tmp)) 64))
1698 ((string-match "[0-9]+" count-tmp)
1699 count-tmp)))))
1700 (when firstp
1701 (org-lparse-end-paragraph)
1702 (org-lparse-begin-list type))
1704 (let ((arg (cond ((equal type "d") desc-tag)
1705 ((equal type "o") counter))))
1706 (org-lparse-begin 'LIST-ITEM type arg))
1708 ;; If line had a checkbox, some additional modification is required.
1709 (when checkbox
1710 (setq body
1711 (concat
1712 (org-lparse-format
1713 'FONTIFY (concat
1715 (cond
1716 ((string-match "X" checkbox) "X")
1717 ((string-match " " checkbox)
1718 (org-lparse-format 'SPACES 1))
1719 (t "-"))
1720 "]")
1721 'code)
1723 body)))
1724 ;; Return modified line
1725 body))
1726 ;; At a list ender: go to next line (side-effects only).
1727 ((equal "ORG-LIST-END-MARKER" line) (throw 'nextline nil))
1728 ;; Not at an item: return line unchanged (side-effects only).
1729 (t line))))
1731 (defun org-lparse-bind-local-variables (opt-plist)
1732 (mapc (lambda (x)
1733 (set (make-local-variable (nth 2 x))
1734 (plist-get opt-plist (car x))))
1735 org-export-plist-vars))
1737 (defvar org-lparse-table-rowgrp-open)
1738 (defvar org-lparse-table-cur-rowgrp-is-hdr)
1739 (defvar org-lparse-footnote-number)
1740 (defvar org-lparse-footnote-definitions)
1741 (defvar org-lparse-footnote-buffer)
1742 (defvar org-lparse-output-buffer)
1744 (defcustom org-lparse-debug nil
1745 "Enable or Disable logging of `org-lparse' callbacks.
1746 The parameters passed to the backend-registered ENTITY-CONTROL
1747 and ENTITY-FORMAT callbacks are logged as comment strings in the
1748 exported buffer. (org-lparse-format 'COMMENT fmt args) is used
1749 for logging. Customize this variable only if you are an expert
1750 user. Valid values of this variable are:
1751 nil : Disable logging
1752 control : Log all invocations of `org-lparse-begin' and
1753 `org-lparse-end' callbacks.
1754 format : Log invocations of `org-lparse-format' callbacks.
1755 t : Log all invocations of `org-lparse-begin', `org-lparse-end'
1756 and `org-lparse-format' callbacks,"
1757 :group 'org-lparse
1758 :type '(choice
1759 (const :tag "Disable" nil)
1760 (const :tag "Format callbacks" format)
1761 (const :tag "Control callbacks" control)
1762 (const :tag "Format and Control callbacks" t)))
1764 (defun org-lparse-begin (entity &rest args)
1765 "Begin ENTITY in current buffer. ARGS is entity specific.
1766 ENTITY can be one of PARAGRAPH, LIST, LIST-ITEM etc.
1768 Use (org-lparse-begin 'LIST \"o\") to begin a list in current
1769 buffer.
1771 See `org-xhtml-entity-control-callbacks-alist' for more
1772 information."
1773 (when (and (member org-lparse-debug '(t control))
1774 (not (eq entity 'DOCUMENT-CONTENT)))
1775 (insert (org-lparse-format 'COMMENT "%s BEGIN %S" entity args)))
1777 (let ((f (cadr (assoc entity org-lparse-entity-control-callbacks-alist))))
1778 (unless f (error "Unknown entity: %s" entity))
1779 (apply f args)))
1781 (defun org-lparse-end (entity &rest args)
1782 "Close ENTITY in current buffer. ARGS is entity specific.
1783 ENTITY can be one of PARAGRAPH, LIST, LIST-ITEM
1784 etc.
1786 Use (org-lparse-end 'LIST \"o\") to close a list in current
1787 buffer.
1789 See `org-xhtml-entity-control-callbacks-alist' for more
1790 information."
1791 (when (and (member org-lparse-debug '(t control))
1792 (not (eq entity 'DOCUMENT-CONTENT)))
1793 (insert (org-lparse-format 'COMMENT "%s END %S" entity args)))
1795 (let ((f (caddr (assoc entity org-lparse-entity-control-callbacks-alist))))
1796 (unless f (error "Unknown entity: %s" entity))
1797 (apply f args)))
1799 (defun org-lparse-begin-paragraph (&optional style)
1800 "Insert <p>, but first close previous paragraph if any."
1801 (org-lparse-end-paragraph)
1802 (org-lparse-begin 'PARAGRAPH style)
1803 (setq org-lparse-par-open t))
1805 (defun org-lparse-end-paragraph ()
1806 "Close paragraph if there is one open."
1807 (when org-lparse-par-open
1808 (org-lparse-end 'PARAGRAPH)
1809 (setq org-lparse-par-open nil)))
1811 (defun org-lparse-end-list-item (&optional type)
1812 "Close <li> if necessary."
1813 (org-lparse-end-paragraph)
1814 (org-lparse-end 'LIST-ITEM (or type "u")))
1816 (defvar org-lparse-dyn-current-environment nil)
1817 (defun org-lparse-begin-environment (style)
1818 (assert (not org-lparse-dyn-current-environment) t)
1819 (setq org-lparse-dyn-current-environment style)
1820 (org-lparse-begin 'ENVIRONMENT style))
1822 (defun org-lparse-end-environment (style)
1823 (org-lparse-end 'ENVIRONMENT style)
1825 (assert (eq org-lparse-dyn-current-environment style) t)
1826 (setq org-lparse-dyn-current-environment nil))
1828 (defun org-lparse-current-environment-p (style)
1829 (eq org-lparse-dyn-current-environment style))
1831 (defun org-lparse-begin-footnote-definition (n)
1832 (unless org-lparse-footnote-buffer
1833 (setq org-lparse-footnote-buffer
1834 (get-buffer-create "*Org HTML Export Footnotes*")))
1835 (set-buffer org-lparse-footnote-buffer)
1836 (erase-buffer)
1837 (setq org-lparse-insert-tag-with-newlines nil)
1838 (org-lparse-begin 'FOOTNOTE-DEFINITION n))
1840 (defun org-lparse-end-footnote-definition (n)
1841 (org-lparse-end 'FOOTNOTE-DEFINITION n)
1842 (setq org-lparse-insert-tag-with-newlines 'both)
1843 (push (cons n (buffer-string)) org-lparse-footnote-definitions)
1844 (set-buffer org-lparse-output-buffer))
1846 (defun org-lparse-format (entity &rest args)
1847 "Format ENTITY in backend-specific way and return it.
1848 ARGS is specific to entity being formatted.
1850 Use (org-lparse-format 'HEADING \"text\" 1) to format text as
1851 level 1 heading.
1853 See `org-xhtml-entity-format-callbacks-alist' for more information."
1854 (when (and (member org-lparse-debug '(t format))
1855 (not (equal entity 'COMMENT)))
1856 (insert (org-lparse-format 'COMMENT "%s: %S" entity args)))
1857 (cond
1858 ((consp entity)
1859 (let ((text (pop args)))
1860 (apply 'org-lparse-format 'TAGS entity text args)))
1862 (let ((f (cdr (assoc entity org-lparse-entity-format-callbacks-alist))))
1863 (unless f (error "Unknown entity: %s" entity))
1864 (apply f args)))))
1866 (defun org-lparse-insert (entity &rest args)
1867 (insert (apply 'org-lparse-format entity args)))
1869 (defun org-lparse-prepare-toc (lines level-offset opt-plist umax-toc)
1870 (let* ((quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
1871 (org-min-level (org-get-min-level lines level-offset))
1872 (org-last-level org-min-level)
1873 level)
1874 (with-temp-buffer
1875 (org-lparse-bind-local-variables opt-plist)
1876 (erase-buffer)
1877 (org-lparse-begin 'TOC (nth 3 (plist-get opt-plist :lang-words)))
1878 (setq
1879 lines
1880 (mapcar
1881 #'(lambda (line)
1882 (when (and (string-match org-todo-line-regexp line)
1883 (not (get-text-property 0 'org-protected line))
1884 (<= (setq level (org-tr-level
1885 (- (match-end 1) (match-beginning 1)
1886 level-offset)))
1887 umax-toc))
1888 (let ((txt (save-match-data
1889 (org-xml-encode-org-text-skip-links
1890 (org-export-cleanup-toc-line
1891 (match-string 3 line)))))
1892 (todo (and
1893 org-export-mark-todo-in-toc
1894 (or (and (match-beginning 2)
1895 (not (member (match-string 2 line)
1896 org-done-keywords)))
1897 (and (= level umax-toc)
1898 (org-search-todo-below
1899 line lines level)))))
1900 tags)
1901 ;; Check for targets
1902 (while (string-match org-any-target-regexp line)
1903 (setq line
1904 (replace-match
1905 (let ((org-lparse-encode-pending t))
1906 (org-lparse-format 'FONTIFY
1907 (match-string 1 line) "target"))
1908 t t line)))
1909 (when (string-match
1910 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
1911 (setq tags (match-string 1 txt)
1912 txt (replace-match "" t nil txt)))
1913 (when (string-match quote-re0 txt)
1914 (setq txt (replace-match "" t t txt)))
1915 (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
1916 (setq txt (replace-match "" t t txt)))
1917 (org-lparse-format
1918 'TOC-ITEM
1919 (let* ((snumber (org-section-number level))
1920 (href (replace-regexp-in-string
1921 "\\." "-" (format "sec-%s" snumber)))
1922 (href
1924 (cdr (assoc
1925 href org-export-preferred-target-alist))
1926 href))
1927 (href (org-solidify-link-text href)))
1928 (org-lparse-format 'TOC-ENTRY snumber todo txt tags href))
1929 level org-last-level)
1930 (setq org-last-level level)))
1931 line)
1932 lines))
1933 (org-lparse-end 'TOC)
1934 (setq org-lparse-toc (buffer-string))))
1935 lines)
1937 (defun org-lparse-format-table-row (fields &optional text-for-empty-fields)
1938 (unless org-lparse-table-ncols
1939 ;; first row of the table
1940 (setq org-lparse-table-ncols (length fields))
1941 (when org-lparse-table-is-styled
1942 (setq org-lparse-table-num-numeric-items-per-column
1943 (make-vector org-lparse-table-ncols 0))
1944 (setq org-lparse-table-colalign-vector
1945 (make-vector org-lparse-table-ncols nil))
1946 (let ((c -1))
1947 (while (< (incf c) org-lparse-table-ncols)
1948 (let ((cookie (cdr (assoc (1+ c) org-lparse-table-colalign-info))))
1949 (setf (aref org-lparse-table-colalign-vector c)
1950 (cond
1951 ((string= cookie "l") "left")
1952 ((string= cookie "r") "right")
1953 ((string= cookie "c") "center")
1954 (t nil))))))))
1955 (incf org-lparse-table-rownum)
1956 (let ((i -1))
1957 (org-lparse-format
1958 'TABLE-ROW
1959 (mapconcat
1960 (lambda (x)
1961 (when (and (string= x "") text-for-empty-fields)
1962 (setq x text-for-empty-fields))
1963 (incf i)
1964 (and org-lparse-table-is-styled
1965 (< i org-lparse-table-ncols)
1966 (string-match org-table-number-regexp x)
1967 (incf (aref org-lparse-table-num-numeric-items-per-column i)))
1968 (org-lparse-format 'TABLE-CELL x org-lparse-table-rownum i))
1969 fields "\n"))))
1971 (defun org-lparse-get (what &optional opt-plist)
1972 "Query for value of WHAT for the current backend `org-lparse-backend'.
1973 See also `org-lparse-backend-get'."
1974 (if (boundp 'org-lparse-backend)
1975 (org-lparse-backend-get (symbol-name org-lparse-backend) what opt-plist)
1976 (error "org-lparse-backend is not bound yet")))
1978 (defun org-lparse-backend-get (backend what &optional opt-plist)
1979 "Query BACKEND for value of WHAT.
1980 Dispatch the call to `org-<backend>-user-get'. If that throws an
1981 error, dispatch the call to `org-<backend>-get'. See
1982 `org-xhtml-get' for all known settings queried for by
1983 `org-lparse' during the course of export."
1984 (assert (stringp backend) t)
1985 (unless (org-lparse-backend-is-native-p backend)
1986 (error "Unknown native backend %s" backend))
1987 (let ((backend-get-method (intern (format "org-%s-get" backend)))
1988 (backend-user-get-method (intern (format "org-%s-user-get" backend))))
1989 (cond
1990 ((functionp backend-get-method)
1991 (condition-case nil
1992 (funcall backend-user-get-method what opt-plist)
1993 (error (funcall backend-get-method what opt-plist))))
1995 (error "Native backend %s doesn't define %s" backend backend-get-method)))))
1997 (defun org-lparse-insert-tag (tag &rest args)
1998 (when (member org-lparse-insert-tag-with-newlines '(lead both))
1999 (insert "\n"))
2000 (insert (apply 'format tag args))
2001 (when (member org-lparse-insert-tag-with-newlines '(trail both))
2002 (insert "\n")))
2004 (defun org-lparse-get-targets-from-title (title)
2005 (let* ((target (org-get-text-property-any 0 'target title))
2006 (extra-targets (assoc target org-export-target-aliases))
2007 (target (or (cdr (assoc target org-export-preferred-target-alist))
2008 target)))
2009 (cons target (remove target extra-targets))))
2011 (defun org-lparse-suffix-from-snumber (snumber)
2012 (let* ((snu (replace-regexp-in-string "\\." "-" snumber))
2013 (href (cdr (assoc (concat "sec-" snu)
2014 org-export-preferred-target-alist))))
2015 (org-solidify-link-text (or href snu))))
2017 (defun org-lparse-begin-level (level title umax head-count)
2018 "Insert a new LEVEL in HTML export.
2019 When TITLE is nil, just close all open levels."
2020 (org-lparse-end-level level umax)
2021 (unless title (error "Why is heading nil"))
2022 (let* ((targets (org-lparse-get-targets-from-title title))
2023 (target (car targets)) (extra-targets (cdr targets))
2024 (target (and target (org-solidify-link-text target)))
2025 (extra-class (org-get-text-property-any 0 'html-container-class title))
2026 snumber tags level1 class)
2027 (when (string-match (org-re "\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") title)
2028 (setq tags (and org-export-with-tags (match-string 1 title)))
2029 (setq title (replace-match "" t t title)))
2030 (if (> level umax)
2031 (progn
2032 (if (aref org-levels-open (1- level))
2033 (org-lparse-end-list-item)
2034 (aset org-levels-open (1- level) t)
2035 (org-lparse-end-paragraph)
2036 (org-lparse-begin-list 'unordered))
2037 (org-lparse-begin
2038 'LIST-ITEM 'unordered target
2039 (org-lparse-format 'HEADLINE title extra-targets tags)))
2040 (aset org-levels-open (1- level) t)
2041 (setq snumber (org-section-number level))
2042 (setq level1 (+ level (or (org-lparse-get 'TOPLEVEL-HLEVEL) 1) -1))
2043 (unless (= head-count 1)
2044 (org-lparse-end-outline-text-or-outline))
2045 (org-lparse-begin-outline-and-outline-text
2046 level1 snumber title tags target extra-targets extra-class)
2047 (org-lparse-begin-paragraph))))
2049 (defun org-lparse-end-level (level umax)
2050 (org-lparse-end-paragraph)
2051 (loop for l from org-level-max downto level
2052 do (when (aref org-levels-open (1- l))
2053 ;; Terminate one level in HTML export
2054 (if (<= l umax)
2055 (org-lparse-end-outline-text-or-outline)
2056 (org-lparse-end-list-item)
2057 (org-lparse-end-list 'unordered))
2058 (aset org-levels-open (1- l) nil))))
2060 (defvar org-lparse-outline-text-open)
2061 (defun org-lparse-begin-outline-and-outline-text (level1 snumber title tags
2062 target extra-targets
2063 extra-class)
2064 (org-lparse-begin
2065 'OUTLINE level1 snumber title tags target extra-targets extra-class)
2066 (org-lparse-begin-outline-text level1 snumber extra-class))
2068 (defun org-lparse-end-outline-text-or-outline ()
2069 (cond
2070 (org-lparse-outline-text-open
2071 (org-lparse-end 'OUTLINE-TEXT)
2072 (setq org-lparse-outline-text-open nil))
2073 (t (org-lparse-end 'OUTLINE))))
2075 (defun org-lparse-begin-outline-text (level1 snumber extra-class)
2076 (assert (not org-lparse-outline-text-open) t)
2077 (setq org-lparse-outline-text-open t)
2078 (org-lparse-begin 'OUTLINE-TEXT level1 snumber extra-class))
2080 (defun org-lparse-html-list-type-to-canonical-list-type (ltype)
2081 (cdr (assoc ltype '(("o" . ordered)
2082 ("u" . unordered)
2083 ("d" . description)))))
2085 (defvar org-lparse-list-level) ; dynamically bound in org-do-lparse
2086 (defun org-lparse-begin-list (ltype)
2087 (incf org-lparse-list-level)
2088 (org-lparse-begin 'LIST ltype))
2090 (defun org-lparse-end-list (ltype)
2091 (decf org-lparse-list-level)
2092 (org-lparse-end 'LIST ltype))
2094 (defvar org-lparse-table-rowgrp-info)
2095 (defun org-lparse-begin-table-rowgroup (&optional is-header-row)
2096 (push (cons (1+ org-lparse-table-rownum) :start) org-lparse-table-rowgrp-info)
2097 (org-lparse-begin 'TABLE-ROWGROUP is-header-row))
2099 (defun org-lparse-end-table ()
2100 (when org-lparse-table-is-styled
2101 ;; column groups
2102 (unless (car org-table-colgroup-info)
2103 (setq org-table-colgroup-info
2104 (cons :start (cdr org-table-colgroup-info))))
2106 ;; column alignment
2107 (let ((c -1))
2108 (mapc
2109 (lambda (x)
2110 (incf c)
2111 (setf (aref org-lparse-table-colalign-vector c)
2112 (or (aref org-lparse-table-colalign-vector c)
2113 (if (> (/ (float x) (1+ org-lparse-table-rownum))
2114 org-table-number-fraction)
2115 "right" "left"))))
2116 org-lparse-table-num-numeric-items-per-column)))
2117 (org-lparse-end 'TABLE))
2119 (defvar org-lparse-encode-pending nil)
2121 (defun org-lparse-format-tags (tag text prefix suffix &rest args)
2122 (cond
2123 ((consp tag)
2124 (concat prefix (apply 'format (car tag) args) text suffix
2125 (format (cdr tag))))
2126 ((stringp tag) ; singleton tag
2127 (concat prefix (apply 'format tag args) text))))
2129 (defun org-xml-fix-class-name (kwd) ; audit callers of this function
2130 "Turn todo keyword into a valid class name.
2131 Replaces invalid characters with \"_\"."
2132 (save-match-data
2133 (while (string-match "[^a-zA-Z0-9_]" kwd)
2134 (setq kwd (replace-match "_" t t kwd))))
2135 kwd)
2137 (defun org-lparse-format-todo (todo)
2138 (org-lparse-format 'FONTIFY
2139 (concat
2140 (ignore-errors (org-lparse-get 'TODO-KWD-CLASS-PREFIX))
2141 (org-xml-fix-class-name todo))
2142 (list (if (member todo org-done-keywords) "done" "todo")
2143 todo)))
2145 (defun org-lparse-format-extra-targets (extra-targets)
2146 (if (not extra-targets) ""
2147 (mapconcat (lambda (x)
2148 (setq x (org-solidify-link-text
2149 (if (org-uuidgen-p x) (concat "ID-" x) x)))
2150 (org-lparse-format 'ANCHOR "" x))
2151 extra-targets "")))
2153 (defun org-lparse-format-org-tags (tags)
2154 (if (not tags) ""
2155 (org-lparse-format
2156 'FONTIFY (mapconcat
2157 (lambda (x)
2158 (org-lparse-format
2159 'FONTIFY x
2160 (concat
2161 (ignore-errors (org-lparse-get 'TAG-CLASS-PREFIX))
2162 (org-xml-fix-class-name x))))
2163 (org-split-string tags ":")
2164 (org-lparse-format 'SPACES 1)) "tag")))
2166 (defun org-lparse-format-section-number (&optional snumber level)
2167 (and org-export-with-section-numbers
2168 (not org-lparse-body-only) snumber level
2169 (org-lparse-format 'FONTIFY snumber (format "section-number-%d" level))))
2171 (defun org-lparse-warn (msg)
2172 (if (not org-lparse-use-flashy-warning)
2173 (message msg)
2174 (put-text-property 0 (length msg) 'face 'font-lock-warning-face msg)
2175 (message msg)
2176 (sleep-for 3)))
2178 (defun org-xml-format-href (s)
2179 "Make sure the S is valid as a href reference in an XHTML document."
2180 (save-match-data
2181 (let ((start 0))
2182 (while (string-match "&" s start)
2183 (setq start (+ (match-beginning 0) 3)
2184 s (replace-match "&amp;" t t s)))))
2187 (defun org-xml-format-desc (s)
2188 "Make sure the S is valid as a description in a link."
2189 (if (and s (not (get-text-property 1 'org-protected s)))
2190 (save-match-data
2191 (org-xml-encode-org-text s))
2194 (provide 'org-lparse)
2196 ;;; org-lparse.el ends here