org-docbook.el 52 KB

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