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