org-docbook.el 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  1. ;;; org-docbook.el --- DocBook exporter for org-mode
  2. ;;
  3. ;; Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
  4. ;;
  5. ;; Emacs Lisp Archive Entry
  6. ;; Filename: org-docbook.el
  7. ;; Version: 6.35e
  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. (run-hooks 'org-export-first-hook)
  333. ;; Make sure we have a file name when we need it.
  334. (when (and (not (or to-buffer body-only))
  335. (not buffer-file-name))
  336. (if (buffer-base-buffer)
  337. (org-set-local 'buffer-file-name
  338. (with-current-buffer (buffer-base-buffer)
  339. buffer-file-name))
  340. (error "Need a file name to be able to export")))
  341. (message "Exporting...")
  342. (setq-default org-todo-line-regexp org-todo-line-regexp)
  343. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  344. (setq-default org-done-keywords org-done-keywords)
  345. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  346. (let* ((opt-plist
  347. (org-export-process-option-filters
  348. (org-combine-plists (org-default-export-plist)
  349. ext-plist
  350. (org-infile-export-plist))))
  351. (link-validate (plist-get opt-plist :link-validation-function))
  352. valid
  353. (odd org-odd-levels-only)
  354. (region-p (org-region-active-p))
  355. (rbeg (and region-p (region-beginning)))
  356. (rend (and region-p (region-end)))
  357. (subtree-p
  358. (if (plist-get opt-plist :ignore-subtree-p)
  359. nil
  360. (when region-p
  361. (save-excursion
  362. (goto-char rbeg)
  363. (and (org-at-heading-p)
  364. (>= (org-end-of-subtree t t) rend))))))
  365. (level-offset (if subtree-p
  366. (save-excursion
  367. (goto-char rbeg)
  368. (+ (funcall outline-level)
  369. (if org-odd-levels-only 1 0)))
  370. 0))
  371. (opt-plist (setq org-export-opt-plist
  372. (if subtree-p
  373. (org-export-add-subtree-options opt-plist rbeg)
  374. opt-plist)))
  375. ;; The following two are dynamically scoped into other
  376. ;; routines below.
  377. (org-current-export-dir
  378. (or pub-dir (org-export-directory :docbook opt-plist)))
  379. (org-current-export-file buffer-file-name)
  380. (level 0) (line "") (origline "") txt todo
  381. (filename (if to-buffer nil
  382. (expand-file-name
  383. (concat
  384. (file-name-sans-extension
  385. (or (and subtree-p
  386. (org-entry-get (region-beginning)
  387. "EXPORT_FILE_NAME" t))
  388. (file-name-nondirectory buffer-file-name)))
  389. org-export-docbook-extension)
  390. (file-name-as-directory
  391. (or pub-dir (org-export-directory :docbook opt-plist))))))
  392. (current-dir (if buffer-file-name
  393. (file-name-directory buffer-file-name)
  394. default-directory))
  395. (buffer (if to-buffer
  396. (cond
  397. ((eq to-buffer 'string) (get-buffer-create "*Org DocBook Export*"))
  398. (t (get-buffer-create to-buffer)))
  399. (find-file-noselect filename)))
  400. ;; org-levels-open is a global variable
  401. (org-levels-open (make-vector org-level-max nil))
  402. (date (plist-get opt-plist :date))
  403. (author (or (plist-get opt-plist :author)
  404. user-full-name))
  405. (email (plist-get opt-plist :email))
  406. firstname othername surname
  407. (title (or (and subtree-p (org-export-get-title-from-subtree))
  408. (plist-get opt-plist :title)
  409. (and (not
  410. (plist-get opt-plist :skip-before-1st-heading))
  411. (org-export-grab-title-from-buffer))
  412. (and buffer-file-name
  413. (file-name-sans-extension
  414. (file-name-nondirectory buffer-file-name)))
  415. "UNTITLED"))
  416. ;; We will use HTML table formatter to export tables to DocBook
  417. ;; format, so need to set html-table-tag here.
  418. (html-table-tag (plist-get opt-plist :html-table-tag))
  419. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  420. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  421. (inquote nil)
  422. (infixed nil)
  423. (inverse nil)
  424. (in-local-list nil)
  425. (local-list-type nil)
  426. (local-list-indent nil)
  427. (llt org-plain-list-ordered-item-terminator)
  428. (email (plist-get opt-plist :email))
  429. (language (plist-get opt-plist :language))
  430. (lang-words nil)
  431. cnt
  432. (start 0)
  433. (coding-system (and (boundp 'buffer-file-coding-system)
  434. buffer-file-coding-system))
  435. (coding-system-for-write (or org-export-docbook-coding-system
  436. coding-system))
  437. (save-buffer-coding-system (or org-export-docbook-coding-system
  438. coding-system))
  439. (charset (and coding-system-for-write
  440. (fboundp 'coding-system-get)
  441. (coding-system-get coding-system-for-write
  442. 'mime-charset)))
  443. (region
  444. (buffer-substring
  445. (if region-p (region-beginning) (point-min))
  446. (if region-p (region-end) (point-max))))
  447. (lines
  448. (org-split-string
  449. (org-export-preprocess-string
  450. region
  451. :emph-multiline t
  452. :for-docbook t
  453. :skip-before-1st-heading
  454. (plist-get opt-plist :skip-before-1st-heading)
  455. :drawers (plist-get opt-plist :drawers)
  456. :todo-keywords (plist-get opt-plist :todo-keywords)
  457. :tags (plist-get opt-plist :tags)
  458. :priority (plist-get opt-plist :priority)
  459. :footnotes (plist-get opt-plist :footnotes)
  460. :timestamps (plist-get opt-plist :timestamps)
  461. :archived-trees
  462. (plist-get opt-plist :archived-trees)
  463. :select-tags (plist-get opt-plist :select-tags)
  464. :exclude-tags (plist-get opt-plist :exclude-tags)
  465. :add-text
  466. (plist-get opt-plist :text)
  467. :LaTeX-fragments
  468. (plist-get opt-plist :LaTeX-fragments))
  469. "[\r\n]"))
  470. ;; Use literal output to show check boxes.
  471. (checkbox-start
  472. (nth 1 (assoc "=" org-export-docbook-emphasis-alist)))
  473. (checkbox-end
  474. (nth 2 (assoc "=" org-export-docbook-emphasis-alist)))
  475. table-open type
  476. table-buffer table-orig-buffer
  477. ind item-type starter didclose
  478. rpl path attr caption label desc descp desc1 desc2 link
  479. fnc item-tag
  480. footref-seen footnote-list
  481. id-file
  482. )
  483. ;; Fine detailed info about author name.
  484. (if (string-match "\\([^ ]+\\) \\(.+ \\)?\\([^ ]+\\)" author)
  485. (progn
  486. (setq firstname (match-string 1 author)
  487. othername (or (match-string 2 author) "")
  488. surname (match-string 3 author))))
  489. ;; Get all footnote text.
  490. (setq footnote-list
  491. (org-export-docbook-get-footnotes lines))
  492. (let ((inhibit-read-only t))
  493. (org-unmodified
  494. (remove-text-properties (point-min) (point-max)
  495. '(:org-license-to-kill t))))
  496. (setq org-min-level (org-get-min-level lines level-offset))
  497. (setq org-last-level org-min-level)
  498. (org-init-section-numbers)
  499. ;; Get and save the date.
  500. (cond
  501. ((and date (string-match "%" date))
  502. (setq date (format-time-string date)))
  503. (date)
  504. (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
  505. ;; Get the language-dependent settings
  506. (setq lang-words (or (assoc language org-export-language-setup)
  507. (assoc "en" org-export-language-setup)))
  508. ;; Switch to the output buffer. Use fundamental-mode for now. We
  509. ;; could turn on nXML mode later and do some indentation.
  510. (set-buffer buffer)
  511. (let ((inhibit-read-only t)) (erase-buffer))
  512. (fundamental-mode)
  513. (org-install-letbind)
  514. (and (fboundp 'set-buffer-file-coding-system)
  515. (set-buffer-file-coding-system coding-system-for-write))
  516. ;; The main body...
  517. (let ((case-fold-search nil)
  518. (org-odd-levels-only odd))
  519. ;; Create local variables for all options, to make sure all called
  520. ;; functions get the correct information
  521. (mapc (lambda (x)
  522. (set (make-local-variable (nth 2 x))
  523. (plist-get opt-plist (car x))))
  524. org-export-plist-vars)
  525. ;; Insert DocBook file header, title, and author info.
  526. (unless body-only
  527. (insert org-export-docbook-header)
  528. (if org-export-docbook-doctype
  529. (insert org-export-docbook-doctype))
  530. (insert "<!-- Date: " date " -->\n")
  531. (insert (format "<!-- DocBook XML file generated by Org-mode %s Emacs %s -->\n"
  532. org-version emacs-major-version))
  533. (insert org-export-docbook-article-header)
  534. (insert (format
  535. "\n <title>%s</title>
  536. <info>
  537. <author>
  538. <personname>
  539. <firstname>%s</firstname> <othername>%s</othername> <surname>%s</surname>
  540. </personname>
  541. %s
  542. </author>
  543. </info>\n"
  544. (org-docbook-expand title)
  545. firstname othername surname
  546. (if (and org-export-email-info
  547. email (string-match "\\S-" email))
  548. (concat "<email>" email "</email>") "")
  549. )))
  550. (org-init-section-numbers)
  551. (org-export-docbook-open-para)
  552. ;; Loop over all the lines...
  553. (while (setq line (pop lines) origline line)
  554. (catch 'nextline
  555. ;; End of quote section?
  556. (when (and inquote (string-match "^\\*+ " line))
  557. (insert "]]></programlisting>\n")
  558. (org-export-docbook-open-para)
  559. (setq inquote nil))
  560. ;; Inside a quote section?
  561. (when inquote
  562. (insert (org-docbook-protect line) "\n")
  563. (throw 'nextline nil))
  564. ;; Fixed-width, verbatim lines (examples)
  565. (when (and org-export-with-fixed-width
  566. (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line))
  567. (when (not infixed)
  568. (setq infixed t)
  569. (org-export-docbook-close-para-maybe)
  570. (insert "<programlisting><![CDATA["))
  571. (insert (match-string 3 line) "\n")
  572. (when (or (not lines)
  573. (not (string-match "^[ \t]*\\(:.*\\)"
  574. (car lines))))
  575. (setq infixed nil)
  576. (insert "]]></programlisting>\n")
  577. (org-export-docbook-open-para))
  578. (throw 'nextline nil))
  579. (org-export-docbook-close-lists-maybe line)
  580. ;; Protected HTML
  581. (when (get-text-property 0 'org-protected line)
  582. (let (par (ind (get-text-property 0 'original-indentation line)))
  583. (when (re-search-backward
  584. "\\(<para>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  585. (setq par (match-string 1))
  586. (replace-match "\\2\n"))
  587. (insert line "\n")
  588. (while (and lines
  589. (or (= (length (car lines)) 0)
  590. (not ind)
  591. (equal ind (get-text-property 0 'original-indentation (car lines))))
  592. (or (= (length (car lines)) 0)
  593. (get-text-property 0 'org-protected (car lines))))
  594. (insert (pop lines) "\n"))
  595. (and par (insert "<para>\n")))
  596. (throw 'nextline nil))
  597. ;; Start of block quotes and verses
  598. (when (or (equal "ORG-BLOCKQUOTE-START" line)
  599. (and (equal "ORG-VERSE-START" line)
  600. (setq inverse t)))
  601. (org-export-docbook-close-para-maybe)
  602. (insert "<blockquote>")
  603. ;; Check whether attribution for this blockquote exists.
  604. (let (tmp1
  605. attribution
  606. (end (if inverse "ORG-VERSE-END" "ORG-BLOCKQUOTE-END"))
  607. (quote-lines nil))
  608. (while (and (setq tmp1 (pop lines))
  609. (not (equal end tmp1)))
  610. (push tmp1 quote-lines))
  611. (push tmp1 lines) ; Put back quote end mark
  612. ;; Check the last line in the quote to see if it contains
  613. ;; the attribution.
  614. (setq tmp1 (pop quote-lines))
  615. (if (string-match "\\(^.*\\)\\(--[ \t]+\\)\\(.+\\)$" tmp1)
  616. (progn
  617. (setq attribution (match-string 3 tmp1))
  618. (when (save-match-data
  619. (string-match "[^ \t]" (match-string 1 tmp1)))
  620. (push (match-string 1 tmp1) lines)))
  621. (push tmp1 lines))
  622. (while (setq tmp1 (pop quote-lines))
  623. (push tmp1 lines))
  624. (when attribution
  625. (insert "<attribution>" attribution "</attribution>")))
  626. ;; Insert <literallayout> for verse.
  627. (if inverse
  628. (insert "\n<literallayout>")
  629. (org-export-docbook-open-para))
  630. (throw 'nextline nil))
  631. ;; End of block quotes
  632. (when (equal "ORG-BLOCKQUOTE-END" line)
  633. (org-export-docbook-close-para-maybe)
  634. (insert "</blockquote>\n")
  635. (org-export-docbook-open-para)
  636. (throw 'nextline nil))
  637. ;; End of verses
  638. (when (equal "ORG-VERSE-END" line)
  639. (insert "</literallayout>\n</blockquote>\n")
  640. (org-export-docbook-open-para)
  641. (setq inverse nil)
  642. (throw 'nextline nil))
  643. ;; Text centering. Element <para role="centered"> does not
  644. ;; seem to work with FOP, so for now we use <informaltable> to
  645. ;; center the text, which can contain multiple paragraphs.
  646. (when (equal "ORG-CENTER-START" line)
  647. (org-export-docbook-close-para-maybe)
  648. (insert "<informaltable frame=\"none\" colsep=\"0\" rowsep=\"0\">\n"
  649. "<tgroup align=\"center\" cols=\"1\">\n"
  650. "<tbody><row><entry>\n")
  651. (org-export-docbook-open-para)
  652. (throw 'nextline nil))
  653. (when (equal "ORG-CENTER-END" line)
  654. (org-export-docbook-close-para-maybe)
  655. (insert "</entry></row></tbody>\n"
  656. "</tgroup>\n</informaltable>\n")
  657. (org-export-docbook-open-para)
  658. (throw 'nextline nil))
  659. ;; Make targets to anchors. Note that currently FOP does not
  660. ;; seem to support <anchor> tags when generating PDF output,
  661. ;; but this can be used in DocBook --> HTML conversion.
  662. (setq start 0)
  663. (while (string-match
  664. "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line start)
  665. (cond
  666. ((get-text-property (match-beginning 1) 'org-protected line)
  667. (setq start (match-end 1)))
  668. ((match-end 2)
  669. (setq line (replace-match
  670. (format "@<anchor xml:id=\"%s\"/>"
  671. (org-solidify-link-text (match-string 1 line)))
  672. t t line)))
  673. (t
  674. (setq line (replace-match
  675. (format "@<anchor xml:id=\"%s\"/>"
  676. (org-solidify-link-text (match-string 1 line)))
  677. t t line)))))
  678. ;; Put time stamps and related keywords into special mark-up
  679. ;; elements.
  680. (setq line (org-export-docbook-handle-time-stamps line))
  681. ;; Replace "&", "<" and ">" by "&amp;", "&lt;" and "&gt;".
  682. ;; Handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>").
  683. ;; Also handle sub_superscripts and check boxes.
  684. (or (string-match org-table-hline-regexp line)
  685. (setq line (org-docbook-expand line)))
  686. ;; Format the links
  687. (setq start 0)
  688. (while (string-match org-bracket-link-analytic-regexp++ line start)
  689. (setq start (match-beginning 0))
  690. (setq path (save-match-data (org-link-unescape
  691. (match-string 3 line))))
  692. (setq type (cond
  693. ((match-end 2) (match-string 2 line))
  694. ((save-match-data
  695. (or (file-name-absolute-p path)
  696. (string-match "^\\.\\.?/" path)))
  697. "file")
  698. (t "internal")))
  699. (setq path (org-extract-attributes (org-link-unescape path)))
  700. (setq attr (get-text-property 0 'org-attributes path)
  701. caption (get-text-property 0 'org-caption path)
  702. label (get-text-property 0 'org-label path))
  703. (setq desc1 (if (match-end 5) (match-string 5 line))
  704. desc2 (if (match-end 2) (concat type ":" path) path)
  705. descp (and desc1 (not (equal desc1 desc2)))
  706. desc (or desc1 desc2))
  707. ;; Make an image out of the description if that is so wanted
  708. (when (and descp (org-file-image-p
  709. desc org-export-docbook-inline-image-extensions))
  710. (save-match-data
  711. (if (string-match "^file:" desc)
  712. (setq desc (substring desc (match-end 0))))))
  713. ;; FIXME: do we need to unescape here somewhere?
  714. (cond
  715. ((equal type "internal")
  716. (setq rpl (format "<link linkend=\"%s\">%s</link>"
  717. (org-solidify-link-text
  718. (save-match-data (org-link-unescape path)) nil)
  719. (org-export-docbook-format-desc desc))))
  720. ((and (equal type "id")
  721. (setq id-file (org-id-find-id-file path)))
  722. ;; This is an id: link to another file (if it was the same file,
  723. ;; it would have become an internal link...)
  724. (save-match-data
  725. (setq id-file (file-relative-name
  726. id-file (file-name-directory org-current-export-file)))
  727. (setq id-file (concat (file-name-sans-extension id-file)
  728. org-export-docbook-extension))
  729. (setq rpl (format "<link xlink:href=\"%s#%s\">%s</link>"
  730. id-file path (org-export-docbook-format-desc desc)))))
  731. ((member type '("http" "https"))
  732. ;; Standard URL, just check if we need to inline an image
  733. (if (and (or (eq t org-export-docbook-inline-images)
  734. (and org-export-docbook-inline-images (not descp)))
  735. (org-file-image-p
  736. path org-export-docbook-inline-image-extensions))
  737. (setq rpl (org-export-docbook-format-image
  738. (concat type ":" path)))
  739. (setq link (concat type ":" path))
  740. (setq rpl (format "<link xlink:href=\"%s\">%s</link>"
  741. (org-export-html-format-href link)
  742. (org-export-docbook-format-desc desc)))
  743. ))
  744. ((member type '("ftp" "mailto" "news"))
  745. ;; Standard URL
  746. (setq link (concat type ":" path))
  747. (setq rpl (format "<link xlink:href=\"%s\">%s</link>"
  748. (org-export-html-format-href link)
  749. (org-export-docbook-format-desc desc))))
  750. ((string= type "coderef")
  751. (setq rpl (format (org-export-get-coderef-format path (and descp desc))
  752. (cdr (assoc path org-export-code-refs)))))
  753. ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
  754. ;; The link protocol has a function for format the link
  755. (setq rpl
  756. (save-match-data
  757. (funcall fnc (org-link-unescape path) desc1 'html))))
  758. ((string= type "file")
  759. ;; FILE link
  760. (let* ((filename path)
  761. (abs-p (file-name-absolute-p filename))
  762. thefile file-is-image-p search)
  763. (save-match-data
  764. (if (string-match "::\\(.*\\)" filename)
  765. (setq search (match-string 1 filename)
  766. filename (replace-match "" t nil filename)))
  767. (setq valid
  768. (if (functionp link-validate)
  769. (funcall link-validate filename current-dir)
  770. t))
  771. (setq file-is-image-p
  772. (org-file-image-p
  773. filename org-export-docbook-inline-image-extensions))
  774. (setq thefile (if abs-p (expand-file-name filename) filename))
  775. ;; Carry over the properties (expand-file-name will
  776. ;; discard the properties of filename)
  777. (add-text-properties 0 (1- (length thefile))
  778. (list 'org-caption caption
  779. 'org-attributes attr
  780. 'org-label label)
  781. thefile)
  782. (when (and org-export-docbook-link-org-files-as-docbook
  783. (string-match "\\.org$" thefile))
  784. (setq thefile (concat (substring thefile 0
  785. (match-beginning 0))
  786. org-export-docbook-extension))
  787. (if (and search
  788. ;; make sure this is can be used as target search
  789. (not (string-match "^[0-9]*$" search))
  790. (not (string-match "^\\*" search))
  791. (not (string-match "^/.*/$" search)))
  792. (setq thefile (concat thefile "#"
  793. (org-solidify-link-text
  794. (org-link-unescape search)))))
  795. (when (string-match "^file:" desc)
  796. (setq desc (replace-match "" t t desc))
  797. (if (string-match "\\.org$" desc)
  798. (setq desc (replace-match "" t t desc))))))
  799. (setq rpl (if (and file-is-image-p
  800. (or (eq t org-export-docbook-inline-images)
  801. (and org-export-docbook-inline-images
  802. (not descp))))
  803. (progn
  804. (message "image %s %s" thefile org-docbook-para-open)
  805. (org-export-docbook-format-image thefile))
  806. (format "<link xlink:href=\"%s\">%s</link>"
  807. thefile (org-export-docbook-format-desc desc))))
  808. (if (not valid) (setq rpl desc))))
  809. (t
  810. ;; Just publish the path, as default
  811. (setq rpl (concat "&lt;" type ":"
  812. (save-match-data (org-link-unescape path))
  813. "&gt;"))))
  814. (setq line (replace-match rpl t t line)
  815. start (+ start (length rpl))))
  816. ;; TODO items: can we do something better?!
  817. (if (and (string-match org-todo-line-regexp line)
  818. (match-beginning 2))
  819. (setq line
  820. (concat (substring line 0 (match-beginning 2))
  821. "[" (match-string 2 line) "]"
  822. (substring line (match-end 2)))))
  823. ;; Does this contain a reference to a footnote?
  824. (when org-export-with-footnotes
  825. (setq start 0)
  826. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  827. (if (get-text-property (match-beginning 2) 'org-protected line)
  828. (setq start (match-end 2))
  829. (let* ((num (match-string 2 line))
  830. (footnote-def (assoc num footnote-list)))
  831. (if (assoc num footref-seen)
  832. (setq line (replace-match
  833. (format "%s<footnoteref linkend=\"%s%s\"/>"
  834. (match-string 1 line)
  835. org-export-docbook-footnote-id-prefix num)
  836. t t line))
  837. (setq line (replace-match
  838. (format "%s<footnote xml:id=\"%s%s\"><para>%s</para></footnote>"
  839. (match-string 1 line)
  840. org-export-docbook-footnote-id-prefix
  841. num
  842. (if footnote-def
  843. (save-match-data
  844. (org-docbook-expand (cdr footnote-def)))
  845. (format "FOOTNOTE DEFINITION NOT FOUND: %s" num)))
  846. t t line))
  847. (push (cons num 1) footref-seen))))))
  848. (cond
  849. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  850. ;; This is a headline
  851. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
  852. level-offset))
  853. txt (match-string 2 line))
  854. (if (string-match quote-re0 txt)
  855. (setq txt (replace-match "" t t txt)))
  856. (when in-local-list
  857. ;; Close any local lists before inserting a new header line
  858. (while local-list-type
  859. (let ((listtype (car local-list-type)))
  860. (org-export-docbook-close-li listtype)
  861. (insert (cond
  862. ((equal listtype "o") "</orderedlist>\n")
  863. ((equal listtype "u") "</itemizedlist>\n")
  864. ((equal listtype "d") "</variablelist>\n"))))
  865. (pop local-list-type))
  866. (setq local-list-indent nil
  867. in-local-list nil))
  868. (org-export-docbook-level-start level txt)
  869. ;; QUOTES
  870. (when (string-match quote-re line)
  871. (org-export-docbook-close-para-maybe)
  872. (insert "<programlisting><![CDATA[")
  873. (setq inquote t)))
  874. ;; Tables: since version 4.3 of DocBook DTD, HTML tables are
  875. ;; supported. We can use existing HTML table exporter code
  876. ;; here.
  877. ((and org-export-with-tables
  878. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  879. (if (not table-open)
  880. ;; New table starts
  881. (setq table-open t
  882. table-buffer nil
  883. table-orig-buffer nil))
  884. ;; Accumulate lines
  885. (setq table-buffer (cons line table-buffer)
  886. table-orig-buffer (cons origline table-orig-buffer))
  887. (when (or (not lines)
  888. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  889. (car lines))))
  890. (setq table-open nil
  891. table-buffer (nreverse table-buffer)
  892. table-orig-buffer (nreverse table-orig-buffer))
  893. (org-export-docbook-close-para-maybe)
  894. (insert (org-export-docbook-finalize-table
  895. (org-format-table-html table-buffer table-orig-buffer)))))
  896. (t
  897. ;; Normal lines
  898. (when (string-match
  899. (cond
  900. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  901. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  902. ((= llt ?\)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  903. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  904. line)
  905. (setq ind (or (get-text-property 0 'original-indentation line)
  906. (org-get-string-indentation line))
  907. item-type (if (match-beginning 4) "o" "u")
  908. starter (if (match-beginning 2)
  909. (substring (match-string 2 line) 0 -1))
  910. line (substring line (match-beginning 5))
  911. item-tag nil)
  912. (if (and starter (string-match "\\(.*?\\) ::[ \t]*" line))
  913. (setq item-type "d"
  914. item-tag (match-string 1 line)
  915. line (substring line (match-end 0))))
  916. (when (and (not (equal item-type "d"))
  917. (not (string-match "[^ \t]" line)))
  918. ;; Empty line. Pretend indentation is large.
  919. (setq ind (if org-empty-line-terminates-plain-lists
  920. 0
  921. (1+ (or (car local-list-indent) 1)))))
  922. (setq didclose nil)
  923. (while (and in-local-list
  924. (or (and (= ind (car local-list-indent))
  925. (not starter))
  926. (< ind (car local-list-indent))))
  927. (setq didclose t)
  928. (let ((listtype (car local-list-type)))
  929. (org-export-docbook-close-li listtype)
  930. (insert (cond
  931. ((equal listtype "o") "</orderedlist>\n")
  932. ((equal listtype "u") "</itemizedlist>\n")
  933. ((equal listtype "d") "</variablelist>\n"))))
  934. (pop local-list-type) (pop local-list-indent)
  935. (setq in-local-list local-list-indent))
  936. (cond
  937. ((and starter
  938. (or (not in-local-list)
  939. (> ind (car local-list-indent))))
  940. ;; Start new (level of) list
  941. (org-export-docbook-close-para-maybe)
  942. (insert (cond
  943. ((equal item-type "u") "<itemizedlist>\n<listitem>\n")
  944. ((equal item-type "o") "<orderedlist>\n<listitem>\n")
  945. ((equal item-type "d")
  946. (format "<variablelist>\n<varlistentry><term>%s</term><listitem>\n" item-tag))))
  947. ;; For DocBook, we need to open a para right after tag
  948. ;; <listitem>.
  949. (org-export-docbook-open-para)
  950. (push item-type local-list-type)
  951. (push ind local-list-indent)
  952. (setq in-local-list t))
  953. (starter
  954. ;; Continue current list
  955. (let ((listtype (car local-list-type)))
  956. (org-export-docbook-close-li listtype)
  957. (insert (cond
  958. ((equal listtype "o") "<listitem>")
  959. ((equal listtype "u") "<listitem>")
  960. ((equal listtype "d") (format
  961. "<varlistentry><term>%s</term><listitem>"
  962. (or item-tag
  963. "???"))))))
  964. ;; For DocBook, we need to open a para right after tag
  965. ;; <listitem>.
  966. (org-export-docbook-open-para))
  967. (didclose
  968. ;; We did close a list, normal text follows: need <para>
  969. (org-export-docbook-open-para)))
  970. ;; Checkboxes.
  971. (if (string-match "^[ \t]*\\(\\[[X -]\\]\\)" line)
  972. (setq line
  973. (replace-match (concat checkbox-start
  974. (match-string 1 line)
  975. checkbox-end)
  976. t t line))))
  977. ;; Empty lines start a new paragraph. If hand-formatted lists
  978. ;; are not fully interpreted, lines starting with "-", "+", "*"
  979. ;; also start a new paragraph.
  980. (if (and (string-match "^ [-+*]-\\|^[ \t]*$" line)
  981. (not inverse))
  982. (org-export-docbook-open-para))
  983. ;; Is this the start of a footnote?
  984. (when org-export-with-footnotes
  985. (when (and (boundp 'footnote-section-tag-regexp)
  986. (string-match (concat "^" footnote-section-tag-regexp)
  987. line))
  988. ;; ignore this line
  989. (throw 'nextline nil))
  990. ;; These footnote lines have been read and saved before,
  991. ;; ignore them at this time.
  992. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  993. (org-export-docbook-close-para-maybe)
  994. (throw 'nextline nil)))
  995. ;; FIXME: It might be a good idea to add an option to
  996. ;; support line break processing instruction <?linebreak?>.
  997. ;; Org-mode supports line break "\\" in HTML exporter, and
  998. ;; some DocBook users may also want to force line breaks
  999. ;; even though DocBook only supports that in
  1000. ;; <literallayout>.
  1001. (insert line "\n")))))
  1002. ;; Properly close all local lists and other lists
  1003. (when inquote
  1004. (insert "]]></programlisting>\n")
  1005. (org-export-docbook-open-para))
  1006. (when in-local-list
  1007. ;; Close any local lists before inserting a new header line
  1008. (while local-list-type
  1009. (let ((listtype (car local-list-type)))
  1010. (org-export-docbook-close-li listtype)
  1011. (insert (cond
  1012. ((equal listtype "o") "</orderedlist>\n")
  1013. ((equal listtype "u") "</itemizedlist>\n")
  1014. ((equal listtype "d") "</variablelist>\n"))))
  1015. (pop local-list-type))
  1016. (setq local-list-indent nil
  1017. in-local-list nil))
  1018. ;; Close all open sections.
  1019. (org-export-docbook-level-start 1 nil)
  1020. (unless (plist-get opt-plist :buffer-will-be-killed)
  1021. (normal-mode)
  1022. (if (eq major-mode (default-value 'major-mode))
  1023. (nxml-mode)))
  1024. ;; Remove empty paragraphs and lists. Replace them with a
  1025. ;; newline.
  1026. (goto-char (point-min))
  1027. (while (re-search-forward
  1028. "[ \r\n\t]*\\(<para>\\)[ \r\n\t]*</para>[ \r\n\t]*" nil t)
  1029. (when (not (get-text-property (match-beginning 1) 'org-protected))
  1030. (replace-match "\n")
  1031. ;; Avoid empty <listitem></listitem> caused by inline tasks.
  1032. ;; We should add an empty para to make everything valid.
  1033. (when (and (looking-at "</listitem>")
  1034. (save-excursion
  1035. (backward-char (length "<listitem>\n"))
  1036. (looking-at "<listitem>")))
  1037. (insert "<para></para>"))
  1038. (backward-char 1)))
  1039. ;; Fill empty sections with <para></para>. This is to make sure
  1040. ;; that the DocBook document generated is valid and well-formed.
  1041. (goto-char (point-min))
  1042. (while (re-search-forward
  1043. "</title>\\([ \r\n\t]*\\)</section>" nil t)
  1044. (when (not (get-text-property (match-beginning 0) 'org-protected))
  1045. (replace-match "\n<para></para>\n" nil nil nil 1)))
  1046. ;; Insert the last closing tag.
  1047. (goto-char (point-max))
  1048. (unless body-only
  1049. (insert "</article>"))
  1050. (run-hooks 'org-export-docbook-final-hook)
  1051. (or to-buffer (save-buffer))
  1052. (goto-char (point-min))
  1053. (or (org-export-push-to-kill-ring "DocBook")
  1054. (message "Exporting... done"))
  1055. (if (eq to-buffer 'string)
  1056. (prog1 (buffer-substring (point-min) (point-max))
  1057. (kill-buffer (current-buffer)))
  1058. (current-buffer)))))
  1059. (defun org-export-docbook-open-para ()
  1060. "Insert <para>, but first close previous paragraph if any."
  1061. (org-export-docbook-close-para-maybe)
  1062. (insert "\n<para>")
  1063. (setq org-docbook-para-open t))
  1064. (defun org-export-docbook-close-para-maybe ()
  1065. "Close DocBook paragraph if there is one open."
  1066. (when org-docbook-para-open
  1067. (insert "</para>\n")
  1068. (setq org-docbook-para-open nil)))
  1069. (defun org-export-docbook-close-li (&optional type)
  1070. "Close list if necessary."
  1071. (org-export-docbook-close-para-maybe)
  1072. (if (equal type "d")
  1073. (insert "</listitem></varlistentry>\n")
  1074. (insert "</listitem>\n")))
  1075. (defvar in-local-list)
  1076. (defvar local-list-indent)
  1077. (defvar local-list-type)
  1078. (defun org-export-docbook-close-lists-maybe (line)
  1079. (let ((ind (or (get-text-property 0 'original-indentation line)))
  1080. ; (and (string-match "\\S-" line)
  1081. ; (org-get-indentation line))))
  1082. didclose)
  1083. (when ind
  1084. (while (and in-local-list
  1085. (<= ind (car local-list-indent)))
  1086. (setq didclose t)
  1087. (let ((listtype (car local-list-type)))
  1088. (org-export-docbook-close-li listtype)
  1089. (insert (cond
  1090. ((equal listtype "o") "</orderedlist>\n")
  1091. ((equal listtype "u") "</itemizedlist>\n")
  1092. ((equal listtype "d") "</variablelist>\n"))))
  1093. (pop local-list-type) (pop local-list-indent)
  1094. (setq in-local-list local-list-indent))
  1095. (and didclose (org-export-docbook-open-para)))))
  1096. (defun org-export-docbook-level-start (level title)
  1097. "Insert a new level in DocBook export.
  1098. When TITLE is nil, just close all open levels."
  1099. (org-export-docbook-close-para-maybe)
  1100. (let* ((target (and title (org-get-text-property-any 0 'target title)))
  1101. (l org-level-max)
  1102. section-number)
  1103. (while (>= l level)
  1104. (if (aref org-levels-open (1- l))
  1105. (progn
  1106. (insert "</section>\n")
  1107. (aset org-levels-open (1- l) nil)))
  1108. (setq l (1- l)))
  1109. (when title
  1110. ;; If title is nil, this means this function is called to close
  1111. ;; all levels, so the rest is done only if title is given.
  1112. ;;
  1113. ;; Format tags: put them into a superscript like format.
  1114. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  1115. (setq title
  1116. (replace-match
  1117. (if org-export-with-tags
  1118. (save-match-data
  1119. (concat
  1120. "<superscript>"
  1121. (match-string 1 title)
  1122. "</superscript>"))
  1123. "")
  1124. t t title)))
  1125. (aset org-levels-open (1- level) t)
  1126. (setq section-number (org-section-number level))
  1127. (insert (format "\n<section xml:id=\"%s%s\">\n<title>%s</title>"
  1128. org-export-docbook-section-id-prefix
  1129. section-number title))
  1130. (org-export-docbook-open-para))))
  1131. (defun org-docbook-expand (string)
  1132. "Prepare STRING for DocBook export.
  1133. Applies all active conversions. If there are links in the
  1134. string, don't modify these."
  1135. (let* ((re (concat org-bracket-link-regexp "\\|"
  1136. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  1137. m s l res)
  1138. (while (setq m (string-match re string))
  1139. (setq s (substring string 0 m)
  1140. l (match-string 0 string)
  1141. string (substring string (match-end 0)))
  1142. (push (org-docbook-do-expand s) res)
  1143. (push l res))
  1144. (push (org-docbook-do-expand string) res)
  1145. (apply 'concat (nreverse res))))
  1146. (defun org-docbook-do-expand (s)
  1147. "Apply all active conversions to translate special ASCII to DocBook."
  1148. (setq s (org-html-protect s))
  1149. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  1150. (setq s (replace-match "<\\1>" t nil s)))
  1151. (if org-export-with-emphasize
  1152. (setq s (org-export-docbook-convert-emphasize s)))
  1153. (if org-export-with-special-strings
  1154. (setq s (org-export-docbook-convert-special-strings s)))
  1155. (if org-export-with-sub-superscripts
  1156. (setq s (org-export-docbook-convert-sub-super s)))
  1157. (if org-export-with-TeX-macros
  1158. (let ((start 0) wd rep)
  1159. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)\\({}\\)?"
  1160. s start))
  1161. (if (get-text-property (match-beginning 0) 'org-protected s)
  1162. (setq start (match-end 0))
  1163. (setq wd (match-string 1 s))
  1164. (if (setq rep (org-entity-get-representation wd 'html))
  1165. (setq s (replace-match rep t t s))
  1166. (setq start (+ start (length wd))))))))
  1167. s)
  1168. (defun org-export-docbook-format-desc (desc)
  1169. "Make sure DESC is valid as a description in a link."
  1170. (save-match-data
  1171. (org-docbook-do-expand desc)))
  1172. (defun org-export-docbook-convert-emphasize (string)
  1173. "Apply emphasis for DocBook exporting."
  1174. (let ((s 0) rpl)
  1175. (while (string-match org-emph-re string s)
  1176. (if (not (equal
  1177. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  1178. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  1179. (setq s (match-beginning 0)
  1180. rpl
  1181. (concat
  1182. (match-string 1 string)
  1183. (nth 1 (assoc (match-string 3 string)
  1184. org-export-docbook-emphasis-alist))
  1185. (match-string 4 string)
  1186. (nth 2 (assoc (match-string 3 string)
  1187. org-export-docbook-emphasis-alist))
  1188. (match-string 5 string))
  1189. string (replace-match rpl t t string)
  1190. s (+ s (- (length rpl) 2)))
  1191. (setq s (1+ s))))
  1192. string))
  1193. (defun org-docbook-protect (string)
  1194. (org-html-protect string))
  1195. ;; For now, simply return string as it is.
  1196. (defun org-export-docbook-convert-special-strings (string)
  1197. "Convert special characters in STRING to DocBook."
  1198. string)
  1199. (defun org-export-docbook-get-footnotes (lines)
  1200. "Given a list of LINES, return a list of alist footnotes."
  1201. (let ((list nil) line)
  1202. (while (setq line (pop lines))
  1203. (if (string-match "^[ \t]*\\[\\([0-9]+\\)\\] \\(.+\\)" line)
  1204. (push (cons (match-string 1 line) (match-string 2 line))
  1205. list)))
  1206. list))
  1207. (defun org-export-docbook-format-image (src)
  1208. "Create image element in DocBook."
  1209. (save-match-data
  1210. (let* ((caption (org-find-text-property-in-string 'org-caption src))
  1211. (attr (or (org-find-text-property-in-string 'org-attributes src)
  1212. ""))
  1213. (label (org-find-text-property-in-string 'org-label src))
  1214. (default-attr org-export-docbook-default-image-attributes)
  1215. tmp)
  1216. (setq caption (and caption (org-html-do-expand caption)))
  1217. (while (setq tmp (pop default-attr))
  1218. (if (not (string-match (concat (car tmp) "=") attr))
  1219. (setq attr (concat attr " " (car tmp) "=" (cdr tmp)))))
  1220. (format "<mediaobject%s>
  1221. <imageobject>\n<imagedata fileref=\"%s\" %s/>\n</imageobject>
  1222. %s</mediaobject>"
  1223. (if label (concat " xml:id=\"" label "\"") "")
  1224. src attr
  1225. (if caption
  1226. (concat "<caption>\n<para>"
  1227. caption
  1228. "</para>\n</caption>\n")
  1229. "")
  1230. ))))
  1231. (defun org-export-docbook-preprocess (parameters)
  1232. "Extra preprocessing work for DocBook export."
  1233. ;; Merge lines starting with "\par" to one line. Such lines are
  1234. ;; regarded as the continuation of a long footnote.
  1235. (goto-char (point-min))
  1236. (while (re-search-forward "\n\\(\\\\par\\>\\)" nil t)
  1237. (if (not (get-text-property (match-beginning 1) 'org-protected))
  1238. (replace-match ""))))
  1239. (defun org-export-docbook-finalize-table (table)
  1240. "Clean up TABLE and turn it into DocBook format.
  1241. This function adds a label to the table if it is available, and
  1242. also changes TABLE to informaltable if caption does not exist.
  1243. TABLE is a string containing the HTML code generated by
  1244. `org-format-table-html' for a table in Org-mode buffer."
  1245. (let (table-with-label)
  1246. ;; Get the label if it exists, and move it into the <table> element.
  1247. (setq table-with-label
  1248. (if (string-match
  1249. "^<table \\(\\(.\\|\n\\)+\\)<a name=\"\\(.+\\)\" id=\".+\"></a>\n\\(\\(.\\|\n\\)+\\)</table>"
  1250. table)
  1251. (replace-match (concat "<table xml:id=\"" (match-string 3 table) "\" "
  1252. (match-string 1 table)
  1253. (match-string 4 table)
  1254. "</table>")
  1255. nil nil table)
  1256. table))
  1257. ;; Change <table> into <informaltable> if caption does not exist.
  1258. (if (string-match
  1259. "^<table \\(\\(.\\|\n\\)+\\)<caption></caption>\n\\(\\(.\\|\n\\)+\\)</table>"
  1260. table-with-label)
  1261. (replace-match (concat "<informaltable "
  1262. (match-string 1 table-with-label)
  1263. (match-string 3 table-with-label)
  1264. "</informaltable>")
  1265. nil nil table-with-label)
  1266. table-with-label)))
  1267. ;; Note: This function is very similar to
  1268. ;; org-export-html-convert-sub-super. They can be merged in the future.
  1269. (defun org-export-docbook-convert-sub-super (string)
  1270. "Convert sub- and superscripts in STRING for DocBook."
  1271. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  1272. (while (string-match org-match-substring-regexp string s)
  1273. (cond
  1274. ((and requireb (match-end 8)) (setq s (match-end 2)))
  1275. ((get-text-property (match-beginning 2) 'org-protected string)
  1276. (setq s (match-end 2)))
  1277. (t
  1278. (setq s (match-end 1)
  1279. key (if (string= (match-string 2 string) "_")
  1280. "subscript"
  1281. "superscript")
  1282. c (or (match-string 8 string)
  1283. (match-string 6 string)
  1284. (match-string 5 string))
  1285. string (replace-match
  1286. (concat (match-string 1 string)
  1287. "<" key ">" c "</" key ">")
  1288. t t string)))))
  1289. (while (string-match "\\\\\\([_^]\\)" string)
  1290. (setq string (replace-match (match-string 1 string) t t string)))
  1291. string))
  1292. (defun org-export-docbook-protect-tags (string)
  1293. "Change ``<...>'' in string STRING into ``@<...>''.
  1294. This is normally needed when STRING contains DocBook elements
  1295. that need to be preserved in later phase of DocBook exporting."
  1296. (let ((start 0))
  1297. (while (string-match "<\\([^>]*\\)>" string start)
  1298. (setq string (replace-match
  1299. "@<\\1>" t nil string)
  1300. start (match-end 0)))
  1301. string))
  1302. (defun org-export-docbook-handle-time-stamps (line)
  1303. "Format time stamps in string LINE."
  1304. (let (replaced
  1305. (kw-markup (org-export-docbook-protect-tags
  1306. org-export-docbook-keywords-markup))
  1307. (ts-markup (org-export-docbook-protect-tags
  1308. org-export-docbook-timestamp-markup)))
  1309. (while (string-match org-maybe-keyword-time-regexp line)
  1310. (setq replaced
  1311. (concat replaced
  1312. (substring line 0 (match-beginning 0))
  1313. (if (match-end 1)
  1314. (format kw-markup
  1315. (match-string 1 line)))
  1316. " "
  1317. (format ts-markup
  1318. (substring (org-translate-time
  1319. (match-string 3 line)) 1 -1)))
  1320. line (substring line (match-end 0))))
  1321. (concat replaced line)))
  1322. (provide 'org-docbook)
  1323. ;; arch-tag: a24a127c-d365-4c2a-9e9b-f7dcb0ebfdc3
  1324. ;;; org-docbook.el ends here