org-docbook.el 52 KB

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