org-docbook.el 53 KB

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