org-odt.el 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601
  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. (defcustom org-export-odt-preferred-output-format nil
  232. "Automatically post-process to this format after exporting to \"odt\".
  233. Interactive commands `org-export-as-odt' and
  234. `org-export-as-odt-and-open' export first to \"odt\" format and
  235. then use an external converter to convert the resulting document
  236. to this format.
  237. The converter used is that specified with CONVERT-METHOD option
  238. in `org-odt-get'. If the above option is unspecified then
  239. `org-lparse-convert-process' is used.
  240. The format specified here should be listed in OTHER-BACKENDS
  241. option of `org-odt-get' or `org-lparse-convert-capabilities' as
  242. appropriate."
  243. :group 'org-odt
  244. :type '(choice :convert-widget
  245. (lambda (w)
  246. (apply 'widget-convert (widget-type w)
  247. (eval (car (widget-get w :args)))))
  248. `((const :tag "None" nil)
  249. ,@(mapcar (lambda (c)
  250. `(const :tag ,(car c) ,(car c)))
  251. (org-lparse-get-other-backends "odt")))))
  252. ;;;###autoload
  253. (defun org-export-as-odt-and-open (arg)
  254. "Export the outline as ODT and immediately open it with a browser.
  255. If there is an active region, export only the region.
  256. The prefix ARG specifies how many levels of the outline should become
  257. headlines. The default is 3. Lower levels will become bulleted lists."
  258. (interactive "P")
  259. (org-lparse-and-open
  260. (or org-export-odt-preferred-output-format "odt") "odt" arg))
  261. ;;;###autoload
  262. (defun org-export-as-odt-batch ()
  263. "Call the function `org-lparse-batch'.
  264. This function can be used in batch processing as:
  265. emacs --batch
  266. --load=$HOME/lib/emacs/org.el
  267. --eval \"(setq org-export-headline-levels 2)\"
  268. --visit=MyFile --funcall org-export-as-odt-batch"
  269. (org-lparse-batch "odt"))
  270. ;;;###autoload
  271. (defun org-export-as-odt-to-buffer (arg)
  272. "Call `org-lparse-odt` with output to a temporary buffer.
  273. No file is created. The prefix ARG is passed through to `org-lparse-to-buffer'."
  274. (interactive "P")
  275. (org-lparse-to-buffer "odt" arg))
  276. ;;;###autoload
  277. (defun org-replace-region-by-odt (beg end)
  278. "Assume the current region has org-mode syntax, and convert it to ODT.
  279. This can be used in any buffer. For example, you could write an
  280. itemized list in org-mode syntax in an ODT buffer and then use this
  281. command to convert it."
  282. (interactive "r")
  283. (org-replace-region-by "odt" beg end))
  284. ;;;###autoload
  285. (defun org-export-region-as-odt (beg end &optional body-only buffer)
  286. "Convert region from BEG to END in org-mode buffer to ODT.
  287. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  288. contents, and only produce the region of converted text, useful for
  289. cut-and-paste operations.
  290. If BUFFER is a buffer or a string, use/create that buffer as a target
  291. of the converted ODT. If BUFFER is the symbol `string', return the
  292. produced ODT as a string and leave not buffer behind. For example,
  293. a Lisp program could call this function in the following way:
  294. (setq odt (org-export-region-as-odt beg end t 'string))
  295. When called interactively, the output buffer is selected, and shown
  296. in a window. A non-interactive call will only return the buffer."
  297. (interactive "r\nP")
  298. (org-lparse-region "odt" beg end body-only buffer))
  299. ;;; org-export-as-odt
  300. ;;;###autoload
  301. (defun org-export-as-odt (arg &optional hidden ext-plist
  302. to-buffer body-only pub-dir)
  303. "Export the outline as a OpenDocumentText file.
  304. If there is an active region, export only the region. The prefix
  305. ARG specifies how many levels of the outline should become
  306. headlines. The default is 3. Lower levels will become bulleted
  307. lists. HIDDEN is obsolete and does nothing.
  308. EXT-PLIST is a property list with external parameters overriding
  309. org-mode's default settings, but still inferior to file-local
  310. settings. When TO-BUFFER is non-nil, create a buffer with that
  311. name and export to that buffer. If TO-BUFFER is the symbol
  312. `string', don't leave any buffer behind but just return the
  313. resulting XML as a string. When BODY-ONLY is set, don't produce
  314. the file header and footer, simply return the content of
  315. <body>...</body>, without even the body tags themselves. When
  316. PUB-DIR is set, use this as the publishing directory."
  317. (interactive "P")
  318. (org-lparse (or org-export-odt-preferred-output-format "odt")
  319. "odt" arg hidden ext-plist to-buffer body-only pub-dir))
  320. (defvar org-odt-entity-control-callbacks-alist
  321. `((EXPORT
  322. . (org-odt-begin-export org-odt-end-export))
  323. (DOCUMENT-CONTENT
  324. . (org-odt-begin-document-content org-odt-end-document-content))
  325. (DOCUMENT-BODY
  326. . (org-odt-begin-document-body org-odt-end-document-body))
  327. (TOC
  328. . (org-odt-begin-toc org-odt-end-toc))
  329. (ENVIRONMENT
  330. . (org-odt-begin-environment org-odt-end-environment))
  331. (FOOTNOTE-DEFINITION
  332. . (org-odt-begin-footnote-definition org-odt-end-footnote-definition))
  333. (TABLE
  334. . (org-odt-begin-table org-odt-end-table))
  335. (TABLE-ROWGROUP
  336. . (org-odt-begin-table-rowgroup org-odt-end-table-rowgroup))
  337. (LIST
  338. . (org-odt-begin-list org-odt-end-list))
  339. (LIST-ITEM
  340. . (org-odt-begin-list-item org-odt-end-list-item))
  341. (OUTLINE
  342. . (org-odt-begin-outline org-odt-end-outline))
  343. (OUTLINE-TEXT
  344. . (org-odt-begin-outline-text org-odt-end-outline-text))
  345. (PARAGRAPH
  346. . (org-odt-begin-paragraph org-odt-end-paragraph)))
  347. "")
  348. (defvar org-odt-entity-format-callbacks-alist
  349. `((EXTRA-TARGETS . org-lparse-format-extra-targets)
  350. (ORG-TAGS . org-lparse-format-org-tags)
  351. (SECTION-NUMBER . org-lparse-format-section-number)
  352. (HEADLINE . org-odt-format-headline)
  353. (TOC-ENTRY . org-odt-format-toc-entry)
  354. (TOC-ITEM . org-odt-format-toc-item)
  355. (TAGS . org-odt-format-tags)
  356. (SPACES . org-odt-format-spaces)
  357. (TABS . org-odt-format-tabs)
  358. (LINE-BREAK . org-odt-format-line-break)
  359. (FONTIFY . org-odt-format-fontify)
  360. (TODO . org-lparse-format-todo)
  361. (LINK . org-odt-format-link)
  362. (INLINE-IMAGE . org-odt-format-inline-image)
  363. (ORG-LINK . org-odt-format-org-link)
  364. (HEADING . org-odt-format-heading)
  365. (ANCHOR . org-odt-format-anchor)
  366. (TABLE . org-lparse-format-table)
  367. (TABLE-ROW . org-odt-format-table-row)
  368. (TABLE-CELL . org-odt-format-table-cell)
  369. (FOOTNOTES-SECTION . ignore)
  370. (FOOTNOTE-REFERENCE . org-odt-format-footnote-reference)
  371. (HORIZONTAL-LINE . org-odt-format-horizontal-line)
  372. (COMMENT . org-odt-format-comment)
  373. (LINE . org-odt-format-line)
  374. (ORG-ENTITY . org-odt-format-org-entity))
  375. "")
  376. ;;;_. callbacks
  377. ;;;_. control callbacks
  378. ;;;_ , document body
  379. (defun org-odt-begin-office-body ()
  380. (insert "
  381. <office:body>
  382. <office:text>
  383. <text:sequence-decls>
  384. <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Illustration\"/>
  385. <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Table\"/>
  386. <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Text\"/>
  387. <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Drawing\"/>
  388. </text:sequence-decls>"))
  389. ;; Following variable is let bound when `org-do-lparse' is in
  390. ;; progress. See org-html.el.
  391. (defvar org-lparse-toc)
  392. (defun org-odt-begin-document-body (opt-plist)
  393. (org-odt-begin-office-body)
  394. (let ((title (plist-get opt-plist :title)))
  395. (when title
  396. (insert
  397. (org-odt-format-stylized-paragraph 'title title))))
  398. ;; insert toc
  399. (when org-lparse-toc
  400. (insert "\n" org-lparse-toc "\n")))
  401. (defvar org-lparse-body-only) ; let bound during org-do-lparse
  402. (defvar org-lparse-to-buffer) ; let bound during org-do-lparse
  403. (defun org-odt-end-document-body (opt-plist)
  404. (unless org-lparse-body-only
  405. (org-lparse-insert-tag "</office:text>")
  406. (org-lparse-insert-tag "</office:body>")))
  407. (defconst org-odt-document-content-header
  408. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  409. <office:document-content
  410. xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
  411. xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\"
  412. xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\"
  413. xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\"
  414. xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\"
  415. xmlns:fo=\"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0\"
  416. xmlns:xlink=\"http://www.w3.org/1999/xlink\"
  417. xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
  418. xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
  419. xmlns:number=\"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0\"
  420. xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\"
  421. xmlns:chart=\"urn:oasis:names:tc:opendocument:xmlns:chart:1.0\"
  422. xmlns:dr3d=\"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0\"
  423. xmlns:math=\"http://www.w3.org/1998/Math/MathML\"
  424. xmlns:form=\"urn:oasis:names:tc:opendocument:xmlns:form:1.0\"
  425. xmlns:script=\"urn:oasis:names:tc:opendocument:xmlns:script:1.0\"
  426. xmlns:ooo=\"http://openoffice.org/2004/office\"
  427. xmlns:ooow=\"http://openoffice.org/2004/writer\"
  428. xmlns:oooc=\"http://openoffice.org/2004/calc\"
  429. xmlns:dom=\"http://www.w3.org/2001/xml-events\"
  430. xmlns:xforms=\"http://www.w3.org/2002/xforms\"
  431. xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
  432. xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
  433. xmlns:rpt=\"http://openoffice.org/2005/report\"
  434. xmlns:of=\"urn:oasis:names:tc:opendocument:xmlns:of:1.2\"
  435. xmlns:xodt=\"http://www.w3.org/1999/xodt\"
  436. xmlns:field=\"urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0\" office:version=\"1.2\">
  437. ")
  438. (defun org-odt-begin-document-content (opt-plist)
  439. ;; document header
  440. (insert org-odt-document-content-header)
  441. ;; automatic styles
  442. (insert-file-contents
  443. (or org-export-odt-automatic-styles-file
  444. (expand-file-name "styles/OrgOdtAutomaticStyles.xml"
  445. org-odt-data-dir)))
  446. (goto-char (point-max)))
  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. (defun org-odt-begin-environment (style)
  481. (case style
  482. ((blockquote verse center quote)
  483. (org-lparse-begin-paragraph style)
  484. (list))
  485. ((fixedwidth native)
  486. (org-lparse-end-paragraph)
  487. (list))
  488. (t (error "Unknown environment %s" style))))
  489. (defun org-odt-end-environment (style)
  490. (case style
  491. ((blockquote verse center quote)
  492. (org-lparse-end-paragraph)
  493. (list))
  494. ((fixedwidth native)
  495. (org-lparse-begin-paragraph)
  496. (list))
  497. (t (error "Unknown environment %s" style))))
  498. (defvar org-lparse-list-level) ; dynamically bound in org-do-lparse
  499. (defun org-odt-begin-list (ltype)
  500. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  501. ltype))
  502. (let* ((style-name (org-odt-get-style-name-for-entity 'list ltype))
  503. (extra (concat (when (= org-lparse-list-level 1)
  504. " text:continue-numbering=\"false\"")
  505. (when style-name
  506. (format " text:style-name=\"%s\"" style-name)))))
  507. (case ltype
  508. ((ordered unordered description)
  509. (org-lparse-end-paragraph)
  510. (org-lparse-insert-tag "<text:list%s>" extra))
  511. (t (error "Unknown list type: %s" ltype)))))
  512. (defun org-odt-end-list (ltype)
  513. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  514. ltype))
  515. (if ltype
  516. (org-lparse-insert-tag "</text:list>")
  517. (error "Unknown list type: %s" ltype)))
  518. (defun org-odt-begin-list-item (ltype &optional arg headline)
  519. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  520. ltype))
  521. (case ltype
  522. (ordered
  523. (assert (not headline) t)
  524. (let* ((counter arg) (extra ""))
  525. (org-lparse-insert-tag "<text:list-item>")
  526. (org-lparse-begin-paragraph)))
  527. (unordered
  528. (let* ((id arg) (extra ""))
  529. (org-lparse-insert-tag "<text:list-item>")
  530. (org-lparse-begin-paragraph)
  531. (insert (if headline (org-odt-format-target headline id)
  532. (org-odt-format-bookmark "" id)))))
  533. (description
  534. (assert (not headline) t)
  535. (let ((term (or arg "(no term)")))
  536. (insert
  537. (org-odt-format-tags
  538. '("<text:list-item>" . "</text:list-item>")
  539. (org-odt-format-stylized-paragraph 'definition-term term)))
  540. (org-lparse-begin 'LIST-ITEM 'unordered)
  541. (org-lparse-begin-list 'description)
  542. (org-lparse-begin 'LIST-ITEM 'unordered)))
  543. (t (error "Unknown list type"))))
  544. (defun org-odt-end-list-item (ltype)
  545. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  546. ltype))
  547. (case ltype
  548. ((ordered unordered)
  549. (org-lparse-insert-tag "</text:list-item>"))
  550. (description
  551. (org-lparse-end-list-item)
  552. (org-lparse-end-list 'description)
  553. (org-lparse-end-list-item))
  554. (t (error "Unknown list type"))))
  555. ;; Following variables are let bound when table emission is in
  556. ;; progress. See org-lparse.el.
  557. (defvar org-lparse-table-begin-marker)
  558. (defvar org-lparse-table-ncols)
  559. (defvar org-lparse-table-rowgrp-open)
  560. (defvar org-lparse-table-rownum)
  561. (defvar org-lparse-table-cur-rowgrp-is-hdr)
  562. (defvar org-lparse-table-is-styled)
  563. (defvar org-lparse-table-rowgrp-info)
  564. (defvar org-lparse-table-colalign-vector)
  565. (defun org-odt-begin-table (caption label attributes)
  566. (when label
  567. (insert
  568. (org-odt-format-stylized-paragraph
  569. 'table (org-odt-format-entity-caption label caption "Table"))))
  570. (org-lparse-insert-tag
  571. "<table:table table:name=\"%s\" table:style-name=\"%s\">"
  572. (or label "") "OrgTable")
  573. (setq org-lparse-table-begin-marker (point)))
  574. (defun org-odt-end-table ()
  575. (goto-char org-lparse-table-begin-marker)
  576. (loop for level from 0 below org-lparse-table-ncols
  577. do (insert (org-odt-format-tags "<table:table-column/>" "")))
  578. ;; fill style attributes for table cells
  579. (when org-lparse-table-is-styled
  580. (while (re-search-forward "@@\\(table-cell:p\\|table-cell:style-name\\)@@\\([0-9]+\\)@@\\([0-9]+\\)@@" nil t)
  581. (let ((spec (match-string 1))
  582. (r (string-to-number (match-string 2)))
  583. (c (string-to-number (match-string 3))))
  584. (cond
  585. ((equal spec "table-cell:p")
  586. (let ((style-name (org-odt-get-paragraph-style-for-table-cell r c)))
  587. (replace-match style-name t t)))
  588. ((equal spec "table-cell:style-name")
  589. (let ((style-name (org-odt-get-style-name-for-table-cell r c)))
  590. (replace-match style-name t t)))))))
  591. (goto-char (point-max))
  592. (org-lparse-insert-tag "</table:table>"))
  593. (defun org-odt-begin-table-rowgroup (&optional is-header-row)
  594. (when org-lparse-table-rowgrp-open
  595. (org-lparse-end 'TABLE-ROWGROUP))
  596. (org-lparse-insert-tag (if is-header-row
  597. "<table:table-header-rows>"
  598. "<table:table-rows>"))
  599. (setq org-lparse-table-rowgrp-open t)
  600. (setq org-lparse-table-cur-rowgrp-is-hdr is-header-row))
  601. (defun org-odt-end-table-rowgroup ()
  602. (when org-lparse-table-rowgrp-open
  603. (setq org-lparse-table-rowgrp-open nil)
  604. (org-lparse-insert-tag
  605. (if org-lparse-table-cur-rowgrp-is-hdr
  606. "</table:table-header-rows>" "</table:table-rows>"))))
  607. (defun org-odt-format-table-row (row)
  608. (org-odt-format-tags
  609. '("<table:table-row>" . "</table:table-row>") row))
  610. (defun org-odt-get-style-name-for-table-cell (r c)
  611. (concat
  612. "OrgTblCell"
  613. (cond
  614. ((= r 0) "T")
  615. ((eq (cdr (assoc r org-lparse-table-rowgrp-info)) :start) "T")
  616. (t ""))
  617. (when (= r org-lparse-table-rownum) "B")
  618. (cond
  619. ((= c 0) "")
  620. ((or (memq (nth c org-table-colgroup-info) '(:start :startend))
  621. (memq (nth (1- c) org-table-colgroup-info) '(:end :startend))) "L")
  622. (t ""))))
  623. (defun org-odt-get-paragraph-style-for-table-cell (r c)
  624. (capitalize (aref org-lparse-table-colalign-vector c)))
  625. (defun org-odt-format-table-cell (data r c)
  626. (if (not org-lparse-table-is-styled)
  627. (org-odt-format-tags
  628. '("<table:table-cell>" . "</table:table-cell>")
  629. (org-odt-format-stylized-paragraph
  630. (cond
  631. (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
  632. ((and (= c 0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS))
  633. "OrgTableHeading")
  634. (t "OrgTableContents"))
  635. data))
  636. (let* ((style-name-cookie
  637. (format "@@table-cell:style-name@@%03d@@%03d@@" r c))
  638. (paragraph-style-cookie
  639. (concat
  640. (cond
  641. (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
  642. ((and (= c 0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS))
  643. "OrgTableHeading")
  644. (t "OrgTableContents"))
  645. (format "@@table-cell:p@@%03d@@%03d@@" r c))))
  646. (org-odt-format-tags
  647. '("<table:table-cell table:style-name=\"%s\">" .
  648. "</table:table-cell>")
  649. (org-odt-format-stylized-paragraph paragraph-style-cookie data)
  650. style-name-cookie))))
  651. (defun org-odt-begin-footnote-definition (n)
  652. (org-lparse-begin-paragraph 'footnote))
  653. (defun org-odt-end-footnote-definition (n)
  654. (org-lparse-end-paragraph))
  655. (defun org-odt-begin-toc (lang-specific-heading)
  656. (insert
  657. (format "
  658. <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
  659. <text:table-of-content-source text:outline-level=\"10\">
  660. <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
  661. " lang-specific-heading))
  662. (loop for level from 1 upto 10
  663. do (insert (format
  664. "
  665. <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
  666. <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
  667. <text:index-entry-chapter/>
  668. <text:index-entry-text/>
  669. <text:index-entry-link-end/>
  670. </text:table-of-content-entry-template>
  671. " level level)))
  672. (insert
  673. (format "
  674. </text:table-of-content-source>
  675. <text:index-body>
  676. <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
  677. <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
  678. </text:index-title>
  679. " lang-specific-heading)))
  680. (defun org-odt-end-toc ()
  681. (insert "
  682. </text:index-body>
  683. </text:table-of-content>
  684. "))
  685. (defun org-odt-format-toc-entry (snumber todo headline tags href)
  686. (setq headline (concat
  687. (and org-export-with-section-numbers
  688. (concat snumber ". "))
  689. headline
  690. (and tags
  691. (concat
  692. (org-lparse-format 'SPACES 3)
  693. (org-lparse-format 'FONTIFY tags "tag")))))
  694. (when todo
  695. (setq headline (org-lparse-format 'FONTIFY headline "todo")))
  696. (let ((org-odt-suppress-xref t))
  697. (org-odt-format-link headline (concat "#" href))))
  698. (defun org-odt-format-toc-item (toc-entry level org-last-level)
  699. (let ((style (format "Contents_20_%d"
  700. (+ level (or (org-lparse-get 'TOPLEVEL-HLEVEL) 1) -1))))
  701. (insert "\n" (org-odt-format-stylized-paragraph style toc-entry) "\n")))
  702. ;; Following variable is let bound during 'ORG-LINK callback. See
  703. ;; org-html.el
  704. (defvar org-lparse-link-description-is-image nil)
  705. (defun org-odt-format-link (desc href &optional attr)
  706. (cond
  707. ((and (= (string-to-char href) ?#) (not org-odt-suppress-xref))
  708. (setq href (concat org-export-odt-bookmark-prefix (substring href 1)))
  709. (org-odt-format-tags
  710. '("<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"%s\">" .
  711. "</text:bookmark-ref>")
  712. desc href))
  713. (org-lparse-link-description-is-image
  714. (org-odt-format-tags
  715. '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</draw:a>")
  716. desc href (or attr "")))
  717. (t
  718. (org-odt-format-tags
  719. '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</text:a>")
  720. desc href (or attr "")))))
  721. (defun org-odt-format-spaces (n)
  722. (org-odt-format-tags "<text:s text:c=\"%d\"/>" "" n))
  723. (defun org-odt-format-tabs (&optional n)
  724. (let ((tab "<text:tab/>")
  725. (n (or n 1)))
  726. (insert tab)))
  727. (defun org-odt-format-line-break ()
  728. (org-odt-format-tags "<text:line-break/>" ""))
  729. (defun org-odt-format-horizontal-line ()
  730. (org-odt-format-stylized-paragraph 'horizontal-line ""))
  731. (defun org-odt-format-line (line)
  732. (case org-lparse-dyn-current-environment
  733. (fixedwidth (concat (org-odt-format-source-code-or-example-line
  734. (org-xml-encode-plain-text line)) "\n"))
  735. (t (concat line "\n"))))
  736. (defun org-odt-format-comment (fmt &rest args)
  737. (let ((comment (apply 'format fmt args)))
  738. (format "\n<!-- %s -->\n" comment)))
  739. (defun org-odt-format-org-entity (wd)
  740. (org-entity-get-representation wd 'utf8))
  741. (defun org-odt-fill-tabs-and-spaces (line)
  742. (replace-regexp-in-string
  743. "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
  744. (cond
  745. ((string= s "\t") (org-odt-format-tabs))
  746. ((> (length s) 1)
  747. (org-odt-format-spaces (length s)))
  748. (t " "))) line))
  749. (defun org-odt-format-source-code-or-example-line (line)
  750. (org-odt-format-stylized-paragraph 'src (org-odt-fill-tabs-and-spaces line)))
  751. (defun org-odt-format-example (lines)
  752. (mapconcat
  753. (lambda (line)
  754. (org-odt-format-source-code-or-example-line line))
  755. (org-split-string lines "[\r\n]") "\n"))
  756. (defun org-odt-format-source-code-or-example (lines lang caption textareap
  757. cols rows num cont
  758. rpllbl fmt)
  759. (org-odt-format-example (org-export-number-lines
  760. (org-xml-encode-plain-text-lines lines)
  761. 0 0 num cont rpllbl fmt)))
  762. (defun org-xml-encode-plain-text-lines (rtn)
  763. (mapconcat 'org-xml-encode-plain-text (org-split-string rtn "[\r\n]") "\n"))
  764. (defun org-odt-remap-stylenames (style-name)
  765. (or
  766. (cdr (assoc style-name '(("timestamp-wrapper" . "OrgTimestampWrapper")
  767. ("timestamp" . "OrgTimestamp")
  768. ("timestamp-kwd" . "OrgTimestampKeyword")
  769. ("tag" . "OrgTag")
  770. ("todo" . "OrgTodo")
  771. ("done" . "OrgDone")
  772. ("target" . "OrgTarget"))))
  773. style-name))
  774. (defun org-odt-format-fontify (text style &optional id)
  775. (let* ((style-name
  776. (cond
  777. ((stringp style)
  778. (org-odt-remap-stylenames style))
  779. ((symbolp style)
  780. (org-odt-get-style-name-for-entity 'character style))
  781. ((listp style)
  782. (assert (< 1 (length style)))
  783. (let ((parent-style (pop style)))
  784. (mapconcat (lambda (s)
  785. ;; (assert (stringp s) t)
  786. (org-odt-remap-stylenames s)) style "")
  787. (org-odt-remap-stylenames parent-style)))
  788. (t (error "Don't how to handle style %s" style)))))
  789. (org-odt-format-tags
  790. '("<text:span text:style-name=\"%s\">" . "</text:span>")
  791. text style-name)))
  792. (defun org-odt-relocate-relative-path (path dir)
  793. (if (file-name-absolute-p path) path
  794. (file-relative-name (expand-file-name path dir)
  795. (expand-file-name "eyecandy" dir))))
  796. (defun org-odt-format-inline-image (thefile)
  797. (let* ((thelink (if (file-name-absolute-p thefile) thefile
  798. (org-xml-format-href
  799. (org-odt-relocate-relative-path
  800. thefile org-current-export-file))))
  801. (href
  802. (org-odt-format-tags
  803. "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
  804. (if org-export-odt-embed-images
  805. (org-odt-copy-image-file thefile) thelink))))
  806. (org-export-odt-format-image thefile href)))
  807. (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
  808. descp)
  809. "Make an HTML link.
  810. OPT-PLIST is an options list.
  811. TYPE is the device-type of the link (THIS://foo.html)
  812. PATH is the path of the link (http://THIS#locationx)
  813. FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
  814. DESC is the link description, if any.
  815. ATTR is a string of other attributes of the a element.
  816. MAY-INLINE-P allows inlining it as an image."
  817. (declare (special org-lparse-par-open))
  818. (save-match-data
  819. (let* ((may-inline-p
  820. (and (member type-1 '("http" "https" "file"))
  821. (org-lparse-should-inline-p path descp)
  822. (not fragment)))
  823. (type (if (equal type-1 "id") "file" type-1))
  824. (filename path)
  825. (thefile path))
  826. (cond
  827. ;; check for inlined images
  828. ((and (member type '("file"))
  829. (not fragment)
  830. (org-file-image-p
  831. filename org-odt-export-inline-image-extensions)
  832. (or (eq t org-odt-export-inline-images)
  833. (and org-odt-export-inline-images (not descp))))
  834. ;; (when (and (string= type "file") (file-name-absolute-p path))
  835. ;; (setq thefile (concat "file://" (expand-file-name path))))
  836. ;; (setq thefile (org-xml-format-href thefile))
  837. ;; (org-export-html-format-image thefile)
  838. (org-odt-format-inline-image thefile))
  839. (t
  840. (when (string= type "file")
  841. (setq thefile
  842. (cond
  843. ((file-name-absolute-p path)
  844. (concat "file://" (expand-file-name path)))
  845. (t (org-odt-relocate-relative-path
  846. thefile org-current-export-file)))))
  847. (when (and (member type '("" "http" "https" "file" "coderef"))
  848. fragment)
  849. (setq thefile (concat thefile "#" fragment)))
  850. (setq thefile (org-xml-format-href thefile))
  851. (when (not (member type '("" "file" "coderef")))
  852. (setq thefile (concat type ":" thefile)))
  853. (let ((org-odt-suppress-xref (string= type "coderef")))
  854. (org-odt-format-link
  855. (org-xml-format-desc desc) thefile attr)))))))
  856. (defun org-odt-format-heading (text level &optional id)
  857. (let* ((text (if id (org-odt-format-target text id) text)))
  858. (org-odt-format-tags
  859. '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
  860. "</text:h>") text level level)))
  861. (defun org-odt-format-headline (title extra-targets tags
  862. &optional snumber level)
  863. (concat
  864. (org-lparse-format 'EXTRA-TARGETS extra-targets)
  865. ;; No need to generate section numbers. They are auto-generated by
  866. ;; the application
  867. ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
  868. title
  869. (and tags (concat (org-lparse-format 'SPACES 3)
  870. (org-lparse-format 'ORG-TAGS tags)))))
  871. (defun org-odt-format-anchor (text name &optional class)
  872. (org-odt-format-target text name))
  873. (defun org-odt-format-bookmark (text id)
  874. (if id
  875. (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id)
  876. text))
  877. (defun org-odt-format-target (text id)
  878. (let ((name (concat org-export-odt-bookmark-prefix id)))
  879. (concat
  880. (and id (org-odt-format-tags
  881. "<text:bookmark-start text:name=\"%s\"/>" "" name))
  882. (org-odt-format-bookmark text id)
  883. (and id (org-odt-format-tags
  884. "<text:bookmark-end text:name=\"%s\"/>" "" name)))))
  885. (defun org-odt-format-footnote (n def)
  886. (let ((id (concat "fn" n))
  887. (note-class "footnote")
  888. (par-style "Footnote"))
  889. (org-odt-format-tags
  890. '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
  891. "</text:note>")
  892. (concat
  893. (org-odt-format-tags
  894. '("<text:note-citation>" . "</text:note-citation>")
  895. n)
  896. (org-odt-format-tags
  897. '("<text:note-body>" . "</text:note-body>")
  898. def))
  899. id note-class)))
  900. (defun org-odt-format-footnote-reference (n def refcnt)
  901. (if (= refcnt 1)
  902. (org-odt-format-footnote n def)
  903. (org-odt-format-footnote-ref n)))
  904. (defun org-odt-format-footnote-ref (n)
  905. (let ((note-class "footnote")
  906. (ref-format "text")
  907. (ref-name (concat "fn" n)))
  908. (org-odt-format-tags
  909. '("<text:span text:style-name=\"%s\">" . "</text:span>")
  910. (org-odt-format-tags
  911. '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" . "</text:note-ref>")
  912. n note-class ref-format ref-name)
  913. "OrgSuperscript")))
  914. (defun org-odt-get-image-name (file-name)
  915. (require 'sha1)
  916. (file-relative-name
  917. (expand-file-name
  918. (concat (sha1 file-name) "." (file-name-extension file-name)) "Pictures")))
  919. (defun org-export-odt-format-image (src href
  920. ;; par-open
  921. )
  922. "Create image tag with source and attributes."
  923. (save-match-data
  924. (let (embed-as caption attr label attr-plist size width height)
  925. (cond
  926. ((string-match "^ltxpng/" src)
  927. ;; FIXME: Anyway the latex src can be embedded as an
  928. ;; annotation
  929. ;; (org-find-text-property-in-string 'org-latex-src src)
  930. (setq caption nil attr nil label nil embed-as 'character))
  931. (t
  932. (setq caption (org-find-text-property-in-string 'org-caption src)
  933. caption (and caption (org-xml-format-desc caption))
  934. attr (org-find-text-property-in-string 'org-attributes src)
  935. label (org-find-text-property-in-string 'org-label src)
  936. embed-as 'paragraph)))
  937. (setq attr-plist (when attr (read attr)))
  938. (setq size (org-odt-image-size-from-file
  939. src (plist-get attr-plist :width)
  940. (plist-get attr-plist :height)
  941. (plist-get attr-plist :scale) nil embed-as))
  942. (org-export-odt-do-format-image embed-as caption attr label
  943. size href))))
  944. (defun org-export-odt-do-format-image (embed-as caption attr label
  945. size href)
  946. "Create image tag with source and attributes."
  947. (save-match-data
  948. (let ((width (car size)) (height (cdr size))
  949. (draw-frame-pair
  950. '("<draw:frame draw:style-name=\"%s\"
  951. text:anchor-type=\"%s\"
  952. draw:z-index=\"%d\" %s>" . "</draw:frame>")))
  953. (cond
  954. ((and (not caption) (not label))
  955. (let (style-name anchor-type)
  956. (cond
  957. ((eq embed-as 'paragraph)
  958. (setq style-name "OrgGraphicsParagraph" anchor-type "paragraph"))
  959. ((eq embed-as 'character)
  960. (setq style-name "OrgGraphicsBaseline" anchor-type "as-char")))
  961. (org-odt-format-tags
  962. draw-frame-pair href style-name anchor-type 0
  963. (org-odt-image-attrs-from-size width height))))
  964. (t
  965. (concat
  966. ;; (when par-open (org-odt-close-par))
  967. (org-odt-format-tags
  968. draw-frame-pair
  969. (org-odt-format-tags
  970. '("<draw:text-box fo:min-height=\"%dcm\">" . "</draw:text-box>")
  971. (org-odt-format-stylized-paragraph
  972. 'illustration
  973. (concat
  974. (let ((extra " style:rel-width=\"100%\" style:rel-height=\"scale\""))
  975. (org-odt-format-tags
  976. draw-frame-pair href "OrgGraphicsParagraphContent" "paragraph" 2
  977. (concat (org-odt-image-attrs-from-size width height) extra)))
  978. (org-odt-format-entity-caption label caption)))
  979. height)
  980. "OrgFrame" "paragraph" 1
  981. (org-odt-image-attrs-from-size width))
  982. ;; (when par-open (org-odt-open-par))
  983. ))))))
  984. ;; xml files generated on-the-fly
  985. (defconst org-export-odt-save-list
  986. '("mimetype" "META-INF/manifest.xml" "content.xml" "meta.xml" "styles.xml"))
  987. ;; xml files that are copied
  988. (defconst org-export-odt-nosave-list '())
  989. ;; xml files that contribute to the final odt file
  990. (defvar org-export-odt-file-list nil)
  991. (defconst org-export-odt-manifest-lines
  992. '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  993. "<manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">"
  994. "<manifest:file-entry manifest:media-type=\"application/vnd.oasis.opendocument.text\" manifest:version=\"1.2\" manifest:full-path=\"/\"/>"
  995. "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"content.xml\"/>"
  996. "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"styles.xml\"/>"
  997. "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"meta.xml\"/>"
  998. "<manifest:file-entry manifest:media-type=\"\" manifest:full-path=\"Pictures/\"/>") . ("</manifest:manifest>")))
  999. (defconst org-export-odt-meta-lines
  1000. '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  1001. "<office:document-meta"
  1002. " xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\""
  1003. " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
  1004. " xmlns:dc=\"http://purl.org/dc/elements/1.1/\""
  1005. " xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\""
  1006. " xmlns:ooo=\"http://openoffice.org/2004/office\" "
  1007. " office:version=\"1.2\">"
  1008. " <office:meta>") . (" </office:meta>" "</office:document-meta>")))
  1009. (defun org-odt-copy-image-file (path &optional target-file)
  1010. "Returns the internal name of the file"
  1011. (let* ((image-type (file-name-extension path))
  1012. (media-type (format "image/%s" image-type))
  1013. (src-file (expand-file-name
  1014. path (file-name-directory org-current-export-file)))
  1015. (target-file (or target-file (org-odt-get-image-name src-file)))
  1016. ;; FIXME
  1017. (body-only nil))
  1018. (when (not org-lparse-to-buffer)
  1019. (message "Embedding %s as %s ..."
  1020. (substring-no-properties path) target-file)
  1021. (copy-file src-file target-file 'overwrite)
  1022. (org-odt-update-manifest-file media-type target-file)
  1023. (push target-file org-export-odt-file-list)) target-file))
  1024. (defun org-odt-image-attrs-from-size (&optional width height)
  1025. (concat
  1026. (when width (format "svg:width=\"%0.2fcm\"" width))
  1027. " "
  1028. (when height (format "svg:height=\"%0.2fcm\"" height))))
  1029. (defvar org-export-odt-image-size-probe-method
  1030. '(emacs imagemagick force)
  1031. "Ordered list of methods by for determining size of an embedded
  1032. image.")
  1033. (defvar org-export-odt-default-image-sizes-alist
  1034. '(("character" . (5 . 0.4))
  1035. ("paragraph" . (5 . 5)))
  1036. "Hardcoded image dimensions one for each of the anchor
  1037. methods.")
  1038. (defun org-odt-do-image-size (probe-method file &optional dpi anchor-type)
  1039. (setq dpi (or dpi org-export-odt-pixels-per-inch))
  1040. (setq anchor-type (or anchor-type "paragraph"))
  1041. (flet ((size-in-cms (size-in-pixels)
  1042. (flet ((pixels-to-cms (pixels)
  1043. (let* ((cms-per-inch 2.54)
  1044. (inches (/ pixels dpi)))
  1045. (* cms-per-inch inches))))
  1046. (and size-in-pixels
  1047. (cons (pixels-to-cms (car size-in-pixels))
  1048. (pixels-to-cms (cdr size-in-pixels)))))))
  1049. (case probe-method
  1050. (emacs
  1051. (size-in-cms (ignore-errors (image-size (create-image file) 'pixels))))
  1052. (imagemagick
  1053. (size-in-cms
  1054. (let ((dim (shell-command-to-string
  1055. (format "identify -format \"%%w:%%h\" \"%s\"" file))))
  1056. (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
  1057. (cons (string-to-number (match-string 1 dim))
  1058. (string-to-number (match-string 2 dim)))))))
  1059. (t
  1060. (cdr (assoc-string anchor-type
  1061. org-export-odt-default-image-sizes-alist))))))
  1062. (defun org-odt-image-size-from-file (file &optional user-width
  1063. user-height scale dpi embed-as)
  1064. (unless (file-name-absolute-p file)
  1065. (setq file (expand-file-name
  1066. file (file-name-directory org-current-export-file))))
  1067. (let* (size width height)
  1068. (unless (and user-height user-width)
  1069. (loop for probe-method in org-export-odt-image-size-probe-method
  1070. until size
  1071. do (setq size (org-odt-do-image-size
  1072. probe-method file dpi embed-as)))
  1073. (or size (error "Cannot determine Image size. Aborting ..."))
  1074. (setq width (car size) height (cdr size)))
  1075. (cond
  1076. (scale
  1077. (setq width (* width scale) height (* height scale)))
  1078. ((and user-height user-width)
  1079. (setq width user-width height user-height))
  1080. (user-height
  1081. (setq width (* user-height (/ width height)) height user-height))
  1082. (user-width
  1083. (setq height (* user-width (/ height width)) width user-width))
  1084. (t (ignore)))
  1085. (cons width height)))
  1086. (defvar org-odt-default-entity "Illustration")
  1087. (defun org-odt-format-entity-caption (label caption &optional default-entity)
  1088. (if (not label) (or caption "")
  1089. (let* ((label-components (org-odt-parse-label label))
  1090. (entity (car label-components))
  1091. (seqno (cdr label-components))
  1092. (caption (and caption (concat ": " caption))))
  1093. (unless seqno
  1094. (setq seqno label
  1095. entity (or default-entity org-odt-default-entity)))
  1096. (concat
  1097. entity " "
  1098. (org-odt-format-tags
  1099. '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" . "</text:sequence>")
  1100. seqno label entity entity)
  1101. caption))))
  1102. (defun org-odt-format-tags (tag text &rest args)
  1103. (let ((prefix (when org-lparse-encode-pending "@"))
  1104. (suffix (when org-lparse-encode-pending "@")))
  1105. (apply 'org-lparse-format-tags tag text prefix suffix args)))
  1106. (defun org-odt-init-outfile (filename)
  1107. (unless (executable-find "zip")
  1108. ;; Not at all OSes ship with zip by default
  1109. (error "Executable \"zip\" needed for creating OpenDocument files. Aborting."))
  1110. (let* ((outdir (make-temp-file org-export-odt-tmpdir-prefix t))
  1111. (mimetype-file (expand-file-name "mimetype" outdir))
  1112. (content-file (expand-file-name "content.xml" outdir))
  1113. (manifest-file (expand-file-name "META-INF/manifest.xml" outdir))
  1114. (meta-file (expand-file-name "meta.xml" outdir))
  1115. (styles-file (expand-file-name "styles.xml" outdir))
  1116. (pictures-dir (expand-file-name "Pictures" outdir))
  1117. (body-only nil))
  1118. ;; content file
  1119. (with-current-buffer (find-file-noselect content-file t)
  1120. (erase-buffer))
  1121. ;; FIXME: How to factor in body-only here
  1122. (unless body-only
  1123. ;; manifest file
  1124. (make-directory (file-name-directory manifest-file))
  1125. (with-current-buffer (find-file-noselect manifest-file t)
  1126. (erase-buffer)
  1127. (insert (mapconcat 'identity (car org-export-odt-manifest-lines) "\n"))
  1128. (insert "\n")
  1129. (save-excursion
  1130. (insert (mapconcat 'identity (cdr org-export-odt-manifest-lines) "\n"))))
  1131. ;; meta file
  1132. (with-current-buffer (find-file-noselect meta-file t)
  1133. (erase-buffer)
  1134. (insert (mapconcat 'identity (car org-export-odt-meta-lines) "\n"))
  1135. (insert "\n")
  1136. (save-excursion
  1137. (insert (mapconcat 'identity (cdr org-export-odt-meta-lines) "\n"))))
  1138. ;; mimetype
  1139. (with-current-buffer (find-file-noselect mimetype-file t)
  1140. (insert "application/vnd.oasis.opendocument.text"))
  1141. ;; styles file
  1142. ;; (copy-file org-export-odt-styles-file styles-file t)
  1143. ;; Pictures dir
  1144. (make-directory pictures-dir)
  1145. ;; initialize list of files that contribute to the odt file
  1146. (setq org-export-odt-file-list
  1147. (append org-export-odt-save-list org-export-odt-nosave-list)))
  1148. content-file))
  1149. (defconst org-odt-manifest-file-entry-tag
  1150. "<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"/>")
  1151. (defun org-odt-save-as-outfile (target opt-plist)
  1152. ;; write meta file
  1153. (org-odt-update-meta-file opt-plist)
  1154. ;; write styles file
  1155. (org-odt-copy-styles-file)
  1156. ;; Update styles.xml - take care of outline numbering
  1157. (with-current-buffer
  1158. (find-file-noselect (expand-file-name "styles.xml") t)
  1159. ;; Don't make automatic backup of styles.xml file. This setting
  1160. ;; prevents the backedup styles.xml file from being zipped in to
  1161. ;; odt file. This is more of a hackish fix. Better alternative
  1162. ;; would be to fix the zip command so that the output odt file
  1163. ;; includes only the needed files and excludes any auto-generated
  1164. ;; extra files like backups and auto-saves etc etc. Note that
  1165. ;; currently the zip command zips up the entire temp directory so
  1166. ;; that any auto-generated files created under the hood ends up in
  1167. ;; the resulting odt file.
  1168. (set (make-local-variable 'backup-inhibited) t)
  1169. ;; Import local setting of `org-export-with-section-numbers'
  1170. (org-lparse-bind-local-variables opt-plist)
  1171. (org-odt-configure-outline-numbering
  1172. (if org-export-with-section-numbers org-export-headline-levels 0)))
  1173. (let ((zipdir default-directory))
  1174. (message "Switching to directory %s" (expand-file-name zipdir))
  1175. ;; save all xml files
  1176. (mapc (lambda (file)
  1177. (with-current-buffer
  1178. (find-file-noselect (expand-file-name file) t)
  1179. ;; prettify output
  1180. (indent-region (point-min) (point-max))
  1181. (save-buffer)))
  1182. org-export-odt-save-list)
  1183. (let* ((target-name (file-name-nondirectory target))
  1184. (target-dir (file-name-directory target))
  1185. (cmds `(("zip" "-mX0" ,target-name "mimetype")
  1186. ("zip" "-rmTq" ,target-name "."))))
  1187. (when (file-exists-p target)
  1188. ;; FIXME: If the file is locked this throws a cryptic error
  1189. (delete-file target))
  1190. (let ((coding-system-for-write 'no-conversion) exitcode)
  1191. (message "Creating odt file...")
  1192. (mapc
  1193. (lambda (cmd)
  1194. (message "Running %s" (mapconcat 'identity cmd " "))
  1195. (setq exitcode
  1196. (apply 'call-process (car cmd) nil nil nil (cdr cmd)))
  1197. (or (zerop exitcode)
  1198. (error "Unable to create odt file (%S)" exitcode)))
  1199. cmds))
  1200. ;; move the file from outdir to target-dir
  1201. (rename-file target-name target-dir)
  1202. ;; kill all xml buffers
  1203. (mapc (lambda (file)
  1204. (kill-buffer
  1205. (find-file-noselect (expand-file-name file zipdir) t)))
  1206. org-export-odt-save-list)
  1207. (delete-directory zipdir)))
  1208. (message "Created %s" target)
  1209. (set-buffer (find-file-noselect target t)))
  1210. (defun org-odt-format-date (date)
  1211. (let ((warning-msg
  1212. "OpenDocument files require that dates be in ISO-8601 format. Please review your DATE options for compatibility."))
  1213. ;; If the user is not careful with the date specification, an
  1214. ;; invalid meta.xml will be emitted.
  1215. ;; For now honor user's diktat and let him off with a warning
  1216. ;; message. This is OK as LibreOffice (and possibly other
  1217. ;; apps) doesn't deem this deviation as critical and continue
  1218. ;; to load the file.
  1219. ;; FIXME: Surely there a better way to handle this. Revisit this
  1220. ;; later.
  1221. (cond
  1222. ((and date (string-match "%" date))
  1223. ;; Honor user's diktat. See comments above
  1224. (org-lparse-warn warning-msg)
  1225. (format-time-string date))
  1226. (date
  1227. ;; Honor user's diktat. See comments above
  1228. (org-lparse-warn warning-msg)
  1229. date)
  1230. (t
  1231. ;; ISO 8601 format
  1232. (format-time-string "%Y-%m-%dT%T%:z")))))
  1233. (defun org-odt-update-meta-file (opt-plist)
  1234. (with-current-buffer
  1235. (find-file-noselect (expand-file-name "meta.xml") t)
  1236. (let ((date (org-odt-format-date (plist-get opt-plist :date)))
  1237. (author (or (plist-get opt-plist :author) ""))
  1238. (email (plist-get opt-plist :email))
  1239. (keywords (plist-get opt-plist :keywords))
  1240. (description (plist-get opt-plist :description))
  1241. (title (plist-get opt-plist :title)))
  1242. (insert
  1243. "\n"
  1244. (org-odt-format-tags '("<dc:creator>" . "</dc:creator>") author)
  1245. (org-odt-format-tags
  1246. '("\n<meta:initial-creator>" . "</meta:initial-creator>") author)
  1247. (org-odt-format-tags '("\n<dc:date>" . "</dc:date>") date)
  1248. (org-odt-format-tags
  1249. '("\n<meta:creation-date>" . "</meta:creation-date>") date)
  1250. (org-odt-format-tags '("\n<meta:generator>" . "</meta:generator>")
  1251. (when org-export-creator-info
  1252. (format "Org-%s/Emacs-%s"
  1253. org-version emacs-version)))
  1254. (org-odt-format-tags '("\n<meta:keyword>" . "</meta:keyword>") keywords)
  1255. (org-odt-format-tags '("\n<dc:subject>" . "</dc:subject>") description)
  1256. (org-odt-format-tags '("\n<dc:title>" . "</dc:title>") title)
  1257. "\n"))))
  1258. (defun org-odt-update-manifest-file (media-type full-path)
  1259. (with-current-buffer
  1260. (find-file-noselect (expand-file-name "META-INF/manifest.xml") t)
  1261. (insert (format org-odt-manifest-file-entry-tag media-type full-path))))
  1262. (defun org-odt-finalize-outfile ()
  1263. (message "org-newodt: Finalizing outfile")
  1264. (org-odt-delete-empty-paragraphs))
  1265. (defun org-odt-delete-empty-paragraphs ()
  1266. (goto-char (point-min))
  1267. (let ((open "<text:p[^>]*>")
  1268. (close "</text:p>"))
  1269. (while (re-search-forward (format "%s[ \r\n\t]*%s" open close) nil t)
  1270. (replace-match ""))))
  1271. (defun org-odt-get (what &optional opt-plist)
  1272. (case what
  1273. (BACKEND 'odt)
  1274. (EXPORT-DIR (org-export-directory :html opt-plist))
  1275. (FILE-NAME-EXTENSION "odt")
  1276. (EXPORT-BUFFER-NAME "*Org ODT Export*")
  1277. (ENTITY-CONTROL org-odt-entity-control-callbacks-alist)
  1278. (ENTITY-FORMAT org-odt-entity-format-callbacks-alist)
  1279. (INIT-METHOD 'org-odt-init-outfile)
  1280. (FINAL-METHOD 'org-odt-finalize-outfile)
  1281. (SAVE-METHOD 'org-odt-save-as-outfile)
  1282. ;; (OTHER-BACKENDS) ; see note in `org-xhtml-get'
  1283. ;; (CONVERT-METHOD) ; see note in `org-xhtml-get'
  1284. (TOPLEVEL-HLEVEL 1)
  1285. (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps)
  1286. (INLINE-IMAGES 'maybe)
  1287. (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
  1288. (PLAIN-TEXT-MAP '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
  1289. (TABLE-FIRST-COLUMN-AS-LABELS nil)
  1290. (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY "," 'superscript))
  1291. (CODING-SYSTEM-FOR-WRITE 'utf-8)
  1292. (CODING-SYSTEM-FOR-SAVE 'utf-8)
  1293. (t (error "Unknown property: %s" what))))
  1294. (defun org-odt-parse-label (label)
  1295. (save-match-data
  1296. (if (not (string-match "\\`[a-zA-Z]+:\\(.+\\)" label))
  1297. (cons label nil)
  1298. (cons
  1299. (capitalize (substring label 0 (1- (match-beginning 1))))
  1300. (substring label (match-beginning 1))))))
  1301. (defvar org-lparse-latex-fragment-fallback) ; set by org-do-lparse
  1302. (defun org-export-odt-preprocess (parameters)
  1303. "Convert LaTeX fragments to images."
  1304. (when (and org-current-export-file
  1305. (plist-get parameters :LaTeX-fragments))
  1306. (org-format-latex
  1307. (concat "ltxpng/" (file-name-sans-extension
  1308. (file-name-nondirectory
  1309. org-current-export-file)))
  1310. org-current-export-dir nil "Creating LaTeX image %s"
  1311. nil nil
  1312. (cond
  1313. ((eq (plist-get parameters :LaTeX-fragments) 'verbatim) 'verbatim)
  1314. ;; Investigate MathToWeb for converting TeX equations to MathML
  1315. ;; See http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01755.html
  1316. ((or (eq (plist-get parameters :LaTeX-fragments) 'mathjax )
  1317. (eq (plist-get parameters :LaTeX-fragments) t ))
  1318. (org-lparse-warn
  1319. (concat
  1320. "Use of MathJax is incompatible with ODT exporter. "
  1321. (format "Using %S instead." org-lparse-latex-fragment-fallback)))
  1322. org-lparse-latex-fragment-fallback)
  1323. ((eq (plist-get parameters :LaTeX-fragments) 'dvipng ) 'dvipng)
  1324. (t nil))))
  1325. (goto-char (point-min))
  1326. (let (label label-components category value pretty-label)
  1327. (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t)
  1328. (org-if-unprotected-at (match-beginning 1)
  1329. (setq label (match-string 1)
  1330. label-components (org-odt-parse-label label)
  1331. category (car label-components)
  1332. value (cdr label-components)
  1333. pretty-label (if value (concat category " " value) label))
  1334. (replace-match
  1335. (let ((org-lparse-encode-pending t))
  1336. (org-odt-format-tags
  1337. '("<text:sequence-ref text:reference-format=\"category-and-value\" text:ref-name=\"%s\">"
  1338. . "</text:sequence-ref>") pretty-label label)) t t)))))
  1339. (declare-function archive-zip-extract "arc-mode.el" (archive name))
  1340. (defun org-odt-zip-extract-one (archive member &optional target)
  1341. (require 'arc-mode)
  1342. (let* ((target (or target default-directory))
  1343. (archive (expand-file-name archive))
  1344. (archive-zip-extract
  1345. (list "unzip" "-qq" "-o" "-d" target))
  1346. exit-code command-output)
  1347. (setq command-output
  1348. (with-temp-buffer
  1349. (setq exit-code (archive-zip-extract archive member))
  1350. (buffer-string)))
  1351. (unless (zerop exit-code)
  1352. (message command-output)
  1353. (error "Extraction failed"))))
  1354. (defun org-odt-zip-extract (archive members &optional target)
  1355. (when (atom members) (setq members (list members)))
  1356. (mapc (lambda (member)
  1357. (org-odt-zip-extract-one archive member target))
  1358. members))
  1359. (defun org-odt-copy-styles-file (&optional styles-file)
  1360. ;; Non-availability of styles.xml is not a critical error. For now
  1361. ;; throw an error purely for aesthetic reasons.
  1362. (setq styles-file (or styles-file
  1363. org-export-odt-styles-file
  1364. (expand-file-name "styles/OrgOdtStyles.xml"
  1365. org-odt-data-dir)
  1366. (error "org-odt: Missing styles file?")))
  1367. (cond
  1368. ((listp styles-file)
  1369. (let ((archive (nth 0 styles-file))
  1370. (members (nth 1 styles-file)))
  1371. (org-odt-zip-extract archive members)
  1372. (mapc
  1373. (lambda (member)
  1374. (when (org-file-image-p member)
  1375. (let* ((image-type (file-name-extension member))
  1376. (media-type (format "image/%s" image-type)))
  1377. (org-odt-update-manifest-file media-type member))))
  1378. members)))
  1379. ((and (stringp styles-file) (file-exists-p styles-file))
  1380. (let ((styles-file-type (file-name-extension styles-file)))
  1381. (cond
  1382. ((string= styles-file-type "xml")
  1383. (copy-file styles-file "styles.xml" t))
  1384. ((member styles-file-type '("odt" "ott"))
  1385. (org-odt-zip-extract styles-file "styles.xml")))))
  1386. (t
  1387. (error (format "Invalid specification of styles.xml file: %S"
  1388. org-export-odt-styles-file)))))
  1389. (defvar org-export-odt-factory-settings
  1390. "d4328fb9d1b6cb211d4320ff546829f26700dc5e"
  1391. "SHA1 hash of OrgOdtStyles.xml.")
  1392. (defun org-odt-configure-outline-numbering (level)
  1393. "Outline numbering is retained only upto LEVEL.
  1394. To disable outline numbering pass a LEVEL of 0."
  1395. (if (not (string= org-export-odt-factory-settings (sha1 (current-buffer))))
  1396. (org-lparse-warn
  1397. "org-odt: Using custom styles file? Consider tweaking styles.xml for better output. To suppress this warning update `org-export-odt-factory-settings'")
  1398. (goto-char (point-min))
  1399. (let ((regex
  1400. "<text:outline-level-style\\(.*\\)text:level=\"\\(.*\\)\"\\(.*\\)>")
  1401. (replacement
  1402. "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
  1403. (while (re-search-forward regex nil t)
  1404. (when (> (string-to-number (match-string 1)) level)
  1405. (replace-match replacement t nil))))
  1406. (save-buffer 0)))
  1407. (provide 'org-odt)
  1408. ;;; org-odt.el ends here