org-e-publish: Fix publishing for files not directly in :base-directory
[org-mode.git] / lisp / org-lparse.el
blob1413dd1ced5d7b9653741f126dab1a6a7be56659
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 (org-do-lparse arg hidden ext-plist to-buffer body-only pub-dir)
446 (remove-hook 'org-export-preprocess-hook
447 'org-lparse-strip-experimental-blocks-maybe)
448 (remove-hook 'org-export-preprocess-after-blockquote-hook
449 'org-lparse-preprocess-after-blockquote)))
451 (defcustom org-lparse-use-flashy-warning nil
452 "Control flashing of messages logged with `org-lparse-warn'.
453 When non-nil, messages are fontified with warning face and the
454 exporter lingers for a while to catch user's attention."
455 :type 'boolean
456 :group 'org-lparse)
458 (defun org-lparse-convert-read-params ()
459 "Return IN-FILE and OUT-FMT params for `org-lparse-do-convert'.
460 This is a helper routine for interactive use."
461 (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
462 (in-file (read-file-name "File to be converted: "
463 nil buffer-file-name t))
464 (in-fmt (file-name-extension in-file))
465 (out-fmt-choices (org-lparse-reachable-formats in-fmt))
466 (out-fmt
467 (or (and out-fmt-choices
468 (funcall input "Output format: "
469 out-fmt-choices nil nil nil))
470 (error
471 "No known converter or no known output formats for %s files"
472 in-fmt))))
473 (list in-file out-fmt)))
475 (eval-when-compile
476 (require 'browse-url))
478 (defun org-lparse-do-convert (in-file out-fmt &optional prefix-arg)
479 "Workhorse routine for `org-export-odt-convert'."
480 (require 'browse-url)
481 (let* ((in-file (expand-file-name (or in-file buffer-file-name)))
482 (dummy (or (file-readable-p in-file)
483 (error "Cannot read %s" in-file)))
484 (in-fmt (file-name-extension in-file))
485 (out-fmt (or out-fmt (error "Output format unspecified")))
486 (how (or (org-lparse-reachable-p in-fmt out-fmt)
487 (error "Cannot convert from %s format to %s format?"
488 in-fmt out-fmt)))
489 (convert-process (car how))
490 (out-file (concat (file-name-sans-extension in-file) "."
491 (nth 1 (or (cdr how) out-fmt))))
492 (extra-options (or (nth 2 (cdr how)) ""))
493 (out-dir (file-name-directory in-file))
494 (cmd (format-spec convert-process
495 `((?i . ,(shell-quote-argument in-file))
496 (?I . ,(browse-url-file-url in-file))
497 (?f . ,out-fmt)
498 (?o . ,out-file)
499 (?O . ,(browse-url-file-url out-file))
500 (?d . , (shell-quote-argument out-dir))
501 (?D . ,(browse-url-file-url out-dir))
502 (?x . ,extra-options)))))
503 (when (file-exists-p out-file)
504 (delete-file out-file))
506 (message "Executing %s" cmd)
507 (let ((cmd-output (shell-command-to-string cmd)))
508 (message "%s" cmd-output))
510 (cond
511 ((file-exists-p out-file)
512 (message "Exported to %s" out-file)
513 (when prefix-arg
514 (message "Opening %s..." out-file)
515 (org-open-file out-file 'system))
516 out-file)
518 (message "Export to %s failed" out-file)
519 nil))))
521 (defvar org-lparse-insert-tag-with-newlines 'both)
523 ;; Following variables are let-bound during `org-lparse'
524 (defvar org-lparse-dyn-first-heading-pos)
525 (defvar org-lparse-toc)
526 (defvar org-lparse-entity-control-callbacks-alist)
527 (defvar org-lparse-entity-format-callbacks-alist)
528 (defvar org-lparse-backend nil
529 "The native backend to which the document is currently exported.
530 This variable is let bound during `org-lparse'. Valid values are
531 one of the symbols corresponding to `org-lparse-native-backends'.
533 Compare this variable with `org-export-current-backend' which is
534 bound only during `org-export-preprocess-string' stage of the
535 export process.
537 See also `org-lparse-other-backend'.")
539 (defvar org-lparse-other-backend nil
540 "The target backend to which the document is currently exported.
541 This variable is let bound during `org-lparse'. This variable is
542 set to either `org-lparse-backend' or one of the symbols
543 corresponding to OTHER-BACKENDS specification of the
544 org-lparse-backend.
546 For example, if a document is exported to \"odt\" then both
547 org-lparse-backend and org-lparse-other-backend are bound to
548 'odt. On the other hand, if a document is exported to \"odt\"
549 and then converted to \"doc\" then org-lparse-backend is set to
550 'odt and org-lparse-other-backend is set to 'doc.")
552 (defvar org-lparse-body-only nil
553 "Bind this to BODY-ONLY arg of `org-lparse'.")
555 (defvar org-lparse-to-buffer nil
556 "Bind this to TO-BUFFER arg of `org-lparse'.")
558 (defun org-lparse-get-block-params (params)
559 (save-match-data
560 (when params
561 (setq params (org-trim params))
562 (unless (string-match "\\`(.*)\\'" params)
563 (setq params (format "(%s)" params)))
564 (ignore-errors (read params)))))
566 (defvar org-heading-keyword-regexp-format) ; defined in org.el
567 (defvar org-lparse-special-blocks '("list-table" "annotation"))
568 (defun org-do-lparse (arg &optional hidden ext-plist
569 to-buffer body-only pub-dir)
570 "Export the outline to various formats.
571 See `org-lparse' for more information. This function is a
572 html-agnostic version of the `org-export-as-html' function in 7.5
573 version."
574 ;; Make sure we have a file name when we need it.
575 (when (and (not (or to-buffer body-only))
576 (not buffer-file-name))
577 (if (buffer-base-buffer)
578 (org-set-local 'buffer-file-name
579 (with-current-buffer (buffer-base-buffer)
580 buffer-file-name))
581 (error "Need a file name to be able to export")))
583 (org-lparse-warn
584 (format "Exporting to %s using org-lparse..."
585 (upcase (symbol-name
586 (or org-lparse-backend org-lparse-other-backend)))))
588 (setq-default org-todo-line-regexp org-todo-line-regexp)
589 (setq-default org-deadline-line-regexp org-deadline-line-regexp)
590 (setq-default org-done-keywords org-done-keywords)
591 (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
592 (let* (hfy-user-sheet-assoc ; let `htmlfontify' know that
593 ; we are interested in
594 ; collecting styles
595 org-lparse-encode-pending
596 org-lparse-par-open
597 (org-lparse-par-open-stashed 0)
599 ;; list related vars
600 (org-lparse-list-stack '())
602 ;; list-table related vars
603 org-lparse-list-table-p
604 org-lparse-list-table:table-cell-open
605 org-lparse-list-table:table-row
606 org-lparse-list-table:lines
608 org-lparse-outline-text-open
609 (org-lparse-latex-fragment-fallback ; currently used only by
610 ; odt exporter
611 (or (ignore-errors (org-lparse-get 'LATEX-FRAGMENT-FALLBACK))
612 (if (and (org-check-external-command "latex" "" t)
613 (org-check-external-command "dvipng" "" t))
614 'dvipng
615 'verbatim)))
616 (org-lparse-insert-tag-with-newlines 'both)
617 (org-lparse-to-buffer to-buffer)
618 (org-lparse-body-only body-only)
619 (org-lparse-entity-control-callbacks-alist
620 (org-lparse-get 'ENTITY-CONTROL))
621 (org-lparse-entity-format-callbacks-alist
622 (org-lparse-get 'ENTITY-FORMAT))
623 (opt-plist
624 (org-export-process-option-filters
625 (org-combine-plists (org-default-export-plist)
626 ext-plist
627 (org-infile-export-plist))))
628 (body-only (or body-only (plist-get opt-plist :body-only)))
629 valid org-lparse-dyn-first-heading-pos
630 (odd org-odd-levels-only)
631 (region-p (org-region-active-p))
632 (rbeg (and region-p (region-beginning)))
633 (rend (and region-p (region-end)))
634 (subtree-p
635 (if (plist-get opt-plist :ignore-subtree-p)
637 (when region-p
638 (save-excursion
639 (goto-char rbeg)
640 (and (org-at-heading-p)
641 (>= (org-end-of-subtree t t) rend))))))
642 (level-offset (if subtree-p
643 (save-excursion
644 (goto-char rbeg)
645 (+ (funcall outline-level)
646 (if org-odd-levels-only 1 0)))
648 (opt-plist (setq org-export-opt-plist
649 (if subtree-p
650 (org-export-add-subtree-options opt-plist rbeg)
651 opt-plist)))
652 ;; The following two are dynamically scoped into other
653 ;; routines below.
654 (org-current-export-dir
655 (or pub-dir (org-lparse-get 'EXPORT-DIR opt-plist)))
656 (org-current-export-file buffer-file-name)
657 (level 0) (line "") (origline "") txt todo
658 (umax nil)
659 (umax-toc nil)
660 (filename (if to-buffer nil
661 (expand-file-name
662 (concat
663 (file-name-sans-extension
664 (or (and subtree-p
665 (org-entry-get (region-beginning)
666 "EXPORT_FILE_NAME" t))
667 (file-name-nondirectory buffer-file-name)))
668 "." (org-lparse-get 'FILE-NAME-EXTENSION opt-plist))
669 (file-name-as-directory
670 (or pub-dir (org-lparse-get 'EXPORT-DIR opt-plist))))))
671 (current-dir (if buffer-file-name
672 (file-name-directory buffer-file-name)
673 default-directory))
674 (auto-insert nil) ; Avoid any auto-insert stuff for the new file
675 (buffer (if to-buffer
676 (cond
677 ((eq to-buffer 'string)
678 (get-buffer-create (org-lparse-get 'EXPORT-BUFFER-NAME)))
679 (t (get-buffer-create to-buffer)))
680 (find-file-noselect
681 (or (let ((f (org-lparse-get 'INIT-METHOD)))
682 (and f (functionp f) (funcall f filename)))
683 filename))))
684 (org-levels-open (make-vector org-level-max nil))
685 (dummy (mapc
686 (lambda(p)
687 (let* ((val (plist-get opt-plist p))
688 (val (org-xml-encode-org-text-skip-links val)))
689 (setq opt-plist (plist-put opt-plist p val))))
690 '(:date :author :keywords :description)))
691 (date (plist-get opt-plist :date))
692 (date (cond
693 ((and date (string-match "%" date))
694 (format-time-string date))
695 (date date)
696 (t (format-time-string "%Y-%m-%d %T %Z"))))
697 (dummy (setq opt-plist (plist-put opt-plist :effective-date date)))
698 (title (org-xml-encode-org-text-skip-links
699 (or (and subtree-p (org-export-get-title-from-subtree))
700 (plist-get opt-plist :title)
701 (and (not body-only)
702 (not
703 (plist-get opt-plist :skip-before-1st-heading))
704 (org-export-grab-title-from-buffer))
705 (and buffer-file-name
706 (file-name-sans-extension
707 (file-name-nondirectory buffer-file-name)))
708 "UNTITLED")))
709 (dummy (setq opt-plist (plist-put opt-plist :title title)))
710 (html-table-tag (plist-get opt-plist :html-table-tag))
711 (quote-re0 (concat "^ *" org-quote-string "\\( +\\|[ \t]*$\\)"))
712 (quote-re (format org-heading-keyword-regexp-format
713 org-quote-string))
714 (org-lparse-dyn-current-environment nil)
715 ;; Get the language-dependent settings
716 (lang-words (or (assoc (plist-get opt-plist :language)
717 org-export-language-setup)
718 (assoc "en" org-export-language-setup)))
719 (dummy (setq opt-plist (plist-put opt-plist :lang-words lang-words)))
720 (head-count 0) cnt
721 (start 0)
722 (coding-system-for-write
723 (or (ignore-errors (org-lparse-get 'CODING-SYSTEM-FOR-WRITE))
724 (and (boundp 'buffer-file-coding-system)
725 buffer-file-coding-system)))
726 (save-buffer-coding-system
727 (or (ignore-errors (org-lparse-get 'CODING-SYSTEM-FOR-SAVE))
728 (and (boundp 'buffer-file-coding-system)
729 buffer-file-coding-system)))
730 (region
731 (buffer-substring
732 (if region-p (region-beginning) (point-min))
733 (if region-p (region-end) (point-max))))
734 (org-export-have-math nil)
735 (org-export-footnotes-seen nil)
736 (org-export-footnotes-data (org-footnote-all-labels 'with-defs))
737 (org-footnote-insert-pos-for-preprocessor 'point-min)
738 (org-lparse-opt-plist opt-plist)
739 (lines
740 (org-split-string
741 (org-export-preprocess-string
742 region
743 :emph-multiline t
744 :for-backend (if (equal org-lparse-backend 'xhtml) ; hack
745 'html
746 org-lparse-backend)
747 :skip-before-1st-heading
748 (plist-get opt-plist :skip-before-1st-heading)
749 :drawers (plist-get opt-plist :drawers)
750 :todo-keywords (plist-get opt-plist :todo-keywords)
751 :tasks (plist-get opt-plist :tasks)
752 :tags (plist-get opt-plist :tags)
753 :priority (plist-get opt-plist :priority)
754 :footnotes (plist-get opt-plist :footnotes)
755 :timestamps (plist-get opt-plist :timestamps)
756 :archived-trees
757 (plist-get opt-plist :archived-trees)
758 :select-tags (plist-get opt-plist :select-tags)
759 :exclude-tags (plist-get opt-plist :exclude-tags)
760 :add-text
761 (plist-get opt-plist :text)
762 :LaTeX-fragments
763 (plist-get opt-plist :LaTeX-fragments))
764 "[\r\n]"))
765 table-open
766 table-buffer table-orig-buffer
768 rpl path attr desc descp desc1 desc2 link
769 snumber fnc
770 footnotes footref-seen
771 org-lparse-output-buffer
772 org-lparse-footnote-definitions
773 org-lparse-footnote-number
774 ;; collection
775 org-lparse-collect-buffer
776 (org-lparse-collect-count 0) ; things will get haywire if
777 ; collections are chained. Use
778 ; this variable to assert this
779 ; pre-requisite
780 org-lparse-toc
781 href
784 (let ((inhibit-read-only t))
785 (org-unmodified
786 (remove-text-properties (point-min) (point-max)
787 '(:org-license-to-kill t))))
789 (message "Exporting...")
790 (org-init-section-numbers)
792 ;; Switch to the output buffer
793 (setq org-lparse-output-buffer buffer)
794 (set-buffer org-lparse-output-buffer)
795 (let ((inhibit-read-only t)) (erase-buffer))
796 (fundamental-mode)
797 (org-install-letbind)
799 (and (fboundp 'set-buffer-file-coding-system)
800 (set-buffer-file-coding-system coding-system-for-write))
802 (let ((case-fold-search nil)
803 (org-odd-levels-only odd))
804 ;; create local variables for all options, to make sure all called
805 ;; functions get the correct information
806 (mapc (lambda (x)
807 (set (make-local-variable (nth 2 x))
808 (plist-get opt-plist (car x))))
809 org-export-plist-vars)
810 (setq umax (if arg (prefix-numeric-value arg)
811 org-export-headline-levels))
812 (setq umax-toc (if (integerp org-export-with-toc)
813 (min org-export-with-toc umax)
814 umax))
815 (setq org-lparse-opt-plist
816 (plist-put org-lparse-opt-plist :headline-levels umax))
818 (when (and org-export-with-toc (not body-only))
819 (setq lines (org-lparse-prepare-toc
820 lines level-offset opt-plist umax-toc)))
822 (unless body-only
823 (org-lparse-begin 'DOCUMENT-CONTENT opt-plist)
824 (org-lparse-begin 'DOCUMENT-BODY opt-plist))
826 (setq head-count 0)
827 (org-init-section-numbers)
829 (org-lparse-begin-paragraph)
831 (while (setq line (pop lines) origline line)
832 (catch 'nextline
833 (when (and (org-lparse-current-environment-p 'quote)
834 (string-match org-outline-regexp-bol line))
835 (org-lparse-end-environment 'quote))
837 (when (org-lparse-current-environment-p 'quote)
838 (org-lparse-insert 'LINE line)
839 (throw 'nextline nil))
841 ;; Fixed-width, verbatim lines (examples)
842 (when (and org-export-with-fixed-width
843 (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line))
844 (when (not (org-lparse-current-environment-p 'fixedwidth))
845 (org-lparse-begin-environment 'fixedwidth))
846 (org-lparse-insert 'LINE (match-string 3 line))
847 (when (or (not lines)
848 (not (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)"
849 (car lines))))
850 (org-lparse-end-environment 'fixedwidth))
851 (throw 'nextline nil))
853 ;; Native Text
854 (when (and (get-text-property 0 'org-native-text line)
855 ;; Make sure it is the entire line that is protected
856 (not (< (or (next-single-property-change
857 0 'org-native-text line) 10000)
858 (length line))))
859 (let ((ind (get-text-property 0 'original-indentation line)))
860 (org-lparse-begin-environment 'native)
861 (org-lparse-insert 'LINE line)
862 (while (and lines
863 (or (= (length (car lines)) 0)
864 (not ind)
865 (equal ind (get-text-property
866 0 'original-indentation (car lines))))
867 (or (= (length (car lines)) 0)
868 (get-text-property 0 'org-native-text (car lines))))
869 (org-lparse-insert 'LINE (pop lines)))
870 (org-lparse-end-environment 'native))
871 (throw 'nextline nil))
873 ;; Protected HTML
874 (when (and (get-text-property 0 'org-protected line)
875 ;; Make sure it is the entire line that is protected
876 (not (< (or (next-single-property-change
877 0 'org-protected line) 10000)
878 (length line))))
879 (let ((ind (get-text-property 0 'original-indentation line)))
880 (org-lparse-insert 'LINE line)
881 (while (and lines
882 (or (= (length (car lines)) 0)
883 (not ind)
884 (equal ind (get-text-property
885 0 'original-indentation (car lines))))
886 (or (= (length (car lines)) 0)
887 (get-text-property 0 'org-protected (car lines))))
888 (org-lparse-insert 'LINE (pop lines))))
889 (throw 'nextline nil))
891 ;; Blockquotes, verse, and center
892 (when (string-match
893 "^ORG-\\(.+\\)-\\(START\\|END\\)\\([ \t]+.*\\)?$" line)
894 (let* ((style (intern (downcase (match-string 1 line))))
895 (env-options-plist (org-lparse-get-block-params
896 (match-string 3 line)))
897 (f (cdr (assoc (match-string 2 line)
898 '(("START" . org-lparse-begin-environment)
899 ("END" . org-lparse-end-environment))))))
900 (when (memq style
901 (append
902 '(blockquote verse center)
903 (mapcar 'intern org-lparse-special-blocks)))
904 (funcall f style env-options-plist)
905 (throw 'nextline nil))))
907 (when (org-lparse-current-environment-p 'verse)
908 (let ((i (org-get-string-indentation line)))
909 (if (> i 0)
910 (setq line (concat
911 (let ((org-lparse-encode-pending t))
912 (org-lparse-format 'SPACES (* 2 i)))
913 " " (org-trim line))))
914 (unless (string-match "\\\\\\\\[ \t]*$" line)
915 (setq line (concat line "\\\\")))))
917 ;; make targets to anchors
918 (setq start 0)
919 (while (string-match
920 "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line start)
921 (cond
922 ((get-text-property (match-beginning 1) 'org-protected line)
923 (setq start (match-end 1)))
924 ((match-end 2)
925 (setq line (replace-match
926 (let ((org-lparse-encode-pending t))
927 (org-lparse-format
928 'ANCHOR "" (org-solidify-link-text
929 (match-string 1 line))))
930 t t line)))
931 ((and org-export-with-toc (equal (string-to-char line) ?*))
932 ;; FIXME: NOT DEPENDENT on TOC?????????????????????
933 (setq line (replace-match
934 (let ((org-lparse-encode-pending t))
935 (org-lparse-format
936 'FONTIFY (match-string 1 line) "target"))
937 ;; (concat "@<i>" (match-string 1 line) "@</i> ")
938 t t line)))
940 (setq line (replace-match
941 (concat
942 (let ((org-lparse-encode-pending t))
943 (org-lparse-format
944 'ANCHOR (match-string 1 line)
945 (org-solidify-link-text (match-string 1 line))
946 "target")) " ")
947 t t line)))))
949 (let ((org-lparse-encode-pending t))
950 (setq line (org-lparse-handle-time-stamps line)))
952 ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
953 ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
954 ;; Also handle sub_superscripts and checkboxes
955 (or (string-match org-table-hline-regexp line)
956 (string-match "^[ \t]*\\([+]-\\||[ ]\\)[-+ |]*[+|][ \t]*$" line)
957 (setq line (org-xml-encode-org-text-skip-links line)))
959 (setq line (org-lparse-format-org-link line opt-plist))
961 ;; TODO items
962 (if (and org-todo-line-regexp
963 (string-match org-todo-line-regexp line)
964 (match-beginning 2))
965 (setq line (concat
966 (substring line 0 (match-beginning 2))
967 (org-lparse-format 'TODO (match-string 2 line))
968 (substring line (match-end 2)))))
970 ;; Does this contain a reference to a footnote?
971 (when org-export-with-footnotes
972 (setq start 0)
973 (while (string-match "\\([^* \t].*?\\)[ \t]*\\[\\([0-9]+\\)\\]" line start)
974 ;; Discard protected matches not clearly identified as
975 ;; footnote markers.
976 (if (or (get-text-property (match-beginning 2) 'org-protected line)
977 (not (get-text-property (match-beginning 2) 'org-footnote line)))
978 (setq start (match-end 2))
979 (let ((n (match-string 2 line)) refcnt a)
980 (if (setq a (assoc n footref-seen))
981 (progn
982 (setcdr a (1+ (cdr a)))
983 (setq refcnt (cdr a)))
984 (setq refcnt 1)
985 (push (cons n 1) footref-seen))
986 (setq line
987 (replace-match
988 (concat
989 (or (match-string 1 line) "")
990 (org-lparse-format
991 'FOOTNOTE-REFERENCE
992 n (cdr (assoc n org-lparse-footnote-definitions))
993 refcnt)
994 ;; If another footnote is following the
995 ;; current one, add a separator.
996 (if (save-match-data
997 (string-match "\\`\\[[0-9]+\\]"
998 (substring line (match-end 0))))
999 (ignore-errors
1000 (org-lparse-get 'FOOTNOTE-SEPARATOR))
1001 ""))
1002 t t line))))))
1004 (cond
1005 ((string-match "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$" line)
1006 ;; This is a headline
1007 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
1008 level-offset))
1009 txt (match-string 2 line))
1010 (if (string-match quote-re0 txt)
1011 (setq txt (replace-match "" t t txt)))
1012 (if (<= level (max umax umax-toc))
1013 (setq head-count (+ head-count 1)))
1014 (unless org-lparse-dyn-first-heading-pos
1015 (setq org-lparse-dyn-first-heading-pos (point)))
1016 (org-lparse-begin-level level txt umax head-count)
1018 ;; QUOTES
1019 (when (string-match quote-re line)
1020 (org-lparse-begin-environment 'quote)))
1022 ((and org-export-with-tables
1023 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
1024 (when (not table-open)
1025 ;; New table starts
1026 (setq table-open t table-buffer nil table-orig-buffer nil))
1028 ;; Accumulate lines
1029 (setq table-buffer (cons line table-buffer)
1030 table-orig-buffer (cons origline table-orig-buffer))
1031 (when (or (not lines)
1032 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
1033 (car lines))))
1034 (setq table-open nil
1035 table-buffer (nreverse table-buffer)
1036 table-orig-buffer (nreverse table-orig-buffer))
1037 (org-lparse-end-paragraph)
1038 (when org-lparse-list-table-p
1039 (error "Regular tables are not allowed in a list-table block"))
1040 (org-lparse-insert 'TABLE table-buffer table-orig-buffer)))
1042 ;; Normal lines
1044 ;; This line either is list item or end a list.
1045 (when (get-text-property 0 'list-item line)
1046 (setq line (org-lparse-export-list-line
1047 line
1048 (get-text-property 0 'list-item line)
1049 (get-text-property 0 'list-struct line)
1050 (get-text-property 0 'list-prevs line))))
1052 ;; Horizontal line
1053 (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
1054 (with-org-lparse-preserve-paragraph-state
1055 (org-lparse-insert 'HORIZONTAL-LINE))
1056 (throw 'nextline nil))
1058 ;; Empty lines start a new paragraph. If hand-formatted lists
1059 ;; are not fully interpreted, lines starting with "-", "+", "*"
1060 ;; also start a new paragraph.
1061 (when (string-match "^ [-+*]-\\|^[ \t]*$" line)
1062 (when org-lparse-footnote-number
1063 (org-lparse-end-footnote-definition org-lparse-footnote-number)
1064 (setq org-lparse-footnote-number nil))
1065 (org-lparse-begin-paragraph))
1067 ;; Is this the start of a footnote?
1068 (when org-export-with-footnotes
1069 (when (and (boundp 'footnote-section-tag-regexp)
1070 (string-match (concat "^" footnote-section-tag-regexp)
1071 line))
1072 ;; ignore this line
1073 (throw 'nextline nil))
1074 (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
1075 (org-lparse-end-paragraph)
1076 (setq org-lparse-footnote-number (match-string 1 line))
1077 (setq line (replace-match "" t t line))
1078 (org-lparse-begin-footnote-definition org-lparse-footnote-number)))
1079 ;; Check if the line break needs to be conserved
1080 (cond
1081 ((string-match "\\\\\\\\[ \t]*$" line)
1082 (setq line (replace-match
1083 (org-lparse-format 'LINE-BREAK)
1084 t t line)))
1085 (org-export-preserve-breaks
1086 (setq line (concat line (org-lparse-format 'LINE-BREAK)))))
1088 ;; Check if a paragraph should be started
1089 (let ((start 0))
1090 (while (and org-lparse-par-open
1091 (string-match "\\\\par\\>" line start))
1092 (error "FIXME")
1093 ;; Leave a space in the </p> so that the footnote matcher
1094 ;; does not see this.
1095 (if (not (get-text-property (match-beginning 0)
1096 'org-protected line))
1097 (setq line (replace-match "</p ><p >" t t line)))
1098 (setq start (match-end 0))))
1100 (org-lparse-insert 'LINE line)))))
1102 ;; Properly close all local lists and other lists
1103 (when (org-lparse-current-environment-p 'quote)
1104 (org-lparse-end-environment 'quote))
1106 (org-lparse-end-level 1 umax)
1108 ;; the </div> to close the last text-... div.
1109 (when (and (> umax 0) org-lparse-dyn-first-heading-pos)
1110 (org-lparse-end-outline-text-or-outline))
1112 (org-lparse-end 'DOCUMENT-BODY opt-plist)
1113 (unless body-only
1114 (org-lparse-end 'DOCUMENT-CONTENT))
1116 (org-lparse-end 'EXPORT)
1118 ;; kill collection buffer
1119 (when org-lparse-collect-buffer
1120 (kill-buffer org-lparse-collect-buffer))
1122 (goto-char (point-min))
1123 (or (org-export-push-to-kill-ring
1124 (upcase (symbol-name org-lparse-backend)))
1125 (message "Exporting... done"))
1127 (cond
1128 ((not to-buffer)
1129 (let ((f (org-lparse-get 'SAVE-METHOD)))
1130 (or (and f (functionp f) (funcall f filename opt-plist))
1131 (save-buffer)))
1132 (or (and (boundp 'org-lparse-other-backend)
1133 org-lparse-other-backend
1134 (not (equal org-lparse-backend org-lparse-other-backend))
1135 (org-lparse-do-convert
1136 buffer-file-name (symbol-name org-lparse-other-backend)))
1137 (current-buffer)))
1138 ((eq to-buffer 'string)
1139 (prog1 (buffer-substring (point-min) (point-max))
1140 (kill-buffer (current-buffer))))
1141 (t (current-buffer))))))
1143 (defun org-lparse-format-table (lines olines)
1144 "Returns backend-specific code for org-type and table-type tables."
1145 (if (stringp lines)
1146 (setq lines (org-split-string lines "\n")))
1147 (if (string-match "^[ \t]*|" (car lines))
1148 ;; A normal org table
1149 (org-lparse-format-org-table lines nil)
1150 ;; Table made by table.el
1151 (or (org-lparse-format-table-table-using-table-generate-source
1152 ;; FIXME: Need to take care of this during merge
1153 (if (eq org-lparse-backend 'xhtml) 'html org-lparse-backend)
1154 olines
1155 (not org-export-prefer-native-exporter-for-tables))
1156 ;; We are here only when table.el table has NO col or row
1157 ;; spanning and the user prefers using org's own converter for
1158 ;; exporting of such simple table.el tables.
1159 (org-lparse-format-table-table lines))))
1161 (defun org-lparse-table-get-colalign-info (lines)
1162 (let ((col-cookies (org-find-text-property-in-string
1163 'org-col-cookies (car lines))))
1164 (when (and col-cookies org-table-clean-did-remove-column)
1165 (setq col-cookies
1166 (mapcar (lambda (x) (cons (1- (car x)) (cdr x))) col-cookies)))
1167 col-cookies))
1169 (defvar org-lparse-table-style)
1170 (defvar org-lparse-table-ncols)
1171 (defvar org-lparse-table-rownum)
1172 (defvar org-lparse-table-is-styled)
1173 (defvar org-lparse-table-begin-marker)
1174 (defvar org-lparse-table-num-numeric-items-per-column)
1175 (defvar org-lparse-table-colalign-info)
1176 (defvar org-lparse-table-colalign-vector)
1178 ;; Following variables are defined in org-table.el
1179 (defvar org-table-number-fraction)
1180 (defvar org-table-number-regexp)
1181 (defun org-lparse-org-table-to-list-table (lines &optional splice)
1182 "Convert org-table to list-table.
1183 LINES is a list of the form (ROW1 ROW2 ROW3 ...) where each
1184 element is a `string' representing a single row of org-table.
1185 Thus each ROW has vertical separators \"|\" separating the table
1186 fields. A ROW could also be a row-group separator of the form
1187 \"|---...|\". Return a list of the form (ROW1 ROW2 ROW3
1188 ...). ROW could either be symbol `:hrule' or a list of the
1189 form (FIELD1 FIELD2 FIELD3 ...) as appropriate."
1190 (let (line lines-1)
1191 (cond
1192 (splice
1193 (while (setq line (pop lines))
1194 (unless (string-match "^[ \t]*|-" line)
1195 (push (org-split-string line "[ \t]*|[ \t]*") lines-1))))
1197 (while (setq line (pop lines))
1198 (cond
1199 ((string-match "^[ \t]*|-" line)
1200 (when lines
1201 (push :hrule lines-1)))
1203 (push (org-split-string line "[ \t]*|[ \t]*") lines-1))))))
1204 (nreverse lines-1)))
1206 (defun org-lparse-insert-org-table (lines &optional splice)
1207 "Format a org-type table into backend-specific code.
1208 LINES is a list of lines. Optional argument SPLICE means, do not
1209 insert header and surrounding <table> tags, just format the lines.
1210 Optional argument NO-CSS means use XHTML attributes instead of CSS
1211 for formatting. This is required for the DocBook exporter."
1212 (require 'org-table)
1213 ;; Get rid of hlines at beginning and end
1214 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
1215 (setq lines (nreverse lines))
1216 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
1217 (setq lines (nreverse lines))
1218 (when org-export-table-remove-special-lines
1219 ;; Check if the table has a marking column. If yes remove the
1220 ;; column and the special lines
1221 (setq lines (org-table-clean-before-export lines)))
1222 (let* ((caption (org-find-text-property-in-string 'org-caption (car lines)))
1223 (short-caption (or (org-find-text-property-in-string
1224 'org-caption-shortn (car lines)) caption))
1225 (caption (and caption (org-xml-encode-org-text caption)))
1226 (short-caption (and short-caption
1227 (org-xml-encode-plain-text short-caption)))
1228 (label (org-find-text-property-in-string 'org-label (car lines)))
1229 (org-lparse-table-colalign-info (org-lparse-table-get-colalign-info lines))
1230 (attributes (org-find-text-property-in-string 'org-attributes
1231 (car lines)))
1232 (head (and org-export-highlight-first-table-line
1233 (delq nil (mapcar
1234 (lambda (x) (string-match "^[ \t]*|-" x))
1235 (cdr lines))))))
1236 (setq lines (org-lparse-org-table-to-list-table lines splice))
1237 (org-lparse-insert-list-table
1238 lines splice caption label attributes head org-lparse-table-colalign-info
1239 short-caption)))
1241 (defun org-lparse-insert-list-table (lines &optional splice
1242 caption label attributes head
1243 org-lparse-table-colalign-info
1244 short-caption)
1245 (or (featurep 'org-table) ; required for
1246 (require 'org-table)) ; `org-table-number-regexp'
1247 (let* ((org-lparse-table-rownum -1) org-lparse-table-ncols i (cnt 0)
1248 tbopen fields line
1249 org-lparse-table-cur-rowgrp-is-hdr
1250 org-lparse-table-rowgrp-open
1251 org-lparse-table-num-numeric-items-per-column
1252 org-lparse-table-colalign-vector n
1253 org-lparse-table-rowgrp-info
1254 org-lparse-table-begin-marker
1255 (org-lparse-table-style 'org-table)
1256 org-lparse-table-is-styled)
1257 (cond
1258 (splice
1259 (setq org-lparse-table-is-styled nil)
1260 (while (setq line (pop lines))
1261 (insert (org-lparse-format-table-row line) "\n")))
1263 (setq org-lparse-table-is-styled t)
1264 (org-lparse-begin 'TABLE caption label attributes short-caption)
1265 (setq org-lparse-table-begin-marker (point))
1266 (org-lparse-begin-table-rowgroup head)
1267 (while (setq line (pop lines))
1268 (cond
1269 ((equal line :hrule)
1270 (org-lparse-begin-table-rowgroup))
1272 (insert (org-lparse-format-table-row line) "\n"))))
1273 (org-lparse-end 'TABLE-ROWGROUP)
1274 (org-lparse-end-table)))))
1276 (defun org-lparse-format-org-table (lines &optional splice)
1277 (with-temp-buffer
1278 (org-lparse-insert-org-table lines splice)
1279 (buffer-substring-no-properties (point-min) (point-max))))
1281 (defun org-lparse-format-list-table (lines &optional splice)
1282 (with-temp-buffer
1283 (org-lparse-insert-list-table lines splice)
1284 (buffer-substring-no-properties (point-min) (point-max))))
1286 (defun org-lparse-insert-table-table (lines)
1287 "Format a table generated by table.el into backend-specific code.
1288 This conversion does *not* use `table-generate-source' from table.el.
1289 This has the advantage that Org-mode's HTML conversions can be used.
1290 But it has the disadvantage, that no cell- or row-spanning is allowed."
1291 (let (line field-buffer
1292 (org-lparse-table-cur-rowgrp-is-hdr
1293 org-export-highlight-first-table-line)
1294 (caption nil)
1295 (short-caption nil)
1296 (attributes nil)
1297 (label nil)
1298 (org-lparse-table-style 'table-table)
1299 (org-lparse-table-is-styled nil)
1300 fields org-lparse-table-ncols i (org-lparse-table-rownum -1)
1301 (empty (org-lparse-format 'SPACES 1)))
1302 (org-lparse-begin 'TABLE caption label attributes short-caption)
1303 (while (setq line (pop lines))
1304 (cond
1305 ((string-match "^[ \t]*\\+-" line)
1306 (when field-buffer
1307 (let ((org-export-table-row-tags '("<tr>" . "</tr>"))
1308 ;; (org-export-html-table-use-header-tags-for-first-column nil)
1310 (insert (org-lparse-format-table-row field-buffer empty)))
1311 (setq org-lparse-table-cur-rowgrp-is-hdr nil)
1312 (setq field-buffer nil)))
1314 ;; Break the line into fields and store the fields
1315 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
1316 (if field-buffer
1317 (setq field-buffer (mapcar
1318 (lambda (x)
1319 (concat x (org-lparse-format 'LINE-BREAK)
1320 (pop fields)))
1321 field-buffer))
1322 (setq field-buffer fields)))))
1323 (org-lparse-end-table)))
1325 (defun org-lparse-format-table-table (lines)
1326 (with-temp-buffer
1327 (org-lparse-insert-table-table lines)
1328 (buffer-substring-no-properties (point-min) (point-max))))
1330 (defvar table-source-languages) ; defined in table.el
1331 (defun org-lparse-format-table-table-using-table-generate-source (backend
1332 lines
1333 &optional
1334 spanned-only)
1335 "Format a table into BACKEND, using `table-generate-source' from table.el.
1336 Use SPANNED-ONLY to suppress exporting of simple table.el tables.
1338 When SPANNED-ONLY is nil, all table.el tables are exported. When
1339 SPANNED-ONLY is non-nil, only tables with either row or column
1340 spans are exported.
1342 This routine returns the generated source or nil as appropriate.
1344 Refer docstring of `org-export-prefer-native-exporter-for-tables'
1345 for further information."
1346 (require 'table)
1347 (with-current-buffer (get-buffer-create " org-tmp1 ")
1348 (erase-buffer)
1349 (insert (mapconcat 'identity lines "\n"))
1350 (goto-char (point-min))
1351 (if (not (re-search-forward "|[^+]" nil t))
1352 (error "Error processing table"))
1353 (table-recognize-table)
1354 (when (or (not spanned-only)
1355 (let* ((dim (table-query-dimension))
1356 (c (nth 4 dim)) (r (nth 5 dim)) (cells (nth 6 dim)))
1357 (not (= (* c r) cells))))
1358 (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
1359 (cond
1360 ((member backend table-source-languages)
1361 (table-generate-source backend " org-tmp2 ")
1362 (set-buffer " org-tmp2 ")
1363 (buffer-substring (point-min) (point-max)))
1365 ;; table.el doesn't support the given backend. Currently this
1366 ;; happens in case of odt export. Strip the table from the
1367 ;; generated document. A better alternative would be to embed
1368 ;; the table as ascii text in the output document.
1369 (org-lparse-warn
1370 (concat
1371 "Found table.el-type table in the source org file. "
1372 (format "table.el doesn't support %s backend. "
1373 (upcase (symbol-name backend)))
1374 "Skipping ahead ..."))
1375 "")))))
1377 (defun org-lparse-handle-time-stamps (s)
1378 "Format time stamps in string S, or remove them."
1379 (catch 'exit
1380 (let (r b)
1381 (when org-maybe-keyword-time-regexp
1382 (while (string-match org-maybe-keyword-time-regexp s)
1383 (or b (setq b (substring s 0 (match-beginning 0))))
1384 (setq r (concat
1385 r (substring s 0 (match-beginning 0)) " "
1386 (org-lparse-format
1387 'FONTIFY
1388 (concat
1389 (if (match-end 1)
1390 (org-lparse-format
1391 'FONTIFY
1392 (match-string 1 s) "timestamp-kwd"))
1394 (org-lparse-format
1395 'FONTIFY
1396 (substring (org-translate-time (match-string 3 s)) 1 -1)
1397 "timestamp"))
1398 "timestamp-wrapper"))
1399 s (substring s (match-end 0)))))
1401 ;; Line break if line started and ended with time stamp stuff
1402 (if (not r)
1404 (setq r (concat r s))
1405 (unless (string-match "\\S-" (concat b s))
1406 (setq r (concat r (org-lparse-format 'LINE-BREAK))))
1407 r))))
1409 (defun org-xml-encode-plain-text (s)
1410 "Convert plain text characters to HTML equivalent.
1411 Possible conversions are set in `org-export-html-protect-char-alist'."
1412 (let ((cl (org-lparse-get 'PLAIN-TEXT-MAP)) c)
1413 (while (setq c (pop cl))
1414 (let ((start 0))
1415 (while (string-match (car c) s start)
1416 (setq s (replace-match (cdr c) t t s)
1417 start (1+ (match-beginning 0))))))
1420 (defun org-xml-encode-org-text-skip-links (string)
1421 "Prepare STRING for HTML export. Apply all active conversions.
1422 If there are links in the string, don't modify these. If STRING
1423 is nil, return nil."
1424 (when string
1425 (let* ((re (concat org-bracket-link-regexp "\\|"
1426 (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")))
1427 m s l res)
1428 (while (setq m (string-match re string))
1429 (setq s (substring string 0 m)
1430 l (match-string 0 string)
1431 string (substring string (match-end 0)))
1432 (push (org-xml-encode-org-text s) res)
1433 (push l res))
1434 (push (org-xml-encode-org-text string) res)
1435 (apply 'concat (nreverse res)))))
1437 (defun org-xml-encode-org-text (s)
1438 "Apply all active conversions to translate special ASCII to HTML."
1439 (setq s (org-xml-encode-plain-text s))
1440 (if org-export-html-expand
1441 (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
1442 (setq s (replace-match "<\\1>" t nil s))))
1443 (if org-export-with-emphasize
1444 (setq s (org-lparse-apply-char-styles s)))
1445 (if org-export-with-special-strings
1446 (setq s (org-lparse-convert-special-strings s)))
1447 (if org-export-with-sub-superscripts
1448 (setq s (org-lparse-apply-sub-superscript-styles s)))
1449 (if org-export-with-TeX-macros
1450 (let ((start 0) wd rep)
1451 (while (setq start (string-match "\\\\\\([a-zA-Z]+[0-9]*\\)\\({}\\)?"
1452 s start))
1453 (if (get-text-property (match-beginning 0) 'org-protected s)
1454 (setq start (match-end 0))
1455 (setq wd (match-string 1 s))
1456 (if (setq rep (org-lparse-format 'ORG-ENTITY wd))
1457 (setq s (replace-match rep t t s))
1458 (setq start (+ start (length wd))))))))
1461 (defun org-lparse-convert-special-strings (string)
1462 "Convert special characters in STRING to HTML."
1463 (let ((all (org-lparse-get 'SPECIAL-STRING-REGEXPS))
1464 e a re rpl start)
1465 (while (setq a (pop all))
1466 (setq re (car a) rpl (cdr a) start 0)
1467 (while (string-match re string start)
1468 (if (get-text-property (match-beginning 0) 'org-protected string)
1469 (setq start (match-end 0))
1470 (setq string (replace-match rpl t nil string)))))
1471 string))
1473 (defun org-lparse-apply-sub-superscript-styles (string)
1474 "Apply subscript and superscript styles to STRING.
1475 Use `org-export-with-sub-superscripts' to control application of
1476 sub and superscript styles."
1477 (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
1478 (while (string-match org-match-substring-regexp string s)
1479 (cond
1480 ((and requireb (match-end 8)) (setq s (match-end 2)))
1481 ((get-text-property (match-beginning 2) 'org-protected string)
1482 (setq s (match-end 2)))
1484 (setq s (match-end 1)
1485 key (if (string= (match-string 2 string) "_")
1486 'subscript 'superscript)
1487 c (or (match-string 8 string)
1488 (match-string 6 string)
1489 (match-string 5 string))
1490 string (replace-match
1491 (concat (match-string 1 string)
1492 (org-lparse-format 'FONTIFY c key))
1493 t t string)))))
1494 (while (string-match "\\\\\\([_^]\\)" string)
1495 (setq string (replace-match (match-string 1 string) t t string)))
1496 string))
1498 (defvar org-lparse-char-styles
1499 `(("*" bold)
1500 ("/" emphasis)
1501 ("_" underline)
1502 ("=" code)
1503 ("~" verbatim)
1504 ("+" strike))
1505 "Map Org emphasis markers to char styles.
1506 This is an alist where each element is of the
1507 form (ORG-EMPHASIS-CHAR . CHAR-STYLE).")
1509 (defun org-lparse-apply-char-styles (string)
1510 "Apply char styles to STRING.
1511 The variable `org-lparse-char-styles' controls how the Org
1512 emphasis markers are interpreted."
1513 (let ((s 0) rpl)
1514 (while (string-match org-emph-re string s)
1515 (if (not (equal
1516 (substring string (match-beginning 3) (1+ (match-beginning 3)))
1517 (substring string (match-beginning 4) (1+ (match-beginning 4)))))
1518 (setq s (match-beginning 0)
1520 (concat
1521 (match-string 1 string)
1522 (org-lparse-format
1523 'FONTIFY (match-string 4 string)
1524 (nth 1 (assoc (match-string 3 string)
1525 org-lparse-char-styles)))
1526 (match-string 5 string))
1527 string (replace-match rpl t t string)
1528 s (+ s (- (length rpl) 2)))
1529 (setq s (1+ s))))
1530 string))
1532 (defun org-lparse-export-list-line (line pos struct prevs)
1533 "Insert list syntax in export buffer. Return LINE, maybe modified.
1535 POS is the item position or line position the line had before
1536 modifications to buffer. STRUCT is the list structure. PREVS is
1537 the alist of previous items."
1538 (let* ((get-type
1539 (function
1540 ;; Translate type of list containing POS to "d", "o" or
1541 ;; "u".
1542 (lambda (pos struct prevs)
1543 (let ((type (org-list-get-list-type pos struct prevs)))
1544 (cond
1545 ((eq 'ordered type) "o")
1546 ((eq 'descriptive type) "d")
1547 (t "u"))))))
1548 (get-closings
1549 (function
1550 ;; Return list of all items and sublists ending at POS, in
1551 ;; reverse order.
1552 (lambda (pos)
1553 (let (out)
1554 (catch 'exit
1555 (mapc (lambda (e)
1556 (let ((end (nth 6 e))
1557 (item (car e)))
1558 (cond
1559 ((= end pos) (push item out))
1560 ((>= item pos) (throw 'exit nil)))))
1561 struct))
1562 out)))))
1563 ;; First close any previous item, or list, ending at POS.
1564 (mapc (lambda (e)
1565 (let* ((lastp (= (org-list-get-last-item e struct prevs) e))
1566 (first-item (org-list-get-list-begin e struct prevs))
1567 (type (funcall get-type first-item struct prevs)))
1568 (org-lparse-end-paragraph)
1569 ;; Ending for every item
1570 (org-lparse-end-list-item-1 type)
1571 ;; We're ending last item of the list: end list.
1572 (when lastp
1573 (org-lparse-end-list type)
1574 (org-lparse-begin-paragraph))))
1575 (funcall get-closings pos))
1576 (cond
1577 ;; At an item: insert appropriate tags in export buffer.
1578 ((assq pos struct)
1579 (string-match
1580 (concat "[ \t]*\\(\\S-+[ \t]*\\)"
1581 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
1582 "\\(?:\\(\\[[ X-]\\]\\)[ \t]+\\)?"
1583 "\\(?:\\(.*\\)[ \t]+::\\(?:[ \t]+\\|$\\)\\)?"
1584 "\\(.*\\)") line)
1585 (let* ((checkbox (match-string 3 line))
1586 (desc-tag (or (match-string 4 line) "???"))
1587 (body (or (match-string 5 line) ""))
1588 (list-beg (org-list-get-list-begin pos struct prevs))
1589 (firstp (= list-beg pos))
1590 ;; Always refer to first item to determine list type, in
1591 ;; case list is ill-formed.
1592 (type (funcall get-type list-beg struct prevs))
1593 (counter (let ((count-tmp (org-list-get-counter pos struct)))
1594 (cond
1595 ((not count-tmp) nil)
1596 ((string-match "[A-Za-z]" count-tmp)
1597 (- (string-to-char (upcase count-tmp)) 64))
1598 ((string-match "[0-9]+" count-tmp)
1599 count-tmp)))))
1600 (when firstp
1601 (org-lparse-end-paragraph)
1602 (org-lparse-begin-list type))
1604 (let ((arg (cond ((equal type "d") desc-tag)
1605 ((equal type "o") counter))))
1606 (org-lparse-begin-list-item type arg))
1608 ;; If line had a checkbox, some additional modification is required.
1609 (when checkbox
1610 (setq body
1611 (concat
1612 (org-lparse-format
1613 'FONTIFY (concat
1615 (cond
1616 ((string-match "X" checkbox) "X")
1617 ((string-match " " checkbox)
1618 (org-lparse-format 'SPACES 1))
1619 (t "-"))
1620 "]")
1621 'code)
1623 body)))
1624 ;; Return modified line
1625 body))
1626 ;; At a list ender: go to next line (side-effects only).
1627 ((equal "ORG-LIST-END-MARKER" line) (throw 'nextline nil))
1628 ;; Not at an item: return line unchanged (side-effects only).
1629 (t line))))
1631 (defun org-lparse-bind-local-variables (opt-plist)
1632 (mapc (lambda (x)
1633 (set (make-local-variable (nth 2 x))
1634 (plist-get opt-plist (car x))))
1635 org-export-plist-vars))
1637 (defvar org-lparse-table-rowgrp-open)
1638 (defvar org-lparse-table-cur-rowgrp-is-hdr)
1639 (defvar org-lparse-footnote-number)
1640 (defvar org-lparse-footnote-definitions)
1641 (defvar org-lparse-output-buffer nil
1642 "Buffer to which `org-do-lparse' writes to.
1643 This buffer contains the contents of the to-be-created exported
1644 document.")
1646 (defcustom org-lparse-debug nil
1647 "Enable or Disable logging of `org-lparse' callbacks.
1648 The parameters passed to the backend-registered ENTITY-CONTROL
1649 and ENTITY-FORMAT callbacks are logged as comment strings in the
1650 exported buffer. (org-lparse-format 'COMMENT fmt args) is used
1651 for logging. Customize this variable only if you are an expert
1652 user. Valid values of this variable are:
1653 nil : Disable logging
1654 control : Log all invocations of `org-lparse-begin' and
1655 `org-lparse-end' callbacks.
1656 format : Log invocations of `org-lparse-format' callbacks.
1657 t : Log all invocations of `org-lparse-begin', `org-lparse-end'
1658 and `org-lparse-format' callbacks,"
1659 :group 'org-lparse
1660 :type '(choice
1661 (const :tag "Disable" nil)
1662 (const :tag "Format callbacks" format)
1663 (const :tag "Control callbacks" control)
1664 (const :tag "Format and Control callbacks" t)))
1666 (defun org-lparse-begin (entity &rest args)
1667 "Begin ENTITY in current buffer. ARGS is entity specific.
1668 ENTITY can be one of PARAGRAPH, LIST, LIST-ITEM etc.
1670 Use (org-lparse-begin 'LIST \"o\") to begin a list in current
1671 buffer.
1673 See `org-xhtml-entity-control-callbacks-alist' for more
1674 information."
1675 (when (and (member org-lparse-debug '(t control))
1676 (not (eq entity 'DOCUMENT-CONTENT)))
1677 (insert (org-lparse-format 'COMMENT "%s BEGIN %S" entity args)))
1679 (let ((f (cadr (assoc entity org-lparse-entity-control-callbacks-alist))))
1680 (unless f (error "Unknown entity: %s" entity))
1681 (apply f args)))
1683 (defun org-lparse-end (entity &rest args)
1684 "Close ENTITY in current buffer. ARGS is entity specific.
1685 ENTITY can be one of PARAGRAPH, LIST, LIST-ITEM
1686 etc.
1688 Use (org-lparse-end 'LIST \"o\") to close a list in current
1689 buffer.
1691 See `org-xhtml-entity-control-callbacks-alist' for more
1692 information."
1693 (when (and (member org-lparse-debug '(t control))
1694 (not (eq entity 'DOCUMENT-CONTENT)))
1695 (insert (org-lparse-format 'COMMENT "%s END %S" entity args)))
1697 (let ((f (caddr (assoc entity org-lparse-entity-control-callbacks-alist))))
1698 (unless f (error "Unknown entity: %s" entity))
1699 (apply f args)))
1701 (defun org-lparse-begin-paragraph (&optional style)
1702 "Insert <p>, but first close previous paragraph if any."
1703 (org-lparse-end-paragraph)
1704 (org-lparse-begin 'PARAGRAPH style)
1705 (setq org-lparse-par-open t))
1707 (defun org-lparse-end-paragraph ()
1708 "Close paragraph if there is one open."
1709 (when org-lparse-par-open
1710 (org-lparse-end 'PARAGRAPH)
1711 (setq org-lparse-par-open nil)))
1713 (defun org-lparse-end-list-item-1 (&optional type)
1714 "Close <li> if necessary."
1715 (org-lparse-end-paragraph)
1716 (org-lparse-end-list-item (or type "u")))
1718 (define-obsolete-function-alias
1719 'org-lparse-preprocess-after-blockquote-hook
1720 'org-lparse-preprocess-after-blockquote
1721 "24.3")
1723 (defun org-lparse-preprocess-after-blockquote ()
1724 "Treat `org-lparse-special-blocks' specially."
1725 (goto-char (point-min))
1726 (while (re-search-forward
1727 "^[ \t]*#\\+\\(begin\\|end\\)_\\(\\S-+\\)[ \t]*\\(.*\\)$" nil t)
1728 (when (member (downcase (match-string 2)) org-lparse-special-blocks)
1729 (replace-match
1730 (if (equal (downcase (match-string 1)) "begin")
1731 (format "ORG-%s-START %s" (upcase (match-string 2))
1732 (match-string 3))
1733 (format "ORG-%s-END %s" (upcase (match-string 2))
1734 (match-string 3))) t t))))
1736 (define-obsolete-function-alias
1737 'org-lparse-strip-experimental-blocks-maybe-hook
1738 'org-lparse-strip-experimental-blocks-maybe
1739 "24.3")
1741 (defun org-lparse-strip-experimental-blocks-maybe ()
1742 "Strip \"list-table\" and \"annotation\" blocks.
1743 Stripping happens only when the exported backend is not one of
1744 \"odt\" or \"xhtml\"."
1745 (when (not org-lparse-backend)
1746 (message "Stripping following blocks - %S" org-lparse-special-blocks)
1747 (goto-char (point-min))
1748 (let ((case-fold-search t))
1749 (while
1750 (re-search-forward
1751 "^[ \t]*#\\+begin_\\(\\S-+\\)\\([ \t]+.*\\)?\n\\([^\000]*?\\)\n[ \t]*#\\+end_\\1\\>.*"
1752 nil t)
1753 (when (member (match-string 1) org-lparse-special-blocks)
1754 (replace-match "" t t))))))
1756 (defvar org-lparse-list-table-p nil
1757 "Non-nil if `org-do-lparse' is within a list-table.")
1759 (defvar org-lparse-dyn-current-environment nil)
1760 (defun org-lparse-begin-environment (style &optional env-options-plist)
1761 (case style
1762 (list-table
1763 (setq org-lparse-list-table-p t))
1764 (t (setq org-lparse-dyn-current-environment style)
1765 (org-lparse-begin 'ENVIRONMENT style env-options-plist))))
1767 (defun org-lparse-end-environment (style &optional env-options-plist)
1768 (case style
1769 (list-table
1770 (setq org-lparse-list-table-p nil))
1771 (t (org-lparse-end 'ENVIRONMENT style env-options-plist)
1772 (setq org-lparse-dyn-current-environment nil))))
1774 (defun org-lparse-current-environment-p (style)
1775 (eq org-lparse-dyn-current-environment style))
1777 (defun org-lparse-begin-footnote-definition (n)
1778 (org-lparse-begin-collect)
1779 (setq org-lparse-insert-tag-with-newlines nil)
1780 (org-lparse-begin 'FOOTNOTE-DEFINITION n))
1782 (defun org-lparse-end-footnote-definition (n)
1783 (org-lparse-end 'FOOTNOTE-DEFINITION n)
1784 (setq org-lparse-insert-tag-with-newlines 'both)
1785 (let ((footnote-def (org-lparse-end-collect)))
1786 ;; Cleanup newlines in footnote definition. This ensures that a
1787 ;; transcoded line is never (wrongly) broken in to multiple lines.
1788 (let ((pos 0))
1789 (while (string-match "[\r\n]+" footnote-def pos)
1790 (setq pos (1+ (match-beginning 0)))
1791 (setq footnote-def (replace-match " " t t footnote-def))))
1792 (push (cons n footnote-def) org-lparse-footnote-definitions)))
1794 (defvar org-lparse-collect-buffer nil
1795 "An auxiliary buffer named \"*Org Lparse Collect*\".
1796 `org-do-lparse' uses this as output buffer while collecting
1797 footnote definitions and table-cell contents of list-tables. See
1798 `org-lparse-begin-collect' and `org-lparse-end-collect'.")
1800 (defvar org-lparse-collect-count nil
1801 "Count number of calls to `org-lparse-begin-collect'.
1802 Use this counter to catch chained collections if they ever
1803 happen.")
1805 (defun org-lparse-begin-collect ()
1806 "Temporarily switch to `org-lparse-collect-buffer'.
1807 Also erase it's contents."
1808 (unless (zerop org-lparse-collect-count)
1809 (error "FIXME (org-lparse.el): Encountered chained collections"))
1810 (incf org-lparse-collect-count)
1811 (unless org-lparse-collect-buffer
1812 (setq org-lparse-collect-buffer
1813 (get-buffer-create "*Org Lparse Collect*")))
1814 (set-buffer org-lparse-collect-buffer)
1815 (erase-buffer))
1817 (defun org-lparse-end-collect ()
1818 "Switch to `org-lparse-output-buffer'.
1819 Return contents of `org-lparse-collect-buffer' as a `string'."
1820 (assert (> org-lparse-collect-count 0))
1821 (decf org-lparse-collect-count)
1822 (prog1 (buffer-string)
1823 (erase-buffer)
1824 (set-buffer org-lparse-output-buffer)))
1826 (defun org-lparse-format (entity &rest args)
1827 "Format ENTITY in backend-specific way and return it.
1828 ARGS is specific to entity being formatted.
1830 Use (org-lparse-format 'HEADING \"text\" 1) to format text as
1831 level 1 heading.
1833 See `org-xhtml-entity-format-callbacks-alist' for more information."
1834 (when (and (member org-lparse-debug '(t format))
1835 (not (equal entity 'COMMENT)))
1836 (insert (org-lparse-format 'COMMENT "%s: %S" entity args)))
1837 (cond
1838 ((consp entity)
1839 (let ((text (pop args)))
1840 (apply 'org-lparse-format 'TAGS entity text args)))
1842 (let ((f (cdr (assoc entity org-lparse-entity-format-callbacks-alist))))
1843 (unless f (error "Unknown entity: %s" entity))
1844 (apply f args)))))
1846 (defun org-lparse-insert (entity &rest args)
1847 (insert (apply 'org-lparse-format entity args)))
1849 (defun org-lparse-prepare-toc (lines level-offset opt-plist umax-toc)
1850 (let* ((quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
1851 (org-min-level (org-get-min-level lines level-offset))
1852 (org-last-level org-min-level)
1853 level)
1854 (with-temp-buffer
1855 (org-lparse-bind-local-variables opt-plist)
1856 (erase-buffer)
1857 (org-lparse-begin 'TOC (nth 3 (plist-get opt-plist :lang-words)) umax-toc)
1858 (setq
1859 lines
1860 (mapcar
1861 #'(lambda (line)
1862 (when (and (string-match org-todo-line-regexp line)
1863 (not (get-text-property 0 'org-protected line))
1864 (<= (setq level (org-tr-level
1865 (- (match-end 1) (match-beginning 1)
1866 level-offset)))
1867 umax-toc))
1868 (let ((txt (save-match-data
1869 (org-xml-encode-org-text-skip-links
1870 (org-export-cleanup-toc-line
1871 (match-string 3 line)))))
1872 (todo (and
1873 org-export-mark-todo-in-toc
1874 (or (and (match-beginning 2)
1875 (not (member (match-string 2 line)
1876 org-done-keywords)))
1877 (and (= level umax-toc)
1878 (org-search-todo-below
1879 line lines level)))))
1880 tags)
1881 ;; Check for targets
1882 (while (string-match org-any-target-regexp line)
1883 (setq line
1884 (replace-match
1885 (let ((org-lparse-encode-pending t))
1886 (org-lparse-format 'FONTIFY
1887 (match-string 1 line) "target"))
1888 t t line)))
1889 (when (string-match
1890 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
1891 (setq tags (match-string 1 txt)
1892 txt (replace-match "" t nil txt)))
1893 (when (string-match quote-re0 txt)
1894 (setq txt (replace-match "" t t txt)))
1895 (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
1896 (setq txt (replace-match "" t t txt)))
1897 (org-lparse-format
1898 'TOC-ITEM
1899 (let* ((snumber (org-section-number level))
1900 (href (replace-regexp-in-string
1901 "\\." "-" (format "sec-%s" snumber)))
1902 (href
1904 (cdr (assoc
1905 href org-export-preferred-target-alist))
1906 href))
1907 (href (org-solidify-link-text href)))
1908 (org-lparse-format 'TOC-ENTRY snumber todo txt tags href))
1909 level org-last-level)
1910 (setq org-last-level level)))
1911 line)
1912 lines))
1913 (org-lparse-end 'TOC)
1914 (setq org-lparse-toc (buffer-string))))
1915 lines)
1917 (defun org-lparse-format-table-row (fields &optional text-for-empty-fields)
1918 (if org-lparse-table-ncols
1919 ;; second and subsequent rows of the table
1920 (when (and org-lparse-list-table-p
1921 (> (length fields) org-lparse-table-ncols))
1922 (error "Table row has %d columns but header row claims %d columns"
1923 (length fields) org-lparse-table-ncols))
1924 ;; first row of the table
1925 (setq org-lparse-table-ncols (length fields))
1926 (when org-lparse-table-is-styled
1927 (setq org-lparse-table-num-numeric-items-per-column
1928 (make-vector org-lparse-table-ncols 0))
1929 (setq org-lparse-table-colalign-vector
1930 (make-vector org-lparse-table-ncols nil))
1931 (let ((c -1))
1932 (while (< (incf c) org-lparse-table-ncols)
1933 (let* ((col-cookie (cdr (assoc (1+ c) org-lparse-table-colalign-info)))
1934 (align (nth 0 col-cookie)))
1935 (setf (aref org-lparse-table-colalign-vector c)
1936 (cond
1937 ((string= align "l") "left")
1938 ((string= align "r") "right")
1939 ((string= align "c") "center"))))))))
1940 (incf org-lparse-table-rownum)
1941 (let ((i -1))
1942 (org-lparse-format
1943 'TABLE-ROW
1944 (mapconcat
1945 (lambda (x)
1946 (when (and (string= x "") text-for-empty-fields)
1947 (setq x text-for-empty-fields))
1948 (incf i)
1949 (let (col-cookie horiz-span)
1950 (when org-lparse-table-is-styled
1951 (when (and (< i org-lparse-table-ncols)
1952 (string-match org-table-number-regexp x))
1953 (incf (aref org-lparse-table-num-numeric-items-per-column i)))
1954 (setq col-cookie (cdr (assoc (1+ i) org-lparse-table-colalign-info))
1955 horiz-span (nth 1 col-cookie)))
1956 (org-lparse-format
1957 'TABLE-CELL x org-lparse-table-rownum i (or horiz-span 0))))
1958 fields "\n"))))
1960 (defun org-lparse-get (what &optional opt-plist)
1961 "Query for value of WHAT for the current backend `org-lparse-backend'.
1962 See also `org-lparse-backend-get'."
1963 (if (boundp 'org-lparse-backend)
1964 (org-lparse-backend-get (symbol-name org-lparse-backend) what opt-plist)
1965 (error "org-lparse-backend is not bound yet")))
1967 (defun org-lparse-backend-get (backend what &optional opt-plist)
1968 "Query BACKEND for value of WHAT.
1969 Dispatch the call to `org-<backend>-user-get'. If that throws an
1970 error, dispatch the call to `org-<backend>-get'. See
1971 `org-xhtml-get' for all known settings queried for by
1972 `org-lparse' during the course of export."
1973 (assert (stringp backend) t)
1974 (unless (org-lparse-backend-is-native-p backend)
1975 (error "Unknown native backend %s" backend))
1976 (let ((backend-get-method (intern (format "org-%s-get" backend)))
1977 (backend-user-get-method (intern (format "org-%s-user-get" backend))))
1978 (cond
1979 ((functionp backend-get-method)
1980 (condition-case nil
1981 (funcall backend-user-get-method what opt-plist)
1982 (error (funcall backend-get-method what opt-plist))))
1984 (error "Native backend %s doesn't define %s" backend backend-get-method)))))
1986 (defun org-lparse-insert-tag (tag &rest args)
1987 (when (member org-lparse-insert-tag-with-newlines '(lead both))
1988 (insert "\n"))
1989 (insert (apply 'format tag args))
1990 (when (member org-lparse-insert-tag-with-newlines '(trail both))
1991 (insert "\n")))
1993 (defun org-lparse-get-targets-from-title (title)
1994 (let* ((target (org-get-text-property-any 0 'target title))
1995 (extra-targets (assoc target org-export-target-aliases))
1996 (target (or (cdr (assoc target org-export-preferred-target-alist))
1997 target)))
1998 (cons target (remove target extra-targets))))
2000 (defun org-lparse-suffix-from-snumber (snumber)
2001 (let* ((snu (replace-regexp-in-string "\\." "-" snumber))
2002 (href (cdr (assoc (concat "sec-" snu)
2003 org-export-preferred-target-alist))))
2004 (org-solidify-link-text (or href snu))))
2006 (defun org-lparse-begin-level (level title umax head-count)
2007 "Insert a new LEVEL in HTML export.
2008 When TITLE is nil, just close all open levels."
2009 (org-lparse-end-level level umax)
2010 (unless title (error "Why is heading nil"))
2011 (let* ((targets (org-lparse-get-targets-from-title title))
2012 (target (car targets)) (extra-targets (cdr targets))
2013 (target (and target (org-solidify-link-text target)))
2014 (extra-class (org-get-text-property-any 0 'html-container-class title))
2015 snumber tags level1 class)
2016 (when (string-match (org-re "\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") title)
2017 (setq tags (and org-export-with-tags (match-string 1 title)))
2018 (setq title (replace-match "" t t title)))
2019 (if (> level umax)
2020 (progn
2021 (if (aref org-levels-open (1- level))
2022 (org-lparse-end-list-item-1)
2023 (aset org-levels-open (1- level) t)
2024 (org-lparse-end-paragraph)
2025 (org-lparse-begin-list 'unordered))
2026 (org-lparse-begin-list-item
2027 'unordered target (org-lparse-format
2028 'HEADLINE title extra-targets tags)))
2029 (aset org-levels-open (1- level) t)
2030 (setq snumber (org-section-number level))
2031 (setq level1 (+ level (or (org-lparse-get 'TOPLEVEL-HLEVEL) 1) -1))
2032 (unless (= head-count 1)
2033 (org-lparse-end-outline-text-or-outline))
2034 (org-lparse-begin-outline-and-outline-text
2035 level1 snumber title tags target extra-targets extra-class)
2036 (org-lparse-begin-paragraph))))
2038 (defun org-lparse-end-level (level umax)
2039 (org-lparse-end-paragraph)
2040 (loop for l from org-level-max downto level
2041 do (when (aref org-levels-open (1- l))
2042 ;; Terminate one level in HTML export
2043 (if (<= l umax)
2044 (org-lparse-end-outline-text-or-outline)
2045 (org-lparse-end-list-item-1)
2046 (org-lparse-end-list 'unordered))
2047 (aset org-levels-open (1- l) nil))))
2049 (defvar org-lparse-outline-text-open)
2050 (defun org-lparse-begin-outline-and-outline-text (level1 snumber title tags
2051 target extra-targets
2052 extra-class)
2053 (org-lparse-begin
2054 'OUTLINE level1 snumber title tags target extra-targets extra-class)
2055 (org-lparse-begin-outline-text level1 snumber extra-class))
2057 (defun org-lparse-end-outline-text-or-outline ()
2058 (cond
2059 (org-lparse-outline-text-open
2060 (org-lparse-end 'OUTLINE-TEXT)
2061 (setq org-lparse-outline-text-open nil))
2062 (t (org-lparse-end 'OUTLINE))))
2064 (defun org-lparse-begin-outline-text (level1 snumber extra-class)
2065 (assert (not org-lparse-outline-text-open) t)
2066 (setq org-lparse-outline-text-open t)
2067 (org-lparse-begin 'OUTLINE-TEXT level1 snumber extra-class))
2069 (defun org-lparse-html-list-type-to-canonical-list-type (ltype)
2070 (cdr (assoc ltype '(("o" . ordered)
2071 ("u" . unordered)
2072 ("d" . description)))))
2074 ;; following vars are bound during `org-do-lparse'
2075 (defvar org-lparse-list-stack)
2076 (defvar org-lparse-list-table:table-row)
2077 (defvar org-lparse-list-table:lines)
2079 ;; Notes on LIST-TABLES
2080 ;; ====================
2081 ;; Lists withing "list-table" blocks (as shown below)
2083 ;; #+begin_list-table
2084 ;; - Row 1
2085 ;; - 1.1
2086 ;; - 1.2
2087 ;; - 1.3
2088 ;; - Row 2
2089 ;; - 2.1
2090 ;; - 2.2
2091 ;; - 2.3
2092 ;; #+end_list-table
2094 ;; will be exported as though it were a table as shown below.
2096 ;; | Row 1 | 1.1 | 1.2 | 1.3 |
2097 ;; | Row 2 | 2.1 | 2.2 | 2.3 |
2099 ;; Note that org-tables are NOT multi-line and each line is mapped to
2100 ;; a unique row in the exported document. So if an exported table
2101 ;; needs to contain a single paragraph (with copious text) it needs to
2102 ;; be typed up in a single line. Editing such long lines using the
2103 ;; table editor will be a cumbersome task. Furthermore inclusion of
2104 ;; multi-paragraph text in a table cell is well-nigh impossible.
2106 ;; LIST-TABLEs are meant to circumvent the above problems with
2107 ;; org-tables.
2109 ;; Note that in the example above the list items could be paragraphs
2110 ;; themselves and the list can be arbitrarily deep.
2112 ;; Inspired by following thread:
2113 ;; https://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01101.html
2115 (defun org-lparse-begin-list (ltype)
2116 (push ltype org-lparse-list-stack)
2117 (let ((list-level (length org-lparse-list-stack)))
2118 (cond
2119 ((not org-lparse-list-table-p)
2120 (org-lparse-begin 'LIST ltype))
2121 ;; process LIST-TABLE
2122 ((= 1 list-level)
2123 ;; begin LIST-TABLE
2124 (setq org-lparse-list-table:lines nil)
2125 (setq org-lparse-list-table:table-row nil))
2126 ((= 2 list-level)
2127 (ignore))
2129 (org-lparse-begin 'LIST ltype)))))
2131 (defun org-lparse-end-list (ltype)
2132 (pop org-lparse-list-stack)
2133 (let ((list-level (length org-lparse-list-stack)))
2134 (cond
2135 ((not org-lparse-list-table-p)
2136 (org-lparse-end 'LIST ltype))
2137 ;; process LIST-TABLE
2138 ((= 0 list-level)
2139 ;; end LIST-TABLE
2140 (insert (org-lparse-format-list-table
2141 (nreverse org-lparse-list-table:lines))))
2142 ((= 1 list-level)
2143 (ignore))
2145 (org-lparse-end 'LIST ltype)))))
2147 (defun org-lparse-begin-list-item (ltype &optional arg headline)
2148 (let ((list-level (length org-lparse-list-stack)))
2149 (cond
2150 ((not org-lparse-list-table-p)
2151 (org-lparse-begin 'LIST-ITEM ltype arg headline))
2152 ;; process LIST-TABLE
2153 ((= 1 list-level)
2154 ;; begin TABLE-ROW for LIST-TABLE
2155 (setq org-lparse-list-table:table-row nil)
2156 (org-lparse-begin-list-table:table-cell))
2157 ((= 2 list-level)
2158 ;; begin TABLE-CELL for LIST-TABLE
2159 (org-lparse-begin-list-table:table-cell))
2161 (org-lparse-begin 'LIST-ITEM ltype arg headline)))))
2163 (defun org-lparse-end-list-item (ltype)
2164 (let ((list-level (length org-lparse-list-stack)))
2165 (cond
2166 ((not org-lparse-list-table-p)
2167 (org-lparse-end 'LIST-ITEM ltype))
2168 ;; process LIST-TABLE
2169 ((= 1 list-level)
2170 ;; end TABLE-ROW for LIST-TABLE
2171 (org-lparse-end-list-table:table-cell)
2172 (push (nreverse org-lparse-list-table:table-row)
2173 org-lparse-list-table:lines))
2174 ((= 2 list-level)
2175 ;; end TABLE-CELL for LIST-TABLE
2176 (org-lparse-end-list-table:table-cell))
2178 (org-lparse-end 'LIST-ITEM ltype)))))
2180 (defvar org-lparse-list-table:table-cell-open)
2181 (defun org-lparse-begin-list-table:table-cell ()
2182 (org-lparse-end-list-table:table-cell)
2183 (setq org-lparse-list-table:table-cell-open t)
2184 (org-lparse-begin-collect)
2185 (org-lparse-begin-paragraph))
2187 (defun org-lparse-end-list-table:table-cell ()
2188 (when org-lparse-list-table:table-cell-open
2189 (setq org-lparse-list-table:table-cell-open nil)
2190 (org-lparse-end-paragraph)
2191 (push (org-lparse-end-collect)
2192 org-lparse-list-table:table-row)))
2194 (defvar org-lparse-table-rowgrp-info)
2195 (defun org-lparse-begin-table-rowgroup (&optional is-header-row)
2196 (push (cons (1+ org-lparse-table-rownum) :start) org-lparse-table-rowgrp-info)
2197 (org-lparse-begin 'TABLE-ROWGROUP is-header-row))
2199 (defun org-lparse-end-table ()
2200 (when org-lparse-table-is-styled
2201 ;; column groups
2202 (unless (car org-table-colgroup-info)
2203 (setq org-table-colgroup-info
2204 (cons :start (cdr org-table-colgroup-info))))
2206 ;; column alignment
2207 (let ((c -1))
2208 (mapc
2209 (lambda (x)
2210 (incf c)
2211 (setf (aref org-lparse-table-colalign-vector c)
2212 (or (aref org-lparse-table-colalign-vector c)
2213 (if (> (/ (float x) (1+ org-lparse-table-rownum))
2214 org-table-number-fraction)
2215 "right" "left"))))
2216 org-lparse-table-num-numeric-items-per-column)))
2217 (org-lparse-end 'TABLE))
2219 (defvar org-lparse-encode-pending nil)
2221 (defun org-lparse-format-tags (tag text prefix suffix &rest args)
2222 (cond
2223 ((consp tag)
2224 (concat prefix (apply 'format (car tag) args) text suffix
2225 (format (cdr tag))))
2226 ((stringp tag) ; singleton tag
2227 (concat prefix (apply 'format tag args) text))))
2229 (defun org-xml-fix-class-name (kwd) ; audit callers of this function
2230 "Turn todo keyword into a valid class name.
2231 Replaces invalid characters with \"_\"."
2232 (save-match-data
2233 (while (string-match "[^a-zA-Z0-9_]" kwd)
2234 (setq kwd (replace-match "_" t t kwd))))
2235 kwd)
2237 (defun org-lparse-format-todo (todo)
2238 (org-lparse-format 'FONTIFY
2239 (concat
2240 (ignore-errors (org-lparse-get 'TODO-KWD-CLASS-PREFIX))
2241 (org-xml-fix-class-name todo))
2242 (list (if (member todo org-done-keywords) "done" "todo")
2243 todo)))
2245 (defun org-lparse-format-extra-targets (extra-targets)
2246 (if (not extra-targets) ""
2247 (mapconcat (lambda (x)
2248 (setq x (org-solidify-link-text
2249 (if (org-uuidgen-p x) (concat "ID-" x) x)))
2250 (org-lparse-format 'ANCHOR "" x))
2251 extra-targets "")))
2253 (defun org-lparse-format-org-tags (tags)
2254 (if (not tags) ""
2255 (org-lparse-format
2256 'FONTIFY (mapconcat
2257 (lambda (x)
2258 (org-lparse-format
2259 'FONTIFY x
2260 (concat
2261 (ignore-errors (org-lparse-get 'TAG-CLASS-PREFIX))
2262 (org-xml-fix-class-name x))))
2263 (org-split-string tags ":")
2264 (org-lparse-format 'SPACES 1)) "tag")))
2266 (defun org-lparse-format-section-number (&optional snumber level)
2267 (and org-export-with-section-numbers
2268 (not org-lparse-body-only) snumber level
2269 (org-lparse-format 'FONTIFY snumber (format "section-number-%d" level))))
2271 (defun org-lparse-warn (msg)
2272 (if (not org-lparse-use-flashy-warning)
2273 (message msg)
2274 (put-text-property 0 (length msg) 'face 'font-lock-warning-face msg)
2275 (message msg)
2276 (sleep-for 3)))
2278 (defun org-xml-format-href (s)
2279 "Make sure the S is valid as a href reference in an XHTML document."
2280 (save-match-data
2281 (let ((start 0))
2282 (while (string-match "&" s start)
2283 (setq start (+ (match-beginning 0) 3)
2284 s (replace-match "&amp;" t t s)))))
2287 (defun org-xml-format-desc (s)
2288 "Make sure the S is valid as a description in a link."
2289 (if (and s (not (get-text-property 1 'org-protected s)))
2290 (save-match-data
2291 (org-xml-encode-org-text s))
2294 (provide 'org-lparse)
2296 ;; Local variables:
2297 ;; generated-autoload-file: "org-loaddefs.el"
2298 ;; End:
2300 ;;; org-lparse.el ends here