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