org-odt.el 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577
  1. ;;; org-odt.el --- OpenDocumentText export for Org-mode
  2. ;; Copyright (C) 2010, 2011
  3. ;; Jambunathan <kjambunathan at gmail dot com>
  4. ;; Author: Jambunathan K <kjambunathan at gmail dot com>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; Homepage: http://orgmode.org
  7. ;; Version: 0.8
  8. ;; This file is not (yet) part of GNU Emacs.
  9. ;; However, it is distributed under the same license.
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  21. ;;
  22. ;;; Commentary:
  23. ;;; Code:
  24. (eval-when-compile (require 'cl))
  25. (require 'org-lparse)
  26. (defun org-odt-end-export ()
  27. ;; remove empty paragraphs
  28. (goto-char (point-min))
  29. (while (re-search-forward
  30. "<text:p\\( text:style-name=\"Text_20_body\"\\)?>[ \r\n\t]*</text:p>"
  31. nil t)
  32. (replace-match ""))
  33. (goto-char (point-min))
  34. ;; Convert whitespace place holders
  35. (goto-char (point-min))
  36. (let (beg end n)
  37. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  38. (setq n (get-text-property beg 'org-whitespace)
  39. end (next-single-property-change beg 'org-whitespace))
  40. (goto-char beg)
  41. (delete-region beg end)
  42. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  43. (make-string n ?x)))))
  44. ;; Remove empty lines at the beginning of the file.
  45. (goto-char (point-min))
  46. (when (looking-at "\\s-+\n") (replace-match ""))
  47. ;; Remove display properties
  48. (remove-text-properties (point-min) (point-max) '(display t)))
  49. (defvar org-odt-suppress-xref nil)
  50. (defconst org-export-odt-special-string-regexps
  51. '(("\\\\-" . "&#x00ad;\\1") ; shy
  52. ("---\\([^-]\\)" . "&#x2014;\\1") ; mdash
  53. ("--\\([^-]\\)" . "&#x2013;\\1") ; ndash
  54. ("\\.\\.\\." . "&#x2026;")) ; hellip
  55. "Regular expressions for special string conversion.")
  56. (defconst org-odt-lib-dir (file-name-directory load-file-name))
  57. (defconst org-odt-data-dir
  58. (let ((dir1 (expand-file-name "../odt" org-odt-lib-dir)) ; git
  59. (dir2 (expand-file-name "./contrib/odt" org-odt-lib-dir))) ; elpa
  60. (cond
  61. ((file-directory-p dir1) dir1)
  62. ((file-directory-p dir2) dir2)
  63. (t (error "Cannot find factory styles file. Check package dir layout"))))
  64. "Directory that holds auxiliary files used by the ODT exporter.
  65. The 'styles' subdir contains the following xml files -
  66. 'OrgOdtStyles.xml' and 'OrgOdtAutomaticStyles.xml' - which are
  67. used as factory settings of `org-export-odt-styles-file' and
  68. `org-export-odt-automatic-styles-file'.
  69. The 'etc/schema' subdir contains rnc files for validating of
  70. OpenDocument xml files.")
  71. (defvar org-odt-file-extensions
  72. '(("odt" . "OpenDocument Text")
  73. ("ott" . "OpenDocument Text Template")
  74. ("odm" . "OpenDocument Master Document")
  75. ("ods" . "OpenDocument Spreadsheet")
  76. ("ots" . "OpenDocument Spreadsheet Template")
  77. ("odg" . "OpenDocument Drawing (Graphics)")
  78. ("otg" . "OpenDocument Drawing Template")
  79. ("odp" . "OpenDocument Presentation")
  80. ("otp" . "OpenDocument Presentation Template")
  81. ("odi" . "OpenDocument Image")
  82. ("odf" . "OpenDocument Formula")
  83. ("odc" . "OpenDocument Chart")
  84. ("doc" . "Microsoft Text")
  85. ("docx" . "Microsoft Text")
  86. ("xls" . "Microsoft Spreadsheet")
  87. ("xlsx" . "Microsoft Spreadsheet")
  88. ("ppt" . "Microsoft Presentation")
  89. ("pptx" . "Microsoft Presentation")))
  90. (defvar org-odt-ms-file-extensions
  91. '(("doc" . "Microsoft Text")
  92. ("docx" . "Microsoft Text")
  93. ("xls" . "Microsoft Spreadsheet")
  94. ("xlsx" . "Microsoft Spreadsheet")
  95. ("ppt" . "Microsoft Presentation")
  96. ("pptx" . "Microsoft Presentation")))
  97. ;; RelaxNG validation of OpenDocument xml files
  98. (eval-after-load 'rng-nxml
  99. '(setq rng-nxml-auto-validate-flag t))
  100. (eval-after-load 'rng-loc
  101. '(add-to-list 'rng-schema-locating-files
  102. (expand-file-name "etc/schema/schemas.xml" org-odt-data-dir)))
  103. (mapc
  104. (lambda (desc)
  105. ;; Let Org open all OpenDocument files using system-registered app
  106. (add-to-list 'org-file-apps
  107. (cons (concat "\\." (car desc) "\\'") 'system))
  108. ;; Let Emacs open all OpenDocument files in archive mode
  109. (add-to-list 'auto-mode-alist
  110. (cons (concat "\\." (car desc) "\\'") 'archive-mode)))
  111. org-odt-file-extensions)
  112. (mapc
  113. (lambda (desc)
  114. ;; Let Org open all Microsoft files using system-registered app
  115. (add-to-list 'org-file-apps
  116. (cons (concat "\\." (car desc) "\\'") 'system)))
  117. org-odt-ms-file-extensions)
  118. ;; register the odt exporter with the pre-processor
  119. (add-to-list 'org-export-backends 'odt)
  120. ;; register the odt exporter with org-lparse library
  121. (org-lparse-register-backend 'odt)
  122. (defun org-odt-unload-function ()
  123. ;; notify org-lparse library on unload
  124. (org-lparse-unregister-backend 'odt)
  125. nil)
  126. (defcustom org-export-odt-automatic-styles-file nil
  127. "Automatic styles for use with ODT exporter.
  128. If unspecified, the file under `org-odt-data-dir' is used."
  129. :type 'file
  130. :group 'org-export-odt)
  131. (defcustom org-export-odt-styles-file nil
  132. "Default styles file for use with ODT export.
  133. Valid values are one of:
  134. 1. nil
  135. 2. path to a styles.xml file
  136. 3. path to a *.odt or a *.ott file
  137. 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
  138. ...))
  139. In case of option 1, an in-built styles.xml is used. See
  140. `org-odt-data-dir' for more information.
  141. In case of option 3, the specified file is unzipped and the
  142. styles.xml embedded therein is used.
  143. In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
  144. and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
  145. generated odt file. Use relative path for specifying the
  146. FILE-MEMBERS. styles.xml must be specified as one of the
  147. FILE-MEMBERS.
  148. Use options 1, 2 or 3 only if styles.xml alone suffices for
  149. achieving the desired formatting. Use option 4, if the styles.xml
  150. references additional files like header and footer images for
  151. achieving the desired formattting."
  152. :group 'org-export-odt
  153. :type
  154. '(choice
  155. (const :tag "Factory settings" nil)
  156. (file :must-match t :tag "styles.xml")
  157. (file :must-match t :tag "ODT or OTT file")
  158. (list :tag "ODT or OTT file + Members"
  159. (file :must-match t :tag "ODF Text or Text Template file")
  160. (cons :tag "Members"
  161. (file :tag " Member" "styles.xml")
  162. (repeat (file :tag "Member"))))))
  163. (defconst org-export-odt-tmpdir-prefix "odt-")
  164. (defconst org-export-odt-bookmark-prefix "OrgXref.")
  165. (defcustom org-export-odt-use-bookmarks-for-internal-links t
  166. "Export Internal links as bookmarks?."
  167. :type 'boolean
  168. :group 'org-export-odt)
  169. (defcustom org-export-odt-embed-images t
  170. "Should the images be copied in to the odt file or just linked?"
  171. :type 'boolean
  172. :group 'org-export-odt)
  173. (defcustom org-odt-export-inline-images 'maybe
  174. "Non-nil means inline images into exported HTML pages.
  175. This is done using an <img> tag. When nil, an anchor with href is used to
  176. link to the image. If this option is `maybe', then images in links with
  177. an empty description will be inlined, while images with a description will
  178. be linked only."
  179. :group 'org-odt-export
  180. :type '(choice (const :tag "Never" nil)
  181. (const :tag "Always" t)
  182. (const :tag "When there is no description" maybe)))
  183. (defcustom org-odt-export-inline-image-extensions
  184. '("png" "jpeg" "jpg" "gif")
  185. "Extensions of image files that can be inlined into HTML."
  186. :type '(repeat (string :tag "Extension"))
  187. :group 'org-odt-export)
  188. (defcustom org-export-odt-pixels-per-inch display-pixels-per-inch
  189. ;; FIXME add docstring
  190. ""
  191. :type 'float
  192. :group 'org-export-odt)
  193. (defvar org-export-odt-default-org-styles-alist
  194. '((paragraph . ((default . "Text_20_body")
  195. (fixedwidth . "OrgSourceBlock")
  196. (verse . "OrgVerse")
  197. (quote . "Quotations")
  198. (blockquote . "Quotations")
  199. (center . "OrgCenter")
  200. (left . "OrgLeft")
  201. (right . "OrgRight")
  202. (title . "Heading_20_1.title")
  203. (footnote . "Footnote")
  204. (src . "OrgSourceBlock")
  205. (illustration . "Illustration")
  206. (table . "Table")
  207. (definition-term . "Text_20_body_20_bold")
  208. (horizontal-line . "Horizontal_20_Line")))
  209. (character . ((bold . "Bold")
  210. (emphasis . "Emphasis")
  211. (code . "OrgCode")
  212. (verbatim . "OrgCode")
  213. (strike . "Strikethrough")
  214. (underline . "Underline")
  215. (subscript . "OrgSubscript")
  216. (superscript . "OrgSuperscript")))
  217. (list . ((ordered . "OrgNumberedList")
  218. (unordered . "OrgBulletedList")
  219. (description . "OrgDescriptionList"))))
  220. "Default styles for various entities.")
  221. (defvar org-export-odt-org-styles-alist org-export-odt-default-org-styles-alist)
  222. (defun org-odt-get-style-name-for-entity (category &optional entity)
  223. (let ((entity (or entity 'default)))
  224. (or
  225. (cdr (assoc entity (cdr (assoc category
  226. org-export-odt-org-styles-alist))))
  227. (cdr (assoc entity (cdr (assoc category
  228. org-export-odt-default-org-styles-alist))))
  229. (error "Cannot determine style name for entity %s of type %s"
  230. entity category))))
  231. ;;;###autoload
  232. (defun org-export-as-odt-and-open (arg)
  233. "Export the outline as ODT and immediately open it with a browser.
  234. If there is an active region, export only the region.
  235. The prefix ARG specifies how many levels of the outline should become
  236. headlines. The default is 3. Lower levels will become bulleted lists."
  237. (interactive "P")
  238. (org-lparse-and-open "odt" "odt" arg))
  239. ;;;###autoload
  240. (defun org-export-as-odt-batch ()
  241. "Call the function `org-lparse-batch'.
  242. This function can be used in batch processing as:
  243. emacs --batch
  244. --load=$HOME/lib/emacs/org.el
  245. --eval \"(setq org-export-headline-levels 2)\"
  246. --visit=MyFile --funcall org-export-as-odt-batch"
  247. (org-lparse-batch "odt"))
  248. ;;;###autoload
  249. (defun org-export-as-odt-to-buffer (arg)
  250. "Call `org-lparse-odt` with output to a temporary buffer.
  251. No file is created. The prefix ARG is passed through to `org-lparse-to-buffer'."
  252. (interactive "P")
  253. (org-lparse-to-buffer "odt" arg))
  254. ;;;###autoload
  255. (defun org-replace-region-by-odt (beg end)
  256. "Assume the current region has org-mode syntax, and convert it to ODT.
  257. This can be used in any buffer. For example, you could write an
  258. itemized list in org-mode syntax in an ODT buffer and then use this
  259. command to convert it."
  260. (interactive "r")
  261. (org-replace-region-by "odt" beg end))
  262. ;;;###autoload
  263. (defun org-export-region-as-odt (beg end &optional body-only buffer)
  264. "Convert region from BEG to END in org-mode buffer to ODT.
  265. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  266. contents, and only produce the region of converted text, useful for
  267. cut-and-paste operations.
  268. If BUFFER is a buffer or a string, use/create that buffer as a target
  269. of the converted ODT. If BUFFER is the symbol `string', return the
  270. produced ODT as a string and leave not buffer behind. For example,
  271. a Lisp program could call this function in the following way:
  272. (setq odt (org-export-region-as-odt beg end t 'string))
  273. When called interactively, the output buffer is selected, and shown
  274. in a window. A non-interactive call will only return the buffer."
  275. (interactive "r\nP")
  276. (org-lparse-region "odt" beg end body-only buffer))
  277. ;;; org-export-as-odt
  278. ;;;###autoload
  279. (defun org-export-as-odt (arg &optional hidden ext-plist
  280. to-buffer body-only pub-dir)
  281. "Export the outline as a OpenDocumentText file.
  282. If there is an active region, export only the region. The prefix
  283. ARG specifies how many levels of the outline should become
  284. headlines. The default is 3. Lower levels will become bulleted
  285. lists. HIDDEN is obsolete and does nothing.
  286. EXT-PLIST is a property list with external parameters overriding
  287. org-mode's default settings, but still inferior to file-local
  288. settings. When TO-BUFFER is non-nil, create a buffer with that
  289. name and export to that buffer. If TO-BUFFER is the symbol
  290. `string', don't leave any buffer behind but just return the
  291. resulting XML as a string. When BODY-ONLY is set, don't produce
  292. the file header and footer, simply return the content of
  293. <body>...</body>, without even the body tags themselves. When
  294. PUB-DIR is set, use this as the publishing directory."
  295. (interactive "P")
  296. (org-lparse "odt" "odt" arg hidden ext-plist to-buffer body-only pub-dir))
  297. (defvar org-odt-entity-control-callbacks-alist
  298. `((EXPORT
  299. . (org-odt-begin-export org-odt-end-export))
  300. (DOCUMENT-CONTENT
  301. . (org-odt-begin-document-content org-odt-end-document-content))
  302. (DOCUMENT-BODY
  303. . (org-odt-begin-document-body org-odt-end-document-body))
  304. (TOC
  305. . (org-odt-begin-toc org-odt-end-toc))
  306. (ENVIRONMENT
  307. . (org-odt-begin-environment org-odt-end-environment))
  308. (FOOTNOTE-DEFINITION
  309. . (org-odt-begin-footnote-definition org-odt-end-footnote-definition))
  310. (TABLE
  311. . (org-odt-begin-table org-odt-end-table))
  312. (TABLE-ROWGROUP
  313. . (org-odt-begin-table-rowgroup org-odt-end-table-rowgroup))
  314. (LIST
  315. . (org-odt-begin-list org-odt-end-list))
  316. (LIST-ITEM
  317. . (org-odt-begin-list-item org-odt-end-list-item))
  318. (OUTLINE
  319. . (org-odt-begin-outline org-odt-end-outline))
  320. (OUTLINE-TEXT
  321. . (org-odt-begin-outline-text org-odt-end-outline-text))
  322. (PARAGRAPH
  323. . (org-odt-begin-paragraph org-odt-end-paragraph)))
  324. "")
  325. (defvar org-odt-entity-format-callbacks-alist
  326. `((EXTRA-TARGETS . org-lparse-format-extra-targets)
  327. (ORG-TAGS . org-lparse-format-org-tags)
  328. (SECTION-NUMBER . org-lparse-format-section-number)
  329. (HEADLINE . org-odt-format-headline)
  330. (TOC-ENTRY . org-odt-format-toc-entry)
  331. (TOC-ITEM . org-odt-format-toc-item)
  332. (TAGS . org-odt-format-tags)
  333. (SPACES . org-odt-format-spaces)
  334. (TABS . org-odt-format-tabs)
  335. (LINE-BREAK . org-odt-format-line-break)
  336. (FONTIFY . org-odt-format-fontify)
  337. (TODO . org-lparse-format-todo)
  338. (LINK . org-odt-format-link)
  339. (INLINE-IMAGE . org-odt-format-inline-image)
  340. (ORG-LINK . org-odt-format-org-link)
  341. (HEADING . org-odt-format-heading)
  342. (ANCHOR . org-odt-format-anchor)
  343. (TABLE . org-lparse-format-table)
  344. (TABLE-ROW . org-odt-format-table-row)
  345. (TABLE-CELL . org-odt-format-table-cell)
  346. (FOOTNOTES-SECTION . ignore)
  347. (FOOTNOTE-REFERENCE . org-odt-format-footnote-reference)
  348. (HORIZONTAL-LINE . org-odt-format-horizontal-line)
  349. (COMMENT . org-odt-format-comment)
  350. (LINE . org-odt-format-line)
  351. (ORG-ENTITY . org-odt-format-org-entity))
  352. "")
  353. ;;;_. callbacks
  354. ;;;_. control callbacks
  355. ;;;_ , document body
  356. (defun org-odt-begin-office-body ()
  357. (insert "
  358. <office:body>
  359. <office:text>
  360. <text:sequence-decls>
  361. <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Illustration\"/>
  362. <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Table\"/>
  363. <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Text\"/>
  364. <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Drawing\"/>
  365. </text:sequence-decls>"))
  366. ;; Following variable is let bound when `org-do-lparse' is in
  367. ;; progress. See org-html.el.
  368. (defvar org-lparse-toc)
  369. (defun org-odt-begin-document-body (opt-plist)
  370. (org-odt-begin-office-body)
  371. (let ((title (plist-get opt-plist :title)))
  372. (when title
  373. (insert
  374. (org-odt-format-stylized-paragraph 'title title))))
  375. ;; insert toc
  376. (when org-lparse-toc
  377. (insert "\n" org-lparse-toc "\n")))
  378. (defvar org-lparse-body-only) ; let bound during org-do-lparse
  379. (defvar org-lparse-to-buffer) ; let bound during org-do-lparse
  380. (defun org-odt-end-document-body (opt-plist)
  381. (unless org-lparse-body-only
  382. (org-lparse-insert-tag "</office:text>")
  383. (org-lparse-insert-tag "</office:body>")))
  384. (defconst org-odt-document-content-header
  385. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  386. <office:document-content
  387. xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
  388. xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\"
  389. xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\"
  390. xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\"
  391. xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\"
  392. xmlns:fo=\"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0\"
  393. xmlns:xlink=\"http://www.w3.org/1999/xlink\"
  394. xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
  395. xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
  396. xmlns:number=\"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0\"
  397. xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\"
  398. xmlns:chart=\"urn:oasis:names:tc:opendocument:xmlns:chart:1.0\"
  399. xmlns:dr3d=\"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0\"
  400. xmlns:math=\"http://www.w3.org/1998/Math/MathML\"
  401. xmlns:form=\"urn:oasis:names:tc:opendocument:xmlns:form:1.0\"
  402. xmlns:script=\"urn:oasis:names:tc:opendocument:xmlns:script:1.0\"
  403. xmlns:ooo=\"http://openoffice.org/2004/office\"
  404. xmlns:ooow=\"http://openoffice.org/2004/writer\"
  405. xmlns:oooc=\"http://openoffice.org/2004/calc\"
  406. xmlns:dom=\"http://www.w3.org/2001/xml-events\"
  407. xmlns:xforms=\"http://www.w3.org/2002/xforms\"
  408. xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
  409. xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
  410. xmlns:rpt=\"http://openoffice.org/2005/report\"
  411. xmlns:of=\"urn:oasis:names:tc:opendocument:xmlns:of:1.2\"
  412. xmlns:xodt=\"http://www.w3.org/1999/xodt\"
  413. xmlns:field=\"urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0\" office:version=\"1.2\">
  414. ")
  415. (defun org-odt-begin-document-content (opt-plist)
  416. ;; document header
  417. (insert org-odt-document-content-header)
  418. ;; automatic styles
  419. (insert-file-contents
  420. (or org-export-odt-automatic-styles-file
  421. (expand-file-name "styles/OrgOdtAutomaticStyles.xml"
  422. org-odt-data-dir)))
  423. (goto-char (point-max)))
  424. (defun org-odt-end-document-content ()
  425. (org-lparse-insert-tag "</office:document-content>"))
  426. (defun org-odt-begin-outline (level1 snumber title tags
  427. target extra-targets class)
  428. (org-lparse-insert
  429. 'HEADING (org-lparse-format
  430. 'HEADLINE title extra-targets tags snumber level1)
  431. level1 target))
  432. (defun org-odt-end-outline ()
  433. (ignore))
  434. (defun org-odt-begin-outline-text (level1 snumber class)
  435. (ignore))
  436. (defun org-odt-end-outline-text ()
  437. (ignore))
  438. (defun org-odt-begin-paragraph (&optional style)
  439. (org-lparse-insert-tag
  440. "<text:p%s>" (org-odt-get-extra-attrs-for-paragraph-style style)))
  441. (defun org-odt-end-paragraph ()
  442. (org-lparse-insert-tag "</text:p>"))
  443. (defun org-odt-get-extra-attrs-for-paragraph-style (style)
  444. (let (style-name)
  445. (setq style-name
  446. (cond
  447. ((stringp style) style)
  448. ((symbolp style) (org-odt-get-style-name-for-entity
  449. 'paragraph style))))
  450. (unless style-name
  451. (error "Don't know how to handle paragraph style %s" style))
  452. (format " text:style-name=\"%s\"" style-name)))
  453. (defun org-odt-format-stylized-paragraph (style text)
  454. (org-odt-format-tags
  455. '("<text:p%s>" . "</text:p>") text
  456. (org-odt-get-extra-attrs-for-paragraph-style style)))
  457. (defun org-odt-begin-environment (style)
  458. (case style
  459. ((blockquote verse center quote)
  460. (org-lparse-begin-paragraph style)
  461. (list))
  462. ((fixedwidth native)
  463. (org-lparse-end-paragraph)
  464. (list))
  465. (t (error "Unknown environment %s" style))))
  466. (defun org-odt-end-environment (style)
  467. (case style
  468. ((blockquote verse center quote)
  469. (org-lparse-end-paragraph)
  470. (list))
  471. ((fixedwidth native)
  472. (org-lparse-begin-paragraph)
  473. (list))
  474. (t (error "Unknown environment %s" style))))
  475. (defun org-odt-begin-list (ltype &optional arg1)
  476. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  477. ltype))
  478. (let* ((style-name (org-odt-get-style-name-for-entity 'list ltype))
  479. (extra (if style-name
  480. (format " text:style-name=\"%s\"" style-name) "")))
  481. ;; FIXME: Handle arg1 incase of ordered lists.
  482. (case ltype
  483. ((ordered unordered description)
  484. (org-lparse-end-paragraph)
  485. (org-lparse-insert-tag "<text:list%s>" extra))
  486. (t (error "Unknown list type: %s" ltype)))))
  487. (defun org-odt-end-list (ltype)
  488. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  489. ltype))
  490. (if ltype
  491. (org-lparse-insert-tag "</text:list>")
  492. (error "Unknown list type: %s" ltype)))
  493. (defun org-odt-begin-list-item (ltype &optional arg headline)
  494. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  495. ltype))
  496. (case ltype
  497. (ordered
  498. (assert (not headline) t)
  499. (let* ((counter arg) (extra ""))
  500. (org-lparse-insert-tag "<text:list-item>")
  501. (org-lparse-begin-paragraph)))
  502. (unordered
  503. (let* ((id arg) (extra ""))
  504. (org-lparse-insert-tag "<text:list-item>")
  505. (org-lparse-begin-paragraph)
  506. (insert (if headline (org-odt-format-target headline id)
  507. (org-odt-format-bookmark "" id)))))
  508. (description
  509. (assert (not headline) t)
  510. (let ((term (or arg "(no term)")))
  511. (insert
  512. (org-odt-format-tags
  513. '("<text:list-item>" . "</text:list-item>")
  514. (org-odt-format-stylized-paragraph 'definition-term term)))
  515. (org-lparse-begin 'LIST-ITEM 'unordered)
  516. (org-lparse-begin 'LIST 'description)
  517. (org-lparse-begin 'LIST-ITEM 'unordered)))
  518. (t (error "Unknown list type"))))
  519. (defun org-odt-end-list-item (ltype)
  520. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  521. ltype))
  522. (case ltype
  523. ((ordered unordered)
  524. (org-lparse-insert-tag "</text:list-item>"))
  525. (description
  526. (org-lparse-end-list-item)
  527. (org-lparse-end 'LIST 'description)
  528. (org-lparse-end-list-item))
  529. (t (error "Unknown list type"))))
  530. ;; Following variables are let bound when table emission is in
  531. ;; progress. See org-lparse.el.
  532. (defvar org-lparse-table-begin-marker)
  533. (defvar org-lparse-table-ncols)
  534. (defvar org-lparse-table-rowgrp-open)
  535. (defvar org-lparse-table-rownum)
  536. (defvar org-lparse-table-cur-rowgrp-is-hdr)
  537. (defvar org-lparse-table-is-styled)
  538. (defvar org-lparse-table-rowgrp-info)
  539. (defvar org-lparse-table-colalign-vector)
  540. (defun org-odt-begin-table (caption label attributes)
  541. (when label
  542. (insert
  543. (org-odt-format-stylized-paragraph
  544. 'table (org-odt-format-entity-caption label caption "Table"))))
  545. (org-lparse-insert-tag
  546. "<table:table table:name=\"%s\" table:style-name=\"%s\">"
  547. (or label "") "OrgTable")
  548. (setq org-lparse-table-begin-marker (point)))
  549. (defun org-odt-end-table ()
  550. (goto-char org-lparse-table-begin-marker)
  551. (loop for level from 0 below org-lparse-table-ncols
  552. do (insert (org-odt-format-tags "<table:table-column/>" "")))
  553. ;; fill style attributes for table cells
  554. (when org-lparse-table-is-styled
  555. (while (re-search-forward "@@\\(table-cell:p\\|table-cell:style-name\\)@@\\([0-9]+\\)@@\\([0-9]+\\)@@" nil t)
  556. (let ((spec (match-string 1))
  557. (r (string-to-number (match-string 2)))
  558. (c (string-to-number (match-string 3))))
  559. (cond
  560. ((equal spec "table-cell:p")
  561. (let ((style-name (org-odt-get-paragraph-style-for-table-cell r c)))
  562. (replace-match style-name t t)))
  563. ((equal spec "table-cell:style-name")
  564. (let ((style-name (org-odt-get-style-name-for-table-cell r c)))
  565. (replace-match style-name t t)))))))
  566. (goto-char (point-max))
  567. (org-lparse-insert-tag "</table:table>"))
  568. (defun org-odt-begin-table-rowgroup (&optional is-header-row)
  569. (when org-lparse-table-rowgrp-open
  570. (org-lparse-end 'TABLE-ROWGROUP))
  571. (org-lparse-insert-tag (if is-header-row
  572. "<table:table-header-rows>"
  573. "<table:table-rows>"))
  574. (setq org-lparse-table-rowgrp-open t)
  575. (setq org-lparse-table-cur-rowgrp-is-hdr is-header-row))
  576. (defun org-odt-end-table-rowgroup ()
  577. (when org-lparse-table-rowgrp-open
  578. (setq org-lparse-table-rowgrp-open nil)
  579. (org-lparse-insert-tag
  580. (if org-lparse-table-cur-rowgrp-is-hdr
  581. "</table:table-header-rows>" "</table:table-rows>"))))
  582. (defun org-odt-format-table-row (row)
  583. (org-odt-format-tags
  584. '("<table:table-row>" . "</table:table-row>") row))
  585. (defun org-odt-get-style-name-for-table-cell (r c)
  586. (concat
  587. "OrgTblCell"
  588. (cond
  589. ((= r 0) "T")
  590. ((eq (cdr (assoc r org-lparse-table-rowgrp-info)) :start) "T")
  591. (t ""))
  592. (when (= r org-lparse-table-rownum) "B")
  593. (cond
  594. ((= c 0) "")
  595. ((or (memq (nth c org-table-colgroup-info) '(:start :startend))
  596. (memq (nth (1- c) org-table-colgroup-info) '(:end :startend))) "L")
  597. (t ""))))
  598. (defun org-odt-get-paragraph-style-for-table-cell (r c)
  599. (capitalize (aref org-lparse-table-colalign-vector c)))
  600. (defun org-odt-format-table-cell (data r c)
  601. (if (not org-lparse-table-is-styled)
  602. (org-odt-format-tags
  603. '("<table:table-cell>" . "</table:table-cell>")
  604. (org-odt-format-stylized-paragraph
  605. (cond
  606. (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
  607. ((and (= c 0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS))
  608. "OrgTableHeading")
  609. (t "OrgTableContents"))
  610. data))
  611. (let* ((style-name-cookie
  612. (format "@@table-cell:style-name@@%03d@@%03d@@" r c))
  613. (paragraph-style-cookie
  614. (concat
  615. (cond
  616. (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
  617. ((and (= c 0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS))
  618. "OrgTableHeading")
  619. (t "OrgTableContents"))
  620. (format "@@table-cell:p@@%03d@@%03d@@" r c))))
  621. (org-odt-format-tags
  622. '("<table:table-cell table:style-name=\"%s\">" .
  623. "</table:table-cell>")
  624. (org-odt-format-stylized-paragraph paragraph-style-cookie data)
  625. style-name-cookie))))
  626. (defun org-odt-begin-footnote-definition (n)
  627. (org-lparse-begin-paragraph 'footnote))
  628. (defun org-odt-end-footnote-definition (n)
  629. (org-lparse-end-paragraph))
  630. (defun org-odt-begin-toc (lang-specific-heading)
  631. (insert
  632. (format "
  633. <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
  634. <text:table-of-content-source text:outline-level=\"10\">
  635. <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
  636. " lang-specific-heading))
  637. (loop for level from 1 upto 10
  638. do (insert (format
  639. "
  640. <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
  641. <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
  642. <text:index-entry-chapter/>
  643. <text:index-entry-text/>
  644. <text:index-entry-link-end/>
  645. </text:table-of-content-entry-template>
  646. " level level)))
  647. (insert
  648. (format "
  649. </text:table-of-content-source>
  650. <text:index-body>
  651. <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
  652. <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
  653. </text:index-title>
  654. " lang-specific-heading)))
  655. (defun org-odt-end-toc ()
  656. (insert "
  657. </text:index-body>
  658. </text:table-of-content>
  659. "))
  660. (defun org-odt-format-toc-entry (snumber todo headline tags href)
  661. (setq headline (concat
  662. (and org-export-with-section-numbers
  663. (concat snumber ". "))
  664. headline
  665. (and tags
  666. (concat
  667. (org-lparse-format 'SPACES 3)
  668. (org-lparse-format 'FONTIFY tags "tag")))))
  669. (when todo
  670. (setq headline (org-lparse-format 'FONTIFY headline "todo")))
  671. (let ((org-odt-suppress-xref t))
  672. (org-odt-format-link headline (concat "#" href))))
  673. (defun org-odt-format-toc-item (toc-entry level org-last-level)
  674. (let ((style (format "Contents_20_%d"
  675. (+ level (or (org-lparse-get 'TOPLEVEL-HLEVEL) 1) -1))))
  676. (insert "\n" (org-odt-format-stylized-paragraph style toc-entry) "\n")))
  677. ;; Following variable is let bound during 'ORG-LINK callback. See
  678. ;; org-html.el
  679. (defvar org-lparse-link-description-is-image nil)
  680. (defun org-odt-format-link (desc href &optional attr)
  681. (cond
  682. ((and (= (string-to-char href) ?#) (not org-odt-suppress-xref))
  683. (setq href (concat org-export-odt-bookmark-prefix (substring href 1)))
  684. (org-odt-format-tags
  685. '("<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"%s\">" .
  686. "</text:bookmark-ref>")
  687. desc href))
  688. (org-lparse-link-description-is-image
  689. (org-odt-format-tags
  690. '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</draw:a>")
  691. desc href (or attr "")))
  692. (t
  693. (org-odt-format-tags
  694. '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</text:a>")
  695. desc href (or attr "")))))
  696. (defun org-odt-format-spaces (n)
  697. (org-odt-format-tags "<text:s text:c=\"%d\"/>" "" n))
  698. (defun org-odt-format-tabs (&optional n)
  699. (let ((tab "<text:tab/>")
  700. (n (or n 1)))
  701. (insert tab)))
  702. (defun org-odt-format-line-break ()
  703. (org-odt-format-tags "<text:line-break/>" ""))
  704. (defun org-odt-format-horizontal-line ()
  705. (org-odt-format-stylized-paragraph 'horizontal-line ""))
  706. (defun org-odt-format-line (line)
  707. (case org-lparse-dyn-current-environment
  708. (fixedwidth (concat (org-odt-format-source-code-or-example-line
  709. (org-xml-encode-plain-text line)) "\n"))
  710. (t (concat line "\n"))))
  711. (defun org-odt-format-comment (fmt &rest args)
  712. (let ((comment (apply 'format fmt args)))
  713. (format "\n<!-- %s -->\n" comment)))
  714. (defun org-odt-format-org-entity (wd)
  715. (org-entity-get-representation wd 'utf8))
  716. (defun org-odt-fill-tabs-and-spaces (line)
  717. (replace-regexp-in-string
  718. "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
  719. (cond
  720. ((string= s "\t") (org-odt-format-tabs))
  721. ((> (length s) 1)
  722. (org-odt-format-spaces (length s)))
  723. (t " "))) line))
  724. (defun org-odt-format-source-code-or-example-line (line)
  725. (org-odt-format-stylized-paragraph 'src (org-odt-fill-tabs-and-spaces line)))
  726. (defun org-odt-format-example (lines)
  727. (mapconcat
  728. (lambda (line)
  729. (org-odt-format-source-code-or-example-line line))
  730. (org-split-string lines "[\r\n]") "\n"))
  731. (defun org-odt-format-source-code-or-example (lines lang caption textareap
  732. cols rows num cont
  733. rpllbl fmt)
  734. (org-odt-format-example (org-export-number-lines
  735. (org-xml-encode-plain-text-lines lines)
  736. 0 0 num cont rpllbl fmt)))
  737. (defun org-xml-encode-plain-text-lines (rtn)
  738. (mapconcat 'org-xml-encode-plain-text (org-split-string rtn "[\r\n]") "\n"))
  739. (defun org-odt-remap-stylenames (style-name)
  740. (or
  741. (cdr (assoc style-name '(("timestamp-wrapper" . "OrgTimestampWrapper")
  742. ("timestamp" . "OrgTimestamp")
  743. ("timestamp-kwd" . "OrgTimestampKeyword")
  744. ("tag" . "OrgTag")
  745. ("todo" . "OrgTodo")
  746. ("done" . "OrgDone")
  747. ("target" . "OrgTarget"))))
  748. style-name))
  749. (defun org-odt-format-fontify (text style &optional id)
  750. (let* ((style-name
  751. (cond
  752. ((stringp style)
  753. (org-odt-remap-stylenames style))
  754. ((symbolp style)
  755. (org-odt-get-style-name-for-entity 'character style))
  756. ((listp style)
  757. (assert (< 1 (length style)))
  758. (let ((parent-style (pop style)))
  759. (mapconcat (lambda (s)
  760. ;; (assert (stringp s) t)
  761. (org-odt-remap-stylenames s)) style "")
  762. (org-odt-remap-stylenames parent-style)))
  763. (t (error "Don't how to handle style %s" style)))))
  764. (org-odt-format-tags
  765. '("<text:span text:style-name=\"%s\">" . "</text:span>")
  766. text style-name)))
  767. (defun org-odt-relocate-relative-path (path dir)
  768. (if (file-name-absolute-p path) path
  769. (file-relative-name (expand-file-name path dir)
  770. (expand-file-name "eyecandy" dir))))
  771. (defun org-odt-format-inline-image (thefile)
  772. (let* ((thelink (if (file-name-absolute-p thefile) thefile
  773. (org-xml-format-href
  774. (org-odt-relocate-relative-path
  775. thefile org-current-export-file))))
  776. (href
  777. (org-odt-format-tags
  778. "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
  779. (if org-export-odt-embed-images
  780. (org-odt-copy-image-file thefile) thelink))))
  781. (org-export-odt-format-image thefile href)))
  782. (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
  783. descp)
  784. "Make an HTML link.
  785. OPT-PLIST is an options list.
  786. TYPE is the device-type of the link (THIS://foo.html)
  787. PATH is the path of the link (http://THIS#locationx)
  788. FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
  789. DESC is the link description, if any.
  790. ATTR is a string of other attributes of the a element.
  791. MAY-INLINE-P allows inlining it as an image."
  792. (declare (special org-lparse-par-open))
  793. (save-match-data
  794. (let* ((may-inline-p
  795. (and (member type-1 '("http" "https" "file"))
  796. (org-lparse-should-inline-p path descp)
  797. (not fragment)))
  798. (type (if (equal type-1 "id") "file" type-1))
  799. (filename path)
  800. (thefile path))
  801. (cond
  802. ;; check for inlined images
  803. ((and (member type '("file"))
  804. (not fragment)
  805. (org-file-image-p
  806. filename org-odt-export-inline-image-extensions)
  807. (or (eq t org-odt-export-inline-images)
  808. (and org-odt-export-inline-images (not descp))))
  809. ;; (when (and (string= type "file") (file-name-absolute-p path))
  810. ;; (setq thefile (concat "file://" (expand-file-name path))))
  811. ;; (setq thefile (org-xml-format-href thefile))
  812. ;; (org-export-html-format-image thefile)
  813. (org-odt-format-inline-image thefile))
  814. (t
  815. (when (string= type "file")
  816. (setq thefile
  817. (cond
  818. ((file-name-absolute-p path)
  819. (concat "file://" (expand-file-name path)))
  820. (t (org-odt-relocate-relative-path
  821. thefile org-current-export-file)))))
  822. (when (and (member type '("" "http" "https" "file" "coderef"))
  823. fragment)
  824. (setq thefile (concat thefile "#" fragment)))
  825. (setq thefile (org-xml-format-href thefile))
  826. (when (not (member type '("" "file" "coderef")))
  827. (setq thefile (concat type ":" thefile)))
  828. (let ((org-odt-suppress-xref (string= type "coderef")))
  829. (org-odt-format-link
  830. (org-xml-format-desc desc) thefile attr)))))))
  831. (defun org-odt-format-heading (text level &optional id)
  832. (let* ((text (if id (org-odt-format-target text id) text)))
  833. (org-odt-format-tags
  834. '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
  835. "</text:h>") text level level)))
  836. (defun org-odt-format-headline (title extra-targets tags
  837. &optional snumber level)
  838. (concat
  839. (org-lparse-format 'EXTRA-TARGETS extra-targets)
  840. ;; No need to generate section numbers. They are auto-generated by
  841. ;; the application
  842. ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
  843. title
  844. (and tags (concat (org-lparse-format 'SPACES 3)
  845. (org-lparse-format 'ORG-TAGS tags)))))
  846. (defun org-odt-format-anchor (text name &optional class)
  847. (org-odt-format-target text name))
  848. (defun org-odt-format-bookmark (text id)
  849. (if id
  850. (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id)
  851. text))
  852. (defun org-odt-format-target (text id)
  853. (let ((name (concat org-export-odt-bookmark-prefix id)))
  854. (concat
  855. (and id (org-odt-format-tags
  856. "<text:bookmark-start text:name=\"%s\"/>" "" name))
  857. (org-odt-format-bookmark text id)
  858. (and id (org-odt-format-tags
  859. "<text:bookmark-end text:name=\"%s\"/>" "" name)))))
  860. (defun org-odt-format-footnote (n def)
  861. (let ((id (concat "fn" n))
  862. (note-class "footnote")
  863. (par-style "Footnote"))
  864. (org-odt-format-tags
  865. '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
  866. "</text:note>")
  867. (concat
  868. (org-odt-format-tags
  869. '("<text:note-citation>" . "</text:note-citation>")
  870. n)
  871. (org-odt-format-tags
  872. '("<text:note-body>" . "</text:note-body>")
  873. def))
  874. id note-class)))
  875. (defun org-odt-format-footnote-reference (n def refcnt)
  876. (if (= refcnt 1)
  877. (org-odt-format-footnote n def)
  878. (org-odt-format-footnote-ref n)))
  879. (defun org-odt-format-footnote-ref (n)
  880. (let ((note-class "footnote")
  881. (ref-format "text")
  882. (ref-name (concat "fn" n)))
  883. (org-odt-format-tags
  884. '("<text:span text:style-name=\"%s\">" . "</text:span>")
  885. (org-odt-format-tags
  886. '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" . "</text:note-ref>")
  887. n note-class ref-format ref-name)
  888. "OrgSuperscript")))
  889. (defun org-odt-get-image-name (file-name)
  890. (require 'sha1)
  891. (file-relative-name
  892. (expand-file-name
  893. (concat (sha1 file-name) "." (file-name-extension file-name)) "Pictures")))
  894. (defun org-export-odt-format-image (src href
  895. ;; par-open
  896. )
  897. "Create image tag with source and attributes."
  898. (save-match-data
  899. (let (embed-as caption attr label attr-plist size width height)
  900. (cond
  901. ((string-match "^ltxpng/" src)
  902. ;; FIXME: Anyway the latex src can be embedded as an
  903. ;; annotation
  904. ;; (org-find-text-property-in-string 'org-latex-src src)
  905. (setq caption nil attr nil label nil embed-as 'character))
  906. (t
  907. (setq caption (org-find-text-property-in-string 'org-caption src)
  908. caption (and caption (org-xml-format-desc caption))
  909. attr (org-find-text-property-in-string 'org-attributes src)
  910. label (org-find-text-property-in-string 'org-label src)
  911. embed-as 'paragraph)))
  912. (setq attr-plist (when attr (read attr)))
  913. (setq size (org-odt-image-size-from-file
  914. src (plist-get attr-plist :width)
  915. (plist-get attr-plist :height)
  916. (plist-get attr-plist :scale) nil embed-as))
  917. (org-export-odt-do-format-image embed-as caption attr label
  918. size href))))
  919. (defun org-export-odt-do-format-image (embed-as caption attr label
  920. size href)
  921. "Create image tag with source and attributes."
  922. (save-match-data
  923. (let ((width (car size)) (height (cdr size))
  924. (draw-frame-pair
  925. '("<draw:frame draw:style-name=\"%s\"
  926. text:anchor-type=\"%s\"
  927. draw:z-index=\"%d\" %s>" . "</draw:frame>")))
  928. (cond
  929. ((and (not caption) (not label))
  930. (let (style-name anchor-type)
  931. (cond
  932. ((eq embed-as 'paragraph)
  933. (setq style-name "OrgGraphicsParagraph" anchor-type "paragraph"))
  934. ((eq embed-as 'character)
  935. (setq style-name "OrgGraphicsBaseline" anchor-type "as-char")))
  936. (org-odt-format-tags
  937. draw-frame-pair href style-name anchor-type 0
  938. (org-odt-image-attrs-from-size width height))))
  939. (t
  940. (concat
  941. ;; (when par-open (org-odt-close-par))
  942. (org-odt-format-tags
  943. draw-frame-pair
  944. (org-odt-format-tags
  945. '("<draw:text-box fo:min-height=\"%dcm\">" . "</draw:text-box>")
  946. (org-odt-format-stylized-paragraph
  947. 'illustration
  948. (concat
  949. (let ((extra " style:rel-width=\"100%\" style:rel-height=\"scale\""))
  950. (org-odt-format-tags
  951. draw-frame-pair href "OrgGraphicsParagraphContent" "paragraph" 2
  952. (concat (org-odt-image-attrs-from-size width height) extra)))
  953. (org-odt-format-entity-caption label caption)))
  954. height)
  955. "OrgFrame" "paragraph" 1
  956. (org-odt-image-attrs-from-size width))
  957. ;; (when par-open (org-odt-open-par))
  958. ))))))
  959. ;; xml files generated on-the-fly
  960. (defconst org-export-odt-save-list
  961. '("mimetype" "META-INF/manifest.xml" "content.xml" "meta.xml" "styles.xml"))
  962. ;; xml files that are copied
  963. (defconst org-export-odt-nosave-list '())
  964. ;; xml files that contribute to the final odt file
  965. (defvar org-export-odt-file-list nil)
  966. (defconst org-export-odt-manifest-lines
  967. '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  968. "<manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">"
  969. "<manifest:file-entry manifest:media-type=\"application/vnd.oasis.opendocument.text\" manifest:version=\"1.2\" manifest:full-path=\"/\"/>"
  970. "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"content.xml\"/>"
  971. "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"styles.xml\"/>"
  972. "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"meta.xml\"/>"
  973. "<manifest:file-entry manifest:media-type=\"\" manifest:full-path=\"Pictures/\"/>") . ("</manifest:manifest>")))
  974. (defconst org-export-odt-meta-lines
  975. '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  976. "<office:document-meta"
  977. " xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\""
  978. " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
  979. " xmlns:dc=\"http://purl.org/dc/elements/1.1/\""
  980. " xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\""
  981. " xmlns:ooo=\"http://openoffice.org/2004/office\" "
  982. " office:version=\"1.2\">"
  983. " <office:meta>") . (" </office:meta>" "</office:document-meta>")))
  984. (defun org-odt-copy-image-file (path &optional target-file)
  985. "Returns the internal name of the file"
  986. (let* ((image-type (file-name-extension path))
  987. (media-type (format "image/%s" image-type))
  988. (src-file (expand-file-name
  989. path (file-name-directory org-current-export-file)))
  990. (target-file (or target-file (org-odt-get-image-name src-file)))
  991. ;; FIXME
  992. (body-only nil))
  993. (when (not org-lparse-to-buffer)
  994. (message "Embedding %s as %s ..."
  995. (substring-no-properties path) target-file)
  996. (copy-file src-file target-file 'overwrite)
  997. (org-odt-update-manifest-file media-type target-file)
  998. (push target-file org-export-odt-file-list)) target-file))
  999. (defun org-odt-image-attrs-from-size (&optional width height)
  1000. (concat
  1001. (when width (format "svg:width=\"%0.2fcm\"" width))
  1002. " "
  1003. (when height (format "svg:height=\"%0.2fcm\"" height))))
  1004. (defvar org-export-odt-image-size-probe-method
  1005. '(emacs imagemagick force)
  1006. "Ordered list of methods by for determining size of an embedded
  1007. image.")
  1008. (defvar org-export-odt-default-image-sizes-alist
  1009. '(("character" . (5 . 0.4))
  1010. ("paragraph" . (5 . 5)))
  1011. "Hardcoded image dimensions one for each of the anchor
  1012. methods.")
  1013. (defun org-odt-do-image-size (probe-method file &optional dpi anchor-type)
  1014. (setq dpi (or dpi org-export-odt-pixels-per-inch))
  1015. (setq anchor-type (or anchor-type "paragraph"))
  1016. (flet ((size-in-cms (size-in-pixels)
  1017. (flet ((pixels-to-cms (pixels)
  1018. (let* ((cms-per-inch 2.54)
  1019. (inches (/ pixels dpi)))
  1020. (* cms-per-inch inches))))
  1021. (and size-in-pixels
  1022. (cons (pixels-to-cms (car size-in-pixels))
  1023. (pixels-to-cms (cdr size-in-pixels)))))))
  1024. (case probe-method
  1025. (emacs
  1026. (size-in-cms (ignore-errors (image-size (create-image file) 'pixels))))
  1027. (imagemagick
  1028. (size-in-cms
  1029. (let ((dim (shell-command-to-string
  1030. (format "identify -format \"%%w:%%h\" \"%s\"" file))))
  1031. (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
  1032. (cons (string-to-number (match-string 1 dim))
  1033. (string-to-number (match-string 2 dim)))))))
  1034. (t
  1035. (cdr (assoc-string anchor-type
  1036. org-export-odt-default-image-sizes-alist))))))
  1037. (defun org-odt-image-size-from-file (file &optional user-width
  1038. user-height scale dpi embed-as)
  1039. (unless (file-name-absolute-p file)
  1040. (setq file (expand-file-name
  1041. file (file-name-directory org-current-export-file))))
  1042. (let* (size width height)
  1043. (unless (and user-height user-width)
  1044. (loop for probe-method in org-export-odt-image-size-probe-method
  1045. until size
  1046. do (setq size (org-odt-do-image-size
  1047. probe-method file dpi embed-as)))
  1048. (or size (error "Cannot determine Image size. Aborting ..."))
  1049. (setq width (car size) height (cdr size)))
  1050. (cond
  1051. (scale
  1052. (setq width (* width scale) height (* height scale)))
  1053. ((and user-height user-width)
  1054. (setq width user-width height user-height))
  1055. (user-height
  1056. (setq width (* user-height (/ width height)) height user-height))
  1057. (user-width
  1058. (setq height (* user-width (/ height width)) width user-width))
  1059. (t (ignore)))
  1060. (cons width height)))
  1061. (defvar org-odt-default-entity "Illustration")
  1062. (defun org-odt-format-entity-caption (label caption &optional default-entity)
  1063. (if (not label) (or caption "")
  1064. (let* ((label-components (org-odt-parse-label label))
  1065. (entity (car label-components))
  1066. (seqno (cdr label-components))
  1067. (caption (and caption (concat ": " caption))))
  1068. (unless seqno
  1069. (setq seqno label
  1070. entity (or default-entity org-odt-default-entity)))
  1071. (concat
  1072. entity " "
  1073. (org-odt-format-tags
  1074. '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" . "</text:sequence>")
  1075. seqno label entity entity)
  1076. caption))))
  1077. (defun org-odt-format-tags (tag text &rest args)
  1078. (let ((prefix (when org-lparse-encode-pending "@"))
  1079. (suffix (when org-lparse-encode-pending "@")))
  1080. (apply 'org-lparse-format-tags tag text prefix suffix args)))
  1081. (defun org-odt-init-outfile (filename)
  1082. (unless (executable-find "zip")
  1083. ;; Not at all OSes ship with zip by default
  1084. (error "Executable \"zip\" needed for creating OpenDocument files. Aborting."))
  1085. (let* ((outdir (make-temp-file org-export-odt-tmpdir-prefix t))
  1086. (mimetype-file (expand-file-name "mimetype" outdir))
  1087. (content-file (expand-file-name "content.xml" outdir))
  1088. (manifest-file (expand-file-name "META-INF/manifest.xml" outdir))
  1089. (meta-file (expand-file-name "meta.xml" outdir))
  1090. (styles-file (expand-file-name "styles.xml" outdir))
  1091. (pictures-dir (expand-file-name "Pictures" outdir))
  1092. (body-only nil))
  1093. ;; content file
  1094. (with-current-buffer (find-file-noselect content-file t)
  1095. (erase-buffer))
  1096. ;; FIXME: How to factor in body-only here
  1097. (unless body-only
  1098. ;; manifest file
  1099. (make-directory (file-name-directory manifest-file))
  1100. (with-current-buffer (find-file-noselect manifest-file t)
  1101. (erase-buffer)
  1102. (insert (mapconcat 'identity (car org-export-odt-manifest-lines) "\n"))
  1103. (insert "\n")
  1104. (save-excursion
  1105. (insert (mapconcat 'identity (cdr org-export-odt-manifest-lines) "\n"))))
  1106. ;; meta file
  1107. (with-current-buffer (find-file-noselect meta-file t)
  1108. (erase-buffer)
  1109. (insert (mapconcat 'identity (car org-export-odt-meta-lines) "\n"))
  1110. (insert "\n")
  1111. (save-excursion
  1112. (insert (mapconcat 'identity (cdr org-export-odt-meta-lines) "\n"))))
  1113. ;; mimetype
  1114. (with-current-buffer (find-file-noselect mimetype-file t)
  1115. (insert "application/vnd.oasis.opendocument.text"))
  1116. ;; styles file
  1117. ;; (copy-file org-export-odt-styles-file styles-file t)
  1118. ;; Pictures dir
  1119. (make-directory pictures-dir)
  1120. ;; initialize list of files that contribute to the odt file
  1121. (setq org-export-odt-file-list
  1122. (append org-export-odt-save-list org-export-odt-nosave-list)))
  1123. content-file))
  1124. (defconst org-odt-manifest-file-entry-tag
  1125. "<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"/>")
  1126. (defun org-odt-save-as-outfile (target opt-plist)
  1127. ;; write meta file
  1128. (org-odt-update-meta-file opt-plist)
  1129. ;; write styles file
  1130. (org-odt-copy-styles-file)
  1131. ;; Update styles.xml - take care of outline numbering
  1132. (with-current-buffer
  1133. (find-file-noselect (expand-file-name "styles.xml") t)
  1134. ;; Don't make automatic backup of styles.xml file. This setting
  1135. ;; prevents the backedup styles.xml file from being zipped in to
  1136. ;; odt file. This is more of a hackish fix. Better alternative
  1137. ;; would be to fix the zip command so that the output odt file
  1138. ;; includes only the needed files and excludes any auto-generated
  1139. ;; extra files like backups and auto-saves etc etc. Note that
  1140. ;; currently the zip command zips up the entire temp directory so
  1141. ;; that any auto-generated files created under the hood ends up in
  1142. ;; the resulting odt file.
  1143. (set (make-local-variable 'backup-inhibited) t)
  1144. ;; Import local setting of `org-export-with-section-numbers'
  1145. (org-lparse-bind-local-variables opt-plist)
  1146. (org-odt-configure-outline-numbering
  1147. (if org-export-with-section-numbers org-export-headline-levels 0)))
  1148. (let ((zipdir default-directory))
  1149. (message "Switching to directory %s" (expand-file-name zipdir))
  1150. ;; save all xml files
  1151. (mapc (lambda (file)
  1152. (with-current-buffer
  1153. (find-file-noselect (expand-file-name file) t)
  1154. ;; prettify output
  1155. (indent-region (point-min) (point-max))
  1156. (save-buffer)))
  1157. org-export-odt-save-list)
  1158. (let* ((target-name (file-name-nondirectory target))
  1159. (target-dir (file-name-directory target))
  1160. (cmds `(("zip" "-mX0" ,target-name "mimetype")
  1161. ("zip" "-rmTq" ,target-name "."))))
  1162. (when (file-exists-p target)
  1163. ;; FIXME: If the file is locked this throws a cryptic error
  1164. (delete-file target))
  1165. (let ((coding-system-for-write 'no-conversion) exitcode)
  1166. (message "Creating odt file...")
  1167. (mapc
  1168. (lambda (cmd)
  1169. (message "Running %s" (mapconcat 'identity cmd " "))
  1170. (setq exitcode
  1171. (apply 'call-process (car cmd) nil nil nil (cdr cmd)))
  1172. (or (zerop exitcode)
  1173. (error "Unable to create odt file (%S)" exitcode)))
  1174. cmds))
  1175. ;; move the file from outdir to target-dir
  1176. (rename-file target-name target-dir)
  1177. ;; kill all xml buffers
  1178. (mapc (lambda (file)
  1179. (kill-buffer
  1180. (find-file-noselect (expand-file-name file zipdir) t)))
  1181. org-export-odt-save-list)
  1182. (delete-directory zipdir)))
  1183. (message "Created %s" target)
  1184. (set-buffer (find-file-noselect target t)))
  1185. (defun org-odt-format-date (date)
  1186. (let ((warning-msg
  1187. "OpenDocument files require that dates be in ISO-8601 format. Please review your DATE options for compatibility."))
  1188. ;; If the user is not careful with the date specification, an
  1189. ;; invalid meta.xml will be emitted.
  1190. ;; For now honor user's diktat and let him off with a warning
  1191. ;; message. This is OK as LibreOffice (and possibly other
  1192. ;; apps) doesn't deem this deviation as critical and continue
  1193. ;; to load the file.
  1194. ;; FIXME: Surely there a better way to handle this. Revisit this
  1195. ;; later.
  1196. (cond
  1197. ((and date (string-match "%" date))
  1198. ;; Honor user's diktat. See comments above
  1199. (org-lparse-warn warning-msg)
  1200. (format-time-string date))
  1201. (date
  1202. ;; Honor user's diktat. See comments above
  1203. (org-lparse-warn warning-msg)
  1204. date)
  1205. (t
  1206. ;; ISO 8601 format
  1207. (format-time-string "%Y-%m-%dT%T%:z")))))
  1208. (defun org-odt-update-meta-file (opt-plist)
  1209. (with-current-buffer
  1210. (find-file-noselect (expand-file-name "meta.xml") t)
  1211. (let ((date (org-odt-format-date (plist-get opt-plist :date)))
  1212. (author (or (plist-get opt-plist :author) ""))
  1213. (email (plist-get opt-plist :email))
  1214. (keywords (plist-get opt-plist :keywords))
  1215. (description (plist-get opt-plist :description))
  1216. (title (plist-get opt-plist :title)))
  1217. (insert
  1218. "\n"
  1219. (org-odt-format-tags '("<dc:creator>" . "</dc:creator>") author)
  1220. (org-odt-format-tags
  1221. '("\n<meta:initial-creator>" . "</meta:initial-creator>") author)
  1222. (org-odt-format-tags '("\n<dc:date>" . "</dc:date>") date)
  1223. (org-odt-format-tags
  1224. '("\n<meta:creation-date>" . "</meta:creation-date>") date)
  1225. (org-odt-format-tags '("\n<meta:generator>" . "</meta:generator>")
  1226. (when org-export-creator-info
  1227. (format "Org-%s/Emacs-%s"
  1228. org-version emacs-version)))
  1229. (org-odt-format-tags '("\n<meta:keyword>" . "</meta:keyword>") keywords)
  1230. (org-odt-format-tags '("\n<dc:subject>" . "</dc:subject>") description)
  1231. (org-odt-format-tags '("\n<dc:title>" . "</dc:title>") title)
  1232. "\n"))))
  1233. (defun org-odt-update-manifest-file (media-type full-path)
  1234. (with-current-buffer
  1235. (find-file-noselect (expand-file-name "META-INF/manifest.xml") t)
  1236. (insert (format org-odt-manifest-file-entry-tag media-type full-path))))
  1237. (defun org-odt-finalize-outfile ()
  1238. (message "org-newodt: Finalizing outfile")
  1239. (org-odt-delete-empty-paragraphs))
  1240. (defun org-odt-delete-empty-paragraphs ()
  1241. (goto-char (point-min))
  1242. (let ((open "<text:p[^>]*>")
  1243. (close "</text:p>"))
  1244. (while (re-search-forward (format "%s[ \r\n\t]*%s" open close) nil t)
  1245. (replace-match ""))))
  1246. (defun org-odt-get (what &optional opt-plist)
  1247. (case what
  1248. (BACKEND 'odt)
  1249. (EXPORT-DIR (org-export-directory :html opt-plist))
  1250. (FILE-NAME-EXTENSION "odt")
  1251. (EXPORT-BUFFER-NAME "*Org ODT Export*")
  1252. (ENTITY-CONTROL org-odt-entity-control-callbacks-alist)
  1253. (ENTITY-FORMAT org-odt-entity-format-callbacks-alist)
  1254. (INIT-METHOD 'org-odt-init-outfile)
  1255. (FINAL-METHOD 'org-odt-finalize-outfile)
  1256. (SAVE-METHOD 'org-odt-save-as-outfile)
  1257. (OTHER-BACKENDS
  1258. '("bib" "doc" "doc6" "doc95" "html" "xhtml" "latex" "odt" "ott" "pdf" "rtf"
  1259. "sdw" "sdw3" "sdw4" "stw " "sxw" "mediawiki" "text" "txt" "uot" "vor"
  1260. "vor3" "vor4" "docbook" "ooxml" "ppt" "odp"))
  1261. (CONVERT-METHOD org-lparse-convert-process)
  1262. (TOPLEVEL-HLEVEL 1)
  1263. (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps)
  1264. (INLINE-IMAGES 'maybe)
  1265. (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
  1266. (PLAIN-TEXT-MAP '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
  1267. (TABLE-FIRST-COLUMN-AS-LABELS nil)
  1268. (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY "," 'superscript))
  1269. (CODING-SYSTEM-FOR-WRITE 'utf-8)
  1270. (CODING-SYSTEM-FOR-SAVE 'utf-8)
  1271. (t (error "Unknown property: %s" what))))
  1272. (defun org-odt-parse-label (label)
  1273. (save-match-data
  1274. (if (not (string-match "\\`[a-zA-Z]+:\\(.+\\)" label))
  1275. (cons label nil)
  1276. (cons
  1277. (capitalize (substring label 0 (1- (match-beginning 1))))
  1278. (substring label (match-beginning 1))))))
  1279. (defvar org-lparse-latex-fragment-fallback) ; set by org-do-lparse
  1280. (defun org-export-odt-preprocess (parameters)
  1281. "Convert LaTeX fragments to images."
  1282. (when (and org-current-export-file
  1283. (plist-get parameters :LaTeX-fragments))
  1284. (org-format-latex
  1285. (concat "ltxpng/" (file-name-sans-extension
  1286. (file-name-nondirectory
  1287. org-current-export-file)))
  1288. org-current-export-dir nil "Creating LaTeX image %s"
  1289. nil nil
  1290. (cond
  1291. ((eq (plist-get parameters :LaTeX-fragments) 'verbatim) 'verbatim)
  1292. ;; Investigate MathToWeb for converting TeX equations to MathML
  1293. ;; See http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01755.html
  1294. ((or (eq (plist-get parameters :LaTeX-fragments) 'mathjax )
  1295. (eq (plist-get parameters :LaTeX-fragments) t ))
  1296. (org-lparse-warn
  1297. (concat
  1298. "Use of MathJax is incompatible with ODT exporter. "
  1299. (format "Using %S instead." org-lparse-latex-fragment-fallback)))
  1300. org-lparse-latex-fragment-fallback)
  1301. ((eq (plist-get parameters :LaTeX-fragments) 'dvipng ) 'dvipng)
  1302. (t nil))))
  1303. (goto-char (point-min))
  1304. (let (label label-components category value pretty-label)
  1305. (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t)
  1306. (org-if-unprotected-at (match-beginning 1)
  1307. (setq label (match-string 1)
  1308. label-components (org-odt-parse-label label)
  1309. category (car label-components)
  1310. value (cdr label-components)
  1311. pretty-label (if value (concat category " " value) label))
  1312. (replace-match
  1313. (let ((org-lparse-encode-pending t))
  1314. (org-odt-format-tags
  1315. '("<text:sequence-ref text:reference-format=\"category-and-value\" text:ref-name=\"%s\">"
  1316. . "</text:sequence-ref>") pretty-label label)) t t)))))
  1317. (declare-function archive-zip-extract "arc-mode.el" (archive name))
  1318. (defun org-odt-zip-extract-one (archive member &optional target)
  1319. (require 'arc-mode)
  1320. (let* ((target (or target default-directory))
  1321. (archive (expand-file-name archive))
  1322. (archive-zip-extract
  1323. (list "unzip" "-qq" "-o" "-d" target))
  1324. exit-code command-output)
  1325. (setq command-output
  1326. (with-temp-buffer
  1327. (setq exit-code (archive-zip-extract archive member))
  1328. (buffer-string)))
  1329. (unless (zerop exit-code)
  1330. (message command-output)
  1331. (error "Extraction failed"))))
  1332. (defun org-odt-zip-extract (archive members &optional target)
  1333. (when (atom members) (setq members (list members)))
  1334. (mapc (lambda (member)
  1335. (org-odt-zip-extract-one archive member target))
  1336. members))
  1337. (defun org-odt-copy-styles-file (&optional styles-file)
  1338. ;; Non-availability of styles.xml is not a critical error. For now
  1339. ;; throw an error purely for aesthetic reasons.
  1340. (setq styles-file (or styles-file
  1341. org-export-odt-styles-file
  1342. (expand-file-name "styles/OrgOdtStyles.xml"
  1343. org-odt-data-dir)
  1344. (error "org-odt: Missing styles file?")))
  1345. (cond
  1346. ((listp styles-file)
  1347. (let ((archive (nth 0 styles-file))
  1348. (members (nth 1 styles-file)))
  1349. (org-odt-zip-extract archive members)
  1350. (mapc
  1351. (lambda (member)
  1352. (when (org-file-image-p member)
  1353. (let* ((image-type (file-name-extension member))
  1354. (media-type (format "image/%s" image-type)))
  1355. (org-odt-update-manifest-file media-type member))))
  1356. members)))
  1357. ((and (stringp styles-file) (file-exists-p styles-file))
  1358. (let ((styles-file-type (file-name-extension styles-file)))
  1359. (cond
  1360. ((string= styles-file-type "xml")
  1361. (copy-file styles-file "styles.xml" t))
  1362. ((member styles-file-type '("odt" "ott"))
  1363. (org-odt-zip-extract styles-file "styles.xml")))))
  1364. (t
  1365. (error (format "Invalid specification of styles.xml file: %S"
  1366. org-export-odt-styles-file)))))
  1367. (defvar org-export-odt-factory-settings
  1368. "d4328fb9d1b6cb211d4320ff546829f26700dc5e"
  1369. "SHA1 hash of OrgOdtStyles.xml.")
  1370. (defun org-odt-configure-outline-numbering (level)
  1371. "Outline numbering is retained only upto LEVEL.
  1372. To disable outline numbering pass a LEVEL of 0."
  1373. (if (not (string= org-export-odt-factory-settings (sha1 (current-buffer))))
  1374. (org-lparse-warn
  1375. "org-odt: Using custom styles file? Consider tweaking styles.xml for better output. To suppress this warning update `org-export-odt-factory-settings'")
  1376. (goto-char (point-min))
  1377. (let ((regex
  1378. "<text:outline-level-style\\(.*\\)text:level=\"\\(.*\\)\"\\(.*\\)>")
  1379. (replacement
  1380. "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
  1381. (while (re-search-forward regex nil t)
  1382. (when (> (string-to-number (match-string 1)) level)
  1383. (replace-match replacement t nil))))
  1384. (save-buffer 0)))
  1385. (provide 'org-odt)
  1386. ;;; org-odt.el ends here