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