org-docbook.el 50 KB

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