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