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