org-odt.el 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676
  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. (defvar org-odt-section-count 0)
  575. (defun org-odt-begin-section (style &optional name)
  576. (setq name (or name (format "Section-%d" (incf org-odt-section-count))))
  577. (org-lparse-insert-tag
  578. "<text:section text:style-name=\"%s\" text:name=\"%s\">" style 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-indentedp nil)
  828. (defun org-odt-begin-table (caption label attributes)
  829. (setq org-odt-table-indentedp (not (null org-lparse-list-stack)))
  830. (when org-odt-table-indentedp
  831. ;; Within the Org file, the table is appearing within a list item.
  832. ;; OpenDocument doesn't allow table to appear within list items.
  833. ;; Temporarily terminate the list, emit the table and then
  834. ;; re-continue the list.
  835. (org-odt-discontinue-list)
  836. ;; Put the Table in an indented section.
  837. (let ((level (length org-odt-list-stack-stashed)))
  838. (org-odt-begin-section (format "OrgIndentedSection-Level-%d" level))))
  839. (setq org-odt-table-style attributes)
  840. (setq org-odt-table-style-spec
  841. (assoc org-odt-table-style org-export-odt-table-styles))
  842. (when label
  843. (insert
  844. (org-odt-format-stylized-paragraph
  845. 'table (org-odt-format-entity-caption label caption "__Table__"))))
  846. (org-lparse-insert-tag
  847. "<table:table table:name=\"%s\" table:style-name=\"%s\">"
  848. (or label "") (or (nth 1 org-odt-table-style-spec) "OrgTable"))
  849. (setq org-lparse-table-begin-marker (point)))
  850. (defvar org-lparse-table-colalign-info)
  851. (defun org-odt-end-table ()
  852. (goto-char org-lparse-table-begin-marker)
  853. (loop for level from 0 below org-lparse-table-ncols
  854. do (let* ((col-cookie (and org-lparse-table-is-styled
  855. (cdr (assoc (1+ level)
  856. org-lparse-table-colalign-info))))
  857. (extra-columns (or (nth 1 col-cookie) 0)))
  858. (dotimes (i (1+ extra-columns))
  859. (insert
  860. (org-odt-format-tags
  861. "<table:table-column table:style-name=\"%sColumn\"/>"
  862. "" (or (nth 1 org-odt-table-style-spec) "OrgTable"))))
  863. (insert "\n")))
  864. ;; fill style attributes for table cells
  865. (when org-lparse-table-is-styled
  866. (while (re-search-forward "@@\\(table-cell:p\\|table-cell:style-name\\)@@\\([0-9]+\\)@@\\([0-9]+\\)@@" nil t)
  867. (let* ((spec (match-string 1))
  868. (r (string-to-number (match-string 2)))
  869. (c (string-to-number (match-string 3)))
  870. (cell-styles (org-odt-get-table-cell-styles
  871. r c org-odt-table-style-spec))
  872. (table-cell-style (car cell-styles))
  873. (table-cell-paragraph-style (cdr cell-styles)))
  874. (cond
  875. ((equal spec "table-cell:p")
  876. (replace-match table-cell-paragraph-style t t))
  877. ((equal spec "table-cell:style-name")
  878. (replace-match table-cell-style t t))))))
  879. (goto-char (point-max))
  880. (org-lparse-insert-tag "</table:table>")
  881. (when org-odt-table-indentedp
  882. (org-odt-end-section)
  883. (org-odt-continue-list)))
  884. (defun org-odt-begin-table-rowgroup (&optional is-header-row)
  885. (when org-lparse-table-rowgrp-open
  886. (org-lparse-end 'TABLE-ROWGROUP))
  887. (org-lparse-insert-tag (if is-header-row
  888. "<table:table-header-rows>"
  889. "<table:table-rows>"))
  890. (setq org-lparse-table-rowgrp-open t)
  891. (setq org-lparse-table-cur-rowgrp-is-hdr is-header-row))
  892. (defun org-odt-end-table-rowgroup ()
  893. (when org-lparse-table-rowgrp-open
  894. (setq org-lparse-table-rowgrp-open nil)
  895. (org-lparse-insert-tag
  896. (if org-lparse-table-cur-rowgrp-is-hdr
  897. "</table:table-header-rows>" "</table:table-rows>"))))
  898. (defun org-odt-format-table-row (row)
  899. (org-odt-format-tags
  900. '("<table:table-row>" . "</table:table-row>") row))
  901. (defun org-odt-get-table-cell-styles (r c &optional style-spec)
  902. "Retrieve styles applicable to a table cell.
  903. R and C are (zero-based) row and column numbers of the table
  904. cell. STYLE-SPEC is an entry in `org-export-odt-table-styles'
  905. applicable to the current table. It is `nil' if the table is not
  906. associated with any style attributes.
  907. Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
  908. When STYLE-SPEC is nil, style the table cell the conventional way
  909. - choose cell borders based on row and column groupings and
  910. choose paragraph alignment based on `org-col-cookies' text
  911. property. See also
  912. `org-odt-get-paragraph-style-cookie-for-table-cell'.
  913. When STYLE-SPEC is non-nil, ignore the above cookie and return
  914. styles congruent with the ODF-1.2 specification."
  915. (cond
  916. (style-spec
  917. ;; LibreOffice - particularly the Writer - honors neither table
  918. ;; templates nor custom table-cell styles. Inorder to retain
  919. ;; inter-operability with LibreOffice, only automatic styles are
  920. ;; used for styling of table-cells. The current implementation is
  921. ;; congruent with ODF-1.2 specification and hence is
  922. ;; future-compatible.
  923. ;; Additional Note: LibreOffice's AutoFormat facility for tables -
  924. ;; which recognizes as many as 16 different cell types - is much
  925. ;; richer. Unfortunately it is NOT amenable to easy configuration
  926. ;; by hand.
  927. (let* ((template-name (nth 1 style-spec))
  928. (cell-style-selectors (nth 2 style-spec))
  929. (cell-type
  930. (cond
  931. ((and (cdr (assoc 'use-first-column-styles cell-style-selectors))
  932. (= c 0)) "FirstColumn")
  933. ((and (cdr (assoc 'use-last-column-styles cell-style-selectors))
  934. (= c (1- org-lparse-table-ncols))) "LastColumn")
  935. ((and (cdr (assoc 'use-first-row-styles cell-style-selectors))
  936. (= r 0)) "FirstRow")
  937. ((and (cdr (assoc 'use-last-row-styles cell-style-selectors))
  938. (= r org-lparse-table-rownum))
  939. "LastRow")
  940. ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
  941. (= (% r 2) 1)) "EvenRow")
  942. ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
  943. (= (% r 2) 0)) "OddRow")
  944. ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
  945. (= (% c 2) 1)) "EvenColumn")
  946. ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
  947. (= (% c 2) 0)) "OddColumn")
  948. (t ""))))
  949. (cons
  950. (concat template-name cell-type "TableCell")
  951. (concat template-name cell-type "TableParagraph"))))
  952. (t
  953. (cons
  954. (concat
  955. "OrgTblCell"
  956. (cond
  957. ((= r 0) "T")
  958. ((eq (cdr (assoc r org-lparse-table-rowgrp-info)) :start) "T")
  959. (t ""))
  960. (when (= r org-lparse-table-rownum) "B")
  961. (cond
  962. ((= c 0) "")
  963. ((or (memq (nth c org-table-colgroup-info) '(:start :startend))
  964. (memq (nth (1- c) org-table-colgroup-info) '(:end :startend))) "L")
  965. (t "")))
  966. (capitalize (aref org-lparse-table-colalign-vector c))))))
  967. (defun org-odt-get-paragraph-style-cookie-for-table-cell (r c)
  968. (concat
  969. (and (not org-odt-table-style-spec)
  970. (cond
  971. (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
  972. ((and (= c 0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS))
  973. "OrgTableHeading")
  974. (t "OrgTableContents")))
  975. (and org-lparse-table-is-styled
  976. (format "@@table-cell:p@@%03d@@%03d@@" r c))))
  977. (defun org-odt-get-style-name-cookie-for-table-cell (r c)
  978. (when org-lparse-table-is-styled
  979. (format "@@table-cell:style-name@@%03d@@%03d@@" r c)))
  980. (defun org-odt-format-table-cell (data r c horiz-span)
  981. (concat
  982. (let* ((paragraph-style-cookie
  983. (org-odt-get-paragraph-style-cookie-for-table-cell r c))
  984. (style-name-cookie
  985. (org-odt-get-style-name-cookie-for-table-cell r c))
  986. (extra (and style-name-cookie
  987. (format " table:style-name=\"%s\"" style-name-cookie)))
  988. (extra (concat extra
  989. (and (> horiz-span 0)
  990. (format " table:number-columns-spanned=\"%d\""
  991. (1+ horiz-span))))))
  992. (org-odt-format-tags
  993. '("<table:table-cell%s>" . "</table:table-cell>")
  994. (if org-lparse-list-table-p data
  995. (org-odt-format-stylized-paragraph paragraph-style-cookie data)) extra))
  996. (let (s)
  997. (dotimes (i horiz-span)
  998. (setq s (concat s "\n<table:covered-table-cell/>"))) s)
  999. "\n"))
  1000. (defun org-odt-begin-footnote-definition (n)
  1001. (org-lparse-begin-paragraph 'footnote))
  1002. (defun org-odt-end-footnote-definition (n)
  1003. (org-lparse-end-paragraph))
  1004. (defun org-odt-begin-toc (lang-specific-heading max-level)
  1005. (insert
  1006. (format "
  1007. <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
  1008. <text:table-of-content-source text:outline-level=\"%d\">
  1009. <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
  1010. " max-level lang-specific-heading))
  1011. (loop for level from 1 upto 10
  1012. do (insert (format
  1013. "
  1014. <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
  1015. <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
  1016. <text:index-entry-chapter/>
  1017. <text:index-entry-text/>
  1018. <text:index-entry-link-end/>
  1019. </text:table-of-content-entry-template>
  1020. " level level)))
  1021. (insert
  1022. (format "
  1023. </text:table-of-content-source>
  1024. <text:index-body>
  1025. <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
  1026. <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
  1027. </text:index-title>
  1028. " lang-specific-heading)))
  1029. (defun org-odt-end-toc ()
  1030. (insert "
  1031. </text:index-body>
  1032. </text:table-of-content>
  1033. "))
  1034. (defun org-odt-format-toc-entry (snumber todo headline tags href)
  1035. (setq headline (concat
  1036. (and org-export-with-section-numbers
  1037. (concat snumber ". "))
  1038. headline
  1039. (and tags
  1040. (concat
  1041. (org-lparse-format 'SPACES 3)
  1042. (org-lparse-format 'FONTIFY tags "tag")))))
  1043. (when todo
  1044. (setq headline (org-lparse-format 'FONTIFY headline "todo")))
  1045. (let ((org-odt-suppress-xref t))
  1046. (org-odt-format-link headline (concat "#" href))))
  1047. (defun org-odt-format-toc-item (toc-entry level org-last-level)
  1048. (let ((style (format "Contents_20_%d"
  1049. (+ level (or (org-lparse-get 'TOPLEVEL-HLEVEL) 1) -1))))
  1050. (insert "\n" (org-odt-format-stylized-paragraph style toc-entry) "\n")))
  1051. ;; Following variable is let bound during 'ORG-LINK callback. See
  1052. ;; org-html.el
  1053. (defvar org-lparse-link-description-is-image nil)
  1054. (defun org-odt-format-link (desc href &optional attr)
  1055. (cond
  1056. ((and (= (string-to-char href) ?#) (not org-odt-suppress-xref))
  1057. (setq href (concat org-export-odt-bookmark-prefix (substring href 1)))
  1058. (let ((xref-format "text"))
  1059. (when (numberp desc)
  1060. (setq desc (format "%d" desc) xref-format "number"))
  1061. (org-odt-format-tags
  1062. '("<text:bookmark-ref text:reference-format=\"%s\" text:ref-name=\"%s\">" .
  1063. "</text:bookmark-ref>")
  1064. desc xref-format href)))
  1065. (org-lparse-link-description-is-image
  1066. (org-odt-format-tags
  1067. '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</draw:a>")
  1068. desc href (or attr "")))
  1069. (t
  1070. (org-odt-format-tags
  1071. '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</text:a>")
  1072. desc href (or attr "")))))
  1073. (defun org-odt-format-spaces (n)
  1074. (cond
  1075. ((= n 1) " ")
  1076. ((> n 1) (concat
  1077. " " (org-odt-format-tags "<text:s text:c=\"%d\"/>" "" (1- n))))
  1078. (t "")))
  1079. (defun org-odt-format-tabs (&optional n)
  1080. (let ((tab "<text:tab/>")
  1081. (n (or n 1)))
  1082. (insert tab)))
  1083. (defun org-odt-format-line-break ()
  1084. (org-odt-format-tags "<text:line-break/>" ""))
  1085. (defun org-odt-format-horizontal-line ()
  1086. (org-odt-format-stylized-paragraph 'horizontal-line ""))
  1087. (defun org-odt-encode-plain-text (line &optional no-whitespace-filling)
  1088. (setq line (org-xml-encode-plain-text line))
  1089. (if no-whitespace-filling line
  1090. (org-odt-fill-tabs-and-spaces line)))
  1091. (defun org-odt-format-line (line)
  1092. (case org-lparse-dyn-current-environment
  1093. (fixedwidth (concat
  1094. (org-odt-format-stylized-paragraph
  1095. 'fixedwidth (org-odt-encode-plain-text line)) "\n"))
  1096. (t (concat line "\n"))))
  1097. (defun org-odt-format-comment (fmt &rest args)
  1098. (let ((comment (apply 'format fmt args)))
  1099. (format "\n<!-- %s -->\n" comment)))
  1100. (defun org-odt-format-org-entity (wd)
  1101. (org-entity-get-representation wd 'utf8))
  1102. (defun org-odt-fill-tabs-and-spaces (line)
  1103. (replace-regexp-in-string
  1104. "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
  1105. (cond
  1106. ((string= s "\t") (org-odt-format-tabs))
  1107. (t (org-odt-format-spaces (length s))))) line))
  1108. (defcustom org-export-odt-fontify-srcblocks t
  1109. "Specify whether or not source blocks need to be fontified.
  1110. Turn this option on if you want to colorize the source code
  1111. blocks in the exported file. For colorization to work, you need
  1112. to make available an enhanced version of `htmlfontify' library."
  1113. :type 'boolean
  1114. :group 'org-export-odt)
  1115. (defun org-odt-format-source-line-with-line-number-and-label
  1116. (line rpllbl num fontifier par-style)
  1117. (let ((keep-label (not (numberp rpllbl)))
  1118. (ref (org-find-text-property-in-string 'org-coderef line)))
  1119. (setq line (concat line (and keep-label ref (format "(%s)" ref))))
  1120. (setq line (funcall fontifier line))
  1121. (when ref
  1122. (setq line (org-odt-format-target line (concat "coderef-" ref))))
  1123. (setq line (org-odt-format-stylized-paragraph par-style line))
  1124. (if (not num) line
  1125. (org-odt-format-tags '("<text:list-item>" . "</text:list-item>") line))))
  1126. (defun org-odt-format-source-code-or-example-plain
  1127. (lines lang caption textareap cols rows num cont rpllbl fmt)
  1128. "Format source or example blocks much like fixedwidth blocks.
  1129. Use this when `org-export-odt-fontify-srcblocks' option is turned
  1130. off."
  1131. (let* ((lines (org-split-string lines "[\r\n]"))
  1132. (line-count (length lines))
  1133. (i 0))
  1134. (mapconcat
  1135. (lambda (line)
  1136. (incf i)
  1137. (org-odt-format-source-line-with-line-number-and-label
  1138. line rpllbl num 'org-odt-encode-plain-text
  1139. (if (= i line-count) "OrgFixedWidthBlockLastLine"
  1140. "OrgFixedWidthBlock")))
  1141. lines "\n")))
  1142. (defvar org-src-block-paragraph-format
  1143. "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
  1144. <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
  1145. <style:background-image/>
  1146. </style:paragraph-properties>
  1147. <style:text-properties fo:color=\"%s\"/>
  1148. </style:style>"
  1149. "Custom paragraph style for colorized source and example blocks.
  1150. This style is much the same as that of \"OrgFixedWidthBlock\"
  1151. except that the foreground and background colors are set
  1152. according to the default face identified by the `htmlfontify'.")
  1153. (defvar hfy-optimisations)
  1154. (declare-function hfy-face-to-style "htmlfontify" (fn))
  1155. (declare-function hfy-face-or-def-to-name "htmlfontify" (fn))
  1156. (defun org-odt-hfy-face-to-css (fn)
  1157. "Create custom style for face FN.
  1158. When FN is the default face, use it's foreground and background
  1159. properties to create \"OrgSrcBlock\" paragraph style. Otherwise
  1160. use it's color attribute to create a character style whose name
  1161. is obtained from FN. Currently all attributes of FN other than
  1162. color are ignored.
  1163. The style name for a face FN is derived using the following
  1164. operations on the face name in that order - de-dash, CamelCase
  1165. and prefix with \"OrgSrc\". For example,
  1166. `font-lock-function-name-face' is associated with
  1167. \"OrgSrcFontLockFunctionNameFace\"."
  1168. (let* ((css-list (hfy-face-to-style fn))
  1169. (style-name ((lambda (fn)
  1170. (concat "OrgSrc"
  1171. (mapconcat
  1172. 'capitalize (split-string
  1173. (hfy-face-or-def-to-name fn) "-")
  1174. ""))) fn))
  1175. (color-val (cdr (assoc "color" css-list)))
  1176. (background-color-val (cdr (assoc "background" css-list)))
  1177. (style (and org-export-odt-create-custom-styles-for-srcblocks
  1178. (cond
  1179. ((eq fn 'default)
  1180. (format org-src-block-paragraph-format
  1181. background-color-val color-val))
  1182. (t
  1183. (format
  1184. "
  1185. <style:style style:name=\"%s\" style:family=\"text\">
  1186. <style:text-properties fo:color=\"%s\"/>
  1187. </style:style>" style-name color-val))))))
  1188. (cons style-name style)))
  1189. (defun org-odt-insert-custom-styles-for-srcblocks (styles)
  1190. "Save STYLES used for colorizing of source blocks.
  1191. Update styles.xml with styles that were collected as part of
  1192. `org-odt-hfy-face-to-css' callbacks."
  1193. (when styles
  1194. (with-current-buffer
  1195. (find-file-noselect (expand-file-name "styles.xml") t)
  1196. (goto-char (point-min))
  1197. (when (re-search-forward "</office:styles>" nil t)
  1198. (goto-char (match-beginning 0))
  1199. (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n")))))
  1200. (defun org-odt-format-source-code-or-example-colored
  1201. (lines lang caption textareap cols rows num cont rpllbl fmt)
  1202. "Format source or example blocks using `htmlfontify-string'.
  1203. Use this routine when `org-export-odt-fontify-srcblocks' option
  1204. is turned on."
  1205. (let* ((lang-m (and lang (or (cdr (assoc lang org-src-lang-modes)) lang)))
  1206. (mode (and lang-m (intern (concat (if (symbolp lang-m)
  1207. (symbol-name lang-m)
  1208. lang-m) "-mode"))))
  1209. (org-inhibit-startup t)
  1210. (org-startup-folded nil)
  1211. (lines (with-temp-buffer
  1212. (insert lines)
  1213. (if (functionp mode) (funcall mode) (fundamental-mode))
  1214. (font-lock-fontify-buffer)
  1215. (buffer-string)))
  1216. (hfy-html-quote-regex "\\([<\"&> ]\\)")
  1217. (hfy-html-quote-map '(("\"" "&quot;")
  1218. ("<" "&lt;")
  1219. ("&" "&amp;")
  1220. (">" "&gt;")
  1221. (" " "<text:s/>")
  1222. (" " "<text:tab/>")))
  1223. (hfy-face-to-css 'org-odt-hfy-face-to-css)
  1224. (hfy-optimisations-1 (copy-seq hfy-optimisations))
  1225. (hfy-optimisations (add-to-list 'hfy-optimisations-1
  1226. 'body-text-only))
  1227. (hfy-begin-span-handler
  1228. (lambda (style text-block text-id text-begins-block-p)
  1229. (insert (format "<text:span text:style-name=\"%s\">" style))))
  1230. (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
  1231. (when (fboundp 'htmlfontify-string)
  1232. (let* ((lines (org-split-string lines "[\r\n]"))
  1233. (line-count (length lines))
  1234. (i 0))
  1235. (mapconcat
  1236. (lambda (line)
  1237. (incf i)
  1238. (org-odt-format-source-line-with-line-number-and-label
  1239. line rpllbl num 'htmlfontify-string
  1240. (if (= i line-count) "OrgSrcBlockLastLine" "OrgSrcBlock")))
  1241. lines "\n")))))
  1242. (defun org-odt-format-source-code-or-example (lines lang caption textareap
  1243. cols rows num cont
  1244. rpllbl fmt)
  1245. "Format source or example blocks for export.
  1246. Use `org-odt-format-source-code-or-example-plain' or
  1247. `org-odt-format-source-code-or-example-colored' depending on the
  1248. value of `org-export-odt-fontify-srcblocks."
  1249. (setq lines (org-export-number-lines
  1250. lines 0 0 num cont rpllbl fmt 'preprocess)
  1251. lines (funcall
  1252. (or (and org-export-odt-fontify-srcblocks
  1253. (or (featurep 'htmlfontify)
  1254. ;; htmlfontify.el was introduced in Emacs 23.2
  1255. ;; So load it with some caution
  1256. (require 'htmlfontify nil t))
  1257. (fboundp 'htmlfontify-string)
  1258. 'org-odt-format-source-code-or-example-colored)
  1259. 'org-odt-format-source-code-or-example-plain)
  1260. lines lang caption textareap cols rows num cont rpllbl fmt))
  1261. (if (not num) lines
  1262. (let ((extra (format " text:continue-numbering=\"%s\""
  1263. (if cont "true" "false"))))
  1264. (org-odt-format-tags
  1265. '("<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>"
  1266. . "</text:list>") lines extra))))
  1267. (defun org-odt-remap-stylenames (style-name)
  1268. (or
  1269. (cdr (assoc style-name '(("timestamp-wrapper" . "OrgTimestampWrapper")
  1270. ("timestamp" . "OrgTimestamp")
  1271. ("timestamp-kwd" . "OrgTimestampKeyword")
  1272. ("tag" . "OrgTag")
  1273. ("todo" . "OrgTodo")
  1274. ("done" . "OrgDone")
  1275. ("target" . "OrgTarget"))))
  1276. style-name))
  1277. (defun org-odt-format-fontify (text style &optional id)
  1278. (let* ((style-name
  1279. (cond
  1280. ((stringp style)
  1281. (org-odt-remap-stylenames style))
  1282. ((symbolp style)
  1283. (org-odt-get-style-name-for-entity 'character style))
  1284. ((listp style)
  1285. (assert (< 1 (length style)))
  1286. (let ((parent-style (pop style)))
  1287. (mapconcat (lambda (s)
  1288. ;; (assert (stringp s) t)
  1289. (org-odt-remap-stylenames s)) style "")
  1290. (org-odt-remap-stylenames parent-style)))
  1291. (t (error "Don't how to handle style %s" style)))))
  1292. (org-odt-format-tags
  1293. '("<text:span text:style-name=\"%s\">" . "</text:span>")
  1294. text style-name)))
  1295. (defun org-odt-relocate-relative-path (path dir)
  1296. (if (file-name-absolute-p path) path
  1297. (file-relative-name (expand-file-name path dir)
  1298. (expand-file-name "eyecandy" dir))))
  1299. (defun org-odt-format-inline-image (thefile)
  1300. (let* ((thelink (if (file-name-absolute-p thefile) thefile
  1301. (org-xml-format-href
  1302. (org-odt-relocate-relative-path
  1303. thefile org-current-export-file))))
  1304. (href
  1305. (org-odt-format-tags
  1306. "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
  1307. (if org-export-odt-embed-images
  1308. (org-odt-copy-image-file thefile) thelink))))
  1309. (org-export-odt-format-image thefile href)))
  1310. (defun org-export-odt-format-formula (src href &optional embed-as)
  1311. "Create image tag with source and attributes."
  1312. (save-match-data
  1313. (let* ((caption (org-find-text-property-in-string 'org-caption src))
  1314. (caption (and caption (org-xml-format-desc caption)))
  1315. (label (org-find-text-property-in-string 'org-label src))
  1316. (latex-frag (org-find-text-property-in-string 'org-latex-src src))
  1317. (embed-as (or embed-as
  1318. (and latex-frag
  1319. (org-find-text-property-in-string
  1320. 'org-latex-src-embed-type src))
  1321. (if (or caption label) 'paragraph 'character)))
  1322. width height)
  1323. (when latex-frag
  1324. (setq href (org-propertize href :title "LaTeX Fragment"
  1325. :description latex-frag)))
  1326. (cond
  1327. ((eq embed-as 'character)
  1328. (org-odt-format-entity "InlineFormula" href width height))
  1329. (t
  1330. (org-lparse-end-paragraph)
  1331. (org-lparse-insert-list-table
  1332. `((,(org-odt-format-entity
  1333. (if caption "CaptionedDisplayFormula" "DisplayFormula")
  1334. href width height :caption caption :label nil)
  1335. ,(if (not label) ""
  1336. (org-odt-format-entity-caption label nil "__MathFormula__"))))
  1337. nil nil nil "OrgEquation" nil '((1 "c" 8) (2 "c" 1)))
  1338. (throw 'nextline nil))))))
  1339. (defvar org-odt-embedded-formulas-count 0)
  1340. (defun org-odt-copy-formula-file (path)
  1341. "Returns the internal name of the file"
  1342. (let* ((src-file (expand-file-name
  1343. path (file-name-directory org-current-export-file)))
  1344. (target-dir (format "Formula-%04d/"
  1345. (incf org-odt-embedded-formulas-count)))
  1346. (target-file (concat target-dir "content.xml")))
  1347. (when (not org-lparse-to-buffer)
  1348. (message "Embedding %s as %s ..."
  1349. (substring-no-properties path) target-file)
  1350. (make-directory target-dir)
  1351. (org-odt-create-manifest-file-entry
  1352. "application/vnd.oasis.opendocument.formula" target-dir "1.2")
  1353. (case (org-odt-is-formula-link-p src-file)
  1354. (mathml
  1355. (copy-file src-file target-file 'overwrite))
  1356. (odf
  1357. (org-odt-zip-extract-one src-file "content.xml" target-dir))
  1358. (t
  1359. (error "%s is not a formula file" src-file)))
  1360. (org-odt-create-manifest-file-entry "text/xml" target-file))
  1361. target-file))
  1362. (defun org-odt-format-inline-formula (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:object xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
  1370. (file-name-directory (org-odt-copy-formula-file thefile)))))
  1371. (org-export-odt-format-formula thefile href)))
  1372. (defun org-odt-is-formula-link-p (file)
  1373. (let ((case-fold-search nil))
  1374. (cond
  1375. ((string-match "\\.\\(mathml\\|mml\\)\\'" file)
  1376. 'mathml)
  1377. ((string-match "\\.odf\\'" file)
  1378. 'odf))))
  1379. (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
  1380. descp)
  1381. "Make a OpenDocument link.
  1382. OPT-PLIST is an options list.
  1383. TYPE-1 is the device-type of the link (THIS://foo.html).
  1384. PATH is the path of the link (http://THIS#location).
  1385. FRAGMENT is the fragment part of the link, if any (foo.html#THIS).
  1386. DESC is the link description, if any.
  1387. ATTR is a string of other attributes of the a element."
  1388. (declare (special org-lparse-par-open))
  1389. (save-match-data
  1390. (let* ((may-inline-p
  1391. (and (member type-1 '("http" "https" "file"))
  1392. (org-lparse-should-inline-p path descp)
  1393. (not fragment)))
  1394. (type (if (equal type-1 "id") "file" type-1))
  1395. (filename path)
  1396. (thefile path))
  1397. (cond
  1398. ;; check for inlined images
  1399. ((and (member type '("file"))
  1400. (not fragment)
  1401. (org-file-image-p
  1402. filename org-export-odt-inline-image-extensions)
  1403. (or (eq t org-export-odt-inline-images)
  1404. (and org-export-odt-inline-images (not descp))))
  1405. (org-odt-format-inline-image thefile))
  1406. ;; check for embedded formulas
  1407. ((and (member type '("file"))
  1408. (not fragment)
  1409. (org-odt-is-formula-link-p filename)
  1410. (or (not descp)))
  1411. (org-odt-format-inline-formula thefile))
  1412. ((string= type "coderef")
  1413. (let* ((ref fragment)
  1414. (lineno-or-ref (cdr (assoc ref org-export-code-refs)))
  1415. (desc (and descp desc))
  1416. (org-odt-suppress-xref nil)
  1417. (href (org-xml-format-href (concat "#coderef-" ref))))
  1418. (cond
  1419. ((and (numberp lineno-or-ref) (not desc))
  1420. (org-odt-format-link lineno-or-ref href))
  1421. ((and (numberp lineno-or-ref) desc
  1422. (string-match (regexp-quote (concat "(" ref ")")) desc))
  1423. (format (replace-match "%s" t t desc)
  1424. (org-odt-format-link lineno-or-ref href)))
  1425. (t
  1426. (setq desc (format
  1427. (if (and desc (string-match
  1428. (regexp-quote (concat "(" ref ")"))
  1429. desc))
  1430. (replace-match "%s" t t desc)
  1431. (or desc "%s"))
  1432. lineno-or-ref))
  1433. (org-odt-format-link (org-xml-format-desc desc) href)))))
  1434. (t
  1435. (when (string= type "file")
  1436. (setq thefile
  1437. (cond
  1438. ((file-name-absolute-p path)
  1439. (concat "file://" (expand-file-name path)))
  1440. (t (org-odt-relocate-relative-path
  1441. thefile org-current-export-file)))))
  1442. (when (and (member type '("" "http" "https" "file")) fragment)
  1443. (setq thefile (concat thefile "#" fragment)))
  1444. (setq thefile (org-xml-format-href thefile))
  1445. (when (not (member type '("" "file")))
  1446. (setq thefile (concat type ":" thefile)))
  1447. (let ((org-odt-suppress-xref nil))
  1448. (org-odt-format-link
  1449. (org-xml-format-desc desc) thefile attr)))))))
  1450. (defun org-odt-format-heading (text level &optional id)
  1451. (let* ((text (if id (org-odt-format-target text id) text)))
  1452. (org-odt-format-tags
  1453. '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
  1454. "</text:h>") text level level)))
  1455. (defun org-odt-format-headline (title extra-targets tags
  1456. &optional snumber level)
  1457. (concat
  1458. (org-lparse-format 'EXTRA-TARGETS extra-targets)
  1459. ;; No need to generate section numbers. They are auto-generated by
  1460. ;; the application
  1461. ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
  1462. title
  1463. (and tags (concat (org-lparse-format 'SPACES 3)
  1464. (org-lparse-format 'ORG-TAGS tags)))))
  1465. (defun org-odt-format-anchor (text name &optional class)
  1466. (org-odt-format-target text name))
  1467. (defun org-odt-format-bookmark (text id)
  1468. (if id
  1469. (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id)
  1470. text))
  1471. (defun org-odt-format-target (text id)
  1472. (let ((name (concat org-export-odt-bookmark-prefix id)))
  1473. (concat
  1474. (and id (org-odt-format-tags
  1475. "<text:bookmark-start text:name=\"%s\"/>" "" name))
  1476. (org-odt-format-bookmark text id)
  1477. (and id (org-odt-format-tags
  1478. "<text:bookmark-end text:name=\"%s\"/>" "" name)))))
  1479. (defun org-odt-format-footnote (n def)
  1480. (let ((id (concat "fn" n))
  1481. (note-class "footnote")
  1482. (par-style "Footnote"))
  1483. (org-odt-format-tags
  1484. '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
  1485. "</text:note>")
  1486. (concat
  1487. (org-odt-format-tags
  1488. '("<text:note-citation>" . "</text:note-citation>")
  1489. n)
  1490. (org-odt-format-tags
  1491. '("<text:note-body>" . "</text:note-body>")
  1492. def))
  1493. id note-class)))
  1494. (defun org-odt-format-footnote-reference (n def refcnt)
  1495. (if (= refcnt 1)
  1496. (org-odt-format-footnote n def)
  1497. (org-odt-format-footnote-ref n)))
  1498. (defun org-odt-format-footnote-ref (n)
  1499. (let ((note-class "footnote")
  1500. (ref-format "text")
  1501. (ref-name (concat "fn" n)))
  1502. (org-odt-format-tags
  1503. '("<text:span text:style-name=\"%s\">" . "</text:span>")
  1504. (org-odt-format-tags
  1505. '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" . "</text:note-ref>")
  1506. n note-class ref-format ref-name)
  1507. "OrgSuperscript")))
  1508. (defun org-odt-get-image-name (file-name)
  1509. (require 'sha1)
  1510. (file-relative-name
  1511. (expand-file-name
  1512. (concat (sha1 file-name) "." (file-name-extension file-name)) "Pictures")))
  1513. (defun org-export-odt-format-image (src href)
  1514. "Create image tag with source and attributes."
  1515. (save-match-data
  1516. (let* ((caption (org-find-text-property-in-string 'org-caption src))
  1517. (caption (and caption (org-xml-format-desc caption)))
  1518. (attr (org-find-text-property-in-string 'org-attributes src))
  1519. (label (org-find-text-property-in-string 'org-label src))
  1520. (latex-frag (org-find-text-property-in-string
  1521. 'org-latex-src src))
  1522. (category (and latex-frag "__DvipngImage__"))
  1523. (attr-plist (org-lparse-get-block-params attr))
  1524. (user-frame-anchor
  1525. (car (assoc-string (plist-get attr-plist :anchor)
  1526. (if (or caption label)
  1527. '(("paragraph") ("page"))
  1528. '(("character") ("paragraph") ("page"))) t)))
  1529. (user-frame-style
  1530. (and user-frame-anchor (plist-get attr-plist :style)))
  1531. (user-frame-attrs
  1532. (and user-frame-anchor (plist-get attr-plist :attributes)))
  1533. (user-frame-params
  1534. (list user-frame-style user-frame-attrs user-frame-anchor))
  1535. (embed-as (cond
  1536. (latex-frag
  1537. (symbol-name
  1538. (or (org-find-text-property-in-string
  1539. 'org-latex-src-embed-type src) 'character)))
  1540. (user-frame-anchor)
  1541. (t "paragraph")))
  1542. (size (org-odt-image-size-from-file
  1543. src (plist-get attr-plist :width)
  1544. (plist-get attr-plist :height)
  1545. (plist-get attr-plist :scale) nil embed-as))
  1546. (width (car size)) (height (cdr size)))
  1547. (when latex-frag
  1548. (setq href (org-propertize href :title "LaTeX Fragment"
  1549. :description latex-frag)))
  1550. (let ((frame-style-handle (concat (and (or caption label) "Captioned")
  1551. embed-as "Image")))
  1552. (org-odt-format-entity
  1553. frame-style-handle href width height
  1554. :caption caption :label label :category category
  1555. :user-frame-params user-frame-params)))))
  1556. (defun org-odt-format-object-description (title description)
  1557. (concat (and title (org-odt-format-tags
  1558. '("<svg:title>" . "</svg:title>")
  1559. (org-odt-encode-plain-text title t)))
  1560. (and description (org-odt-format-tags
  1561. '("<svg:desc>" . "</svg:desc>")
  1562. (org-odt-encode-plain-text description t)))))
  1563. (defun org-odt-format-frame (text width height style &optional
  1564. extra anchor-type)
  1565. (let ((frame-attrs
  1566. (concat
  1567. (if width (format " svg:width=\"%0.2fcm\"" width) "")
  1568. (if height (format " svg:height=\"%0.2fcm\"" height) "")
  1569. extra
  1570. (format " text:anchor-type=\"%s\"" (or anchor-type "paragraph")))))
  1571. (org-odt-format-tags
  1572. '("<draw:frame draw:style-name=\"%s\"%s>" . "</draw:frame>")
  1573. (concat text (org-odt-format-object-description
  1574. (get-text-property 0 :title text)
  1575. (get-text-property 0 :description text)))
  1576. style frame-attrs)))
  1577. (defun org-odt-format-textbox (text width height style &optional
  1578. extra anchor-type)
  1579. (org-odt-format-frame
  1580. (org-odt-format-tags
  1581. '("<draw:text-box %s>" . "</draw:text-box>")
  1582. text (concat (format " fo:min-height=\"%0.2fcm\"" (or height .2))
  1583. (unless width
  1584. (format " fo:min-width=\"%0.2fcm\"" (or width .2)))))
  1585. width nil style extra anchor-type))
  1586. (defun org-odt-format-inlinetask (heading content
  1587. &optional todo priority tags)
  1588. (org-odt-format-stylized-paragraph
  1589. nil (org-odt-format-textbox
  1590. (concat (org-odt-format-stylized-paragraph
  1591. "OrgInlineTaskHeading"
  1592. (org-lparse-format
  1593. 'HEADLINE (concat (org-lparse-format-todo todo) " " heading)
  1594. nil tags))
  1595. content) nil nil "OrgInlineTaskFrame" " style:rel-width=\"100%\"")))
  1596. (defvar org-odt-entity-frame-styles
  1597. '(("CharacterImage" "__Figure__" ("OrgInlineImage" nil "as-char"))
  1598. ("ParagraphImage" "__Figure__" ("OrgDisplayImage" nil "paragraph"))
  1599. ("PageImage" "__Figure__" ("OrgPageImage" nil "page"))
  1600. ("CaptionedParagraphImage" "__Figure__"
  1601. ("OrgCaptionedImage"
  1602. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  1603. ("OrgImageCaptionFrame" nil "paragraph"))
  1604. ("CaptionedPageImage" "__Figure__"
  1605. ("OrgCaptionedImage"
  1606. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  1607. ("OrgPageImageCaptionFrame" nil "page"))
  1608. ("InlineFormula" "__MathFormula__" ("OrgInlineFormula" nil "as-char"))
  1609. ("DisplayFormula" "__MathFormula__" ("OrgDisplayFormula" nil "as-char"))
  1610. ("CaptionedDisplayFormula" "__MathFormula__"
  1611. ("OrgCaptionedFormula" nil "paragraph")
  1612. ("OrgFormulaCaptionFrame" nil "as-char"))))
  1613. (defun org-odt-merge-frame-params(default-frame-params user-frame-params)
  1614. (if (not user-frame-params) default-frame-params
  1615. (assert (= (length default-frame-params) 3))
  1616. (assert (= (length user-frame-params) 3))
  1617. (loop for user-frame-param in user-frame-params
  1618. for default-frame-param in default-frame-params
  1619. collect (or user-frame-param default-frame-param))))
  1620. (defun* org-odt-format-entity (entity href width height
  1621. &key caption label category
  1622. user-frame-params)
  1623. (let* ((entity-style (assoc-string entity org-odt-entity-frame-styles t))
  1624. default-frame-params frame-params)
  1625. (cond
  1626. ((not (or caption label))
  1627. (setq default-frame-params (nth 2 entity-style))
  1628. (setq frame-params (org-odt-merge-frame-params
  1629. default-frame-params user-frame-params))
  1630. (apply 'org-odt-format-frame href width height frame-params))
  1631. (t
  1632. (setq default-frame-params (nth 3 entity-style))
  1633. (setq frame-params (org-odt-merge-frame-params
  1634. default-frame-params user-frame-params))
  1635. (apply 'org-odt-format-textbox
  1636. (org-odt-format-stylized-paragraph
  1637. 'illustration
  1638. (concat
  1639. (apply 'org-odt-format-frame href width height
  1640. (nth 2 entity-style))
  1641. (org-odt-format-entity-caption
  1642. label caption (or category (nth 1 entity-style)))))
  1643. width height frame-params)))))
  1644. (defvar org-odt-embedded-images-count 0)
  1645. (defun org-odt-copy-image-file (path)
  1646. "Returns the internal name of the file"
  1647. (let* ((image-type (file-name-extension path))
  1648. (media-type (format "image/%s" image-type))
  1649. (src-file (expand-file-name
  1650. path (file-name-directory org-current-export-file)))
  1651. (target-dir "Images/")
  1652. (target-file
  1653. (format "%s%04d.%s" target-dir
  1654. (incf org-odt-embedded-images-count) image-type)))
  1655. (when (not org-lparse-to-buffer)
  1656. (message "Embedding %s as %s ..."
  1657. (substring-no-properties path) target-file)
  1658. (when (= 1 org-odt-embedded-images-count)
  1659. (make-directory target-dir)
  1660. (org-odt-create-manifest-file-entry "" target-dir))
  1661. (copy-file src-file target-file 'overwrite)
  1662. (org-odt-create-manifest-file-entry media-type target-file))
  1663. target-file))
  1664. (defvar org-export-odt-image-size-probe-method
  1665. '(emacs imagemagick force)
  1666. "Ordered list of methods by for determining size of an embedded
  1667. image.")
  1668. (defvar org-export-odt-default-image-sizes-alist
  1669. '(("character" . (5 . 0.4))
  1670. ("paragraph" . (5 . 5)))
  1671. "Hardcoded image dimensions one for each of the anchor
  1672. methods.")
  1673. ;; A4 page size is 21.0 by 29.7 cms
  1674. ;; The default page settings has 2cm margin on each of the sides. So
  1675. ;; the effective text area is 17.0 by 25.7 cm
  1676. (defvar org-export-odt-max-image-size '(17.0 . 20.0)
  1677. "Limiting dimensions for an embedded image.")
  1678. (defun org-odt-do-image-size (probe-method file &optional dpi anchor-type)
  1679. (setq dpi (or dpi org-export-odt-pixels-per-inch))
  1680. (setq anchor-type (or anchor-type "paragraph"))
  1681. (flet ((size-in-cms (size-in-pixels)
  1682. (flet ((pixels-to-cms (pixels)
  1683. (let* ((cms-per-inch 2.54)
  1684. (inches (/ pixels dpi)))
  1685. (* cms-per-inch inches))))
  1686. (and size-in-pixels
  1687. (cons (pixels-to-cms (car size-in-pixels))
  1688. (pixels-to-cms (cdr size-in-pixels)))))))
  1689. (case probe-method
  1690. (emacs
  1691. (size-in-cms (ignore-errors (image-size (create-image file) 'pixels))))
  1692. (imagemagick
  1693. (size-in-cms
  1694. (let ((dim (shell-command-to-string
  1695. (format "identify -format \"%%w:%%h\" \"%s\"" file))))
  1696. (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
  1697. (cons (string-to-number (match-string 1 dim))
  1698. (string-to-number (match-string 2 dim)))))))
  1699. (t
  1700. (cdr (assoc-string anchor-type
  1701. org-export-odt-default-image-sizes-alist))))))
  1702. (defun org-odt-image-size-from-file (file &optional user-width
  1703. user-height scale dpi embed-as)
  1704. (unless (file-name-absolute-p file)
  1705. (setq file (expand-file-name
  1706. file (file-name-directory org-current-export-file))))
  1707. (let* (size width height)
  1708. (unless (and user-height user-width)
  1709. (loop for probe-method in org-export-odt-image-size-probe-method
  1710. until size
  1711. do (setq size (org-odt-do-image-size
  1712. probe-method file dpi embed-as)))
  1713. (or size (error "Cannot determine Image size. Aborting ..."))
  1714. (setq width (car size) height (cdr size)))
  1715. (cond
  1716. (scale
  1717. (setq width (* width scale) height (* height scale)))
  1718. ((and user-height user-width)
  1719. (setq width user-width height user-height))
  1720. (user-height
  1721. (setq width (* user-height (/ width height)) height user-height))
  1722. (user-width
  1723. (setq height (* user-width (/ height width)) width user-width))
  1724. (t (ignore)))
  1725. ;; ensure that an embedded image fits comfortably within a page
  1726. (let ((max-width (car org-export-odt-max-image-size))
  1727. (max-height (cdr org-export-odt-max-image-size)))
  1728. (when (or (> width max-width) (> height max-height))
  1729. (let* ((scale1 (/ max-width width))
  1730. (scale2 (/ max-height height))
  1731. (scale (min scale1 scale2)))
  1732. (setq width (* scale width) height (* scale height)))))
  1733. (cons width height)))
  1734. (defvar org-odt-entity-labels-alist nil
  1735. "Associate Labels with the Labeled entities.
  1736. Each element of the alist is of the form (LABEL-NAME
  1737. CATEGORY-NAME SEQNO LABEL-STYLE-NAME). LABEL-NAME is same as
  1738. that specified by \"#+LABEL: ...\" line. CATEGORY-NAME is the
  1739. type of the entity that LABEL-NAME is attached to. CATEGORY-NAME
  1740. can be one of \"Table\", \"Figure\" or \"Equation\". SEQNO is
  1741. the unique number assigned to the referenced entity on a
  1742. per-CATEGORY basis. It is generated sequentially and is 1-based.
  1743. LABEL-STYLE-NAME is a key `org-odt-label-styles'.
  1744. See `org-odt-add-label-definition' and
  1745. `org-odt-fixup-label-references'.")
  1746. (defvar org-odt-entity-counts-plist nil
  1747. "Plist of running counters of SEQNOs for each of the CATEGORY-NAMEs.
  1748. See `org-odt-entity-labels-alist' for known CATEGORY-NAMEs.")
  1749. (defvar org-odt-label-styles
  1750. '(("text" "(%n)" "text" "(%n)")
  1751. ("category-and-value" "%e %n%c" "category-and-value" "%e %n"))
  1752. "Specify how labels are applied and referenced.
  1753. This is an alist where each element is of the
  1754. form (LABEL-STYLE-NAME LABEL-ATTACH-FMT LABEL-REF-MODE
  1755. LABEL-REF-FMT).
  1756. LABEL-ATTACH-FMT controls how labels and captions are attached to
  1757. an entity. It may contain following specifiers - %e, %n and %c.
  1758. %e is replaced with the CATEGORY-NAME. %n is replaced with
  1759. \"<text:sequence ...> SEQNO </text:sequence>\". %c is replaced
  1760. with CAPTION. See `org-odt-format-label-definition'.
  1761. LABEL-REF-MODE and LABEL-REF-FMT controls how label references
  1762. are generated. The following XML is generated for a label
  1763. reference - \"<text:sequence-ref
  1764. text:reference-format=\"LABEL-REF-MODE\" ...> LABEL-REF-FMT
  1765. </text:sequence-ref>\". LABEL-REF-FMT may contain following
  1766. specifiers - %e and %n. %e is replaced with the CATEGORY-NAME.
  1767. %n is replaced with SEQNO. See
  1768. `org-odt-format-label-reference'.")
  1769. (defvar org-odt-category-map-alist
  1770. '(("__Table__" "Table" "category-and-value")
  1771. ("__Figure__" "Figure" "category-and-value")
  1772. ("__MathFormula__" "Equation" "text")
  1773. ("__DvipngImage__" "Equation" "category-and-value"))
  1774. "Map a CATEGORY-HANDLE to CATEGORY-NAME and LABEL-STYLE.
  1775. This is an alist where each element is of the form
  1776. \\(CATEGORY-HANDLE CATEGORY-NAME LABEL-STYLE\\). CATEGORY_HANDLE
  1777. could either be one of the internal handles (as seen above) or be
  1778. derived from the \"#+LABEL:<label-name>\" specification. See
  1779. `org-export-odt-get-category-from-label'. CATEGORY-NAME and
  1780. LABEL-STYLE are used for generating ODT labels. See
  1781. `org-odt-label-styles'.")
  1782. (defvar org-export-odt-user-categories
  1783. '("Illustration" "Table" "Text" "Drawing" "Equation" "Figure"))
  1784. (defvar org-export-odt-get-category-from-label nil
  1785. "Should category of label be inferred from label itself.
  1786. When this option is non-nil, a label is parsed in to two
  1787. component parts delimited by a \":\" (colon) as shown here -
  1788. #+LABEL:[CATEGORY-HANDLE:]EXTRA. The CATEGORY-HANDLE is mapped
  1789. to a CATEGORY-NAME and LABEL-STYLE using
  1790. `org-odt-category-map-alist'. (If no such map is provided and
  1791. CATEGORY-NAME is set to CATEGORY-HANDLE and LABEL-STYLE is set to
  1792. \"category-and-value\"). If CATEGORY-NAME so obtained is listed
  1793. under `org-export-odt-user-categories' then the user specified
  1794. styles are used. Otherwise styles as determined by the internal
  1795. CATEGORY-HANDLE is used. See
  1796. `org-odt-get-label-category-and-style' for details.")
  1797. (defun org-odt-get-label-category-and-style (label default-category)
  1798. "See `org-export-odt-get-category-from-label'."
  1799. (let ((default-category-map
  1800. (assoc default-category org-odt-category-map-alist))
  1801. user-category user-category-map category)
  1802. (cond
  1803. ((not org-export-odt-get-category-from-label)
  1804. default-category-map)
  1805. ((not (setq user-category
  1806. (save-match-data
  1807. (and (string-match "\\`\\(.*\\):.+" label)
  1808. (match-string 1 label)))))
  1809. default-category-map)
  1810. (t
  1811. (setq user-category-map
  1812. (or (assoc user-category org-odt-category-map-alist)
  1813. (list nil user-category "category-and-value"))
  1814. category (nth 1 user-category-map))
  1815. (if (member category org-export-odt-user-categories)
  1816. user-category-map
  1817. default-category-map)))))
  1818. (defun org-odt-add-label-definition (label default-category)
  1819. "Create an entry in `org-odt-entity-labels-alist' and return it."
  1820. (setq label (substring-no-properties label))
  1821. (let* ((label-props (org-odt-get-label-category-and-style
  1822. label default-category))
  1823. (category (nth 1 label-props))
  1824. (counter category)
  1825. (label-style (nth 2 label-props))
  1826. (sequence-var (intern (mapconcat
  1827. 'downcase
  1828. (org-split-string counter) "-")))
  1829. (seqno (1+ (or (plist-get org-odt-entity-counts-plist sequence-var)
  1830. 0)))
  1831. (label-props (list label category seqno label-style)))
  1832. (setq org-odt-entity-counts-plist
  1833. (plist-put org-odt-entity-counts-plist sequence-var seqno))
  1834. (push label-props org-odt-entity-labels-alist)
  1835. label-props))
  1836. (defun org-odt-format-label-definition (caption label category seqno label-style)
  1837. (assert label)
  1838. (format-spec
  1839. (cadr (assoc-string label-style org-odt-label-styles t))
  1840. `((?e . ,category)
  1841. (?n . ,(org-odt-format-tags
  1842. '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" . "</text:sequence>")
  1843. (format "%d" seqno) label category category))
  1844. (?c . ,(or (and caption (concat ": " caption)) "")))))
  1845. (defun org-odt-format-label-reference (label category seqno label-style)
  1846. (assert label)
  1847. (save-match-data
  1848. (let* ((fmt (cddr (assoc-string label-style org-odt-label-styles t)))
  1849. (fmt1 (car fmt))
  1850. (fmt2 (cadr fmt)))
  1851. (org-odt-format-tags
  1852. '("<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">"
  1853. . "</text:sequence-ref>")
  1854. (format-spec fmt2 `((?e . ,category)
  1855. (?n . ,(format "%d" seqno)))) fmt1 label))))
  1856. (defun org-odt-fixup-label-references ()
  1857. (goto-char (point-min))
  1858. (while (re-search-forward
  1859. "<text:sequence-ref text:ref-name=\"\\([^\"]+\\)\">[ \t\n]*</text:sequence-ref>"
  1860. nil t)
  1861. (let* ((label (match-string 1))
  1862. (label-def (assoc label org-odt-entity-labels-alist))
  1863. (rpl (and label-def
  1864. (apply 'org-odt-format-label-reference label-def))))
  1865. (if rpl (replace-match rpl t t)
  1866. (org-lparse-warn
  1867. (format "Unable to resolve reference to label \"%s\"" label))))))
  1868. (defun org-odt-format-entity-caption (label caption category)
  1869. (or (and label
  1870. (apply 'org-odt-format-label-definition
  1871. caption (org-odt-add-label-definition label category)))
  1872. caption ""))
  1873. (defun org-odt-format-tags (tag text &rest args)
  1874. (let ((prefix (when org-lparse-encode-pending "@"))
  1875. (suffix (when org-lparse-encode-pending "@")))
  1876. (apply 'org-lparse-format-tags tag text prefix suffix args)))
  1877. (defvar org-odt-manifest-file-entries nil)
  1878. (defun org-odt-init-outfile (filename)
  1879. (unless (executable-find "zip")
  1880. ;; Not at all OSes ship with zip by default
  1881. (error "Executable \"zip\" needed for creating OpenDocument files"))
  1882. (let* ((outdir (make-temp-file
  1883. (format org-export-odt-tmpdir-prefix org-lparse-backend) t))
  1884. (content-file (expand-file-name "content.xml" outdir)))
  1885. ;; init conten.xml
  1886. (with-current-buffer (find-file-noselect content-file t))
  1887. ;; reset variables
  1888. (setq org-odt-manifest-file-entries nil
  1889. org-odt-embedded-images-count 0
  1890. org-odt-embedded-formulas-count 0
  1891. org-odt-section-count 0
  1892. org-odt-entity-labels-alist nil
  1893. org-odt-list-stack-stashed nil
  1894. org-odt-entity-counts-plist nil)
  1895. content-file))
  1896. (defcustom org-export-odt-prettify-xml nil
  1897. "Specify whether or not the xml output should be prettified.
  1898. When this option is turned on, `indent-region' is run on all
  1899. component xml buffers before they are saved. Turn this off for
  1900. regular use. Turn this on if you need to examine the xml
  1901. visually."
  1902. :group 'org-export-odt
  1903. :type 'boolean)
  1904. (defvar hfy-user-sheet-assoc) ; bound during org-do-lparse
  1905. (defun org-odt-save-as-outfile (target opt-plist)
  1906. ;; write meta file
  1907. (org-odt-update-meta-file opt-plist)
  1908. ;; write styles file
  1909. (when (equal org-lparse-backend 'odt)
  1910. (org-odt-update-styles-file opt-plist))
  1911. ;; create mimetype file
  1912. (let ((mimetype (org-odt-write-mimetype-file org-lparse-backend)))
  1913. (org-odt-create-manifest-file-entry mimetype "/" "1.2"))
  1914. ;; create a manifest entry for content.xml
  1915. (org-odt-create-manifest-file-entry "text/xml" "content.xml")
  1916. ;; write out the manifest entries before zipping
  1917. (org-odt-write-manifest-file)
  1918. (let ((xml-files '("mimetype" "META-INF/manifest.xml" "content.xml"
  1919. "meta.xml"))
  1920. (zipdir default-directory))
  1921. (when (equal org-lparse-backend 'odt)
  1922. (push "styles.xml" xml-files))
  1923. (message "Switching to directory %s" (expand-file-name zipdir))
  1924. ;; save all xml files
  1925. (mapc (lambda (file)
  1926. (with-current-buffer
  1927. (find-file-noselect (expand-file-name file) t)
  1928. ;; prettify output if needed
  1929. (when org-export-odt-prettify-xml
  1930. (indent-region (point-min) (point-max)))
  1931. (save-buffer 0)))
  1932. xml-files)
  1933. (let* ((target-name (file-name-nondirectory target))
  1934. (target-dir (file-name-directory target))
  1935. (cmds `(("zip" "-mX0" ,target-name "mimetype")
  1936. ("zip" "-rmTq" ,target-name "."))))
  1937. (when (file-exists-p target)
  1938. ;; FIXME: If the file is locked this throws a cryptic error
  1939. (delete-file target))
  1940. (let ((coding-system-for-write 'no-conversion) exitcode err-string)
  1941. (message "Creating odt file...")
  1942. (mapc
  1943. (lambda (cmd)
  1944. (message "Running %s" (mapconcat 'identity cmd " "))
  1945. (setq err-string
  1946. (with-output-to-string
  1947. (setq exitcode
  1948. (apply 'call-process (car cmd)
  1949. nil standard-output nil (cdr cmd)))))
  1950. (or (zerop exitcode)
  1951. (ignore (message "%s" err-string))
  1952. (error "Unable to create odt file (%S)" exitcode)))
  1953. cmds))
  1954. ;; move the file from outdir to target-dir
  1955. (rename-file target-name target-dir)
  1956. ;; kill all xml buffers
  1957. (mapc (lambda (file)
  1958. (kill-buffer
  1959. (find-file-noselect (expand-file-name file zipdir) t)))
  1960. xml-files)
  1961. (delete-directory zipdir)))
  1962. (message "Created %s" target)
  1963. (set-buffer (find-file-noselect target t)))
  1964. (defconst org-odt-manifest-file-entry-tag
  1965. "
  1966. <manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
  1967. (defun org-odt-create-manifest-file-entry (&rest args)
  1968. (push args org-odt-manifest-file-entries))
  1969. (defun org-odt-write-manifest-file ()
  1970. (make-directory "META-INF")
  1971. (let ((manifest-file (expand-file-name "META-INF/manifest.xml")))
  1972. (with-current-buffer
  1973. (find-file-noselect manifest-file t)
  1974. (insert
  1975. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  1976. <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
  1977. (mapc
  1978. (lambda (file-entry)
  1979. (let* ((version (nth 2 file-entry))
  1980. (extra (if version
  1981. (format " manifest:version=\"%s\"" version)
  1982. "")))
  1983. (insert
  1984. (format org-odt-manifest-file-entry-tag
  1985. (nth 0 file-entry) (nth 1 file-entry) extra))))
  1986. org-odt-manifest-file-entries)
  1987. (insert "\n</manifest:manifest>"))))
  1988. (defun org-odt-update-meta-file (opt-plist)
  1989. (let ((date (org-odt-format-date (plist-get opt-plist :date)))
  1990. (author (or (plist-get opt-plist :author) ""))
  1991. (email (plist-get opt-plist :email))
  1992. (keywords (plist-get opt-plist :keywords))
  1993. (description (plist-get opt-plist :description))
  1994. (title (plist-get opt-plist :title)))
  1995. (write-region
  1996. (concat
  1997. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  1998. <office:document-meta
  1999. xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
  2000. xmlns:xlink=\"http://www.w3.org/1999/xlink\"
  2001. xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
  2002. xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
  2003. xmlns:ooo=\"http://openoffice.org/2004/office\"
  2004. office:version=\"1.2\">
  2005. <office:meta>" "\n"
  2006. (org-odt-format-author)
  2007. (org-odt-format-tags
  2008. '("\n<meta:initial-creator>" . "</meta:initial-creator>") author)
  2009. (org-odt-format-tags '("\n<dc:date>" . "</dc:date>") date)
  2010. (org-odt-format-tags
  2011. '("\n<meta:creation-date>" . "</meta:creation-date>") date)
  2012. (org-odt-format-tags '("\n<meta:generator>" . "</meta:generator>")
  2013. (when org-export-creator-info
  2014. (format "Org-%s/Emacs-%s"
  2015. org-version emacs-version)))
  2016. (org-odt-format-tags '("\n<meta:keyword>" . "</meta:keyword>") keywords)
  2017. (org-odt-format-tags '("\n<dc:subject>" . "</dc:subject>") description)
  2018. (org-odt-format-tags '("\n<dc:title>" . "</dc:title>") title)
  2019. "\n"
  2020. " </office:meta>" "</office:document-meta>")
  2021. nil (expand-file-name "meta.xml")))
  2022. ;; create a manifest entry for meta.xml
  2023. (org-odt-create-manifest-file-entry "text/xml" "meta.xml"))
  2024. (defun org-odt-update-styles-file (opt-plist)
  2025. ;; write styles file
  2026. (let ((styles-file (plist-get opt-plist :odt-styles-file)))
  2027. (org-odt-copy-styles-file (and styles-file
  2028. (read (org-trim styles-file)))))
  2029. ;; Update styles.xml - take care of outline numbering
  2030. (with-current-buffer
  2031. (find-file-noselect (expand-file-name "styles.xml") t)
  2032. ;; Don't make automatic backup of styles.xml file. This setting
  2033. ;; prevents the backed-up styles.xml file from being zipped in to
  2034. ;; odt file. This is more of a hackish fix. Better alternative
  2035. ;; would be to fix the zip command so that the output odt file
  2036. ;; includes only the needed files and excludes any auto-generated
  2037. ;; extra files like backups and auto-saves etc etc. Note that
  2038. ;; currently the zip command zips up the entire temp directory so
  2039. ;; that any auto-generated files created under the hood ends up in
  2040. ;; the resulting odt file.
  2041. (set (make-local-variable 'backup-inhibited) t)
  2042. ;; Import local setting of `org-export-with-section-numbers'
  2043. (org-lparse-bind-local-variables opt-plist)
  2044. (org-odt-configure-outline-numbering
  2045. (if org-export-with-section-numbers org-export-headline-levels 0)))
  2046. ;; Write custom styles for source blocks
  2047. (org-odt-insert-custom-styles-for-srcblocks
  2048. (mapconcat
  2049. (lambda (style)
  2050. (format " %s\n" (cddr style)))
  2051. hfy-user-sheet-assoc "")))
  2052. (defun org-odt-write-mimetype-file (format)
  2053. ;; create mimetype file
  2054. (let ((mimetype
  2055. (case format
  2056. (odt "application/vnd.oasis.opendocument.text")
  2057. (odf "application/vnd.oasis.opendocument.formula")
  2058. (t (error "Unknown OpenDocument backend %S" org-lparse-backend)))))
  2059. (write-region mimetype nil (expand-file-name "mimetype"))
  2060. mimetype))
  2061. (defun org-odt-finalize-outfile ()
  2062. (org-odt-delete-empty-paragraphs))
  2063. (defun org-odt-delete-empty-paragraphs ()
  2064. (goto-char (point-min))
  2065. (let ((open "<text:p[^>]*>")
  2066. (close "</text:p>"))
  2067. (while (re-search-forward (format "%s[ \r\n\t]*%s" open close) nil t)
  2068. (replace-match ""))))
  2069. (defcustom org-export-odt-convert-processes
  2070. '(("BasicODConverter"
  2071. ("soffice" "-norestore" "-invisible" "-headless"
  2072. "\"macro:///BasicODConverter.Main.Convert(%I,%f,%O)\""))
  2073. ("unoconv"
  2074. ("unoconv" "-f" "%f" "-o" "%d" "%i")))
  2075. "Specify a list of document converters and their usage.
  2076. The converters in this list are offered as choices while
  2077. customizing `org-export-odt-convert-process'.
  2078. This variable is an alist where each element is of the
  2079. form (CONVERTER-NAME CONVERTER-PROCESS). CONVERTER-NAME is name
  2080. of the converter. CONVERTER-PROCESS specifies the command-line
  2081. syntax of the converter and is of the form (CONVERTER-PROGRAM
  2082. ARG1 ARG2 ...). CONVERTER-PROGRAM is the name of the executable.
  2083. ARG1, ARG2 etc are command line options that are passed to
  2084. CONVERTER-PROGRAM. Format specifiers can be used in the ARGs and
  2085. they are interpreted as below:
  2086. %i input file name in full
  2087. %I input file name as a URL
  2088. %f format of the output file
  2089. %o output file name in full
  2090. %O output file name as a URL
  2091. %d output dir in full
  2092. %D output dir as a URL."
  2093. :group 'org-export-odt
  2094. :type
  2095. '(choice
  2096. (const :tag "None" nil)
  2097. (alist :tag "Converters"
  2098. :key-type (string :tag "Converter Name")
  2099. :value-type (group (cons (string :tag "Executable")
  2100. (repeat (string :tag "Command line args")))))))
  2101. (defcustom org-export-odt-convert-process nil
  2102. "Use this converter to convert from \"odt\" format to other formats.
  2103. During customization, the list of converter names are populated
  2104. from `org-export-odt-convert-processes'."
  2105. :group 'org-export-odt
  2106. :type '(choice :convert-widget
  2107. (lambda (w)
  2108. (apply 'widget-convert (widget-type w)
  2109. (eval (car (widget-get w :args)))))
  2110. `((const :tag "None" nil)
  2111. ,@(mapcar (lambda (c)
  2112. `(const :tag ,(car c) ,(car c)))
  2113. org-export-odt-convert-processes))))
  2114. (defcustom org-export-odt-convert-capabilities
  2115. '(("Text"
  2116. ("odt" "ott" "doc" "rtf")
  2117. (("pdf" "pdf") ("odt" "odt") ("xhtml" "html") ("rtf" "rtf")
  2118. ("ott" "ott") ("doc" "doc") ("ooxml" "xml") ("html" "html")))
  2119. ("Web"
  2120. ("html" "xhtml") (("pdf" "pdf") ("odt" "txt") ("html" "html")))
  2121. ("Spreadsheet"
  2122. ("ods" "ots" "xls" "csv")
  2123. (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv")
  2124. ("ods" "ods") ("xls" "xls") ("xhtml" "xhtml") ("ooxml" "xml")))
  2125. ("Presentation"
  2126. ("odp" "otp" "ppt")
  2127. (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("xhtml" "xml")
  2128. ("otp" "otp") ("ppt" "ppt") ("odg" "odg") ("html" "html"))))
  2129. "Specify input and output formats of `org-export-odt-convert-process'.
  2130. More correctly, specify the set of input and output formats that
  2131. the user is actually interested in.
  2132. This variable is an alist where each element is of the
  2133. form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
  2134. INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
  2135. alist where each element is of the form (OUTPUT-FMT
  2136. OUTPUT-FILE-EXTENSION).
  2137. The variable is interpreted as follows:
  2138. `org-export-odt-convert-process' can take any document that is in
  2139. INPUT-FMT-LIST and produce any document that is in the
  2140. OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
  2141. OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
  2142. serves dual purposes:
  2143. - It is used for populating completion candidates during
  2144. `org-export-odt-convert' commands.
  2145. - It is used as the value of \"%f\" specifier in
  2146. `org-export-odt-convert-process'.
  2147. DOCUMENT-CLASS is used to group a set of file formats in
  2148. INPUT-FMT-LIST in to a single class.
  2149. Note that this variable inherently captures how LibreOffice based
  2150. converters work. LibreOffice maps documents of various formats
  2151. to classes like Text, Web, Spreadsheet, Presentation etc and
  2152. allow document of a given class (irrespective of it's source
  2153. format) to be converted to any of the export formats associated
  2154. with that class.
  2155. See default setting of this variable for an typical
  2156. configuration."
  2157. :group 'org-export-odt
  2158. :type
  2159. '(choice
  2160. (const :tag "None" nil)
  2161. (alist :key-type (string :tag "Document Class")
  2162. :value-type
  2163. (group (repeat :tag "Input formats" (string :tag "Input format"))
  2164. (alist :tag "Output formats"
  2165. :key-type (string :tag "Output format")
  2166. :value-type
  2167. (group (string :tag "Output file extension")))))))
  2168. (declare-function org-create-math-formula "org"
  2169. (latex-frag &optional mathml-file))
  2170. ;;;###autoload
  2171. (defun org-export-odt-convert (&optional in-file out-fmt prefix-arg)
  2172. "Convert IN-FILE to format OUT-FMT using a command line converter.
  2173. IN-FILE is the file to be converted. If unspecified, it defaults
  2174. to variable `buffer-file-name'. OUT-FMT is the desired output
  2175. format. Use `org-export-odt-convert-process' as the converter.
  2176. If PREFIX-ARG is non-nil then the newly converted file is opened
  2177. using `org-open-file'."
  2178. (interactive
  2179. (append (org-lparse-convert-read-params) current-prefix-arg))
  2180. (org-lparse-do-convert in-file out-fmt prefix-arg))
  2181. (defun org-odt-get (what &optional opt-plist)
  2182. (case what
  2183. (BACKEND 'odt)
  2184. (EXPORT-DIR (org-export-directory :html opt-plist))
  2185. (FILE-NAME-EXTENSION "odt")
  2186. (EXPORT-BUFFER-NAME "*Org ODT Export*")
  2187. (ENTITY-CONTROL org-odt-entity-control-callbacks-alist)
  2188. (ENTITY-FORMAT org-odt-entity-format-callbacks-alist)
  2189. (INIT-METHOD 'org-odt-init-outfile)
  2190. (FINAL-METHOD 'org-odt-finalize-outfile)
  2191. (SAVE-METHOD 'org-odt-save-as-outfile)
  2192. (CONVERT-METHOD
  2193. (and org-export-odt-convert-process
  2194. (cadr (assoc-string org-export-odt-convert-process
  2195. org-export-odt-convert-processes t))))
  2196. (CONVERT-CAPABILITIES
  2197. (and org-export-odt-convert-process
  2198. (cadr (assoc-string org-export-odt-convert-process
  2199. org-export-odt-convert-processes t))
  2200. org-export-odt-convert-capabilities))
  2201. (TOPLEVEL-HLEVEL 1)
  2202. (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps)
  2203. (INLINE-IMAGES 'maybe)
  2204. (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
  2205. (PLAIN-TEXT-MAP '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
  2206. (TABLE-FIRST-COLUMN-AS-LABELS nil)
  2207. (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY "," 'superscript))
  2208. (CODING-SYSTEM-FOR-WRITE 'utf-8)
  2209. (CODING-SYSTEM-FOR-SAVE 'utf-8)
  2210. (t (error "Unknown property: %s" what))))
  2211. (defvar org-lparse-latex-fragment-fallback) ; set by org-do-lparse
  2212. (defun org-export-odt-do-preprocess-latex-fragments ()
  2213. "Convert LaTeX fragments to images."
  2214. (let* ((latex-frag-opt (plist-get org-lparse-opt-plist :LaTeX-fragments))
  2215. (latex-frag-opt ; massage the options
  2216. (or (and (member latex-frag-opt '(mathjax t))
  2217. (not (and (fboundp 'org-format-latex-mathml-available-p)
  2218. (org-format-latex-mathml-available-p)))
  2219. (prog1 org-lparse-latex-fragment-fallback
  2220. (org-lparse-warn
  2221. (concat
  2222. "LaTeX to MathML converter not available. "
  2223. (format "Using %S instead."
  2224. org-lparse-latex-fragment-fallback)))))
  2225. latex-frag-opt))
  2226. cache-dir display-msg)
  2227. (cond
  2228. ((eq latex-frag-opt 'dvipng)
  2229. (setq cache-dir "ltxpng/")
  2230. (setq display-msg "Creating LaTeX image %s"))
  2231. ((member latex-frag-opt '(mathjax t))
  2232. (setq latex-frag-opt 'mathml)
  2233. (setq cache-dir "ltxmathml/")
  2234. (setq display-msg "Creating MathML formula %s")))
  2235. (when (and org-current-export-file)
  2236. (org-format-latex
  2237. (concat cache-dir (file-name-sans-extension
  2238. (file-name-nondirectory org-current-export-file)))
  2239. org-current-export-dir nil display-msg
  2240. nil nil latex-frag-opt))))
  2241. (defadvice org-format-latex-as-mathml
  2242. (after org-odt-protect-latex-fragment activate)
  2243. "Encode LaTeX fragment as XML.
  2244. Do this when translation to MathML fails."
  2245. (when (or (not (> (length ad-return-value) 0))
  2246. (get-text-property 0 'org-protected ad-return-value))
  2247. (setq ad-return-value
  2248. (org-propertize (org-odt-encode-plain-text (ad-get-arg 0))
  2249. 'org-protected t))))
  2250. (defun org-export-odt-preprocess-latex-fragments ()
  2251. (when (equal org-export-current-backend 'odt)
  2252. (org-export-odt-do-preprocess-latex-fragments)))
  2253. (defun org-export-odt-preprocess-label-references ()
  2254. (goto-char (point-min))
  2255. (let (label label-components category value pretty-label)
  2256. (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t)
  2257. (org-if-unprotected-at (match-beginning 1)
  2258. (replace-match
  2259. (let ((org-lparse-encode-pending t)
  2260. (label (match-string 1)))
  2261. ;; markup generated below is mostly an eye-candy. At
  2262. ;; pre-processing stage, there is no information on which
  2263. ;; entity a label reference points to. The actual markup
  2264. ;; is generated as part of `org-odt-fixup-label-references'
  2265. ;; which gets called at the fag end of export. By this
  2266. ;; time we would have seen and collected all the label
  2267. ;; definitions in `org-odt-entity-labels-alist'.
  2268. (org-odt-format-tags
  2269. '("<text:sequence-ref text:ref-name=\"%s\">" .
  2270. "</text:sequence-ref>")
  2271. "" (org-add-props label '(org-protected t)))) t t)))))
  2272. ;; process latex fragments as part of
  2273. ;; `org-export-preprocess-after-blockquote-hook'. Note that this hook
  2274. ;; is the one that is closest and well before the call to
  2275. ;; `org-export-attach-captions-and-attributes' in
  2276. ;; `org-export-preprocess-string'. The above arrangement permits
  2277. ;; captions, labels and attributes to be attached to png images
  2278. ;; generated out of latex equations.
  2279. (add-hook 'org-export-preprocess-after-blockquote-hook
  2280. 'org-export-odt-preprocess-latex-fragments)
  2281. (defun org-export-odt-preprocess (parameters)
  2282. (org-export-odt-preprocess-label-references))
  2283. (declare-function archive-zip-extract "arc-mode.el" (archive name))
  2284. (defun org-odt-zip-extract-one (archive member &optional target)
  2285. (require 'arc-mode)
  2286. (let* ((target (or target default-directory))
  2287. (archive (expand-file-name archive))
  2288. (archive-zip-extract
  2289. (list "unzip" "-qq" "-o" "-d" target))
  2290. exit-code command-output)
  2291. (setq command-output
  2292. (with-temp-buffer
  2293. (setq exit-code (archive-zip-extract archive member))
  2294. (buffer-string)))
  2295. (unless (zerop exit-code)
  2296. (message command-output)
  2297. (error "Extraction failed"))))
  2298. (defun org-odt-zip-extract (archive members &optional target)
  2299. (when (atom members) (setq members (list members)))
  2300. (mapc (lambda (member)
  2301. (org-odt-zip-extract-one archive member target))
  2302. members))
  2303. (defun org-odt-copy-styles-file (&optional styles-file)
  2304. ;; Non-availability of styles.xml is not a critical error. For now
  2305. ;; throw an error purely for aesthetic reasons.
  2306. (setq styles-file (or styles-file
  2307. org-export-odt-styles-file
  2308. (expand-file-name "OrgOdtStyles.xml"
  2309. org-odt-styles-dir)
  2310. (error "org-odt: Missing styles file?")))
  2311. (cond
  2312. ((listp styles-file)
  2313. (let ((archive (nth 0 styles-file))
  2314. (members (nth 1 styles-file)))
  2315. (org-odt-zip-extract archive members)
  2316. (mapc
  2317. (lambda (member)
  2318. (when (org-file-image-p member)
  2319. (let* ((image-type (file-name-extension member))
  2320. (media-type (format "image/%s" image-type)))
  2321. (org-odt-create-manifest-file-entry media-type member))))
  2322. members)))
  2323. ((and (stringp styles-file) (file-exists-p styles-file))
  2324. (let ((styles-file-type (file-name-extension styles-file)))
  2325. (cond
  2326. ((string= styles-file-type "xml")
  2327. (copy-file styles-file "styles.xml" t))
  2328. ((member styles-file-type '("odt" "ott"))
  2329. (org-odt-zip-extract styles-file "styles.xml")))))
  2330. (t
  2331. (error (format "Invalid specification of styles.xml file: %S"
  2332. org-export-odt-styles-file))))
  2333. ;; create a manifest entry for styles.xml
  2334. (org-odt-create-manifest-file-entry "text/xml" "styles.xml"))
  2335. (defvar org-export-odt-factory-settings
  2336. "d4328fb9d1b6cb211d4320ff546829f26700dc5e"
  2337. "SHA1 hash of OrgOdtStyles.xml.")
  2338. (defun org-odt-configure-outline-numbering (level)
  2339. "Outline numbering is retained only upto LEVEL.
  2340. To disable outline numbering pass a LEVEL of 0."
  2341. (goto-char (point-min))
  2342. (let ((regex
  2343. "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
  2344. (replacement
  2345. "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
  2346. (while (re-search-forward regex nil t)
  2347. (when (> (string-to-number (match-string 2)) level)
  2348. (replace-match replacement t nil))))
  2349. (save-buffer 0))
  2350. ;;;###autoload
  2351. (defun org-export-as-odf (latex-frag &optional odf-file)
  2352. "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
  2353. Use `org-create-math-formula' to convert LATEX-FRAG first to
  2354. MathML. When invoked as an interactive command, use
  2355. `org-latex-regexps' to infer LATEX-FRAG from currently active
  2356. region. If no LaTeX fragments are found, prompt for it. Push
  2357. MathML source to kill ring, if `org-export-copy-to-kill-ring' is
  2358. non-nil."
  2359. (interactive
  2360. `(,(let (frag)
  2361. (setq frag (and (setq frag (and (region-active-p)
  2362. (buffer-substring (region-beginning)
  2363. (region-end))))
  2364. (loop for e in org-latex-regexps
  2365. thereis (when (string-match (nth 1 e) frag)
  2366. (match-string (nth 2 e) frag)))))
  2367. (read-string "LaTeX Fragment: " frag nil frag))
  2368. ,(let ((odf-filename (expand-file-name
  2369. (concat
  2370. (file-name-sans-extension
  2371. (or (file-name-nondirectory buffer-file-name)))
  2372. "." "odf")
  2373. (file-name-directory buffer-file-name))))
  2374. (message "default val is %s" odf-filename)
  2375. (read-file-name "ODF filename: " nil odf-filename nil
  2376. (file-name-nondirectory odf-filename)))))
  2377. (let* ((org-lparse-backend 'odf)
  2378. org-lparse-opt-plist
  2379. (filename (or odf-file
  2380. (expand-file-name
  2381. (concat
  2382. (file-name-sans-extension
  2383. (or (file-name-nondirectory buffer-file-name)))
  2384. "." "odf")
  2385. (file-name-directory buffer-file-name))))
  2386. (buffer (find-file-noselect (org-odt-init-outfile filename)))
  2387. (coding-system-for-write 'utf-8)
  2388. (save-buffer-coding-system 'utf-8))
  2389. (set-buffer buffer)
  2390. (set-buffer-file-coding-system coding-system-for-write)
  2391. (let ((mathml (org-create-math-formula latex-frag)))
  2392. (unless mathml (error "No Math formula created"))
  2393. (insert mathml)
  2394. (or (org-export-push-to-kill-ring
  2395. (upcase (symbol-name org-lparse-backend)))
  2396. (message "Exporting... done")))
  2397. (org-odt-save-as-outfile filename nil)))
  2398. ;;;###autoload
  2399. (defun org-export-as-odf-and-open ()
  2400. "Export LaTeX fragment as OpenDocument formula and immediately open it.
  2401. Use `org-export-as-odf' to read LaTeX fragment and OpenDocument
  2402. formula file."
  2403. (interactive)
  2404. (org-lparse-and-open
  2405. nil nil nil (call-interactively 'org-export-as-odf)))
  2406. (provide 'org-odt)
  2407. ;;; org-odt.el ends here