org-odt.el 104 KB

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