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