org-odt.el 93 KB

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