org-docbook.el 50 KB

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