org-odt.el 104 KB

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