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