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