org-docbook.el 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  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.25trans
  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. ;; $Id: org-docbook.el 35 2009-03-23 01:03:21Z baoqiu $
  13. ;; URL:
  14. ;; This file is NOT part of GNU Emacs.
  15. ;; GNU Emacs is free software: you can redistribute it and/or modify
  16. ;; it under the terms of the GNU General Public License as published by
  17. ;; the Free Software Foundation, either version 3 of the License, or
  18. ;; (at your option) any later version.
  19. ;; GNU Emacs is distributed in the hope that it will be useful,
  20. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. ;; GNU General Public License for more details.
  23. ;; You should have received a copy of the GNU General Public License
  24. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  25. ;; Commentary:
  26. ;;
  27. ;; This library implements a DocBook exporter for org-mode. The basic
  28. ;; idea and design is very similar to what `org-export-as-html' has.
  29. ;; Code prototype was also started with `org-export-as-html'.
  30. ;;
  31. ;; Put this file into your load-path and the following line into your
  32. ;; ~/.emacs:
  33. ;;
  34. ;; (require 'org-docbook)
  35. ;;
  36. ;; The interactive functions are similar to those of the HTML and LaTeX
  37. ;; exporters:
  38. ;;
  39. ;; M-x `org-export-as-docbook'
  40. ;; M-x `org-export-as-docbook-pdf'
  41. ;; M-x `org-export-as-docbook-pdf-and-open'
  42. ;; M-x `org-export-as-docbook-batch'
  43. ;; M-x `org-export-as-docbook-to-buffer'
  44. ;; M-x `org-export-region-as-docbook'
  45. ;; M-x `org-replace-region-by-docbook'
  46. ;;
  47. ;; Note that, in order to generate PDF files using the DocBook XML files
  48. ;; created by DocBook exporter, the following two variables have to be
  49. ;; set based on what DocBook tools you use for XSLT processor and XSL-FO
  50. ;; processor:
  51. ;;
  52. ;; org-export-docbook-xslt-proc-command
  53. ;; org-export-docbook-xsl-fo-proc-command
  54. ;;
  55. ;; Check the document of these two variables to see examples of how they
  56. ;; can be set.
  57. ;;
  58. ;; If the Org file to be exported contains special characters written in
  59. ;; TeX-like syntax, like \alpha and \beta, you need to include the right
  60. ;; entity file(s) in the DOCTYPE declaration for the DocBook XML file.
  61. ;; This is required to make the DocBook XML file valid. The DOCTYPE
  62. ;; declaration string can be set using the following variable:
  63. ;;
  64. ;; org-export-docbook-doctype
  65. ;;
  66. ;;; Code:
  67. (eval-when-compile
  68. (require 'cl))
  69. (require 'footnote)
  70. (require 'org)
  71. (require 'org-exp)
  72. (require 'org-html)
  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 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. ;;; Autoload functions:
  197. ;;;###autoload
  198. (defun org-export-as-docbook-batch ()
  199. "Call `org-export-as-docbook' in batch style.
  200. This function can be used in batch processing.
  201. For example:
  202. $ emacs --batch
  203. --load=$HOME/lib/emacs/org.el
  204. --visit=MyOrgFile.org --funcall org-export-as-docbook-batch"
  205. (org-export-as-docbook 'hidden))
  206. ;;;###autoload
  207. (defun org-export-as-docbook-to-buffer ()
  208. "Call `org-export-as-docbook' with output to a temporary buffer.
  209. No file is created."
  210. (interactive)
  211. (org-export-as-docbook nil nil "*Org DocBook Export*")
  212. (switch-to-buffer-other-window "*Org DocBook Export*"))
  213. ;;;###autoload
  214. (defun org-replace-region-by-docbook (beg end)
  215. "Replace the region from BEG to END with its DocBook export.
  216. It assumes the region has `org-mode' syntax, and then convert it to
  217. DocBook. This can be used in any buffer. For example, you could
  218. write an itemized list in `org-mode' syntax in an DocBook buffer and
  219. then use this command to convert it."
  220. (interactive "r")
  221. (let (reg docbook buf)
  222. (save-window-excursion
  223. (if (org-mode-p)
  224. (setq docbook (org-export-region-as-docbook
  225. beg end t 'string))
  226. (setq reg (buffer-substring beg end)
  227. buf (get-buffer-create "*Org tmp*"))
  228. (save-excursion
  229. (set-buffer buf)
  230. (erase-buffer)
  231. (insert reg)
  232. (org-mode)
  233. (setq docbook (org-export-region-as-docbook
  234. (point-min) (point-max) t 'string)))
  235. (kill-buffer buf)))
  236. (delete-region beg end)
  237. (insert docbook)))
  238. ;;;###autoload
  239. (defun org-export-region-as-docbook (beg end &optional body-only buffer)
  240. "Convert region from BEG to END in `org-mode' buffer to DocBook.
  241. If prefix arg BODY-ONLY is set, omit file header and footer and
  242. only produce the region of converted text, useful for
  243. cut-and-paste operations. If BUFFER is a buffer or a string,
  244. use/create that buffer as a target of the converted DocBook. If
  245. BUFFER is the symbol `string', return the produced DocBook as a
  246. string and leave not buffer behind. For example, a Lisp program
  247. could call this function in the following way:
  248. (setq docbook (org-export-region-as-docbook beg end t 'string))
  249. When called interactively, the output buffer is selected, and shown
  250. in a window. A non-interactive call will only retunr the buffer."
  251. (interactive "r\nP")
  252. (when (interactive-p)
  253. (setq buffer "*Org DocBook Export*"))
  254. (let ((transient-mark-mode t)
  255. (zmacs-regions t)
  256. rtn)
  257. (goto-char end)
  258. (set-mark (point)) ;; To activate the region
  259. (goto-char beg)
  260. (setq rtn (org-export-as-docbook
  261. nil nil
  262. buffer body-only))
  263. (if (fboundp 'deactivate-mark) (deactivate-mark))
  264. (if (and (interactive-p) (bufferp rtn))
  265. (switch-to-buffer-other-window rtn)
  266. rtn)))
  267. ;;;###autoload
  268. (defun org-export-as-docbook-pdf (&optional hidden ext-plist
  269. to-buffer body-only pub-dir)
  270. "Export as DocBook XML file, and generate PDF file."
  271. (interactive "P")
  272. (if (or (not org-export-docbook-xslt-proc-command)
  273. (not (string-match "%s.+%s" org-export-docbook-xslt-proc-command)))
  274. (error "XSLT processor command is not set correctly"))
  275. (if (or (not org-export-docbook-xsl-fo-proc-command)
  276. (not (string-match "%s.+%s" org-export-docbook-xsl-fo-proc-command)))
  277. (error "XSL-FO processor command is not set correctly"))
  278. (message "Exporting to PDF...")
  279. (let* ((wconfig (current-window-configuration))
  280. (docbook-buf (org-export-as-docbook hidden ext-plist
  281. to-buffer body-only pub-dir))
  282. (filename (buffer-file-name docbook-buf))
  283. (base (file-name-sans-extension filename))
  284. (fofile (concat base ".fo"))
  285. (pdffile (concat base ".pdf")))
  286. (and (file-exists-p pdffile) (delete-file pdffile))
  287. (message "Processing DocBook XML file...")
  288. (shell-command (format org-export-docbook-xslt-proc-command
  289. fofile (shell-quote-argument filename)))
  290. (shell-command (format org-export-docbook-xsl-fo-proc-command
  291. fofile pdffile))
  292. (message "Processing DocBook file...done")
  293. (if (not (file-exists-p pdffile))
  294. (error "PDF file was not produced")
  295. (set-window-configuration wconfig)
  296. (message "Exporting to PDF...done")
  297. pdffile)))
  298. ;;;###autoload
  299. (defun org-export-as-docbook-pdf-and-open ()
  300. "Export as DocBook XML file, generate PDF file, and open it."
  301. (interactive)
  302. (let ((pdffile (org-export-as-docbook-pdf)))
  303. (if pdffile
  304. (org-open-file pdffile)
  305. (error "PDF file was not produced"))))
  306. ;;;###autoload
  307. (defun org-export-as-docbook (&optional hidden ext-plist
  308. to-buffer body-only pub-dir)
  309. "Export the current buffer as a DocBook file.
  310. If there is an active region, export only the region. When
  311. HIDDEN is non-nil, don't display the HTML buffer. EXT-PLIST is a
  312. property list with external parameters overriding org-mode's
  313. default settings, but still inferior to file-local settings.
  314. When TO-BUFFER is non-nil, create a buffer with that name and
  315. export to that buffer. If TO-BUFFER is the symbol `string',
  316. don't leave any buffer behind but just return the resulting HTML
  317. as a string. When BODY-ONLY is set, don't produce the file
  318. header and footer, simply return the content of the document (all
  319. top-level sections). When PUB-DIR is set, use this as the
  320. publishing directory."
  321. (interactive "P")
  322. ;; Make sure we have a file name when we need it.
  323. (when (and (not (or to-buffer body-only))
  324. (not buffer-file-name))
  325. (if (buffer-base-buffer)
  326. (org-set-local 'buffer-file-name
  327. (with-current-buffer (buffer-base-buffer)
  328. buffer-file-name))
  329. (error "Need a file name to be able to export.")))
  330. (message "Exporting...")
  331. (setq-default org-todo-line-regexp org-todo-line-regexp)
  332. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  333. (setq-default org-done-keywords org-done-keywords)
  334. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  335. (let* ((opt-plist
  336. (org-export-process-option-filters
  337. (org-combine-plists (org-default-export-plist)
  338. ext-plist
  339. (org-infile-export-plist))))
  340. (link-validate (plist-get opt-plist :link-validation-function))
  341. valid
  342. (odd org-odd-levels-only)
  343. (region-p (org-region-active-p))
  344. (rbeg (and region-p (region-beginning)))
  345. (rend (and region-p (region-end)))
  346. (subtree-p
  347. (if (plist-get opt-plist :ignore-subree-p)
  348. nil
  349. (when region-p
  350. (save-excursion
  351. (goto-char rbeg)
  352. (and (org-at-heading-p)
  353. (>= (org-end-of-subtree t t) rend))))))
  354. (level-offset (if subtree-p
  355. (save-excursion
  356. (goto-char rbeg)
  357. (+ (funcall outline-level)
  358. (if org-odd-levels-only 1 0)))
  359. 0))
  360. (opt-plist (if subtree-p
  361. (org-export-add-subtree-options opt-plist rbeg)
  362. opt-plist))
  363. ;; The following two are dynamically scoped into other
  364. ;; routines below.
  365. (org-current-export-dir
  366. (or pub-dir (org-export-directory :docbook opt-plist)))
  367. (org-current-export-file buffer-file-name)
  368. (level 0) (line "") (origline "") txt todo
  369. (filename (if to-buffer nil
  370. (expand-file-name
  371. (concat
  372. (file-name-sans-extension
  373. (or (and subtree-p
  374. (org-entry-get (region-beginning)
  375. "EXPORT_FILE_NAME" t))
  376. (file-name-nondirectory buffer-file-name)))
  377. org-export-docbook-extension)
  378. (file-name-as-directory
  379. (or pub-dir (org-export-directory :docbook opt-plist))))))
  380. (current-dir (if buffer-file-name
  381. (file-name-directory buffer-file-name)
  382. default-directory))
  383. (buffer (if to-buffer
  384. (cond
  385. ((eq to-buffer 'string) (get-buffer-create "*Org DocBook Export*"))
  386. (t (get-buffer-create to-buffer)))
  387. (find-file-noselect filename)))
  388. ;; org-levels-open is a global variable
  389. (org-levels-open (make-vector org-level-max nil))
  390. (date (plist-get opt-plist :date))
  391. (author (or (plist-get opt-plist :author)
  392. user-full-name))
  393. (email (plist-get opt-plist :email))
  394. firstname othername surname
  395. (title (or (and subtree-p (org-export-get-title-from-subtree))
  396. (plist-get opt-plist :title)
  397. (and (not
  398. (plist-get opt-plist :skip-before-1st-heading))
  399. (org-export-grab-title-from-buffer))
  400. (and buffer-file-name
  401. (file-name-sans-extension
  402. (file-name-nondirectory buffer-file-name)))
  403. "UNTITLED"))
  404. ;; We will use HTML table formatter to export tables to DocBook
  405. ;; format, so need to set html-table-tag here.
  406. (html-table-tag (plist-get opt-plist :html-table-tag))
  407. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  408. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  409. (inquote nil)
  410. (infixed nil)
  411. (inverse nil)
  412. (in-local-list nil)
  413. (local-list-type nil)
  414. (local-list-indent nil)
  415. (llt org-plain-list-ordered-item-terminator)
  416. (email (plist-get opt-plist :email))
  417. (language (plist-get opt-plist :language))
  418. (lang-words nil)
  419. cnt
  420. (start 0)
  421. (coding-system (and (boundp 'buffer-file-coding-system)
  422. buffer-file-coding-system))
  423. (coding-system-for-write (or org-export-docbook-coding-system
  424. coding-system))
  425. (save-buffer-coding-system (or org-export-docbook-coding-system
  426. coding-system))
  427. (charset (and coding-system-for-write
  428. (fboundp 'coding-system-get)
  429. (coding-system-get coding-system-for-write
  430. 'mime-charset)))
  431. (region
  432. (buffer-substring
  433. (if region-p (region-beginning) (point-min))
  434. (if region-p (region-end) (point-max))))
  435. (lines
  436. (org-split-string
  437. (org-export-preprocess-string
  438. region
  439. :emph-multiline t
  440. :for-docbook t
  441. :skip-before-1st-heading
  442. (plist-get opt-plist :skip-before-1st-heading)
  443. :drawers (plist-get opt-plist :drawers)
  444. :todo-keywords (plist-get opt-plist :todo-keywords)
  445. :tags (plist-get opt-plist :tags)
  446. :priority (plist-get opt-plist :priority)
  447. :footnotes (plist-get opt-plist :footnotes)
  448. :timestamps (plist-get opt-plist :timestamps)
  449. :archived-trees
  450. (plist-get opt-plist :archived-trees)
  451. :select-tags (plist-get opt-plist :select-tags)
  452. :exclude-tags (plist-get opt-plist :exclude-tags)
  453. :add-text
  454. (plist-get opt-plist :text)
  455. :LaTeX-fragments
  456. (plist-get opt-plist :LaTeX-fragments))
  457. "[\r\n]"))
  458. ;; Use literal output to show check boxes.
  459. (checkbox-start
  460. (nth 1 (assoc "=" org-export-docbook-emphasis-alist)))
  461. (checkbox-end
  462. (nth 2 (assoc "=" org-export-docbook-emphasis-alist)))
  463. table-open type
  464. table-buffer table-orig-buffer
  465. ind item-type starter didclose
  466. rpl path attr caption label desc descp desc1 desc2 link
  467. fnc item-tag
  468. footref-seen footnote-list
  469. id-file
  470. )
  471. ;; Fine detailed info about author name.
  472. (if (string-match "\\([^ ]+\\) \\(.+ \\)?\\([^ ]+\\)" author)
  473. (progn
  474. (setq firstname (match-string 1 author)
  475. othername (or (match-string 2 author) "")
  476. surname (match-string 3 author))))
  477. ;; Get all footnote text.
  478. (setq footnote-list
  479. (org-export-docbook-get-footnotes lines))
  480. (let ((inhibit-read-only t))
  481. (org-unmodified
  482. (remove-text-properties (point-min) (point-max)
  483. '(:org-license-to-kill t))))
  484. (setq org-min-level (org-get-min-level lines level-offset))
  485. (setq org-last-level org-min-level)
  486. (org-init-section-numbers)
  487. ;; Get and save the date.
  488. (cond
  489. ((and date (string-match "%" date))
  490. (setq date (format-time-string date)))
  491. (date)
  492. (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
  493. ;; Get the language-dependent settings
  494. (setq lang-words (or (assoc language org-export-language-setup)
  495. (assoc "en" org-export-language-setup)))
  496. ;; Switch to the output buffer. Use fundamental-mode for now. We
  497. ;; could turn on nXML mode later and do some indentation.
  498. (set-buffer buffer)
  499. (let ((inhibit-read-only t)) (erase-buffer))
  500. (fundamental-mode)
  501. (and (fboundp 'set-buffer-file-coding-system)
  502. (set-buffer-file-coding-system coding-system-for-write))
  503. ;; The main body...
  504. (let ((case-fold-search nil)
  505. (org-odd-levels-only odd))
  506. ;; Create local variables for all options, to make sure all called
  507. ;; functions get the correct information
  508. (mapc (lambda (x)
  509. (set (make-local-variable (nth 2 x))
  510. (plist-get opt-plist (car x))))
  511. org-export-plist-vars)
  512. ;; Insert DocBook file header, title, and author info.
  513. (unless body-only
  514. (insert org-export-docbook-header)
  515. (if org-export-docbook-doctype
  516. (insert org-export-docbook-doctype))
  517. (insert "<!-- Date: " date " -->\n")
  518. (insert (format "<!-- DocBook XML file generated by Org-mode %s Emacs %s -->\n"
  519. org-version emacs-major-version))
  520. (insert org-export-docbook-article-header)
  521. (insert (format
  522. "\n <title>%s</title>
  523. <info>
  524. <author>
  525. <personname>
  526. <firstname>%s</firstname> <othername>%s</othername> <surname>%s</surname>
  527. </personname>
  528. %s
  529. </author>
  530. </info>\n"
  531. (org-docbook-expand title)
  532. firstname othername surname
  533. (if email (concat "<email>" email "</email>") "")
  534. )))
  535. (org-init-section-numbers)
  536. (org-export-docbook-open-para)
  537. ;; Loop over all the lines...
  538. (while (setq line (pop lines) origline line)
  539. (catch 'nextline
  540. ;; End of quote section?
  541. (when (and inquote (string-match "^\\*+ " line))
  542. (insert "]]>\n</programlisting>\n")
  543. (org-export-docbook-open-para)
  544. (setq inquote nil))
  545. ;; Inside a quote section?
  546. (when inquote
  547. (insert (org-docbook-protect line) "\n")
  548. (throw 'nextline nil))
  549. ;; Fixed-width, verbatim lines (examples)
  550. (when (and org-export-with-fixed-width
  551. (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line))
  552. (when (not infixed)
  553. (setq infixed t)
  554. (org-export-docbook-close-para-maybe)
  555. (insert "<programlisting><![CDATA["))
  556. (insert (match-string 3 line) "\n")
  557. (when (or (not lines)
  558. (not (string-match "^[ \t]*\\(:.*\\)"
  559. (car lines))))
  560. (setq infixed nil)
  561. (insert "]]>\n</programlisting>\n")
  562. (org-export-docbook-open-para))
  563. (throw 'nextline nil))
  564. ;; Protected HTML
  565. (when (get-text-property 0 'org-protected line)
  566. (let (par)
  567. (when (re-search-backward
  568. "\\(<para>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  569. (setq par (match-string 1))
  570. (replace-match "\\2\n"))
  571. (insert line "\n")
  572. (while (and lines
  573. (or (= (length (car lines)) 0)
  574. (get-text-property 0 'org-protected (car lines))))
  575. (insert (pop lines) "\n"))
  576. (and par (insert "<para>\n")))
  577. (throw 'nextline nil))
  578. ;; Start of block quotes and verses
  579. (when (or (equal "ORG-BLOCKQUOTE-START" line)
  580. (and (equal "ORG-VERSE-START" line)
  581. (setq inverse t)))
  582. (org-export-docbook-close-para-maybe)
  583. (insert "<blockquote>")
  584. ;; Check whether attribution for this blockquote exists.
  585. (let (tmp1
  586. attribution
  587. (end (if inverse "ORG-VERSE-END" "ORG-BLOCKQUOTE-END"))
  588. (quote-lines nil))
  589. (while (and (setq tmp1 (pop lines))
  590. (not (equal end tmp1)))
  591. (push tmp1 quote-lines))
  592. (push tmp1 lines) ; Put back quote end mark
  593. ;; Check the last line in the quote to see if it contains
  594. ;; the attribution.
  595. (setq tmp1 (pop quote-lines))
  596. (if (string-match "\\(^.*\\)\\(--[ \t]+\\)\\(.+\\)$" tmp1)
  597. (progn
  598. (setq attribution (match-string 3 tmp1))
  599. (when (save-match-data
  600. (string-match "[^ \t]" (match-string 1 tmp1)))
  601. (push (match-string 1 tmp1) lines)))
  602. (push tmp1 lines))
  603. (while (setq tmp1 (pop quote-lines))
  604. (push tmp1 lines))
  605. (when attribution
  606. (insert "<attribution>" attribution "</attribution>")))
  607. ;; Insert <literallayout> for verse.
  608. (if inverse
  609. (insert "\n<literallayout>")
  610. (org-export-docbook-open-para))
  611. (throw 'nextline nil))
  612. ;; End of block quotes
  613. (when (equal "ORG-BLOCKQUOTE-END" line)
  614. (org-export-docbook-close-para-maybe)
  615. (insert "</blockquote>\n")
  616. (org-export-docbook-open-para)
  617. (throw 'nextline nil))
  618. ;; End of verses
  619. (when (equal "ORG-VERSE-END" line)
  620. (insert "</literallayout>\n</blockquote>\n")
  621. (org-export-docbook-open-para)
  622. (setq inverse nil)
  623. (throw 'nextline nil))
  624. ;; Text centering. Element <para role="centered"> does not
  625. ;; seem to work with FOP, so for now we use <informaltable> to
  626. ;; center the text, which can contain multiple paragraphs.
  627. (when (equal "ORG-CENTER-START" line)
  628. (org-export-docbook-close-para-maybe)
  629. (insert "<informaltable frame=\"none\" colsep=\"0\" rowsep=\"0\">\n"
  630. "<tgroup align=\"center\" cols=\"1\">\n"
  631. "<tbody><row><entry>\n")
  632. (org-export-docbook-open-para)
  633. (throw 'nextline nil))
  634. (when (equal "ORG-CENTER-END" line)
  635. (org-export-docbook-close-para-maybe)
  636. (insert "</entry></row></tbody>\n"
  637. "</tgroup>\n</informaltable>\n")
  638. (org-export-docbook-open-para)
  639. (throw 'nextline nil))
  640. ;; Make targets to anchors. Note that currently FOP does not
  641. ;; seem to support <anchor> tags when generating PDF output,
  642. ;; but this can be used in DocBook --> HTML conversion.
  643. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  644. (cond
  645. ((match-end 2)
  646. (setq line (replace-match
  647. (format "@<anchor xml:id=\"%s\"/>"
  648. (org-solidify-link-text (match-string 1 line)))
  649. t t line)))
  650. (t
  651. (setq line (replace-match
  652. (format "@<anchor xml:id=\"%s\"/>"
  653. (org-solidify-link-text (match-string 1 line)))
  654. t t line)))))
  655. ;; Replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  656. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  657. ;; Also handle sub_superscripts and checkboxes
  658. (or (string-match org-table-hline-regexp line)
  659. (setq line (org-docbook-expand line)))
  660. ;; Format the links
  661. (setq start 0)
  662. (while (string-match org-bracket-link-analytic-regexp++ line start)
  663. (setq start (match-beginning 0))
  664. (setq path (save-match-data (org-link-unescape
  665. (match-string 3 line))))
  666. (setq type (cond
  667. ((match-end 2) (match-string 2 line))
  668. ((save-match-data
  669. (or (file-name-absolute-p path)
  670. (string-match "^\\.\\.?/" path)))
  671. "file")
  672. (t "internal")))
  673. (setq path (org-extract-attributes (org-link-unescape path)))
  674. (setq attr (get-text-property 0 'org-attributes path)
  675. caption (get-text-property 0 'org-caption path)
  676. label (get-text-property 0 'org-label path))
  677. (setq desc1 (if (match-end 5) (match-string 5 line))
  678. desc2 (if (match-end 2) (concat type ":" path) path)
  679. descp (and desc1 (not (equal desc1 desc2)))
  680. desc (or desc1 desc2))
  681. ;; Make an image out of the description if that is so wanted
  682. (when (and descp (org-file-image-p
  683. desc org-export-docbook-inline-image-extensions))
  684. (save-match-data
  685. (if (string-match "^file:" desc)
  686. (setq desc (substring desc (match-end 0))))))
  687. ;; FIXME: do we need to unescape here somewhere?
  688. (cond
  689. ((equal type "internal")
  690. (setq rpl (format "<link linkend=\"%s\">%s</link>"
  691. (org-solidify-link-text
  692. (save-match-data (org-link-unescape path)) nil)
  693. (org-export-docbook-format-desc desc))))
  694. ((and (equal type "id")
  695. (setq id-file (org-id-find-id-file path)))
  696. ;; This is an id: link to another file (if it was the same file,
  697. ;; it would have become an internal link...)
  698. (setq id-file (file-relative-name
  699. id-file (file-name-directory org-current-export-file)))
  700. (setq id-file (concat (file-name-sans-extension id-file)
  701. org-export-docbook-extension))
  702. (setq rpl (format "<link xlink:href=\"%s#%s\">%s</link>"
  703. id-file path (org-export-docbook-format-desc desc))))
  704. ((member type '("http" "https"))
  705. ;; Standard URL, just check if we need to inline an image
  706. (if (and (or (eq t org-export-docbook-inline-images)
  707. (and org-export-docbook-inline-images (not descp)))
  708. (org-file-image-p
  709. path org-export-docbook-inline-image-extensions))
  710. (setq rpl (org-export-docbook-format-image
  711. (concat type ":" path)))
  712. (setq link (concat type ":" path))
  713. (setq rpl (format "<link xlink:href=\"%s\">%s</link>"
  714. (org-export-html-format-href link)
  715. (org-export-docbook-format-desc desc)))
  716. ))
  717. ((member type '("ftp" "mailto" "news"))
  718. ;; Standard URL
  719. (setq link (concat type ":" path))
  720. (setq rpl (format "<link xlink:href=\"%s\">%s</link>"
  721. (org-export-html-format-href link)
  722. (org-export-docbook-format-desc desc))))
  723. ((string= type "coderef")
  724. (setq rpl (format (org-export-get-coderef-format path (and descp desc))
  725. (cdr (assoc path org-export-code-refs)))))
  726. ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
  727. ;; The link protocol has a function for format the link
  728. (setq rpl
  729. (save-match-data
  730. (funcall fnc (org-link-unescape path) desc1 'html))))
  731. ((string= type "file")
  732. ;; FILE link
  733. (let* ((filename path)
  734. (abs-p (file-name-absolute-p filename))
  735. thefile file-is-image-p search)
  736. (save-match-data
  737. (if (string-match "::\\(.*\\)" filename)
  738. (setq search (match-string 1 filename)
  739. filename (replace-match "" t nil filename)))
  740. (setq valid
  741. (if (functionp link-validate)
  742. (funcall link-validate filename current-dir)
  743. t))
  744. (setq file-is-image-p
  745. (org-file-image-p
  746. filename org-export-docbook-inline-image-extensions))
  747. (setq thefile (if abs-p (expand-file-name filename) filename))
  748. ;; Carry over the properties (expand-file-name will
  749. ;; discard the properties of filename)
  750. (add-text-properties 0 (1- (length thefile))
  751. (list 'org-caption caption
  752. 'org-attributes attr
  753. 'org-label label)
  754. thefile)
  755. (when (and org-export-docbook-link-org-files-as-docbook
  756. (string-match "\\.org$" thefile))
  757. (setq thefile (concat (substring thefile 0
  758. (match-beginning 0))
  759. org-export-docbook-extension))
  760. (if (and search
  761. ;; make sure this is can be used as target search
  762. (not (string-match "^[0-9]*$" search))
  763. (not (string-match "^\\*" search))
  764. (not (string-match "^/.*/$" search)))
  765. (setq thefile (concat thefile "#"
  766. (org-solidify-link-text
  767. (org-link-unescape search)))))
  768. (when (string-match "^file:" desc)
  769. (setq desc (replace-match "" t t desc))
  770. (if (string-match "\\.org$" desc)
  771. (setq desc (replace-match "" t t desc))))))
  772. (setq rpl (if (and file-is-image-p
  773. (or (eq t org-export-docbook-inline-images)
  774. (and org-export-docbook-inline-images
  775. (not descp))))
  776. (progn
  777. (message "image %s %s" thefile org-docbook-para-open)
  778. (org-export-docbook-format-image thefile))
  779. (format "<link xlink:href=\"%s\">%s</link>"
  780. thefile (org-export-docbook-format-desc desc))))
  781. (if (not valid) (setq rpl desc))))
  782. (t
  783. ;; Just publish the path, as default
  784. (setq rpl (concat "&lt;" type ":"
  785. (save-match-data (org-link-unescape path))
  786. "&gt;"))))
  787. (setq line (replace-match rpl t t line)
  788. start (+ start (length rpl))))
  789. ;; TODO items: can we do something better?!
  790. (if (and (string-match org-todo-line-regexp line)
  791. (match-beginning 2))
  792. (setq line
  793. (concat (substring line 0 (match-beginning 2))
  794. "[" (match-string 2 line) "]"
  795. (substring line (match-end 2)))))
  796. ;; Does this contain a reference to a footnote?
  797. (when org-export-with-footnotes
  798. (setq start 0)
  799. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  800. (if (get-text-property (match-beginning 2) 'org-protected line)
  801. (setq start (match-end 2))
  802. (let ((num (match-string 2 line)))
  803. (if (assoc num footref-seen)
  804. (setq line (replace-match
  805. (format "%s<footnoteref linkend=\"%s%s\"/>"
  806. (match-string 1 line)
  807. org-export-docbook-footnote-id-prefix num)
  808. t t line))
  809. (setq line (replace-match
  810. (format "%s<footnote xml:id=\"%s%s\"><para>%s</para></footnote>"
  811. (match-string 1 line)
  812. org-export-docbook-footnote-id-prefix
  813. num
  814. (save-match-data
  815. (org-docbook-expand
  816. (cdr (assoc num footnote-list)))))
  817. t t line))
  818. (push (cons num 1) footref-seen))))))
  819. (cond
  820. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  821. ;; This is a headline
  822. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
  823. level-offset))
  824. txt (match-string 2 line))
  825. (if (string-match quote-re0 txt)
  826. (setq txt (replace-match "" t t txt)))
  827. (when in-local-list
  828. ;; Close any local lists before inserting a new header line
  829. (while local-list-type
  830. (let ((listtype (car local-list-type)))
  831. (org-export-docbook-close-li listtype)
  832. (insert (cond
  833. ((equal listtype "o") "</orderedlist>\n")
  834. ((equal listtype "u") "</itemizedlist>\n")
  835. ((equal listtype "d") "</variablelist>\n"))))
  836. (pop local-list-type))
  837. (setq local-list-indent nil
  838. in-local-list nil))
  839. (org-export-docbook-level-start level txt)
  840. ;; QUOTES
  841. (when (string-match quote-re line)
  842. (org-export-docbook-close-para-maybe)
  843. (insert "<programlisting><![CDATA[")
  844. (setq inquote t)))
  845. ;; Tables: since version 4.3 of DocBook DTD, HTML tables are
  846. ;; supported. We can use existing HTML table exporter code
  847. ;; here.
  848. ((and org-export-with-tables
  849. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  850. (if (not table-open)
  851. ;; New table starts
  852. (setq table-open t
  853. table-buffer nil
  854. table-orig-buffer nil))
  855. ;; Accumulate lines
  856. (setq table-buffer (cons line table-buffer)
  857. table-orig-buffer (cons origline table-orig-buffer))
  858. (when (or (not lines)
  859. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  860. (car lines))))
  861. (setq table-open nil
  862. table-buffer (nreverse table-buffer)
  863. table-orig-buffer (nreverse table-orig-buffer))
  864. (org-export-docbook-close-para-maybe)
  865. (insert (org-export-docbook-finalize-table
  866. (org-format-table-html table-buffer table-orig-buffer)))))
  867. (t
  868. ;; Normal lines
  869. (when (string-match
  870. (cond
  871. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  872. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  873. ((= llt ?\)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  874. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  875. line)
  876. (setq ind (org-get-string-indentation line)
  877. item-type (if (match-beginning 4) "o" "u")
  878. starter (if (match-beginning 2)
  879. (substring (match-string 2 line) 0 -1))
  880. line (substring line (match-beginning 5))
  881. item-tag nil)
  882. (if (and starter (string-match "\\(.*?\\) ::[ \t]*" line))
  883. (setq item-type "d"
  884. item-tag (match-string 1 line)
  885. line (substring line (match-end 0))))
  886. (when (and (not (equal item-type "d"))
  887. (not (string-match "[^ \t]" line)))
  888. ;; Empty line. Pretend indentation is large.
  889. (setq ind (if org-empty-line-terminates-plain-lists
  890. 0
  891. (1+ (or (car local-list-indent) 1)))))
  892. (setq didclose nil)
  893. (while (and in-local-list
  894. (or (and (= ind (car local-list-indent))
  895. (not starter))
  896. (< ind (car local-list-indent))))
  897. (setq didclose t)
  898. (let ((listtype (car local-list-type)))
  899. (org-export-docbook-close-li listtype)
  900. (insert (cond
  901. ((equal listtype "o") "</orderedlist>\n")
  902. ((equal listtype "u") "</itemizedlist>\n")
  903. ((equal listtype "d") "</variablelist>\n"))))
  904. (pop local-list-type) (pop local-list-indent)
  905. (setq in-local-list local-list-indent))
  906. (cond
  907. ((and starter
  908. (or (not in-local-list)
  909. (> ind (car local-list-indent))))
  910. ;; Start new (level of) list
  911. (org-export-docbook-close-para-maybe)
  912. (insert (cond
  913. ((equal item-type "u") "<itemizedlist>\n<listitem>\n")
  914. ((equal item-type "o") "<orderedlist>\n<listitem>\n")
  915. ((equal item-type "d")
  916. (format "<variablelist>\n<varlistentry><term>%s</term><listitem>\n" item-tag))))
  917. ;; For DocBook, we need to open a para right after tag
  918. ;; <listitem>.
  919. (org-export-docbook-open-para)
  920. (push item-type local-list-type)
  921. (push ind local-list-indent)
  922. (setq in-local-list t))
  923. (starter
  924. ;; Continue current list
  925. (let ((listtype (car local-list-type)))
  926. (org-export-docbook-close-li listtype)
  927. (insert (cond
  928. ((equal listtype "o") "<listitem>")
  929. ((equal listtype "u") "<listitem>")
  930. ((equal listtype "d") (format
  931. "<varlistentry><term>%s</term><listitem>"
  932. (or item-tag
  933. "???"))))))
  934. ;; For DocBook, we need to open a para right after tag
  935. ;; <listitem>.
  936. (org-export-docbook-open-para))
  937. (didclose
  938. ;; We did close a list, normal text follows: need <para>
  939. (org-export-docbook-open-para)))
  940. ;; Checkboxes.
  941. (if (string-match "^[ \t]*\\(\\[[X -]\\]\\)" line)
  942. (setq line
  943. (replace-match (concat checkbox-start
  944. (match-string 1 line)
  945. checkbox-end)
  946. t t line))))
  947. ;; Empty lines start a new paragraph. If hand-formatted lists
  948. ;; are not fully interpreted, lines starting with "-", "+", "*"
  949. ;; also start a new paragraph.
  950. (if (and (string-match "^ [-+*]-\\|^[ \t]*$" line)
  951. (not inverse))
  952. (org-export-docbook-open-para))
  953. ;; Is this the start of a footnote?
  954. (when org-export-with-footnotes
  955. (when (and (boundp 'footnote-section-tag-regexp)
  956. (string-match (concat "^" footnote-section-tag-regexp)
  957. line))
  958. ;; ignore this line
  959. (throw 'nextline nil))
  960. ;; These footnote lines have been read and saved before,
  961. ;; ignore them at this time.
  962. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  963. (org-export-docbook-close-para-maybe)
  964. (throw 'nextline nil)))
  965. ;; FIXME: It might be a good idea to add an option to
  966. ;; support line break processing instruction <?linebreak?>.
  967. ;; Org-mode supports line break "\\" in HTML exporter, and
  968. ;; some DocBook users may also want to force line breaks
  969. ;; even though DocBook only supports that in
  970. ;; <literallayout>.
  971. (insert line "\n")))))
  972. ;; Properly close all local lists and other lists
  973. (when inquote
  974. (insert "]]>\n</programlisting>\n")
  975. (org-export-docbook-open-para))
  976. (when in-local-list
  977. ;; Close any local lists before inserting a new header line
  978. (while local-list-type
  979. (let ((listtype (car local-list-type)))
  980. (org-export-docbook-close-li listtype)
  981. (insert (cond
  982. ((equal listtype "o") "</orderedlist>\n")
  983. ((equal listtype "u") "</itemizedlist>\n")
  984. ((equal listtype "d") "</variablelist>\n"))))
  985. (pop local-list-type))
  986. (setq local-list-indent nil
  987. in-local-list nil))
  988. ;; Close all open sections.
  989. (org-export-docbook-level-start 1 nil)
  990. (unless (plist-get opt-plist :buffer-will-be-killed)
  991. (normal-mode)
  992. (if (eq major-mode default-major-mode)
  993. (nxml-mode)))
  994. ;; Remove empty paragraphs and lists. Replace them with a
  995. ;; newline.
  996. (goto-char (point-min))
  997. (while (re-search-forward
  998. "[ \r\n\t]*\\(<para>\\)[ \r\n\t]*</para>[ \r\n\t]*" nil t)
  999. (when (not (get-text-property (match-beginning 1) 'org-protected))
  1000. (replace-match "\n")
  1001. (backward-char 1)))
  1002. ;; Fill empty sections with <para></para>. This is to make sure
  1003. ;; that the DocBook document generated is valid and well-formed.
  1004. (goto-char (point-min))
  1005. (while (re-search-forward
  1006. "</title>\\([ \r\n\t]*\\)</section>" nil t)
  1007. (when (not (get-text-property (match-beginning 0) 'org-protected))
  1008. (replace-match "\n<para></para>\n" nil nil nil 1)))
  1009. ;; Insert the last closing tag.
  1010. (goto-char (point-max))
  1011. (unless body-only
  1012. (insert "</article>"))
  1013. (or to-buffer (save-buffer))
  1014. (goto-char (point-min))
  1015. (message "Exporting... done")
  1016. (if (eq to-buffer 'string)
  1017. (prog1 (buffer-substring (point-min) (point-max))
  1018. (kill-buffer (current-buffer)))
  1019. (current-buffer)))))
  1020. (defun org-export-docbook-open-para ()
  1021. "Insert <para>, but first close previous paragraph if any."
  1022. (org-export-docbook-close-para-maybe)
  1023. (insert "\n<para>")
  1024. (setq org-docbook-para-open t))
  1025. (defun org-export-docbook-close-para-maybe ()
  1026. "Close DocBook paragraph if there is one open."
  1027. (when org-docbook-para-open
  1028. (insert "</para>\n")
  1029. (setq org-docbook-para-open nil)))
  1030. (defun org-export-docbook-close-li (&optional type)
  1031. "Close list if necessary."
  1032. (org-export-docbook-close-para-maybe)
  1033. (if (equal type "d")
  1034. (insert "</listitem></varlistentry>\n")
  1035. (insert "</listitem>\n")))
  1036. (defun org-export-docbook-level-start (level title)
  1037. "Insert a new level in DocBook export.
  1038. When TITLE is nil, just close all open levels."
  1039. (org-export-docbook-close-para-maybe)
  1040. (let* ((target (and title (org-get-text-property-any 0 'target title)))
  1041. (l org-level-max)
  1042. section-number)
  1043. (while (>= l level)
  1044. (if (aref org-levels-open (1- l))
  1045. (progn
  1046. (insert "</section>\n")
  1047. (aset org-levels-open (1- l) nil)))
  1048. (setq l (1- l)))
  1049. (when title
  1050. ;; If title is nil, this means this function is called to close
  1051. ;; all levels, so the rest is done only if title is given.
  1052. ;;
  1053. ;; Format tags: put them into a superscript like format.
  1054. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  1055. (setq title
  1056. (replace-match
  1057. (if org-export-with-tags
  1058. (save-match-data
  1059. (concat
  1060. "<superscript>"
  1061. (match-string 1 title)
  1062. "</superscript>"))
  1063. "")
  1064. t t title)))
  1065. (aset org-levels-open (1- level) t)
  1066. (setq section-number (org-section-number level))
  1067. (insert (format "\n<section xml:id=\"%s%s\">\n<title>%s</title>"
  1068. org-export-docbook-section-id-prefix
  1069. section-number title))
  1070. (org-export-docbook-open-para))))
  1071. (defun org-docbook-expand (string)
  1072. "Prepare STRING for DocBook export.
  1073. Applies all active conversions. If there are links in the
  1074. string, don't modify these."
  1075. (let* ((re (concat org-bracket-link-regexp "\\|"
  1076. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  1077. m s l res)
  1078. (while (setq m (string-match re string))
  1079. (setq s (substring string 0 m)
  1080. l (match-string 0 string)
  1081. string (substring string (match-end 0)))
  1082. (push (org-docbook-do-expand s) res)
  1083. (push l res))
  1084. (push (org-docbook-do-expand string) res)
  1085. (apply 'concat (nreverse res))))
  1086. (defun org-docbook-do-expand (s)
  1087. "Apply all active conversions to translate special ASCII to DocBook."
  1088. (setq s (org-html-protect s))
  1089. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  1090. (setq s (replace-match "<\\1>" t nil s)))
  1091. (if org-export-with-emphasize
  1092. (setq s (org-export-docbook-convert-emphasize s)))
  1093. (if org-export-with-special-strings
  1094. (setq s (org-export-docbook-convert-special-strings s)))
  1095. (if org-export-with-sub-superscripts
  1096. (setq s (org-export-docbook-convert-sub-super s)))
  1097. (if org-export-with-TeX-macros
  1098. (let ((start 0) wd ass)
  1099. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)\\({}\\)?"
  1100. s start))
  1101. (if (get-text-property (match-beginning 0) 'org-protected s)
  1102. (setq start (match-end 0))
  1103. (setq wd (match-string 1 s))
  1104. (if (setq ass (assoc wd org-html-entities))
  1105. (setq s (replace-match (or (cdr ass)
  1106. (concat "&" (car ass) ";"))
  1107. t t s))
  1108. (setq start (+ start (length wd))))))))
  1109. s)
  1110. (defun org-export-docbook-format-desc (desc)
  1111. "Make sure DESC is valid as a description in a link."
  1112. (save-match-data
  1113. (org-docbook-do-expand desc)))
  1114. (defun org-export-docbook-convert-emphasize (string)
  1115. "Apply emphasis for DocBook exporting."
  1116. (let ((s 0) rpl)
  1117. (while (string-match org-emph-re string s)
  1118. (if (not (equal
  1119. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  1120. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  1121. (setq s (match-beginning 0)
  1122. rpl
  1123. (concat
  1124. (match-string 1 string)
  1125. (nth 1 (assoc (match-string 3 string)
  1126. org-export-docbook-emphasis-alist))
  1127. (match-string 4 string)
  1128. (nth 2 (assoc (match-string 3 string)
  1129. org-export-docbook-emphasis-alist))
  1130. (match-string 5 string))
  1131. string (replace-match rpl t t string)
  1132. s (+ s (- (length rpl) 2)))
  1133. (setq s (1+ s))))
  1134. string))
  1135. (defun org-docbook-protect (string)
  1136. (org-html-protect string))
  1137. ;; For now, simply return string as it is.
  1138. (defun org-export-docbook-convert-special-strings (string)
  1139. "Convert special characters in STRING to DocBook."
  1140. string)
  1141. (defun org-export-docbook-get-footnotes (lines)
  1142. "Given a list of LINES, return a list of alist footnotes."
  1143. (let ((list nil) line)
  1144. (while (setq line (pop lines))
  1145. (if (string-match "^[ \t]*\\[\\([0-9]+\\)\\] \\(.+\\)" line)
  1146. (push (cons (match-string 1 line) (match-string 2 line))
  1147. list)))
  1148. list))
  1149. (defun org-export-docbook-format-image (src)
  1150. "Create image element in DocBook."
  1151. (save-match-data
  1152. (let* ((caption (org-find-text-property-in-string 'org-caption src))
  1153. (attr (or (org-find-text-property-in-string 'org-attributes src)
  1154. ""))
  1155. (label (org-find-text-property-in-string 'org-label src))
  1156. (default-attr org-export-docbook-default-image-attributes)
  1157. tmp)
  1158. (while (setq tmp (pop default-attr))
  1159. (if (not (string-match (concat (car tmp) "=") attr))
  1160. (setq attr (concat attr " " (car tmp) "=" (cdr tmp)))))
  1161. (format "<mediaobject%s>
  1162. <imageobject>\n<imagedata fileref=\"%s\" %s/>\n</imageobject>
  1163. %s</mediaobject>"
  1164. (if label (concat " xml:id=\"" label "\"") "")
  1165. src attr
  1166. (if caption
  1167. (concat "<caption>\n<para>"
  1168. caption
  1169. "</para>\n</caption>\n")
  1170. "")
  1171. ))))
  1172. (defun org-export-docbook-preprocess (parameters)
  1173. "Extra preprocessing work for DocBook export."
  1174. ;; Merge lines starting with "\par" to one line. Such lines are
  1175. ;; regarded as the continuation of a long footnote.
  1176. (goto-char (point-min))
  1177. (while (re-search-forward "\n\\(\\\\par\\>\\)" nil t)
  1178. (if (not (get-text-property (match-beginning 1) 'org-protected))
  1179. (replace-match ""))))
  1180. (defun org-export-docbook-finalize-table (table)
  1181. "Change TABLE to informaltable if caption does not exist.
  1182. TABLE is a string containing the HTML code generated by
  1183. `org-format-table-html' for a table in Org-mode buffer."
  1184. (if (string-match
  1185. "^<table \\(\\(.\\|\n\\)+\\)<caption></caption>\n\\(\\(.\\|\n\\)+\\)</table>"
  1186. table)
  1187. (replace-match (concat "<informaltable "
  1188. (match-string 1 table)
  1189. (match-string 3 table)
  1190. "</informaltable>")
  1191. nil nil table)
  1192. table))
  1193. ;; Note: This function is very similar to
  1194. ;; org-export-html-convert-sub-super. They can be merged in the future.
  1195. (defun org-export-docbook-convert-sub-super (string)
  1196. "Convert sub- and superscripts in STRING for DocBook."
  1197. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  1198. (while (string-match org-match-substring-regexp string s)
  1199. (cond
  1200. ((and requireb (match-end 8)) (setq s (match-end 2)))
  1201. ((get-text-property (match-beginning 2) 'org-protected string)
  1202. (setq s (match-end 2)))
  1203. (t
  1204. (setq s (match-end 1)
  1205. key (if (string= (match-string 2 string) "_")
  1206. "subscript"
  1207. "superscript")
  1208. c (or (match-string 8 string)
  1209. (match-string 6 string)
  1210. (match-string 5 string))
  1211. string (replace-match
  1212. (concat (match-string 1 string)
  1213. "<" key ">" c "</" key ">")
  1214. t t string)))))
  1215. (while (string-match "\\\\\\([_^]\\)" string)
  1216. (setq string (replace-match (match-string 1 string) t t string)))
  1217. string))
  1218. (provide 'org-docbook)
  1219. ;;; org-docbook.el ends here