org-odt.el 54 KB

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