org-docbook.el 50 KB

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