org-odt.el 103 KB

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