org-odt.el 78 KB

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