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