org-odt: Use lang specific value for "Table Of Contents"
[org-mode.git] / contrib / lisp / org-odt.el
blobea4e32b93c135f2dce54b34f13d7c989e9f28d48
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 (format "
696 </text:table-of-content-source>
698 <text:index-body>
699 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
700 <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
701 </text:index-title>
702 " lang-specific-heading)))
704 (defun org-odt-end-toc ()
705 (insert "
706 </text:index-body>
707 </text:table-of-content>
710 (defun org-odt-format-toc-entry (snumber todo headline tags href)
711 (setq headline (concat
712 (and org-export-with-section-numbers
713 (concat snumber ". "))
714 headline
715 (and tags
716 (concat
717 (org-lparse-format 'SPACES 3)
718 (org-lparse-format 'FONTIFY tags "tag")))))
719 (when todo
720 (setq headline (org-lparse-format 'FONTIFY headline "todo")))
722 (let ((org-odt-suppress-xref t))
723 (org-odt-format-link headline (concat "#" href))))
725 (defun org-odt-format-toc-item (toc-entry level org-last-level)
726 (let ((style (format "Contents_20_%d"
727 (+ level (or (org-lparse-get 'TOPLEVEL-HLEVEL) 1) -1))))
728 (insert "\n" (org-odt-format-stylized-paragraph style toc-entry) "\n")))
730 ;; Following variable is let bound during 'ORG-LINK callback. See
731 ;; org-html.el
732 (defvar org-lparse-link-description-is-image nil)
733 (defun org-odt-format-link (desc href &optional attr)
734 (cond
735 ((and (= (string-to-char href) ?#) (not org-odt-suppress-xref))
736 (setq href (concat org-export-odt-bookmark-prefix (substring href 1)))
737 (org-odt-format-tags
738 '("<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"%s\">" .
739 "</text:bookmark-ref>")
740 desc href))
741 (org-lparse-link-description-is-image
742 (org-odt-format-tags
743 '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</draw:a>")
744 desc href (or attr "")))
746 (org-odt-format-tags
747 '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</text:a>")
748 desc href (or attr "")))))
750 (defun org-odt-format-spaces (n)
751 (org-odt-format-tags "<text:s text:c=\"%d\"/>" "" n))
753 (defun org-odt-format-tabs (&optional n)
754 (let ((tab "<text:tab/>")
755 (n (or n 1)))
756 (insert tab)))
758 (defun org-odt-format-line-break ()
759 (org-odt-format-tags "<text:line-break/>" ""))
761 (defun org-odt-format-horizontal-line ()
762 (org-odt-format-stylized-paragraph 'horizontal-line ""))
764 (defun org-odt-format-line (line)
765 (case org-lparse-dyn-current-environment
766 (fixedwidth (concat (org-odt-format-source-code-or-example-line
767 (org-xml-encode-plain-text line)) "\n"))
768 (t (concat line "\n"))))
770 (defun org-odt-format-comment (fmt &rest args)
771 (let ((comment (apply 'format fmt args)))
772 (format "\n<!-- %s -->\n" comment)))
774 (defun org-odt-format-org-entity (wd)
775 (org-entity-get-representation wd 'utf8))
777 (defun org-odt-fill-tabs-and-spaces (line)
778 (replace-regexp-in-string
779 "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
780 (cond
781 ((string= s "\t") (org-odt-format-tabs))
782 ((> (length s) 1)
783 (org-odt-format-spaces (length s)))
784 (t " "))) line))
786 (defun org-odt-format-source-code-or-example-line (line)
787 (org-odt-format-stylized-paragraph 'src (org-odt-fill-tabs-and-spaces line)))
789 (defun org-odt-format-example (lines)
790 (mapconcat
791 (lambda (line)
792 (org-odt-format-source-code-or-example-line line))
793 (org-split-string lines "[\r\n]") "\n"))
795 (defun org-odt-format-source-code-or-example (lines lang caption textareap
796 cols rows num cont
797 rpllbl fmt)
798 (org-odt-format-example (org-export-number-lines
799 (org-xml-encode-plain-text-lines lines)
800 0 0 num cont rpllbl fmt)))
802 (defun org-xml-encode-plain-text-lines (rtn)
803 (mapconcat 'org-xml-encode-plain-text (org-split-string rtn "[\r\n]") "\n"))
805 (defun org-odt-remap-stylenames (style-name)
807 (cdr (assoc style-name '(("timestamp-wrapper" . "OrgTimestampWrapper")
808 ("timestamp" . "OrgTimestamp")
809 ("timestamp-kwd" . "OrgTimestampKeyword")
810 ("tag" . "OrgTag")
811 ("todo" . "OrgTodo")
812 ("done" . "OrgDone")
813 ("target" . "OrgTarget"))))
814 style-name))
816 (defun org-odt-format-fontify (text style &optional id)
817 (let* ((style-name
818 (cond
819 ((stringp style)
820 (org-odt-remap-stylenames style))
821 ((symbolp style)
822 (org-odt-get-style-name-for-entity 'character style))
823 ((listp style)
824 (assert (< 1 (length style)))
825 (let ((parent-style (pop style)))
826 (mapconcat (lambda (s)
827 ;; (assert (stringp s) t)
828 (org-odt-remap-stylenames s)) style "")
829 (org-odt-remap-stylenames parent-style)))
830 (t (error "Don't how to handle style %s" style)))))
831 (org-odt-format-tags
832 '("<text:span text:style-name=\"%s\">" . "</text:span>")
833 text style-name)))
835 (defun org-odt-relocate-relative-path (path dir)
836 (if (file-name-absolute-p path) path
837 (file-relative-name (expand-file-name path dir)
838 (expand-file-name "eyecandy" dir))))
840 (defun org-odt-format-inline-image (thefile)
841 (let* ((thelink (if (file-name-absolute-p thefile) thefile
842 (org-xml-format-href
843 (org-odt-relocate-relative-path
844 thefile org-current-export-file))))
845 (href
846 (org-odt-format-tags
847 "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
848 (if org-export-odt-embed-images
849 (org-odt-copy-image-file thefile) thelink))))
850 (org-export-odt-format-image thefile href)))
852 (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
853 descp)
854 "Make an HTML link.
855 OPT-PLIST is an options list.
856 TYPE is the device-type of the link (THIS://foo.html)
857 PATH is the path of the link (http://THIS#locationx)
858 FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
859 DESC is the link description, if any.
860 ATTR is a string of other attributes of the a element.
861 MAY-INLINE-P allows inlining it as an image."
863 (declare (special org-lparse-par-open))
864 (save-match-data
865 (let* ((may-inline-p
866 (and (member type-1 '("http" "https" "file"))
867 (org-lparse-should-inline-p path descp)
868 (not fragment)))
869 (type (if (equal type-1 "id") "file" type-1))
870 (filename path)
871 (thefile path))
873 (cond
874 ;; check for inlined images
875 ((and (member type '("file"))
876 (not fragment)
877 (org-file-image-p
878 filename org-odt-export-inline-image-extensions)
879 (or (eq t org-odt-export-inline-images)
880 (and org-odt-export-inline-images (not descp))))
882 ;; (when (and (string= type "file") (file-name-absolute-p path))
883 ;; (setq thefile (concat "file://" (expand-file-name path))))
884 ;; (setq thefile (org-xml-format-href thefile))
885 ;; (org-export-html-format-image thefile)
886 (org-odt-format-inline-image thefile))
888 (when (string= type "file")
889 (setq thefile
890 (cond
891 ((file-name-absolute-p path)
892 (concat "file://" (expand-file-name path)))
893 (t (org-odt-relocate-relative-path
894 thefile org-current-export-file)))))
896 (when (and (member type '("" "http" "https" "file" "coderef"))
897 fragment)
898 (setq thefile (concat thefile "#" fragment)))
900 (setq thefile (org-xml-format-href thefile))
902 (when (not (member type '("" "file" "coderef")))
903 (setq thefile (concat type ":" thefile)))
905 (let ((org-odt-suppress-xref (string= type "coderef")))
906 (org-odt-format-link
907 (org-xml-format-desc desc) thefile attr)))))))
909 (defun org-odt-format-heading (text level &optional id)
910 (let* ((text (if id (org-odt-format-target text id) text)))
911 (org-odt-format-tags
912 '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
913 "</text:h>") text level level)))
915 (defun org-odt-format-headline (title extra-targets tags
916 &optional snumber level)
917 (concat
918 (org-lparse-format 'EXTRA-TARGETS extra-targets)
920 ;; No need to generate section numbers. They are auto-generated by
921 ;; the application
923 ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
924 title
925 (and tags (concat (org-lparse-format 'SPACES 3)
926 (org-lparse-format 'ORG-TAGS tags)))))
928 (defun org-odt-format-anchor (text name &optional class)
929 (org-odt-format-target text name))
931 (defun org-odt-format-bookmark (text id)
932 (if id
933 (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id)
934 text))
936 (defun org-odt-format-target (text id)
937 (let ((name (concat org-export-odt-bookmark-prefix id)))
938 (concat
939 (and id (org-odt-format-tags
940 "<text:bookmark-start text:name=\"%s\"/>" "" name))
941 (org-odt-format-bookmark text id)
942 (and id (org-odt-format-tags
943 "<text:bookmark-end text:name=\"%s\"/>" "" name)))))
945 (defun org-odt-format-footnote (n def)
946 (let ((id (concat "fn" n))
947 (note-class "footnote")
948 (par-style "Footnote"))
949 (org-odt-format-tags
950 '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
951 "</text:note>")
952 (concat
953 (org-odt-format-tags
954 '("<text:note-citation>" . "</text:note-citation>")
956 (org-odt-format-tags
957 '("<text:note-body>" . "</text:note-body>")
958 def))
959 id note-class)))
961 (defun org-odt-format-footnote-reference (n def refcnt)
962 (if (= refcnt 1)
963 (org-odt-format-footnote n def)
964 (org-odt-format-footnote-ref n)))
966 (defun org-odt-format-footnote-ref (n)
967 (let ((note-class "footnote")
968 (ref-format "text")
969 (ref-name (concat "fn" n)))
970 (org-odt-format-tags
971 '("<text:span text:style-name=\"%s\">" . "</text:span>")
972 (org-odt-format-tags
973 '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" . "</text:note-ref>")
974 n note-class ref-format ref-name)
975 "OrgSuperscript")))
977 (defun org-odt-get-image-name (file-name)
978 (require 'sha1)
979 (file-relative-name
980 (expand-file-name
981 (concat (sha1 file-name) "." (file-name-extension file-name)) "Pictures")))
983 (defun org-export-odt-format-image (src href
984 ;; par-open
986 "Create image tag with source and attributes."
987 (save-match-data
989 (let (embed-as caption attr label attr-plist size width height)
991 (cond
992 ((string-match "^ltxpng/" src)
993 ;; FIXME: Anyway the latex src can be embedded as an
994 ;; annotation
996 ;; (org-find-text-property-in-string 'org-latex-src src)
997 (setq caption nil attr nil label nil embed-as 'character))
1000 (setq caption (org-find-text-property-in-string 'org-caption src)
1001 caption (and caption (org-xml-format-desc caption))
1002 attr (org-find-text-property-in-string 'org-attributes src)
1003 label (org-find-text-property-in-string 'org-label src)
1004 embed-as 'paragraph)))
1006 (setq attr-plist (when attr (read attr)))
1007 (setq size (org-odt-image-size-from-file
1008 src (plist-get attr-plist :width)
1009 (plist-get attr-plist :height)
1010 (plist-get attr-plist :scale) nil embed-as))
1012 (org-export-odt-do-format-image embed-as caption attr label
1013 size href))))
1015 (defun org-export-odt-do-format-image (embed-as caption attr label
1016 size href)
1017 "Create image tag with source and attributes."
1018 (save-match-data
1019 (let ((width (car size)) (height (cdr size))
1020 (draw-frame-pair
1021 '("<draw:frame draw:style-name=\"%s\"
1022 text:anchor-type=\"%s\"
1023 draw:z-index=\"%d\" %s>" . "</draw:frame>")))
1024 (cond
1025 ((and (not caption) (not label))
1026 (let (style-name anchor-type)
1027 (cond
1028 ((eq embed-as 'paragraph)
1029 (setq style-name "OrgGraphicsParagraph" anchor-type "paragraph"))
1030 ((eq embed-as 'character)
1031 (setq style-name "OrgGraphicsBaseline" anchor-type "as-char")))
1032 (org-odt-format-tags
1033 draw-frame-pair href style-name anchor-type 0
1034 (org-odt-image-attrs-from-size width height))))
1037 (concat
1038 ;; (when par-open (org-odt-close-par))
1039 (org-odt-format-tags
1040 draw-frame-pair
1041 (org-odt-format-tags
1042 '("<draw:text-box fo:min-height=\"%dcm\">" . "</draw:text-box>")
1043 (org-odt-format-stylized-paragraph
1044 'illustration
1045 (concat
1046 (let ((extra " style:rel-width=\"100%\" style:rel-height=\"scale\""))
1047 (org-odt-format-tags
1048 draw-frame-pair href "OrgGraphicsParagraphContent" "paragraph" 2
1049 (concat (org-odt-image-attrs-from-size width height) extra)))
1050 (org-odt-format-entity-caption label caption)))
1051 height)
1052 "OrgFrame" "paragraph" 1
1053 (org-odt-image-attrs-from-size width))
1055 ;; (when par-open (org-odt-open-par))
1056 ))))))
1058 ;; xml files generated on-the-fly
1059 (defconst org-export-odt-save-list
1060 '("META-INF/manifest.xml" "content.xml" "meta.xml" "styles.xml"))
1062 ;; xml files that are copied
1063 (defconst org-export-odt-nosave-list '())
1065 ;; xml files that contribute to the final odt file
1066 (defvar org-export-odt-file-list nil)
1068 (defconst org-export-odt-manifest-lines
1069 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1070 "<manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">"
1071 "<manifest:file-entry manifest:media-type=\"application/vnd.oasis.opendocument.text\" manifest:version=\"1.2\" manifest:full-path=\"/\"/>"
1072 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"content.xml\"/>"
1073 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"styles.xml\"/>"
1074 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"meta.xml\"/>"
1075 "<manifest:file-entry manifest:media-type=\"\" manifest:full-path=\"Pictures/\"/>") . ("</manifest:manifest>")))
1077 (defconst org-export-odt-meta-lines
1078 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1079 "<office:document-meta"
1080 " xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\""
1081 " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
1082 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\""
1083 " xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\""
1084 " xmlns:ooo=\"http://openoffice.org/2004/office\" "
1085 " office:version=\"1.2\">"
1086 " <office:meta>") . (" </office:meta>" "</office:document-meta>")))
1088 (defun org-odt-copy-image-file (path &optional target-file)
1089 "Returns the internal name of the file"
1090 (let* ((image-type (file-name-extension path))
1091 (media-type (format "image/%s" image-type))
1092 (src-file (expand-file-name
1093 path (file-name-directory org-current-export-file)))
1094 (target-file (or target-file (org-odt-get-image-name src-file)))
1095 ;; FIXME
1096 (body-only nil))
1098 (when (not org-lparse-to-buffer)
1099 (message "Embedding %s as %s ..."
1100 (substring-no-properties path) target-file)
1101 (copy-file src-file target-file 'overwrite)
1102 (org-odt-update-manifest-file media-type target-file)
1103 (push target-file org-export-odt-file-list)) target-file))
1105 (defun org-odt-image-attrs-from-size (&optional width height)
1106 (concat
1107 (when width (format "svg:width=\"%0.2fcm\"" width))
1109 (when height (format "svg:height=\"%0.2fcm\"" height))))
1111 (defvar org-export-odt-image-size-probe-method
1112 '(emacs imagemagick force)
1113 "Ordered list of methods by for determining size of an embedded
1114 image.")
1116 (defvar org-export-odt-default-image-sizes-alist
1117 '(("character" . (5 . 0.4))
1118 ("paragraph" . (5 . 5)))
1119 "Hardcoded image dimensions one for each of the anchor
1120 methods.")
1122 (defun org-odt-do-image-size (probe-method file &optional dpi anchor-type)
1123 (setq dpi (or dpi org-export-odt-pixels-per-inch))
1124 (setq anchor-type (or anchor-type "paragraph"))
1125 (flet ((size-in-cms (size-in-pixels)
1126 (flet ((pixels-to-cms (pixels)
1127 (let* ((cms-per-inch 2.54)
1128 (inches (/ pixels dpi)))
1129 (* cms-per-inch inches))))
1130 (and size-in-pixels
1131 (cons (pixels-to-cms (car size-in-pixels))
1132 (pixels-to-cms (cdr size-in-pixels)))))))
1133 (case probe-method
1134 (emacs
1135 (size-in-cms (ignore-errors (image-size (create-image file) 'pixels))))
1136 (imagemagick
1137 (size-in-cms
1138 (let ((dim (shell-command-to-string
1139 (format "identify -format \"%%w:%%h\" \"%s\"" file))))
1140 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
1141 (cons (string-to-number (match-string 1 dim))
1142 (string-to-number (match-string 2 dim)))))))
1144 (cdr (assoc-string anchor-type
1145 org-export-odt-default-image-sizes-alist))))))
1147 (defun org-odt-image-size-from-file (file &optional user-width
1148 user-height scale dpi embed-as)
1149 (unless (file-name-absolute-p file)
1150 (setq file (expand-file-name
1151 file (file-name-directory org-current-export-file))))
1152 (let* (size width height)
1153 (unless (and user-height user-width)
1154 (loop for probe-method in org-export-odt-image-size-probe-method
1155 until size
1156 do (setq size (org-odt-do-image-size
1157 probe-method file dpi embed-as)))
1158 (or size (error "Cannot determine Image size. Aborting ..."))
1159 (setq width (car size) height (cdr size)))
1160 (cond
1161 (scale
1162 (setq width (* width scale) height (* height scale)))
1163 ((and user-height user-width)
1164 (setq width user-width height user-height))
1165 (user-height
1166 (setq width (* user-height (/ width height)) height user-height))
1167 (user-width
1168 (setq height (* user-width (/ height width)) width user-width))
1169 (t (ignore)))
1170 (cons width height)))
1172 (defvar org-odt-default-entity "Illustration")
1173 (defun org-odt-format-entity-caption (label caption &optional default-entity)
1174 (if (not label) (or caption "")
1175 (let* ((label-components (org-odt-parse-label label))
1176 (entity (car label-components))
1177 (seqno (cdr label-components))
1178 (caption (and caption (concat ": " caption))))
1179 (unless seqno
1180 (setq seqno label
1181 entity (or default-entity org-odt-default-entity)))
1182 (concat
1183 entity " "
1184 (org-odt-format-tags
1185 '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" . "</text:sequence>")
1186 seqno label entity entity)
1187 caption))))
1189 (defun org-odt-format-tags (tag text &rest args)
1190 (let ((prefix (when org-lparse-encode-pending "@"))
1191 (suffix (when org-lparse-encode-pending "@")))
1192 (apply 'org-lparse-format-tags tag text prefix suffix args)))
1194 (defun org-odt-init-outfile (filename)
1195 (let* ((outdir (make-temp-file org-export-odt-tmpdir-prefix t))
1196 (mimetype-file (expand-file-name "mimetype" outdir))
1197 (content-file (expand-file-name "content.xml" outdir))
1198 (manifest-file (expand-file-name "META-INF/manifest.xml" outdir))
1199 (meta-file (expand-file-name "meta.xml" outdir))
1200 (styles-file (expand-file-name "styles.xml" outdir))
1201 (pictures-dir (expand-file-name "Pictures" outdir))
1202 (body-only nil))
1204 ;; content file
1205 (with-current-buffer (find-file-noselect content-file t)
1206 (erase-buffer))
1208 ;; FIXME: How to factor in body-only here
1209 (unless body-only
1210 ;; manifest file
1211 (make-directory (file-name-directory manifest-file))
1212 (with-current-buffer (find-file-noselect manifest-file t)
1213 (erase-buffer)
1214 (insert (mapconcat 'identity (car org-export-odt-manifest-lines) "\n"))
1215 (insert "\n")
1216 (save-excursion
1217 (insert (mapconcat 'identity (cdr org-export-odt-manifest-lines) "\n"))))
1219 ;; meta file
1220 (with-current-buffer (find-file-noselect meta-file t)
1221 (erase-buffer)
1222 (insert (mapconcat 'identity (car org-export-odt-meta-lines) "\n"))
1223 (insert "\n")
1224 (save-excursion
1225 (insert (mapconcat 'identity (cdr org-export-odt-meta-lines) "\n"))))
1227 ;; styles file
1228 ;; (copy-file org-export-odt-styles-file styles-file t)
1230 ;; Pictures dir
1231 (make-directory pictures-dir)
1233 ;; initialize list of files that contribute to the odt file
1234 (setq org-export-odt-file-list
1235 (append org-export-odt-save-list org-export-odt-nosave-list)))
1236 content-file))
1238 (defconst org-export-odt-mimetype-lines
1239 '("application/vnd.oasis.opendocument.text"))
1241 (defconst org-odt-manifest-file-entry-tag
1242 "<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"/>")
1244 (defun org-odt-save-as-outfile (target opt-plist)
1245 ;; write meta file
1246 (org-odt-update-meta-file opt-plist)
1248 ;; write styles file
1249 (org-odt-copy-styles-file)
1251 ;; Update styles.xml - take care of outline numbering
1252 (with-current-buffer
1253 (find-file-noselect (expand-file-name "styles.xml") t)
1254 ;; Don't make automatic backup of styles.xml file. This setting
1255 ;; prevents the backedup styles.xml file from being zipped in to
1256 ;; odt file. This is more of a hackish fix. Better alternative
1257 ;; would be to fix the zip command so that the output odt file
1258 ;; includes only the needed files and excludes any auto-generated
1259 ;; extra files like backups and auto-saves etc etc. Note that
1260 ;; currently the zip command zips up the entire temp directory so
1261 ;; that any auto-generated files created under the hood ends up in
1262 ;; the resulting odt file.
1263 (set (make-local-variable 'backup-inhibited) t)
1265 ;; Import local setting of `org-export-with-section-numbers'
1266 (org-lparse-bind-local-variables opt-plist)
1267 (org-odt-configure-outline-numbering
1268 (if org-export-with-section-numbers org-export-headline-levels 0)))
1270 (let ((zipdir default-directory))
1271 (message "Switching to directory %s" (expand-file-name zipdir))
1273 ;; save all xml files
1274 (mapc (lambda (file)
1275 (with-current-buffer
1276 (find-file-noselect (expand-file-name file) t)
1277 ;; prettify output
1278 (indent-region (point-min) (point-max))
1279 (save-buffer)))
1280 org-export-odt-save-list)
1282 (let* ((target-name (file-name-nondirectory target))
1283 (target-dir (file-name-directory target))
1284 (cmd (format "zip -rmTq %s %s" target-name ".")))
1285 (when (file-exists-p target)
1286 ;; FIXME: If the file is locked this throws a cryptic error
1287 (delete-file target))
1289 (let ((coding-system-for-write 'no-conversion) exitcode)
1290 (message "Creating odt file using \"%s\"" cmd)
1291 (setq exitcode
1292 (apply 'call-process
1293 "zip"
1297 (append (list "-rmTq") (list target-name "."))))
1299 (or (zerop exitcode)
1300 (error "Unable to create odt file (%S)" exitcode)))
1302 ;; move the file from outdir to target-dir
1303 (rename-file target-name target-dir)
1305 ;; kill all xml buffers
1306 (mapc (lambda (file)
1307 (kill-buffer
1308 (find-file-noselect (expand-file-name file zipdir) t)))
1309 org-export-odt-save-list)
1311 (delete-directory zipdir)))
1313 (message "Created %s" target)
1314 (set-buffer (find-file-noselect target t)))
1316 (defun org-odt-update-meta-file (opt-plist)
1317 (with-current-buffer
1318 (find-file-noselect (expand-file-name "meta.xml") t)
1319 (let ((date (or (plist-get opt-plist :effective-date) ""))
1320 (author (or (plist-get opt-plist :author) ""))
1321 (email (plist-get opt-plist :email))
1322 (keywords (plist-get opt-plist :keywords))
1323 (description (plist-get opt-plist :description))
1324 (title (plist-get opt-plist :title)))
1326 (insert
1327 "\n"
1328 (org-odt-format-tags '("<dc:creator>" . "</dc:creator>") author)
1329 (org-odt-format-tags
1330 '("\n<meta:initial-creator>" . "</meta:initial-creator>") author)
1331 (org-odt-format-tags '("\n<dc:date>" . "</dc:date>") date)
1332 (org-odt-format-tags
1333 '("\n<meta:creation-date>" . "</meta:creation-date>") date)
1334 (org-odt-format-tags '("\n<meta:generator>" . "</meta:generator>")
1335 (when org-export-creator-info
1336 (format "Org-%s/Emacs-%s"
1337 org-version emacs-version)))
1338 (org-odt-format-tags '("\n<meta:keyword>" . "</meta:keyword>") keywords)
1339 (org-odt-format-tags '("\n<dc:subject>" . "</dc:subject>") description)
1340 (org-odt-format-tags '("\n<dc:title>" . "</dc:title>") title)
1341 "\n"))))
1343 (defun org-odt-update-manifest-file (media-type full-path)
1344 (with-current-buffer
1345 (find-file-noselect (expand-file-name "META-INF/manifest.xml") t)
1346 (insert (format org-odt-manifest-file-entry-tag media-type full-path))))
1348 (defun org-odt-finalize-outfile ()
1349 (message "org-newodt: Finalizing outfile")
1350 (org-odt-delete-empty-paragraphs))
1352 (defun org-odt-delete-empty-paragraphs ()
1353 (goto-char (point-min))
1354 (let ((open "<text:p[^>]*>")
1355 (close "</text:p>"))
1356 (while (re-search-forward (format "%s[ \r\n\t]*%s" open close) nil t)
1357 (replace-match ""))))
1359 (defun org-odt-get (what &optional opt-plist)
1360 (case what
1361 (BACKEND 'odt)
1362 (EXPORT-DIR (org-export-directory :html opt-plist))
1363 (FILE-NAME-EXTENSION "odt")
1364 (EXPORT-BUFFER-NAME "*Org ODT Export*")
1365 (ENTITY-CONTROL org-odt-entity-control-callbacks-alist)
1366 (ENTITY-FORMAT org-odt-entity-format-callbacks-alist)
1367 (INIT-METHOD 'org-odt-init-outfile)
1368 (FINAL-METHOD 'org-odt-finalize-outfile)
1369 (SAVE-METHOD 'org-odt-save-as-outfile)
1370 (OTHER-BACKENDS
1371 '("bib" "doc" "doc6" "doc95" "html" "xhtml" "latex" "odt" "ott" "pdf" "rtf"
1372 "sdw" "sdw3" "sdw4" "stw " "sxw" "mediawiki" "text" "txt" "uot" "vor"
1373 "vor3" "vor4" "docbook" "ooxml" "ppt" "odp"))
1374 (CONVERT-METHOD org-lparse-convert-process)
1375 (TOPLEVEL-HLEVEL 1)
1376 (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps)
1377 (INLINE-IMAGES 'maybe)
1378 (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
1379 (PLAIN-TEXT-MAP '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
1380 (TABLE-FIRST-COLUMN-AS-LABELS nil)
1381 (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY "," 'superscript))
1382 (CODING-SYSTEM-FOR-WRITE 'utf-8)
1383 (CODING-SYSTEM-FOR-SAVE 'utf-8)
1384 (t (error "Unknown property: %s" what))))
1386 (defun org-odt-parse-label (label)
1387 (save-match-data
1388 (if (not (string-match "\\`[a-zA-Z]+:\\(.+\\)" label))
1389 (cons label nil)
1390 (cons
1391 (capitalize (substring label 0 (1- (match-beginning 1))))
1392 (substring label (match-beginning 1))))))
1394 (defvar org-lparse-latex-fragment-fallback) ; set by org-do-lparse
1395 (defun org-export-odt-preprocess (parameters)
1396 "Convert LaTeX fragments to images."
1397 (when (and org-current-export-file
1398 (plist-get parameters :LaTeX-fragments))
1399 (org-format-latex
1400 (concat "ltxpng/" (file-name-sans-extension
1401 (file-name-nondirectory
1402 org-current-export-file)))
1403 org-current-export-dir nil "Creating LaTeX image %s"
1404 nil nil
1405 (cond
1406 ((eq (plist-get parameters :LaTeX-fragments) 'verbatim) 'verbatim)
1407 ;; Investigate MathToWeb for converting TeX equations to MathML
1408 ;; See http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01755.html
1409 ((or (eq (plist-get parameters :LaTeX-fragments) 'mathjax )
1410 (eq (plist-get parameters :LaTeX-fragments) t ))
1411 (org-lparse-warn
1412 (concat
1413 "Use of MathJax is incompatible with ODT exporter. "
1414 (format "Using %S instead." org-lparse-latex-fragment-fallback)))
1415 org-lparse-latex-fragment-fallback)
1416 ((eq (plist-get parameters :LaTeX-fragments) 'dvipng ) 'dvipng)
1417 (t nil))))
1418 (goto-char (point-min))
1419 (let (label label-components category value pretty-label)
1420 (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t)
1421 (org-if-unprotected-at (match-beginning 1)
1422 (setq label (match-string 1)
1423 label-components (org-odt-parse-label label)
1424 category (car label-components)
1425 value (cdr label-components)
1426 pretty-label (if value (concat category " " value) label))
1427 (replace-match
1428 (let ((org-lparse-encode-pending t))
1429 (org-odt-format-tags
1430 '("<text:sequence-ref text:reference-format=\"category-and-value\" text:ref-name=\"%s\">"
1431 . "</text:sequence-ref>") pretty-label label)) t t)))))
1433 (declare-function archive-zip-extract "arc-mode.el" (archive name))
1434 (defun org-odt-zip-extract-one (archive member &optional target)
1435 (require 'arc-mode)
1436 (let* ((target (or target default-directory))
1437 (archive (expand-file-name archive))
1438 (archive-zip-extract
1439 (list "unzip" "-qq" "-o" "-d" target))
1440 exit-code command-output)
1441 (setq command-output
1442 (with-temp-buffer
1443 (setq exit-code (archive-zip-extract archive member))
1444 (buffer-string)))
1445 (unless (zerop exit-code)
1446 (message command-output)
1447 (error "Extraction failed"))))
1449 (defun org-odt-zip-extract (archive members &optional target)
1450 (when (atom members) (setq members (list members)))
1451 (mapc (lambda (member)
1452 (org-odt-zip-extract-one archive member target))
1453 members))
1455 (defun org-odt-copy-styles-file (&optional styles-file)
1456 ;; Non-availability of styles.xml is not a critical error. For now
1457 ;; throw an error purely for aesthetic reasons.
1458 (setq styles-file (or styles-file
1459 org-export-odt-styles-file
1460 (expand-file-name "styles/OrgOdtStyles.xml"
1461 org-odt-data-dir)
1462 (error "org-odt: Missing styles file?")))
1463 (cond
1464 ((listp styles-file)
1465 (let ((archive (nth 0 styles-file))
1466 (members (nth 1 styles-file)))
1467 (org-odt-zip-extract archive members)
1468 (mapc
1469 (lambda (member)
1470 (when (org-file-image-p member)
1471 (let* ((image-type (file-name-extension member))
1472 (media-type (format "image/%s" image-type)))
1473 (org-odt-update-manifest-file media-type member))))
1474 members)))
1475 ((and (stringp styles-file) (file-exists-p styles-file))
1476 (let ((styles-file-type (file-name-extension styles-file)))
1477 (cond
1478 ((string= styles-file-type "xml")
1479 (copy-file styles-file "styles.xml" t))
1480 ((member styles-file-type '("odt" "ott"))
1481 (org-odt-zip-extract styles-file "styles.xml")))))
1483 (error (format "Invalid specification of styles.xml file: %S"
1484 org-export-odt-styles-file)))))
1486 (defvar org-export-odt-factory-settings
1487 "d4328fb9d1b6cb211d4320ff546829f26700dc5e"
1488 "SHA1 hash of OrgOdtStyles.xml.")
1490 (defun org-odt-configure-outline-numbering (level)
1491 "Outline numbering is retained only upto LEVEL.
1492 To disable outline numbering pass a LEVEL of 0."
1493 (if (not (string= org-export-odt-factory-settings (sha1 (current-buffer))))
1494 (org-lparse-warn
1495 "org-odt: Using custom styles file? Consider tweaking styles.xml for better output. To suppress this warning update `org-export-odt-factory-settings'")
1496 (goto-char (point-min))
1497 (let ((regex
1498 "<text:outline-level-style\\(.*\\)text:level=\"\\(.*\\)\"\\(.*\\)>")
1499 (replacement
1500 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1501 (while (re-search-forward regex nil t)
1502 (when (> (string-to-number (match-string 1)) level)
1503 (replace-match replacement t nil))))
1504 (save-buffer 0)))
1506 (provide 'org-odt)
1508 ;;; org-odt.el ends here