Release 6.26b
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / org-docbook.el
blob4e606fa90dea36042bad289fe08cce58ceb3abb1
1 ;;; org-docbook.el --- DocBook exporter for org-mode
2 ;;
3 ;; Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
4 ;;
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: org-docbook.el
7 ;; Version: 6.26b
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 ;; $Id: org-docbook.el 35 2009-03-23 01:03:21Z baoqiu $
13 ;; URL:
15 ;; This file is NOT part of GNU Emacs.
17 ;; GNU Emacs is free software: you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation, either version 3 of the License, or
20 ;; (at your option) any later version.
22 ;; GNU Emacs is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;; GNU General Public License for more details.
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
30 ;; Commentary:
32 ;; This library implements a DocBook exporter for org-mode. The basic
33 ;; idea and design is very similar to what `org-export-as-html' has.
34 ;; Code prototype was also started with `org-export-as-html'.
36 ;; Put this file into your load-path and the following line into your
37 ;; ~/.emacs:
39 ;; (require 'org-docbook)
41 ;; The interactive functions are similar to those of the HTML and LaTeX
42 ;; exporters:
44 ;; M-x `org-export-as-docbook'
45 ;; M-x `org-export-as-docbook-pdf'
46 ;; M-x `org-export-as-docbook-pdf-and-open'
47 ;; M-x `org-export-as-docbook-batch'
48 ;; M-x `org-export-as-docbook-to-buffer'
49 ;; M-x `org-export-region-as-docbook'
50 ;; M-x `org-replace-region-by-docbook'
52 ;; Note that, in order to generate PDF files using the DocBook XML files
53 ;; created by DocBook exporter, the following two variables have to be
54 ;; set based on what DocBook tools you use for XSLT processor and XSL-FO
55 ;; processor:
57 ;; org-export-docbook-xslt-proc-command
58 ;; org-export-docbook-xsl-fo-proc-command
60 ;; Check the document of these two variables to see examples of how they
61 ;; can be set.
63 ;; If the Org file to be exported contains special characters written in
64 ;; TeX-like syntax, like \alpha and \beta, you need to include the right
65 ;; entity file(s) in the DOCTYPE declaration for the DocBook XML file.
66 ;; This is required to make the DocBook XML file valid. The DOCTYPE
67 ;; declaration string can be set using the following variable:
69 ;; org-export-docbook-doctype
71 ;;; Code:
73 (eval-when-compile
74 (require 'cl))
76 (require 'footnote)
77 (require 'org)
78 (require 'org-exp)
79 (require 'org-html)
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 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. Like
146 `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 "Alist 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-proc-command nil
188 "XSLT processor command used by DocBook exporter.
189 This is the command used to process a DocBook XML file to
190 generate the formatting object (FO) file.
192 The value of this variable should be a format control string that
193 includes two `%s' arguments: the first one is for the output FO
194 file name, and the second one is for the input DocBook XML file
195 name.
197 For example, if you use Saxon as the XSLT processor, you may want
198 to set the variable to
200 \"java com.icl.saxon.StyleSheet -o %s %s /path/to/docbook.xsl\"
202 If you use Xalan, you can set it to
204 \"java org.apache.xalan.xslt.Process -out %s -in %s -xsl /path/to/docbook.xsl\"
206 For xsltproc, the following string should work:
208 \"xsltproc --output %s /path/to/docbook.xsl %s\"
210 You need to replace \"/path/to/docbook.xsl\" with the actual path
211 to the DocBook stylesheet file on your machine. You can also
212 replace it with your own customization layer if you have one.
214 You can include additional stylesheet parameters in this command.
215 Just make sure that they meet the syntax requirement of each
216 processor."
217 :group 'org-export-docbook
218 :type 'string)
220 (defcustom org-export-docbook-xsl-fo-proc-command nil
221 "XSL-FO processor command used by DocBook exporter.
222 This is the command used to process a formatting object (FO) file
223 to generate the PDF file.
225 The value of this variable should be a format control string that
226 includes two `%s' arguments: the first one is for the input FO
227 file name, and the second one is for the output PDF file name.
229 For example, if you use FOP as the XSL-FO processor, you can set
230 the variable to
232 \"fop %s %s\""
233 :group 'org-export-docbook
234 :type 'string)
236 (defcustom org-export-docbook-keywords-markup "<literal>%s</literal>"
237 "A printf format string to be applied to keywords by DocBook exporter."
238 :group 'org-export-docbook
239 :type 'string)
241 (defcustom org-export-docbook-timestamp-markup "<emphasis>%s</emphasis>"
242 "A printf format string to be applied to time stamps by DocBook exporter."
243 :group 'org-export-docbook
244 :type 'string)
246 ;;; Autoload functions:
248 ;;;###autoload
249 (defun org-export-as-docbook-batch ()
250 "Call `org-export-as-docbook' in batch style.
251 This function can be used in batch processing.
253 For example:
255 $ emacs --batch
256 --load=$HOME/lib/emacs/org.el
257 --visit=MyOrgFile.org --funcall org-export-as-docbook-batch"
258 (org-export-as-docbook 'hidden))
260 ;;;###autoload
261 (defun org-export-as-docbook-to-buffer ()
262 "Call `org-export-as-docbook' with output to a temporary buffer.
263 No file is created."
264 (interactive)
265 (org-export-as-docbook nil nil "*Org DocBook Export*")
266 (switch-to-buffer-other-window "*Org DocBook Export*"))
268 ;;;###autoload
269 (defun org-replace-region-by-docbook (beg end)
270 "Replace the region from BEG to END with its DocBook export.
271 It assumes the region has `org-mode' syntax, and then convert it to
272 DocBook. This can be used in any buffer. For example, you could
273 write an itemized list in `org-mode' syntax in an DocBook buffer and
274 then use this command to convert it."
275 (interactive "r")
276 (let (reg docbook buf)
277 (save-window-excursion
278 (if (org-mode-p)
279 (setq docbook (org-export-region-as-docbook
280 beg end t 'string))
281 (setq reg (buffer-substring beg end)
282 buf (get-buffer-create "*Org tmp*"))
283 (save-excursion
284 (set-buffer buf)
285 (erase-buffer)
286 (insert reg)
287 (org-mode)
288 (setq docbook (org-export-region-as-docbook
289 (point-min) (point-max) t 'string)))
290 (kill-buffer buf)))
291 (delete-region beg end)
292 (insert docbook)))
294 ;;;###autoload
295 (defun org-export-region-as-docbook (beg end &optional body-only buffer)
296 "Convert region from BEG to END in `org-mode' buffer to DocBook.
297 If prefix arg BODY-ONLY is set, omit file header and footer and
298 only produce the region of converted text, useful for
299 cut-and-paste operations. If BUFFER is a buffer or a string,
300 use/create that buffer as a target of the converted DocBook. If
301 BUFFER is the symbol `string', return the produced DocBook as a
302 string and leave not buffer behind. For example, a Lisp program
303 could call this function in the following way:
305 (setq docbook (org-export-region-as-docbook beg end t 'string))
307 When called interactively, the output buffer is selected, and shown
308 in a window. A non-interactive call will only retunr the buffer."
309 (interactive "r\nP")
310 (when (interactive-p)
311 (setq buffer "*Org DocBook Export*"))
312 (let ((transient-mark-mode t)
313 (zmacs-regions t)
314 rtn)
315 (goto-char end)
316 (set-mark (point)) ;; To activate the region
317 (goto-char beg)
318 (setq rtn (org-export-as-docbook
319 nil nil
320 buffer body-only))
321 (if (fboundp 'deactivate-mark) (deactivate-mark))
322 (if (and (interactive-p) (bufferp rtn))
323 (switch-to-buffer-other-window rtn)
324 rtn)))
326 ;;;###autoload
327 (defun org-export-as-docbook-pdf (&optional hidden ext-plist
328 to-buffer body-only pub-dir)
329 "Export as DocBook XML file, and generate PDF file."
330 (interactive "P")
331 (if (or (not org-export-docbook-xslt-proc-command)
332 (not (string-match "%s.+%s" org-export-docbook-xslt-proc-command)))
333 (error "XSLT processor command is not set correctly"))
334 (if (or (not org-export-docbook-xsl-fo-proc-command)
335 (not (string-match "%s.+%s" org-export-docbook-xsl-fo-proc-command)))
336 (error "XSL-FO processor command is not set correctly"))
337 (message "Exporting to PDF...")
338 (let* ((wconfig (current-window-configuration))
339 (docbook-buf (org-export-as-docbook hidden ext-plist
340 to-buffer body-only pub-dir))
341 (filename (buffer-file-name docbook-buf))
342 (base (file-name-sans-extension filename))
343 (fofile (concat base ".fo"))
344 (pdffile (concat base ".pdf")))
345 (and (file-exists-p pdffile) (delete-file pdffile))
346 (message "Processing DocBook XML file...")
347 (shell-command (format org-export-docbook-xslt-proc-command
348 fofile (shell-quote-argument filename)))
349 (shell-command (format org-export-docbook-xsl-fo-proc-command
350 fofile pdffile))
351 (message "Processing DocBook file...done")
352 (if (not (file-exists-p pdffile))
353 (error "PDF file was not produced")
354 (set-window-configuration wconfig)
355 (message "Exporting to PDF...done")
356 pdffile)))
358 ;;;###autoload
359 (defun org-export-as-docbook-pdf-and-open ()
360 "Export as DocBook XML file, generate PDF file, and open it."
361 (interactive)
362 (let ((pdffile (org-export-as-docbook-pdf)))
363 (if pdffile
364 (org-open-file pdffile)
365 (error "PDF file was not produced"))))
367 ;;;###autoload
368 (defun org-export-as-docbook (&optional hidden ext-plist
369 to-buffer body-only pub-dir)
370 "Export the current buffer as a DocBook file.
371 If there is an active region, export only the region. When
372 HIDDEN is non-nil, don't display the HTML buffer. EXT-PLIST is a
373 property list with external parameters overriding org-mode's
374 default settings, but still inferior to file-local settings.
375 When TO-BUFFER is non-nil, create a buffer with that name and
376 export to that buffer. If TO-BUFFER is the symbol `string',
377 don't leave any buffer behind but just return the resulting HTML
378 as a string. When BODY-ONLY is set, don't produce the file
379 header and footer, simply return the content of the document (all
380 top-level sections). When PUB-DIR is set, use this as the
381 publishing directory."
382 (interactive "P")
383 ;; Make sure we have a file name when we need it.
384 (when (and (not (or to-buffer body-only))
385 (not buffer-file-name))
386 (if (buffer-base-buffer)
387 (org-set-local 'buffer-file-name
388 (with-current-buffer (buffer-base-buffer)
389 buffer-file-name))
390 (error "Need a file name to be able to export.")))
392 (message "Exporting...")
393 (setq-default org-todo-line-regexp org-todo-line-regexp)
394 (setq-default org-deadline-line-regexp org-deadline-line-regexp)
395 (setq-default org-done-keywords org-done-keywords)
396 (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
397 (let* ((opt-plist
398 (org-export-process-option-filters
399 (org-combine-plists (org-default-export-plist)
400 ext-plist
401 (org-infile-export-plist))))
402 (link-validate (plist-get opt-plist :link-validation-function))
403 valid
404 (odd org-odd-levels-only)
405 (region-p (org-region-active-p))
406 (rbeg (and region-p (region-beginning)))
407 (rend (and region-p (region-end)))
408 (subtree-p
409 (if (plist-get opt-plist :ignore-subree-p)
411 (when region-p
412 (save-excursion
413 (goto-char rbeg)
414 (and (org-at-heading-p)
415 (>= (org-end-of-subtree t t) rend))))))
416 (level-offset (if subtree-p
417 (save-excursion
418 (goto-char rbeg)
419 (+ (funcall outline-level)
420 (if org-odd-levels-only 1 0)))
422 (opt-plist (if subtree-p
423 (org-export-add-subtree-options opt-plist rbeg)
424 opt-plist))
425 ;; The following two are dynamically scoped into other
426 ;; routines below.
427 (org-current-export-dir
428 (or pub-dir (org-export-directory :docbook opt-plist)))
429 (org-current-export-file buffer-file-name)
430 (level 0) (line "") (origline "") txt todo
431 (filename (if to-buffer nil
432 (expand-file-name
433 (concat
434 (file-name-sans-extension
435 (or (and subtree-p
436 (org-entry-get (region-beginning)
437 "EXPORT_FILE_NAME" t))
438 (file-name-nondirectory buffer-file-name)))
439 org-export-docbook-extension)
440 (file-name-as-directory
441 (or pub-dir (org-export-directory :docbook opt-plist))))))
442 (current-dir (if buffer-file-name
443 (file-name-directory buffer-file-name)
444 default-directory))
445 (buffer (if to-buffer
446 (cond
447 ((eq to-buffer 'string) (get-buffer-create "*Org DocBook Export*"))
448 (t (get-buffer-create to-buffer)))
449 (find-file-noselect filename)))
450 ;; org-levels-open is a global variable
451 (org-levels-open (make-vector org-level-max nil))
452 (date (plist-get opt-plist :date))
453 (author (or (plist-get opt-plist :author)
454 user-full-name))
455 (email (plist-get opt-plist :email))
456 firstname othername surname
457 (title (or (and subtree-p (org-export-get-title-from-subtree))
458 (plist-get opt-plist :title)
459 (and (not
460 (plist-get opt-plist :skip-before-1st-heading))
461 (org-export-grab-title-from-buffer))
462 (and buffer-file-name
463 (file-name-sans-extension
464 (file-name-nondirectory buffer-file-name)))
465 "UNTITLED"))
466 ;; We will use HTML table formatter to export tables to DocBook
467 ;; format, so need to set html-table-tag here.
468 (html-table-tag (plist-get opt-plist :html-table-tag))
469 (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
470 (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
471 (inquote nil)
472 (infixed nil)
473 (inverse nil)
474 (in-local-list nil)
475 (local-list-type nil)
476 (local-list-indent nil)
477 (llt org-plain-list-ordered-item-terminator)
478 (email (plist-get opt-plist :email))
479 (language (plist-get opt-plist :language))
480 (lang-words nil)
482 (start 0)
483 (coding-system (and (boundp 'buffer-file-coding-system)
484 buffer-file-coding-system))
485 (coding-system-for-write (or org-export-docbook-coding-system
486 coding-system))
487 (save-buffer-coding-system (or org-export-docbook-coding-system
488 coding-system))
489 (charset (and coding-system-for-write
490 (fboundp 'coding-system-get)
491 (coding-system-get coding-system-for-write
492 'mime-charset)))
493 (region
494 (buffer-substring
495 (if region-p (region-beginning) (point-min))
496 (if region-p (region-end) (point-max))))
497 (lines
498 (org-split-string
499 (org-export-preprocess-string
500 region
501 :emph-multiline t
502 :for-docbook t
503 :skip-before-1st-heading
504 (plist-get opt-plist :skip-before-1st-heading)
505 :drawers (plist-get opt-plist :drawers)
506 :todo-keywords (plist-get opt-plist :todo-keywords)
507 :tags (plist-get opt-plist :tags)
508 :priority (plist-get opt-plist :priority)
509 :footnotes (plist-get opt-plist :footnotes)
510 :timestamps (plist-get opt-plist :timestamps)
511 :archived-trees
512 (plist-get opt-plist :archived-trees)
513 :select-tags (plist-get opt-plist :select-tags)
514 :exclude-tags (plist-get opt-plist :exclude-tags)
515 :add-text
516 (plist-get opt-plist :text)
517 :LaTeX-fragments
518 (plist-get opt-plist :LaTeX-fragments))
519 "[\r\n]"))
520 ;; Use literal output to show check boxes.
521 (checkbox-start
522 (nth 1 (assoc "=" org-export-docbook-emphasis-alist)))
523 (checkbox-end
524 (nth 2 (assoc "=" org-export-docbook-emphasis-alist)))
525 table-open type
526 table-buffer table-orig-buffer
527 ind item-type starter didclose
528 rpl path attr caption label desc descp desc1 desc2 link
529 fnc item-tag
530 footref-seen footnote-list
531 id-file
534 ;; Fine detailed info about author name.
535 (if (string-match "\\([^ ]+\\) \\(.+ \\)?\\([^ ]+\\)" author)
536 (progn
537 (setq firstname (match-string 1 author)
538 othername (or (match-string 2 author) "")
539 surname (match-string 3 author))))
541 ;; Get all footnote text.
542 (setq footnote-list
543 (org-export-docbook-get-footnotes lines))
545 (let ((inhibit-read-only t))
546 (org-unmodified
547 (remove-text-properties (point-min) (point-max)
548 '(:org-license-to-kill t))))
550 (setq org-min-level (org-get-min-level lines level-offset))
551 (setq org-last-level org-min-level)
552 (org-init-section-numbers)
554 ;; Get and save the date.
555 (cond
556 ((and date (string-match "%" date))
557 (setq date (format-time-string date)))
558 (date)
559 (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
561 ;; Get the language-dependent settings
562 (setq lang-words (or (assoc language org-export-language-setup)
563 (assoc "en" org-export-language-setup)))
565 ;; Switch to the output buffer. Use fundamental-mode for now. We
566 ;; could turn on nXML mode later and do some indentation.
567 (set-buffer buffer)
568 (let ((inhibit-read-only t)) (erase-buffer))
569 (fundamental-mode)
571 (and (fboundp 'set-buffer-file-coding-system)
572 (set-buffer-file-coding-system coding-system-for-write))
574 ;; The main body...
575 (let ((case-fold-search nil)
576 (org-odd-levels-only odd))
578 ;; Create local variables for all options, to make sure all called
579 ;; functions get the correct information
580 (mapc (lambda (x)
581 (set (make-local-variable (nth 2 x))
582 (plist-get opt-plist (car x))))
583 org-export-plist-vars)
585 ;; Insert DocBook file header, title, and author info.
586 (unless body-only
587 (insert org-export-docbook-header)
588 (if org-export-docbook-doctype
589 (insert org-export-docbook-doctype))
590 (insert "<!-- Date: " date " -->\n")
591 (insert (format "<!-- DocBook XML file generated by Org-mode %s Emacs %s -->\n"
592 org-version emacs-major-version))
593 (insert org-export-docbook-article-header)
594 (insert (format
595 "\n <title>%s</title>
596 <info>
597 <author>
598 <personname>
599 <firstname>%s</firstname> <othername>%s</othername> <surname>%s</surname>
600 </personname>
602 </author>
603 </info>\n"
604 (org-docbook-expand title)
605 firstname othername surname
606 (if email (concat "<email>" email "</email>") "")
609 (org-init-section-numbers)
611 (org-export-docbook-open-para)
613 ;; Loop over all the lines...
614 (while (setq line (pop lines) origline line)
615 (catch 'nextline
617 ;; End of quote section?
618 (when (and inquote (string-match "^\\*+ " line))
619 (insert "]]>\n</programlisting>\n")
620 (org-export-docbook-open-para)
621 (setq inquote nil))
622 ;; Inside a quote section?
623 (when inquote
624 (insert (org-docbook-protect line) "\n")
625 (throw 'nextline nil))
627 ;; Fixed-width, verbatim lines (examples)
628 (when (and org-export-with-fixed-width
629 (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line))
630 (when (not infixed)
631 (setq infixed t)
632 (org-export-docbook-close-para-maybe)
633 (insert "<programlisting><![CDATA["))
634 (insert (match-string 3 line) "\n")
635 (when (or (not lines)
636 (not (string-match "^[ \t]*\\(:.*\\)"
637 (car lines))))
638 (setq infixed nil)
639 (insert "]]>\n</programlisting>\n")
640 (org-export-docbook-open-para))
641 (throw 'nextline nil))
643 ;; Protected HTML
644 (when (get-text-property 0 'org-protected line)
645 (let (par)
646 (when (re-search-backward
647 "\\(<para>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
648 (setq par (match-string 1))
649 (replace-match "\\2\n"))
650 (insert line "\n")
651 (while (and lines
652 (or (= (length (car lines)) 0)
653 (get-text-property 0 'org-protected (car lines))))
654 (insert (pop lines) "\n"))
655 (and par (insert "<para>\n")))
656 (throw 'nextline nil))
658 ;; Start of block quotes and verses
659 (when (or (equal "ORG-BLOCKQUOTE-START" line)
660 (and (equal "ORG-VERSE-START" line)
661 (setq inverse t)))
662 (org-export-docbook-close-para-maybe)
663 (insert "<blockquote>")
664 ;; Check whether attribution for this blockquote exists.
665 (let (tmp1
666 attribution
667 (end (if inverse "ORG-VERSE-END" "ORG-BLOCKQUOTE-END"))
668 (quote-lines nil))
669 (while (and (setq tmp1 (pop lines))
670 (not (equal end tmp1)))
671 (push tmp1 quote-lines))
672 (push tmp1 lines) ; Put back quote end mark
673 ;; Check the last line in the quote to see if it contains
674 ;; the attribution.
675 (setq tmp1 (pop quote-lines))
676 (if (string-match "\\(^.*\\)\\(--[ \t]+\\)\\(.+\\)$" tmp1)
677 (progn
678 (setq attribution (match-string 3 tmp1))
679 (when (save-match-data
680 (string-match "[^ \t]" (match-string 1 tmp1)))
681 (push (match-string 1 tmp1) lines)))
682 (push tmp1 lines))
683 (while (setq tmp1 (pop quote-lines))
684 (push tmp1 lines))
685 (when attribution
686 (insert "<attribution>" attribution "</attribution>")))
687 ;; Insert <literallayout> for verse.
688 (if inverse
689 (insert "\n<literallayout>")
690 (org-export-docbook-open-para))
691 (throw 'nextline nil))
693 ;; End of block quotes
694 (when (equal "ORG-BLOCKQUOTE-END" line)
695 (org-export-docbook-close-para-maybe)
696 (insert "</blockquote>\n")
697 (org-export-docbook-open-para)
698 (throw 'nextline nil))
700 ;; End of verses
701 (when (equal "ORG-VERSE-END" line)
702 (insert "</literallayout>\n</blockquote>\n")
703 (org-export-docbook-open-para)
704 (setq inverse nil)
705 (throw 'nextline nil))
707 ;; Text centering. Element <para role="centered"> does not
708 ;; seem to work with FOP, so for now we use <informaltable> to
709 ;; center the text, which can contain multiple paragraphs.
710 (when (equal "ORG-CENTER-START" line)
711 (org-export-docbook-close-para-maybe)
712 (insert "<informaltable frame=\"none\" colsep=\"0\" rowsep=\"0\">\n"
713 "<tgroup align=\"center\" cols=\"1\">\n"
714 "<tbody><row><entry>\n")
715 (org-export-docbook-open-para)
716 (throw 'nextline nil))
718 (when (equal "ORG-CENTER-END" line)
719 (org-export-docbook-close-para-maybe)
720 (insert "</entry></row></tbody>\n"
721 "</tgroup>\n</informaltable>\n")
722 (org-export-docbook-open-para)
723 (throw 'nextline nil))
725 ;; Make targets to anchors. Note that currently FOP does not
726 ;; seem to support <anchor> tags when generating PDF output,
727 ;; but this can be used in DocBook --> HTML conversion.
728 (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
729 (cond
730 ((match-end 2)
731 (setq line (replace-match
732 (format "@<anchor xml:id=\"%s\"/>"
733 (org-solidify-link-text (match-string 1 line)))
734 t t line)))
736 (setq line (replace-match
737 (format "@<anchor xml:id=\"%s\"/>"
738 (org-solidify-link-text (match-string 1 line)))
739 t t line)))))
741 ;; Put time stamps and related keywords into special mark-up
742 ;; elements.
743 (setq line (org-export-docbook-handle-time-stamps line))
745 ;; Replace "&", "<" and ">" by "&amp;", "&lt;" and "&gt;".
746 ;; Handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>").
747 ;; Also handle sub_superscripts and check boxes.
748 (or (string-match org-table-hline-regexp line)
749 (setq line (org-docbook-expand line)))
751 ;; Format the links
752 (setq start 0)
753 (while (string-match org-bracket-link-analytic-regexp++ line start)
754 (setq start (match-beginning 0))
755 (setq path (save-match-data (org-link-unescape
756 (match-string 3 line))))
757 (setq type (cond
758 ((match-end 2) (match-string 2 line))
759 ((save-match-data
760 (or (file-name-absolute-p path)
761 (string-match "^\\.\\.?/" path)))
762 "file")
763 (t "internal")))
764 (setq path (org-extract-attributes (org-link-unescape path)))
765 (setq attr (get-text-property 0 'org-attributes path)
766 caption (get-text-property 0 'org-caption path)
767 label (get-text-property 0 'org-label path))
768 (setq desc1 (if (match-end 5) (match-string 5 line))
769 desc2 (if (match-end 2) (concat type ":" path) path)
770 descp (and desc1 (not (equal desc1 desc2)))
771 desc (or desc1 desc2))
772 ;; Make an image out of the description if that is so wanted
773 (when (and descp (org-file-image-p
774 desc org-export-docbook-inline-image-extensions))
775 (save-match-data
776 (if (string-match "^file:" desc)
777 (setq desc (substring desc (match-end 0))))))
778 ;; FIXME: do we need to unescape here somewhere?
779 (cond
780 ((equal type "internal")
781 (setq rpl (format "<link linkend=\"%s\">%s</link>"
782 (org-solidify-link-text
783 (save-match-data (org-link-unescape path)) nil)
784 (org-export-docbook-format-desc desc))))
785 ((and (equal type "id")
786 (setq id-file (org-id-find-id-file path)))
787 ;; This is an id: link to another file (if it was the same file,
788 ;; it would have become an internal link...)
789 (setq id-file (file-relative-name
790 id-file (file-name-directory org-current-export-file)))
791 (setq id-file (concat (file-name-sans-extension id-file)
792 org-export-docbook-extension))
793 (setq rpl (format "<link xlink:href=\"%s#%s\">%s</link>"
794 id-file path (org-export-docbook-format-desc desc))))
795 ((member type '("http" "https"))
796 ;; Standard URL, just check if we need to inline an image
797 (if (and (or (eq t org-export-docbook-inline-images)
798 (and org-export-docbook-inline-images (not descp)))
799 (org-file-image-p
800 path org-export-docbook-inline-image-extensions))
801 (setq rpl (org-export-docbook-format-image
802 (concat type ":" path)))
803 (setq link (concat type ":" path))
804 (setq rpl (format "<link xlink:href=\"%s\">%s</link>"
805 (org-export-html-format-href link)
806 (org-export-docbook-format-desc desc)))
808 ((member type '("ftp" "mailto" "news"))
809 ;; Standard URL
810 (setq link (concat type ":" path))
811 (setq rpl (format "<link xlink:href=\"%s\">%s</link>"
812 (org-export-html-format-href link)
813 (org-export-docbook-format-desc desc))))
814 ((string= type "coderef")
815 (setq rpl (format (org-export-get-coderef-format path (and descp desc))
816 (cdr (assoc path org-export-code-refs)))))
817 ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
818 ;; The link protocol has a function for format the link
819 (setq rpl
820 (save-match-data
821 (funcall fnc (org-link-unescape path) desc1 'html))))
823 ((string= type "file")
824 ;; FILE link
825 (let* ((filename path)
826 (abs-p (file-name-absolute-p filename))
827 thefile file-is-image-p search)
828 (save-match-data
829 (if (string-match "::\\(.*\\)" filename)
830 (setq search (match-string 1 filename)
831 filename (replace-match "" t nil filename)))
832 (setq valid
833 (if (functionp link-validate)
834 (funcall link-validate filename current-dir)
836 (setq file-is-image-p
837 (org-file-image-p
838 filename org-export-docbook-inline-image-extensions))
839 (setq thefile (if abs-p (expand-file-name filename) filename))
840 ;; Carry over the properties (expand-file-name will
841 ;; discard the properties of filename)
842 (add-text-properties 0 (1- (length thefile))
843 (list 'org-caption caption
844 'org-attributes attr
845 'org-label label)
846 thefile)
847 (when (and org-export-docbook-link-org-files-as-docbook
848 (string-match "\\.org$" thefile))
849 (setq thefile (concat (substring thefile 0
850 (match-beginning 0))
851 org-export-docbook-extension))
852 (if (and search
853 ;; make sure this is can be used as target search
854 (not (string-match "^[0-9]*$" search))
855 (not (string-match "^\\*" search))
856 (not (string-match "^/.*/$" search)))
857 (setq thefile (concat thefile "#"
858 (org-solidify-link-text
859 (org-link-unescape search)))))
860 (when (string-match "^file:" desc)
861 (setq desc (replace-match "" t t desc))
862 (if (string-match "\\.org$" desc)
863 (setq desc (replace-match "" t t desc))))))
864 (setq rpl (if (and file-is-image-p
865 (or (eq t org-export-docbook-inline-images)
866 (and org-export-docbook-inline-images
867 (not descp))))
868 (progn
869 (message "image %s %s" thefile org-docbook-para-open)
870 (org-export-docbook-format-image thefile))
871 (format "<link xlink:href=\"%s\">%s</link>"
872 thefile (org-export-docbook-format-desc desc))))
873 (if (not valid) (setq rpl desc))))
876 ;; Just publish the path, as default
877 (setq rpl (concat "&lt;" type ":"
878 (save-match-data (org-link-unescape path))
879 "&gt;"))))
880 (setq line (replace-match rpl t t line)
881 start (+ start (length rpl))))
883 ;; TODO items: can we do something better?!
884 (if (and (string-match org-todo-line-regexp line)
885 (match-beginning 2))
886 (setq line
887 (concat (substring line 0 (match-beginning 2))
888 "[" (match-string 2 line) "]"
889 (substring line (match-end 2)))))
891 ;; Does this contain a reference to a footnote?
892 (when org-export-with-footnotes
893 (setq start 0)
894 (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
895 (if (get-text-property (match-beginning 2) 'org-protected line)
896 (setq start (match-end 2))
897 (let ((num (match-string 2 line)))
898 (if (assoc num footref-seen)
899 (setq line (replace-match
900 (format "%s<footnoteref linkend=\"%s%s\"/>"
901 (match-string 1 line)
902 org-export-docbook-footnote-id-prefix num)
903 t t line))
904 (setq line (replace-match
905 (format "%s<footnote xml:id=\"%s%s\"><para>%s</para></footnote>"
906 (match-string 1 line)
907 org-export-docbook-footnote-id-prefix
909 (save-match-data
910 (org-docbook-expand
911 (cdr (assoc num footnote-list)))))
912 t t line))
913 (push (cons num 1) footref-seen))))))
915 (cond
916 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
917 ;; This is a headline
918 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
919 level-offset))
920 txt (match-string 2 line))
921 (if (string-match quote-re0 txt)
922 (setq txt (replace-match "" t t txt)))
923 (when in-local-list
924 ;; Close any local lists before inserting a new header line
925 (while local-list-type
926 (let ((listtype (car local-list-type)))
927 (org-export-docbook-close-li listtype)
928 (insert (cond
929 ((equal listtype "o") "</orderedlist>\n")
930 ((equal listtype "u") "</itemizedlist>\n")
931 ((equal listtype "d") "</variablelist>\n"))))
932 (pop local-list-type))
933 (setq local-list-indent nil
934 in-local-list nil))
935 (org-export-docbook-level-start level txt)
936 ;; QUOTES
937 (when (string-match quote-re line)
938 (org-export-docbook-close-para-maybe)
939 (insert "<programlisting><![CDATA[")
940 (setq inquote t)))
942 ;; Tables: since version 4.3 of DocBook DTD, HTML tables are
943 ;; supported. We can use existing HTML table exporter code
944 ;; here.
945 ((and org-export-with-tables
946 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
947 (if (not table-open)
948 ;; New table starts
949 (setq table-open t
950 table-buffer nil
951 table-orig-buffer nil))
952 ;; Accumulate lines
953 (setq table-buffer (cons line table-buffer)
954 table-orig-buffer (cons origline table-orig-buffer))
955 (when (or (not lines)
956 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
957 (car lines))))
958 (setq table-open nil
959 table-buffer (nreverse table-buffer)
960 table-orig-buffer (nreverse table-orig-buffer))
961 (org-export-docbook-close-para-maybe)
962 (insert (org-export-docbook-finalize-table
963 (org-format-table-html table-buffer table-orig-buffer)))))
965 ;; Normal lines
966 (when (string-match
967 (cond
968 ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
969 ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
970 ((= llt ?\)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
971 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
972 line)
973 (setq ind (org-get-string-indentation line)
974 item-type (if (match-beginning 4) "o" "u")
975 starter (if (match-beginning 2)
976 (substring (match-string 2 line) 0 -1))
977 line (substring line (match-beginning 5))
978 item-tag nil)
979 (if (and starter (string-match "\\(.*?\\) ::[ \t]*" line))
980 (setq item-type "d"
981 item-tag (match-string 1 line)
982 line (substring line (match-end 0))))
983 (when (and (not (equal item-type "d"))
984 (not (string-match "[^ \t]" line)))
985 ;; Empty line. Pretend indentation is large.
986 (setq ind (if org-empty-line-terminates-plain-lists
988 (1+ (or (car local-list-indent) 1)))))
989 (setq didclose nil)
990 (while (and in-local-list
991 (or (and (= ind (car local-list-indent))
992 (not starter))
993 (< ind (car local-list-indent))))
994 (setq didclose t)
995 (let ((listtype (car local-list-type)))
996 (org-export-docbook-close-li listtype)
997 (insert (cond
998 ((equal listtype "o") "</orderedlist>\n")
999 ((equal listtype "u") "</itemizedlist>\n")
1000 ((equal listtype "d") "</variablelist>\n"))))
1001 (pop local-list-type) (pop local-list-indent)
1002 (setq in-local-list local-list-indent))
1003 (cond
1004 ((and starter
1005 (or (not in-local-list)
1006 (> ind (car local-list-indent))))
1007 ;; Start new (level of) list
1008 (org-export-docbook-close-para-maybe)
1009 (insert (cond
1010 ((equal item-type "u") "<itemizedlist>\n<listitem>\n")
1011 ((equal item-type "o") "<orderedlist>\n<listitem>\n")
1012 ((equal item-type "d")
1013 (format "<variablelist>\n<varlistentry><term>%s</term><listitem>\n" item-tag))))
1014 ;; For DocBook, we need to open a para right after tag
1015 ;; <listitem>.
1016 (org-export-docbook-open-para)
1017 (push item-type local-list-type)
1018 (push ind local-list-indent)
1019 (setq in-local-list t))
1020 (starter
1021 ;; Continue current list
1022 (let ((listtype (car local-list-type)))
1023 (org-export-docbook-close-li listtype)
1024 (insert (cond
1025 ((equal listtype "o") "<listitem>")
1026 ((equal listtype "u") "<listitem>")
1027 ((equal listtype "d") (format
1028 "<varlistentry><term>%s</term><listitem>"
1029 (or item-tag
1030 "???"))))))
1031 ;; For DocBook, we need to open a para right after tag
1032 ;; <listitem>.
1033 (org-export-docbook-open-para))
1034 (didclose
1035 ;; We did close a list, normal text follows: need <para>
1036 (org-export-docbook-open-para)))
1037 ;; Checkboxes.
1038 (if (string-match "^[ \t]*\\(\\[[X -]\\]\\)" line)
1039 (setq line
1040 (replace-match (concat checkbox-start
1041 (match-string 1 line)
1042 checkbox-end)
1043 t t line))))
1045 ;; Empty lines start a new paragraph. If hand-formatted lists
1046 ;; are not fully interpreted, lines starting with "-", "+", "*"
1047 ;; also start a new paragraph.
1048 (if (and (string-match "^ [-+*]-\\|^[ \t]*$" line)
1049 (not inverse))
1050 (org-export-docbook-open-para))
1052 ;; Is this the start of a footnote?
1053 (when org-export-with-footnotes
1054 (when (and (boundp 'footnote-section-tag-regexp)
1055 (string-match (concat "^" footnote-section-tag-regexp)
1056 line))
1057 ;; ignore this line
1058 (throw 'nextline nil))
1059 ;; These footnote lines have been read and saved before,
1060 ;; ignore them at this time.
1061 (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
1062 (org-export-docbook-close-para-maybe)
1063 (throw 'nextline nil)))
1065 ;; FIXME: It might be a good idea to add an option to
1066 ;; support line break processing instruction <?linebreak?>.
1067 ;; Org-mode supports line break "\\" in HTML exporter, and
1068 ;; some DocBook users may also want to force line breaks
1069 ;; even though DocBook only supports that in
1070 ;; <literallayout>.
1072 (insert line "\n")))))
1074 ;; Properly close all local lists and other lists
1075 (when inquote
1076 (insert "]]>\n</programlisting>\n")
1077 (org-export-docbook-open-para))
1078 (when in-local-list
1079 ;; Close any local lists before inserting a new header line
1080 (while local-list-type
1081 (let ((listtype (car local-list-type)))
1082 (org-export-docbook-close-li listtype)
1083 (insert (cond
1084 ((equal listtype "o") "</orderedlist>\n")
1085 ((equal listtype "u") "</itemizedlist>\n")
1086 ((equal listtype "d") "</variablelist>\n"))))
1087 (pop local-list-type))
1088 (setq local-list-indent nil
1089 in-local-list nil))
1090 ;; Close all open sections.
1091 (org-export-docbook-level-start 1 nil)
1093 (unless (plist-get opt-plist :buffer-will-be-killed)
1094 (normal-mode)
1095 (if (eq major-mode default-major-mode)
1096 (nxml-mode)))
1098 ;; Remove empty paragraphs and lists. Replace them with a
1099 ;; newline.
1100 (goto-char (point-min))
1101 (while (re-search-forward
1102 "[ \r\n\t]*\\(<para>\\)[ \r\n\t]*</para>[ \r\n\t]*" nil t)
1103 (when (not (get-text-property (match-beginning 1) 'org-protected))
1104 (replace-match "\n")
1105 (backward-char 1)))
1106 ;; Fill empty sections with <para></para>. This is to make sure
1107 ;; that the DocBook document generated is valid and well-formed.
1108 (goto-char (point-min))
1109 (while (re-search-forward
1110 "</title>\\([ \r\n\t]*\\)</section>" nil t)
1111 (when (not (get-text-property (match-beginning 0) 'org-protected))
1112 (replace-match "\n<para></para>\n" nil nil nil 1)))
1113 ;; Insert the last closing tag.
1114 (goto-char (point-max))
1115 (unless body-only
1116 (insert "</article>"))
1117 (or to-buffer (save-buffer))
1118 (goto-char (point-min))
1119 (message "Exporting... done")
1120 (if (eq to-buffer 'string)
1121 (prog1 (buffer-substring (point-min) (point-max))
1122 (kill-buffer (current-buffer)))
1123 (current-buffer)))))
1125 (defun org-export-docbook-open-para ()
1126 "Insert <para>, but first close previous paragraph if any."
1127 (org-export-docbook-close-para-maybe)
1128 (insert "\n<para>")
1129 (setq org-docbook-para-open t))
1131 (defun org-export-docbook-close-para-maybe ()
1132 "Close DocBook paragraph if there is one open."
1133 (when org-docbook-para-open
1134 (insert "</para>\n")
1135 (setq org-docbook-para-open nil)))
1137 (defun org-export-docbook-close-li (&optional type)
1138 "Close list if necessary."
1139 (org-export-docbook-close-para-maybe)
1140 (if (equal type "d")
1141 (insert "</listitem></varlistentry>\n")
1142 (insert "</listitem>\n")))
1144 (defun org-export-docbook-level-start (level title)
1145 "Insert a new level in DocBook export.
1146 When TITLE is nil, just close all open levels."
1147 (org-export-docbook-close-para-maybe)
1148 (let* ((target (and title (org-get-text-property-any 0 'target title)))
1149 (l org-level-max)
1150 section-number)
1151 (while (>= l level)
1152 (if (aref org-levels-open (1- l))
1153 (progn
1154 (insert "</section>\n")
1155 (aset org-levels-open (1- l) nil)))
1156 (setq l (1- l)))
1157 (when title
1158 ;; If title is nil, this means this function is called to close
1159 ;; all levels, so the rest is done only if title is given.
1161 ;; Format tags: put them into a superscript like format.
1162 (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
1163 (setq title
1164 (replace-match
1165 (if org-export-with-tags
1166 (save-match-data
1167 (concat
1168 "<superscript>"
1169 (match-string 1 title)
1170 "</superscript>"))
1172 t t title)))
1173 (aset org-levels-open (1- level) t)
1174 (setq section-number (org-section-number level))
1175 (insert (format "\n<section xml:id=\"%s%s\">\n<title>%s</title>"
1176 org-export-docbook-section-id-prefix
1177 section-number title))
1178 (org-export-docbook-open-para))))
1180 (defun org-docbook-expand (string)
1181 "Prepare STRING for DocBook export.
1182 Applies all active conversions. If there are links in the
1183 string, don't modify these."
1184 (let* ((re (concat org-bracket-link-regexp "\\|"
1185 (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
1186 m s l res)
1187 (while (setq m (string-match re string))
1188 (setq s (substring string 0 m)
1189 l (match-string 0 string)
1190 string (substring string (match-end 0)))
1191 (push (org-docbook-do-expand s) res)
1192 (push l res))
1193 (push (org-docbook-do-expand string) res)
1194 (apply 'concat (nreverse res))))
1196 (defun org-docbook-do-expand (s)
1197 "Apply all active conversions to translate special ASCII to DocBook."
1198 (setq s (org-html-protect s))
1199 (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
1200 (setq s (replace-match "<\\1>" t nil s)))
1201 (if org-export-with-emphasize
1202 (setq s (org-export-docbook-convert-emphasize s)))
1203 (if org-export-with-special-strings
1204 (setq s (org-export-docbook-convert-special-strings s)))
1205 (if org-export-with-sub-superscripts
1206 (setq s (org-export-docbook-convert-sub-super s)))
1207 (if org-export-with-TeX-macros
1208 (let ((start 0) wd ass)
1209 (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)\\({}\\)?"
1210 s start))
1211 (if (get-text-property (match-beginning 0) 'org-protected s)
1212 (setq start (match-end 0))
1213 (setq wd (match-string 1 s))
1214 (if (setq ass (assoc wd org-html-entities))
1215 (setq s (replace-match (or (cdr ass)
1216 (concat "&" (car ass) ";"))
1217 t t s))
1218 (setq start (+ start (length wd))))))))
1221 (defun org-export-docbook-format-desc (desc)
1222 "Make sure DESC is valid as a description in a link."
1223 (save-match-data
1224 (org-docbook-do-expand desc)))
1226 (defun org-export-docbook-convert-emphasize (string)
1227 "Apply emphasis for DocBook exporting."
1228 (let ((s 0) rpl)
1229 (while (string-match org-emph-re string s)
1230 (if (not (equal
1231 (substring string (match-beginning 3) (1+ (match-beginning 3)))
1232 (substring string (match-beginning 4) (1+ (match-beginning 4)))))
1233 (setq s (match-beginning 0)
1235 (concat
1236 (match-string 1 string)
1237 (nth 1 (assoc (match-string 3 string)
1238 org-export-docbook-emphasis-alist))
1239 (match-string 4 string)
1240 (nth 2 (assoc (match-string 3 string)
1241 org-export-docbook-emphasis-alist))
1242 (match-string 5 string))
1243 string (replace-match rpl t t string)
1244 s (+ s (- (length rpl) 2)))
1245 (setq s (1+ s))))
1246 string))
1248 (defun org-docbook-protect (string)
1249 (org-html-protect string))
1251 ;; For now, simply return string as it is.
1252 (defun org-export-docbook-convert-special-strings (string)
1253 "Convert special characters in STRING to DocBook."
1254 string)
1256 (defun org-export-docbook-get-footnotes (lines)
1257 "Given a list of LINES, return a list of alist footnotes."
1258 (let ((list nil) line)
1259 (while (setq line (pop lines))
1260 (if (string-match "^[ \t]*\\[\\([0-9]+\\)\\] \\(.+\\)" line)
1261 (push (cons (match-string 1 line) (match-string 2 line))
1262 list)))
1263 list))
1265 (defun org-export-docbook-format-image (src)
1266 "Create image element in DocBook."
1267 (save-match-data
1268 (let* ((caption (org-find-text-property-in-string 'org-caption src))
1269 (attr (or (org-find-text-property-in-string 'org-attributes src)
1270 ""))
1271 (label (org-find-text-property-in-string 'org-label src))
1272 (default-attr org-export-docbook-default-image-attributes)
1273 tmp)
1274 (while (setq tmp (pop default-attr))
1275 (if (not (string-match (concat (car tmp) "=") attr))
1276 (setq attr (concat attr " " (car tmp) "=" (cdr tmp)))))
1277 (format "<mediaobject%s>
1278 <imageobject>\n<imagedata fileref=\"%s\" %s/>\n</imageobject>
1279 %s</mediaobject>"
1280 (if label (concat " xml:id=\"" label "\"") "")
1281 src attr
1282 (if caption
1283 (concat "<caption>\n<para>"
1284 caption
1285 "</para>\n</caption>\n")
1287 ))))
1289 (defun org-export-docbook-preprocess (parameters)
1290 "Extra preprocessing work for DocBook export."
1291 ;; Merge lines starting with "\par" to one line. Such lines are
1292 ;; regarded as the continuation of a long footnote.
1293 (goto-char (point-min))
1294 (while (re-search-forward "\n\\(\\\\par\\>\\)" nil t)
1295 (if (not (get-text-property (match-beginning 1) 'org-protected))
1296 (replace-match ""))))
1298 (defun org-export-docbook-finalize-table (table)
1299 "Change TABLE to informaltable if caption does not exist.
1300 TABLE is a string containing the HTML code generated by
1301 `org-format-table-html' for a table in Org-mode buffer."
1302 (if (string-match
1303 "^<table \\(\\(.\\|\n\\)+\\)<caption></caption>\n\\(\\(.\\|\n\\)+\\)</table>"
1304 table)
1305 (replace-match (concat "<informaltable "
1306 (match-string 1 table)
1307 (match-string 3 table)
1308 "</informaltable>")
1309 nil nil table)
1310 table))
1312 ;; Note: This function is very similar to
1313 ;; org-export-html-convert-sub-super. They can be merged in the future.
1314 (defun org-export-docbook-convert-sub-super (string)
1315 "Convert sub- and superscripts in STRING for DocBook."
1316 (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
1317 (while (string-match org-match-substring-regexp string s)
1318 (cond
1319 ((and requireb (match-end 8)) (setq s (match-end 2)))
1320 ((get-text-property (match-beginning 2) 'org-protected string)
1321 (setq s (match-end 2)))
1323 (setq s (match-end 1)
1324 key (if (string= (match-string 2 string) "_")
1325 "subscript"
1326 "superscript")
1327 c (or (match-string 8 string)
1328 (match-string 6 string)
1329 (match-string 5 string))
1330 string (replace-match
1331 (concat (match-string 1 string)
1332 "<" key ">" c "</" key ">")
1333 t t string)))))
1334 (while (string-match "\\\\\\([_^]\\)" string)
1335 (setq string (replace-match (match-string 1 string) t t string)))
1336 string))
1338 (defun org-export-docbook-protect-tags (string)
1339 "Change ``<...>'' in string STRING into ``@<...>''.
1340 This is normally needed when STRING contains DocBook elements
1341 that need to be preserved in later phase of DocBook exporting."
1342 (let ((start 0))
1343 (while (string-match "<\\([^>]*\\)>" string start)
1344 (setq string (replace-match
1345 "@<\\1>" t nil string)
1346 start (match-end 0)))
1347 string))
1349 (defun org-export-docbook-handle-time-stamps (line)
1350 "Format time stamps in string LINE."
1351 (let (replaced
1352 (kw-markup (org-export-docbook-protect-tags
1353 org-export-docbook-keywords-markup))
1354 (ts-markup (org-export-docbook-protect-tags
1355 org-export-docbook-timestamp-markup)))
1356 (while (string-match org-maybe-keyword-time-regexp line)
1357 (setq replaced
1358 (concat replaced
1359 (substring line 0 (match-beginning 0))
1360 (if (match-end 1)
1361 (format kw-markup
1362 (match-string 1 line)))
1364 (format ts-markup
1365 (substring (org-translate-time
1366 (match-string 3 line)) 1 -1)))
1367 line (substring line (match-end 0))))
1368 (concat replaced line)))
1370 (provide 'org-docbook)
1372 ;;; org-docbook.el ends here