org-odt.el 77 KB

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