babel: smarter `org-babel-ref-split-args' -- fixes bug parsing indexed function-style...
[rgr-org-mode.git] / lisp / org-docbook.el
blobab0a0862930df35babb9e55e996948529bde10ec
1 ;;; org-docbook.el --- DocBook exporter for org-mode
2 ;;
3 ;; Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
4 ;;
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: org-docbook.el
7 ;; Version: 6.34trans
8 ;; Author: Baoqiu Cui <cbaoqiu AT yahoo DOT com>
9 ;; Maintainer: Baoqiu Cui <cbaoqiu AT yahoo DOT com>
10 ;; Keywords: org, wp, docbook
11 ;; Description: Converts an org-mode buffer into DocBook
12 ;; URL:
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software: you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation, either version 3 of the License, or
19 ;; (at your option) any later version.
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; Commentary:
31 ;; This library implements a DocBook exporter for org-mode. The basic
32 ;; idea and design is very similar to what `org-export-as-html' has.
33 ;; Code prototype was also started with `org-export-as-html'.
35 ;; Put this file into your load-path and the following line into your
36 ;; ~/.emacs:
38 ;; (require 'org-docbook)
40 ;; The interactive functions are similar to those of the HTML and LaTeX
41 ;; exporters:
43 ;; M-x `org-export-as-docbook'
44 ;; M-x `org-export-as-docbook-pdf'
45 ;; M-x `org-export-as-docbook-pdf-and-open'
46 ;; M-x `org-export-as-docbook-batch'
47 ;; M-x `org-export-as-docbook-to-buffer'
48 ;; M-x `org-export-region-as-docbook'
49 ;; M-x `org-replace-region-by-docbook'
51 ;; Note that, in order to generate PDF files using the DocBook XML files
52 ;; created by DocBook exporter, the following two variables have to be
53 ;; set based on what DocBook tools you use for XSLT processor and XSL-FO
54 ;; processor:
56 ;; org-export-docbook-xslt-proc-command
57 ;; org-export-docbook-xsl-fo-proc-command
59 ;; Check the document of these two variables to see examples of how they
60 ;; can be set.
62 ;; If the Org file to be exported contains special characters written in
63 ;; TeX-like syntax, like \alpha and \beta, you need to include the right
64 ;; entity file(s) in the DOCTYPE declaration for the DocBook XML file.
65 ;; This is required to make the DocBook XML file valid. The DOCTYPE
66 ;; declaration string can be set using the following variable:
68 ;; org-export-docbook-doctype
70 ;;; Code:
72 (eval-when-compile
73 (require 'cl))
75 (require 'footnote)
76 (require 'org)
77 (require 'org-exp)
78 (require 'org-html)
80 ;;; Variables:
82 (defvar org-docbook-para-open nil)
83 (defvar org-export-docbook-inline-images t)
84 (defvar org-export-docbook-link-org-files-as-docbook nil)
86 (declare-function org-id-find-id-file "org-id" (id))
88 ;;; User variables:
90 (defgroup org-export-docbook nil
91 "Options for exporting Org-mode files to DocBook."
92 :tag "Org Export DocBook"
93 :group 'org-export)
95 (defcustom org-export-docbook-extension ".xml"
96 "Extension of DocBook XML files."
97 :group 'org-export-docbook
98 :type 'string)
100 (defcustom org-export-docbook-header "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
101 "Header of DocBook XML files."
102 :group 'org-export-docbook
103 :type 'string)
105 (defcustom org-export-docbook-doctype nil
106 "DOCTYPE declaration string for DocBook XML files.
107 This can be used to include entities that are needed to handle
108 special characters in Org files.
110 For example, if the Org file to be exported contains XHTML
111 entities, you can set this variable to:
113 \"<!DOCTYPE article [
114 <!ENTITY % xhtml1-symbol PUBLIC
115 \"-//W3C//ENTITIES Symbol for HTML//EN//XML\"
116 \"http://www.w3.org/2003/entities/2007/xhtml1-symbol.ent\"
118 %xhtml1-symbol;
122 If you want to process DocBook documents without an Internet
123 connection, it is suggested that you download the required entity
124 file(s) and use system identifier(s) (external files) in the
125 DOCTYPE declaration."
126 :group 'org-export-docbook
127 :type 'string)
129 (defcustom org-export-docbook-article-header "<article xmlns=\"http://docbook.org/ns/docbook\"
130 xmlns:xlink=\"http://www.w3.org/1999/xlink\" version=\"5.0\" xml:lang=\"en\">"
131 "Article header of DocBook XML files."
132 :group 'org-export-docbook
133 :type 'string)
135 (defcustom org-export-docbook-section-id-prefix "sec-"
136 "Prefix of section IDs used during exporting.
137 This can be set before exporting to avoid same set of section IDs
138 being used again and again, which can be a problem when multiple
139 people work on the same document."
140 :group 'org-export-docbook
141 :type 'string)
143 (defcustom org-export-docbook-footnote-id-prefix "fn-"
144 "The prefix of footnote IDs used during exporting. Like
145 `org-export-docbook-section-id-prefix', this variable can help
146 avoid same set of footnote IDs being used multiple times."
147 :group 'org-export-docbook
148 :type 'string)
150 (defcustom org-export-docbook-emphasis-alist
151 `(("*" "<emphasis role=\"bold\">" "</emphasis>")
152 ("/" "<emphasis>" "</emphasis>")
153 ("_" "<emphasis role=\"underline\">" "</emphasis>")
154 ("=" "<code>" "</code>")
155 ("~" "<literal>" "</literal>")
156 ("+" "<emphasis role=\"strikethrough\">" "</emphasis>"))
157 "Alist of DocBook expressions to convert emphasis fontifiers.
158 Each element of the list is a list of three elements.
159 The first element is the character used as a marker for fontification.
160 The second element is a formatting string to wrap fontified text with.
161 The third element decides whether to protect converted text from other
162 conversions."
163 :group 'org-export-docbook
164 :type 'alist)
166 (defcustom org-export-docbook-default-image-attributes
167 `(("align" . "\"center\"")
168 ("valign". "\"middle\""))
169 "Alist of default DocBook image attributes.
170 These attributes will be inserted into element <imagedata> by
171 default, but users can override them using `#+ATTR_DocBook:'."
172 :group 'org-export-docbook
173 :type 'alist)
175 (defcustom org-export-docbook-inline-image-extensions
176 '("jpeg" "jpg" "png" "gif" "svg")
177 "Extensions of image files that can be inlined into DocBook."
178 :group 'org-export-docbook
179 :type '(repeat (string :tag "Extension")))
181 (defcustom org-export-docbook-coding-system nil
182 "Coding system for DocBook XML files."
183 :group 'org-export-docbook
184 :type 'coding-system)
186 (defcustom org-export-docbook-xslt-proc-command nil
187 "XSLT processor command used by DocBook exporter.
188 This is the command used to process a DocBook XML file to
189 generate the formatting object (FO) file.
191 The value of this variable should be a format control string that
192 includes two `%s' arguments: the first one is for the output FO
193 file name, and the second one is for the input DocBook XML file
194 name.
196 For example, if you use Saxon as the XSLT processor, you may want
197 to set the variable to
199 \"java com.icl.saxon.StyleSheet -o %s %s /path/to/docbook.xsl\"
201 If you use Xalan, you can set it to
203 \"java org.apache.xalan.xslt.Process -out %s -in %s -xsl /path/to/docbook.xsl\"
205 For xsltproc, the following string should work:
207 \"xsltproc --output %s /path/to/docbook.xsl %s\"
209 You need to replace \"/path/to/docbook.xsl\" with the actual path
210 to the DocBook stylesheet file on your machine. You can also
211 replace it with your own customization layer if you have one.
213 You can include additional stylesheet parameters in this command.
214 Just make sure that they meet the syntax requirement of each
215 processor."
216 :group 'org-export-docbook
217 :type 'string)
219 (defcustom org-export-docbook-xsl-fo-proc-command nil
220 "XSL-FO processor command used by DocBook exporter.
221 This is the command used to process a formatting object (FO) file
222 to generate the PDF file.
224 The value of this variable should be a format control string that
225 includes two `%s' arguments: the first one is for the input FO
226 file name, and the second one is for the output PDF file name.
228 For example, if you use FOP as the XSL-FO processor, you can set
229 the variable to
231 \"fop %s %s\""
232 :group 'org-export-docbook
233 :type 'string)
235 (defcustom org-export-docbook-keywords-markup "<literal>%s</literal>"
236 "A printf format string to be applied to keywords by DocBook exporter."
237 :group 'org-export-docbook
238 :type 'string)
240 (defcustom org-export-docbook-timestamp-markup "<emphasis>%s</emphasis>"
241 "A printf format string to be applied to time stamps by DocBook exporter."
242 :group 'org-export-docbook
243 :type 'string)
245 ;;; Hooks
247 (defvar org-export-docbook-final-hook nil
248 "Hook run at the end of DocBook export, in the new buffer.")
250 ;;; Autoload functions:
252 ;;;###autoload
253 (defun org-export-as-docbook-batch ()
254 "Call `org-export-as-docbook' in batch style.
255 This function can be used in batch processing.
257 For example:
259 $ emacs --batch
260 --load=$HOME/lib/emacs/org.el
261 --visit=MyOrgFile.org --funcall org-export-as-docbook-batch"
262 (org-export-as-docbook 'hidden))
264 ;;;###autoload
265 (defun org-export-as-docbook-to-buffer ()
266 "Call `org-export-as-docbook' with output to a temporary buffer.
267 No file is created."
268 (interactive)
269 (org-export-as-docbook nil nil "*Org DocBook Export*")
270 (when org-export-show-temporary-export-buffer
271 (switch-to-buffer-other-window "*Org DocBook Export*")))
273 ;;;###autoload
274 (defun org-replace-region-by-docbook (beg end)
275 "Replace the region from BEG to END with its DocBook export.
276 It assumes the region has `org-mode' syntax, and then convert it to
277 DocBook. This can be used in any buffer. For example, you could
278 write an itemized list in `org-mode' syntax in an DocBook buffer and
279 then use this command to convert it."
280 (interactive "r")
281 (let (reg docbook buf)
282 (save-window-excursion
283 (if (org-mode-p)
284 (setq docbook (org-export-region-as-docbook
285 beg end t 'string))
286 (setq reg (buffer-substring beg end)
287 buf (get-buffer-create "*Org tmp*"))
288 (with-current-buffer buf
289 (erase-buffer)
290 (insert reg)
291 (org-mode)
292 (setq docbook (org-export-region-as-docbook
293 (point-min) (point-max) t 'string)))
294 (kill-buffer buf)))
295 (delete-region beg end)
296 (insert docbook)))
298 ;;;###autoload
299 (defun org-export-region-as-docbook (beg end &optional body-only buffer)
300 "Convert region from BEG to END in `org-mode' buffer to DocBook.
301 If prefix arg BODY-ONLY is set, omit file header and footer and
302 only produce the region of converted text, useful for
303 cut-and-paste operations. If BUFFER is a buffer or a string,
304 use/create that buffer as a target of the converted DocBook. If
305 BUFFER is the symbol `string', return the produced DocBook as a
306 string and leave not buffer behind. For example, a Lisp program
307 could call this function in the following way:
309 (setq docbook (org-export-region-as-docbook beg end t 'string))
311 When called interactively, the output buffer is selected, and shown
312 in a window. A non-interactive call will only return the buffer."
313 (interactive "r\nP")
314 (when (interactive-p)
315 (setq buffer "*Org DocBook Export*"))
316 (let ((transient-mark-mode t)
317 (zmacs-regions t)
318 rtn)
319 (goto-char end)
320 (set-mark (point)) ;; To activate the region
321 (goto-char beg)
322 (setq rtn (org-export-as-docbook
323 nil nil
324 buffer body-only))
325 (if (fboundp 'deactivate-mark) (deactivate-mark))
326 (if (and (interactive-p) (bufferp rtn))
327 (switch-to-buffer-other-window rtn)
328 rtn)))
330 ;;;###autoload
331 (defun org-export-as-docbook-pdf (&optional hidden ext-plist
332 to-buffer body-only pub-dir)
333 "Export as DocBook XML file, and generate PDF file."
334 (interactive "P")
335 (if (or (not org-export-docbook-xslt-proc-command)
336 (not (string-match "%s.+%s" org-export-docbook-xslt-proc-command)))
337 (error "XSLT processor command is not set correctly"))
338 (if (or (not org-export-docbook-xsl-fo-proc-command)
339 (not (string-match "%s.+%s" org-export-docbook-xsl-fo-proc-command)))
340 (error "XSL-FO processor command is not set correctly"))
341 (message "Exporting to PDF...")
342 (let* ((wconfig (current-window-configuration))
343 (docbook-buf (org-export-as-docbook hidden ext-plist
344 to-buffer body-only pub-dir))
345 (filename (buffer-file-name docbook-buf))
346 (base (file-name-sans-extension filename))
347 (fofile (concat base ".fo"))
348 (pdffile (concat base ".pdf")))
349 (and (file-exists-p pdffile) (delete-file pdffile))
350 (message "Processing DocBook XML file...")
351 (shell-command (format org-export-docbook-xslt-proc-command
352 fofile (shell-quote-argument filename)))
353 (shell-command (format org-export-docbook-xsl-fo-proc-command
354 fofile pdffile))
355 (message "Processing DocBook file...done")
356 (if (not (file-exists-p pdffile))
357 (error "PDF file was not produced")
358 (set-window-configuration wconfig)
359 (message "Exporting to PDF...done")
360 pdffile)))
362 ;;;###autoload
363 (defun org-export-as-docbook-pdf-and-open ()
364 "Export as DocBook XML file, generate PDF file, and open it."
365 (interactive)
366 (let ((pdffile (org-export-as-docbook-pdf)))
367 (if pdffile
368 (org-open-file pdffile)
369 (error "PDF file was not produced"))))
371 ;;;###autoload
372 (defun org-export-as-docbook (&optional hidden ext-plist
373 to-buffer body-only pub-dir)
374 "Export the current buffer as a DocBook file.
375 If there is an active region, export only the region. When
376 HIDDEN is obsolete and does nothing. EXT-PLIST is a
377 property list with external parameters overriding org-mode's
378 default settings, but still inferior to file-local settings.
379 When TO-BUFFER is non-nil, create a buffer with that name and
380 export to that buffer. If TO-BUFFER is the symbol `string',
381 don't leave any buffer behind but just return the resulting HTML
382 as a string. When BODY-ONLY is set, don't produce the file
383 header and footer, simply return the content of the document (all
384 top-level sections). When PUB-DIR is set, use this as the
385 publishing directory."
386 (interactive "P")
387 (run-hooks 'org-export-first-hook)
389 ;; Make sure we have a file name when we need it.
390 (when (and (not (or to-buffer body-only))
391 (not buffer-file-name))
392 (if (buffer-base-buffer)
393 (org-set-local 'buffer-file-name
394 (with-current-buffer (buffer-base-buffer)
395 buffer-file-name))
396 (error "Need a file name to be able to export")))
398 (message "Exporting...")
399 (setq-default org-todo-line-regexp org-todo-line-regexp)
400 (setq-default org-deadline-line-regexp org-deadline-line-regexp)
401 (setq-default org-done-keywords org-done-keywords)
402 (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
403 (let* ((opt-plist
404 (org-export-process-option-filters
405 (org-combine-plists (org-default-export-plist)
406 ext-plist
407 (org-infile-export-plist))))
408 (link-validate (plist-get opt-plist :link-validation-function))
409 valid
410 (odd org-odd-levels-only)
411 (region-p (org-region-active-p))
412 (rbeg (and region-p (region-beginning)))
413 (rend (and region-p (region-end)))
414 (subtree-p
415 (if (plist-get opt-plist :ignore-subtree-p)
417 (when region-p
418 (save-excursion
419 (goto-char rbeg)
420 (and (org-at-heading-p)
421 (>= (org-end-of-subtree t t) rend))))))
422 (level-offset (if subtree-p
423 (save-excursion
424 (goto-char rbeg)
425 (+ (funcall outline-level)
426 (if org-odd-levels-only 1 0)))
428 (opt-plist (setq org-export-opt-plist
429 (if subtree-p
430 (org-export-add-subtree-options opt-plist rbeg)
431 opt-plist)))
432 ;; The following two are dynamically scoped into other
433 ;; routines below.
434 (org-current-export-dir
435 (or pub-dir (org-export-directory :docbook opt-plist)))
436 (org-current-export-file buffer-file-name)
437 (level 0) (line "") (origline "") txt todo
438 (filename (if to-buffer nil
439 (expand-file-name
440 (concat
441 (file-name-sans-extension
442 (or (and subtree-p
443 (org-entry-get (region-beginning)
444 "EXPORT_FILE_NAME" t))
445 (file-name-nondirectory buffer-file-name)))
446 org-export-docbook-extension)
447 (file-name-as-directory
448 (or pub-dir (org-export-directory :docbook opt-plist))))))
449 (current-dir (if buffer-file-name
450 (file-name-directory buffer-file-name)
451 default-directory))
452 (buffer (if to-buffer
453 (cond
454 ((eq to-buffer 'string) (get-buffer-create "*Org DocBook Export*"))
455 (t (get-buffer-create to-buffer)))
456 (find-file-noselect filename)))
457 ;; org-levels-open is a global variable
458 (org-levels-open (make-vector org-level-max nil))
459 (date (plist-get opt-plist :date))
460 (author (or (plist-get opt-plist :author)
461 user-full-name))
462 (email (plist-get opt-plist :email))
463 firstname othername surname
464 (title (or (and subtree-p (org-export-get-title-from-subtree))
465 (plist-get opt-plist :title)
466 (and (not
467 (plist-get opt-plist :skip-before-1st-heading))
468 (org-export-grab-title-from-buffer))
469 (and buffer-file-name
470 (file-name-sans-extension
471 (file-name-nondirectory buffer-file-name)))
472 "UNTITLED"))
473 ;; We will use HTML table formatter to export tables to DocBook
474 ;; format, so need to set html-table-tag here.
475 (html-table-tag (plist-get opt-plist :html-table-tag))
476 (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
477 (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
478 (inquote nil)
479 (infixed nil)
480 (inverse nil)
481 (in-local-list nil)
482 (local-list-type nil)
483 (local-list-indent nil)
484 (llt org-plain-list-ordered-item-terminator)
485 (email (plist-get opt-plist :email))
486 (language (plist-get opt-plist :language))
487 (lang-words nil)
489 (start 0)
490 (coding-system (and (boundp 'buffer-file-coding-system)
491 buffer-file-coding-system))
492 (coding-system-for-write (or org-export-docbook-coding-system
493 coding-system))
494 (save-buffer-coding-system (or org-export-docbook-coding-system
495 coding-system))
496 (charset (and coding-system-for-write
497 (fboundp 'coding-system-get)
498 (coding-system-get coding-system-for-write
499 'mime-charset)))
500 (region
501 (buffer-substring
502 (if region-p (region-beginning) (point-min))
503 (if region-p (region-end) (point-max))))
504 (lines
505 (org-split-string
506 (org-export-preprocess-string
507 region
508 :emph-multiline t
509 :for-docbook t
510 :skip-before-1st-heading
511 (plist-get opt-plist :skip-before-1st-heading)
512 :drawers (plist-get opt-plist :drawers)
513 :todo-keywords (plist-get opt-plist :todo-keywords)
514 :tags (plist-get opt-plist :tags)
515 :priority (plist-get opt-plist :priority)
516 :footnotes (plist-get opt-plist :footnotes)
517 :timestamps (plist-get opt-plist :timestamps)
518 :archived-trees
519 (plist-get opt-plist :archived-trees)
520 :select-tags (plist-get opt-plist :select-tags)
521 :exclude-tags (plist-get opt-plist :exclude-tags)
522 :add-text
523 (plist-get opt-plist :text)
524 :LaTeX-fragments
525 (plist-get opt-plist :LaTeX-fragments))
526 "[\r\n]"))
527 ;; Use literal output to show check boxes.
528 (checkbox-start
529 (nth 1 (assoc "=" org-export-docbook-emphasis-alist)))
530 (checkbox-end
531 (nth 2 (assoc "=" org-export-docbook-emphasis-alist)))
532 table-open type
533 table-buffer table-orig-buffer
534 ind item-type starter didclose
535 rpl path attr caption label desc descp desc1 desc2 link
536 fnc item-tag
537 footref-seen footnote-list
538 id-file
541 ;; Fine detailed info about author name.
542 (if (string-match "\\([^ ]+\\) \\(.+ \\)?\\([^ ]+\\)" author)
543 (progn
544 (setq firstname (match-string 1 author)
545 othername (or (match-string 2 author) "")
546 surname (match-string 3 author))))
548 ;; Get all footnote text.
549 (setq footnote-list
550 (org-export-docbook-get-footnotes lines))
552 (let ((inhibit-read-only t))
553 (org-unmodified
554 (remove-text-properties (point-min) (point-max)
555 '(:org-license-to-kill t))))
557 (setq org-min-level (org-get-min-level lines level-offset))
558 (setq org-last-level org-min-level)
559 (org-init-section-numbers)
561 ;; Get and save the date.
562 (cond
563 ((and date (string-match "%" date))
564 (setq date (format-time-string date)))
565 (date)
566 (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
568 ;; Get the language-dependent settings
569 (setq lang-words (or (assoc language org-export-language-setup)
570 (assoc "en" org-export-language-setup)))
572 ;; Switch to the output buffer. Use fundamental-mode for now. We
573 ;; could turn on nXML mode later and do some indentation.
574 (set-buffer buffer)
575 (let ((inhibit-read-only t)) (erase-buffer))
576 (fundamental-mode)
577 (org-install-letbind)
579 (and (fboundp 'set-buffer-file-coding-system)
580 (set-buffer-file-coding-system coding-system-for-write))
582 ;; The main body...
583 (let ((case-fold-search nil)
584 (org-odd-levels-only odd))
586 ;; Create local variables for all options, to make sure all called
587 ;; functions get the correct information
588 (mapc (lambda (x)
589 (set (make-local-variable (nth 2 x))
590 (plist-get opt-plist (car x))))
591 org-export-plist-vars)
593 ;; Insert DocBook file header, title, and author info.
594 (unless body-only
595 (insert org-export-docbook-header)
596 (if org-export-docbook-doctype
597 (insert org-export-docbook-doctype))
598 (insert "<!-- Date: " date " -->\n")
599 (insert (format "<!-- DocBook XML file generated by Org-mode %s Emacs %s -->\n"
600 org-version emacs-major-version))
601 (insert org-export-docbook-article-header)
602 (insert (format
603 "\n <title>%s</title>
604 <info>
605 <author>
606 <personname>
607 <firstname>%s</firstname> <othername>%s</othername> <surname>%s</surname>
608 </personname>
610 </author>
611 </info>\n"
612 (org-docbook-expand title)
613 firstname othername surname
614 (if email (concat "<email>" email "</email>") "")
617 (org-init-section-numbers)
619 (org-export-docbook-open-para)
621 ;; Loop over all the lines...
622 (while (setq line (pop lines) origline line)
623 (catch 'nextline
625 ;; End of quote section?
626 (when (and inquote (string-match "^\\*+ " line))
627 (insert "]]>\n</programlisting>\n")
628 (org-export-docbook-open-para)
629 (setq inquote nil))
630 ;; Inside a quote section?
631 (when inquote
632 (insert (org-docbook-protect line) "\n")
633 (throw 'nextline nil))
635 ;; Fixed-width, verbatim lines (examples)
636 (when (and org-export-with-fixed-width
637 (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line))
638 (when (not infixed)
639 (setq infixed t)
640 (org-export-docbook-close-para-maybe)
641 (insert "<programlisting><![CDATA["))
642 (insert (match-string 3 line) "\n")
643 (when (or (not lines)
644 (not (string-match "^[ \t]*\\(:.*\\)"
645 (car lines))))
646 (setq infixed nil)
647 (insert "]]>\n</programlisting>\n")
648 (org-export-docbook-open-para))
649 (throw 'nextline nil))
651 (org-export-docbook-close-lists-maybe line)
653 ;; Protected HTML
654 (when (get-text-property 0 'org-protected line)
655 (let (par (ind (get-text-property 0 'original-indentation line)))
656 (when (re-search-backward
657 "\\(<para>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
658 (setq par (match-string 1))
659 (replace-match "\\2\n"))
660 (insert line "\n")
661 (while (and lines
662 (or (= (length (car lines)) 0)
663 (not ind)
664 (equal ind (get-text-property 0 'original-indentation (car lines))))
665 (or (= (length (car lines)) 0)
666 (get-text-property 0 'org-protected (car lines))))
667 (insert (pop lines) "\n"))
668 (and par (insert "<para>\n")))
669 (throw 'nextline nil))
671 ;; Start of block quotes and verses
672 (when (or (equal "ORG-BLOCKQUOTE-START" line)
673 (and (equal "ORG-VERSE-START" line)
674 (setq inverse t)))
675 (org-export-docbook-close-para-maybe)
676 (insert "<blockquote>")
677 ;; Check whether attribution for this blockquote exists.
678 (let (tmp1
679 attribution
680 (end (if inverse "ORG-VERSE-END" "ORG-BLOCKQUOTE-END"))
681 (quote-lines nil))
682 (while (and (setq tmp1 (pop lines))
683 (not (equal end tmp1)))
684 (push tmp1 quote-lines))
685 (push tmp1 lines) ; Put back quote end mark
686 ;; Check the last line in the quote to see if it contains
687 ;; the attribution.
688 (setq tmp1 (pop quote-lines))
689 (if (string-match "\\(^.*\\)\\(--[ \t]+\\)\\(.+\\)$" tmp1)
690 (progn
691 (setq attribution (match-string 3 tmp1))
692 (when (save-match-data
693 (string-match "[^ \t]" (match-string 1 tmp1)))
694 (push (match-string 1 tmp1) lines)))
695 (push tmp1 lines))
696 (while (setq tmp1 (pop quote-lines))
697 (push tmp1 lines))
698 (when attribution
699 (insert "<attribution>" attribution "</attribution>")))
700 ;; Insert <literallayout> for verse.
701 (if inverse
702 (insert "\n<literallayout>")
703 (org-export-docbook-open-para))
704 (throw 'nextline nil))
706 ;; End of block quotes
707 (when (equal "ORG-BLOCKQUOTE-END" line)
708 (org-export-docbook-close-para-maybe)
709 (insert "</blockquote>\n")
710 (org-export-docbook-open-para)
711 (throw 'nextline nil))
713 ;; End of verses
714 (when (equal "ORG-VERSE-END" line)
715 (insert "</literallayout>\n</blockquote>\n")
716 (org-export-docbook-open-para)
717 (setq inverse nil)
718 (throw 'nextline nil))
720 ;; Text centering. Element <para role="centered"> does not
721 ;; seem to work with FOP, so for now we use <informaltable> to
722 ;; center the text, which can contain multiple paragraphs.
723 (when (equal "ORG-CENTER-START" line)
724 (org-export-docbook-close-para-maybe)
725 (insert "<informaltable frame=\"none\" colsep=\"0\" rowsep=\"0\">\n"
726 "<tgroup align=\"center\" cols=\"1\">\n"
727 "<tbody><row><entry>\n")
728 (org-export-docbook-open-para)
729 (throw 'nextline nil))
731 (when (equal "ORG-CENTER-END" line)
732 (org-export-docbook-close-para-maybe)
733 (insert "</entry></row></tbody>\n"
734 "</tgroup>\n</informaltable>\n")
735 (org-export-docbook-open-para)
736 (throw 'nextline nil))
738 ;; Make targets to anchors. Note that currently FOP does not
739 ;; seem to support <anchor> tags when generating PDF output,
740 ;; but this can be used in DocBook --> HTML conversion.
741 (setq start 0)
742 (while (string-match
743 "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line start)
744 (cond
745 ((get-text-property (match-beginning 1) 'org-protected line)
746 (setq start (match-end 1)))
747 ((match-end 2)
748 (setq line (replace-match
749 (format "@<anchor xml:id=\"%s\"/>"
750 (org-solidify-link-text (match-string 1 line)))
751 t t line)))
753 (setq line (replace-match
754 (format "@<anchor xml:id=\"%s\"/>"
755 (org-solidify-link-text (match-string 1 line)))
756 t t line)))))
758 ;; Put time stamps and related keywords into special mark-up
759 ;; elements.
760 (setq line (org-export-docbook-handle-time-stamps line))
762 ;; Replace "&", "<" and ">" by "&amp;", "&lt;" and "&gt;".
763 ;; Handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>").
764 ;; Also handle sub_superscripts and check boxes.
765 (or (string-match org-table-hline-regexp line)
766 (setq line (org-docbook-expand line)))
768 ;; Format the links
769 (setq start 0)
770 (while (string-match org-bracket-link-analytic-regexp++ line start)
771 (setq start (match-beginning 0))
772 (setq path (save-match-data (org-link-unescape
773 (match-string 3 line))))
774 (setq type (cond
775 ((match-end 2) (match-string 2 line))
776 ((save-match-data
777 (or (file-name-absolute-p path)
778 (string-match "^\\.\\.?/" path)))
779 "file")
780 (t "internal")))
781 (setq path (org-extract-attributes (org-link-unescape path)))
782 (setq attr (get-text-property 0 'org-attributes path)
783 caption (get-text-property 0 'org-caption path)
784 label (get-text-property 0 'org-label path))
785 (setq desc1 (if (match-end 5) (match-string 5 line))
786 desc2 (if (match-end 2) (concat type ":" path) path)
787 descp (and desc1 (not (equal desc1 desc2)))
788 desc (or desc1 desc2))
789 ;; Make an image out of the description if that is so wanted
790 (when (and descp (org-file-image-p
791 desc org-export-docbook-inline-image-extensions))
792 (save-match-data
793 (if (string-match "^file:" desc)
794 (setq desc (substring desc (match-end 0))))))
795 ;; FIXME: do we need to unescape here somewhere?
796 (cond
797 ((equal type "internal")
798 (setq rpl (format "<link linkend=\"%s\">%s</link>"
799 (org-solidify-link-text
800 (save-match-data (org-link-unescape path)) nil)
801 (org-export-docbook-format-desc desc))))
802 ((and (equal type "id")
803 (setq id-file (org-id-find-id-file path)))
804 ;; This is an id: link to another file (if it was the same file,
805 ;; it would have become an internal link...)
806 (save-match-data
807 (setq id-file (file-relative-name
808 id-file (file-name-directory org-current-export-file)))
809 (setq id-file (concat (file-name-sans-extension id-file)
810 org-export-docbook-extension))
811 (setq rpl (format "<link xlink:href=\"%s#%s\">%s</link>"
812 id-file path (org-export-docbook-format-desc desc)))))
813 ((member type '("http" "https"))
814 ;; Standard URL, just check if we need to inline an image
815 (if (and (or (eq t org-export-docbook-inline-images)
816 (and org-export-docbook-inline-images (not descp)))
817 (org-file-image-p
818 path org-export-docbook-inline-image-extensions))
819 (setq rpl (org-export-docbook-format-image
820 (concat type ":" path)))
821 (setq link (concat type ":" path))
822 (setq rpl (format "<link xlink:href=\"%s\">%s</link>"
823 (org-export-html-format-href link)
824 (org-export-docbook-format-desc desc)))
826 ((member type '("ftp" "mailto" "news"))
827 ;; Standard URL
828 (setq link (concat type ":" path))
829 (setq rpl (format "<link xlink:href=\"%s\">%s</link>"
830 (org-export-html-format-href link)
831 (org-export-docbook-format-desc desc))))
832 ((string= type "coderef")
833 (setq rpl (format (org-export-get-coderef-format path (and descp desc))
834 (cdr (assoc path org-export-code-refs)))))
835 ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
836 ;; The link protocol has a function for format the link
837 (setq rpl
838 (save-match-data
839 (funcall fnc (org-link-unescape path) desc1 'html))))
841 ((string= type "file")
842 ;; FILE link
843 (let* ((filename path)
844 (abs-p (file-name-absolute-p filename))
845 thefile file-is-image-p search)
846 (save-match-data
847 (if (string-match "::\\(.*\\)" filename)
848 (setq search (match-string 1 filename)
849 filename (replace-match "" t nil filename)))
850 (setq valid
851 (if (functionp link-validate)
852 (funcall link-validate filename current-dir)
854 (setq file-is-image-p
855 (org-file-image-p
856 filename org-export-docbook-inline-image-extensions))
857 (setq thefile (if abs-p (expand-file-name filename) filename))
858 ;; Carry over the properties (expand-file-name will
859 ;; discard the properties of filename)
860 (add-text-properties 0 (1- (length thefile))
861 (list 'org-caption caption
862 'org-attributes attr
863 'org-label label)
864 thefile)
865 (when (and org-export-docbook-link-org-files-as-docbook
866 (string-match "\\.org$" thefile))
867 (setq thefile (concat (substring thefile 0
868 (match-beginning 0))
869 org-export-docbook-extension))
870 (if (and search
871 ;; make sure this is can be used as target search
872 (not (string-match "^[0-9]*$" search))
873 (not (string-match "^\\*" search))
874 (not (string-match "^/.*/$" search)))
875 (setq thefile (concat thefile "#"
876 (org-solidify-link-text
877 (org-link-unescape search)))))
878 (when (string-match "^file:" desc)
879 (setq desc (replace-match "" t t desc))
880 (if (string-match "\\.org$" desc)
881 (setq desc (replace-match "" t t desc))))))
882 (setq rpl (if (and file-is-image-p
883 (or (eq t org-export-docbook-inline-images)
884 (and org-export-docbook-inline-images
885 (not descp))))
886 (progn
887 (message "image %s %s" thefile org-docbook-para-open)
888 (org-export-docbook-format-image thefile))
889 (format "<link xlink:href=\"%s\">%s</link>"
890 thefile (org-export-docbook-format-desc desc))))
891 (if (not valid) (setq rpl desc))))
894 ;; Just publish the path, as default
895 (setq rpl (concat "&lt;" type ":"
896 (save-match-data (org-link-unescape path))
897 "&gt;"))))
898 (setq line (replace-match rpl t t line)
899 start (+ start (length rpl))))
901 ;; TODO items: can we do something better?!
902 (if (and (string-match org-todo-line-regexp line)
903 (match-beginning 2))
904 (setq line
905 (concat (substring line 0 (match-beginning 2))
906 "[" (match-string 2 line) "]"
907 (substring line (match-end 2)))))
909 ;; Does this contain a reference to a footnote?
910 (when org-export-with-footnotes
911 (setq start 0)
912 (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
913 (if (get-text-property (match-beginning 2) 'org-protected line)
914 (setq start (match-end 2))
915 (let ((num (match-string 2 line)))
916 (if (assoc num footref-seen)
917 (setq line (replace-match
918 (format "%s<footnoteref linkend=\"%s%s\"/>"
919 (match-string 1 line)
920 org-export-docbook-footnote-id-prefix num)
921 t t line))
922 (setq line (replace-match
923 (format "%s<footnote xml:id=\"%s%s\"><para>%s</para></footnote>"
924 (match-string 1 line)
925 org-export-docbook-footnote-id-prefix
927 (save-match-data
928 (org-docbook-expand
929 (cdr (assoc num footnote-list)))))
930 t t line))
931 (push (cons num 1) footref-seen))))))
933 (cond
934 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
935 ;; This is a headline
936 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
937 level-offset))
938 txt (match-string 2 line))
939 (if (string-match quote-re0 txt)
940 (setq txt (replace-match "" t t txt)))
941 (when in-local-list
942 ;; Close any local lists before inserting a new header line
943 (while local-list-type
944 (let ((listtype (car local-list-type)))
945 (org-export-docbook-close-li listtype)
946 (insert (cond
947 ((equal listtype "o") "</orderedlist>\n")
948 ((equal listtype "u") "</itemizedlist>\n")
949 ((equal listtype "d") "</variablelist>\n"))))
950 (pop local-list-type))
951 (setq local-list-indent nil
952 in-local-list nil))
953 (org-export-docbook-level-start level txt)
954 ;; QUOTES
955 (when (string-match quote-re line)
956 (org-export-docbook-close-para-maybe)
957 (insert "<programlisting><![CDATA[")
958 (setq inquote t)))
960 ;; Tables: since version 4.3 of DocBook DTD, HTML tables are
961 ;; supported. We can use existing HTML table exporter code
962 ;; here.
963 ((and org-export-with-tables
964 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
965 (if (not table-open)
966 ;; New table starts
967 (setq table-open t
968 table-buffer nil
969 table-orig-buffer nil))
970 ;; Accumulate lines
971 (setq table-buffer (cons line table-buffer)
972 table-orig-buffer (cons origline table-orig-buffer))
973 (when (or (not lines)
974 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
975 (car lines))))
976 (setq table-open nil
977 table-buffer (nreverse table-buffer)
978 table-orig-buffer (nreverse table-orig-buffer))
979 (org-export-docbook-close-para-maybe)
980 (insert (org-export-docbook-finalize-table
981 (org-format-table-html table-buffer table-orig-buffer)))))
983 ;; Normal lines
984 (when (string-match
985 (cond
986 ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
987 ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
988 ((= llt ?\)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
989 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
990 line)
991 (setq ind (or (get-text-property 0 'original-indentation line)
992 (org-get-string-indentation line))
993 item-type (if (match-beginning 4) "o" "u")
994 starter (if (match-beginning 2)
995 (substring (match-string 2 line) 0 -1))
996 line (substring line (match-beginning 5))
997 item-tag nil)
998 (if (and starter (string-match "\\(.*?\\) ::[ \t]*" line))
999 (setq item-type "d"
1000 item-tag (match-string 1 line)
1001 line (substring line (match-end 0))))
1002 (when (and (not (equal item-type "d"))
1003 (not (string-match "[^ \t]" line)))
1004 ;; Empty line. Pretend indentation is large.
1005 (setq ind (if org-empty-line-terminates-plain-lists
1007 (1+ (or (car local-list-indent) 1)))))
1008 (setq didclose nil)
1009 (while (and in-local-list
1010 (or (and (= ind (car local-list-indent))
1011 (not starter))
1012 (< ind (car local-list-indent))))
1013 (setq didclose t)
1014 (let ((listtype (car local-list-type)))
1015 (org-export-docbook-close-li listtype)
1016 (insert (cond
1017 ((equal listtype "o") "</orderedlist>\n")
1018 ((equal listtype "u") "</itemizedlist>\n")
1019 ((equal listtype "d") "</variablelist>\n"))))
1020 (pop local-list-type) (pop local-list-indent)
1021 (setq in-local-list local-list-indent))
1022 (cond
1023 ((and starter
1024 (or (not in-local-list)
1025 (> ind (car local-list-indent))))
1026 ;; Start new (level of) list
1027 (org-export-docbook-close-para-maybe)
1028 (insert (cond
1029 ((equal item-type "u") "<itemizedlist>\n<listitem>\n")
1030 ((equal item-type "o") "<orderedlist>\n<listitem>\n")
1031 ((equal item-type "d")
1032 (format "<variablelist>\n<varlistentry><term>%s</term><listitem>\n" item-tag))))
1033 ;; For DocBook, we need to open a para right after tag
1034 ;; <listitem>.
1035 (org-export-docbook-open-para)
1036 (push item-type local-list-type)
1037 (push ind local-list-indent)
1038 (setq in-local-list t))
1039 (starter
1040 ;; Continue current list
1041 (let ((listtype (car local-list-type)))
1042 (org-export-docbook-close-li listtype)
1043 (insert (cond
1044 ((equal listtype "o") "<listitem>")
1045 ((equal listtype "u") "<listitem>")
1046 ((equal listtype "d") (format
1047 "<varlistentry><term>%s</term><listitem>"
1048 (or item-tag
1049 "???"))))))
1050 ;; For DocBook, we need to open a para right after tag
1051 ;; <listitem>.
1052 (org-export-docbook-open-para))
1053 (didclose
1054 ;; We did close a list, normal text follows: need <para>
1055 (org-export-docbook-open-para)))
1056 ;; Checkboxes.
1057 (if (string-match "^[ \t]*\\(\\[[X -]\\]\\)" line)
1058 (setq line
1059 (replace-match (concat checkbox-start
1060 (match-string 1 line)
1061 checkbox-end)
1062 t t line))))
1064 ;; Empty lines start a new paragraph. If hand-formatted lists
1065 ;; are not fully interpreted, lines starting with "-", "+", "*"
1066 ;; also start a new paragraph.
1067 (if (and (string-match "^ [-+*]-\\|^[ \t]*$" line)
1068 (not inverse))
1069 (org-export-docbook-open-para))
1071 ;; Is this the start of a footnote?
1072 (when org-export-with-footnotes
1073 (when (and (boundp 'footnote-section-tag-regexp)
1074 (string-match (concat "^" footnote-section-tag-regexp)
1075 line))
1076 ;; ignore this line
1077 (throw 'nextline nil))
1078 ;; These footnote lines have been read and saved before,
1079 ;; ignore them at this time.
1080 (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
1081 (org-export-docbook-close-para-maybe)
1082 (throw 'nextline nil)))
1084 ;; FIXME: It might be a good idea to add an option to
1085 ;; support line break processing instruction <?linebreak?>.
1086 ;; Org-mode supports line break "\\" in HTML exporter, and
1087 ;; some DocBook users may also want to force line breaks
1088 ;; even though DocBook only supports that in
1089 ;; <literallayout>.
1091 (insert line "\n")))))
1093 ;; Properly close all local lists and other lists
1094 (when inquote
1095 (insert "]]>\n</programlisting>\n")
1096 (org-export-docbook-open-para))
1097 (when in-local-list
1098 ;; Close any local lists before inserting a new header line
1099 (while local-list-type
1100 (let ((listtype (car local-list-type)))
1101 (org-export-docbook-close-li listtype)
1102 (insert (cond
1103 ((equal listtype "o") "</orderedlist>\n")
1104 ((equal listtype "u") "</itemizedlist>\n")
1105 ((equal listtype "d") "</variablelist>\n"))))
1106 (pop local-list-type))
1107 (setq local-list-indent nil
1108 in-local-list nil))
1109 ;; Close all open sections.
1110 (org-export-docbook-level-start 1 nil)
1112 (unless (plist-get opt-plist :buffer-will-be-killed)
1113 (normal-mode)
1114 (if (eq major-mode (default-value 'major-mode))
1115 (nxml-mode)))
1117 ;; Remove empty paragraphs and lists. Replace them with a
1118 ;; newline.
1119 (goto-char (point-min))
1120 (while (re-search-forward
1121 "[ \r\n\t]*\\(<para>\\)[ \r\n\t]*</para>[ \r\n\t]*" nil t)
1122 (when (not (get-text-property (match-beginning 1) 'org-protected))
1123 (replace-match "\n")
1124 (backward-char 1)))
1125 ;; Fill empty sections with <para></para>. This is to make sure
1126 ;; that the DocBook document generated is valid and well-formed.
1127 (goto-char (point-min))
1128 (while (re-search-forward
1129 "</title>\\([ \r\n\t]*\\)</section>" nil t)
1130 (when (not (get-text-property (match-beginning 0) 'org-protected))
1131 (replace-match "\n<para></para>\n" nil nil nil 1)))
1132 ;; Insert the last closing tag.
1133 (goto-char (point-max))
1134 (unless body-only
1135 (insert "</article>"))
1136 (run-hooks 'org-export-docbook-final-hook)
1137 (or to-buffer (save-buffer))
1138 (goto-char (point-min))
1139 (or (org-export-push-to-kill-ring "DocBook")
1140 (message "Exporting... done"))
1141 (if (eq to-buffer 'string)
1142 (prog1 (buffer-substring (point-min) (point-max))
1143 (kill-buffer (current-buffer)))
1144 (current-buffer)))))
1146 (defun org-export-docbook-open-para ()
1147 "Insert <para>, but first close previous paragraph if any."
1148 (org-export-docbook-close-para-maybe)
1149 (insert "\n<para>")
1150 (setq org-docbook-para-open t))
1152 (defun org-export-docbook-close-para-maybe ()
1153 "Close DocBook paragraph if there is one open."
1154 (when org-docbook-para-open
1155 (insert "</para>\n")
1156 (setq org-docbook-para-open nil)))
1158 (defun org-export-docbook-close-li (&optional type)
1159 "Close list if necessary."
1160 (org-export-docbook-close-para-maybe)
1161 (if (equal type "d")
1162 (insert "</listitem></varlistentry>\n")
1163 (insert "</listitem>\n")))
1165 (defvar in-local-list)
1166 (defvar local-list-indent)
1167 (defvar local-list-type)
1168 (defun org-export-docbook-close-lists-maybe (line)
1169 (let ((ind (or (get-text-property 0 'original-indentation line)))
1170 ; (and (string-match "\\S-" line)
1171 ; (org-get-indentation line))))
1172 didclose)
1173 (when ind
1174 (while (and in-local-list
1175 (<= ind (car local-list-indent)))
1176 (setq didclose t)
1177 (let ((listtype (car local-list-type)))
1178 (org-export-docbook-close-li listtype)
1179 (insert (cond
1180 ((equal listtype "o") "</orderedlist>\n")
1181 ((equal listtype "u") "</itemizedlist>\n")
1182 ((equal listtype "d") "</variablelist>\n"))))
1183 (pop local-list-type) (pop local-list-indent)
1184 (setq in-local-list local-list-indent))
1185 (and didclose (org-export-docbook-open-para)))))
1187 (defun org-export-docbook-level-start (level title)
1188 "Insert a new level in DocBook export.
1189 When TITLE is nil, just close all open levels."
1190 (org-export-docbook-close-para-maybe)
1191 (let* ((target (and title (org-get-text-property-any 0 'target title)))
1192 (l org-level-max)
1193 section-number)
1194 (while (>= l level)
1195 (if (aref org-levels-open (1- l))
1196 (progn
1197 (insert "</section>\n")
1198 (aset org-levels-open (1- l) nil)))
1199 (setq l (1- l)))
1200 (when title
1201 ;; If title is nil, this means this function is called to close
1202 ;; all levels, so the rest is done only if title is given.
1204 ;; Format tags: put them into a superscript like format.
1205 (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
1206 (setq title
1207 (replace-match
1208 (if org-export-with-tags
1209 (save-match-data
1210 (concat
1211 "<superscript>"
1212 (match-string 1 title)
1213 "</superscript>"))
1215 t t title)))
1216 (aset org-levels-open (1- level) t)
1217 (setq section-number (org-section-number level))
1218 (insert (format "\n<section xml:id=\"%s%s\">\n<title>%s</title>"
1219 org-export-docbook-section-id-prefix
1220 section-number title))
1221 (org-export-docbook-open-para))))
1223 (defun org-docbook-expand (string)
1224 "Prepare STRING for DocBook export.
1225 Applies all active conversions. If there are links in the
1226 string, don't modify these."
1227 (let* ((re (concat org-bracket-link-regexp "\\|"
1228 (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
1229 m s l res)
1230 (while (setq m (string-match re string))
1231 (setq s (substring string 0 m)
1232 l (match-string 0 string)
1233 string (substring string (match-end 0)))
1234 (push (org-docbook-do-expand s) res)
1235 (push l res))
1236 (push (org-docbook-do-expand string) res)
1237 (apply 'concat (nreverse res))))
1239 (defun org-docbook-do-expand (s)
1240 "Apply all active conversions to translate special ASCII to DocBook."
1241 (setq s (org-html-protect s))
1242 (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
1243 (setq s (replace-match "<\\1>" t nil s)))
1244 (if org-export-with-emphasize
1245 (setq s (org-export-docbook-convert-emphasize s)))
1246 (if org-export-with-special-strings
1247 (setq s (org-export-docbook-convert-special-strings s)))
1248 (if org-export-with-sub-superscripts
1249 (setq s (org-export-docbook-convert-sub-super s)))
1250 (if org-export-with-TeX-macros
1251 (let ((start 0) wd ass)
1252 (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)\\({}\\)?"
1253 s start))
1254 (if (get-text-property (match-beginning 0) 'org-protected s)
1255 (setq start (match-end 0))
1256 (setq wd (match-string 1 s))
1257 (if (setq ass (assoc wd org-html-entities))
1258 (setq s (replace-match (or (cdr ass)
1259 (concat "&" (car ass) ";"))
1260 t t s))
1261 (setq start (+ start (length wd))))))))
1264 (defun org-export-docbook-format-desc (desc)
1265 "Make sure DESC is valid as a description in a link."
1266 (save-match-data
1267 (org-docbook-do-expand desc)))
1269 (defun org-export-docbook-convert-emphasize (string)
1270 "Apply emphasis for DocBook exporting."
1271 (let ((s 0) rpl)
1272 (while (string-match org-emph-re string s)
1273 (if (not (equal
1274 (substring string (match-beginning 3) (1+ (match-beginning 3)))
1275 (substring string (match-beginning 4) (1+ (match-beginning 4)))))
1276 (setq s (match-beginning 0)
1278 (concat
1279 (match-string 1 string)
1280 (nth 1 (assoc (match-string 3 string)
1281 org-export-docbook-emphasis-alist))
1282 (match-string 4 string)
1283 (nth 2 (assoc (match-string 3 string)
1284 org-export-docbook-emphasis-alist))
1285 (match-string 5 string))
1286 string (replace-match rpl t t string)
1287 s (+ s (- (length rpl) 2)))
1288 (setq s (1+ s))))
1289 string))
1291 (defun org-docbook-protect (string)
1292 (org-html-protect string))
1294 ;; For now, simply return string as it is.
1295 (defun org-export-docbook-convert-special-strings (string)
1296 "Convert special characters in STRING to DocBook."
1297 string)
1299 (defun org-export-docbook-get-footnotes (lines)
1300 "Given a list of LINES, return a list of alist footnotes."
1301 (let ((list nil) line)
1302 (while (setq line (pop lines))
1303 (if (string-match "^[ \t]*\\[\\([0-9]+\\)\\] \\(.+\\)" line)
1304 (push (cons (match-string 1 line) (match-string 2 line))
1305 list)))
1306 list))
1308 (defun org-export-docbook-format-image (src)
1309 "Create image element in DocBook."
1310 (save-match-data
1311 (let* ((caption (org-find-text-property-in-string 'org-caption src))
1312 (attr (or (org-find-text-property-in-string 'org-attributes src)
1313 ""))
1314 (label (org-find-text-property-in-string 'org-label src))
1315 (default-attr org-export-docbook-default-image-attributes)
1316 tmp)
1317 (while (setq tmp (pop default-attr))
1318 (if (not (string-match (concat (car tmp) "=") attr))
1319 (setq attr (concat attr " " (car tmp) "=" (cdr tmp)))))
1320 (format "<mediaobject%s>
1321 <imageobject>\n<imagedata fileref=\"%s\" %s/>\n</imageobject>
1322 %s</mediaobject>"
1323 (if label (concat " xml:id=\"" label "\"") "")
1324 src attr
1325 (if caption
1326 (concat "<caption>\n<para>"
1327 caption
1328 "</para>\n</caption>\n")
1330 ))))
1332 (defun org-export-docbook-preprocess (parameters)
1333 "Extra preprocessing work for DocBook export."
1334 ;; Merge lines starting with "\par" to one line. Such lines are
1335 ;; regarded as the continuation of a long footnote.
1336 (goto-char (point-min))
1337 (while (re-search-forward "\n\\(\\\\par\\>\\)" nil t)
1338 (if (not (get-text-property (match-beginning 1) 'org-protected))
1339 (replace-match ""))))
1341 (defun org-export-docbook-finalize-table (table)
1342 "Change TABLE to informaltable if caption does not exist.
1343 TABLE is a string containing the HTML code generated by
1344 `org-format-table-html' for a table in Org-mode buffer."
1345 (if (string-match
1346 "^<table \\(\\(.\\|\n\\)+\\)<caption></caption>\n\\(\\(.\\|\n\\)+\\)</table>"
1347 table)
1348 (replace-match (concat "<informaltable "
1349 (match-string 1 table)
1350 (match-string 3 table)
1351 "</informaltable>")
1352 nil nil table)
1353 table))
1355 ;; Note: This function is very similar to
1356 ;; org-export-html-convert-sub-super. They can be merged in the future.
1357 (defun org-export-docbook-convert-sub-super (string)
1358 "Convert sub- and superscripts in STRING for DocBook."
1359 (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
1360 (while (string-match org-match-substring-regexp string s)
1361 (cond
1362 ((and requireb (match-end 8)) (setq s (match-end 2)))
1363 ((get-text-property (match-beginning 2) 'org-protected string)
1364 (setq s (match-end 2)))
1366 (setq s (match-end 1)
1367 key (if (string= (match-string 2 string) "_")
1368 "subscript"
1369 "superscript")
1370 c (or (match-string 8 string)
1371 (match-string 6 string)
1372 (match-string 5 string))
1373 string (replace-match
1374 (concat (match-string 1 string)
1375 "<" key ">" c "</" key ">")
1376 t t string)))))
1377 (while (string-match "\\\\\\([_^]\\)" string)
1378 (setq string (replace-match (match-string 1 string) t t string)))
1379 string))
1381 (defun org-export-docbook-protect-tags (string)
1382 "Change ``<...>'' in string STRING into ``@<...>''.
1383 This is normally needed when STRING contains DocBook elements
1384 that need to be preserved in later phase of DocBook exporting."
1385 (let ((start 0))
1386 (while (string-match "<\\([^>]*\\)>" string start)
1387 (setq string (replace-match
1388 "@<\\1>" t nil string)
1389 start (match-end 0)))
1390 string))
1392 (defun org-export-docbook-handle-time-stamps (line)
1393 "Format time stamps in string LINE."
1394 (let (replaced
1395 (kw-markup (org-export-docbook-protect-tags
1396 org-export-docbook-keywords-markup))
1397 (ts-markup (org-export-docbook-protect-tags
1398 org-export-docbook-timestamp-markup)))
1399 (while (string-match org-maybe-keyword-time-regexp line)
1400 (setq replaced
1401 (concat replaced
1402 (substring line 0 (match-beginning 0))
1403 (if (match-end 1)
1404 (format kw-markup
1405 (match-string 1 line)))
1407 (format ts-markup
1408 (substring (org-translate-time
1409 (match-string 3 line)) 1 -1)))
1410 line (substring line (match-end 0))))
1411 (concat replaced line)))
1413 (provide 'org-docbook)
1415 ;; arch-tag: a24a127c-d365-4c2a-9e9b-f7dcb0ebfdc3
1416 ;;; org-docbook.el ends here