org-docbook.el 50 KB

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