org-odt.el 97 KB

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