org-docbook.el 52 KB

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