org-odt.el 94 KB

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