org-odt.el 94 KB

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