ob-exp: Escape code when re-creating a src blocks
[org-mode.git] / lisp / org-lparse.el
blobc5ced3ef01cdf170029943fdf3824e25aa4c6cb2
1 ;;; org-lparse.el --- Line-oriented parser-exporter for Org-mode
3 ;; Copyright (C) 2010-2012 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 part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; `org-lparse' is the entry point for the generic line-oriented
27 ;; exporter. `org-do-lparse' is the genericized version of the
28 ;; original `org-export-as-html' routine.
30 ;; `org-lparse-native-backends' is a good starting point for
31 ;; exploring the generic exporter.
33 ;; Following new interactive commands are provided by this library.
34 ;; `org-lparse', `org-lparse-and-open', `org-lparse-to-buffer'
35 ;; `org-replace-region-by', `org-lparse-region'.
37 ;; Note that the above routines correspond to the following routines
38 ;; in the html exporter `org-export-as-html',
39 ;; `org-export-as-html-and-open', `org-export-as-html-to-buffer',
40 ;; `org-replace-region-by-html' and `org-export-region-as-html'.
42 ;; The new interactive command `org-lparse-convert' can be used to
43 ;; convert documents between various formats. Use this to command,
44 ;; for example, to convert odt file to doc or pdf format.
46 ;;; Code:
47 (eval-when-compile
48 (require 'cl))
49 (require 'org-exp)
50 (require 'org-list)
51 (require 'format-spec)
53 (defun org-lparse-and-open (target-backend native-backend arg
54 &optional file-or-buf)
55 "Export outline to TARGET-BACKEND via NATIVE-BACKEND and open exported file.
56 If there is an active region, export only the region. The prefix
57 ARG specifies how many levels of the outline should become
58 headlines. The default is 3. Lower levels will become bulleted
59 lists."
60 (let (f (file-or-buf (or file-or-buf
61 (org-lparse target-backend native-backend
62 arg 'hidden))))
63 (when file-or-buf
64 (setq f (cond
65 ((bufferp file-or-buf) buffer-file-name)
66 ((file-exists-p file-or-buf) file-or-buf)
67 (t (error "org-lparse-and-open: This shouldn't happen"))))
68 (message "Opening file %s" f)
69 (org-open-file f 'system)
70 (when org-export-kill-product-buffer-when-displayed
71 (kill-buffer (current-buffer))))))
73 (defun org-lparse-batch (target-backend &optional native-backend)
74 "Call the function `org-lparse'.
75 This function can be used in batch processing as:
76 emacs --batch
77 --load=$HOME/lib/emacs/org.el
78 --eval \"(setq org-export-headline-levels 2)\"
79 --visit=MyFile --funcall org-lparse-batch"
80 (setq native-backend (or native-backend target-backend))
81 (org-lparse target-backend native-backend
82 org-export-headline-levels 'hidden))
84 (defun org-lparse-to-buffer (backend arg)
85 "Call `org-lparse' with output to a temporary buffer.
86 No file is created. The prefix ARG is passed through to
87 `org-lparse'."
88 (let ((tempbuf (format "*Org %s Export*" (upcase backend))))
89 (org-lparse backend backend arg nil nil tempbuf)
90 (when org-export-show-temporary-export-buffer
91 (switch-to-buffer-other-window tempbuf))))
93 (defun org-replace-region-by (backend beg end)
94 "Assume the current region has org-mode syntax, and convert it to HTML.
95 This can be used in any buffer. For example, you could write an
96 itemized list in org-mode syntax in an HTML buffer and then use
97 this command to convert it."
98 (let (reg backend-string buf pop-up-frames)
99 (save-window-excursion
100 (if (derived-mode-p 'org-mode)
101 (setq backend-string (org-lparse-region backend beg end t 'string))
102 (setq reg (buffer-substring beg end)
103 buf (get-buffer-create "*Org tmp*"))
104 (with-current-buffer buf
105 (erase-buffer)
106 (insert reg)
107 (org-mode)
108 (setq backend-string (org-lparse-region backend (point-min)
109 (point-max) t 'string)))
110 (kill-buffer buf)))
111 (delete-region beg end)
112 (insert backend-string)))
114 (defun org-lparse-region (backend beg end &optional body-only buffer)
115 "Convert region from BEG to END in org-mode buffer to HTML.
116 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
117 contents, and only produce the region of converted text, useful for
118 cut-and-paste operations.
119 If BUFFER is a buffer or a string, use/create that buffer as a target
120 of the converted HTML. If BUFFER is the symbol `string', return the
121 produced HTML as a string and leave not buffer behind. For example,
122 a Lisp program could call this function in the following way:
124 (setq html (org-lparse-region \"html\" beg end t 'string))
126 When called interactively, the output buffer is selected, and shown
127 in a window. A non-interactive call will only return the buffer."
128 (let ((transient-mark-mode t) (zmacs-regions t)
129 ext-plist rtn)
130 (setq ext-plist (plist-put ext-plist :ignore-subtree-p t))
131 (goto-char end)
132 (set-mark (point)) ;; to activate the region
133 (goto-char beg)
134 (setq rtn (org-lparse backend backend nil nil ext-plist buffer body-only))
135 (if (fboundp 'deactivate-mark) (deactivate-mark))
136 (if (and (org-called-interactively-p 'any) (bufferp rtn))
137 (switch-to-buffer-other-window rtn)
138 rtn)))
140 (defvar org-lparse-par-open nil)
142 (defun org-lparse-should-inline-p (filename descp)
143 "Return non-nil if link FILENAME should be inlined.
144 The decision to inline the FILENAME link is based on the current
145 settings. DESCP is the boolean of whether there was a link
146 description. See variables `org-export-html-inline-images' and
147 `org-export-html-inline-image-extensions'."
148 (let ((inline-images (org-lparse-get 'INLINE-IMAGES))
149 (inline-image-extensions
150 (org-lparse-get 'INLINE-IMAGE-EXTENSIONS)))
151 (and (or (eq t inline-images) (and inline-images (not descp)))
152 (org-file-image-p filename inline-image-extensions))))
154 (defun org-lparse-format-org-link (line opt-plist)
155 "Return LINE with markup of Org mode links.
156 OPT-PLIST is the export options list."
157 (let ((start 0)
158 (current-dir (if buffer-file-name
159 (file-name-directory buffer-file-name)
160 default-directory))
161 (link-validate (plist-get opt-plist :link-validation-function))
162 type id-file fnc
163 rpl path attr desc descp desc1 desc2 link
164 org-lparse-link-description-is-image)
165 (while (string-match org-bracket-link-analytic-regexp++ line start)
166 (setq org-lparse-link-description-is-image nil)
167 (setq start (match-beginning 0))
168 (setq path (save-match-data (org-link-unescape
169 (match-string 3 line))))
170 (setq type (cond
171 ((match-end 2) (match-string 2 line))
172 ((save-match-data
173 (or (file-name-absolute-p path)
174 (string-match "^\\.\\.?/" path)))
175 "file")
176 (t "internal")))
177 (setq path (org-extract-attributes path))
178 (setq attr (get-text-property 0 'org-attributes path))
179 (setq desc1 (if (match-end 5) (match-string 5 line))
180 desc2 (if (match-end 2) (concat type ":" path) path)
181 descp (and desc1 (not (equal desc1 desc2)))
182 desc (or desc1 desc2))
183 ;; Make an image out of the description if that is so wanted
184 (when (and descp (org-file-image-p
185 desc (org-lparse-get 'INLINE-IMAGE-EXTENSIONS)))
186 (setq org-lparse-link-description-is-image t)
187 (save-match-data
188 (if (string-match "^file:" desc)
189 (setq desc (substring desc (match-end 0)))))
190 (save-match-data
191 (setq desc (org-add-props
192 (org-lparse-format 'INLINE-IMAGE desc)
193 '(org-protected t)))))
194 (cond
195 ((equal type "internal")
196 (let
197 ((frag-0
198 (if (= (string-to-char path) ?#)
199 (substring path 1)
200 path)))
201 (setq rpl
202 (org-lparse-format
203 'ORG-LINK opt-plist "" "" (org-solidify-link-text
204 (save-match-data
205 (org-link-unescape frag-0))
206 nil) desc attr descp))))
207 ((and (equal type "id")
208 (setq id-file (org-id-find-id-file path)))
209 ;; This is an id: link to another file (if it was the same file,
210 ;; it would have become an internal link...)
211 (save-match-data
212 (setq id-file (file-relative-name
213 id-file
214 (file-name-directory org-current-export-file)))
215 (setq rpl
216 (org-lparse-format
217 'ORG-LINK opt-plist type id-file
218 (concat (if (org-uuidgen-p path) "ID-") path)
219 desc attr descp))))
220 ((member type '("http" "https"))
221 ;; standard URL, can inline as image
222 (setq rpl
223 (org-lparse-format
224 'ORG-LINK opt-plist type path nil desc attr descp)))
225 ((member type '("ftp" "mailto" "news"))
226 ;; standard URL, can't inline as image
227 (setq rpl
228 (org-lparse-format
229 'ORG-LINK opt-plist type path nil desc attr descp)))
231 ((string= type "coderef")
232 (setq rpl (org-lparse-format
233 'ORG-LINK opt-plist type "" path desc nil descp)))
235 ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
236 ;; The link protocol has a function for format the link
237 (setq rpl (save-match-data
238 (funcall fnc (org-link-unescape path)
239 desc1 (and (boundp 'org-lparse-backend)
240 (case org-lparse-backend
241 (xhtml 'html)
242 (t org-lparse-backend)))))))
243 ((string= type "file")
244 ;; FILE link
245 (save-match-data
246 (let*
247 ((components
249 (string-match "::\\(.*\\)" path)
250 (list
251 (replace-match "" t nil path)
252 (match-string 1 path))
253 (list path nil)))
255 ;;The proper path, without a fragment
256 (path-1
257 (first components))
259 ;;The raw fragment
260 (fragment-0
261 (second components))
263 ;;Check the fragment. If it can't be used as
264 ;;target fragment we'll pass nil instead.
265 (fragment-1
267 (and fragment-0
268 (not (string-match "^[0-9]*$" fragment-0))
269 (not (string-match "^\\*" fragment-0))
270 (not (string-match "^/.*/$" fragment-0)))
271 (org-solidify-link-text
272 (org-link-unescape fragment-0))
273 nil))
274 (desc-2
275 ;;Description minus "file:" and ".org"
276 (if (string-match "^file:" desc)
277 (let
278 ((desc-1 (replace-match "" t t desc)))
279 (if (string-match "\\.org$" desc-1)
280 (replace-match "" t t desc-1)
281 desc-1))
282 desc)))
284 (setq rpl
286 (and
287 (functionp link-validate)
288 (not (funcall link-validate path-1 current-dir)))
289 desc
290 (org-lparse-format
291 'ORG-LINK opt-plist "file" path-1 fragment-1
292 desc-2 attr descp))))))
295 ;; just publish the path, as default
296 (setq rpl (concat "<i>&lt;" type ":"
297 (save-match-data (org-link-unescape path))
298 "&gt;</i>"))))
299 (setq line (replace-match rpl t t line)
300 start (+ start (length rpl))))
301 line))
303 (defvar org-lparse-par-open-stashed) ; bound during `org-do-lparse'
304 (defun org-lparse-stash-save-paragraph-state ()
305 (assert (zerop org-lparse-par-open-stashed))
306 (setq org-lparse-par-open-stashed org-lparse-par-open)
307 (setq org-lparse-par-open nil))
309 (defun org-lparse-stash-pop-paragraph-state ()
310 (setq org-lparse-par-open org-lparse-par-open-stashed)
311 (setq org-lparse-par-open-stashed 0))
313 (defmacro with-org-lparse-preserve-paragraph-state (&rest body)
314 `(let ((org-lparse-do-open-par org-lparse-par-open))
315 (org-lparse-end-paragraph)
316 ,@body
317 (when org-lparse-do-open-par
318 (org-lparse-begin-paragraph))))
319 (def-edebug-spec with-org-lparse-preserve-paragraph-state (body))
321 (defvar org-lparse-native-backends nil
322 "List of native backends registered with `org-lparse'.
323 A backend can use `org-lparse-register-backend' to add itself to
324 this list.
326 All native backends must implement a get routine and a mandatory
327 set of callback routines.
329 The get routine must be named as org-<backend>-get where backend
330 is the name of the backend. The exporter uses `org-lparse-get'
331 and retrieves the backend-specific callback by querying for
332 ENTITY-CONTROL and ENTITY-FORMAT variables.
334 For the sake of illustration, the html backend implements
335 `org-xhtml-get'. It returns
336 `org-xhtml-entity-control-callbacks-alist' and
337 `org-xhtml-entity-format-callbacks-alist' as the values of
338 ENTITY-CONTROL and ENTITY-FORMAT settings.")
340 (defun org-lparse-register-backend (backend)
341 "Make BACKEND known to `org-lparse' library.
342 Add BACKEND to `org-lparse-native-backends'."
343 (when backend
344 (setq backend (cond
345 ((symbolp backend) (symbol-name backend))
346 ((stringp backend) backend)
347 (t (error "Error while registering backend: %S" backend))))
348 (add-to-list 'org-lparse-native-backends backend)))
350 (defun org-lparse-unregister-backend (backend)
351 (setq org-lparse-native-backends
352 (remove (cond
353 ((symbolp backend) (symbol-name backend))
354 ((stringp backend) backend))
355 org-lparse-native-backends))
356 (message "Unregistered backend %S" backend))
358 (defun org-lparse-do-reachable-formats (in-fmt)
359 "Return verbose info about formats to which IN-FMT can be converted.
360 Return a list where each element is of the
361 form (CONVERTER-PROCESS . OUTPUT-FMT-ALIST). See
362 `org-export-odt-convert-processes' for CONVERTER-PROCESS and see
363 `org-export-odt-convert-capabilities' for OUTPUT-FMT-ALIST."
364 (let (reachable-formats)
365 (dolist (backend org-lparse-native-backends reachable-formats)
366 (let* ((converter (org-lparse-backend-get
367 backend 'CONVERT-METHOD))
368 (capabilities (org-lparse-backend-get
369 backend 'CONVERT-CAPABILITIES)))
370 (when converter
371 (dolist (c capabilities)
372 (when (member in-fmt (nth 1 c))
373 (push (cons converter (nth 2 c)) reachable-formats))))))))
375 (defun org-lparse-reachable-formats (in-fmt)
376 "Return list of formats to which IN-FMT can be converted.
377 The list of the form (OUTPUT-FMT-1 OUTPUT-FMT-2 ...)."
378 (let (l)
379 (mapc (lambda (e) (add-to-list 'l e))
380 (apply 'append (mapcar
381 (lambda (e) (mapcar 'car (cdr e)))
382 (org-lparse-do-reachable-formats in-fmt))))
385 (defun org-lparse-reachable-p (in-fmt out-fmt)
386 "Return non-nil if IN-FMT can be converted to OUT-FMT."
387 (catch 'done
388 (let ((reachable-formats (org-lparse-do-reachable-formats in-fmt)))
389 (dolist (e reachable-formats)
390 (let ((out-fmt-spec (assoc out-fmt (cdr e))))
391 (when out-fmt-spec
392 (throw 'done (cons (car e) out-fmt-spec))))))))
394 (defun org-lparse-backend-is-native-p (backend)
395 (member backend org-lparse-native-backends))
397 (defun org-lparse (target-backend native-backend arg
398 &optional hidden ext-plist
399 to-buffer body-only pub-dir)
400 "Export the outline to various formats.
401 If there is an active region, export only the region. The
402 outline is first exported to NATIVE-BACKEND and optionally
403 converted to TARGET-BACKEND. See `org-lparse-native-backends'
404 for list of known native backends. Each native backend can
405 specify a converter and list of target backends it exports to
406 using the CONVERT-PROCESS and OTHER-BACKENDS settings of it's get
407 method. See `org-xhtml-get' for an illustrative example.
409 ARG is a prefix argument that specifies how many levels of
410 outline should become headlines. The default is 3. Lower levels
411 will become bulleted lists.
413 HIDDEN is obsolete and does nothing.
415 EXT-PLIST is a property list that controls various aspects of
416 export. The settings here override org-mode's default settings
417 and but are inferior to file-local settings.
419 TO-BUFFER dumps the exported lines to a buffer or a string
420 instead of a file. If TO-BUFFER is the symbol `string' return the
421 exported lines as a string. If TO-BUFFER is non-nil, create a
422 buffer with that name and export to that buffer.
424 BODY-ONLY controls the presence of header and footer lines in
425 exported text. If BODY-ONLY is non-nil, don't produce the file
426 header and footer, simply return the content of <body>...</body>,
427 without even the body tags themselves.
429 PUB-DIR specifies the publishing directory."
430 (let* ((org-lparse-backend (intern native-backend))
431 (org-lparse-other-backend (and target-backend
432 (intern target-backend))))
433 (add-hook 'org-export-preprocess-hook
434 'org-lparse-strip-experimental-blocks-maybe)
435 (add-hook 'org-export-preprocess-after-blockquote-hook
436 'org-lparse-preprocess-after-blockquote)
437 (unless (org-lparse-backend-is-native-p native-backend)
438 (error "Don't know how to export natively to backend %s" native-backend))
440 (unless (or (equal native-backend target-backend)
441 (org-lparse-reachable-p native-backend target-backend))
442 (error "Don't know how to export to backend %s %s" target-backend
443 (format "via %s" native-backend)))
444 (run-hooks 'org-export-first-hook)
445 (prog1
446 (org-do-lparse arg hidden ext-plist to-buffer body-only pub-dir)
447 (remove-hook 'org-export-preprocess-hook
448 'org-lparse-strip-experimental-blocks-maybe)
449 (remove-hook 'org-export-preprocess-after-blockquote-hook
450 'org-lparse-preprocess-after-blockquote))))
452 (defcustom org-lparse-use-flashy-warning nil
453 "Control flashing of messages logged with `org-lparse-warn'.
454 When non-nil, messages are fontified with warning face and the
455 exporter lingers for a while to catch user's attention."
456 :type 'boolean
457 :group 'org-lparse)
459 (defun org-lparse-convert-read-params ()
460 "Return IN-FILE and OUT-FMT params for `org-lparse-do-convert'.
461 This is a helper routine for interactive use."
462 (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
463 (in-file (read-file-name "File to be converted: "
464 nil buffer-file-name t))
465 (in-fmt (file-name-extension in-file))
466 (out-fmt-choices (org-lparse-reachable-formats in-fmt))
467 (out-fmt
468 (or (and out-fmt-choices
469 (funcall input "Output format: "
470 out-fmt-choices nil nil nil))
471 (error
472 "No known converter or no known output formats for %s files"
473 in-fmt))))
474 (list in-file out-fmt)))
476 (eval-when-compile
477 (require 'browse-url))
479 (declare-function browse-url-file-url "browse-url" (file))
481 (defun org-lparse-do-convert (in-file out-fmt &optional prefix-arg)
482 "Workhorse routine for `org-export-odt-convert'."
483 (require 'browse-url)
484 (let* ((in-file (expand-file-name (or in-file buffer-file-name)))
485 (dummy (or (file-readable-p in-file)
486 (error "Cannot read %s" in-file)))
487 (in-fmt (file-name-extension in-file))
488 (out-fmt (or out-fmt (error "Output format unspecified")))
489 (how (or (org-lparse-reachable-p in-fmt out-fmt)
490 (error "Cannot convert from %s format to %s format?"
491 in-fmt out-fmt)))
492 (convert-process (car how))
493 (out-file (concat (file-name-sans-extension in-file) "."
494 (nth 1 (or (cdr how) out-fmt))))
495 (extra-options (or (nth 2 (cdr how)) ""))
496 (out-dir (file-name-directory in-file))
497 (cmd (format-spec convert-process
498 `((?i . ,(shell-quote-argument in-file))
499 (?I . ,(browse-url-file-url in-file))
500 (?f . ,out-fmt)
501 (?o . ,out-file)
502 (?O . ,(browse-url-file-url out-file))
503 (?d . , (shell-quote-argument out-dir))
504 (?D . ,(browse-url-file-url out-dir))
505 (?x . ,extra-options)))))
506 (when (file-exists-p out-file)
507 (delete-file out-file))
509 (message "Executing %s" cmd)
510 (let ((cmd-output (shell-command-to-string cmd)))
511 (message "%s" cmd-output))
513 (cond
514 ((file-exists-p out-file)
515 (message "Exported to %s" out-file)
516 (when prefix-arg
517 (message "Opening %s..." out-file)
518 (org-open-file out-file 'system))
519 out-file)
521 (message "Export to %s failed" out-file)
522 nil))))
524 (defvar org-lparse-insert-tag-with-newlines 'both)
526 ;; Following variables are let-bound during `org-lparse'
527 (defvar org-lparse-dyn-first-heading-pos)
528 (defvar org-lparse-toc)
529 (defvar org-lparse-entity-control-callbacks-alist)
530 (defvar org-lparse-entity-format-callbacks-alist)
531 (defvar org-lparse-backend nil
532 "The native backend to which the document is currently exported.
533 This variable is let bound during `org-lparse'. Valid values are
534 one of the symbols corresponding to `org-lparse-native-backends'.
536 Compare this variable with `org-export-current-backend' which is
537 bound only during `org-export-preprocess-string' stage of the
538 export process.
540 See also `org-lparse-other-backend'.")
542 (defvar org-lparse-other-backend nil
543 "The target backend to which the document is currently exported.
544 This variable is let bound during `org-lparse'. This variable is
545 set to either `org-lparse-backend' or one of the symbols
546 corresponding to OTHER-BACKENDS specification of the
547 org-lparse-backend.
549 For example, if a document is exported to \"odt\" then both
550 org-lparse-backend and org-lparse-other-backend are bound to
551 'odt. On the other hand, if a document is exported to \"odt\"
552 and then converted to \"doc\" then org-lparse-backend is set to
553 'odt and org-lparse-other-backend is set to 'doc.")
555 (defvar org-lparse-body-only nil
556 "Bind this to BODY-ONLY arg of `org-lparse'.")
558 (defvar org-lparse-to-buffer nil
559 "Bind this to TO-BUFFER arg of `org-lparse'.")
561 (defun org-lparse-get-block-params (params)
562 (save-match-data
563 (when params
564 (setq params (org-trim params))
565 (unless (string-match "\\`(.*)\\'" params)
566 (setq params (format "(%s)" params)))
567 (ignore-errors (read params)))))
569 (defvar org-heading-keyword-regexp-format) ; defined in org.el
570 (defvar org-lparse-special-blocks '("list-table" "annotation"))
571 (defun org-do-lparse (arg &optional hidden ext-plist
572 to-buffer body-only pub-dir)
573 "Export the outline to various formats.
574 See `org-lparse' for more information. This function is a
575 html-agnostic version of the `org-export-as-html' function in 7.5
576 version."
577 ;; Make sure we have a file name when we need it.
578 (when (and (not (or to-buffer body-only))
579 (not buffer-file-name))
580 (if (buffer-base-buffer)
581 (org-set-local 'buffer-file-name
582 (with-current-buffer (buffer-base-buffer)
583 buffer-file-name))
584 (error "Need a file name to be able to export")))
586 (org-lparse-warn
587 (format "Exporting to %s using org-lparse..."
588 (upcase (symbol-name
589 (or org-lparse-backend org-lparse-other-backend)))))
591 (setq-default org-todo-line-regexp org-todo-line-regexp)
592 (setq-default org-deadline-line-regexp org-deadline-line-regexp)
593 (setq-default org-done-keywords org-done-keywords)
594 (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
595 (let* (hfy-user-sheet-assoc ; let `htmlfontify' know that
596 ; we are interested in
597 ; collecting styles
598 org-lparse-encode-pending
599 org-lparse-par-open
600 (org-lparse-par-open-stashed 0)
602 ;; list related vars
603 (org-lparse-list-stack '())
605 ;; list-table related vars
606 org-lparse-list-table-p
607 org-lparse-list-table:table-cell-open
608 org-lparse-list-table:table-row
609 org-lparse-list-table:lines
611 org-lparse-outline-text-open
612 (org-lparse-latex-fragment-fallback ; currently used only by
613 ; odt exporter
614 (or (ignore-errors (org-lparse-get 'LATEX-FRAGMENT-FALLBACK))
615 (if (and (org-check-external-command "latex" "" t)
616 (org-check-external-command "dvipng" "" t))
617 'dvipng
618 'verbatim)))
619 (org-lparse-insert-tag-with-newlines 'both)
620 (org-lparse-to-buffer to-buffer)
621 (org-lparse-body-only body-only)
622 (org-lparse-entity-control-callbacks-alist
623 (org-lparse-get 'ENTITY-CONTROL))
624 (org-lparse-entity-format-callbacks-alist
625 (org-lparse-get 'ENTITY-FORMAT))
626 (opt-plist
627 (org-export-process-option-filters
628 (org-combine-plists (org-default-export-plist)
629 ext-plist
630 (org-infile-export-plist))))
631 (body-only (or body-only (plist-get opt-plist :body-only)))
632 valid org-lparse-dyn-first-heading-pos
633 (odd org-odd-levels-only)
634 (region-p (org-region-active-p))
635 (rbeg (and region-p (region-beginning)))
636 (rend (and region-p (region-end)))
637 (subtree-p
638 (if (plist-get opt-plist :ignore-subtree-p)
640 (when region-p
641 (save-excursion
642 (goto-char rbeg)
643 (and (org-at-heading-p)
644 (>= (org-end-of-subtree t t) rend))))))
645 (level-offset (if subtree-p
646 (save-excursion
647 (goto-char rbeg)
648 (+ (funcall outline-level)
649 (if org-odd-levels-only 1 0)))
651 (opt-plist (setq org-export-opt-plist
652 (if subtree-p
653 (org-export-add-subtree-options opt-plist rbeg)
654 opt-plist)))
655 ;; The following two are dynamically scoped into other
656 ;; routines below.
657 (org-current-export-dir
658 (or pub-dir (org-lparse-get 'EXPORT-DIR opt-plist)))
659 (org-current-export-file buffer-file-name)
660 (level 0) (line "") (origline "") txt todo
661 (umax nil)
662 (umax-toc nil)
663 (filename (if to-buffer nil
664 (expand-file-name
665 (concat
666 (file-name-sans-extension
667 (or (and subtree-p
668 (org-entry-get (region-beginning)
669 "EXPORT_FILE_NAME" t))
670 (file-name-nondirectory buffer-file-name)))
671 "." (org-lparse-get 'FILE-NAME-EXTENSION opt-plist))
672 (file-name-as-directory
673 (or pub-dir (org-lparse-get 'EXPORT-DIR opt-plist))))))
674 (current-dir (if buffer-file-name
675 (file-name-directory buffer-file-name)
676 default-directory))
677 (auto-insert nil) ; Avoid any auto-insert stuff for the new file
678 (buffer (if to-buffer
679 (cond
680 ((eq to-buffer 'string)
681 (get-buffer-create (org-lparse-get 'EXPORT-BUFFER-NAME)))
682 (t (get-buffer-create to-buffer)))
683 (find-file-noselect
684 (or (let ((f (org-lparse-get 'INIT-METHOD)))
685 (and f (functionp f) (funcall f filename)))
686 filename))))
687 (org-levels-open (make-vector org-level-max nil))
688 (dummy (mapc
689 (lambda(p)
690 (let* ((val (plist-get opt-plist p))
691 (val (org-xml-encode-org-text-skip-links val)))
692 (setq opt-plist (plist-put opt-plist p val))))
693 '(:date :author :keywords :description)))
694 (date (plist-get opt-plist :date))
695 (date (cond
696 ((and date (string-match "%" date))
697 (format-time-string date))
698 (date date)
699 (t (format-time-string "%Y-%m-%d %T %Z"))))
700 (dummy (setq opt-plist (plist-put opt-plist :effective-date date)))
701 (title (org-xml-encode-org-text-skip-links
702 (or (and subtree-p (org-export-get-title-from-subtree))
703 (plist-get opt-plist :title)
704 (and (not body-only)
705 (not
706 (plist-get opt-plist :skip-before-1st-heading))
707 (org-export-grab-title-from-buffer))
708 (and buffer-file-name
709 (file-name-sans-extension
710 (file-name-nondirectory buffer-file-name)))
711 "UNTITLED")))
712 (dummy (setq opt-plist (plist-put opt-plist :title title)))
713 (html-table-tag (plist-get opt-plist :html-table-tag))
714 (quote-re0 (concat "^ *" org-quote-string "\\( +\\|[ \t]*$\\)"))
715 (quote-re (format org-heading-keyword-regexp-format
716 org-quote-string))
717 (org-lparse-dyn-current-environment nil)
718 ;; Get the language-dependent settings
719 (lang-words (or (assoc (plist-get opt-plist :language)
720 org-export-language-setup)
721 (assoc "en" org-export-language-setup)))
722 (dummy (setq opt-plist (plist-put opt-plist :lang-words lang-words)))
723 (head-count 0) cnt
724 (start 0)
725 (coding-system-for-write
726 (or (ignore-errors (org-lparse-get 'CODING-SYSTEM-FOR-WRITE))
727 (and (boundp 'buffer-file-coding-system)
728 buffer-file-coding-system)))
729 (save-buffer-coding-system
730 (or (ignore-errors (org-lparse-get 'CODING-SYSTEM-FOR-SAVE))
731 (and (boundp 'buffer-file-coding-system)
732 buffer-file-coding-system)))
733 (region
734 (buffer-substring
735 (if region-p (region-beginning) (point-min))
736 (if region-p (region-end) (point-max))))
737 (org-export-have-math nil)
738 (org-export-footnotes-seen nil)
739 (org-export-footnotes-data (org-footnote-all-labels 'with-defs))
740 (org-footnote-insert-pos-for-preprocessor 'point-min)
741 (org-lparse-opt-plist opt-plist)
742 (lines
743 (org-split-string
744 (org-export-preprocess-string
745 region
746 :emph-multiline t
747 :for-backend (if (equal org-lparse-backend 'xhtml) ; hack
748 'html
749 org-lparse-backend)
750 :skip-before-1st-heading
751 (plist-get opt-plist :skip-before-1st-heading)
752 :drawers (plist-get opt-plist :drawers)
753 :todo-keywords (plist-get opt-plist :todo-keywords)
754 :tasks (plist-get opt-plist :tasks)
755 :tags (plist-get opt-plist :tags)
756 :priority (plist-get opt-plist :priority)
757 :footnotes (plist-get opt-plist :footnotes)
758 :timestamps (plist-get opt-plist :timestamps)
759 :archived-trees
760 (plist-get opt-plist :archived-trees)
761 :select-tags (plist-get opt-plist :select-tags)
762 :exclude-tags (plist-get opt-plist :exclude-tags)
763 :add-text
764 (plist-get opt-plist :text)
765 :LaTeX-fragments
766 (plist-get opt-plist :LaTeX-fragments))
767 "[\r\n]"))
768 table-open
769 table-buffer table-orig-buffer
771 rpl path attr desc descp desc1 desc2 link
772 snumber fnc
773 footnotes footref-seen
774 org-lparse-output-buffer
775 org-lparse-footnote-definitions
776 org-lparse-footnote-number
777 ;; collection
778 org-lparse-collect-buffer
779 (org-lparse-collect-count 0) ; things will get haywire if
780 ; collections are chained. Use
781 ; this variable to assert this
782 ; pre-requisite
783 org-lparse-toc
784 href
787 (let ((inhibit-read-only t))
788 (org-unmodified
789 (remove-text-properties (point-min) (point-max)
790 '(:org-license-to-kill t))))
792 (message "Exporting...")
793 (org-init-section-numbers)
795 ;; Switch to the output buffer
796 (setq org-lparse-output-buffer buffer)
797 (set-buffer org-lparse-output-buffer)
798 (let ((inhibit-read-only t)) (erase-buffer))
799 (fundamental-mode)
800 (org-install-letbind)
802 (and (fboundp 'set-buffer-file-coding-system)
803 (set-buffer-file-coding-system coding-system-for-write))
805 (let ((case-fold-search nil)
806 (org-odd-levels-only odd))
807 ;; create local variables for all options, to make sure all called
808 ;; functions get the correct information
809 (mapc (lambda (x)
810 (set (make-local-variable (nth 2 x))
811 (plist-get opt-plist (car x))))
812 org-export-plist-vars)
813 (setq umax (if arg (prefix-numeric-value arg)
814 org-export-headline-levels))
815 (setq umax-toc (if (integerp org-export-with-toc)
816 (min org-export-with-toc umax)
817 umax))
818 (setq org-lparse-opt-plist
819 (plist-put org-lparse-opt-plist :headline-levels umax))
821 (when (and org-export-with-toc (not body-only))
822 (setq lines (org-lparse-prepare-toc
823 lines level-offset opt-plist umax-toc)))
825 (unless body-only
826 (org-lparse-begin 'DOCUMENT-CONTENT opt-plist)
827 (org-lparse-begin 'DOCUMENT-BODY opt-plist))
829 (setq head-count 0)
830 (org-init-section-numbers)
832 (org-lparse-begin-paragraph)
834 (while (setq line (pop lines) origline line)
835 (catch 'nextline
836 (when (and (org-lparse-current-environment-p 'quote)
837 (string-match org-outline-regexp-bol line))
838 (org-lparse-end-environment 'quote))
840 (when (org-lparse-current-environment-p 'quote)
841 (org-lparse-insert 'LINE line)
842 (throw 'nextline nil))
844 ;; Fixed-width, verbatim lines (examples)
845 (when (and org-export-with-fixed-width
846 (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line))
847 (when (not (org-lparse-current-environment-p 'fixedwidth))
848 (org-lparse-begin-environment 'fixedwidth))
849 (org-lparse-insert 'LINE (match-string 3 line))
850 (when (or (not lines)
851 (not (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)"
852 (car lines))))
853 (org-lparse-end-environment 'fixedwidth))
854 (throw 'nextline nil))
856 ;; Native Text
857 (when (and (get-text-property 0 'org-native-text line)
858 ;; Make sure it is the entire line that is protected
859 (not (< (or (next-single-property-change
860 0 'org-native-text line) 10000)
861 (length line))))
862 (let ((ind (get-text-property 0 'original-indentation line)))
863 (org-lparse-begin-environment 'native)
864 (org-lparse-insert 'LINE line)
865 (while (and lines
866 (or (= (length (car lines)) 0)
867 (not ind)
868 (equal ind (get-text-property
869 0 'original-indentation (car lines))))
870 (or (= (length (car lines)) 0)
871 (get-text-property 0 'org-native-text (car lines))))
872 (org-lparse-insert 'LINE (pop lines)))
873 (org-lparse-end-environment 'native))
874 (throw 'nextline nil))
876 ;; Protected HTML
877 (when (and (get-text-property 0 'org-protected line)
878 ;; Make sure it is the entire line that is protected
879 (not (< (or (next-single-property-change
880 0 'org-protected line) 10000)
881 (length line))))
882 (let ((ind (get-text-property 0 'original-indentation line)))
883 (org-lparse-insert 'LINE line)
884 (while (and lines
885 (or (= (length (car lines)) 0)
886 (not ind)
887 (equal ind (get-text-property
888 0 'original-indentation (car lines))))
889 (or (= (length (car lines)) 0)
890 (get-text-property 0 'org-protected (car lines))))
891 (org-lparse-insert 'LINE (pop lines))))
892 (throw 'nextline nil))
894 ;; Blockquotes, verse, and center
895 (when (string-match
896 "^ORG-\\(.+\\)-\\(START\\|END\\)\\([ \t]+.*\\)?$" line)
897 (let* ((style (intern (downcase (match-string 1 line))))
898 (env-options-plist (org-lparse-get-block-params
899 (match-string 3 line)))
900 (f (cdr (assoc (match-string 2 line)
901 '(("START" . org-lparse-begin-environment)
902 ("END" . org-lparse-end-environment))))))
903 (when (memq style
904 (append
905 '(blockquote verse center)
906 (mapcar 'intern org-lparse-special-blocks)))
907 (funcall f style env-options-plist)
908 (throw 'nextline nil))))
910 (when (org-lparse-current-environment-p 'verse)
911 (let ((i (org-get-string-indentation line)))
912 (if (> i 0)
913 (setq line (concat
914 (let ((org-lparse-encode-pending t))
915 (org-lparse-format 'SPACES (* 2 i)))
916 " " (org-trim line))))
917 (unless (string-match "\\\\\\\\[ \t]*$" line)
918 (setq line (concat line "\\\\")))))
920 ;; make targets to anchors
921 (setq start 0)
922 (while (string-match
923 "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line start)
924 (cond
925 ((get-text-property (match-beginning 1) 'org-protected line)
926 (setq start (match-end 1)))
927 ((match-end 2)
928 (setq line (replace-match
929 (let ((org-lparse-encode-pending t))
930 (org-lparse-format
931 'ANCHOR "" (org-solidify-link-text
932 (match-string 1 line))))
933 t t line)))
934 ((and org-export-with-toc (equal (string-to-char line) ?*))
935 ;; FIXME: NOT DEPENDENT on TOC?????????????????????
936 (setq line (replace-match
937 (let ((org-lparse-encode-pending t))
938 (org-lparse-format
939 'FONTIFY (match-string 1 line) "target"))
940 ;; (concat "@<i>" (match-string 1 line) "@</i> ")
941 t t line)))
943 (setq line (replace-match
944 (concat
945 (let ((org-lparse-encode-pending t))
946 (org-lparse-format
947 'ANCHOR (match-string 1 line)
948 (org-solidify-link-text (match-string 1 line))
949 "target")) " ")
950 t t line)))))
952 (let ((org-lparse-encode-pending t))
953 (setq line (org-lparse-handle-time-stamps line)))
955 ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
956 ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
957 ;; Also handle sub_superscripts and checkboxes
958 (or (string-match org-table-hline-regexp line)
959 (string-match "^[ \t]*\\([+]-\\||[ ]\\)[-+ |]*[+|][ \t]*$" line)
960 (setq line (org-xml-encode-org-text-skip-links line)))
962 (setq line (org-lparse-format-org-link line opt-plist))
964 ;; TODO items
965 (if (and org-todo-line-regexp
966 (string-match org-todo-line-regexp line)
967 (match-beginning 2))
968 (setq line (concat
969 (substring line 0 (match-beginning 2))
970 (org-lparse-format 'TODO (match-string 2 line))
971 (substring line (match-end 2)))))
973 ;; Does this contain a reference to a footnote?
974 (when org-export-with-footnotes
975 (setq start 0)
976 (while (string-match "\\([^* \t].*?\\)[ \t]*\\[\\([0-9]+\\)\\]" line start)
977 ;; Discard protected matches not clearly identified as
978 ;; footnote markers.
979 (if (or (get-text-property (match-beginning 2) 'org-protected line)
980 (not (get-text-property (match-beginning 2) 'org-footnote line)))
981 (setq start (match-end 2))
982 (let ((n (match-string 2 line)) refcnt a)
983 (if (setq a (assoc n footref-seen))
984 (progn
985 (setcdr a (1+ (cdr a)))
986 (setq refcnt (cdr a)))
987 (setq refcnt 1)
988 (push (cons n 1) footref-seen))
989 (setq line
990 (replace-match
991 (concat
992 (or (match-string 1 line) "")
993 (org-lparse-format
994 'FOOTNOTE-REFERENCE
995 n (cdr (assoc n org-lparse-footnote-definitions))
996 refcnt)
997 ;; If another footnote is following the
998 ;; current one, add a separator.
999 (if (save-match-data
1000 (string-match "\\`\\[[0-9]+\\]"
1001 (substring line (match-end 0))))
1002 (ignore-errors
1003 (org-lparse-get 'FOOTNOTE-SEPARATOR))
1004 ""))
1005 t t line))))))
1007 (cond
1008 ((string-match "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$" line)
1009 ;; This is a headline
1010 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
1011 level-offset))
1012 txt (match-string 2 line))
1013 (if (string-match quote-re0 txt)
1014 (setq txt (replace-match "" t t txt)))
1015 (if (<= level (max umax umax-toc))
1016 (setq head-count (+ head-count 1)))
1017 (unless org-lparse-dyn-first-heading-pos
1018 (setq org-lparse-dyn-first-heading-pos (point)))
1019 (org-lparse-begin-level level txt umax head-count)
1021 ;; QUOTES
1022 (when (string-match quote-re line)
1023 (org-lparse-begin-environment 'quote)))
1025 ((and org-export-with-tables
1026 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
1027 (when (not table-open)
1028 ;; New table starts
1029 (setq table-open t table-buffer nil table-orig-buffer nil))
1031 ;; Accumulate lines
1032 (setq table-buffer (cons line table-buffer)
1033 table-orig-buffer (cons origline table-orig-buffer))
1034 (when (or (not lines)
1035 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
1036 (car lines))))
1037 (setq table-open nil
1038 table-buffer (nreverse table-buffer)
1039 table-orig-buffer (nreverse table-orig-buffer))
1040 (org-lparse-end-paragraph)
1041 (when org-lparse-list-table-p
1042 (error "Regular tables are not allowed in a list-table block"))
1043 (org-lparse-insert 'TABLE table-buffer table-orig-buffer)))
1045 ;; Normal lines
1047 ;; This line either is list item or end a list.
1048 (when (get-text-property 0 'list-item line)
1049 (setq line (org-lparse-export-list-line
1050 line
1051 (get-text-property 0 'list-item line)
1052 (get-text-property 0 'list-struct line)
1053 (get-text-property 0 'list-prevs line))))
1055 ;; Horizontal line
1056 (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
1057 (with-org-lparse-preserve-paragraph-state
1058 (org-lparse-insert 'HORIZONTAL-LINE))
1059 (throw 'nextline nil))
1061 ;; Empty lines start a new paragraph. If hand-formatted lists
1062 ;; are not fully interpreted, lines starting with "-", "+", "*"
1063 ;; also start a new paragraph.
1064 (when (string-match "^ [-+*]-\\|^[ \t]*$" line)
1065 (when org-lparse-footnote-number
1066 (org-lparse-end-footnote-definition org-lparse-footnote-number)
1067 (setq org-lparse-footnote-number nil))
1068 (org-lparse-begin-paragraph))
1070 ;; Is this the start of a footnote?
1071 (when org-export-with-footnotes
1072 (when (and (boundp 'footnote-section-tag-regexp)
1073 (string-match (concat "^" footnote-section-tag-regexp)
1074 line))
1075 ;; ignore this line
1076 (throw 'nextline nil))
1077 (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
1078 (org-lparse-end-paragraph)
1079 (setq org-lparse-footnote-number (match-string 1 line))
1080 (setq line (replace-match "" t t line))
1081 (org-lparse-begin-footnote-definition org-lparse-footnote-number)))
1082 ;; Check if the line break needs to be conserved
1083 (cond
1084 ((string-match "\\\\\\\\[ \t]*$" line)
1085 (setq line (replace-match
1086 (org-lparse-format 'LINE-BREAK)
1087 t t line)))
1088 (org-export-preserve-breaks
1089 (setq line (concat line (org-lparse-format 'LINE-BREAK)))))
1091 ;; Check if a paragraph should be started
1092 (let ((start 0))
1093 (while (and org-lparse-par-open
1094 (string-match "\\\\par\\>" line start))
1095 (error "FIXME")
1096 ;; Leave a space in the </p> so that the footnote matcher
1097 ;; does not see this.
1098 (if (not (get-text-property (match-beginning 0)
1099 'org-protected line))
1100 (setq line (replace-match "</p ><p >" t t line)))
1101 (setq start (match-end 0))))
1103 (org-lparse-insert 'LINE line)))))
1105 ;; Properly close all local lists and other lists
1106 (when (org-lparse-current-environment-p 'quote)
1107 (org-lparse-end-environment 'quote))
1109 (org-lparse-end-level 1 umax)
1111 ;; the </div> to close the last text-... div.
1112 (when (and (> umax 0) org-lparse-dyn-first-heading-pos)
1113 (org-lparse-end-outline-text-or-outline))
1115 (org-lparse-end 'DOCUMENT-BODY opt-plist)
1116 (unless body-only
1117 (org-lparse-end 'DOCUMENT-CONTENT))
1119 (org-lparse-end 'EXPORT)
1121 ;; kill collection buffer
1122 (when org-lparse-collect-buffer
1123 (kill-buffer org-lparse-collect-buffer))
1125 (goto-char (point-min))
1126 (or (org-export-push-to-kill-ring
1127 (upcase (symbol-name org-lparse-backend)))
1128 (message "Exporting... done"))
1130 (cond
1131 ((not to-buffer)
1132 (let ((f (org-lparse-get 'SAVE-METHOD)))
1133 (or (and f (functionp f) (funcall f filename opt-plist))
1134 (save-buffer)))
1135 (or (and (boundp 'org-lparse-other-backend)
1136 org-lparse-other-backend
1137 (not (equal org-lparse-backend org-lparse-other-backend))
1138 (org-lparse-do-convert
1139 buffer-file-name (symbol-name org-lparse-other-backend)))
1140 (current-buffer)))
1141 ((eq to-buffer 'string)
1142 (prog1 (buffer-substring (point-min) (point-max))
1143 (kill-buffer (current-buffer))))
1144 (t (current-buffer))))))
1146 (defun org-lparse-format-table (lines olines)
1147 "Returns backend-specific code for org-type and table-type tables."
1148 (if (stringp lines)
1149 (setq lines (org-split-string lines "\n")))
1150 (if (string-match "^[ \t]*|" (car lines))
1151 ;; A normal org table
1152 (org-lparse-format-org-table lines nil)
1153 ;; Table made by table.el
1154 (or (org-lparse-format-table-table-using-table-generate-source
1155 ;; FIXME: Need to take care of this during merge
1156 (if (eq org-lparse-backend 'xhtml) 'html org-lparse-backend)
1157 olines
1158 (not org-export-prefer-native-exporter-for-tables))
1159 ;; We are here only when table.el table has NO col or row
1160 ;; spanning and the user prefers using org's own converter for
1161 ;; exporting of such simple table.el tables.
1162 (org-lparse-format-table-table lines))))
1164 (defun org-lparse-table-get-colalign-info (lines)
1165 (let ((col-cookies (org-find-text-property-in-string
1166 'org-col-cookies (car lines))))
1167 (when (and col-cookies org-table-clean-did-remove-column)
1168 (setq col-cookies
1169 (mapcar (lambda (x) (cons (1- (car x)) (cdr x))) col-cookies)))
1170 col-cookies))
1172 (defvar org-lparse-table-style)
1173 (defvar org-lparse-table-ncols)
1174 (defvar org-lparse-table-rownum)
1175 (defvar org-lparse-table-is-styled)
1176 (defvar org-lparse-table-begin-marker)
1177 (defvar org-lparse-table-num-numeric-items-per-column)
1178 (defvar org-lparse-table-colalign-info)
1179 (defvar org-lparse-table-colalign-vector)
1181 ;; Following variables are defined in org-table.el
1182 (defvar org-table-number-fraction)
1183 (defvar org-table-number-regexp)
1184 (defun org-lparse-org-table-to-list-table (lines &optional splice)
1185 "Convert org-table to list-table.
1186 LINES is a list of the form (ROW1 ROW2 ROW3 ...) where each
1187 element is a `string' representing a single row of org-table.
1188 Thus each ROW has vertical separators \"|\" separating the table
1189 fields. A ROW could also be a row-group separator of the form
1190 \"|---...|\". Return a list of the form (ROW1 ROW2 ROW3
1191 ...). ROW could either be symbol `:hrule' or a list of the
1192 form (FIELD1 FIELD2 FIELD3 ...) as appropriate."
1193 (let (line lines-1)
1194 (cond
1195 (splice
1196 (while (setq line (pop lines))
1197 (unless (string-match "^[ \t]*|-" line)
1198 (push (org-split-string line "[ \t]*|[ \t]*") lines-1))))
1200 (while (setq line (pop lines))
1201 (cond
1202 ((string-match "^[ \t]*|-" line)
1203 (when lines
1204 (push :hrule lines-1)))
1206 (push (org-split-string line "[ \t]*|[ \t]*") lines-1))))))
1207 (nreverse lines-1)))
1209 (defun org-lparse-insert-org-table (lines &optional splice)
1210 "Format a org-type table into backend-specific code.
1211 LINES is a list of lines. Optional argument SPLICE means, do not
1212 insert header and surrounding <table> tags, just format the lines.
1213 Optional argument NO-CSS means use XHTML attributes instead of CSS
1214 for formatting. This is required for the DocBook exporter."
1215 (require 'org-table)
1216 ;; Get rid of hlines at beginning and end
1217 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
1218 (setq lines (nreverse lines))
1219 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
1220 (setq lines (nreverse lines))
1221 (when org-export-table-remove-special-lines
1222 ;; Check if the table has a marking column. If yes remove the
1223 ;; column and the special lines
1224 (setq lines (org-table-clean-before-export lines)))
1225 (let* ((caption (org-find-text-property-in-string 'org-caption (car lines)))
1226 (short-caption (or (org-find-text-property-in-string
1227 'org-caption-shortn (car lines)) caption))
1228 (caption (and caption (org-xml-encode-org-text caption)))
1229 (short-caption (and short-caption
1230 (org-xml-encode-plain-text short-caption)))
1231 (label (org-find-text-property-in-string 'org-label (car lines)))
1232 (org-lparse-table-colalign-info (org-lparse-table-get-colalign-info lines))
1233 (attributes (org-find-text-property-in-string 'org-attributes
1234 (car lines)))
1235 (head (and org-export-highlight-first-table-line
1236 (delq nil (mapcar
1237 (lambda (x) (string-match "^[ \t]*|-" x))
1238 (cdr lines))))))
1239 (setq lines (org-lparse-org-table-to-list-table lines splice))
1240 (org-lparse-insert-list-table
1241 lines splice caption label attributes head org-lparse-table-colalign-info
1242 short-caption)))
1244 (defun org-lparse-insert-list-table (lines &optional splice
1245 caption label attributes head
1246 org-lparse-table-colalign-info
1247 short-caption)
1248 (or (featurep 'org-table) ; required for
1249 (require 'org-table)) ; `org-table-number-regexp'
1250 (let* ((org-lparse-table-rownum -1) org-lparse-table-ncols i (cnt 0)
1251 tbopen fields line
1252 org-lparse-table-cur-rowgrp-is-hdr
1253 org-lparse-table-rowgrp-open
1254 org-lparse-table-num-numeric-items-per-column
1255 org-lparse-table-colalign-vector n
1256 org-lparse-table-rowgrp-info
1257 org-lparse-table-begin-marker
1258 (org-lparse-table-style 'org-table)
1259 org-lparse-table-is-styled)
1260 (cond
1261 (splice
1262 (setq org-lparse-table-is-styled nil)
1263 (while (setq line (pop lines))
1264 (insert (org-lparse-format-table-row line) "\n")))
1266 (setq org-lparse-table-is-styled t)
1267 (org-lparse-begin 'TABLE caption label attributes short-caption)
1268 (setq org-lparse-table-begin-marker (point))
1269 (org-lparse-begin-table-rowgroup head)
1270 (while (setq line (pop lines))
1271 (cond
1272 ((equal line :hrule)
1273 (org-lparse-begin-table-rowgroup))
1275 (insert (org-lparse-format-table-row line) "\n"))))
1276 (org-lparse-end 'TABLE-ROWGROUP)
1277 (org-lparse-end-table)))))
1279 (defun org-lparse-format-org-table (lines &optional splice)
1280 (with-temp-buffer
1281 (org-lparse-insert-org-table lines splice)
1282 (buffer-substring-no-properties (point-min) (point-max))))
1284 (defun org-lparse-format-list-table (lines &optional splice)
1285 (with-temp-buffer
1286 (org-lparse-insert-list-table lines splice)
1287 (buffer-substring-no-properties (point-min) (point-max))))
1289 (defun org-lparse-insert-table-table (lines)
1290 "Format a table generated by table.el into backend-specific code.
1291 This conversion does *not* use `table-generate-source' from table.el.
1292 This has the advantage that Org-mode's HTML conversions can be used.
1293 But it has the disadvantage, that no cell- or row-spanning is allowed."
1294 (let (line field-buffer
1295 (org-lparse-table-cur-rowgrp-is-hdr
1296 org-export-highlight-first-table-line)
1297 (caption nil)
1298 (short-caption nil)
1299 (attributes nil)
1300 (label nil)
1301 (org-lparse-table-style 'table-table)
1302 (org-lparse-table-is-styled nil)
1303 fields org-lparse-table-ncols i (org-lparse-table-rownum -1)
1304 (empty (org-lparse-format 'SPACES 1)))
1305 (org-lparse-begin 'TABLE caption label attributes short-caption)
1306 (while (setq line (pop lines))
1307 (cond
1308 ((string-match "^[ \t]*\\+-" line)
1309 (when field-buffer
1310 (let ((org-export-table-row-tags '("<tr>" . "</tr>"))
1311 ;; (org-export-html-table-use-header-tags-for-first-column nil)
1313 (insert (org-lparse-format-table-row field-buffer empty)))
1314 (setq org-lparse-table-cur-rowgrp-is-hdr nil)
1315 (setq field-buffer nil)))
1317 ;; Break the line into fields and store the fields
1318 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
1319 (if field-buffer
1320 (setq field-buffer (mapcar
1321 (lambda (x)
1322 (concat x (org-lparse-format 'LINE-BREAK)
1323 (pop fields)))
1324 field-buffer))
1325 (setq field-buffer fields)))))
1326 (org-lparse-end-table)))
1328 (defun org-lparse-format-table-table (lines)
1329 (with-temp-buffer
1330 (org-lparse-insert-table-table lines)
1331 (buffer-substring-no-properties (point-min) (point-max))))
1333 (defvar table-source-languages) ; defined in table.el
1334 (defun org-lparse-format-table-table-using-table-generate-source (backend
1335 lines
1336 &optional
1337 spanned-only)
1338 "Format a table into BACKEND, using `table-generate-source' from table.el.
1339 Use SPANNED-ONLY to suppress exporting of simple table.el tables.
1341 When SPANNED-ONLY is nil, all table.el tables are exported. When
1342 SPANNED-ONLY is non-nil, only tables with either row or column
1343 spans are exported.
1345 This routine returns the generated source or nil as appropriate.
1347 Refer docstring of `org-export-prefer-native-exporter-for-tables'
1348 for further information."
1349 (require 'table)
1350 (with-current-buffer (get-buffer-create " org-tmp1 ")
1351 (erase-buffer)
1352 (insert (mapconcat 'identity lines "\n"))
1353 (goto-char (point-min))
1354 (if (not (re-search-forward "|[^+]" nil t))
1355 (error "Error processing table"))
1356 (table-recognize-table)
1357 (when (or (not spanned-only)
1358 (let* ((dim (table-query-dimension))
1359 (c (nth 4 dim)) (r (nth 5 dim)) (cells (nth 6 dim)))
1360 (not (= (* c r) cells))))
1361 (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
1362 (cond
1363 ((member backend table-source-languages)
1364 (table-generate-source backend " org-tmp2 ")
1365 (set-buffer " org-tmp2 ")
1366 (buffer-substring (point-min) (point-max)))
1368 ;; table.el doesn't support the given backend. Currently this
1369 ;; happens in case of odt export. Strip the table from the
1370 ;; generated document. A better alternative would be to embed
1371 ;; the table as ascii text in the output document.
1372 (org-lparse-warn
1373 (concat
1374 "Found table.el-type table in the source org file. "
1375 (format "table.el doesn't support %s backend. "
1376 (upcase (symbol-name backend)))
1377 "Skipping ahead ..."))
1378 "")))))
1380 (defun org-lparse-handle-time-stamps (s)
1381 "Format time stamps in string S, or remove them."
1382 (catch 'exit
1383 (let (r b)
1384 (when org-maybe-keyword-time-regexp
1385 (while (string-match org-maybe-keyword-time-regexp s)
1386 (or b (setq b (substring s 0 (match-beginning 0))))
1387 (setq r (concat
1388 r (substring s 0 (match-beginning 0)) " "
1389 (org-lparse-format
1390 'FONTIFY
1391 (concat
1392 (if (match-end 1)
1393 (org-lparse-format
1394 'FONTIFY
1395 (match-string 1 s) "timestamp-kwd"))
1397 (org-lparse-format
1398 'FONTIFY
1399 (substring (org-translate-time (match-string 3 s)) 1 -1)
1400 "timestamp"))
1401 "timestamp-wrapper"))
1402 s (substring s (match-end 0)))))
1404 ;; Line break if line started and ended with time stamp stuff
1405 (if (not r)
1407 (setq r (concat r s))
1408 (unless (string-match "\\S-" (concat b s))
1409 (setq r (concat r (org-lparse-format 'LINE-BREAK))))
1410 r))))
1412 (defun org-xml-encode-plain-text (s)
1413 "Convert plain text characters to HTML equivalent.
1414 Possible conversions are set in `org-export-html-protect-char-alist'."
1415 (let ((cl (org-lparse-get 'PLAIN-TEXT-MAP)) c)
1416 (while (setq c (pop cl))
1417 (let ((start 0))
1418 (while (string-match (car c) s start)
1419 (setq s (replace-match (cdr c) t t s)
1420 start (1+ (match-beginning 0))))))
1423 (defun org-xml-encode-org-text-skip-links (string)
1424 "Prepare STRING for HTML export. Apply all active conversions.
1425 If there are links in the string, don't modify these. If STRING
1426 is nil, return nil."
1427 (when string
1428 (let* ((re (concat org-bracket-link-regexp "\\|"
1429 (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")))
1430 m s l res)
1431 (while (setq m (string-match re string))
1432 (setq s (substring string 0 m)
1433 l (match-string 0 string)
1434 string (substring string (match-end 0)))
1435 (push (org-xml-encode-org-text s) res)
1436 (push l res))
1437 (push (org-xml-encode-org-text string) res)
1438 (apply 'concat (nreverse res)))))
1440 (defun org-xml-encode-org-text (s)
1441 "Apply all active conversions to translate special ASCII to HTML."
1442 (setq s (org-xml-encode-plain-text s))
1443 (if org-export-html-expand
1444 (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
1445 (setq s (replace-match "<\\1>" t nil s))))
1446 (if org-export-with-emphasize
1447 (setq s (org-lparse-apply-char-styles s)))
1448 (if org-export-with-special-strings
1449 (setq s (org-lparse-convert-special-strings s)))
1450 (if org-export-with-sub-superscripts
1451 (setq s (org-lparse-apply-sub-superscript-styles s)))
1452 (if org-export-with-TeX-macros
1453 (let ((start 0) wd rep)
1454 (while (setq start (string-match "\\\\\\([a-zA-Z]+[0-9]*\\)\\({}\\)?"
1455 s start))
1456 (if (get-text-property (match-beginning 0) 'org-protected s)
1457 (setq start (match-end 0))
1458 (setq wd (match-string 1 s))
1459 (if (setq rep (org-lparse-format 'ORG-ENTITY wd))
1460 (setq s (replace-match rep t t s))
1461 (setq start (+ start (length wd))))))))
1464 (defun org-lparse-convert-special-strings (string)
1465 "Convert special characters in STRING to HTML."
1466 (let ((all (org-lparse-get 'SPECIAL-STRING-REGEXPS))
1467 e a re rpl start)
1468 (while (setq a (pop all))
1469 (setq re (car a) rpl (cdr a) start 0)
1470 (while (string-match re string start)
1471 (if (get-text-property (match-beginning 0) 'org-protected string)
1472 (setq start (match-end 0))
1473 (setq string (replace-match rpl t nil string)))))
1474 string))
1476 (defun org-lparse-apply-sub-superscript-styles (string)
1477 "Apply subscript and superscript styles to STRING.
1478 Use `org-export-with-sub-superscripts' to control application of
1479 sub and superscript styles."
1480 (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
1481 (while (string-match org-match-substring-regexp string s)
1482 (cond
1483 ((and requireb (match-end 8)) (setq s (match-end 2)))
1484 ((get-text-property (match-beginning 2) 'org-protected string)
1485 (setq s (match-end 2)))
1487 (setq s (match-end 1)
1488 key (if (string= (match-string 2 string) "_")
1489 'subscript 'superscript)
1490 c (or (match-string 8 string)
1491 (match-string 6 string)
1492 (match-string 5 string))
1493 string (replace-match
1494 (concat (match-string 1 string)
1495 (org-lparse-format 'FONTIFY c key))
1496 t t string)))))
1497 (while (string-match "\\\\\\([_^]\\)" string)
1498 (setq string (replace-match (match-string 1 string) t t string)))
1499 string))
1501 (defvar org-lparse-char-styles
1502 `(("*" bold)
1503 ("/" emphasis)
1504 ("_" underline)
1505 ("=" code)
1506 ("~" verbatim)
1507 ("+" strike))
1508 "Map Org emphasis markers to char styles.
1509 This is an alist where each element is of the
1510 form (ORG-EMPHASIS-CHAR . CHAR-STYLE).")
1512 (defun org-lparse-apply-char-styles (string)
1513 "Apply char styles to STRING.
1514 The variable `org-lparse-char-styles' controls how the Org
1515 emphasis markers are interpreted."
1516 (let ((s 0) rpl)
1517 (while (string-match org-emph-re string s)
1518 (if (not (equal
1519 (substring string (match-beginning 3) (1+ (match-beginning 3)))
1520 (substring string (match-beginning 4) (1+ (match-beginning 4)))))
1521 (setq s (match-beginning 0)
1523 (concat
1524 (match-string 1 string)
1525 (org-lparse-format
1526 'FONTIFY (match-string 4 string)
1527 (nth 1 (assoc (match-string 3 string)
1528 org-lparse-char-styles)))
1529 (match-string 5 string))
1530 string (replace-match rpl t t string)
1531 s (+ s (- (length rpl) 2)))
1532 (setq s (1+ s))))
1533 string))
1535 (defun org-lparse-export-list-line (line pos struct prevs)
1536 "Insert list syntax in export buffer. Return LINE, maybe modified.
1538 POS is the item position or line position the line had before
1539 modifications to buffer. STRUCT is the list structure. PREVS is
1540 the alist of previous items."
1541 (let* ((get-type
1542 (function
1543 ;; Translate type of list containing POS to "d", "o" or
1544 ;; "u".
1545 (lambda (pos struct prevs)
1546 (let ((type (org-list-get-list-type pos struct prevs)))
1547 (cond
1548 ((eq 'ordered type) "o")
1549 ((eq 'descriptive type) "d")
1550 (t "u"))))))
1551 (get-closings
1552 (function
1553 ;; Return list of all items and sublists ending at POS, in
1554 ;; reverse order.
1555 (lambda (pos)
1556 (let (out)
1557 (catch 'exit
1558 (mapc (lambda (e)
1559 (let ((end (nth 6 e))
1560 (item (car e)))
1561 (cond
1562 ((= end pos) (push item out))
1563 ((>= item pos) (throw 'exit nil)))))
1564 struct))
1565 out)))))
1566 ;; First close any previous item, or list, ending at POS.
1567 (mapc (lambda (e)
1568 (let* ((lastp (= (org-list-get-last-item e struct prevs) e))
1569 (first-item (org-list-get-list-begin e struct prevs))
1570 (type (funcall get-type first-item struct prevs)))
1571 (org-lparse-end-paragraph)
1572 ;; Ending for every item
1573 (org-lparse-end-list-item-1 type)
1574 ;; We're ending last item of the list: end list.
1575 (when lastp
1576 (org-lparse-end-list type)
1577 (org-lparse-begin-paragraph))))
1578 (funcall get-closings pos))
1579 (cond
1580 ;; At an item: insert appropriate tags in export buffer.
1581 ((assq pos struct)
1582 (string-match
1583 (concat "[ \t]*\\(\\S-+[ \t]*\\)"
1584 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
1585 "\\(?:\\(\\[[ X-]\\]\\)[ \t]+\\)?"
1586 "\\(?:\\(.*\\)[ \t]+::\\(?:[ \t]+\\|$\\)\\)?"
1587 "\\(.*\\)") line)
1588 (let* ((checkbox (match-string 3 line))
1589 (desc-tag (or (match-string 4 line) "???"))
1590 (body (or (match-string 5 line) ""))
1591 (list-beg (org-list-get-list-begin pos struct prevs))
1592 (firstp (= list-beg pos))
1593 ;; Always refer to first item to determine list type, in
1594 ;; case list is ill-formed.
1595 (type (funcall get-type list-beg struct prevs))
1596 (counter (let ((count-tmp (org-list-get-counter pos struct)))
1597 (cond
1598 ((not count-tmp) nil)
1599 ((string-match "[A-Za-z]" count-tmp)
1600 (- (string-to-char (upcase count-tmp)) 64))
1601 ((string-match "[0-9]+" count-tmp)
1602 count-tmp)))))
1603 (when firstp
1604 (org-lparse-end-paragraph)
1605 (org-lparse-begin-list type))
1607 (let ((arg (cond ((equal type "d") desc-tag)
1608 ((equal type "o") counter))))
1609 (org-lparse-begin-list-item type arg))
1611 ;; If line had a checkbox, some additional modification is required.
1612 (when checkbox
1613 (setq body
1614 (concat
1615 (org-lparse-format
1616 'FONTIFY (concat
1618 (cond
1619 ((string-match "X" checkbox) "X")
1620 ((string-match " " checkbox)
1621 (org-lparse-format 'SPACES 1))
1622 (t "-"))
1623 "]")
1624 'code)
1626 body)))
1627 ;; Return modified line
1628 body))
1629 ;; At a list ender: go to next line (side-effects only).
1630 ((equal "ORG-LIST-END-MARKER" line) (throw 'nextline nil))
1631 ;; Not at an item: return line unchanged (side-effects only).
1632 (t line))))
1634 (defun org-lparse-bind-local-variables (opt-plist)
1635 (mapc (lambda (x)
1636 (set (make-local-variable (nth 2 x))
1637 (plist-get opt-plist (car x))))
1638 org-export-plist-vars))
1640 (defvar org-lparse-table-rowgrp-open)
1641 (defvar org-lparse-table-cur-rowgrp-is-hdr)
1642 (defvar org-lparse-footnote-number)
1643 (defvar org-lparse-footnote-definitions)
1644 (defvar org-lparse-output-buffer nil
1645 "Buffer to which `org-do-lparse' writes to.
1646 This buffer contains the contents of the to-be-created exported
1647 document.")
1649 (defcustom org-lparse-debug nil
1650 "Enable or Disable logging of `org-lparse' callbacks.
1651 The parameters passed to the backend-registered ENTITY-CONTROL
1652 and ENTITY-FORMAT callbacks are logged as comment strings in the
1653 exported buffer. (org-lparse-format 'COMMENT fmt args) is used
1654 for logging. Customize this variable only if you are an expert
1655 user. Valid values of this variable are:
1656 nil : Disable logging
1657 control : Log all invocations of `org-lparse-begin' and
1658 `org-lparse-end' callbacks.
1659 format : Log invocations of `org-lparse-format' callbacks.
1660 t : Log all invocations of `org-lparse-begin', `org-lparse-end'
1661 and `org-lparse-format' callbacks,"
1662 :group 'org-lparse
1663 :type '(choice
1664 (const :tag "Disable" nil)
1665 (const :tag "Format callbacks" format)
1666 (const :tag "Control callbacks" control)
1667 (const :tag "Format and Control callbacks" t)))
1669 (defun org-lparse-begin (entity &rest args)
1670 "Begin ENTITY in current buffer. ARGS is entity specific.
1671 ENTITY can be one of PARAGRAPH, LIST, LIST-ITEM etc.
1673 Use (org-lparse-begin 'LIST \"o\") to begin a list in current
1674 buffer.
1676 See `org-xhtml-entity-control-callbacks-alist' for more
1677 information."
1678 (when (and (member org-lparse-debug '(t control))
1679 (not (eq entity 'DOCUMENT-CONTENT)))
1680 (insert (org-lparse-format 'COMMENT "%s BEGIN %S" entity args)))
1682 (let ((f (cadr (assoc entity org-lparse-entity-control-callbacks-alist))))
1683 (unless f (error "Unknown entity: %s" entity))
1684 (apply f args)))
1686 (defun org-lparse-end (entity &rest args)
1687 "Close ENTITY in current buffer. ARGS is entity specific.
1688 ENTITY can be one of PARAGRAPH, LIST, LIST-ITEM
1689 etc.
1691 Use (org-lparse-end 'LIST \"o\") to close a list in current
1692 buffer.
1694 See `org-xhtml-entity-control-callbacks-alist' for more
1695 information."
1696 (when (and (member org-lparse-debug '(t control))
1697 (not (eq entity 'DOCUMENT-CONTENT)))
1698 (insert (org-lparse-format 'COMMENT "%s END %S" entity args)))
1700 (let ((f (caddr (assoc entity org-lparse-entity-control-callbacks-alist))))
1701 (unless f (error "Unknown entity: %s" entity))
1702 (apply f args)))
1704 (defun org-lparse-begin-paragraph (&optional style)
1705 "Insert <p>, but first close previous paragraph if any."
1706 (org-lparse-end-paragraph)
1707 (org-lparse-begin 'PARAGRAPH style)
1708 (setq org-lparse-par-open t))
1710 (defun org-lparse-end-paragraph ()
1711 "Close paragraph if there is one open."
1712 (when org-lparse-par-open
1713 (org-lparse-end 'PARAGRAPH)
1714 (setq org-lparse-par-open nil)))
1716 (defun org-lparse-end-list-item-1 (&optional type)
1717 "Close <li> if necessary."
1718 (org-lparse-end-paragraph)
1719 (org-lparse-end-list-item (or type "u")))
1721 (define-obsolete-function-alias
1722 'org-lparse-preprocess-after-blockquote-hook
1723 'org-lparse-preprocess-after-blockquote
1724 "24.3")
1726 (defun org-lparse-preprocess-after-blockquote ()
1727 "Treat `org-lparse-special-blocks' specially."
1728 (goto-char (point-min))
1729 (while (re-search-forward
1730 "^[ \t]*#\\+\\(begin\\|end\\)_\\(\\S-+\\)[ \t]*\\(.*\\)$" nil t)
1731 (when (member (downcase (match-string 2)) org-lparse-special-blocks)
1732 (replace-match
1733 (if (equal (downcase (match-string 1)) "begin")
1734 (format "ORG-%s-START %s" (upcase (match-string 2))
1735 (match-string 3))
1736 (format "ORG-%s-END %s" (upcase (match-string 2))
1737 (match-string 3))) t t))))
1739 (define-obsolete-function-alias
1740 'org-lparse-strip-experimental-blocks-maybe-hook
1741 'org-lparse-strip-experimental-blocks-maybe
1742 "24.3")
1744 (defun org-lparse-strip-experimental-blocks-maybe ()
1745 "Strip \"list-table\" and \"annotation\" blocks.
1746 Stripping happens only when the exported backend is not one of
1747 \"odt\" or \"xhtml\"."
1748 (when (not org-lparse-backend)
1749 (message "Stripping following blocks - %S" org-lparse-special-blocks)
1750 (goto-char (point-min))
1751 (let ((case-fold-search t))
1752 (while
1753 (re-search-forward
1754 "^[ \t]*#\\+begin_\\(\\S-+\\)\\([ \t]+.*\\)?\n\\([^\000]*?\\)\n[ \t]*#\\+end_\\1\\>.*"
1755 nil t)
1756 (when (member (match-string 1) org-lparse-special-blocks)
1757 (replace-match "" t t))))))
1759 (defvar org-lparse-list-table-p nil
1760 "Non-nil if `org-do-lparse' is within a list-table.")
1762 (defvar org-lparse-dyn-current-environment nil)
1763 (defun org-lparse-begin-environment (style &optional env-options-plist)
1764 (case style
1765 (list-table
1766 (setq org-lparse-list-table-p t))
1767 (t (setq org-lparse-dyn-current-environment style)
1768 (org-lparse-begin 'ENVIRONMENT style env-options-plist))))
1770 (defun org-lparse-end-environment (style &optional env-options-plist)
1771 (case style
1772 (list-table
1773 (setq org-lparse-list-table-p nil))
1774 (t (org-lparse-end 'ENVIRONMENT style env-options-plist)
1775 (setq org-lparse-dyn-current-environment nil))))
1777 (defun org-lparse-current-environment-p (style)
1778 (eq org-lparse-dyn-current-environment style))
1780 (defun org-lparse-begin-footnote-definition (n)
1781 (org-lparse-begin-collect)
1782 (setq org-lparse-insert-tag-with-newlines nil)
1783 (org-lparse-begin 'FOOTNOTE-DEFINITION n))
1785 (defun org-lparse-end-footnote-definition (n)
1786 (org-lparse-end 'FOOTNOTE-DEFINITION n)
1787 (setq org-lparse-insert-tag-with-newlines 'both)
1788 (let ((footnote-def (org-lparse-end-collect)))
1789 ;; Cleanup newlines in footnote definition. This ensures that a
1790 ;; transcoded line is never (wrongly) broken in to multiple lines.
1791 (let ((pos 0))
1792 (while (string-match "[\r\n]+" footnote-def pos)
1793 (setq pos (1+ (match-beginning 0)))
1794 (setq footnote-def (replace-match " " t t footnote-def))))
1795 (push (cons n footnote-def) org-lparse-footnote-definitions)))
1797 (defvar org-lparse-collect-buffer nil
1798 "An auxiliary buffer named \"*Org Lparse Collect*\".
1799 `org-do-lparse' uses this as output buffer while collecting
1800 footnote definitions and table-cell contents of list-tables. See
1801 `org-lparse-begin-collect' and `org-lparse-end-collect'.")
1803 (defvar org-lparse-collect-count nil
1804 "Count number of calls to `org-lparse-begin-collect'.
1805 Use this counter to catch chained collections if they ever
1806 happen.")
1808 (defun org-lparse-begin-collect ()
1809 "Temporarily switch to `org-lparse-collect-buffer'.
1810 Also erase it's contents."
1811 (unless (zerop org-lparse-collect-count)
1812 (error "FIXME (org-lparse.el): Encountered chained collections"))
1813 (incf org-lparse-collect-count)
1814 (unless org-lparse-collect-buffer
1815 (setq org-lparse-collect-buffer
1816 (get-buffer-create "*Org Lparse Collect*")))
1817 (set-buffer org-lparse-collect-buffer)
1818 (erase-buffer))
1820 (defun org-lparse-end-collect ()
1821 "Switch to `org-lparse-output-buffer'.
1822 Return contents of `org-lparse-collect-buffer' as a `string'."
1823 (assert (> org-lparse-collect-count 0))
1824 (decf org-lparse-collect-count)
1825 (prog1 (buffer-string)
1826 (erase-buffer)
1827 (set-buffer org-lparse-output-buffer)))
1829 (defun org-lparse-format (entity &rest args)
1830 "Format ENTITY in backend-specific way and return it.
1831 ARGS is specific to entity being formatted.
1833 Use (org-lparse-format 'HEADING \"text\" 1) to format text as
1834 level 1 heading.
1836 See `org-xhtml-entity-format-callbacks-alist' for more information."
1837 (when (and (member org-lparse-debug '(t format))
1838 (not (equal entity 'COMMENT)))
1839 (insert (org-lparse-format 'COMMENT "%s: %S" entity args)))
1840 (cond
1841 ((consp entity)
1842 (let ((text (pop args)))
1843 (apply 'org-lparse-format 'TAGS entity text args)))
1845 (let ((f (cdr (assoc entity org-lparse-entity-format-callbacks-alist))))
1846 (unless f (error "Unknown entity: %s" entity))
1847 (apply f args)))))
1849 (defun org-lparse-insert (entity &rest args)
1850 (insert (apply 'org-lparse-format entity args)))
1852 (defun org-lparse-prepare-toc (lines level-offset opt-plist umax-toc)
1853 (let* ((quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
1854 (org-min-level (org-get-min-level lines level-offset))
1855 (org-last-level org-min-level)
1856 level)
1857 (with-temp-buffer
1858 (org-lparse-bind-local-variables opt-plist)
1859 (erase-buffer)
1860 (org-lparse-begin 'TOC (nth 3 (plist-get opt-plist :lang-words)) umax-toc)
1861 (setq
1862 lines
1863 (mapcar
1864 #'(lambda (line)
1865 (when (and (string-match org-todo-line-regexp line)
1866 (not (get-text-property 0 'org-protected line))
1867 (<= (setq level (org-tr-level
1868 (- (match-end 1) (match-beginning 1)
1869 level-offset)))
1870 umax-toc))
1871 (let ((txt (save-match-data
1872 (org-xml-encode-org-text-skip-links
1873 (org-export-cleanup-toc-line
1874 (match-string 3 line)))))
1875 (todo (and
1876 org-export-mark-todo-in-toc
1877 (or (and (match-beginning 2)
1878 (not (member (match-string 2 line)
1879 org-done-keywords)))
1880 (and (= level umax-toc)
1881 (org-search-todo-below
1882 line lines level)))))
1883 tags)
1884 ;; Check for targets
1885 (while (string-match org-any-target-regexp line)
1886 (setq line
1887 (replace-match
1888 (let ((org-lparse-encode-pending t))
1889 (org-lparse-format 'FONTIFY
1890 (match-string 1 line) "target"))
1891 t t line)))
1892 (when (string-match
1893 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
1894 (setq tags (match-string 1 txt)
1895 txt (replace-match "" t nil txt)))
1896 (when (string-match quote-re0 txt)
1897 (setq txt (replace-match "" t t txt)))
1898 (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
1899 (setq txt (replace-match "" t t txt)))
1900 (org-lparse-format
1901 'TOC-ITEM
1902 (let* ((snumber (org-section-number level))
1903 (href (replace-regexp-in-string
1904 "\\." "-" (format "sec-%s" snumber)))
1905 (href
1907 (cdr (assoc
1908 href org-export-preferred-target-alist))
1909 href))
1910 (href (org-solidify-link-text href)))
1911 (org-lparse-format 'TOC-ENTRY snumber todo txt tags href))
1912 level org-last-level)
1913 (setq org-last-level level)))
1914 line)
1915 lines))
1916 (org-lparse-end 'TOC)
1917 (setq org-lparse-toc (buffer-string))))
1918 lines)
1920 (defun org-lparse-format-table-row (fields &optional text-for-empty-fields)
1921 (if org-lparse-table-ncols
1922 ;; second and subsequent rows of the table
1923 (when (and org-lparse-list-table-p
1924 (> (length fields) org-lparse-table-ncols))
1925 (error "Table row has %d columns but header row claims %d columns"
1926 (length fields) org-lparse-table-ncols))
1927 ;; first row of the table
1928 (setq org-lparse-table-ncols (length fields))
1929 (when org-lparse-table-is-styled
1930 (setq org-lparse-table-num-numeric-items-per-column
1931 (make-vector org-lparse-table-ncols 0))
1932 (setq org-lparse-table-colalign-vector
1933 (make-vector org-lparse-table-ncols nil))
1934 (let ((c -1))
1935 (while (< (incf c) org-lparse-table-ncols)
1936 (let* ((col-cookie (cdr (assoc (1+ c) org-lparse-table-colalign-info)))
1937 (align (nth 0 col-cookie)))
1938 (setf (aref org-lparse-table-colalign-vector c)
1939 (cond
1940 ((string= align "l") "left")
1941 ((string= align "r") "right")
1942 ((string= align "c") "center"))))))))
1943 (incf org-lparse-table-rownum)
1944 (let ((i -1))
1945 (org-lparse-format
1946 'TABLE-ROW
1947 (mapconcat
1948 (lambda (x)
1949 (when (and (string= x "") text-for-empty-fields)
1950 (setq x text-for-empty-fields))
1951 (incf i)
1952 (let (col-cookie horiz-span)
1953 (when org-lparse-table-is-styled
1954 (when (and (< i org-lparse-table-ncols)
1955 (string-match org-table-number-regexp x))
1956 (incf (aref org-lparse-table-num-numeric-items-per-column i)))
1957 (setq col-cookie (cdr (assoc (1+ i) org-lparse-table-colalign-info))
1958 horiz-span (nth 1 col-cookie)))
1959 (org-lparse-format
1960 'TABLE-CELL x org-lparse-table-rownum i (or horiz-span 0))))
1961 fields "\n"))))
1963 (defun org-lparse-get (what &optional opt-plist)
1964 "Query for value of WHAT for the current backend `org-lparse-backend'.
1965 See also `org-lparse-backend-get'."
1966 (if (boundp 'org-lparse-backend)
1967 (org-lparse-backend-get (symbol-name org-lparse-backend) what opt-plist)
1968 (error "org-lparse-backend is not bound yet")))
1970 (defun org-lparse-backend-get (backend what &optional opt-plist)
1971 "Query BACKEND for value of WHAT.
1972 Dispatch the call to `org-<backend>-user-get'. If that throws an
1973 error, dispatch the call to `org-<backend>-get'. See
1974 `org-xhtml-get' for all known settings queried for by
1975 `org-lparse' during the course of export."
1976 (assert (stringp backend) t)
1977 (unless (org-lparse-backend-is-native-p backend)
1978 (error "Unknown native backend %s" backend))
1979 (let ((backend-get-method (intern (format "org-%s-get" backend)))
1980 (backend-user-get-method (intern (format "org-%s-user-get" backend))))
1981 (cond
1982 ((functionp backend-get-method)
1983 (condition-case nil
1984 (funcall backend-user-get-method what opt-plist)
1985 (error (funcall backend-get-method what opt-plist))))
1987 (error "Native backend %s doesn't define %s" backend backend-get-method)))))
1989 (defun org-lparse-insert-tag (tag &rest args)
1990 (when (member org-lparse-insert-tag-with-newlines '(lead both))
1991 (insert "\n"))
1992 (insert (apply 'format tag args))
1993 (when (member org-lparse-insert-tag-with-newlines '(trail both))
1994 (insert "\n")))
1996 (defun org-lparse-get-targets-from-title (title)
1997 (let* ((target (org-get-text-property-any 0 'target title))
1998 (extra-targets (assoc target org-export-target-aliases))
1999 (target (or (cdr (assoc target org-export-preferred-target-alist))
2000 target)))
2001 (cons target (remove target extra-targets))))
2003 (defun org-lparse-suffix-from-snumber (snumber)
2004 (let* ((snu (replace-regexp-in-string "\\." "-" snumber))
2005 (href (cdr (assoc (concat "sec-" snu)
2006 org-export-preferred-target-alist))))
2007 (org-solidify-link-text (or href snu))))
2009 (defun org-lparse-begin-level (level title umax head-count)
2010 "Insert a new LEVEL in HTML export.
2011 When TITLE is nil, just close all open levels."
2012 (org-lparse-end-level level umax)
2013 (unless title (error "Why is heading nil"))
2014 (let* ((targets (org-lparse-get-targets-from-title title))
2015 (target (car targets)) (extra-targets (cdr targets))
2016 (target (and target (org-solidify-link-text target)))
2017 (extra-class (org-get-text-property-any 0 'html-container-class title))
2018 snumber tags level1 class)
2019 (when (string-match (org-re "\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") title)
2020 (setq tags (and org-export-with-tags (match-string 1 title)))
2021 (setq title (replace-match "" t t title)))
2022 (if (> level umax)
2023 (progn
2024 (if (aref org-levels-open (1- level))
2025 (org-lparse-end-list-item-1)
2026 (aset org-levels-open (1- level) t)
2027 (org-lparse-end-paragraph)
2028 (org-lparse-begin-list 'unordered))
2029 (org-lparse-begin-list-item
2030 'unordered target (org-lparse-format
2031 'HEADLINE title extra-targets tags)))
2032 (aset org-levels-open (1- level) t)
2033 (setq snumber (org-section-number level))
2034 (setq level1 (+ level (or (org-lparse-get 'TOPLEVEL-HLEVEL) 1) -1))
2035 (unless (= head-count 1)
2036 (org-lparse-end-outline-text-or-outline))
2037 (org-lparse-begin-outline-and-outline-text
2038 level1 snumber title tags target extra-targets extra-class)
2039 (org-lparse-begin-paragraph))))
2041 (defun org-lparse-end-level (level umax)
2042 (org-lparse-end-paragraph)
2043 (loop for l from org-level-max downto level
2044 do (when (aref org-levels-open (1- l))
2045 ;; Terminate one level in HTML export
2046 (if (<= l umax)
2047 (org-lparse-end-outline-text-or-outline)
2048 (org-lparse-end-list-item-1)
2049 (org-lparse-end-list 'unordered))
2050 (aset org-levels-open (1- l) nil))))
2052 (defvar org-lparse-outline-text-open)
2053 (defun org-lparse-begin-outline-and-outline-text (level1 snumber title tags
2054 target extra-targets
2055 extra-class)
2056 (org-lparse-begin
2057 'OUTLINE level1 snumber title tags target extra-targets extra-class)
2058 (org-lparse-begin-outline-text level1 snumber extra-class))
2060 (defun org-lparse-end-outline-text-or-outline ()
2061 (cond
2062 (org-lparse-outline-text-open
2063 (org-lparse-end 'OUTLINE-TEXT)
2064 (setq org-lparse-outline-text-open nil))
2065 (t (org-lparse-end 'OUTLINE))))
2067 (defun org-lparse-begin-outline-text (level1 snumber extra-class)
2068 (assert (not org-lparse-outline-text-open) t)
2069 (setq org-lparse-outline-text-open t)
2070 (org-lparse-begin 'OUTLINE-TEXT level1 snumber extra-class))
2072 (defun org-lparse-html-list-type-to-canonical-list-type (ltype)
2073 (cdr (assoc ltype '(("o" . ordered)
2074 ("u" . unordered)
2075 ("d" . description)))))
2077 ;; following vars are bound during `org-do-lparse'
2078 (defvar org-lparse-list-stack)
2079 (defvar org-lparse-list-table:table-row)
2080 (defvar org-lparse-list-table:lines)
2082 ;; Notes on LIST-TABLES
2083 ;; ====================
2084 ;; Lists withing "list-table" blocks (as shown below)
2086 ;; #+begin_list-table
2087 ;; - Row 1
2088 ;; - 1.1
2089 ;; - 1.2
2090 ;; - 1.3
2091 ;; - Row 2
2092 ;; - 2.1
2093 ;; - 2.2
2094 ;; - 2.3
2095 ;; #+end_list-table
2097 ;; will be exported as though it were a table as shown below.
2099 ;; | Row 1 | 1.1 | 1.2 | 1.3 |
2100 ;; | Row 2 | 2.1 | 2.2 | 2.3 |
2102 ;; Note that org-tables are NOT multi-line and each line is mapped to
2103 ;; a unique row in the exported document. So if an exported table
2104 ;; needs to contain a single paragraph (with copious text) it needs to
2105 ;; be typed up in a single line. Editing such long lines using the
2106 ;; table editor will be a cumbersome task. Furthermore inclusion of
2107 ;; multi-paragraph text in a table cell is well-nigh impossible.
2109 ;; LIST-TABLEs are meant to circumvent the above problems with
2110 ;; org-tables.
2112 ;; Note that in the example above the list items could be paragraphs
2113 ;; themselves and the list can be arbitrarily deep.
2115 ;; Inspired by following thread:
2116 ;; https://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01101.html
2118 (defun org-lparse-begin-list (ltype)
2119 (push ltype org-lparse-list-stack)
2120 (let ((list-level (length org-lparse-list-stack)))
2121 (cond
2122 ((not org-lparse-list-table-p)
2123 (org-lparse-begin 'LIST ltype))
2124 ;; process LIST-TABLE
2125 ((= 1 list-level)
2126 ;; begin LIST-TABLE
2127 (setq org-lparse-list-table:lines nil)
2128 (setq org-lparse-list-table:table-row nil))
2129 ((= 2 list-level)
2130 (ignore))
2132 (org-lparse-begin 'LIST ltype)))))
2134 (defun org-lparse-end-list (ltype)
2135 (pop org-lparse-list-stack)
2136 (let ((list-level (length org-lparse-list-stack)))
2137 (cond
2138 ((not org-lparse-list-table-p)
2139 (org-lparse-end 'LIST ltype))
2140 ;; process LIST-TABLE
2141 ((= 0 list-level)
2142 ;; end LIST-TABLE
2143 (insert (org-lparse-format-list-table
2144 (nreverse org-lparse-list-table:lines))))
2145 ((= 1 list-level)
2146 (ignore))
2148 (org-lparse-end 'LIST ltype)))))
2150 (defun org-lparse-begin-list-item (ltype &optional arg headline)
2151 (let ((list-level (length org-lparse-list-stack)))
2152 (cond
2153 ((not org-lparse-list-table-p)
2154 (org-lparse-begin 'LIST-ITEM ltype arg headline))
2155 ;; process LIST-TABLE
2156 ((= 1 list-level)
2157 ;; begin TABLE-ROW for LIST-TABLE
2158 (setq org-lparse-list-table:table-row nil)
2159 (org-lparse-begin-list-table:table-cell))
2160 ((= 2 list-level)
2161 ;; begin TABLE-CELL for LIST-TABLE
2162 (org-lparse-begin-list-table:table-cell))
2164 (org-lparse-begin 'LIST-ITEM ltype arg headline)))))
2166 (defun org-lparse-end-list-item (ltype)
2167 (let ((list-level (length org-lparse-list-stack)))
2168 (cond
2169 ((not org-lparse-list-table-p)
2170 (org-lparse-end 'LIST-ITEM ltype))
2171 ;; process LIST-TABLE
2172 ((= 1 list-level)
2173 ;; end TABLE-ROW for LIST-TABLE
2174 (org-lparse-end-list-table:table-cell)
2175 (push (nreverse org-lparse-list-table:table-row)
2176 org-lparse-list-table:lines))
2177 ((= 2 list-level)
2178 ;; end TABLE-CELL for LIST-TABLE
2179 (org-lparse-end-list-table:table-cell))
2181 (org-lparse-end 'LIST-ITEM ltype)))))
2183 (defvar org-lparse-list-table:table-cell-open)
2184 (defun org-lparse-begin-list-table:table-cell ()
2185 (org-lparse-end-list-table:table-cell)
2186 (setq org-lparse-list-table:table-cell-open t)
2187 (org-lparse-begin-collect)
2188 (org-lparse-begin-paragraph))
2190 (defun org-lparse-end-list-table:table-cell ()
2191 (when org-lparse-list-table:table-cell-open
2192 (setq org-lparse-list-table:table-cell-open nil)
2193 (org-lparse-end-paragraph)
2194 (push (org-lparse-end-collect)
2195 org-lparse-list-table:table-row)))
2197 (defvar org-lparse-table-rowgrp-info)
2198 (defun org-lparse-begin-table-rowgroup (&optional is-header-row)
2199 (push (cons (1+ org-lparse-table-rownum) :start) org-lparse-table-rowgrp-info)
2200 (org-lparse-begin 'TABLE-ROWGROUP is-header-row))
2202 (defun org-lparse-end-table ()
2203 (when org-lparse-table-is-styled
2204 ;; column groups
2205 (unless (car org-table-colgroup-info)
2206 (setq org-table-colgroup-info
2207 (cons :start (cdr org-table-colgroup-info))))
2209 ;; column alignment
2210 (let ((c -1))
2211 (mapc
2212 (lambda (x)
2213 (incf c)
2214 (setf (aref org-lparse-table-colalign-vector c)
2215 (or (aref org-lparse-table-colalign-vector c)
2216 (if (> (/ (float x) (1+ org-lparse-table-rownum))
2217 org-table-number-fraction)
2218 "right" "left"))))
2219 org-lparse-table-num-numeric-items-per-column)))
2220 (org-lparse-end 'TABLE))
2222 (defvar org-lparse-encode-pending nil)
2224 (defun org-lparse-format-tags (tag text prefix suffix &rest args)
2225 (cond
2226 ((consp tag)
2227 (concat prefix (apply 'format (car tag) args) text suffix
2228 (format (cdr tag))))
2229 ((stringp tag) ; singleton tag
2230 (concat prefix (apply 'format tag args) text))))
2232 (defun org-xml-fix-class-name (kwd) ; audit callers of this function
2233 "Turn todo keyword into a valid class name.
2234 Replaces invalid characters with \"_\"."
2235 (save-match-data
2236 (while (string-match "[^a-zA-Z0-9_]" kwd)
2237 (setq kwd (replace-match "_" t t kwd))))
2238 kwd)
2240 (defun org-lparse-format-todo (todo)
2241 (org-lparse-format 'FONTIFY
2242 (concat
2243 (ignore-errors (org-lparse-get 'TODO-KWD-CLASS-PREFIX))
2244 (org-xml-fix-class-name todo))
2245 (list (if (member todo org-done-keywords) "done" "todo")
2246 todo)))
2248 (defun org-lparse-format-extra-targets (extra-targets)
2249 (if (not extra-targets) ""
2250 (mapconcat (lambda (x)
2251 (setq x (org-solidify-link-text
2252 (if (org-uuidgen-p x) (concat "ID-" x) x)))
2253 (org-lparse-format 'ANCHOR "" x))
2254 extra-targets "")))
2256 (defun org-lparse-format-org-tags (tags)
2257 (if (not tags) ""
2258 (org-lparse-format
2259 'FONTIFY (mapconcat
2260 (lambda (x)
2261 (org-lparse-format
2262 'FONTIFY x
2263 (concat
2264 (ignore-errors (org-lparse-get 'TAG-CLASS-PREFIX))
2265 (org-xml-fix-class-name x))))
2266 (org-split-string tags ":")
2267 (org-lparse-format 'SPACES 1)) "tag")))
2269 (defun org-lparse-format-section-number (&optional snumber level)
2270 (and org-export-with-section-numbers
2271 (not org-lparse-body-only) snumber level
2272 (org-lparse-format 'FONTIFY snumber (format "section-number-%d" level))))
2274 (defun org-lparse-warn (msg)
2275 (if (not org-lparse-use-flashy-warning)
2276 (message msg)
2277 (put-text-property 0 (length msg) 'face 'font-lock-warning-face msg)
2278 (message msg)
2279 (sleep-for 3)))
2281 (defun org-xml-format-href (s)
2282 "Make sure the S is valid as a href reference in an XHTML document."
2283 (save-match-data
2284 (let ((start 0))
2285 (while (string-match "&" s start)
2286 (setq start (+ (match-beginning 0) 3)
2287 s (replace-match "&amp;" t t s)))))
2290 (defun org-xml-format-desc (s)
2291 "Make sure the S is valid as a description in a link."
2292 (if (and s (not (get-text-property 1 'org-protected s)))
2293 (save-match-data
2294 (org-xml-encode-org-text s))
2297 (provide 'org-lparse)
2299 ;; Local variables:
2300 ;; generated-autoload-file: "org-loaddefs.el"
2301 ;; End:
2303 ;;; org-lparse.el ends here