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