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