org-docbook.el 51 KB

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