org-odt.el 105 KB

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