org-odt.el 105 KB

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