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
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.
57 (defun org-lparse-and-open (target-backend native-backend arg
58 &optional file-or-buf
)
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 (let (f (file-or-buf (or file-or-buf
65 (org-lparse target-backend native-backend
69 ((bufferp file-or-buf
) buffer-file-name
)
70 ((file-exists-p file-or-buf
) file-or-buf
)
71 (t (error "org-lparse-and-open: This shouldn't happen"))))
72 (message "Opening file %s" f
)
74 (when org-export-kill-product-buffer-when-displayed
75 (kill-buffer (current-buffer))))))
78 (defun org-lparse-batch (target-backend &optional native-backend
)
79 "Call the function `org-lparse'.
80 This function can be used in batch processing as:
82 --load=$HOME/lib/emacs/org.el
83 --eval \"(setq org-export-headline-levels 2)\"
84 --visit=MyFile --funcall org-lparse-batch"
85 (setq native-backend
(or native-backend target-backend
))
86 (org-lparse target-backend native-backend
87 org-export-headline-levels
'hidden
))
90 (defun org-lparse-to-buffer (backend arg
)
91 "Call `org-lparse' with output to a temporary buffer.
92 No file is created. The prefix ARG is passed through to
94 (let ((tempbuf (format "*Org %s Export*" (upcase backend
))))
95 (org-lparse backend backend arg nil nil tempbuf
)
96 (when org-export-show-temporary-export-buffer
97 (switch-to-buffer-other-window tempbuf
))))
100 (defun org-replace-region-by (backend beg end
)
101 "Assume the current region has org-mode syntax, and convert it to HTML.
102 This can be used in any buffer. For example, you could write an
103 itemized list in org-mode syntax in an HTML buffer and then use
104 this command to convert it."
105 (let (reg backend-string buf pop-up-frames
)
106 (save-window-excursion
107 (if (eq major-mode
'org-mode
)
108 (setq backend-string
(org-lparse-region backend beg end t
'string
))
109 (setq reg
(buffer-substring beg end
)
110 buf
(get-buffer-create "*Org tmp*"))
111 (with-current-buffer buf
115 (setq backend-string
(org-lparse-region backend
(point-min)
116 (point-max) t
'string
)))
118 (delete-region beg end
)
119 (insert backend-string
)))
122 (defun org-lparse-region (backend beg end
&optional body-only buffer
)
123 "Convert region from BEG to END in org-mode buffer to HTML.
124 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
125 contents, and only produce the region of converted text, useful for
126 cut-and-paste operations.
127 If BUFFER is a buffer or a string, use/create that buffer as a target
128 of the converted HTML. If BUFFER is the symbol `string', return the
129 produced HTML as a string and leave not buffer behind. For example,
130 a Lisp program could call this function in the following way:
132 (setq html (org-lparse-region \"html\" beg end t 'string))
134 When called interactively, the output buffer is selected, and shown
135 in a window. A non-interactive call will only return the buffer."
136 (let ((transient-mark-mode t
) (zmacs-regions t
)
138 (setq ext-plist
(plist-put ext-plist
:ignore-subtree-p t
))
140 (set-mark (point)) ;; to activate the region
142 (setq rtn
(org-lparse backend backend nil nil ext-plist buffer body-only
))
143 (if (fboundp 'deactivate-mark
) (deactivate-mark))
144 (if (and (org-called-interactively-p 'any
) (bufferp rtn
))
145 (switch-to-buffer-other-window rtn
)
148 (defvar org-lparse-par-open nil
)
150 (defun org-lparse-should-inline-p (filename descp
)
151 "Return non-nil if link FILENAME should be inlined.
152 The decision to inline the FILENAME link is based on the current
153 settings. DESCP is the boolean of whether there was a link
154 description. See variables `org-export-html-inline-images' and
155 `org-export-html-inline-image-extensions'."
156 (let ((inline-images (org-lparse-get 'INLINE-IMAGES
))
157 (inline-image-extensions
158 (org-lparse-get 'INLINE-IMAGE-EXTENSIONS
)))
159 (and (or (eq t inline-images
) (and inline-images
(not descp
)))
160 (org-file-image-p filename inline-image-extensions
))))
162 (defun org-lparse-format-org-link (line opt-plist
)
163 "Return LINE with markup of Org mode links.
164 OPT-PLIST is the export options list."
166 (current-dir (if buffer-file-name
167 (file-name-directory buffer-file-name
)
169 (link-validate (plist-get opt-plist
:link-validation-function
))
171 rpl path attr desc descp desc1 desc2 link
172 org-lparse-link-description-is-image
)
173 (while (string-match org-bracket-link-analytic-regexp
++ line start
)
174 (setq org-lparse-link-description-is-image nil
)
175 (setq start
(match-beginning 0))
176 (setq path
(save-match-data (org-link-unescape
177 (match-string 3 line
))))
179 ((match-end 2) (match-string 2 line
))
181 (or (file-name-absolute-p path
)
182 (string-match "^\\.\\.?/" path
)))
185 (setq path
(org-extract-attributes path
))
186 (setq attr
(get-text-property 0 'org-attributes path
))
187 (setq desc1
(if (match-end 5) (match-string 5 line
))
188 desc2
(if (match-end 2) (concat type
":" path
) path
)
189 descp
(and desc1
(not (equal desc1 desc2
)))
190 desc
(or desc1 desc2
))
191 ;; Make an image out of the description if that is so wanted
192 (when (and descp
(org-file-image-p
193 desc
(org-lparse-get 'INLINE-IMAGE-EXTENSIONS
)))
194 (setq org-lparse-link-description-is-image t
)
196 (if (string-match "^file:" desc
)
197 (setq desc
(substring desc
(match-end 0)))))
199 (setq desc
(org-add-props
200 (org-lparse-format 'INLINE-IMAGE desc
)
201 '(org-protected t
)))))
203 ((equal type
"internal")
206 (if (= (string-to-char path
) ?
#)
211 'ORG-LINK opt-plist
"" "" (org-solidify-link-text
213 (org-link-unescape frag-0
))
214 nil
) desc attr descp
))))
215 ((and (equal type
"id")
216 (setq id-file
(org-id-find-id-file path
)))
217 ;; This is an id: link to another file (if it was the same file,
218 ;; it would have become an internal link...)
220 (setq id-file
(file-relative-name
222 (file-name-directory org-current-export-file
)))
225 'ORG-LINK opt-plist type id-file
226 (concat (if (org-uuidgen-p path
) "ID-") path
)
228 ((member type
'("http" "https"))
229 ;; standard URL, can inline as image
232 'ORG-LINK opt-plist type path nil desc attr descp
)))
233 ((member type
'("ftp" "mailto" "news"))
234 ;; standard URL, can't inline as image
237 'ORG-LINK opt-plist type path nil desc attr descp
)))
239 ((string= type
"coderef")
240 (setq rpl
(org-lparse-format
241 'ORG-LINK opt-plist type
"" path desc nil descp
)))
243 ((functionp (setq fnc
(nth 2 (assoc type org-link-protocols
))))
244 ;; The link protocol has a function for format the link
245 (setq rpl
(save-match-data
246 (funcall fnc
(org-link-unescape path
)
247 desc1
(case org-lparse-backend
249 (t org-lparse-backend
))))))
250 ((string= type
"file")
256 (string-match "::\\(.*\\)" path
)
258 (replace-match "" t nil path
)
259 (match-string 1 path
))
262 ;;The proper path, without a fragment
270 ;;Check the fragment. If it can't be used as
271 ;;target fragment we'll pass nil instead.
275 (not (string-match "^[0-9]*$" fragment-0
))
276 (not (string-match "^\\*" fragment-0
))
277 (not (string-match "^/.*/$" fragment-0
)))
278 (org-solidify-link-text
279 (org-link-unescape fragment-0
))
282 ;;Description minus "file:" and ".org"
283 (if (string-match "^file:" desc
)
285 ((desc-1 (replace-match "" t t desc
)))
286 (if (string-match "\\.org$" desc-1
)
287 (replace-match "" t t desc-1
)
294 (functionp link-validate
)
295 (not (funcall link-validate path-1 current-dir
)))
298 'ORG-LINK opt-plist
"file" path-1 fragment-1
299 desc-2 attr descp
))))))
302 ;; just publish the path, as default
303 (setq rpl
(concat "<i><" type
":"
304 (save-match-data (org-link-unescape path
))
306 (setq line
(replace-match rpl t t line
)
307 start
(+ start
(length rpl
))))
310 (defvar org-lparse-par-open-stashed
) ; bound during `org-do-lparse'
311 (defun org-lparse-stash-save-paragraph-state ()
312 (assert (zerop org-lparse-par-open-stashed
))
313 (setq org-lparse-par-open-stashed org-lparse-par-open
)
314 (setq org-lparse-par-open nil
))
316 (defun org-lparse-stash-pop-paragraph-state ()
317 (setq org-lparse-par-open org-lparse-par-open-stashed
)
318 (setq org-lparse-par-open-stashed
0))
320 (defmacro with-org-lparse-preserve-paragraph-state
(&rest body
)
321 `(let ((org-lparse-do-open-par org-lparse-par-open
))
322 (org-lparse-end-paragraph)
324 (when org-lparse-do-open-par
325 (org-lparse-begin-paragraph))))
326 (def-edebug-spec with-org-lparse-preserve-paragraph-state
(body))
328 (defvar org-lparse-native-backends nil
329 "List of native backends registered with `org-lparse'.
330 A backend can use `org-lparse-register-backend' to add itself to
333 All native backends must implement a get routine and a mandatory
334 set of callback routines.
336 The get routine must be named as org-<backend>-get where backend
337 is the name of the backend. The exporter uses `org-lparse-get'
338 and retrieves the backend-specific callback by querying for
339 ENTITY-CONTROL and ENTITY-FORMAT variables.
341 For the sake of illustration, the html backend implements
342 `org-xhtml-get'. It returns
343 `org-xhtml-entity-control-callbacks-alist' and
344 `org-xhtml-entity-format-callbacks-alist' as the values of
345 ENTITY-CONTROL and ENTITY-FORMAT settings.")
347 (defun org-lparse-register-backend (backend)
348 "Make BACKEND known to `org-lparse' library.
349 Add BACKEND to `org-lparse-native-backends'."
352 ((symbolp backend
) (symbol-name backend
))
353 ((stringp backend
) backend
)
354 (t (error "Error while registering backend: %S" backend
))))
355 (add-to-list 'org-lparse-native-backends backend
)))
357 (defun org-lparse-unregister-backend (backend)
358 (setq org-lparse-native-backends
360 ((symbolp backend
) (symbol-name backend
))
361 ((stringp backend
) backend
))
362 org-lparse-native-backends
))
363 (message "Unregistered backend %S" backend
))
365 (defun org-lparse-do-reachable-formats (in-fmt)
366 "Return verbose info about formats to which IN-FMT can be converted.
367 Return a list where each element is of the
368 form (CONVERTER-PROCESS . OUTPUT-FMT-ALIST). See
369 `org-export-odt-convert-processes' for CONVERTER-PROCESS and see
370 `org-export-odt-convert-capabilities' for OUTPUT-FMT-ALIST."
371 (let (reachable-formats)
372 (dolist (backend org-lparse-native-backends reachable-formats
)
373 (let* ((converter (org-lparse-backend-get
374 backend
'CONVERT-METHOD
))
375 (capabilities (org-lparse-backend-get
376 backend
'CONVERT-CAPABILITIES
)))
378 (dolist (c capabilities
)
379 (when (member in-fmt
(nth 1 c
))
380 (push (cons converter
(nth 2 c
)) reachable-formats
))))))))
382 (defun org-lparse-reachable-formats (in-fmt)
383 "Return list of formats to which IN-FMT can be converted.
384 The list of the form (OUTPUT-FMT-1 OUTPUT-FMT-2 ...)."
386 (mapc (lambda (e) (add-to-list 'l e
))
387 (apply 'append
(mapcar
388 (lambda (e) (mapcar 'car
(cdr e
)))
389 (org-lparse-do-reachable-formats in-fmt
))))
392 (defun org-lparse-reachable-p (in-fmt out-fmt
)
393 "Return non-nil if IN-FMT can be converted to OUT-FMT."
395 (let ((reachable-formats (org-lparse-do-reachable-formats in-fmt
)))
396 (dolist (e reachable-formats
)
397 (let ((out-fmt-spec (assoc out-fmt
(cdr e
))))
399 (throw 'done
(cons (car e
) out-fmt-spec
))))))))
401 (defun org-lparse-backend-is-native-p (backend)
402 (member backend org-lparse-native-backends
))
404 (defun org-lparse (target-backend native-backend arg
405 &optional hidden ext-plist
406 to-buffer body-only pub-dir
)
407 "Export the outline to various formats.
408 If there is an active region, export only the region. The
409 outline is first exported to NATIVE-BACKEND and optionally
410 converted to TARGET-BACKEND. See `org-lparse-native-backends'
411 for list of known native backends. Each native backend can
412 specify a converter and list of target backends it exports to
413 using the CONVERT-PROCESS and OTHER-BACKENDS settings of it's get
414 method. See `org-xhtml-get' for an illustrative example.
416 ARG is a prefix argument that specifies how many levels of
417 outline should become headlines. The default is 3. Lower levels
418 will become bulleted lists.
420 HIDDEN is obsolete and does nothing.
422 EXT-PLIST is a property list that controls various aspects of
423 export. The settings here override org-mode's default settings
424 and but are inferior to file-local settings.
426 TO-BUFFER dumps the exported lines to a buffer or a string
427 instead of a file. If TO-BUFFER is the symbol `string' return the
428 exported lines as a string. If TO-BUFFER is non-nil, create a
429 buffer with that name and export to that buffer.
431 BODY-ONLY controls the presence of header and footer lines in
432 exported text. If BODY-ONLY is non-nil, don't produce the file
433 header and footer, simply return the content of <body>...</body>,
434 without even the body tags themselves.
436 PUB-DIR specifies the publishing directory."
437 (let* ((org-lparse-backend (intern native-backend
))
438 (org-lparse-other-backend (and target-backend
439 (intern target-backend
))))
440 (unless (org-lparse-backend-is-native-p native-backend
)
441 (error "Don't know how to export natively to backend %s" native-backend
))
443 (unless (or (equal native-backend target-backend
)
444 (org-lparse-reachable-p native-backend target-backend
))
445 (error "Don't know how to export to backend %s %s" target-backend
446 (format "via %s" native-backend
)))
447 (run-hooks 'org-export-first-hook
)
448 (org-do-lparse arg hidden ext-plist to-buffer body-only pub-dir
)))
450 (defcustom org-lparse-use-flashy-warning nil
451 "Control flashing of messages logged with `org-lparse-warn'.
452 When non-nil, messages are fontified with warning face and the
453 exporter lingers for a while to catch user's attention."
457 (defun org-lparse-convert-read-params ()
458 "Return IN-FILE and OUT-FMT params for `org-lparse-do-convert'.
459 This is a helper routine for interactive use."
460 (let* ((input (if (featurep 'ido
) 'ido-completing-read
'completing-read
))
461 (in-file (read-file-name "File to be converted: "
462 nil buffer-file-name t
))
463 (in-fmt (file-name-extension in-file
))
464 (out-fmt-choices (org-lparse-reachable-formats in-fmt
))
466 (or (and out-fmt-choices
467 (funcall input
"Output format: "
468 out-fmt-choices nil nil nil
))
470 "No known converter or no known output formats for %s files"
472 (list in-file out-fmt
)))
474 (defun org-lparse-do-convert (in-file out-fmt
&optional prefix-arg
)
475 "Workhorse routine for `org-export-odt-convert'."
476 (require 'browse-url
)
477 (let* ((in-file (expand-file-name (or in-file buffer-file-name
)))
478 (dummy (or (file-readable-p in-file
)
479 (error "Cannot read %s" in-file
)))
480 (in-fmt (file-name-extension in-file
))
481 (out-fmt (or out-fmt
(error "Output format unspecified")))
482 (how (or (org-lparse-reachable-p in-fmt out-fmt
)
483 (error "Cannot convert from %s format to %s format?"
485 (convert-process (car how
))
486 (program (car convert-process
))
487 (dummy (and (or program
(error "Converter not configured"))
488 (or (executable-find program
)
489 (error "Cannot find converter %s" program
))))
490 (out-file (concat (file-name-sans-extension in-file
) "."
491 (nth 1 (or (cdr how
) out-fmt
))))
492 (out-dir (file-name-directory in-file
))
493 (arglist (mapcar (lambda (arg)
495 arg
`((?i .
,in-file
)
496 (?I .
,(browse-url-file-url in-file
))
499 (?O .
,(browse-url-file-url out-file
))
501 (?D .
,(browse-url-file-url out-dir
)))))
502 (cdr convert-process
))))
503 (when (file-exists-p out-file
)
504 (delete-file out-file
))
506 (message "Executing %s %s" program
(mapconcat 'identity arglist
" "))
507 (apply 'call-process program nil nil nil arglist
)
509 ((file-exists-p out-file
)
510 (message "Exported to %s using %s" out-file program
)
512 (message "Opening %s..." out-file
)
513 (org-open-file out-file
))
515 ;; (set-buffer (find-file-noselect out-file))
518 (message "Export to %s failed" out-file
)
521 (defvar org-lparse-insert-tag-with-newlines
'both
)
523 ;; Following variables are let-bound during `org-lparse'
524 (defvar org-lparse-dyn-first-heading-pos
)
525 (defvar org-lparse-toc
)
526 (defvar org-lparse-entity-control-callbacks-alist
)
527 (defvar org-lparse-entity-format-callbacks-alist
)
528 (defvar org-lparse-backend nil
529 "The native backend to which the document is currently exported.
530 This variable is let bound during `org-lparse'. Valid values are
531 one of the symbols corresponding to `org-lparse-native-backends'.
533 Compare this variable with `org-export-current-backend' which is
534 bound only during `org-export-preprocess-string' stage of the
537 See also `org-lparse-other-backend'.")
539 (defvar org-lparse-other-backend nil
540 "The target backend to which the document is currently exported.
541 This variable is let bound during `org-lparse'. This variable is
542 set to either `org-lparse-backend' or one of the symbols
543 corresponding to OTHER-BACKENDS specification of the
546 For example, if a document is exported to \"odt\" then both
547 org-lparse-backend and org-lparse-other-backend are bound to
548 'odt. On the other hand, if a document is exported to \"odt\"
549 and then converted to \"doc\" then org-lparse-backend is set to
550 'odt and org-lparse-other-backend is set to 'doc.")
552 (defvar org-lparse-body-only nil
553 "Bind this to BODY-ONLY arg of `org-lparse'.")
555 (defvar org-lparse-to-buffer nil
556 "Bind this to TO-BUFFER arg of `org-lparse'.")
558 (defun org-lparse-get-block-params (params)
561 (setq params
(org-trim params
))
562 (unless (string-match "\\`(.*)\\'" params
)
563 (setq params
(format "(%s)" params
)))
564 (ignore-errors (read params
)))))
566 (defvar org-heading-keyword-regexp-format
) ; defined in org.el
567 (defvar org-lparse-special-blocks
'("list-table" "annotation"))
568 (defun org-do-lparse (arg &optional hidden ext-plist
569 to-buffer body-only pub-dir
)
570 "Export the outline to various formats.
571 See `org-lparse' for more information. This function is a
572 html-agnostic version of the `org-export-as-html' function in 7.5
574 ;; Make sure we have a file name when we need it.
575 (when (and (not (or to-buffer body-only
))
576 (not buffer-file-name
))
577 (if (buffer-base-buffer)
578 (org-set-local 'buffer-file-name
579 (with-current-buffer (buffer-base-buffer)
581 (error "Need a file name to be able to export")))
584 (format "Exporting to %s using org-lparse..."
586 (or org-lparse-backend org-lparse-other-backend
)))))
588 (setq-default org-todo-line-regexp org-todo-line-regexp
)
589 (setq-default org-deadline-line-regexp org-deadline-line-regexp
)
590 (setq-default org-done-keywords org-done-keywords
)
591 (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp
)
592 (let* (hfy-user-sheet-assoc ; let `htmlfontify' know that
593 ; we are interested in
595 org-lparse-encode-pending
597 (org-lparse-par-open-stashed 0)
600 (org-lparse-list-level 0) ; list level starts at 1. A
601 ; value of 0 implies we are
602 ; outside of any list
603 (org-lparse-list-item-count 0)
604 org-lparse-list-stack
606 ;; list-table related vars
607 org-lparse-list-table-p
608 org-lparse-list-table
:table-cell-open
609 org-lparse-list-table
:table-row
610 org-lparse-list-table
:lines
612 org-lparse-outline-text-open
613 (org-lparse-latex-fragment-fallback ; currently used only by
615 (or (ignore-errors (org-lparse-get 'LATEX-FRAGMENT-FALLBACK
))
616 (if (and (org-check-external-command "latex" "" t
)
617 (org-check-external-command "dvipng" "" t
))
620 (org-lparse-insert-tag-with-newlines 'both
)
621 (org-lparse-to-buffer to-buffer
)
622 (org-lparse-body-only body-only
)
623 (org-lparse-entity-control-callbacks-alist
624 (org-lparse-get 'ENTITY-CONTROL
))
625 (org-lparse-entity-format-callbacks-alist
626 (org-lparse-get 'ENTITY-FORMAT
))
628 (org-export-process-option-filters
629 (org-combine-plists (org-default-export-plist)
631 (org-infile-export-plist))))
632 (body-only (or body-only
(plist-get opt-plist
:body-only
)))
633 valid org-lparse-dyn-first-heading-pos
634 (odd org-odd-levels-only
)
635 (region-p (org-region-active-p))
636 (rbeg (and region-p
(region-beginning)))
637 (rend (and region-p
(region-end)))
639 (if (plist-get opt-plist
:ignore-subtree-p
)
644 (and (org-at-heading-p)
645 (>= (org-end-of-subtree t t
) rend
))))))
646 (level-offset (if subtree-p
649 (+ (funcall outline-level
)
650 (if org-odd-levels-only
1 0)))
652 (opt-plist (setq org-export-opt-plist
654 (org-export-add-subtree-options opt-plist rbeg
)
656 ;; The following two are dynamically scoped into other
658 (org-current-export-dir
659 (or pub-dir
(org-lparse-get 'EXPORT-DIR opt-plist
)))
660 (org-current-export-file buffer-file-name
)
661 (level 0) (line "") (origline "") txt todo
664 (filename (if to-buffer nil
667 (file-name-sans-extension
669 (org-entry-get (region-beginning)
670 "EXPORT_FILE_NAME" t
))
671 (file-name-nondirectory buffer-file-name
)))
672 "." (org-lparse-get 'FILE-NAME-EXTENSION opt-plist
))
673 (file-name-as-directory
674 (or pub-dir
(org-lparse-get 'EXPORT-DIR opt-plist
))))))
675 (current-dir (if buffer-file-name
676 (file-name-directory buffer-file-name
)
678 (auto-insert nil
) ; Avoid any auto-insert stuff for the new file
679 (buffer (if to-buffer
681 ((eq to-buffer
'string
)
682 (get-buffer-create (org-lparse-get 'EXPORT-BUFFER-NAME
)))
683 (t (get-buffer-create to-buffer
)))
685 (or (let ((f (org-lparse-get 'INIT-METHOD
)))
686 (and f
(functionp f
) (funcall f filename
)))
688 (org-levels-open (make-vector org-level-max nil
))
691 (let* ((val (plist-get opt-plist p
))
692 (val (org-xml-encode-org-text-skip-links val
)))
693 (setq opt-plist
(plist-put opt-plist p val
))))
694 '(:date
:author
:keywords
:description
)))
695 (date (plist-get opt-plist
:date
))
697 ((and date
(string-match "%" date
))
698 (format-time-string date
))
700 (t (format-time-string "%Y-%m-%d %T %Z"))))
701 (dummy (setq opt-plist
(plist-put opt-plist
:effective-date date
)))
702 (title (org-xml-encode-org-text-skip-links
703 (or (and subtree-p
(org-export-get-title-from-subtree))
704 (plist-get opt-plist
:title
)
707 (plist-get opt-plist
:skip-before-1st-heading
))
708 (org-export-grab-title-from-buffer))
709 (and buffer-file-name
710 (file-name-sans-extension
711 (file-name-nondirectory buffer-file-name
)))
713 (dummy (setq opt-plist
(plist-put opt-plist
:title title
)))
714 (html-table-tag (plist-get opt-plist
:html-table-tag
))
715 (quote-re0 (concat "^ *" org-quote-string
"\\( +\\|[ \t]*$\\)"))
716 (quote-re (format org-heading-keyword-regexp-format
718 (org-lparse-dyn-current-environment nil
)
719 ;; Get the language-dependent settings
720 (lang-words (or (assoc (plist-get opt-plist
:language
)
721 org-export-language-setup
)
722 (assoc "en" org-export-language-setup
)))
723 (dummy (setq opt-plist
(plist-put opt-plist
:lang-words lang-words
)))
726 (coding-system-for-write
727 (or (ignore-errors (org-lparse-get 'CODING-SYSTEM-FOR-WRITE
))
728 (and (boundp 'buffer-file-coding-system
)
729 buffer-file-coding-system
)))
730 (save-buffer-coding-system
731 (or (ignore-errors (org-lparse-get 'CODING-SYSTEM-FOR-SAVE
))
732 (and (boundp 'buffer-file-coding-system
)
733 buffer-file-coding-system
)))
736 (if region-p
(region-beginning) (point-min))
737 (if region-p
(region-end) (point-max))))
738 (org-export-have-math nil
)
739 (org-export-footnotes-seen nil
)
740 (org-export-footnotes-data (org-footnote-all-labels 'with-defs
))
741 (org-footnote-insert-pos-for-preprocessor 'point-min
)
742 (org-lparse-opt-plist opt-plist
)
745 (org-export-preprocess-string
748 :for-backend
(if (equal org-lparse-backend
'xhtml
) ; hack
751 :skip-before-1st-heading
752 (plist-get opt-plist
:skip-before-1st-heading
)
753 :drawers
(plist-get opt-plist
:drawers
)
754 :todo-keywords
(plist-get opt-plist
:todo-keywords
)
755 :tasks
(plist-get opt-plist
:tasks
)
756 :tags
(plist-get opt-plist
:tags
)
757 :priority
(plist-get opt-plist
:priority
)
758 :footnotes
(plist-get opt-plist
:footnotes
)
759 :timestamps
(plist-get opt-plist
:timestamps
)
761 (plist-get opt-plist
:archived-trees
)
762 :select-tags
(plist-get opt-plist
:select-tags
)
763 :exclude-tags
(plist-get opt-plist
:exclude-tags
)
765 (plist-get opt-plist
:text
)
767 (plist-get opt-plist
:LaTeX-fragments
))
770 table-buffer table-orig-buffer
772 rpl path attr desc descp desc1 desc2 link
774 footnotes footref-seen
775 org-lparse-output-buffer
776 org-lparse-footnote-definitions
777 org-lparse-footnote-number
779 org-lparse-collect-buffer
780 (org-lparse-collect-count 0) ; things will get haywire if
781 ; collections are chained. Use
782 ; this variable to assert this
788 (let ((inhibit-read-only t
))
790 (remove-text-properties (point-min) (point-max)
791 '(:org-license-to-kill t
))))
793 (message "Exporting...")
794 (org-init-section-numbers)
796 ;; Switch to the output buffer
797 (setq org-lparse-output-buffer buffer
)
798 (set-buffer org-lparse-output-buffer
)
799 (let ((inhibit-read-only t
)) (erase-buffer))
801 (org-install-letbind)
803 (and (fboundp 'set-buffer-file-coding-system
)
804 (set-buffer-file-coding-system coding-system-for-write
))
806 (let ((case-fold-search nil
)
807 (org-odd-levels-only odd
))
808 ;; create local variables for all options, to make sure all called
809 ;; functions get the correct information
811 (set (make-local-variable (nth 2 x
))
812 (plist-get opt-plist
(car x
))))
813 org-export-plist-vars
)
814 (setq umax
(if arg
(prefix-numeric-value arg
)
815 org-export-headline-levels
))
816 (setq umax-toc
(if (integerp org-export-with-toc
)
817 (min org-export-with-toc umax
)
820 (when (and org-export-with-toc
(not body-only
))
821 (setq lines
(org-lparse-prepare-toc
822 lines level-offset opt-plist umax-toc
)))
825 (org-lparse-begin 'DOCUMENT-CONTENT opt-plist
)
826 (org-lparse-begin 'DOCUMENT-BODY opt-plist
))
829 (org-init-section-numbers)
831 (org-lparse-begin-paragraph)
833 (while (setq line
(pop lines
) origline line
)
835 (when (and (org-lparse-current-environment-p 'quote
)
836 (string-match org-outline-regexp-bol line
))
837 (org-lparse-end-environment 'quote
))
839 (when (org-lparse-current-environment-p 'quote
)
840 (org-lparse-insert 'LINE line
)
841 (throw 'nextline nil
))
843 ;; Fixed-width, verbatim lines (examples)
844 (when (and org-export-with-fixed-width
845 (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line
))
846 (when (not (org-lparse-current-environment-p 'fixedwidth
))
847 (org-lparse-begin-environment 'fixedwidth
))
848 (org-lparse-insert 'LINE
(match-string 3 line
))
849 (when (or (not lines
)
850 (not (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)"
852 (org-lparse-end-environment 'fixedwidth
))
853 (throw 'nextline nil
))
855 ;; Notes: The baseline version of org-html.el (git commit
856 ;; 3d802e), while encoutering a *line-long* protected text,
857 ;; does one of the following two things based on the state
858 ;; of the export buffer.
860 ;; 1. If a paragraph element has just been opened and
861 ;; contains only whitespace as content, insert the
862 ;; protected text as part of the previous paragraph.
864 ;; 2. If the paragraph element has already been opened and
865 ;; contains some valid content insert the protected text
866 ;; as part of the current paragraph.
870 ;; Scenario 1 mentioned above kicks in when a block of
871 ;; protected text has to be inserted enbloc. For example,
872 ;; this happens, when inserting an source or example block
873 ;; or preformatted content enclosed in #+backend,
874 ;; #+begin_bakend ... #+end_backend)
876 ;; Scenario 2 mentioned above kicks in when the protected
877 ;; text is part of a running sentence. For example this
878 ;; happens in the case of an *multiline* LaTeX equation that
879 ;; needs to be inserted verbatim.
881 ;; org-html.el in the master branch seems to do some
882 ;; jugglery by moving paragraphs around. Inorder to make
883 ;; these changes backend-agnostic introduce a new text
884 ;; property org-native-text and impose the added semantics
885 ;; that these protected blocks appear outside of a
886 ;; conventional paragraph element.
888 ;; Extra Note: Check whether org-example and org-native-text
889 ;; are entirely equivalent.
891 ;; Fixes bug reported by Christian Moe concerning verbatim
893 ;; on git commit 533ba3f90250a1f25f494c390d639ea6274f235c
894 ;; http://repo.or.cz/w/org-mode/org-jambu.git/shortlog/refs/heads/staging
895 ;; See http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01379.html
898 (when (and (get-text-property 0 'org-native-text line
)
899 ;; Make sure it is the entire line that is protected
900 (not (< (or (next-single-property-change
901 0 'org-native-text line
) 10000)
903 (let ((ind (get-text-property 0 'original-indentation line
)))
904 (org-lparse-begin-environment 'native
)
905 (org-lparse-insert 'LINE line
)
907 (or (= (length (car lines
)) 0)
909 (equal ind
(get-text-property
910 0 'original-indentation
(car lines
))))
911 (or (= (length (car lines
)) 0)
912 (get-text-property 0 'org-native-text
(car lines
))))
913 (org-lparse-insert 'LINE
(pop lines
)))
914 (org-lparse-end-environment 'native
))
915 (throw 'nextline nil
))
918 (when (and (get-text-property 0 'org-protected line
)
919 ;; Make sure it is the entire line that is protected
920 (not (< (or (next-single-property-change
921 0 'org-protected line
) 10000)
923 (let ((ind (get-text-property 0 'original-indentation line
)))
924 (org-lparse-insert 'LINE line
)
926 (or (= (length (car lines
)) 0)
928 (equal ind
(get-text-property
929 0 'original-indentation
(car lines
))))
930 (or (= (length (car lines
)) 0)
931 (get-text-property 0 'org-protected
(car lines
))))
932 (org-lparse-insert 'LINE
(pop lines
))))
933 (throw 'nextline nil
))
935 ;; Blockquotes, verse, and center
937 "^ORG-\\(.+\\)-\\(START\\|END\\)\\([ \t]+.*\\)?$" line
)
938 (let* ((style (intern (downcase (match-string 1 line
))))
939 (env-options-plist (org-lparse-get-block-params
940 (match-string 3 line
)))
941 (f (cdr (assoc (match-string 2 line
)
942 '(("START" . org-lparse-begin-environment
)
943 ("END" . org-lparse-end-environment
))))))
946 '(blockquote verse center
)
947 (mapcar 'intern org-lparse-special-blocks
)))
948 (funcall f style env-options-plist
)
949 (throw 'nextline nil
))))
951 (run-hooks 'org-export-html-after-blockquotes-hook
)
952 (when (org-lparse-current-environment-p 'verse
)
953 (let ((i (org-get-string-indentation line
)))
956 (let ((org-lparse-encode-pending t
))
957 (org-lparse-format 'SPACES
(* 2 i
)))
958 " " (org-trim line
))))
959 (unless (string-match "\\\\\\\\[ \t]*$" line
)
960 (setq line
(concat line
"\\\\")))))
962 ;; make targets to anchors
965 "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line start
)
967 ((get-text-property (match-beginning 1) 'org-protected line
)
968 (setq start
(match-end 1)))
970 (setq line
(replace-match
971 (let ((org-lparse-encode-pending t
))
973 'ANCHOR
"" (org-solidify-link-text
974 (match-string 1 line
))))
976 ((and org-export-with-toc
(equal (string-to-char line
) ?
*))
977 ;; FIXME: NOT DEPENDENT on TOC?????????????????????
978 (setq line
(replace-match
979 (let ((org-lparse-encode-pending t
))
981 'FONTIFY
(match-string 1 line
) "target"))
982 ;; (concat "@<i>" (match-string 1 line) "@</i> ")
985 (setq line
(replace-match
987 (let ((org-lparse-encode-pending t
))
989 'ANCHOR
(match-string 1 line
)
990 (org-solidify-link-text (match-string 1 line
))
994 (let ((org-lparse-encode-pending t
))
995 (setq line
(org-lparse-handle-time-stamps line
)))
997 ;; replace "&" by "&", "<" and ">" by "<" and ">"
998 ;; handle @<..> HTML tags (replace "@>..<" by "<..>")
999 ;; Also handle sub_superscripts and checkboxes
1000 (or (string-match org-table-hline-regexp line
)
1001 (string-match "^[ \t]*\\([+]-\\||[ ]\\)[-+ |]*[+|][ \t]*$" line
)
1002 (setq line
(org-xml-encode-org-text-skip-links line
)))
1004 (setq line
(org-lparse-format-org-link line opt-plist
))
1007 (if (and org-todo-line-regexp
1008 (string-match org-todo-line-regexp line
)
1009 (match-beginning 2))
1011 (substring line
0 (match-beginning 2))
1012 (org-lparse-format 'TODO
(match-string 2 line
))
1013 (substring line
(match-end 2)))))
1015 ;; Does this contain a reference to a footnote?
1016 (when org-export-with-footnotes
1018 (while (string-match "\\([^* \t].*?\\)[ \t]*\\[\\([0-9]+\\)\\]" line start
)
1019 ;; Discard protected matches not clearly identified as
1020 ;; footnote markers.
1021 (if (or (get-text-property (match-beginning 2) 'org-protected line
)
1022 (not (get-text-property (match-beginning 2) 'org-footnote line
)))
1023 (setq start
(match-end 2))
1024 (let ((n (match-string 2 line
)) refcnt a
)
1025 (if (setq a
(assoc n footref-seen
))
1027 (setcdr a
(1+ (cdr a
)))
1028 (setq refcnt
(cdr a
)))
1030 (push (cons n
1) footref-seen
))
1034 (or (match-string 1 line
) "")
1037 n
(cdr (assoc n org-lparse-footnote-definitions
))
1039 ;; If another footnote is following the
1040 ;; current one, add a separator.
1041 (if (save-match-data
1042 (string-match "\\`\\[[0-9]+\\]"
1043 (substring line
(match-end 0))))
1045 (org-lparse-get 'FOOTNOTE-SEPARATOR
))
1050 ((string-match "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$" line
)
1051 ;; This is a headline
1052 (setq level
(org-tr-level (- (match-end 1) (match-beginning 1)
1054 txt
(match-string 2 line
))
1055 (if (string-match quote-re0 txt
)
1056 (setq txt
(replace-match "" t t txt
)))
1057 (if (<= level
(max umax umax-toc
))
1058 (setq head-count
(+ head-count
1)))
1059 (unless org-lparse-dyn-first-heading-pos
1060 (setq org-lparse-dyn-first-heading-pos
(point)))
1061 (org-lparse-begin-level level txt umax head-count
)
1064 (when (string-match quote-re line
)
1065 (org-lparse-begin-environment 'quote
)))
1067 ((and org-export-with-tables
1068 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line
))
1069 (when (not table-open
)
1071 (setq table-open t table-buffer nil table-orig-buffer nil
))
1074 (setq table-buffer
(cons line table-buffer
)
1075 table-orig-buffer
(cons origline table-orig-buffer
))
1076 (when (or (not lines
)
1077 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
1079 (setq table-open nil
1080 table-buffer
(nreverse table-buffer
)
1081 table-orig-buffer
(nreverse table-orig-buffer
))
1082 (org-lparse-end-paragraph)
1083 (org-lparse-insert 'TABLE table-buffer table-orig-buffer
)))
1088 ;; This line either is list item or end a list.
1089 (when (get-text-property 0 'list-item line
)
1090 (setq line
(org-lparse-export-list-line
1092 (get-text-property 0 'list-item line
)
1093 (get-text-property 0 'list-struct line
)
1094 (get-text-property 0 'list-prevs line
))))
1097 (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line
)
1098 (with-org-lparse-preserve-paragraph-state
1099 (org-lparse-insert 'HORIZONTAL-LINE
))
1100 (throw 'nextline nil
))
1102 ;; Empty lines start a new paragraph. If hand-formatted lists
1103 ;; are not fully interpreted, lines starting with "-", "+", "*"
1104 ;; also start a new paragraph.
1105 (when (string-match "^ [-+*]-\\|^[ \t]*$" line
)
1106 (when org-lparse-footnote-number
1107 (org-lparse-end-footnote-definition org-lparse-footnote-number
)
1108 (setq org-lparse-footnote-number nil
))
1109 (org-lparse-begin-paragraph))
1111 ;; Is this the start of a footnote?
1112 (when org-export-with-footnotes
1113 (when (and (boundp 'footnote-section-tag-regexp
)
1114 (string-match (concat "^" footnote-section-tag-regexp
)
1117 (throw 'nextline nil
))
1118 (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line
)
1119 (org-lparse-end-paragraph)
1120 (setq org-lparse-footnote-number
(match-string 1 line
))
1121 (setq line
(replace-match "" t t line
))
1122 (org-lparse-begin-footnote-definition org-lparse-footnote-number
)))
1123 ;; Check if the line break needs to be conserved
1125 ((string-match "\\\\\\\\[ \t]*$" line
)
1126 (setq line
(replace-match
1127 (org-lparse-format 'LINE-BREAK
)
1129 (org-export-preserve-breaks
1130 (setq line
(concat line
(org-lparse-format 'LINE-BREAK
)))))
1132 ;; Check if a paragraph should be started
1134 (while (and org-lparse-par-open
1135 (string-match "\\\\par\\>" line start
))
1137 ;; Leave a space in the </p> so that the footnote matcher
1138 ;; does not see this.
1139 (if (not (get-text-property (match-beginning 0)
1140 'org-protected line
))
1141 (setq line
(replace-match "</p ><p >" t t line
)))
1142 (setq start
(match-end 0))))
1144 (org-lparse-insert 'LINE line
)))))
1146 ;; Properly close all local lists and other lists
1147 (when (org-lparse-current-environment-p 'quote
)
1148 (org-lparse-end-environment 'quote
))
1150 (org-lparse-end-level 1 umax
)
1152 ;; the </div> to close the last text-... div.
1153 (when (and (> umax
0) org-lparse-dyn-first-heading-pos
)
1154 (org-lparse-end-outline-text-or-outline))
1156 (org-lparse-end 'DOCUMENT-BODY opt-plist
)
1158 (org-lparse-end 'DOCUMENT-CONTENT
))
1160 (unless (plist-get opt-plist
:buffer-will-be-killed
)
1163 (org-lparse-end 'EXPORT
)
1165 ;; kill collection buffer
1166 (when org-lparse-collect-buffer
1167 (kill-buffer org-lparse-collect-buffer
))
1169 (goto-char (point-min))
1170 (or (org-export-push-to-kill-ring
1171 (upcase (symbol-name org-lparse-backend
)))
1172 (message "Exporting... done"))
1176 (let ((f (org-lparse-get 'SAVE-METHOD
)))
1177 (or (and f
(functionp f
) (funcall f filename opt-plist
))
1179 (or (and (boundp 'org-lparse-other-backend
)
1180 org-lparse-other-backend
1181 (not (equal org-lparse-backend org-lparse-other-backend
))
1182 (org-lparse-do-convert
1183 buffer-file-name
(symbol-name org-lparse-other-backend
)))
1185 ((eq to-buffer
'string
)
1186 (prog1 (buffer-substring (point-min) (point-max))
1187 (kill-buffer (current-buffer))))
1188 (t (current-buffer))))))
1190 (defun org-lparse-format-table (lines olines
)
1191 "Retuns backend-specific code for org-type and table-type tables."
1193 (setq lines
(org-split-string lines
"\n")))
1194 (if (string-match "^[ \t]*|" (car lines
))
1195 ;; A normal org table
1196 (org-lparse-format-org-table lines nil
)
1197 ;; Table made by table.el
1198 (or (org-lparse-format-table-table-using-table-generate-source
1199 ;; FIXME: Need to take care of this during merge
1200 (if (eq org-lparse-backend
'xhtml
) 'html org-lparse-backend
)
1202 (not org-export-prefer-native-exporter-for-tables
))
1203 ;; We are here only when table.el table has NO col or row
1204 ;; spanning and the user prefers using org's own converter for
1205 ;; exporting of such simple table.el tables.
1206 (org-lparse-format-table-table lines
))))
1208 (defun org-lparse-table-get-colalign-info (lines)
1209 (let ((col-cookies (org-find-text-property-in-string
1210 'org-col-cookies
(car lines
))))
1211 (when (and col-cookies org-table-clean-did-remove-column
)
1213 (mapcar (lambda (x) (cons (1- (car x
)) (cdr x
))) col-cookies
)))
1216 (defvar org-lparse-table-style
)
1217 (defvar org-lparse-table-ncols
)
1218 (defvar org-lparse-table-rownum
)
1219 (defvar org-lparse-table-is-styled
)
1220 (defvar org-lparse-table-begin-marker
)
1221 (defvar org-lparse-table-num-numeric-items-per-column
)
1222 (defvar org-lparse-table-colalign-info
)
1223 (defvar org-lparse-table-colalign-vector
)
1225 ;; Following variables are defined in org-table.el
1226 (defvar org-table-number-fraction
)
1227 (defvar org-table-number-regexp
)
1228 (defun org-lparse-org-table-to-list-table (lines &optional splice
)
1229 "Convert org-table to list-table.
1230 LINES is a list of the form (ROW1 ROW2 ROW3 ...) where each
1231 element is a `string' representing a single row of org-table.
1232 Thus each ROW has vertical separators \"|\" separating the table
1233 fields. A ROW could also be a row-group separator of the form
1234 \"|---...|\". Return a list of the form (ROW1 ROW2 ROW3
1235 ...). ROW could either be symbol `:hrule' or a list of the
1236 form (FIELD1 FIELD2 FIELD3 ...) as appropriate."
1240 (while (setq line
(pop lines
))
1241 (unless (string-match "^[ \t]*|-" line
)
1242 (push (org-split-string line
"[ \t]*|[ \t]*") lines-1
))))
1244 (while (setq line
(pop lines
))
1246 ((string-match "^[ \t]*|-" line
)
1248 (push :hrule lines-1
)))
1250 (push (org-split-string line
"[ \t]*|[ \t]*") lines-1
))))))
1251 (nreverse lines-1
)))
1253 (defun org-lparse-insert-org-table (lines &optional splice
)
1254 "Format a org-type table into backend-specific code.
1255 LINES is a list of lines. Optional argument SPLICE means, do not
1256 insert header and surrounding <table> tags, just format the lines.
1257 Optional argument NO-CSS means use XHTML attributes instead of CSS
1258 for formatting. This is required for the DocBook exporter."
1259 (require 'org-table
)
1260 ;; Get rid of hlines at beginning and end
1261 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
1262 (setq lines
(nreverse lines
))
1263 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
1264 (setq lines
(nreverse lines
))
1265 (when org-export-table-remove-special-lines
1266 ;; Check if the table has a marking column. If yes remove the
1267 ;; column and the special lines
1268 (setq lines
(org-table-clean-before-export lines
)))
1269 (let* ((caption (org-find-text-property-in-string 'org-caption
(car lines
)))
1270 (caption (and caption
(org-xml-encode-org-text caption
)))
1271 (label (org-find-text-property-in-string 'org-label
(car lines
)))
1272 (org-lparse-table-colalign-info (org-lparse-table-get-colalign-info lines
))
1273 (attributes (org-find-text-property-in-string 'org-attributes
1275 (head (and org-export-highlight-first-table-line
1277 (lambda (x) (string-match "^[ \t]*|-" x
))
1279 (setq lines
(org-lparse-org-table-to-list-table lines splice
))
1280 (org-lparse-insert-list-table
1281 lines splice caption label attributes head org-lparse-table-colalign-info
)))
1283 (defun org-lparse-insert-list-table (lines &optional splice
1284 caption label attributes head
1285 org-lparse-table-colalign-info
)
1286 (or (featurep 'org-table
) ; required for
1287 (require 'org-table
)) ; `org-table-number-regexp'
1288 (let* ((org-lparse-table-rownum -
1) org-lparse-table-ncols i
(cnt 0)
1290 org-lparse-table-cur-rowgrp-is-hdr
1291 org-lparse-table-rowgrp-open
1292 org-lparse-table-num-numeric-items-per-column
1293 org-lparse-table-colalign-vector n
1294 org-lparse-table-rowgrp-info
1295 org-lparse-table-begin-marker
1296 (org-lparse-table-style 'org-table
)
1297 org-lparse-table-is-styled
)
1300 (setq org-lparse-table-is-styled nil
)
1301 (while (setq line
(pop lines
))
1302 (insert (org-lparse-format-table-row line
) "\n")))
1304 (setq org-lparse-table-is-styled t
)
1305 (org-lparse-begin 'TABLE caption label attributes
)
1306 (setq org-lparse-table-begin-marker
(point))
1307 (org-lparse-begin-table-rowgroup head
)
1308 (while (setq line
(pop lines
))
1310 ((equal line
:hrule
)
1311 (org-lparse-begin-table-rowgroup))
1313 (insert (org-lparse-format-table-row line
) "\n"))))
1314 (org-lparse-end 'TABLE-ROWGROUP
)
1315 (org-lparse-end-table)))))
1317 (defun org-lparse-format-org-table (lines &optional splice
)
1319 (org-lparse-insert-org-table lines splice
)
1320 (buffer-substring-no-properties (point-min) (point-max))))
1322 (defun org-lparse-format-list-table (lines &optional splice
)
1324 (org-lparse-insert-list-table lines splice
)
1325 (buffer-substring-no-properties (point-min) (point-max))))
1327 (defun org-lparse-insert-table-table (lines)
1328 "Format a table generated by table.el into backend-specific code.
1329 This conversion does *not* use `table-generate-source' from table.el.
1330 This has the advantage that Org-mode's HTML conversions can be used.
1331 But it has the disadvantage, that no cell- or row-spanning is allowed."
1332 (let (line field-buffer
1333 (org-lparse-table-cur-rowgrp-is-hdr
1334 org-export-highlight-first-table-line
)
1338 (org-lparse-table-style 'table-table
)
1339 (org-lparse-table-is-styled nil
)
1340 fields org-lparse-table-ncols i
(org-lparse-table-rownum -
1)
1341 (empty (org-lparse-format 'SPACES
1)))
1342 (org-lparse-begin 'TABLE caption label attributes
)
1343 (while (setq line
(pop lines
))
1345 ((string-match "^[ \t]*\\+-" line
)
1347 (let ((org-export-table-row-tags '("<tr>" .
"</tr>"))
1348 ;; (org-export-html-table-use-header-tags-for-first-column nil)
1350 (insert (org-lparse-format-table-row field-buffer empty
)))
1351 (setq org-lparse-table-cur-rowgrp-is-hdr nil
)
1352 (setq field-buffer nil
)))
1354 ;; Break the line into fields and store the fields
1355 (setq fields
(org-split-string line
"[ \t]*|[ \t]*"))
1357 (setq field-buffer
(mapcar
1359 (concat x
(org-lparse-format 'LINE-BREAK
)
1362 (setq field-buffer fields
)))))
1363 (org-lparse-end-table)))
1365 (defun org-lparse-format-table-table (lines)
1367 (org-lparse-insert-table-table lines
)
1368 (buffer-substring-no-properties (point-min) (point-max))))
1370 (defvar table-source-languages
) ; defined in table.el
1371 (defun org-lparse-format-table-table-using-table-generate-source (backend
1375 "Format a table into BACKEND, using `table-generate-source' from table.el.
1376 Use SPANNED-ONLY to suppress exporting of simple table.el tables.
1378 When SPANNED-ONLY is nil, all table.el tables are exported. When
1379 SPANNED-ONLY is non-nil, only tables with either row or column
1382 This routine returns the generated source or nil as appropriate.
1384 Refer docstring of `org-export-prefer-native-exporter-for-tables'
1385 for further information."
1387 (with-current-buffer (get-buffer-create " org-tmp1 ")
1389 (insert (mapconcat 'identity lines
"\n"))
1390 (goto-char (point-min))
1391 (if (not (re-search-forward "|[^+]" nil t
))
1392 (error "Error processing table"))
1393 (table-recognize-table)
1394 (when (or (not spanned-only
)
1395 (let* ((dim (table-query-dimension))
1396 (c (nth 4 dim
)) (r (nth 5 dim
)) (cells (nth 6 dim
)))
1397 (not (= (* c r
) cells
))))
1398 (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
1400 ((member backend table-source-languages
)
1401 (table-generate-source backend
" org-tmp2 ")
1402 (set-buffer " org-tmp2 ")
1403 (buffer-substring (point-min) (point-max)))
1405 ;; table.el doesn't support the given backend. Currently this
1406 ;; happens in case of odt export. Strip the table from the
1407 ;; generated document. A better alternative would be to embed
1408 ;; the table as ascii text in the output document.
1411 "Found table.el-type table in the source org file. "
1412 (format "table.el doesn't support %s backend. "
1413 (upcase (symbol-name backend
)))
1414 "Skipping ahead ..."))
1417 (defun org-lparse-handle-time-stamps (s)
1418 "Format time stamps in string S, or remove them."
1421 (when org-maybe-keyword-time-regexp
1422 (while (string-match org-maybe-keyword-time-regexp s
)
1423 (or b
(setq b
(substring s
0 (match-beginning 0))))
1425 r
(substring s
0 (match-beginning 0)) " "
1432 (match-string 1 s
) "timestamp-kwd"))
1436 (substring (org-translate-time (match-string 3 s
)) 1 -
1)
1438 "timestamp-wrapper"))
1439 s
(substring s
(match-end 0)))))
1441 ;; Line break if line started and ended with time stamp stuff
1444 (setq r
(concat r s
))
1445 (unless (string-match "\\S-" (concat b s
))
1446 (setq r
(concat r
(org-lparse-format 'LINE-BREAK
))))
1449 (defun org-xml-encode-plain-text (s)
1450 "Convert plain text characters to HTML equivalent.
1451 Possible conversions are set in `org-export-html-protect-char-alist'."
1452 (let ((cl (org-lparse-get 'PLAIN-TEXT-MAP
)) c
)
1453 (while (setq c
(pop cl
))
1455 (while (string-match (car c
) s start
)
1456 (setq s
(replace-match (cdr c
) t t s
)
1457 start
(1+ (match-beginning 0))))))
1460 (defun org-xml-encode-org-text-skip-links (string)
1461 "Prepare STRING for HTML export. Apply all active conversions.
1462 If there are links in the string, don't modify these. If STRING
1463 is nil, return nil."
1465 (let* ((re (concat org-bracket-link-regexp
"\\|"
1466 (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")))
1468 (while (setq m
(string-match re string
))
1469 (setq s
(substring string
0 m
)
1470 l
(match-string 0 string
)
1471 string
(substring string
(match-end 0)))
1472 (push (org-xml-encode-org-text s
) res
)
1474 (push (org-xml-encode-org-text string
) res
)
1475 (apply 'concat
(nreverse res
)))))
1477 (defun org-xml-encode-org-text (s)
1478 "Apply all active conversions to translate special ASCII to HTML."
1479 (setq s
(org-xml-encode-plain-text s
))
1480 (if org-export-html-expand
1481 (while (string-match "@<\\([^&]*\\)>" s
)
1482 (setq s
(replace-match "<\\1>" t nil s
))))
1483 (if org-export-with-emphasize
1484 (setq s
(org-lparse-apply-char-styles s
)))
1485 (if org-export-with-special-strings
1486 (setq s
(org-lparse-convert-special-strings s
)))
1487 (if org-export-with-sub-superscripts
1488 (setq s
(org-lparse-apply-sub-superscript-styles s
)))
1489 (if org-export-with-TeX-macros
1490 (let ((start 0) wd rep
)
1491 (while (setq start
(string-match "\\\\\\([a-zA-Z]+[0-9]*\\)\\({}\\)?"
1493 (if (get-text-property (match-beginning 0) 'org-protected s
)
1494 (setq start
(match-end 0))
1495 (setq wd
(match-string 1 s
))
1496 (if (setq rep
(org-lparse-format 'ORG-ENTITY wd
))
1497 (setq s
(replace-match rep t t s
))
1498 (setq start
(+ start
(length wd
))))))))
1501 (defun org-lparse-convert-special-strings (string)
1502 "Convert special characters in STRING to HTML."
1503 (let ((all (org-lparse-get 'SPECIAL-STRING-REGEXPS
))
1505 (while (setq a
(pop all
))
1506 (setq re
(car a
) rpl
(cdr a
) start
0)
1507 (while (string-match re string start
)
1508 (if (get-text-property (match-beginning 0) 'org-protected string
)
1509 (setq start
(match-end 0))
1510 (setq string
(replace-match rpl t nil string
)))))
1513 (defun org-lparse-apply-sub-superscript-styles (string)
1514 "Apply subscript and superscript styles to STRING.
1515 Use `org-export-with-sub-superscripts' to control application of
1516 sub and superscript styles."
1517 (let (key c
(s 0) (requireb (eq org-export-with-sub-superscripts
'{})))
1518 (while (string-match org-match-substring-regexp string s
)
1520 ((and requireb
(match-end 8)) (setq s
(match-end 2)))
1521 ((get-text-property (match-beginning 2) 'org-protected string
)
1522 (setq s
(match-end 2)))
1524 (setq s
(match-end 1)
1525 key
(if (string= (match-string 2 string
) "_")
1526 'subscript
'superscript
)
1527 c
(or (match-string 8 string
)
1528 (match-string 6 string
)
1529 (match-string 5 string
))
1530 string
(replace-match
1531 (concat (match-string 1 string
)
1532 (org-lparse-format 'FONTIFY c key
))
1534 (while (string-match "\\\\\\([_^]\\)" string
)
1535 (setq string
(replace-match (match-string 1 string
) t t string
)))
1538 (defvar org-lparse-char-styles
1545 "Map Org emphasis markers to char styles.
1546 This is an alist where each element is of the
1547 form (ORG-EMPHASIS-CHAR . CHAR-STYLE).")
1549 (defun org-lparse-apply-char-styles (string)
1550 "Apply char styles to STRING.
1551 The variable `org-lparse-char-styles' controls how the Org
1552 emphasis markers are interpreted."
1554 (while (string-match org-emph-re string s
)
1556 (substring string
(match-beginning 3) (1+ (match-beginning 3)))
1557 (substring string
(match-beginning 4) (1+ (match-beginning 4)))))
1558 (setq s
(match-beginning 0)
1561 (match-string 1 string
)
1563 'FONTIFY
(match-string 4 string
)
1564 (nth 1 (assoc (match-string 3 string
)
1565 org-lparse-char-styles
)))
1566 (match-string 5 string
))
1567 string
(replace-match rpl t t string
)
1568 s
(+ s
(- (length rpl
) 2)))
1572 (defun org-lparse-export-list-line (line pos struct prevs
)
1573 "Insert list syntax in export buffer. Return LINE, maybe modified.
1575 POS is the item position or line position the line had before
1576 modifications to buffer. STRUCT is the list structure. PREVS is
1577 the alist of previous items."
1580 ;; Translate type of list containing POS to "d", "o" or
1582 (lambda (pos struct prevs
)
1583 (let ((type (org-list-get-list-type pos struct prevs
)))
1585 ((eq 'ordered type
) "o")
1586 ((eq 'descriptive type
) "d")
1590 ;; Return list of all items and sublists ending at POS, in
1596 (let ((end (nth 6 e
))
1599 ((= end pos
) (push item out
))
1600 ((>= item pos
) (throw 'exit nil
)))))
1603 ;; First close any previous item, or list, ending at POS.
1605 (let* ((lastp (= (org-list-get-last-item e struct prevs
) e
))
1606 (first-item (org-list-get-list-begin e struct prevs
))
1607 (type (funcall get-type first-item struct prevs
)))
1608 (org-lparse-end-paragraph)
1609 ;; Ending for every item
1610 (org-lparse-end-list-item-1 type
)
1611 ;; We're ending last item of the list: end list.
1613 (org-lparse-end-list type
)
1614 (org-lparse-begin-paragraph))))
1615 (funcall get-closings pos
))
1617 ;; At an item: insert appropriate tags in export buffer.
1620 (concat "[ \t]*\\(\\S-+[ \t]*\\)"
1621 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
1622 "\\(?:\\(\\[[ X-]\\]\\)[ \t]+\\)?"
1623 "\\(?:\\(.*\\)[ \t]+::\\(?:[ \t]+\\|$\\)\\)?"
1625 (let* ((checkbox (match-string 3 line
))
1626 (desc-tag (or (match-string 4 line
) "???"))
1627 (body (or (match-string 5 line
) ""))
1628 (list-beg (org-list-get-list-begin pos struct prevs
))
1629 (firstp (= list-beg pos
))
1630 ;; Always refer to first item to determine list type, in
1631 ;; case list is ill-formed.
1632 (type (funcall get-type list-beg struct prevs
))
1633 (counter (let ((count-tmp (org-list-get-counter pos struct
)))
1635 ((not count-tmp
) nil
)
1636 ((string-match "[A-Za-z]" count-tmp
)
1637 (- (string-to-char (upcase count-tmp
)) 64))
1638 ((string-match "[0-9]+" count-tmp
)
1641 (org-lparse-end-paragraph)
1642 (org-lparse-begin-list type
))
1644 (let ((arg (cond ((equal type
"d") desc-tag
)
1645 ((equal type
"o") counter
))))
1646 (org-lparse-begin-list-item type arg
))
1648 ;; If line had a checkbox, some additional modification is required.
1656 ((string-match "X" checkbox
) "X")
1657 ((string-match " " checkbox
)
1658 (org-lparse-format 'SPACES
1))
1664 ;; Return modified line
1666 ;; At a list ender: go to next line (side-effects only).
1667 ((equal "ORG-LIST-END-MARKER" line
) (throw 'nextline nil
))
1668 ;; Not at an item: return line unchanged (side-effects only).
1671 (defun org-lparse-bind-local-variables (opt-plist)
1673 (set (make-local-variable (nth 2 x
))
1674 (plist-get opt-plist
(car x
))))
1675 org-export-plist-vars
))
1677 (defvar org-lparse-table-rowgrp-open
)
1678 (defvar org-lparse-table-cur-rowgrp-is-hdr
)
1679 (defvar org-lparse-footnote-number
)
1680 (defvar org-lparse-footnote-definitions
)
1681 (defvar org-lparse-output-buffer nil
1682 "Buffer to which `org-do-lparse' writes to.
1683 This buffer contains the contents of the to-be-created exported
1686 (defcustom org-lparse-debug nil
1687 "Enable or Disable logging of `org-lparse' callbacks.
1688 The parameters passed to the backend-registered ENTITY-CONTROL
1689 and ENTITY-FORMAT callbacks are logged as comment strings in the
1690 exported buffer. (org-lparse-format 'COMMENT fmt args) is used
1691 for logging. Customize this variable only if you are an expert
1692 user. Valid values of this variable are:
1693 nil : Disable logging
1694 control : Log all invocations of `org-lparse-begin' and
1695 `org-lparse-end' callbacks.
1696 format : Log invocations of `org-lparse-format' callbacks.
1697 t : Log all invocations of `org-lparse-begin', `org-lparse-end'
1698 and `org-lparse-format' callbacks,"
1701 (const :tag
"Disable" nil
)
1702 (const :tag
"Format callbacks" format
)
1703 (const :tag
"Control callbacks" control
)
1704 (const :tag
"Format and Control callbacks" t
)))
1706 (defun org-lparse-begin (entity &rest args
)
1707 "Begin ENTITY in current buffer. ARGS is entity specific.
1708 ENTITY can be one of PARAGRAPH, LIST, LIST-ITEM etc.
1710 Use (org-lparse-begin 'LIST \"o\") to begin a list in current
1713 See `org-xhtml-entity-control-callbacks-alist' for more
1715 (when (and (member org-lparse-debug
'(t control
))
1716 (not (eq entity
'DOCUMENT-CONTENT
)))
1717 (insert (org-lparse-format 'COMMENT
"%s BEGIN %S" entity args
)))
1719 (let ((f (cadr (assoc entity org-lparse-entity-control-callbacks-alist
))))
1720 (unless f
(error "Unknown entity: %s" entity
))
1723 (defun org-lparse-end (entity &rest args
)
1724 "Close ENTITY in current buffer. ARGS is entity specific.
1725 ENTITY can be one of PARAGRAPH, LIST, LIST-ITEM
1728 Use (org-lparse-end 'LIST \"o\") to close a list in current
1731 See `org-xhtml-entity-control-callbacks-alist' for more
1733 (when (and (member org-lparse-debug
'(t control
))
1734 (not (eq entity
'DOCUMENT-CONTENT
)))
1735 (insert (org-lparse-format 'COMMENT
"%s END %S" entity args
)))
1737 (let ((f (caddr (assoc entity org-lparse-entity-control-callbacks-alist
))))
1738 (unless f
(error "Unknown entity: %s" entity
))
1741 (defun org-lparse-begin-paragraph (&optional style
)
1742 "Insert <p>, but first close previous paragraph if any."
1743 (org-lparse-end-paragraph)
1744 (org-lparse-begin 'PARAGRAPH style
)
1745 (setq org-lparse-par-open t
))
1747 (defun org-lparse-end-paragraph ()
1748 "Close paragraph if there is one open."
1749 (when org-lparse-par-open
1750 (org-lparse-end 'PARAGRAPH
)
1751 (setq org-lparse-par-open nil
)))
1753 (defun org-lparse-end-list-item-1 (&optional type
)
1754 "Close <li> if necessary."
1755 (org-lparse-end-paragraph)
1756 (org-lparse-end-list-item (or type
"u")))
1758 (defun org-lparse-preprocess-after-blockquote-hook ()
1759 "Treat `org-lparse-special-blocks' specially."
1760 (goto-char (point-min))
1761 (while (re-search-forward
1762 "^[ \t]*#\\+\\(begin\\|end\\)_\\(\\S-+\\)[ \t]*\\(.*\\)$" nil t
)
1763 (when (member (downcase (match-string 2)) org-lparse-special-blocks
)
1765 (if (equal (downcase (match-string 1)) "begin")
1766 (format "ORG-%s-START %s" (upcase (match-string 2))
1768 (format "ORG-%s-END %s" (upcase (match-string 2))
1769 (match-string 3))) t t
))))
1771 (add-hook 'org-export-preprocess-after-blockquote-hook
1772 'org-lparse-preprocess-after-blockquote-hook
)
1774 (defun org-lparse-strip-experimental-blocks-maybe-hook ()
1775 "Strip \"list-table\" and \"annotation\" blocks.
1776 Stripping happens only when the exported backend is not one of
1777 \"odt\" or \"xhtml\"."
1778 (when (not org-lparse-backend
)
1779 (message "Stripping following blocks - %S" org-lparse-special-blocks
)
1780 (goto-char (point-min))
1781 (let ((case-fold-search t
))
1784 "^[ \t]*#\\+begin_\\(\\S-+\\)\\([ \t]+.*\\)?\n\\([^\000]*?\\)\n[ \t]*#\\+end_\\1\\>.*"
1786 (when (member (match-string 1) org-lparse-special-blocks
)
1787 (replace-match "" t t
))))))
1789 (add-hook 'org-export-preprocess-hook
1790 'org-lparse-strip-experimental-blocks-maybe-hook
)
1792 (defvar org-lparse-list-table-p nil
1793 "Non-nil if `org-do-lparse' is within a list-table.")
1795 (defvar org-lparse-dyn-current-environment nil
)
1796 (defun org-lparse-begin-environment (style &optional env-options-plist
)
1799 (setq org-lparse-list-table-p t
))
1801 (setq org-lparse-dyn-current-environment style
)
1802 (org-lparse-begin 'ENVIRONMENT style env-options-plist
))))
1804 (defun org-lparse-end-environment (style &optional env-options-plist
)
1807 (setq org-lparse-list-table-p nil
))
1809 (org-lparse-end 'ENVIRONMENT style env-options-plist
)
1810 (setq org-lparse-dyn-current-environment nil
))))
1812 (defun org-lparse-current-environment-p (style)
1813 (eq org-lparse-dyn-current-environment style
))
1815 (defun org-lparse-begin-footnote-definition (n)
1816 (org-lparse-begin-collect)
1817 (setq org-lparse-insert-tag-with-newlines nil
)
1818 (org-lparse-begin 'FOOTNOTE-DEFINITION n
))
1820 (defun org-lparse-end-footnote-definition (n)
1821 (org-lparse-end 'FOOTNOTE-DEFINITION n
)
1822 (setq org-lparse-insert-tag-with-newlines
'both
)
1823 (let ((footnote-def (org-lparse-end-collect)))
1824 (push (cons n footnote-def
) org-lparse-footnote-definitions
)))
1826 (defvar org-lparse-collect-buffer nil
1827 "An auxiliary buffer named \"*Org Lparse Collect*\".
1828 `org-do-lparse' uses this as output buffer while collecting
1829 footnote definitions and table-cell contents of list-tables. See
1830 `org-lparse-begin-collect' and `org-lparse-end-collect'.")
1832 (defvar org-lparse-collect-count nil
1833 "Count number of calls to `org-lparse-begin-collect'.
1834 Use this counter to catch chained collections if they ever
1837 (defun org-lparse-begin-collect ()
1838 "Temporarily switch to `org-lparse-collect-buffer'.
1839 Also erase it's contents."
1840 (unless (zerop org-lparse-collect-count
)
1841 (error "FIXME (org-lparse.el): Encountered chained collections"))
1842 (incf org-lparse-collect-count
)
1843 (unless org-lparse-collect-buffer
1844 (setq org-lparse-collect-buffer
1845 (get-buffer-create "*Org Lparse Collect*")))
1846 (set-buffer org-lparse-collect-buffer
)
1849 (defun org-lparse-end-collect ()
1850 "Switch to `org-lparse-output-buffer'.
1851 Return contents of `org-lparse-collect-buffer' as a `string'."
1852 (assert (> org-lparse-collect-count
0))
1853 (decf org-lparse-collect-count
)
1854 (prog1 (buffer-string)
1856 (set-buffer org-lparse-output-buffer
)))
1858 (defun org-lparse-format (entity &rest args
)
1859 "Format ENTITY in backend-specific way and return it.
1860 ARGS is specific to entity being formatted.
1862 Use (org-lparse-format 'HEADING \"text\" 1) to format text as
1865 See `org-xhtml-entity-format-callbacks-alist' for more information."
1866 (when (and (member org-lparse-debug
'(t format
))
1867 (not (equal entity
'COMMENT
)))
1868 (insert (org-lparse-format 'COMMENT
"%s: %S" entity args
)))
1871 (let ((text (pop args
)))
1872 (apply 'org-lparse-format
'TAGS entity text args
)))
1874 (let ((f (cdr (assoc entity org-lparse-entity-format-callbacks-alist
))))
1875 (unless f
(error "Unknown entity: %s" entity
))
1878 (defun org-lparse-insert (entity &rest args
)
1879 (insert (apply 'org-lparse-format entity args
)))
1881 (defun org-lparse-prepare-toc (lines level-offset opt-plist umax-toc
)
1882 (let* ((quote-re0 (concat "^[ \t]*" org-quote-string
"\\>"))
1883 (org-min-level (org-get-min-level lines level-offset
))
1884 (org-last-level org-min-level
)
1887 (org-lparse-bind-local-variables opt-plist
)
1889 (org-lparse-begin 'TOC
(nth 3 (plist-get opt-plist
:lang-words
)) umax-toc
)
1894 (when (and (string-match org-todo-line-regexp line
)
1895 (not (get-text-property 0 'org-protected line
))
1896 (<= (setq level
(org-tr-level
1897 (- (match-end 1) (match-beginning 1)
1900 (let ((txt (save-match-data
1901 (org-xml-encode-org-text-skip-links
1902 (org-export-cleanup-toc-line
1903 (match-string 3 line
)))))
1905 org-export-mark-todo-in-toc
1906 (or (and (match-beginning 2)
1907 (not (member (match-string 2 line
)
1908 org-done-keywords
)))
1909 (and (= level umax-toc
)
1910 (org-search-todo-below
1911 line lines level
)))))
1913 ;; Check for targets
1914 (while (string-match org-any-target-regexp line
)
1917 (let ((org-lparse-encode-pending t
))
1918 (org-lparse-format 'FONTIFY
1919 (match-string 1 line
) "target"))
1922 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt
)
1923 (setq tags
(match-string 1 txt
)
1924 txt
(replace-match "" t nil txt
)))
1925 (when (string-match quote-re0 txt
)
1926 (setq txt
(replace-match "" t t txt
)))
1927 (while (string-match "<\\(<\\)+\\|>\\(>\\)+" txt
)
1928 (setq txt
(replace-match "" t t txt
)))
1931 (let* ((snumber (org-section-number level
))
1932 (href (replace-regexp-in-string
1933 "\\." "-" (format "sec-%s" snumber
)))
1937 href org-export-preferred-target-alist
))
1939 (href (org-solidify-link-text href
)))
1940 (org-lparse-format 'TOC-ENTRY snumber todo txt tags href
))
1941 level org-last-level
)
1942 (setq org-last-level level
)))
1945 (org-lparse-end 'TOC
)
1946 (setq org-lparse-toc
(buffer-string))))
1949 (defun org-lparse-format-table-row (fields &optional text-for-empty-fields
)
1950 (if org-lparse-table-ncols
1951 ;; second and subsequent rows of the table
1952 (when (and org-lparse-list-table-p
1953 (> (length fields
) org-lparse-table-ncols
))
1954 (error "Table row has %d columns but header row claims %d columns"
1955 (length fields
) org-lparse-table-ncols
))
1956 ;; first row of the table
1957 (setq org-lparse-table-ncols
(length fields
))
1958 (when org-lparse-table-is-styled
1959 (setq org-lparse-table-num-numeric-items-per-column
1960 (make-vector org-lparse-table-ncols
0))
1961 (setq org-lparse-table-colalign-vector
1962 (make-vector org-lparse-table-ncols nil
))
1964 (while (< (incf c
) org-lparse-table-ncols
)
1965 (let* ((col-cookie (cdr (assoc (1+ c
) org-lparse-table-colalign-info
)))
1966 (align (nth 0 col-cookie
)))
1967 (setf (aref org-lparse-table-colalign-vector c
)
1969 ((string= align
"l") "left")
1970 ((string= align
"r") "right")
1971 ((string= align
"c") "center")
1973 (incf org-lparse-table-rownum
)
1979 (when (and (string= x
"") text-for-empty-fields
)
1980 (setq x text-for-empty-fields
))
1982 (let (col-cookie horiz-span
)
1983 (when org-lparse-table-is-styled
1984 (when (and (< i org-lparse-table-ncols
)
1985 (string-match org-table-number-regexp x
))
1986 (incf (aref org-lparse-table-num-numeric-items-per-column i
)))
1987 (setq col-cookie
(cdr (assoc (1+ i
) org-lparse-table-colalign-info
))
1988 horiz-span
(nth 1 col-cookie
)))
1990 'TABLE-CELL x org-lparse-table-rownum i
(or horiz-span
0))))
1993 (defun org-lparse-get (what &optional opt-plist
)
1994 "Query for value of WHAT for the current backend `org-lparse-backend'.
1995 See also `org-lparse-backend-get'."
1996 (if (boundp 'org-lparse-backend
)
1997 (org-lparse-backend-get (symbol-name org-lparse-backend
) what opt-plist
)
1998 (error "org-lparse-backend is not bound yet")))
2000 (defun org-lparse-backend-get (backend what
&optional opt-plist
)
2001 "Query BACKEND for value of WHAT.
2002 Dispatch the call to `org-<backend>-user-get'. If that throws an
2003 error, dispatch the call to `org-<backend>-get'. See
2004 `org-xhtml-get' for all known settings queried for by
2005 `org-lparse' during the course of export."
2006 (assert (stringp backend
) t
)
2007 (unless (org-lparse-backend-is-native-p backend
)
2008 (error "Unknown native backend %s" backend
))
2009 (let ((backend-get-method (intern (format "org-%s-get" backend
)))
2010 (backend-user-get-method (intern (format "org-%s-user-get" backend
))))
2012 ((functionp backend-get-method
)
2014 (funcall backend-user-get-method what opt-plist
)
2015 (error (funcall backend-get-method what opt-plist
))))
2017 (error "Native backend %s doesn't define %s" backend backend-get-method
)))))
2019 (defun org-lparse-insert-tag (tag &rest args
)
2020 (when (member org-lparse-insert-tag-with-newlines
'(lead both
))
2022 (insert (apply 'format tag args
))
2023 (when (member org-lparse-insert-tag-with-newlines
'(trail both
))
2026 (defun org-lparse-get-targets-from-title (title)
2027 (let* ((target (org-get-text-property-any 0 'target title
))
2028 (extra-targets (assoc target org-export-target-aliases
))
2029 (target (or (cdr (assoc target org-export-preferred-target-alist
))
2031 (cons target
(remove target extra-targets
))))
2033 (defun org-lparse-suffix-from-snumber (snumber)
2034 (let* ((snu (replace-regexp-in-string "\\." "-" snumber
))
2035 (href (cdr (assoc (concat "sec-" snu
)
2036 org-export-preferred-target-alist
))))
2037 (org-solidify-link-text (or href snu
))))
2039 (defun org-lparse-begin-level (level title umax head-count
)
2040 "Insert a new LEVEL in HTML export.
2041 When TITLE is nil, just close all open levels."
2042 (org-lparse-end-level level umax
)
2043 (unless title
(error "Why is heading nil"))
2044 (let* ((targets (org-lparse-get-targets-from-title title
))
2045 (target (car targets
)) (extra-targets (cdr targets
))
2046 (target (and target
(org-solidify-link-text target
)))
2047 (extra-class (org-get-text-property-any 0 'html-container-class title
))
2048 snumber tags level1 class
)
2049 (when (string-match (org-re "\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") title
)
2050 (setq tags
(and org-export-with-tags
(match-string 1 title
)))
2051 (setq title
(replace-match "" t t title
)))
2054 (if (aref org-levels-open
(1- level
))
2055 (org-lparse-end-list-item-1)
2056 (aset org-levels-open
(1- level
) t
)
2057 (org-lparse-end-paragraph)
2058 (org-lparse-begin-list 'unordered
))
2059 (org-lparse-begin-list-item
2060 'unordered target
(org-lparse-format
2061 'HEADLINE title extra-targets tags
)))
2062 (aset org-levels-open
(1- level
) t
)
2063 (setq snumber
(org-section-number level
))
2064 (setq level1
(+ level
(or (org-lparse-get 'TOPLEVEL-HLEVEL
) 1) -
1))
2065 (unless (= head-count
1)
2066 (org-lparse-end-outline-text-or-outline))
2067 (org-lparse-begin-outline-and-outline-text
2068 level1 snumber title tags target extra-targets extra-class
)
2069 (org-lparse-begin-paragraph))))
2071 (defun org-lparse-end-level (level umax
)
2072 (org-lparse-end-paragraph)
2073 (loop for l from org-level-max downto level
2074 do
(when (aref org-levels-open
(1- l
))
2075 ;; Terminate one level in HTML export
2077 (org-lparse-end-outline-text-or-outline)
2078 (org-lparse-end-list-item-1)
2079 (org-lparse-end-list 'unordered
))
2080 (aset org-levels-open
(1- l
) nil
))))
2082 (defvar org-lparse-outline-text-open
)
2083 (defun org-lparse-begin-outline-and-outline-text (level1 snumber title tags
2084 target extra-targets
2087 'OUTLINE level1 snumber title tags target extra-targets extra-class
)
2088 (org-lparse-begin-outline-text level1 snumber extra-class
))
2090 (defun org-lparse-end-outline-text-or-outline ()
2092 (org-lparse-outline-text-open
2093 (org-lparse-end 'OUTLINE-TEXT
)
2094 (setq org-lparse-outline-text-open nil
))
2095 (t (org-lparse-end 'OUTLINE
))))
2097 (defun org-lparse-begin-outline-text (level1 snumber extra-class
)
2098 (assert (not org-lparse-outline-text-open
) t
)
2099 (setq org-lparse-outline-text-open t
)
2100 (org-lparse-begin 'OUTLINE-TEXT level1 snumber extra-class
))
2102 (defun org-lparse-html-list-type-to-canonical-list-type (ltype)
2103 (cdr (assoc ltype
'(("o" . ordered
)
2105 ("d" . description
)))))
2107 ;; following vars are bound during `org-do-lparse'
2108 (defvar org-lparse-list-level
)
2109 (defvar org-lparse-list-item-count
)
2110 (defvar org-lparse-list-stack
)
2111 (defvar org-lparse-list-table
:table-row
)
2112 (defvar org-lparse-list-table
:lines
)
2114 ;; Notes on LIST-TABLES
2115 ;; ====================
2116 ;; Lists withing "list-table" blocks (as shown below)
2118 ;; #+begin_list-table
2129 ;; will be exported as though it were a table as shown below.
2131 ;; | Row 1 | 1.1 | 1.2 | 1.3 |
2132 ;; | Row 2 | 2.1 | 2.2 | 2.3 |
2134 ;; Note that org-tables are NOT multi-line and each line is mapped to
2135 ;; a unique row in the exported document. So if an exported table
2136 ;; needs to contain a single paragraph (with copious text) it needs to
2137 ;; be typed up in a single line. Editing such long lines using the
2138 ;; table editor will be a cumbersome task. Furthermore inclusion of
2139 ;; multi-paragraph text in a table cell is well-nigh impossible.
2141 ;; LIST-TABLEs are meant to circumvent the above problems with
2144 ;; Note that in the example above the list items could be paragraphs
2145 ;; themselves and the list can be arbitrarily deep.
2147 ;; Inspired by following thread:
2148 ;; https://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01101.html
2150 (defun org-lparse-begin-list (ltype)
2151 (incf org-lparse-list-level
)
2152 (push org-lparse-list-item-count org-lparse-list-stack
)
2153 (setq org-lparse-list-item-count
0)
2155 ((not org-lparse-list-table-p
)
2156 (org-lparse-begin 'LIST ltype
))
2157 ;; process LIST-TABLE
2158 ((= 1 org-lparse-list-level
)
2160 (setq org-lparse-list-table
:lines nil
)
2161 (setq org-lparse-list-table
:table-row nil
))
2162 ((= 2 org-lparse-list-level
)
2165 (org-lparse-begin 'LIST ltype
))))
2167 (defun org-lparse-end-list (ltype)
2168 (setq org-lparse-list-item-count
(pop org-lparse-list-stack
))
2169 (decf org-lparse-list-level
)
2171 ((not org-lparse-list-table-p
)
2172 (org-lparse-end 'LIST ltype
))
2173 ;; process LIST-TABLE
2174 ((= 0 org-lparse-list-level
)
2176 (insert (org-lparse-format-list-table
2177 (nreverse org-lparse-list-table
:lines
))))
2178 ((= 1 org-lparse-list-level
)
2181 (org-lparse-end 'LIST ltype
))))
2183 (defun org-lparse-begin-list-item (ltype &optional arg headline
)
2184 (incf org-lparse-list-item-count
)
2186 ((not org-lparse-list-table-p
)
2187 (org-lparse-begin 'LIST-ITEM ltype arg headline
))
2188 ;; process LIST-TABLE
2189 ((and (= 1 org-lparse-list-level
)
2190 (= 1 org-lparse-list-item-count
))
2191 ;; begin TABLE-ROW for LIST-TABLE
2192 (setq org-lparse-list-table
:table-row nil
)
2193 (org-lparse-begin-list-table:table-cell
))
2194 ((and (= 2 org-lparse-list-level
)
2195 (= 1 org-lparse-list-item-count
))
2196 ;; begin TABLE-CELL for LIST-TABLE
2197 (org-lparse-begin-list-table:table-cell
))
2199 (org-lparse-begin 'LIST-ITEM ltype arg headline
))))
2201 (defun org-lparse-end-list-item (ltype)
2202 (decf org-lparse-list-item-count
)
2204 ((not org-lparse-list-table-p
)
2205 (org-lparse-end 'LIST-ITEM ltype
))
2206 ;; process LIST-TABLE
2207 ((and (= 1 org-lparse-list-level
)
2208 (= 0 org-lparse-list-item-count
))
2209 ;; end TABLE-ROW for LIST-TABLE
2210 (org-lparse-end-list-table:table-cell
)
2211 (push (nreverse org-lparse-list-table
:table-row
)
2212 org-lparse-list-table
:lines
))
2213 ((= 2 org-lparse-list-level
)
2214 ;; end TABLE-CELL for LIST-TABLE
2215 (org-lparse-end-list-table:table-cell
))
2217 (org-lparse-end 'LIST-ITEM ltype
))))
2219 (defvar org-lparse-list-table
:table-cell-open
)
2220 (defun org-lparse-begin-list-table:table-cell
()
2221 (org-lparse-end-list-table:table-cell
)
2222 (setq org-lparse-list-table
:table-cell-open t
)
2223 (org-lparse-begin-collect)
2224 (org-lparse-begin-paragraph))
2226 (defun org-lparse-end-list-table:table-cell
()
2227 (when org-lparse-list-table
:table-cell-open
2228 (setq org-lparse-list-table
:table-cell-open nil
)
2229 (org-lparse-end-paragraph)
2230 (push (org-lparse-end-collect)
2231 org-lparse-list-table
:table-row
)))
2233 (defvar org-lparse-table-rowgrp-info
)
2234 (defun org-lparse-begin-table-rowgroup (&optional is-header-row
)
2235 (push (cons (1+ org-lparse-table-rownum
) :start
) org-lparse-table-rowgrp-info
)
2236 (org-lparse-begin 'TABLE-ROWGROUP is-header-row
))
2238 (defun org-lparse-end-table ()
2239 (when org-lparse-table-is-styled
2241 (unless (car org-table-colgroup-info
)
2242 (setq org-table-colgroup-info
2243 (cons :start
(cdr org-table-colgroup-info
))))
2250 (setf (aref org-lparse-table-colalign-vector c
)
2251 (or (aref org-lparse-table-colalign-vector c
)
2252 (if (> (/ (float x
) (1+ org-lparse-table-rownum
))
2253 org-table-number-fraction
)
2255 org-lparse-table-num-numeric-items-per-column
)))
2256 (org-lparse-end 'TABLE
))
2258 (defvar org-lparse-encode-pending nil
)
2260 (defun org-lparse-format-tags (tag text prefix suffix
&rest args
)
2263 (concat prefix
(apply 'format
(car tag
) args
) text suffix
2264 (format (cdr tag
))))
2265 ((stringp tag
) ; singleton tag
2266 (concat prefix
(apply 'format tag args
) text
))))
2268 (defun org-xml-fix-class-name (kwd) ; audit callers of this function
2269 "Turn todo keyword into a valid class name.
2270 Replaces invalid characters with \"_\"."
2272 (while (string-match "[^a-zA-Z0-9_]" kwd
)
2273 (setq kwd
(replace-match "_" t t kwd
))))
2276 (defun org-lparse-format-todo (todo)
2277 (org-lparse-format 'FONTIFY
2279 (ignore-errors (org-lparse-get 'TODO-KWD-CLASS-PREFIX
))
2280 (org-xml-fix-class-name todo
))
2281 (list (if (member todo org-done-keywords
) "done" "todo")
2284 (defun org-lparse-format-extra-targets (extra-targets)
2285 (if (not extra-targets
) ""
2286 (mapconcat (lambda (x)
2287 (setq x
(org-solidify-link-text
2288 (if (org-uuidgen-p x
) (concat "ID-" x
) x
)))
2289 (org-lparse-format 'ANCHOR
"" x
))
2292 (defun org-lparse-format-org-tags (tags)
2300 (ignore-errors (org-lparse-get 'TAG-CLASS-PREFIX
))
2301 (org-xml-fix-class-name x
))))
2302 (org-split-string tags
":")
2303 (org-lparse-format 'SPACES
1)) "tag")))
2305 (defun org-lparse-format-section-number (&optional snumber level
)
2306 (and org-export-with-section-numbers
2307 (not org-lparse-body-only
) snumber level
2308 (org-lparse-format 'FONTIFY snumber
(format "section-number-%d" level
))))
2310 (defun org-lparse-warn (msg)
2311 (if (not org-lparse-use-flashy-warning
)
2313 (put-text-property 0 (length msg
) 'face
'font-lock-warning-face msg
)
2317 (defun org-xml-format-href (s)
2318 "Make sure the S is valid as a href reference in an XHTML document."
2321 (while (string-match "&" s start
)
2322 (setq start
(+ (match-beginning 0) 3)
2323 s
(replace-match "&" t t s
)))))
2326 (defun org-xml-format-desc (s)
2327 "Make sure the S is valid as a description in a link."
2328 (if (and s
(not (get-text-property 1 'org-protected s
)))
2330 (org-xml-encode-org-text s
))
2333 (provide 'org-lparse
)
2335 ;;; org-lparse.el ends here