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