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