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