1 ;;; org-docbook.el --- DocBook exporter for org-mode
3 ;; Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: org-docbook.el
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 $
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/>.
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
39 ;; (require 'org-docbook)
41 ;; The interactive functions are similar to those of the HTML and LaTeX
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
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
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
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))
90 (defgroup org-export-docbook nil
91 "Options for exporting Org-mode files to DocBook."
92 :tag
"Org Export DocBook"
95 (defcustom org-export-docbook-extension
".xml"
96 "Extension of DocBook XML files."
97 :group
'org-export-docbook
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
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\"
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
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
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
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
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
163 :group
'org-export-docbook
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
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
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
216 :group
'org-export-docbook
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
232 :group
'org-export-docbook
235 ;;; Autoload functions:
238 (defun org-export-as-docbook-batch ()
239 "Call `org-export-as-docbook' in batch style.
240 This function can be used in batch processing.
245 --load=$HOME/lib/emacs/org.el
246 --visit=MyOrgFile.org --funcall org-export-as-docbook-batch"
247 (org-export-as-docbook 'hidden
))
250 (defun org-export-as-docbook-to-buffer ()
251 "Call `org-export-as-docbook' with output to a temporary buffer.
254 (org-export-as-docbook nil nil
"*Org DocBook Export*")
255 (switch-to-buffer-other-window "*Org DocBook Export*"))
258 (defun org-replace-region-by-docbook (beg end
)
259 "Replace the region from BEG to END with its DocBook export.
260 It assumes the region has `org-mode' syntax, and then convert it to
261 DocBook. This can be used in any buffer. For example, you could
262 write an itemized list in `org-mode' syntax in an DocBook buffer and
263 then use this command to convert it."
265 (let (reg docbook buf
)
266 (save-window-excursion
268 (setq docbook
(org-export-region-as-docbook
270 (setq reg
(buffer-substring beg end
)
271 buf
(get-buffer-create "*Org tmp*"))
277 (setq docbook
(org-export-region-as-docbook
278 (point-min) (point-max) t
'string
)))
280 (delete-region beg end
)
284 (defun org-export-region-as-docbook (beg end
&optional body-only buffer
)
285 "Convert region from BEG to END in `org-mode' buffer to DocBook.
286 If prefix arg BODY-ONLY is set, omit file header and footer and
287 only produce the region of converted text, useful for
288 cut-and-paste operations. If BUFFER is a buffer or a string,
289 use/create that buffer as a target of the converted DocBook. If
290 BUFFER is the symbol `string', return the produced DocBook as a
291 string and leave not buffer behind. For example, a Lisp program
292 could call this function in the following way:
294 (setq docbook (org-export-region-as-docbook beg end t 'string))
296 When called interactively, the output buffer is selected, and shown
297 in a window. A non-interactive call will only retunr the buffer."
299 (when (interactive-p)
300 (setq buffer
"*Org DocBook Export*"))
301 (let ((transient-mark-mode t
)
305 (set-mark (point)) ;; To activate the region
307 (setq rtn
(org-export-as-docbook
310 (if (fboundp 'deactivate-mark
) (deactivate-mark))
311 (if (and (interactive-p) (bufferp rtn
))
312 (switch-to-buffer-other-window rtn
)
316 (defun org-export-as-docbook-pdf (&optional hidden ext-plist
317 to-buffer body-only pub-dir
)
318 "Export as DocBook XML file, and generate PDF file."
320 (if (or (not org-export-docbook-xslt-proc-command
)
321 (not (string-match "%s.+%s" org-export-docbook-xslt-proc-command
)))
322 (error "XSLT processor command is not set correctly"))
323 (if (or (not org-export-docbook-xsl-fo-proc-command
)
324 (not (string-match "%s.+%s" org-export-docbook-xsl-fo-proc-command
)))
325 (error "XSL-FO processor command is not set correctly"))
326 (message "Exporting to PDF...")
327 (let* ((wconfig (current-window-configuration))
328 (docbook-buf (org-export-as-docbook hidden ext-plist
329 to-buffer body-only pub-dir
))
330 (filename (buffer-file-name docbook-buf
))
331 (base (file-name-sans-extension filename
))
332 (fofile (concat base
".fo"))
333 (pdffile (concat base
".pdf")))
334 (and (file-exists-p pdffile
) (delete-file pdffile
))
335 (message "Processing DocBook XML file...")
336 (shell-command (format org-export-docbook-xslt-proc-command
337 fofile
(shell-quote-argument filename
)))
338 (shell-command (format org-export-docbook-xsl-fo-proc-command
340 (message "Processing DocBook file...done")
341 (if (not (file-exists-p pdffile
))
342 (error "PDF file was not produced")
343 (set-window-configuration wconfig
)
344 (message "Exporting to PDF...done")
348 (defun org-export-as-docbook-pdf-and-open ()
349 "Export as DocBook XML file, generate PDF file, and open it."
351 (let ((pdffile (org-export-as-docbook-pdf)))
353 (org-open-file pdffile
)
354 (error "PDF file was not produced"))))
357 (defun org-export-as-docbook (&optional hidden ext-plist
358 to-buffer body-only pub-dir
)
359 "Export the current buffer as a DocBook file.
360 If there is an active region, export only the region. When
361 HIDDEN is non-nil, don't display the HTML buffer. EXT-PLIST is a
362 property list with external parameters overriding org-mode's
363 default settings, but still inferior to file-local settings.
364 When TO-BUFFER is non-nil, create a buffer with that name and
365 export to that buffer. If TO-BUFFER is the symbol `string',
366 don't leave any buffer behind but just return the resulting HTML
367 as a string. When BODY-ONLY is set, don't produce the file
368 header and footer, simply return the content of the document (all
369 top-level sections). When PUB-DIR is set, use this as the
370 publishing directory."
372 ;; Make sure we have a file name when we need it.
373 (when (and (not (or to-buffer body-only
))
374 (not buffer-file-name
))
375 (if (buffer-base-buffer)
376 (org-set-local 'buffer-file-name
377 (with-current-buffer (buffer-base-buffer)
379 (error "Need a file name to be able to export.")))
381 (message "Exporting...")
382 (setq-default org-todo-line-regexp org-todo-line-regexp
)
383 (setq-default org-deadline-line-regexp org-deadline-line-regexp
)
384 (setq-default org-done-keywords org-done-keywords
)
385 (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp
)
387 (org-export-process-option-filters
388 (org-combine-plists (org-default-export-plist)
390 (org-infile-export-plist))))
391 (link-validate (plist-get opt-plist
:link-validation-function
))
393 (odd org-odd-levels-only
)
394 (region-p (org-region-active-p))
395 (rbeg (and region-p
(region-beginning)))
396 (rend (and region-p
(region-end)))
398 (if (plist-get opt-plist
:ignore-subree-p
)
403 (and (org-at-heading-p)
404 (>= (org-end-of-subtree t t
) rend
))))))
405 (level-offset (if subtree-p
408 (+ (funcall outline-level
)
409 (if org-odd-levels-only
1 0)))
411 (opt-plist (if subtree-p
412 (org-export-add-subtree-options opt-plist rbeg
)
414 ;; The following two are dynamically scoped into other
416 (org-current-export-dir
417 (or pub-dir
(org-export-directory :docbook opt-plist
)))
418 (org-current-export-file buffer-file-name
)
419 (level 0) (line "") (origline "") txt todo
420 (filename (if to-buffer nil
423 (file-name-sans-extension
425 (org-entry-get (region-beginning)
426 "EXPORT_FILE_NAME" t
))
427 (file-name-nondirectory buffer-file-name
)))
428 org-export-docbook-extension
)
429 (file-name-as-directory
430 (or pub-dir
(org-export-directory :docbook opt-plist
))))))
431 (current-dir (if buffer-file-name
432 (file-name-directory buffer-file-name
)
434 (buffer (if to-buffer
436 ((eq to-buffer
'string
) (get-buffer-create "*Org DocBook Export*"))
437 (t (get-buffer-create to-buffer
)))
438 (find-file-noselect filename
)))
439 ;; org-levels-open is a global variable
440 (org-levels-open (make-vector org-level-max nil
))
441 (date (plist-get opt-plist
:date
))
442 (author (or (plist-get opt-plist
:author
)
444 (email (plist-get opt-plist
:email
))
445 firstname othername surname
446 (title (or (and subtree-p
(org-export-get-title-from-subtree))
447 (plist-get opt-plist
:title
)
449 (plist-get opt-plist
:skip-before-1st-heading
))
450 (org-export-grab-title-from-buffer))
451 (and buffer-file-name
452 (file-name-sans-extension
453 (file-name-nondirectory buffer-file-name
)))
455 ;; We will use HTML table formatter to export tables to DocBook
456 ;; format, so need to set html-table-tag here.
457 (html-table-tag (plist-get opt-plist
:html-table-tag
))
458 (quote-re0 (concat "^[ \t]*" org-quote-string
"\\>"))
459 (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string
"\\>\\)"))
464 (local-list-type nil
)
465 (local-list-indent nil
)
466 (llt org-plain-list-ordered-item-terminator
)
467 (email (plist-get opt-plist
:email
))
468 (language (plist-get opt-plist
:language
))
472 (coding-system (and (boundp 'buffer-file-coding-system
)
473 buffer-file-coding-system
))
474 (coding-system-for-write (or org-export-docbook-coding-system
476 (save-buffer-coding-system (or org-export-docbook-coding-system
478 (charset (and coding-system-for-write
479 (fboundp 'coding-system-get
)
480 (coding-system-get coding-system-for-write
484 (if region-p
(region-beginning) (point-min))
485 (if region-p
(region-end) (point-max))))
488 (org-export-preprocess-string
492 :skip-before-1st-heading
493 (plist-get opt-plist
:skip-before-1st-heading
)
494 :drawers
(plist-get opt-plist
:drawers
)
495 :todo-keywords
(plist-get opt-plist
:todo-keywords
)
496 :tags
(plist-get opt-plist
:tags
)
497 :priority
(plist-get opt-plist
:priority
)
498 :footnotes
(plist-get opt-plist
:footnotes
)
499 :timestamps
(plist-get opt-plist
:timestamps
)
501 (plist-get opt-plist
:archived-trees
)
502 :select-tags
(plist-get opt-plist
:select-tags
)
503 :exclude-tags
(plist-get opt-plist
:exclude-tags
)
505 (plist-get opt-plist
:text
)
507 (plist-get opt-plist
:LaTeX-fragments
))
509 ;; Use literal output to show check boxes.
511 (nth 1 (assoc "=" org-export-docbook-emphasis-alist
)))
513 (nth 2 (assoc "=" org-export-docbook-emphasis-alist
)))
515 table-buffer table-orig-buffer
516 ind item-type starter didclose
517 rpl path attr caption label desc descp desc1 desc2 link
519 footref-seen footnote-list
523 ;; Fine detailed info about author name.
524 (if (string-match "\\([^ ]+\\) \\(.+ \\)?\\([^ ]+\\)" author
)
526 (setq firstname
(match-string 1 author
)
527 othername
(or (match-string 2 author
) "")
528 surname
(match-string 3 author
))))
530 ;; Get all footnote text.
532 (org-export-docbook-get-footnotes lines
))
534 (let ((inhibit-read-only t
))
536 (remove-text-properties (point-min) (point-max)
537 '(:org-license-to-kill t
))))
539 (setq org-min-level
(org-get-min-level lines level-offset
))
540 (setq org-last-level org-min-level
)
541 (org-init-section-numbers)
543 ;; Get and save the date.
545 ((and date
(string-match "%" date
))
546 (setq date
(format-time-string date
)))
548 (t (setq date
(format-time-string "%Y-%m-%d %T %Z"))))
550 ;; Get the language-dependent settings
551 (setq lang-words
(or (assoc language org-export-language-setup
)
552 (assoc "en" org-export-language-setup
)))
554 ;; Switch to the output buffer. Use fundamental-mode for now. We
555 ;; could turn on nXML mode later and do some indentation.
557 (let ((inhibit-read-only t
)) (erase-buffer))
560 (and (fboundp 'set-buffer-file-coding-system
)
561 (set-buffer-file-coding-system coding-system-for-write
))
564 (let ((case-fold-search nil
)
565 (org-odd-levels-only odd
))
567 ;; Create local variables for all options, to make sure all called
568 ;; functions get the correct information
570 (set (make-local-variable (nth 2 x
))
571 (plist-get opt-plist
(car x
))))
572 org-export-plist-vars
)
574 ;; Insert DocBook file header, title, and author info.
576 (insert org-export-docbook-header
)
577 (if org-export-docbook-doctype
578 (insert org-export-docbook-doctype
))
579 (insert "<!-- Date: " date
" -->\n")
580 (insert (format "<!-- DocBook XML file generated by Org-mode %s Emacs %s -->\n"
581 org-version emacs-major-version
))
582 (insert org-export-docbook-article-header
)
584 "\n <title>%s</title>
588 <firstname>%s</firstname> <othername>%s</othername> <surname>%s</surname>
593 (org-docbook-expand title
)
594 firstname othername surname
595 (if email
(concat "<email>" email
"</email>") "")
598 (org-init-section-numbers)
600 (org-export-docbook-open-para)
602 ;; Loop over all the lines...
603 (while (setq line
(pop lines
) origline line
)
606 ;; End of quote section?
607 (when (and inquote
(string-match "^\\*+ " line
))
608 (insert "]]>\n</programlisting>\n")
610 ;; Inside a quote section?
612 (insert (org-docbook-protect line
) "\n")
613 (throw 'nextline nil
))
615 ;; Fixed-width, verbatim lines (examples)
616 (when (and org-export-with-fixed-width
617 (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line
))
620 (org-export-docbook-close-para-maybe)
621 (insert "<programlisting><![CDATA["))
622 (insert (match-string 3 line
) "\n")
623 (when (or (not lines
)
624 (not (string-match "^[ \t]*\\(:.*\\)"
627 (insert "]]>\n</programlisting>\n"))
628 (throw 'nextline nil
))
631 (when (get-text-property 0 'org-protected line
)
633 (when (re-search-backward
634 "\\(<para>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t
)
635 (setq par
(match-string 1))
636 (replace-match "\\2\n"))
639 (or (= (length (car lines
)) 0)
640 (get-text-property 0 'org-protected
(car lines
))))
641 (insert (pop lines
) "\n"))
642 (and par
(insert "<para>\n")))
643 (throw 'nextline nil
))
645 ;; Start of block quotes and verses
646 (when (or (equal "ORG-BLOCKQUOTE-START" line
)
647 (and (equal "ORG-VERSE-START" line
)
649 (org-export-docbook-close-para-maybe)
650 (insert "<blockquote>")
651 ;; Check whether attribution for this blockquote exists.
654 (end (if inverse
"ORG-VERSE-END" "ORG-BLOCKQUOTE-END"))
656 (while (and (setq tmp1
(pop lines
))
657 (not (equal end tmp1
)))
658 (push tmp1 quote-lines
))
659 (push tmp1 lines
) ; Put back quote end mark
660 ;; Check the last line in the quote to see if it contains
662 (setq tmp1
(pop quote-lines
))
663 (if (string-match "\\(^.*\\)\\(--[ \t]+\\)\\(.+\\)$" tmp1
)
665 (setq attribution
(match-string 3 tmp1
))
666 (when (save-match-data
667 (string-match "[^ \t]" (match-string 1 tmp1
)))
668 (push (match-string 1 tmp1
) lines
)))
670 (while (setq tmp1
(pop quote-lines
))
673 (insert "<attribution>" attribution
"</attribution>")))
674 ;; Insert <literallayout> for verse.
676 (insert "\n<literallayout>")
677 (org-export-docbook-open-para))
678 (throw 'nextline nil
))
680 ;; End of block quotes
681 (when (equal "ORG-BLOCKQUOTE-END" line
)
682 (org-export-docbook-close-para-maybe)
683 (insert "</blockquote>\n")
684 (throw 'nextline nil
))
687 (when (equal "ORG-VERSE-END" line
)
688 (insert "</literallayout>\n</blockquote>\n")
690 (throw 'nextline nil
))
692 ;; Text centering. Element <para role="centered"> does not
693 ;; seem to work with FOP, so for now we use <informaltable> to
694 ;; center the text, which can contain multiple paragraphs.
695 (when (equal "ORG-CENTER-START" line
)
696 (org-export-docbook-close-para-maybe)
697 (insert "<informaltable frame=\"none\" colsep=\"0\" rowsep=\"0\">\n"
698 "<tgroup align=\"center\" cols=\"1\">\n"
699 "<tbody><row><entry>\n")
700 (org-export-docbook-open-para)
701 (throw 'nextline nil
))
703 (when (equal "ORG-CENTER-END" line
)
704 (org-export-docbook-close-para-maybe)
705 (insert "</entry></row></tbody>\n"
706 "</tgroup>\n</informaltable>\n")
707 (throw 'nextline nil
))
709 ;; Make targets to anchors. Note that currently FOP does not
710 ;; seem to support <anchor> tags when generating PDF output,
711 ;; but this can be used in DocBook --> HTML conversion.
712 (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line
)
715 (setq line
(replace-match
716 (format "@<anchor xml:id=\"%s\"/>"
717 (org-solidify-link-text (match-string 1 line
)))
720 (setq line
(replace-match
721 (format "@<anchor xml:id=\"%s\"/>"
722 (org-solidify-link-text (match-string 1 line
)))
725 ;; Replace "&" by "&", "<" and ">" by "<" and ">"
726 ;; handle @<..> HTML tags (replace "@>..<" by "<..>")
727 ;; Also handle sub_superscripts and checkboxes
728 (or (string-match org-table-hline-regexp line
)
729 (setq line
(org-docbook-expand line
)))
733 (while (string-match org-bracket-link-analytic-regexp
++ line start
)
734 (setq start
(match-beginning 0))
735 (setq path
(save-match-data (org-link-unescape
736 (match-string 3 line
))))
738 ((match-end 2) (match-string 2 line
))
740 (or (file-name-absolute-p path
)
741 (string-match "^\\.\\.?/" path
)))
744 (setq path
(org-extract-attributes (org-link-unescape path
)))
745 (setq attr
(get-text-property 0 'org-attributes path
)
746 caption
(get-text-property 0 'org-caption path
)
747 label
(get-text-property 0 'org-label path
))
748 (setq desc1
(if (match-end 5) (match-string 5 line
))
749 desc2
(if (match-end 2) (concat type
":" path
) path
)
750 descp
(and desc1
(not (equal desc1 desc2
)))
751 desc
(or desc1 desc2
))
752 ;; Make an image out of the description if that is so wanted
753 (when (and descp
(org-file-image-p
754 desc org-export-docbook-inline-image-extensions
))
756 (if (string-match "^file:" desc
)
757 (setq desc
(substring desc
(match-end 0))))))
758 ;; FIXME: do we need to unescape here somewhere?
760 ((equal type
"internal")
761 (setq rpl
(format "<link linkend=\"%s\">%s</link>"
762 (org-solidify-link-text
763 (save-match-data (org-link-unescape path
)) nil
)
764 (org-export-docbook-format-desc desc
))))
765 ((and (equal type
"id")
766 (setq id-file
(org-id-find-id-file path
)))
767 ;; This is an id: link to another file (if it was the same file,
768 ;; it would have become an internal link...)
769 (setq id-file
(file-relative-name
770 id-file
(file-name-directory org-current-export-file
)))
771 (setq id-file
(concat (file-name-sans-extension id-file
)
772 org-export-docbook-extension
))
773 (setq rpl
(format "<link xlink:href=\"%s#%s\">%s</link>"
774 id-file path
(org-export-docbook-format-desc desc
))))
775 ((member type
'("http" "https"))
776 ;; Standard URL, just check if we need to inline an image
777 (if (and (or (eq t org-export-docbook-inline-images
)
778 (and org-export-docbook-inline-images
(not descp
)))
780 path org-export-docbook-inline-image-extensions
))
781 (setq rpl
(org-export-docbook-format-image
782 (concat type
":" path
)))
783 (setq link
(concat type
":" path
))
784 (setq rpl
(format "<link xlink:href=\"%s\">%s</link>"
785 (org-export-html-format-href link
)
786 (org-export-docbook-format-desc desc
)))
788 ((member type
'("ftp" "mailto" "news"))
790 (setq link
(concat type
":" path
))
791 (setq rpl
(format "<link xlink:href=\"%s\">%s</link>"
792 (org-export-html-format-href link
)
793 (org-export-docbook-format-desc desc
))))
794 ((string= type
"coderef")
795 (setq rpl
(format (org-export-get-coderef-format path
(and descp desc
))
796 (cdr (assoc path org-export-code-refs
)))))
797 ((functionp (setq fnc
(nth 2 (assoc type org-link-protocols
))))
798 ;; The link protocol has a function for format the link
801 (funcall fnc
(org-link-unescape path
) desc1
'html
))))
803 ((string= type
"file")
805 (let* ((filename path
)
806 (abs-p (file-name-absolute-p filename
))
807 thefile file-is-image-p search
)
809 (if (string-match "::\\(.*\\)" filename
)
810 (setq search
(match-string 1 filename
)
811 filename
(replace-match "" t nil filename
)))
813 (if (functionp link-validate
)
814 (funcall link-validate filename current-dir
)
816 (setq file-is-image-p
818 filename org-export-docbook-inline-image-extensions
))
819 (setq thefile
(if abs-p
(expand-file-name filename
) filename
))
820 ;; Carry over the properties (expand-file-name will
821 ;; discard the properties of filename)
822 (add-text-properties 0 (1- (length thefile
))
823 (list 'org-caption caption
827 (when (and org-export-docbook-link-org-files-as-docbook
828 (string-match "\\.org$" thefile
))
829 (setq thefile
(concat (substring thefile
0
831 org-export-docbook-extension
))
833 ;; make sure this is can be used as target search
834 (not (string-match "^[0-9]*$" search
))
835 (not (string-match "^\\*" search
))
836 (not (string-match "^/.*/$" search
)))
837 (setq thefile
(concat thefile
"#"
838 (org-solidify-link-text
839 (org-link-unescape search
)))))
840 (when (string-match "^file:" desc
)
841 (setq desc
(replace-match "" t t desc
))
842 (if (string-match "\\.org$" desc
)
843 (setq desc
(replace-match "" t t desc
))))))
844 (setq rpl
(if (and file-is-image-p
845 (or (eq t org-export-docbook-inline-images
)
846 (and org-export-docbook-inline-images
849 (message "image %s %s" thefile org-docbook-para-open
)
850 (org-export-docbook-format-image thefile
))
851 (format "<link xlink:href=\"%s\">%s</link>"
852 thefile
(org-export-docbook-format-desc desc
))))
853 (if (not valid
) (setq rpl desc
))))
856 ;; Just publish the path, as default
857 (setq rpl
(concat "<" type
":"
858 (save-match-data (org-link-unescape path
))
860 (setq line
(replace-match rpl t t line
)
861 start
(+ start
(length rpl
))))
863 ;; TODO items: can we do something better?!
864 (if (and (string-match org-todo-line-regexp line
)
867 (concat (substring line
0 (match-beginning 2))
868 "[" (match-string 2 line
) "]"
869 (substring line
(match-end 2)))))
871 ;; Does this contain a reference to a footnote?
872 (when org-export-with-footnotes
874 (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start
)
875 (if (get-text-property (match-beginning 2) 'org-protected line
)
876 (setq start
(match-end 2))
877 (let ((num (match-string 2 line
)))
878 (if (assoc num footref-seen
)
879 (setq line
(replace-match
880 (format "%s<footnoteref linkend=\"%s%s\"/>"
881 (match-string 1 line
)
882 org-export-docbook-footnote-id-prefix num
)
884 (setq line
(replace-match
885 (format "%s<footnote xml:id=\"%s%s\"><para>%s</para></footnote>"
886 (match-string 1 line
)
887 org-export-docbook-footnote-id-prefix
891 (cdr (assoc num footnote-list
)))))
893 (push (cons num
1) footref-seen
))))))
896 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line
)
897 ;; This is a headline
898 (setq level
(org-tr-level (- (match-end 1) (match-beginning 1)
900 txt
(match-string 2 line
))
901 (if (string-match quote-re0 txt
)
902 (setq txt
(replace-match "" t t txt
)))
904 ;; Close any local lists before inserting a new header line
905 (while local-list-type
906 (let ((listtype (car local-list-type
)))
907 (org-export-docbook-close-li listtype
)
909 ((equal listtype
"o") "</orderedlist>\n")
910 ((equal listtype
"u") "</itemizedlist>\n")
911 ((equal listtype
"d") "</variablelist>\n"))))
912 (pop local-list-type
))
913 (setq local-list-indent nil
915 (org-export-docbook-level-start level txt
)
917 (when (string-match quote-re line
)
918 (org-export-docbook-close-para-maybe)
919 (insert "<programlisting><![CDATA[")
922 ;; Tables: since version 4.3 of DocBook DTD, HTML tables are
923 ;; supported. We can use existing HTML table exporter code
925 ((and org-export-with-tables
926 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line
))
931 table-orig-buffer nil
))
933 (setq table-buffer
(cons line table-buffer
)
934 table-orig-buffer
(cons origline table-orig-buffer
))
935 (when (or (not lines
)
936 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
939 table-buffer
(nreverse table-buffer
)
940 table-orig-buffer
(nreverse table-orig-buffer
))
941 (org-export-docbook-close-para-maybe)
942 (insert (org-export-docbook-finalize-table
943 (org-format-table-html table-buffer table-orig-buffer
)))))
948 ((eq llt t
) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
949 ((= llt ?.
) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
950 ((= llt ?\
)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
951 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
953 (setq ind
(org-get-string-indentation line
)
954 item-type
(if (match-beginning 4) "o" "u")
955 starter
(if (match-beginning 2)
956 (substring (match-string 2 line
) 0 -
1))
957 line
(substring line
(match-beginning 5))
959 (if (and starter
(string-match "\\(.*?\\) ::[ \t]*" line
))
961 item-tag
(match-string 1 line
)
962 line
(substring line
(match-end 0))))
963 (when (and (not (equal item-type
"d"))
964 (not (string-match "[^ \t]" line
)))
965 ;; Empty line. Pretend indentation is large.
966 (setq ind
(if org-empty-line-terminates-plain-lists
968 (1+ (or (car local-list-indent
) 1)))))
970 (while (and in-local-list
971 (or (and (= ind
(car local-list-indent
))
973 (< ind
(car local-list-indent
))))
975 (let ((listtype (car local-list-type
)))
976 (org-export-docbook-close-li listtype
)
978 ((equal listtype
"o") "</orderedlist>\n")
979 ((equal listtype
"u") "</itemizedlist>\n")
980 ((equal listtype
"d") "</variablelist>\n"))))
981 (pop local-list-type
) (pop local-list-indent
)
982 (setq in-local-list local-list-indent
))
985 (or (not in-local-list
)
986 (> ind
(car local-list-indent
))))
987 ;; Start new (level of) list
988 (org-export-docbook-close-para-maybe)
990 ((equal item-type
"u") "<itemizedlist>\n<listitem>\n")
991 ((equal item-type
"o") "<orderedlist>\n<listitem>\n")
992 ((equal item-type
"d")
993 (format "<variablelist>\n<varlistentry><term>%s</term><listitem>\n" item-tag
))))
994 ;; For DocBook, we need to open a para right after tag
996 (org-export-docbook-open-para)
997 (push item-type local-list-type
)
998 (push ind local-list-indent
)
999 (setq in-local-list t
))
1001 ;; Continue current list
1002 (let ((listtype (car local-list-type
)))
1003 (org-export-docbook-close-li listtype
)
1005 ((equal listtype
"o") "<listitem>")
1006 ((equal listtype
"u") "<listitem>")
1007 ((equal listtype
"d") (format
1008 "<varlistentry><term>%s</term><listitem>"
1011 ;; For DocBook, we need to open a para right after tag
1013 (org-export-docbook-open-para))
1015 ;; We did close a list, normal text follows: need <para>
1016 (org-export-docbook-open-para)))
1018 (if (string-match "^[ \t]*\\(\\[[X -]\\]\\)" line
)
1020 (replace-match (concat checkbox-start
1021 (match-string 1 line
)
1025 ;; Empty lines start a new paragraph. If hand-formatted lists
1026 ;; are not fully interpreted, lines starting with "-", "+", "*"
1027 ;; also start a new paragraph.
1028 (if (and (string-match "^ [-+*]-\\|^[ \t]*$" line
)
1030 (org-export-docbook-open-para))
1032 ;; Is this the start of a footnote?
1033 (when org-export-with-footnotes
1034 (when (and (boundp 'footnote-section-tag-regexp
)
1035 (string-match (concat "^" footnote-section-tag-regexp
)
1038 (throw 'nextline nil
))
1039 ;; These footnote lines have been read and saved before,
1040 ;; ignore them at this time.
1041 (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line
)
1042 (org-export-docbook-close-para-maybe)
1043 (throw 'nextline nil
)))
1045 ;; FIXME: It might be a good idea to add an option to
1046 ;; support line break processing instruction <?linebreak?>.
1047 ;; Org-mode supports line break "\\" in HTML exporter, and
1048 ;; some DocBook users may also want to force line breaks
1049 ;; even though DocBook only supports that in
1052 (insert line
"\n")))))
1054 ;; Properly close all local lists and other lists
1056 (insert "]]>\n</programlisting>\n")
1057 (org-export-docbook-open-para))
1059 ;; Close any local lists before inserting a new header line
1060 (while local-list-type
1061 (let ((listtype (car local-list-type
)))
1062 (org-export-docbook-close-li listtype
)
1064 ((equal listtype
"o") "</orderedlist>\n")
1065 ((equal listtype
"u") "</itemizedlist>\n")
1066 ((equal listtype
"d") "</variablelist>\n"))))
1067 (pop local-list-type
))
1068 (setq local-list-indent nil
1070 ;; Close all open sections.
1071 (org-export-docbook-level-start 1 nil
)
1073 (unless (plist-get opt-plist
:buffer-will-be-killed
)
1075 (if (eq major-mode default-major-mode
)
1078 ;; Remove empty paragraphs and lists. Replace them with a
1080 (goto-char (point-min))
1081 (while (re-search-forward
1082 "[ \r\n\t]*\\(<para>\\)[ \r\n\t]*</para>[ \r\n\t]*" nil t
)
1083 (when (not (get-text-property (match-beginning 1) 'org-protected
))
1084 (replace-match "\n")
1086 ;; Fill empty sections with <para></para>. This is to make sure
1087 ;; that the DocBook document generated is valid and well-formed.
1088 (goto-char (point-min))
1089 (while (re-search-forward
1090 "</title>\\([ \r\n\t]*\\)</section>" nil t
)
1091 (when (not (get-text-property (match-beginning 0) 'org-protected
))
1092 (replace-match "\n<para></para>\n" nil nil nil
1)))
1093 ;; Insert the last closing tag.
1094 (goto-char (point-max))
1096 (insert "</article>"))
1097 (or to-buffer
(save-buffer))
1098 (goto-char (point-min))
1099 (message "Exporting... done")
1100 (if (eq to-buffer
'string
)
1101 (prog1 (buffer-substring (point-min) (point-max))
1102 (kill-buffer (current-buffer)))
1103 (current-buffer)))))
1105 (defun org-export-docbook-open-para ()
1106 "Insert <para>, but first close previous paragraph if any."
1107 (org-export-docbook-close-para-maybe)
1109 (setq org-docbook-para-open t
))
1111 (defun org-export-docbook-close-para-maybe ()
1112 "Close DocBook paragraph if there is one open."
1113 (when org-docbook-para-open
1114 (insert "</para>\n")
1115 (setq org-docbook-para-open nil
)))
1117 (defun org-export-docbook-close-li (&optional type
)
1118 "Close list if necessary."
1119 (org-export-docbook-close-para-maybe)
1120 (if (equal type
"d")
1121 (insert "</listitem></varlistentry>\n")
1122 (insert "</listitem>\n")))
1124 (defun org-export-docbook-level-start (level title
)
1125 "Insert a new level in DocBook export.
1126 When TITLE is nil, just close all open levels."
1127 (org-export-docbook-close-para-maybe)
1128 (let* ((target (and title
(org-get-text-property-any 0 'target title
)))
1132 (if (aref org-levels-open
(1- l
))
1134 (insert "</section>\n")
1135 (aset org-levels-open
(1- l
) nil
)))
1138 ;; If title is nil, this means this function is called to close
1139 ;; all levels, so the rest is done only if title is given.
1141 ;; Format tags: put them into a superscript like format.
1142 (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title
)
1145 (if org-export-with-tags
1149 (match-string 1 title
)
1153 (aset org-levels-open
(1- level
) t
)
1154 (setq section-number
(org-section-number level
))
1155 (insert (format "\n<section xml:id=\"%s%s\">\n<title>%s</title>"
1156 org-export-docbook-section-id-prefix
1157 section-number title
))
1158 (org-export-docbook-open-para))))
1160 (defun org-docbook-expand (string)
1161 "Prepare STRING for DocBook export.
1162 Applies all active conversions. If there are links in the
1163 string, don't modify these."
1164 (let* ((re (concat org-bracket-link-regexp
"\\|"
1165 (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
1167 (while (setq m
(string-match re string
))
1168 (setq s
(substring string
0 m
)
1169 l
(match-string 0 string
)
1170 string
(substring string
(match-end 0)))
1171 (push (org-docbook-do-expand s
) res
)
1173 (push (org-docbook-do-expand string
) res
)
1174 (apply 'concat
(nreverse res
))))
1176 (defun org-docbook-do-expand (s)
1177 "Apply all active conversions to translate special ASCII to DocBook."
1178 (setq s
(org-html-protect s
))
1179 (while (string-match "@<\\([^&]*\\)>" s
)
1180 (setq s
(replace-match "<\\1>" t nil s
)))
1181 (if org-export-with-emphasize
1182 (setq s
(org-export-docbook-convert-emphasize s
)))
1183 (if org-export-with-special-strings
1184 (setq s
(org-export-docbook-convert-special-strings s
)))
1185 (if org-export-with-sub-superscripts
1186 (setq s
(org-export-docbook-convert-sub-super s
)))
1187 (if org-export-with-TeX-macros
1188 (let ((start 0) wd ass
)
1189 (while (setq start
(string-match "\\\\\\([a-zA-Z]+\\)\\({}\\)?"
1191 (if (get-text-property (match-beginning 0) 'org-protected s
)
1192 (setq start
(match-end 0))
1193 (setq wd
(match-string 1 s
))
1194 (if (setq ass
(assoc wd org-html-entities
))
1195 (setq s
(replace-match (or (cdr ass
)
1196 (concat "&" (car ass
) ";"))
1198 (setq start
(+ start
(length wd
))))))))
1201 (defun org-export-docbook-format-desc (desc)
1202 "Make sure DESC is valid as a description in a link."
1204 (org-docbook-do-expand desc
)))
1206 (defun org-export-docbook-convert-emphasize (string)
1207 "Apply emphasis for DocBook exporting."
1209 (while (string-match org-emph-re string s
)
1211 (substring string
(match-beginning 3) (1+ (match-beginning 3)))
1212 (substring string
(match-beginning 4) (1+ (match-beginning 4)))))
1213 (setq s
(match-beginning 0)
1216 (match-string 1 string
)
1217 (nth 1 (assoc (match-string 3 string
)
1218 org-export-docbook-emphasis-alist
))
1219 (match-string 4 string
)
1220 (nth 2 (assoc (match-string 3 string
)
1221 org-export-docbook-emphasis-alist
))
1222 (match-string 5 string
))
1223 string
(replace-match rpl t t string
)
1224 s
(+ s
(- (length rpl
) 2)))
1228 (defun org-docbook-protect (string)
1229 (org-html-protect string
))
1231 ;; For now, simply return string as it is.
1232 (defun org-export-docbook-convert-special-strings (string)
1233 "Convert special characters in STRING to DocBook."
1236 (defun org-export-docbook-get-footnotes (lines)
1237 "Given a list of LINES, return a list of alist footnotes."
1238 (let ((list nil
) line
)
1239 (while (setq line
(pop lines
))
1240 (if (string-match "^[ \t]*\\[\\([0-9]+\\)\\] \\(.+\\)" line
)
1241 (push (cons (match-string 1 line
) (match-string 2 line
))
1245 (defun org-export-docbook-format-image (src)
1246 "Create image element in DocBook."
1248 (let* ((caption (org-find-text-property-in-string 'org-caption src
))
1249 (attr (or (org-find-text-property-in-string 'org-attributes src
)
1251 (label (org-find-text-property-in-string 'org-label src
))
1252 (default-attr org-export-docbook-default-image-attributes
)
1254 (while (setq tmp
(pop default-attr
))
1255 (if (not (string-match (concat (car tmp
) "=") attr
))
1256 (setq attr
(concat attr
" " (car tmp
) "=" (cdr tmp
)))))
1257 (format "<mediaobject%s>
1258 <imageobject>\n<imagedata fileref=\"%s\" %s/>\n</imageobject>
1260 (if label
(concat " xml:id=\"" label
"\"") "")
1263 (concat "<caption>\n<para>"
1265 "</para>\n</caption>\n")
1269 (defun org-export-docbook-preprocess (parameters)
1270 "Extra preprocessing work for DocBook export."
1271 ;; Merge lines starting with "\par" to one line. Such lines are
1272 ;; regarded as the continuation of a long footnote.
1273 (goto-char (point-min))
1274 (while (re-search-forward "\n\\(\\\\par\\>\\)" nil t
)
1275 (if (not (get-text-property (match-beginning 1) 'org-protected
))
1276 (replace-match ""))))
1278 (defun org-export-docbook-finalize-table (table)
1279 "Change TABLE to informaltable if caption does not exist.
1280 TABLE is a string containing the HTML code generated by
1281 `org-format-table-html' for a table in Org-mode buffer."
1283 "^<table \\(\\(.\\|\n\\)+\\)<caption></caption>\n\\(\\(.\\|\n\\)+\\)</table>"
1285 (replace-match (concat "<informaltable "
1286 (match-string 1 table
)
1287 (match-string 3 table
)
1292 ;; Note: This function is very similar to
1293 ;; org-export-html-convert-sub-super. They can be merged in the future.
1294 (defun org-export-docbook-convert-sub-super (string)
1295 "Convert sub- and superscripts in STRING for DocBook."
1296 (let (key c
(s 0) (requireb (eq org-export-with-sub-superscripts
'{})))
1297 (while (string-match org-match-substring-regexp string s
)
1299 ((and requireb
(match-end 8)) (setq s
(match-end 2)))
1300 ((get-text-property (match-beginning 2) 'org-protected string
)
1301 (setq s
(match-end 2)))
1303 (setq s
(match-end 1)
1304 key
(if (string= (match-string 2 string
) "_")
1307 c
(or (match-string 8 string
)
1308 (match-string 6 string
)
1309 (match-string 5 string
))
1310 string
(replace-match
1311 (concat (match-string 1 string
)
1312 "<" key
">" c
"</" key
">")
1314 (while (string-match "\\\\\\([_^]\\)" string
)
1315 (setq string
(replace-match (match-string 1 string
) t t string
)))
1318 (provide 'org-docbook
)
1320 ;;; org-docbook.el ends here