org-odt.el 99 KB

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