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