org-lparse/org-odt: Add docstring. Silence byte-compiler
[org-mode/org-mode-NeilSmithlineMods.git] / contrib / lisp / org-lparse.el
blobb37b683590ab5260f5a7da41556b769a77e535a1
1 ;;; org-lparse.el --- Line-oriented 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 all new interactive command `org-export-convert' can be used
47 ;;; to convert documents between various formats. Use this to
48 ;;; command, 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
341 '("xhtml" "odt")
342 "List of native backends registered with `org-lparse'.
343 All native backends must implement a get routine and a mandatory
344 set of callback routines.
346 The get routine must be named as org-<backend>-get where backend
347 is the name of the backend. The exporter uses `org-lparse-get'
348 and retrieves the backend-specific callback by querying for
349 ENTITY-CONTROL and ENTITY-FORMAT variables.
351 For the sake of illustration, the html backend implements
352 `org-xhtml-get'. It returns
353 `org-xhtml-entity-control-callbacks-alist' and
354 `org-xhtml-entity-format-callbacks-alist' as the values of
355 ENTITY-CONTROL and ENTITY-FORMAT settings.")
357 (defun org-lparse-get-other-backends (native-backend)
358 (org-lparse-backend-get native-backend 'OTHER-BACKENDS))
360 (defun org-lparse-all-backends ()
361 (let (all-backends)
362 (flet ((add (other native)
363 (let ((val (assoc-string other all-backends t)))
364 (if val (setcdr val (nconc (list native) (cdr val)))
365 (push (cons other (list native)) all-backends)))))
366 (loop for backend in org-lparse-native-backends
367 do (loop for other in (org-lparse-get-other-backends backend)
368 do (add other backend))))
369 all-backends))
371 (defun org-lparse-backend-is-native-p (backend)
372 (member backend org-lparse-native-backends))
374 (defun org-lparse (target-backend native-backend arg
375 &optional hidden ext-plist
376 to-buffer body-only pub-dir)
377 "Export the outline to various formats.
378 If there is an active region, export only the region. The outline
379 is first exported to NATIVE-BACKEND and optionally converted to
380 TARGET-BACKEND. See `org-lparse-native-backends' for list of
381 known native backends. Each native backend can specify a
382 converter and list of target backends it exports to using the
383 CONVERT-PROCESS and OTHER-BACKENDS settings of it's get
384 method. See `org-xhtml-get' for an illustrative example.
386 ARG is a prefix argument that specifies how many levels of
387 outline should become headlines. The default is 3. Lower levels
388 will become bulleted lists.
390 HIDDEN is obsolete and does nothing.
392 EXT-PLIST is a property list that controls various aspects of
393 export. The settings here override org-mode's default settings
394 and but are inferior to file-local settings.
396 TO-BUFFER dumps the exported lines to a buffer or a string
397 instead of a file. If TO-BUFFER is the symbol `string' return the
398 exported lines as a string. If TO-BUFFER is non-nil, create a
399 buffer with that name and export to that buffer.
401 BODY-ONLY controls the presence of header and footer lines in
402 exported text. If BODY-ONLY is non-nil, don't produce the file
403 header and footer, simply return the content of <body>...</body>,
404 without even the body tags themselves.
406 PUB-DIR specifies the publishing directory."
407 (interactive
408 (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
409 (all-backends (org-lparse-all-backends))
410 (target-backend
411 (funcall input "Export to: " all-backends nil t nil))
412 (native-backend
414 ;; (and (org-lparse-backend-is-native-p target-backend)
415 ;; target-backend)
416 (funcall input "Use Native backend: "
417 (cdr (assoc target-backend all-backends)) nil t nil))))
418 (list target-backend native-backend current-prefix-arg)))
419 (let* ((org-lparse-backend (intern native-backend))
420 (org-lparse-other-backend (intern target-backend)))
421 (unless (org-lparse-backend-is-native-p native-backend)
422 (error "Don't know how to export natively to backend %s" native-backend))
423 (unless (or (not target-backend)
424 (equal target-backend native-backend)
425 (member target-backend (org-lparse-get 'OTHER-BACKENDS)))
426 (error "Don't know how to export to backend %s %s" target-backend
427 (format "via %s" native-backend)))
428 (run-hooks 'org-export-first-hook)
429 (org-do-lparse arg hidden ext-plist to-buffer body-only pub-dir)))
431 (defcustom org-export-convert-process
432 '("soffice" "-norestore" "-invisible" "-headless" "\"macro:///BasicODConverter.Main.Convert(%I,%f,%O)\"")
433 "Command to covert a Org exported format to other formats.
434 The variable is an list of the form (PROCESS ARG1 ARG2 ARG3
435 ...). Format specifiers used in the ARGs are replaced as below.
436 %i input file name in full
437 %I input file name as a URL
438 %f format of the output file
439 %o output file name in full
440 %O output file name as a URL
441 %d output dir in full
442 %D output dir as a URL"
443 :group 'org-export)
445 (defun org-export-convert (&optional in-file fmt)
446 "Convert file from one format to another using a converter.
447 IN-FILE is the file to be converted. If unspecified, it defaults
448 to variable `buffer-file-name'. FMT is the desired output format. If the
449 backend has registered a CONVERT-METHOD via it's get function
450 then that converter is used. Otherwise
451 `org-export-conver-process' is used."
452 (interactive
453 (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
454 (in-file (read-file-name "File to be converted: "
455 nil buffer-file-name t))
456 (fmt (funcall input "Output format: "
457 (or (ignore-errors
458 (org-lparse-get-other-backends
459 (file-name-extension in-file)))
460 (org-lparse-all-backends))
461 nil nil nil)))
462 (list in-file fmt)))
463 (require 'browse-url)
464 (let* ((in-file (expand-file-name (or in-file buffer-file-name)))
465 (fmt (or fmt "doc") )
466 (out-file (concat (file-name-sans-extension in-file) "." fmt))
467 (out-dir (file-name-directory in-file))
468 (backend (when (boundp 'org-lparse-backend) org-lparse-backend))
469 (convert-process
470 (or (ignore-errors (org-lparse-backend-get backend 'CONVERT-METHOD))
471 org-export-convert-process))
472 program arglist)
474 (setq program (and convert-process (consp convert-process)
475 (car convert-process)))
476 (unless (executable-find program)
477 (error "Unable to locate the converter %s" program))
479 (setq arglist
480 (mapcar (lambda (arg)
481 (format-spec arg `((?i . ,in-file)
482 (?I . ,(browse-url-file-url in-file))
483 (?f . ,fmt)
484 (?o . ,out-file)
485 (?O . ,(browse-url-file-url out-file))
486 (?d . ,out-dir)
487 (?D . ,(browse-url-file-url out-dir)))))
488 (cdr convert-process)))
489 (ignore-errors (delete-file out-file))
491 (message "Executing %s %s" program (mapconcat 'identity arglist " "))
492 (apply 'call-process program nil nil nil arglist)
494 (cond
495 ((file-exists-p out-file)
496 (message "Exported to %s using %s" out-file program)
497 out-file
498 ;; (set-buffer (find-file-noselect out-file))
501 (message "Export to %s failed" out-file)
502 nil))))
504 (defvar org-lparse-insert-tag-with-newlines 'both)
506 ;; Following variables are let-bound during `org-lparse'
507 (defvar org-lparse-dyn-first-heading-pos)
508 (defvar org-lparse-toc)
509 (defvar org-lparse-entity-control-callbacks-alist)
510 (defvar org-lparse-entity-format-callbacks-alist)
511 (defvar org-lparse-backend nil
512 "The native backend to which the document is currently exported.
513 This variable is let bound during `org-lparse'. Valid values are
514 one of the symbols corresponding to `org-lparse-native-backends'.
516 Compare this variable with `org-export-current-backend' which is
517 bound only during `org-export-preprocess-string' stage of the
518 export process.
520 See also `org-lparse-other-backend'.")
522 (defvar org-lparse-other-backend nil
523 "The target backend to which the document is currently exported.
524 This variable is let bound during `org-lparse'. This variable is
525 set to either `org-lparse-backend' or one of the symbols
526 corresponding to OTHER-BACKENDS specification of the
527 org-lparse-backend.
529 For example, if a document is exported to \"odt\" then both
530 org-lparse-backend and org-lparse-other-backend are bound to
531 'odt. On the other hand, if a document is exported to \"odt\"
532 and then converted to \"doc\" then org-lparse-backend is set to
533 'odt and org-lparse-other-backend is set to 'doc.")
535 (defvar org-lparse-body-only nil
536 "Bind this to BODY-ONLY arg of `org-lparse'.")
538 (defvar org-lparse-to-buffer nil
539 "Bind this to TO-BUFFER arg of `org-lparse'.")
541 (defun org-do-lparse (arg &optional hidden ext-plist
542 to-buffer body-only pub-dir)
543 "Export the outline to various formats.
544 See `org-lparse' for more information. This function is a
545 html-agnostic version of the `org-export-as-html' function in 7.5
546 version."
547 ;; Make sure we have a file name when we need it.
548 (when (and (not (or to-buffer body-only))
549 (not buffer-file-name))
550 (if (buffer-base-buffer)
551 (org-set-local 'buffer-file-name
552 (with-current-buffer (buffer-base-buffer)
553 buffer-file-name))
554 (error "Need a file name to be able to export")))
556 (org-lparse-warn
557 (format "Exporting to %s using org-lparse..."
558 (upcase (symbol-name
559 (or org-lparse-backend org-lparse-other-backend)))))
561 (setq-default org-todo-line-regexp org-todo-line-regexp)
562 (setq-default org-deadline-line-regexp org-deadline-line-regexp)
563 (setq-default org-done-keywords org-done-keywords)
564 (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
565 (let* (org-lparse-encode-pending
566 org-lparse-par-open
567 org-lparse-outline-text-open
568 (org-lparse-latex-fragment-fallback ; currently used only by
569 ; odt exporter
570 (or (ignore-errors (org-lparse-get 'LATEX-FRAGMENT-FALLBACK))
571 (if (and (org-check-external-command "latex" "" t)
572 (org-check-external-command "dvipng" "" t))
573 'dvipng
574 'verbatim)))
575 (org-lparse-insert-tag-with-newlines 'both)
576 (org-lparse-to-buffer to-buffer)
577 (org-lparse-body-only body-only)
578 (org-lparse-entity-control-callbacks-alist
579 (org-lparse-get 'ENTITY-CONTROL))
580 (org-lparse-entity-format-callbacks-alist
581 (org-lparse-get 'ENTITY-FORMAT))
582 (opt-plist
583 (org-export-process-option-filters
584 (org-combine-plists (org-default-export-plist)
585 ext-plist
586 (org-infile-export-plist))))
587 (body-only (or body-only (plist-get opt-plist :body-only)))
588 valid org-lparse-dyn-first-heading-pos
589 (odd org-odd-levels-only)
590 (region-p (org-region-active-p))
591 (rbeg (and region-p (region-beginning)))
592 (rend (and region-p (region-end)))
593 (subtree-p
594 (if (plist-get opt-plist :ignore-subtree-p)
596 (when region-p
597 (save-excursion
598 (goto-char rbeg)
599 (and (org-at-heading-p)
600 (>= (org-end-of-subtree t t) rend))))))
601 (level-offset (if subtree-p
602 (save-excursion
603 (goto-char rbeg)
604 (+ (funcall outline-level)
605 (if org-odd-levels-only 1 0)))
607 (opt-plist (setq org-export-opt-plist
608 (if subtree-p
609 (org-export-add-subtree-options opt-plist rbeg)
610 opt-plist)))
611 ;; The following two are dynamically scoped into other
612 ;; routines below.
613 (org-current-export-dir
614 (or pub-dir (org-lparse-get 'EXPORT-DIR opt-plist)))
615 (org-current-export-file buffer-file-name)
616 (level 0) (line "") (origline "") txt todo
617 (umax nil)
618 (umax-toc nil)
619 (filename (if to-buffer nil
620 (expand-file-name
621 (concat
622 (file-name-sans-extension
623 (or (and subtree-p
624 (org-entry-get (region-beginning)
625 "EXPORT_FILE_NAME" t))
626 (file-name-nondirectory buffer-file-name)))
627 "." (org-lparse-get 'FILE-NAME-EXTENSION opt-plist))
628 (file-name-as-directory
629 (or pub-dir (org-lparse-get 'EXPORT-DIR opt-plist))))))
630 (current-dir (if buffer-file-name
631 (file-name-directory buffer-file-name)
632 default-directory))
633 (buffer (if to-buffer
634 (cond
635 ((eq to-buffer 'string)
636 (get-buffer-create (org-lparse-get 'EXPORT-BUFFER-NAME)))
637 (t (get-buffer-create to-buffer)))
638 (find-file-noselect
639 (or (let ((f (org-lparse-get 'INIT-METHOD)))
640 (and f (functionp f) (funcall f filename)))
641 filename))))
642 (org-levels-open (make-vector org-level-max nil))
643 (date (plist-get opt-plist :date))
644 (date (cond
645 ((and date (string-match "%" date))
646 (format-time-string date))
647 (date date)
648 (t (format-time-string "%Y-%m-%d %T %Z"))))
649 (dummy (setq opt-plist (plist-put opt-plist :effective-date date)))
650 (title (org-xml-encode-org-text-skip-links
651 (or (and subtree-p (org-export-get-title-from-subtree))
652 (plist-get opt-plist :title)
653 (and (not body-only)
654 (not
655 (plist-get opt-plist :skip-before-1st-heading))
656 (org-export-grab-title-from-buffer))
657 (and buffer-file-name
658 (file-name-sans-extension
659 (file-name-nondirectory buffer-file-name)))
660 "UNTITLED")))
661 (dummy (setq opt-plist (plist-put opt-plist :title title)))
662 (html-table-tag (plist-get opt-plist :html-table-tag))
663 (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
664 (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
665 (org-lparse-dyn-current-environment nil)
666 ;; Get the language-dependent settings
667 (lang-words (or (assoc (plist-get opt-plist :language)
668 org-export-language-setup)
669 (assoc "en" org-export-language-setup)))
670 (dummy (setq opt-plist (plist-put opt-plist :lang-words lang-words)))
671 (head-count 0) cnt
672 (start 0)
673 (coding-system-for-write
674 (or (ignore-errors (org-lparse-get 'CODING-SYSTEM-FOR-WRITE))
675 (and (boundp 'buffer-file-coding-system)
676 buffer-file-coding-system)))
677 (save-buffer-coding-system
678 (or (ignore-errors (org-lparse-get 'CODING-SYSTEM-FOR-SAVE))
679 (and (boundp 'buffer-file-coding-system)
680 buffer-file-coding-system)))
681 (region
682 (buffer-substring
683 (if region-p (region-beginning) (point-min))
684 (if region-p (region-end) (point-max))))
685 (org-export-have-math nil)
686 (org-export-footnotes-seen nil)
687 (org-export-footnotes-data (org-footnote-all-labels 'with-defs))
688 (org-footnote-insert-pos-for-preprocessor 'point-min)
689 (lines
690 (org-split-string
691 (org-export-preprocess-string
692 region
693 :emph-multiline t
694 :for-backend (if (equal org-lparse-backend 'xhtml) ; hack
695 'html
696 org-lparse-backend)
697 :skip-before-1st-heading
698 (plist-get opt-plist :skip-before-1st-heading)
699 :drawers (plist-get opt-plist :drawers)
700 :todo-keywords (plist-get opt-plist :todo-keywords)
701 :tasks (plist-get opt-plist :tasks)
702 :tags (plist-get opt-plist :tags)
703 :priority (plist-get opt-plist :priority)
704 :footnotes (plist-get opt-plist :footnotes)
705 :timestamps (plist-get opt-plist :timestamps)
706 :archived-trees
707 (plist-get opt-plist :archived-trees)
708 :select-tags (plist-get opt-plist :select-tags)
709 :exclude-tags (plist-get opt-plist :exclude-tags)
710 :add-text
711 (plist-get opt-plist :text)
712 :LaTeX-fragments
713 (plist-get opt-plist :LaTeX-fragments))
714 "[\r\n]"))
715 table-open
716 table-buffer table-orig-buffer
718 rpl path attr desc descp desc1 desc2 link
719 snumber fnc
720 footnotes footref-seen
721 org-lparse-output-buffer
722 org-lparse-footnote-definitions
723 org-lparse-footnote-number
724 org-lparse-footnote-buffer
725 org-lparse-toc
726 href
729 (let ((inhibit-read-only t))
730 (org-unmodified
731 (remove-text-properties (point-min) (point-max)
732 '(:org-license-to-kill t))))
734 (message "Exporting...")
735 (org-init-section-numbers)
737 ;; Switch to the output buffer
738 (setq org-lparse-output-buffer buffer)
739 (set-buffer org-lparse-output-buffer)
740 (let ((inhibit-read-only t)) (erase-buffer))
741 (fundamental-mode)
742 (org-install-letbind)
744 (and (fboundp 'set-buffer-file-coding-system)
745 (set-buffer-file-coding-system coding-system-for-write))
747 (let ((case-fold-search nil)
748 (org-odd-levels-only odd))
749 ;; create local variables for all options, to make sure all called
750 ;; functions get the correct information
751 (mapc (lambda (x)
752 (set (make-local-variable (nth 2 x))
753 (plist-get opt-plist (car x))))
754 org-export-plist-vars)
755 (setq umax (if arg (prefix-numeric-value arg)
756 org-export-headline-levels))
757 (setq umax-toc (if (integerp org-export-with-toc)
758 (min org-export-with-toc umax)
759 umax))
761 (when (and org-export-with-toc (not body-only))
762 (setq lines (org-lparse-prepare-toc
763 lines level-offset opt-plist umax-toc)))
765 (unless body-only
766 (org-lparse-begin 'DOCUMENT-CONTENT opt-plist)
767 (org-lparse-begin 'DOCUMENT-BODY opt-plist))
769 (setq head-count 0)
770 (org-init-section-numbers)
772 (org-lparse-begin-paragraph)
774 (while (setq line (pop lines) origline line)
775 (catch 'nextline
776 (when (and (org-lparse-current-environment-p 'quote)
777 (string-match "^\\*+ " line))
778 (org-lparse-end-environment 'quote))
780 (when (org-lparse-current-environment-p 'quote)
781 (org-lparse-insert 'LINE line)
782 (throw 'nextline nil))
784 ;; Fixed-width, verbatim lines (examples)
785 (when (and org-export-with-fixed-width
786 (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line))
787 (when (not (org-lparse-current-environment-p 'fixedwidth))
788 (org-lparse-begin-environment 'fixedwidth))
789 (org-lparse-insert 'LINE (match-string 3 line))
790 (when (or (not lines)
791 (not (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)"
792 (car lines))))
793 (org-lparse-end-environment 'fixedwidth))
794 (throw 'nextline nil))
796 ;; Notes: The baseline version of org-html.el (git commit
797 ;; 3d802e), while encoutering a *line-long* protected text,
798 ;; does one of the following two things based on the state
799 ;; of the export buffer.
801 ;; 1. If a paragraph element has just been opened and
802 ;; contains only whitespace as content, insert the
803 ;; protected text as part of the previous paragraph.
805 ;; 2. If the paragraph element has already been opened and
806 ;; contains some valid content insert the protected text
807 ;; as part of the current paragraph.
809 ;; I think --->
811 ;; Scenario 1 mentioned above kicks in when a block of
812 ;; protected text has to be inserted enbloc. For example,
813 ;; this happens, when inserting an source or example block
814 ;; or preformatted content enclosed in #+backend,
815 ;; #+begin_bakend ... #+end_backend)
817 ;; Scenario 2 mentioned above kicks in when the protected
818 ;; text is part of a running sentence. For example this
819 ;; happens in the case of an *multiline* LaTeX equation that
820 ;; needs to be inserted verbatim.
822 ;; org-html.el in the master branch seems to do some
823 ;; jugglery by moving paragraphs around. Inorder to make
824 ;; these changes backend-agnostic introduce a new text
825 ;; property org-native-text and impose the added semantics
826 ;; that these protected blocks appear outside of a
827 ;; conventional paragraph element.
829 ;; Extra Note: Check whether org-example and org-native-text
830 ;; are entirely equivalent.
832 ;; Fixes bug reported by Christian Moe concerning verbatim
833 ;; LaTeX fragments.
834 ;; on git commit 533ba3f90250a1f25f494c390d639ea6274f235c
835 ;; http://repo.or.cz/w/org-mode/org-jambu.git/shortlog/refs/heads/staging
836 ;; See http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01379.html
838 ;; Native Text
839 (when (and (get-text-property 0 'org-native-text line)
840 ;; Make sure it is the entire line that is protected
841 (not (< (or (next-single-property-change
842 0 'org-native-text line) 10000)
843 (length line))))
844 (let ((ind (get-text-property 0 'original-indentation line)))
845 (org-lparse-begin-environment 'native)
846 (org-lparse-insert 'LINE line)
847 (while (and lines
848 (or (= (length (car lines)) 0)
849 (not ind)
850 (equal ind (get-text-property
851 0 'original-indentation (car lines))))
852 (or (= (length (car lines)) 0)
853 (get-text-property 0 'org-native-text (car lines))))
854 (org-lparse-insert 'LINE (pop lines)))
855 (org-lparse-end-environment 'native))
856 (throw 'nextline nil))
858 ;; Protected HTML
859 (when (and (get-text-property 0 'org-protected line)
860 ;; Make sure it is the entire line that is protected
861 (not (< (or (next-single-property-change
862 0 'org-protected line) 10000)
863 (length line))))
864 (let ((ind (get-text-property 0 'original-indentation line)))
865 (org-lparse-insert 'LINE line)
866 (while (and lines
867 (or (= (length (car lines)) 0)
868 (not ind)
869 (equal ind (get-text-property
870 0 'original-indentation (car lines))))
871 (or (= (length (car lines)) 0)
872 (get-text-property 0 'org-protected (car lines))))
873 (org-lparse-insert 'LINE (pop lines))))
874 (throw 'nextline nil))
876 ;; Blockquotes, verse, and center
877 (when (string-match "^ORG-\\(.+\\)-\\(START\\|END\\)$" line)
878 (let* ((style (intern (downcase (match-string 1 line))))
879 (f (cdr (assoc (match-string 2 line)
880 '(("START" . org-lparse-begin-environment)
881 ("END" . org-lparse-end-environment))))))
882 (when (memq style '(blockquote verse center))
883 (funcall f style)
884 (throw 'nextline nil))))
886 (run-hooks 'org-export-html-after-blockquotes-hook)
887 (when (org-lparse-current-environment-p 'verse)
888 (let ((i (org-get-string-indentation line)))
889 (if (> i 0)
890 (setq line (concat
891 (let ((org-lparse-encode-pending t))
892 (org-lparse-format 'SPACES (* 2 i)))
893 " " (org-trim line))))
894 (unless (string-match "\\\\\\\\[ \t]*$" line)
895 (setq line (concat line "\\\\")))))
897 ;; make targets to anchors
898 (setq start 0)
899 (while (string-match
900 "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line start)
901 (cond
902 ((get-text-property (match-beginning 1) 'org-protected line)
903 (setq start (match-end 1)))
904 ((match-end 2)
905 (setq line (replace-match
906 (let ((org-lparse-encode-pending t))
907 (org-lparse-format
908 'ANCHOR "" (org-solidify-link-text
909 (match-string 1 line))))
910 t t line)))
911 ((and org-export-with-toc (equal (string-to-char line) ?*))
912 ;; FIXME: NOT DEPENDENT on TOC?????????????????????
913 (setq line (replace-match
914 (let ((org-lparse-encode-pending t))
915 (org-lparse-format
916 'FONTIFY (match-string 1 line) "target"))
917 ;; (concat "@<i>" (match-string 1 line) "@</i> ")
918 t t line)))
920 (setq line (replace-match
921 (concat
922 (let ((org-lparse-encode-pending t))
923 (org-lparse-format
924 'ANCHOR (match-string 1 line)
925 (org-solidify-link-text (match-string 1 line))
926 "target")) " ")
927 t t line)))))
929 (let ((org-lparse-encode-pending t))
930 (setq line (org-lparse-handle-time-stamps line)))
932 ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
933 ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
934 ;; Also handle sub_superscripts and checkboxes
935 (or (string-match org-table-hline-regexp line)
936 (string-match "^[ \t]*\\([+]-\\||[ ]\\)[-+ |]*[+|][ \t]*$" line)
937 (setq line (org-xml-encode-org-text-skip-links line)))
939 (setq line (org-lparse-format-org-link line opt-plist))
941 ;; TODO items
942 (if (and (string-match org-todo-line-regexp line)
943 (match-beginning 2))
944 (setq line (concat
945 (substring line 0 (match-beginning 2))
946 (org-lparse-format 'TODO (match-string 2 line))
947 (substring line (match-end 2)))))
949 ;; Does this contain a reference to a footnote?
950 (when org-export-with-footnotes
951 (setq start 0)
952 (while (string-match "\\([^* \t].*?\\)[ \t]*\\[\\([0-9]+\\)\\]" line start)
953 ;; Discard protected matches not clearly identified as
954 ;; footnote markers.
955 (if (or (get-text-property (match-beginning 2) 'org-protected line)
956 (not (get-text-property (match-beginning 2) 'org-footnote line)))
957 (setq start (match-end 2))
958 (let ((n (match-string 2 line)) refcnt a)
959 (if (setq a (assoc n footref-seen))
960 (progn
961 (setcdr a (1+ (cdr a)))
962 (setq refcnt (cdr a)))
963 (setq refcnt 1)
964 (push (cons n 1) footref-seen))
965 (setq line
966 (replace-match
967 (concat
968 (or (match-string 1 line) "")
969 (org-lparse-format
970 'FOOTNOTE-REFERENCE
971 n (cdr (assoc n org-lparse-footnote-definitions))
972 refcnt)
973 ;; If another footnote is following the
974 ;; current one, add a separator.
975 (if (save-match-data
976 (string-match "\\`\\[[0-9]+\\]"
977 (substring line (match-end 0))))
978 (ignore-errors
979 (org-lparse-get 'FOOTNOTE-SEPARATOR))
980 ""))
981 t t line))))))
983 (cond
984 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
985 ;; This is a headline
986 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
987 level-offset))
988 txt (match-string 2 line))
989 (if (string-match quote-re0 txt)
990 (setq txt (replace-match "" t t txt)))
991 (if (<= level (max umax umax-toc))
992 (setq head-count (+ head-count 1)))
993 (unless org-lparse-dyn-first-heading-pos
994 (setq org-lparse-dyn-first-heading-pos (point)))
995 (org-lparse-begin-level level txt umax head-count)
997 ;; QUOTES
998 (when (string-match quote-re line)
999 (org-lparse-begin-environment 'quote)))
1001 ((and org-export-with-tables
1002 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
1003 (when (not table-open)
1004 ;; New table starts
1005 (setq table-open t table-buffer nil table-orig-buffer nil))
1007 ;; Accumulate lines
1008 (setq table-buffer (cons line table-buffer)
1009 table-orig-buffer (cons origline table-orig-buffer))
1010 (when (or (not lines)
1011 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
1012 (car lines))))
1013 (setq table-open nil
1014 table-buffer (nreverse table-buffer)
1015 table-orig-buffer (nreverse table-orig-buffer))
1016 (org-lparse-end-paragraph)
1017 (org-lparse-insert 'TABLE table-buffer table-orig-buffer)))
1019 ;; Normal lines
1022 ;; This line either is list item or end a list.
1023 (when (get-text-property 0 'list-item line)
1024 (setq line (org-lparse-export-list-line
1025 line
1026 (get-text-property 0 'list-item line)
1027 (get-text-property 0 'list-struct line)
1028 (get-text-property 0 'list-prevs line))))
1030 ;; Horizontal line
1031 (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
1032 (with-org-lparse-preserve-paragraph-state
1033 (org-lparse-insert 'HORIZONTAL-LINE))
1034 (throw 'nextline nil))
1036 ;; Empty lines start a new paragraph. If hand-formatted lists
1037 ;; are not fully interpreted, lines starting with "-", "+", "*"
1038 ;; also start a new paragraph.
1039 (when (string-match "^ [-+*]-\\|^[ \t]*$" line)
1040 (when org-lparse-footnote-number
1041 (org-lparse-end-footnote-definition org-lparse-footnote-number)
1042 (setq org-lparse-footnote-number nil))
1043 (org-lparse-begin-paragraph))
1045 ;; Is this the start of a footnote?
1046 (when org-export-with-footnotes
1047 (when (and (boundp 'footnote-section-tag-regexp)
1048 (string-match (concat "^" footnote-section-tag-regexp)
1049 line))
1050 ;; ignore this line
1051 (throw 'nextline nil))
1052 (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
1053 (org-lparse-end-paragraph)
1054 (setq org-lparse-footnote-number (match-string 1 line))
1055 (setq line (replace-match "" t t line))
1056 (org-lparse-begin-footnote-definition org-lparse-footnote-number)))
1057 ;; Check if the line break needs to be conserved
1058 (cond
1059 ((string-match "\\\\\\\\[ \t]*$" line)
1060 (setq line (replace-match
1061 (org-lparse-format 'LINE-BREAK)
1062 t t line)))
1063 (org-export-preserve-breaks
1064 (setq line (concat line (org-lparse-format 'LINE-BREAK)))))
1066 ;; Check if a paragraph should be started
1067 (let ((start 0))
1068 (while (and org-lparse-par-open
1069 (string-match "\\\\par\\>" line start))
1070 (error "FIXME")
1071 ;; Leave a space in the </p> so that the footnote matcher
1072 ;; does not see this.
1073 (if (not (get-text-property (match-beginning 0)
1074 'org-protected line))
1075 (setq line (replace-match "</p ><p >" t t line)))
1076 (setq start (match-end 0))))
1078 (org-lparse-insert 'LINE line)))))
1080 ;; Properly close all local lists and other lists
1081 (when (org-lparse-current-environment-p 'quote)
1082 (org-lparse-end-environment 'quote))
1084 (org-lparse-end-level 1 umax)
1086 ;; the </div> to close the last text-... div.
1087 (when (and (> umax 0) org-lparse-dyn-first-heading-pos)
1088 (org-lparse-end-outline-text-or-outline))
1090 (org-lparse-end 'DOCUMENT-BODY opt-plist)
1091 (unless body-only
1092 (org-lparse-end 'DOCUMENT-CONTENT))
1094 (unless (plist-get opt-plist :buffer-will-be-killed)
1095 (set-auto-mode t))
1097 (org-lparse-end 'EXPORT)
1099 (goto-char (point-min))
1100 (or (org-export-push-to-kill-ring
1101 (upcase (symbol-name org-lparse-backend)))
1102 (message "Exporting... done"))
1104 (cond
1105 ((not to-buffer)
1106 (let ((f (org-lparse-get 'SAVE-METHOD)))
1107 (or (and f (functionp f) (funcall f filename opt-plist))
1108 (save-buffer)))
1109 (or (when (and (boundp 'org-lparse-other-backend)
1110 org-lparse-other-backend
1111 (not (equal org-lparse-backend org-lparse-other-backend)))
1112 (let ((org-export-convert-process (org-lparse-get 'CONVERT-METHOD)))
1113 (when org-export-convert-process
1114 (org-export-convert buffer-file-name
1115 (symbol-name org-lparse-other-backend)))))
1116 (current-buffer)))
1117 ((eq to-buffer 'string)
1118 (prog1 (buffer-substring (point-min) (point-max))
1119 (kill-buffer (current-buffer))))
1120 (t (current-buffer))))))
1122 (defun org-lparse-format-table (lines olines)
1123 "Retuns backend-specific code for org-type and table-type
1124 tables."
1125 (if (stringp lines)
1126 (setq lines (org-split-string lines "\n")))
1127 (if (string-match "^[ \t]*|" (car lines))
1128 ;; A normal org table
1129 (org-lparse-format-org-table lines nil)
1130 ;; Table made by table.el
1131 (or (org-lparse-format-table-table-using-table-generate-source
1132 org-lparse-backend olines
1133 (not org-export-prefer-native-exporter-for-tables))
1134 ;; We are here only when table.el table has NO col or row
1135 ;; spanning and the user prefers using org's own converter for
1136 ;; exporting of such simple table.el tables.
1137 (org-lparse-format-table-table lines))))
1139 (defun org-lparse-table-get-colalign-info (lines)
1140 (let ((forced-aligns (org-find-text-property-in-string
1141 'org-forced-aligns (car lines))))
1142 (when (and forced-aligns org-table-clean-did-remove-column)
1143 (setq forced-aligns
1144 (mapcar (lambda (x) (cons (1- (car x)) (cdr x))) forced-aligns)))
1146 forced-aligns))
1148 (defvar org-lparse-table-style)
1149 (defvar org-lparse-table-ncols)
1150 (defvar org-lparse-table-rownum)
1151 (defvar org-lparse-table-is-styled)
1152 (defvar org-lparse-table-begin-marker)
1153 (defvar org-lparse-table-num-numeric-items-per-column)
1154 (defvar org-lparse-table-colalign-info)
1155 (defvar org-lparse-table-colalign-vector)
1157 ;; Following variables are defined in org-table.el
1158 (defvar org-table-number-fraction)
1159 (defvar org-table-number-regexp)
1161 (defun org-lparse-do-format-org-table (lines &optional splice)
1162 "Format a org-type table into backend-specific code.
1163 LINES is a list of lines. Optional argument SPLICE means, do not
1164 insert header and surrounding <table> tags, just format the lines.
1165 Optional argument NO-CSS means use XHTML attributes instead of CSS
1166 for formatting. This is required for the DocBook exporter."
1167 (require 'org-table)
1168 ;; Get rid of hlines at beginning and end
1169 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
1170 (setq lines (nreverse lines))
1171 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
1172 (setq lines (nreverse lines))
1173 (when org-export-table-remove-special-lines
1174 ;; Check if the table has a marking column. If yes remove the
1175 ;; column and the special lines
1176 (setq lines (org-table-clean-before-export lines)))
1178 (let* ((caption (org-find-text-property-in-string 'org-caption (car lines)))
1179 (caption (and caption (org-xml-encode-org-text caption)))
1180 (label (org-find-text-property-in-string 'org-label (car lines)))
1181 (org-lparse-table-colalign-info (org-lparse-table-get-colalign-info lines))
1182 (attributes (org-find-text-property-in-string 'org-attributes
1183 (car lines)))
1184 (head (and org-export-highlight-first-table-line
1185 (delq nil (mapcar
1186 (lambda (x) (string-match "^[ \t]*|-" x))
1187 (cdr lines)))))
1188 (org-lparse-table-rownum -1) org-lparse-table-ncols i (cnt 0)
1189 tbopen line fields
1190 org-lparse-table-cur-rowgrp-is-hdr
1191 org-lparse-table-rowgrp-open
1192 org-lparse-table-num-numeric-items-per-column
1193 org-lparse-table-colalign-vector n
1194 org-lparse-table-rowgrp-info
1195 org-lparse-table-begin-marker
1196 (org-lparse-table-style 'org-table)
1197 org-lparse-table-is-styled)
1198 (cond
1199 (splice
1200 (setq org-lparse-table-is-styled nil)
1201 (while (setq line (pop lines))
1202 (unless (string-match "^[ \t]*|-" line)
1203 (insert
1204 (org-lparse-format-table-row
1205 (org-split-string line "[ \t]*|[ \t]*")) "\n"))))
1207 (setq org-lparse-table-is-styled t)
1208 (org-lparse-begin 'TABLE caption label attributes)
1209 (setq org-lparse-table-begin-marker (point))
1210 (org-lparse-begin-table-rowgroup head)
1211 (while (setq line (pop lines))
1212 (cond
1213 ((string-match "^[ \t]*|-" line)
1214 (when lines (org-lparse-begin-table-rowgroup)))
1216 (insert
1217 (org-lparse-format-table-row
1218 (org-split-string line "[ \t]*|[ \t]*")) "\n"))))
1219 (org-lparse-end 'TABLE-ROWGROUP)
1220 (org-lparse-end-table)))))
1222 (defun org-lparse-format-org-table (lines &optional splice)
1223 (with-temp-buffer
1224 (org-lparse-do-format-org-table lines splice)
1225 (buffer-substring-no-properties (point-min) (point-max))))
1227 (defun org-lparse-do-format-table-table (lines)
1228 "Format a table generated by table.el into backend-specific code.
1229 This conversion does *not* use `table-generate-source' from table.el.
1230 This has the advantage that Org-mode's HTML conversions can be used.
1231 But it has the disadvantage, that no cell- or row-spanning is allowed."
1232 (let (line field-buffer
1233 (org-lparse-table-cur-rowgrp-is-hdr
1234 org-export-highlight-first-table-line)
1235 (caption nil)
1236 (attributes nil)
1237 (label nil)
1238 (org-lparse-table-style 'table-table)
1239 (org-lparse-table-is-styled nil)
1240 fields org-lparse-table-ncols i (org-lparse-table-rownum -1)
1241 (empty (org-lparse-format 'SPACES 1)))
1242 (org-lparse-begin 'TABLE caption label attributes)
1243 (while (setq line (pop lines))
1244 (cond
1245 ((string-match "^[ \t]*\\+-" line)
1246 (when field-buffer
1247 (let ((org-export-table-row-tags '("<tr>" . "</tr>"))
1248 ;; (org-export-html-table-use-header-tags-for-first-column nil)
1250 (insert (org-lparse-format-table-row field-buffer empty)))
1251 (setq org-lparse-table-cur-rowgrp-is-hdr nil)
1252 (setq field-buffer nil)))
1254 ;; Break the line into fields and store the fields
1255 (setq fields (org-split-string line "[ \t]*|[ \t]*"))
1256 (if field-buffer
1257 (setq field-buffer (mapcar
1258 (lambda (x)
1259 (concat x (org-lparse-format 'LINE-BREAK)
1260 (pop fields)))
1261 field-buffer))
1262 (setq field-buffer fields)))))
1263 (org-lparse-end-table)))
1265 (defun org-lparse-format-table-table (lines)
1266 (with-temp-buffer
1267 (org-lparse-do-format-table-table lines)
1268 (buffer-substring-no-properties (point-min) (point-max))))
1270 (defvar table-source-languages) ; defined in table.el
1271 (defun org-lparse-format-table-table-using-table-generate-source (backend
1272 lines
1273 &optional
1274 spanned-only)
1275 "Format a table into BACKEND, using `table-generate-source' from table.el.
1276 Use SPANNED-ONLY to suppress exporting of simple table.el tables.
1278 When SPANNED-ONLY is nil, all table.el tables are exported. When
1279 SPANNED-ONLY is non-nil, only tables with either row or column
1280 spans are exported.
1282 This routine returns the generated source or nil as appropriate.
1284 Refer docstring of `org-export-prefer-native-exporter-for-tables'
1285 for further information."
1286 (require 'table)
1287 (with-current-buffer (get-buffer-create " org-tmp1 ")
1288 (erase-buffer)
1289 (insert (mapconcat 'identity lines "\n"))
1290 (goto-char (point-min))
1291 (if (not (re-search-forward "|[^+]" nil t))
1292 (error "Error processing table"))
1293 (table-recognize-table)
1294 (when (or (not spanned-only)
1295 (let* ((dim (table-query-dimension))
1296 (c (nth 4 dim)) (r (nth 5 dim)) (cells (nth 6 dim)))
1297 (not (= (* c r) cells))))
1298 (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
1299 (cond
1300 ((member backend table-source-languages)
1301 (table-generate-source backend " org-tmp2 ")
1302 (set-buffer " org-tmp2 ")
1303 (buffer-substring (point-min) (point-max)))
1305 ;; table.el doesn't support the given backend. Currently this
1306 ;; happens in case of odt export. Strip the table from the
1307 ;; generated document. A better alternative would be to embed
1308 ;; the table as ascii text in the output document.
1309 (org-lparse-warn
1310 (concat
1311 "Found table.el-type table in the source org file. "
1312 (format "table.el doesn't support %s backend. "
1313 (upcase (symbol-name backend)))
1314 "Skipping ahead ..."))
1315 "")))))
1317 (defun org-lparse-handle-time-stamps (s)
1318 "Format time stamps in string S, or remove them."
1319 (catch 'exit
1320 (let (r b)
1321 (while (string-match org-maybe-keyword-time-regexp s)
1322 (or b (setq b (substring s 0 (match-beginning 0))))
1323 (setq r (concat
1324 r (substring s 0 (match-beginning 0))
1325 (org-lparse-format
1326 'FONTIFY
1327 (concat
1328 (if (match-end 1)
1329 (org-lparse-format
1330 'FONTIFY
1331 (match-string 1 s) "timestamp-kwd"))
1332 (org-lparse-format
1333 'FONTIFY
1334 (substring (org-translate-time (match-string 3 s)) 1 -1)
1335 "timestamp"))
1336 "timestamp-wrapper"))
1337 s (substring s (match-end 0))))
1338 ;; Line break if line started and ended with time stamp stuff
1339 (if (not r)
1341 (setq r (concat r s))
1342 (unless (string-match "\\S-" (concat b s))
1343 (setq r (concat r (org-lparse-format 'LINE-BREAK))))
1344 r))))
1346 (defun org-xml-encode-plain-text (s)
1347 "Convert plain text characters to HTML equivalent.
1348 Possible conversions are set in `org-export-html-protect-char-alist'."
1349 (let ((cl (org-lparse-get 'PLAIN-TEXT-MAP)) c)
1350 (while (setq c (pop cl))
1351 (let ((start 0))
1352 (while (string-match (car c) s start)
1353 (setq s (replace-match (cdr c) t t s)
1354 start (1+ (match-beginning 0))))))
1357 (defun org-xml-encode-org-text-skip-links (string)
1358 "Prepare STRING for HTML export. Apply all active conversions.
1359 If there are links in the string, don't modify these."
1360 (let* ((re (concat org-bracket-link-regexp "\\|"
1361 (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")))
1362 m s l res)
1363 (while (setq m (string-match re string))
1364 (setq s (substring string 0 m)
1365 l (match-string 0 string)
1366 string (substring string (match-end 0)))
1367 (push (org-xml-encode-org-text s) res)
1368 (push l res))
1369 (push (org-xml-encode-org-text string) res)
1370 (apply 'concat (nreverse res))))
1372 (defun org-xml-encode-org-text (s)
1373 "Apply all active conversions to translate special ASCII to HTML."
1374 (setq s (org-xml-encode-plain-text s))
1375 (if org-export-html-expand
1376 (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
1377 (setq s (replace-match "<\\1>" t nil s))))
1378 (if org-export-with-emphasize
1379 (setq s (org-lparse-apply-char-styles s)))
1380 (if org-export-with-special-strings
1381 (setq s (org-lparse-convert-special-strings s)))
1382 (if org-export-with-sub-superscripts
1383 (setq s (org-lparse-apply-sub-superscript-styles s)))
1384 (if org-export-with-TeX-macros
1385 (let ((start 0) wd rep)
1386 (while (setq start (string-match "\\\\\\([a-zA-Z]+[0-9]*\\)\\({}\\)?"
1387 s start))
1388 (if (get-text-property (match-beginning 0) 'org-protected s)
1389 (setq start (match-end 0))
1390 (setq wd (match-string 1 s))
1391 (if (setq rep (org-lparse-format 'ORG-ENTITY wd))
1392 (setq s (replace-match rep t t s))
1393 (setq start (+ start (length wd))))))))
1396 (defun org-lparse-convert-special-strings (string)
1397 "Convert special characters in STRING to HTML."
1398 (let ((all (org-lparse-get 'SPECIAL-STRING-REGEXPS))
1399 e a re rpl start)
1400 (while (setq a (pop all))
1401 (setq re (car a) rpl (cdr a) start 0)
1402 (while (string-match re string start)
1403 (if (get-text-property (match-beginning 0) 'org-protected string)
1404 (setq start (match-end 0))
1405 (setq string (replace-match rpl t nil string)))))
1406 string))
1408 (defun org-lparse-apply-sub-superscript-styles (string)
1409 "Apply subscript and superscript styles to STRING.
1410 Use `org-export-with-sub-superscripts' to control application of
1411 sub and superscript styles."
1412 (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
1413 (while (string-match org-match-substring-regexp string s)
1414 (cond
1415 ((and requireb (match-end 8)) (setq s (match-end 2)))
1416 ((get-text-property (match-beginning 2) 'org-protected string)
1417 (setq s (match-end 2)))
1419 (setq s (match-end 1)
1420 key (if (string= (match-string 2 string) "_")
1421 'subscript 'superscript)
1422 c (or (match-string 8 string)
1423 (match-string 6 string)
1424 (match-string 5 string))
1425 string (replace-match
1426 (concat (match-string 1 string)
1427 (org-lparse-format 'FONTIFY c key))
1428 t t string)))))
1429 (while (string-match "\\\\\\([_^]\\)" string)
1430 (setq string (replace-match (match-string 1 string) t t string)))
1431 string))
1433 (defvar org-lparse-char-styles
1434 `(("*" bold)
1435 ("/" emphasis)
1436 ("_" underline)
1437 ("=" code)
1438 ("~" verbatim)
1439 ("+" strike))
1440 "Map Org emphasis markers to char styles.
1441 This is an alist where each element is of the
1442 form (ORG-EMPHASIS-CHAR . CHAR-STYLE).")
1444 (defun org-lparse-apply-char-styles (string)
1445 "Apply char styles to STRING.
1446 The variable `org-lparse-char-styles' controls how the Org
1447 emphasis markers are interpreted."
1448 (let ((s 0) rpl)
1449 (while (string-match org-emph-re string s)
1450 (if (not (equal
1451 (substring string (match-beginning 3) (1+ (match-beginning 3)))
1452 (substring string (match-beginning 4) (1+ (match-beginning 4)))))
1453 (setq s (match-beginning 0)
1455 (concat
1456 (match-string 1 string)
1457 (org-lparse-format
1458 'FONTIFY (match-string 4 string)
1459 (nth 1 (assoc (match-string 3 string)
1460 org-lparse-char-styles)))
1461 (match-string 5 string))
1462 string (replace-match rpl t t string)
1463 s (+ s (- (length rpl) 2)))
1464 (setq s (1+ s))))
1465 string))
1467 (defun org-lparse-export-list-line (line pos struct prevs)
1468 "Insert list syntax in export buffer. Return LINE, maybe modified.
1470 POS is the item position or line position the line had before
1471 modifications to buffer. STRUCT is the list structure. PREVS is
1472 the alist of previous items."
1473 (let* ((get-type
1474 (function
1475 ;; Translate type of list containing POS to "d", "o" or
1476 ;; "u".
1477 (lambda (pos struct prevs)
1478 (let ((type (org-list-get-list-type pos struct prevs)))
1479 (cond
1480 ((eq 'ordered type) "o")
1481 ((eq 'descriptive type) "d")
1482 (t "u"))))))
1483 (get-closings
1484 (function
1485 ;; Return list of all items and sublists ending at POS, in
1486 ;; reverse order.
1487 (lambda (pos)
1488 (let (out)
1489 (catch 'exit
1490 (mapc (lambda (e)
1491 (let ((end (nth 6 e))
1492 (item (car e)))
1493 (cond
1494 ((= end pos) (push item out))
1495 ((>= item pos) (throw 'exit nil)))))
1496 struct))
1497 out)))))
1498 ;; First close any previous item, or list, ending at POS.
1499 (mapc (lambda (e)
1500 (let* ((lastp (= (org-list-get-last-item e struct prevs) e))
1501 (first-item (org-list-get-list-begin e struct prevs))
1502 (type (funcall get-type first-item struct prevs)))
1503 (org-lparse-end-paragraph)
1504 ;; Ending for every item
1505 (org-lparse-end-list-item type)
1506 ;; We're ending last item of the list: end list.
1507 (when lastp
1508 (org-lparse-end 'LIST type)
1509 (org-lparse-begin-paragraph))))
1510 (funcall get-closings pos))
1511 (cond
1512 ;; At an item: insert appropriate tags in export buffer.
1513 ((assq pos struct)
1514 (string-match
1515 (concat "[ \t]*\\(\\S-+[ \t]*\\)"
1516 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\]\\)?"
1517 "\\(?:\\(\\[[ X-]\\]\\)[ \t]+\\)?"
1518 "\\(?:\\(.*\\)[ \t]+::[ \t]+\\)?"
1519 "\\(.*\\)") line)
1520 (let* ((checkbox (match-string 3 line))
1521 (desc-tag (or (match-string 4 line) "???"))
1522 (body (or (match-string 5 line) ""))
1523 (list-beg (org-list-get-list-begin pos struct prevs))
1524 (firstp (= list-beg pos))
1525 ;; Always refer to first item to determine list type, in
1526 ;; case list is ill-formed.
1527 (type (funcall get-type list-beg struct prevs))
1528 (counter (let ((count-tmp (org-list-get-counter pos struct)))
1529 (cond
1530 ((not count-tmp) nil)
1531 ((string-match "[A-Za-z]" count-tmp)
1532 (- (string-to-char (upcase count-tmp)) 64))
1533 ((string-match "[0-9]+" count-tmp)
1534 count-tmp)))))
1535 (when firstp
1536 (org-lparse-end-paragraph)
1537 (org-lparse-begin 'LIST type))
1539 (let ((arg (cond ((equal type "d") desc-tag)
1540 ((equal type "o") counter))))
1541 (org-lparse-begin 'LIST-ITEM type arg))
1543 ;; If line had a checkbox, some additional modification is required.
1544 (when checkbox
1545 (setq body
1546 (concat
1547 (org-lparse-format
1548 'FONTIFY (concat
1550 (cond
1551 ((string-match "X" checkbox) "X")
1552 ((string-match " " checkbox)
1553 (org-lparse-format 'SPACES 1))
1554 (t "-"))
1555 "]")
1556 'code)
1558 body)))
1559 ;; Return modified line
1560 body))
1561 ;; At a list ender: go to next line (side-effects only).
1562 ((equal "ORG-LIST-END-MARKER" line) (throw 'nextline nil))
1563 ;; Not at an item: return line unchanged (side-effects only).
1564 (t line))))
1566 (defun org-lparse-bind-local-variables (opt-plist)
1567 (mapc (lambda (x)
1568 (set (make-local-variable (nth 2 x))
1569 (plist-get opt-plist (car x))))
1570 org-export-plist-vars))
1572 (defvar org-lparse-table-rowgrp-open)
1573 (defvar org-lparse-table-cur-rowgrp-is-hdr)
1574 (defvar org-lparse-footnote-number)
1575 (defvar org-lparse-footnote-definitions)
1576 (defvar org-lparse-footnote-buffer)
1577 (defvar org-lparse-output-buffer)
1579 (defcustom org-lparse-debug nil
1581 :group 'org-lparse
1582 :type 'boolean)
1584 (defun org-lparse-begin (entity &rest args)
1585 "Begin ENTITY in current buffer. ARGS is entity specific.
1586 ENTITY can be one of PARAGRAPH, LIST, LIST-ITEM etc.
1588 Use (org-lparse-begin 'LIST \"o\") to begin a list in current
1589 buffer.
1591 See `org-xhtml-entity-control-callbacks-alist' for more
1592 information."
1593 (when (and (member org-lparse-debug '(t control))
1594 (not (eq entity 'DOCUMENT-CONTENT)))
1595 (insert (org-lparse-format 'COMMENT "%s BEGIN %S" entity args)))
1597 (let ((f (cadr (assoc entity org-lparse-entity-control-callbacks-alist))))
1598 (unless f (error "Unknown entity: %s" entity))
1599 (apply f args)))
1601 (defun org-lparse-end (entity &rest args)
1602 "Close ENTITY in current buffer. ARGS is entity specific.
1603 ENTITY can be one of PARAGRAPH, LIST, LIST-ITEM
1604 etc.
1606 Use (org-lparse-end 'LIST \"o\") to close 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 END %S" entity args)))
1615 (let ((f (caddr (assoc entity org-lparse-entity-control-callbacks-alist))))
1616 (unless f (error "Unknown entity: %s" entity))
1617 (apply f args)))
1619 (defun org-lparse-begin-paragraph (&optional style)
1620 "Insert <p>, but first close previous paragraph if any."
1621 (org-lparse-end-paragraph)
1622 (org-lparse-begin 'PARAGRAPH style)
1623 (setq org-lparse-par-open t))
1625 (defun org-lparse-end-paragraph ()
1626 "Close paragraph if there is one open."
1627 (when org-lparse-par-open
1628 (org-lparse-end 'PARAGRAPH)
1629 (setq org-lparse-par-open nil)))
1631 (defun org-lparse-end-list-item (&optional type)
1632 "Close <li> if necessary."
1633 (org-lparse-end-paragraph)
1634 (org-lparse-end 'LIST-ITEM (or type "u")))
1636 (defvar org-lparse-dyn-current-environment nil)
1637 (defun org-lparse-begin-environment (style)
1638 (assert (not org-lparse-dyn-current-environment) t)
1639 (setq org-lparse-dyn-current-environment style)
1640 (org-lparse-begin 'ENVIRONMENT style))
1642 (defun org-lparse-end-environment (style)
1643 (org-lparse-end 'ENVIRONMENT style)
1645 (assert (eq org-lparse-dyn-current-environment style) t)
1646 (setq org-lparse-dyn-current-environment nil))
1648 (defun org-lparse-current-environment-p (style)
1649 (eq org-lparse-dyn-current-environment style))
1651 (defun org-lparse-begin-footnote-definition (n)
1652 (unless org-lparse-footnote-buffer
1653 (setq org-lparse-footnote-buffer
1654 (get-buffer-create "*Org HTML Export Footnotes*")))
1655 (set-buffer org-lparse-footnote-buffer)
1656 (erase-buffer)
1657 (setq org-lparse-insert-tag-with-newlines nil)
1658 (org-lparse-begin 'FOOTNOTE-DEFINITION n))
1660 (defun org-lparse-end-footnote-definition (n)
1661 (org-lparse-end 'FOOTNOTE-DEFINITION n)
1662 (setq org-lparse-insert-tag-with-newlines 'both)
1663 (push (cons n (buffer-string)) org-lparse-footnote-definitions)
1664 (set-buffer org-lparse-output-buffer))
1666 (defun org-lparse-format (entity &rest args)
1667 "Format ENTITY in backend-specific way and return it.
1668 ARGS is specific to entity being formatted.
1670 Use (org-lparse-format 'HEADING \"text\" 1) to format text as
1671 level 1 heading.
1673 See `org-xhtml-entity-format-callbacks-alist' for more information."
1674 (when (and (member org-lparse-debug '(t format))
1675 (not (equal entity 'COMMENT)))
1676 (insert (org-lparse-format 'COMMENT "%s: %S" entity args)))
1677 (cond
1678 ((consp entity)
1679 (let ((text (pop args)))
1680 (apply 'org-lparse-format 'TAGS entity text args)))
1682 (let ((f (cdr (assoc entity org-lparse-entity-format-callbacks-alist))))
1683 (unless f (error "Unknown entity: %s" entity))
1684 (apply f args)))))
1686 (defun org-lparse-insert (entity &rest args)
1687 (insert (apply 'org-lparse-format entity args)))
1689 (defun org-lparse-prepare-toc (lines level-offset opt-plist umax-toc)
1690 (let* ((quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
1691 (org-min-level (org-get-min-level lines level-offset))
1692 (org-last-level org-min-level)
1693 level)
1694 (with-temp-buffer
1695 (org-lparse-bind-local-variables opt-plist)
1696 (erase-buffer)
1697 (org-lparse-begin 'TOC (nth 3 (plist-get opt-plist :lang-words)))
1698 (setq
1699 lines
1700 (mapcar
1701 #'(lambda (line)
1702 (when (and (string-match org-todo-line-regexp line)
1703 (not (get-text-property 0 'org-protected line))
1704 (<= (setq level (org-tr-level
1705 (- (match-end 1) (match-beginning 1)
1706 level-offset)))
1707 umax-toc))
1708 (let ((txt (save-match-data
1709 (org-xml-encode-org-text-skip-links
1710 (org-export-cleanup-toc-line
1711 (match-string 3 line)))))
1712 (todo (and
1713 org-export-mark-todo-in-toc
1714 (or (and (match-beginning 2)
1715 (not (member (match-string 2 line)
1716 org-done-keywords)))
1717 (and (= level umax-toc)
1718 (org-search-todo-below
1719 line lines level)))))
1720 tags)
1721 ;; Check for targets
1722 (while (string-match org-any-target-regexp line)
1723 (setq line
1724 (replace-match
1725 (let ((org-lparse-encode-pending t))
1726 (org-lparse-format 'FONTIFY
1727 (match-string 1 line) "target"))
1728 t t line)))
1729 (when (string-match
1730 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
1731 (setq tags (match-string 1 txt)
1732 txt (replace-match "" t nil txt)))
1733 (when (string-match quote-re0 txt)
1734 (setq txt (replace-match "" t t txt)))
1735 (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
1736 (setq txt (replace-match "" t t txt)))
1737 (org-lparse-format
1738 'TOC-ITEM
1739 (let* ((snumber (org-section-number level))
1740 (href (replace-regexp-in-string
1741 "\\." "-" (format "sec-%s" snumber)))
1742 (href
1744 (cdr (assoc
1745 href org-export-preferred-target-alist))
1746 href))
1747 (href (org-solidify-link-text href)))
1748 (org-lparse-format 'TOC-ENTRY snumber todo txt tags href))
1749 level org-last-level)
1750 (setq org-last-level level)))
1751 line)
1752 lines))
1753 (org-lparse-end 'TOC)
1754 (setq org-lparse-toc (buffer-string))))
1755 lines)
1757 (defun org-lparse-format-table-row (fields &optional text-for-empty-fields)
1758 (unless org-lparse-table-ncols
1759 ;; first row of the table
1760 (setq org-lparse-table-ncols (length fields))
1761 (when org-lparse-table-is-styled
1762 (setq org-lparse-table-num-numeric-items-per-column
1763 (make-vector org-lparse-table-ncols 0))
1764 (setq org-lparse-table-colalign-vector
1765 (make-vector org-lparse-table-ncols nil))
1766 (let ((c -1))
1767 (while (< (incf c) org-lparse-table-ncols)
1768 (let ((cookie (cdr (assoc (1+ c) org-lparse-table-colalign-info))))
1769 (setf (aref org-lparse-table-colalign-vector c)
1770 (cond
1771 ((string= cookie "l") "left")
1772 ((string= cookie "r") "right")
1773 ((string= cookie "c") "center")
1774 (t nil))))))))
1775 (incf org-lparse-table-rownum)
1776 (let ((i -1))
1777 (org-lparse-format
1778 'TABLE-ROW
1779 (mapconcat
1780 (lambda (x)
1781 (when (and (string= x "") text-for-empty-fields)
1782 (setq x text-for-empty-fields))
1783 (incf i)
1784 (and org-lparse-table-is-styled
1785 (< i org-lparse-table-ncols)
1786 (string-match org-table-number-regexp x)
1787 (incf (aref org-lparse-table-num-numeric-items-per-column i)))
1788 (org-lparse-format 'TABLE-CELL x org-lparse-table-rownum i))
1789 fields "\n"))))
1791 (defun org-lparse-get (what &optional opt-plist)
1792 "Query for value of WHAT for the current backend `org-lparse-backend'.
1793 See also `org-lparse-backend-get'."
1794 (if (boundp 'org-lparse-backend)
1795 (org-lparse-backend-get (symbol-name org-lparse-backend) what opt-plist)
1796 (error "org-lparse-backend is not bound yet")))
1798 (defun org-lparse-backend-get (backend what &optional opt-plist)
1799 "Query BACKEND for value of WHAT.
1800 Dispatch the call to `org-<backend>-user-get'. If that throws an
1801 error, dispatch the call to `org-<backend>-get'. See
1802 `org-xhtml-get' for all known settings queried for by
1803 `org-lparse' during the course of export."
1804 (assert (stringp backend) t)
1805 (unless (org-lparse-backend-is-native-p backend)
1806 (error "Unknown native backend %s" backend))
1807 (let ((backend-get-method (intern (format "org-%s-get" backend)))
1808 (backend-user-get-method (intern (format "org-%s-user-get" backend))))
1809 (cond
1810 ((functionp backend-get-method)
1811 (condition-case nil
1812 (funcall backend-user-get-method what opt-plist)
1813 (error (funcall backend-get-method what opt-plist))))
1815 (error "Native backend %s doesn't define %s" backend backend-get-method)))))
1817 (defun org-lparse-insert-tag (tag &rest args)
1818 (when (member org-lparse-insert-tag-with-newlines '(lead both))
1819 (insert "\n"))
1820 (insert (apply 'format tag args))
1821 (when (member org-lparse-insert-tag-with-newlines '(trail both))
1822 (insert "\n")))
1824 (defun org-lparse-get-targets-from-title (title)
1825 (let* ((target (org-get-text-property-any 0 'target title))
1826 (extra-targets (assoc target org-export-target-aliases))
1827 (target (or (cdr (assoc target org-export-preferred-target-alist))
1828 target)))
1829 (cons target (remove target extra-targets))))
1831 (defun org-lparse-suffix-from-snumber (snumber)
1832 (let* ((snu (replace-regexp-in-string "\\." "-" snumber))
1833 (href (cdr (assoc (concat "sec-" snu)
1834 org-export-preferred-target-alist))))
1835 (org-solidify-link-text (or href snu))))
1837 (defun org-lparse-begin-level (level title umax head-count)
1838 "Insert a new LEVEL in HTML export.
1839 When TITLE is nil, just close all open levels."
1840 (org-lparse-end-level level umax)
1841 (unless title (error "Why is heading nil"))
1842 (let* ((targets (org-lparse-get-targets-from-title title))
1843 (target (car targets)) (extra-targets (cdr targets))
1844 (target (and target (org-solidify-link-text target)))
1845 (extra-class (org-get-text-property-any 0 'html-container-class title))
1846 snumber tags level1 class)
1847 (when (string-match (org-re "\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") title)
1848 (setq tags (and org-export-with-tags (match-string 1 title)))
1849 (setq title (replace-match "" t t title)))
1850 (if (> level umax)
1851 (progn
1852 (if (aref org-levels-open (1- level))
1853 (org-lparse-end-list-item)
1854 (aset org-levels-open (1- level) t)
1855 (org-lparse-end-paragraph)
1856 (org-lparse-begin 'LIST 'unordered))
1857 (org-lparse-begin
1858 'LIST-ITEM 'unordered target
1859 (org-lparse-format 'HEADLINE title extra-targets tags)))
1860 (aset org-levels-open (1- level) t)
1861 (setq snumber (org-section-number level))
1862 (setq level1 (+ level (or (org-lparse-get 'TOPLEVEL-HLEVEL) 1) -1))
1863 (unless (= head-count 1)
1864 (org-lparse-end-outline-text-or-outline))
1865 (org-lparse-begin-outline-and-outline-text
1866 level1 snumber title tags target extra-targets extra-class)
1867 (org-lparse-begin-paragraph))))
1869 (defun org-lparse-end-level (level umax)
1870 (org-lparse-end-paragraph)
1871 (loop for l from org-level-max downto level
1872 do (when (aref org-levels-open (1- l))
1873 ;; Terminate one level in HTML export
1874 (if (<= l umax)
1875 (org-lparse-end-outline-text-or-outline)
1876 (org-lparse-end-list-item)
1877 (org-lparse-end 'LIST 'unordered))
1878 (aset org-levels-open (1- l) nil))))
1880 (defvar org-lparse-outline-text-open)
1881 (defun org-lparse-begin-outline-and-outline-text (level1 snumber title tags
1882 target extra-targets
1883 extra-class)
1884 (org-lparse-begin
1885 'OUTLINE level1 snumber title tags target extra-targets extra-class)
1886 (org-lparse-begin-outline-text level1 snumber extra-class))
1888 (defun org-lparse-end-outline-text-or-outline ()
1889 (cond
1890 (org-lparse-outline-text-open
1891 (org-lparse-end 'OUTLINE-TEXT)
1892 (setq org-lparse-outline-text-open nil))
1893 (t (org-lparse-end 'OUTLINE))))
1895 (defun org-lparse-begin-outline-text (level1 snumber extra-class)
1896 (assert (not org-lparse-outline-text-open) t)
1897 (setq org-lparse-outline-text-open t)
1898 (org-lparse-begin 'OUTLINE-TEXT level1 snumber extra-class))
1900 (defun org-lparse-html-list-type-to-canonical-list-type (ltype)
1901 (cdr (assoc ltype '(("o" . ordered)
1902 ("u" . unordered)
1903 ("d" . description)))))
1905 (defvar org-lparse-table-rowgrp-info)
1906 (defun org-lparse-begin-table-rowgroup (&optional is-header-row)
1907 (push (cons (1+ org-lparse-table-rownum) :start) org-lparse-table-rowgrp-info)
1908 (org-lparse-begin 'TABLE-ROWGROUP is-header-row))
1910 (defun org-lparse-end-table ()
1911 (when org-lparse-table-is-styled
1912 ;; column groups
1913 (unless (car org-table-colgroup-info)
1914 (setq org-table-colgroup-info
1915 (cons :start (cdr org-table-colgroup-info))))
1917 ;; column alignment
1918 (let ((c -1))
1919 (mapc
1920 (lambda (x)
1921 (incf c)
1922 (setf (aref org-lparse-table-colalign-vector c)
1923 (or (aref org-lparse-table-colalign-vector c)
1924 (if (> (/ (float x) (1+ org-lparse-table-rownum))
1925 org-table-number-fraction)
1926 "right" "left"))))
1927 org-lparse-table-num-numeric-items-per-column)))
1928 (org-lparse-end 'TABLE))
1930 (defvar org-lparse-encode-pending nil)
1932 (defun org-lparse-format-tags (tag text prefix suffix &rest args)
1933 (cond
1934 ((consp tag)
1935 (concat prefix (apply 'format (car tag) args) text suffix
1936 (format (cdr tag))))
1937 ((stringp tag) ; singleton tag
1938 (concat prefix (apply 'format tag args) text))))
1940 (defun org-xml-fix-class-name (kwd) ; audit callers of this function
1941 "Turn todo keyword into a valid class name.
1942 Replaces invalid characters with \"_\"."
1943 (save-match-data
1944 (while (string-match "[^a-zA-Z0-9_]" kwd)
1945 (setq kwd (replace-match "_" t t kwd))))
1946 kwd)
1948 (defun org-lparse-format-todo (todo)
1949 (org-lparse-format 'FONTIFY
1950 (concat
1951 (ignore-errors (org-lparse-get 'TODO-KWD-CLASS-PREFIX))
1952 (org-xml-fix-class-name todo))
1953 (list (if (member todo org-done-keywords) "done" "todo")
1954 todo)))
1956 (defun org-lparse-format-extra-targets (extra-targets)
1957 (if (not extra-targets) ""
1958 (mapconcat (lambda (x)
1959 (setq x (org-solidify-link-text
1960 (if (org-uuidgen-p x) (concat "ID-" x) x)))
1961 (org-lparse-format 'ANCHOR "" x))
1962 extra-targets "")))
1964 (defun org-lparse-format-org-tags (tags)
1965 (if (not tags) ""
1966 (org-lparse-format
1967 'FONTIFY (mapconcat
1968 (lambda (x)
1969 (org-lparse-format
1970 'FONTIFY x
1971 (concat
1972 (ignore-errors (org-lparse-get 'TAG-CLASS-PREFIX))
1973 (org-xml-fix-class-name x))))
1974 (org-split-string tags ":")
1975 (org-lparse-format 'SPACES 1)) "tag")))
1977 (defun org-lparse-format-section-number (&optional snumber level)
1978 (and org-export-with-section-numbers
1979 (not org-lparse-body-only) snumber level
1980 (org-lparse-format 'FONTIFY snumber (format "section-number-%d" level))))
1982 (defun org-lparse-warn (msg)
1983 (put-text-property 0 (length msg) 'face 'font-lock-warning-face msg)
1984 (message msg)
1985 (sleep-for 3))
1987 (defun org-xml-format-href (s)
1988 "Make sure the S is valid as a href reference in an XHTML document."
1989 (save-match-data
1990 (let ((start 0))
1991 (while (string-match "&" s start)
1992 (setq start (+ (match-beginning 0) 3)
1993 s (replace-match "&amp;" t t s)))))
1996 (defun org-xml-format-desc (s)
1997 "Make sure the S is valid as a description in a link."
1998 (if (and s (not (get-text-property 1 'org-protected s)))
1999 (save-match-data
2000 (org-xml-encode-org-text s))
2003 (provide 'org-lparse)
2005 ;;; org-lparse.el ends here