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