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