org-odt.el 97 KB

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