org-odt.el 102 KB

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