org-odt.el 91 KB

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