org-odt.el 105 KB

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