org-odt.el 102 KB

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