org-docbook.el 51 KB

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