org-docbook.el 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. ;;; org-docbook.el --- DocBook exporter for org-mode
  2. ;;
  3. ;; Copyright (C) 2007-2013 Free Software Foundation, Inc.
  4. ;;
  5. ;; Emacs Lisp Archive Entry
  6. ;; Filename: org-docbook.el
  7. ;; Author: Baoqiu Cui <cbaoqiu AT yahoo DOT com>
  8. ;; Maintainer: Baoqiu Cui <cbaoqiu AT yahoo DOT com>
  9. ;; Keywords: org, wp, docbook
  10. ;; Description: Converts an org-mode buffer into DocBook
  11. ;; URL:
  12. ;; This file is part of GNU Emacs.
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;; Commentary:
  24. ;;
  25. ;; This library implements a DocBook exporter for org-mode. The basic
  26. ;; idea and design is very similar to what `org-export-as-html' has.
  27. ;; Code prototype was also started with `org-export-as-html'.
  28. ;;
  29. ;; Put this file into your load-path and the following line into your
  30. ;; ~/.emacs:
  31. ;;
  32. ;; (require 'org-docbook)
  33. ;;
  34. ;; The interactive functions are similar to those of the HTML and LaTeX
  35. ;; exporters:
  36. ;;
  37. ;; M-x `org-export-as-docbook'
  38. ;; M-x `org-export-as-docbook-pdf'
  39. ;; M-x `org-export-as-docbook-pdf-and-open'
  40. ;; M-x `org-export-as-docbook-batch'
  41. ;; M-x `org-export-as-docbook-to-buffer'
  42. ;; M-x `org-export-region-as-docbook'
  43. ;; M-x `org-replace-region-by-docbook'
  44. ;;
  45. ;; Note that, in order to generate PDF files using the DocBook XML files
  46. ;; created by DocBook exporter, the following two variables have to be
  47. ;; set based on what DocBook tools you use for XSLT processor and XSL-FO
  48. ;; processor:
  49. ;;
  50. ;; org-export-docbook-xslt-proc-command
  51. ;; org-export-docbook-xsl-fo-proc-command
  52. ;;
  53. ;; Check the document of these two variables to see examples of how they
  54. ;; can be set.
  55. ;;
  56. ;; If the Org file to be exported contains special characters written in
  57. ;; TeX-like syntax, like \alpha and \beta, you need to include the right
  58. ;; entity file(s) in the DOCTYPE declaration for the DocBook XML file.
  59. ;; This is required to make the DocBook XML file valid. The DOCTYPE
  60. ;; declaration string can be set using the following variable:
  61. ;;
  62. ;; org-export-docbook-doctype
  63. ;;
  64. ;;; Code:
  65. (eval-when-compile
  66. (require 'cl))
  67. (require 'footnote)
  68. (require 'org)
  69. (require 'org-exp)
  70. (require 'org-html)
  71. (require 'format-spec)
  72. ;;; Variables:
  73. (defvar org-docbook-para-open nil)
  74. (defvar org-export-docbook-inline-images t)
  75. (defvar org-export-docbook-link-org-files-as-docbook nil)
  76. (declare-function org-id-find-id-file "org-id" (id))
  77. ;;; User variables:
  78. (defgroup org-export-docbook nil
  79. "Options for exporting Org-mode files to DocBook."
  80. :tag "Org Export DocBook"
  81. :group 'org-export)
  82. (defcustom org-export-docbook-extension ".xml"
  83. "Extension of DocBook XML files."
  84. :group 'org-export-docbook
  85. :type 'string)
  86. (defcustom org-export-docbook-header "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  87. "Header of DocBook XML files."
  88. :group 'org-export-docbook
  89. :type 'string)
  90. (defcustom org-export-docbook-doctype nil
  91. "DOCTYPE declaration string for DocBook XML files.
  92. This can be used to include entities that are needed to handle
  93. special characters in Org files.
  94. For example, if the Org file to be exported contains XHTML
  95. entities, you can set this variable to:
  96. \"<!DOCTYPE article [
  97. <!ENTITY % xhtml1-symbol PUBLIC
  98. \"-//W3C//ENTITIES Symbol for HTML//EN//XML\"
  99. \"http://www.w3.org/2003/entities/2007/xhtml1-symbol.ent\"
  100. >
  101. %xhtml1-symbol;
  102. ]>
  103. \"
  104. If you want to process DocBook documents without an Internet
  105. connection, it is suggested that you download the required entity
  106. file(s) and use system identifier(s) (external files) in the
  107. DOCTYPE declaration."
  108. :group 'org-export-docbook
  109. :type 'string)
  110. (defcustom org-export-docbook-article-header "<article xmlns=\"http://docbook.org/ns/docbook\"
  111. xmlns:xlink=\"http://www.w3.org/1999/xlink\" version=\"5.0\" xml:lang=\"en\">"
  112. "Article header of DocBook XML files."
  113. :group 'org-export-docbook
  114. :type 'string)
  115. (defcustom org-export-docbook-section-id-prefix "sec-"
  116. "Prefix of section IDs used during exporting.
  117. This can be set before exporting to avoid same set of section IDs
  118. being used again and again, which can be a problem when multiple
  119. people work on the same document."
  120. :group 'org-export-docbook
  121. :type 'string)
  122. (defcustom org-export-docbook-footnote-id-prefix "fn-"
  123. "The prefix of footnote IDs used during exporting.
  124. Like `org-export-docbook-section-id-prefix', this variable can help
  125. avoid same set of footnote IDs being used multiple times."
  126. :group 'org-export-docbook
  127. :type 'string)
  128. (defcustom org-export-docbook-footnote-separator "<superscript>, </superscript>"
  129. "Text used to separate footnotes."
  130. :group 'org-export-docbook
  131. :version "24.1"
  132. :type 'string)
  133. (defcustom org-export-docbook-emphasis-alist
  134. `(("*" "<emphasis role=\"bold\">" "</emphasis>")
  135. ("/" "<emphasis>" "</emphasis>")
  136. ("_" "<emphasis role=\"underline\">" "</emphasis>")
  137. ("=" "<code>" "</code>")
  138. ("~" "<literal>" "</literal>")
  139. ("+" "<emphasis role=\"strikethrough\">" "</emphasis>"))
  140. "A list of DocBook expressions to convert emphasis fontifiers.
  141. Each element of the list is a list of three elements.
  142. The first element is the character used as a marker for fontification.
  143. The second element is a format string to wrap fontified text with.
  144. The third element decides whether to protect converted text from other
  145. conversions."
  146. :group 'org-export-docbook
  147. :type 'alist)
  148. (defcustom org-export-docbook-default-image-attributes
  149. `(("align" . "\"center\"")
  150. ("valign". "\"middle\""))
  151. "Alist of default DocBook image attributes.
  152. These attributes will be inserted into element <imagedata> by
  153. default, but users can override them using `#+ATTR_DocBook:'."
  154. :group 'org-export-docbook
  155. :type 'alist)
  156. (defcustom org-export-docbook-inline-image-extensions
  157. '("jpeg" "jpg" "png" "gif" "svg")
  158. "Extensions of image files that can be inlined into DocBook."
  159. :group 'org-export-docbook
  160. :type '(repeat (string :tag "Extension")))
  161. (defcustom org-export-docbook-coding-system nil
  162. "Coding system for DocBook XML files."
  163. :group 'org-export-docbook
  164. :type 'coding-system)
  165. (defcustom org-export-docbook-xslt-stylesheet nil
  166. "File name of the XSLT stylesheet used by DocBook exporter.
  167. This XSLT stylesheet is used by
  168. `org-export-docbook-xslt-proc-command' to generate the Formatting
  169. Object (FO) files. You can use either `fo/docbook.xsl' that
  170. comes with DocBook, or any customization layer you may have."
  171. :group 'org-export-docbook
  172. :version "24.1"
  173. :type 'string)
  174. (defcustom org-export-docbook-xslt-proc-command nil
  175. "Format of XSLT processor command used by DocBook exporter.
  176. This command is used to process a DocBook XML file to generate
  177. the Formatting Object (FO) file.
  178. The value of this variable should be a format control string that
  179. includes three arguments: `%i', `%o', and `%s'. During exporting
  180. time, `%i' is replaced by the input DocBook XML file name, `%o'
  181. is replaced by the output FO file name, and `%s' is replaced by
  182. `org-export-docbook-xslt-stylesheet' (or the #+XSLT option if it
  183. is specified in the Org file).
  184. For example, if you use Saxon as the XSLT processor, you may want
  185. to set the variable to
  186. \"java com.icl.saxon.StyleSheet -o %o %i %s\"
  187. If you use Xalan, you can set it to
  188. \"java org.apache.xalan.xslt.Process -out %o -in %i -xsl %s\"
  189. For xsltproc, the following string should work:
  190. \"xsltproc --output %o %s %i\"
  191. You can include additional stylesheet parameters in this command.
  192. Just make sure that they meet the syntax requirement of each
  193. processor."
  194. :group 'org-export-docbook
  195. :type 'string)
  196. (defcustom org-export-docbook-xsl-fo-proc-command nil
  197. "Format of XSL-FO processor command used by DocBook exporter.
  198. This command is used to process a Formatting Object (FO) file to
  199. generate the PDF file.
  200. The value of this variable should be a format control string that
  201. includes two arguments: `%i' and `%o'. During exporting time,
  202. `%i' is replaced by the input FO file name, and `%o' is replaced
  203. by the output PDF file name.
  204. For example, if you use FOP as the XSL-FO processor, you can set
  205. the variable to
  206. \"fop %i %o\""
  207. :group 'org-export-docbook
  208. :type 'string)
  209. (defcustom org-export-docbook-keywords-markup "<literal>%s</literal>"
  210. "A printf format string to be applied to keywords by DocBook exporter."
  211. :group 'org-export-docbook
  212. :type 'string)
  213. (defcustom org-export-docbook-timestamp-markup "<emphasis>%s</emphasis>"
  214. "A printf format string to be applied to time stamps by DocBook exporter."
  215. :group 'org-export-docbook
  216. :type 'string)
  217. ;;; Hooks
  218. (defvar org-export-docbook-final-hook nil
  219. "Hook run at the end of DocBook export, in the new buffer.")
  220. ;;; Autoload functions:
  221. (defun org-export-as-docbook-batch ()
  222. "Call `org-export-as-docbook' in batch style.
  223. This function can be used in batch processing.
  224. For example:
  225. $ emacs --batch
  226. --load=$HOME/lib/emacs/org.el
  227. --visit=MyOrgFile.org --funcall org-export-as-docbook-batch"
  228. (org-export-as-docbook))
  229. (defun org-export-as-docbook-to-buffer ()
  230. "Call `org-export-as-docbook' with output to a temporary buffer.
  231. No file is created."
  232. (interactive)
  233. (org-export-as-docbook nil "*Org DocBook Export*")
  234. (when org-export-show-temporary-export-buffer
  235. (switch-to-buffer-other-window "*Org DocBook Export*")))
  236. (defun org-replace-region-by-docbook (beg end)
  237. "Replace the region from BEG to END with its DocBook export.
  238. It assumes the region has `org-mode' syntax, and then convert it to
  239. DocBook. This can be used in any buffer. For example, you could
  240. write an itemized list in `org-mode' syntax in an DocBook buffer and
  241. then use this command to convert it."
  242. (interactive "r")
  243. (let (reg docbook buf)
  244. (save-window-excursion
  245. (if (derived-mode-p 'org-mode)
  246. (setq docbook (org-export-region-as-docbook
  247. beg end t 'string))
  248. (setq reg (buffer-substring beg end)
  249. buf (get-buffer-create "*Org tmp*"))
  250. (with-current-buffer buf
  251. (erase-buffer)
  252. (insert reg)
  253. (org-mode)
  254. (setq docbook (org-export-region-as-docbook
  255. (point-min) (point-max) t 'string)))
  256. (kill-buffer buf)))
  257. (delete-region beg end)
  258. (insert docbook)))
  259. (defun org-export-region-as-docbook (beg end &optional body-only buffer)
  260. "Convert region from BEG to END in `org-mode' buffer to DocBook.
  261. If prefix arg BODY-ONLY is set, omit file header and footer and
  262. only produce the region of converted text, useful for
  263. cut-and-paste operations. If BUFFER is a buffer or a string,
  264. use/create that buffer as a target of the converted DocBook. If
  265. BUFFER is the symbol `string', return the produced DocBook as a
  266. string and leave not buffer behind. For example, a Lisp program
  267. could call this function in the following way:
  268. (setq docbook (org-export-region-as-docbook beg end t 'string))
  269. When called interactively, the output buffer is selected, and shown
  270. in a window. A non-interactive call will only return the buffer."
  271. (interactive "r\nP")
  272. (when (org-called-interactively-p 'any)
  273. (setq buffer "*Org DocBook Export*"))
  274. (let ((transient-mark-mode t)
  275. (zmacs-regions t)
  276. rtn)
  277. (goto-char end)
  278. (set-mark (point)) ;; To activate the region
  279. (goto-char beg)
  280. (setq rtn (org-export-as-docbook nil buffer body-only))
  281. (if (fboundp 'deactivate-mark) (deactivate-mark))
  282. (if (and (org-called-interactively-p 'any) (bufferp rtn))
  283. (switch-to-buffer-other-window rtn)
  284. rtn)))
  285. (defun org-export-as-docbook-pdf (&optional ext-plist to-buffer body-only pub-dir)
  286. "Export as DocBook XML file, and generate PDF file."
  287. (interactive "P")
  288. (if (or (not org-export-docbook-xslt-proc-command)
  289. (not (string-match "%[ios].+%[ios].+%[ios]" org-export-docbook-xslt-proc-command)))
  290. (error "XSLT processor command is not set correctly"))
  291. (if (or (not org-export-docbook-xsl-fo-proc-command)
  292. (not (string-match "%[io].+%[io]" org-export-docbook-xsl-fo-proc-command)))
  293. (error "XSL-FO processor command is not set correctly"))
  294. (message "Exporting to PDF...")
  295. (let* ((wconfig (current-window-configuration))
  296. (opt-plist
  297. (org-export-process-option-filters
  298. (org-combine-plists (org-default-export-plist)
  299. ext-plist
  300. (org-infile-export-plist))))
  301. (docbook-buf (org-export-as-docbook ext-plist to-buffer body-only pub-dir))
  302. (filename (buffer-file-name docbook-buf))
  303. (base (file-name-sans-extension filename))
  304. (fofile (concat base ".fo"))
  305. (pdffile (concat base ".pdf")))
  306. (and (file-exists-p pdffile) (delete-file pdffile))
  307. (message "Processing DocBook XML file...")
  308. (shell-command (format-spec org-export-docbook-xslt-proc-command
  309. (format-spec-make
  310. ?i (shell-quote-argument filename)
  311. ?o (shell-quote-argument fofile)
  312. ?s (shell-quote-argument
  313. (or (plist-get opt-plist :xslt)
  314. org-export-docbook-xslt-stylesheet)))))
  315. (shell-command (format-spec org-export-docbook-xsl-fo-proc-command
  316. (format-spec-make
  317. ?i (shell-quote-argument fofile)
  318. ?o (shell-quote-argument pdffile))))
  319. (message "Processing DocBook file...done")
  320. (if (not (file-exists-p pdffile))
  321. (error "PDF file was not produced")
  322. (set-window-configuration wconfig)
  323. (message "Exporting to PDF...done")
  324. pdffile)))
  325. (defun org-export-as-docbook-pdf-and-open ()
  326. "Export as DocBook XML file, generate PDF file, and open it."
  327. (interactive)
  328. (let ((pdffile (org-export-as-docbook-pdf)))
  329. (if pdffile
  330. (org-open-file pdffile)
  331. (error "PDF file was not produced"))))
  332. (defvar org-heading-keyword-regexp-format) ; defined in org.el
  333. (defun org-export-as-docbook (&optional ext-plist to-buffer body-only pub-dir)
  334. "Export the current buffer as a DocBook file.
  335. If there is an active region, export only the region. When
  336. HIDDEN is obsolete and does nothing. EXT-PLIST is a
  337. property list with external parameters overriding org-mode's
  338. default settings, but still inferior to file-local settings.
  339. When TO-BUFFER is non-nil, create a buffer with that name and
  340. export to that buffer. If TO-BUFFER is the symbol `string',
  341. don't leave any buffer behind but just return the resulting HTML
  342. as a string. When BODY-ONLY is set, don't produce the file
  343. header and footer, simply return the content of the document (all
  344. top-level sections). When PUB-DIR is set, use this as the
  345. publishing directory."
  346. (interactive "P")
  347. (run-hooks 'org-export-first-hook)
  348. ;; Make sure we have a file name when we need it.
  349. (when (and (not (or to-buffer body-only))
  350. (not buffer-file-name))
  351. (if (buffer-base-buffer)
  352. (org-set-local 'buffer-file-name
  353. (with-current-buffer (buffer-base-buffer)
  354. buffer-file-name))
  355. (error "Need a file name to be able to export")))
  356. (message "Exporting...")
  357. (setq-default org-todo-line-regexp org-todo-line-regexp)
  358. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  359. (setq-default org-done-keywords org-done-keywords)
  360. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  361. (let* ((opt-plist
  362. (org-export-process-option-filters
  363. (org-combine-plists (org-default-export-plist)
  364. ext-plist
  365. (org-infile-export-plist))))
  366. (link-validate (plist-get opt-plist :link-validation-function))
  367. valid
  368. (odd org-odd-levels-only)
  369. (region-p (org-region-active-p))
  370. (rbeg (and region-p (region-beginning)))
  371. (rend (and region-p (region-end)))
  372. (subtree-p
  373. (if (plist-get opt-plist :ignore-subtree-p)
  374. nil
  375. (when region-p
  376. (save-excursion
  377. (goto-char rbeg)
  378. (and (org-at-heading-p)
  379. (>= (org-end-of-subtree t t) rend))))))
  380. (level-offset (if subtree-p
  381. (save-excursion
  382. (goto-char rbeg)
  383. (+ (funcall outline-level)
  384. (if org-odd-levels-only 1 0)))
  385. 0))
  386. (opt-plist (setq org-export-opt-plist
  387. (if subtree-p
  388. (org-export-add-subtree-options opt-plist rbeg)
  389. opt-plist)))
  390. ;; The following two are dynamically scoped into other
  391. ;; routines below.
  392. (org-current-export-dir
  393. (or pub-dir (org-export-directory :docbook opt-plist)))
  394. (org-current-export-file buffer-file-name)
  395. (level 0) (line "") (origline "") txt todo
  396. (filename (if to-buffer nil
  397. (expand-file-name
  398. (concat
  399. (file-name-sans-extension
  400. (or (and subtree-p
  401. (org-entry-get (region-beginning)
  402. "EXPORT_FILE_NAME" t))
  403. (file-name-nondirectory buffer-file-name)))
  404. org-export-docbook-extension)
  405. (file-name-as-directory
  406. (or pub-dir (org-export-directory :docbook opt-plist))))))
  407. (current-dir (if buffer-file-name
  408. (file-name-directory buffer-file-name)
  409. default-directory))
  410. (auto-insert nil); Avoid any auto-insert stuff for the new file
  411. (buffer (if to-buffer
  412. (cond
  413. ((eq to-buffer 'string)
  414. (get-buffer-create "*Org DocBook Export*"))
  415. (t (get-buffer-create to-buffer)))
  416. (find-file-noselect filename)))
  417. ;; org-levels-open is a global variable
  418. (org-levels-open (make-vector org-level-max nil))
  419. (date (plist-get opt-plist :date))
  420. (author (or (plist-get opt-plist :author)
  421. user-full-name))
  422. (email (plist-get opt-plist :email))
  423. firstname othername surname
  424. (title (or (and subtree-p (org-export-get-title-from-subtree))
  425. (plist-get opt-plist :title)
  426. (and (not
  427. (plist-get opt-plist :skip-before-1st-heading))
  428. (org-export-grab-title-from-buffer))
  429. (and buffer-file-name
  430. (file-name-sans-extension
  431. (file-name-nondirectory buffer-file-name)))
  432. "UNTITLED"))
  433. ;; We will use HTML table formatter to export tables to DocBook
  434. ;; format, so need to set html-table-tag here.
  435. (html-table-tag (plist-get opt-plist :html-table-tag))
  436. (quote-re0 (concat "^ *" org-quote-string "\\( +\\|[ \t]*$\\)"))
  437. (quote-re (format org-heading-keyword-regexp-format
  438. org-quote-string))
  439. (inquote nil)
  440. (infixed nil)
  441. (inverse nil)
  442. (llt org-plain-list-ordered-item-terminator)
  443. (email (plist-get opt-plist :email))
  444. (language (plist-get opt-plist :language))
  445. (lang-words nil)
  446. cnt
  447. (start 0)
  448. (coding-system (and (boundp 'buffer-file-coding-system)
  449. buffer-file-coding-system))
  450. (coding-system-for-write (or org-export-docbook-coding-system
  451. coding-system))
  452. (save-buffer-coding-system (or org-export-docbook-coding-system
  453. coding-system))
  454. (charset (and coding-system-for-write
  455. (fboundp 'coding-system-get)
  456. (coding-system-get coding-system-for-write
  457. 'mime-charset)))
  458. (region
  459. (buffer-substring
  460. (if region-p (region-beginning) (point-min))
  461. (if region-p (region-end) (point-max))))
  462. (org-export-footnotes-seen nil)
  463. (org-export-footnotes-data (org-footnote-all-labels 'with-defs))
  464. (lines
  465. (org-split-string
  466. (org-export-preprocess-string
  467. region
  468. :emph-multiline t
  469. :for-backend 'docbook
  470. :skip-before-1st-heading
  471. (plist-get opt-plist :skip-before-1st-heading)
  472. :drawers (plist-get opt-plist :drawers)
  473. :todo-keywords (plist-get opt-plist :todo-keywords)
  474. :tasks (plist-get opt-plist :tasks)
  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 org-outline-regexp-bol 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. ;; Discard protected matches not clearly identified as
  845. ;; footnote markers.
  846. (if (or (get-text-property (match-beginning 2) 'org-protected line)
  847. (not (get-text-property (match-beginning 2) 'org-footnote line)))
  848. (setq start (match-end 2))
  849. (let* ((num (match-string 2 line))
  850. (footnote-def (assoc num footnote-list)))
  851. (if (assoc num footref-seen)
  852. (setq line (replace-match
  853. (format "%s<footnoteref linkend=\"%s%s\"/>"
  854. (match-string 1 line)
  855. org-export-docbook-footnote-id-prefix num)
  856. t t line))
  857. (setq line (replace-match
  858. (concat
  859. (format "%s<footnote xml:id=\"%s%s\"><para>%s</para></footnote>"
  860. (match-string 1 line)
  861. org-export-docbook-footnote-id-prefix
  862. num
  863. (if footnote-def
  864. (save-match-data
  865. (org-docbook-expand (cdr footnote-def)))
  866. (format "FOOTNOTE DEFINITION NOT FOUND: %s" num)))
  867. ;; If another footnote is following the
  868. ;; current one, add a separator.
  869. (if (save-match-data
  870. (string-match "\\`\\[[0-9]+\\]"
  871. (substring line (match-end 0))))
  872. org-export-docbook-footnote-separator
  873. ""))
  874. t t line))
  875. (push (cons num 1) footref-seen))))))
  876. (cond
  877. ((string-match "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$" line)
  878. ;; This is a headline
  879. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
  880. level-offset))
  881. txt (match-string 2 line))
  882. (if (string-match quote-re0 txt)
  883. (setq txt (replace-match "" t t txt)))
  884. (org-export-docbook-level-start level txt)
  885. ;; QUOTES
  886. (when (string-match quote-re line)
  887. (org-export-docbook-close-para-maybe)
  888. (insert "<programlisting><![CDATA[")
  889. (setq inquote t)))
  890. ;; Tables: since version 4.3 of DocBook DTD, HTML tables are
  891. ;; supported. We can use existing HTML table exporter code
  892. ;; here.
  893. ((and org-export-with-tables
  894. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  895. (if (not table-open)
  896. ;; New table starts
  897. (setq table-open t
  898. table-buffer nil
  899. table-orig-buffer nil))
  900. ;; Accumulate lines
  901. (setq table-buffer (cons line table-buffer)
  902. table-orig-buffer (cons origline table-orig-buffer))
  903. (when (or (not lines)
  904. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  905. (car lines))))
  906. (setq table-open nil
  907. table-buffer (nreverse table-buffer)
  908. table-orig-buffer (nreverse table-orig-buffer))
  909. (org-export-docbook-close-para-maybe)
  910. (insert (org-export-docbook-finalize-table
  911. (org-format-table-html table-buffer table-orig-buffer
  912. 'no-css)))))
  913. ;; Normal lines
  914. (t
  915. ;; This line either is list item or end a list.
  916. (when (when (get-text-property 0 'list-item line)
  917. (setq line (org-export-docbook-list-line
  918. line
  919. (get-text-property 0 'list-item line)
  920. (get-text-property 0 'list-struct line)
  921. (get-text-property 0 'list-prevs line)))))
  922. ;; Empty lines start a new paragraph. If hand-formatted lists
  923. ;; are not fully interpreted, lines starting with "-", "+", "*"
  924. ;; also start a new paragraph.
  925. (if (and (string-match "^ [-+*]-\\|^[ \t]*$" line)
  926. (not inverse))
  927. (org-export-docbook-open-para))
  928. ;; Is this the start of a footnote?
  929. (when org-export-with-footnotes
  930. (when (and (boundp 'footnote-section-tag-regexp)
  931. (string-match (concat "^" footnote-section-tag-regexp)
  932. line))
  933. ;; ignore this line
  934. (throw 'nextline nil))
  935. ;; These footnote lines have been read and saved before,
  936. ;; ignore them at this time.
  937. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  938. (org-export-docbook-close-para-maybe)
  939. (throw 'nextline nil)))
  940. ;; FIXME: It might be a good idea to add an option to
  941. ;; support line break processing instruction <?linebreak?>.
  942. ;; Org-mode supports line break "\\" in HTML exporter, and
  943. ;; some DocBook users may also want to force line breaks
  944. ;; even though DocBook only supports that in
  945. ;; <literallayout>.
  946. (insert line "\n")))))
  947. ;; Properly close all local lists and other lists
  948. (when inquote
  949. (insert "]]></programlisting>\n")
  950. (org-export-docbook-open-para))
  951. ;; Close all open sections.
  952. (org-export-docbook-level-start 1 nil)
  953. (unless (plist-get opt-plist :buffer-will-be-killed)
  954. (normal-mode)
  955. (if (eq major-mode (default-value 'major-mode))
  956. (nxml-mode)))
  957. ;; Remove empty paragraphs. Replace them with a newline.
  958. (goto-char (point-min))
  959. (while (re-search-forward
  960. "[ \r\n\t]*\\(<para>\\)[ \r\n\t]*</para>[ \r\n\t]*" nil t)
  961. (when (not (get-text-property (match-beginning 1) 'org-protected))
  962. (replace-match "\n")
  963. (backward-char 1)))
  964. ;; Fill empty sections with <para></para>. This is to make sure
  965. ;; that the DocBook document generated is valid and well-formed.
  966. (goto-char (point-min))
  967. (while (re-search-forward
  968. "</title>\\([ \r\n\t]*\\)</section>" nil t)
  969. (when (not (get-text-property (match-beginning 0) 'org-protected))
  970. (replace-match "\n<para></para>\n" nil nil nil 1)))
  971. ;; Insert the last closing tag.
  972. (goto-char (point-max))
  973. (unless body-only
  974. (insert "</article>"))
  975. (run-hooks 'org-export-docbook-final-hook)
  976. (or to-buffer (save-buffer))
  977. (goto-char (point-min))
  978. (or (org-export-push-to-kill-ring "DocBook")
  979. (message "Exporting... done"))
  980. (if (eq to-buffer 'string)
  981. (prog1 (buffer-substring (point-min) (point-max))
  982. (kill-buffer (current-buffer)))
  983. (current-buffer)))))
  984. (defun org-export-docbook-open-para ()
  985. "Insert <para>, but first close previous paragraph if any."
  986. (org-export-docbook-close-para-maybe)
  987. (insert "\n<para>")
  988. (setq org-docbook-para-open t))
  989. (defun org-export-docbook-close-para-maybe ()
  990. "Close DocBook paragraph if there is one open."
  991. (when org-docbook-para-open
  992. (insert "</para>\n")
  993. (setq org-docbook-para-open nil)))
  994. (defun org-export-docbook-close-li (&optional type)
  995. "Close list if necessary."
  996. (org-export-docbook-close-para-maybe)
  997. (if (equal type "d")
  998. (insert "</listitem></varlistentry>\n")
  999. (insert "</listitem>\n")))
  1000. (defun org-export-docbook-level-start (level title)
  1001. "Insert a new level in DocBook export.
  1002. When TITLE is nil, just close all open levels."
  1003. (org-export-docbook-close-para-maybe)
  1004. (let* ((target (and title (org-get-text-property-any 0 'target title)))
  1005. (l org-level-max)
  1006. section-number)
  1007. (while (>= l level)
  1008. (if (aref org-levels-open (1- l))
  1009. (progn
  1010. (insert "</section>\n")
  1011. (aset org-levels-open (1- l) nil)))
  1012. (setq l (1- l)))
  1013. (when title
  1014. ;; If title is nil, this means this function is called to close
  1015. ;; all levels, so the rest is done only if title is given.
  1016. ;;
  1017. ;; Format tags: put them into a superscript like format.
  1018. (when (string-match (org-re "\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") title)
  1019. (setq title
  1020. (replace-match
  1021. (if org-export-with-tags
  1022. (save-match-data
  1023. (concat
  1024. "<superscript>"
  1025. (match-string 1 title)
  1026. "</superscript>"))
  1027. "")
  1028. t t title)))
  1029. (aset org-levels-open (1- level) t)
  1030. (setq section-number (org-section-number level))
  1031. (insert (format "\n<section xml:id=\"%s%s\">\n<title>%s</title>"
  1032. org-export-docbook-section-id-prefix
  1033. (replace-regexp-in-string "\\." "_" section-number)
  1034. title))
  1035. (org-export-docbook-open-para))))
  1036. (defun org-docbook-expand (string)
  1037. "Prepare STRING for DocBook export.
  1038. Applies all active conversions. If there are links in the
  1039. string, don't modify these."
  1040. (let* ((re (concat org-bracket-link-regexp "\\|"
  1041. (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")))
  1042. m s l res)
  1043. (while (setq m (string-match re string))
  1044. (setq s (substring string 0 m)
  1045. l (match-string 0 string)
  1046. string (substring string (match-end 0)))
  1047. (push (org-docbook-do-expand s) res)
  1048. (push l res))
  1049. (push (org-docbook-do-expand string) res)
  1050. (apply 'concat (nreverse res))))
  1051. (defun org-docbook-do-expand (s)
  1052. "Apply all active conversions to translate special ASCII to DocBook."
  1053. (setq s (org-html-protect s))
  1054. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  1055. (setq s (replace-match "<\\1>" t nil s)))
  1056. (if org-export-with-emphasize
  1057. (setq s (org-export-docbook-convert-emphasize s)))
  1058. (if org-export-with-special-strings
  1059. (setq s (org-export-docbook-convert-special-strings s)))
  1060. (if org-export-with-sub-superscripts
  1061. (setq s (org-export-docbook-convert-sub-super s)))
  1062. (if org-export-with-TeX-macros
  1063. (let ((start 0) wd rep)
  1064. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)\\({}\\)?"
  1065. s start))
  1066. (if (get-text-property (match-beginning 0) 'org-protected s)
  1067. (setq start (match-end 0))
  1068. (setq wd (match-string 1 s))
  1069. (if (setq rep (org-entity-get-representation wd 'html))
  1070. (setq s (replace-match rep t t s))
  1071. (setq start (+ start (length wd))))))))
  1072. s)
  1073. (defun org-export-docbook-format-desc (desc)
  1074. "Make sure DESC is valid as a description in a link."
  1075. (save-match-data
  1076. (org-docbook-do-expand desc)))
  1077. (defun org-export-docbook-convert-emphasize (string)
  1078. "Apply emphasis for DocBook exporting."
  1079. (let ((s 0) rpl)
  1080. (while (string-match org-emph-re string s)
  1081. (if (not (equal
  1082. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  1083. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  1084. (setq s (match-beginning 0)
  1085. rpl
  1086. (concat
  1087. (match-string 1 string)
  1088. (nth 1 (assoc (match-string 3 string)
  1089. org-export-docbook-emphasis-alist))
  1090. (match-string 4 string)
  1091. (nth 2 (assoc (match-string 3 string)
  1092. org-export-docbook-emphasis-alist))
  1093. (match-string 5 string))
  1094. string (replace-match rpl t t string)
  1095. s (+ s (- (length rpl) 2)))
  1096. (setq s (1+ s))))
  1097. string))
  1098. (defun org-docbook-protect (string)
  1099. (org-html-protect string))
  1100. ;; For now, simply return string as it is.
  1101. (defun org-export-docbook-convert-special-strings (string)
  1102. "Convert special characters in STRING to DocBook."
  1103. string)
  1104. (defun org-export-docbook-get-footnotes (lines)
  1105. "Given a list of LINES, return a list of alist footnotes."
  1106. (let ((list nil) line)
  1107. (while (setq line (pop lines))
  1108. (if (string-match "^[ \t]*\\[\\([0-9]+\\)\\] \\(.+\\)" line)
  1109. (push (cons (match-string 1 line) (match-string 2 line))
  1110. list)))
  1111. list))
  1112. (defun org-export-docbook-format-image (src)
  1113. "Create image element in DocBook."
  1114. (save-match-data
  1115. (let* ((caption (org-find-text-property-in-string 'org-caption src))
  1116. (attr (or (org-find-text-property-in-string 'org-attributes src)
  1117. ""))
  1118. (label (org-find-text-property-in-string 'org-label src))
  1119. (default-attr org-export-docbook-default-image-attributes)
  1120. tmp)
  1121. (setq caption (and caption (org-html-do-expand caption)))
  1122. (while (setq tmp (pop default-attr))
  1123. (if (not (string-match (concat (car tmp) "=") attr))
  1124. (setq attr (concat attr " " (car tmp) "=" (cdr tmp)))))
  1125. (format "<mediaobject%s>
  1126. <imageobject>\n<imagedata fileref=\"%s\" %s/>\n</imageobject>
  1127. %s</mediaobject>"
  1128. (if label (concat " xml:id=\"" label "\"") "")
  1129. src attr
  1130. (if caption
  1131. (concat "<caption>\n<para>"
  1132. caption
  1133. "</para>\n</caption>\n")
  1134. "")
  1135. ))))
  1136. (defun org-export-docbook-preprocess (parameters)
  1137. "Extra preprocessing work for DocBook export."
  1138. ;; Merge lines starting with "\par" to one line. Such lines are
  1139. ;; regarded as the continuation of a long footnote.
  1140. (goto-char (point-min))
  1141. (while (re-search-forward "\n\\(\\\\par\\>\\)" nil t)
  1142. (if (not (get-text-property (match-beginning 1) 'org-protected))
  1143. (replace-match ""))))
  1144. (defun org-export-docbook-finalize-table (table)
  1145. "Clean up TABLE and turn it into DocBook format.
  1146. This function adds a label to the table if it is available, and
  1147. also changes TABLE to informaltable if caption does not exist.
  1148. TABLE is a string containing the HTML code generated by
  1149. `org-format-table-html' for a table in Org-mode buffer."
  1150. (let (table-with-label)
  1151. ;; Get the label if it exists, and move it into the <table> element.
  1152. (setq table-with-label
  1153. (if (string-match
  1154. "^<table \\(\\(.\\|\n\\)+\\)<a name=\"\\(.+\\)\" id=\".+\"></a>\n\\(\\(.\\|\n\\)+\\)</table>"
  1155. table)
  1156. (replace-match (concat "<table xml:id=\"" (match-string 3 table) "\" "
  1157. (match-string 1 table)
  1158. (match-string 4 table)
  1159. "</table>")
  1160. nil t table)
  1161. table))
  1162. ;; Change <table> into <informaltable> if caption does not exist.
  1163. (if (string-match
  1164. "^<table \\(\\(.\\|\n\\)+\\)<caption></caption>\n\\(\\(.\\|\n\\)+\\)</table>"
  1165. table-with-label)
  1166. (replace-match (concat "<informaltable "
  1167. (match-string 1 table-with-label)
  1168. (match-string 3 table-with-label)
  1169. "</informaltable>")
  1170. nil t table-with-label)
  1171. table-with-label)))
  1172. ;; Note: This function is very similar to
  1173. ;; org-export-html-convert-sub-super. They can be merged in the future.
  1174. (defun org-export-docbook-convert-sub-super (string)
  1175. "Convert sub- and superscripts in STRING for DocBook."
  1176. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  1177. (while (string-match org-match-substring-regexp string s)
  1178. (cond
  1179. ((and requireb (match-end 8)) (setq s (match-end 2)))
  1180. ((get-text-property (match-beginning 2) 'org-protected string)
  1181. (setq s (match-end 2)))
  1182. (t
  1183. (setq s (match-end 1)
  1184. key (if (string= (match-string 2 string) "_")
  1185. "subscript"
  1186. "superscript")
  1187. c (or (match-string 8 string)
  1188. (match-string 6 string)
  1189. (match-string 5 string))
  1190. string (replace-match
  1191. (concat (match-string 1 string)
  1192. "<" key ">" c "</" key ">")
  1193. t t string)))))
  1194. (while (string-match "\\\\\\([_^]\\)" string)
  1195. (setq string (replace-match (match-string 1 string) t t string)))
  1196. string))
  1197. (defun org-export-docbook-protect-tags (string)
  1198. "Change ``<...>'' in string STRING into ``@<...>''.
  1199. This is normally needed when STRING contains DocBook elements
  1200. that need to be preserved in later phase of DocBook exporting."
  1201. (let ((start 0))
  1202. (while (string-match "<\\([^>]*\\)>" string start)
  1203. (setq string (replace-match
  1204. "@<\\1>" t nil string)
  1205. start (match-end 0)))
  1206. string))
  1207. (defun org-export-docbook-handle-time-stamps (line)
  1208. "Format time stamps in string LINE."
  1209. (let (replaced
  1210. (kw-markup (org-export-docbook-protect-tags
  1211. org-export-docbook-keywords-markup))
  1212. (ts-markup (org-export-docbook-protect-tags
  1213. org-export-docbook-timestamp-markup)))
  1214. (while (string-match org-maybe-keyword-time-regexp line)
  1215. (setq replaced
  1216. (concat replaced
  1217. (substring line 0 (match-beginning 0))
  1218. (if (match-end 1)
  1219. (format kw-markup
  1220. (match-string 1 line)))
  1221. " "
  1222. (format ts-markup
  1223. (substring (org-translate-time
  1224. (match-string 3 line)) 1 -1)))
  1225. line (substring line (match-end 0))))
  1226. (concat replaced line)))
  1227. (defun org-export-docbook-list-line (line pos struct prevs)
  1228. "Insert list syntax in export buffer. Return LINE, maybe modified.
  1229. POS is the item position or line position the line had before
  1230. modifications to buffer. STRUCT is the list structure. PREVS is
  1231. the alist of previous items."
  1232. (let* ((get-type
  1233. (function
  1234. ;; Translate type of list containing POS to "ordered",
  1235. ;; "variable" or "itemized".
  1236. (lambda (pos struct prevs)
  1237. (let ((type (org-list-get-list-type pos struct prevs)))
  1238. (cond
  1239. ((eq 'ordered type) "ordered")
  1240. ((eq 'descriptive type) "variable")
  1241. (t "itemized"))))))
  1242. (get-closings
  1243. (function
  1244. ;; Return list of all items and sublists ending at POS, in
  1245. ;; reverse order.
  1246. (lambda (pos)
  1247. (let (out)
  1248. (catch 'exit
  1249. (mapc (lambda (e)
  1250. (let ((end (nth 6 e))
  1251. (item (car e)))
  1252. (cond
  1253. ((= end pos) (push item out))
  1254. ((>= item pos) (throw 'exit nil)))))
  1255. struct))
  1256. out)))))
  1257. ;; First close any previous item, or list, ending at POS.
  1258. (mapc (lambda (e)
  1259. (let* ((lastp (= (org-list-get-last-item e struct prevs) e))
  1260. (first-item (org-list-get-list-begin e struct prevs))
  1261. (type (funcall get-type first-item struct prevs)))
  1262. ;; Ending for every item
  1263. (org-export-docbook-close-para-maybe)
  1264. (insert (if (equal type "variable")
  1265. "</listitem></varlistentry>\n"
  1266. "</listitem>\n"))
  1267. ;; We're ending last item of the list: end list.
  1268. (when lastp
  1269. (insert (format "</%slist>\n" type))
  1270. (org-export-docbook-open-para))))
  1271. (funcall get-closings pos))
  1272. (cond
  1273. ;; At an item: insert appropriate tags in export buffer.
  1274. ((assq pos struct)
  1275. (string-match (concat "[ \t]*\\(\\S-+[ \t]*\\)"
  1276. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[a-zA-Z]\\)\\][ \t]*\\)?"
  1277. "\\(?:\\(\\[[ X-]\\]\\)[ \t]+\\)?"
  1278. "\\(?:\\(.*\\)[ \t]+::\\(?:[ \t]+\\|$\\)\\)?"
  1279. "\\(.*\\)")
  1280. line)
  1281. (let* ((checkbox (match-string 3 line))
  1282. (desc-tag (or (match-string 4 line) "???"))
  1283. (body (match-string 5 line))
  1284. (list-beg (org-list-get-list-begin pos struct prevs))
  1285. (firstp (= list-beg pos))
  1286. ;; Always refer to first item to determine list type, in
  1287. ;; case list is ill-formed.
  1288. (type (funcall get-type list-beg struct prevs))
  1289. ;; Special variables for ordered lists.
  1290. (counter (let ((count-tmp (org-list-get-counter pos struct)))
  1291. (cond
  1292. ((not count-tmp) nil)
  1293. ((string-match "[A-Za-z]" count-tmp)
  1294. (- (string-to-char (upcase count-tmp)) 64))
  1295. ((string-match "[0-9]+" count-tmp)
  1296. count-tmp)))))
  1297. ;; When FIRSTP, a new list or sub-list is starting.
  1298. (when firstp
  1299. (org-export-docbook-close-para-maybe)
  1300. (insert (format "<%slist>\n" type)))
  1301. (insert (cond
  1302. ((equal type "variable")
  1303. (format "<varlistentry><term>%s</term><listitem>" desc-tag))
  1304. ((and (equal type "ordered") counter)
  1305. (format "<listitem override=\"%s\">" counter))
  1306. (t "<listitem>")))
  1307. ;; For DocBook, we need to open a para right after tag
  1308. ;; <listitem>.
  1309. (org-export-docbook-open-para)
  1310. ;; If line had a checkbox, some additional modification is required.
  1311. (when checkbox (setq body (concat checkbox " " body)))
  1312. ;; Return modified line
  1313. body))
  1314. ;; At a list ender: normal text follows: need <para>.
  1315. ((equal "ORG-LIST-END-MARKER" line)
  1316. (org-export-docbook-open-para)
  1317. (throw 'nextline nil))
  1318. ;; Not at an item: return line unchanged (side-effects only).
  1319. (t line))))
  1320. (provide 'org-docbook)
  1321. ;; Local variables:
  1322. ;; generated-autoload-file: "org-loaddefs.el"
  1323. ;; End:
  1324. ;;; org-docbook.el ends here