1 ;;; org-lparse.el --- Line-oriented parser-exporter for Org-mode
3 ;; Copyright (C) 2010-2011 Free Software Foundation, Inc.
5 ;; Author: Jambunathan K <kjambunathan at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
9 ;; This file is not (yet) part of GNU Emacs.
10 ;; However, it is distributed under the same license.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;; `org-lparse' is the entry point for the generic line-oriented
29 ;; exporter. `org-do-lparse' is the genericized version of the
30 ;; original `org-export-as-html' routine.
32 ;; `org-lparse-native-backends' is a good starting point for
33 ;; exploring the generic exporter.
35 ;; Following new interactive commands are provided by this library.
36 ;; `org-lparse', `org-lparse-and-open', `org-lparse-to-buffer'
37 ;; `org-replace-region-by', `org-lparse-region'.
39 ;; Note that the above routines correspond to the following routines
40 ;; in the html exporter `org-export-as-html',
41 ;; `org-export-as-html-and-open', `org-export-as-html-to-buffer',
42 ;; `org-replace-region-by-html' and `org-export-region-as-html'.
44 ;; The new interactive command `org-lparse-convert' can be used to
45 ;; convert documents between various formats. Use this to command,
46 ;; for example, to convert odt file to doc or pdf format.
48 ;; See README.org file that comes with this library for answers to
49 ;; FAQs and more information on using this library.
56 (require 'format-spec
)
59 (defun org-lparse-and-open (target-backend native-backend arg
60 &optional file-or-buf
)
61 "Export outline to TARGET-BACKEND via NATIVE-BACKEND and open exported file.
62 If there is an active region, export only the region. The prefix
63 ARG specifies how many levels of the outline should become
64 headlines. The default is 3. Lower levels will become bulleted
66 (let (f (file-or-buf (or file-or-buf
67 (org-lparse target-backend native-backend
71 ((bufferp file-or-buf
) buffer-file-name
)
72 ((file-exists-p file-or-buf
) file-or-buf
)
73 (t (error "org-lparse-and-open: This shouldn't happen"))))
74 (message "Opening file %s" f
)
76 (when org-export-kill-product-buffer-when-displayed
77 (kill-buffer (current-buffer))))))
80 (defun org-lparse-batch (target-backend &optional native-backend
)
81 "Call the function `org-lparse'.
82 This function can be used in batch processing as:
84 --load=$HOME/lib/emacs/org.el
85 --eval \"(setq org-export-headline-levels 2)\"
86 --visit=MyFile --funcall org-lparse-batch"
87 (setq native-backend
(or native-backend target-backend
))
88 (org-lparse target-backend native-backend
89 org-export-headline-levels
'hidden
))
92 (defun org-lparse-to-buffer (backend arg
)
93 "Call `org-lparse' with output to a temporary buffer.
94 No file is created. The prefix ARG is passed through to
96 (let ((tempbuf (format "*Org %s Export*" (upcase backend
))))
97 (org-lparse backend backend arg nil nil tempbuf
)
98 (when org-export-show-temporary-export-buffer
99 (switch-to-buffer-other-window tempbuf
))))
102 (defun org-replace-region-by (backend beg end
)
103 "Assume the current region has org-mode syntax, and convert it to HTML.
104 This can be used in any buffer. For example, you could write an
105 itemized list in org-mode syntax in an HTML buffer and then use
106 this command to convert it."
107 (let (reg backend-string buf pop-up-frames
)
108 (save-window-excursion
109 (if (eq major-mode
'org-mode
)
110 (setq backend-string
(org-lparse-region backend beg end t
'string
))
111 (setq reg
(buffer-substring beg end
)
112 buf
(get-buffer-create "*Org tmp*"))
113 (with-current-buffer buf
117 (setq backend-string
(org-lparse-region backend
(point-min)
118 (point-max) t
'string
)))
120 (delete-region beg end
)
121 (insert backend-string
)))
124 (defun org-lparse-region (backend beg end
&optional body-only buffer
)
125 "Convert region from BEG to END in org-mode buffer to HTML.
126 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
127 contents, and only produce the region of converted text, useful for
128 cut-and-paste operations.
129 If BUFFER is a buffer or a string, use/create that buffer as a target
130 of the converted HTML. If BUFFER is the symbol `string', return the
131 produced HTML as a string and leave not buffer behind. For example,
132 a Lisp program could call this function in the following way:
134 (setq html (org-lparse-region \"html\" beg end t 'string))
136 When called interactively, the output buffer is selected, and shown
137 in a window. A non-interactive call will only return the buffer."
138 (let ((transient-mark-mode t
) (zmacs-regions t
)
140 (setq ext-plist
(plist-put ext-plist
:ignore-subtree-p t
))
142 (set-mark (point)) ;; to activate the region
144 (setq rtn
(org-lparse backend backend nil nil ext-plist buffer body-only
))
145 (if (fboundp 'deactivate-mark
) (deactivate-mark))
146 (if (and (org-called-interactively-p 'any
) (bufferp rtn
))
147 (switch-to-buffer-other-window rtn
)
150 (defvar org-lparse-par-open nil
)
152 (defun org-lparse-should-inline-p (filename descp
)
153 "Return non-nil if link FILENAME should be inlined.
154 The decision to inline the FILENAME link is based on the current
155 settings. DESCP is the boolean of whether there was a link
156 description. See variables `org-export-html-inline-images' and
157 `org-export-html-inline-image-extensions'."
158 (let ((inline-images (org-lparse-get 'INLINE-IMAGES
))
159 (inline-image-extensions
160 (org-lparse-get 'INLINE-IMAGE-EXTENSIONS
)))
161 (and (or (eq t inline-images
) (and inline-images
(not descp
)))
162 (org-file-image-p filename inline-image-extensions
))))
164 (defun org-lparse-format-org-link (line opt-plist
)
165 "Return LINE with markup of Org mode links.
166 OPT-PLIST is the export options list."
168 (current-dir (if buffer-file-name
169 (file-name-directory buffer-file-name
)
171 (link-validate (plist-get opt-plist
:link-validation-function
))
173 rpl path attr desc descp desc1 desc2 link
174 org-lparse-link-description-is-image
)
175 (while (string-match org-bracket-link-analytic-regexp
++ line start
)
176 (setq org-lparse-link-description-is-image nil
)
177 (setq start
(match-beginning 0))
178 (setq path
(save-match-data (org-link-unescape
179 (match-string 3 line
))))
181 ((match-end 2) (match-string 2 line
))
183 (or (file-name-absolute-p path
)
184 (string-match "^\\.\\.?/" path
)))
187 (setq path
(org-extract-attributes path
))
188 (setq attr
(get-text-property 0 'org-attributes path
))
189 (setq desc1
(if (match-end 5) (match-string 5 line
))
190 desc2
(if (match-end 2) (concat type
":" path
) path
)
191 descp
(and desc1
(not (equal desc1 desc2
)))
192 desc
(or desc1 desc2
))
193 ;; Make an image out of the description if that is so wanted
194 (when (and descp
(org-file-image-p
195 desc
(org-lparse-get 'INLINE-IMAGE-EXTENSIONS
)))
196 (setq org-lparse-link-description-is-image t
)
198 (if (string-match "^file:" desc
)
199 (setq desc
(substring desc
(match-end 0)))))
201 (setq desc
(org-add-props
202 (org-lparse-format 'INLINE-IMAGE desc
)
203 '(org-protected t
)))))
205 ((equal type
"internal")
208 (if (= (string-to-char path
) ?
#)
213 'ORG-LINK opt-plist
"" "" (org-solidify-link-text
215 (org-link-unescape frag-0
))
216 nil
) desc attr descp
))))
217 ((and (equal type
"id")
218 (setq id-file
(org-id-find-id-file path
)))
219 ;; This is an id: link to another file (if it was the same file,
220 ;; it would have become an internal link...)
222 (setq id-file
(file-relative-name
224 (file-name-directory org-current-export-file
)))
227 'ORG-LINK opt-plist type id-file
228 (concat (if (org-uuidgen-p path
) "ID-") path
)
230 ((member type
'("http" "https"))
231 ;; standard URL, can inline as image
234 'ORG-LINK opt-plist type path nil desc attr descp
)))
235 ((member type
'("ftp" "mailto" "news"))
236 ;; standard URL, can't inline as image
239 'ORG-LINK opt-plist type path nil desc attr descp
)))
241 ((string= type
"coderef")
242 (setq rpl
(org-lparse-format
243 'ORG-LINK opt-plist type
"" path desc nil descp
)))
245 ((functionp (setq fnc
(nth 2 (assoc type org-link-protocols
))))
246 ;; The link protocol has a function for format the link
247 (setq rpl
(save-match-data
248 (funcall fnc
(org-link-unescape path
)
249 desc1
(and (boundp 'org-lparse-backend
)
250 (case org-lparse-backend
252 (t org-lparse-backend
)))))))
253 ((string= type
"file")
259 (string-match "::\\(.*\\)" path
)
261 (replace-match "" t nil path
)
262 (match-string 1 path
))
265 ;;The proper path, without a fragment
273 ;;Check the fragment. If it can't be used as
274 ;;target fragment we'll pass nil instead.
278 (not (string-match "^[0-9]*$" fragment-0
))
279 (not (string-match "^\\*" fragment-0
))
280 (not (string-match "^/.*/$" fragment-0
)))
281 (org-solidify-link-text
282 (org-link-unescape fragment-0
))
285 ;;Description minus "file:" and ".org"
286 (if (string-match "^file:" desc
)
288 ((desc-1 (replace-match "" t t desc
)))
289 (if (string-match "\\.org$" desc-1
)
290 (replace-match "" t t desc-1
)
297 (functionp link-validate
)
298 (not (funcall link-validate path-1 current-dir
)))
301 'ORG-LINK opt-plist
"file" path-1 fragment-1
302 desc-2 attr descp
))))))
305 ;; just publish the path, as default
306 (setq rpl
(concat "<i><" type
":"
307 (save-match-data (org-link-unescape path
))
309 (setq line
(replace-match rpl t t line
)
310 start
(+ start
(length rpl
))))
313 (defvar org-lparse-par-open-stashed
) ; bound during `org-do-lparse'
314 (defun org-lparse-stash-save-paragraph-state ()
315 (assert (zerop org-lparse-par-open-stashed
))
316 (setq org-lparse-par-open-stashed org-lparse-par-open
)
317 (setq org-lparse-par-open nil
))
319 (defun org-lparse-stash-pop-paragraph-state ()
320 (setq org-lparse-par-open org-lparse-par-open-stashed
)
321 (setq org-lparse-par-open-stashed
0))
323 (defmacro with-org-lparse-preserve-paragraph-state
(&rest body
)
324 `(let ((org-lparse-do-open-par org-lparse-par-open
))
325 (org-lparse-end-paragraph)
327 (when org-lparse-do-open-par
328 (org-lparse-begin-paragraph))))
329 (def-edebug-spec with-org-lparse-preserve-paragraph-state
(body))
331 (defvar org-lparse-native-backends nil
332 "List of native backends registered with `org-lparse'.
333 A backend can use `org-lparse-register-backend' to add itself to
336 All native backends must implement a get routine and a mandatory
337 set of callback routines.
339 The get routine must be named as org-<backend>-get where backend
340 is the name of the backend. The exporter uses `org-lparse-get'
341 and retrieves the backend-specific callback by querying for
342 ENTITY-CONTROL and ENTITY-FORMAT variables.
344 For the sake of illustration, the html backend implements
345 `org-xhtml-get'. It returns
346 `org-xhtml-entity-control-callbacks-alist' and
347 `org-xhtml-entity-format-callbacks-alist' as the values of
348 ENTITY-CONTROL and ENTITY-FORMAT settings.")
350 (defun org-lparse-register-backend (backend)
351 "Make BACKEND known to `org-lparse' library.
352 Add BACKEND to `org-lparse-native-backends'."
355 ((symbolp backend
) (symbol-name backend
))
356 ((stringp backend
) backend
)
357 (t (error "Error while registering backend: %S" backend
))))
358 (add-to-list 'org-lparse-native-backends backend
)))
360 (defun org-lparse-unregister-backend (backend)
361 (setq org-lparse-native-backends
363 ((symbolp backend
) (symbol-name backend
))
364 ((stringp backend
) backend
))
365 org-lparse-native-backends
))
366 (message "Unregistered backend %S" backend
))
368 (defun org-lparse-do-reachable-formats (in-fmt)
369 "Return verbose info about formats to which IN-FMT can be converted.
370 Return a list where each element is of the
371 form (CONVERTER-PROCESS . OUTPUT-FMT-ALIST). See
372 `org-export-odt-convert-processes' for CONVERTER-PROCESS and see
373 `org-export-odt-convert-capabilities' for OUTPUT-FMT-ALIST."
374 (let (reachable-formats)
375 (dolist (backend org-lparse-native-backends reachable-formats
)
376 (let* ((converter (org-lparse-backend-get
377 backend
'CONVERT-METHOD
))
378 (capabilities (org-lparse-backend-get
379 backend
'CONVERT-CAPABILITIES
)))
381 (dolist (c capabilities
)
382 (when (member in-fmt
(nth 1 c
))
383 (push (cons converter
(nth 2 c
)) reachable-formats
))))))))
385 (defun org-lparse-reachable-formats (in-fmt)
386 "Return list of formats to which IN-FMT can be converted.
387 The list of the form (OUTPUT-FMT-1 OUTPUT-FMT-2 ...)."
389 (mapc (lambda (e) (add-to-list 'l e
))
390 (apply 'append
(mapcar
391 (lambda (e) (mapcar 'car
(cdr e
)))
392 (org-lparse-do-reachable-formats in-fmt
))))
395 (defun org-lparse-reachable-p (in-fmt out-fmt
)
396 "Return non-nil if IN-FMT can be converted to OUT-FMT."
398 (let ((reachable-formats (org-lparse-do-reachable-formats in-fmt
)))
399 (dolist (e reachable-formats
)
400 (let ((out-fmt-spec (assoc out-fmt
(cdr e
))))
402 (throw 'done
(cons (car e
) out-fmt-spec
))))))))
404 (defun org-lparse-backend-is-native-p (backend)
405 (member backend org-lparse-native-backends
))
407 (defun org-lparse (target-backend native-backend arg
408 &optional hidden ext-plist
409 to-buffer body-only pub-dir
)
410 "Export the outline to various formats.
411 If there is an active region, export only the region. The
412 outline is first exported to NATIVE-BACKEND and optionally
413 converted to TARGET-BACKEND. See `org-lparse-native-backends'
414 for list of known native backends. Each native backend can
415 specify a converter and list of target backends it exports to
416 using the CONVERT-PROCESS and OTHER-BACKENDS settings of it's get
417 method. See `org-xhtml-get' for an illustrative example.
419 ARG is a prefix argument that specifies how many levels of
420 outline should become headlines. The default is 3. Lower levels
421 will become bulleted lists.
423 HIDDEN is obsolete and does nothing.
425 EXT-PLIST is a property list that controls various aspects of
426 export. The settings here override org-mode's default settings
427 and but are inferior to file-local settings.
429 TO-BUFFER dumps the exported lines to a buffer or a string
430 instead of a file. If TO-BUFFER is the symbol `string' return the
431 exported lines as a string. If TO-BUFFER is non-nil, create a
432 buffer with that name and export to that buffer.
434 BODY-ONLY controls the presence of header and footer lines in
435 exported text. If BODY-ONLY is non-nil, don't produce the file
436 header and footer, simply return the content of <body>...</body>,
437 without even the body tags themselves.
439 PUB-DIR specifies the publishing directory."
440 (let* ((org-lparse-backend (intern native-backend
))
441 (org-lparse-other-backend (and target-backend
442 (intern target-backend
))))
443 (unless (org-lparse-backend-is-native-p native-backend
)
444 (error "Don't know how to export natively to backend %s" native-backend
))
446 (unless (or (equal native-backend target-backend
)
447 (org-lparse-reachable-p native-backend target-backend
))
448 (error "Don't know how to export to backend %s %s" target-backend
449 (format "via %s" native-backend
)))
450 (run-hooks 'org-export-first-hook
)
451 (org-do-lparse arg hidden ext-plist to-buffer body-only pub-dir
)))
453 (defcustom org-lparse-use-flashy-warning nil
454 "Control flashing of messages logged with `org-lparse-warn'.
455 When non-nil, messages are fontified with warning face and the
456 exporter lingers for a while to catch user's attention."
460 (defun org-lparse-convert-read-params ()
461 "Return IN-FILE and OUT-FMT params for `org-lparse-do-convert'.
462 This is a helper routine for interactive use."
463 (let* ((input (if (featurep 'ido
) 'ido-completing-read
'completing-read
))
464 (in-file (read-file-name "File to be converted: "
465 nil buffer-file-name t
))
466 (in-fmt (file-name-extension in-file
))
467 (out-fmt-choices (org-lparse-reachable-formats in-fmt
))
469 (or (and out-fmt-choices
470 (funcall input
"Output format: "
471 out-fmt-choices nil nil nil
))
473 "No known converter or no known output formats for %s files"
475 (list in-file out-fmt
)))
478 (require 'browse-url
))
480 (defun org-lparse-do-convert (in-file out-fmt
&optional prefix-arg
)
481 "Workhorse routine for `org-export-odt-convert'."
482 (require 'browse-url
)
483 (let* ((in-file (expand-file-name (or in-file buffer-file-name
)))
484 (dummy (or (file-readable-p in-file
)
485 (error "Cannot read %s" in-file
)))
486 (in-fmt (file-name-extension in-file
))
487 (out-fmt (or out-fmt
(error "Output format unspecified")))
488 (how (or (org-lparse-reachable-p in-fmt out-fmt
)
489 (error "Cannot convert from %s format to %s format?"
491 (convert-process (car how
))
492 (program (car convert-process
))
493 (dummy (and (or program
(error "Converter not configured"))
494 (or (executable-find program
)
495 (error "Cannot find converter %s" program
))))
496 (out-file (concat (file-name-sans-extension in-file
) "."
497 (nth 1 (or (cdr how
) out-fmt
))))
498 (out-dir (file-name-directory in-file
))
499 (arglist (mapcar (lambda (arg)
501 arg
`((?i .
,in-file
)
502 (?I .
,(browse-url-file-url in-file
))
505 (?O .
,(browse-url-file-url out-file
))
507 (?D .
,(browse-url-file-url out-dir
)))))
508 (cdr convert-process
))))
509 (when (file-exists-p out-file
)
510 (delete-file out-file
))
512 (message "Executing %s %s" program
(mapconcat 'identity arglist
" "))
513 (apply 'call-process program nil nil nil arglist
)
515 ((file-exists-p out-file
)
516 (message "Exported to %s using %s" out-file program
)
518 (message "Opening %s..." out-file
)
519 (org-open-file out-file
))
521 ;; (set-buffer (find-file-noselect out-file))
524 (message "Export to %s failed" out-file
)
527 (defvar org-lparse-insert-tag-with-newlines
'both
)
529 ;; Following variables are let-bound during `org-lparse'
530 (defvar org-lparse-dyn-first-heading-pos
)
531 (defvar org-lparse-toc
)
532 (defvar org-lparse-entity-control-callbacks-alist
)
533 (defvar org-lparse-entity-format-callbacks-alist
)
534 (defvar org-lparse-backend nil
535 "The native backend to which the document is currently exported.
536 This variable is let bound during `org-lparse'. Valid values are
537 one of the symbols corresponding to `org-lparse-native-backends'.
539 Compare this variable with `org-export-current-backend' which is
540 bound only during `org-export-preprocess-string' stage of the
543 See also `org-lparse-other-backend'.")
545 (defvar org-lparse-other-backend nil
546 "The target backend to which the document is currently exported.
547 This variable is let bound during `org-lparse'. This variable is
548 set to either `org-lparse-backend' or one of the symbols
549 corresponding to OTHER-BACKENDS specification of the
552 For example, if a document is exported to \"odt\" then both
553 org-lparse-backend and org-lparse-other-backend are bound to
554 'odt. On the other hand, if a document is exported to \"odt\"
555 and then converted to \"doc\" then org-lparse-backend is set to
556 'odt and org-lparse-other-backend is set to 'doc.")
558 (defvar org-lparse-body-only nil
559 "Bind this to BODY-ONLY arg of `org-lparse'.")
561 (defvar org-lparse-to-buffer nil
562 "Bind this to TO-BUFFER arg of `org-lparse'.")
564 (defun org-lparse-get-block-params (params)
567 (setq params
(org-trim params
))
568 (unless (string-match "\\`(.*)\\'" params
)
569 (setq params
(format "(%s)" params
)))
570 (ignore-errors (read params
)))))
572 (defvar org-heading-keyword-regexp-format
) ; defined in org.el
573 (defvar org-lparse-special-blocks
'("list-table" "annotation"))
574 (defun org-do-lparse (arg &optional hidden ext-plist
575 to-buffer body-only pub-dir
)
576 "Export the outline to various formats.
577 See `org-lparse' for more information. This function is a
578 html-agnostic version of the `org-export-as-html' function in 7.5
580 ;; Make sure we have a file name when we need it.
581 (when (and (not (or to-buffer body-only
))
582 (not buffer-file-name
))
583 (if (buffer-base-buffer)
584 (org-set-local 'buffer-file-name
585 (with-current-buffer (buffer-base-buffer)
587 (error "Need a file name to be able to export")))
590 (format "Exporting to %s using org-lparse..."
592 (or org-lparse-backend org-lparse-other-backend
)))))
594 (setq-default org-todo-line-regexp org-todo-line-regexp
)
595 (setq-default org-deadline-line-regexp org-deadline-line-regexp
)
596 (setq-default org-done-keywords org-done-keywords
)
597 (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp
)
598 (let* (hfy-user-sheet-assoc ; let `htmlfontify' know that
599 ; we are interested in
601 org-lparse-encode-pending
603 (org-lparse-par-open-stashed 0)
606 (org-lparse-list-level 0) ; list level starts at 1. A
607 ; value of 0 implies we are
608 ; outside of any list
609 (org-lparse-list-item-count 0)
610 org-lparse-list-stack
612 ;; list-table related vars
613 org-lparse-list-table-p
614 org-lparse-list-table
:table-cell-open
615 org-lparse-list-table
:table-row
616 org-lparse-list-table
:lines
618 org-lparse-outline-text-open
619 (org-lparse-latex-fragment-fallback ; currently used only by
621 (or (ignore-errors (org-lparse-get 'LATEX-FRAGMENT-FALLBACK
))
622 (if (and (org-check-external-command "latex" "" t
)
623 (org-check-external-command "dvipng" "" t
))
626 (org-lparse-insert-tag-with-newlines 'both
)
627 (org-lparse-to-buffer to-buffer
)
628 (org-lparse-body-only body-only
)
629 (org-lparse-entity-control-callbacks-alist
630 (org-lparse-get 'ENTITY-CONTROL
))
631 (org-lparse-entity-format-callbacks-alist
632 (org-lparse-get 'ENTITY-FORMAT
))
634 (org-export-process-option-filters
635 (org-combine-plists (org-default-export-plist)
637 (org-infile-export-plist))))
638 (body-only (or body-only
(plist-get opt-plist
:body-only
)))
639 valid org-lparse-dyn-first-heading-pos
640 (odd org-odd-levels-only
)
641 (region-p (org-region-active-p))
642 (rbeg (and region-p
(region-beginning)))
643 (rend (and region-p
(region-end)))
645 (if (plist-get opt-plist
:ignore-subtree-p
)
650 (and (org-at-heading-p)
651 (>= (org-end-of-subtree t t
) rend
))))))
652 (level-offset (if subtree-p
655 (+ (funcall outline-level
)
656 (if org-odd-levels-only
1 0)))
658 (opt-plist (setq org-export-opt-plist
660 (org-export-add-subtree-options opt-plist rbeg
)
662 ;; The following two are dynamically scoped into other
664 (org-current-export-dir
665 (or pub-dir
(org-lparse-get 'EXPORT-DIR opt-plist
)))
666 (org-current-export-file buffer-file-name
)
667 (level 0) (line "") (origline "") txt todo
670 (filename (if to-buffer nil
673 (file-name-sans-extension
675 (org-entry-get (region-beginning)
676 "EXPORT_FILE_NAME" t
))
677 (file-name-nondirectory buffer-file-name
)))
678 "." (org-lparse-get 'FILE-NAME-EXTENSION opt-plist
))
679 (file-name-as-directory
680 (or pub-dir
(org-lparse-get 'EXPORT-DIR opt-plist
))))))
681 (current-dir (if buffer-file-name
682 (file-name-directory buffer-file-name
)
684 (auto-insert nil
) ; Avoid any auto-insert stuff for the new file
685 (buffer (if to-buffer
687 ((eq to-buffer
'string
)
688 (get-buffer-create (org-lparse-get 'EXPORT-BUFFER-NAME
)))
689 (t (get-buffer-create to-buffer
)))
691 (or (let ((f (org-lparse-get 'INIT-METHOD
)))
692 (and f
(functionp f
) (funcall f filename
)))
694 (org-levels-open (make-vector org-level-max nil
))
697 (let* ((val (plist-get opt-plist p
))
698 (val (org-xml-encode-org-text-skip-links val
)))
699 (setq opt-plist
(plist-put opt-plist p val
))))
700 '(:date
:author
:keywords
:description
)))
701 (date (plist-get opt-plist
:date
))
703 ((and date
(string-match "%" date
))
704 (format-time-string date
))
706 (t (format-time-string "%Y-%m-%d %T %Z"))))
707 (dummy (setq opt-plist
(plist-put opt-plist
:effective-date date
)))
708 (title (org-xml-encode-org-text-skip-links
709 (or (and subtree-p
(org-export-get-title-from-subtree))
710 (plist-get opt-plist
:title
)
713 (plist-get opt-plist
:skip-before-1st-heading
))
714 (org-export-grab-title-from-buffer))
715 (and buffer-file-name
716 (file-name-sans-extension
717 (file-name-nondirectory buffer-file-name
)))
719 (dummy (setq opt-plist
(plist-put opt-plist
:title title
)))
720 (html-table-tag (plist-get opt-plist
:html-table-tag
))
721 (quote-re0 (concat "^ *" org-quote-string
"\\( +\\|[ \t]*$\\)"))
722 (quote-re (format org-heading-keyword-regexp-format
724 (org-lparse-dyn-current-environment nil
)
725 ;; Get the language-dependent settings
726 (lang-words (or (assoc (plist-get opt-plist
:language
)
727 org-export-language-setup
)
728 (assoc "en" org-export-language-setup
)))
729 (dummy (setq opt-plist
(plist-put opt-plist
:lang-words lang-words
)))
732 (coding-system-for-write
733 (or (ignore-errors (org-lparse-get 'CODING-SYSTEM-FOR-WRITE
))
734 (and (boundp 'buffer-file-coding-system
)
735 buffer-file-coding-system
)))
736 (save-buffer-coding-system
737 (or (ignore-errors (org-lparse-get 'CODING-SYSTEM-FOR-SAVE
))
738 (and (boundp 'buffer-file-coding-system
)
739 buffer-file-coding-system
)))
742 (if region-p
(region-beginning) (point-min))
743 (if region-p
(region-end) (point-max))))
744 (org-export-have-math nil
)
745 (org-export-footnotes-seen nil
)
746 (org-export-footnotes-data (org-footnote-all-labels 'with-defs
))
747 (org-footnote-insert-pos-for-preprocessor 'point-min
)
748 (org-lparse-opt-plist opt-plist
)
751 (org-export-preprocess-string
754 :for-backend
(if (equal org-lparse-backend
'xhtml
) ; hack
757 :skip-before-1st-heading
758 (plist-get opt-plist
:skip-before-1st-heading
)
759 :drawers
(plist-get opt-plist
:drawers
)
760 :todo-keywords
(plist-get opt-plist
:todo-keywords
)
761 :tasks
(plist-get opt-plist
:tasks
)
762 :tags
(plist-get opt-plist
:tags
)
763 :priority
(plist-get opt-plist
:priority
)
764 :footnotes
(plist-get opt-plist
:footnotes
)
765 :timestamps
(plist-get opt-plist
:timestamps
)
767 (plist-get opt-plist
:archived-trees
)
768 :select-tags
(plist-get opt-plist
:select-tags
)
769 :exclude-tags
(plist-get opt-plist
:exclude-tags
)
771 (plist-get opt-plist
:text
)
773 (plist-get opt-plist
:LaTeX-fragments
))
776 table-buffer table-orig-buffer
778 rpl path attr desc descp desc1 desc2 link
780 footnotes footref-seen
781 org-lparse-output-buffer
782 org-lparse-footnote-definitions
783 org-lparse-footnote-number
785 org-lparse-collect-buffer
786 (org-lparse-collect-count 0) ; things will get haywire if
787 ; collections are chained. Use
788 ; this variable to assert this
794 (let ((inhibit-read-only t
))
796 (remove-text-properties (point-min) (point-max)
797 '(:org-license-to-kill t
))))
799 (message "Exporting...")
800 (org-init-section-numbers)
802 ;; Switch to the output buffer
803 (setq org-lparse-output-buffer buffer
)
804 (set-buffer org-lparse-output-buffer
)
805 (let ((inhibit-read-only t
)) (erase-buffer))
807 (org-install-letbind)
809 (and (fboundp 'set-buffer-file-coding-system
)
810 (set-buffer-file-coding-system coding-system-for-write
))
812 (let ((case-fold-search nil
)
813 (org-odd-levels-only odd
))
814 ;; create local variables for all options, to make sure all called
815 ;; functions get the correct information
817 (set (make-local-variable (nth 2 x
))
818 (plist-get opt-plist
(car x
))))
819 org-export-plist-vars
)
820 (setq umax
(if arg
(prefix-numeric-value arg
)
821 org-export-headline-levels
))
822 (setq umax-toc
(if (integerp org-export-with-toc
)
823 (min org-export-with-toc umax
)
826 (when (and org-export-with-toc
(not body-only
))
827 (setq lines
(org-lparse-prepare-toc
828 lines level-offset opt-plist umax-toc
)))
831 (org-lparse-begin 'DOCUMENT-CONTENT opt-plist
)
832 (org-lparse-begin 'DOCUMENT-BODY opt-plist
))
835 (org-init-section-numbers)
837 (org-lparse-begin-paragraph)
839 (while (setq line
(pop lines
) origline line
)
841 (when (and (org-lparse-current-environment-p 'quote
)
842 (string-match org-outline-regexp-bol line
))
843 (org-lparse-end-environment 'quote
))
845 (when (org-lparse-current-environment-p 'quote
)
846 (org-lparse-insert 'LINE line
)
847 (throw 'nextline nil
))
849 ;; Fixed-width, verbatim lines (examples)
850 (when (and org-export-with-fixed-width
851 (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line
))
852 (when (not (org-lparse-current-environment-p 'fixedwidth
))
853 (org-lparse-begin-environment 'fixedwidth
))
854 (org-lparse-insert 'LINE
(match-string 3 line
))
855 (when (or (not lines
)
856 (not (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)"
858 (org-lparse-end-environment 'fixedwidth
))
859 (throw 'nextline nil
))
861 ;; Notes: The baseline version of org-html.el (git commit
862 ;; 3d802e), while encoutering a *line-long* protected text,
863 ;; does one of the following two things based on the state
864 ;; of the export buffer.
866 ;; 1. If a paragraph element has just been opened and
867 ;; contains only whitespace as content, insert the
868 ;; protected text as part of the previous paragraph.
870 ;; 2. If the paragraph element has already been opened and
871 ;; contains some valid content insert the protected text
872 ;; as part of the current paragraph.
876 ;; Scenario 1 mentioned above kicks in when a block of
877 ;; protected text has to be inserted enbloc. For example,
878 ;; this happens, when inserting an source or example block
879 ;; or preformatted content enclosed in #+backend,
880 ;; #+begin_bakend ... #+end_backend)
882 ;; Scenario 2 mentioned above kicks in when the protected
883 ;; text is part of a running sentence. For example this
884 ;; happens in the case of an *multiline* LaTeX equation that
885 ;; needs to be inserted verbatim.
887 ;; org-html.el in the master branch seems to do some
888 ;; jugglery by moving paragraphs around. Inorder to make
889 ;; these changes backend-agnostic introduce a new text
890 ;; property org-native-text and impose the added semantics
891 ;; that these protected blocks appear outside of a
892 ;; conventional paragraph element.
894 ;; Extra Note: Check whether org-example and org-native-text
895 ;; are entirely equivalent.
897 ;; Fixes bug reported by Christian Moe concerning verbatim
899 ;; on git commit 533ba3f90250a1f25f494c390d639ea6274f235c
900 ;; http://repo.or.cz/w/org-mode/org-jambu.git/shortlog/refs/heads/staging
901 ;; See http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01379.html
904 (when (and (get-text-property 0 'org-native-text line
)
905 ;; Make sure it is the entire line that is protected
906 (not (< (or (next-single-property-change
907 0 'org-native-text line
) 10000)
909 (let ((ind (get-text-property 0 'original-indentation line
)))
910 (org-lparse-begin-environment 'native
)
911 (org-lparse-insert 'LINE line
)
913 (or (= (length (car lines
)) 0)
915 (equal ind
(get-text-property
916 0 'original-indentation
(car lines
))))
917 (or (= (length (car lines
)) 0)
918 (get-text-property 0 'org-native-text
(car lines
))))
919 (org-lparse-insert 'LINE
(pop lines
)))
920 (org-lparse-end-environment 'native
))
921 (throw 'nextline nil
))
924 (when (and (get-text-property 0 'org-protected line
)
925 ;; Make sure it is the entire line that is protected
926 (not (< (or (next-single-property-change
927 0 'org-protected line
) 10000)
929 (let ((ind (get-text-property 0 'original-indentation line
)))
930 (org-lparse-insert 'LINE line
)
932 (or (= (length (car lines
)) 0)
934 (equal ind
(get-text-property
935 0 'original-indentation
(car lines
))))
936 (or (= (length (car lines
)) 0)
937 (get-text-property 0 'org-protected
(car lines
))))
938 (org-lparse-insert 'LINE
(pop lines
))))
939 (throw 'nextline nil
))
941 ;; Blockquotes, verse, and center
943 "^ORG-\\(.+\\)-\\(START\\|END\\)\\([ \t]+.*\\)?$" line
)
944 (let* ((style (intern (downcase (match-string 1 line
))))
945 (env-options-plist (org-lparse-get-block-params
946 (match-string 3 line
)))
947 (f (cdr (assoc (match-string 2 line
)
948 '(("START" . org-lparse-begin-environment
)
949 ("END" . org-lparse-end-environment
))))))
952 '(blockquote verse center
)
953 (mapcar 'intern org-lparse-special-blocks
)))
954 (funcall f style env-options-plist
)
955 (throw 'nextline nil
))))
957 (run-hooks 'org-export-html-after-blockquotes-hook
)
958 (when (org-lparse-current-environment-p 'verse
)
959 (let ((i (org-get-string-indentation line
)))
962 (let ((org-lparse-encode-pending t
))
963 (org-lparse-format 'SPACES
(* 2 i
)))
964 " " (org-trim line
))))
965 (unless (string-match "\\\\\\\\[ \t]*$" line
)
966 (setq line
(concat line
"\\\\")))))
968 ;; make targets to anchors
971 "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line start
)
973 ((get-text-property (match-beginning 1) 'org-protected line
)
974 (setq start
(match-end 1)))
976 (setq line
(replace-match
977 (let ((org-lparse-encode-pending t
))
979 'ANCHOR
"" (org-solidify-link-text
980 (match-string 1 line
))))
982 ((and org-export-with-toc
(equal (string-to-char line
) ?
*))
983 ;; FIXME: NOT DEPENDENT on TOC?????????????????????
984 (setq line
(replace-match
985 (let ((org-lparse-encode-pending t
))
987 'FONTIFY
(match-string 1 line
) "target"))
988 ;; (concat "@<i>" (match-string 1 line) "@</i> ")
991 (setq line
(replace-match
993 (let ((org-lparse-encode-pending t
))
995 'ANCHOR
(match-string 1 line
)
996 (org-solidify-link-text (match-string 1 line
))
1000 (let ((org-lparse-encode-pending t
))
1001 (setq line
(org-lparse-handle-time-stamps line
)))
1003 ;; replace "&" by "&", "<" and ">" by "<" and ">"
1004 ;; handle @<..> HTML tags (replace "@>..<" by "<..>")
1005 ;; Also handle sub_superscripts and checkboxes
1006 (or (string-match org-table-hline-regexp line
)
1007 (string-match "^[ \t]*\\([+]-\\||[ ]\\)[-+ |]*[+|][ \t]*$" line
)
1008 (setq line
(org-xml-encode-org-text-skip-links line
)))
1010 (setq line
(org-lparse-format-org-link line opt-plist
))
1013 (if (and org-todo-line-regexp
1014 (string-match org-todo-line-regexp line
)
1015 (match-beginning 2))
1017 (substring line
0 (match-beginning 2))
1018 (org-lparse-format 'TODO
(match-string 2 line
))
1019 (substring line
(match-end 2)))))
1021 ;; Does this contain a reference to a footnote?
1022 (when org-export-with-footnotes
1024 (while (string-match "\\([^* \t].*?\\)[ \t]*\\[\\([0-9]+\\)\\]" line start
)
1025 ;; Discard protected matches not clearly identified as
1026 ;; footnote markers.
1027 (if (or (get-text-property (match-beginning 2) 'org-protected line
)
1028 (not (get-text-property (match-beginning 2) 'org-footnote line
)))
1029 (setq start
(match-end 2))
1030 (let ((n (match-string 2 line
)) refcnt a
)
1031 (if (setq a
(assoc n footref-seen
))
1033 (setcdr a
(1+ (cdr a
)))
1034 (setq refcnt
(cdr a
)))
1036 (push (cons n
1) footref-seen
))
1040 (or (match-string 1 line
) "")
1043 n
(cdr (assoc n org-lparse-footnote-definitions
))
1045 ;; If another footnote is following the
1046 ;; current one, add a separator.
1047 (if (save-match-data
1048 (string-match "\\`\\[[0-9]+\\]"
1049 (substring line
(match-end 0))))
1051 (org-lparse-get 'FOOTNOTE-SEPARATOR
))
1056 ((string-match "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$" line
)
1057 ;; This is a headline
1058 (setq level
(org-tr-level (- (match-end 1) (match-beginning 1)
1060 txt
(match-string 2 line
))
1061 (if (string-match quote-re0 txt
)
1062 (setq txt
(replace-match "" t t txt
)))
1063 (if (<= level
(max umax umax-toc
))
1064 (setq head-count
(+ head-count
1)))
1065 (unless org-lparse-dyn-first-heading-pos
1066 (setq org-lparse-dyn-first-heading-pos
(point)))
1067 (org-lparse-begin-level level txt umax head-count
)
1070 (when (string-match quote-re line
)
1071 (org-lparse-begin-environment 'quote
)))
1073 ((and org-export-with-tables
1074 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line
))
1075 (when (not table-open
)
1077 (setq table-open t table-buffer nil table-orig-buffer nil
))
1080 (setq table-buffer
(cons line table-buffer
)
1081 table-orig-buffer
(cons origline table-orig-buffer
))
1082 (when (or (not lines
)
1083 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
1085 (setq table-open nil
1086 table-buffer
(nreverse table-buffer
)
1087 table-orig-buffer
(nreverse table-orig-buffer
))
1088 (org-lparse-end-paragraph)
1089 (org-lparse-insert 'TABLE table-buffer table-orig-buffer
)))
1094 ;; This line either is list item or end a list.
1095 (when (get-text-property 0 'list-item line
)
1096 (setq line
(org-lparse-export-list-line
1098 (get-text-property 0 'list-item line
)
1099 (get-text-property 0 'list-struct line
)
1100 (get-text-property 0 'list-prevs line
))))
1103 (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line
)
1104 (with-org-lparse-preserve-paragraph-state
1105 (org-lparse-insert 'HORIZONTAL-LINE
))
1106 (throw 'nextline nil
))
1108 ;; Empty lines start a new paragraph. If hand-formatted lists
1109 ;; are not fully interpreted, lines starting with "-", "+", "*"
1110 ;; also start a new paragraph.
1111 (when (string-match "^ [-+*]-\\|^[ \t]*$" line
)
1112 (when org-lparse-footnote-number
1113 (org-lparse-end-footnote-definition org-lparse-footnote-number
)
1114 (setq org-lparse-footnote-number nil
))
1115 (org-lparse-begin-paragraph))
1117 ;; Is this the start of a footnote?
1118 (when org-export-with-footnotes
1119 (when (and (boundp 'footnote-section-tag-regexp
)
1120 (string-match (concat "^" footnote-section-tag-regexp
)
1123 (throw 'nextline nil
))
1124 (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line
)
1125 (org-lparse-end-paragraph)
1126 (setq org-lparse-footnote-number
(match-string 1 line
))
1127 (setq line
(replace-match "" t t line
))
1128 (org-lparse-begin-footnote-definition org-lparse-footnote-number
)))
1129 ;; Check if the line break needs to be conserved
1131 ((string-match "\\\\\\\\[ \t]*$" line
)
1132 (setq line
(replace-match
1133 (org-lparse-format 'LINE-BREAK
)
1135 (org-export-preserve-breaks
1136 (setq line
(concat line
(org-lparse-format 'LINE-BREAK
)))))
1138 ;; Check if a paragraph should be started
1140 (while (and org-lparse-par-open
1141 (string-match "\\\\par\\>" line start
))
1143 ;; Leave a space in the </p> so that the footnote matcher
1144 ;; does not see this.
1145 (if (not (get-text-property (match-beginning 0)
1146 'org-protected line
))
1147 (setq line
(replace-match "</p ><p >" t t line
)))
1148 (setq start
(match-end 0))))
1150 (org-lparse-insert 'LINE line
)))))
1152 ;; Properly close all local lists and other lists
1153 (when (org-lparse-current-environment-p 'quote
)
1154 (org-lparse-end-environment 'quote
))
1156 (org-lparse-end-level 1 umax
)
1158 ;; the </div> to close the last text-... div.
1159 (when (and (> umax
0) org-lparse-dyn-first-heading-pos
)
1160 (org-lparse-end-outline-text-or-outline))
1162 (org-lparse-end 'DOCUMENT-BODY opt-plist
)
1164 (org-lparse-end 'DOCUMENT-CONTENT
))
1166 (unless (plist-get opt-plist
:buffer-will-be-killed
)
1169 (org-lparse-end 'EXPORT
)
1171 ;; kill collection buffer
1172 (when org-lparse-collect-buffer
1173 (kill-buffer org-lparse-collect-buffer
))
1175 (goto-char (point-min))
1176 (or (org-export-push-to-kill-ring
1177 (upcase (symbol-name org-lparse-backend
)))
1178 (message "Exporting... done"))
1182 (let ((f (org-lparse-get 'SAVE-METHOD
)))
1183 (or (and f
(functionp f
) (funcall f filename opt-plist
))
1185 (or (and (boundp 'org-lparse-other-backend
)
1186 org-lparse-other-backend
1187 (not (equal org-lparse-backend org-lparse-other-backend
))
1188 (org-lparse-do-convert
1189 buffer-file-name
(symbol-name org-lparse-other-backend
)))
1191 ((eq to-buffer
'string
)
1192 (prog1 (buffer-substring (point-min) (point-max))
1193 (kill-buffer (current-buffer))))
1194 (t (current-buffer))))))
1196 (defun org-lparse-format-table (lines olines
)
1197 "Retuns backend-specific code for org-type and table-type tables."
1199 (setq lines
(org-split-string lines
"\n")))
1200 (if (string-match "^[ \t]*|" (car lines
))
1201 ;; A normal org table
1202 (org-lparse-format-org-table lines nil
)
1203 ;; Table made by table.el
1204 (or (org-lparse-format-table-table-using-table-generate-source
1205 ;; FIXME: Need to take care of this during merge
1206 (if (eq org-lparse-backend
'xhtml
) 'html org-lparse-backend
)
1208 (not org-export-prefer-native-exporter-for-tables
))
1209 ;; We are here only when table.el table has NO col or row
1210 ;; spanning and the user prefers using org's own converter for
1211 ;; exporting of such simple table.el tables.
1212 (org-lparse-format-table-table lines
))))
1214 (defun org-lparse-table-get-colalign-info (lines)
1215 (let ((col-cookies (org-find-text-property-in-string
1216 'org-col-cookies
(car lines
))))
1217 (when (and col-cookies org-table-clean-did-remove-column
)
1219 (mapcar (lambda (x) (cons (1- (car x
)) (cdr x
))) col-cookies
)))
1222 (defvar org-lparse-table-style
)
1223 (defvar org-lparse-table-ncols
)
1224 (defvar org-lparse-table-rownum
)
1225 (defvar org-lparse-table-is-styled
)
1226 (defvar org-lparse-table-begin-marker
)
1227 (defvar org-lparse-table-num-numeric-items-per-column
)
1228 (defvar org-lparse-table-colalign-info
)
1229 (defvar org-lparse-table-colalign-vector
)
1231 ;; Following variables are defined in org-table.el
1232 (defvar org-table-number-fraction
)
1233 (defvar org-table-number-regexp
)
1234 (defun org-lparse-org-table-to-list-table (lines &optional splice
)
1235 "Convert org-table to list-table.
1236 LINES is a list of the form (ROW1 ROW2 ROW3 ...) where each
1237 element is a `string' representing a single row of org-table.
1238 Thus each ROW has vertical separators \"|\" separating the table
1239 fields. A ROW could also be a row-group separator of the form
1240 \"|---...|\". Return a list of the form (ROW1 ROW2 ROW3
1241 ...). ROW could either be symbol `:hrule' or a list of the
1242 form (FIELD1 FIELD2 FIELD3 ...) as appropriate."
1246 (while (setq line
(pop lines
))
1247 (unless (string-match "^[ \t]*|-" line
)
1248 (push (org-split-string line
"[ \t]*|[ \t]*") lines-1
))))
1250 (while (setq line
(pop lines
))
1252 ((string-match "^[ \t]*|-" line
)
1254 (push :hrule lines-1
)))
1256 (push (org-split-string line
"[ \t]*|[ \t]*") lines-1
))))))
1257 (nreverse lines-1
)))
1259 (defun org-lparse-insert-org-table (lines &optional splice
)
1260 "Format a org-type table into backend-specific code.
1261 LINES is a list of lines. Optional argument SPLICE means, do not
1262 insert header and surrounding <table> tags, just format the lines.
1263 Optional argument NO-CSS means use XHTML attributes instead of CSS
1264 for formatting. This is required for the DocBook exporter."
1265 (require 'org-table
)
1266 ;; Get rid of hlines at beginning and end
1267 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
1268 (setq lines
(nreverse lines
))
1269 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
1270 (setq lines
(nreverse lines
))
1271 (when org-export-table-remove-special-lines
1272 ;; Check if the table has a marking column. If yes remove the
1273 ;; column and the special lines
1274 (setq lines
(org-table-clean-before-export lines
)))
1275 (let* ((caption (org-find-text-property-in-string 'org-caption
(car lines
)))
1276 (caption (and caption
(org-xml-encode-org-text caption
)))
1277 (label (org-find-text-property-in-string 'org-label
(car lines
)))
1278 (org-lparse-table-colalign-info (org-lparse-table-get-colalign-info lines
))
1279 (attributes (org-find-text-property-in-string 'org-attributes
1281 (head (and org-export-highlight-first-table-line
1283 (lambda (x) (string-match "^[ \t]*|-" x
))
1285 (setq lines
(org-lparse-org-table-to-list-table lines splice
))
1286 (org-lparse-insert-list-table
1287 lines splice caption label attributes head org-lparse-table-colalign-info
)))
1289 (defun org-lparse-insert-list-table (lines &optional splice
1290 caption label attributes head
1291 org-lparse-table-colalign-info
)
1292 (or (featurep 'org-table
) ; required for
1293 (require 'org-table
)) ; `org-table-number-regexp'
1294 (let* ((org-lparse-table-rownum -
1) org-lparse-table-ncols i
(cnt 0)
1296 org-lparse-table-cur-rowgrp-is-hdr
1297 org-lparse-table-rowgrp-open
1298 org-lparse-table-num-numeric-items-per-column
1299 org-lparse-table-colalign-vector n
1300 org-lparse-table-rowgrp-info
1301 org-lparse-table-begin-marker
1302 (org-lparse-table-style 'org-table
)
1303 org-lparse-table-is-styled
)
1306 (setq org-lparse-table-is-styled nil
)
1307 (while (setq line
(pop lines
))
1308 (insert (org-lparse-format-table-row line
) "\n")))
1310 (setq org-lparse-table-is-styled t
)
1311 (org-lparse-begin 'TABLE caption label attributes
)
1312 (setq org-lparse-table-begin-marker
(point))
1313 (org-lparse-begin-table-rowgroup head
)
1314 (while (setq line
(pop lines
))
1316 ((equal line
:hrule
)
1317 (org-lparse-begin-table-rowgroup))
1319 (insert (org-lparse-format-table-row line
) "\n"))))
1320 (org-lparse-end 'TABLE-ROWGROUP
)
1321 (org-lparse-end-table)))))
1323 (defun org-lparse-format-org-table (lines &optional splice
)
1325 (org-lparse-insert-org-table lines splice
)
1326 (buffer-substring-no-properties (point-min) (point-max))))
1328 (defun org-lparse-format-list-table (lines &optional splice
)
1330 (org-lparse-insert-list-table lines splice
)
1331 (buffer-substring-no-properties (point-min) (point-max))))
1333 (defun org-lparse-insert-table-table (lines)
1334 "Format a table generated by table.el into backend-specific code.
1335 This conversion does *not* use `table-generate-source' from table.el.
1336 This has the advantage that Org-mode's HTML conversions can be used.
1337 But it has the disadvantage, that no cell- or row-spanning is allowed."
1338 (let (line field-buffer
1339 (org-lparse-table-cur-rowgrp-is-hdr
1340 org-export-highlight-first-table-line
)
1344 (org-lparse-table-style 'table-table
)
1345 (org-lparse-table-is-styled nil
)
1346 fields org-lparse-table-ncols i
(org-lparse-table-rownum -
1)
1347 (empty (org-lparse-format 'SPACES
1)))
1348 (org-lparse-begin 'TABLE caption label attributes
)
1349 (while (setq line
(pop lines
))
1351 ((string-match "^[ \t]*\\+-" line
)
1353 (let ((org-export-table-row-tags '("<tr>" .
"</tr>"))
1354 ;; (org-export-html-table-use-header-tags-for-first-column nil)
1356 (insert (org-lparse-format-table-row field-buffer empty
)))
1357 (setq org-lparse-table-cur-rowgrp-is-hdr nil
)
1358 (setq field-buffer nil
)))
1360 ;; Break the line into fields and store the fields
1361 (setq fields
(org-split-string line
"[ \t]*|[ \t]*"))
1363 (setq field-buffer
(mapcar
1365 (concat x
(org-lparse-format 'LINE-BREAK
)
1368 (setq field-buffer fields
)))))
1369 (org-lparse-end-table)))
1371 (defun org-lparse-format-table-table (lines)
1373 (org-lparse-insert-table-table lines
)
1374 (buffer-substring-no-properties (point-min) (point-max))))
1376 (defvar table-source-languages
) ; defined in table.el
1377 (defun org-lparse-format-table-table-using-table-generate-source (backend
1381 "Format a table into BACKEND, using `table-generate-source' from table.el.
1382 Use SPANNED-ONLY to suppress exporting of simple table.el tables.
1384 When SPANNED-ONLY is nil, all table.el tables are exported. When
1385 SPANNED-ONLY is non-nil, only tables with either row or column
1388 This routine returns the generated source or nil as appropriate.
1390 Refer docstring of `org-export-prefer-native-exporter-for-tables'
1391 for further information."
1393 (with-current-buffer (get-buffer-create " org-tmp1 ")
1395 (insert (mapconcat 'identity lines
"\n"))
1396 (goto-char (point-min))
1397 (if (not (re-search-forward "|[^+]" nil t
))
1398 (error "Error processing table"))
1399 (table-recognize-table)
1400 (when (or (not spanned-only
)
1401 (let* ((dim (table-query-dimension))
1402 (c (nth 4 dim
)) (r (nth 5 dim
)) (cells (nth 6 dim
)))
1403 (not (= (* c r
) cells
))))
1404 (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
1406 ((member backend table-source-languages
)
1407 (table-generate-source backend
" org-tmp2 ")
1408 (set-buffer " org-tmp2 ")
1409 (buffer-substring (point-min) (point-max)))
1411 ;; table.el doesn't support the given backend. Currently this
1412 ;; happens in case of odt export. Strip the table from the
1413 ;; generated document. A better alternative would be to embed
1414 ;; the table as ascii text in the output document.
1417 "Found table.el-type table in the source org file. "
1418 (format "table.el doesn't support %s backend. "
1419 (upcase (symbol-name backend
)))
1420 "Skipping ahead ..."))
1423 (defun org-lparse-handle-time-stamps (s)
1424 "Format time stamps in string S, or remove them."
1427 (when org-maybe-keyword-time-regexp
1428 (while (string-match org-maybe-keyword-time-regexp s
)
1429 (or b
(setq b
(substring s
0 (match-beginning 0))))
1431 r
(substring s
0 (match-beginning 0)) " "
1438 (match-string 1 s
) "timestamp-kwd"))
1442 (substring (org-translate-time (match-string 3 s
)) 1 -
1)
1444 "timestamp-wrapper"))
1445 s
(substring s
(match-end 0)))))
1447 ;; Line break if line started and ended with time stamp stuff
1450 (setq r
(concat r s
))
1451 (unless (string-match "\\S-" (concat b s
))
1452 (setq r
(concat r
(org-lparse-format 'LINE-BREAK
))))
1455 (defun org-xml-encode-plain-text (s)
1456 "Convert plain text characters to HTML equivalent.
1457 Possible conversions are set in `org-export-html-protect-char-alist'."
1458 (let ((cl (org-lparse-get 'PLAIN-TEXT-MAP
)) c
)
1459 (while (setq c
(pop cl
))
1461 (while (string-match (car c
) s start
)
1462 (setq s
(replace-match (cdr c
) t t s
)
1463 start
(1+ (match-beginning 0))))))
1466 (defun org-xml-encode-org-text-skip-links (string)
1467 "Prepare STRING for HTML export. Apply all active conversions.
1468 If there are links in the string, don't modify these. If STRING
1469 is nil, return nil."
1471 (let* ((re (concat org-bracket-link-regexp
"\\|"
1472 (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")))
1474 (while (setq m
(string-match re string
))
1475 (setq s
(substring string
0 m
)
1476 l
(match-string 0 string
)
1477 string
(substring string
(match-end 0)))
1478 (push (org-xml-encode-org-text s
) res
)
1480 (push (org-xml-encode-org-text string
) res
)
1481 (apply 'concat
(nreverse res
)))))
1483 (defun org-xml-encode-org-text (s)
1484 "Apply all active conversions to translate special ASCII to HTML."
1485 (setq s
(org-xml-encode-plain-text s
))
1486 (if org-export-html-expand
1487 (while (string-match "@<\\([^&]*\\)>" s
)
1488 (setq s
(replace-match "<\\1>" t nil s
))))
1489 (if org-export-with-emphasize
1490 (setq s
(org-lparse-apply-char-styles s
)))
1491 (if org-export-with-special-strings
1492 (setq s
(org-lparse-convert-special-strings s
)))
1493 (if org-export-with-sub-superscripts
1494 (setq s
(org-lparse-apply-sub-superscript-styles s
)))
1495 (if org-export-with-TeX-macros
1496 (let ((start 0) wd rep
)
1497 (while (setq start
(string-match "\\\\\\([a-zA-Z]+[0-9]*\\)\\({}\\)?"
1499 (if (get-text-property (match-beginning 0) 'org-protected s
)
1500 (setq start
(match-end 0))
1501 (setq wd
(match-string 1 s
))
1502 (if (setq rep
(org-lparse-format 'ORG-ENTITY wd
))
1503 (setq s
(replace-match rep t t s
))
1504 (setq start
(+ start
(length wd
))))))))
1507 (defun org-lparse-convert-special-strings (string)
1508 "Convert special characters in STRING to HTML."
1509 (let ((all (org-lparse-get 'SPECIAL-STRING-REGEXPS
))
1511 (while (setq a
(pop all
))
1512 (setq re
(car a
) rpl
(cdr a
) start
0)
1513 (while (string-match re string start
)
1514 (if (get-text-property (match-beginning 0) 'org-protected string
)
1515 (setq start
(match-end 0))
1516 (setq string
(replace-match rpl t nil string
)))))
1519 (defun org-lparse-apply-sub-superscript-styles (string)
1520 "Apply subscript and superscript styles to STRING.
1521 Use `org-export-with-sub-superscripts' to control application of
1522 sub and superscript styles."
1523 (let (key c
(s 0) (requireb (eq org-export-with-sub-superscripts
'{})))
1524 (while (string-match org-match-substring-regexp string s
)
1526 ((and requireb
(match-end 8)) (setq s
(match-end 2)))
1527 ((get-text-property (match-beginning 2) 'org-protected string
)
1528 (setq s
(match-end 2)))
1530 (setq s
(match-end 1)
1531 key
(if (string= (match-string 2 string
) "_")
1532 'subscript
'superscript
)
1533 c
(or (match-string 8 string
)
1534 (match-string 6 string
)
1535 (match-string 5 string
))
1536 string
(replace-match
1537 (concat (match-string 1 string
)
1538 (org-lparse-format 'FONTIFY c key
))
1540 (while (string-match "\\\\\\([_^]\\)" string
)
1541 (setq string
(replace-match (match-string 1 string
) t t string
)))
1544 (defvar org-lparse-char-styles
1551 "Map Org emphasis markers to char styles.
1552 This is an alist where each element is of the
1553 form (ORG-EMPHASIS-CHAR . CHAR-STYLE).")
1555 (defun org-lparse-apply-char-styles (string)
1556 "Apply char styles to STRING.
1557 The variable `org-lparse-char-styles' controls how the Org
1558 emphasis markers are interpreted."
1560 (while (string-match org-emph-re string s
)
1562 (substring string
(match-beginning 3) (1+ (match-beginning 3)))
1563 (substring string
(match-beginning 4) (1+ (match-beginning 4)))))
1564 (setq s
(match-beginning 0)
1567 (match-string 1 string
)
1569 'FONTIFY
(match-string 4 string
)
1570 (nth 1 (assoc (match-string 3 string
)
1571 org-lparse-char-styles
)))
1572 (match-string 5 string
))
1573 string
(replace-match rpl t t string
)
1574 s
(+ s
(- (length rpl
) 2)))
1578 (defun org-lparse-export-list-line (line pos struct prevs
)
1579 "Insert list syntax in export buffer. Return LINE, maybe modified.
1581 POS is the item position or line position the line had before
1582 modifications to buffer. STRUCT is the list structure. PREVS is
1583 the alist of previous items."
1586 ;; Translate type of list containing POS to "d", "o" or
1588 (lambda (pos struct prevs
)
1589 (let ((type (org-list-get-list-type pos struct prevs
)))
1591 ((eq 'ordered type
) "o")
1592 ((eq 'descriptive type
) "d")
1596 ;; Return list of all items and sublists ending at POS, in
1602 (let ((end (nth 6 e
))
1605 ((= end pos
) (push item out
))
1606 ((>= item pos
) (throw 'exit nil
)))))
1609 ;; First close any previous item, or list, ending at POS.
1611 (let* ((lastp (= (org-list-get-last-item e struct prevs
) e
))
1612 (first-item (org-list-get-list-begin e struct prevs
))
1613 (type (funcall get-type first-item struct prevs
)))
1614 (org-lparse-end-paragraph)
1615 ;; Ending for every item
1616 (org-lparse-end-list-item-1 type
)
1617 ;; We're ending last item of the list: end list.
1619 (org-lparse-end-list type
)
1620 (org-lparse-begin-paragraph))))
1621 (funcall get-closings pos
))
1623 ;; At an item: insert appropriate tags in export buffer.
1626 (concat "[ \t]*\\(\\S-+[ \t]*\\)"
1627 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
1628 "\\(?:\\(\\[[ X-]\\]\\)[ \t]+\\)?"
1629 "\\(?:\\(.*\\)[ \t]+::\\(?:[ \t]+\\|$\\)\\)?"
1631 (let* ((checkbox (match-string 3 line
))
1632 (desc-tag (or (match-string 4 line
) "???"))
1633 (body (or (match-string 5 line
) ""))
1634 (list-beg (org-list-get-list-begin pos struct prevs
))
1635 (firstp (= list-beg pos
))
1636 ;; Always refer to first item to determine list type, in
1637 ;; case list is ill-formed.
1638 (type (funcall get-type list-beg struct prevs
))
1639 (counter (let ((count-tmp (org-list-get-counter pos struct
)))
1641 ((not count-tmp
) nil
)
1642 ((string-match "[A-Za-z]" count-tmp
)
1643 (- (string-to-char (upcase count-tmp
)) 64))
1644 ((string-match "[0-9]+" count-tmp
)
1647 (org-lparse-end-paragraph)
1648 (org-lparse-begin-list type
))
1650 (let ((arg (cond ((equal type
"d") desc-tag
)
1651 ((equal type
"o") counter
))))
1652 (org-lparse-begin-list-item type arg
))
1654 ;; If line had a checkbox, some additional modification is required.
1662 ((string-match "X" checkbox
) "X")
1663 ((string-match " " checkbox
)
1664 (org-lparse-format 'SPACES
1))
1670 ;; Return modified line
1672 ;; At a list ender: go to next line (side-effects only).
1673 ((equal "ORG-LIST-END-MARKER" line
) (throw 'nextline nil
))
1674 ;; Not at an item: return line unchanged (side-effects only).
1677 (defun org-lparse-bind-local-variables (opt-plist)
1679 (set (make-local-variable (nth 2 x
))
1680 (plist-get opt-plist
(car x
))))
1681 org-export-plist-vars
))
1683 (defvar org-lparse-table-rowgrp-open
)
1684 (defvar org-lparse-table-cur-rowgrp-is-hdr
)
1685 (defvar org-lparse-footnote-number
)
1686 (defvar org-lparse-footnote-definitions
)
1687 (defvar org-lparse-output-buffer nil
1688 "Buffer to which `org-do-lparse' writes to.
1689 This buffer contains the contents of the to-be-created exported
1692 (defcustom org-lparse-debug nil
1693 "Enable or Disable logging of `org-lparse' callbacks.
1694 The parameters passed to the backend-registered ENTITY-CONTROL
1695 and ENTITY-FORMAT callbacks are logged as comment strings in the
1696 exported buffer. (org-lparse-format 'COMMENT fmt args) is used
1697 for logging. Customize this variable only if you are an expert
1698 user. Valid values of this variable are:
1699 nil : Disable logging
1700 control : Log all invocations of `org-lparse-begin' and
1701 `org-lparse-end' callbacks.
1702 format : Log invocations of `org-lparse-format' callbacks.
1703 t : Log all invocations of `org-lparse-begin', `org-lparse-end'
1704 and `org-lparse-format' callbacks,"
1707 (const :tag
"Disable" nil
)
1708 (const :tag
"Format callbacks" format
)
1709 (const :tag
"Control callbacks" control
)
1710 (const :tag
"Format and Control callbacks" t
)))
1712 (defun org-lparse-begin (entity &rest args
)
1713 "Begin ENTITY in current buffer. ARGS is entity specific.
1714 ENTITY can be one of PARAGRAPH, LIST, LIST-ITEM etc.
1716 Use (org-lparse-begin 'LIST \"o\") to begin a list in current
1719 See `org-xhtml-entity-control-callbacks-alist' for more
1721 (when (and (member org-lparse-debug
'(t control
))
1722 (not (eq entity
'DOCUMENT-CONTENT
)))
1723 (insert (org-lparse-format 'COMMENT
"%s BEGIN %S" entity args
)))
1725 (let ((f (cadr (assoc entity org-lparse-entity-control-callbacks-alist
))))
1726 (unless f
(error "Unknown entity: %s" entity
))
1729 (defun org-lparse-end (entity &rest args
)
1730 "Close ENTITY in current buffer. ARGS is entity specific.
1731 ENTITY can be one of PARAGRAPH, LIST, LIST-ITEM
1734 Use (org-lparse-end 'LIST \"o\") to close a list in current
1737 See `org-xhtml-entity-control-callbacks-alist' for more
1739 (when (and (member org-lparse-debug
'(t control
))
1740 (not (eq entity
'DOCUMENT-CONTENT
)))
1741 (insert (org-lparse-format 'COMMENT
"%s END %S" entity args
)))
1743 (let ((f (caddr (assoc entity org-lparse-entity-control-callbacks-alist
))))
1744 (unless f
(error "Unknown entity: %s" entity
))
1747 (defun org-lparse-begin-paragraph (&optional style
)
1748 "Insert <p>, but first close previous paragraph if any."
1749 (org-lparse-end-paragraph)
1750 (org-lparse-begin 'PARAGRAPH style
)
1751 (setq org-lparse-par-open t
))
1753 (defun org-lparse-end-paragraph ()
1754 "Close paragraph if there is one open."
1755 (when org-lparse-par-open
1756 (org-lparse-end 'PARAGRAPH
)
1757 (setq org-lparse-par-open nil
)))
1759 (defun org-lparse-end-list-item-1 (&optional type
)
1760 "Close <li> if necessary."
1761 (org-lparse-end-paragraph)
1762 (org-lparse-end-list-item (or type
"u")))
1764 (defun org-lparse-preprocess-after-blockquote-hook ()
1765 "Treat `org-lparse-special-blocks' specially."
1766 (goto-char (point-min))
1767 (while (re-search-forward
1768 "^[ \t]*#\\+\\(begin\\|end\\)_\\(\\S-+\\)[ \t]*\\(.*\\)$" nil t
)
1769 (when (member (downcase (match-string 2)) org-lparse-special-blocks
)
1771 (if (equal (downcase (match-string 1)) "begin")
1772 (format "ORG-%s-START %s" (upcase (match-string 2))
1774 (format "ORG-%s-END %s" (upcase (match-string 2))
1775 (match-string 3))) t t
))))
1777 (add-hook 'org-export-preprocess-after-blockquote-hook
1778 'org-lparse-preprocess-after-blockquote-hook
)
1780 (defun org-lparse-strip-experimental-blocks-maybe-hook ()
1781 "Strip \"list-table\" and \"annotation\" blocks.
1782 Stripping happens only when the exported backend is not one of
1783 \"odt\" or \"xhtml\"."
1784 (when (not org-lparse-backend
)
1785 (message "Stripping following blocks - %S" org-lparse-special-blocks
)
1786 (goto-char (point-min))
1787 (let ((case-fold-search t
))
1790 "^[ \t]*#\\+begin_\\(\\S-+\\)\\([ \t]+.*\\)?\n\\([^\000]*?\\)\n[ \t]*#\\+end_\\1\\>.*"
1792 (when (member (match-string 1) org-lparse-special-blocks
)
1793 (replace-match "" t t
))))))
1795 (add-hook 'org-export-preprocess-hook
1796 'org-lparse-strip-experimental-blocks-maybe-hook
)
1798 (defvar org-lparse-list-table-p nil
1799 "Non-nil if `org-do-lparse' is within a list-table.")
1801 (defvar org-lparse-dyn-current-environment nil
)
1802 (defun org-lparse-begin-environment (style &optional env-options-plist
)
1805 (setq org-lparse-list-table-p t
))
1806 (t (setq org-lparse-dyn-current-environment style
)
1807 (org-lparse-begin 'ENVIRONMENT style env-options-plist
))))
1809 (defun org-lparse-end-environment (style &optional env-options-plist
)
1812 (setq org-lparse-list-table-p nil
))
1813 (t (org-lparse-end 'ENVIRONMENT style env-options-plist
)
1814 (setq org-lparse-dyn-current-environment nil
))))
1816 (defun org-lparse-current-environment-p (style)
1817 (eq org-lparse-dyn-current-environment style
))
1819 (defun org-lparse-begin-footnote-definition (n)
1820 (org-lparse-begin-collect)
1821 (setq org-lparse-insert-tag-with-newlines nil
)
1822 (org-lparse-begin 'FOOTNOTE-DEFINITION n
))
1824 (defun org-lparse-end-footnote-definition (n)
1825 (org-lparse-end 'FOOTNOTE-DEFINITION n
)
1826 (setq org-lparse-insert-tag-with-newlines
'both
)
1827 (let ((footnote-def (org-lparse-end-collect)))
1828 (push (cons n footnote-def
) org-lparse-footnote-definitions
)))
1830 (defvar org-lparse-collect-buffer nil
1831 "An auxiliary buffer named \"*Org Lparse Collect*\".
1832 `org-do-lparse' uses this as output buffer while collecting
1833 footnote definitions and table-cell contents of list-tables. See
1834 `org-lparse-begin-collect' and `org-lparse-end-collect'.")
1836 (defvar org-lparse-collect-count nil
1837 "Count number of calls to `org-lparse-begin-collect'.
1838 Use this counter to catch chained collections if they ever
1841 (defun org-lparse-begin-collect ()
1842 "Temporarily switch to `org-lparse-collect-buffer'.
1843 Also erase it's contents."
1844 (unless (zerop org-lparse-collect-count
)
1845 (error "FIXME (org-lparse.el): Encountered chained collections"))
1846 (incf org-lparse-collect-count
)
1847 (unless org-lparse-collect-buffer
1848 (setq org-lparse-collect-buffer
1849 (get-buffer-create "*Org Lparse Collect*")))
1850 (set-buffer org-lparse-collect-buffer
)
1853 (defun org-lparse-end-collect ()
1854 "Switch to `org-lparse-output-buffer'.
1855 Return contents of `org-lparse-collect-buffer' as a `string'."
1856 (assert (> org-lparse-collect-count
0))
1857 (decf org-lparse-collect-count
)
1858 (prog1 (buffer-string)
1860 (set-buffer org-lparse-output-buffer
)))
1862 (defun org-lparse-format (entity &rest args
)
1863 "Format ENTITY in backend-specific way and return it.
1864 ARGS is specific to entity being formatted.
1866 Use (org-lparse-format 'HEADING \"text\" 1) to format text as
1869 See `org-xhtml-entity-format-callbacks-alist' for more information."
1870 (when (and (member org-lparse-debug
'(t format
))
1871 (not (equal entity
'COMMENT
)))
1872 (insert (org-lparse-format 'COMMENT
"%s: %S" entity args
)))
1875 (let ((text (pop args
)))
1876 (apply 'org-lparse-format
'TAGS entity text args
)))
1878 (let ((f (cdr (assoc entity org-lparse-entity-format-callbacks-alist
))))
1879 (unless f
(error "Unknown entity: %s" entity
))
1882 (defun org-lparse-insert (entity &rest args
)
1883 (insert (apply 'org-lparse-format entity args
)))
1885 (defun org-lparse-prepare-toc (lines level-offset opt-plist umax-toc
)
1886 (let* ((quote-re0 (concat "^[ \t]*" org-quote-string
"\\>"))
1887 (org-min-level (org-get-min-level lines level-offset
))
1888 (org-last-level org-min-level
)
1891 (org-lparse-bind-local-variables opt-plist
)
1893 (org-lparse-begin 'TOC
(nth 3 (plist-get opt-plist
:lang-words
)) umax-toc
)
1898 (when (and (string-match org-todo-line-regexp line
)
1899 (not (get-text-property 0 'org-protected line
))
1900 (<= (setq level
(org-tr-level
1901 (- (match-end 1) (match-beginning 1)
1904 (let ((txt (save-match-data
1905 (org-xml-encode-org-text-skip-links
1906 (org-export-cleanup-toc-line
1907 (match-string 3 line
)))))
1909 org-export-mark-todo-in-toc
1910 (or (and (match-beginning 2)
1911 (not (member (match-string 2 line
)
1912 org-done-keywords
)))
1913 (and (= level umax-toc
)
1914 (org-search-todo-below
1915 line lines level
)))))
1917 ;; Check for targets
1918 (while (string-match org-any-target-regexp line
)
1921 (let ((org-lparse-encode-pending t
))
1922 (org-lparse-format 'FONTIFY
1923 (match-string 1 line
) "target"))
1926 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt
)
1927 (setq tags
(match-string 1 txt
)
1928 txt
(replace-match "" t nil txt
)))
1929 (when (string-match quote-re0 txt
)
1930 (setq txt
(replace-match "" t t txt
)))
1931 (while (string-match "<\\(<\\)+\\|>\\(>\\)+" txt
)
1932 (setq txt
(replace-match "" t t txt
)))
1935 (let* ((snumber (org-section-number level
))
1936 (href (replace-regexp-in-string
1937 "\\." "-" (format "sec-%s" snumber
)))
1941 href org-export-preferred-target-alist
))
1943 (href (org-solidify-link-text href
)))
1944 (org-lparse-format 'TOC-ENTRY snumber todo txt tags href
))
1945 level org-last-level
)
1946 (setq org-last-level level
)))
1949 (org-lparse-end 'TOC
)
1950 (setq org-lparse-toc
(buffer-string))))
1953 (defun org-lparse-format-table-row (fields &optional text-for-empty-fields
)
1954 (if org-lparse-table-ncols
1955 ;; second and subsequent rows of the table
1956 (when (and org-lparse-list-table-p
1957 (> (length fields
) org-lparse-table-ncols
))
1958 (error "Table row has %d columns but header row claims %d columns"
1959 (length fields
) org-lparse-table-ncols
))
1960 ;; first row of the table
1961 (setq org-lparse-table-ncols
(length fields
))
1962 (when org-lparse-table-is-styled
1963 (setq org-lparse-table-num-numeric-items-per-column
1964 (make-vector org-lparse-table-ncols
0))
1965 (setq org-lparse-table-colalign-vector
1966 (make-vector org-lparse-table-ncols nil
))
1968 (while (< (incf c
) org-lparse-table-ncols
)
1969 (let* ((col-cookie (cdr (assoc (1+ c
) org-lparse-table-colalign-info
)))
1970 (align (nth 0 col-cookie
)))
1971 (setf (aref org-lparse-table-colalign-vector c
)
1973 ((string= align
"l") "left")
1974 ((string= align
"r") "right")
1975 ((string= align
"c") "center")
1977 (incf org-lparse-table-rownum
)
1983 (when (and (string= x
"") text-for-empty-fields
)
1984 (setq x text-for-empty-fields
))
1986 (let (col-cookie horiz-span
)
1987 (when org-lparse-table-is-styled
1988 (when (and (< i org-lparse-table-ncols
)
1989 (string-match org-table-number-regexp x
))
1990 (incf (aref org-lparse-table-num-numeric-items-per-column i
)))
1991 (setq col-cookie
(cdr (assoc (1+ i
) org-lparse-table-colalign-info
))
1992 horiz-span
(nth 1 col-cookie
)))
1994 'TABLE-CELL x org-lparse-table-rownum i
(or horiz-span
0))))
1997 (defun org-lparse-get (what &optional opt-plist
)
1998 "Query for value of WHAT for the current backend `org-lparse-backend'.
1999 See also `org-lparse-backend-get'."
2000 (if (boundp 'org-lparse-backend
)
2001 (org-lparse-backend-get (symbol-name org-lparse-backend
) what opt-plist
)
2002 (error "org-lparse-backend is not bound yet")))
2004 (defun org-lparse-backend-get (backend what
&optional opt-plist
)
2005 "Query BACKEND for value of WHAT.
2006 Dispatch the call to `org-<backend>-user-get'. If that throws an
2007 error, dispatch the call to `org-<backend>-get'. See
2008 `org-xhtml-get' for all known settings queried for by
2009 `org-lparse' during the course of export."
2010 (assert (stringp backend
) t
)
2011 (unless (org-lparse-backend-is-native-p backend
)
2012 (error "Unknown native backend %s" backend
))
2013 (let ((backend-get-method (intern (format "org-%s-get" backend
)))
2014 (backend-user-get-method (intern (format "org-%s-user-get" backend
))))
2016 ((functionp backend-get-method
)
2018 (funcall backend-user-get-method what opt-plist
)
2019 (error (funcall backend-get-method what opt-plist
))))
2021 (error "Native backend %s doesn't define %s" backend backend-get-method
)))))
2023 (defun org-lparse-insert-tag (tag &rest args
)
2024 (when (member org-lparse-insert-tag-with-newlines
'(lead both
))
2026 (insert (apply 'format tag args
))
2027 (when (member org-lparse-insert-tag-with-newlines
'(trail both
))
2030 (defun org-lparse-get-targets-from-title (title)
2031 (let* ((target (org-get-text-property-any 0 'target title
))
2032 (extra-targets (assoc target org-export-target-aliases
))
2033 (target (or (cdr (assoc target org-export-preferred-target-alist
))
2035 (cons target
(remove target extra-targets
))))
2037 (defun org-lparse-suffix-from-snumber (snumber)
2038 (let* ((snu (replace-regexp-in-string "\\." "-" snumber
))
2039 (href (cdr (assoc (concat "sec-" snu
)
2040 org-export-preferred-target-alist
))))
2041 (org-solidify-link-text (or href snu
))))
2043 (defun org-lparse-begin-level (level title umax head-count
)
2044 "Insert a new LEVEL in HTML export.
2045 When TITLE is nil, just close all open levels."
2046 (org-lparse-end-level level umax
)
2047 (unless title
(error "Why is heading nil"))
2048 (let* ((targets (org-lparse-get-targets-from-title title
))
2049 (target (car targets
)) (extra-targets (cdr targets
))
2050 (target (and target
(org-solidify-link-text target
)))
2051 (extra-class (org-get-text-property-any 0 'html-container-class title
))
2052 snumber tags level1 class
)
2053 (when (string-match (org-re "\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") title
)
2054 (setq tags
(and org-export-with-tags
(match-string 1 title
)))
2055 (setq title
(replace-match "" t t title
)))
2058 (if (aref org-levels-open
(1- level
))
2059 (org-lparse-end-list-item-1)
2060 (aset org-levels-open
(1- level
) t
)
2061 (org-lparse-end-paragraph)
2062 (org-lparse-begin-list 'unordered
))
2063 (org-lparse-begin-list-item
2064 'unordered target
(org-lparse-format
2065 'HEADLINE title extra-targets tags
)))
2066 (aset org-levels-open
(1- level
) t
)
2067 (setq snumber
(org-section-number level
))
2068 (setq level1
(+ level
(or (org-lparse-get 'TOPLEVEL-HLEVEL
) 1) -
1))
2069 (unless (= head-count
1)
2070 (org-lparse-end-outline-text-or-outline))
2071 (org-lparse-begin-outline-and-outline-text
2072 level1 snumber title tags target extra-targets extra-class
)
2073 (org-lparse-begin-paragraph))))
2075 (defun org-lparse-end-level (level umax
)
2076 (org-lparse-end-paragraph)
2077 (loop for l from org-level-max downto level
2078 do
(when (aref org-levels-open
(1- l
))
2079 ;; Terminate one level in HTML export
2081 (org-lparse-end-outline-text-or-outline)
2082 (org-lparse-end-list-item-1)
2083 (org-lparse-end-list 'unordered
))
2084 (aset org-levels-open
(1- l
) nil
))))
2086 (defvar org-lparse-outline-text-open
)
2087 (defun org-lparse-begin-outline-and-outline-text (level1 snumber title tags
2088 target extra-targets
2091 'OUTLINE level1 snumber title tags target extra-targets extra-class
)
2092 (org-lparse-begin-outline-text level1 snumber extra-class
))
2094 (defun org-lparse-end-outline-text-or-outline ()
2096 (org-lparse-outline-text-open
2097 (org-lparse-end 'OUTLINE-TEXT
)
2098 (setq org-lparse-outline-text-open nil
))
2099 (t (org-lparse-end 'OUTLINE
))))
2101 (defun org-lparse-begin-outline-text (level1 snumber extra-class
)
2102 (assert (not org-lparse-outline-text-open
) t
)
2103 (setq org-lparse-outline-text-open t
)
2104 (org-lparse-begin 'OUTLINE-TEXT level1 snumber extra-class
))
2106 (defun org-lparse-html-list-type-to-canonical-list-type (ltype)
2107 (cdr (assoc ltype
'(("o" . ordered
)
2109 ("d" . description
)))))
2111 ;; following vars are bound during `org-do-lparse'
2112 (defvar org-lparse-list-level
)
2113 (defvar org-lparse-list-item-count
)
2114 (defvar org-lparse-list-stack
)
2115 (defvar org-lparse-list-table
:table-row
)
2116 (defvar org-lparse-list-table
:lines
)
2118 ;; Notes on LIST-TABLES
2119 ;; ====================
2120 ;; Lists withing "list-table" blocks (as shown below)
2122 ;; #+begin_list-table
2133 ;; will be exported as though it were a table as shown below.
2135 ;; | Row 1 | 1.1 | 1.2 | 1.3 |
2136 ;; | Row 2 | 2.1 | 2.2 | 2.3 |
2138 ;; Note that org-tables are NOT multi-line and each line is mapped to
2139 ;; a unique row in the exported document. So if an exported table
2140 ;; needs to contain a single paragraph (with copious text) it needs to
2141 ;; be typed up in a single line. Editing such long lines using the
2142 ;; table editor will be a cumbersome task. Furthermore inclusion of
2143 ;; multi-paragraph text in a table cell is well-nigh impossible.
2145 ;; LIST-TABLEs are meant to circumvent the above problems with
2148 ;; Note that in the example above the list items could be paragraphs
2149 ;; themselves and the list can be arbitrarily deep.
2151 ;; Inspired by following thread:
2152 ;; https://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01101.html
2154 (defun org-lparse-begin-list (ltype)
2155 (incf org-lparse-list-level
)
2156 (push org-lparse-list-item-count org-lparse-list-stack
)
2157 (setq org-lparse-list-item-count
0)
2159 ((not org-lparse-list-table-p
)
2160 (org-lparse-begin 'LIST ltype
))
2161 ;; process LIST-TABLE
2162 ((= 1 org-lparse-list-level
)
2164 (setq org-lparse-list-table
:lines nil
)
2165 (setq org-lparse-list-table
:table-row nil
))
2166 ((= 2 org-lparse-list-level
)
2169 (org-lparse-begin 'LIST ltype
))))
2171 (defun org-lparse-end-list (ltype)
2172 (setq org-lparse-list-item-count
(pop org-lparse-list-stack
))
2173 (decf org-lparse-list-level
)
2175 ((not org-lparse-list-table-p
)
2176 (org-lparse-end 'LIST ltype
))
2177 ;; process LIST-TABLE
2178 ((= 0 org-lparse-list-level
)
2180 (insert (org-lparse-format-list-table
2181 (nreverse org-lparse-list-table
:lines
))))
2182 ((= 1 org-lparse-list-level
)
2185 (org-lparse-end 'LIST ltype
))))
2187 (defun org-lparse-begin-list-item (ltype &optional arg headline
)
2188 (incf org-lparse-list-item-count
)
2190 ((not org-lparse-list-table-p
)
2191 (org-lparse-begin 'LIST-ITEM ltype arg headline
))
2192 ;; process LIST-TABLE
2193 ((and (= 1 org-lparse-list-level
)
2194 (= 1 org-lparse-list-item-count
))
2195 ;; begin TABLE-ROW for LIST-TABLE
2196 (setq org-lparse-list-table
:table-row nil
)
2197 (org-lparse-begin-list-table:table-cell
))
2198 ((and (= 2 org-lparse-list-level
)
2199 (= 1 org-lparse-list-item-count
))
2200 ;; begin TABLE-CELL for LIST-TABLE
2201 (org-lparse-begin-list-table:table-cell
))
2203 (org-lparse-begin 'LIST-ITEM ltype arg headline
))))
2205 (defun org-lparse-end-list-item (ltype)
2206 (decf org-lparse-list-item-count
)
2208 ((not org-lparse-list-table-p
)
2209 (org-lparse-end 'LIST-ITEM ltype
))
2210 ;; process LIST-TABLE
2211 ((and (= 1 org-lparse-list-level
)
2212 (= 0 org-lparse-list-item-count
))
2213 ;; end TABLE-ROW for LIST-TABLE
2214 (org-lparse-end-list-table:table-cell
)
2215 (push (nreverse org-lparse-list-table
:table-row
)
2216 org-lparse-list-table
:lines
))
2217 ((= 2 org-lparse-list-level
)
2218 ;; end TABLE-CELL for LIST-TABLE
2219 (org-lparse-end-list-table:table-cell
))
2221 (org-lparse-end 'LIST-ITEM ltype
))))
2223 (defvar org-lparse-list-table
:table-cell-open
)
2224 (defun org-lparse-begin-list-table:table-cell
()
2225 (org-lparse-end-list-table:table-cell
)
2226 (setq org-lparse-list-table
:table-cell-open t
)
2227 (org-lparse-begin-collect)
2228 (org-lparse-begin-paragraph))
2230 (defun org-lparse-end-list-table:table-cell
()
2231 (when org-lparse-list-table
:table-cell-open
2232 (setq org-lparse-list-table
:table-cell-open nil
)
2233 (org-lparse-end-paragraph)
2234 (push (org-lparse-end-collect)
2235 org-lparse-list-table
:table-row
)))
2237 (defvar org-lparse-table-rowgrp-info
)
2238 (defun org-lparse-begin-table-rowgroup (&optional is-header-row
)
2239 (push (cons (1+ org-lparse-table-rownum
) :start
) org-lparse-table-rowgrp-info
)
2240 (org-lparse-begin 'TABLE-ROWGROUP is-header-row
))
2242 (defun org-lparse-end-table ()
2243 (when org-lparse-table-is-styled
2245 (unless (car org-table-colgroup-info
)
2246 (setq org-table-colgroup-info
2247 (cons :start
(cdr org-table-colgroup-info
))))
2254 (setf (aref org-lparse-table-colalign-vector c
)
2255 (or (aref org-lparse-table-colalign-vector c
)
2256 (if (> (/ (float x
) (1+ org-lparse-table-rownum
))
2257 org-table-number-fraction
)
2259 org-lparse-table-num-numeric-items-per-column
)))
2260 (org-lparse-end 'TABLE
))
2262 (defvar org-lparse-encode-pending nil
)
2264 (defun org-lparse-format-tags (tag text prefix suffix
&rest args
)
2267 (concat prefix
(apply 'format
(car tag
) args
) text suffix
2268 (format (cdr tag
))))
2269 ((stringp tag
) ; singleton tag
2270 (concat prefix
(apply 'format tag args
) text
))))
2272 (defun org-xml-fix-class-name (kwd) ; audit callers of this function
2273 "Turn todo keyword into a valid class name.
2274 Replaces invalid characters with \"_\"."
2276 (while (string-match "[^a-zA-Z0-9_]" kwd
)
2277 (setq kwd
(replace-match "_" t t kwd
))))
2280 (defun org-lparse-format-todo (todo)
2281 (org-lparse-format 'FONTIFY
2283 (ignore-errors (org-lparse-get 'TODO-KWD-CLASS-PREFIX
))
2284 (org-xml-fix-class-name todo
))
2285 (list (if (member todo org-done-keywords
) "done" "todo")
2288 (defun org-lparse-format-extra-targets (extra-targets)
2289 (if (not extra-targets
) ""
2290 (mapconcat (lambda (x)
2291 (setq x
(org-solidify-link-text
2292 (if (org-uuidgen-p x
) (concat "ID-" x
) x
)))
2293 (org-lparse-format 'ANCHOR
"" x
))
2296 (defun org-lparse-format-org-tags (tags)
2304 (ignore-errors (org-lparse-get 'TAG-CLASS-PREFIX
))
2305 (org-xml-fix-class-name x
))))
2306 (org-split-string tags
":")
2307 (org-lparse-format 'SPACES
1)) "tag")))
2309 (defun org-lparse-format-section-number (&optional snumber level
)
2310 (and org-export-with-section-numbers
2311 (not org-lparse-body-only
) snumber level
2312 (org-lparse-format 'FONTIFY snumber
(format "section-number-%d" level
))))
2314 (defun org-lparse-warn (msg)
2315 (if (not org-lparse-use-flashy-warning
)
2317 (put-text-property 0 (length msg
) 'face
'font-lock-warning-face msg
)
2321 (defun org-xml-format-href (s)
2322 "Make sure the S is valid as a href reference in an XHTML document."
2325 (while (string-match "&" s start
)
2326 (setq start
(+ (match-beginning 0) 3)
2327 s
(replace-match "&" t t s
)))))
2330 (defun org-xml-format-desc (s)
2331 "Make sure the S is valid as a description in a link."
2332 (if (and s
(not (get-text-property 1 'org-protected s
)))
2334 (org-xml-encode-org-text s
))
2337 (provide 'org-lparse
)
2339 ;;; org-lparse.el ends here