org-odt.el 99 KB

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