org-lparse.el: Fix export of timestamps that occur as list items
[org-mode.git] / contrib / lisp / org-lparse.el
blobd4927e5a6c20156c23beefc9d33c2604a0388458
1 ;;; org-lparse.el --- Line-oriented parser-exporter for Org-mode
3 ;; Copyright (C) 2010, 2011
4 ;; Jambunathan <kjambunathan at gmail dot com>
6 ;; Author: Jambunathan K <kjambunathan at gmail dot com>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
9 ;; Version: 0.8
11 ;; This file is not (yet) part of GNU Emacs.
12 ;; However, it is distributed under the same license.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;;; Commentary:
30 ;; `org-lparse' is the entry point for the generic line-oriented
31 ;; exporter. `org-do-lparse' is the genericized version of the
32 ;; original `org-export-as-html' routine.
34 ;; `org-lparse-native-backends' is a good starting point for
35 ;; exploring the generic exporter.
37 ;; Following new interactive commands are provided by this library.
38 ;; `org-lparse', `org-lparse-and-open', `org-lparse-to-buffer'
39 ;; `org-replace-region-by', `org-lparse-region'.
41 ;; Note that the above routines correspond to the following routines
42 ;; in the html exporter `org-export-as-html',
43 ;; `org-export-as-html-and-open', `org-export-as-html-to-buffer',
44 ;; `org-replace-region-by-html' and `org-export-region-as-html'.
46 ;; The new interactive command `org-lparse-convert' can be used to
47 ;; convert documents between various formats. Use this to command,
48 ;; for example, to convert odt file to doc or pdf format.
50 ;; See README.org file that comes with this library for answers to
51 ;; FAQs and more information on using this library.
53 ;;; Code:
55 (require 'org-exp)
56 (require 'org-list)
58 ;;;###autoload
59 (defun org-lparse-and-open (target-backend native-backend arg)
60 "Export the outline to TARGET-BACKEND via NATIVE-BACKEND and open exported file.
61 If there is an active region, export only the region. The prefix
62 ARG specifies how many levels of the outline should become
63 headlines. The default is 3. Lower levels will become bulleted
64 lists."
65 ;; (interactive "Mbackend: \nP")
66 (interactive
67 (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
68 (all-backends (org-lparse-all-backends))
69 (target-backend
70 (funcall input "Export to: " all-backends nil t nil))
71 (native-backend
72 (or
73 ;; (and (org-lparse-backend-is-native-p target-backend)
74 ;; target-backend)
75 (funcall input "Use Native backend: "
76 (cdr (assoc target-backend all-backends)) nil t nil))))
77 (list target-backend native-backend current-prefix-arg)))
78 (let (f (file-or-buf (org-lparse target-backend native-backend
79 arg 'hidden)))
80 (when file-or-buf
81 (setq f (cond
82 ((bufferp file-or-buf) buffer-file-name)
83 ((file-exists-p file-or-buf) file-or-buf)
84 (t (error "org-lparse-and-open: This shouldn't happen"))))
85 (message "Opening file %s" f)
86 (org-open-file f)
87 (when org-export-kill-product-buffer-when-displayed
88 (kill-buffer (current-buffer))))))
90 ;;;###autoload
91 (defun org-lparse-batch (target-backend &optional native-backend)
92 "Call the function `org-lparse'.
93 This function can be used in batch processing as:
94 emacs --batch
95 --load=$HOME/lib/emacs/org.el
96 --eval \"(setq org-export-headline-levels 2)\"
97 --visit=MyFile --funcall org-lparse-batch"
98 (setq native-backend (or native-backend target-backend))
99 (org-lparse target-backend native-backend
100 org-export-headline-levels 'hidden))
102 ;;;###autoload
103 (defun org-lparse-to-buffer (backend arg)
104 "Call `org-lparse' with output to a temporary buffer.
105 No file is created. The prefix ARG is passed through to
106 `org-lparse'."
107 (interactive "Mbackend: \nP")
108 (let ((tempbuf (format "*Org %s Export*" (upcase backend))))
109 (org-lparse backend backend arg nil nil tempbuf)
110 (when org-export-show-temporary-export-buffer
111 (switch-to-buffer-other-window tempbuf))))
113 ;;;###autoload
114 (defun org-replace-region-by (backend beg end)
115 "Assume the current region has org-mode syntax, and convert it to HTML.
116 This can be used in any buffer. For example, you could write an
117 itemized list in org-mode syntax in an HTML buffer and then use this
118 command to convert it."
119 (interactive "Mbackend: \nr")
120 (let (reg backend-string buf pop-up-frames)
121 (save-window-excursion
122 (if (org-mode-p)
123 (setq backend-string (org-lparse-region backend beg end t 'string))
124 (setq reg (buffer-substring beg end)
125 buf (get-buffer-create "*Org tmp*"))
126 (with-current-buffer buf
127 (erase-buffer)
128 (insert reg)
129 (org-mode)
130 (setq backend-string (org-lparse-region backend (point-min)
131 (point-max) t 'string)))
132 (kill-buffer buf)))
133 (delete-region beg end)
134 (insert backend-string)))
136 ;;;###autoload
137 (defun org-lparse-region (backend beg end &optional body-only buffer)
138 "Convert region from BEG to END in org-mode buffer to HTML.
139 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
140 contents, and only produce the region of converted text, useful for
141 cut-and-paste operations.
142 If BUFFER is a buffer or a string, use/create that buffer as a target
143 of the converted HTML. If BUFFER is the symbol `string', return the
144 produced HTML as a string and leave not buffer behind. For example,
145 a Lisp program could call this function in the following way:
147 (setq html (org-lparse-region \"html\" beg end t 'string))
149 When called interactively, the output buffer is selected, and shown
150 in a window. A non-interactive call will only return the buffer."
151 (interactive "Mbackend: \nr\nP")
152 (when (org-called-interactively-p 'any)
153 (setq buffer (format "*Org %s Export*" (upcase backend))))
154 (let ((transient-mark-mode t) (zmacs-regions t)
155 ext-plist rtn)
156 (setq ext-plist (plist-put ext-plist :ignore-subtree-p t))
157 (goto-char end)
158 (set-mark (point)) ;; to activate the region
159 (goto-char beg)
160 (setq rtn (org-lparse backend backend nil nil ext-plist buffer body-only))
161 (if (fboundp 'deactivate-mark) (deactivate-mark))
162 (if (and (org-called-interactively-p 'any) (bufferp rtn))
163 (switch-to-buffer-other-window rtn)
164 rtn)))
166 (defvar org-lparse-par-open nil)
168 (defun org-lparse-should-inline-p (filename descp)
169 "Return non-nil if link FILENAME should be inlined.
170 The decision to inline the FILENAME link is based on the current
171 settings. DESCP is the boolean of whether there was a link
172 description. See variables `org-export-html-inline-images' and
173 `org-export-html-inline-image-extensions'."
174 (let ((inline-images (org-lparse-get 'INLINE-IMAGES))
175 (inline-image-extensions
176 (org-lparse-get 'INLINE-IMAGE-EXTENSIONS)))
177 (and (or (eq t inline-images) (and inline-images (not descp)))
178 (org-file-image-p filename inline-image-extensions))))
180 (defun org-lparse-format-org-link (line opt-plist)
181 "Return LINE with markup of Org mode links.
182 OPT-PLIST is the export options list."
183 (let ((start 0)
184 (current-dir (if buffer-file-name
185 (file-name-directory buffer-file-name)
186 default-directory))
187 (link-validate (plist-get opt-plist :link-validation-function))
188 type id-file fnc
189 rpl path attr desc descp desc1 desc2 link
190 org-lparse-link-description-is-image)
191 (while (string-match org-bracket-link-analytic-regexp++ line start)
192 (setq org-lparse-link-description-is-image nil)
193 (setq start (match-beginning 0))
194 (setq path (save-match-data (org-link-unescape
195 (match-string 3 line))))
196 (setq type (cond
197 ((match-end 2) (match-string 2 line))
198 ((save-match-data
199 (or (file-name-absolute-p path)
200 (string-match "^\\.\\.?/" path)))
201 "file")
202 (t "internal")))
203 (setq path (org-extract-attributes (org-link-unescape path)))
204 (setq attr (get-text-property 0 'org-attributes path))
205 (setq desc1 (if (match-end 5) (match-string 5 line))
206 desc2 (if (match-end 2) (concat type ":" path) path)
207 descp (and desc1 (not (equal desc1 desc2)))
208 desc (or desc1 desc2))
209 ;; Make an image out of the description if that is so wanted
210 (when (and descp (org-file-image-p
211 desc (org-lparse-get 'INLINE-IMAGE-EXTENSIONS)))
212 (setq org-lparse-link-description-is-image t)
213 (save-match-data
214 (if (string-match "^file:" desc)
215 (setq desc (substring desc (match-end 0)))))
216 (save-match-data
217 (setq desc (org-add-props
218 (org-lparse-format 'INLINE-IMAGE desc)
219 '(org-protected t)))))
220 (cond
221 ((equal type "internal")
222 (let
223 ((frag-0
224 (if (= (string-to-char path) ?#)
225 (substring path 1)
226 path)))
227 (setq rpl
228 (org-lparse-format
229 'ORG-LINK opt-plist "" "" (org-solidify-link-text
230 (save-match-data
231 (org-link-unescape frag-0))
232 nil) desc attr descp))))
233 ((and (equal type "id")
234 (setq id-file (org-id-find-id-file path)))
235 ;; This is an id: link to another file (if it was the same file,
236 ;; it would have become an internal link...)
237 (save-match-data
238 (setq id-file (file-relative-name
239 id-file
240 (file-name-directory org-current-export-file)))
241 (setq rpl
242 (org-lparse-format
243 'ORG-LINK opt-plist type id-file
244 (concat (if (org-uuidgen-p path) "ID-") path)
245 desc attr descp))))
246 ((member type '("http" "https"))
247 ;; standard URL, can inline as image
248 (setq rpl
249 (org-lparse-format
250 'ORG-LINK opt-plist type path nil desc attr descp)))
251 ((member type '("ftp" "mailto" "news"))
252 ;; standard URL, can't inline as image
253 (setq rpl
254 (org-lparse-format
255 'ORG-LINK opt-plist type path nil desc attr descp)))
257 ((string= type "coderef")
258 (setq rpl
259 (org-lparse-format
260 'ORG-LINK opt-plist type "" (format "coderef-%s" path)
261 (format
262 (org-export-get-coderef-format
263 path
264 (and descp desc))
265 (cdr (assoc path org-export-code-refs))) nil descp)))
267 ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
268 ;; The link protocol has a function for format the link
269 (setq rpl
270 (save-match-data
271 (funcall fnc (org-link-unescape path) desc1 'html))))
273 ((string= type "file")
274 ;; FILE link
275 (save-match-data
276 (let*
277 ((components
279 (string-match "::\\(.*\\)" path)
280 (list
281 (replace-match "" t nil path)
282 (match-string 1 path))
283 (list path nil)))
285 ;;The proper path, without a fragment
286 (path-1
287 (first components))
289 ;;The raw fragment
290 (fragment-0
291 (second components))
293 ;;Check the fragment. If it can't be used as
294 ;;target fragment we'll pass nil instead.
295 (fragment-1
297 (and fragment-0
298 (not (string-match "^[0-9]*$" fragment-0))
299 (not (string-match "^\\*" fragment-0))
300 (not (string-match "^/.*/$" fragment-0)))
301 (org-solidify-link-text
302 (org-link-unescape fragment-0))
303 nil))
304 (desc-2
305 ;;Description minus "file:" and ".org"
306 (if (string-match "^file:" desc)
307 (let
308 ((desc-1 (replace-match "" t t desc)))
309 (if (string-match "\\.org$" desc-1)
310 (replace-match "" t t desc-1)
311 desc-1))
312 desc)))
314 (setq rpl
316 (and
317 (functionp link-validate)
318 (not (funcall link-validate path-1 current-dir)))
319 desc
320 (org-lparse-format
321 'ORG-LINK opt-plist "file" path-1 fragment-1
322 desc-2 attr descp))))))
325 ;; just publish the path, as default
326 (setq rpl (concat "<i>&lt;" type ":"
327 (save-match-data (org-link-unescape path))
328 "&gt;</i>"))))
329 (setq line (replace-match rpl t t line)
330 start (+ start (length rpl))))
331 line))
333 (defmacro with-org-lparse-preserve-paragraph-state (&rest body)
334 `(let ((org-lparse-do-open-par org-lparse-par-open))
335 (org-lparse-end-paragraph)
336 ,@body
337 (when org-lparse-do-open-par
338 (org-lparse-begin-paragraph))))
340 (defvar org-lparse-native-backends nil
341 "List of native backends registered with `org-lparse'.
342 A backend can use `org-lparse-register-backend' to add itself to
343 this list.
345 All native backends must implement a get routine and a mandatory
346 set of callback routines.
348 The get routine must be named as org-<backend>-get where backend
349 is the name of the backend. The exporter uses `org-lparse-get'
350 and retrieves the backend-specific callback by querying for
351 ENTITY-CONTROL and ENTITY-FORMAT variables.
353 For the sake of illustration, the html backend implements
354 `org-xhtml-get'. It returns
355 `org-xhtml-entity-control-callbacks-alist' and
356 `org-xhtml-entity-format-callbacks-alist' as the values of
357 ENTITY-CONTROL and ENTITY-FORMAT settings.")
359 (defun org-lparse-register-backend (backend)
360 "Make BACKEND known to org-lparse library.
361 Add BACKEND to `org-lparse-native-backends'."
362 (when backend
363 (setq backend (cond
364 ((symbolp backend) (symbol-name backend))
365 ((stringp backend) backend)
366 (t (error "Error while registering backend: %S" backend))))
367 (add-to-list 'org-lparse-native-backends backend)))
369 (defun org-lparse-get-other-backends (native-backend)
370 (org-lparse-backend-get native-backend 'OTHER-BACKENDS))
372 (defun org-lparse-all-backends ()
373 (let (all-backends)
374 (flet ((add (other native)
375 (let ((val (assoc-string other all-backends t)))
376 (if val (setcdr val (nconc (list native) (cdr val)))
377 (push (cons other (list native)) all-backends)))))
378 (loop for backend in org-lparse-native-backends
379 do (loop for other in (org-lparse-get-other-backends backend)
380 do (add other backend))))
381 all-backends))
383 (defun org-lparse-backend-is-native-p (backend)
384 (member backend org-lparse-native-backends))
386 (defun org-lparse (target-backend native-backend arg
387 &optional hidden ext-plist
388 to-buffer body-only pub-dir)
389 "Export the outline to various formats.
390 If there is an active region, export only the region. The outline
391 is first exported to NATIVE-BACKEND and optionally converted to
392 TARGET-BACKEND. See `org-lparse-native-backends' for list of
393 known native backends. Each native backend can specify a
394 converter and list of target backends it exports to using the
395 CONVERT-PROCESS and OTHER-BACKENDS settings of it's get
396 method. See `org-xhtml-get' for an illustrative example.
398 ARG is a prefix argument that specifies how many levels of
399 outline should become headlines. The default is 3. Lower levels
400 will become bulleted lists.
402 HIDDEN is obsolete and does nothing.
404 EXT-PLIST is a property list that controls various aspects of
405 export. The settings here override org-mode's default settings
406 and but are inferior to file-local settings.
408 TO-BUFFER dumps the exported lines to a buffer or a string
409 instead of a file. If TO-BUFFER is the symbol `string' return the
410 exported lines as a string. If TO-BUFFER is non-nil, create a
411 buffer with that name and export to that buffer.
413 BODY-ONLY controls the presence of header and footer lines in
414 exported text. If BODY-ONLY is non-nil, don't produce the file
415 header and footer, simply return the content of <body>...</body>,
416 without even the body tags themselves.
418 PUB-DIR specifies the publishing directory."
419 (interactive
420 (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
421 (all-backends (org-lparse-all-backends))
422 (target-backend
423 (funcall input "Export to: " all-backends nil t nil))
424 (native-backend
426 ;; (and (org-lparse-backend-is-native-p target-backend)
427 ;; target-backend)
428 (funcall input "Use Native backend: "
429 (cdr (assoc target-backend all-backends)) nil t nil))))
430 (list target-backend native-backend current-prefix-arg)))
431 (let* ((org-lparse-backend (intern native-backend))
432 (org-lparse-other-backend (intern target-backend)))
433 (unless (org-lparse-backend-is-native-p native-backend)
434 (error "Don't know how to export natively to backend %s" native-backend))
435 (unless (or (not target-backend)
436 (equal target-backend native-backend)
437 (member target-backend (org-lparse-get 'OTHER-BACKENDS)))
438 (error "Don't know how to export to backend %s %s" target-backend
439 (format "via %s" native-backend)))
440 (run-hooks 'org-export-first-hook)
441 (org-do-lparse arg hidden ext-plist to-buffer body-only pub-dir)))
443 (defcustom org-lparse-convert-process
444 '("soffice" "-norestore" "-invisible" "-headless" "\"macro:///BasicODConverter.Main.Convert(%I,%f,%O)\"")
445 "Command to covert a Org exported format to other formats.
446 The variable is an list of the form (PROCESS ARG1 ARG2 ARG3
447 ...). Format specifiers used in the ARGs are replaced as below.
448 %i input file name in full
449 %I input file name as a URL
450 %f format of the output file
451 %o output file name in full
452 %O output file name as a URL
453 %d output dir in full
454 %D output dir as a URL"
455 :group 'org-lparse)
457 (defcustom org-lparse-use-flashy-warning t
458 "Use flashy warnings when exporting to ODT."
459 :type 'boolean
460 :group 'org-export)
462 (defun org-lparse-convert (&optional in-file fmt)
463 "Convert file from one format to another using a converter.
464 IN-FILE is the file to be converted. If unspecified, it defaults
465 to variable `buffer-file-name'. FMT is the desired output format. If the
466 backend has registered a CONVERT-METHOD via it's get function
467 then that converter is used. Otherwise
468 `org-export-conver-process' is used."
469 (interactive
470 (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
471 (in-file (read-file-name "File to be converted: "
472 nil buffer-file-name t))
473 (fmt (funcall input "Output format: "
474 (or (ignore-errors
475 (org-lparse-get-other-backends
476 (file-name-extension in-file)))
477 (org-lparse-all-backends))
478 nil nil nil)))
479 (list in-file fmt)))
480 (require 'browse-url)
481 (let* ((in-file (expand-file-name (or in-file buffer-file-name)))
482 (fmt (or fmt "doc") )
483 (out-file (concat (file-name-sans-extension in-file) "." fmt))
484 (out-dir (file-name-directory in-file))
485 (backend (when (boundp 'org-lparse-backend) org-lparse-backend))
486 (convert-process
487 (or (ignore-errors (org-lparse-backend-get backend 'CONVERT-METHOD))
488 org-lparse-convert-process))
489 program arglist)
491 (setq program (and convert-process (consp convert-process)
492 (car convert-process)))
493 (unless (executable-find program)
494 (error "Unable to locate the converter %s" program))
496 (setq arglist
497 (mapcar (lambda (arg)
498 (format-spec arg `((?i . ,in-file)
499 (?I . ,(browse-url-file-url in-file))
500 (?f . ,fmt)
501 (?o . ,out-file)
502 (?O . ,(browse-url-file-url out-file))
503 (?d . ,out-dir)
504 (?D . ,(browse-url-file-url out-dir)))))
505 (cdr convert-process)))
506 (ignore-errors (delete-file out-file))
508 (message "Executing %s %s" program (mapconcat 'identity arglist " "))
509 (apply 'call-process program nil nil nil arglist)
511 (cond
512 ((file-exists-p out-file)
513 (message "Exported to %s using %s" out-file program)
514 out-file
515 ;; (set-buffer (find-file-noselect out-file))
518 (message "Export to %s failed" out-file)
519 nil))))
521 (defvar org-lparse-insert-tag-with-newlines 'both)
523 ;; Following variables are let-bound during `org-lparse'
524 (defvar org-lparse-dyn-first-heading-pos)
525 (defvar org-lparse-toc)
526 (defvar org-lparse-entity-control-callbacks-alist)
527 (defvar org-lparse-entity-format-callbacks-alist)
528 (defvar org-lparse-backend nil
529 "The native backend to which the document is currently exported.
530 This variable is let bound during `org-lparse'. Valid values are
531 one of the symbols corresponding to `org-lparse-native-backends'.
533 Compare this variable with `org-export-current-backend' which is
534 bound only during `org-export-preprocess-string' stage of the
535 export process.
537 See also `org-lparse-other-backend'.")
539 (defvar org-lparse-other-backend nil
540 "The target backend to which the document is currently exported.
541 This variable is let bound during `org-lparse'. This variable is
542 set to either `org-lparse-backend' or one of the symbols
543 corresponding to OTHER-BACKENDS specification of the
544 org-lparse-backend.
546 For example, if a document is exported to \"odt\" then both
547 org-lparse-backend and org-lparse-other-backend are bound to
548 'odt. On the other hand, if a document is exported to \"odt\"
549 and then converted to \"doc\" then org-lparse-backend is set to
550 'odt and org-lparse-other-backend is set to 'doc.")
552 (defvar org-lparse-body-only nil
553 "Bind this to BODY-ONLY arg of `org-lparse'.")
555 (defvar org-lparse-to-buffer nil
556 "Bind this to TO-BUFFER arg of `org-lparse'.")
558 (defun org-do-lparse (arg &optional hidden ext-plist
559 to-buffer body-only pub-dir)
560 "Export the outline to various formats.
561 See `org-lparse' for more information. This function is a
562 html-agnostic version of the `org-export-as-html' function in 7.5
563 version."
564 ;; Make sure we have a file name when we need it.
565 (when (and (not (or to-buffer body-only))
566 (not buffer-file-name))
567 (if (buffer-base-buffer)
568 (org-set-local 'buffer-file-name
569 (with-current-buffer (buffer-base-buffer)
570 buffer-file-name))
571 (error "Need a file name to be able to export")))
573 (org-lparse-warn
574 (format "Exporting to %s using org-lparse..."
575 (upcase (symbol-name
576 (or org-lparse-backend org-lparse-other-backend)))))
578 (setq-default org-todo-line-regexp org-todo-line-regexp)
579 (setq-default org-deadline-line-regexp org-deadline-line-regexp)
580 (setq-default org-done-keywords org-done-keywords)
581 (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
582 (let* (org-lparse-encode-pending
583 org-lparse-par-open
584 org-lparse-outline-text-open
585 (org-lparse-latex-fragment-fallback ; currently used only by
586 ; odt exporter
587 (or (ignore-errors (org-lparse-get 'LATEX-FRAGMENT-FALLBACK))
588 (if (and (org-check-external-command "latex" "" t)
589 (org-check-external-command "dvipng" "" t))
590 'dvipng
591 'verbatim)))
592 (org-lparse-insert-tag-with-newlines 'both)
593 (org-lparse-to-buffer to-buffer)
594 (org-lparse-body-only body-only)
595 (org-lparse-entity-control-callbacks-alist
596 (org-lparse-get 'ENTITY-CONTROL))
597 (org-lparse-entity-format-callbacks-alist
598 (org-lparse-get 'ENTITY-FORMAT))
599 (opt-plist
600 (org-export-process-option-filters
601 (org-combine-plists (org-default-export-plist)
602 ext-plist
603 (org-infile-export-plist))))
604 (body-only (or body-only (plist-get opt-plist :body-only)))
605 valid org-lparse-dyn-first-heading-pos
606 (odd org-odd-levels-only)
607 (region-p (org-region-active-p))
608 (rbeg (and region-p (region-beginning)))
609 (rend (and region-p (region-end)))
610 (subtree-p
611 (if (plist-get opt-plist :ignore-subtree-p)
613 (when region-p
614 (save-excursion
615 (goto-char rbeg)
616 (and (org-at-heading-p)
617 (>= (org-end-of-subtree t t) rend))))))
618 (level-offset (if subtree-p
619 (save-excursion
620 (goto-char rbeg)
621 (+ (funcall outline-level)
622 (if org-odd-levels-only 1 0)))
624 (opt-plist (setq org-export-opt-plist
625 (if subtree-p
626 (org-export-add-subtree-options opt-plist rbeg)
627 opt-plist)))
628 ;; The following two are dynamically scoped into other
629 ;; routines below.
630 (org-current-export-dir
631 (or pub-dir (org-lparse-get 'EXPORT-DIR opt-plist)))
632 (org-current-export-file buffer-file-name)
633 (level 0) (line "") (origline "") txt todo
634 (umax nil)
635 (umax-toc nil)
636 (filename (if to-buffer nil
637 (expand-file-name
638 (concat
639 (file-name-sans-extension
640 (or (and subtree-p
641 (org-entry-get (region-beginning)
642 "EXPORT_FILE_NAME" t))
643 (file-name-nondirectory buffer-file-name)))
644 "." (org-lparse-get 'FILE-NAME-EXTENSION opt-plist))
645 (file-name-as-directory
646 (or pub-dir (org-lparse-get 'EXPORT-DIR opt-plist))))))
647 (current-dir (if buffer-file-name
648 (file-name-directory buffer-file-name)
649 default-directory))
650 (buffer (if to-buffer
651 (cond
652 ((eq to-buffer 'string)
653 (get-buffer-create (org-lparse-get 'EXPORT-BUFFER-NAME)))
654 (t (get-buffer-create to-buffer)))
655 (find-file-noselect
656 (or (let ((f (org-lparse-get 'INIT-METHOD)))
657 (and f (functionp f) (funcall f filename)))
658 filename))))
659 (org-levels-open (make-vector org-level-max nil))
660 (date (plist-get opt-plist :date))
661 (date (cond
662 ((and date (string-match "%" date))
663 (format-time-string date))
664 (date date)
665 (t (format-time-string "%Y-%m-%d %T %Z"))))
666 (dummy (setq opt-plist (plist-put opt-plist :effective-date date)))
667 (title (org-xml-encode-org-text-skip-links
668 (or (and subtree-p (org-export-get-title-from-subtree))
669 (plist-get opt-plist :title)
670 (and (not body-only)
671 (not
672 (plist-get opt-plist :skip-before-1st-heading))
673 (org-export-grab-title-from-buffer))
674 (and buffer-file-name
675 (file-name-sans-extension
676 (file-name-nondirectory buffer-file-name)))
677 "UNTITLED")))
678 (dummy (setq opt-plist (plist-put opt-plist :title title)))
679 (html-table-tag (plist-get opt-plist :html-table-tag))
680 (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
681 (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
682 (org-lparse-dyn-current-environment nil)
683 ;; Get the language-dependent settings
684 (lang-words (or (assoc (plist-get opt-plist :language)
685 org-export-language-setup)
686 (assoc "en" org-export-language-setup)))
687 (dummy (setq opt-plist (plist-put opt-plist :lang-words lang-words)))
688 (head-count 0) cnt
689 (start 0)
690 (coding-system-for-write
691 (or (ignore-errors (org-lparse-get 'CODING-SYSTEM-FOR-WRITE))
692 (and (boundp 'buffer-file-coding-system)
693 buffer-file-coding-system)))
694 (save-buffer-coding-system
695 (or (ignore-errors (org-lparse-get 'CODING-SYSTEM-FOR-SAVE))
696 (and (boundp 'buffer-file-coding-system)
697 buffer-file-coding-system)))
698 (region
699 (buffer-substring
700 (if region-p (region-beginning) (point-min))
701 (if region-p (region-end) (point-max))))
702 (org-export-have-math nil)
703 (org-export-footnotes-seen nil)
704 (org-export-footnotes-data (org-footnote-all-labels 'with-defs))
705 (org-footnote-insert-pos-for-preprocessor 'point-min)
706 (lines
707 (org-split-string
708 (org-export-preprocess-string
709 region
710 :emph-multiline t
711 :for-backend (if (equal org-lparse-backend 'xhtml) ; hack
712 'html
713 org-lparse-backend)
714 :skip-before-1st-heading
715 (plist-get opt-plist :skip-before-1st-heading)
716 :drawers (plist-get opt-plist :drawers)
717 :todo-keywords (plist-get opt-plist :todo-keywords)
718 :tasks (plist-get opt-plist :tasks)
719 :tags (plist-get opt-plist :tags)
720 :priority (plist-get opt-plist :priority)
721 :footnotes (plist-get opt-plist :footnotes)
722 :timestamps (plist-get opt-plist :timestamps)
723 :archived-trees
724 (plist-get opt-plist :archived-trees)
725 :select-tags (plist-get opt-plist :select-tags)
726 :exclude-tags (plist-get opt-plist :exclude-tags)
727 :add-text
728 (plist-get opt-plist :text)
729 :LaTeX-fragments
730 (plist-get opt-plist :LaTeX-fragments))
731 "[\r\n]"))
732 table-open
733 table-buffer table-orig-buffer
735 rpl path attr desc descp desc1 desc2 link
736 snumber fnc
737 footnotes footref-seen
738 org-lparse-output-buffer
739 org-lparse-footnote-definitions
740 org-lparse-footnote-number
741 org-lparse-footnote-buffer
742 org-lparse-toc
743 href
746 (let ((inhibit-read-only t))
747 (org-unmodified
748 (remove-text-properties (point-min) (point-max)
749 '(:org-license-to-kill t))))
751 (message "Exporting...")
752 (org-init-section-numbers)
754 ;; Switch to the output buffer
755 (setq org-lparse-output-buffer buffer)
756 (set-buffer org-lparse-output-buffer)
757 (let ((inhibit-read-only t)) (erase-buffer))
758 (fundamental-mode)
759 (org-install-letbind)
761 (and (fboundp 'set-buffer-file-coding-system)
762 (set-buffer-file-coding-system coding-system-for-write))
764 (let ((case-fold-search nil)
765 (org-odd-levels-only odd))
766 ;; create local variables for all options, to make sure all called
767 ;; functions get the correct information
768 (mapc (lambda (x)
769 (set (make-local-variable (nth 2 x))
770 (plist-get opt-plist (car x))))
771 org-export-plist-vars)
772 (setq umax (if arg (prefix-numeric-value arg)
773 org-export-headline-levels))
774 (setq umax-toc (if (integerp org-export-with-toc)
775 (min org-export-with-toc umax)
776 umax))
778 (when (and org-export-with-toc (not body-only))
779 (setq lines (org-lparse-prepare-toc
780 lines level-offset opt-plist umax-toc)))
782 (unless body-only
783 (org-lparse-begin 'DOCUMENT-CONTENT opt-plist)
784 (org-lparse-begin 'DOCUMENT-BODY opt-plist))
786 (setq head-count 0)
787 (org-init-section-numbers)
789 (org-lparse-begin-paragraph)
791 (while (setq line (pop lines) origline line)
792 (catch 'nextline
793 (when (and (org-lparse-current-environment-p 'quote)
794 (string-match "^\\*+ " line))
795 (org-lparse-end-environment 'quote))
797 (when (org-lparse-current-environment-p 'quote)
798 (org-lparse-insert 'LINE line)
799 (throw 'nextline nil))
801 ;; Fixed-width, verbatim lines (examples)
802 (when (and org-export-with-fixed-width
803 (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line))
804 (when (not (org-lparse-current-environment-p 'fixedwidth))
805 (org-lparse-begin-environment 'fixedwidth))
806 (org-lparse-insert 'LINE (match-string 3 line))
807 (when (or (not lines)
808 (not (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)"
809 (car lines))))
810 (org-lparse-end-environment 'fixedwidth))
811 (throw 'nextline nil))
813 ;; Notes: The baseline version of org-html.el (git commit
814 ;; 3d802e), while encoutering a *line-long* protected text,
815 ;; does one of the following two things based on the state
816 ;; of the export buffer.
818 ;; 1. If a paragraph element has just been opened and
819 ;; contains only whitespace as content, insert the
820 ;; protected text as part of the previous paragraph.
822 ;; 2. If the paragraph element has already been opened and
823 ;; contains some valid content insert the protected text
824 ;; as part of the current paragraph.
826 ;; I think --->
828 ;; Scenario 1 mentioned above kicks in when a block of
829 ;; protected text has to be inserted enbloc. For example,
830 ;; this happens, when inserting an source or example block
831 ;; or preformatted content enclosed in #+backend,
832 ;; #+begin_bakend ... #+end_backend)
834 ;; Scenario 2 mentioned above kicks in when the protected
835 ;; text is part of a running sentence. For example this
836 ;; happens in the case of an *multiline* LaTeX equation that
837 ;; needs to be inserted verbatim.
839 ;; org-html.el in the master branch seems to do some
840 ;; jugglery by moving paragraphs around. Inorder to make
841 ;; these changes backend-agnostic introduce a new text
842 ;; property org-native-text and impose the added semantics
843 ;; that these protected blocks appear outside of a
844 ;; conventional paragraph element.
846 ;; Extra Note: Check whether org-example and org-native-text
847 ;; are entirely equivalent.
849 ;; Fixes bug reported by Christian Moe concerning verbatim
850 ;; LaTeX fragments.
851 ;; on git commit 533ba3f90250a1f25f494c390d639ea6274f235c
852 ;; http://repo.or.cz/w/org-mode/org-jambu.git/shortlog/refs/heads/staging
853 ;; See http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01379.html
855 ;; Native Text
856 (when (and (get-text-property 0 'org-native-text line)
857 ;; Make sure it is the entire line that is protected
858 (not (< (or (next-single-property-change
859 0 'org-native-text line) 10000)
860 (length line))))
861 (let ((ind (get-text-property 0 'original-indentation line)))
862 (org-lparse-begin-environment 'native)
863 (org-lparse-insert 'LINE line)
864 (while (and lines
865 (or (= (length (car lines)) 0)
866 (not ind)
867 (equal ind (get-text-property
868 0 'original-indentation (car lines))))
869 (or (= (length (car lines)) 0)
870 (get-text-property 0 'org-native-text (car lines))))
871 (org-lparse-insert 'LINE (pop lines)))
872 (org-lparse-end-environment 'native))
873 (throw 'nextline nil))
875 ;; Protected HTML
876 (when (and (get-text-property 0 'org-protected line)
877 ;; Make sure it is the entire line that is protected
878 (not (< (or (next-single-property-change
879 0 'org-protected line) 10000)
880 (length line))))
881 (let ((ind (get-text-property 0 'original-indentation line)))
882 (org-lparse-insert 'LINE line)
883 (while (and lines
884 (or (= (length (car lines)) 0)
885 (not ind)
886 (equal ind (get-text-property
887 0 'original-indentation (car lines))))
888 (or (= (length (car lines)) 0)
889 (get-text-property 0 'org-protected (car lines))))
890 (org-lparse-insert 'LINE (pop lines))))
891 (throw 'nextline nil))
893 ;; Blockquotes, verse, and center
894 (when (string-match "^ORG-\\(.+\\)-\\(START\\|END\\)$" line)
895 (let* ((style (intern (downcase (match-string 1 line))))
896 (f (cdr (assoc (match-string 2 line)
897 '(("START" . org-lparse-begin-environment)
898 ("END" . org-lparse-end-environment))))))
899 (when (memq style '(blockquote verse center))
900 (funcall f style)
901 (throw 'nextline nil))))
903 (run-hooks 'org-export-html-after-blockquotes-hook)
904 (when (org-lparse-current-environment-p 'verse)
905 (let ((i (org-get-string-indentation line)))
906 (if (> i 0)
907 (setq line (concat
908 (let ((org-lparse-encode-pending t))
909 (org-lparse-format 'SPACES (* 2 i)))
910 " " (org-trim line))))
911 (unless (string-match "\\\\\\\\[ \t]*$" line)
912 (setq line (concat line "\\\\")))))
914 ;; make targets to anchors
915 (setq start 0)
916 (while (string-match
917 "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line start)
918 (cond
919 ((get-text-property (match-beginning 1) 'org-protected line)
920 (setq start (match-end 1)))
921 ((match-end 2)
922 (setq line (replace-match
923 (let ((org-lparse-encode-pending t))
924 (org-lparse-format
925 'ANCHOR "" (org-solidify-link-text
926 (match-string 1 line))))
927 t t line)))
928 ((and org-export-with-toc (equal (string-to-char line) ?*))
929 ;; FIXME: NOT DEPENDENT on TOC?????????????????????
930 (setq line (replace-match
931 (let ((org-lparse-encode-pending t))
932 (org-lparse-format
933 'FONTIFY (match-string 1 line) "target"))
934 ;; (concat "@<i>" (match-string 1 line) "@</i> ")
935 t t line)))
937 (setq line (replace-match
938 (concat
939 (let ((org-lparse-encode-pending t))
940 (org-lparse-format
941 'ANCHOR (match-string 1 line)
942 (org-solidify-link-text (match-string 1 line))
943 "target")) " ")
944 t t line)))))
946 (let ((org-lparse-encode-pending t))
947 (setq line (org-lparse-handle-time-stamps line)))
949 ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
950 ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
951 ;; Also handle sub_superscripts and checkboxes
952 (or (string-match org-table-hline-regexp line)
953 (string-match "^[ \t]*\\([+]-\\||[ ]\\)[-+ |]*[+|][ \t]*$" line)
954 (setq line (org-xml-encode-org-text-skip-links line)))
956 (setq line (org-lparse-format-org-link line opt-plist))
958 ;; TODO items
959 (if (and (string-match org-todo-line-regexp line)
960 (match-beginning 2))
961 (setq line (concat
962 (substring line 0 (match-beginning 2))
963 (org-lparse-format 'TODO (match-string 2 line))
964 (substring line (match-end 2)))))
966 ;; Does this contain a reference to a footnote?
967 (when org-export-with-footnotes
968 (setq start 0)
969 (while (string-match "\\([^* \t].*?\\)[ \t]*\\[\\([0-9]+\\)\\]" line start)
970 ;; Discard protected matches not clearly identified as
971 ;; footnote markers.
972 (if (or (get-text-property (match-beginning 2) 'org-protected line)
973 (not (get-text-property (match-beginning 2) 'org-footnote line)))
974 (setq start (match-end 2))
975 (let ((n (match-string 2 line)) refcnt a)
976 (if (setq a (assoc n footref-seen))
977 (progn
978 (setcdr a (1+ (cdr a)))
979 (setq refcnt (cdr a)))
980 (setq refcnt 1)
981 (push (cons n 1) footref-seen))
982 (setq line
983 (replace-match
984 (concat
985 (or (match-string 1 line) "")
986 (org-lparse-format
987 'FOOTNOTE-REFERENCE
988 n (cdr (assoc n org-lparse-footnote-definitions))
989 refcnt)
990 ;; If another footnote is following the
991 ;; current one, add a separator.
992 (if (save-match-data
993 (string-match "\\`\\[[0-9]+\\]"
994 (substring line (match-end 0))))
995 (ignore-errors
996 (org-lparse-get 'FOOTNOTE-SEPARATOR))
997 ""))
998 t t line))))))
1000 (cond
1001 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
1002 ;; This is a headline
1003 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
1004 level-offset))
1005 txt (match-string 2 line))
1006 (if (string-match quote-re0 txt)
1007 (setq txt (replace-match "" t t txt)))
1008 (if (<= level (max umax umax-toc))
1009 (setq head-count (+ head-count 1)))
1010 (unless org-lparse-dyn-first-heading-pos
1011 (setq org-lparse-dyn-first-heading-pos (point)))
1012 (org-lparse-begin-level level txt umax head-count)
1014 ;; QUOTES
1015 (when (string-match quote-re line)
1016 (org-lparse-begin-environment 'quote)))
1018 ((and org-export-with-tables
1019 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
1020 (when (not table-open)
1021 ;; New table starts
1022 (setq table-open t table-buffer nil table-orig-buffer nil))
1024 ;; Accumulate lines
1025 (setq table-buffer (cons line table-buffer)
1026 table-orig-buffer (cons origline table-orig-buffer))
1027 (when (or (not lines)
1028 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
1029 (car lines))))
1030 (setq table-open nil
1031 table-buffer (nreverse table-buffer)
1032 table-orig-buffer (nreverse table-orig-buffer))
1033 (org-lparse-end-paragraph)
1034 (org-lparse-insert 'TABLE table-buffer table-orig-buffer)))
1036 ;; Normal lines
1039 ;; This line either is list item or end a list.
1040 (when (get-text-property 0 'list-item line)
1041 (setq line (org-lparse-export-list-line
1042 line
1043 (get-text-property 0 'list-item line)
1044 (get-text-property 0 'list-struct line)
1045 (get-text-property 0 'list-prevs line))))
1047 ;; Horizontal line
1048 (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
1049 (with-org-lparse-preserve-paragraph-state
1050 (org-lparse-insert 'HORIZONTAL-LINE))
1051 (throw 'nextline nil))
1053 ;; Empty lines start a new paragraph. If hand-formatted lists
1054 ;; are not fully interpreted, lines starting with "-", "+", "*"
1055 ;; also start a new paragraph.
1056 (when (string-match "^ [-+*]-\\|^[ \t]*$" line)
1057 (when org-lparse-footnote-number
1058 (org-lparse-end-footnote-definition org-lparse-footnote-number)
1059 (setq org-lparse-footnote-number nil))
1060 (org-lparse-begin-paragraph))
1062 ;; Is this the start of a footnote?
1063 (when org-export-with-footnotes
1064 (when (and (boundp 'footnote-section-tag-regexp)
1065 (string-match (concat "^" footnote-section-tag-regexp)
1066 line))
1067 ;; ignore this line
1068 (throw 'nextline nil))
1069 (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
1070 (org-lparse-end-paragraph)
1071 (setq org-lparse-footnote-number (match-string 1 line))
1072 (setq line (replace-match "" t t line))
1073 (org-lparse-begin-footnote-definition org-lparse-footnote-number)))
1074 ;; Check if the line break needs to be conserved
1075 (cond
1076 ((string-match "\\\\\\\\[ \t]*$" line)
1077 (setq line (replace-match
1078 (org-lparse-format 'LINE-BREAK)
1079 t t line)))
1080 (org-export-preserve-breaks
1081 (setq line (concat line (org-lparse-format 'LINE-BREAK)))))
1083 ;; Check if a paragraph should be started
1084 (let ((start 0))
1085 (while (and org-lparse-par-open
1086 (string-match "\\\\par\\>" line start))
1087 (error "FIXME")
1088 ;; Leave a space in the </p> so that the footnote matcher
1089 ;; does not see this.
1090 (if (not (get-text-property (match-beginning 0)
1091 'org-protected line))
1092 (setq line (replace-match "</p ><p >" t t line)))
1093 (setq start (match-end 0))))
1095 (org-lparse-insert 'LINE line)))))
1097 ;; Properly close all local lists and other lists
1098 (when (org-lparse-current-environment-p 'quote)
1099 (org-lparse-end-environment 'quote))
1101 (org-lparse-end-level 1 umax)
1103 ;; the </div> to close the last text-... div.
1104 (when (and (> umax 0) org-lparse-dyn-first-heading-pos)
1105 (org-lparse-end-outline-text-or-outline))
1107 (org-lparse-end 'DOCUMENT-BODY opt-plist)
1108 (unless body-only
1109 (org-lparse-end 'DOCUMENT-CONTENT))
1111 (unless (plist-get opt-plist :buffer-will-be-killed)
1112 (set-auto-mode t))
1114 (org-lparse-end 'EXPORT)
1116 (goto-char (point-min))
1117 (or (org-export-push-to-kill-ring
1118 (upcase (symbol-name org-lparse-backend)))
1119 (message "Exporting... done"))
1121 (cond
1122 ((not to-buffer)
1123 (let ((f (org-lparse-get 'SAVE-METHOD)))
1124 (or (and f (functionp f) (funcall f filename opt-plist))
1125 (save-buffer)))
1126 (or (when (and (boundp 'org-lparse-other-backend)
1127 org-lparse-other-backend
1128 (not (equal org-lparse-backend org-lparse-other-backend)))
1129 (let ((org-lparse-convert-process (org-lparse-get 'CONVERT-METHOD)))
1130 (when org-lparse-convert-process
1131 (org-lparse-convert buffer-file-name
1132 (symbol-name org-lparse-other-backend)))))
1133 (current-buffer)))
1134 ((eq to-buffer 'string)
1135 (prog1 (buffer-substring (point-min) (point-max))
1136 (kill-buffer (current-buffer))))
1137 (t (current-buffer))))))
1139 (defun org-lparse-format-table (lines olines)
1140 "Retuns backend-specific code for org-type and table-type
1141 tables."
1142 (if (stringp lines)
1143 (setq lines (org-split-string lines "\n")))
1144 (if (string-match "^[ \t]*|" (car lines))
1145 ;; A normal org table
1146 (org-lparse-format-org-table lines nil)
1147 ;; Table made by table.el
1148 (or (org-lparse-format-table-table-using-table-generate-source
1149 org-lparse-backend olines
1150 (not org-export-prefer-native-exporter-for-tables))
1151 ;; We are here only when table.el table has NO col or row
1152 ;; spanning and the user prefers using org's own converter for
1153 ;; exporting of such simple table.el tables.
1154 (org-lparse-format-table-table lines))))
1156 (defun org-lparse-table-get-colalign-info (lines)
1157 (let ((forced-aligns (org-find-text-property-in-string
1158 'org-forced-aligns (car lines))))
1159 (when (and forced-aligns org-table-clean-did-remove-column)
1160 (setq forced-aligns
1161 (mapcar (lambda (x) (cons (1- (car x)) (cdr x))) forced-aligns)))
1163 forced-aligns))
1165 (defvar org-lparse-table-style)
1166 (defvar org-lparse-table-ncols)
1167 (defvar org-lparse-table-rownum)
1168 (defvar org-lparse-table-is-styled)
1169 (defvar org-lparse-table-begin-marker)
1170 (defvar org-lparse-table-num-numeric-items-per-column)
1171 (defvar org-lparse-table-colalign-info)
1172 (defvar org-lparse-table-colalign-vector)
1174 ;; Following variables are defined in org-table.el
1175 (defvar org-table-number-fraction)
1176 (defvar org-table-number-regexp)
1178 (defun org-lparse-do-format-org-table (lines &optional splice)
1179 "Format a org-type table into backend-specific code.
1180 LINES is a list of lines. Optional argument SPLICE means, do not
1181 insert header and surrounding <table> tags, just format the lines.
1182 Optional argument NO-CSS means use XHTML attributes instead of CSS
1183 for formatting. This is required for the DocBook exporter."
1184 (require 'org-table)
1185 ;; Get rid of hlines at beginning and end
1186 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
1187 (setq lines (nreverse lines))
1188 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
1189 (setq lines (nreverse lines))
1190 (when org-export-table-remove-special-lines
1191 ;; Check if the table has a marking column. If yes remove the
1192 ;; column and the special lines
1193 (setq lines (org-table-clean-before-export lines)))
1195 (let* ((caption (org-find-text-property-in-string 'org-caption (car lines)))
1196 (caption (and caption (org-xml-encode-org-text caption)))
1197 (label (org-find-text-property-in-string 'org-label (car lines)))
1198 (org-lparse-table-colalign-info (org-lparse-table-get-colalign-info lines))
1199 (attributes (org-find-text-property-in-string 'org-attributes
1200 (car lines)))
1201 (head (and org-export-highlight-first-table-line
1202 (delq nil (mapcar
1203 (lambda (x) (string-match "^[ \t]*|-" x))
1204 (cdr lines)))))
1205 (org-lparse-table-rownum -1) org-lparse-table-ncols i (cnt 0)
1206 tbopen line fields
1207 org-lparse-table-cur-rowgrp-is-hdr
1208 org-lparse-table-rowgrp-open
1209 org-lparse-table-num-numeric-items-per-column
1210 org-lparse-table-colalign-vector n
1211 org-lparse-table-rowgrp-info
1212 org-lparse-table-begin-marker
1213 (org-lparse-table-style 'org-table)
1214 org-lparse-table-is-styled)
1215 (cond
1216 (splice
1217 (setq org-lparse-table-is-styled nil)
1218 (while (setq line (pop lines))
1219 (unless (string-match "^[ \t]*|-" line)
1220 (insert
1221 (org-lparse-format-table-row
1222 (org-split-string line "[ \t]*|[ \t]*")) "\n"))))
1224 (setq org-lparse-table-is-styled t)
1225 (org-lparse-begin 'TABLE caption label attributes)
1226 (setq org-lparse-table-begin-marker (point))
1227 (org-lparse-begin-table-rowgroup head)
1228 (while (setq line (pop lines))
1229 (cond
1230 ((string-match "^[ \t]*|-" line)
1231 (when lines (org-lparse-begin-table-rowgroup)))
1233 (insert
1234 (org-lparse-format-table-row
1235 (org-split-string line "[ \t]*|[ \t]*")) "\n"))))
1236 (org-lparse-end 'TABLE-ROWGROUP)
1237 (org-lparse-end-table)))))
1239 (defun org-lparse-format-org-table (lines &optional splice)
1240 (with-temp-buffer
1241 (org-lparse-do-format-org-table lines splice)
1242 (buffer-substring-no-properties (point-min) (point-max))))
1244 (defun org-lparse-do-format-table-table (lines)
1245 "Format a table generated by table.el into backend-specific code.
1246 This conversion does *not* use `table-generate-source' from table.el.
1247 This has the advantage that Org-mode's HTML conversions can be used.
1248 But it has the disadvantage, that no cell- or row-spanning is allowed."
1249 (let (line field-buffer
1250 (org-lparse-table-cur-rowgrp-is-hdr
1251 org-export-highlight-first-table-line)
1252 (caption nil)
1253 (attributes nil)
1254 (label nil)
1255 (org-lparse-table-style 'table-table)
1256 (org-lparse-table-is-styled nil)
1257 fields org-lparse-table-ncols i (org-lparse-table-rownum -1)
1258 (empty (org-lparse-format 'SPACES 1)))
1259 (org-lparse-begin 'TABLE caption label attributes)
1260 (while (setq line (pop lines))
1261 (cond
1262 ((string-match "^[ \t]*\\+-" line)
1263 (when field-buffer
1264 (let ((org-export-table-row-tags '("<tr>" . "</tr>"))
1265 ;; (org-export-html-table-use-header-tags-for-first-column nil)
1267 (insert (org-lparse-format-table-row field-buffer empty)))
1268 (setq org-lparse-table-cur-rowgrp-is-hdr nil)
1269 (setq field-buffer nil)))
1271 ;; Break the line into fields and store the fields
1272 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
1273 (if field-buffer
1274 (setq field-buffer (mapcar
1275 (lambda (x)
1276 (concat x (org-lparse-format 'LINE-BREAK)
1277 (pop fields)))
1278 field-buffer))
1279 (setq field-buffer fields)))))
1280 (org-lparse-end-table)))
1282 (defun org-lparse-format-table-table (lines)
1283 (with-temp-buffer
1284 (org-lparse-do-format-table-table lines)
1285 (buffer-substring-no-properties (point-min) (point-max))))
1287 (defvar table-source-languages) ; defined in table.el
1288 (defun org-lparse-format-table-table-using-table-generate-source (backend
1289 lines
1290 &optional
1291 spanned-only)
1292 "Format a table into BACKEND, using `table-generate-source' from table.el.
1293 Use SPANNED-ONLY to suppress exporting of simple table.el tables.
1295 When SPANNED-ONLY is nil, all table.el tables are exported. When
1296 SPANNED-ONLY is non-nil, only tables with either row or column
1297 spans are exported.
1299 This routine returns the generated source or nil as appropriate.
1301 Refer docstring of `org-export-prefer-native-exporter-for-tables'
1302 for further information."
1303 (require 'table)
1304 (with-current-buffer (get-buffer-create " org-tmp1 ")
1305 (erase-buffer)
1306 (insert (mapconcat 'identity lines "\n"))
1307 (goto-char (point-min))
1308 (if (not (re-search-forward "|[^+]" nil t))
1309 (error "Error processing table"))
1310 (table-recognize-table)
1311 (when (or (not spanned-only)
1312 (let* ((dim (table-query-dimension))
1313 (c (nth 4 dim)) (r (nth 5 dim)) (cells (nth 6 dim)))
1314 (not (= (* c r) cells))))
1315 (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
1316 (cond
1317 ((member backend table-source-languages)
1318 (table-generate-source backend " org-tmp2 ")
1319 (set-buffer " org-tmp2 ")
1320 (buffer-substring (point-min) (point-max)))
1322 ;; table.el doesn't support the given backend. Currently this
1323 ;; happens in case of odt export. Strip the table from the
1324 ;; generated document. A better alternative would be to embed
1325 ;; the table as ascii text in the output document.
1326 (org-lparse-warn
1327 (concat
1328 "Found table.el-type table in the source org file. "
1329 (format "table.el doesn't support %s backend. "
1330 (upcase (symbol-name backend)))
1331 "Skipping ahead ..."))
1332 "")))))
1334 (defun org-lparse-handle-time-stamps (s)
1335 "Format time stamps in string S, or remove them."
1336 (catch 'exit
1337 (let (r b)
1338 (while (string-match org-maybe-keyword-time-regexp s)
1339 (or b (setq b (substring s 0 (match-beginning 0))))
1340 (setq r (concat
1341 r (substring s 0 (match-beginning 0)) " "
1342 (org-lparse-format
1343 'FONTIFY
1344 (concat
1345 (if (match-end 1)
1346 (org-lparse-format
1347 'FONTIFY
1348 (match-string 1 s) "timestamp-kwd"))
1350 (org-lparse-format
1351 'FONTIFY
1352 (substring (org-translate-time (match-string 3 s)) 1 -1)
1353 "timestamp"))
1354 "timestamp-wrapper"))
1355 s (substring s (match-end 0))))
1356 ;; Line break if line started and ended with time stamp stuff
1357 (if (not r)
1359 (setq r (concat r s))
1360 (unless (string-match "\\S-" (concat b s))
1361 (setq r (concat r (org-lparse-format 'LINE-BREAK))))
1362 r))))
1364 (defun org-xml-encode-plain-text (s)
1365 "Convert plain text characters to HTML equivalent.
1366 Possible conversions are set in `org-export-html-protect-char-alist'."
1367 (let ((cl (org-lparse-get 'PLAIN-TEXT-MAP)) c)
1368 (while (setq c (pop cl))
1369 (let ((start 0))
1370 (while (string-match (car c) s start)
1371 (setq s (replace-match (cdr c) t t s)
1372 start (1+ (match-beginning 0))))))
1375 (defun org-xml-encode-org-text-skip-links (string)
1376 "Prepare STRING for HTML export. Apply all active conversions.
1377 If there are links in the string, don't modify these."
1378 (let* ((re (concat org-bracket-link-regexp "\\|"
1379 (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")))
1380 m s l res)
1381 (while (setq m (string-match re string))
1382 (setq s (substring string 0 m)
1383 l (match-string 0 string)
1384 string (substring string (match-end 0)))
1385 (push (org-xml-encode-org-text s) res)
1386 (push l res))
1387 (push (org-xml-encode-org-text string) res)
1388 (apply 'concat (nreverse res))))
1390 (defun org-xml-encode-org-text (s)
1391 "Apply all active conversions to translate special ASCII to HTML."
1392 (setq s (org-xml-encode-plain-text s))
1393 (if org-export-html-expand
1394 (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
1395 (setq s (replace-match "<\\1>" t nil s))))
1396 (if org-export-with-emphasize
1397 (setq s (org-lparse-apply-char-styles s)))
1398 (if org-export-with-special-strings
1399 (setq s (org-lparse-convert-special-strings s)))
1400 (if org-export-with-sub-superscripts
1401 (setq s (org-lparse-apply-sub-superscript-styles s)))
1402 (if org-export-with-TeX-macros
1403 (let ((start 0) wd rep)
1404 (while (setq start (string-match "\\\\\\([a-zA-Z]+[0-9]*\\)\\({}\\)?"
1405 s start))
1406 (if (get-text-property (match-beginning 0) 'org-protected s)
1407 (setq start (match-end 0))
1408 (setq wd (match-string 1 s))
1409 (if (setq rep (org-lparse-format 'ORG-ENTITY wd))
1410 (setq s (replace-match rep t t s))
1411 (setq start (+ start (length wd))))))))
1414 (defun org-lparse-convert-special-strings (string)
1415 "Convert special characters in STRING to HTML."
1416 (let ((all (org-lparse-get 'SPECIAL-STRING-REGEXPS))
1417 e a re rpl start)
1418 (while (setq a (pop all))
1419 (setq re (car a) rpl (cdr a) start 0)
1420 (while (string-match re string start)
1421 (if (get-text-property (match-beginning 0) 'org-protected string)
1422 (setq start (match-end 0))
1423 (setq string (replace-match rpl t nil string)))))
1424 string))
1426 (defun org-lparse-apply-sub-superscript-styles (string)
1427 "Apply subscript and superscript styles to STRING.
1428 Use `org-export-with-sub-superscripts' to control application of
1429 sub and superscript styles."
1430 (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
1431 (while (string-match org-match-substring-regexp string s)
1432 (cond
1433 ((and requireb (match-end 8)) (setq s (match-end 2)))
1434 ((get-text-property (match-beginning 2) 'org-protected string)
1435 (setq s (match-end 2)))
1437 (setq s (match-end 1)
1438 key (if (string= (match-string 2 string) "_")
1439 'subscript 'superscript)
1440 c (or (match-string 8 string)
1441 (match-string 6 string)
1442 (match-string 5 string))
1443 string (replace-match
1444 (concat (match-string 1 string)
1445 (org-lparse-format 'FONTIFY c key))
1446 t t string)))))
1447 (while (string-match "\\\\\\([_^]\\)" string)
1448 (setq string (replace-match (match-string 1 string) t t string)))
1449 string))
1451 (defvar org-lparse-char-styles
1452 `(("*" bold)
1453 ("/" emphasis)
1454 ("_" underline)
1455 ("=" code)
1456 ("~" verbatim)
1457 ("+" strike))
1458 "Map Org emphasis markers to char styles.
1459 This is an alist where each element is of the
1460 form (ORG-EMPHASIS-CHAR . CHAR-STYLE).")
1462 (defun org-lparse-apply-char-styles (string)
1463 "Apply char styles to STRING.
1464 The variable `org-lparse-char-styles' controls how the Org
1465 emphasis markers are interpreted."
1466 (let ((s 0) rpl)
1467 (while (string-match org-emph-re string s)
1468 (if (not (equal
1469 (substring string (match-beginning 3) (1+ (match-beginning 3)))
1470 (substring string (match-beginning 4) (1+ (match-beginning 4)))))
1471 (setq s (match-beginning 0)
1473 (concat
1474 (match-string 1 string)
1475 (org-lparse-format
1476 'FONTIFY (match-string 4 string)
1477 (nth 1 (assoc (match-string 3 string)
1478 org-lparse-char-styles)))
1479 (match-string 5 string))
1480 string (replace-match rpl t t string)
1481 s (+ s (- (length rpl) 2)))
1482 (setq s (1+ s))))
1483 string))
1485 (defun org-lparse-export-list-line (line pos struct prevs)
1486 "Insert list syntax in export buffer. Return LINE, maybe modified.
1488 POS is the item position or line position the line had before
1489 modifications to buffer. STRUCT is the list structure. PREVS is
1490 the alist of previous items."
1491 (let* ((get-type
1492 (function
1493 ;; Translate type of list containing POS to "d", "o" or
1494 ;; "u".
1495 (lambda (pos struct prevs)
1496 (let ((type (org-list-get-list-type pos struct prevs)))
1497 (cond
1498 ((eq 'ordered type) "o")
1499 ((eq 'descriptive type) "d")
1500 (t "u"))))))
1501 (get-closings
1502 (function
1503 ;; Return list of all items and sublists ending at POS, in
1504 ;; reverse order.
1505 (lambda (pos)
1506 (let (out)
1507 (catch 'exit
1508 (mapc (lambda (e)
1509 (let ((end (nth 6 e))
1510 (item (car e)))
1511 (cond
1512 ((= end pos) (push item out))
1513 ((>= item pos) (throw 'exit nil)))))
1514 struct))
1515 out)))))
1516 ;; First close any previous item, or list, ending at POS.
1517 (mapc (lambda (e)
1518 (let* ((lastp (= (org-list-get-last-item e struct prevs) e))
1519 (first-item (org-list-get-list-begin e struct prevs))
1520 (type (funcall get-type first-item struct prevs)))
1521 (org-lparse-end-paragraph)
1522 ;; Ending for every item
1523 (org-lparse-end-list-item type)
1524 ;; We're ending last item of the list: end list.
1525 (when lastp
1526 (org-lparse-end 'LIST type)
1527 (org-lparse-begin-paragraph))))
1528 (funcall get-closings pos))
1529 (cond
1530 ;; At an item: insert appropriate tags in export buffer.
1531 ((assq pos struct)
1532 (string-match
1533 (concat "[ \t]*\\(\\S-+[ \t]*\\)"
1534 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
1535 "\\(?:\\(\\[[ X-]\\]\\)[ \t]+\\)?"
1536 "\\(?:\\(.*\\)[ \t]+::\\(?:[ \t]+\\|$\\)\\)?"
1537 "\\(.*\\)") line)
1538 (let* ((checkbox (match-string 3 line))
1539 (desc-tag (or (match-string 4 line) "???"))
1540 (body (or (match-string 5 line) ""))
1541 (list-beg (org-list-get-list-begin pos struct prevs))
1542 (firstp (= list-beg pos))
1543 ;; Always refer to first item to determine list type, in
1544 ;; case list is ill-formed.
1545 (type (funcall get-type list-beg struct prevs))
1546 (counter (let ((count-tmp (org-list-get-counter pos struct)))
1547 (cond
1548 ((not count-tmp) nil)
1549 ((string-match "[A-Za-z]" count-tmp)
1550 (- (string-to-char (upcase count-tmp)) 64))
1551 ((string-match "[0-9]+" count-tmp)
1552 count-tmp)))))
1553 (when firstp
1554 (org-lparse-end-paragraph)
1555 (org-lparse-begin 'LIST type))
1557 (let ((arg (cond ((equal type "d") desc-tag)
1558 ((equal type "o") counter))))
1559 (org-lparse-begin 'LIST-ITEM type arg))
1561 ;; If line had a checkbox, some additional modification is required.
1562 (when checkbox
1563 (setq body
1564 (concat
1565 (org-lparse-format
1566 'FONTIFY (concat
1568 (cond
1569 ((string-match "X" checkbox) "X")
1570 ((string-match " " checkbox)
1571 (org-lparse-format 'SPACES 1))
1572 (t "-"))
1573 "]")
1574 'code)
1576 body)))
1577 ;; Return modified line
1578 body))
1579 ;; At a list ender: go to next line (side-effects only).
1580 ((equal "ORG-LIST-END-MARKER" line) (throw 'nextline nil))
1581 ;; Not at an item: return line unchanged (side-effects only).
1582 (t line))))
1584 (defun org-lparse-bind-local-variables (opt-plist)
1585 (mapc (lambda (x)
1586 (set (make-local-variable (nth 2 x))
1587 (plist-get opt-plist (car x))))
1588 org-export-plist-vars))
1590 (defvar org-lparse-table-rowgrp-open)
1591 (defvar org-lparse-table-cur-rowgrp-is-hdr)
1592 (defvar org-lparse-footnote-number)
1593 (defvar org-lparse-footnote-definitions)
1594 (defvar org-lparse-footnote-buffer)
1595 (defvar org-lparse-output-buffer)
1597 (defcustom org-lparse-debug nil
1598 "Turn on/off debugging for `org-lparse'."
1599 :type 'boolean
1600 :group 'org-lparse)
1602 (defun org-lparse-begin (entity &rest args)
1603 "Begin ENTITY in current buffer. ARGS is entity specific.
1604 ENTITY can be one of PARAGRAPH, LIST, LIST-ITEM etc.
1606 Use (org-lparse-begin 'LIST \"o\") to begin a list in current
1607 buffer.
1609 See `org-xhtml-entity-control-callbacks-alist' for more
1610 information."
1611 (when (and (member org-lparse-debug '(t control))
1612 (not (eq entity 'DOCUMENT-CONTENT)))
1613 (insert (org-lparse-format 'COMMENT "%s BEGIN %S" entity args)))
1615 (let ((f (cadr (assoc entity org-lparse-entity-control-callbacks-alist))))
1616 (unless f (error "Unknown entity: %s" entity))
1617 (apply f args)))
1619 (defun org-lparse-end (entity &rest args)
1620 "Close ENTITY in current buffer. ARGS is entity specific.
1621 ENTITY can be one of PARAGRAPH, LIST, LIST-ITEM
1622 etc.
1624 Use (org-lparse-end 'LIST \"o\") to close a list in current
1625 buffer.
1627 See `org-xhtml-entity-control-callbacks-alist' for more
1628 information."
1629 (when (and (member org-lparse-debug '(t control))
1630 (not (eq entity 'DOCUMENT-CONTENT)))
1631 (insert (org-lparse-format 'COMMENT "%s END %S" entity args)))
1633 (let ((f (caddr (assoc entity org-lparse-entity-control-callbacks-alist))))
1634 (unless f (error "Unknown entity: %s" entity))
1635 (apply f args)))
1637 (defun org-lparse-begin-paragraph (&optional style)
1638 "Insert <p>, but first close previous paragraph if any."
1639 (org-lparse-end-paragraph)
1640 (org-lparse-begin 'PARAGRAPH style)
1641 (setq org-lparse-par-open t))
1643 (defun org-lparse-end-paragraph ()
1644 "Close paragraph if there is one open."
1645 (when org-lparse-par-open
1646 (org-lparse-end 'PARAGRAPH)
1647 (setq org-lparse-par-open nil)))
1649 (defun org-lparse-end-list-item (&optional type)
1650 "Close <li> if necessary."
1651 (org-lparse-end-paragraph)
1652 (org-lparse-end 'LIST-ITEM (or type "u")))
1654 (defvar org-lparse-dyn-current-environment nil)
1655 (defun org-lparse-begin-environment (style)
1656 (assert (not org-lparse-dyn-current-environment) t)
1657 (setq org-lparse-dyn-current-environment style)
1658 (org-lparse-begin 'ENVIRONMENT style))
1660 (defun org-lparse-end-environment (style)
1661 (org-lparse-end 'ENVIRONMENT style)
1663 (assert (eq org-lparse-dyn-current-environment style) t)
1664 (setq org-lparse-dyn-current-environment nil))
1666 (defun org-lparse-current-environment-p (style)
1667 (eq org-lparse-dyn-current-environment style))
1669 (defun org-lparse-begin-footnote-definition (n)
1670 (unless org-lparse-footnote-buffer
1671 (setq org-lparse-footnote-buffer
1672 (get-buffer-create "*Org HTML Export Footnotes*")))
1673 (set-buffer org-lparse-footnote-buffer)
1674 (erase-buffer)
1675 (setq org-lparse-insert-tag-with-newlines nil)
1676 (org-lparse-begin 'FOOTNOTE-DEFINITION n))
1678 (defun org-lparse-end-footnote-definition (n)
1679 (org-lparse-end 'FOOTNOTE-DEFINITION n)
1680 (setq org-lparse-insert-tag-with-newlines 'both)
1681 (push (cons n (buffer-string)) org-lparse-footnote-definitions)
1682 (set-buffer org-lparse-output-buffer))
1684 (defun org-lparse-format (entity &rest args)
1685 "Format ENTITY in backend-specific way and return it.
1686 ARGS is specific to entity being formatted.
1688 Use (org-lparse-format 'HEADING \"text\" 1) to format text as
1689 level 1 heading.
1691 See `org-xhtml-entity-format-callbacks-alist' for more information."
1692 (when (and (member org-lparse-debug '(t format))
1693 (not (equal entity 'COMMENT)))
1694 (insert (org-lparse-format 'COMMENT "%s: %S" entity args)))
1695 (cond
1696 ((consp entity)
1697 (let ((text (pop args)))
1698 (apply 'org-lparse-format 'TAGS entity text args)))
1700 (let ((f (cdr (assoc entity org-lparse-entity-format-callbacks-alist))))
1701 (unless f (error "Unknown entity: %s" entity))
1702 (apply f args)))))
1704 (defun org-lparse-insert (entity &rest args)
1705 (insert (apply 'org-lparse-format entity args)))
1707 (defun org-lparse-prepare-toc (lines level-offset opt-plist umax-toc)
1708 (let* ((quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
1709 (org-min-level (org-get-min-level lines level-offset))
1710 (org-last-level org-min-level)
1711 level)
1712 (with-temp-buffer
1713 (org-lparse-bind-local-variables opt-plist)
1714 (erase-buffer)
1715 (org-lparse-begin 'TOC (nth 3 (plist-get opt-plist :lang-words)))
1716 (setq
1717 lines
1718 (mapcar
1719 #'(lambda (line)
1720 (when (and (string-match org-todo-line-regexp line)
1721 (not (get-text-property 0 'org-protected line))
1722 (<= (setq level (org-tr-level
1723 (- (match-end 1) (match-beginning 1)
1724 level-offset)))
1725 umax-toc))
1726 (let ((txt (save-match-data
1727 (org-xml-encode-org-text-skip-links
1728 (org-export-cleanup-toc-line
1729 (match-string 3 line)))))
1730 (todo (and
1731 org-export-mark-todo-in-toc
1732 (or (and (match-beginning 2)
1733 (not (member (match-string 2 line)
1734 org-done-keywords)))
1735 (and (= level umax-toc)
1736 (org-search-todo-below
1737 line lines level)))))
1738 tags)
1739 ;; Check for targets
1740 (while (string-match org-any-target-regexp line)
1741 (setq line
1742 (replace-match
1743 (let ((org-lparse-encode-pending t))
1744 (org-lparse-format 'FONTIFY
1745 (match-string 1 line) "target"))
1746 t t line)))
1747 (when (string-match
1748 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
1749 (setq tags (match-string 1 txt)
1750 txt (replace-match "" t nil txt)))
1751 (when (string-match quote-re0 txt)
1752 (setq txt (replace-match "" t t txt)))
1753 (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
1754 (setq txt (replace-match "" t t txt)))
1755 (org-lparse-format
1756 'TOC-ITEM
1757 (let* ((snumber (org-section-number level))
1758 (href (replace-regexp-in-string
1759 "\\." "-" (format "sec-%s" snumber)))
1760 (href
1762 (cdr (assoc
1763 href org-export-preferred-target-alist))
1764 href))
1765 (href (org-solidify-link-text href)))
1766 (org-lparse-format 'TOC-ENTRY snumber todo txt tags href))
1767 level org-last-level)
1768 (setq org-last-level level)))
1769 line)
1770 lines))
1771 (org-lparse-end 'TOC)
1772 (setq org-lparse-toc (buffer-string))))
1773 lines)
1775 (defun org-lparse-format-table-row (fields &optional text-for-empty-fields)
1776 (unless org-lparse-table-ncols
1777 ;; first row of the table
1778 (setq org-lparse-table-ncols (length fields))
1779 (when org-lparse-table-is-styled
1780 (setq org-lparse-table-num-numeric-items-per-column
1781 (make-vector org-lparse-table-ncols 0))
1782 (setq org-lparse-table-colalign-vector
1783 (make-vector org-lparse-table-ncols nil))
1784 (let ((c -1))
1785 (while (< (incf c) org-lparse-table-ncols)
1786 (let ((cookie (cdr (assoc (1+ c) org-lparse-table-colalign-info))))
1787 (setf (aref org-lparse-table-colalign-vector c)
1788 (cond
1789 ((string= cookie "l") "left")
1790 ((string= cookie "r") "right")
1791 ((string= cookie "c") "center")
1792 (t nil))))))))
1793 (incf org-lparse-table-rownum)
1794 (let ((i -1))
1795 (org-lparse-format
1796 'TABLE-ROW
1797 (mapconcat
1798 (lambda (x)
1799 (when (and (string= x "") text-for-empty-fields)
1800 (setq x text-for-empty-fields))
1801 (incf i)
1802 (and org-lparse-table-is-styled
1803 (< i org-lparse-table-ncols)
1804 (string-match org-table-number-regexp x)
1805 (incf (aref org-lparse-table-num-numeric-items-per-column i)))
1806 (org-lparse-format 'TABLE-CELL x org-lparse-table-rownum i))
1807 fields "\n"))))
1809 (defun org-lparse-get (what &optional opt-plist)
1810 "Query for value of WHAT for the current backend `org-lparse-backend'.
1811 See also `org-lparse-backend-get'."
1812 (if (boundp 'org-lparse-backend)
1813 (org-lparse-backend-get (symbol-name org-lparse-backend) what opt-plist)
1814 (error "org-lparse-backend is not bound yet")))
1816 (defun org-lparse-backend-get (backend what &optional opt-plist)
1817 "Query BACKEND for value of WHAT.
1818 Dispatch the call to `org-<backend>-user-get'. If that throws an
1819 error, dispatch the call to `org-<backend>-get'. See
1820 `org-xhtml-get' for all known settings queried for by
1821 `org-lparse' during the course of export."
1822 (assert (stringp backend) t)
1823 (unless (org-lparse-backend-is-native-p backend)
1824 (error "Unknown native backend %s" backend))
1825 (let ((backend-get-method (intern (format "org-%s-get" backend)))
1826 (backend-user-get-method (intern (format "org-%s-user-get" backend))))
1827 (cond
1828 ((functionp backend-get-method)
1829 (condition-case nil
1830 (funcall backend-user-get-method what opt-plist)
1831 (error (funcall backend-get-method what opt-plist))))
1833 (error "Native backend %s doesn't define %s" backend backend-get-method)))))
1835 (defun org-lparse-insert-tag (tag &rest args)
1836 (when (member org-lparse-insert-tag-with-newlines '(lead both))
1837 (insert "\n"))
1838 (insert (apply 'format tag args))
1839 (when (member org-lparse-insert-tag-with-newlines '(trail both))
1840 (insert "\n")))
1842 (defun org-lparse-get-targets-from-title (title)
1843 (let* ((target (org-get-text-property-any 0 'target title))
1844 (extra-targets (assoc target org-export-target-aliases))
1845 (target (or (cdr (assoc target org-export-preferred-target-alist))
1846 target)))
1847 (cons target (remove target extra-targets))))
1849 (defun org-lparse-suffix-from-snumber (snumber)
1850 (let* ((snu (replace-regexp-in-string "\\." "-" snumber))
1851 (href (cdr (assoc (concat "sec-" snu)
1852 org-export-preferred-target-alist))))
1853 (org-solidify-link-text (or href snu))))
1855 (defun org-lparse-begin-level (level title umax head-count)
1856 "Insert a new LEVEL in HTML export.
1857 When TITLE is nil, just close all open levels."
1858 (org-lparse-end-level level umax)
1859 (unless title (error "Why is heading nil"))
1860 (let* ((targets (org-lparse-get-targets-from-title title))
1861 (target (car targets)) (extra-targets (cdr targets))
1862 (target (and target (org-solidify-link-text target)))
1863 (extra-class (org-get-text-property-any 0 'html-container-class title))
1864 snumber tags level1 class)
1865 (when (string-match (org-re "\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") title)
1866 (setq tags (and org-export-with-tags (match-string 1 title)))
1867 (setq title (replace-match "" t t title)))
1868 (if (> level umax)
1869 (progn
1870 (if (aref org-levels-open (1- level))
1871 (org-lparse-end-list-item)
1872 (aset org-levels-open (1- level) t)
1873 (org-lparse-end-paragraph)
1874 (org-lparse-begin 'LIST 'unordered))
1875 (org-lparse-begin
1876 'LIST-ITEM 'unordered target
1877 (org-lparse-format 'HEADLINE title extra-targets tags)))
1878 (aset org-levels-open (1- level) t)
1879 (setq snumber (org-section-number level))
1880 (setq level1 (+ level (or (org-lparse-get 'TOPLEVEL-HLEVEL) 1) -1))
1881 (unless (= head-count 1)
1882 (org-lparse-end-outline-text-or-outline))
1883 (org-lparse-begin-outline-and-outline-text
1884 level1 snumber title tags target extra-targets extra-class)
1885 (org-lparse-begin-paragraph))))
1887 (defun org-lparse-end-level (level umax)
1888 (org-lparse-end-paragraph)
1889 (loop for l from org-level-max downto level
1890 do (when (aref org-levels-open (1- l))
1891 ;; Terminate one level in HTML export
1892 (if (<= l umax)
1893 (org-lparse-end-outline-text-or-outline)
1894 (org-lparse-end-list-item)
1895 (org-lparse-end 'LIST 'unordered))
1896 (aset org-levels-open (1- l) nil))))
1898 (defvar org-lparse-outline-text-open)
1899 (defun org-lparse-begin-outline-and-outline-text (level1 snumber title tags
1900 target extra-targets
1901 extra-class)
1902 (org-lparse-begin
1903 'OUTLINE level1 snumber title tags target extra-targets extra-class)
1904 (org-lparse-begin-outline-text level1 snumber extra-class))
1906 (defun org-lparse-end-outline-text-or-outline ()
1907 (cond
1908 (org-lparse-outline-text-open
1909 (org-lparse-end 'OUTLINE-TEXT)
1910 (setq org-lparse-outline-text-open nil))
1911 (t (org-lparse-end 'OUTLINE))))
1913 (defun org-lparse-begin-outline-text (level1 snumber extra-class)
1914 (assert (not org-lparse-outline-text-open) t)
1915 (setq org-lparse-outline-text-open t)
1916 (org-lparse-begin 'OUTLINE-TEXT level1 snumber extra-class))
1918 (defun org-lparse-html-list-type-to-canonical-list-type (ltype)
1919 (cdr (assoc ltype '(("o" . ordered)
1920 ("u" . unordered)
1921 ("d" . description)))))
1923 (defvar org-lparse-table-rowgrp-info)
1924 (defun org-lparse-begin-table-rowgroup (&optional is-header-row)
1925 (push (cons (1+ org-lparse-table-rownum) :start) org-lparse-table-rowgrp-info)
1926 (org-lparse-begin 'TABLE-ROWGROUP is-header-row))
1928 (defun org-lparse-end-table ()
1929 (when org-lparse-table-is-styled
1930 ;; column groups
1931 (unless (car org-table-colgroup-info)
1932 (setq org-table-colgroup-info
1933 (cons :start (cdr org-table-colgroup-info))))
1935 ;; column alignment
1936 (let ((c -1))
1937 (mapc
1938 (lambda (x)
1939 (incf c)
1940 (setf (aref org-lparse-table-colalign-vector c)
1941 (or (aref org-lparse-table-colalign-vector c)
1942 (if (> (/ (float x) (1+ org-lparse-table-rownum))
1943 org-table-number-fraction)
1944 "right" "left"))))
1945 org-lparse-table-num-numeric-items-per-column)))
1946 (org-lparse-end 'TABLE))
1948 (defvar org-lparse-encode-pending nil)
1950 (defun org-lparse-format-tags (tag text prefix suffix &rest args)
1951 (cond
1952 ((consp tag)
1953 (concat prefix (apply 'format (car tag) args) text suffix
1954 (format (cdr tag))))
1955 ((stringp tag) ; singleton tag
1956 (concat prefix (apply 'format tag args) text))))
1958 (defun org-xml-fix-class-name (kwd) ; audit callers of this function
1959 "Turn todo keyword into a valid class name.
1960 Replaces invalid characters with \"_\"."
1961 (save-match-data
1962 (while (string-match "[^a-zA-Z0-9_]" kwd)
1963 (setq kwd (replace-match "_" t t kwd))))
1964 kwd)
1966 (defun org-lparse-format-todo (todo)
1967 (org-lparse-format 'FONTIFY
1968 (concat
1969 (ignore-errors (org-lparse-get 'TODO-KWD-CLASS-PREFIX))
1970 (org-xml-fix-class-name todo))
1971 (list (if (member todo org-done-keywords) "done" "todo")
1972 todo)))
1974 (defun org-lparse-format-extra-targets (extra-targets)
1975 (if (not extra-targets) ""
1976 (mapconcat (lambda (x)
1977 (setq x (org-solidify-link-text
1978 (if (org-uuidgen-p x) (concat "ID-" x) x)))
1979 (org-lparse-format 'ANCHOR "" x))
1980 extra-targets "")))
1982 (defun org-lparse-format-org-tags (tags)
1983 (if (not tags) ""
1984 (org-lparse-format
1985 'FONTIFY (mapconcat
1986 (lambda (x)
1987 (org-lparse-format
1988 'FONTIFY x
1989 (concat
1990 (ignore-errors (org-lparse-get 'TAG-CLASS-PREFIX))
1991 (org-xml-fix-class-name x))))
1992 (org-split-string tags ":")
1993 (org-lparse-format 'SPACES 1)) "tag")))
1995 (defun org-lparse-format-section-number (&optional snumber level)
1996 (and org-export-with-section-numbers
1997 (not org-lparse-body-only) snumber level
1998 (org-lparse-format 'FONTIFY snumber (format "section-number-%d" level))))
2000 (defun org-lparse-warn (msg)
2001 (if (not org-lparse-use-flashy-warning)
2002 (message msg)
2003 (put-text-property 0 (length msg) 'face 'font-lock-warning-face msg)
2004 (message msg)
2005 (sleep-for 3)))
2007 (defun org-xml-format-href (s)
2008 "Make sure the S is valid as a href reference in an XHTML document."
2009 (save-match-data
2010 (let ((start 0))
2011 (while (string-match "&" s start)
2012 (setq start (+ (match-beginning 0) 3)
2013 s (replace-match "&amp;" t t s)))))
2016 (defun org-xml-format-desc (s)
2017 "Make sure the S is valid as a description in a link."
2018 (if (and s (not (get-text-property 1 'org-protected s)))
2019 (save-match-data
2020 (org-xml-encode-org-text s))
2023 (provide 'org-lparse)
2025 ;;; org-lparse.el ends here