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