Release 6.30
[org-mode.git] / lisp / org-docbook.el
blob0f22b3dcb6ca09a9ff22093a7cfcd77e7a0c2a24
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.30
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 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 ;;; Autoload functions:
247 ;;;###autoload
248 (defun org-export-as-docbook-batch ()
249 "Call `org-export-as-docbook' in batch style.
250 This function can be used in batch processing.
252 For example:
254 $ emacs --batch
255 --load=$HOME/lib/emacs/org.el
256 --visit=MyOrgFile.org --funcall org-export-as-docbook-batch"
257 (org-export-as-docbook 'hidden))
259 ;;;###autoload
260 (defun org-export-as-docbook-to-buffer ()
261 "Call `org-export-as-docbook' with output to a temporary buffer.
262 No file is created."
263 (interactive)
264 (org-export-as-docbook nil nil "*Org DocBook Export*")
265 (when org-export-show-temporary-export-buffer
266 (switch-to-buffer-other-window "*Org DocBook Export*")))
268 ;;;###autoload
269 (defun org-replace-region-by-docbook (beg end)
270 "Replace the region from BEG to END with its DocBook export.
271 It assumes the region has `org-mode' syntax, and then convert it to
272 DocBook. This can be used in any buffer. For example, you could
273 write an itemized list in `org-mode' syntax in an DocBook buffer and
274 then use this command to convert it."
275 (interactive "r")
276 (let (reg docbook buf)
277 (save-window-excursion
278 (if (org-mode-p)
279 (setq docbook (org-export-region-as-docbook
280 beg end t 'string))
281 (setq reg (buffer-substring beg end)
282 buf (get-buffer-create "*Org tmp*"))
283 (save-excursion
284 (set-buffer buf)
285 (erase-buffer)
286 (insert reg)
287 (org-mode)
288 (setq docbook (org-export-region-as-docbook
289 (point-min) (point-max) t 'string)))
290 (kill-buffer buf)))
291 (delete-region beg end)
292 (insert docbook)))
294 ;;;###autoload
295 (defun org-export-region-as-docbook (beg end &optional body-only buffer)
296 "Convert region from BEG to END in `org-mode' buffer to DocBook.
297 If prefix arg BODY-ONLY is set, omit file header and footer and
298 only produce the region of converted text, useful for
299 cut-and-paste operations. If BUFFER is a buffer or a string,
300 use/create that buffer as a target of the converted DocBook. If
301 BUFFER is the symbol `string', return the produced DocBook as a
302 string and leave not buffer behind. For example, a Lisp program
303 could call this function in the following way:
305 (setq docbook (org-export-region-as-docbook beg end t 'string))
307 When called interactively, the output buffer is selected, and shown
308 in a window. A non-interactive call will only return the buffer."
309 (interactive "r\nP")
310 (when (interactive-p)
311 (setq buffer "*Org DocBook Export*"))
312 (let ((transient-mark-mode t)
313 (zmacs-regions t)
314 rtn)
315 (goto-char end)
316 (set-mark (point)) ;; To activate the region
317 (goto-char beg)
318 (setq rtn (org-export-as-docbook
319 nil nil
320 buffer body-only))
321 (if (fboundp 'deactivate-mark) (deactivate-mark))
322 (if (and (interactive-p) (bufferp rtn))
323 (switch-to-buffer-other-window rtn)
324 rtn)))
326 ;;;###autoload
327 (defun org-export-as-docbook-pdf (&optional hidden ext-plist
328 to-buffer body-only pub-dir)
329 "Export as DocBook XML file, and generate PDF file."
330 (interactive "P")
331 (if (or (not org-export-docbook-xslt-proc-command)
332 (not (string-match "%s.+%s" org-export-docbook-xslt-proc-command)))
333 (error "XSLT processor command is not set correctly"))
334 (if (or (not org-export-docbook-xsl-fo-proc-command)
335 (not (string-match "%s.+%s" org-export-docbook-xsl-fo-proc-command)))
336 (error "XSL-FO processor command is not set correctly"))
337 (message "Exporting to PDF...")
338 (let* ((wconfig (current-window-configuration))
339 (docbook-buf (org-export-as-docbook hidden ext-plist
340 to-buffer body-only pub-dir))
341 (filename (buffer-file-name docbook-buf))
342 (base (file-name-sans-extension filename))
343 (fofile (concat base ".fo"))
344 (pdffile (concat base ".pdf")))
345 (and (file-exists-p pdffile) (delete-file pdffile))
346 (message "Processing DocBook XML file...")
347 (shell-command (format org-export-docbook-xslt-proc-command
348 fofile (shell-quote-argument filename)))
349 (shell-command (format org-export-docbook-xsl-fo-proc-command
350 fofile pdffile))
351 (message "Processing DocBook file...done")
352 (if (not (file-exists-p pdffile))
353 (error "PDF file was not produced")
354 (set-window-configuration wconfig)
355 (message "Exporting to PDF...done")
356 pdffile)))
358 ;;;###autoload
359 (defun org-export-as-docbook-pdf-and-open ()
360 "Export as DocBook XML file, generate PDF file, and open it."
361 (interactive)
362 (let ((pdffile (org-export-as-docbook-pdf)))
363 (if pdffile
364 (org-open-file pdffile)
365 (error "PDF file was not produced"))))
367 ;;;###autoload
368 (defun org-export-as-docbook (&optional hidden ext-plist
369 to-buffer body-only pub-dir)
370 "Export the current buffer as a DocBook file.
371 If there is an active region, export only the region. When
372 HIDDEN is obsolete and does nothing. EXT-PLIST is a
373 property list with external parameters overriding org-mode's
374 default settings, but still inferior to file-local settings.
375 When TO-BUFFER is non-nil, create a buffer with that name and
376 export to that buffer. If TO-BUFFER is the symbol `string',
377 don't leave any buffer behind but just return the resulting HTML
378 as a string. When BODY-ONLY is set, don't produce the file
379 header and footer, simply return the content of the document (all
380 top-level sections). When PUB-DIR is set, use this as the
381 publishing directory."
382 (interactive "P")
383 ;; Make sure we have a file name when we need it.
384 (when (and (not (or to-buffer body-only))
385 (not buffer-file-name))
386 (if (buffer-base-buffer)
387 (org-set-local 'buffer-file-name
388 (with-current-buffer (buffer-base-buffer)
389 buffer-file-name))
390 (error "Need a file name to be able to export.")))
392 (message "Exporting...")
393 (setq-default org-todo-line-regexp org-todo-line-regexp)
394 (setq-default org-deadline-line-regexp org-deadline-line-regexp)
395 (setq-default org-done-keywords org-done-keywords)
396 (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
397 (let* ((opt-plist
398 (org-export-process-option-filters
399 (org-combine-plists (org-default-export-plist)
400 ext-plist
401 (org-infile-export-plist))))
402 (link-validate (plist-get opt-plist :link-validation-function))
403 valid
404 (odd org-odd-levels-only)
405 (region-p (org-region-active-p))
406 (rbeg (and region-p (region-beginning)))
407 (rend (and region-p (region-end)))
408 (subtree-p
409 (if (plist-get opt-plist :ignore-subree-p)
411 (when region-p
412 (save-excursion
413 (goto-char rbeg)
414 (and (org-at-heading-p)
415 (>= (org-end-of-subtree t t) rend))))))
416 (level-offset (if subtree-p
417 (save-excursion
418 (goto-char rbeg)
419 (+ (funcall outline-level)
420 (if org-odd-levels-only 1 0)))
422 (opt-plist (setq org-export-opt-plist
423 (if subtree-p
424 (org-export-add-subtree-options opt-plist rbeg)
425 opt-plist)))
426 ;; The following two are dynamically scoped into other
427 ;; routines below.
428 (org-current-export-dir
429 (or pub-dir (org-export-directory :docbook opt-plist)))
430 (org-current-export-file buffer-file-name)
431 (level 0) (line "") (origline "") txt todo
432 (filename (if to-buffer nil
433 (expand-file-name
434 (concat
435 (file-name-sans-extension
436 (or (and subtree-p
437 (org-entry-get (region-beginning)
438 "EXPORT_FILE_NAME" t))
439 (file-name-nondirectory buffer-file-name)))
440 org-export-docbook-extension)
441 (file-name-as-directory
442 (or pub-dir (org-export-directory :docbook opt-plist))))))
443 (current-dir (if buffer-file-name
444 (file-name-directory buffer-file-name)
445 default-directory))
446 (buffer (if to-buffer
447 (cond
448 ((eq to-buffer 'string) (get-buffer-create "*Org DocBook Export*"))
449 (t (get-buffer-create to-buffer)))
450 (find-file-noselect filename)))
451 ;; org-levels-open is a global variable
452 (org-levels-open (make-vector org-level-max nil))
453 (date (plist-get opt-plist :date))
454 (author (or (plist-get opt-plist :author)
455 user-full-name))
456 (email (plist-get opt-plist :email))
457 firstname othername surname
458 (title (or (and subtree-p (org-export-get-title-from-subtree))
459 (plist-get opt-plist :title)
460 (and (not
461 (plist-get opt-plist :skip-before-1st-heading))
462 (org-export-grab-title-from-buffer))
463 (and buffer-file-name
464 (file-name-sans-extension
465 (file-name-nondirectory buffer-file-name)))
466 "UNTITLED"))
467 ;; We will use HTML table formatter to export tables to DocBook
468 ;; format, so need to set html-table-tag here.
469 (html-table-tag (plist-get opt-plist :html-table-tag))
470 (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
471 (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
472 (inquote nil)
473 (infixed nil)
474 (inverse nil)
475 (in-local-list nil)
476 (local-list-type nil)
477 (local-list-indent nil)
478 (llt org-plain-list-ordered-item-terminator)
479 (email (plist-get opt-plist :email))
480 (language (plist-get opt-plist :language))
481 (lang-words nil)
483 (start 0)
484 (coding-system (and (boundp 'buffer-file-coding-system)
485 buffer-file-coding-system))
486 (coding-system-for-write (or org-export-docbook-coding-system
487 coding-system))
488 (save-buffer-coding-system (or org-export-docbook-coding-system
489 coding-system))
490 (charset (and coding-system-for-write
491 (fboundp 'coding-system-get)
492 (coding-system-get coding-system-for-write
493 'mime-charset)))
494 (region
495 (buffer-substring
496 (if region-p (region-beginning) (point-min))
497 (if region-p (region-end) (point-max))))
498 (lines
499 (org-split-string
500 (org-export-preprocess-string
501 region
502 :emph-multiline t
503 :for-docbook t
504 :skip-before-1st-heading
505 (plist-get opt-plist :skip-before-1st-heading)
506 :drawers (plist-get opt-plist :drawers)
507 :todo-keywords (plist-get opt-plist :todo-keywords)
508 :tags (plist-get opt-plist :tags)
509 :priority (plist-get opt-plist :priority)
510 :footnotes (plist-get opt-plist :footnotes)
511 :timestamps (plist-get opt-plist :timestamps)
512 :archived-trees
513 (plist-get opt-plist :archived-trees)
514 :select-tags (plist-get opt-plist :select-tags)
515 :exclude-tags (plist-get opt-plist :exclude-tags)
516 :add-text
517 (plist-get opt-plist :text)
518 :LaTeX-fragments
519 (plist-get opt-plist :LaTeX-fragments))
520 "[\r\n]"))
521 ;; Use literal output to show check boxes.
522 (checkbox-start
523 (nth 1 (assoc "=" org-export-docbook-emphasis-alist)))
524 (checkbox-end
525 (nth 2 (assoc "=" org-export-docbook-emphasis-alist)))
526 table-open type
527 table-buffer table-orig-buffer
528 ind item-type starter didclose
529 rpl path attr caption label desc descp desc1 desc2 link
530 fnc item-tag
531 footref-seen footnote-list
532 id-file
535 ;; Fine detailed info about author name.
536 (if (string-match "\\([^ ]+\\) \\(.+ \\)?\\([^ ]+\\)" author)
537 (progn
538 (setq firstname (match-string 1 author)
539 othername (or (match-string 2 author) "")
540 surname (match-string 3 author))))
542 ;; Get all footnote text.
543 (setq footnote-list
544 (org-export-docbook-get-footnotes lines))
546 (let ((inhibit-read-only t))
547 (org-unmodified
548 (remove-text-properties (point-min) (point-max)
549 '(:org-license-to-kill t))))
551 (setq org-min-level (org-get-min-level lines level-offset))
552 (setq org-last-level org-min-level)
553 (org-init-section-numbers)
555 ;; Get and save the date.
556 (cond
557 ((and date (string-match "%" date))
558 (setq date (format-time-string date)))
559 (date)
560 (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
562 ;; Get the language-dependent settings
563 (setq lang-words (or (assoc language org-export-language-setup)
564 (assoc "en" org-export-language-setup)))
566 ;; Switch to the output buffer. Use fundamental-mode for now. We
567 ;; could turn on nXML mode later and do some indentation.
568 (set-buffer buffer)
569 (let ((inhibit-read-only t)) (erase-buffer))
570 (fundamental-mode)
571 (org-install-letbind)
573 (and (fboundp 'set-buffer-file-coding-system)
574 (set-buffer-file-coding-system coding-system-for-write))
576 ;; The main body...
577 (let ((case-fold-search nil)
578 (org-odd-levels-only odd))
580 ;; Create local variables for all options, to make sure all called
581 ;; functions get the correct information
582 (mapc (lambda (x)
583 (set (make-local-variable (nth 2 x))
584 (plist-get opt-plist (car x))))
585 org-export-plist-vars)
587 ;; Insert DocBook file header, title, and author info.
588 (unless body-only
589 (insert org-export-docbook-header)
590 (if org-export-docbook-doctype
591 (insert org-export-docbook-doctype))
592 (insert "<!-- Date: " date " -->\n")
593 (insert (format "<!-- DocBook XML file generated by Org-mode %s Emacs %s -->\n"
594 org-version emacs-major-version))
595 (insert org-export-docbook-article-header)
596 (insert (format
597 "\n <title>%s</title>
598 <info>
599 <author>
600 <personname>
601 <firstname>%s</firstname> <othername>%s</othername> <surname>%s</surname>
602 </personname>
604 </author>
605 </info>\n"
606 (org-docbook-expand title)
607 firstname othername surname
608 (if email (concat "<email>" email "</email>") "")
611 (org-init-section-numbers)
613 (org-export-docbook-open-para)
615 ;; Loop over all the lines...
616 (while (setq line (pop lines) origline line)
617 (catch 'nextline
619 ;; End of quote section?
620 (when (and inquote (string-match "^\\*+ " line))
621 (insert "]]>\n</programlisting>\n")
622 (org-export-docbook-open-para)
623 (setq inquote nil))
624 ;; Inside a quote section?
625 (when inquote
626 (insert (org-docbook-protect line) "\n")
627 (throw 'nextline nil))
629 ;; Fixed-width, verbatim lines (examples)
630 (when (and org-export-with-fixed-width
631 (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line))
632 (when (not infixed)
633 (setq infixed t)
634 (org-export-docbook-close-para-maybe)
635 (insert "<programlisting><![CDATA["))
636 (insert (match-string 3 line) "\n")
637 (when (or (not lines)
638 (not (string-match "^[ \t]*\\(:.*\\)"
639 (car lines))))
640 (setq infixed nil)
641 (insert "]]>\n</programlisting>\n")
642 (org-export-docbook-open-para))
643 (throw 'nextline nil))
645 (org-export-docbook-close-lists-maybe line)
647 ;; Protected HTML
648 (when (get-text-property 0 'org-protected line)
649 (let (par (ind (get-text-property 0 'original-indentation line)))
650 (when (re-search-backward
651 "\\(<para>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
652 (setq par (match-string 1))
653 (replace-match "\\2\n"))
654 (insert line "\n")
655 (while (and lines
656 (or (= (length (car lines)) 0)
657 (not ind)
658 (equal ind (get-text-property 0 'original-indentation (car lines))))
659 (or (= (length (car lines)) 0)
660 (get-text-property 0 'org-protected (car lines))))
661 (insert (pop lines) "\n"))
662 (and par (insert "<para>\n")))
663 (throw 'nextline nil))
665 ;; Start of block quotes and verses
666 (when (or (equal "ORG-BLOCKQUOTE-START" line)
667 (and (equal "ORG-VERSE-START" line)
668 (setq inverse t)))
669 (org-export-docbook-close-para-maybe)
670 (insert "<blockquote>")
671 ;; Check whether attribution for this blockquote exists.
672 (let (tmp1
673 attribution
674 (end (if inverse "ORG-VERSE-END" "ORG-BLOCKQUOTE-END"))
675 (quote-lines nil))
676 (while (and (setq tmp1 (pop lines))
677 (not (equal end tmp1)))
678 (push tmp1 quote-lines))
679 (push tmp1 lines) ; Put back quote end mark
680 ;; Check the last line in the quote to see if it contains
681 ;; the attribution.
682 (setq tmp1 (pop quote-lines))
683 (if (string-match "\\(^.*\\)\\(--[ \t]+\\)\\(.+\\)$" tmp1)
684 (progn
685 (setq attribution (match-string 3 tmp1))
686 (when (save-match-data
687 (string-match "[^ \t]" (match-string 1 tmp1)))
688 (push (match-string 1 tmp1) lines)))
689 (push tmp1 lines))
690 (while (setq tmp1 (pop quote-lines))
691 (push tmp1 lines))
692 (when attribution
693 (insert "<attribution>" attribution "</attribution>")))
694 ;; Insert <literallayout> for verse.
695 (if inverse
696 (insert "\n<literallayout>")
697 (org-export-docbook-open-para))
698 (throw 'nextline nil))
700 ;; End of block quotes
701 (when (equal "ORG-BLOCKQUOTE-END" line)
702 (org-export-docbook-close-para-maybe)
703 (insert "</blockquote>\n")
704 (org-export-docbook-open-para)
705 (throw 'nextline nil))
707 ;; End of verses
708 (when (equal "ORG-VERSE-END" line)
709 (insert "</literallayout>\n</blockquote>\n")
710 (org-export-docbook-open-para)
711 (setq inverse nil)
712 (throw 'nextline nil))
714 ;; Text centering. Element <para role="centered"> does not
715 ;; seem to work with FOP, so for now we use <informaltable> to
716 ;; center the text, which can contain multiple paragraphs.
717 (when (equal "ORG-CENTER-START" line)
718 (org-export-docbook-close-para-maybe)
719 (insert "<informaltable frame=\"none\" colsep=\"0\" rowsep=\"0\">\n"
720 "<tgroup align=\"center\" cols=\"1\">\n"
721 "<tbody><row><entry>\n")
722 (org-export-docbook-open-para)
723 (throw 'nextline nil))
725 (when (equal "ORG-CENTER-END" line)
726 (org-export-docbook-close-para-maybe)
727 (insert "</entry></row></tbody>\n"
728 "</tgroup>\n</informaltable>\n")
729 (org-export-docbook-open-para)
730 (throw 'nextline nil))
732 ;; Make targets to anchors. Note that currently FOP does not
733 ;; seem to support <anchor> tags when generating PDF output,
734 ;; but this can be used in DocBook --> HTML conversion.
735 (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
736 (cond
737 ((match-end 2)
738 (setq line (replace-match
739 (format "@<anchor xml:id=\"%s\"/>"
740 (org-solidify-link-text (match-string 1 line)))
741 t t line)))
743 (setq line (replace-match
744 (format "@<anchor xml:id=\"%s\"/>"
745 (org-solidify-link-text (match-string 1 line)))
746 t t line)))))
748 ;; Put time stamps and related keywords into special mark-up
749 ;; elements.
750 (setq line (org-export-docbook-handle-time-stamps line))
752 ;; Replace "&", "<" and ">" by "&amp;", "&lt;" and "&gt;".
753 ;; Handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>").
754 ;; Also handle sub_superscripts and check boxes.
755 (or (string-match org-table-hline-regexp line)
756 (setq line (org-docbook-expand line)))
758 ;; Format the links
759 (setq start 0)
760 (while (string-match org-bracket-link-analytic-regexp++ line start)
761 (setq start (match-beginning 0))
762 (setq path (save-match-data (org-link-unescape
763 (match-string 3 line))))
764 (setq type (cond
765 ((match-end 2) (match-string 2 line))
766 ((save-match-data
767 (or (file-name-absolute-p path)
768 (string-match "^\\.\\.?/" path)))
769 "file")
770 (t "internal")))
771 (setq path (org-extract-attributes (org-link-unescape path)))
772 (setq attr (get-text-property 0 'org-attributes path)
773 caption (get-text-property 0 'org-caption path)
774 label (get-text-property 0 'org-label path))
775 (setq desc1 (if (match-end 5) (match-string 5 line))
776 desc2 (if (match-end 2) (concat type ":" path) path)
777 descp (and desc1 (not (equal desc1 desc2)))
778 desc (or desc1 desc2))
779 ;; Make an image out of the description if that is so wanted
780 (when (and descp (org-file-image-p
781 desc org-export-docbook-inline-image-extensions))
782 (save-match-data
783 (if (string-match "^file:" desc)
784 (setq desc (substring desc (match-end 0))))))
785 ;; FIXME: do we need to unescape here somewhere?
786 (cond
787 ((equal type "internal")
788 (setq rpl (format "<link linkend=\"%s\">%s</link>"
789 (org-solidify-link-text
790 (save-match-data (org-link-unescape path)) nil)
791 (org-export-docbook-format-desc desc))))
792 ((and (equal type "id")
793 (setq id-file (org-id-find-id-file path)))
794 ;; This is an id: link to another file (if it was the same file,
795 ;; it would have become an internal link...)
796 (save-match-data
797 (setq id-file (file-relative-name
798 id-file (file-name-directory org-current-export-file)))
799 (setq id-file (concat (file-name-sans-extension id-file)
800 org-export-docbook-extension))
801 (setq rpl (format "<link xlink:href=\"%s#%s\">%s</link>"
802 id-file path (org-export-docbook-format-desc desc)))))
803 ((member type '("http" "https"))
804 ;; Standard URL, just check if we need to inline an image
805 (if (and (or (eq t org-export-docbook-inline-images)
806 (and org-export-docbook-inline-images (not descp)))
807 (org-file-image-p
808 path org-export-docbook-inline-image-extensions))
809 (setq rpl (org-export-docbook-format-image
810 (concat type ":" path)))
811 (setq link (concat type ":" path))
812 (setq rpl (format "<link xlink:href=\"%s\">%s</link>"
813 (org-export-html-format-href link)
814 (org-export-docbook-format-desc desc)))
816 ((member type '("ftp" "mailto" "news"))
817 ;; Standard URL
818 (setq link (concat type ":" path))
819 (setq rpl (format "<link xlink:href=\"%s\">%s</link>"
820 (org-export-html-format-href link)
821 (org-export-docbook-format-desc desc))))
822 ((string= type "coderef")
823 (setq rpl (format (org-export-get-coderef-format path (and descp desc))
824 (cdr (assoc path org-export-code-refs)))))
825 ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
826 ;; The link protocol has a function for format the link
827 (setq rpl
828 (save-match-data
829 (funcall fnc (org-link-unescape path) desc1 'html))))
831 ((string= type "file")
832 ;; FILE link
833 (let* ((filename path)
834 (abs-p (file-name-absolute-p filename))
835 thefile file-is-image-p search)
836 (save-match-data
837 (if (string-match "::\\(.*\\)" filename)
838 (setq search (match-string 1 filename)
839 filename (replace-match "" t nil filename)))
840 (setq valid
841 (if (functionp link-validate)
842 (funcall link-validate filename current-dir)
844 (setq file-is-image-p
845 (org-file-image-p
846 filename org-export-docbook-inline-image-extensions))
847 (setq thefile (if abs-p (expand-file-name filename) filename))
848 ;; Carry over the properties (expand-file-name will
849 ;; discard the properties of filename)
850 (add-text-properties 0 (1- (length thefile))
851 (list 'org-caption caption
852 'org-attributes attr
853 'org-label label)
854 thefile)
855 (when (and org-export-docbook-link-org-files-as-docbook
856 (string-match "\\.org$" thefile))
857 (setq thefile (concat (substring thefile 0
858 (match-beginning 0))
859 org-export-docbook-extension))
860 (if (and search
861 ;; make sure this is can be used as target search
862 (not (string-match "^[0-9]*$" search))
863 (not (string-match "^\\*" search))
864 (not (string-match "^/.*/$" search)))
865 (setq thefile (concat thefile "#"
866 (org-solidify-link-text
867 (org-link-unescape search)))))
868 (when (string-match "^file:" desc)
869 (setq desc (replace-match "" t t desc))
870 (if (string-match "\\.org$" desc)
871 (setq desc (replace-match "" t t desc))))))
872 (setq rpl (if (and file-is-image-p
873 (or (eq t org-export-docbook-inline-images)
874 (and org-export-docbook-inline-images
875 (not descp))))
876 (progn
877 (message "image %s %s" thefile org-docbook-para-open)
878 (org-export-docbook-format-image thefile))
879 (format "<link xlink:href=\"%s\">%s</link>"
880 thefile (org-export-docbook-format-desc desc))))
881 (if (not valid) (setq rpl desc))))
884 ;; Just publish the path, as default
885 (setq rpl (concat "&lt;" type ":"
886 (save-match-data (org-link-unescape path))
887 "&gt;"))))
888 (setq line (replace-match rpl t t line)
889 start (+ start (length rpl))))
891 ;; TODO items: can we do something better?!
892 (if (and (string-match org-todo-line-regexp line)
893 (match-beginning 2))
894 (setq line
895 (concat (substring line 0 (match-beginning 2))
896 "[" (match-string 2 line) "]"
897 (substring line (match-end 2)))))
899 ;; Does this contain a reference to a footnote?
900 (when org-export-with-footnotes
901 (setq start 0)
902 (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
903 (if (get-text-property (match-beginning 2) 'org-protected line)
904 (setq start (match-end 2))
905 (let ((num (match-string 2 line)))
906 (if (assoc num footref-seen)
907 (setq line (replace-match
908 (format "%s<footnoteref linkend=\"%s%s\"/>"
909 (match-string 1 line)
910 org-export-docbook-footnote-id-prefix num)
911 t t line))
912 (setq line (replace-match
913 (format "%s<footnote xml:id=\"%s%s\"><para>%s</para></footnote>"
914 (match-string 1 line)
915 org-export-docbook-footnote-id-prefix
917 (save-match-data
918 (org-docbook-expand
919 (cdr (assoc num footnote-list)))))
920 t t line))
921 (push (cons num 1) footref-seen))))))
923 (cond
924 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
925 ;; This is a headline
926 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
927 level-offset))
928 txt (match-string 2 line))
929 (if (string-match quote-re0 txt)
930 (setq txt (replace-match "" t t txt)))
931 (when in-local-list
932 ;; Close any local lists before inserting a new header line
933 (while local-list-type
934 (let ((listtype (car local-list-type)))
935 (org-export-docbook-close-li listtype)
936 (insert (cond
937 ((equal listtype "o") "</orderedlist>\n")
938 ((equal listtype "u") "</itemizedlist>\n")
939 ((equal listtype "d") "</variablelist>\n"))))
940 (pop local-list-type))
941 (setq local-list-indent nil
942 in-local-list nil))
943 (org-export-docbook-level-start level txt)
944 ;; QUOTES
945 (when (string-match quote-re line)
946 (org-export-docbook-close-para-maybe)
947 (insert "<programlisting><![CDATA[")
948 (setq inquote t)))
950 ;; Tables: since version 4.3 of DocBook DTD, HTML tables are
951 ;; supported. We can use existing HTML table exporter code
952 ;; here.
953 ((and org-export-with-tables
954 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
955 (if (not table-open)
956 ;; New table starts
957 (setq table-open t
958 table-buffer nil
959 table-orig-buffer nil))
960 ;; Accumulate lines
961 (setq table-buffer (cons line table-buffer)
962 table-orig-buffer (cons origline table-orig-buffer))
963 (when (or (not lines)
964 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
965 (car lines))))
966 (setq table-open nil
967 table-buffer (nreverse table-buffer)
968 table-orig-buffer (nreverse table-orig-buffer))
969 (org-export-docbook-close-para-maybe)
970 (insert (org-export-docbook-finalize-table
971 (org-format-table-html table-buffer table-orig-buffer)))))
973 ;; Normal lines
974 (when (string-match
975 (cond
976 ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
977 ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
978 ((= llt ?\)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
979 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
980 line)
981 (setq ind (or (get-text-property 0 'original-indentation line)
982 (org-get-string-indentation line))
983 item-type (if (match-beginning 4) "o" "u")
984 starter (if (match-beginning 2)
985 (substring (match-string 2 line) 0 -1))
986 line (substring line (match-beginning 5))
987 item-tag nil)
988 (if (and starter (string-match "\\(.*?\\) ::[ \t]*" line))
989 (setq item-type "d"
990 item-tag (match-string 1 line)
991 line (substring line (match-end 0))))
992 (when (and (not (equal item-type "d"))
993 (not (string-match "[^ \t]" line)))
994 ;; Empty line. Pretend indentation is large.
995 (setq ind (if org-empty-line-terminates-plain-lists
997 (1+ (or (car local-list-indent) 1)))))
998 (setq didclose nil)
999 (while (and in-local-list
1000 (or (and (= ind (car local-list-indent))
1001 (not starter))
1002 (< ind (car local-list-indent))))
1003 (setq didclose t)
1004 (let ((listtype (car local-list-type)))
1005 (org-export-docbook-close-li listtype)
1006 (insert (cond
1007 ((equal listtype "o") "</orderedlist>\n")
1008 ((equal listtype "u") "</itemizedlist>\n")
1009 ((equal listtype "d") "</variablelist>\n"))))
1010 (pop local-list-type) (pop local-list-indent)
1011 (setq in-local-list local-list-indent))
1012 (cond
1013 ((and starter
1014 (or (not in-local-list)
1015 (> ind (car local-list-indent))))
1016 ;; Start new (level of) list
1017 (org-export-docbook-close-para-maybe)
1018 (insert (cond
1019 ((equal item-type "u") "<itemizedlist>\n<listitem>\n")
1020 ((equal item-type "o") "<orderedlist>\n<listitem>\n")
1021 ((equal item-type "d")
1022 (format "<variablelist>\n<varlistentry><term>%s</term><listitem>\n" item-tag))))
1023 ;; For DocBook, we need to open a para right after tag
1024 ;; <listitem>.
1025 (org-export-docbook-open-para)
1026 (push item-type local-list-type)
1027 (push ind local-list-indent)
1028 (setq in-local-list t))
1029 (starter
1030 ;; Continue current list
1031 (let ((listtype (car local-list-type)))
1032 (org-export-docbook-close-li listtype)
1033 (insert (cond
1034 ((equal listtype "o") "<listitem>")
1035 ((equal listtype "u") "<listitem>")
1036 ((equal listtype "d") (format
1037 "<varlistentry><term>%s</term><listitem>"
1038 (or item-tag
1039 "???"))))))
1040 ;; For DocBook, we need to open a para right after tag
1041 ;; <listitem>.
1042 (org-export-docbook-open-para))
1043 (didclose
1044 ;; We did close a list, normal text follows: need <para>
1045 (org-export-docbook-open-para)))
1046 ;; Checkboxes.
1047 (if (string-match "^[ \t]*\\(\\[[X -]\\]\\)" line)
1048 (setq line
1049 (replace-match (concat checkbox-start
1050 (match-string 1 line)
1051 checkbox-end)
1052 t t line))))
1054 ;; Empty lines start a new paragraph. If hand-formatted lists
1055 ;; are not fully interpreted, lines starting with "-", "+", "*"
1056 ;; also start a new paragraph.
1057 (if (and (string-match "^ [-+*]-\\|^[ \t]*$" line)
1058 (not inverse))
1059 (org-export-docbook-open-para))
1061 ;; Is this the start of a footnote?
1062 (when org-export-with-footnotes
1063 (when (and (boundp 'footnote-section-tag-regexp)
1064 (string-match (concat "^" footnote-section-tag-regexp)
1065 line))
1066 ;; ignore this line
1067 (throw 'nextline nil))
1068 ;; These footnote lines have been read and saved before,
1069 ;; ignore them at this time.
1070 (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
1071 (org-export-docbook-close-para-maybe)
1072 (throw 'nextline nil)))
1074 ;; FIXME: It might be a good idea to add an option to
1075 ;; support line break processing instruction <?linebreak?>.
1076 ;; Org-mode supports line break "\\" in HTML exporter, and
1077 ;; some DocBook users may also want to force line breaks
1078 ;; even though DocBook only supports that in
1079 ;; <literallayout>.
1081 (insert line "\n")))))
1083 ;; Properly close all local lists and other lists
1084 (when inquote
1085 (insert "]]>\n</programlisting>\n")
1086 (org-export-docbook-open-para))
1087 (when in-local-list
1088 ;; Close any local lists before inserting a new header line
1089 (while local-list-type
1090 (let ((listtype (car local-list-type)))
1091 (org-export-docbook-close-li listtype)
1092 (insert (cond
1093 ((equal listtype "o") "</orderedlist>\n")
1094 ((equal listtype "u") "</itemizedlist>\n")
1095 ((equal listtype "d") "</variablelist>\n"))))
1096 (pop local-list-type))
1097 (setq local-list-indent nil
1098 in-local-list nil))
1099 ;; Close all open sections.
1100 (org-export-docbook-level-start 1 nil)
1102 (unless (plist-get opt-plist :buffer-will-be-killed)
1103 (normal-mode)
1104 (if (eq major-mode default-major-mode)
1105 (nxml-mode)))
1107 ;; Remove empty paragraphs and lists. Replace them with a
1108 ;; newline.
1109 (goto-char (point-min))
1110 (while (re-search-forward
1111 "[ \r\n\t]*\\(<para>\\)[ \r\n\t]*</para>[ \r\n\t]*" nil t)
1112 (when (not (get-text-property (match-beginning 1) 'org-protected))
1113 (replace-match "\n")
1114 (backward-char 1)))
1115 ;; Fill empty sections with <para></para>. This is to make sure
1116 ;; that the DocBook document generated is valid and well-formed.
1117 (goto-char (point-min))
1118 (while (re-search-forward
1119 "</title>\\([ \r\n\t]*\\)</section>" nil t)
1120 (when (not (get-text-property (match-beginning 0) 'org-protected))
1121 (replace-match "\n<para></para>\n" nil nil nil 1)))
1122 ;; Insert the last closing tag.
1123 (goto-char (point-max))
1124 (unless body-only
1125 (insert "</article>"))
1126 (or to-buffer (save-buffer))
1127 (goto-char (point-min))
1128 (or (org-export-push-to-kill-ring "DocBook")
1129 (message "Exporting... done"))
1130 (if (eq to-buffer 'string)
1131 (prog1 (buffer-substring (point-min) (point-max))
1132 (kill-buffer (current-buffer)))
1133 (current-buffer)))))
1135 (defun org-export-docbook-open-para ()
1136 "Insert <para>, but first close previous paragraph if any."
1137 (org-export-docbook-close-para-maybe)
1138 (insert "\n<para>")
1139 (setq org-docbook-para-open t))
1141 (defun org-export-docbook-close-para-maybe ()
1142 "Close DocBook paragraph if there is one open."
1143 (when org-docbook-para-open
1144 (insert "</para>\n")
1145 (setq org-docbook-para-open nil)))
1147 (defun org-export-docbook-close-li (&optional type)
1148 "Close list if necessary."
1149 (org-export-docbook-close-para-maybe)
1150 (if (equal type "d")
1151 (insert "</listitem></varlistentry>\n")
1152 (insert "</listitem>\n")))
1154 (defvar in-local-list)
1155 (defvar local-list-indent)
1156 (defvar local-list-type)
1157 (defun org-export-docbook-close-lists-maybe (line)
1158 (let ((ind (or (get-text-property 0 'original-indentation line)))
1159 ; (and (string-match "\\S-" line)
1160 ; (org-get-indentation line))))
1161 didclose)
1162 (when ind
1163 (while (and in-local-list
1164 (<= ind (car local-list-indent)))
1165 (setq didclose t)
1166 (let ((listtype (car local-list-type)))
1167 (org-export-docbook-close-li listtype)
1168 (insert (cond
1169 ((equal listtype "o") "</orderedlist>\n")
1170 ((equal listtype "u") "</itemizedlist>\n")
1171 ((equal listtype "d") "</variablelist>\n"))))
1172 (pop local-list-type) (pop local-list-indent)
1173 (setq in-local-list local-list-indent))
1174 (and didclose (org-export-docbook-open-para)))))
1176 (defun org-export-docbook-level-start (level title)
1177 "Insert a new level in DocBook export.
1178 When TITLE is nil, just close all open levels."
1179 (org-export-docbook-close-para-maybe)
1180 (let* ((target (and title (org-get-text-property-any 0 'target title)))
1181 (l org-level-max)
1182 section-number)
1183 (while (>= l level)
1184 (if (aref org-levels-open (1- l))
1185 (progn
1186 (insert "</section>\n")
1187 (aset org-levels-open (1- l) nil)))
1188 (setq l (1- l)))
1189 (when title
1190 ;; If title is nil, this means this function is called to close
1191 ;; all levels, so the rest is done only if title is given.
1193 ;; Format tags: put them into a superscript like format.
1194 (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
1195 (setq title
1196 (replace-match
1197 (if org-export-with-tags
1198 (save-match-data
1199 (concat
1200 "<superscript>"
1201 (match-string 1 title)
1202 "</superscript>"))
1204 t t title)))
1205 (aset org-levels-open (1- level) t)
1206 (setq section-number (org-section-number level))
1207 (insert (format "\n<section xml:id=\"%s%s\">\n<title>%s</title>"
1208 org-export-docbook-section-id-prefix
1209 section-number title))
1210 (org-export-docbook-open-para))))
1212 (defun org-docbook-expand (string)
1213 "Prepare STRING for DocBook export.
1214 Applies all active conversions. If there are links in the
1215 string, don't modify these."
1216 (let* ((re (concat org-bracket-link-regexp "\\|"
1217 (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
1218 m s l res)
1219 (while (setq m (string-match re string))
1220 (setq s (substring string 0 m)
1221 l (match-string 0 string)
1222 string (substring string (match-end 0)))
1223 (push (org-docbook-do-expand s) res)
1224 (push l res))
1225 (push (org-docbook-do-expand string) res)
1226 (apply 'concat (nreverse res))))
1228 (defun org-docbook-do-expand (s)
1229 "Apply all active conversions to translate special ASCII to DocBook."
1230 (setq s (org-html-protect s))
1231 (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
1232 (setq s (replace-match "<\\1>" t nil s)))
1233 (if org-export-with-emphasize
1234 (setq s (org-export-docbook-convert-emphasize s)))
1235 (if org-export-with-special-strings
1236 (setq s (org-export-docbook-convert-special-strings s)))
1237 (if org-export-with-sub-superscripts
1238 (setq s (org-export-docbook-convert-sub-super s)))
1239 (if org-export-with-TeX-macros
1240 (let ((start 0) wd ass)
1241 (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)\\({}\\)?"
1242 s start))
1243 (if (get-text-property (match-beginning 0) 'org-protected s)
1244 (setq start (match-end 0))
1245 (setq wd (match-string 1 s))
1246 (if (setq ass (assoc wd org-html-entities))
1247 (setq s (replace-match (or (cdr ass)
1248 (concat "&" (car ass) ";"))
1249 t t s))
1250 (setq start (+ start (length wd))))))))
1253 (defun org-export-docbook-format-desc (desc)
1254 "Make sure DESC is valid as a description in a link."
1255 (save-match-data
1256 (org-docbook-do-expand desc)))
1258 (defun org-export-docbook-convert-emphasize (string)
1259 "Apply emphasis for DocBook exporting."
1260 (let ((s 0) rpl)
1261 (while (string-match org-emph-re string s)
1262 (if (not (equal
1263 (substring string (match-beginning 3) (1+ (match-beginning 3)))
1264 (substring string (match-beginning 4) (1+ (match-beginning 4)))))
1265 (setq s (match-beginning 0)
1267 (concat
1268 (match-string 1 string)
1269 (nth 1 (assoc (match-string 3 string)
1270 org-export-docbook-emphasis-alist))
1271 (match-string 4 string)
1272 (nth 2 (assoc (match-string 3 string)
1273 org-export-docbook-emphasis-alist))
1274 (match-string 5 string))
1275 string (replace-match rpl t t string)
1276 s (+ s (- (length rpl) 2)))
1277 (setq s (1+ s))))
1278 string))
1280 (defun org-docbook-protect (string)
1281 (org-html-protect string))
1283 ;; For now, simply return string as it is.
1284 (defun org-export-docbook-convert-special-strings (string)
1285 "Convert special characters in STRING to DocBook."
1286 string)
1288 (defun org-export-docbook-get-footnotes (lines)
1289 "Given a list of LINES, return a list of alist footnotes."
1290 (let ((list nil) line)
1291 (while (setq line (pop lines))
1292 (if (string-match "^[ \t]*\\[\\([0-9]+\\)\\] \\(.+\\)" line)
1293 (push (cons (match-string 1 line) (match-string 2 line))
1294 list)))
1295 list))
1297 (defun org-export-docbook-format-image (src)
1298 "Create image element in DocBook."
1299 (save-match-data
1300 (let* ((caption (org-find-text-property-in-string 'org-caption src))
1301 (attr (or (org-find-text-property-in-string 'org-attributes src)
1302 ""))
1303 (label (org-find-text-property-in-string 'org-label src))
1304 (default-attr org-export-docbook-default-image-attributes)
1305 tmp)
1306 (while (setq tmp (pop default-attr))
1307 (if (not (string-match (concat (car tmp) "=") attr))
1308 (setq attr (concat attr " " (car tmp) "=" (cdr tmp)))))
1309 (format "<mediaobject%s>
1310 <imageobject>\n<imagedata fileref=\"%s\" %s/>\n</imageobject>
1311 %s</mediaobject>"
1312 (if label (concat " xml:id=\"" label "\"") "")
1313 src attr
1314 (if caption
1315 (concat "<caption>\n<para>"
1316 caption
1317 "</para>\n</caption>\n")
1319 ))))
1321 (defun org-export-docbook-preprocess (parameters)
1322 "Extra preprocessing work for DocBook export."
1323 ;; Merge lines starting with "\par" to one line. Such lines are
1324 ;; regarded as the continuation of a long footnote.
1325 (goto-char (point-min))
1326 (while (re-search-forward "\n\\(\\\\par\\>\\)" nil t)
1327 (if (not (get-text-property (match-beginning 1) 'org-protected))
1328 (replace-match ""))))
1330 (defun org-export-docbook-finalize-table (table)
1331 "Change TABLE to informaltable if caption does not exist.
1332 TABLE is a string containing the HTML code generated by
1333 `org-format-table-html' for a table in Org-mode buffer."
1334 (if (string-match
1335 "^<table \\(\\(.\\|\n\\)+\\)<caption></caption>\n\\(\\(.\\|\n\\)+\\)</table>"
1336 table)
1337 (replace-match (concat "<informaltable "
1338 (match-string 1 table)
1339 (match-string 3 table)
1340 "</informaltable>")
1341 nil nil table)
1342 table))
1344 ;; Note: This function is very similar to
1345 ;; org-export-html-convert-sub-super. They can be merged in the future.
1346 (defun org-export-docbook-convert-sub-super (string)
1347 "Convert sub- and superscripts in STRING for DocBook."
1348 (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
1349 (while (string-match org-match-substring-regexp string s)
1350 (cond
1351 ((and requireb (match-end 8)) (setq s (match-end 2)))
1352 ((get-text-property (match-beginning 2) 'org-protected string)
1353 (setq s (match-end 2)))
1355 (setq s (match-end 1)
1356 key (if (string= (match-string 2 string) "_")
1357 "subscript"
1358 "superscript")
1359 c (or (match-string 8 string)
1360 (match-string 6 string)
1361 (match-string 5 string))
1362 string (replace-match
1363 (concat (match-string 1 string)
1364 "<" key ">" c "</" key ">")
1365 t t string)))))
1366 (while (string-match "\\\\\\([_^]\\)" string)
1367 (setq string (replace-match (match-string 1 string) t t string)))
1368 string))
1370 (defun org-export-docbook-protect-tags (string)
1371 "Change ``<...>'' in string STRING into ``@<...>''.
1372 This is normally needed when STRING contains DocBook elements
1373 that need to be preserved in later phase of DocBook exporting."
1374 (let ((start 0))
1375 (while (string-match "<\\([^>]*\\)>" string start)
1376 (setq string (replace-match
1377 "@<\\1>" t nil string)
1378 start (match-end 0)))
1379 string))
1381 (defun org-export-docbook-handle-time-stamps (line)
1382 "Format time stamps in string LINE."
1383 (let (replaced
1384 (kw-markup (org-export-docbook-protect-tags
1385 org-export-docbook-keywords-markup))
1386 (ts-markup (org-export-docbook-protect-tags
1387 org-export-docbook-timestamp-markup)))
1388 (while (string-match org-maybe-keyword-time-regexp line)
1389 (setq replaced
1390 (concat replaced
1391 (substring line 0 (match-beginning 0))
1392 (if (match-end 1)
1393 (format kw-markup
1394 (match-string 1 line)))
1396 (format ts-markup
1397 (substring (org-translate-time
1398 (match-string 3 line)) 1 -1)))
1399 line (substring line (match-end 0))))
1400 (concat replaced line)))
1402 (provide 'org-docbook)
1404 ;;; org-docbook.el ends here