org-docbook.el 47 KB

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