org-odt.el 102 KB

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