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 this
117 command to convert it."
118 (interactive "Mbackend: \nr")
119 (let (reg backend-string buf pop-up-frames
)
120 (save-window-excursion
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 (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
)
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")
259 'ORG-LINK opt-plist type
"" (format "coderef-%s" path
)
261 (org-export-get-coderef-format
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
270 (funcall fnc
(org-link-unescape path
) desc1
'html
))))
272 ((string= type
"file")
278 (string-match "::\\(.*\\)" path
)
280 (replace-match "" t nil path
)
281 (match-string 1 path
))
284 ;;The proper path, without a fragment
292 ;;Check the fragment. If it can't be used as
293 ;;target fragment we'll pass nil instead.
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
))
304 ;;Description minus "file:" and ".org"
305 (if (string-match "^file:" desc
)
307 ((desc-1 (replace-match "" t t desc
)))
308 (if (string-match "\\.org$" desc-1
)
309 (replace-match "" t t desc-1
)
316 (functionp link-validate
)
317 (not (funcall link-validate path-1 current-dir
)))
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><" type
":"
326 (save-match-data (org-link-unescape path
))
328 (setq line
(replace-match rpl t t line
)
329 start
(+ start
(length rpl
))))
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)
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
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'."
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
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
380 (when (org-lparse-get-converter in-fmt
)
381 (or (ignore-errors (org-lparse-backend-get in-fmt
'OTHER-BACKENDS
) )
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."
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
))))
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 outline
422 is first exported to NATIVE-BACKEND and optionally converted to
423 TARGET-BACKEND. See `org-lparse-native-backends' for list of
424 known native backends. Each native backend can specify a
425 converter and list of target backends it exports to using the
426 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."
451 (let* ((input (if (featurep 'ido
) 'ido-completing-read
'completing-read
))
452 (all-backends (org-lparse-all-backends))
455 (funcall input
"Export to: " all-backends nil t nil
)))
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
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)\""))
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:
493 %i input file name in full
494 %I input file name as a URL
495 %f format of the output file
496 %o output file name in full
497 %O output file name as a URL
498 %d output dir in full
499 %D output dir as a URL."
503 (const :tag
"None" nil
)
504 (alist :tag
"Converters"
505 :key-type
(string :tag
"Converter Name")
506 :value-type
(group (cons (string :tag
"Executable")
507 (repeat (string :tag
"Command line args")))))))
508 (defcustom org-lparse-convert-process nil
509 "Command to convert from an Org exported format to other formats.
510 During customization, the list of choices are populated from
511 `org-lparse-convert-processes'. Refer afore-mentioned variable
512 for further information."
514 :type
'(choice :convert-widget
516 (apply 'widget-convert
(widget-type w
)
517 (eval (car (widget-get w
:args
)))))
518 `((const :tag
"None" nil
)
519 ,@(mapcar (lambda (c)
520 `(const :tag
,(car c
) ,(cadr c
)))
521 org-lparse-convert-processes
))))
523 (defcustom org-lparse-convert-capabilities
525 ("odt" "ott" "doc" "rtf")
526 (("pdf" "pdf") ("odt" "odt") ("xhtml" "html") ("rtf" "rtf")
527 ("ott" "ott") ("doc" "doc") ("ooxml" "xml") ("html" "html")))
529 ("html" "xhtml") (("pdf" "pdf") ("odt" "txt") ("html" "html")))
531 ("ods" "ots" "xls" "csv")
532 (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv")
533 ("ods" "ods") ("xls" "xls") ("xhtml" "xhtml") ("ooxml" "xml")))
536 (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("xhtml" "xml")
537 ("otp" "otp") ("ppt" "ppt") ("odg" "odg") ("html" "html"))))
538 "Specify input and output formats of `org-lparse-convert-process'.
539 More correctly, specify the set of input and output formats that
540 the user is actually interested in.
542 This variable is an alist where each element is of the
543 form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
544 INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
545 alist where each element is of the form (OUTPUT-FMT
546 OUTPUT-FILE-EXTENSION).
548 The variable is interpreted as follows:
549 `org-lparse-convert-process' can take any document that is in
550 INPUT-FMT-LIST and produce any document that is in the
551 OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
552 OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
553 serves dual purposes:
554 - It is used for populating completion candidates during
555 `org-lparse' and `org-lparse-convert' commands.
557 - It is used as the value of \"%f\" specifier in
558 `org-lparse-convert-process'.
560 DOCUMENT-CLASS is used to group a set of file formats in
561 INPUT-FMT-LIST in to a single class.
563 Note that this variable inherently captures how LibreOffice based
564 converters work. LibreOffice maps documents of various formats
565 to classes like Text, Web, Spreadsheet, Presentation etc and
566 allow document of a given class (irrespective of it's source
567 format) to be converted to any of the export formats associated
570 See default setting of this variable for an typical
575 (const :tag
"None" nil
)
576 (alist :key-type
(string :tag
"Document Class")
578 (group (repeat :tag
"Input formats" (string :tag
"Input format"))
579 (alist :tag
"Output formats"
580 :key-type
(string :tag
"Output format")
582 (group (string :tag
"Output file extension")))))))
584 (defcustom org-lparse-use-flashy-warning nil
585 "Control flashing of messages logged with `org-lparse-warn'.
586 When non-nil, messages are fontified with warning face and the
587 exporter lingers for a while to catch user's attention."
591 (defun org-lparse-convert (&optional in-file out-fmt prefix-arg
)
592 "Convert IN-FILE to format OUT-FMT using a command line converter.
593 IN-FILE is the file to be converted. If unspecified, it defaults
594 to variable `buffer-file-name'. OUT-FMT is the desired output
595 format. If the backend has registered a CONVERT-METHOD as part
596 of it's get function then that converter is used. Otherwise
597 `org-lparse-convert-process' is used. If PREFIX-ARG is non-nil
598 then the newly converted file is opened using `org-open-file'."
600 (let* ((input (if (featurep 'ido
) 'ido-completing-read
'completing-read
))
601 (in-file (read-file-name "File to be converted: "
602 nil buffer-file-name t
))
603 (in-fmt (file-name-extension in-file
))
604 (out-fmt-choices (org-lparse-get-other-backends in-fmt
))
606 (or (and out-fmt-choices
607 (funcall input
"Output format: "
608 out-fmt-choices nil nil nil
))
610 "No known converter or no known output formats for %s files"
612 (list in-file out-fmt current-prefix-arg
)))
613 (require 'browse-url
)
614 (let* ((in-file (expand-file-name (or in-file buffer-file-name
)))
615 (dummy (or (file-readable-p in-file
)
616 (error "Cannot read %s" in-file
)))
617 (in-fmt (file-name-extension in-file
))
618 (out-fmt (or out-fmt
(error "Output format unspecified")))
619 (convert-process (org-lparse-get-converter in-fmt
))
620 (program (car convert-process
))
621 (dummy (and (or program
(error "Converter not configured"))
622 (or (executable-find program
)
623 (error "Cannot find converter %s" program
))))
625 (or (assoc out-fmt
(org-lparse-get-other-backends in-fmt
))
626 (error "Cannot convert from %s to %s format" in-fmt out-fmt
)))
627 (out-file (concat (file-name-sans-extension in-file
) "."
628 (nth 1 out-fmt-alist
)))
629 (out-dir (file-name-directory in-file
))
630 (arglist (mapcar (lambda (arg)
632 arg
`((?i .
,in-file
)
633 (?I .
,(browse-url-file-url in-file
))
636 (?O .
,(browse-url-file-url out-file
))
638 (?D .
,(browse-url-file-url out-dir
)))))
639 (cdr convert-process
))))
640 (when (file-exists-p out-file
)
641 (delete-file out-file
))
643 (message "Executing %s %s" program
(mapconcat 'identity arglist
" "))
644 (apply 'call-process program nil nil nil arglist
)
646 ((file-exists-p out-file
)
647 (message "Exported to %s using %s" out-file program
)
649 (message "Opening %s..." out-file
)
650 (org-open-file out-file
))
652 ;; (set-buffer (find-file-noselect out-file))
655 (message "Export to %s failed" out-file
)
658 (defvar org-lparse-insert-tag-with-newlines
'both
)
660 ;; Following variables are let-bound during `org-lparse'
661 (defvar org-lparse-dyn-first-heading-pos
)
662 (defvar org-lparse-toc
)
663 (defvar org-lparse-entity-control-callbacks-alist
)
664 (defvar org-lparse-entity-format-callbacks-alist
)
665 (defvar org-lparse-backend nil
666 "The native backend to which the document is currently exported.
667 This variable is let bound during `org-lparse'. Valid values are
668 one of the symbols corresponding to `org-lparse-native-backends'.
670 Compare this variable with `org-export-current-backend' which is
671 bound only during `org-export-preprocess-string' stage of the
674 See also `org-lparse-other-backend'.")
676 (defvar org-lparse-other-backend nil
677 "The target backend to which the document is currently exported.
678 This variable is let bound during `org-lparse'. This variable is
679 set to either `org-lparse-backend' or one of the symbols
680 corresponding to OTHER-BACKENDS specification of the
683 For example, if a document is exported to \"odt\" then both
684 org-lparse-backend and org-lparse-other-backend are bound to
685 'odt. On the other hand, if a document is exported to \"odt\"
686 and then converted to \"doc\" then org-lparse-backend is set to
687 'odt and org-lparse-other-backend is set to 'doc.")
689 (defvar org-lparse-body-only nil
690 "Bind this to BODY-ONLY arg of `org-lparse'.")
692 (defvar org-lparse-to-buffer nil
693 "Bind this to TO-BUFFER arg of `org-lparse'.")
695 (defun org-do-lparse (arg &optional hidden ext-plist
696 to-buffer body-only pub-dir
)
697 "Export the outline to various formats.
698 See `org-lparse' for more information. This function is a
699 html-agnostic version of the `org-export-as-html' function in 7.5
701 ;; Make sure we have a file name when we need it.
702 (when (and (not (or to-buffer body-only
))
703 (not buffer-file-name
))
704 (if (buffer-base-buffer)
705 (org-set-local 'buffer-file-name
706 (with-current-buffer (buffer-base-buffer)
708 (error "Need a file name to be able to export")))
711 (format "Exporting to %s using org-lparse..."
713 (or org-lparse-backend org-lparse-other-backend
)))))
715 (setq-default org-todo-line-regexp org-todo-line-regexp
)
716 (setq-default org-deadline-line-regexp org-deadline-line-regexp
)
717 (setq-default org-done-keywords org-done-keywords
)
718 (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp
)
719 (let* (org-lparse-encode-pending
721 (org-lparse-list-level 0) ; list level starts at 1. A
722 ; value of 0 implies we are
723 ; outside of any list
724 org-lparse-outline-text-open
725 (org-lparse-latex-fragment-fallback ; currently used only by
727 (or (ignore-errors (org-lparse-get 'LATEX-FRAGMENT-FALLBACK
))
728 (if (and (org-check-external-command "latex" "" t
)
729 (org-check-external-command "dvipng" "" t
))
732 (org-lparse-insert-tag-with-newlines 'both
)
733 (org-lparse-to-buffer to-buffer
)
734 (org-lparse-body-only body-only
)
735 (org-lparse-entity-control-callbacks-alist
736 (org-lparse-get 'ENTITY-CONTROL
))
737 (org-lparse-entity-format-callbacks-alist
738 (org-lparse-get 'ENTITY-FORMAT
))
740 (org-export-process-option-filters
741 (org-combine-plists (org-default-export-plist)
743 (org-infile-export-plist))))
744 (body-only (or body-only
(plist-get opt-plist
:body-only
)))
745 valid org-lparse-dyn-first-heading-pos
746 (odd org-odd-levels-only
)
747 (region-p (org-region-active-p))
748 (rbeg (and region-p
(region-beginning)))
749 (rend (and region-p
(region-end)))
751 (if (plist-get opt-plist
:ignore-subtree-p
)
756 (and (org-at-heading-p)
757 (>= (org-end-of-subtree t t
) rend
))))))
758 (level-offset (if subtree-p
761 (+ (funcall outline-level
)
762 (if org-odd-levels-only
1 0)))
764 (opt-plist (setq org-export-opt-plist
766 (org-export-add-subtree-options opt-plist rbeg
)
768 ;; The following two are dynamically scoped into other
770 (org-current-export-dir
771 (or pub-dir
(org-lparse-get 'EXPORT-DIR opt-plist
)))
772 (org-current-export-file buffer-file-name
)
773 (level 0) (line "") (origline "") txt todo
776 (filename (if to-buffer nil
779 (file-name-sans-extension
781 (org-entry-get (region-beginning)
782 "EXPORT_FILE_NAME" t
))
783 (file-name-nondirectory buffer-file-name
)))
784 "." (org-lparse-get 'FILE-NAME-EXTENSION opt-plist
))
785 (file-name-as-directory
786 (or pub-dir
(org-lparse-get 'EXPORT-DIR opt-plist
))))))
787 (current-dir (if buffer-file-name
788 (file-name-directory buffer-file-name
)
790 (buffer (if to-buffer
792 ((eq to-buffer
'string
)
793 (get-buffer-create (org-lparse-get 'EXPORT-BUFFER-NAME
)))
794 (t (get-buffer-create to-buffer
)))
796 (or (let ((f (org-lparse-get 'INIT-METHOD
)))
797 (and f
(functionp f
) (funcall f filename
)))
799 (org-levels-open (make-vector org-level-max nil
))
800 (date (plist-get opt-plist
:date
))
802 ((and date
(string-match "%" date
))
803 (format-time-string date
))
805 (t (format-time-string "%Y-%m-%d %T %Z"))))
806 (dummy (setq opt-plist
(plist-put opt-plist
:effective-date date
)))
807 (title (org-xml-encode-org-text-skip-links
808 (or (and subtree-p
(org-export-get-title-from-subtree))
809 (plist-get opt-plist
:title
)
812 (plist-get opt-plist
:skip-before-1st-heading
))
813 (org-export-grab-title-from-buffer))
814 (and buffer-file-name
815 (file-name-sans-extension
816 (file-name-nondirectory buffer-file-name
)))
818 (dummy (setq opt-plist
(plist-put opt-plist
:title title
)))
819 (html-table-tag (plist-get opt-plist
:html-table-tag
))
820 (quote-re0 (concat "^[ \t]*" org-quote-string
"\\>"))
821 (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string
"\\>\\)"))
822 (org-lparse-dyn-current-environment nil
)
823 ;; Get the language-dependent settings
824 (lang-words (or (assoc (plist-get opt-plist
:language
)
825 org-export-language-setup
)
826 (assoc "en" org-export-language-setup
)))
827 (dummy (setq opt-plist
(plist-put opt-plist
:lang-words lang-words
)))
830 (coding-system-for-write
831 (or (ignore-errors (org-lparse-get 'CODING-SYSTEM-FOR-WRITE
))
832 (and (boundp 'buffer-file-coding-system
)
833 buffer-file-coding-system
)))
834 (save-buffer-coding-system
835 (or (ignore-errors (org-lparse-get 'CODING-SYSTEM-FOR-SAVE
))
836 (and (boundp 'buffer-file-coding-system
)
837 buffer-file-coding-system
)))
840 (if region-p
(region-beginning) (point-min))
841 (if region-p
(region-end) (point-max))))
842 (org-export-have-math nil
)
843 (org-export-footnotes-seen nil
)
844 (org-export-footnotes-data (org-footnote-all-labels 'with-defs
))
845 (org-footnote-insert-pos-for-preprocessor 'point-min
)
848 (org-export-preprocess-string
851 :for-backend
(if (equal org-lparse-backend
'xhtml
) ; hack
854 :skip-before-1st-heading
855 (plist-get opt-plist
:skip-before-1st-heading
)
856 :drawers
(plist-get opt-plist
:drawers
)
857 :todo-keywords
(plist-get opt-plist
:todo-keywords
)
858 :tasks
(plist-get opt-plist
:tasks
)
859 :tags
(plist-get opt-plist
:tags
)
860 :priority
(plist-get opt-plist
:priority
)
861 :footnotes
(plist-get opt-plist
:footnotes
)
862 :timestamps
(plist-get opt-plist
:timestamps
)
864 (plist-get opt-plist
:archived-trees
)
865 :select-tags
(plist-get opt-plist
:select-tags
)
866 :exclude-tags
(plist-get opt-plist
:exclude-tags
)
868 (plist-get opt-plist
:text
)
870 (plist-get opt-plist
:LaTeX-fragments
))
873 table-buffer table-orig-buffer
875 rpl path attr desc descp desc1 desc2 link
877 footnotes footref-seen
878 org-lparse-output-buffer
879 org-lparse-footnote-definitions
880 org-lparse-footnote-number
881 org-lparse-footnote-buffer
886 (let ((inhibit-read-only t
))
888 (remove-text-properties (point-min) (point-max)
889 '(:org-license-to-kill t
))))
891 (message "Exporting...")
892 (org-init-section-numbers)
894 ;; Switch to the output buffer
895 (setq org-lparse-output-buffer buffer
)
896 (set-buffer org-lparse-output-buffer
)
897 (let ((inhibit-read-only t
)) (erase-buffer))
899 (org-install-letbind)
901 (and (fboundp 'set-buffer-file-coding-system
)
902 (set-buffer-file-coding-system coding-system-for-write
))
904 (let ((case-fold-search nil
)
905 (org-odd-levels-only odd
))
906 ;; create local variables for all options, to make sure all called
907 ;; functions get the correct information
909 (set (make-local-variable (nth 2 x
))
910 (plist-get opt-plist
(car x
))))
911 org-export-plist-vars
)
912 (setq umax
(if arg
(prefix-numeric-value arg
)
913 org-export-headline-levels
))
914 (setq umax-toc
(if (integerp org-export-with-toc
)
915 (min org-export-with-toc umax
)
918 (when (and org-export-with-toc
(not body-only
))
919 (setq lines
(org-lparse-prepare-toc
920 lines level-offset opt-plist umax-toc
)))
923 (org-lparse-begin 'DOCUMENT-CONTENT opt-plist
)
924 (org-lparse-begin 'DOCUMENT-BODY opt-plist
))
927 (org-init-section-numbers)
929 (org-lparse-begin-paragraph)
931 (while (setq line
(pop lines
) origline line
)
933 (when (and (org-lparse-current-environment-p 'quote
)
934 (string-match org-outline-regexp-bol line
))
935 (org-lparse-end-environment 'quote
))
937 (when (org-lparse-current-environment-p 'quote
)
938 (org-lparse-insert 'LINE line
)
939 (throw 'nextline nil
))
941 ;; Fixed-width, verbatim lines (examples)
942 (when (and org-export-with-fixed-width
943 (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line
))
944 (when (not (org-lparse-current-environment-p 'fixedwidth
))
945 (org-lparse-begin-environment 'fixedwidth
))
946 (org-lparse-insert 'LINE
(match-string 3 line
))
947 (when (or (not lines
)
948 (not (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)"
950 (org-lparse-end-environment 'fixedwidth
))
951 (throw 'nextline nil
))
953 ;; Notes: The baseline version of org-html.el (git commit
954 ;; 3d802e), while encoutering a *line-long* protected text,
955 ;; does one of the following two things based on the state
956 ;; of the export buffer.
958 ;; 1. If a paragraph element has just been opened and
959 ;; contains only whitespace as content, insert the
960 ;; protected text as part of the previous paragraph.
962 ;; 2. If the paragraph element has already been opened and
963 ;; contains some valid content insert the protected text
964 ;; as part of the current paragraph.
968 ;; Scenario 1 mentioned above kicks in when a block of
969 ;; protected text has to be inserted enbloc. For example,
970 ;; this happens, when inserting an source or example block
971 ;; or preformatted content enclosed in #+backend,
972 ;; #+begin_bakend ... #+end_backend)
974 ;; Scenario 2 mentioned above kicks in when the protected
975 ;; text is part of a running sentence. For example this
976 ;; happens in the case of an *multiline* LaTeX equation that
977 ;; needs to be inserted verbatim.
979 ;; org-html.el in the master branch seems to do some
980 ;; jugglery by moving paragraphs around. Inorder to make
981 ;; these changes backend-agnostic introduce a new text
982 ;; property org-native-text and impose the added semantics
983 ;; that these protected blocks appear outside of a
984 ;; conventional paragraph element.
986 ;; Extra Note: Check whether org-example and org-native-text
987 ;; are entirely equivalent.
989 ;; Fixes bug reported by Christian Moe concerning verbatim
991 ;; on git commit 533ba3f90250a1f25f494c390d639ea6274f235c
992 ;; http://repo.or.cz/w/org-mode/org-jambu.git/shortlog/refs/heads/staging
993 ;; See http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01379.html
996 (when (and (get-text-property 0 'org-native-text line
)
997 ;; Make sure it is the entire line that is protected
998 (not (< (or (next-single-property-change
999 0 'org-native-text line
) 10000)
1001 (let ((ind (get-text-property 0 'original-indentation line
)))
1002 (org-lparse-begin-environment 'native
)
1003 (org-lparse-insert 'LINE line
)
1005 (or (= (length (car lines
)) 0)
1007 (equal ind
(get-text-property
1008 0 'original-indentation
(car lines
))))
1009 (or (= (length (car lines
)) 0)
1010 (get-text-property 0 'org-native-text
(car lines
))))
1011 (org-lparse-insert 'LINE
(pop lines
)))
1012 (org-lparse-end-environment 'native
))
1013 (throw 'nextline nil
))
1016 (when (and (get-text-property 0 'org-protected line
)
1017 ;; Make sure it is the entire line that is protected
1018 (not (< (or (next-single-property-change
1019 0 'org-protected line
) 10000)
1021 (let ((ind (get-text-property 0 'original-indentation line
)))
1022 (org-lparse-insert 'LINE line
)
1024 (or (= (length (car lines
)) 0)
1026 (equal ind
(get-text-property
1027 0 'original-indentation
(car lines
))))
1028 (or (= (length (car lines
)) 0)
1029 (get-text-property 0 'org-protected
(car lines
))))
1030 (org-lparse-insert 'LINE
(pop lines
))))
1031 (throw 'nextline nil
))
1033 ;; Blockquotes, verse, and center
1034 (when (string-match "^ORG-\\(.+\\)-\\(START\\|END\\)$" line
)
1035 (let* ((style (intern (downcase (match-string 1 line
))))
1036 (f (cdr (assoc (match-string 2 line
)
1037 '(("START" . org-lparse-begin-environment
)
1038 ("END" . org-lparse-end-environment
))))))
1039 (when (memq style
'(blockquote verse center
))
1041 (throw 'nextline nil
))))
1043 (run-hooks 'org-export-html-after-blockquotes-hook
)
1044 (when (org-lparse-current-environment-p 'verse
)
1045 (let ((i (org-get-string-indentation line
)))
1048 (let ((org-lparse-encode-pending t
))
1049 (org-lparse-format 'SPACES
(* 2 i
)))
1050 " " (org-trim line
))))
1051 (unless (string-match "\\\\\\\\[ \t]*$" line
)
1052 (setq line
(concat line
"\\\\")))))
1054 ;; make targets to anchors
1056 (while (string-match
1057 "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line start
)
1059 ((get-text-property (match-beginning 1) 'org-protected line
)
1060 (setq start
(match-end 1)))
1062 (setq line
(replace-match
1063 (let ((org-lparse-encode-pending t
))
1065 'ANCHOR
"" (org-solidify-link-text
1066 (match-string 1 line
))))
1068 ((and org-export-with-toc
(equal (string-to-char line
) ?
*))
1069 ;; FIXME: NOT DEPENDENT on TOC?????????????????????
1070 (setq line
(replace-match
1071 (let ((org-lparse-encode-pending t
))
1073 'FONTIFY
(match-string 1 line
) "target"))
1074 ;; (concat "@<i>" (match-string 1 line) "@</i> ")
1077 (setq line
(replace-match
1079 (let ((org-lparse-encode-pending t
))
1081 'ANCHOR
(match-string 1 line
)
1082 (org-solidify-link-text (match-string 1 line
))
1086 (let ((org-lparse-encode-pending t
))
1087 (setq line
(org-lparse-handle-time-stamps line
)))
1089 ;; replace "&" by "&", "<" and ">" by "<" and ">"
1090 ;; handle @<..> HTML tags (replace "@>..<" by "<..>")
1091 ;; Also handle sub_superscripts and checkboxes
1092 (or (string-match org-table-hline-regexp line
)
1093 (string-match "^[ \t]*\\([+]-\\||[ ]\\)[-+ |]*[+|][ \t]*$" line
)
1094 (setq line
(org-xml-encode-org-text-skip-links line
)))
1096 (setq line
(org-lparse-format-org-link line opt-plist
))
1099 (if (and (string-match org-todo-line-regexp line
)
1100 (match-beginning 2))
1102 (substring line
0 (match-beginning 2))
1103 (org-lparse-format 'TODO
(match-string 2 line
))
1104 (substring line
(match-end 2)))))
1106 ;; Does this contain a reference to a footnote?
1107 (when org-export-with-footnotes
1109 (while (string-match "\\([^* \t].*?\\)[ \t]*\\[\\([0-9]+\\)\\]" line start
)
1110 ;; Discard protected matches not clearly identified as
1111 ;; footnote markers.
1112 (if (or (get-text-property (match-beginning 2) 'org-protected line
)
1113 (not (get-text-property (match-beginning 2) 'org-footnote line
)))
1114 (setq start
(match-end 2))
1115 (let ((n (match-string 2 line
)) refcnt a
)
1116 (if (setq a
(assoc n footref-seen
))
1118 (setcdr a
(1+ (cdr a
)))
1119 (setq refcnt
(cdr a
)))
1121 (push (cons n
1) footref-seen
))
1125 (or (match-string 1 line
) "")
1128 n
(cdr (assoc n org-lparse-footnote-definitions
))
1130 ;; If another footnote is following the
1131 ;; current one, add a separator.
1132 (if (save-match-data
1133 (string-match "\\`\\[[0-9]+\\]"
1134 (substring line
(match-end 0))))
1136 (org-lparse-get 'FOOTNOTE-SEPARATOR
))
1141 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line
)
1142 ;; This is a headline
1143 (setq level
(org-tr-level (- (match-end 1) (match-beginning 1)
1145 txt
(match-string 2 line
))
1146 (if (string-match quote-re0 txt
)
1147 (setq txt
(replace-match "" t t txt
)))
1148 (if (<= level
(max umax umax-toc
))
1149 (setq head-count
(+ head-count
1)))
1150 (unless org-lparse-dyn-first-heading-pos
1151 (setq org-lparse-dyn-first-heading-pos
(point)))
1152 (org-lparse-begin-level level txt umax head-count
)
1155 (when (string-match quote-re line
)
1156 (org-lparse-begin-environment 'quote
)))
1158 ((and org-export-with-tables
1159 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line
))
1160 (when (not table-open
)
1162 (setq table-open t table-buffer nil table-orig-buffer nil
))
1165 (setq table-buffer
(cons line table-buffer
)
1166 table-orig-buffer
(cons origline table-orig-buffer
))
1167 (when (or (not lines
)
1168 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
1170 (setq table-open nil
1171 table-buffer
(nreverse table-buffer
)
1172 table-orig-buffer
(nreverse table-orig-buffer
))
1173 (org-lparse-end-paragraph)
1174 (org-lparse-insert 'TABLE table-buffer table-orig-buffer
)))
1179 ;; This line either is list item or end a list.
1180 (when (get-text-property 0 'list-item line
)
1181 (setq line
(org-lparse-export-list-line
1183 (get-text-property 0 'list-item line
)
1184 (get-text-property 0 'list-struct line
)
1185 (get-text-property 0 'list-prevs line
))))
1188 (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line
)
1189 (with-org-lparse-preserve-paragraph-state
1190 (org-lparse-insert 'HORIZONTAL-LINE
))
1191 (throw 'nextline nil
))
1193 ;; Empty lines start a new paragraph. If hand-formatted lists
1194 ;; are not fully interpreted, lines starting with "-", "+", "*"
1195 ;; also start a new paragraph.
1196 (when (string-match "^ [-+*]-\\|^[ \t]*$" line
)
1197 (when org-lparse-footnote-number
1198 (org-lparse-end-footnote-definition org-lparse-footnote-number
)
1199 (setq org-lparse-footnote-number nil
))
1200 (org-lparse-begin-paragraph))
1202 ;; Is this the start of a footnote?
1203 (when org-export-with-footnotes
1204 (when (and (boundp 'footnote-section-tag-regexp
)
1205 (string-match (concat "^" footnote-section-tag-regexp
)
1208 (throw 'nextline nil
))
1209 (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line
)
1210 (org-lparse-end-paragraph)
1211 (setq org-lparse-footnote-number
(match-string 1 line
))
1212 (setq line
(replace-match "" t t line
))
1213 (org-lparse-begin-footnote-definition org-lparse-footnote-number
)))
1214 ;; Check if the line break needs to be conserved
1216 ((string-match "\\\\\\\\[ \t]*$" line
)
1217 (setq line
(replace-match
1218 (org-lparse-format 'LINE-BREAK
)
1220 (org-export-preserve-breaks
1221 (setq line
(concat line
(org-lparse-format 'LINE-BREAK
)))))
1223 ;; Check if a paragraph should be started
1225 (while (and org-lparse-par-open
1226 (string-match "\\\\par\\>" line start
))
1228 ;; Leave a space in the </p> so that the footnote matcher
1229 ;; does not see this.
1230 (if (not (get-text-property (match-beginning 0)
1231 'org-protected line
))
1232 (setq line
(replace-match "</p ><p >" t t line
)))
1233 (setq start
(match-end 0))))
1235 (org-lparse-insert 'LINE line
)))))
1237 ;; Properly close all local lists and other lists
1238 (when (org-lparse-current-environment-p 'quote
)
1239 (org-lparse-end-environment 'quote
))
1241 (org-lparse-end-level 1 umax
)
1243 ;; the </div> to close the last text-... div.
1244 (when (and (> umax
0) org-lparse-dyn-first-heading-pos
)
1245 (org-lparse-end-outline-text-or-outline))
1247 (org-lparse-end 'DOCUMENT-BODY opt-plist
)
1249 (org-lparse-end 'DOCUMENT-CONTENT
))
1251 (unless (plist-get opt-plist
:buffer-will-be-killed
)
1254 (org-lparse-end 'EXPORT
)
1256 (goto-char (point-min))
1257 (or (org-export-push-to-kill-ring
1258 (upcase (symbol-name org-lparse-backend
)))
1259 (message "Exporting... done"))
1263 (let ((f (org-lparse-get 'SAVE-METHOD
)))
1264 (or (and f
(functionp f
) (funcall f filename opt-plist
))
1266 (or (when (and (boundp 'org-lparse-other-backend
)
1267 org-lparse-other-backend
1268 (not (equal org-lparse-backend org-lparse-other-backend
)))
1269 (let ((org-lparse-convert-process
1270 (org-lparse-get-converter org-lparse-backend
)))
1271 (when org-lparse-convert-process
1272 (org-lparse-convert buffer-file-name
1273 (symbol-name org-lparse-other-backend
)))))
1275 ((eq to-buffer
'string
)
1276 (prog1 (buffer-substring (point-min) (point-max))
1277 (kill-buffer (current-buffer))))
1278 (t (current-buffer))))))
1280 (defun org-lparse-format-table (lines olines
)
1281 "Retuns backend-specific code for org-type and table-type tables."
1283 (setq lines
(org-split-string lines
"\n")))
1284 (if (string-match "^[ \t]*|" (car lines
))
1285 ;; A normal org table
1286 (org-lparse-format-org-table lines nil
)
1287 ;; Table made by table.el
1288 (or (org-lparse-format-table-table-using-table-generate-source
1289 org-lparse-backend olines
1290 (not org-export-prefer-native-exporter-for-tables
))
1291 ;; We are here only when table.el table has NO col or row
1292 ;; spanning and the user prefers using org's own converter for
1293 ;; exporting of such simple table.el tables.
1294 (org-lparse-format-table-table lines
))))
1296 (defun org-lparse-table-get-colalign-info (lines)
1297 (let ((forced-aligns (org-find-text-property-in-string
1298 'org-forced-aligns
(car lines
))))
1299 (when (and forced-aligns org-table-clean-did-remove-column
)
1301 (mapcar (lambda (x) (cons (1- (car x
)) (cdr x
))) forced-aligns
)))
1305 (defvar org-lparse-table-style
)
1306 (defvar org-lparse-table-ncols
)
1307 (defvar org-lparse-table-rownum
)
1308 (defvar org-lparse-table-is-styled
)
1309 (defvar org-lparse-table-begin-marker
)
1310 (defvar org-lparse-table-num-numeric-items-per-column
)
1311 (defvar org-lparse-table-colalign-info
)
1312 (defvar org-lparse-table-colalign-vector
)
1314 ;; Following variables are defined in org-table.el
1315 (defvar org-table-number-fraction
)
1316 (defvar org-table-number-regexp
)
1318 (defun org-lparse-do-format-org-table (lines &optional splice
)
1319 "Format a org-type table into backend-specific code.
1320 LINES is a list of lines. Optional argument SPLICE means, do not
1321 insert header and surrounding <table> tags, just format the lines.
1322 Optional argument NO-CSS means use XHTML attributes instead of CSS
1323 for formatting. This is required for the DocBook exporter."
1324 (require 'org-table
)
1325 ;; Get rid of hlines at beginning and end
1326 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
1327 (setq lines
(nreverse lines
))
1328 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
1329 (setq lines
(nreverse lines
))
1330 (when org-export-table-remove-special-lines
1331 ;; Check if the table has a marking column. If yes remove the
1332 ;; column and the special lines
1333 (setq lines
(org-table-clean-before-export lines
)))
1335 (let* ((caption (org-find-text-property-in-string 'org-caption
(car lines
)))
1336 (caption (and caption
(org-xml-encode-org-text caption
)))
1337 (label (org-find-text-property-in-string 'org-label
(car lines
)))
1338 (org-lparse-table-colalign-info (org-lparse-table-get-colalign-info lines
))
1339 (attributes (org-find-text-property-in-string 'org-attributes
1341 (head (and org-export-highlight-first-table-line
1343 (lambda (x) (string-match "^[ \t]*|-" x
))
1345 (org-lparse-table-rownum -
1) org-lparse-table-ncols i
(cnt 0)
1347 org-lparse-table-cur-rowgrp-is-hdr
1348 org-lparse-table-rowgrp-open
1349 org-lparse-table-num-numeric-items-per-column
1350 org-lparse-table-colalign-vector n
1351 org-lparse-table-rowgrp-info
1352 org-lparse-table-begin-marker
1353 (org-lparse-table-style 'org-table
)
1354 org-lparse-table-is-styled
)
1357 (setq org-lparse-table-is-styled nil
)
1358 (while (setq line
(pop lines
))
1359 (unless (string-match "^[ \t]*|-" line
)
1361 (org-lparse-format-table-row
1362 (org-split-string line
"[ \t]*|[ \t]*")) "\n"))))
1364 (setq org-lparse-table-is-styled t
)
1365 (org-lparse-begin 'TABLE caption label attributes
)
1366 (setq org-lparse-table-begin-marker
(point))
1367 (org-lparse-begin-table-rowgroup head
)
1368 (while (setq line
(pop lines
))
1370 ((string-match "^[ \t]*|-" line
)
1371 (when lines
(org-lparse-begin-table-rowgroup)))
1374 (org-lparse-format-table-row
1375 (org-split-string line
"[ \t]*|[ \t]*")) "\n"))))
1376 (org-lparse-end 'TABLE-ROWGROUP
)
1377 (org-lparse-end-table)))))
1379 (defun org-lparse-format-org-table (lines &optional splice
)
1381 (org-lparse-do-format-org-table lines splice
)
1382 (buffer-substring-no-properties (point-min) (point-max))))
1384 (defun org-lparse-do-format-table-table (lines)
1385 "Format a table generated by table.el into backend-specific code.
1386 This conversion does *not* use `table-generate-source' from table.el.
1387 This has the advantage that Org-mode's HTML conversions can be used.
1388 But it has the disadvantage, that no cell- or row-spanning is allowed."
1389 (let (line field-buffer
1390 (org-lparse-table-cur-rowgrp-is-hdr
1391 org-export-highlight-first-table-line
)
1395 (org-lparse-table-style 'table-table
)
1396 (org-lparse-table-is-styled nil
)
1397 fields org-lparse-table-ncols i
(org-lparse-table-rownum -
1)
1398 (empty (org-lparse-format 'SPACES
1)))
1399 (org-lparse-begin 'TABLE caption label attributes
)
1400 (while (setq line
(pop lines
))
1402 ((string-match "^[ \t]*\\+-" line
)
1404 (let ((org-export-table-row-tags '("<tr>" .
"</tr>"))
1405 ;; (org-export-html-table-use-header-tags-for-first-column nil)
1407 (insert (org-lparse-format-table-row field-buffer empty
)))
1408 (setq org-lparse-table-cur-rowgrp-is-hdr nil
)
1409 (setq field-buffer nil
)))
1411 ;; Break the line into fields and store the fields
1412 (setq fields
(org-split-string line
"[ \t]*|[ \t]*"))
1414 (setq field-buffer
(mapcar
1416 (concat x
(org-lparse-format 'LINE-BREAK
)
1419 (setq field-buffer fields
)))))
1420 (org-lparse-end-table)))
1422 (defun org-lparse-format-table-table (lines)
1424 (org-lparse-do-format-table-table lines
)
1425 (buffer-substring-no-properties (point-min) (point-max))))
1427 (defvar table-source-languages
) ; defined in table.el
1428 (defun org-lparse-format-table-table-using-table-generate-source (backend
1432 "Format a table into BACKEND, using `table-generate-source' from table.el.
1433 Use SPANNED-ONLY to suppress exporting of simple table.el tables.
1435 When SPANNED-ONLY is nil, all table.el tables are exported. When
1436 SPANNED-ONLY is non-nil, only tables with either row or column
1439 This routine returns the generated source or nil as appropriate.
1441 Refer docstring of `org-export-prefer-native-exporter-for-tables'
1442 for further information."
1444 (with-current-buffer (get-buffer-create " org-tmp1 ")
1446 (insert (mapconcat 'identity lines
"\n"))
1447 (goto-char (point-min))
1448 (if (not (re-search-forward "|[^+]" nil t
))
1449 (error "Error processing table"))
1450 (table-recognize-table)
1451 (when (or (not spanned-only
)
1452 (let* ((dim (table-query-dimension))
1453 (c (nth 4 dim
)) (r (nth 5 dim
)) (cells (nth 6 dim
)))
1454 (not (= (* c r
) cells
))))
1455 (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
1457 ((member backend table-source-languages
)
1458 (table-generate-source backend
" org-tmp2 ")
1459 (set-buffer " org-tmp2 ")
1460 (buffer-substring (point-min) (point-max)))
1462 ;; table.el doesn't support the given backend. Currently this
1463 ;; happens in case of odt export. Strip the table from the
1464 ;; generated document. A better alternative would be to embed
1465 ;; the table as ascii text in the output document.
1468 "Found table.el-type table in the source org file. "
1469 (format "table.el doesn't support %s backend. "
1470 (upcase (symbol-name backend
)))
1471 "Skipping ahead ..."))
1474 (defun org-lparse-handle-time-stamps (s)
1475 "Format time stamps in string S, or remove them."
1478 (while (string-match org-maybe-keyword-time-regexp s
)
1479 (or b
(setq b
(substring s
0 (match-beginning 0))))
1481 r
(substring s
0 (match-beginning 0)) " "
1488 (match-string 1 s
) "timestamp-kwd"))
1492 (substring (org-translate-time (match-string 3 s
)) 1 -
1)
1494 "timestamp-wrapper"))
1495 s
(substring s
(match-end 0))))
1496 ;; Line break if line started and ended with time stamp stuff
1499 (setq r
(concat r s
))
1500 (unless (string-match "\\S-" (concat b s
))
1501 (setq r
(concat r
(org-lparse-format 'LINE-BREAK
))))
1504 (defun org-xml-encode-plain-text (s)
1505 "Convert plain text characters to HTML equivalent.
1506 Possible conversions are set in `org-export-html-protect-char-alist'."
1507 (let ((cl (org-lparse-get 'PLAIN-TEXT-MAP
)) c
)
1508 (while (setq c
(pop cl
))
1510 (while (string-match (car c
) s start
)
1511 (setq s
(replace-match (cdr c
) t t s
)
1512 start
(1+ (match-beginning 0))))))
1515 (defun org-xml-encode-org-text-skip-links (string)
1516 "Prepare STRING for HTML export. Apply all active conversions.
1517 If there are links in the string, don't modify these."
1518 (let* ((re (concat org-bracket-link-regexp
"\\|"
1519 (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")))
1521 (while (setq m
(string-match re string
))
1522 (setq s
(substring string
0 m
)
1523 l
(match-string 0 string
)
1524 string
(substring string
(match-end 0)))
1525 (push (org-xml-encode-org-text s
) res
)
1527 (push (org-xml-encode-org-text string
) res
)
1528 (apply 'concat
(nreverse res
))))
1530 (defun org-xml-encode-org-text (s)
1531 "Apply all active conversions to translate special ASCII to HTML."
1532 (setq s
(org-xml-encode-plain-text s
))
1533 (if org-export-html-expand
1534 (while (string-match "@<\\([^&]*\\)>" s
)
1535 (setq s
(replace-match "<\\1>" t nil s
))))
1536 (if org-export-with-emphasize
1537 (setq s
(org-lparse-apply-char-styles s
)))
1538 (if org-export-with-special-strings
1539 (setq s
(org-lparse-convert-special-strings s
)))
1540 (if org-export-with-sub-superscripts
1541 (setq s
(org-lparse-apply-sub-superscript-styles s
)))
1542 (if org-export-with-TeX-macros
1543 (let ((start 0) wd rep
)
1544 (while (setq start
(string-match "\\\\\\([a-zA-Z]+[0-9]*\\)\\({}\\)?"
1546 (if (get-text-property (match-beginning 0) 'org-protected s
)
1547 (setq start
(match-end 0))
1548 (setq wd
(match-string 1 s
))
1549 (if (setq rep
(org-lparse-format 'ORG-ENTITY wd
))
1550 (setq s
(replace-match rep t t s
))
1551 (setq start
(+ start
(length wd
))))))))
1554 (defun org-lparse-convert-special-strings (string)
1555 "Convert special characters in STRING to HTML."
1556 (let ((all (org-lparse-get 'SPECIAL-STRING-REGEXPS
))
1558 (while (setq a
(pop all
))
1559 (setq re
(car a
) rpl
(cdr a
) start
0)
1560 (while (string-match re string start
)
1561 (if (get-text-property (match-beginning 0) 'org-protected string
)
1562 (setq start
(match-end 0))
1563 (setq string
(replace-match rpl t nil string
)))))
1566 (defun org-lparse-apply-sub-superscript-styles (string)
1567 "Apply subscript and superscript styles to STRING.
1568 Use `org-export-with-sub-superscripts' to control application of
1569 sub and superscript styles."
1570 (let (key c
(s 0) (requireb (eq org-export-with-sub-superscripts
'{})))
1571 (while (string-match org-match-substring-regexp string s
)
1573 ((and requireb
(match-end 8)) (setq s
(match-end 2)))
1574 ((get-text-property (match-beginning 2) 'org-protected string
)
1575 (setq s
(match-end 2)))
1577 (setq s
(match-end 1)
1578 key
(if (string= (match-string 2 string
) "_")
1579 'subscript
'superscript
)
1580 c
(or (match-string 8 string
)
1581 (match-string 6 string
)
1582 (match-string 5 string
))
1583 string
(replace-match
1584 (concat (match-string 1 string
)
1585 (org-lparse-format 'FONTIFY c key
))
1587 (while (string-match "\\\\\\([_^]\\)" string
)
1588 (setq string
(replace-match (match-string 1 string
) t t string
)))
1591 (defvar org-lparse-char-styles
1598 "Map Org emphasis markers to char styles.
1599 This is an alist where each element is of the
1600 form (ORG-EMPHASIS-CHAR . CHAR-STYLE).")
1602 (defun org-lparse-apply-char-styles (string)
1603 "Apply char styles to STRING.
1604 The variable `org-lparse-char-styles' controls how the Org
1605 emphasis markers are interpreted."
1607 (while (string-match org-emph-re string s
)
1609 (substring string
(match-beginning 3) (1+ (match-beginning 3)))
1610 (substring string
(match-beginning 4) (1+ (match-beginning 4)))))
1611 (setq s
(match-beginning 0)
1614 (match-string 1 string
)
1616 'FONTIFY
(match-string 4 string
)
1617 (nth 1 (assoc (match-string 3 string
)
1618 org-lparse-char-styles
)))
1619 (match-string 5 string
))
1620 string
(replace-match rpl t t string
)
1621 s
(+ s
(- (length rpl
) 2)))
1625 (defun org-lparse-export-list-line (line pos struct prevs
)
1626 "Insert list syntax in export buffer. Return LINE, maybe modified.
1628 POS is the item position or line position the line had before
1629 modifications to buffer. STRUCT is the list structure. PREVS is
1630 the alist of previous items."
1633 ;; Translate type of list containing POS to "d", "o" or
1635 (lambda (pos struct prevs
)
1636 (let ((type (org-list-get-list-type pos struct prevs
)))
1638 ((eq 'ordered type
) "o")
1639 ((eq 'descriptive type
) "d")
1643 ;; Return list of all items and sublists ending at POS, in
1649 (let ((end (nth 6 e
))
1652 ((= end pos
) (push item out
))
1653 ((>= item pos
) (throw 'exit nil
)))))
1656 ;; First close any previous item, or list, ending at POS.
1658 (let* ((lastp (= (org-list-get-last-item e struct prevs
) e
))
1659 (first-item (org-list-get-list-begin e struct prevs
))
1660 (type (funcall get-type first-item struct prevs
)))
1661 (org-lparse-end-paragraph)
1662 ;; Ending for every item
1663 (org-lparse-end-list-item type
)
1664 ;; We're ending last item of the list: end list.
1666 (org-lparse-end-list type
)
1667 (org-lparse-begin-paragraph))))
1668 (funcall get-closings pos
))
1670 ;; At an item: insert appropriate tags in export buffer.
1673 (concat "[ \t]*\\(\\S-+[ \t]*\\)"
1674 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
1675 "\\(?:\\(\\[[ X-]\\]\\)[ \t]+\\)?"
1676 "\\(?:\\(.*\\)[ \t]+::\\(?:[ \t]+\\|$\\)\\)?"
1678 (let* ((checkbox (match-string 3 line
))
1679 (desc-tag (or (match-string 4 line
) "???"))
1680 (body (or (match-string 5 line
) ""))
1681 (list-beg (org-list-get-list-begin pos struct prevs
))
1682 (firstp (= list-beg pos
))
1683 ;; Always refer to first item to determine list type, in
1684 ;; case list is ill-formed.
1685 (type (funcall get-type list-beg struct prevs
))
1686 (counter (let ((count-tmp (org-list-get-counter pos struct
)))
1688 ((not count-tmp
) nil
)
1689 ((string-match "[A-Za-z]" count-tmp
)
1690 (- (string-to-char (upcase count-tmp
)) 64))
1691 ((string-match "[0-9]+" count-tmp
)
1694 (org-lparse-end-paragraph)
1695 (org-lparse-begin-list type
))
1697 (let ((arg (cond ((equal type
"d") desc-tag
)
1698 ((equal type
"o") counter
))))
1699 (org-lparse-begin 'LIST-ITEM type arg
))
1701 ;; If line had a checkbox, some additional modification is required.
1709 ((string-match "X" checkbox
) "X")
1710 ((string-match " " checkbox
)
1711 (org-lparse-format 'SPACES
1))
1717 ;; Return modified line
1719 ;; At a list ender: go to next line (side-effects only).
1720 ((equal "ORG-LIST-END-MARKER" line
) (throw 'nextline nil
))
1721 ;; Not at an item: return line unchanged (side-effects only).
1724 (defun org-lparse-bind-local-variables (opt-plist)
1726 (set (make-local-variable (nth 2 x
))
1727 (plist-get opt-plist
(car x
))))
1728 org-export-plist-vars
))
1730 (defvar org-lparse-table-rowgrp-open
)
1731 (defvar org-lparse-table-cur-rowgrp-is-hdr
)
1732 (defvar org-lparse-footnote-number
)
1733 (defvar org-lparse-footnote-definitions
)
1734 (defvar org-lparse-footnote-buffer
)
1735 (defvar org-lparse-output-buffer
)
1737 (defcustom org-lparse-debug nil
1738 "Enable or Disable logging of `org-lparse' callbacks.
1739 The parameters passed to the backend-registered ENTITY-CONTROL
1740 and ENTITY-FORMAT callbacks are logged as comment strings in the
1741 exported buffer. (org-lparse-format 'COMMENT fmt args) is used
1742 for logging. Customize this variable only if you are an expert
1743 user. Valid values of this variable are:
1744 nil : Disable logging
1745 control : Log all invocations of `org-lparse-begin' and
1746 `org-lparse-end' callbacks.
1747 format : Log invocations of `org-lparse-format' callbacks.
1748 t : Log all invocations of `org-lparse-begin', `org-lparse-end'
1749 and `org-lparse-format' callbacks,"
1752 (const :tag
"Disable" nil
)
1753 (const :tag
"Format callbacks" format
)
1754 (const :tag
"Control callbacks" control
)
1755 (const :tag
"Format and Control callbacks" t
)))
1757 (defun org-lparse-begin (entity &rest args
)
1758 "Begin ENTITY in current buffer. ARGS is entity specific.
1759 ENTITY can be one of PARAGRAPH, LIST, LIST-ITEM etc.
1761 Use (org-lparse-begin 'LIST \"o\") to begin a list in current
1764 See `org-xhtml-entity-control-callbacks-alist' for more
1766 (when (and (member org-lparse-debug
'(t control
))
1767 (not (eq entity
'DOCUMENT-CONTENT
)))
1768 (insert (org-lparse-format 'COMMENT
"%s BEGIN %S" entity args
)))
1770 (let ((f (cadr (assoc entity org-lparse-entity-control-callbacks-alist
))))
1771 (unless f
(error "Unknown entity: %s" entity
))
1774 (defun org-lparse-end (entity &rest args
)
1775 "Close ENTITY in current buffer. ARGS is entity specific.
1776 ENTITY can be one of PARAGRAPH, LIST, LIST-ITEM
1779 Use (org-lparse-end 'LIST \"o\") to close a list in current
1782 See `org-xhtml-entity-control-callbacks-alist' for more
1784 (when (and (member org-lparse-debug
'(t control
))
1785 (not (eq entity
'DOCUMENT-CONTENT
)))
1786 (insert (org-lparse-format 'COMMENT
"%s END %S" entity args
)))
1788 (let ((f (caddr (assoc entity org-lparse-entity-control-callbacks-alist
))))
1789 (unless f
(error "Unknown entity: %s" entity
))
1792 (defun org-lparse-begin-paragraph (&optional style
)
1793 "Insert <p>, but first close previous paragraph if any."
1794 (org-lparse-end-paragraph)
1795 (org-lparse-begin 'PARAGRAPH style
)
1796 (setq org-lparse-par-open t
))
1798 (defun org-lparse-end-paragraph ()
1799 "Close paragraph if there is one open."
1800 (when org-lparse-par-open
1801 (org-lparse-end 'PARAGRAPH
)
1802 (setq org-lparse-par-open nil
)))
1804 (defun org-lparse-end-list-item (&optional type
)
1805 "Close <li> if necessary."
1806 (org-lparse-end-paragraph)
1807 (org-lparse-end 'LIST-ITEM
(or type
"u")))
1809 (defvar org-lparse-dyn-current-environment nil
)
1810 (defun org-lparse-begin-environment (style)
1811 (assert (not org-lparse-dyn-current-environment
) t
)
1812 (setq org-lparse-dyn-current-environment style
)
1813 (org-lparse-begin 'ENVIRONMENT style
))
1815 (defun org-lparse-end-environment (style)
1816 (org-lparse-end 'ENVIRONMENT style
)
1818 (assert (eq org-lparse-dyn-current-environment style
) t
)
1819 (setq org-lparse-dyn-current-environment nil
))
1821 (defun org-lparse-current-environment-p (style)
1822 (eq org-lparse-dyn-current-environment style
))
1824 (defun org-lparse-begin-footnote-definition (n)
1825 (unless org-lparse-footnote-buffer
1826 (setq org-lparse-footnote-buffer
1827 (get-buffer-create "*Org HTML Export Footnotes*")))
1828 (set-buffer org-lparse-footnote-buffer
)
1830 (setq org-lparse-insert-tag-with-newlines nil
)
1831 (org-lparse-begin 'FOOTNOTE-DEFINITION n
))
1833 (defun org-lparse-end-footnote-definition (n)
1834 (org-lparse-end 'FOOTNOTE-DEFINITION n
)
1835 (setq org-lparse-insert-tag-with-newlines
'both
)
1836 (push (cons n
(buffer-string)) org-lparse-footnote-definitions
)
1837 (set-buffer org-lparse-output-buffer
))
1839 (defun org-lparse-format (entity &rest args
)
1840 "Format ENTITY in backend-specific way and return it.
1841 ARGS is specific to entity being formatted.
1843 Use (org-lparse-format 'HEADING \"text\" 1) to format text as
1846 See `org-xhtml-entity-format-callbacks-alist' for more information."
1847 (when (and (member org-lparse-debug
'(t format
))
1848 (not (equal entity
'COMMENT
)))
1849 (insert (org-lparse-format 'COMMENT
"%s: %S" entity args
)))
1852 (let ((text (pop args
)))
1853 (apply 'org-lparse-format
'TAGS entity text args
)))
1855 (let ((f (cdr (assoc entity org-lparse-entity-format-callbacks-alist
))))
1856 (unless f
(error "Unknown entity: %s" entity
))
1859 (defun org-lparse-insert (entity &rest args
)
1860 (insert (apply 'org-lparse-format entity args
)))
1862 (defun org-lparse-prepare-toc (lines level-offset opt-plist umax-toc
)
1863 (let* ((quote-re0 (concat "^[ \t]*" org-quote-string
"\\>"))
1864 (org-min-level (org-get-min-level lines level-offset
))
1865 (org-last-level org-min-level
)
1868 (org-lparse-bind-local-variables opt-plist
)
1870 (org-lparse-begin 'TOC
(nth 3 (plist-get opt-plist
:lang-words
)))
1875 (when (and (string-match org-todo-line-regexp line
)
1876 (not (get-text-property 0 'org-protected line
))
1877 (<= (setq level
(org-tr-level
1878 (- (match-end 1) (match-beginning 1)
1881 (let ((txt (save-match-data
1882 (org-xml-encode-org-text-skip-links
1883 (org-export-cleanup-toc-line
1884 (match-string 3 line
)))))
1886 org-export-mark-todo-in-toc
1887 (or (and (match-beginning 2)
1888 (not (member (match-string 2 line
)
1889 org-done-keywords
)))
1890 (and (= level umax-toc
)
1891 (org-search-todo-below
1892 line lines level
)))))
1894 ;; Check for targets
1895 (while (string-match org-any-target-regexp line
)
1898 (let ((org-lparse-encode-pending t
))
1899 (org-lparse-format 'FONTIFY
1900 (match-string 1 line
) "target"))
1903 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt
)
1904 (setq tags
(match-string 1 txt
)
1905 txt
(replace-match "" t nil txt
)))
1906 (when (string-match quote-re0 txt
)
1907 (setq txt
(replace-match "" t t txt
)))
1908 (while (string-match "<\\(<\\)+\\|>\\(>\\)+" txt
)
1909 (setq txt
(replace-match "" t t txt
)))
1912 (let* ((snumber (org-section-number level
))
1913 (href (replace-regexp-in-string
1914 "\\." "-" (format "sec-%s" snumber
)))
1918 href org-export-preferred-target-alist
))
1920 (href (org-solidify-link-text href
)))
1921 (org-lparse-format 'TOC-ENTRY snumber todo txt tags href
))
1922 level org-last-level
)
1923 (setq org-last-level level
)))
1926 (org-lparse-end 'TOC
)
1927 (setq org-lparse-toc
(buffer-string))))
1930 (defun org-lparse-format-table-row (fields &optional text-for-empty-fields
)
1931 (unless org-lparse-table-ncols
1932 ;; first row of the table
1933 (setq org-lparse-table-ncols
(length fields
))
1934 (when org-lparse-table-is-styled
1935 (setq org-lparse-table-num-numeric-items-per-column
1936 (make-vector org-lparse-table-ncols
0))
1937 (setq org-lparse-table-colalign-vector
1938 (make-vector org-lparse-table-ncols nil
))
1940 (while (< (incf c
) org-lparse-table-ncols
)
1941 (let ((cookie (cdr (assoc (1+ c
) org-lparse-table-colalign-info
))))
1942 (setf (aref org-lparse-table-colalign-vector c
)
1944 ((string= cookie
"l") "left")
1945 ((string= cookie
"r") "right")
1946 ((string= cookie
"c") "center")
1948 (incf org-lparse-table-rownum
)
1954 (when (and (string= x
"") text-for-empty-fields
)
1955 (setq x text-for-empty-fields
))
1957 (and org-lparse-table-is-styled
1958 (< i org-lparse-table-ncols
)
1959 (string-match org-table-number-regexp x
)
1960 (incf (aref org-lparse-table-num-numeric-items-per-column i
)))
1961 (org-lparse-format 'TABLE-CELL x org-lparse-table-rownum i
))
1964 (defun org-lparse-get (what &optional opt-plist
)
1965 "Query for value of WHAT for the current backend `org-lparse-backend'.
1966 See also `org-lparse-backend-get'."
1967 (if (boundp 'org-lparse-backend
)
1968 (org-lparse-backend-get (symbol-name org-lparse-backend
) what opt-plist
)
1969 (error "org-lparse-backend is not bound yet")))
1971 (defun org-lparse-backend-get (backend what
&optional opt-plist
)
1972 "Query BACKEND for value of WHAT.
1973 Dispatch the call to `org-<backend>-user-get'. If that throws an
1974 error, dispatch the call to `org-<backend>-get'. See
1975 `org-xhtml-get' for all known settings queried for by
1976 `org-lparse' during the course of export."
1977 (assert (stringp backend
) t
)
1978 (unless (org-lparse-backend-is-native-p backend
)
1979 (error "Unknown native backend %s" backend
))
1980 (let ((backend-get-method (intern (format "org-%s-get" backend
)))
1981 (backend-user-get-method (intern (format "org-%s-user-get" backend
))))
1983 ((functionp backend-get-method
)
1985 (funcall backend-user-get-method what opt-plist
)
1986 (error (funcall backend-get-method what opt-plist
))))
1988 (error "Native backend %s doesn't define %s" backend backend-get-method
)))))
1990 (defun org-lparse-insert-tag (tag &rest args
)
1991 (when (member org-lparse-insert-tag-with-newlines
'(lead both
))
1993 (insert (apply 'format tag args
))
1994 (when (member org-lparse-insert-tag-with-newlines
'(trail both
))
1997 (defun org-lparse-get-targets-from-title (title)
1998 (let* ((target (org-get-text-property-any 0 'target title
))
1999 (extra-targets (assoc target org-export-target-aliases
))
2000 (target (or (cdr (assoc target org-export-preferred-target-alist
))
2002 (cons target
(remove target extra-targets
))))
2004 (defun org-lparse-suffix-from-snumber (snumber)
2005 (let* ((snu (replace-regexp-in-string "\\." "-" snumber
))
2006 (href (cdr (assoc (concat "sec-" snu
)
2007 org-export-preferred-target-alist
))))
2008 (org-solidify-link-text (or href snu
))))
2010 (defun org-lparse-begin-level (level title umax head-count
)
2011 "Insert a new LEVEL in HTML export.
2012 When TITLE is nil, just close all open levels."
2013 (org-lparse-end-level level umax
)
2014 (unless title
(error "Why is heading nil"))
2015 (let* ((targets (org-lparse-get-targets-from-title title
))
2016 (target (car targets
)) (extra-targets (cdr targets
))
2017 (target (and target
(org-solidify-link-text target
)))
2018 (extra-class (org-get-text-property-any 0 'html-container-class title
))
2019 snumber tags level1 class
)
2020 (when (string-match (org-re "\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") title
)
2021 (setq tags
(and org-export-with-tags
(match-string 1 title
)))
2022 (setq title
(replace-match "" t t title
)))
2025 (if (aref org-levels-open
(1- level
))
2026 (org-lparse-end-list-item)
2027 (aset org-levels-open
(1- level
) t
)
2028 (org-lparse-end-paragraph)
2029 (org-lparse-begin-list 'unordered
))
2031 'LIST-ITEM
'unordered target
2032 (org-lparse-format 'HEADLINE title extra-targets tags
)))
2033 (aset org-levels-open
(1- level
) t
)
2034 (setq snumber
(org-section-number level
))
2035 (setq level1
(+ level
(or (org-lparse-get 'TOPLEVEL-HLEVEL
) 1) -
1))
2036 (unless (= head-count
1)
2037 (org-lparse-end-outline-text-or-outline))
2038 (org-lparse-begin-outline-and-outline-text
2039 level1 snumber title tags target extra-targets extra-class
)
2040 (org-lparse-begin-paragraph))))
2042 (defun org-lparse-end-level (level umax
)
2043 (org-lparse-end-paragraph)
2044 (loop for l from org-level-max downto level
2045 do
(when (aref org-levels-open
(1- l
))
2046 ;; Terminate one level in HTML export
2048 (org-lparse-end-outline-text-or-outline)
2049 (org-lparse-end-list-item)
2050 (org-lparse-end-list 'unordered
))
2051 (aset org-levels-open
(1- l
) nil
))))
2053 (defvar org-lparse-outline-text-open
)
2054 (defun org-lparse-begin-outline-and-outline-text (level1 snumber title tags
2055 target extra-targets
2058 'OUTLINE level1 snumber title tags target extra-targets extra-class
)
2059 (org-lparse-begin-outline-text level1 snumber extra-class
))
2061 (defun org-lparse-end-outline-text-or-outline ()
2063 (org-lparse-outline-text-open
2064 (org-lparse-end 'OUTLINE-TEXT
)
2065 (setq org-lparse-outline-text-open nil
))
2066 (t (org-lparse-end 'OUTLINE
))))
2068 (defun org-lparse-begin-outline-text (level1 snumber extra-class
)
2069 (assert (not org-lparse-outline-text-open
) t
)
2070 (setq org-lparse-outline-text-open t
)
2071 (org-lparse-begin 'OUTLINE-TEXT level1 snumber extra-class
))
2073 (defun org-lparse-html-list-type-to-canonical-list-type (ltype)
2074 (cdr (assoc ltype
'(("o" . ordered
)
2076 ("d" . description
)))))
2078 (defvar org-lparse-list-level
) ; dynamically bound in org-do-lparse
2079 (defun org-lparse-begin-list (ltype)
2080 (incf org-lparse-list-level
)
2081 (org-lparse-begin 'LIST ltype
))
2083 (defun org-lparse-end-list (ltype)
2084 (decf org-lparse-list-level
)
2085 (org-lparse-end 'LIST ltype
))
2087 (defvar org-lparse-table-rowgrp-info
)
2088 (defun org-lparse-begin-table-rowgroup (&optional is-header-row
)
2089 (push (cons (1+ org-lparse-table-rownum
) :start
) org-lparse-table-rowgrp-info
)
2090 (org-lparse-begin 'TABLE-ROWGROUP is-header-row
))
2092 (defun org-lparse-end-table ()
2093 (when org-lparse-table-is-styled
2095 (unless (car org-table-colgroup-info
)
2096 (setq org-table-colgroup-info
2097 (cons :start
(cdr org-table-colgroup-info
))))
2104 (setf (aref org-lparse-table-colalign-vector c
)
2105 (or (aref org-lparse-table-colalign-vector c
)
2106 (if (> (/ (float x
) (1+ org-lparse-table-rownum
))
2107 org-table-number-fraction
)
2109 org-lparse-table-num-numeric-items-per-column
)))
2110 (org-lparse-end 'TABLE
))
2112 (defvar org-lparse-encode-pending nil
)
2114 (defun org-lparse-format-tags (tag text prefix suffix
&rest args
)
2117 (concat prefix
(apply 'format
(car tag
) args
) text suffix
2118 (format (cdr tag
))))
2119 ((stringp tag
) ; singleton tag
2120 (concat prefix
(apply 'format tag args
) text
))))
2122 (defun org-xml-fix-class-name (kwd) ; audit callers of this function
2123 "Turn todo keyword into a valid class name.
2124 Replaces invalid characters with \"_\"."
2126 (while (string-match "[^a-zA-Z0-9_]" kwd
)
2127 (setq kwd
(replace-match "_" t t kwd
))))
2130 (defun org-lparse-format-todo (todo)
2131 (org-lparse-format 'FONTIFY
2133 (ignore-errors (org-lparse-get 'TODO-KWD-CLASS-PREFIX
))
2134 (org-xml-fix-class-name todo
))
2135 (list (if (member todo org-done-keywords
) "done" "todo")
2138 (defun org-lparse-format-extra-targets (extra-targets)
2139 (if (not extra-targets
) ""
2140 (mapconcat (lambda (x)
2141 (setq x
(org-solidify-link-text
2142 (if (org-uuidgen-p x
) (concat "ID-" x
) x
)))
2143 (org-lparse-format 'ANCHOR
"" x
))
2146 (defun org-lparse-format-org-tags (tags)
2154 (ignore-errors (org-lparse-get 'TAG-CLASS-PREFIX
))
2155 (org-xml-fix-class-name x
))))
2156 (org-split-string tags
":")
2157 (org-lparse-format 'SPACES
1)) "tag")))
2159 (defun org-lparse-format-section-number (&optional snumber level
)
2160 (and org-export-with-section-numbers
2161 (not org-lparse-body-only
) snumber level
2162 (org-lparse-format 'FONTIFY snumber
(format "section-number-%d" level
))))
2164 (defun org-lparse-warn (msg)
2165 (if (not org-lparse-use-flashy-warning
)
2167 (put-text-property 0 (length msg
) 'face
'font-lock-warning-face msg
)
2171 (defun org-xml-format-href (s)
2172 "Make sure the S is valid as a href reference in an XHTML document."
2175 (while (string-match "&" s start
)
2176 (setq start
(+ (match-beginning 0) 3)
2177 s
(replace-match "&" t t s
)))))
2180 (defun org-xml-format-desc (s)
2181 "Make sure the S is valid as a description in a link."
2182 (if (and s
(not (get-text-property 1 'org-protected s
)))
2184 (org-xml-encode-org-text s
))
2187 (provide 'org-lparse
)
2189 ;;; org-lparse.el ends here