org-odt.el 66 KB

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