org-docbook.el 47 KB

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