org-odt.el 99 KB

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