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