org-odt.el 102 KB

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