Merge branch 'master' of orgmode.org:org-mode
[org-mode.git] / lisp / org-docbook.el
blobe70df252e89d6aa84862ef58e7e786412e0b26cc
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 (eq major-mode 'org-mode)
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 (auto-insert nil); Avoid any auto-insert stuff for the new file
478 (buffer (if to-buffer
479 (cond
480 ((eq to-buffer 'string)
481 (get-buffer-create "*Org DocBook Export*"))
482 (t (get-buffer-create to-buffer)))
483 (find-file-noselect filename)))
484 ;; org-levels-open is a global variable
485 (org-levels-open (make-vector org-level-max nil))
486 (date (plist-get opt-plist :date))
487 (author (or (plist-get opt-plist :author)
488 user-full-name))
489 (email (plist-get opt-plist :email))
490 firstname othername surname
491 (title (or (and subtree-p (org-export-get-title-from-subtree))
492 (plist-get opt-plist :title)
493 (and (not
494 (plist-get opt-plist :skip-before-1st-heading))
495 (org-export-grab-title-from-buffer))
496 (and buffer-file-name
497 (file-name-sans-extension
498 (file-name-nondirectory buffer-file-name)))
499 "UNTITLED"))
500 ;; We will use HTML table formatter to export tables to DocBook
501 ;; format, so need to set html-table-tag here.
502 (html-table-tag (plist-get opt-plist :html-table-tag))
503 (quote-re0 (concat "^ *" org-quote-string "\\( +\\|[ \t]*$\\)"))
504 (quote-re (format org-heading-keyword-regexp-format
505 org-quote-string))
506 (inquote nil)
507 (infixed nil)
508 (inverse nil)
509 (llt org-plain-list-ordered-item-terminator)
510 (email (plist-get opt-plist :email))
511 (language (plist-get opt-plist :language))
512 (lang-words nil)
514 (start 0)
515 (coding-system (and (boundp 'buffer-file-coding-system)
516 buffer-file-coding-system))
517 (coding-system-for-write (or org-export-docbook-coding-system
518 coding-system))
519 (save-buffer-coding-system (or org-export-docbook-coding-system
520 coding-system))
521 (charset (and coding-system-for-write
522 (fboundp 'coding-system-get)
523 (coding-system-get coding-system-for-write
524 'mime-charset)))
525 (region
526 (buffer-substring
527 (if region-p (region-beginning) (point-min))
528 (if region-p (region-end) (point-max))))
529 (org-export-footnotes-seen nil)
530 (org-export-footnotes-data (org-footnote-all-labels 'with-defs))
531 (lines
532 (org-split-string
533 (org-export-preprocess-string
534 region
535 :emph-multiline t
536 :for-backend 'docbook
537 :skip-before-1st-heading
538 (plist-get opt-plist :skip-before-1st-heading)
539 :drawers (plist-get opt-plist :drawers)
540 :todo-keywords (plist-get opt-plist :todo-keywords)
541 :tasks (plist-get opt-plist :tasks)
542 :tags (plist-get opt-plist :tags)
543 :priority (plist-get opt-plist :priority)
544 :footnotes (plist-get opt-plist :footnotes)
545 :timestamps (plist-get opt-plist :timestamps)
546 :archived-trees
547 (plist-get opt-plist :archived-trees)
548 :select-tags (plist-get opt-plist :select-tags)
549 :exclude-tags (plist-get opt-plist :exclude-tags)
550 :add-text
551 (plist-get opt-plist :text)
552 :LaTeX-fragments
553 (plist-get opt-plist :LaTeX-fragments))
554 "[\r\n]"))
555 ;; Use literal output to show check boxes.
556 (checkbox-start
557 (nth 1 (assoc "=" org-export-docbook-emphasis-alist)))
558 (checkbox-end
559 (nth 2 (assoc "=" org-export-docbook-emphasis-alist)))
560 table-open type
561 table-buffer table-orig-buffer
562 ind item-type starter
563 rpl path attr caption label desc descp desc1 desc2 link
564 fnc item-tag item-number
565 footref-seen footnote-list
566 id-file
569 ;; Fine detailed info about author name.
570 (if (string-match "\\([^ ]+\\) \\(.+ \\)?\\([^ ]+\\)" author)
571 (progn
572 (setq firstname (match-string 1 author)
573 othername (or (match-string 2 author) "")
574 surname (match-string 3 author))))
576 ;; Get all footnote text.
577 (setq footnote-list
578 (org-export-docbook-get-footnotes lines))
580 (let ((inhibit-read-only t))
581 (org-unmodified
582 (remove-text-properties (point-min) (point-max)
583 '(:org-license-to-kill t))))
585 (setq org-min-level (org-get-min-level lines level-offset))
586 (setq org-last-level org-min-level)
587 (org-init-section-numbers)
589 ;; Get and save the date.
590 (cond
591 ((and date (string-match "%" date))
592 (setq date (format-time-string date)))
593 (date)
594 (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
596 ;; Get the language-dependent settings
597 (setq lang-words (or (assoc language org-export-language-setup)
598 (assoc "en" org-export-language-setup)))
600 ;; Switch to the output buffer. Use fundamental-mode for now. We
601 ;; could turn on nXML mode later and do some indentation.
602 (set-buffer buffer)
603 (let ((inhibit-read-only t)) (erase-buffer))
604 (fundamental-mode)
605 (org-install-letbind)
607 (and (fboundp 'set-buffer-file-coding-system)
608 (set-buffer-file-coding-system coding-system-for-write))
610 ;; The main body...
611 (let ((case-fold-search nil)
612 (org-odd-levels-only odd))
614 ;; Create local variables for all options, to make sure all called
615 ;; functions get the correct information
616 (mapc (lambda (x)
617 (set (make-local-variable (nth 2 x))
618 (plist-get opt-plist (car x))))
619 org-export-plist-vars)
621 ;; Insert DocBook file header, title, and author info.
622 (unless body-only
623 (insert org-export-docbook-header)
624 (if org-export-docbook-doctype
625 (insert org-export-docbook-doctype))
626 (insert "<!-- Date: " date " -->\n")
627 (insert (format "<!-- DocBook XML file generated by Org-mode %s Emacs %s -->\n"
628 org-version emacs-major-version))
629 (insert org-export-docbook-article-header)
630 (insert (format
631 "\n <title>%s</title>
632 <info>
633 <author>
634 <personname>
635 <firstname>%s</firstname> <othername>%s</othername> <surname>%s</surname>
636 </personname>
638 </author>
639 </info>\n"
640 (org-docbook-expand title)
641 firstname othername surname
642 (if (and org-export-email-info
643 email (string-match "\\S-" email))
644 (concat "<email>" email "</email>") "")
647 (org-init-section-numbers)
649 (org-export-docbook-open-para)
651 ;; Loop over all the lines...
652 (while (setq line (pop lines) origline line)
653 (catch 'nextline
655 ;; End of quote section?
656 (when (and inquote (string-match org-outline-regexp-bol line))
657 (insert "]]></programlisting>\n")
658 (org-export-docbook-open-para)
659 (setq inquote nil))
660 ;; Inside a quote section?
661 (when inquote
662 (insert (org-docbook-protect line) "\n")
663 (throw 'nextline nil))
665 ;; Fixed-width, verbatim lines (examples)
666 (when (and org-export-with-fixed-width
667 (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line))
668 (when (not infixed)
669 (setq infixed t)
670 (org-export-docbook-close-para-maybe)
671 (insert "<programlisting><![CDATA["))
672 (insert (match-string 3 line) "\n")
673 (when (or (not lines)
674 (not (string-match "^[ \t]*\\(:.*\\)"
675 (car lines))))
676 (setq infixed nil)
677 (insert "]]></programlisting>\n")
678 (org-export-docbook-open-para))
679 (throw 'nextline nil))
681 ;; Protected HTML
682 (when (get-text-property 0 'org-protected line)
683 (let (par (ind (get-text-property 0 'original-indentation line)))
684 (when (re-search-backward
685 "\\(<para>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
686 (setq par (match-string 1))
687 (replace-match "\\2\n"))
688 (insert line "\n")
689 (while (and lines
690 (or (= (length (car lines)) 0)
691 (not ind)
692 (equal ind (get-text-property 0 'original-indentation (car lines))))
693 (or (= (length (car lines)) 0)
694 (get-text-property 0 'org-protected (car lines))))
695 (insert (pop lines) "\n"))
696 (and par (insert "<para>\n")))
697 (throw 'nextline nil))
699 ;; Start of block quotes and verses
700 (when (or (equal "ORG-BLOCKQUOTE-START" line)
701 (and (equal "ORG-VERSE-START" line)
702 (setq inverse t)))
703 (org-export-docbook-close-para-maybe)
704 (insert "<blockquote>")
705 ;; Check whether attribution for this blockquote exists.
706 (let (tmp1
707 attribution
708 (end (if inverse "ORG-VERSE-END" "ORG-BLOCKQUOTE-END"))
709 (quote-lines nil))
710 (while (and (setq tmp1 (pop lines))
711 (not (equal end tmp1)))
712 (push tmp1 quote-lines))
713 (push tmp1 lines) ; Put back quote end mark
714 ;; Check the last line in the quote to see if it contains
715 ;; the attribution.
716 (setq tmp1 (pop quote-lines))
717 (if (string-match "\\(^.*\\)\\(--[ \t]+\\)\\(.+\\)$" tmp1)
718 (progn
719 (setq attribution (match-string 3 tmp1))
720 (when (save-match-data
721 (string-match "[^ \t]" (match-string 1 tmp1)))
722 (push (match-string 1 tmp1) lines)))
723 (push tmp1 lines))
724 (while (setq tmp1 (pop quote-lines))
725 (push tmp1 lines))
726 (when attribution
727 (insert "<attribution>" attribution "</attribution>")))
728 ;; Insert <literallayout> for verse.
729 (if inverse
730 (insert "\n<literallayout>")
731 (org-export-docbook-open-para))
732 (throw 'nextline nil))
734 ;; End of block quotes
735 (when (equal "ORG-BLOCKQUOTE-END" line)
736 (org-export-docbook-close-para-maybe)
737 (insert "</blockquote>\n")
738 (org-export-docbook-open-para)
739 (throw 'nextline nil))
741 ;; End of verses
742 (when (equal "ORG-VERSE-END" line)
743 (insert "</literallayout>\n</blockquote>\n")
744 (org-export-docbook-open-para)
745 (setq inverse nil)
746 (throw 'nextline nil))
748 ;; Text centering. Element <para role="centered"> does not
749 ;; seem to work with FOP, so for now we use <informaltable> to
750 ;; center the text, which can contain multiple paragraphs.
751 (when (equal "ORG-CENTER-START" line)
752 (org-export-docbook-close-para-maybe)
753 (insert "<informaltable frame=\"none\" colsep=\"0\" rowsep=\"0\">\n"
754 "<tgroup align=\"center\" cols=\"1\">\n"
755 "<tbody><row><entry>\n")
756 (org-export-docbook-open-para)
757 (throw 'nextline nil))
759 (when (equal "ORG-CENTER-END" line)
760 (org-export-docbook-close-para-maybe)
761 (insert "</entry></row></tbody>\n"
762 "</tgroup>\n</informaltable>\n")
763 (org-export-docbook-open-para)
764 (throw 'nextline nil))
766 ;; Make targets to anchors. Note that currently FOP does not
767 ;; seem to support <anchor> tags when generating PDF output,
768 ;; but this can be used in DocBook --> HTML conversion.
769 (setq start 0)
770 (while (string-match
771 "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line start)
772 (cond
773 ((get-text-property (match-beginning 1) 'org-protected line)
774 (setq start (match-end 1)))
775 ((match-end 2)
776 (setq line (replace-match
777 (format "@<anchor xml:id=\"%s\"/>"
778 (org-solidify-link-text (match-string 1 line)))
779 t t line)))
781 (setq line (replace-match
782 (format "@<anchor xml:id=\"%s\"/>"
783 (org-solidify-link-text (match-string 1 line)))
784 t t line)))))
786 ;; Put time stamps and related keywords into special mark-up
787 ;; elements.
788 (setq line (org-export-docbook-handle-time-stamps line))
790 ;; Replace "&", "<" and ">" by "&amp;", "&lt;" and "&gt;".
791 ;; Handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>").
792 ;; Also handle sub_superscripts and check boxes.
793 (or (string-match org-table-hline-regexp line)
794 (setq line (org-docbook-expand line)))
796 ;; Format the links
797 (setq start 0)
798 (while (string-match org-bracket-link-analytic-regexp++ line start)
799 (setq start (match-beginning 0))
800 (setq path (save-match-data (org-link-unescape
801 (match-string 3 line))))
802 (setq type (cond
803 ((match-end 2) (match-string 2 line))
804 ((save-match-data
805 (or (file-name-absolute-p path)
806 (string-match "^\\.\\.?/" path)))
807 "file")
808 (t "internal")))
809 (setq path (org-extract-attributes (org-link-unescape path)))
810 (setq attr (get-text-property 0 'org-attributes path)
811 caption (get-text-property 0 'org-caption path)
812 label (get-text-property 0 'org-label path))
813 (setq desc1 (if (match-end 5) (match-string 5 line))
814 desc2 (if (match-end 2) (concat type ":" path) path)
815 descp (and desc1 (not (equal desc1 desc2)))
816 desc (or desc1 desc2))
817 ;; Make an image out of the description if that is so wanted
818 (when (and descp (org-file-image-p
819 desc org-export-docbook-inline-image-extensions))
820 (save-match-data
821 (if (string-match "^file:" desc)
822 (setq desc (substring desc (match-end 0))))))
823 ;; FIXME: do we need to unescape here somewhere?
824 (cond
825 ((equal type "internal")
826 (setq rpl (format "<link linkend=\"%s\">%s</link>"
827 (org-solidify-link-text
828 (save-match-data (org-link-unescape path)) nil)
829 (org-export-docbook-format-desc desc))))
830 ((and (equal type "id")
831 (setq id-file (org-id-find-id-file path)))
832 ;; This is an id: link to another file (if it was the same file,
833 ;; it would have become an internal link...)
834 (save-match-data
835 (setq id-file (file-relative-name
836 id-file (file-name-directory org-current-export-file)))
837 (setq id-file (concat (file-name-sans-extension id-file)
838 org-export-docbook-extension))
839 (setq rpl (format "<link xlink:href=\"%s#%s\">%s</link>"
840 id-file path (org-export-docbook-format-desc desc)))))
841 ((member type '("http" "https"))
842 ;; Standard URL, just check if we need to inline an image
843 (if (and (or (eq t org-export-docbook-inline-images)
844 (and org-export-docbook-inline-images (not descp)))
845 (org-file-image-p
846 path org-export-docbook-inline-image-extensions))
847 (setq rpl (org-export-docbook-format-image
848 (concat type ":" path)))
849 (setq link (concat type ":" path))
850 (setq rpl (format "<link xlink:href=\"%s\">%s</link>"
851 (org-export-html-format-href link)
852 (org-export-docbook-format-desc desc)))
854 ((member type '("ftp" "mailto" "news"))
855 ;; Standard URL
856 (setq link (concat type ":" path))
857 (setq rpl (format "<link xlink:href=\"%s\">%s</link>"
858 (org-export-html-format-href link)
859 (org-export-docbook-format-desc desc))))
860 ((string= type "coderef")
861 (setq rpl (format (org-export-get-coderef-format path (and descp desc))
862 (cdr (assoc path org-export-code-refs)))))
863 ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
864 ;; The link protocol has a function for format the link
865 (setq rpl
866 (save-match-data
867 (funcall fnc (org-link-unescape path) desc1 'html))))
869 ((string= type "file")
870 ;; FILE link
871 (let* ((filename path)
872 (abs-p (file-name-absolute-p filename))
873 thefile file-is-image-p search)
874 (save-match-data
875 (if (string-match "::\\(.*\\)" filename)
876 (setq search (match-string 1 filename)
877 filename (replace-match "" t nil filename)))
878 (setq valid
879 (if (functionp link-validate)
880 (funcall link-validate filename current-dir)
882 (setq file-is-image-p
883 (org-file-image-p
884 filename org-export-docbook-inline-image-extensions))
885 (setq thefile (if abs-p (expand-file-name filename) filename))
886 ;; Carry over the properties (expand-file-name will
887 ;; discard the properties of filename)
888 (add-text-properties 0 (1- (length thefile))
889 (list 'org-caption caption
890 'org-attributes attr
891 'org-label label)
892 thefile)
893 (when (and org-export-docbook-link-org-files-as-docbook
894 (string-match "\\.org$" thefile))
895 (setq thefile (concat (substring thefile 0
896 (match-beginning 0))
897 org-export-docbook-extension))
898 (if (and search
899 ;; make sure this is can be used as target search
900 (not (string-match "^[0-9]*$" search))
901 (not (string-match "^\\*" search))
902 (not (string-match "^/.*/$" search)))
903 (setq thefile (concat thefile "#"
904 (org-solidify-link-text
905 (org-link-unescape search)))))
906 (when (string-match "^file:" desc)
907 (setq desc (replace-match "" t t desc))
908 (if (string-match "\\.org$" desc)
909 (setq desc (replace-match "" t t desc))))))
910 (setq rpl (if (and file-is-image-p
911 (or (eq t org-export-docbook-inline-images)
912 (and org-export-docbook-inline-images
913 (not descp))))
914 (progn
915 (message "image %s %s" thefile org-docbook-para-open)
916 (org-export-docbook-format-image thefile))
917 (format "<link xlink:href=\"%s\">%s</link>"
918 thefile (org-export-docbook-format-desc desc))))
919 (if (not valid) (setq rpl desc))))
922 ;; Just publish the path, as default
923 (setq rpl (concat "&lt;" type ":"
924 (save-match-data (org-link-unescape path))
925 "&gt;"))))
926 (setq line (replace-match rpl t t line)
927 start (+ start (length rpl))))
929 ;; TODO items: can we do something better?!
930 (if (and (string-match org-todo-line-regexp line)
931 (match-beginning 2))
932 (setq line
933 (concat (substring line 0 (match-beginning 2))
934 "[" (match-string 2 line) "]"
935 (substring line (match-end 2)))))
937 ;; Does this contain a reference to a footnote?
938 (when org-export-with-footnotes
939 (setq start 0)
940 (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
941 ;; Discard protected matches not clearly identified as
942 ;; footnote markers.
943 (if (or (get-text-property (match-beginning 2) 'org-protected line)
944 (not (get-text-property (match-beginning 2) 'org-footnote line)))
945 (setq start (match-end 2))
946 (let* ((num (match-string 2 line))
947 (footnote-def (assoc num footnote-list)))
948 (if (assoc num footref-seen)
949 (setq line (replace-match
950 (format "%s<footnoteref linkend=\"%s%s\"/>"
951 (match-string 1 line)
952 org-export-docbook-footnote-id-prefix num)
953 t t line))
954 (setq line (replace-match
955 (concat
956 (format "%s<footnote xml:id=\"%s%s\"><para>%s</para></footnote>"
957 (match-string 1 line)
958 org-export-docbook-footnote-id-prefix
960 (if footnote-def
961 (save-match-data
962 (org-docbook-expand (cdr footnote-def)))
963 (format "FOOTNOTE DEFINITION NOT FOUND: %s" num)))
964 ;; If another footnote is following the
965 ;; current one, add a separator.
966 (if (save-match-data
967 (string-match "\\`\\[[0-9]+\\]"
968 (substring line (match-end 0))))
969 org-export-docbook-footnote-separator
970 ""))
971 t t line))
972 (push (cons num 1) footref-seen))))))
974 (cond
975 ((string-match "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$" line)
976 ;; This is a headline
977 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
978 level-offset))
979 txt (match-string 2 line))
980 (if (string-match quote-re0 txt)
981 (setq txt (replace-match "" t t txt)))
982 (org-export-docbook-level-start level txt)
983 ;; QUOTES
984 (when (string-match quote-re line)
985 (org-export-docbook-close-para-maybe)
986 (insert "<programlisting><![CDATA[")
987 (setq inquote t)))
989 ;; Tables: since version 4.3 of DocBook DTD, HTML tables are
990 ;; supported. We can use existing HTML table exporter code
991 ;; here.
992 ((and org-export-with-tables
993 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
994 (if (not table-open)
995 ;; New table starts
996 (setq table-open t
997 table-buffer nil
998 table-orig-buffer nil))
999 ;; Accumulate lines
1000 (setq table-buffer (cons line table-buffer)
1001 table-orig-buffer (cons origline table-orig-buffer))
1002 (when (or (not lines)
1003 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
1004 (car lines))))
1005 (setq table-open nil
1006 table-buffer (nreverse table-buffer)
1007 table-orig-buffer (nreverse table-orig-buffer))
1008 (org-export-docbook-close-para-maybe)
1009 (insert (org-export-docbook-finalize-table
1010 (org-format-table-html table-buffer table-orig-buffer
1011 'no-css)))))
1013 ;; Normal lines
1015 ;; This line either is list item or end a list.
1016 (when (when (get-text-property 0 'list-item line)
1017 (setq line (org-export-docbook-list-line
1018 line
1019 (get-text-property 0 'list-item line)
1020 (get-text-property 0 'list-struct line)
1021 (get-text-property 0 'list-prevs line)))))
1023 ;; Empty lines start a new paragraph. If hand-formatted lists
1024 ;; are not fully interpreted, lines starting with "-", "+", "*"
1025 ;; also start a new paragraph.
1026 (if (and (string-match "^ [-+*]-\\|^[ \t]*$" line)
1027 (not inverse))
1028 (org-export-docbook-open-para))
1030 ;; Is this the start of a footnote?
1031 (when org-export-with-footnotes
1032 (when (and (boundp 'footnote-section-tag-regexp)
1033 (string-match (concat "^" footnote-section-tag-regexp)
1034 line))
1035 ;; ignore this line
1036 (throw 'nextline nil))
1037 ;; These footnote lines have been read and saved before,
1038 ;; ignore them at this time.
1039 (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
1040 (org-export-docbook-close-para-maybe)
1041 (throw 'nextline nil)))
1043 ;; FIXME: It might be a good idea to add an option to
1044 ;; support line break processing instruction <?linebreak?>.
1045 ;; Org-mode supports line break "\\" in HTML exporter, and
1046 ;; some DocBook users may also want to force line breaks
1047 ;; even though DocBook only supports that in
1048 ;; <literallayout>.
1050 (insert line "\n")))))
1052 ;; Properly close all local lists and other lists
1053 (when inquote
1054 (insert "]]></programlisting>\n")
1055 (org-export-docbook-open-para))
1057 ;; Close all open sections.
1058 (org-export-docbook-level-start 1 nil)
1060 (unless (plist-get opt-plist :buffer-will-be-killed)
1061 (normal-mode)
1062 (if (eq major-mode (default-value 'major-mode))
1063 (nxml-mode)))
1065 ;; Remove empty paragraphs. Replace them with a newline.
1066 (goto-char (point-min))
1067 (while (re-search-forward
1068 "[ \r\n\t]*\\(<para>\\)[ \r\n\t]*</para>[ \r\n\t]*" nil t)
1069 (when (not (get-text-property (match-beginning 1) 'org-protected))
1070 (replace-match "\n")
1071 (backward-char 1)))
1072 ;; Fill empty sections with <para></para>. This is to make sure
1073 ;; that the DocBook document generated is valid and well-formed.
1074 (goto-char (point-min))
1075 (while (re-search-forward
1076 "</title>\\([ \r\n\t]*\\)</section>" nil t)
1077 (when (not (get-text-property (match-beginning 0) 'org-protected))
1078 (replace-match "\n<para></para>\n" nil nil nil 1)))
1079 ;; Insert the last closing tag.
1080 (goto-char (point-max))
1081 (unless body-only
1082 (insert "</article>"))
1083 (run-hooks 'org-export-docbook-final-hook)
1084 (or to-buffer (save-buffer))
1085 (goto-char (point-min))
1086 (or (org-export-push-to-kill-ring "DocBook")
1087 (message "Exporting... done"))
1088 (if (eq to-buffer 'string)
1089 (prog1 (buffer-substring (point-min) (point-max))
1090 (kill-buffer (current-buffer)))
1091 (current-buffer)))))
1093 (defun org-export-docbook-open-para ()
1094 "Insert <para>, but first close previous paragraph if any."
1095 (org-export-docbook-close-para-maybe)
1096 (insert "\n<para>")
1097 (setq org-docbook-para-open t))
1099 (defun org-export-docbook-close-para-maybe ()
1100 "Close DocBook paragraph if there is one open."
1101 (when org-docbook-para-open
1102 (insert "</para>\n")
1103 (setq org-docbook-para-open nil)))
1105 (defun org-export-docbook-close-li (&optional type)
1106 "Close list if necessary."
1107 (org-export-docbook-close-para-maybe)
1108 (if (equal type "d")
1109 (insert "</listitem></varlistentry>\n")
1110 (insert "</listitem>\n")))
1112 (defun org-export-docbook-level-start (level title)
1113 "Insert a new level in DocBook export.
1114 When TITLE is nil, just close all open levels."
1115 (org-export-docbook-close-para-maybe)
1116 (let* ((target (and title (org-get-text-property-any 0 'target title)))
1117 (l org-level-max)
1118 section-number)
1119 (while (>= l level)
1120 (if (aref org-levels-open (1- l))
1121 (progn
1122 (insert "</section>\n")
1123 (aset org-levels-open (1- l) nil)))
1124 (setq l (1- l)))
1125 (when title
1126 ;; If title is nil, this means this function is called to close
1127 ;; all levels, so the rest is done only if title is given.
1129 ;; Format tags: put them into a superscript like format.
1130 (when (string-match (org-re "\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") title)
1131 (setq title
1132 (replace-match
1133 (if org-export-with-tags
1134 (save-match-data
1135 (concat
1136 "<superscript>"
1137 (match-string 1 title)
1138 "</superscript>"))
1140 t t title)))
1141 (aset org-levels-open (1- level) t)
1142 (setq section-number (org-section-number level))
1143 (insert (format "\n<section xml:id=\"%s%s\">\n<title>%s</title>"
1144 org-export-docbook-section-id-prefix
1145 (replace-regexp-in-string "\\." "_" section-number)
1146 title))
1147 (org-export-docbook-open-para))))
1149 (defun org-docbook-expand (string)
1150 "Prepare STRING for DocBook export.
1151 Applies all active conversions. If there are links in the
1152 string, don't modify these."
1153 (let* ((re (concat org-bracket-link-regexp "\\|"
1154 (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")))
1155 m s l res)
1156 (while (setq m (string-match re string))
1157 (setq s (substring string 0 m)
1158 l (match-string 0 string)
1159 string (substring string (match-end 0)))
1160 (push (org-docbook-do-expand s) res)
1161 (push l res))
1162 (push (org-docbook-do-expand string) res)
1163 (apply 'concat (nreverse res))))
1165 (defun org-docbook-do-expand (s)
1166 "Apply all active conversions to translate special ASCII to DocBook."
1167 (setq s (org-html-protect s))
1168 (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
1169 (setq s (replace-match "<\\1>" t nil s)))
1170 (if org-export-with-emphasize
1171 (setq s (org-export-docbook-convert-emphasize s)))
1172 (if org-export-with-special-strings
1173 (setq s (org-export-docbook-convert-special-strings s)))
1174 (if org-export-with-sub-superscripts
1175 (setq s (org-export-docbook-convert-sub-super s)))
1176 (if org-export-with-TeX-macros
1177 (let ((start 0) wd rep)
1178 (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)\\({}\\)?"
1179 s start))
1180 (if (get-text-property (match-beginning 0) 'org-protected s)
1181 (setq start (match-end 0))
1182 (setq wd (match-string 1 s))
1183 (if (setq rep (org-entity-get-representation wd 'html))
1184 (setq s (replace-match rep t t s))
1185 (setq start (+ start (length wd))))))))
1188 (defun org-export-docbook-format-desc (desc)
1189 "Make sure DESC is valid as a description in a link."
1190 (save-match-data
1191 (org-docbook-do-expand desc)))
1193 (defun org-export-docbook-convert-emphasize (string)
1194 "Apply emphasis for DocBook exporting."
1195 (let ((s 0) rpl)
1196 (while (string-match org-emph-re string s)
1197 (if (not (equal
1198 (substring string (match-beginning 3) (1+ (match-beginning 3)))
1199 (substring string (match-beginning 4) (1+ (match-beginning 4)))))
1200 (setq s (match-beginning 0)
1202 (concat
1203 (match-string 1 string)
1204 (nth 1 (assoc (match-string 3 string)
1205 org-export-docbook-emphasis-alist))
1206 (match-string 4 string)
1207 (nth 2 (assoc (match-string 3 string)
1208 org-export-docbook-emphasis-alist))
1209 (match-string 5 string))
1210 string (replace-match rpl t t string)
1211 s (+ s (- (length rpl) 2)))
1212 (setq s (1+ s))))
1213 string))
1215 (defun org-docbook-protect (string)
1216 (org-html-protect string))
1218 ;; For now, simply return string as it is.
1219 (defun org-export-docbook-convert-special-strings (string)
1220 "Convert special characters in STRING to DocBook."
1221 string)
1223 (defun org-export-docbook-get-footnotes (lines)
1224 "Given a list of LINES, return a list of alist footnotes."
1225 (let ((list nil) line)
1226 (while (setq line (pop lines))
1227 (if (string-match "^[ \t]*\\[\\([0-9]+\\)\\] \\(.+\\)" line)
1228 (push (cons (match-string 1 line) (match-string 2 line))
1229 list)))
1230 list))
1232 (defun org-export-docbook-format-image (src)
1233 "Create image element in DocBook."
1234 (save-match-data
1235 (let* ((caption (org-find-text-property-in-string 'org-caption src))
1236 (attr (or (org-find-text-property-in-string 'org-attributes src)
1237 ""))
1238 (label (org-find-text-property-in-string 'org-label src))
1239 (default-attr org-export-docbook-default-image-attributes)
1240 tmp)
1241 (setq caption (and caption (org-html-do-expand caption)))
1242 (while (setq tmp (pop default-attr))
1243 (if (not (string-match (concat (car tmp) "=") attr))
1244 (setq attr (concat attr " " (car tmp) "=" (cdr tmp)))))
1245 (format "<mediaobject%s>
1246 <imageobject>\n<imagedata fileref=\"%s\" %s/>\n</imageobject>
1247 %s</mediaobject>"
1248 (if label (concat " xml:id=\"" label "\"") "")
1249 src attr
1250 (if caption
1251 (concat "<caption>\n<para>"
1252 caption
1253 "</para>\n</caption>\n")
1255 ))))
1257 (defun org-export-docbook-preprocess (parameters)
1258 "Extra preprocessing work for DocBook export."
1259 ;; Merge lines starting with "\par" to one line. Such lines are
1260 ;; regarded as the continuation of a long footnote.
1261 (goto-char (point-min))
1262 (while (re-search-forward "\n\\(\\\\par\\>\\)" nil t)
1263 (if (not (get-text-property (match-beginning 1) 'org-protected))
1264 (replace-match ""))))
1266 (defun org-export-docbook-finalize-table (table)
1267 "Clean up TABLE and turn it into DocBook format.
1268 This function adds a label to the table if it is available, and
1269 also changes TABLE to informaltable if caption does not exist.
1270 TABLE is a string containing the HTML code generated by
1271 `org-format-table-html' for a table in Org-mode buffer."
1272 (let (table-with-label)
1273 ;; Get the label if it exists, and move it into the <table> element.
1274 (setq table-with-label
1275 (if (string-match
1276 "^<table \\(\\(.\\|\n\\)+\\)<a name=\"\\(.+\\)\" id=\".+\"></a>\n\\(\\(.\\|\n\\)+\\)</table>"
1277 table)
1278 (replace-match (concat "<table xml:id=\"" (match-string 3 table) "\" "
1279 (match-string 1 table)
1280 (match-string 4 table)
1281 "</table>")
1282 nil t table)
1283 table))
1284 ;; Change <table> into <informaltable> if caption does not exist.
1285 (if (string-match
1286 "^<table \\(\\(.\\|\n\\)+\\)<caption></caption>\n\\(\\(.\\|\n\\)+\\)</table>"
1287 table-with-label)
1288 (replace-match (concat "<informaltable "
1289 (match-string 1 table-with-label)
1290 (match-string 3 table-with-label)
1291 "</informaltable>")
1292 nil t table-with-label)
1293 table-with-label)))
1295 ;; Note: This function is very similar to
1296 ;; org-export-html-convert-sub-super. They can be merged in the future.
1297 (defun org-export-docbook-convert-sub-super (string)
1298 "Convert sub- and superscripts in STRING for DocBook."
1299 (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
1300 (while (string-match org-match-substring-regexp string s)
1301 (cond
1302 ((and requireb (match-end 8)) (setq s (match-end 2)))
1303 ((get-text-property (match-beginning 2) 'org-protected string)
1304 (setq s (match-end 2)))
1306 (setq s (match-end 1)
1307 key (if (string= (match-string 2 string) "_")
1308 "subscript"
1309 "superscript")
1310 c (or (match-string 8 string)
1311 (match-string 6 string)
1312 (match-string 5 string))
1313 string (replace-match
1314 (concat (match-string 1 string)
1315 "<" key ">" c "</" key ">")
1316 t t string)))))
1317 (while (string-match "\\\\\\([_^]\\)" string)
1318 (setq string (replace-match (match-string 1 string) t t string)))
1319 string))
1321 (defun org-export-docbook-protect-tags (string)
1322 "Change ``<...>'' in string STRING into ``@<...>''.
1323 This is normally needed when STRING contains DocBook elements
1324 that need to be preserved in later phase of DocBook exporting."
1325 (let ((start 0))
1326 (while (string-match "<\\([^>]*\\)>" string start)
1327 (setq string (replace-match
1328 "@<\\1>" t nil string)
1329 start (match-end 0)))
1330 string))
1332 (defun org-export-docbook-handle-time-stamps (line)
1333 "Format time stamps in string LINE."
1334 (let (replaced
1335 (kw-markup (org-export-docbook-protect-tags
1336 org-export-docbook-keywords-markup))
1337 (ts-markup (org-export-docbook-protect-tags
1338 org-export-docbook-timestamp-markup)))
1339 (while (string-match org-maybe-keyword-time-regexp line)
1340 (setq replaced
1341 (concat replaced
1342 (substring line 0 (match-beginning 0))
1343 (if (match-end 1)
1344 (format kw-markup
1345 (match-string 1 line)))
1347 (format ts-markup
1348 (substring (org-translate-time
1349 (match-string 3 line)) 1 -1)))
1350 line (substring line (match-end 0))))
1351 (concat replaced line)))
1353 (defun org-export-docbook-list-line (line pos struct prevs)
1354 "Insert list syntax in export buffer. Return LINE, maybe modified.
1356 POS is the item position or line position the line had before
1357 modifications to buffer. STRUCT is the list structure. PREVS is
1358 the alist of previous items."
1359 (let* ((get-type
1360 (function
1361 ;; Translate type of list containing POS to "ordered",
1362 ;; "variable" or "itemized".
1363 (lambda (pos struct prevs)
1364 (let ((type (org-list-get-list-type pos struct prevs)))
1365 (cond
1366 ((eq 'ordered type) "ordered")
1367 ((eq 'descriptive type) "variable")
1368 (t "itemized"))))))
1369 (get-closings
1370 (function
1371 ;; Return list of all items and sublists ending at POS, in
1372 ;; reverse order.
1373 (lambda (pos)
1374 (let (out)
1375 (catch 'exit
1376 (mapc (lambda (e)
1377 (let ((end (nth 6 e))
1378 (item (car e)))
1379 (cond
1380 ((= end pos) (push item out))
1381 ((>= item pos) (throw 'exit nil)))))
1382 struct))
1383 out)))))
1384 ;; First close any previous item, or list, ending at POS.
1385 (mapc (lambda (e)
1386 (let* ((lastp (= (org-list-get-last-item e struct prevs) e))
1387 (first-item (org-list-get-list-begin e struct prevs))
1388 (type (funcall get-type first-item struct prevs)))
1389 ;; Ending for every item
1390 (org-export-docbook-close-para-maybe)
1391 (insert (if (equal type "variable")
1392 "</listitem></varlistentry>\n"
1393 "</listitem>\n"))
1394 ;; We're ending last item of the list: end list.
1395 (when lastp
1396 (insert (format "</%slist>\n" type))
1397 (org-export-docbook-open-para))))
1398 (funcall get-closings pos))
1399 (cond
1400 ;; At an item: insert appropriate tags in export buffer.
1401 ((assq pos struct)
1402 (string-match (concat "[ \t]*\\(\\S-+[ \t]*\\)"
1403 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[a-zA-Z]\\)\\][ \t]*\\)?"
1404 "\\(?:\\(\\[[ X-]\\]\\)[ \t]+\\)?"
1405 "\\(?:\\(.*\\)[ \t]+::\\(?:[ \t]+\\|$\\)\\)?"
1406 "\\(.*\\)")
1407 line)
1408 (let* ((checkbox (match-string 3 line))
1409 (desc-tag (or (match-string 4 line) "???"))
1410 (body (match-string 5 line))
1411 (list-beg (org-list-get-list-begin pos struct prevs))
1412 (firstp (= list-beg pos))
1413 ;; Always refer to first item to determine list type, in
1414 ;; case list is ill-formed.
1415 (type (funcall get-type list-beg struct prevs))
1416 ;; Special variables for ordered lists.
1417 (counter (let ((count-tmp (org-list-get-counter pos struct)))
1418 (cond
1419 ((not count-tmp) nil)
1420 ((string-match "[A-Za-z]" count-tmp)
1421 (- (string-to-char (upcase count-tmp)) 64))
1422 ((string-match "[0-9]+" count-tmp)
1423 count-tmp)))))
1424 ;; When FIRSTP, a new list or sub-list is starting.
1425 (when firstp
1426 (org-export-docbook-close-para-maybe)
1427 (insert (format "<%slist>\n" type)))
1428 (insert (cond
1429 ((equal type "variable")
1430 (format "<varlistentry><term>%s</term><listitem>" desc-tag))
1431 ((and (equal type "ordered") counter)
1432 (format "<listitem override=\"%s\">" counter))
1433 (t "<listitem>")))
1434 ;; For DocBook, we need to open a para right after tag
1435 ;; <listitem>.
1436 (org-export-docbook-open-para)
1437 ;; If line had a checkbox, some additional modification is required.
1438 (when checkbox (setq body (concat checkbox " " body)))
1439 ;; Return modified line
1440 body))
1441 ;; At a list ender: normal text follows: need <para>.
1442 ((equal "ORG-LIST-END-MARKER" line)
1443 (org-export-docbook-open-para)
1444 (throw 'nextline nil))
1445 ;; Not at an item: return line unchanged (side-effects only).
1446 (t line))))
1448 (provide 'org-docbook)
1450 ;;; org-docbook.el ends here