org-odt.el 104 KB

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