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