org-docbook.el 51 KB

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