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