org-docbook.el 51 KB

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