Keep byte compiler happy
[org-mode.git] / lisp / org-docbook.el
blob02704c488f935e268c53d421a059dc1b2c7c3f53
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 :tags (plist-get opt-plist :tags)
533 :priority (plist-get opt-plist :priority)
534 :footnotes (plist-get opt-plist :footnotes)
535 :timestamps (plist-get opt-plist :timestamps)
536 :archived-trees
537 (plist-get opt-plist :archived-trees)
538 :select-tags (plist-get opt-plist :select-tags)
539 :exclude-tags (plist-get opt-plist :exclude-tags)
540 :add-text
541 (plist-get opt-plist :text)
542 :LaTeX-fragments
543 (plist-get opt-plist :LaTeX-fragments))
544 "[\r\n]"))
545 ;; Use literal output to show check boxes.
546 (checkbox-start
547 (nth 1 (assoc "=" org-export-docbook-emphasis-alist)))
548 (checkbox-end
549 (nth 2 (assoc "=" org-export-docbook-emphasis-alist)))
550 table-open type
551 table-buffer table-orig-buffer
552 ind item-type starter
553 rpl path attr caption label desc descp desc1 desc2 link
554 fnc item-tag item-number
555 footref-seen footnote-list
556 id-file
559 ;; Fine detailed info about author name.
560 (if (string-match "\\([^ ]+\\) \\(.+ \\)?\\([^ ]+\\)" author)
561 (progn
562 (setq firstname (match-string 1 author)
563 othername (or (match-string 2 author) "")
564 surname (match-string 3 author))))
566 ;; Get all footnote text.
567 (setq footnote-list
568 (org-export-docbook-get-footnotes lines))
570 (let ((inhibit-read-only t))
571 (org-unmodified
572 (remove-text-properties (point-min) (point-max)
573 '(:org-license-to-kill t))))
575 (setq org-min-level (org-get-min-level lines level-offset))
576 (setq org-last-level org-min-level)
577 (org-init-section-numbers)
579 ;; Get and save the date.
580 (cond
581 ((and date (string-match "%" date))
582 (setq date (format-time-string date)))
583 (date)
584 (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
586 ;; Get the language-dependent settings
587 (setq lang-words (or (assoc language org-export-language-setup)
588 (assoc "en" org-export-language-setup)))
590 ;; Switch to the output buffer. Use fundamental-mode for now. We
591 ;; could turn on nXML mode later and do some indentation.
592 (set-buffer buffer)
593 (let ((inhibit-read-only t)) (erase-buffer))
594 (fundamental-mode)
595 (org-install-letbind)
597 (and (fboundp 'set-buffer-file-coding-system)
598 (set-buffer-file-coding-system coding-system-for-write))
600 ;; The main body...
601 (let ((case-fold-search nil)
602 (org-odd-levels-only odd))
604 ;; Create local variables for all options, to make sure all called
605 ;; functions get the correct information
606 (mapc (lambda (x)
607 (set (make-local-variable (nth 2 x))
608 (plist-get opt-plist (car x))))
609 org-export-plist-vars)
611 ;; Insert DocBook file header, title, and author info.
612 (unless body-only
613 (insert org-export-docbook-header)
614 (if org-export-docbook-doctype
615 (insert org-export-docbook-doctype))
616 (insert "<!-- Date: " date " -->\n")
617 (insert (format "<!-- DocBook XML file generated by Org-mode %s Emacs %s -->\n"
618 org-version emacs-major-version))
619 (insert org-export-docbook-article-header)
620 (insert (format
621 "\n <title>%s</title>
622 <info>
623 <author>
624 <personname>
625 <firstname>%s</firstname> <othername>%s</othername> <surname>%s</surname>
626 </personname>
628 </author>
629 </info>\n"
630 (org-docbook-expand title)
631 firstname othername surname
632 (if (and org-export-email-info
633 email (string-match "\\S-" email))
634 (concat "<email>" email "</email>") "")
637 (org-init-section-numbers)
639 (org-export-docbook-open-para)
641 ;; Loop over all the lines...
642 (while (setq line (pop lines) origline line)
643 (catch 'nextline
645 ;; End of quote section?
646 (when (and inquote (string-match "^\\*+ " line))
647 (insert "]]></programlisting>\n")
648 (org-export-docbook-open-para)
649 (setq inquote nil))
650 ;; Inside a quote section?
651 (when inquote
652 (insert (org-docbook-protect line) "\n")
653 (throw 'nextline nil))
655 ;; Fixed-width, verbatim lines (examples)
656 (when (and org-export-with-fixed-width
657 (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line))
658 (when (not infixed)
659 (setq infixed t)
660 (org-export-docbook-close-para-maybe)
661 (insert "<programlisting><![CDATA["))
662 (insert (match-string 3 line) "\n")
663 (when (or (not lines)
664 (not (string-match "^[ \t]*\\(:.*\\)"
665 (car lines))))
666 (setq infixed nil)
667 (insert "]]></programlisting>\n")
668 (org-export-docbook-open-para))
669 (throw 'nextline nil))
671 ;; Protected HTML
672 (when (get-text-property 0 'org-protected line)
673 (let (par (ind (get-text-property 0 'original-indentation line)))
674 (when (re-search-backward
675 "\\(<para>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
676 (setq par (match-string 1))
677 (replace-match "\\2\n"))
678 (insert line "\n")
679 (while (and lines
680 (or (= (length (car lines)) 0)
681 (not ind)
682 (equal ind (get-text-property 0 'original-indentation (car lines))))
683 (or (= (length (car lines)) 0)
684 (get-text-property 0 'org-protected (car lines))))
685 (insert (pop lines) "\n"))
686 (and par (insert "<para>\n")))
687 (throw 'nextline nil))
689 ;; Start of block quotes and verses
690 (when (or (equal "ORG-BLOCKQUOTE-START" line)
691 (and (equal "ORG-VERSE-START" line)
692 (setq inverse t)))
693 (org-export-docbook-close-para-maybe)
694 (insert "<blockquote>")
695 ;; Check whether attribution for this blockquote exists.
696 (let (tmp1
697 attribution
698 (end (if inverse "ORG-VERSE-END" "ORG-BLOCKQUOTE-END"))
699 (quote-lines nil))
700 (while (and (setq tmp1 (pop lines))
701 (not (equal end tmp1)))
702 (push tmp1 quote-lines))
703 (push tmp1 lines) ; Put back quote end mark
704 ;; Check the last line in the quote to see if it contains
705 ;; the attribution.
706 (setq tmp1 (pop quote-lines))
707 (if (string-match "\\(^.*\\)\\(--[ \t]+\\)\\(.+\\)$" tmp1)
708 (progn
709 (setq attribution (match-string 3 tmp1))
710 (when (save-match-data
711 (string-match "[^ \t]" (match-string 1 tmp1)))
712 (push (match-string 1 tmp1) lines)))
713 (push tmp1 lines))
714 (while (setq tmp1 (pop quote-lines))
715 (push tmp1 lines))
716 (when attribution
717 (insert "<attribution>" attribution "</attribution>")))
718 ;; Insert <literallayout> for verse.
719 (if inverse
720 (insert "\n<literallayout>")
721 (org-export-docbook-open-para))
722 (throw 'nextline nil))
724 ;; End of block quotes
725 (when (equal "ORG-BLOCKQUOTE-END" line)
726 (org-export-docbook-close-para-maybe)
727 (insert "</blockquote>\n")
728 (org-export-docbook-open-para)
729 (throw 'nextline nil))
731 ;; End of verses
732 (when (equal "ORG-VERSE-END" line)
733 (insert "</literallayout>\n</blockquote>\n")
734 (org-export-docbook-open-para)
735 (setq inverse nil)
736 (throw 'nextline nil))
738 ;; Text centering. Element <para role="centered"> does not
739 ;; seem to work with FOP, so for now we use <informaltable> to
740 ;; center the text, which can contain multiple paragraphs.
741 (when (equal "ORG-CENTER-START" line)
742 (org-export-docbook-close-para-maybe)
743 (insert "<informaltable frame=\"none\" colsep=\"0\" rowsep=\"0\">\n"
744 "<tgroup align=\"center\" cols=\"1\">\n"
745 "<tbody><row><entry>\n")
746 (org-export-docbook-open-para)
747 (throw 'nextline nil))
749 (when (equal "ORG-CENTER-END" line)
750 (org-export-docbook-close-para-maybe)
751 (insert "</entry></row></tbody>\n"
752 "</tgroup>\n</informaltable>\n")
753 (org-export-docbook-open-para)
754 (throw 'nextline nil))
756 ;; Make targets to anchors. Note that currently FOP does not
757 ;; seem to support <anchor> tags when generating PDF output,
758 ;; but this can be used in DocBook --> HTML conversion.
759 (setq start 0)
760 (while (string-match
761 "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line start)
762 (cond
763 ((get-text-property (match-beginning 1) 'org-protected line)
764 (setq start (match-end 1)))
765 ((match-end 2)
766 (setq line (replace-match
767 (format "@<anchor xml:id=\"%s\"/>"
768 (org-solidify-link-text (match-string 1 line)))
769 t t line)))
771 (setq line (replace-match
772 (format "@<anchor xml:id=\"%s\"/>"
773 (org-solidify-link-text (match-string 1 line)))
774 t t line)))))
776 ;; Put time stamps and related keywords into special mark-up
777 ;; elements.
778 (setq line (org-export-docbook-handle-time-stamps line))
780 ;; Replace "&", "<" and ">" by "&amp;", "&lt;" and "&gt;".
781 ;; Handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>").
782 ;; Also handle sub_superscripts and check boxes.
783 (or (string-match org-table-hline-regexp line)
784 (setq line (org-docbook-expand line)))
786 ;; Format the links
787 (setq start 0)
788 (while (string-match org-bracket-link-analytic-regexp++ line start)
789 (setq start (match-beginning 0))
790 (setq path (save-match-data (org-link-unescape
791 (match-string 3 line))))
792 (setq type (cond
793 ((match-end 2) (match-string 2 line))
794 ((save-match-data
795 (or (file-name-absolute-p path)
796 (string-match "^\\.\\.?/" path)))
797 "file")
798 (t "internal")))
799 (setq path (org-extract-attributes (org-link-unescape path)))
800 (setq attr (get-text-property 0 'org-attributes path)
801 caption (get-text-property 0 'org-caption path)
802 label (get-text-property 0 'org-label path))
803 (setq desc1 (if (match-end 5) (match-string 5 line))
804 desc2 (if (match-end 2) (concat type ":" path) path)
805 descp (and desc1 (not (equal desc1 desc2)))
806 desc (or desc1 desc2))
807 ;; Make an image out of the description if that is so wanted
808 (when (and descp (org-file-image-p
809 desc org-export-docbook-inline-image-extensions))
810 (save-match-data
811 (if (string-match "^file:" desc)
812 (setq desc (substring desc (match-end 0))))))
813 ;; FIXME: do we need to unescape here somewhere?
814 (cond
815 ((equal type "internal")
816 (setq rpl (format "<link linkend=\"%s\">%s</link>"
817 (org-solidify-link-text
818 (save-match-data (org-link-unescape path)) nil)
819 (org-export-docbook-format-desc desc))))
820 ((and (equal type "id")
821 (setq id-file (org-id-find-id-file path)))
822 ;; This is an id: link to another file (if it was the same file,
823 ;; it would have become an internal link...)
824 (save-match-data
825 (setq id-file (file-relative-name
826 id-file (file-name-directory org-current-export-file)))
827 (setq id-file (concat (file-name-sans-extension id-file)
828 org-export-docbook-extension))
829 (setq rpl (format "<link xlink:href=\"%s#%s\">%s</link>"
830 id-file path (org-export-docbook-format-desc desc)))))
831 ((member type '("http" "https"))
832 ;; Standard URL, just check if we need to inline an image
833 (if (and (or (eq t org-export-docbook-inline-images)
834 (and org-export-docbook-inline-images (not descp)))
835 (org-file-image-p
836 path org-export-docbook-inline-image-extensions))
837 (setq rpl (org-export-docbook-format-image
838 (concat type ":" path)))
839 (setq link (concat type ":" path))
840 (setq rpl (format "<link xlink:href=\"%s\">%s</link>"
841 (org-export-html-format-href link)
842 (org-export-docbook-format-desc desc)))
844 ((member type '("ftp" "mailto" "news"))
845 ;; Standard URL
846 (setq link (concat type ":" path))
847 (setq rpl (format "<link xlink:href=\"%s\">%s</link>"
848 (org-export-html-format-href link)
849 (org-export-docbook-format-desc desc))))
850 ((string= type "coderef")
851 (setq rpl (format (org-export-get-coderef-format path (and descp desc))
852 (cdr (assoc path org-export-code-refs)))))
853 ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
854 ;; The link protocol has a function for format the link
855 (setq rpl
856 (save-match-data
857 (funcall fnc (org-link-unescape path) desc1 'html))))
859 ((string= type "file")
860 ;; FILE link
861 (let* ((filename path)
862 (abs-p (file-name-absolute-p filename))
863 thefile file-is-image-p search)
864 (save-match-data
865 (if (string-match "::\\(.*\\)" filename)
866 (setq search (match-string 1 filename)
867 filename (replace-match "" t nil filename)))
868 (setq valid
869 (if (functionp link-validate)
870 (funcall link-validate filename current-dir)
872 (setq file-is-image-p
873 (org-file-image-p
874 filename org-export-docbook-inline-image-extensions))
875 (setq thefile (if abs-p (expand-file-name filename) filename))
876 ;; Carry over the properties (expand-file-name will
877 ;; discard the properties of filename)
878 (add-text-properties 0 (1- (length thefile))
879 (list 'org-caption caption
880 'org-attributes attr
881 'org-label label)
882 thefile)
883 (when (and org-export-docbook-link-org-files-as-docbook
884 (string-match "\\.org$" thefile))
885 (setq thefile (concat (substring thefile 0
886 (match-beginning 0))
887 org-export-docbook-extension))
888 (if (and search
889 ;; make sure this is can be used as target search
890 (not (string-match "^[0-9]*$" search))
891 (not (string-match "^\\*" search))
892 (not (string-match "^/.*/$" search)))
893 (setq thefile (concat thefile "#"
894 (org-solidify-link-text
895 (org-link-unescape search)))))
896 (when (string-match "^file:" desc)
897 (setq desc (replace-match "" t t desc))
898 (if (string-match "\\.org$" desc)
899 (setq desc (replace-match "" t t desc))))))
900 (setq rpl (if (and file-is-image-p
901 (or (eq t org-export-docbook-inline-images)
902 (and org-export-docbook-inline-images
903 (not descp))))
904 (progn
905 (message "image %s %s" thefile org-docbook-para-open)
906 (org-export-docbook-format-image thefile))
907 (format "<link xlink:href=\"%s\">%s</link>"
908 thefile (org-export-docbook-format-desc desc))))
909 (if (not valid) (setq rpl desc))))
912 ;; Just publish the path, as default
913 (setq rpl (concat "&lt;" type ":"
914 (save-match-data (org-link-unescape path))
915 "&gt;"))))
916 (setq line (replace-match rpl t t line)
917 start (+ start (length rpl))))
919 ;; TODO items: can we do something better?!
920 (if (and (string-match org-todo-line-regexp line)
921 (match-beginning 2))
922 (setq line
923 (concat (substring line 0 (match-beginning 2))
924 "[" (match-string 2 line) "]"
925 (substring line (match-end 2)))))
927 ;; Does this contain a reference to a footnote?
928 (when org-export-with-footnotes
929 (setq start 0)
930 (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
931 (if (get-text-property (match-beginning 2) 'org-protected line)
932 (setq start (match-end 2))
933 (let* ((num (match-string 2 line))
934 (footnote-def (assoc num footnote-list)))
935 (if (assoc num footref-seen)
936 (setq line (replace-match
937 (format "%s<footnoteref linkend=\"%s%s\"/>"
938 (match-string 1 line)
939 org-export-docbook-footnote-id-prefix num)
940 t t line))
941 (setq line (replace-match
942 (format "%s<footnote xml:id=\"%s%s\"><para>%s</para></footnote>"
943 (match-string 1 line)
944 org-export-docbook-footnote-id-prefix
946 (if footnote-def
947 (save-match-data
948 (org-docbook-expand (cdr footnote-def)))
949 (format "FOOTNOTE DEFINITION NOT FOUND: %s" num)))
950 t t line))
951 (push (cons num 1) footref-seen))))))
953 (cond
954 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
955 ;; This is a headline
956 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
957 level-offset))
958 txt (match-string 2 line))
959 (if (string-match quote-re0 txt)
960 (setq txt (replace-match "" t t txt)))
961 (org-export-docbook-level-start level txt)
962 ;; QUOTES
963 (when (string-match quote-re line)
964 (org-export-docbook-close-para-maybe)
965 (insert "<programlisting><![CDATA[")
966 (setq inquote t)))
968 ;; Tables: since version 4.3 of DocBook DTD, HTML tables are
969 ;; supported. We can use existing HTML table exporter code
970 ;; here.
971 ((and org-export-with-tables
972 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
973 (if (not table-open)
974 ;; New table starts
975 (setq table-open t
976 table-buffer nil
977 table-orig-buffer nil))
978 ;; Accumulate lines
979 (setq table-buffer (cons line table-buffer)
980 table-orig-buffer (cons origline table-orig-buffer))
981 (when (or (not lines)
982 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
983 (car lines))))
984 (setq table-open nil
985 table-buffer (nreverse table-buffer)
986 table-orig-buffer (nreverse table-orig-buffer))
987 (org-export-docbook-close-para-maybe)
988 (insert (org-export-docbook-finalize-table
989 (org-format-table-html table-buffer table-orig-buffer
990 'no-css)))))
992 ;; Normal lines
994 ;; This line either is list item or end a list.
995 (when (when (get-text-property 0 'list-item line)
996 (setq line (org-export-docbook-list-line
997 line
998 (get-text-property 0 'list-item line)
999 (get-text-property 0 'list-struct line)
1000 (get-text-property 0 'list-prevs line)))))
1002 ;; Empty lines start a new paragraph. If hand-formatted lists
1003 ;; are not fully interpreted, lines starting with "-", "+", "*"
1004 ;; also start a new paragraph.
1005 (if (and (string-match "^ [-+*]-\\|^[ \t]*$" line)
1006 (not inverse))
1007 (org-export-docbook-open-para))
1009 ;; Is this the start of a footnote?
1010 (when org-export-with-footnotes
1011 (when (and (boundp 'footnote-section-tag-regexp)
1012 (string-match (concat "^" footnote-section-tag-regexp)
1013 line))
1014 ;; ignore this line
1015 (throw 'nextline nil))
1016 ;; These footnote lines have been read and saved before,
1017 ;; ignore them at this time.
1018 (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
1019 (org-export-docbook-close-para-maybe)
1020 (throw 'nextline nil)))
1022 ;; FIXME: It might be a good idea to add an option to
1023 ;; support line break processing instruction <?linebreak?>.
1024 ;; Org-mode supports line break "\\" in HTML exporter, and
1025 ;; some DocBook users may also want to force line breaks
1026 ;; even though DocBook only supports that in
1027 ;; <literallayout>.
1029 (insert line "\n")))))
1031 ;; Properly close all local lists and other lists
1032 (when inquote
1033 (insert "]]></programlisting>\n")
1034 (org-export-docbook-open-para))
1036 ;; Close all open sections.
1037 (org-export-docbook-level-start 1 nil)
1039 (unless (plist-get opt-plist :buffer-will-be-killed)
1040 (normal-mode)
1041 (if (eq major-mode (default-value 'major-mode))
1042 (nxml-mode)))
1044 ;; Remove empty paragraphs. Replace them with a newline.
1045 (goto-char (point-min))
1046 (while (re-search-forward
1047 "[ \r\n\t]*\\(<para>\\)[ \r\n\t]*</para>[ \r\n\t]*" nil t)
1048 (when (not (get-text-property (match-beginning 1) 'org-protected))
1049 (replace-match "\n")
1050 (backward-char 1)))
1051 ;; Fill empty sections with <para></para>. This is to make sure
1052 ;; that the DocBook document generated is valid and well-formed.
1053 (goto-char (point-min))
1054 (while (re-search-forward
1055 "</title>\\([ \r\n\t]*\\)</section>" nil t)
1056 (when (not (get-text-property (match-beginning 0) 'org-protected))
1057 (replace-match "\n<para></para>\n" nil nil nil 1)))
1058 ;; Insert the last closing tag.
1059 (goto-char (point-max))
1060 (unless body-only
1061 (insert "</article>"))
1062 (run-hooks 'org-export-docbook-final-hook)
1063 (or to-buffer (save-buffer))
1064 (goto-char (point-min))
1065 (or (org-export-push-to-kill-ring "DocBook")
1066 (message "Exporting... done"))
1067 (if (eq to-buffer 'string)
1068 (prog1 (buffer-substring (point-min) (point-max))
1069 (kill-buffer (current-buffer)))
1070 (current-buffer)))))
1072 (defun org-export-docbook-open-para ()
1073 "Insert <para>, but first close previous paragraph if any."
1074 (org-export-docbook-close-para-maybe)
1075 (insert "\n<para>")
1076 (setq org-docbook-para-open t))
1078 (defun org-export-docbook-close-para-maybe ()
1079 "Close DocBook paragraph if there is one open."
1080 (when org-docbook-para-open
1081 (insert "</para>\n")
1082 (setq org-docbook-para-open nil)))
1084 (defun org-export-docbook-close-li (&optional type)
1085 "Close list if necessary."
1086 (org-export-docbook-close-para-maybe)
1087 (if (equal type "d")
1088 (insert "</listitem></varlistentry>\n")
1089 (insert "</listitem>\n")))
1091 (defun org-export-docbook-level-start (level title)
1092 "Insert a new level in DocBook export.
1093 When TITLE is nil, just close all open levels."
1094 (org-export-docbook-close-para-maybe)
1095 (let* ((target (and title (org-get-text-property-any 0 'target title)))
1096 (l org-level-max)
1097 section-number)
1098 (while (>= l level)
1099 (if (aref org-levels-open (1- l))
1100 (progn
1101 (insert "</section>\n")
1102 (aset org-levels-open (1- l) nil)))
1103 (setq l (1- l)))
1104 (when title
1105 ;; If title is nil, this means this function is called to close
1106 ;; all levels, so the rest is done only if title is given.
1108 ;; Format tags: put them into a superscript like format.
1109 (when (string-match (org-re "\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") title)
1110 (setq title
1111 (replace-match
1112 (if org-export-with-tags
1113 (save-match-data
1114 (concat
1115 "<superscript>"
1116 (match-string 1 title)
1117 "</superscript>"))
1119 t t title)))
1120 (aset org-levels-open (1- level) t)
1121 (setq section-number (org-section-number level))
1122 (insert (format "\n<section xml:id=\"%s%s\">\n<title>%s</title>"
1123 org-export-docbook-section-id-prefix
1124 (replace-regexp-in-string "\\." "_" section-number)
1125 title))
1126 (org-export-docbook-open-para))))
1128 (defun org-docbook-expand (string)
1129 "Prepare STRING for DocBook export.
1130 Applies all active conversions. If there are links in the
1131 string, don't modify these."
1132 (let* ((re (concat org-bracket-link-regexp "\\|"
1133 (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")))
1134 m s l res)
1135 (while (setq m (string-match re string))
1136 (setq s (substring string 0 m)
1137 l (match-string 0 string)
1138 string (substring string (match-end 0)))
1139 (push (org-docbook-do-expand s) res)
1140 (push l res))
1141 (push (org-docbook-do-expand string) res)
1142 (apply 'concat (nreverse res))))
1144 (defun org-docbook-do-expand (s)
1145 "Apply all active conversions to translate special ASCII to DocBook."
1146 (setq s (org-html-protect s))
1147 (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
1148 (setq s (replace-match "<\\1>" t nil s)))
1149 (if org-export-with-emphasize
1150 (setq s (org-export-docbook-convert-emphasize s)))
1151 (if org-export-with-special-strings
1152 (setq s (org-export-docbook-convert-special-strings s)))
1153 (if org-export-with-sub-superscripts
1154 (setq s (org-export-docbook-convert-sub-super s)))
1155 (if org-export-with-TeX-macros
1156 (let ((start 0) wd rep)
1157 (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)\\({}\\)?"
1158 s start))
1159 (if (get-text-property (match-beginning 0) 'org-protected s)
1160 (setq start (match-end 0))
1161 (setq wd (match-string 1 s))
1162 (if (setq rep (org-entity-get-representation wd 'html))
1163 (setq s (replace-match rep t t s))
1164 (setq start (+ start (length wd))))))))
1167 (defun org-export-docbook-format-desc (desc)
1168 "Make sure DESC is valid as a description in a link."
1169 (save-match-data
1170 (org-docbook-do-expand desc)))
1172 (defun org-export-docbook-convert-emphasize (string)
1173 "Apply emphasis for DocBook exporting."
1174 (let ((s 0) rpl)
1175 (while (string-match org-emph-re string s)
1176 (if (not (equal
1177 (substring string (match-beginning 3) (1+ (match-beginning 3)))
1178 (substring string (match-beginning 4) (1+ (match-beginning 4)))))
1179 (setq s (match-beginning 0)
1181 (concat
1182 (match-string 1 string)
1183 (nth 1 (assoc (match-string 3 string)
1184 org-export-docbook-emphasis-alist))
1185 (match-string 4 string)
1186 (nth 2 (assoc (match-string 3 string)
1187 org-export-docbook-emphasis-alist))
1188 (match-string 5 string))
1189 string (replace-match rpl t t string)
1190 s (+ s (- (length rpl) 2)))
1191 (setq s (1+ s))))
1192 string))
1194 (defun org-docbook-protect (string)
1195 (org-html-protect string))
1197 ;; For now, simply return string as it is.
1198 (defun org-export-docbook-convert-special-strings (string)
1199 "Convert special characters in STRING to DocBook."
1200 string)
1202 (defun org-export-docbook-get-footnotes (lines)
1203 "Given a list of LINES, return a list of alist footnotes."
1204 (let ((list nil) line)
1205 (while (setq line (pop lines))
1206 (if (string-match "^[ \t]*\\[\\([0-9]+\\)\\] \\(.+\\)" line)
1207 (push (cons (match-string 1 line) (match-string 2 line))
1208 list)))
1209 list))
1211 (defun org-export-docbook-format-image (src)
1212 "Create image element in DocBook."
1213 (save-match-data
1214 (let* ((caption (org-find-text-property-in-string 'org-caption src))
1215 (attr (or (org-find-text-property-in-string 'org-attributes src)
1216 ""))
1217 (label (org-find-text-property-in-string 'org-label src))
1218 (default-attr org-export-docbook-default-image-attributes)
1219 tmp)
1220 (setq caption (and caption (org-html-do-expand caption)))
1221 (while (setq tmp (pop default-attr))
1222 (if (not (string-match (concat (car tmp) "=") attr))
1223 (setq attr (concat attr " " (car tmp) "=" (cdr tmp)))))
1224 (format "<mediaobject%s>
1225 <imageobject>\n<imagedata fileref=\"%s\" %s/>\n</imageobject>
1226 %s</mediaobject>"
1227 (if label (concat " xml:id=\"" label "\"") "")
1228 src attr
1229 (if caption
1230 (concat "<caption>\n<para>"
1231 caption
1232 "</para>\n</caption>\n")
1234 ))))
1236 (defun org-export-docbook-preprocess (parameters)
1237 "Extra preprocessing work for DocBook export."
1238 ;; Merge lines starting with "\par" to one line. Such lines are
1239 ;; regarded as the continuation of a long footnote.
1240 (goto-char (point-min))
1241 (while (re-search-forward "\n\\(\\\\par\\>\\)" nil t)
1242 (if (not (get-text-property (match-beginning 1) 'org-protected))
1243 (replace-match ""))))
1245 (defun org-export-docbook-finalize-table (table)
1246 "Clean up TABLE and turn it into DocBook format.
1247 This function adds a label to the table if it is available, and
1248 also changes TABLE to informaltable if caption does not exist.
1249 TABLE is a string containing the HTML code generated by
1250 `org-format-table-html' for a table in Org-mode buffer."
1251 (let (table-with-label)
1252 ;; Get the label if it exists, and move it into the <table> element.
1253 (setq table-with-label
1254 (if (string-match
1255 "^<table \\(\\(.\\|\n\\)+\\)<a name=\"\\(.+\\)\" id=\".+\"></a>\n\\(\\(.\\|\n\\)+\\)</table>"
1256 table)
1257 (replace-match (concat "<table xml:id=\"" (match-string 3 table) "\" "
1258 (match-string 1 table)
1259 (match-string 4 table)
1260 "</table>")
1261 nil t table)
1262 table))
1263 ;; Change <table> into <informaltable> if caption does not exist.
1264 (if (string-match
1265 "^<table \\(\\(.\\|\n\\)+\\)<caption></caption>\n\\(\\(.\\|\n\\)+\\)</table>"
1266 table-with-label)
1267 (replace-match (concat "<informaltable "
1268 (match-string 1 table-with-label)
1269 (match-string 3 table-with-label)
1270 "</informaltable>")
1271 nil t table-with-label)
1272 table-with-label)))
1274 ;; Note: This function is very similar to
1275 ;; org-export-html-convert-sub-super. They can be merged in the future.
1276 (defun org-export-docbook-convert-sub-super (string)
1277 "Convert sub- and superscripts in STRING for DocBook."
1278 (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
1279 (while (string-match org-match-substring-regexp string s)
1280 (cond
1281 ((and requireb (match-end 8)) (setq s (match-end 2)))
1282 ((get-text-property (match-beginning 2) 'org-protected string)
1283 (setq s (match-end 2)))
1285 (setq s (match-end 1)
1286 key (if (string= (match-string 2 string) "_")
1287 "subscript"
1288 "superscript")
1289 c (or (match-string 8 string)
1290 (match-string 6 string)
1291 (match-string 5 string))
1292 string (replace-match
1293 (concat (match-string 1 string)
1294 "<" key ">" c "</" key ">")
1295 t t string)))))
1296 (while (string-match "\\\\\\([_^]\\)" string)
1297 (setq string (replace-match (match-string 1 string) t t string)))
1298 string))
1300 (defun org-export-docbook-protect-tags (string)
1301 "Change ``<...>'' in string STRING into ``@<...>''.
1302 This is normally needed when STRING contains DocBook elements
1303 that need to be preserved in later phase of DocBook exporting."
1304 (let ((start 0))
1305 (while (string-match "<\\([^>]*\\)>" string start)
1306 (setq string (replace-match
1307 "@<\\1>" t nil string)
1308 start (match-end 0)))
1309 string))
1311 (defun org-export-docbook-handle-time-stamps (line)
1312 "Format time stamps in string LINE."
1313 (let (replaced
1314 (kw-markup (org-export-docbook-protect-tags
1315 org-export-docbook-keywords-markup))
1316 (ts-markup (org-export-docbook-protect-tags
1317 org-export-docbook-timestamp-markup)))
1318 (while (string-match org-maybe-keyword-time-regexp line)
1319 (setq replaced
1320 (concat replaced
1321 (substring line 0 (match-beginning 0))
1322 (if (match-end 1)
1323 (format kw-markup
1324 (match-string 1 line)))
1326 (format ts-markup
1327 (substring (org-translate-time
1328 (match-string 3 line)) 1 -1)))
1329 line (substring line (match-end 0))))
1330 (concat replaced line)))
1332 (defun org-export-docbook-list-line (line pos struct prevs)
1333 "Insert list syntax in export buffer. Return LINE, maybe modified.
1335 POS is the item position or line position the line had before
1336 modifications to buffer. STRUCT is the list structure. PREVS is
1337 the alist of previous items."
1338 (let* ((get-type
1339 (function
1340 ;; Translate type of list containing POS to "ordered",
1341 ;; "variable" or "itemized".
1342 (lambda (pos struct prevs)
1343 (let ((type (org-list-get-list-type pos struct prevs)))
1344 (cond
1345 ((eq 'ordered type) "ordered")
1346 ((eq 'descriptive type) "variable")
1347 (t "itemized"))))))
1348 (get-closings
1349 (function
1350 ;; Return list of all items and sublists ending at POS, in
1351 ;; reverse order.
1352 (lambda (pos)
1353 (let (out)
1354 (catch 'exit
1355 (mapc (lambda (e)
1356 (let ((end (nth 6 e))
1357 (item (car e)))
1358 (cond
1359 ((= end pos) (push item out))
1360 ((>= item pos) (throw 'exit nil)))))
1361 struct))
1362 out)))))
1363 ;; First close any previous item, or list, ending at POS.
1364 (mapc (lambda (e)
1365 (let* ((lastp (= (org-list-get-last-item e struct prevs) e))
1366 (first-item (org-list-get-list-begin e struct prevs))
1367 (type (funcall get-type first-item struct prevs)))
1368 ;; Ending for every item
1369 (org-export-docbook-close-para-maybe)
1370 (insert (if (equal type "variable")
1371 "</listitem></varlistentry>\n"
1372 "</listitem>\n"))
1373 ;; We're ending last item of the list: end list.
1374 (when lastp
1375 (insert (format "</%slist>\n" type))
1376 (org-export-docbook-open-para))))
1377 (funcall get-closings pos))
1378 (cond
1379 ;; At an item: insert appropriate tags in export buffer.
1380 ((assq pos struct)
1381 (string-match (concat "[ \t]*\\(\\S-+[ \t]*\\)"
1382 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[a-zA-Z]\\)\\]\\)?"
1383 "\\(?:\\(\\[[ X-]\\]\\)[ \t]+\\)?"
1384 "\\(?:\\(.*\\)[ \t]+::[ \t]+\\)?"
1385 "\\(.*\\)")
1386 line)
1387 (let* ((checkbox (match-string 3 line))
1388 (desc-tag (or (match-string 4 line) "???"))
1389 (body (match-string 5 line))
1390 (list-beg (org-list-get-list-begin pos struct prevs))
1391 (firstp (= list-beg pos))
1392 ;; Always refer to first item to determine list type, in
1393 ;; case list is ill-formed.
1394 (type (funcall get-type list-beg struct prevs))
1395 ;; Special variables for ordered lists.
1396 (counter (let ((count-tmp (org-list-get-counter pos struct)))
1397 (cond
1398 ((not count-tmp) nil)
1399 ((string-match "[A-Za-z]" count-tmp)
1400 (- (string-to-char (upcase count-tmp)) 64))
1401 ((string-match "[0-9]+" count-tmp)
1402 count-tmp)))))
1403 ;; When FIRSTP, a new list or sub-list is starting.
1404 (when firstp
1405 (org-export-docbook-close-para-maybe)
1406 (insert (format "<%slist>\n" type)))
1407 (insert (cond
1408 ((equal type "variable")
1409 (format "<varlistentry><term>%s</term><listitem>" desc-tag))
1410 ((and (equal type "ordered") counter)
1411 (format "<listitem override=\"%s\">" counter))
1412 (t "<listitem>")))
1413 ;; For DocBook, we need to open a para right after tag
1414 ;; <listitem>.
1415 (org-export-docbook-open-para)
1416 ;; If line had a checkbox, some additional modification is required.
1417 (when checkbox (setq body (concat checkbox " " body)))
1418 ;; Return modified line
1419 body))
1420 ;; At a list ender: normal text follows: need <para>.
1421 ((equal "ORG-LIST-END-MARKER" line)
1422 (org-export-docbook-open-para)
1423 (throw 'nextline nil))
1424 ;; Not at an item: return line unchanged (side-effects only).
1425 (t line))))
1427 (provide 'org-docbook)
1429 ;; arch-tag: a24a127c-d365-4c2a-9e9b-f7dcb0ebfdc3
1430 ;;; org-docbook.el ends here