org-docbook.el 49 KB

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