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