org-odt.el 58 KB

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