org-odt.el 95 KB

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