org-docbook.el 52 KB

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