org-odt.el 102 KB

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