org-odt.el 105 KB

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