org-odt.el 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093
  1. ;;; org-odt.el --- OpenDocumentText export for Org-mode
  2. ;; Copyright (C) 2010-2011 Jambunathan <kjambunathan at gmail dot com>
  3. ;; Author: Jambunathan K <kjambunathan at gmail dot com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.8
  7. ;; This file is not (yet) part of GNU Emacs.
  8. ;; However, it is distributed under the same license.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20. ;;
  21. ;;; Commentary:
  22. ;;; Code:
  23. (eval-when-compile (require 'cl))
  24. (require 'org-lparse)
  25. (defun org-odt-end-export ()
  26. (org-odt-fixup-label-references)
  27. ;; remove empty paragraphs
  28. (goto-char (point-min))
  29. (while (re-search-forward
  30. "<text:p\\( text:style-name=\"Text_20_body\"\\)?>[ \r\n\t]*</text:p>"
  31. nil t)
  32. (replace-match ""))
  33. (goto-char (point-min))
  34. ;; Convert whitespace place holders
  35. (goto-char (point-min))
  36. (let (beg end n)
  37. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  38. (setq n (get-text-property beg 'org-whitespace)
  39. end (next-single-property-change beg 'org-whitespace))
  40. (goto-char beg)
  41. (delete-region beg end)
  42. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  43. (make-string n ?x)))))
  44. ;; Remove empty lines at the beginning of the file.
  45. (goto-char (point-min))
  46. (when (looking-at "\\s-+\n") (replace-match ""))
  47. ;; Remove display properties
  48. (remove-text-properties (point-min) (point-max) '(display t)))
  49. (defvar org-odt-suppress-xref nil)
  50. (defconst org-export-odt-special-string-regexps
  51. '(("\\\\-" . "&#x00ad;\\1") ; shy
  52. ("---\\([^-]\\)" . "&#x2014;\\1") ; mdash
  53. ("--\\([^-]\\)" . "&#x2013;\\1") ; ndash
  54. ("\\.\\.\\." . "&#x2026;")) ; hellip
  55. "Regular expressions for special string conversion.")
  56. (defconst org-odt-lib-dir (file-name-directory load-file-name))
  57. (defconst org-odt-data-dir
  58. (let ((dir1 (expand-file-name "../odt" org-odt-lib-dir)) ; git
  59. (dir2 (expand-file-name "./contrib/odt" org-odt-lib-dir))) ; elpa
  60. (cond
  61. ((file-directory-p dir1) dir1)
  62. ((file-directory-p dir2) dir2)
  63. (t (error "Cannot find factory styles file. Check package dir layout"))))
  64. "Directory that holds auxiliary files used by the ODT exporter.
  65. The 'styles' subdir contains the following xml files -
  66. 'OrgOdtStyles.xml' and 'OrgOdtContentTemplate.xml' - which are
  67. used as factory settings of `org-export-odt-styles-file' and
  68. `org-export-odt-content-template-file'.
  69. The 'etc/schema' subdir contains rnc files for validating of
  70. OpenDocument xml files.")
  71. (defvar org-odt-file-extensions
  72. '(("odt" . "OpenDocument Text")
  73. ("ott" . "OpenDocument Text Template")
  74. ("odm" . "OpenDocument Master Document")
  75. ("ods" . "OpenDocument Spreadsheet")
  76. ("ots" . "OpenDocument Spreadsheet Template")
  77. ("odg" . "OpenDocument Drawing (Graphics)")
  78. ("otg" . "OpenDocument Drawing Template")
  79. ("odp" . "OpenDocument Presentation")
  80. ("otp" . "OpenDocument Presentation Template")
  81. ("odi" . "OpenDocument Image")
  82. ("odf" . "OpenDocument Formula")
  83. ("odc" . "OpenDocument Chart")
  84. ("doc" . "Microsoft Text")
  85. ("docx" . "Microsoft Text")
  86. ("xls" . "Microsoft Spreadsheet")
  87. ("xlsx" . "Microsoft Spreadsheet")
  88. ("ppt" . "Microsoft Presentation")
  89. ("pptx" . "Microsoft Presentation")))
  90. (defvar org-odt-ms-file-extensions
  91. '(("doc" . "Microsoft Text")
  92. ("docx" . "Microsoft Text")
  93. ("xls" . "Microsoft Spreadsheet")
  94. ("xlsx" . "Microsoft Spreadsheet")
  95. ("ppt" . "Microsoft Presentation")
  96. ("pptx" . "Microsoft Presentation")))
  97. ;; RelaxNG validation of OpenDocument xml files
  98. (eval-after-load 'rng-nxml
  99. '(setq rng-nxml-auto-validate-flag t))
  100. (eval-after-load 'rng-loc
  101. '(add-to-list 'rng-schema-locating-files
  102. (expand-file-name "etc/schema/schemas.xml" org-odt-data-dir)))
  103. (mapc
  104. (lambda (desc)
  105. ;; Let Org open all OpenDocument files using system-registered app
  106. (add-to-list 'org-file-apps
  107. (cons (concat "\\." (car desc) "\\'") 'system))
  108. ;; Let Emacs open all OpenDocument files in archive mode
  109. (add-to-list 'auto-mode-alist
  110. (cons (concat "\\." (car desc) "\\'") 'archive-mode)))
  111. org-odt-file-extensions)
  112. (mapc
  113. (lambda (desc)
  114. ;; Let Org open all Microsoft files using system-registered app
  115. (add-to-list 'org-file-apps
  116. (cons (concat "\\." (car desc) "\\'") 'system)))
  117. org-odt-ms-file-extensions)
  118. ;; register the odt exporter with the pre-processor
  119. (add-to-list 'org-export-backends 'odt)
  120. ;; register the odt exporter with org-lparse library
  121. (org-lparse-register-backend 'odt)
  122. (defun org-odt-unload-function ()
  123. (org-lparse-unregister-backend 'odt)
  124. (remove-hook 'org-export-preprocess-after-blockquote-hook
  125. 'org-export-odt-preprocess-latex-fragments)
  126. nil)
  127. (defcustom org-export-odt-content-template-file nil
  128. "Template file for \"content.xml\".
  129. The exporter embeds the exported content just before
  130. \"</office:text>\" element.
  131. If unspecified, the file named \"OrgOdtContentTemplate.xml\"
  132. under `org-odt-data-dir' is used."
  133. :type 'file
  134. :group 'org-export-odt)
  135. (defcustom org-export-odt-styles-file nil
  136. "Default styles file for use with ODT export.
  137. Valid values are one of:
  138. 1. nil
  139. 2. path to a styles.xml file
  140. 3. path to a *.odt or a *.ott file
  141. 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
  142. ...))
  143. In case of option 1, an in-built styles.xml is used. See
  144. `org-odt-data-dir' for more information.
  145. In case of option 3, the specified file is unzipped and the
  146. styles.xml embedded therein is used.
  147. In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
  148. and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
  149. generated odt file. Use relative path for specifying the
  150. FILE-MEMBERS. styles.xml must be specified as one of the
  151. FILE-MEMBERS.
  152. Use options 1, 2 or 3 only if styles.xml alone suffices for
  153. achieving the desired formatting. Use option 4, if the styles.xml
  154. references additional files like header and footer images for
  155. achieving the desired formattting."
  156. :group 'org-export-odt
  157. :type
  158. '(choice
  159. (const :tag "Factory settings" nil)
  160. (file :must-match t :tag "styles.xml")
  161. (file :must-match t :tag "ODT or OTT file")
  162. (list :tag "ODT or OTT file + Members"
  163. (file :must-match t :tag "ODF Text or Text Template file")
  164. (cons :tag "Members"
  165. (file :tag " Member" "styles.xml")
  166. (repeat (file :tag "Member"))))))
  167. (defconst org-export-odt-tmpdir-prefix "odt-")
  168. (defconst org-export-odt-bookmark-prefix "OrgXref.")
  169. (defcustom org-export-odt-use-bookmarks-for-internal-links t
  170. "Export Internal links as bookmarks?."
  171. :type 'boolean
  172. :group 'org-export-odt)
  173. (defcustom org-export-odt-embed-images t
  174. "Should the images be copied in to the odt file or just linked?"
  175. :type 'boolean
  176. :group 'org-export-odt)
  177. (defcustom org-odt-export-inline-images 'maybe
  178. "Non-nil means inline images into exported HTML pages.
  179. This is done using an <img> tag. When nil, an anchor with href is used to
  180. link to the image. If this option is `maybe', then images in links with
  181. an empty description will be inlined, while images with a description will
  182. be linked only."
  183. :group 'org-odt-export
  184. :type '(choice (const :tag "Never" nil)
  185. (const :tag "Always" t)
  186. (const :tag "When there is no description" maybe)))
  187. (defcustom org-odt-export-inline-image-extensions
  188. '("png" "jpeg" "jpg" "gif")
  189. "Extensions of image files that can be inlined into HTML."
  190. :type '(repeat (string :tag "Extension"))
  191. :group 'org-odt-export)
  192. (defcustom org-export-odt-pixels-per-inch display-pixels-per-inch
  193. ;; FIXME add docstring
  194. ""
  195. :type 'float
  196. :group 'org-export-odt)
  197. (defvar org-export-odt-default-org-styles-alist
  198. '((paragraph . ((default . "Text_20_body")
  199. (fixedwidth . "OrgFixedWidthBlock")
  200. (verse . "OrgVerse")
  201. (quote . "Quotations")
  202. (blockquote . "Quotations")
  203. (center . "OrgCenter")
  204. (left . "OrgLeft")
  205. (right . "OrgRight")
  206. (title . "Heading_20_1.title")
  207. (footnote . "Footnote")
  208. (src . "OrgSrcBlock")
  209. (illustration . "Illustration")
  210. (table . "Table")
  211. (definition-term . "Text_20_body_20_bold")
  212. (horizontal-line . "Horizontal_20_Line")))
  213. (character . ((bold . "Bold")
  214. (emphasis . "Emphasis")
  215. (code . "OrgCode")
  216. (verbatim . "OrgCode")
  217. (strike . "Strikethrough")
  218. (underline . "Underline")
  219. (subscript . "OrgSubscript")
  220. (superscript . "OrgSuperscript")))
  221. (list . ((ordered . "OrgNumberedList")
  222. (unordered . "OrgBulletedList")
  223. (description . "OrgDescriptionList"))))
  224. "Default styles for various entities.")
  225. (defvar org-export-odt-org-styles-alist org-export-odt-default-org-styles-alist)
  226. (defun org-odt-get-style-name-for-entity (category &optional entity)
  227. (let ((entity (or entity 'default)))
  228. (or
  229. (cdr (assoc entity (cdr (assoc category
  230. org-export-odt-org-styles-alist))))
  231. (cdr (assoc entity (cdr (assoc category
  232. org-export-odt-default-org-styles-alist))))
  233. (error "Cannot determine style name for entity %s of type %s"
  234. entity category))))
  235. (defcustom org-export-odt-preferred-output-format nil
  236. "Automatically post-process to this format after exporting to \"odt\".
  237. Interactive commands `org-export-as-odt' and
  238. `org-export-as-odt-and-open' export first to \"odt\" format and
  239. then use an external converter to convert the resulting document
  240. to this format.
  241. The converter used is that specified with CONVERT-METHOD option
  242. in `org-odt-get'. If the above option is unspecified then
  243. `org-lparse-convert-process' is used.
  244. The format specified here should be listed in OTHER-BACKENDS
  245. option of `org-odt-get' or `org-lparse-convert-capabilities' as
  246. appropriate."
  247. :group 'org-odt
  248. :type '(choice :convert-widget
  249. (lambda (w)
  250. (apply 'widget-convert (widget-type w)
  251. (eval (car (widget-get w :args)))))
  252. `((const :tag "None" nil)
  253. ,@(mapcar (lambda (c)
  254. `(const :tag ,(car c) ,(car c)))
  255. (org-lparse-get-other-backends "odt")))))
  256. ;;;###autoload
  257. (defun org-export-as-odt-and-open (arg)
  258. "Export the outline as ODT and immediately open it with a browser.
  259. If there is an active region, export only the region.
  260. The prefix ARG specifies how many levels of the outline should become
  261. headlines. The default is 3. Lower levels will become bulleted lists."
  262. (interactive "P")
  263. (org-lparse-and-open
  264. (or org-export-odt-preferred-output-format "odt") "odt" arg))
  265. ;;;###autoload
  266. (defun org-export-as-odt-batch ()
  267. "Call the function `org-lparse-batch'.
  268. This function can be used in batch processing as:
  269. emacs --batch
  270. --load=$HOME/lib/emacs/org.el
  271. --eval \"(setq org-export-headline-levels 2)\"
  272. --visit=MyFile --funcall org-export-as-odt-batch"
  273. (org-lparse-batch "odt"))
  274. ;;;###autoload
  275. (defun org-export-as-odt-to-buffer (arg)
  276. "Call `org-lparse-odt` with output to a temporary buffer.
  277. No file is created. The prefix ARG is passed through to `org-lparse-to-buffer'."
  278. (interactive "P")
  279. (org-lparse-to-buffer "odt" arg))
  280. ;;;###autoload
  281. (defun org-replace-region-by-odt (beg end)
  282. "Assume the current region has org-mode syntax, and convert it to ODT.
  283. This can be used in any buffer. For example, you could write an
  284. itemized list in org-mode syntax in an ODT buffer and then use this
  285. command to convert it."
  286. (interactive "r")
  287. (org-replace-region-by "odt" beg end))
  288. ;;;###autoload
  289. (defun org-export-region-as-odt (beg end &optional body-only buffer)
  290. "Convert region from BEG to END in org-mode buffer to ODT.
  291. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  292. contents, and only produce the region of converted text, useful for
  293. cut-and-paste operations.
  294. If BUFFER is a buffer or a string, use/create that buffer as a target
  295. of the converted ODT. If BUFFER is the symbol `string', return the
  296. produced ODT as a string and leave not buffer behind. For example,
  297. a Lisp program could call this function in the following way:
  298. (setq odt (org-export-region-as-odt beg end t 'string))
  299. When called interactively, the output buffer is selected, and shown
  300. in a window. A non-interactive call will only return the buffer."
  301. (interactive "r\nP")
  302. (org-lparse-region "odt" beg end body-only buffer))
  303. ;;; org-export-as-odt
  304. ;;;###autoload
  305. (defun org-export-as-odt (arg &optional hidden ext-plist
  306. to-buffer body-only pub-dir)
  307. "Export the outline as a OpenDocumentText file.
  308. If there is an active region, export only the region. The prefix
  309. ARG specifies how many levels of the outline should become
  310. headlines. The default is 3. Lower levels will become bulleted
  311. lists. HIDDEN is obsolete and does nothing.
  312. EXT-PLIST is a property list with external parameters overriding
  313. org-mode's default settings, but still inferior to file-local
  314. settings. When TO-BUFFER is non-nil, create a buffer with that
  315. name and export to that buffer. If TO-BUFFER is the symbol
  316. `string', don't leave any buffer behind but just return the
  317. resulting XML as a string. When BODY-ONLY is set, don't produce
  318. the file header and footer, simply return the content of
  319. <body>...</body>, without even the body tags themselves. When
  320. PUB-DIR is set, use this as the publishing directory."
  321. (interactive "P")
  322. (org-lparse (or org-export-odt-preferred-output-format "odt")
  323. "odt" arg hidden ext-plist to-buffer body-only pub-dir))
  324. (defvar org-odt-entity-control-callbacks-alist
  325. `((EXPORT
  326. . (org-odt-begin-export org-odt-end-export))
  327. (DOCUMENT-CONTENT
  328. . (org-odt-begin-document-content org-odt-end-document-content))
  329. (DOCUMENT-BODY
  330. . (org-odt-begin-document-body org-odt-end-document-body))
  331. (TOC
  332. . (org-odt-begin-toc org-odt-end-toc))
  333. (ENVIRONMENT
  334. . (org-odt-begin-environment org-odt-end-environment))
  335. (FOOTNOTE-DEFINITION
  336. . (org-odt-begin-footnote-definition org-odt-end-footnote-definition))
  337. (TABLE
  338. . (org-odt-begin-table org-odt-end-table))
  339. (TABLE-ROWGROUP
  340. . (org-odt-begin-table-rowgroup org-odt-end-table-rowgroup))
  341. (LIST
  342. . (org-odt-begin-list org-odt-end-list))
  343. (LIST-ITEM
  344. . (org-odt-begin-list-item org-odt-end-list-item))
  345. (OUTLINE
  346. . (org-odt-begin-outline org-odt-end-outline))
  347. (OUTLINE-TEXT
  348. . (org-odt-begin-outline-text org-odt-end-outline-text))
  349. (PARAGRAPH
  350. . (org-odt-begin-paragraph org-odt-end-paragraph)))
  351. "")
  352. (defvar org-odt-entity-format-callbacks-alist
  353. `((EXTRA-TARGETS . org-lparse-format-extra-targets)
  354. (ORG-TAGS . org-lparse-format-org-tags)
  355. (SECTION-NUMBER . org-lparse-format-section-number)
  356. (HEADLINE . org-odt-format-headline)
  357. (TOC-ENTRY . org-odt-format-toc-entry)
  358. (TOC-ITEM . org-odt-format-toc-item)
  359. (TAGS . org-odt-format-tags)
  360. (SPACES . org-odt-format-spaces)
  361. (TABS . org-odt-format-tabs)
  362. (LINE-BREAK . org-odt-format-line-break)
  363. (FONTIFY . org-odt-format-fontify)
  364. (TODO . org-lparse-format-todo)
  365. (LINK . org-odt-format-link)
  366. (INLINE-IMAGE . org-odt-format-inline-image)
  367. (ORG-LINK . org-odt-format-org-link)
  368. (HEADING . org-odt-format-heading)
  369. (ANCHOR . org-odt-format-anchor)
  370. (TABLE . org-lparse-format-table)
  371. (TABLE-ROW . org-odt-format-table-row)
  372. (TABLE-CELL . org-odt-format-table-cell)
  373. (FOOTNOTES-SECTION . ignore)
  374. (FOOTNOTE-REFERENCE . org-odt-format-footnote-reference)
  375. (HORIZONTAL-LINE . org-odt-format-horizontal-line)
  376. (COMMENT . org-odt-format-comment)
  377. (LINE . org-odt-format-line)
  378. (ORG-ENTITY . org-odt-format-org-entity))
  379. "")
  380. ;;;_. callbacks
  381. ;;;_. control callbacks
  382. ;;;_ , document body
  383. (defun org-odt-begin-office-body ()
  384. ;; automatic styles
  385. (insert-file-contents
  386. (or org-export-odt-content-template-file
  387. (expand-file-name "styles/OrgOdtContentTemplate.xml"
  388. org-odt-data-dir)))
  389. (goto-char (point-min))
  390. (re-search-forward "</office:text>" nil nil)
  391. (delete-region (match-beginning 0) (point-max)))
  392. ;; Following variable is let bound when `org-do-lparse' is in
  393. ;; progress. See org-html.el.
  394. (defvar org-lparse-toc)
  395. (defun org-odt-begin-document-body (opt-plist)
  396. (org-odt-begin-office-body)
  397. (let ((title (plist-get opt-plist :title)))
  398. (when title
  399. (insert
  400. (org-odt-format-stylized-paragraph 'title title))))
  401. ;; insert toc
  402. (when org-lparse-toc
  403. (insert "\n" org-lparse-toc "\n")))
  404. (defvar org-lparse-body-only) ; let bound during org-do-lparse
  405. (defvar org-lparse-to-buffer) ; let bound during org-do-lparse
  406. (defun org-odt-end-document-body (opt-plist)
  407. (unless org-lparse-body-only
  408. (org-lparse-insert-tag "</office:text>")
  409. (org-lparse-insert-tag "</office:body>")))
  410. (defun org-odt-begin-document-content (opt-plist)
  411. (ignore))
  412. (defun org-odt-end-document-content ()
  413. (org-lparse-insert-tag "</office:document-content>"))
  414. (defun org-odt-begin-outline (level1 snumber title tags
  415. target extra-targets class)
  416. (org-lparse-insert
  417. 'HEADING (org-lparse-format
  418. 'HEADLINE title extra-targets tags snumber level1)
  419. level1 target))
  420. (defun org-odt-end-outline ()
  421. (ignore))
  422. (defun org-odt-begin-outline-text (level1 snumber class)
  423. (ignore))
  424. (defun org-odt-end-outline-text ()
  425. (ignore))
  426. (defun org-odt-begin-paragraph (&optional style)
  427. (org-lparse-insert-tag
  428. "<text:p%s>" (org-odt-get-extra-attrs-for-paragraph-style style)))
  429. (defun org-odt-end-paragraph ()
  430. (org-lparse-insert-tag "</text:p>"))
  431. (defun org-odt-get-extra-attrs-for-paragraph-style (style)
  432. (let (style-name)
  433. (setq style-name
  434. (cond
  435. ((stringp style) style)
  436. ((symbolp style) (org-odt-get-style-name-for-entity
  437. 'paragraph style))))
  438. (unless style-name
  439. (error "Don't know how to handle paragraph style %s" style))
  440. (format " text:style-name=\"%s\"" style-name)))
  441. (defun org-odt-format-stylized-paragraph (style text)
  442. (org-odt-format-tags
  443. '("<text:p%s>" . "</text:p>") text
  444. (org-odt-get-extra-attrs-for-paragraph-style style)))
  445. (defun org-odt-begin-environment (style)
  446. (case style
  447. ((blockquote verse center quote)
  448. (org-lparse-begin-paragraph style)
  449. (list))
  450. ((fixedwidth native)
  451. (org-lparse-end-paragraph)
  452. (list))
  453. (t (error "Unknown environment %s" style))))
  454. (defun org-odt-end-environment (style)
  455. (case style
  456. ((blockquote verse center quote)
  457. (org-lparse-end-paragraph)
  458. (list))
  459. ((fixedwidth native)
  460. (org-lparse-begin-paragraph)
  461. (list))
  462. (t (error "Unknown environment %s" style))))
  463. (defvar org-lparse-list-level) ; dynamically bound in org-do-lparse
  464. (defun org-odt-begin-list (ltype)
  465. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  466. ltype))
  467. (let* ((style-name (org-odt-get-style-name-for-entity 'list ltype))
  468. (extra (concat (when (= org-lparse-list-level 1)
  469. " text:continue-numbering=\"false\"")
  470. (when style-name
  471. (format " text:style-name=\"%s\"" style-name)))))
  472. (case ltype
  473. ((ordered unordered description)
  474. (org-lparse-end-paragraph)
  475. (org-lparse-insert-tag "<text:list%s>" extra))
  476. (t (error "Unknown list type: %s" ltype)))))
  477. (defun org-odt-end-list (ltype)
  478. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  479. ltype))
  480. (if ltype
  481. (org-lparse-insert-tag "</text:list>")
  482. (error "Unknown list type: %s" ltype)))
  483. (defun org-odt-begin-list-item (ltype &optional arg headline)
  484. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  485. ltype))
  486. (case ltype
  487. (ordered
  488. (assert (not headline) t)
  489. (let* ((counter arg) (extra ""))
  490. (org-lparse-insert-tag "<text:list-item>")
  491. (org-lparse-begin-paragraph)))
  492. (unordered
  493. (let* ((id arg) (extra ""))
  494. (org-lparse-insert-tag "<text:list-item>")
  495. (org-lparse-begin-paragraph)
  496. (insert (if headline (org-odt-format-target headline id)
  497. (org-odt-format-bookmark "" id)))))
  498. (description
  499. (assert (not headline) t)
  500. (let ((term (or arg "(no term)")))
  501. (insert
  502. (org-odt-format-tags
  503. '("<text:list-item>" . "</text:list-item>")
  504. (org-odt-format-stylized-paragraph 'definition-term term)))
  505. (org-lparse-begin-list-item 'unordered)
  506. (org-lparse-begin-list 'description)
  507. (org-lparse-begin-list-item 'unordered)))
  508. (t (error "Unknown list type"))))
  509. (defun org-odt-end-list-item (ltype)
  510. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  511. ltype))
  512. (case ltype
  513. ((ordered unordered)
  514. (org-lparse-insert-tag "</text:list-item>"))
  515. (description
  516. (org-lparse-end-list-item-1)
  517. (org-lparse-end-list 'description)
  518. (org-lparse-end-list-item-1))
  519. (t (error "Unknown list type"))))
  520. ;; Following variables are let bound when table emission is in
  521. ;; progress. See org-lparse.el.
  522. (defvar org-lparse-table-begin-marker)
  523. (defvar org-lparse-table-ncols)
  524. (defvar org-lparse-table-rowgrp-open)
  525. (defvar org-lparse-table-rownum)
  526. (defvar org-lparse-table-cur-rowgrp-is-hdr)
  527. (defvar org-lparse-table-is-styled)
  528. (defvar org-lparse-table-rowgrp-info)
  529. (defvar org-lparse-table-colalign-vector)
  530. (defvar org-odt-table-style nil
  531. "Table style specified by \"#+ATTR_ODT: <style-name>\" line.
  532. This is set during `org-odt-begin-table'.")
  533. (defvar org-odt-table-style-spec nil
  534. "Entry for `org-odt-table-style' in `org-export-odt-table-styles'.")
  535. (defcustom org-export-odt-table-styles nil
  536. "Specify how Table Styles should be derived from a Table Template.
  537. This is a list where each element is of the
  538. form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
  539. TABLE-STYLE-NAME is the style associated with the table through
  540. `org-odt-table-style'.
  541. TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
  542. TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
  543. below) that is included in
  544. `org-export-odt-content-template-file'.
  545. TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
  546. \"TableCell\"
  547. PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
  548. \"TableParagraph\"
  549. TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
  550. \"FirstRow\" | \"LastRow\" |
  551. \"EvenRow\" | \"OddRow\" |
  552. \"EvenColumn\" | \"OddColumn\" | \"\"
  553. where \"+\" above denotes string concatenation.
  554. TABLE-CELL-OPTIONS is an alist where each element is of the
  555. form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
  556. TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
  557. `use-last-row-styles' |
  558. `use-first-column-styles' |
  559. `use-last-column-styles' |
  560. `use-banding-rows-styles' |
  561. `use-banding-columns-styles' |
  562. `use-first-row-styles'
  563. ON-OR-OFF := `t' | `nil'
  564. For example, with the following configuration
  565. \(setq org-export-odt-table-styles
  566. '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
  567. \(\(use-first-row-styles . t\)
  568. \(use-first-column-styles . t\)\)\)
  569. \(\"TableWithHeaderColumns\" \"Custom\"
  570. \(\(use-first-column-styles . t\)\)\)\)\)
  571. 1. A table associated with \"TableWithHeaderRowsAndColumns\"
  572. style will use the following table-cell styles -
  573. \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
  574. \"CustomTableCell\" and the following paragraph styles
  575. \"CustomFirstRowTableParagraph\",
  576. \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
  577. as appropriate.
  578. 2. A table associated with \"TableWithHeaderColumns\" style will
  579. use the following table-cell styles -
  580. \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
  581. following paragraph styles
  582. \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
  583. as appropriate..
  584. Note that TABLE-TEMPLATE-NAME corresponds to the
  585. \"<table:table-template>\" elements contained within
  586. \"<office:styles>\". The entries (TABLE-STYLE-NAME
  587. TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
  588. \"table:template-name\" and \"table:use-first-row-styles\" etc
  589. attributes of \"<table:table>\" element. Refer ODF-1.2
  590. specification for more information. Also consult the
  591. implementation filed under `org-odt-get-table-cell-styles'."
  592. :group 'org-export-odt
  593. :type '(choice
  594. (const :tag "None" nil)
  595. (repeat :tag "Table Styles"
  596. (list :tag "Table Style Specification"
  597. (string :tag "Table Style Name")
  598. (string :tag "Table Template Name")
  599. (alist :options (use-first-row-styles
  600. use-last-row-styles
  601. use-first-column-styles
  602. use-last-column-styles
  603. use-banding-rows-styles
  604. use-banding-columns-styles)
  605. :key-type symbol
  606. :value-type (const :tag "True" t))))))
  607. (defun org-odt-begin-table (caption label attributes)
  608. (setq org-odt-table-style attributes)
  609. (setq org-odt-table-style-spec
  610. (assoc org-odt-table-style org-export-odt-table-styles))
  611. (when label
  612. (insert
  613. (org-odt-format-stylized-paragraph
  614. 'table (org-odt-format-entity-caption label caption "Table"))))
  615. (org-lparse-insert-tag
  616. "<table:table table:name=\"%s\" table:style-name=\"%s\">"
  617. (or label "") (or (nth 1 org-odt-table-style-spec) "OrgTable"))
  618. (setq org-lparse-table-begin-marker (point)))
  619. (defun org-odt-end-table ()
  620. (goto-char org-lparse-table-begin-marker)
  621. (loop for level from 0 below org-lparse-table-ncols
  622. do (insert
  623. (org-odt-format-tags
  624. "<table:table-column table:style-name=\"%sColumn\"/>"
  625. "" (or (nth 1 org-odt-table-style-spec) "OrgTable"))))
  626. ;; fill style attributes for table cells
  627. (when org-lparse-table-is-styled
  628. (while (re-search-forward "@@\\(table-cell:p\\|table-cell:style-name\\)@@\\([0-9]+\\)@@\\([0-9]+\\)@@" nil t)
  629. (let* ((spec (match-string 1))
  630. (r (string-to-number (match-string 2)))
  631. (c (string-to-number (match-string 3)))
  632. (cell-styles (org-odt-get-table-cell-styles
  633. r c org-odt-table-style-spec))
  634. (table-cell-style (car cell-styles))
  635. (table-cell-paragraph-style (cdr cell-styles)))
  636. (cond
  637. ((equal spec "table-cell:p")
  638. (replace-match table-cell-paragraph-style t t))
  639. ((equal spec "table-cell:style-name")
  640. (replace-match table-cell-style t t))))))
  641. (goto-char (point-max))
  642. (org-lparse-insert-tag "</table:table>"))
  643. (defun org-odt-begin-table-rowgroup (&optional is-header-row)
  644. (when org-lparse-table-rowgrp-open
  645. (org-lparse-end 'TABLE-ROWGROUP))
  646. (org-lparse-insert-tag (if is-header-row
  647. "<table:table-header-rows>"
  648. "<table:table-rows>"))
  649. (setq org-lparse-table-rowgrp-open t)
  650. (setq org-lparse-table-cur-rowgrp-is-hdr is-header-row))
  651. (defun org-odt-end-table-rowgroup ()
  652. (when org-lparse-table-rowgrp-open
  653. (setq org-lparse-table-rowgrp-open nil)
  654. (org-lparse-insert-tag
  655. (if org-lparse-table-cur-rowgrp-is-hdr
  656. "</table:table-header-rows>" "</table:table-rows>"))))
  657. (defun org-odt-format-table-row (row)
  658. (org-odt-format-tags
  659. '("<table:table-row>" . "</table:table-row>") row))
  660. (defun org-odt-get-table-cell-styles (r c &optional style-spec)
  661. "Retrieve styles applicable to a table cell.
  662. R and C are (zero-based) row and column numbers of the table
  663. cell. STYLE-SPEC is an entry in `org-export-odt-table-styles'
  664. applicable to the current table. It is `nil' if the table is not
  665. associated with any style attributes.
  666. Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
  667. When STYLE-SPEC is nil, style the table cell the conventional way
  668. - choose cell borders based on row and column groupings and
  669. choose paragraph alignment based on `org-col-cookies' text
  670. property. See also
  671. `org-odt-get-paragraph-style-cookie-for-table-cell'.
  672. When STYLE-SPEC is non-nil, ignore the above cookie and return
  673. styles congruent with the ODF-1.2 specification."
  674. (cond
  675. (style-spec
  676. ;; LibreOffice - particularly the Writer - honors neither table
  677. ;; templates nor custom table-cell styles. Inorder to retain
  678. ;; inter-operability with LibreOffice, only automatic styles are
  679. ;; used for styling of table-cells. The current implementation is
  680. ;; congruent with ODF-1.2 specification and hence is
  681. ;; future-compatible.
  682. ;; Additional Note: LibreOffice's AutoFormat facility for tables -
  683. ;; which recognizes as many as 16 different cell types - is much
  684. ;; richer. Unfortunately it is NOT amenable to easy configuration
  685. ;; by hand.
  686. (let* ((template-name (nth 1 style-spec))
  687. (cell-style-selectors (nth 2 style-spec))
  688. (cell-type
  689. (cond
  690. ((and (cdr (assoc 'use-first-column-styles cell-style-selectors))
  691. (= c 0)) "FirstColumn")
  692. ((and (cdr (assoc 'use-last-column-styles cell-style-selectors))
  693. (= c (1- org-lparse-table-ncols))) "LastColumn")
  694. ((and (cdr (assoc 'use-first-row-styles cell-style-selectors))
  695. (= r 0)) "FirstRow")
  696. ((and (cdr (assoc 'use-last-row-styles cell-style-selectors))
  697. (= r org-lparse-table-rownum))
  698. "LastRow")
  699. ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
  700. (= (% r 2) 1)) "EvenRow")
  701. ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
  702. (= (% r 2) 0)) "OddRow")
  703. ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
  704. (= (% c 2) 1)) "EvenColumn")
  705. ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
  706. (= (% c 2) 0)) "OddColumn")
  707. (t ""))))
  708. (cons
  709. (concat template-name cell-type "TableCell")
  710. (concat template-name cell-type "TableParagraph"))))
  711. (t
  712. (cons
  713. (concat
  714. "OrgTblCell"
  715. (cond
  716. ((= r 0) "T")
  717. ((eq (cdr (assoc r org-lparse-table-rowgrp-info)) :start) "T")
  718. (t ""))
  719. (when (= r org-lparse-table-rownum) "B")
  720. (cond
  721. ((= c 0) "")
  722. ((or (memq (nth c org-table-colgroup-info) '(:start :startend))
  723. (memq (nth (1- c) org-table-colgroup-info) '(:end :startend))) "L")
  724. (t "")))
  725. (capitalize (aref org-lparse-table-colalign-vector c))))))
  726. (defun org-odt-get-paragraph-style-cookie-for-table-cell (r c)
  727. (concat
  728. (and (not org-odt-table-style-spec)
  729. (cond
  730. (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
  731. ((and (= c 0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS))
  732. "OrgTableHeading")
  733. (t "OrgTableContents")))
  734. (and org-lparse-table-is-styled
  735. (format "@@table-cell:p@@%03d@@%03d@@" r c))))
  736. (defun org-odt-get-style-name-cookie-for-table-cell (r c)
  737. (when org-lparse-table-is-styled
  738. (format "@@table-cell:style-name@@%03d@@%03d@@" r c)))
  739. (defun org-odt-format-table-cell (data r c)
  740. (let* ((paragraph-style-cookie
  741. (org-odt-get-paragraph-style-cookie-for-table-cell r c))
  742. (style-name-cookie
  743. (org-odt-get-style-name-cookie-for-table-cell r c))
  744. (extra (if style-name-cookie
  745. (format " table:style-name=\"%s\"" style-name-cookie) "")))
  746. (org-odt-format-tags
  747. '("<table:table-cell%s>" . "</table:table-cell>")
  748. (if org-lparse-list-table-p data
  749. (org-odt-format-stylized-paragraph paragraph-style-cookie data)) extra)))
  750. (defun org-odt-begin-footnote-definition (n)
  751. (org-lparse-begin-paragraph 'footnote))
  752. (defun org-odt-end-footnote-definition (n)
  753. (org-lparse-end-paragraph))
  754. (defun org-odt-begin-toc (lang-specific-heading)
  755. (insert
  756. (format "
  757. <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
  758. <text:table-of-content-source text:outline-level=\"10\">
  759. <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
  760. " lang-specific-heading))
  761. (loop for level from 1 upto 10
  762. do (insert (format
  763. "
  764. <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
  765. <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
  766. <text:index-entry-chapter/>
  767. <text:index-entry-text/>
  768. <text:index-entry-link-end/>
  769. </text:table-of-content-entry-template>
  770. " level level)))
  771. (insert
  772. (format "
  773. </text:table-of-content-source>
  774. <text:index-body>
  775. <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
  776. <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
  777. </text:index-title>
  778. " lang-specific-heading)))
  779. (defun org-odt-end-toc ()
  780. (insert "
  781. </text:index-body>
  782. </text:table-of-content>
  783. "))
  784. (defun org-odt-format-toc-entry (snumber todo headline tags href)
  785. (setq headline (concat
  786. (and org-export-with-section-numbers
  787. (concat snumber ". "))
  788. headline
  789. (and tags
  790. (concat
  791. (org-lparse-format 'SPACES 3)
  792. (org-lparse-format 'FONTIFY tags "tag")))))
  793. (when todo
  794. (setq headline (org-lparse-format 'FONTIFY headline "todo")))
  795. (let ((org-odt-suppress-xref t))
  796. (org-odt-format-link headline (concat "#" href))))
  797. (defun org-odt-format-toc-item (toc-entry level org-last-level)
  798. (let ((style (format "Contents_20_%d"
  799. (+ level (or (org-lparse-get 'TOPLEVEL-HLEVEL) 1) -1))))
  800. (insert "\n" (org-odt-format-stylized-paragraph style toc-entry) "\n")))
  801. ;; Following variable is let bound during 'ORG-LINK callback. See
  802. ;; org-html.el
  803. (defvar org-lparse-link-description-is-image nil)
  804. (defun org-odt-format-link (desc href &optional attr)
  805. (cond
  806. ((and (= (string-to-char href) ?#) (not org-odt-suppress-xref))
  807. (setq href (concat org-export-odt-bookmark-prefix (substring href 1)))
  808. (let ((xref-format "text"))
  809. (when (numberp desc)
  810. (setq desc (format "%d" desc) xref-format "number"))
  811. (org-odt-format-tags
  812. '("<text:bookmark-ref text:reference-format=\"%s\" text:ref-name=\"%s\">" .
  813. "</text:bookmark-ref>")
  814. desc xref-format href)))
  815. (org-lparse-link-description-is-image
  816. (org-odt-format-tags
  817. '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</draw:a>")
  818. desc href (or attr "")))
  819. (t
  820. (org-odt-format-tags
  821. '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</text:a>")
  822. desc href (or attr "")))))
  823. (defun org-odt-format-spaces (n)
  824. (cond
  825. ((= n 1) " ")
  826. ((> n 1) (concat
  827. " " (org-odt-format-tags "<text:s text:c=\"%d\"/>" "" (1- n))))
  828. (t "")))
  829. (defun org-odt-format-tabs (&optional n)
  830. (let ((tab "<text:tab/>")
  831. (n (or n 1)))
  832. (insert tab)))
  833. (defun org-odt-format-line-break ()
  834. (org-odt-format-tags "<text:line-break/>" ""))
  835. (defun org-odt-format-horizontal-line ()
  836. (org-odt-format-stylized-paragraph 'horizontal-line ""))
  837. (defun org-odt-format-line (line)
  838. (case org-lparse-dyn-current-environment
  839. (fixedwidth (concat
  840. (org-odt-format-stylized-paragraph
  841. 'fixedwidth (org-odt-fill-tabs-and-spaces
  842. (org-xml-encode-plain-text line))) "\n"))
  843. (t (concat line "\n"))))
  844. (defun org-odt-format-comment (fmt &rest args)
  845. (let ((comment (apply 'format fmt args)))
  846. (format "\n<!-- %s -->\n" comment)))
  847. (defun org-odt-format-org-entity (wd)
  848. (org-entity-get-representation wd 'utf8))
  849. (defun org-odt-fill-tabs-and-spaces (line)
  850. (replace-regexp-in-string
  851. "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
  852. (cond
  853. ((string= s "\t") (org-odt-format-tabs))
  854. (t (org-odt-format-spaces (length s))))) line))
  855. (defcustom org-export-odt-use-htmlfontify t
  856. "Specify whether or not source blocks need to be fontified.
  857. Turn this option on if you want to colorize the source code
  858. blocks in the exported file. For colorization to work, you need
  859. to make available an enhanced version of `htmlfontify' library."
  860. :type 'boolean
  861. :group 'org-export-odt)
  862. (defun org-odt-format-source-line-with-line-number-and-label
  863. (line rpllbl num fontifier par-style)
  864. (let ((keep-label (not (numberp rpllbl)))
  865. (ref (org-find-text-property-in-string 'org-coderef line)))
  866. (setq line (concat line (and keep-label ref (format "(%s)" ref))))
  867. (setq line (funcall fontifier line))
  868. (when ref
  869. (setq line (org-odt-format-target line (concat "coderef-" ref))))
  870. (setq line (org-odt-format-stylized-paragraph par-style line))
  871. (when num
  872. (org-odt-format-tags '("<text:list-item>" . "</text:list-item>") line))))
  873. (defun org-odt-format-source-code-or-example-plain
  874. (lines lang caption textareap cols rows num cont rpllbl fmt)
  875. "Format source or example blocks much like fixedwidth blocks.
  876. Use this when `org-export-odt-use-htmlfontify' option is turned
  877. off."
  878. (let* ((lines (org-split-string lines "[\r\n]"))
  879. (line-count (length lines))
  880. (i 0))
  881. (mapconcat
  882. (lambda (line)
  883. (incf i)
  884. (org-odt-format-source-line-with-line-number-and-label
  885. line rpllbl num (lambda (line)
  886. (org-odt-fill-tabs-and-spaces
  887. (org-xml-encode-plain-text line)))
  888. (if (= i line-count) "OrgFixedWidthBlockLastLine" "OrgFixedWidthBlock")))
  889. lines "\n")))
  890. (defvar org-src-block-paragraph-format
  891. "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
  892. <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
  893. <style:background-image/>
  894. </style:paragraph-properties>
  895. <style:text-properties fo:color=\"%s\"/>
  896. </style:style>"
  897. "Custom paragraph style for colorized source and example blocks.
  898. This style is much the same as that of \"OrgFixedWidthBlock\"
  899. except that the foreground and background colors are set
  900. according to the default face identified by the `htmlfontify'.")
  901. (defun org-odt-hfy-face-to-css (fn)
  902. "Create custom style for face FN.
  903. When FN is the default face, use it's foreground and background
  904. properties to create \"OrgSrcBlock\" paragraph style. Otherwise
  905. use it's color attribute to create a character style whose name
  906. is obtained from FN. Currently all attributes of FN other than
  907. color are ignored.
  908. The style name for a face FN is derived using the following
  909. operations on the face name in that order - de-dash, CamelCase
  910. and prefix with \"OrgSrc\". For example,
  911. `font-lock-function-name-face' is associated with
  912. \"OrgSrcFontLockFunctionNameFace\"."
  913. (let* ((css-list (hfy-face-to-style fn))
  914. (style-name ((lambda (fn)
  915. (concat "OrgSrc"
  916. (mapconcat
  917. 'capitalize (split-string
  918. (hfy-face-or-def-to-name fn) "-")
  919. ""))) fn))
  920. (color-val (cdr (assoc "color" css-list)))
  921. (background-color-val (cdr (assoc "background" css-list)))
  922. (style (and org-export-odt-create-custom-styles-for-srcblocks
  923. (cond
  924. ((eq fn 'default)
  925. (format org-src-block-paragraph-format
  926. background-color-val color-val))
  927. (t
  928. (format
  929. "
  930. <style:style style:name=\"%s\" style:family=\"text\">
  931. <style:text-properties fo:color=\"%s\"/>
  932. </style:style>" style-name color-val))))))
  933. (cons style-name style)))
  934. (defcustom org-export-odt-create-custom-styles-for-srcblocks t
  935. "Whether custom styles for colorized source blocks be automatically created.
  936. When this option is turned on, the exporter creates custom styles
  937. for source blocks based on the advice of `htmlfontify'. Creation
  938. of custom styles happen as part of `org-odt-hfy-face-to-css'.
  939. When this option is turned off exporter does not create such
  940. styles.
  941. Use the latter option if you do not want the custom styles to be
  942. based on your current display settings. It is necessary that the
  943. styles.xml already contains needed styles for colorizing to work.
  944. This variable is effective only if
  945. `org-export-odt-use-htmlfontify' is turned on."
  946. :group 'org-export-odt
  947. :type 'boolean)
  948. (defun org-odt-insert-custom-styles-for-srcblocks (styles)
  949. "Save STYLES used for colorizing of source blocks.
  950. Update styles.xml with styles that were collected as part of
  951. `org-odt-hfy-face-to-css' callbacks."
  952. (when styles
  953. (with-current-buffer
  954. (find-file-noselect (expand-file-name "styles.xml") t)
  955. (goto-char (point-min))
  956. (when (re-search-forward "</office:styles>" nil t)
  957. (goto-char (match-beginning 0))
  958. (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n")))))
  959. (defun org-odt-format-source-code-or-example-colored
  960. (lines lang caption textareap cols rows num cont rpllbl fmt)
  961. "Format source or example blocks using `htmlfontify-string'.
  962. Use this routine when `org-export-odt-use-htmlfontify' option is
  963. turned on."
  964. (let* ((lang-m (and lang (or (cdr (assoc lang org-src-lang-modes)) lang)))
  965. (mode (and lang-m (intern (concat (if (symbolp lang-m)
  966. (symbol-name lang-m)
  967. lang-m) "-mode"))))
  968. (org-inhibit-startup t)
  969. (org-startup-folded nil)
  970. (lines (with-temp-buffer
  971. (insert lines)
  972. (if (functionp mode) (funcall mode) (fundamental-mode))
  973. (font-lock-fontify-buffer)
  974. (buffer-string)))
  975. (hfy-html-quote-regex "\\([<\"&> ]\\)")
  976. (hfy-html-quote-map '(("\"" "&quot;")
  977. ("<" "&lt;")
  978. ("&" "&amp;")
  979. (">" "&gt;")
  980. (" " "<text:s/>")
  981. (" " "<text:tab/>")))
  982. (hfy-face-to-css 'org-odt-hfy-face-to-css)
  983. (hfy-optimisations-1 (copy-seq hfy-optimisations))
  984. (hfy-optimisations (add-to-list 'hfy-optimisations-1
  985. 'body-text-only))
  986. (hfy-begin-span-handler
  987. (lambda (style text-block text-id text-begins-block-p)
  988. (insert (format "<text:span text:style-name=\"%s\">" style))))
  989. (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
  990. (when (fboundp 'htmlfontify-string)
  991. (let* ((lines (org-split-string lines "[\r\n]"))
  992. (line-count (length lines))
  993. (i 0))
  994. (mapconcat
  995. (lambda (line)
  996. (incf i)
  997. (org-odt-format-source-line-with-line-number-and-label
  998. line rpllbl num 'htmlfontify-string
  999. (if (= i line-count) "OrgSrcBlockLastLine" "OrgSrcBlock")))
  1000. lines "\n")))))
  1001. (defun org-odt-format-source-code-or-example (lines lang caption textareap
  1002. cols rows num cont
  1003. rpllbl fmt)
  1004. "Format source or example blocks for export.
  1005. Use `org-odt-format-source-code-or-example-plain' or
  1006. `org-odt-format-source-code-or-example-colored' depending on the
  1007. value of `org-export-odt-use-htmlfontify."
  1008. (setq lines (org-export-number-lines
  1009. lines 0 0 num cont rpllbl fmt ;; 'preprocess
  1010. )
  1011. lines (funcall
  1012. (or (and org-export-odt-use-htmlfontify
  1013. (or (featurep 'htmlfontify)
  1014. (require 'htmlfontify))
  1015. (fboundp 'htmlfontify-string)
  1016. 'org-odt-format-source-code-or-example-colored)
  1017. 'org-odt-format-source-code-or-example-plain)
  1018. lines lang caption textareap cols rows num cont rpllbl fmt))
  1019. (if (not num) lines
  1020. (let ((extra (format " text:continue-numbering=\"%s\""
  1021. (if cont "true" "false"))))
  1022. (org-odt-format-tags
  1023. '("<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>"
  1024. . "</text:list>") lines extra))))
  1025. (defun org-odt-remap-stylenames (style-name)
  1026. (or
  1027. (cdr (assoc style-name '(("timestamp-wrapper" . "OrgTimestampWrapper")
  1028. ("timestamp" . "OrgTimestamp")
  1029. ("timestamp-kwd" . "OrgTimestampKeyword")
  1030. ("tag" . "OrgTag")
  1031. ("todo" . "OrgTodo")
  1032. ("done" . "OrgDone")
  1033. ("target" . "OrgTarget"))))
  1034. style-name))
  1035. (defun org-odt-format-fontify (text style &optional id)
  1036. (let* ((style-name
  1037. (cond
  1038. ((stringp style)
  1039. (org-odt-remap-stylenames style))
  1040. ((symbolp style)
  1041. (org-odt-get-style-name-for-entity 'character style))
  1042. ((listp style)
  1043. (assert (< 1 (length style)))
  1044. (let ((parent-style (pop style)))
  1045. (mapconcat (lambda (s)
  1046. ;; (assert (stringp s) t)
  1047. (org-odt-remap-stylenames s)) style "")
  1048. (org-odt-remap-stylenames parent-style)))
  1049. (t (error "Don't how to handle style %s" style)))))
  1050. (org-odt-format-tags
  1051. '("<text:span text:style-name=\"%s\">" . "</text:span>")
  1052. text style-name)))
  1053. (defun org-odt-relocate-relative-path (path dir)
  1054. (if (file-name-absolute-p path) path
  1055. (file-relative-name (expand-file-name path dir)
  1056. (expand-file-name "eyecandy" dir))))
  1057. (defun org-odt-format-inline-image (thefile)
  1058. (let* ((thelink (if (file-name-absolute-p thefile) thefile
  1059. (org-xml-format-href
  1060. (org-odt-relocate-relative-path
  1061. thefile org-current-export-file))))
  1062. (href
  1063. (org-odt-format-tags
  1064. "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
  1065. (if org-export-odt-embed-images
  1066. (org-odt-copy-image-file thefile) thelink))))
  1067. (org-export-odt-format-image thefile href)))
  1068. (defun org-export-odt-format-formula (src href &optional embed-as)
  1069. "Create image tag with source and attributes."
  1070. (save-match-data
  1071. (let* ((caption (org-find-text-property-in-string 'org-caption src))
  1072. (caption (and caption (org-xml-format-desc caption)))
  1073. (label (org-find-text-property-in-string 'org-label src))
  1074. (embed-as (or embed-as
  1075. (and (org-find-text-property-in-string
  1076. 'org-latex-src src)
  1077. (org-find-text-property-in-string
  1078. 'org-latex-src-embed-type src))
  1079. 'paragraph))
  1080. width height)
  1081. (cond
  1082. ((eq embed-as 'character)
  1083. (org-odt-format-entity "InlineFormula" href width height))
  1084. (t
  1085. (org-lparse-end-paragraph)
  1086. (org-lparse-insert-list-table
  1087. `((,(org-odt-format-entity
  1088. (if caption "CaptionedDisplayFormula" "DisplayFormula")
  1089. href width height caption nil)
  1090. ,(if (not label) ""
  1091. (org-odt-format-entity-caption label nil "Equation"))))
  1092. nil nil nil "OrgEquation" nil '((1 "c" 8) (2 "c" 1)))
  1093. (throw 'nextline nil))))))
  1094. (defvar org-odt-embedded-formulas-count 0)
  1095. (defun org-odt-copy-formula-file (path)
  1096. "Returns the internal name of the file"
  1097. (let* ((src-file (expand-file-name
  1098. path (file-name-directory org-current-export-file)))
  1099. (target-dir (format "Formula-%04d/"
  1100. (incf org-odt-embedded-formulas-count)))
  1101. (target-file (concat target-dir "content.xml")))
  1102. (when (not org-lparse-to-buffer)
  1103. (message "Embedding %s as %s ..."
  1104. (substring-no-properties path) target-file)
  1105. (make-directory target-dir)
  1106. (org-odt-create-manifest-file-entry
  1107. "application/vnd.oasis.opendocument.formula" target-dir "1.2")
  1108. (copy-file src-file target-file 'overwrite)
  1109. (org-odt-create-manifest-file-entry "text/xml" target-file))
  1110. target-file))
  1111. (defun org-odt-format-inline-formula (thefile)
  1112. (let* ((thelink (if (file-name-absolute-p thefile) thefile
  1113. (org-xml-format-href
  1114. (org-odt-relocate-relative-path
  1115. thefile org-current-export-file))))
  1116. (href
  1117. (org-odt-format-tags
  1118. "<draw:object xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
  1119. (file-name-directory (org-odt-copy-formula-file thefile)))))
  1120. (org-export-odt-format-formula thefile href)))
  1121. (defun org-odt-is-formula-link-p (file)
  1122. (member (downcase (file-name-extension file)) '("mathml")))
  1123. (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
  1124. descp)
  1125. "Make an HTML link.
  1126. OPT-PLIST is an options list.
  1127. TYPE is the device-type of the link (THIS://foo.html)
  1128. PATH is the path of the link (http://THIS#locationx)
  1129. FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
  1130. DESC is the link description, if any.
  1131. ATTR is a string of other attributes of the a element.
  1132. MAY-INLINE-P allows inlining it as an image."
  1133. (declare (special org-lparse-par-open))
  1134. (save-match-data
  1135. (let* ((may-inline-p
  1136. (and (member type-1 '("http" "https" "file"))
  1137. (org-lparse-should-inline-p path descp)
  1138. (not fragment)))
  1139. (type (if (equal type-1 "id") "file" type-1))
  1140. (filename path)
  1141. (thefile path))
  1142. (cond
  1143. ;; check for inlined images
  1144. ((and (member type '("file"))
  1145. (not fragment)
  1146. (org-file-image-p
  1147. filename org-odt-export-inline-image-extensions)
  1148. (or (eq t org-odt-export-inline-images)
  1149. (and org-odt-export-inline-images (not descp))))
  1150. (org-odt-format-inline-image thefile))
  1151. ;; check for embedded formulas
  1152. ((and (member type '("file"))
  1153. (not fragment)
  1154. (org-odt-is-formula-link-p filename)
  1155. (or (not descp)))
  1156. (org-odt-format-inline-formula thefile))
  1157. ((string= type "coderef")
  1158. (let* ((ref fragment)
  1159. (lineno-or-ref (cdr (assoc ref org-export-code-refs)))
  1160. (desc (and descp desc))
  1161. (org-odt-suppress-xref nil)
  1162. (href (org-xml-format-href (concat "#coderef-" ref))))
  1163. (cond
  1164. ((and (numberp lineno-or-ref) (not desc))
  1165. (org-odt-format-link lineno-or-ref href))
  1166. ((and (numberp lineno-or-ref) desc
  1167. (string-match (regexp-quote (concat "(" ref ")")) desc))
  1168. (format (replace-match "%s" t t desc)
  1169. (org-odt-format-link lineno-or-ref href)))
  1170. (t
  1171. (setq desc (format
  1172. (if (and desc (string-match
  1173. (regexp-quote (concat "(" ref ")"))
  1174. desc))
  1175. (replace-match "%s" t t desc)
  1176. (or desc "%s"))
  1177. lineno-or-ref))
  1178. (org-odt-format-link (org-xml-format-desc desc) href)))))
  1179. (t
  1180. (when (string= type "file")
  1181. (setq thefile
  1182. (cond
  1183. ((file-name-absolute-p path)
  1184. (concat "file://" (expand-file-name path)))
  1185. (t (org-odt-relocate-relative-path
  1186. thefile org-current-export-file)))))
  1187. (when (and (member type '("" "http" "https" "file")) fragment)
  1188. (setq thefile (concat thefile "#" fragment)))
  1189. (setq thefile (org-xml-format-href thefile))
  1190. (when (not (member type '("" "file")))
  1191. (setq thefile (concat type ":" thefile)))
  1192. (let ((org-odt-suppress-xref nil))
  1193. (org-odt-format-link
  1194. (org-xml-format-desc desc) thefile attr)))))))
  1195. (defun org-odt-format-heading (text level &optional id)
  1196. (let* ((text (if id (org-odt-format-target text id) text)))
  1197. (org-odt-format-tags
  1198. '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
  1199. "</text:h>") text level level)))
  1200. (defun org-odt-format-headline (title extra-targets tags
  1201. &optional snumber level)
  1202. (concat
  1203. (org-lparse-format 'EXTRA-TARGETS extra-targets)
  1204. ;; No need to generate section numbers. They are auto-generated by
  1205. ;; the application
  1206. ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
  1207. title
  1208. (and tags (concat (org-lparse-format 'SPACES 3)
  1209. (org-lparse-format 'ORG-TAGS tags)))))
  1210. (defun org-odt-format-anchor (text name &optional class)
  1211. (org-odt-format-target text name))
  1212. (defun org-odt-format-bookmark (text id)
  1213. (if id
  1214. (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id)
  1215. text))
  1216. (defun org-odt-format-target (text id)
  1217. (let ((name (concat org-export-odt-bookmark-prefix id)))
  1218. (concat
  1219. (and id (org-odt-format-tags
  1220. "<text:bookmark-start text:name=\"%s\"/>" "" name))
  1221. (org-odt-format-bookmark text id)
  1222. (and id (org-odt-format-tags
  1223. "<text:bookmark-end text:name=\"%s\"/>" "" name)))))
  1224. (defun org-odt-format-footnote (n def)
  1225. (let ((id (concat "fn" n))
  1226. (note-class "footnote")
  1227. (par-style "Footnote"))
  1228. (org-odt-format-tags
  1229. '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
  1230. "</text:note>")
  1231. (concat
  1232. (org-odt-format-tags
  1233. '("<text:note-citation>" . "</text:note-citation>")
  1234. n)
  1235. (org-odt-format-tags
  1236. '("<text:note-body>" . "</text:note-body>")
  1237. def))
  1238. id note-class)))
  1239. (defun org-odt-format-footnote-reference (n def refcnt)
  1240. (if (= refcnt 1)
  1241. (org-odt-format-footnote n def)
  1242. (org-odt-format-footnote-ref n)))
  1243. (defun org-odt-format-footnote-ref (n)
  1244. (let ((note-class "footnote")
  1245. (ref-format "text")
  1246. (ref-name (concat "fn" n)))
  1247. (org-odt-format-tags
  1248. '("<text:span text:style-name=\"%s\">" . "</text:span>")
  1249. (org-odt-format-tags
  1250. '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" . "</text:note-ref>")
  1251. n note-class ref-format ref-name)
  1252. "OrgSuperscript")))
  1253. (defun org-odt-get-image-name (file-name)
  1254. (require 'sha1)
  1255. (file-relative-name
  1256. (expand-file-name
  1257. (concat (sha1 file-name) "." (file-name-extension file-name)) "Pictures")))
  1258. (defun org-export-odt-format-image (src href &optional embed-as)
  1259. "Create image tag with source and attributes."
  1260. (save-match-data
  1261. (let* ((caption (org-find-text-property-in-string 'org-caption src))
  1262. (caption (and caption (org-xml-format-desc caption)))
  1263. (attr (org-find-text-property-in-string 'org-attributes src))
  1264. (label (org-find-text-property-in-string 'org-label src))
  1265. (embed-as (or embed-as
  1266. (if (org-find-text-property-in-string
  1267. 'org-latex-src src)
  1268. (or (org-find-text-property-in-string
  1269. 'org-latex-src-embed-type src) 'character)
  1270. 'paragraph)))
  1271. (attr-plist (when attr (read attr)))
  1272. (size (org-odt-image-size-from-file
  1273. src (plist-get attr-plist :width)
  1274. (plist-get attr-plist :height)
  1275. (plist-get attr-plist :scale) nil embed-as))
  1276. (width (car size)) (height (cdr size)))
  1277. (cond
  1278. ((not (or caption label))
  1279. (case embed-as
  1280. (paragraph (org-odt-format-entity "DisplayImage" href width height))
  1281. (character (org-odt-format-entity "InlineImage" href width height))
  1282. (t (error "Unknown value for embed-as %S" embed-as))))
  1283. (t
  1284. (org-odt-format-entity
  1285. "CaptionedDisplayImage" href width height caption label))))))
  1286. (defun org-odt-format-frame (text width height style &optional
  1287. extra anchor-type)
  1288. (let ((frame-attrs
  1289. (concat
  1290. (if width (format " svg:width=\"%0.2fcm\"" width) "")
  1291. (if height (format " svg:height=\"%0.2fcm\"" height) "")
  1292. extra
  1293. (format " text:anchor-type=\"%s\"" (or anchor-type "paragraph")))))
  1294. (org-odt-format-tags
  1295. '("<draw:frame draw:style-name=\"%s\"%s>" . "</draw:frame>")
  1296. text style frame-attrs)))
  1297. (defun org-odt-format-textbox (text width height style &optional
  1298. extra anchor-type)
  1299. (org-odt-format-frame
  1300. (org-odt-format-tags
  1301. '("<draw:text-box %s>" . "</draw:text-box>")
  1302. text (concat (format " fo:min-height=\"%0.2fcm\"" (or height .2))
  1303. (format " fo:min-width=\"%0.2fcm\"" (or width .2))))
  1304. width nil style extra anchor-type))
  1305. (defun org-odt-format-inlinetask (heading content
  1306. &optional todo priority tags)
  1307. (org-odt-format-stylized-paragraph
  1308. nil (org-odt-format-textbox
  1309. (concat (org-odt-format-stylized-paragraph
  1310. "OrgInlineTaskHeading"
  1311. (org-lparse-format
  1312. 'HEADLINE (concat (org-lparse-format-todo todo) " " heading)
  1313. nil tags))
  1314. content) nil nil "OrgInlineTaskFrame" " style:rel-width=\"100%\"")))
  1315. (defvar org-odt-entity-frame-styles
  1316. '(("InlineImage" "Figure" ("OrgInlineImage" nil "as-char"))
  1317. ("DisplayImage" "Figure" ("OrgDisplayImage" nil "paragraph"))
  1318. ("CaptionedDisplayImage" "Figure"
  1319. ("OrgCaptionedImage"
  1320. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  1321. ("OrgImageCaptionFrame"))
  1322. ("InlineFormula" "Equation" ("OrgInlineFormula" nil "as-char"))
  1323. ("DisplayFormula" "Equation" ("OrgDisplayFormula" nil "as-char"))
  1324. ("CaptionedDisplayFormula" "Equation"
  1325. ("OrgCaptionedFormula" nil "paragraph")
  1326. ("OrgFormulaCaptionFrame" nil "as-char"))))
  1327. (defun org-odt-format-entity (entity href width height &optional caption label)
  1328. (let* ((entity-style (assoc entity org-odt-entity-frame-styles))
  1329. (entity-frame (apply 'org-odt-format-frame
  1330. href width height (nth 2 entity-style))))
  1331. (if (not (or caption label)) entity-frame
  1332. (apply 'org-odt-format-textbox
  1333. (org-odt-format-stylized-paragraph
  1334. 'illustration
  1335. (concat entity-frame (org-odt-format-entity-caption
  1336. label caption (nth 1 entity-style))))
  1337. width height (nth 3 entity-style)))))
  1338. (defvar org-odt-embedded-images-count 0)
  1339. (defun org-odt-copy-image-file (path)
  1340. "Returns the internal name of the file"
  1341. (let* ((image-type (file-name-extension path))
  1342. (media-type (format "image/%s" image-type))
  1343. (src-file (expand-file-name
  1344. path (file-name-directory org-current-export-file)))
  1345. (target-dir "Images/")
  1346. (target-file
  1347. (format "%s%04d.%s" target-dir
  1348. (incf org-odt-embedded-images-count) image-type)))
  1349. (when (not org-lparse-to-buffer)
  1350. (message "Embedding %s as %s ..."
  1351. (substring-no-properties path) target-file)
  1352. (when (= 1 org-odt-embedded-images-count)
  1353. (make-directory target-dir)
  1354. (org-odt-create-manifest-file-entry "" target-dir))
  1355. (copy-file src-file target-file 'overwrite)
  1356. (org-odt-create-manifest-file-entry media-type target-file))
  1357. target-file))
  1358. (defvar org-export-odt-image-size-probe-method
  1359. '(emacs imagemagick force)
  1360. "Ordered list of methods by for determining size of an embedded
  1361. image.")
  1362. (defvar org-export-odt-default-image-sizes-alist
  1363. '(("character" . (5 . 0.4))
  1364. ("paragraph" . (5 . 5)))
  1365. "Hardcoded image dimensions one for each of the anchor
  1366. methods.")
  1367. (defun org-odt-do-image-size (probe-method file &optional dpi anchor-type)
  1368. (setq dpi (or dpi org-export-odt-pixels-per-inch))
  1369. (setq anchor-type (or anchor-type "paragraph"))
  1370. (flet ((size-in-cms (size-in-pixels)
  1371. (flet ((pixels-to-cms (pixels)
  1372. (let* ((cms-per-inch 2.54)
  1373. (inches (/ pixels dpi)))
  1374. (* cms-per-inch inches))))
  1375. (and size-in-pixels
  1376. (cons (pixels-to-cms (car size-in-pixels))
  1377. (pixels-to-cms (cdr size-in-pixels)))))))
  1378. (case probe-method
  1379. (emacs
  1380. (size-in-cms (ignore-errors (image-size (create-image file) 'pixels))))
  1381. (imagemagick
  1382. (size-in-cms
  1383. (let ((dim (shell-command-to-string
  1384. (format "identify -format \"%%w:%%h\" \"%s\"" file))))
  1385. (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
  1386. (cons (string-to-number (match-string 1 dim))
  1387. (string-to-number (match-string 2 dim)))))))
  1388. (t
  1389. (cdr (assoc-string anchor-type
  1390. org-export-odt-default-image-sizes-alist))))))
  1391. (defun org-odt-image-size-from-file (file &optional user-width
  1392. user-height scale dpi embed-as)
  1393. (unless (file-name-absolute-p file)
  1394. (setq file (expand-file-name
  1395. file (file-name-directory org-current-export-file))))
  1396. (let* (size width height)
  1397. (unless (and user-height user-width)
  1398. (loop for probe-method in org-export-odt-image-size-probe-method
  1399. until size
  1400. do (setq size (org-odt-do-image-size
  1401. probe-method file dpi embed-as)))
  1402. (or size (error "Cannot determine Image size. Aborting ..."))
  1403. (setq width (car size) height (cdr size)))
  1404. (cond
  1405. (scale
  1406. (setq width (* width scale) height (* height scale)))
  1407. ((and user-height user-width)
  1408. (setq width user-width height user-height))
  1409. (user-height
  1410. (setq width (* user-height (/ width height)) height user-height))
  1411. (user-width
  1412. (setq height (* user-width (/ height width)) width user-width))
  1413. (t (ignore)))
  1414. (cons width height)))
  1415. (defvar org-odt-entity-labels-alist nil
  1416. "Associate Labels with the Labelled entities.
  1417. Each element of the alist is of the form (LABEL-NAME
  1418. CATEGORY-NAME SEQNO). LABEL-NAME is same as that specified by
  1419. \"#+LABEL: ...\" line. CATEGORY-NAME is the type of the entity
  1420. that LABEL-NAME is attached to. CATEGORY-NAME can be one of
  1421. \"Table\", \"Figure\" or \"Equation\". SEQNO is the unique
  1422. number assigned to the referenced entity on a per-CATEGORY basis.
  1423. It is generated sequentially and is 1-based.
  1424. Update this alist with `org-odt-add-label-definition' and
  1425. retrieve an entry with `org-odt-get-label-definition'.")
  1426. (defvar org-odt-entity-counts-plist nil
  1427. "Plist of running counters of SEQNOs for each of the CATEGORY-NAMEs.
  1428. See `org-odt-entity-labels-alist' for known CATEGORY-NAMEs.")
  1429. (defvar org-odt-label-def-ref-spec
  1430. '(("Equation" "(%n)" "text" "(%n)")
  1431. ("" "%e %n%c" "category-and-value" "%e %n"))
  1432. "Specify how labels are applied and referenced.
  1433. This is an alist where each element is of the form (CATEGORY-NAME
  1434. LABEL-APPLY-FMT LABEL-REF-MODE LABEL-REF-FMT). CATEGORY-NAME is
  1435. as defined in `org-odt-entity-labels-alist'. It can additionally
  1436. be an empty string in which case it is used as a catch-all
  1437. specifier.
  1438. LABEL-APPLY-FMT is used for applying labels and captions. It may
  1439. contain following specifiers - %e, %n and %c. %e is replaced
  1440. with the CATEGORY-NAME. %n is replaced with \"<text:sequence
  1441. ...> SEQNO </text:sequence>\". %c is replaced with CAPTION. See
  1442. `org-odt-format-label-definition'.
  1443. LABEL-REF-MODE and LABEL-REF-FMT are used for generating the
  1444. following label reference - \"<text:sequence-ref
  1445. text:reference-format=\"LABEL-REF-MODE\" ...> LABEL-REF-FMT
  1446. </text:sequence-ref>\". LABEL-REF-FMT may contain following
  1447. specifiers - %e and %n. %e is replaced with the CATEGORY-NAME. %n is
  1448. replaced with SEQNO. See `org-odt-format-label-reference'.")
  1449. (defun org-odt-add-label-definition (label category)
  1450. "Return (SEQNO . LABEL-APPLY-FMT).
  1451. See `org-odt-label-def-ref-spec'."
  1452. (setq label (substring-no-properties label))
  1453. (let (seqno label-props fmt (category-sym (intern category)))
  1454. (setq seqno (1+ (plist-get org-odt-entity-counts-plist category-sym))
  1455. org-odt-entity-counts-plist (plist-put org-odt-entity-counts-plist
  1456. category-sym seqno)
  1457. fmt (cadr (or (assoc-string category org-odt-label-def-ref-spec t)
  1458. (assoc-string "" org-odt-label-def-ref-spec t)))
  1459. label-props (list label category seqno))
  1460. (push label-props org-odt-entity-labels-alist)
  1461. (cons seqno fmt)))
  1462. (defun org-odt-get-label-definition (label)
  1463. "Return (LABEL-NAME CATEGORY-NAME SEQNO LABEL-REF-MODE LABEL-REF-FMT).
  1464. See `org-odt-entity-labels-alist' and
  1465. `org-odt-label-def-ref-spec'."
  1466. (let* ((label-props (assoc label org-odt-entity-labels-alist))
  1467. (category (nth 1 label-props)))
  1468. (append label-props
  1469. (cddr (or (assoc-string category org-odt-label-def-ref-spec t)
  1470. (assoc-string "" org-odt-label-def-ref-spec t))))))
  1471. (defun org-odt-format-label-definition (label category caption)
  1472. (assert label)
  1473. (let* ((label-props (org-odt-add-label-definition label category))
  1474. (seqno (car label-props))
  1475. (fmt (cdr label-props)))
  1476. (or (format-spec
  1477. fmt
  1478. `((?e . ,category)
  1479. (?n . ,(org-odt-format-tags
  1480. '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" . "</text:sequence>")
  1481. (format "%d" seqno) label category category))
  1482. (?c . ,(or (and caption (concat ": " caption)) ""))))
  1483. caption "")))
  1484. (defun org-odt-format-label-reference (label category seqno fmt1 fmt2)
  1485. (assert label)
  1486. (save-match-data
  1487. (org-odt-format-tags
  1488. '("<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">"
  1489. . "</text:sequence-ref>")
  1490. (format-spec fmt2 `((?e . ,category)
  1491. (?n . ,(format "%d" seqno)))) fmt1 label)))
  1492. (defun org-odt-fixup-label-references ()
  1493. (goto-char (point-min))
  1494. (while (re-search-forward
  1495. "<text:sequence-ref text:ref-name=\"\\([^\"]+\\)\"/>" nil t)
  1496. (let* ((label (match-string 1)))
  1497. (replace-match
  1498. (apply 'org-odt-format-label-reference
  1499. (org-odt-get-label-definition label)) t t))))
  1500. (defun org-odt-format-entity-caption (label caption category)
  1501. (or (and label (org-odt-format-label-definition label category caption))
  1502. caption ""))
  1503. (defun org-odt-format-tags (tag text &rest args)
  1504. (let ((prefix (when org-lparse-encode-pending "@"))
  1505. (suffix (when org-lparse-encode-pending "@")))
  1506. (apply 'org-lparse-format-tags tag text prefix suffix args)))
  1507. (defun org-odt-init-outfile (filename)
  1508. (unless (executable-find "zip")
  1509. ;; Not at all OSes ship with zip by default
  1510. (error "Executable \"zip\" needed for creating OpenDocument files"))
  1511. (let* ((outdir (make-temp-file org-export-odt-tmpdir-prefix t))
  1512. (content-file (expand-file-name "content.xml" outdir)))
  1513. ;; init conten.xml
  1514. (with-current-buffer (find-file-noselect content-file t))
  1515. ;; reset variables
  1516. (setq org-odt-manifest-file-entries nil
  1517. org-odt-embedded-images-count 0
  1518. org-odt-embedded-formulas-count 0
  1519. org-odt-entity-labels-alist nil
  1520. org-odt-entity-counts-plist (list 'Table 0 'Equation 0 'Figure 0))
  1521. content-file))
  1522. (defcustom org-export-odt-prettify-xml nil
  1523. "Specify whether or not the xml output should be prettified.
  1524. When this option is turned on, `indent-region' is run on all
  1525. component xml buffers before they are saved. Turn this off for
  1526. regular use. Turn this on if you need to examine the xml
  1527. visually."
  1528. :group 'org-export-odt
  1529. :type 'boolean)
  1530. (defvar hfy-user-sheet-assoc) ; bound during org-do-lparse
  1531. (defun org-odt-save-as-outfile (target opt-plist)
  1532. ;; create mimetype file
  1533. (write-region "application/vnd.oasis.opendocument.text" nil
  1534. (expand-file-name "mimetype"))
  1535. ;; write meta file
  1536. (org-odt-update-meta-file opt-plist)
  1537. ;; write styles file
  1538. (org-odt-copy-styles-file)
  1539. ;; Update styles.xml - take care of outline numbering
  1540. (with-current-buffer
  1541. (find-file-noselect (expand-file-name "styles.xml") t)
  1542. ;; Don't make automatic backup of styles.xml file. This setting
  1543. ;; prevents the backedup styles.xml file from being zipped in to
  1544. ;; odt file. This is more of a hackish fix. Better alternative
  1545. ;; would be to fix the zip command so that the output odt file
  1546. ;; includes only the needed files and excludes any auto-generated
  1547. ;; extra files like backups and auto-saves etc etc. Note that
  1548. ;; currently the zip command zips up the entire temp directory so
  1549. ;; that any auto-generated files created under the hood ends up in
  1550. ;; the resulting odt file.
  1551. (set (make-local-variable 'backup-inhibited) t)
  1552. ;; Import local setting of `org-export-with-section-numbers'
  1553. (org-lparse-bind-local-variables opt-plist)
  1554. (org-odt-configure-outline-numbering
  1555. (if org-export-with-section-numbers org-export-headline-levels 0)))
  1556. ;; Write custom stlyes for source blocks
  1557. (org-odt-insert-custom-styles-for-srcblocks
  1558. (mapconcat
  1559. (lambda (style)
  1560. (format " %s\n" (cddr style)))
  1561. hfy-user-sheet-assoc ""))
  1562. ;; create a manifest entry for content.xml
  1563. (org-odt-create-manifest-file-entry
  1564. "application/vnd.oasis.opendocument.text" "/" "1.2")
  1565. (org-odt-create-manifest-file-entry "text/xml" "content.xml")
  1566. ;; write out the manifest entries before zipping
  1567. (org-odt-write-manifest-file)
  1568. (let ((xml-files '("mimetype" "META-INF/manifest.xml" "content.xml"
  1569. "meta.xml" "styles.xml"))
  1570. (zipdir default-directory))
  1571. (message "Switching to directory %s" (expand-file-name zipdir))
  1572. ;; save all xml files
  1573. (mapc (lambda (file)
  1574. (with-current-buffer
  1575. (find-file-noselect (expand-file-name file) t)
  1576. ;; prettify output if needed
  1577. (when org-export-odt-prettify-xml
  1578. (indent-region (point-min) (point-max)))
  1579. (save-buffer 0)))
  1580. xml-files)
  1581. (let* ((target-name (file-name-nondirectory target))
  1582. (target-dir (file-name-directory target))
  1583. (cmds `(("zip" "-mX0" ,target-name "mimetype")
  1584. ("zip" "-rmTq" ,target-name "."))))
  1585. (when (file-exists-p target)
  1586. ;; FIXME: If the file is locked this throws a cryptic error
  1587. (delete-file target))
  1588. (let ((coding-system-for-write 'no-conversion) exitcode err-string)
  1589. (message "Creating odt file...")
  1590. (mapc
  1591. (lambda (cmd)
  1592. (message "Running %s" (mapconcat 'identity cmd " "))
  1593. (setq err-string
  1594. (with-output-to-string
  1595. (setq exitcode
  1596. (apply 'call-process (car cmd)
  1597. nil standard-output nil (cdr cmd)))))
  1598. (or (zerop exitcode)
  1599. (ignore (message "%s" err-string))
  1600. (error "Unable to create odt file (%S)" exitcode)))
  1601. cmds))
  1602. ;; move the file from outdir to target-dir
  1603. (rename-file target-name target-dir)
  1604. ;; kill all xml buffers
  1605. (mapc (lambda (file)
  1606. (kill-buffer
  1607. (find-file-noselect (expand-file-name file zipdir) t)))
  1608. xml-files)
  1609. (delete-directory zipdir)))
  1610. (message "Created %s" target)
  1611. (set-buffer (find-file-noselect target t)))
  1612. (defun org-odt-format-date (date)
  1613. (let ((warning-msg
  1614. "OpenDocument files require that dates be in ISO-8601 format. Please review your DATE options for compatibility."))
  1615. ;; If the user is not careful with the date specification, an
  1616. ;; invalid meta.xml will be emitted.
  1617. ;; For now honor user's diktat and let him off with a warning
  1618. ;; message. This is OK as LibreOffice (and possibly other
  1619. ;; apps) doesn't deem this deviation as critical and continue
  1620. ;; to load the file.
  1621. ;; FIXME: Surely there a better way to handle this. Revisit this
  1622. ;; later.
  1623. (cond
  1624. ((and date (string-match "%" date))
  1625. ;; Honor user's diktat. See comments above
  1626. (org-lparse-warn warning-msg)
  1627. (format-time-string date))
  1628. (date
  1629. ;; Honor user's diktat. See comments above
  1630. (org-lparse-warn warning-msg)
  1631. date)
  1632. (t
  1633. ;; ISO 8601 format
  1634. (let ((stamp (format-time-string "%Y-%m-%dT%H:%M:%S%z")))
  1635. (format "%s:%s" (substring stamp 0 -2) (substring stamp -2)))))))
  1636. (defconst org-odt-manifest-file-entry-tag
  1637. "
  1638. <manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
  1639. (defvar org-odt-manifest-file-entries nil)
  1640. (defun org-odt-create-manifest-file-entry (&rest args)
  1641. (push args org-odt-manifest-file-entries))
  1642. (defun org-odt-write-manifest-file ()
  1643. (make-directory "META-INF")
  1644. (let ((manifest-file (expand-file-name "META-INF/manifest.xml")))
  1645. (write-region
  1646. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  1647. <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n"
  1648. nil manifest-file)
  1649. (mapc
  1650. (lambda (file-entry)
  1651. (let* ((version (nth 2 file-entry))
  1652. (extra (if version
  1653. (format " manifest:version=\"%s\"" version)
  1654. "")))
  1655. (write-region
  1656. (format org-odt-manifest-file-entry-tag
  1657. (nth 0 file-entry) (nth 1 file-entry) extra)
  1658. nil manifest-file t))) org-odt-manifest-file-entries)
  1659. (write-region "\n</manifest:manifest>" nil manifest-file t)))
  1660. (defun org-odt-update-meta-file (opt-plist)
  1661. (let ((date (org-odt-format-date (plist-get opt-plist :date)))
  1662. (author (or (plist-get opt-plist :author) ""))
  1663. (email (plist-get opt-plist :email))
  1664. (keywords (plist-get opt-plist :keywords))
  1665. (description (plist-get opt-plist :description))
  1666. (title (plist-get opt-plist :title)))
  1667. (write-region
  1668. (concat
  1669. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  1670. <office:document-meta
  1671. xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
  1672. xmlns:xlink=\"http://www.w3.org/1999/xlink\"
  1673. xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
  1674. xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
  1675. xmlns:ooo=\"http://openoffice.org/2004/office\"
  1676. office:version=\"1.2\">
  1677. <office:meta>" "\n"
  1678. (org-odt-format-tags '("<dc:creator>" . "</dc:creator>") author)
  1679. (org-odt-format-tags
  1680. '("\n<meta:initial-creator>" . "</meta:initial-creator>") author)
  1681. (org-odt-format-tags '("\n<dc:date>" . "</dc:date>") date)
  1682. (org-odt-format-tags
  1683. '("\n<meta:creation-date>" . "</meta:creation-date>") date)
  1684. (org-odt-format-tags '("\n<meta:generator>" . "</meta:generator>")
  1685. (when org-export-creator-info
  1686. (format "Org-%s/Emacs-%s"
  1687. org-version emacs-version)))
  1688. (org-odt-format-tags '("\n<meta:keyword>" . "</meta:keyword>") keywords)
  1689. (org-odt-format-tags '("\n<dc:subject>" . "</dc:subject>") description)
  1690. (org-odt-format-tags '("\n<dc:title>" . "</dc:title>") title)
  1691. "\n"
  1692. " </office:meta>" "</office:document-meta>")
  1693. nil (expand-file-name "meta.xml")))
  1694. ;; create a manifest entry for meta.xml
  1695. (org-odt-create-manifest-file-entry "text/xml" "meta.xml"))
  1696. (defun org-odt-finalize-outfile ()
  1697. (org-odt-delete-empty-paragraphs))
  1698. (defun org-odt-delete-empty-paragraphs ()
  1699. (goto-char (point-min))
  1700. (let ((open "<text:p[^>]*>")
  1701. (close "</text:p>"))
  1702. (while (re-search-forward (format "%s[ \r\n\t]*%s" open close) nil t)
  1703. (replace-match ""))))
  1704. (defun org-odt-get (what &optional opt-plist)
  1705. (case what
  1706. (BACKEND 'odt)
  1707. (EXPORT-DIR (org-export-directory :html opt-plist))
  1708. (FILE-NAME-EXTENSION "odt")
  1709. (EXPORT-BUFFER-NAME "*Org ODT Export*")
  1710. (ENTITY-CONTROL org-odt-entity-control-callbacks-alist)
  1711. (ENTITY-FORMAT org-odt-entity-format-callbacks-alist)
  1712. (INIT-METHOD 'org-odt-init-outfile)
  1713. (FINAL-METHOD 'org-odt-finalize-outfile)
  1714. (SAVE-METHOD 'org-odt-save-as-outfile)
  1715. ;; (OTHER-BACKENDS) ; see note in `org-xhtml-get'
  1716. ;; (CONVERT-METHOD) ; see note in `org-xhtml-get'
  1717. (TOPLEVEL-HLEVEL 1)
  1718. (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps)
  1719. (INLINE-IMAGES 'maybe)
  1720. (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
  1721. (PLAIN-TEXT-MAP '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
  1722. (TABLE-FIRST-COLUMN-AS-LABELS nil)
  1723. (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY "," 'superscript))
  1724. (CODING-SYSTEM-FOR-WRITE 'utf-8)
  1725. (CODING-SYSTEM-FOR-SAVE 'utf-8)
  1726. (t (error "Unknown property: %s" what))))
  1727. (defvar org-lparse-latex-fragment-fallback) ; set by org-do-lparse
  1728. (defvar org-lparse-opt-plist) ; bound during org-do-lparse
  1729. (defun org-export-odt-do-preprocess-latex-fragments ()
  1730. "Convert LaTeX fragments to images."
  1731. (let* ((latex-frag-opt (plist-get org-lparse-opt-plist :LaTeX-fragments))
  1732. (latex-frag-opt ; massage the options
  1733. (or (and (member latex-frag-opt '(mathjax t))
  1734. (not (and (fboundp 'org-format-latex-mathml-available-p)
  1735. (org-format-latex-mathml-available-p)))
  1736. (prog1 org-lparse-latex-fragment-fallback
  1737. (org-lparse-warn
  1738. (concat
  1739. "LaTeX to MathML converter not available. "
  1740. (format "Using %S instead."
  1741. org-lparse-latex-fragment-fallback)))))
  1742. latex-frag-opt))
  1743. cache-dir display-msg)
  1744. (cond
  1745. ((eq latex-frag-opt 'dvipng)
  1746. (setq cache-dir "ltxpng/")
  1747. (setq display-msg "Creating LaTeX image %s"))
  1748. ((member latex-frag-opt '(mathjax t))
  1749. (setq latex-frag-opt 'mathml)
  1750. (setq cache-dir "ltxmathml/")
  1751. (setq display-msg "Creating MathML formula %s")))
  1752. (when (and org-current-export-file)
  1753. (org-format-latex
  1754. (concat cache-dir (file-name-sans-extension
  1755. (file-name-nondirectory org-current-export-file)))
  1756. org-current-export-dir nil display-msg
  1757. nil nil latex-frag-opt))))
  1758. (defun org-export-odt-preprocess-latex-fragments ()
  1759. (when (equal org-export-current-backend 'odt)
  1760. (org-export-odt-do-preprocess-latex-fragments)))
  1761. (defun org-export-odt-preprocess-label-references ()
  1762. (goto-char (point-min))
  1763. (let (label label-components category value pretty-label)
  1764. (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t)
  1765. (org-if-unprotected-at (match-beginning 1)
  1766. (replace-match
  1767. (let ((org-lparse-encode-pending t)
  1768. (label (match-string 1)))
  1769. ;; markup generated below is mostly an eye-candy. At
  1770. ;; pre-processing stage, there is no information on which
  1771. ;; entity a label reference points to. The actual markup
  1772. ;; is generated as part of `org-odt-fixup-label-references'
  1773. ;; which gets called at the fag end of export. By this
  1774. ;; time we would have seen and collected all the label
  1775. ;; definitions in `org-odt-entity-labels-alist'.
  1776. (org-odt-format-tags
  1777. "<text:sequence-ref text:ref-name=\"%s\"/>" "" label)) t t)))))
  1778. ;; process latex fragments as part of
  1779. ;; `org-export-preprocess-after-blockquote-hook'. Note that this hook
  1780. ;; is the one that is closest and well before the call to
  1781. ;; `org-export-attach-captions-and-attributes' in
  1782. ;; `org-export-preprocess-stirng'. The above arrangement permits
  1783. ;; captions, labels and attributes to be attached to png images
  1784. ;; generated out of latex equations.
  1785. (add-hook 'org-export-preprocess-after-blockquote-hook
  1786. 'org-export-odt-preprocess-latex-fragments)
  1787. (defun org-export-odt-preprocess (parameters)
  1788. (org-export-odt-preprocess-label-references))
  1789. (declare-function archive-zip-extract "arc-mode.el" (archive name))
  1790. (defun org-odt-zip-extract-one (archive member &optional target)
  1791. (require 'arc-mode)
  1792. (let* ((target (or target default-directory))
  1793. (archive (expand-file-name archive))
  1794. (archive-zip-extract
  1795. (list "unzip" "-qq" "-o" "-d" target))
  1796. exit-code command-output)
  1797. (setq command-output
  1798. (with-temp-buffer
  1799. (setq exit-code (archive-zip-extract archive member))
  1800. (buffer-string)))
  1801. (unless (zerop exit-code)
  1802. (message command-output)
  1803. (error "Extraction failed"))))
  1804. (defun org-odt-zip-extract (archive members &optional target)
  1805. (when (atom members) (setq members (list members)))
  1806. (mapc (lambda (member)
  1807. (org-odt-zip-extract-one archive member target))
  1808. members))
  1809. (defun org-odt-copy-styles-file (&optional styles-file)
  1810. ;; Non-availability of styles.xml is not a critical error. For now
  1811. ;; throw an error purely for aesthetic reasons.
  1812. (setq styles-file (or styles-file
  1813. org-export-odt-styles-file
  1814. (expand-file-name "styles/OrgOdtStyles.xml"
  1815. org-odt-data-dir)
  1816. (error "org-odt: Missing styles file?")))
  1817. (cond
  1818. ((listp styles-file)
  1819. (let ((archive (nth 0 styles-file))
  1820. (members (nth 1 styles-file)))
  1821. (org-odt-zip-extract archive members)
  1822. (mapc
  1823. (lambda (member)
  1824. (when (org-file-image-p member)
  1825. (let* ((image-type (file-name-extension member))
  1826. (media-type (format "image/%s" image-type)))
  1827. (org-odt-create-manifest-file-entry media-type member))))
  1828. members)))
  1829. ((and (stringp styles-file) (file-exists-p styles-file))
  1830. (let ((styles-file-type (file-name-extension styles-file)))
  1831. (cond
  1832. ((string= styles-file-type "xml")
  1833. (copy-file styles-file "styles.xml" t))
  1834. ((member styles-file-type '("odt" "ott"))
  1835. (org-odt-zip-extract styles-file "styles.xml")))))
  1836. (t
  1837. (error (format "Invalid specification of styles.xml file: %S"
  1838. org-export-odt-styles-file))))
  1839. ;; create a manifest entry for styles.xml
  1840. (org-odt-create-manifest-file-entry "text/xml" "styles.xml"))
  1841. (defvar org-export-odt-factory-settings
  1842. "d4328fb9d1b6cb211d4320ff546829f26700dc5e"
  1843. "SHA1 hash of OrgOdtStyles.xml.")
  1844. (defun org-odt-configure-outline-numbering (level)
  1845. "Outline numbering is retained only upto LEVEL.
  1846. To disable outline numbering pass a LEVEL of 0."
  1847. (goto-char (point-min))
  1848. (let ((regex
  1849. "<text:outline-level-style\\(.*\\)text:level=\"\\([^\"]*\\)\"\\(.*\\)>")
  1850. (replacement
  1851. "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
  1852. (while (re-search-forward regex nil t)
  1853. (when (> (string-to-number (match-string 2)) level)
  1854. (replace-match replacement t nil))))
  1855. (save-buffer 0))
  1856. (provide 'org-odt)
  1857. ;;; org-odt.el ends here