Release 6.28e
[org-mode/org-tableheadings.git] / lisp / org-docbook.el
blobe1fd4e174c7f5fce01d0d5f9b70b6f57c2701b8b
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.28e
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 (= (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 (setq id-file (file-relative-name
797 id-file (file-name-directory org-current-export-file)))
798 (setq id-file (concat (file-name-sans-extension id-file)
799 org-export-docbook-extension))
800 (setq rpl (format "<link xlink:href=\"%s#%s\">%s</link>"
801 id-file path (org-export-docbook-format-desc desc))))
802 ((member type '("http" "https"))
803 ;; Standard URL, just check if we need to inline an image
804 (if (and (or (eq t org-export-docbook-inline-images)
805 (and org-export-docbook-inline-images (not descp)))
806 (org-file-image-p
807 path org-export-docbook-inline-image-extensions))
808 (setq rpl (org-export-docbook-format-image
809 (concat type ":" path)))
810 (setq link (concat type ":" path))
811 (setq rpl (format "<link xlink:href=\"%s\">%s</link>"
812 (org-export-html-format-href link)
813 (org-export-docbook-format-desc desc)))
815 ((member type '("ftp" "mailto" "news"))
816 ;; Standard URL
817 (setq link (concat type ":" path))
818 (setq rpl (format "<link xlink:href=\"%s\">%s</link>"
819 (org-export-html-format-href link)
820 (org-export-docbook-format-desc desc))))
821 ((string= type "coderef")
822 (setq rpl (format (org-export-get-coderef-format path (and descp desc))
823 (cdr (assoc path org-export-code-refs)))))
824 ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
825 ;; The link protocol has a function for format the link
826 (setq rpl
827 (save-match-data
828 (funcall fnc (org-link-unescape path) desc1 'html))))
830 ((string= type "file")
831 ;; FILE link
832 (let* ((filename path)
833 (abs-p (file-name-absolute-p filename))
834 thefile file-is-image-p search)
835 (save-match-data
836 (if (string-match "::\\(.*\\)" filename)
837 (setq search (match-string 1 filename)
838 filename (replace-match "" t nil filename)))
839 (setq valid
840 (if (functionp link-validate)
841 (funcall link-validate filename current-dir)
843 (setq file-is-image-p
844 (org-file-image-p
845 filename org-export-docbook-inline-image-extensions))
846 (setq thefile (if abs-p (expand-file-name filename) filename))
847 ;; Carry over the properties (expand-file-name will
848 ;; discard the properties of filename)
849 (add-text-properties 0 (1- (length thefile))
850 (list 'org-caption caption
851 'org-attributes attr
852 'org-label label)
853 thefile)
854 (when (and org-export-docbook-link-org-files-as-docbook
855 (string-match "\\.org$" thefile))
856 (setq thefile (concat (substring thefile 0
857 (match-beginning 0))
858 org-export-docbook-extension))
859 (if (and search
860 ;; make sure this is can be used as target search
861 (not (string-match "^[0-9]*$" search))
862 (not (string-match "^\\*" search))
863 (not (string-match "^/.*/$" search)))
864 (setq thefile (concat thefile "#"
865 (org-solidify-link-text
866 (org-link-unescape search)))))
867 (when (string-match "^file:" desc)
868 (setq desc (replace-match "" t t desc))
869 (if (string-match "\\.org$" desc)
870 (setq desc (replace-match "" t t desc))))))
871 (setq rpl (if (and file-is-image-p
872 (or (eq t org-export-docbook-inline-images)
873 (and org-export-docbook-inline-images
874 (not descp))))
875 (progn
876 (message "image %s %s" thefile org-docbook-para-open)
877 (org-export-docbook-format-image thefile))
878 (format "<link xlink:href=\"%s\">%s</link>"
879 thefile (org-export-docbook-format-desc desc))))
880 (if (not valid) (setq rpl desc))))
883 ;; Just publish the path, as default
884 (setq rpl (concat "&lt;" type ":"
885 (save-match-data (org-link-unescape path))
886 "&gt;"))))
887 (setq line (replace-match rpl t t line)
888 start (+ start (length rpl))))
890 ;; TODO items: can we do something better?!
891 (if (and (string-match org-todo-line-regexp line)
892 (match-beginning 2))
893 (setq line
894 (concat (substring line 0 (match-beginning 2))
895 "[" (match-string 2 line) "]"
896 (substring line (match-end 2)))))
898 ;; Does this contain a reference to a footnote?
899 (when org-export-with-footnotes
900 (setq start 0)
901 (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
902 (if (get-text-property (match-beginning 2) 'org-protected line)
903 (setq start (match-end 2))
904 (let ((num (match-string 2 line)))
905 (if (assoc num footref-seen)
906 (setq line (replace-match
907 (format "%s<footnoteref linkend=\"%s%s\"/>"
908 (match-string 1 line)
909 org-export-docbook-footnote-id-prefix num)
910 t t line))
911 (setq line (replace-match
912 (format "%s<footnote xml:id=\"%s%s\"><para>%s</para></footnote>"
913 (match-string 1 line)
914 org-export-docbook-footnote-id-prefix
916 (save-match-data
917 (org-docbook-expand
918 (cdr (assoc num footnote-list)))))
919 t t line))
920 (push (cons num 1) footref-seen))))))
922 (cond
923 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
924 ;; This is a headline
925 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
926 level-offset))
927 txt (match-string 2 line))
928 (if (string-match quote-re0 txt)
929 (setq txt (replace-match "" t t txt)))
930 (when in-local-list
931 ;; Close any local lists before inserting a new header line
932 (while local-list-type
933 (let ((listtype (car local-list-type)))
934 (org-export-docbook-close-li listtype)
935 (insert (cond
936 ((equal listtype "o") "</orderedlist>\n")
937 ((equal listtype "u") "</itemizedlist>\n")
938 ((equal listtype "d") "</variablelist>\n"))))
939 (pop local-list-type))
940 (setq local-list-indent nil
941 in-local-list nil))
942 (org-export-docbook-level-start level txt)
943 ;; QUOTES
944 (when (string-match quote-re line)
945 (org-export-docbook-close-para-maybe)
946 (insert "<programlisting><![CDATA[")
947 (setq inquote t)))
949 ;; Tables: since version 4.3 of DocBook DTD, HTML tables are
950 ;; supported. We can use existing HTML table exporter code
951 ;; here.
952 ((and org-export-with-tables
953 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
954 (if (not table-open)
955 ;; New table starts
956 (setq table-open t
957 table-buffer nil
958 table-orig-buffer nil))
959 ;; Accumulate lines
960 (setq table-buffer (cons line table-buffer)
961 table-orig-buffer (cons origline table-orig-buffer))
962 (when (or (not lines)
963 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
964 (car lines))))
965 (setq table-open nil
966 table-buffer (nreverse table-buffer)
967 table-orig-buffer (nreverse table-orig-buffer))
968 (org-export-docbook-close-para-maybe)
969 (insert (org-export-docbook-finalize-table
970 (org-format-table-html table-buffer table-orig-buffer)))))
972 ;; Normal lines
973 (when (string-match
974 (cond
975 ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
976 ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
977 ((= llt ?\)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
978 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
979 line)
980 (setq ind (or (get-text-property 0 'original-indentation line)
981 (org-get-string-indentation line))
982 item-type (if (match-beginning 4) "o" "u")
983 starter (if (match-beginning 2)
984 (substring (match-string 2 line) 0 -1))
985 line (substring line (match-beginning 5))
986 item-tag nil)
987 (if (and starter (string-match "\\(.*?\\) ::[ \t]*" line))
988 (setq item-type "d"
989 item-tag (match-string 1 line)
990 line (substring line (match-end 0))))
991 (when (and (not (equal item-type "d"))
992 (not (string-match "[^ \t]" line)))
993 ;; Empty line. Pretend indentation is large.
994 (setq ind (if org-empty-line-terminates-plain-lists
996 (1+ (or (car local-list-indent) 1)))))
997 (setq didclose nil)
998 (while (and in-local-list
999 (or (and (= ind (car local-list-indent))
1000 (not starter))
1001 (< ind (car local-list-indent))))
1002 (setq didclose t)
1003 (let ((listtype (car local-list-type)))
1004 (org-export-docbook-close-li listtype)
1005 (insert (cond
1006 ((equal listtype "o") "</orderedlist>\n")
1007 ((equal listtype "u") "</itemizedlist>\n")
1008 ((equal listtype "d") "</variablelist>\n"))))
1009 (pop local-list-type) (pop local-list-indent)
1010 (setq in-local-list local-list-indent))
1011 (cond
1012 ((and starter
1013 (or (not in-local-list)
1014 (> ind (car local-list-indent))))
1015 ;; Start new (level of) list
1016 (org-export-docbook-close-para-maybe)
1017 (insert (cond
1018 ((equal item-type "u") "<itemizedlist>\n<listitem>\n")
1019 ((equal item-type "o") "<orderedlist>\n<listitem>\n")
1020 ((equal item-type "d")
1021 (format "<variablelist>\n<varlistentry><term>%s</term><listitem>\n" item-tag))))
1022 ;; For DocBook, we need to open a para right after tag
1023 ;; <listitem>.
1024 (org-export-docbook-open-para)
1025 (push item-type local-list-type)
1026 (push ind local-list-indent)
1027 (setq in-local-list t))
1028 (starter
1029 ;; Continue current list
1030 (let ((listtype (car local-list-type)))
1031 (org-export-docbook-close-li listtype)
1032 (insert (cond
1033 ((equal listtype "o") "<listitem>")
1034 ((equal listtype "u") "<listitem>")
1035 ((equal listtype "d") (format
1036 "<varlistentry><term>%s</term><listitem>"
1037 (or item-tag
1038 "???"))))))
1039 ;; For DocBook, we need to open a para right after tag
1040 ;; <listitem>.
1041 (org-export-docbook-open-para))
1042 (didclose
1043 ;; We did close a list, normal text follows: need <para>
1044 (org-export-docbook-open-para)))
1045 ;; Checkboxes.
1046 (if (string-match "^[ \t]*\\(\\[[X -]\\]\\)" line)
1047 (setq line
1048 (replace-match (concat checkbox-start
1049 (match-string 1 line)
1050 checkbox-end)
1051 t t line))))
1053 ;; Empty lines start a new paragraph. If hand-formatted lists
1054 ;; are not fully interpreted, lines starting with "-", "+", "*"
1055 ;; also start a new paragraph.
1056 (if (and (string-match "^ [-+*]-\\|^[ \t]*$" line)
1057 (not inverse))
1058 (org-export-docbook-open-para))
1060 ;; Is this the start of a footnote?
1061 (when org-export-with-footnotes
1062 (when (and (boundp 'footnote-section-tag-regexp)
1063 (string-match (concat "^" footnote-section-tag-regexp)
1064 line))
1065 ;; ignore this line
1066 (throw 'nextline nil))
1067 ;; These footnote lines have been read and saved before,
1068 ;; ignore them at this time.
1069 (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
1070 (org-export-docbook-close-para-maybe)
1071 (throw 'nextline nil)))
1073 ;; FIXME: It might be a good idea to add an option to
1074 ;; support line break processing instruction <?linebreak?>.
1075 ;; Org-mode supports line break "\\" in HTML exporter, and
1076 ;; some DocBook users may also want to force line breaks
1077 ;; even though DocBook only supports that in
1078 ;; <literallayout>.
1080 (insert line "\n")))))
1082 ;; Properly close all local lists and other lists
1083 (when inquote
1084 (insert "]]>\n</programlisting>\n")
1085 (org-export-docbook-open-para))
1086 (when in-local-list
1087 ;; Close any local lists before inserting a new header line
1088 (while local-list-type
1089 (let ((listtype (car local-list-type)))
1090 (org-export-docbook-close-li listtype)
1091 (insert (cond
1092 ((equal listtype "o") "</orderedlist>\n")
1093 ((equal listtype "u") "</itemizedlist>\n")
1094 ((equal listtype "d") "</variablelist>\n"))))
1095 (pop local-list-type))
1096 (setq local-list-indent nil
1097 in-local-list nil))
1098 ;; Close all open sections.
1099 (org-export-docbook-level-start 1 nil)
1101 (unless (plist-get opt-plist :buffer-will-be-killed)
1102 (normal-mode)
1103 (if (eq major-mode default-major-mode)
1104 (nxml-mode)))
1106 ;; Remove empty paragraphs and lists. Replace them with a
1107 ;; newline.
1108 (goto-char (point-min))
1109 (while (re-search-forward
1110 "[ \r\n\t]*\\(<para>\\)[ \r\n\t]*</para>[ \r\n\t]*" nil t)
1111 (when (not (get-text-property (match-beginning 1) 'org-protected))
1112 (replace-match "\n")
1113 (backward-char 1)))
1114 ;; Fill empty sections with <para></para>. This is to make sure
1115 ;; that the DocBook document generated is valid and well-formed.
1116 (goto-char (point-min))
1117 (while (re-search-forward
1118 "</title>\\([ \r\n\t]*\\)</section>" nil t)
1119 (when (not (get-text-property (match-beginning 0) 'org-protected))
1120 (replace-match "\n<para></para>\n" nil nil nil 1)))
1121 ;; Insert the last closing tag.
1122 (goto-char (point-max))
1123 (unless body-only
1124 (insert "</article>"))
1125 (or to-buffer (save-buffer))
1126 (goto-char (point-min))
1127 (or (org-export-push-to-kill-ring "DocBook")
1128 (message "Exporting... done"))
1129 (if (eq to-buffer 'string)
1130 (prog1 (buffer-substring (point-min) (point-max))
1131 (kill-buffer (current-buffer)))
1132 (current-buffer)))))
1134 (defun org-export-docbook-open-para ()
1135 "Insert <para>, but first close previous paragraph if any."
1136 (org-export-docbook-close-para-maybe)
1137 (insert "\n<para>")
1138 (setq org-docbook-para-open t))
1140 (defun org-export-docbook-close-para-maybe ()
1141 "Close DocBook paragraph if there is one open."
1142 (when org-docbook-para-open
1143 (insert "</para>\n")
1144 (setq org-docbook-para-open nil)))
1146 (defun org-export-docbook-close-li (&optional type)
1147 "Close list if necessary."
1148 (org-export-docbook-close-para-maybe)
1149 (if (equal type "d")
1150 (insert "</listitem></varlistentry>\n")
1151 (insert "</listitem>\n")))
1153 (defvar in-local-list)
1154 (defvar local-list-indent)
1155 (defvar local-list-type)
1156 (defun org-export-docbook-close-lists-maybe (line)
1157 (let ((ind (or (get-text-property 0 'original-indentation line)))
1158 ; (and (string-match "\\S-" line)
1159 ; (org-get-indentation line))))
1160 didclose)
1161 (when ind
1162 (while (and in-local-list
1163 (<= ind (car local-list-indent)))
1164 (setq didclose t)
1165 (let ((listtype (car local-list-type)))
1166 (org-export-docbook-close-li listtype)
1167 (insert (cond
1168 ((equal listtype "o") "</orderedlist>\n")
1169 ((equal listtype "u") "</itemizedlist>\n")
1170 ((equal listtype "d") "</variablelist>\n"))))
1171 (pop local-list-type) (pop local-list-indent)
1172 (setq in-local-list local-list-indent))
1173 (and didclose (org-export-docbook-open-para)))))
1175 (defun org-export-docbook-level-start (level title)
1176 "Insert a new level in DocBook export.
1177 When TITLE is nil, just close all open levels."
1178 (org-export-docbook-close-para-maybe)
1179 (let* ((target (and title (org-get-text-property-any 0 'target title)))
1180 (l org-level-max)
1181 section-number)
1182 (while (>= l level)
1183 (if (aref org-levels-open (1- l))
1184 (progn
1185 (insert "</section>\n")
1186 (aset org-levels-open (1- l) nil)))
1187 (setq l (1- l)))
1188 (when title
1189 ;; If title is nil, this means this function is called to close
1190 ;; all levels, so the rest is done only if title is given.
1192 ;; Format tags: put them into a superscript like format.
1193 (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
1194 (setq title
1195 (replace-match
1196 (if org-export-with-tags
1197 (save-match-data
1198 (concat
1199 "<superscript>"
1200 (match-string 1 title)
1201 "</superscript>"))
1203 t t title)))
1204 (aset org-levels-open (1- level) t)
1205 (setq section-number (org-section-number level))
1206 (insert (format "\n<section xml:id=\"%s%s\">\n<title>%s</title>"
1207 org-export-docbook-section-id-prefix
1208 section-number title))
1209 (org-export-docbook-open-para))))
1211 (defun org-docbook-expand (string)
1212 "Prepare STRING for DocBook export.
1213 Applies all active conversions. If there are links in the
1214 string, don't modify these."
1215 (let* ((re (concat org-bracket-link-regexp "\\|"
1216 (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
1217 m s l res)
1218 (while (setq m (string-match re string))
1219 (setq s (substring string 0 m)
1220 l (match-string 0 string)
1221 string (substring string (match-end 0)))
1222 (push (org-docbook-do-expand s) res)
1223 (push l res))
1224 (push (org-docbook-do-expand string) res)
1225 (apply 'concat (nreverse res))))
1227 (defun org-docbook-do-expand (s)
1228 "Apply all active conversions to translate special ASCII to DocBook."
1229 (setq s (org-html-protect s))
1230 (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
1231 (setq s (replace-match "<\\1>" t nil s)))
1232 (if org-export-with-emphasize
1233 (setq s (org-export-docbook-convert-emphasize s)))
1234 (if org-export-with-special-strings
1235 (setq s (org-export-docbook-convert-special-strings s)))
1236 (if org-export-with-sub-superscripts
1237 (setq s (org-export-docbook-convert-sub-super s)))
1238 (if org-export-with-TeX-macros
1239 (let ((start 0) wd ass)
1240 (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)\\({}\\)?"
1241 s start))
1242 (if (get-text-property (match-beginning 0) 'org-protected s)
1243 (setq start (match-end 0))
1244 (setq wd (match-string 1 s))
1245 (if (setq ass (assoc wd org-html-entities))
1246 (setq s (replace-match (or (cdr ass)
1247 (concat "&" (car ass) ";"))
1248 t t s))
1249 (setq start (+ start (length wd))))))))
1252 (defun org-export-docbook-format-desc (desc)
1253 "Make sure DESC is valid as a description in a link."
1254 (save-match-data
1255 (org-docbook-do-expand desc)))
1257 (defun org-export-docbook-convert-emphasize (string)
1258 "Apply emphasis for DocBook exporting."
1259 (let ((s 0) rpl)
1260 (while (string-match org-emph-re string s)
1261 (if (not (equal
1262 (substring string (match-beginning 3) (1+ (match-beginning 3)))
1263 (substring string (match-beginning 4) (1+ (match-beginning 4)))))
1264 (setq s (match-beginning 0)
1266 (concat
1267 (match-string 1 string)
1268 (nth 1 (assoc (match-string 3 string)
1269 org-export-docbook-emphasis-alist))
1270 (match-string 4 string)
1271 (nth 2 (assoc (match-string 3 string)
1272 org-export-docbook-emphasis-alist))
1273 (match-string 5 string))
1274 string (replace-match rpl t t string)
1275 s (+ s (- (length rpl) 2)))
1276 (setq s (1+ s))))
1277 string))
1279 (defun org-docbook-protect (string)
1280 (org-html-protect string))
1282 ;; For now, simply return string as it is.
1283 (defun org-export-docbook-convert-special-strings (string)
1284 "Convert special characters in STRING to DocBook."
1285 string)
1287 (defun org-export-docbook-get-footnotes (lines)
1288 "Given a list of LINES, return a list of alist footnotes."
1289 (let ((list nil) line)
1290 (while (setq line (pop lines))
1291 (if (string-match "^[ \t]*\\[\\([0-9]+\\)\\] \\(.+\\)" line)
1292 (push (cons (match-string 1 line) (match-string 2 line))
1293 list)))
1294 list))
1296 (defun org-export-docbook-format-image (src)
1297 "Create image element in DocBook."
1298 (save-match-data
1299 (let* ((caption (org-find-text-property-in-string 'org-caption src))
1300 (attr (or (org-find-text-property-in-string 'org-attributes src)
1301 ""))
1302 (label (org-find-text-property-in-string 'org-label src))
1303 (default-attr org-export-docbook-default-image-attributes)
1304 tmp)
1305 (while (setq tmp (pop default-attr))
1306 (if (not (string-match (concat (car tmp) "=") attr))
1307 (setq attr (concat attr " " (car tmp) "=" (cdr tmp)))))
1308 (format "<mediaobject%s>
1309 <imageobject>\n<imagedata fileref=\"%s\" %s/>\n</imageobject>
1310 %s</mediaobject>"
1311 (if label (concat " xml:id=\"" label "\"") "")
1312 src attr
1313 (if caption
1314 (concat "<caption>\n<para>"
1315 caption
1316 "</para>\n</caption>\n")
1318 ))))
1320 (defun org-export-docbook-preprocess (parameters)
1321 "Extra preprocessing work for DocBook export."
1322 ;; Merge lines starting with "\par" to one line. Such lines are
1323 ;; regarded as the continuation of a long footnote.
1324 (goto-char (point-min))
1325 (while (re-search-forward "\n\\(\\\\par\\>\\)" nil t)
1326 (if (not (get-text-property (match-beginning 1) 'org-protected))
1327 (replace-match ""))))
1329 (defun org-export-docbook-finalize-table (table)
1330 "Change TABLE to informaltable if caption does not exist.
1331 TABLE is a string containing the HTML code generated by
1332 `org-format-table-html' for a table in Org-mode buffer."
1333 (if (string-match
1334 "^<table \\(\\(.\\|\n\\)+\\)<caption></caption>\n\\(\\(.\\|\n\\)+\\)</table>"
1335 table)
1336 (replace-match (concat "<informaltable "
1337 (match-string 1 table)
1338 (match-string 3 table)
1339 "</informaltable>")
1340 nil nil table)
1341 table))
1343 ;; Note: This function is very similar to
1344 ;; org-export-html-convert-sub-super. They can be merged in the future.
1345 (defun org-export-docbook-convert-sub-super (string)
1346 "Convert sub- and superscripts in STRING for DocBook."
1347 (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
1348 (while (string-match org-match-substring-regexp string s)
1349 (cond
1350 ((and requireb (match-end 8)) (setq s (match-end 2)))
1351 ((get-text-property (match-beginning 2) 'org-protected string)
1352 (setq s (match-end 2)))
1354 (setq s (match-end 1)
1355 key (if (string= (match-string 2 string) "_")
1356 "subscript"
1357 "superscript")
1358 c (or (match-string 8 string)
1359 (match-string 6 string)
1360 (match-string 5 string))
1361 string (replace-match
1362 (concat (match-string 1 string)
1363 "<" key ">" c "</" key ">")
1364 t t string)))))
1365 (while (string-match "\\\\\\([_^]\\)" string)
1366 (setq string (replace-match (match-string 1 string) t t string)))
1367 string))
1369 (defun org-export-docbook-protect-tags (string)
1370 "Change ``<...>'' in string STRING into ``@<...>''.
1371 This is normally needed when STRING contains DocBook elements
1372 that need to be preserved in later phase of DocBook exporting."
1373 (let ((start 0))
1374 (while (string-match "<\\([^>]*\\)>" string start)
1375 (setq string (replace-match
1376 "@<\\1>" t nil string)
1377 start (match-end 0)))
1378 string))
1380 (defun org-export-docbook-handle-time-stamps (line)
1381 "Format time stamps in string LINE."
1382 (let (replaced
1383 (kw-markup (org-export-docbook-protect-tags
1384 org-export-docbook-keywords-markup))
1385 (ts-markup (org-export-docbook-protect-tags
1386 org-export-docbook-timestamp-markup)))
1387 (while (string-match org-maybe-keyword-time-regexp line)
1388 (setq replaced
1389 (concat replaced
1390 (substring line 0 (match-beginning 0))
1391 (if (match-end 1)
1392 (format kw-markup
1393 (match-string 1 line)))
1395 (format ts-markup
1396 (substring (org-translate-time
1397 (match-string 3 line)) 1 -1)))
1398 line (substring line (match-end 0))))
1399 (concat replaced line)))
1401 (provide 'org-docbook)
1403 ;;; org-docbook.el ends here