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
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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.
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
64 ;; (interactive "Mbackend: \nP")
66 (let* ((input (if (featurep 'ido
) 'ido-completing-read
'completing-read
))
67 (all-backends (org-lparse-all-backends))
69 (funcall input
"Export to: " all-backends nil t nil
))
72 ;; (and (org-lparse-backend-is-native-p 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
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
)
86 (when org-export-kill-product-buffer-when-displayed
87 (kill-buffer (current-buffer))))))
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:
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
))
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
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
))))
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 (eq major-mode
'org-mode
)
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
129 (setq backend-string
(org-lparse-region backend
(point-min)
130 (point-max) t
'string
)))
132 (delete-region beg end
)
133 (insert backend-string
)))
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
)
155 (setq ext-plist
(plist-put ext-plist
:ignore-subtree-p t
))
157 (set-mark (point)) ;; to activate the region
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
)
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."
183 (current-dir (if buffer-file-name
184 (file-name-directory buffer-file-name
)
186 (link-validate (plist-get opt-plist
:link-validation-function
))
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
))))
196 ((match-end 2) (match-string 2 line
))
198 (or (file-name-absolute-p path
)
199 (string-match "^\\.\\.?/" path
)))
202 (setq path
(org-extract-attributes 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
)
213 (if (string-match "^file:" desc
)
214 (setq desc
(substring desc
(match-end 0)))))
216 (setq desc
(org-add-props
217 (org-lparse-format 'INLINE-IMAGE desc
)
218 '(org-protected t
)))))
220 ((equal type
"internal")
223 (if (= (string-to-char path
) ?
#)
228 'ORG-LINK opt-plist
"" "" (org-solidify-link-text
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...)
237 (setq id-file
(file-relative-name
239 (file-name-directory org-current-export-file
)))
242 'ORG-LINK opt-plist type id-file
243 (concat (if (org-uuidgen-p path
) "ID-") path
)
245 ((member type
'("http" "https"))
246 ;; standard URL, can inline as image
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
254 'ORG-LINK opt-plist type path nil desc attr descp
)))
256 ((string= type
"coderef")
257 (setq rpl
(org-lparse-format
258 'ORG-LINK opt-plist type
"" path desc nil descp
)))
260 ((functionp (setq fnc
(nth 2 (assoc type org-link-protocols
))))
261 ;; The link protocol has a function for format the link
264 (funcall fnc
(org-link-unescape path
) desc1
'html
))))
266 ((string= type
"file")
272 (string-match "::\\(.*\\)" path
)
274 (replace-match "" t nil path
)
275 (match-string 1 path
))
278 ;;The proper path, without a fragment
286 ;;Check the fragment. If it can't be used as
287 ;;target fragment we'll pass nil instead.
291 (not (string-match "^[0-9]*$" fragment-0
))
292 (not (string-match "^\\*" fragment-0
))
293 (not (string-match "^/.*/$" fragment-0
)))
294 (org-solidify-link-text
295 (org-link-unescape fragment-0
))
298 ;;Description minus "file:" and ".org"
299 (if (string-match "^file:" desc
)
301 ((desc-1 (replace-match "" t t desc
)))
302 (if (string-match "\\.org$" desc-1
)
303 (replace-match "" t t desc-1
)
310 (functionp link-validate
)
311 (not (funcall link-validate path-1 current-dir
)))
314 'ORG-LINK opt-plist
"file" path-1 fragment-1
315 desc-2 attr descp
))))))
318 ;; just publish the path, as default
319 (setq rpl
(concat "<i><" type
":"
320 (save-match-data (org-link-unescape path
))
322 (setq line
(replace-match rpl t t line
)
323 start
(+ start
(length rpl
))))
326 (defmacro with-org-lparse-preserve-paragraph-state
(&rest body
)
327 `(let ((org-lparse-do-open-par org-lparse-par-open
))
328 (org-lparse-end-paragraph)
330 (when org-lparse-do-open-par
331 (org-lparse-begin-paragraph))))
332 (def-edebug-spec with-org-lparse-preserve-paragraph-state
(body))
334 (defvar org-lparse-native-backends nil
335 "List of native backends registered with `org-lparse'.
336 A backend can use `org-lparse-register-backend' to add itself to
339 All native backends must implement a get routine and a mandatory
340 set of callback routines.
342 The get routine must be named as org-<backend>-get where backend
343 is the name of the backend. The exporter uses `org-lparse-get'
344 and retrieves the backend-specific callback by querying for
345 ENTITY-CONTROL and ENTITY-FORMAT variables.
347 For the sake of illustration, the html backend implements
348 `org-xhtml-get'. It returns
349 `org-xhtml-entity-control-callbacks-alist' and
350 `org-xhtml-entity-format-callbacks-alist' as the values of
351 ENTITY-CONTROL and ENTITY-FORMAT settings.")
353 (defun org-lparse-register-backend (backend)
354 "Make BACKEND known to `org-lparse' library.
355 Add BACKEND to `org-lparse-native-backends'."
358 ((symbolp backend
) (symbol-name backend
))
359 ((stringp backend
) backend
)
360 (t (error "Error while registering backend: %S" backend
))))
361 (add-to-list 'org-lparse-native-backends backend
)))
363 (defun org-lparse-unregister-backend (backend)
364 (setq org-lparse-native-backends
366 ((symbolp backend
) (symbol-name backend
))
367 ((stringp backend
) backend
))
368 org-lparse-native-backends
))
369 (message "Unregistered backend %S" backend
))
371 (defun org-lparse-get-other-backends (in-fmt)
372 "Return OUTPUT-FMT-ALIST corresponding to IN-FMT.
373 See `org-lparse-convert-capabilities' for definition of
375 (when (org-lparse-get-converter in-fmt
)
376 (or (ignore-errors (org-lparse-backend-get in-fmt
'OTHER-BACKENDS
) )
378 (dolist (c org-lparse-convert-capabilities
)
379 (when (member in-fmt
(nth 1 c
))
380 (throw 'out-fmts
(nth 2 c
))))))))
382 (defun org-lparse-get-converter (in-fmt)
383 "Return converter associated with IN-FMT.
384 See `org-lparse-convert-capabilities' for further information."
385 (or (ignore-errors (org-lparse-backend-get in-fmt
'CONVERT-METHOD
))
386 org-lparse-convert-process
))
388 (defun org-lparse-all-backends ()
389 "Return all formats to which `org-lparse' could export to.
390 The return value is an alist of the form (TARGET-BACKEND
391 NATIVE-BACKEND-1 NATIVE-BACKEND-2 ...) with the condition that
392 the org file can be exported to TARGET-BACKEND via any one of
393 NATIVE-BACKEND-1, NATIVE-BACKEND-2 etc.
395 For example, an entry of the form \"(\"pdf\" \"odt\" \"xhtml\")\"
396 would mean that the org file could be exported to \"pdf\" format
397 by exporting natively either to \"xhtml\" or \"odt\" backends."
399 (flet ((add (other native
)
400 (let ((val (assoc-string other all-backends t
)))
401 (if val
(setcdr val
(nconc (list native
) (cdr val
)))
402 (push (cons other
(list native
)) all-backends
)))))
403 (loop for backend in org-lparse-native-backends
404 do
(loop for other in
405 (mapcar #'car
(org-lparse-get-other-backends backend
))
406 do
(add other backend
))))
409 (defun org-lparse-backend-is-native-p (backend)
410 (member backend org-lparse-native-backends
))
412 (defun org-lparse (target-backend native-backend arg
413 &optional hidden ext-plist
414 to-buffer body-only pub-dir
)
415 "Export the outline to various formats.
416 If there is an active region, export only the region. The
417 outline is first exported to NATIVE-BACKEND and optionally
418 converted to TARGET-BACKEND. See `org-lparse-native-backends'
419 for list of known native backends. Each native backend can
420 specify a converter and list of target backends it exports to
421 using the CONVERT-PROCESS and OTHER-BACKENDS settings of it's get
422 method. See `org-xhtml-get' for an illustrative example.
424 ARG is a prefix argument that specifies how many levels of
425 outline should become headlines. The default is 3. Lower levels
426 will become bulleted lists.
428 HIDDEN is obsolete and does nothing.
430 EXT-PLIST is a property list that controls various aspects of
431 export. The settings here override org-mode's default settings
432 and but are inferior to file-local settings.
434 TO-BUFFER dumps the exported lines to a buffer or a string
435 instead of a file. If TO-BUFFER is the symbol `string' return the
436 exported lines as a string. If TO-BUFFER is non-nil, create a
437 buffer with that name and export to that buffer.
439 BODY-ONLY controls the presence of header and footer lines in
440 exported text. If BODY-ONLY is non-nil, don't produce the file
441 header and footer, simply return the content of <body>...</body>,
442 without even the body tags themselves.
444 PUB-DIR specifies the publishing directory."
446 (let* ((input (if (featurep 'ido
) 'ido-completing-read
'completing-read
))
447 (all-backends (org-lparse-all-backends))
450 (funcall input
"Export to: " all-backends nil t nil
)))
452 (let ((choices (if target-backend
453 (cdr (assoc target-backend all-backends
))
454 (or org-lparse-native-backends
455 (error "No registered backends")))))
456 (funcall input
"Use Native backend: " choices nil t nil
))))
457 (list target-backend native-backend current-prefix-arg
)))
458 (let* ((org-lparse-backend (intern native-backend
))
459 (org-lparse-other-backend (and target-backend
460 (intern target-backend
))))
461 (unless (org-lparse-backend-is-native-p native-backend
)
462 (error "Don't know how to export natively to backend %s" native-backend
))
463 (unless (or (not target-backend
)
464 (equal target-backend native-backend
)
465 (assoc target-backend
(org-lparse-get-other-backends
467 (error "Don't know how to export to backend %s %s" target-backend
468 (format "via %s" native-backend
)))
469 (run-hooks 'org-export-first-hook
)
470 (org-do-lparse arg hidden ext-plist to-buffer body-only pub-dir
)))
472 (defcustom org-lparse-convert-processes
473 '(("BasicODConverter"
474 ("soffice" "-norestore" "-invisible" "-headless"
475 "\"macro:///BasicODConverter.Main.Convert(%I,%f,%O)\""))
477 ("unoconv" "-f" "%f" "-o" "%d" "%i")))
478 "Specify a list of document converters and their usage.
479 The converters in this list are offered as choices while
480 customizing `org-lparse-convert-process'.
482 This variable is an alist where each element is of the
483 form (CONVERTER-NAME (CONVERTER-PROGRAM ARG1 ARG2 ...)).
484 CONVERTER-NAME is name of the converter. CONVERTER-PROGRAM is
485 the name of the executable. ARG1, ARG2 etc are command line
486 options that are passed to CONVERTER-PROGRAM. Format specifiers
487 can be used in the ARGs and they are interpreted as below:
489 %i input file name in full
490 %I input file name as a URL
491 %f format of the output file
492 %o output file name in full
493 %O output file name as a URL
494 %d output dir in full
495 %D output dir as a URL."
499 (const :tag
"None" nil
)
500 (alist :tag
"Converters"
501 :key-type
(string :tag
"Converter Name")
502 :value-type
(group (cons (string :tag
"Executable")
503 (repeat (string :tag
"Command line args")))))))
504 (defcustom org-lparse-convert-process nil
505 "Command to convert from an Org exported format to other formats.
506 During customization, the list of choices are populated from
507 `org-lparse-convert-processes'. Refer afore-mentioned variable
508 for further information."
510 :type
'(choice :convert-widget
512 (apply 'widget-convert
(widget-type w
)
513 (eval (car (widget-get w
:args
)))))
514 `((const :tag
"None" nil
)
515 ,@(mapcar (lambda (c)
516 `(const :tag
,(car c
) ,(cadr c
)))
517 org-lparse-convert-processes
))))
519 (defcustom org-lparse-convert-capabilities
521 ("odt" "ott" "doc" "rtf")
522 (("pdf" "pdf") ("odt" "odt") ("xhtml" "html") ("rtf" "rtf")
523 ("ott" "ott") ("doc" "doc") ("ooxml" "xml") ("html" "html")))
525 ("html" "xhtml") (("pdf" "pdf") ("odt" "txt") ("html" "html")))
527 ("ods" "ots" "xls" "csv")
528 (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv")
529 ("ods" "ods") ("xls" "xls") ("xhtml" "xhtml") ("ooxml" "xml")))
532 (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("xhtml" "xml")
533 ("otp" "otp") ("ppt" "ppt") ("odg" "odg") ("html" "html"))))
534 "Specify input and output formats of `org-lparse-convert-process'.
535 More correctly, specify the set of input and output formats that
536 the user is actually interested in.
538 This variable is an alist where each element is of the
539 form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
540 INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
541 alist where each element is of the form (OUTPUT-FMT
542 OUTPUT-FILE-EXTENSION).
544 The variable is interpreted as follows:
545 `org-lparse-convert-process' can take any document that is in
546 INPUT-FMT-LIST and produce any document that is in the
547 OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
548 OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
549 serves dual purposes:
550 - It is used for populating completion candidates during
551 `org-lparse' and `org-lparse-convert' commands.
553 - It is used as the value of \"%f\" specifier in
554 `org-lparse-convert-process'.
556 DOCUMENT-CLASS is used to group a set of file formats in
557 INPUT-FMT-LIST in to a single class.
559 Note that this variable inherently captures how LibreOffice based
560 converters work. LibreOffice maps documents of various formats
561 to classes like Text, Web, Spreadsheet, Presentation etc and
562 allow document of a given class (irrespective of it's source
563 format) to be converted to any of the export formats associated
566 See default setting of this variable for an typical
571 (const :tag
"None" nil
)
572 (alist :key-type
(string :tag
"Document Class")
574 (group (repeat :tag
"Input formats" (string :tag
"Input format"))
575 (alist :tag
"Output formats"
576 :key-type
(string :tag
"Output format")
578 (group (string :tag
"Output file extension")))))))
580 (defcustom org-lparse-use-flashy-warning nil
581 "Control flashing of messages logged with `org-lparse-warn'.
582 When non-nil, messages are fontified with warning face and the
583 exporter lingers for a while to catch user's attention."
587 (defun org-lparse-convert (&optional in-file out-fmt prefix-arg
)
588 "Convert IN-FILE to format OUT-FMT using a command line converter.
589 IN-FILE is the file to be converted. If unspecified, it defaults
590 to variable `buffer-file-name'. OUT-FMT is the desired output
591 format. If the backend has registered a CONVERT-METHOD as part
592 of it's get function then that converter is used. Otherwise
593 `org-lparse-convert-process' is used. If PREFIX-ARG is non-nil
594 then the newly converted file is opened using `org-open-file'."
596 (let* ((input (if (featurep 'ido
) 'ido-completing-read
'completing-read
))
597 (in-file (read-file-name "File to be converted: "
598 nil buffer-file-name t
))
599 (in-fmt (file-name-extension in-file
))
600 (out-fmt-choices (org-lparse-get-other-backends in-fmt
))
602 (or (and out-fmt-choices
603 (funcall input
"Output format: "
604 out-fmt-choices nil nil nil
))
606 "No known converter or no known output formats for %s files"
608 (list in-file out-fmt current-prefix-arg
)))
609 (require 'browse-url
)
610 (let* ((in-file (expand-file-name (or in-file buffer-file-name
)))
611 (dummy (or (file-readable-p in-file
)
612 (error "Cannot read %s" in-file
)))
613 (in-fmt (file-name-extension in-file
))
614 (out-fmt (or out-fmt
(error "Output format unspecified")))
615 (convert-process (org-lparse-get-converter in-fmt
))
616 (program (car convert-process
))
617 (dummy (and (or program
(error "Converter not configured"))
618 (or (executable-find program
)
619 (error "Cannot find converter %s" program
))))
621 (or (assoc out-fmt
(org-lparse-get-other-backends in-fmt
))
622 (error "Cannot convert from %s to %s format" in-fmt out-fmt
)))
623 (out-file (concat (file-name-sans-extension in-file
) "."
624 (nth 1 out-fmt-alist
)))
625 (out-dir (file-name-directory in-file
))
626 (arglist (mapcar (lambda (arg)
628 arg
`((?i .
,in-file
)
629 (?I .
,(browse-url-file-url in-file
))
632 (?O .
,(browse-url-file-url out-file
))
634 (?D .
,(browse-url-file-url out-dir
)))))
635 (cdr convert-process
))))
636 (when (file-exists-p out-file
)
637 (delete-file out-file
))
639 (message "Executing %s %s" program
(mapconcat 'identity arglist
" "))
640 (apply 'call-process program nil nil nil arglist
)
642 ((file-exists-p out-file
)
643 (message "Exported to %s using %s" out-file program
)
645 (message "Opening %s..." out-file
)
646 (org-open-file out-file
))
648 ;; (set-buffer (find-file-noselect out-file))
651 (message "Export to %s failed" out-file
)
654 (defvar org-lparse-insert-tag-with-newlines
'both
)
656 ;; Following variables are let-bound during `org-lparse'
657 (defvar org-lparse-dyn-first-heading-pos
)
658 (defvar org-lparse-toc
)
659 (defvar org-lparse-entity-control-callbacks-alist
)
660 (defvar org-lparse-entity-format-callbacks-alist
)
661 (defvar org-lparse-backend nil
662 "The native backend to which the document is currently exported.
663 This variable is let bound during `org-lparse'. Valid values are
664 one of the symbols corresponding to `org-lparse-native-backends'.
666 Compare this variable with `org-export-current-backend' which is
667 bound only during `org-export-preprocess-string' stage of the
670 See also `org-lparse-other-backend'.")
672 (defvar org-lparse-other-backend nil
673 "The target backend to which the document is currently exported.
674 This variable is let bound during `org-lparse'. This variable is
675 set to either `org-lparse-backend' or one of the symbols
676 corresponding to OTHER-BACKENDS specification of the
679 For example, if a document is exported to \"odt\" then both
680 org-lparse-backend and org-lparse-other-backend are bound to
681 'odt. On the other hand, if a document is exported to \"odt\"
682 and then converted to \"doc\" then org-lparse-backend is set to
683 'odt and org-lparse-other-backend is set to 'doc.")
685 (defvar org-lparse-body-only nil
686 "Bind this to BODY-ONLY arg of `org-lparse'.")
688 (defvar org-lparse-to-buffer nil
689 "Bind this to TO-BUFFER arg of `org-lparse'.")
691 (defun org-do-lparse (arg &optional hidden ext-plist
692 to-buffer body-only pub-dir
)
693 "Export the outline to various formats.
694 See `org-lparse' for more information. This function is a
695 html-agnostic version of the `org-export-as-html' function in 7.5
697 ;; Make sure we have a file name when we need it.
698 (when (and (not (or to-buffer body-only
))
699 (not buffer-file-name
))
700 (if (buffer-base-buffer)
701 (org-set-local 'buffer-file-name
702 (with-current-buffer (buffer-base-buffer)
704 (error "Need a file name to be able to export")))
707 (format "Exporting to %s using org-lparse..."
709 (or org-lparse-backend org-lparse-other-backend
)))))
711 (setq-default org-todo-line-regexp org-todo-line-regexp
)
712 (setq-default org-deadline-line-regexp org-deadline-line-regexp
)
713 (setq-default org-done-keywords org-done-keywords
)
714 (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp
)
715 (let* (hfy-user-sheet-assoc ; let `htmlfontify' know that
716 ; we are interested in
718 org-lparse-encode-pending
722 (org-lparse-list-level 0) ; list level starts at 1. A
723 ; value of 0 implies we are
724 ; outside of any list
725 (org-lparse-list-item-count 0)
726 org-lparse-list-stack
728 ;; list-table related vars
729 org-lparse-list-table-p
730 org-lparse-list-table
:table-cell-open
731 org-lparse-list-table
:table-row
732 org-lparse-list-table
:lines
734 org-lparse-outline-text-open
735 (org-lparse-latex-fragment-fallback ; currently used only by
737 (or (ignore-errors (org-lparse-get 'LATEX-FRAGMENT-FALLBACK
))
738 (if (and (org-check-external-command "latex" "" t
)
739 (org-check-external-command "dvipng" "" t
))
742 (org-lparse-insert-tag-with-newlines 'both
)
743 (org-lparse-to-buffer to-buffer
)
744 (org-lparse-body-only body-only
)
745 (org-lparse-entity-control-callbacks-alist
746 (org-lparse-get 'ENTITY-CONTROL
))
747 (org-lparse-entity-format-callbacks-alist
748 (org-lparse-get 'ENTITY-FORMAT
))
750 (org-export-process-option-filters
751 (org-combine-plists (org-default-export-plist)
753 (org-infile-export-plist))))
754 (body-only (or body-only
(plist-get opt-plist
:body-only
)))
755 valid org-lparse-dyn-first-heading-pos
756 (odd org-odd-levels-only
)
757 (region-p (org-region-active-p))
758 (rbeg (and region-p
(region-beginning)))
759 (rend (and region-p
(region-end)))
761 (if (plist-get opt-plist
:ignore-subtree-p
)
766 (and (org-at-heading-p)
767 (>= (org-end-of-subtree t t
) rend
))))))
768 (level-offset (if subtree-p
771 (+ (funcall outline-level
)
772 (if org-odd-levels-only
1 0)))
774 (opt-plist (setq org-export-opt-plist
776 (org-export-add-subtree-options opt-plist rbeg
)
778 ;; The following two are dynamically scoped into other
780 (org-current-export-dir
781 (or pub-dir
(org-lparse-get 'EXPORT-DIR opt-plist
)))
782 (org-current-export-file buffer-file-name
)
783 (level 0) (line "") (origline "") txt todo
786 (filename (if to-buffer nil
789 (file-name-sans-extension
791 (org-entry-get (region-beginning)
792 "EXPORT_FILE_NAME" t
))
793 (file-name-nondirectory buffer-file-name
)))
794 "." (org-lparse-get 'FILE-NAME-EXTENSION opt-plist
))
795 (file-name-as-directory
796 (or pub-dir
(org-lparse-get 'EXPORT-DIR opt-plist
))))))
797 (current-dir (if buffer-file-name
798 (file-name-directory buffer-file-name
)
800 (buffer (if to-buffer
802 ((eq to-buffer
'string
)
803 (get-buffer-create (org-lparse-get 'EXPORT-BUFFER-NAME
)))
804 (t (get-buffer-create to-buffer
)))
806 (or (let ((f (org-lparse-get 'INIT-METHOD
)))
807 (and f
(functionp f
) (funcall f filename
)))
809 (org-levels-open (make-vector org-level-max nil
))
810 (date (plist-get opt-plist
:date
))
812 ((and date
(string-match "%" date
))
813 (format-time-string date
))
815 (t (format-time-string "%Y-%m-%d %T %Z"))))
816 (dummy (setq opt-plist
(plist-put opt-plist
:effective-date date
)))
817 (title (org-xml-encode-org-text-skip-links
818 (or (and subtree-p
(org-export-get-title-from-subtree))
819 (plist-get opt-plist
:title
)
822 (plist-get opt-plist
:skip-before-1st-heading
))
823 (org-export-grab-title-from-buffer))
824 (and buffer-file-name
825 (file-name-sans-extension
826 (file-name-nondirectory buffer-file-name
)))
828 (dummy (setq opt-plist
(plist-put opt-plist
:title title
)))
829 (html-table-tag (plist-get opt-plist
:html-table-tag
))
830 (quote-re0 (concat "^[ \t]*" org-quote-string
"\\>"))
831 (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string
"\\>\\)"))
832 (org-lparse-dyn-current-environment nil
)
833 ;; Get the language-dependent settings
834 (lang-words (or (assoc (plist-get opt-plist
:language
)
835 org-export-language-setup
)
836 (assoc "en" org-export-language-setup
)))
837 (dummy (setq opt-plist
(plist-put opt-plist
:lang-words lang-words
)))
840 (coding-system-for-write
841 (or (ignore-errors (org-lparse-get 'CODING-SYSTEM-FOR-WRITE
))
842 (and (boundp 'buffer-file-coding-system
)
843 buffer-file-coding-system
)))
844 (save-buffer-coding-system
845 (or (ignore-errors (org-lparse-get 'CODING-SYSTEM-FOR-SAVE
))
846 (and (boundp 'buffer-file-coding-system
)
847 buffer-file-coding-system
)))
850 (if region-p
(region-beginning) (point-min))
851 (if region-p
(region-end) (point-max))))
852 (org-export-have-math nil
)
853 (org-export-footnotes-seen nil
)
854 (org-export-footnotes-data (org-footnote-all-labels 'with-defs
))
855 (org-footnote-insert-pos-for-preprocessor 'point-min
)
856 (org-lparse-opt-plist opt-plist
)
859 (org-export-preprocess-string
862 :for-backend
(if (equal org-lparse-backend
'xhtml
) ; hack
865 :skip-before-1st-heading
866 (plist-get opt-plist
:skip-before-1st-heading
)
867 :drawers
(plist-get opt-plist
:drawers
)
868 :todo-keywords
(plist-get opt-plist
:todo-keywords
)
869 :tasks
(plist-get opt-plist
:tasks
)
870 :tags
(plist-get opt-plist
:tags
)
871 :priority
(plist-get opt-plist
:priority
)
872 :footnotes
(plist-get opt-plist
:footnotes
)
873 :timestamps
(plist-get opt-plist
:timestamps
)
875 (plist-get opt-plist
:archived-trees
)
876 :select-tags
(plist-get opt-plist
:select-tags
)
877 :exclude-tags
(plist-get opt-plist
:exclude-tags
)
879 (plist-get opt-plist
:text
)
881 (plist-get opt-plist
:LaTeX-fragments
))
884 table-buffer table-orig-buffer
886 rpl path attr desc descp desc1 desc2 link
888 footnotes footref-seen
889 org-lparse-output-buffer
890 org-lparse-footnote-definitions
891 org-lparse-footnote-number
893 org-lparse-collect-buffer
894 (org-lparse-collect-count 0) ; things will get haywire if
895 ; collections are chained. Use
896 ; this variable to assert this
902 (let ((inhibit-read-only t
))
904 (remove-text-properties (point-min) (point-max)
905 '(:org-license-to-kill t
))))
907 (message "Exporting...")
908 (org-init-section-numbers)
910 ;; Switch to the output buffer
911 (setq org-lparse-output-buffer buffer
)
912 (set-buffer org-lparse-output-buffer
)
913 (let ((inhibit-read-only t
)) (erase-buffer))
915 (org-install-letbind)
917 (and (fboundp 'set-buffer-file-coding-system
)
918 (set-buffer-file-coding-system coding-system-for-write
))
920 (let ((case-fold-search nil
)
921 (org-odd-levels-only odd
))
922 ;; create local variables for all options, to make sure all called
923 ;; functions get the correct information
925 (set (make-local-variable (nth 2 x
))
926 (plist-get opt-plist
(car x
))))
927 org-export-plist-vars
)
928 (setq umax
(if arg
(prefix-numeric-value arg
)
929 org-export-headline-levels
))
930 (setq umax-toc
(if (integerp org-export-with-toc
)
931 (min org-export-with-toc umax
)
934 (when (and org-export-with-toc
(not body-only
))
935 (setq lines
(org-lparse-prepare-toc
936 lines level-offset opt-plist umax-toc
)))
939 (org-lparse-begin 'DOCUMENT-CONTENT opt-plist
)
940 (org-lparse-begin 'DOCUMENT-BODY opt-plist
))
943 (org-init-section-numbers)
945 (org-lparse-begin-paragraph)
947 (while (setq line
(pop lines
) origline line
)
949 (when (and (org-lparse-current-environment-p 'quote
)
950 (string-match org-outline-regexp-bol line
))
951 (org-lparse-end-environment 'quote
))
953 (when (org-lparse-current-environment-p 'quote
)
954 (org-lparse-insert 'LINE line
)
955 (throw 'nextline nil
))
957 ;; Fixed-width, verbatim lines (examples)
958 (when (and org-export-with-fixed-width
959 (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line
))
960 (when (not (org-lparse-current-environment-p 'fixedwidth
))
961 (org-lparse-begin-environment 'fixedwidth
))
962 (org-lparse-insert 'LINE
(match-string 3 line
))
963 (when (or (not lines
)
964 (not (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)"
966 (org-lparse-end-environment 'fixedwidth
))
967 (throw 'nextline nil
))
969 ;; Notes: The baseline version of org-html.el (git commit
970 ;; 3d802e), while encoutering a *line-long* protected text,
971 ;; does one of the following two things based on the state
972 ;; of the export buffer.
974 ;; 1. If a paragraph element has just been opened and
975 ;; contains only whitespace as content, insert the
976 ;; protected text as part of the previous paragraph.
978 ;; 2. If the paragraph element has already been opened and
979 ;; contains some valid content insert the protected text
980 ;; as part of the current paragraph.
984 ;; Scenario 1 mentioned above kicks in when a block of
985 ;; protected text has to be inserted enbloc. For example,
986 ;; this happens, when inserting an source or example block
987 ;; or preformatted content enclosed in #+backend,
988 ;; #+begin_bakend ... #+end_backend)
990 ;; Scenario 2 mentioned above kicks in when the protected
991 ;; text is part of a running sentence. For example this
992 ;; happens in the case of an *multiline* LaTeX equation that
993 ;; needs to be inserted verbatim.
995 ;; org-html.el in the master branch seems to do some
996 ;; jugglery by moving paragraphs around. Inorder to make
997 ;; these changes backend-agnostic introduce a new text
998 ;; property org-native-text and impose the added semantics
999 ;; that these protected blocks appear outside of a
1000 ;; conventional paragraph element.
1002 ;; Extra Note: Check whether org-example and org-native-text
1003 ;; are entirely equivalent.
1005 ;; Fixes bug reported by Christian Moe concerning verbatim
1007 ;; on git commit 533ba3f90250a1f25f494c390d639ea6274f235c
1008 ;; http://repo.or.cz/w/org-mode/org-jambu.git/shortlog/refs/heads/staging
1009 ;; See http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01379.html
1012 (when (and (get-text-property 0 'org-native-text line
)
1013 ;; Make sure it is the entire line that is protected
1014 (not (< (or (next-single-property-change
1015 0 'org-native-text line
) 10000)
1017 (let ((ind (get-text-property 0 'original-indentation line
)))
1018 (org-lparse-begin-environment 'native
)
1019 (org-lparse-insert 'LINE line
)
1021 (or (= (length (car lines
)) 0)
1023 (equal ind
(get-text-property
1024 0 'original-indentation
(car lines
))))
1025 (or (= (length (car lines
)) 0)
1026 (get-text-property 0 'org-native-text
(car lines
))))
1027 (org-lparse-insert 'LINE
(pop lines
)))
1028 (org-lparse-end-environment 'native
))
1029 (throw 'nextline nil
))
1032 (when (and (get-text-property 0 'org-protected line
)
1033 ;; Make sure it is the entire line that is protected
1034 (not (< (or (next-single-property-change
1035 0 'org-protected line
) 10000)
1037 (let ((ind (get-text-property 0 'original-indentation line
)))
1038 (org-lparse-insert 'LINE line
)
1040 (or (= (length (car lines
)) 0)
1042 (equal ind
(get-text-property
1043 0 'original-indentation
(car lines
))))
1044 (or (= (length (car lines
)) 0)
1045 (get-text-property 0 'org-protected
(car lines
))))
1046 (org-lparse-insert 'LINE
(pop lines
))))
1047 (throw 'nextline nil
))
1049 ;; Blockquotes, verse, and center
1050 (when (string-match "^ORG-\\(.+\\)-\\(START\\|END\\)$" line
)
1051 (let* ((style (intern (downcase (match-string 1 line
))))
1052 (f (cdr (assoc (match-string 2 line
)
1053 '(("START" . org-lparse-begin-environment
)
1054 ("END" . org-lparse-end-environment
))))))
1055 (when (memq style
'(blockquote verse center list-table
))
1057 (throw 'nextline nil
))))
1059 (run-hooks 'org-export-html-after-blockquotes-hook
)
1060 (when (org-lparse-current-environment-p 'verse
)
1061 (let ((i (org-get-string-indentation line
)))
1064 (let ((org-lparse-encode-pending t
))
1065 (org-lparse-format 'SPACES
(* 2 i
)))
1066 " " (org-trim line
))))
1067 (unless (string-match "\\\\\\\\[ \t]*$" line
)
1068 (setq line
(concat line
"\\\\")))))
1070 ;; make targets to anchors
1072 (while (string-match
1073 "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line start
)
1075 ((get-text-property (match-beginning 1) 'org-protected line
)
1076 (setq start
(match-end 1)))
1078 (setq line
(replace-match
1079 (let ((org-lparse-encode-pending t
))
1081 'ANCHOR
"" (org-solidify-link-text
1082 (match-string 1 line
))))
1084 ((and org-export-with-toc
(equal (string-to-char line
) ?
*))
1085 ;; FIXME: NOT DEPENDENT on TOC?????????????????????
1086 (setq line
(replace-match
1087 (let ((org-lparse-encode-pending t
))
1089 'FONTIFY
(match-string 1 line
) "target"))
1090 ;; (concat "@<i>" (match-string 1 line) "@</i> ")
1093 (setq line
(replace-match
1095 (let ((org-lparse-encode-pending t
))
1097 'ANCHOR
(match-string 1 line
)
1098 (org-solidify-link-text (match-string 1 line
))
1102 (let ((org-lparse-encode-pending t
))
1103 (setq line
(org-lparse-handle-time-stamps line
)))
1105 ;; replace "&" by "&", "<" and ">" by "<" and ">"
1106 ;; handle @<..> HTML tags (replace "@>..<" by "<..>")
1107 ;; Also handle sub_superscripts and checkboxes
1108 (or (string-match org-table-hline-regexp line
)
1109 (string-match "^[ \t]*\\([+]-\\||[ ]\\)[-+ |]*[+|][ \t]*$" line
)
1110 (setq line
(org-xml-encode-org-text-skip-links line
)))
1112 (setq line
(org-lparse-format-org-link line opt-plist
))
1115 (if (and (string-match org-todo-line-regexp line
)
1116 (match-beginning 2))
1118 (substring line
0 (match-beginning 2))
1119 (org-lparse-format 'TODO
(match-string 2 line
))
1120 (substring line
(match-end 2)))))
1122 ;; Does this contain a reference to a footnote?
1123 (when org-export-with-footnotes
1125 (while (string-match "\\([^* \t].*?\\)[ \t]*\\[\\([0-9]+\\)\\]" line start
)
1126 ;; Discard protected matches not clearly identified as
1127 ;; footnote markers.
1128 (if (or (get-text-property (match-beginning 2) 'org-protected line
)
1129 (not (get-text-property (match-beginning 2) 'org-footnote line
)))
1130 (setq start
(match-end 2))
1131 (let ((n (match-string 2 line
)) refcnt a
)
1132 (if (setq a
(assoc n footref-seen
))
1134 (setcdr a
(1+ (cdr a
)))
1135 (setq refcnt
(cdr a
)))
1137 (push (cons n
1) footref-seen
))
1141 (or (match-string 1 line
) "")
1144 n
(cdr (assoc n org-lparse-footnote-definitions
))
1146 ;; If another footnote is following the
1147 ;; current one, add a separator.
1148 (if (save-match-data
1149 (string-match "\\`\\[[0-9]+\\]"
1150 (substring line
(match-end 0))))
1152 (org-lparse-get 'FOOTNOTE-SEPARATOR
))
1157 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line
)
1158 ;; This is a headline
1159 (setq level
(org-tr-level (- (match-end 1) (match-beginning 1)
1161 txt
(match-string 2 line
))
1162 (if (string-match quote-re0 txt
)
1163 (setq txt
(replace-match "" t t txt
)))
1164 (if (<= level
(max umax umax-toc
))
1165 (setq head-count
(+ head-count
1)))
1166 (unless org-lparse-dyn-first-heading-pos
1167 (setq org-lparse-dyn-first-heading-pos
(point)))
1168 (org-lparse-begin-level level txt umax head-count
)
1171 (when (string-match quote-re line
)
1172 (org-lparse-begin-environment 'quote
)))
1174 ((and org-export-with-tables
1175 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line
))
1176 (when (not table-open
)
1178 (setq table-open t table-buffer nil table-orig-buffer nil
))
1181 (setq table-buffer
(cons line table-buffer
)
1182 table-orig-buffer
(cons origline table-orig-buffer
))
1183 (when (or (not lines
)
1184 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
1186 (setq table-open nil
1187 table-buffer
(nreverse table-buffer
)
1188 table-orig-buffer
(nreverse table-orig-buffer
))
1189 (org-lparse-end-paragraph)
1190 (org-lparse-insert 'TABLE table-buffer table-orig-buffer
)))
1195 ;; This line either is list item or end a list.
1196 (when (get-text-property 0 'list-item line
)
1197 (setq line
(org-lparse-export-list-line
1199 (get-text-property 0 'list-item line
)
1200 (get-text-property 0 'list-struct line
)
1201 (get-text-property 0 'list-prevs line
))))
1204 (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line
)
1205 (with-org-lparse-preserve-paragraph-state
1206 (org-lparse-insert 'HORIZONTAL-LINE
))
1207 (throw 'nextline nil
))
1209 ;; Empty lines start a new paragraph. If hand-formatted lists
1210 ;; are not fully interpreted, lines starting with "-", "+", "*"
1211 ;; also start a new paragraph.
1212 (when (string-match "^ [-+*]-\\|^[ \t]*$" line
)
1213 (when org-lparse-footnote-number
1214 (org-lparse-end-footnote-definition org-lparse-footnote-number
)
1215 (setq org-lparse-footnote-number nil
))
1216 (org-lparse-begin-paragraph))
1218 ;; Is this the start of a footnote?
1219 (when org-export-with-footnotes
1220 (when (and (boundp 'footnote-section-tag-regexp
)
1221 (string-match (concat "^" footnote-section-tag-regexp
)
1224 (throw 'nextline nil
))
1225 (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line
)
1226 (org-lparse-end-paragraph)
1227 (setq org-lparse-footnote-number
(match-string 1 line
))
1228 (setq line
(replace-match "" t t line
))
1229 (org-lparse-begin-footnote-definition org-lparse-footnote-number
)))
1230 ;; Check if the line break needs to be conserved
1232 ((string-match "\\\\\\\\[ \t]*$" line
)
1233 (setq line
(replace-match
1234 (org-lparse-format 'LINE-BREAK
)
1236 (org-export-preserve-breaks
1237 (setq line
(concat line
(org-lparse-format 'LINE-BREAK
)))))
1239 ;; Check if a paragraph should be started
1241 (while (and org-lparse-par-open
1242 (string-match "\\\\par\\>" line start
))
1244 ;; Leave a space in the </p> so that the footnote matcher
1245 ;; does not see this.
1246 (if (not (get-text-property (match-beginning 0)
1247 'org-protected line
))
1248 (setq line
(replace-match "</p ><p >" t t line
)))
1249 (setq start
(match-end 0))))
1251 (org-lparse-insert 'LINE line
)))))
1253 ;; Properly close all local lists and other lists
1254 (when (org-lparse-current-environment-p 'quote
)
1255 (org-lparse-end-environment 'quote
))
1257 (org-lparse-end-level 1 umax
)
1259 ;; the </div> to close the last text-... div.
1260 (when (and (> umax
0) org-lparse-dyn-first-heading-pos
)
1261 (org-lparse-end-outline-text-or-outline))
1263 (org-lparse-end 'DOCUMENT-BODY opt-plist
)
1265 (org-lparse-end 'DOCUMENT-CONTENT
))
1267 (unless (plist-get opt-plist
:buffer-will-be-killed
)
1270 (org-lparse-end 'EXPORT
)
1272 ;; kill collection buffer
1273 (when org-lparse-collect-buffer
1274 (kill-buffer org-lparse-collect-buffer
))
1276 (goto-char (point-min))
1277 (or (org-export-push-to-kill-ring
1278 (upcase (symbol-name org-lparse-backend
)))
1279 (message "Exporting... done"))
1283 (let ((f (org-lparse-get 'SAVE-METHOD
)))
1284 (or (and f
(functionp f
) (funcall f filename opt-plist
))
1286 (or (when (and (boundp 'org-lparse-other-backend
)
1287 org-lparse-other-backend
1288 (not (equal org-lparse-backend org-lparse-other-backend
)))
1289 (let ((org-lparse-convert-process
1290 (org-lparse-get-converter org-lparse-backend
)))
1291 (when org-lparse-convert-process
1292 (org-lparse-convert buffer-file-name
1293 (symbol-name org-lparse-other-backend
)))))
1295 ((eq to-buffer
'string
)
1296 (prog1 (buffer-substring (point-min) (point-max))
1297 (kill-buffer (current-buffer))))
1298 (t (current-buffer))))))
1300 (defun org-lparse-format-table (lines olines
)
1301 "Retuns backend-specific code for org-type and table-type tables."
1303 (setq lines
(org-split-string lines
"\n")))
1304 (if (string-match "^[ \t]*|" (car lines
))
1305 ;; A normal org table
1306 (org-lparse-format-org-table lines nil
)
1307 ;; Table made by table.el
1308 (or (org-lparse-format-table-table-using-table-generate-source
1309 ;; FIXME: Need to take care of this during merge
1310 (if (eq org-lparse-backend
'xhtml
) 'html org-lparse-backend
)
1312 (not org-export-prefer-native-exporter-for-tables
))
1313 ;; We are here only when table.el table has NO col or row
1314 ;; spanning and the user prefers using org's own converter for
1315 ;; exporting of such simple table.el tables.
1316 (org-lparse-format-table-table lines
))))
1318 (defun org-lparse-table-get-colalign-info (lines)
1319 (let ((col-cookies (org-find-text-property-in-string
1320 'org-col-cookies
(car lines
))))
1321 (when (and col-cookies org-table-clean-did-remove-column
)
1323 (mapcar (lambda (x) (cons (1- (car x
)) (cdr x
))) col-cookies
)))
1326 (defvar org-lparse-table-style
)
1327 (defvar org-lparse-table-ncols
)
1328 (defvar org-lparse-table-rownum
)
1329 (defvar org-lparse-table-is-styled
)
1330 (defvar org-lparse-table-begin-marker
)
1331 (defvar org-lparse-table-num-numeric-items-per-column
)
1332 (defvar org-lparse-table-colalign-info
)
1333 (defvar org-lparse-table-colalign-vector
)
1335 ;; Following variables are defined in org-table.el
1336 (defvar org-table-number-fraction
)
1337 (defvar org-table-number-regexp
)
1338 (defun org-lparse-org-table-to-list-table (lines &optional splice
)
1339 "Convert org-table to list-table.
1340 LINES is a list of the form (ROW1 ROW2 ROW3 ...) where each
1341 element is a `string' representing a single row of org-table.
1342 Thus each ROW has vertical separators \"|\" separating the table
1343 fields. A ROW could also be a row-group separator of the form
1344 \"|---...|\". Return a list of the form (ROW1 ROW2 ROW3
1345 ...). ROW could either be symbol `:hrule' or a list of the
1346 form (FIELD1 FIELD2 FIELD3 ...) as appropriate."
1350 (while (setq line
(pop lines
))
1351 (unless (string-match "^[ \t]*|-" line
)
1352 (push (org-split-string line
"[ \t]*|[ \t]*") lines-1
))))
1354 (while (setq line
(pop lines
))
1356 ((string-match "^[ \t]*|-" line
)
1358 (push :hrule lines-1
)))
1360 (push (org-split-string line
"[ \t]*|[ \t]*") lines-1
))))))
1361 (nreverse lines-1
)))
1363 (defun org-lparse-insert-org-table (lines &optional splice
)
1364 "Format a org-type table into backend-specific code.
1365 LINES is a list of lines. Optional argument SPLICE means, do not
1366 insert header and surrounding <table> tags, just format the lines.
1367 Optional argument NO-CSS means use XHTML attributes instead of CSS
1368 for formatting. This is required for the DocBook exporter."
1369 (require 'org-table
)
1370 ;; Get rid of hlines at beginning and end
1371 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
1372 (setq lines
(nreverse lines
))
1373 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
1374 (setq lines
(nreverse lines
))
1375 (when org-export-table-remove-special-lines
1376 ;; Check if the table has a marking column. If yes remove the
1377 ;; column and the special lines
1378 (setq lines
(org-table-clean-before-export lines
)))
1379 (let* ((caption (org-find-text-property-in-string 'org-caption
(car lines
)))
1380 (caption (and caption
(org-xml-encode-org-text caption
)))
1381 (label (org-find-text-property-in-string 'org-label
(car lines
)))
1382 (org-lparse-table-colalign-info (org-lparse-table-get-colalign-info lines
))
1383 (attributes (org-find-text-property-in-string 'org-attributes
1385 (head (and org-export-highlight-first-table-line
1387 (lambda (x) (string-match "^[ \t]*|-" x
))
1389 (setq lines
(org-lparse-org-table-to-list-table lines splice
))
1390 (org-lparse-insert-list-table
1391 lines splice caption label attributes head org-lparse-table-colalign-info
)))
1393 (defun org-lparse-insert-list-table (lines &optional splice
1394 caption label attributes head
1395 org-lparse-table-colalign-info
)
1396 (or (featurep 'org-table
) ; required for
1397 (require 'org-table
)) ; `org-table-number-regexp'
1398 (let* ((org-lparse-table-rownum -
1) org-lparse-table-ncols i
(cnt 0)
1400 org-lparse-table-cur-rowgrp-is-hdr
1401 org-lparse-table-rowgrp-open
1402 org-lparse-table-num-numeric-items-per-column
1403 org-lparse-table-colalign-vector n
1404 org-lparse-table-rowgrp-info
1405 org-lparse-table-begin-marker
1406 (org-lparse-table-style 'org-table
)
1407 org-lparse-table-is-styled
)
1410 (setq org-lparse-table-is-styled nil
)
1411 (while (setq line
(pop lines
))
1412 (insert (org-lparse-format-table-row line
) "\n")))
1414 (setq org-lparse-table-is-styled t
)
1415 (org-lparse-begin 'TABLE caption label attributes
)
1416 (setq org-lparse-table-begin-marker
(point))
1417 (org-lparse-begin-table-rowgroup head
)
1418 (while (setq line
(pop lines
))
1420 ((equal line
:hrule
)
1421 (org-lparse-begin-table-rowgroup))
1423 (insert (org-lparse-format-table-row line
) "\n"))))
1424 (org-lparse-end 'TABLE-ROWGROUP
)
1425 (org-lparse-end-table)))))
1427 (defun org-lparse-format-org-table (lines &optional splice
)
1429 (org-lparse-insert-org-table lines splice
)
1430 (buffer-substring-no-properties (point-min) (point-max))))
1432 (defun org-lparse-format-list-table (lines &optional splice
)
1434 (org-lparse-insert-list-table lines splice
)
1435 (buffer-substring-no-properties (point-min) (point-max))))
1437 (defun org-lparse-insert-table-table (lines)
1438 "Format a table generated by table.el into backend-specific code.
1439 This conversion does *not* use `table-generate-source' from table.el.
1440 This has the advantage that Org-mode's HTML conversions can be used.
1441 But it has the disadvantage, that no cell- or row-spanning is allowed."
1442 (let (line field-buffer
1443 (org-lparse-table-cur-rowgrp-is-hdr
1444 org-export-highlight-first-table-line
)
1448 (org-lparse-table-style 'table-table
)
1449 (org-lparse-table-is-styled nil
)
1450 fields org-lparse-table-ncols i
(org-lparse-table-rownum -
1)
1451 (empty (org-lparse-format 'SPACES
1)))
1452 (org-lparse-begin 'TABLE caption label attributes
)
1453 (while (setq line
(pop lines
))
1455 ((string-match "^[ \t]*\\+-" line
)
1457 (let ((org-export-table-row-tags '("<tr>" .
"</tr>"))
1458 ;; (org-export-html-table-use-header-tags-for-first-column nil)
1460 (insert (org-lparse-format-table-row field-buffer empty
)))
1461 (setq org-lparse-table-cur-rowgrp-is-hdr nil
)
1462 (setq field-buffer nil
)))
1464 ;; Break the line into fields and store the fields
1465 (setq fields
(org-split-string line
"[ \t]*|[ \t]*"))
1467 (setq field-buffer
(mapcar
1469 (concat x
(org-lparse-format 'LINE-BREAK
)
1472 (setq field-buffer fields
)))))
1473 (org-lparse-end-table)))
1475 (defun org-lparse-format-table-table (lines)
1477 (org-lparse-insert-table-table lines
)
1478 (buffer-substring-no-properties (point-min) (point-max))))
1480 (defvar table-source-languages
) ; defined in table.el
1481 (defun org-lparse-format-table-table-using-table-generate-source (backend
1485 "Format a table into BACKEND, using `table-generate-source' from table.el.
1486 Use SPANNED-ONLY to suppress exporting of simple table.el tables.
1488 When SPANNED-ONLY is nil, all table.el tables are exported. When
1489 SPANNED-ONLY is non-nil, only tables with either row or column
1492 This routine returns the generated source or nil as appropriate.
1494 Refer docstring of `org-export-prefer-native-exporter-for-tables'
1495 for further information."
1497 (with-current-buffer (get-buffer-create " org-tmp1 ")
1499 (insert (mapconcat 'identity lines
"\n"))
1500 (goto-char (point-min))
1501 (if (not (re-search-forward "|[^+]" nil t
))
1502 (error "Error processing table"))
1503 (table-recognize-table)
1504 (when (or (not spanned-only
)
1505 (let* ((dim (table-query-dimension))
1506 (c (nth 4 dim
)) (r (nth 5 dim
)) (cells (nth 6 dim
)))
1507 (not (= (* c r
) cells
))))
1508 (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
1510 ((member backend table-source-languages
)
1511 (table-generate-source backend
" org-tmp2 ")
1512 (set-buffer " org-tmp2 ")
1513 (buffer-substring (point-min) (point-max)))
1515 ;; table.el doesn't support the given backend. Currently this
1516 ;; happens in case of odt export. Strip the table from the
1517 ;; generated document. A better alternative would be to embed
1518 ;; the table as ascii text in the output document.
1521 "Found table.el-type table in the source org file. "
1522 (format "table.el doesn't support %s backend. "
1523 (upcase (symbol-name backend
)))
1524 "Skipping ahead ..."))
1527 (defun org-lparse-handle-time-stamps (s)
1528 "Format time stamps in string S, or remove them."
1531 (while (string-match org-maybe-keyword-time-regexp s
)
1532 (or b
(setq b
(substring s
0 (match-beginning 0))))
1534 r
(substring s
0 (match-beginning 0)) " "
1541 (match-string 1 s
) "timestamp-kwd"))
1545 (substring (org-translate-time (match-string 3 s
)) 1 -
1)
1547 "timestamp-wrapper"))
1548 s
(substring s
(match-end 0))))
1549 ;; Line break if line started and ended with time stamp stuff
1552 (setq r
(concat r s
))
1553 (unless (string-match "\\S-" (concat b s
))
1554 (setq r
(concat r
(org-lparse-format 'LINE-BREAK
))))
1557 (defun org-xml-encode-plain-text (s)
1558 "Convert plain text characters to HTML equivalent.
1559 Possible conversions are set in `org-export-html-protect-char-alist'."
1560 (let ((cl (org-lparse-get 'PLAIN-TEXT-MAP
)) c
)
1561 (while (setq c
(pop cl
))
1563 (while (string-match (car c
) s start
)
1564 (setq s
(replace-match (cdr c
) t t s
)
1565 start
(1+ (match-beginning 0))))))
1568 (defun org-xml-encode-org-text-skip-links (string)
1569 "Prepare STRING for HTML export. Apply all active conversions.
1570 If there are links in the string, don't modify these."
1571 (let* ((re (concat org-bracket-link-regexp
"\\|"
1572 (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")))
1574 (while (setq m
(string-match re string
))
1575 (setq s
(substring string
0 m
)
1576 l
(match-string 0 string
)
1577 string
(substring string
(match-end 0)))
1578 (push (org-xml-encode-org-text s
) res
)
1580 (push (org-xml-encode-org-text string
) res
)
1581 (apply 'concat
(nreverse res
))))
1583 (defun org-xml-encode-org-text (s)
1584 "Apply all active conversions to translate special ASCII to HTML."
1585 (setq s
(org-xml-encode-plain-text s
))
1586 (if org-export-html-expand
1587 (while (string-match "@<\\([^&]*\\)>" s
)
1588 (setq s
(replace-match "<\\1>" t nil s
))))
1589 (if org-export-with-emphasize
1590 (setq s
(org-lparse-apply-char-styles s
)))
1591 (if org-export-with-special-strings
1592 (setq s
(org-lparse-convert-special-strings s
)))
1593 (if org-export-with-sub-superscripts
1594 (setq s
(org-lparse-apply-sub-superscript-styles s
)))
1595 (if org-export-with-TeX-macros
1596 (let ((start 0) wd rep
)
1597 (while (setq start
(string-match "\\\\\\([a-zA-Z]+[0-9]*\\)\\({}\\)?"
1599 (if (get-text-property (match-beginning 0) 'org-protected s
)
1600 (setq start
(match-end 0))
1601 (setq wd
(match-string 1 s
))
1602 (if (setq rep
(org-lparse-format 'ORG-ENTITY wd
))
1603 (setq s
(replace-match rep t t s
))
1604 (setq start
(+ start
(length wd
))))))))
1607 (defun org-lparse-convert-special-strings (string)
1608 "Convert special characters in STRING to HTML."
1609 (let ((all (org-lparse-get 'SPECIAL-STRING-REGEXPS
))
1611 (while (setq a
(pop all
))
1612 (setq re
(car a
) rpl
(cdr a
) start
0)
1613 (while (string-match re string start
)
1614 (if (get-text-property (match-beginning 0) 'org-protected string
)
1615 (setq start
(match-end 0))
1616 (setq string
(replace-match rpl t nil string
)))))
1619 (defun org-lparse-apply-sub-superscript-styles (string)
1620 "Apply subscript and superscript styles to STRING.
1621 Use `org-export-with-sub-superscripts' to control application of
1622 sub and superscript styles."
1623 (let (key c
(s 0) (requireb (eq org-export-with-sub-superscripts
'{})))
1624 (while (string-match org-match-substring-regexp string s
)
1626 ((and requireb
(match-end 8)) (setq s
(match-end 2)))
1627 ((get-text-property (match-beginning 2) 'org-protected string
)
1628 (setq s
(match-end 2)))
1630 (setq s
(match-end 1)
1631 key
(if (string= (match-string 2 string
) "_")
1632 'subscript
'superscript
)
1633 c
(or (match-string 8 string
)
1634 (match-string 6 string
)
1635 (match-string 5 string
))
1636 string
(replace-match
1637 (concat (match-string 1 string
)
1638 (org-lparse-format 'FONTIFY c key
))
1640 (while (string-match "\\\\\\([_^]\\)" string
)
1641 (setq string
(replace-match (match-string 1 string
) t t string
)))
1644 (defvar org-lparse-char-styles
1651 "Map Org emphasis markers to char styles.
1652 This is an alist where each element is of the
1653 form (ORG-EMPHASIS-CHAR . CHAR-STYLE).")
1655 (defun org-lparse-apply-char-styles (string)
1656 "Apply char styles to STRING.
1657 The variable `org-lparse-char-styles' controls how the Org
1658 emphasis markers are interpreted."
1660 (while (string-match org-emph-re string s
)
1662 (substring string
(match-beginning 3) (1+ (match-beginning 3)))
1663 (substring string
(match-beginning 4) (1+ (match-beginning 4)))))
1664 (setq s
(match-beginning 0)
1667 (match-string 1 string
)
1669 'FONTIFY
(match-string 4 string
)
1670 (nth 1 (assoc (match-string 3 string
)
1671 org-lparse-char-styles
)))
1672 (match-string 5 string
))
1673 string
(replace-match rpl t t string
)
1674 s
(+ s
(- (length rpl
) 2)))
1678 (defun org-lparse-export-list-line (line pos struct prevs
)
1679 "Insert list syntax in export buffer. Return LINE, maybe modified.
1681 POS is the item position or line position the line had before
1682 modifications to buffer. STRUCT is the list structure. PREVS is
1683 the alist of previous items."
1686 ;; Translate type of list containing POS to "d", "o" or
1688 (lambda (pos struct prevs
)
1689 (let ((type (org-list-get-list-type pos struct prevs
)))
1691 ((eq 'ordered type
) "o")
1692 ((eq 'descriptive type
) "d")
1696 ;; Return list of all items and sublists ending at POS, in
1702 (let ((end (nth 6 e
))
1705 ((= end pos
) (push item out
))
1706 ((>= item pos
) (throw 'exit nil
)))))
1709 ;; First close any previous item, or list, ending at POS.
1711 (let* ((lastp (= (org-list-get-last-item e struct prevs
) e
))
1712 (first-item (org-list-get-list-begin e struct prevs
))
1713 (type (funcall get-type first-item struct prevs
)))
1714 (org-lparse-end-paragraph)
1715 ;; Ending for every item
1716 (org-lparse-end-list-item-1 type
)
1717 ;; We're ending last item of the list: end list.
1719 (org-lparse-end-list type
)
1720 (org-lparse-begin-paragraph))))
1721 (funcall get-closings pos
))
1723 ;; At an item: insert appropriate tags in export buffer.
1726 (concat "[ \t]*\\(\\S-+[ \t]*\\)"
1727 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
1728 "\\(?:\\(\\[[ X-]\\]\\)[ \t]+\\)?"
1729 "\\(?:\\(.*\\)[ \t]+::\\(?:[ \t]+\\|$\\)\\)?"
1731 (let* ((checkbox (match-string 3 line
))
1732 (desc-tag (or (match-string 4 line
) "???"))
1733 (body (or (match-string 5 line
) ""))
1734 (list-beg (org-list-get-list-begin pos struct prevs
))
1735 (firstp (= list-beg pos
))
1736 ;; Always refer to first item to determine list type, in
1737 ;; case list is ill-formed.
1738 (type (funcall get-type list-beg struct prevs
))
1739 (counter (let ((count-tmp (org-list-get-counter pos struct
)))
1741 ((not count-tmp
) nil
)
1742 ((string-match "[A-Za-z]" count-tmp
)
1743 (- (string-to-char (upcase count-tmp
)) 64))
1744 ((string-match "[0-9]+" count-tmp
)
1747 (org-lparse-end-paragraph)
1748 (org-lparse-begin-list type
))
1750 (let ((arg (cond ((equal type
"d") desc-tag
)
1751 ((equal type
"o") counter
))))
1752 (org-lparse-begin-list-item type arg
))
1754 ;; If line had a checkbox, some additional modification is required.
1762 ((string-match "X" checkbox
) "X")
1763 ((string-match " " checkbox
)
1764 (org-lparse-format 'SPACES
1))
1770 ;; Return modified line
1772 ;; At a list ender: go to next line (side-effects only).
1773 ((equal "ORG-LIST-END-MARKER" line
) (throw 'nextline nil
))
1774 ;; Not at an item: return line unchanged (side-effects only).
1777 (defun org-lparse-bind-local-variables (opt-plist)
1779 (set (make-local-variable (nth 2 x
))
1780 (plist-get opt-plist
(car x
))))
1781 org-export-plist-vars
))
1783 (defvar org-lparse-table-rowgrp-open
)
1784 (defvar org-lparse-table-cur-rowgrp-is-hdr
)
1785 (defvar org-lparse-footnote-number
)
1786 (defvar org-lparse-footnote-definitions
)
1787 (defvar org-lparse-output-buffer nil
1788 "Buffer to which `org-do-lparse' writes to.
1789 This buffer contains the contents of the to-be-created exported
1792 (defcustom org-lparse-debug nil
1793 "Enable or Disable logging of `org-lparse' callbacks.
1794 The parameters passed to the backend-registered ENTITY-CONTROL
1795 and ENTITY-FORMAT callbacks are logged as comment strings in the
1796 exported buffer. (org-lparse-format 'COMMENT fmt args) is used
1797 for logging. Customize this variable only if you are an expert
1798 user. Valid values of this variable are:
1799 nil : Disable logging
1800 control : Log all invocations of `org-lparse-begin' and
1801 `org-lparse-end' callbacks.
1802 format : Log invocations of `org-lparse-format' callbacks.
1803 t : Log all invocations of `org-lparse-begin', `org-lparse-end'
1804 and `org-lparse-format' callbacks,"
1807 (const :tag
"Disable" nil
)
1808 (const :tag
"Format callbacks" format
)
1809 (const :tag
"Control callbacks" control
)
1810 (const :tag
"Format and Control callbacks" t
)))
1812 (defun org-lparse-begin (entity &rest args
)
1813 "Begin ENTITY in current buffer. ARGS is entity specific.
1814 ENTITY can be one of PARAGRAPH, LIST, LIST-ITEM etc.
1816 Use (org-lparse-begin 'LIST \"o\") to begin a list in current
1819 See `org-xhtml-entity-control-callbacks-alist' for more
1821 (when (and (member org-lparse-debug
'(t control
))
1822 (not (eq entity
'DOCUMENT-CONTENT
)))
1823 (insert (org-lparse-format 'COMMENT
"%s BEGIN %S" entity args
)))
1825 (let ((f (cadr (assoc entity org-lparse-entity-control-callbacks-alist
))))
1826 (unless f
(error "Unknown entity: %s" entity
))
1829 (defun org-lparse-end (entity &rest args
)
1830 "Close ENTITY in current buffer. ARGS is entity specific.
1831 ENTITY can be one of PARAGRAPH, LIST, LIST-ITEM
1834 Use (org-lparse-end 'LIST \"o\") to close a list in current
1837 See `org-xhtml-entity-control-callbacks-alist' for more
1839 (when (and (member org-lparse-debug
'(t control
))
1840 (not (eq entity
'DOCUMENT-CONTENT
)))
1841 (insert (org-lparse-format 'COMMENT
"%s END %S" entity args
)))
1843 (let ((f (caddr (assoc entity org-lparse-entity-control-callbacks-alist
))))
1844 (unless f
(error "Unknown entity: %s" entity
))
1847 (defun org-lparse-begin-paragraph (&optional style
)
1848 "Insert <p>, but first close previous paragraph if any."
1849 (org-lparse-end-paragraph)
1850 (org-lparse-begin 'PARAGRAPH style
)
1851 (setq org-lparse-par-open t
))
1853 (defun org-lparse-end-paragraph ()
1854 "Close paragraph if there is one open."
1855 (when org-lparse-par-open
1856 (org-lparse-end 'PARAGRAPH
)
1857 (setq org-lparse-par-open nil
)))
1859 (defun org-lparse-end-list-item-1 (&optional type
)
1860 "Close <li> if necessary."
1861 (org-lparse-end-paragraph)
1862 (org-lparse-end-list-item (or type
"u")))
1864 (defcustom org-lparse-list-table-enable nil
1865 "Specify whether a list be exported as a table.
1866 When this option is enabled, lists that are enclosed in
1867 \"#+begin_list-table...#+end_list-table\" are exported as
1868 tables. Otherwise they are exported normally."
1872 (defun org-lparse-preprocess-after-blockquote-hook ()
1873 "Treat #+begin_list-table...#+end_list-table blocks specially.
1874 When `org-lparse-list-table-enable' is non-nil, enclose these
1875 blocks within ORG-LIST-TABLE-START...ORG-LIST-TABLE-END."
1876 (when org-lparse-list-table-enable
1877 (goto-char (point-min))
1878 (while (re-search-forward "^[ \t]*#\\+\\(begin\\|end\\)_\\(.*\\)$" nil t
)
1879 (when (string= (downcase (match-string 2)) "list-table")
1880 (replace-match (if (equal (downcase (match-string 1)) "begin")
1881 "ORG-LIST-TABLE-START"
1882 "ORG-LIST-TABLE-END") t t
)))))
1884 (add-hook 'org-export-preprocess-after-blockquote-hook
1885 'org-lparse-preprocess-after-blockquote-hook
)
1887 (defvar org-lparse-list-table-p nil
1888 "Non-nil if `org-do-lparse' is within a list-table.
1889 See `org-lparse-list-table-enable'.")
1891 (defvar org-lparse-dyn-current-environment nil
)
1892 (defun org-lparse-begin-environment (style)
1895 (setq org-lparse-list-table-p org-lparse-list-table-enable
))
1897 (setq org-lparse-dyn-current-environment style
)
1898 (org-lparse-begin 'ENVIRONMENT style
))))
1900 (defun org-lparse-end-environment (style)
1903 (setq org-lparse-list-table-p nil
))
1905 (org-lparse-end 'ENVIRONMENT style
)
1906 (setq org-lparse-dyn-current-environment nil
))))
1908 (defun org-lparse-current-environment-p (style)
1909 (eq org-lparse-dyn-current-environment style
))
1911 (defun org-lparse-begin-footnote-definition (n)
1912 (org-lparse-begin-collect)
1913 (setq org-lparse-insert-tag-with-newlines nil
)
1914 (org-lparse-begin 'FOOTNOTE-DEFINITION n
))
1916 (defun org-lparse-end-footnote-definition (n)
1917 (org-lparse-end 'FOOTNOTE-DEFINITION n
)
1918 (setq org-lparse-insert-tag-with-newlines
'both
)
1919 (let ((footnote-def (org-lparse-end-collect)))
1920 (push (cons n footnote-def
) org-lparse-footnote-definitions
)))
1922 (defvar org-lparse-collect-buffer nil
1923 "An auxiliary buffer named \"*Org Lparse Collect*\".
1924 `org-do-lparse' uses this as output buffer while collecting
1925 footnote definitions and table-cell contents of list-tables. See
1926 `org-lparse-begin-collect' and `org-lparse-end-collect'.")
1928 (defvar org-lparse-collect-count nil
1929 "Count number of calls to `org-lparse-begin-collect'.
1930 Use this counter to catch chained collections if they ever
1933 (defun org-lparse-begin-collect ()
1934 "Temporarily switch to `org-lparse-collect-buffer'.
1935 Also erase it's contents."
1936 (unless (zerop org-lparse-collect-count
)
1937 (error "FIXME (org-lparse.el): Encountered chained collections"))
1938 (incf org-lparse-collect-count
)
1939 (unless org-lparse-collect-buffer
1940 (setq org-lparse-collect-buffer
1941 (get-buffer-create "*Org Lparse Collect*")))
1942 (set-buffer org-lparse-collect-buffer
)
1945 (defun org-lparse-end-collect ()
1946 "Switch to `org-lparse-output-buffer'.
1947 Return contents of `org-lparse-collect-buffer' as a `string'."
1948 (assert (> org-lparse-collect-count
0))
1949 (decf org-lparse-collect-count
)
1950 (prog1 (buffer-string)
1952 (set-buffer org-lparse-output-buffer
)))
1954 (defun org-lparse-format (entity &rest args
)
1955 "Format ENTITY in backend-specific way and return it.
1956 ARGS is specific to entity being formatted.
1958 Use (org-lparse-format 'HEADING \"text\" 1) to format text as
1961 See `org-xhtml-entity-format-callbacks-alist' for more information."
1962 (when (and (member org-lparse-debug
'(t format
))
1963 (not (equal entity
'COMMENT
)))
1964 (insert (org-lparse-format 'COMMENT
"%s: %S" entity args
)))
1967 (let ((text (pop args
)))
1968 (apply 'org-lparse-format
'TAGS entity text args
)))
1970 (let ((f (cdr (assoc entity org-lparse-entity-format-callbacks-alist
))))
1971 (unless f
(error "Unknown entity: %s" entity
))
1974 (defun org-lparse-insert (entity &rest args
)
1975 (insert (apply 'org-lparse-format entity args
)))
1977 (defun org-lparse-prepare-toc (lines level-offset opt-plist umax-toc
)
1978 (let* ((quote-re0 (concat "^[ \t]*" org-quote-string
"\\>"))
1979 (org-min-level (org-get-min-level lines level-offset
))
1980 (org-last-level org-min-level
)
1983 (org-lparse-bind-local-variables opt-plist
)
1985 (org-lparse-begin 'TOC
(nth 3 (plist-get opt-plist
:lang-words
)))
1990 (when (and (string-match org-todo-line-regexp line
)
1991 (not (get-text-property 0 'org-protected line
))
1992 (<= (setq level
(org-tr-level
1993 (- (match-end 1) (match-beginning 1)
1996 (let ((txt (save-match-data
1997 (org-xml-encode-org-text-skip-links
1998 (org-export-cleanup-toc-line
1999 (match-string 3 line
)))))
2001 org-export-mark-todo-in-toc
2002 (or (and (match-beginning 2)
2003 (not (member (match-string 2 line
)
2004 org-done-keywords
)))
2005 (and (= level umax-toc
)
2006 (org-search-todo-below
2007 line lines level
)))))
2009 ;; Check for targets
2010 (while (string-match org-any-target-regexp line
)
2013 (let ((org-lparse-encode-pending t
))
2014 (org-lparse-format 'FONTIFY
2015 (match-string 1 line
) "target"))
2018 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt
)
2019 (setq tags
(match-string 1 txt
)
2020 txt
(replace-match "" t nil txt
)))
2021 (when (string-match quote-re0 txt
)
2022 (setq txt
(replace-match "" t t txt
)))
2023 (while (string-match "<\\(<\\)+\\|>\\(>\\)+" txt
)
2024 (setq txt
(replace-match "" t t txt
)))
2027 (let* ((snumber (org-section-number level
))
2028 (href (replace-regexp-in-string
2029 "\\." "-" (format "sec-%s" snumber
)))
2033 href org-export-preferred-target-alist
))
2035 (href (org-solidify-link-text href
)))
2036 (org-lparse-format 'TOC-ENTRY snumber todo txt tags href
))
2037 level org-last-level
)
2038 (setq org-last-level level
)))
2041 (org-lparse-end 'TOC
)
2042 (setq org-lparse-toc
(buffer-string))))
2045 (defun org-lparse-format-table-row (fields &optional text-for-empty-fields
)
2046 (if org-lparse-table-ncols
2047 ;; second and subsequent rows of the table
2048 (when (and org-lparse-list-table-p
2049 (> (length fields
) org-lparse-table-ncols
))
2050 (error "Table row has %d columns but header row claims %d columns"
2051 (length fields
) org-lparse-table-ncols
))
2052 ;; first row of the table
2053 (setq org-lparse-table-ncols
(length fields
))
2054 (when org-lparse-table-is-styled
2055 (setq org-lparse-table-num-numeric-items-per-column
2056 (make-vector org-lparse-table-ncols
0))
2057 (setq org-lparse-table-colalign-vector
2058 (make-vector org-lparse-table-ncols nil
))
2060 (while (< (incf c
) org-lparse-table-ncols
)
2061 (let* ((col-cookie (cdr (assoc (1+ c
) org-lparse-table-colalign-info
)))
2062 (align (nth 0 col-cookie
)))
2063 (setf (aref org-lparse-table-colalign-vector c
)
2065 ((string= align
"l") "left")
2066 ((string= align
"r") "right")
2067 ((string= align
"c") "center")
2069 (incf org-lparse-table-rownum
)
2075 (when (and (string= x
"") text-for-empty-fields
)
2076 (setq x text-for-empty-fields
))
2078 (let (col-cookie horiz-span
)
2079 (when org-lparse-table-is-styled
2080 (when (and (< i org-lparse-table-ncols
)
2081 (string-match org-table-number-regexp x
))
2082 (incf (aref org-lparse-table-num-numeric-items-per-column i
)))
2083 (setq col-cookie
(cdr (assoc (1+ i
) org-lparse-table-colalign-info
))
2084 horiz-span
(nth 1 col-cookie
)))
2086 'TABLE-CELL x org-lparse-table-rownum i
(or horiz-span
0))))
2089 (defun org-lparse-get (what &optional opt-plist
)
2090 "Query for value of WHAT for the current backend `org-lparse-backend'.
2091 See also `org-lparse-backend-get'."
2092 (if (boundp 'org-lparse-backend
)
2093 (org-lparse-backend-get (symbol-name org-lparse-backend
) what opt-plist
)
2094 (error "org-lparse-backend is not bound yet")))
2096 (defun org-lparse-backend-get (backend what
&optional opt-plist
)
2097 "Query BACKEND for value of WHAT.
2098 Dispatch the call to `org-<backend>-user-get'. If that throws an
2099 error, dispatch the call to `org-<backend>-get'. See
2100 `org-xhtml-get' for all known settings queried for by
2101 `org-lparse' during the course of export."
2102 (assert (stringp backend
) t
)
2103 (unless (org-lparse-backend-is-native-p backend
)
2104 (error "Unknown native backend %s" backend
))
2105 (let ((backend-get-method (intern (format "org-%s-get" backend
)))
2106 (backend-user-get-method (intern (format "org-%s-user-get" backend
))))
2108 ((functionp backend-get-method
)
2110 (funcall backend-user-get-method what opt-plist
)
2111 (error (funcall backend-get-method what opt-plist
))))
2113 (error "Native backend %s doesn't define %s" backend backend-get-method
)))))
2115 (defun org-lparse-insert-tag (tag &rest args
)
2116 (when (member org-lparse-insert-tag-with-newlines
'(lead both
))
2118 (insert (apply 'format tag args
))
2119 (when (member org-lparse-insert-tag-with-newlines
'(trail both
))
2122 (defun org-lparse-get-targets-from-title (title)
2123 (let* ((target (org-get-text-property-any 0 'target title
))
2124 (extra-targets (assoc target org-export-target-aliases
))
2125 (target (or (cdr (assoc target org-export-preferred-target-alist
))
2127 (cons target
(remove target extra-targets
))))
2129 (defun org-lparse-suffix-from-snumber (snumber)
2130 (let* ((snu (replace-regexp-in-string "\\." "-" snumber
))
2131 (href (cdr (assoc (concat "sec-" snu
)
2132 org-export-preferred-target-alist
))))
2133 (org-solidify-link-text (or href snu
))))
2135 (defun org-lparse-begin-level (level title umax head-count
)
2136 "Insert a new LEVEL in HTML export.
2137 When TITLE is nil, just close all open levels."
2138 (org-lparse-end-level level umax
)
2139 (unless title
(error "Why is heading nil"))
2140 (let* ((targets (org-lparse-get-targets-from-title title
))
2141 (target (car targets
)) (extra-targets (cdr targets
))
2142 (target (and target
(org-solidify-link-text target
)))
2143 (extra-class (org-get-text-property-any 0 'html-container-class title
))
2144 snumber tags level1 class
)
2145 (when (string-match (org-re "\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") title
)
2146 (setq tags
(and org-export-with-tags
(match-string 1 title
)))
2147 (setq title
(replace-match "" t t title
)))
2150 (if (aref org-levels-open
(1- level
))
2151 (org-lparse-end-list-item-1)
2152 (aset org-levels-open
(1- level
) t
)
2153 (org-lparse-end-paragraph)
2154 (org-lparse-begin-list 'unordered
))
2155 (org-lparse-begin-list-item
2156 'unordered target
(org-lparse-format
2157 'HEADLINE title extra-targets tags
)))
2158 (aset org-levels-open
(1- level
) t
)
2159 (setq snumber
(org-section-number level
))
2160 (setq level1
(+ level
(or (org-lparse-get 'TOPLEVEL-HLEVEL
) 1) -
1))
2161 (unless (= head-count
1)
2162 (org-lparse-end-outline-text-or-outline))
2163 (org-lparse-begin-outline-and-outline-text
2164 level1 snumber title tags target extra-targets extra-class
)
2165 (org-lparse-begin-paragraph))))
2167 (defun org-lparse-end-level (level umax
)
2168 (org-lparse-end-paragraph)
2169 (loop for l from org-level-max downto level
2170 do
(when (aref org-levels-open
(1- l
))
2171 ;; Terminate one level in HTML export
2173 (org-lparse-end-outline-text-or-outline)
2174 (org-lparse-end-list-item-1)
2175 (org-lparse-end-list 'unordered
))
2176 (aset org-levels-open
(1- l
) nil
))))
2178 (defvar org-lparse-outline-text-open
)
2179 (defun org-lparse-begin-outline-and-outline-text (level1 snumber title tags
2180 target extra-targets
2183 'OUTLINE level1 snumber title tags target extra-targets extra-class
)
2184 (org-lparse-begin-outline-text level1 snumber extra-class
))
2186 (defun org-lparse-end-outline-text-or-outline ()
2188 (org-lparse-outline-text-open
2189 (org-lparse-end 'OUTLINE-TEXT
)
2190 (setq org-lparse-outline-text-open nil
))
2191 (t (org-lparse-end 'OUTLINE
))))
2193 (defun org-lparse-begin-outline-text (level1 snumber extra-class
)
2194 (assert (not org-lparse-outline-text-open
) t
)
2195 (setq org-lparse-outline-text-open t
)
2196 (org-lparse-begin 'OUTLINE-TEXT level1 snumber extra-class
))
2198 (defun org-lparse-html-list-type-to-canonical-list-type (ltype)
2199 (cdr (assoc ltype
'(("o" . ordered
)
2201 ("d" . description
)))))
2203 ;; following vars are bound during `org-do-lparse'
2204 (defvar org-lparse-list-level
)
2205 (defvar org-lparse-list-item-count
)
2206 (defvar org-lparse-list-stack
)
2207 (defvar org-lparse-list-table
:table-row
)
2208 (defvar org-lparse-list-table
:lines
)
2210 ;; Notes on LIST-TABLES
2211 ;; ====================
2212 ;; When `org-lparse-list-table-enable' is non-nil, the following list
2214 ;; #+begin_list-table
2225 ;; will be exported as though it were a table as shown below.
2227 ;; | Row 1 | 1.1 | 1.2 | 1.3 |
2228 ;; | Row 2 | 2.1 | 2.2 | 2.3 |
2230 ;; Note that org-tables are NOT multi-line and each line is mapped to
2231 ;; a unique row in the exported document. So if an exported table
2232 ;; needs to contain a single paragraph (with copious text) it needs to
2233 ;; be typed up in a single line. Editing such long lines using the
2234 ;; table editor will be a cumbersome task. Furthermore inclusion of
2235 ;; multi-paragraph text in a table cell is well-nigh impossible.
2237 ;; LIST-TABLEs are meant to circumvent the above problems with
2240 ;; Note that in the example above the list items could be paragraphs
2241 ;; themselves and the list can be arbitrarily deep.
2243 ;; Inspired by following thread:
2244 ;; https://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01101.html
2246 (defun org-lparse-begin-list (ltype)
2247 (incf org-lparse-list-level
)
2248 (push org-lparse-list-item-count org-lparse-list-stack
)
2249 (setq org-lparse-list-item-count
0)
2251 ((not org-lparse-list-table-p
)
2252 (org-lparse-begin 'LIST ltype
))
2253 ;; process LIST-TABLE
2254 ((= 1 org-lparse-list-level
)
2256 (setq org-lparse-list-table
:lines nil
)
2257 (setq org-lparse-list-table
:table-row nil
))
2258 ((= 2 org-lparse-list-level
)
2261 (org-lparse-begin 'LIST ltype
))))
2263 (defun org-lparse-end-list (ltype)
2264 (setq org-lparse-list-item-count
(pop org-lparse-list-stack
))
2265 (decf org-lparse-list-level
)
2267 ((not org-lparse-list-table-p
)
2268 (org-lparse-end 'LIST ltype
))
2269 ;; process LIST-TABLE
2270 ((= 0 org-lparse-list-level
)
2272 (insert (org-lparse-format-list-table
2273 (nreverse org-lparse-list-table
:lines
))))
2274 ((= 1 org-lparse-list-level
)
2277 (org-lparse-end 'LIST ltype
))))
2279 (defun org-lparse-begin-list-item (ltype &optional arg headline
)
2280 (incf org-lparse-list-item-count
)
2282 ((not org-lparse-list-table-p
)
2283 (org-lparse-begin 'LIST-ITEM ltype arg headline
))
2284 ;; process LIST-TABLE
2285 ((and (= 1 org-lparse-list-level
)
2286 (= 1 org-lparse-list-item-count
))
2287 ;; begin TABLE-ROW for LIST-TABLE
2288 (setq org-lparse-list-table
:table-row nil
)
2289 (org-lparse-begin-list-table:table-cell
))
2290 ((and (= 2 org-lparse-list-level
)
2291 (= 1 org-lparse-list-item-count
))
2292 ;; begin TABLE-CELL for LIST-TABLE
2293 (org-lparse-begin-list-table:table-cell
))
2295 (org-lparse-begin 'LIST-ITEM ltype arg headline
))))
2297 (defun org-lparse-end-list-item (ltype)
2298 (decf org-lparse-list-item-count
)
2300 ((not org-lparse-list-table-p
)
2301 (org-lparse-end 'LIST-ITEM ltype
))
2302 ;; process LIST-TABLE
2303 ((and (= 1 org-lparse-list-level
)
2304 (= 0 org-lparse-list-item-count
))
2305 ;; end TABLE-ROW for LIST-TABLE
2306 (org-lparse-end-list-table:table-cell
)
2307 (push (nreverse org-lparse-list-table
:table-row
)
2308 org-lparse-list-table
:lines
))
2309 ((= 2 org-lparse-list-level
)
2310 ;; end TABLE-CELL for LIST-TABLE
2311 (org-lparse-end-list-table:table-cell
))
2313 (org-lparse-end 'LIST-ITEM ltype
))))
2315 (defvar org-lparse-list-table
:table-cell-open
)
2316 (defun org-lparse-begin-list-table:table-cell
()
2317 (org-lparse-end-list-table:table-cell
)
2318 (setq org-lparse-list-table
:table-cell-open t
)
2319 (org-lparse-begin-collect)
2320 (org-lparse-begin-paragraph))
2322 (defun org-lparse-end-list-table:table-cell
()
2323 (when org-lparse-list-table
:table-cell-open
2324 (setq org-lparse-list-table
:table-cell-open nil
)
2325 (org-lparse-end-paragraph)
2326 (push (org-lparse-end-collect)
2327 org-lparse-list-table
:table-row
)))
2329 (defvar org-lparse-table-rowgrp-info
)
2330 (defun org-lparse-begin-table-rowgroup (&optional is-header-row
)
2331 (push (cons (1+ org-lparse-table-rownum
) :start
) org-lparse-table-rowgrp-info
)
2332 (org-lparse-begin 'TABLE-ROWGROUP is-header-row
))
2334 (defun org-lparse-end-table ()
2335 (when org-lparse-table-is-styled
2337 (unless (car org-table-colgroup-info
)
2338 (setq org-table-colgroup-info
2339 (cons :start
(cdr org-table-colgroup-info
))))
2346 (setf (aref org-lparse-table-colalign-vector c
)
2347 (or (aref org-lparse-table-colalign-vector c
)
2348 (if (> (/ (float x
) (1+ org-lparse-table-rownum
))
2349 org-table-number-fraction
)
2351 org-lparse-table-num-numeric-items-per-column
)))
2352 (org-lparse-end 'TABLE
))
2354 (defvar org-lparse-encode-pending nil
)
2356 (defun org-lparse-format-tags (tag text prefix suffix
&rest args
)
2359 (concat prefix
(apply 'format
(car tag
) args
) text suffix
2360 (format (cdr tag
))))
2361 ((stringp tag
) ; singleton tag
2362 (concat prefix
(apply 'format tag args
) text
))))
2364 (defun org-xml-fix-class-name (kwd) ; audit callers of this function
2365 "Turn todo keyword into a valid class name.
2366 Replaces invalid characters with \"_\"."
2368 (while (string-match "[^a-zA-Z0-9_]" kwd
)
2369 (setq kwd
(replace-match "_" t t kwd
))))
2372 (defun org-lparse-format-todo (todo)
2373 (org-lparse-format 'FONTIFY
2375 (ignore-errors (org-lparse-get 'TODO-KWD-CLASS-PREFIX
))
2376 (org-xml-fix-class-name todo
))
2377 (list (if (member todo org-done-keywords
) "done" "todo")
2380 (defun org-lparse-format-extra-targets (extra-targets)
2381 (if (not extra-targets
) ""
2382 (mapconcat (lambda (x)
2383 (setq x
(org-solidify-link-text
2384 (if (org-uuidgen-p x
) (concat "ID-" x
) x
)))
2385 (org-lparse-format 'ANCHOR
"" x
))
2388 (defun org-lparse-format-org-tags (tags)
2396 (ignore-errors (org-lparse-get 'TAG-CLASS-PREFIX
))
2397 (org-xml-fix-class-name x
))))
2398 (org-split-string tags
":")
2399 (org-lparse-format 'SPACES
1)) "tag")))
2401 (defun org-lparse-format-section-number (&optional snumber level
)
2402 (and org-export-with-section-numbers
2403 (not org-lparse-body-only
) snumber level
2404 (org-lparse-format 'FONTIFY snumber
(format "section-number-%d" level
))))
2406 (defun org-lparse-warn (msg)
2407 (if (not org-lparse-use-flashy-warning
)
2409 (put-text-property 0 (length msg
) 'face
'font-lock-warning-face msg
)
2413 (defun org-xml-format-href (s)
2414 "Make sure the S is valid as a href reference in an XHTML document."
2417 (while (string-match "&" s start
)
2418 (setq start
(+ (match-beginning 0) 3)
2419 s
(replace-match "&" t t s
)))))
2422 (defun org-xml-format-desc (s)
2423 "Make sure the S is valid as a description in a link."
2424 (if (and s
(not (get-text-property 1 'org-protected s
)))
2426 (org-xml-encode-org-text s
))
2429 (provide 'org-lparse
)
2431 ;;; org-lparse.el ends here