test.org: Use verbatim LaTeX fragments. Added noexport for WIP items.
[org-mode/org-jambu.git] / lisp / org-odt.el
blobbd0b1e56f30d7f4f4253d1bd2884a9213224ec12
1 ;;; org-odt.el --- OpenDocumentText export for Org-mode
3 ;; Copyright (C) 2010-2011
4 ;; Free Software Foundation, Inc.
6 ;; Author: Jambunathan K <kjambunathan at gmail dot com>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
9 ;; Version: 7.4
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
29 ;;; Code:
31 ;;;_ preamble
32 (eval-when-compile (require 'cl))
33 (require 'org-html)
35 ;;;_. maybe
37 (defun org-odt-end-export ()
38 ;; remove empty paragraphs
39 (goto-char (point-min))
40 (while (re-search-forward
41 "<text:p\\( text:style-name=\"Text_20_body\"\\)?>[ \r\n\t]*</text:p>"
42 nil t)
43 (replace-match ""))
44 (goto-char (point-min))
46 ;; Convert whitespace place holders
47 (goto-char (point-min))
48 (let (beg end n)
49 (while (setq beg (next-single-property-change (point) 'org-whitespace))
50 (setq n (get-text-property beg 'org-whitespace)
51 end (next-single-property-change beg 'org-whitespace))
52 (goto-char beg)
53 (delete-region beg end)
54 (insert (format "<span style=\"visibility:hidden;\">%s</span>"
55 (make-string n ?x)))))
57 ;; Remove empty lines at the beginning of the file.
58 (goto-char (point-min))
59 (when (looking-at "\\s-+\n") (replace-match ""))
61 ;; Remove display properties
62 (remove-text-properties (point-min) (point-max) '(display t)))
64 ;; Refer comments in org-newhtml.el
65 (defvar org-basehtml-special-paragraph-styles)
67 (defvar org-odt-suppress-xref nil)
69 ;;;_ hacks (to be documented)
71 (defconst org-export-odt-special-string-regexps
72 '(("\\\\-" . "&#x00ad;\\1")
73 ("---\\([^-]\\)" . "&#x2014;\\1")
74 ("--\\([^-]\\)" . "&#x2013;\\1")
75 ("\\.\\.\\." . "&#x2026;"))
76 "Regular expressions for special string conversion.")
79 ;;;_ user-specific
80 ;;;_. custom settings
81 (defcustom org-export-odt-automatic-styles-file nil
82 "Default style file for use with ODT exporter."
83 :group 'org-export-odt
84 :type 'file)
86 ;; TODO: Make configuration user-friendly.
87 (defcustom org-export-odt-styles-file nil
88 "Default style file for use with ODT exporter.
89 Valid values are path to an styles.xml file or a path to a valid
90 *.odt or a *.ott file or a list of the form (FILE (MEMBER1
91 MEMBER2 ...)). In the last case, the specified FILE is unzipped
92 and MEMBER1, MEMBER2 etc are copied in to the generated odt
93 file. The last form is particularly useful if the styles.xml has
94 reference to additional files like header and footer images.
96 :group 'org-export-odt)
97 (defconst org-export-odt-tmpdir-prefix "odt-")
98 (defconst org-export-odt-bookmark-prefix ".OrgXref.")
99 (defcustom org-export-odt-use-bookmarks-for-internal-links t
100 "Export Internal links as bookmarks?."
101 :group 'org-export-odt
102 :type 'boolean)
104 (defcustom org-export-odt-embed-images t
105 "Should the images be copied in to the odt file or just linked?"
106 :group 'org-export-odt
107 :type 'boolean)
109 (defcustom org-odt-export-inline-images 'maybe
110 "Non-nil means inline images into exported HTML pages.
111 This is done using an <img> tag. When nil, an anchor with href is used to
112 link to the image. If this option is `maybe', then images in links with
113 an empty description will be inlined, while images with a description will
114 be linked only."
115 :group 'org-odt-export
116 :type '(choice (const :tag "Never" nil)
117 (const :tag "Always" t)
118 (const :tag "When there is no description" maybe)))
121 (defcustom org-odt-export-inline-image-extensions
122 '("png" "jpeg" "jpg" "gif")
123 "Extensions of image files that can be inlined into HTML."
124 :group 'org-odt-export
125 :type '(repeat (string :tag "Extension")))
128 (defcustom org-export-odt-pixels-per-inch display-pixels-per-inch
130 :group 'org-export-odt
131 :type 'float)
135 ;;;_ , potential custom settings
136 (defvar org-export-odt-default-org-styles-alist
137 '((paragraph . ((default . "Text_20_body")
138 (fixedwidth . "OrgSourceBlock")
139 (verse . "OrgVerse")
140 (quote . "Quotations")
141 (blockquote . "Quotations")
142 (center . "OrgCenter")
143 (left . "OrgLeft")
144 (right . "OrgRight")
145 (title . "Heading_20_1.title")
146 (footnote . "Footnote")
147 (src . "OrgSourceBlock")
148 (illustration . "Illustration")
149 (table . "Table")
150 (definition-term . "Text_20_body_20_bold")
151 (horizontal-line . "Horizontal_20_Line")))
152 (character . ((bold . "Bold")
153 (emphasis . "Emphasis")
154 (code . "OrgCode")
155 (verbatim . "OrgCode")
156 (strike . "Strikethrough")
157 (underline . "Underline")
158 (subscript . "OrgSubscript")
159 (superscript . "OrgSuperscript")))
160 (list . ((ordered . "OrgNumberedList")
161 (unordered . "OrgBulletedList")
162 (description . "OrgDescriptionList"))))
163 "Default styles for various entities.")
165 (defvar org-export-odt-org-styles-alist org-export-odt-default-org-styles-alist)
166 (defun org-odt-get-style-name-for-entity (category &optional entity)
167 (let ((entity (or entity 'default)))
169 (cdr (assoc entity (cdr (assoc category
170 org-export-odt-org-styles-alist))))
171 (cdr (assoc entity (cdr (assoc category
172 org-export-odt-default-org-styles-alist))))
173 (error "Cannot determine style name for entity %s of type %s"
174 entity category))))
177 ;;;_. interactive commands
179 ;;;###autoload
180 (defun org-export-as-odt-and-open (arg)
181 "Export the outline as ODT and immediately open it with a browser.
182 If there is an active region, export only the region.
183 The prefix ARG specifies how many levels of the outline should become
184 headlines. The default is 3. Lower levels will become bulleted lists."
185 (interactive "P")
186 (org-export-as-odt arg 'hidden)
187 (org-open-file buffer-file-name)
188 (when org-export-kill-product-buffer-when-displayed
189 (kill-buffer (current-buffer))))
191 ;;;###autoload
192 (defun org-export-as-odt-batch ()
193 "Call the function `org-export-as-odt'.
194 This function can be used in batch processing as:
195 emacs --batch
196 --load=$HOME/lib/emacs/org.el
197 --eval \"(setq org-export-headline-levels 2)\"
198 --visit=MyFile --funcall org-export-as-odt-batch"
199 (org-export-as-odt org-export-headline-levels 'hidden))
201 ;;;###autoload
202 (defun org-export-as-odt-to-buffer (arg)
203 "Call `org-export-as-odt` with output to a temporary buffer.
204 No file is created. The prefix ARG is passed through to `org-export-as-odt'."
205 (interactive "P")
206 (org-export-as-odt arg nil nil "*Org ODT Export*")
207 (when org-export-show-temporary-export-buffer
208 (switch-to-buffer-other-window "*Org ODT Export*")))
210 ;;;###autoload
211 (defun org-replace-region-by-odt (beg end)
212 "Assume the current region has org-mode syntax, and convert it to ODT.
213 This can be used in any buffer. For example, you could write an
214 itemized list in org-mode syntax in an ODT buffer and then use this
215 command to convert it."
216 (interactive "r")
217 (let (reg odt buf pop-up-frames)
218 (save-window-excursion
219 (if (org-mode-p)
220 (setq odt (org-export-region-as-odt
221 beg end t 'string))
222 (setq reg (buffer-substring beg end)
223 buf (get-buffer-create "*Org tmp*"))
224 (with-current-buffer buf
225 (erase-buffer)
226 (insert reg)
227 (org-mode)
228 (setq odt (org-export-region-as-odt
229 (point-min) (point-max) t 'string)))
230 (kill-buffer buf)))
231 (delete-region beg end)
232 (insert odt)))
234 ;;;###autoload
235 (defun org-export-region-as-odt (beg end &optional body-only buffer)
236 "Convert region from BEG to END in org-mode buffer to ODT.
237 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
238 contents, and only produce the region of converted text, useful for
239 cut-and-paste operations.
240 If BUFFER is a buffer or a string, use/create that buffer as a target
241 of the converted ODT. If BUFFER is the symbol `string', return the
242 produced ODT as a string and leave not buffer behind. For example,
243 a Lisp program could call this function in the following way:
245 (setq odt (org-export-region-as-odt beg end t 'string))
247 When called interactively, the output buffer is selected, and shown
248 in a window. A non-interactive call will only return the buffer."
249 (interactive "r\nP")
250 (when (interactive-p)
251 (setq buffer "*Org ODT Export*"))
252 (let ((transient-mark-mode t) (zmacs-regions t)
253 ext-plist rtn)
254 (setq ext-plist (plist-put ext-plist :ignore-subtree-p t))
255 (goto-char end)
256 (set-mark (point)) ;; to activate the region
257 (goto-char beg)
258 (setq rtn (org-export-as-odt
259 nil nil ext-plist
260 buffer body-only))
261 (if (fboundp 'deactivate-mark) (deactivate-mark))
262 (if (and (interactive-p) (bufferp rtn))
263 (switch-to-buffer-other-window rtn)
264 rtn)))
266 ;;; org-export-as-odt
267 ;;;###autoload
268 (defun org-export-as-odt (arg &optional hidden ext-plist
269 to-buffer body-only pub-dir)
270 "Export the outline as a OpenDocumentText file.
271 If there is an active region, export only the region. The prefix
272 ARG specifies how many levels of the outline should become
273 headlines. The default is 3. Lower levels will become bulleted
274 lists. HIDDEN is obsolete and does nothing.
275 EXT-PLIST is a property list with external parameters overriding
276 org-mode's default settings, but still inferior to file-local
277 settings. When TO-BUFFER is non-nil, create a buffer with that
278 name and export to that buffer. If TO-BUFFER is the symbol
279 `string', don't leave any buffer behind but just return the
280 resulting XML as a string. When BODY-ONLY is set, don't produce
281 the file header and footer, simply return the content of
282 <body>...</body>, without even the body tags themselves. When
283 PUB-DIR is set, use this as the publishing directory."
284 (interactive "P")
285 (run-hooks 'org-export-first-hook)
286 (let* ((org-parse-get-callback 'org-odt-get)
287 (org-export-html-special-string-regexps
288 org-export-odt-special-string-regexps))
289 (org-do-export arg hidden ext-plist to-buffer body-only pub-dir)))
292 ;;;_ parser
293 ;;;_. initialization
294 (defvar org-odt-entity-control-callbacks-alist
295 `((EXPORT
296 . (org-odt-begin-export org-odt-end-export))
297 (DOCUMENT-CONTENT
298 . (org-odt-begin-document-content org-odt-end-document-content))
299 (DOCUMENT-BODY
300 . (org-odt-begin-document-body org-odt-end-document-body))
301 (TOC
302 . (org-odt-begin-toc org-odt-end-toc))
303 (ENVIRONMENT
304 . (org-odt-begin-environment org-odt-end-environment))
305 (LEVEL
306 . (org-html-begin-level org-html-end-level))
307 (FOOTNOTE-DEFINITION
308 . (org-odt-begin-footnote-definition org-odt-end-footnote-definition))
309 (TABLE
310 . (org-odt-begin-table org-odt-end-table))
311 (TABLE-ROWGROUP
312 . (org-odt-begin-table-rowgroup org-odt-end-table-rowgroup))
313 (LIST
314 . (org-odt-begin-list org-odt-end-list))
315 (LIST-ITEM
316 . (org-odt-begin-list-item org-odt-end-list-item))
317 (SECTION
318 . (org-odt-begin-section org-odt-end-section))
319 (PARAGRAPH
320 . (org-odt-begin-paragraph org-odt-end-paragraph)))
323 (defvar org-odt-entity-format-callbacks-alist
324 `((EXTRA-TARGETS . org-html-format-extra-targets)
325 (ORG-TAGS . org-html-format-org-tags)
326 (SECTION-NUMBER . org-html-format-section-number)
327 (HEADLINE . org-odt-format-headline)
328 (TOC-ENTRY . org-odt-format-toc-entry)
329 (TOC-ITEM . org-odt-format-toc-item)
330 (TAGS . org-odt-format-tags)
331 (SPACES . org-odt-format-spaces)
332 (TABS . org-odt-format-tabs)
333 (LINE-BREAK . org-odt-format-line-break)
334 (FONTIFY . org-odt-format-fontify)
335 (TODO . org-html-format-todo)
336 (LINK . org-odt-format-link)
337 (INLINE-IMAGE . org-odt-format-inline-image)
338 (ORG-LINK . org-odt-format-org-link)
339 (HEADING . org-odt-format-heading)
340 (ANCHOR . org-odt-format-anchor)
341 (TABLE-ROW . org-odt-format-table-row)
342 (TABLE-CELL . org-odt-format-table-cell)
343 (FOOTNOTES-SECTION . ignore)
344 (FOOTNOTE-REFERENCE . org-odt-format-footnote-reference)
345 (HORIZONTAL-LINE . org-odt-format-horizontal-line)
346 (COMMENT . org-html-format-comment)
347 (PLAIN . org-odt-format-plain))
350 ;;;_. callbacks
351 ;;;_. control callbacks
352 ;;;_ , document body
353 (defun org-odt-begin-office-body ()
354 (insert "
355 <office:body>
356 <office:text>
357 <text:sequence-decls>
358 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Illustration\"/>
359 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Table\"/>
360 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Text\"/>
361 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Drawing\"/>
362 </text:sequence-decls>"))
365 ;; Following variable is let bound when `org-do-export' is in
366 ;; progress. See org-html.el.
367 (defvar org-parse-table-of-contents)
368 (defun org-odt-begin-document-body (opt-plist)
369 (org-odt-begin-office-body)
370 (let ((title (plist-get opt-plist :title)))
371 (when title
372 (insert
373 (org-odt-format-stylized-paragraph 'title title))))
375 ;; insert toc
376 (when org-parse-table-of-contents
377 (insert "\n" org-parse-table-of-contents "\n")))
379 (defun org-odt-end-document-body (opt-plist)
380 (org-html-insert-tag "</office:text>")
381 (org-html-insert-tag "</office:body>"))
383 (defconst org-odt-document-content-header
384 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
385 <office:document-content
386 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
387 xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\"
388 xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\"
389 xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\"
390 xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\"
391 xmlns:fo=\"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0\"
392 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
393 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
394 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
395 xmlns:number=\"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0\"
396 xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\"
397 xmlns:chart=\"urn:oasis:names:tc:opendocument:xmlns:chart:1.0\"
398 xmlns:dr3d=\"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0\"
399 xmlns:math=\"http://www.w3.org/1998/Math/MathML\"
400 xmlns:form=\"urn:oasis:names:tc:opendocument:xmlns:form:1.0\"
401 xmlns:script=\"urn:oasis:names:tc:opendocument:xmlns:script:1.0\"
402 xmlns:ooo=\"http://openoffice.org/2004/office\"
403 xmlns:ooow=\"http://openoffice.org/2004/writer\"
404 xmlns:oooc=\"http://openoffice.org/2004/calc\"
405 xmlns:dom=\"http://www.w3.org/2001/xml-events\"
406 xmlns:xforms=\"http://www.w3.org/2002/xforms\"
407 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
408 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
409 xmlns:rpt=\"http://openoffice.org/2005/report\"
410 xmlns:of=\"urn:oasis:names:tc:opendocument:xmlns:of:1.2\"
411 xmlns:xodt=\"http://www.w3.org/1999/xodt\"
412 xmlns:field=\"urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0\" office:version=\"1.2\">
417 ;;;_ , document content
418 (defun org-odt-begin-document-content (opt-plist)
419 ;; update meta-file
420 (org-odt-update-meta-file opt-plist)
422 ;; document header
423 (insert org-odt-document-content-header)
425 ;; automatic styles
426 (insert-file-contents org-export-odt-automatic-styles-file)
427 (goto-char (point-max)))
429 (defun org-odt-end-document-content ()
430 (org-html-insert-tag "</office:document-content>"))
433 ;;;_ , level
434 ;;;_ , section
435 (defun org-odt-begin-section (&optional id class
436 extra-class ; ignored for now
438 (let* ((extra
439 (concat
440 (when id (format " text:name=\"%s\"" id))
441 (when class
442 (format " text:style-name=\"%s\"" class)))))
443 (org-html-insert-tag "<text:section%s>" extra)))
445 (defun org-odt-end-section ()
446 (org-html-insert-tag "</text:section>"))
449 ;;;_ , heading
450 ;;;_ , paragraph
451 (defun org-odt-begin-paragraph (&optional style)
452 (org-html-insert-tag
453 "<text:p%s>" (org-odt-get-extra-attrs-for-paragraph-style style)))
455 (defun org-odt-end-paragraph ()
456 (org-html-insert-tag "</text:p>"))
459 (defun org-odt-get-extra-attrs-for-paragraph-style (style)
460 (let (style-name)
461 (setq style-name
462 (cond
463 ((stringp style) style)
464 ((symbolp style) (org-odt-get-style-name-for-entity
465 'paragraph style))))
466 (unless style-name
467 (error "Don't know how to handle paragraph style %s" style))
468 (format " text:style-name=\"%s\"" style-name)))
470 (defun org-odt-format-stylized-paragraph (style text)
471 (org-html-format-tags
472 '("<text:p%s>" . "</text:p>") text
473 (org-odt-get-extra-attrs-for-paragraph-style style)))
476 ;;;_ , environment
477 (defun org-odt-begin-environment (style)
478 (case style
479 ((blockquote verse center quote)
480 (org-parse-begin-paragraph style)
481 (list))
482 ((fixedwidth native)
483 (org-parse-end-paragraph)
484 (list))
485 (t (error "Unknown environment %s" style))))
487 (defun org-odt-end-environment (style)
488 (case style
489 ((blockquote verse center quote)
490 (org-parse-end-paragraph)
491 (list))
492 ((fixedwidth native)
493 (org-parse-begin-paragraph)
494 (list))
495 (t (error "Unknown environment %s" style))))
498 ;;;_ , list
499 (defun org-odt-begin-list (ltype &optional arg1)
500 (setq ltype (or (org-html-html-list-type-to-canonical-list-type ltype)
501 ltype))
502 (let* ((style-name (org-odt-get-style-name-for-entity 'list ltype))
503 (extra (if style-name
504 (format " text:style-name=\"%s\"" style-name) "")))
506 ;; FIXME: Handle arg1 incase of ordered lists.
507 (case ltype
508 ((ordered unordered description)
509 (org-parse-end-paragraph)
510 (org-html-insert-tag "<text:list%s>" extra))
511 (t (error "Unknown list type: %s" ltype)))))
513 (defun org-odt-end-list (ltype)
514 (setq ltype (or (org-html-html-list-type-to-canonical-list-type ltype)
515 ltype))
516 (if ltype
517 (org-html-insert-tag "</text:list>")
518 (error "Unknown list type: %s" ltype)))
520 (defun org-odt-begin-list-item (ltype &optional arg headline)
521 (setq ltype (or (org-html-html-list-type-to-canonical-list-type ltype)
522 ltype))
523 (case ltype
524 (ordered
525 (assert (not headline) t)
526 (let* ((counter arg) (extra ""))
527 (org-html-insert-tag "<text:list-item>")
528 (org-parse-begin-paragraph)))
529 (unordered
530 (let* ((id arg) (extra ""))
531 (org-html-insert-tag "<text:list-item>")
532 (org-parse-begin-paragraph)
533 (insert
534 (if headline (if id (org-odt-format-target headline id) headline)
535 (org-odt-format-bookmark "" id)))))
536 (description
537 (assert (not headline) t)
538 (let ((term (or arg "(no term)")))
539 (insert
540 (org-html-format-tags
541 '("<text:list-item>" . "</text:list-item>")
542 (org-odt-format-stylized-paragraph 'definition-term term)))
543 (org-parse-begin 'LIST-ITEM 'unordered)
544 (org-parse-begin 'LIST 'description)
545 (org-parse-begin 'LIST-ITEM 'unordered)))
546 (t (error "Unknown list type"))))
548 (defun org-odt-end-list-item (ltype)
549 (setq ltype (or (org-html-html-list-type-to-canonical-list-type ltype)
550 ltype))
551 (case ltype
552 ((ordered unordered)
553 (org-html-insert-tag "</text:list-item>"))
554 (description
555 (org-close-li)
556 (org-parse-end 'LIST 'description)
557 (org-close-li))
558 (t (error "Unknown list type"))))
560 ;;;_ , table
562 ;; Following variables are let bound when table emission is in
563 ;; progress. See org-html.el.
564 (defvar org-table-begin-marker)
565 (defvar org-table-ncols)
566 (defvar org-table-rowgroup-open)
567 (defvar org-table-rownum)
568 (defvar org-table-current-rowgroup-is-header)
569 (defvar org-table-is-styled)
570 (defvar org-table-rowgroup-info)
571 (defvar org-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-html-insert-tag
579 "<table:table table:name=\"%s\" table:style-name=\"%s\">"
580 (or label "") "OrgTable")
581 (setq org-table-begin-marker (point)))
583 (defun org-odt-end-table ()
584 (goto-char org-table-begin-marker)
585 (loop for level from 0 below org-table-ncols
586 do (insert (org-html-format-tags "<table:table-column/>" "")))
588 ;; fill style attributes for table cells
589 (when org-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-html-insert-tag "</table:table>"))
605 (defun org-odt-begin-table-rowgroup (&optional is-header-row)
606 (when org-table-rowgroup-open
607 (org-parse-end 'TABLE-ROWGROUP))
608 (org-html-insert-tag (if is-header-row
609 "<table:table-header-rows>"
610 "<table:table-rows>"))
611 (setq org-table-rowgroup-open t)
612 (setq org-table-current-rowgroup-is-header is-header-row))
614 (defun org-odt-end-table-rowgroup ()
615 (when org-table-rowgroup-open
616 (setq org-table-rowgroup-open nil)
617 (org-html-insert-tag
618 (if org-table-current-rowgroup-is-header
619 "</table:table-header-rows>" "</table:table-rows>"))))
621 (defun org-odt-format-table-row (row)
622 (org-html-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-table-rowgroup-info)) :start) "T")
631 (t ""))
632 (when (= r org-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-table-colalign-vector c)))
642 (defun org-odt-format-table-cell (data r c)
643 (if (not org-table-is-styled)
644 (org-html-format-tags
645 '("<table:table-cell>" . "</table:table-cell>")
646 (org-odt-format-stylized-paragraph
647 (cond
648 (org-table-current-rowgroup-is-header "OrgTableHeading")
649 ((and (= c 0) org-export-html-table-use-header-tags-for-first-column)
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-table-current-rowgroup-is-header "OrgTableHeading")
659 ((and (= c 0) org-export-html-table-use-header-tags-for-first-column)
660 "OrgTableHeading")
661 (t "OrgTableContents"))
662 (format "@@table-cell:p@@%03d@@%03d@@" r c))))
663 (org-html-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))))
670 ;;;_ , footnote definition
671 (defun org-odt-begin-footnote-definition (n)
672 (org-parse-begin-paragraph 'footnote))
674 (defun org-odt-end-footnote-definition (n)
675 (org-parse-end-paragraph))
678 ;;;_. format callbacks
680 (defun org-odt-begin-toc (lang-specific-heading)
681 (insert
682 (format "
683 <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
684 <text:table-of-content-source text:outline-level=\"10\">
685 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
686 " lang-specific-heading))
688 (loop for level from 1 upto 10
689 do (insert (format
691 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
692 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
693 <text:index-entry-chapter/>
694 <text:index-entry-text/>
695 <text:index-entry-link-end/>
696 </text:table-of-content-entry-template>
697 " level level)))
699 (insert "
700 </text:table-of-content-source>
702 <text:index-body>
703 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
704 <text:p text:style-name=\"Contents_20_Heading\">Table of Contents</text:p>
705 </text:index-title>
708 (defun org-odt-end-toc ()
709 (insert "
710 </text:index-body>
711 </text:table-of-content>
714 (defun org-odt-format-toc-entry (snumber todo headline tags href)
715 (setq headline (concat
716 (and org-export-with-section-numbers
717 (concat snumber ". "))
718 headline
719 (and tags
720 (concat
721 (org-parse-format 'SPACES 3)
722 (org-parse-format 'FONTIFY tags "tag")))))
723 (when todo
724 (setq headline (org-parse-format 'FONTIFY headline "todo")))
726 (let ((org-odt-suppress-xref t))
727 (org-odt-format-link headline (concat "#" href))))
729 (defun org-odt-format-toc-item (toc-entry level org-last-level)
730 (let ((style (format "Contents_20_%d"
731 (+ level (or (org-parse-get 'TOPLEVEL-HLEVEL) 1) -1))))
732 (insert "\n" (org-odt-format-stylized-paragraph style toc-entry) "\n")))
734 ;; Following variable is let bound during 'ORG-LINK callback. See
735 ;; org-html.el
736 (defvar org-parse-link-description-is-image nil)
737 (defun org-odt-format-link (desc href &optional attr)
738 (cond
739 ((and (= (string-to-char href) ?#) (not org-odt-suppress-xref))
740 (setq href (concat org-export-odt-bookmark-prefix (substring href 1)))
741 (org-html-format-tags
742 '("<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"%s\">" .
743 "</text:bookmark-ref>")
744 desc href))
745 (org-parse-link-description-is-image
746 (org-html-format-tags
747 '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</draw:a>")
748 desc href (or attr "")))
750 (org-html-format-tags
751 '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</text:a>")
752 desc href (or attr "")))))
754 ;;;_ , spaces
755 (defun org-odt-format-spaces (n)
756 (org-html-format-tags "<text:s text:c=\"%d\"/>" "" n))
758 ;;;_ , tabs
759 (defun org-odt-format-tabs (&optional n)
760 (let ((tab "<text:tab/>")
761 (n (or n 1)))
762 (insert tab)))
764 ;;;_ , line break
765 (defun org-odt-format-line-break ()
766 (org-html-format-tags "<text:line-break/>" ""))
768 ;;;_ , horizontal line
769 (defun org-odt-format-horizontal-line ()
770 (org-odt-format-stylized-paragraph 'horizontal-line ""))
772 ;;;_ , line
773 (defun org-odt-format-plain (line)
774 (case org-parse-dyn-current-environment
775 (fixedwidth (concat (org-odt-format-source-code-or-example-line
776 (org-html-protect line)) "\n"))
777 (t (concat line "\n"))))
779 (defun org-odt-fill-tabs-and-spaces (line)
780 (replace-regexp-in-string
781 "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
782 (cond
783 ((string= s "\t") (org-odt-format-tabs))
784 ((> (length s) 1)
785 (org-odt-format-spaces (length s)))
786 (t " "))) line))
788 (defun org-odt-format-source-code-or-example-line (line)
789 (org-odt-format-stylized-paragraph 'src (org-odt-fill-tabs-and-spaces line)))
791 (defun org-odt-format-example (lines)
792 (mapconcat
793 (lambda (line)
794 (org-odt-format-source-code-or-example-line line))
795 (org-split-string lines "[\r\n]") "\n"))
797 (defun org-html-protect-lines (rtn)
798 (mapconcat
799 (lambda (line) (org-html-protect line))
800 (org-split-string rtn "[\r\n]") "\n"))
802 ;;;_ , character styles
803 (defun org-odt-remap-stylenames (style-name)
805 (cdr (assoc style-name '(("timestamp-wrapper" . "OrgTimestampWrapper")
806 ("timestamp" . "OrgTimestamp")
807 ("timestamp-kwd" . "OrgTimestampKeyword")
808 ("tag" . "OrgTag")
809 ("todo" . "OrgTodo")
810 ("done" . "OrgDone")
811 ("target" . "OrgTarget"))))
812 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-html-format-tags
831 '("<text:span text:style-name=\"%s\">" . "</text:span>")
832 text style-name)))
834 ;;;_ , link
835 (defun org-odt-relocate-relative-path (path dir)
836 (if (file-name-absolute-p path) path
837 (file-relative-name (expand-file-name path dir)
838 (expand-file-name "eyecandy" dir))))
840 (defun org-odt-format-inline-image (thefile)
841 (let* ((thelink (if (file-name-absolute-p thefile) thefile
842 (org-export-html-format-href
843 (org-odt-relocate-relative-path
844 thefile org-current-export-file))))
845 (href
846 (org-html-format-tags
847 "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
848 (if org-export-odt-embed-images
849 (org-odt-copy-image-file thefile) thelink))))
850 (org-export-odt-format-image thefile href)))
852 (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
853 descp)
854 "Make an HTML link.
855 OPT-PLIST is an options list.
856 TYPE is the device-type of the link (THIS://foo.html)
857 PATH is the path of the link (http://THIS#locationx)
858 FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
859 DESC is the link description, if any.
860 ATTR is a string of other attributes of the a element.
861 MAY-INLINE-P allows inlining it as an image."
863 (declare (special org-par-open))
864 (save-match-data
865 (let* ((may-inline-p
866 (and (member type-1 '("http" "https" "file"))
867 (org-html-should-inline-p path descp)
868 (not fragment)))
869 (type (if (equal type-1 "id") "file" type-1))
870 (filename path)
871 (thefile path))
873 (cond
874 ;; check for inlined images
875 ((and (member type '("file"))
876 (not fragment)
877 (org-file-image-p
878 filename org-odt-export-inline-image-extensions)
879 (or (eq t org-odt-export-inline-images)
880 (and org-odt-export-inline-images (not descp))))
882 ;; (when (and (string= type "file") (file-name-absolute-p path))
883 ;; (setq thefile (concat "file://" (expand-file-name path))))
884 ;; (setq thefile (org-export-html-format-href thefile))
885 ;; (org-export-html-format-image thefile)
886 (org-odt-format-inline-image thefile))
888 (when (string= type "file")
889 (setq thefile
890 (cond
891 ((file-name-absolute-p path)
892 (concat "file://" (expand-file-name path)))
893 (t (org-odt-relocate-relative-path
894 thefile org-current-export-file)))))
896 (when (and (member type '("" "http" "https" "file" "coderef"))
897 fragment)
898 (setq thefile (concat thefile "#" fragment)))
900 (setq thefile (org-export-html-format-href thefile))
902 (when (not (member type '("" "file" "coderef")))
903 (setq thefile (concat type ":" thefile)))
905 (let ((org-odt-suppress-xref (string= type "coderef")))
906 (org-odt-format-link
907 (org-export-html-format-desc desc) thefile attr)))))))
910 ;;;_ , heading
911 (defun org-odt-format-heading (text level &optional id)
912 (let* ((text (if id (org-odt-format-target text id) text)))
913 (org-html-format-tags
914 '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
915 "</text:h>") text level level)))
917 ;;;_ , headline
919 (defun org-odt-format-headline (title extra-targets tags
920 &optional snumber level)
921 (concat
922 (org-parse-format 'EXTRA-TARGETS extra-targets)
924 ;; No need to generate section numbers. They are auto-generated by
925 ;; the application
927 ;; (concat (org-parse-format 'SECTION-NUMBER snumber level) " ")
928 title
929 (and tags (concat (org-parse-format 'SPACES 3)
930 (org-parse-format 'ORG-TAGS tags)))))
932 ;;;_ , anchor
934 (defun org-odt-format-anchor (text name &optional class)
935 (org-odt-format-target text name))
937 ;;;_ , target
938 (defun org-odt-format-bookmark (text id)
939 (org-html-format-tags "<text:bookmark text:name=\"%s\"/>" text id))
941 (defun org-odt-format-target (text id)
942 (let ((name (concat org-export-odt-bookmark-prefix id)))
943 (concat
944 (org-html-format-tags "<text:bookmark-start text:name=\"%s\"/>" "" name)
945 (org-odt-format-bookmark text id)
946 (org-html-format-tags "<text:bookmark-end text:name=\"%s\"/>" "" name))))
948 ;;;_ , footnote reference
949 (defun org-odt-format-footnote (n def)
950 (let ((id (concat "fn" n))
951 (note-class "footnote")
952 (par-style "Footnote"))
953 (org-html-format-tags
954 '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
955 "</text:note>")
956 (concat
957 (org-html-format-tags
958 '("<text:note-citation>" . "</text:note-citation>")
960 (org-html-format-tags
961 '("<text:note-body>" . "</text:note-body>")
962 def))
963 id note-class)))
966 (defun org-odt-format-footnote-reference (n def refcnt)
967 (if (= refcnt 1)
968 (org-odt-format-footnote n def)
969 (org-odt-format-footnote-ref n)))
972 (defun org-odt-format-footnote-ref (n)
973 (let ((note-class "footnote")
974 (ref-format "text")
975 (ref-name (concat "fn" n)))
976 (org-html-format-tags
977 '("<text:span text:style-name=\"%s\">" . "</text:span>")
978 (org-html-format-tags
979 '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" . "</text:note-ref>")
980 n note-class ref-format ref-name)
981 "OrgSuperscript")))
984 ;;;_ , image
985 (defun org-odt-get-image-name (file-name)
986 (require 'sha1)
987 (file-relative-name
988 (expand-file-name
989 (concat (sha1 file-name) "." (file-name-extension file-name)) "Pictures")))
991 (defun org-export-odt-format-image (src href
992 ;; par-open
994 "Create image tag with source and attributes."
995 (save-match-data
997 (let (embed-as caption attr label attr-plist size width height)
999 (cond
1000 ((string-match "^ltxpng/" src)
1001 ;; FIXME: Anyway the latex src can be embedded as an
1002 ;; annotation
1004 ;; (org-find-text-property-in-string 'org-latex-src src)
1005 (setq caption nil attr nil label nil embed-as 'character))
1008 (setq caption (org-find-text-property-in-string 'org-caption src)
1009 caption (and caption (org-export-html-format-desc caption))
1010 attr (org-find-text-property-in-string 'org-attributes src)
1011 label (org-find-text-property-in-string 'org-label src)
1012 embed-as 'paragraph)))
1014 (setq attr-plist (when attr (read attr)))
1015 (setq size (org-odt-image-size-from-file
1016 src (plist-get attr-plist :width)
1017 (plist-get attr-plist :height)
1018 (plist-get attr-plist :scale)))
1020 (org-export-odt-do-format-image embed-as caption attr label
1021 size href))))
1024 (defun org-export-odt-do-format-image (embed-as caption attr label
1025 size href)
1026 "Create image tag with source and attributes."
1027 (save-match-data
1028 (let ((width (car size)) (height (cdr size))
1029 (draw-frame-pair
1030 '("<draw:frame draw:style-name=\"%s\"
1031 text:anchor-type=\"%s\"
1032 draw:z-index=\"%d\" %s>" . "</draw:frame>")))
1033 (cond
1034 ((and (not caption) (not label))
1035 (let (style-name anchor-type)
1036 (cond
1037 ((eq embed-as 'paragraph)
1038 (setq style-name "OrgGraphicsParagraph" anchor-type "paragraph"))
1039 ((eq embed-as 'character)
1040 (setq style-name "OrgGraphicsBaseline" anchor-type "as-char")))
1041 (org-html-format-tags
1042 draw-frame-pair href style-name anchor-type 0
1043 (org-odt-image-attrs-from-size width height))))
1046 (concat
1047 ;; (when par-open (org-odt-close-par))
1048 (org-html-format-tags
1049 draw-frame-pair
1050 (org-html-format-tags
1051 '("<draw:text-box fo:min-height=\"%dcm\">" . "</draw:text-box>")
1052 (org-odt-format-stylized-paragraph
1053 'illustration
1054 (concat
1055 (let ((extra " style:rel-width=\"100%\" style:rel-height=\"scale\""))
1056 (org-html-format-tags
1057 draw-frame-pair href "OrgGraphicsParagraphContent" "paragraph" 2
1058 (concat (org-odt-image-attrs-from-size width height) extra)))
1059 (org-odt-format-entity-caption label caption)))
1060 height)
1061 "OrgFrame" "paragraph" 1
1062 (org-odt-image-attrs-from-size width))
1064 ;; (when par-open (org-odt-open-par))
1065 ))))))
1067 ;; xml files generated on-the-fly
1068 (defconst org-export-odt-save-list
1069 '("META-INF/manifest.xml" "content.xml" "meta.xml"))
1071 ;; xml files that are copied
1072 (defconst org-export-odt-nosave-list '("styles.xml"))
1074 ;; xml files that contribute to the final odt file
1075 (defvar org-export-odt-file-list nil)
1077 (defconst org-export-odt-manifest-lines
1078 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1079 "<manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">"
1080 "<manifest:file-entry manifest:media-type=\"application/vnd.oasis.opendocument.text\" manifest:version=\"1.2\" manifest:full-path=\"/\"/>"
1081 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"content.xml\"/>"
1082 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"styles.xml\"/>"
1083 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"meta.xml\"/>"
1084 "<manifest:file-entry manifest:media-type=\"\" manifest:full-path=\"Pictures/\"/>") . ("</manifest:manifest>")))
1086 (defconst org-export-odt-meta-lines
1087 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1088 "<office:document-meta"
1089 " xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\""
1090 " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
1091 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\""
1092 " xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\""
1093 " xmlns:ooo=\"http://openoffice.org/2004/office\" "
1094 " office:version=\"1.2\">"
1095 " <office:meta>") . (" </office:meta>" "</office:document-meta>")))
1097 (defun org-odt-copy-image-file (path &optional target-file)
1098 "Returns the internal name of the file"
1099 (let* ((image-type (file-name-extension path))
1100 (media-type (format "image/%s" image-type))
1101 (src-file (expand-file-name
1102 path (file-name-directory org-current-export-file)))
1103 (target-file (or target-file (org-odt-get-image-name src-file)))
1104 ;; FIXME
1105 (body-only nil))
1107 (unless body-only
1108 (message "Embedding %s as %s ..."
1109 (substring-no-properties path) target-file)
1110 (copy-file src-file target-file 'overwrite)
1111 (org-odt-update-manifest-file media-type target-file)
1112 (push target-file org-export-odt-file-list)) target-file))
1114 (defun org-odt-image-attrs-from-size (&optional width height)
1115 (concat
1116 (when width (format "svg:width=\"%0.2fcm\"" width))
1118 (when height (format "svg:height=\"%0.2fcm\"" height))))
1120 (defun org-odt-image-size-from-file (file &optional user-width
1121 user-height scale dpi)
1122 (unless (file-name-absolute-p file)
1123 (setq file (expand-file-name
1124 file (file-name-directory org-current-export-file))))
1126 (setq dpi (or dpi org-export-odt-pixels-per-inch))
1127 (let* ((size (image-size (create-image file) 'pixels))
1128 (width-in-pixels (car size)) (height-in-pixels (cdr size))
1129 (width-in-inches (/ width-in-pixels dpi))
1130 (height-in-inches (/ height-in-pixels dpi))
1131 (cms-per-inch 2.54)
1132 (width-in-cms (* cms-per-inch width-in-inches))
1133 (height-in-cms (* cms-per-inch height-in-inches))
1134 (width width-in-cms) (height height-in-cms))
1135 (cond (scale (setq width (* width scale) height (* height scale)))
1136 ((and user-height user-width)
1137 (setq width user-width height user-height))
1138 (user-height (setq width (* user-height (/ width height))
1139 height user-height))
1140 (user-width (setq height (* user-width (/ height width))
1141 width user-width))
1142 (t (ignore)))
1143 (cons width height)))
1145 (defvar org-odt-default-entity "Illustration")
1146 (defun org-odt-format-entity-caption (label caption &optional default-entity)
1147 (if (not label) (or caption "")
1148 (let* ((label-components (org-odt-parse-label label))
1149 (entity (car label-components))
1150 (seqno (cdr label-components))
1151 (caption (and caption (concat ": " caption))))
1152 (unless seqno
1153 (setq seqno label
1154 entity (or default-entity org-odt-default-entity)))
1155 (concat
1156 entity " "
1157 (org-html-format-tags
1158 '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" . "</text:sequence>")
1159 seqno label entity entity)
1160 caption))))
1162 ;;;_. maintenance callbacks
1163 ;;;_ , init method
1165 (defun org-odt-init-outfile (filename)
1166 (let* ((outdir (make-temp-file org-export-odt-tmpdir-prefix t))
1167 (mimetype-file (expand-file-name "mimetype" outdir))
1168 (content-file (expand-file-name "content.xml" outdir))
1169 (manifest-file (expand-file-name "META-INF/manifest.xml" outdir))
1170 (meta-file (expand-file-name "meta.xml" outdir))
1171 (styles-file (expand-file-name "styles.xml" outdir))
1172 (pictures-dir (expand-file-name "Pictures" outdir))
1173 (body-only nil))
1175 ;; content file
1176 (with-current-buffer (find-file-noselect content-file) (erase-buffer))
1178 ;; FIXME: How to factor in body-only here
1179 (unless body-only
1180 ;; manifest file
1181 (make-directory (file-name-directory manifest-file))
1182 (with-current-buffer (find-file-noselect manifest-file)
1183 (erase-buffer)
1184 (insert (mapconcat 'identity (car org-export-odt-manifest-lines) "\n"))
1185 (insert "\n")
1186 (save-excursion
1187 (insert (mapconcat 'identity (cdr org-export-odt-manifest-lines) "\n"))))
1189 ;; meta file
1190 (with-current-buffer (find-file-noselect meta-file)
1191 (erase-buffer)
1192 (insert (mapconcat 'identity (car org-export-odt-meta-lines) "\n"))
1193 (insert "\n")
1194 (save-excursion
1195 (insert (mapconcat 'identity (cdr org-export-odt-meta-lines) "\n"))))
1197 ;; styles file
1198 ;; (copy-file org-export-odt-styles-file styles-file t)
1200 ;; Pictures dir
1201 (make-directory pictures-dir)
1203 ;; initialize list of files that contribute to the odt file
1204 (setq org-export-odt-file-list
1205 (append org-export-odt-save-list org-export-odt-nosave-list)))
1206 content-file))
1208 ;;;_ , save metod
1209 (defconst org-export-odt-mimetype-lines
1210 '("application/vnd.oasis.opendocument.text"))
1212 (defconst org-odt-manifest-file-entry-tag
1213 "<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"/>")
1215 (defun org-odt-save-as-outfile (target)
1216 (org-odt-copy-styles-file)
1218 ;; Update styles.xml - take care of outline numbering
1219 (with-current-buffer
1220 (find-file-noselect "styles.xml" (file-name-directory buffer-file-name))
1222 ;; Import local setting of `org-export-with-section-numbers'
1223 (org-html-bind-local-variables opt-plist)
1224 (org-odt-configure-outline-numbering
1225 (if org-export-with-section-numbers org-export-headline-levels 0)))
1227 (let ((zipdir default-directory))
1228 (message "Switching to directory %s" (expand-file-name zipdir))
1230 ;; save all xml files
1231 (mapc (lambda (file)
1232 (with-current-buffer (find-file-noselect (expand-file-name file))
1233 ;; prettify output
1234 (indent-region (point-min) (point-max)) (save-buffer)))
1235 org-export-odt-save-list)
1237 (let* ((target-name (file-name-nondirectory target))
1238 (target-dir (file-name-directory target))
1239 (cmd (format "zip -rmTq %s %s" target-name ".")))
1240 (when (file-exists-p target)
1241 ;; FIXME: If the file is locked this throws a cryptic error
1242 (delete-file target))
1244 (let ((coding-system-for-write 'no-conversion) exitcode)
1245 (message "Creating odt file using \"%s\"" cmd)
1246 (setq exitcode
1247 (apply 'call-process
1248 "zip"
1252 (append (list "-rmTq") (list target-name "."))))
1254 (or (zerop exitcode)
1255 (error "Unable to create odt file (%S)" exitcode)))
1257 ;; move the file from outdir to target-dir
1258 (rename-file target-name target-dir)
1260 ;; kill all xml buffers
1261 (mapc (lambda (file)
1262 (kill-buffer (find-file-noselect (expand-file-name file zipdir))))
1263 org-export-odt-save-list)
1265 (delete-directory zipdir)))
1267 ;; FIXME: How to open the file without visiting
1268 (find-file target))
1270 (defun org-odt-update-meta-file (opt-plist)
1271 (with-current-buffer
1272 (find-file-noselect "meta.xml" (file-name-directory buffer-file-name))
1273 (let ((date (or (plist-get opt-plist :effective-date) ""))
1274 (author (or (plist-get opt-plist :author) ""))
1275 (email (plist-get opt-plist :email))
1276 (keywords (plist-get opt-plist :keywords))
1277 (description (plist-get opt-plist :description))
1278 (title (plist-get opt-plist :title)))
1280 (insert
1281 "\n"
1282 (org-html-format-tags '("<dc:creator>" . "</dc:creator>") author)
1283 (org-html-format-tags
1284 '("\n<meta:initial-creator>" . "</meta:initial-creator>") author)
1285 (org-html-format-tags '("\n<dc:date>" . "</dc:date>") date)
1286 (org-html-format-tags
1287 '("\n<meta:creation-date>" . "</meta:creation-date>") date)
1288 (org-html-format-tags '("\n<meta:generator>" . "</meta:generator>")
1289 (when org-export-creator-info
1290 (format "Org-%s/Emacs-%s"
1291 org-version emacs-version)))
1292 (org-html-format-tags '("\n<meta:keyword>" . "</meta:keyword>") keywords)
1293 (org-html-format-tags '("\n<dc:subject>" . "</dc:subject>") description)
1294 (org-html-format-tags '("\n<dc:title>" . "</dc:title>") title)
1295 "\n"))))
1298 (defun org-odt-update-manifest-file (media-type full-path)
1299 (with-current-buffer
1300 (find-file-noselect "META-INF/manifest.xml")
1301 (insert (format org-odt-manifest-file-entry-tag media-type full-path))))
1304 ;;;_ , cleanup method
1306 (defun org-odt-finalize-outfile ()
1307 (message "org-newodt: Finalizing outfile")
1308 (org-odt-delete-empty-paragraphs))
1310 (defun org-odt-delete-empty-paragraphs ()
1311 (goto-char (point-min))
1312 (let ((open "<text:p[^>]*>")
1313 (close "</text:p>"))
1314 (while (re-search-forward (format "%s[ \r\n\t]*%s" open close) nil t)
1315 (replace-match ""))))
1317 ;;;_. get callback
1318 (defun org-odt-get (what &optional opt-plist)
1319 (case what
1320 (BACKEND 'odt)
1321 (EXPORT-DIR (org-export-directory :html opt-plist))
1322 (FILE-NAME-EXTENSION "odt")
1323 (EXPORT-BUFFER-NAME "*Org ODT Export*")
1324 (ENTITY-CONTROL org-odt-entity-control-callbacks-alist)
1325 (ENTITY-FORMAT org-odt-entity-format-callbacks-alist)
1326 (INIT-METHOD 'org-odt-init-outfile)
1327 (FINAL-METHOD 'org-odt-finalize-outfile)
1328 (SAVE-METHOD 'org-odt-save-as-outfile)
1329 (TOPLEVEL-HLEVEL 1)
1330 (t (error "Unknown property: %s" what))))
1332 ;;;_ preprocessor
1334 (defun org-odt-parse-label (label)
1335 (save-match-data
1336 (if (not (string-match "\\`[a-zA-Z]+:\\(.+\\)" label))
1337 (cons label nil)
1338 (cons
1339 (capitalize (substring label 0 (1- (match-beginning 1))))
1340 (substring label (match-beginning 1))))))
1342 (defun org-export-odt-preprocess (parameters)
1343 "Convert LaTeX fragments to images."
1344 (when (and org-current-export-file
1345 (plist-get parameters :LaTeX-fragments))
1346 (org-format-latex
1347 (concat "ltxpng/" (file-name-sans-extension
1348 (file-name-nondirectory
1349 org-current-export-file)))
1350 org-current-export-dir nil "Creating LaTeX image %s"
1351 nil nil
1352 (cond
1353 ((eq (plist-get parameters :LaTeX-fragments) 'verbatim) 'verbatim)
1354 ;; Investigate MathToWeb for converting TeX equations to MathML
1355 ;; See http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01755.html
1356 ((or (eq (plist-get parameters :LaTeX-fragments) 'mathjax )
1357 (eq (plist-get parameters :LaTeX-fragments) t ))
1358 (org-odt-warn "MathJax not supported. Falling back to dvipng")
1359 'dvipng)
1360 ((eq (plist-get parameters :LaTeX-fragments) 'dvipng ) 'dvipng)
1361 (t nil))))
1362 (goto-char (point-min))
1363 (let (label label-components category value pretty-label)
1364 (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t)
1365 (org-if-unprotected-at (match-beginning 1)
1366 (setq label (match-string 1)
1367 label-components (org-odt-parse-label label)
1368 category (car label-components)
1369 value (cdr label-components)
1370 pretty-label (if value (concat category " " value) label))
1371 (replace-match
1372 (let ((org-html-protect t))
1373 (org-html-format-tags
1374 '("<text:sequence-ref text:reference-format=\"category-and-value\" text:ref-name=\"%s\">"
1375 . "</text:sequence-ref>") pretty-label label)) t t)))))
1377 (declare-function archive-zip-extract "arc-mode.el" (archive name))
1378 (defun org-odt-zip-extract-one (archive member &optional target)
1379 (require 'arc-mode)
1380 (let* ((target (or target default-directory))
1381 (archive (expand-file-name archive))
1382 (archive-zip-extract
1383 (list "unzip" "-qq" "-o" "-d" target))
1384 exit-code command-output)
1385 (setq command-output
1386 (with-temp-buffer
1387 (setq exit-code (archive-zip-extract archive member))
1388 (buffer-string)))
1389 (unless (zerop exit-code)
1390 (message command-output)
1391 (error "Extraction failed"))))
1393 (defun org-odt-zip-extract (archive members &optional target)
1394 (when (atom members) (setq members (list members)))
1395 (mapc (lambda (member)
1396 (org-odt-zip-extract-one archive member target))
1397 members))
1399 (defun org-odt-copy-styles-file (&optional styles-file)
1400 ;; Non-availability of styles.xml is not a critical error. For now
1401 ;; throw an error purely for aesthetic reasons.
1402 (setq styles-file (or styles-file
1403 org-export-odt-styles-file
1404 (error "org-odt: Missing styles file?")))
1405 (cond
1406 ((listp styles-file)
1407 (let ((archive (nth 0 styles-file))
1408 (members (nth 1 styles-file)))
1409 (org-odt-zip-extract archive members)
1410 (mapc
1411 (lambda (member)
1412 (when (org-file-image-p member)
1413 (let* ((image-type (file-name-extension member))
1414 (media-type (format "image/%s" image-type)))
1415 (org-odt-update-manifest-file media-type member))))
1416 members)))
1417 ((and (stringp styles-file) (file-exists-p styles-file))
1418 (let ((styles-file-type (file-name-extension styles-file)))
1419 (cond
1420 ((string= styles-file-type "xml")
1421 (copy-file styles-file "styles.xml" t))
1422 ((member styles-file-type '("odt" "ott"))
1423 (org-odt-zip-extract styles-file "styles.xml")))))
1425 (error (format "Invalid specification of styles.xml file: %S"
1426 org-export-odt-styles-file)))))
1429 ;;; convenience routines
1431 (defvar org-export-odt-factory-settings
1432 "b629fe72d6620ffb0fcf684be1412330bed74b14"
1433 "SHA1 hash of OrgOdtStyles.xml.")
1435 (defun org-odt-configure-outline-numbering (level)
1436 "Outline numbering is retained only upto LEVEL.
1437 To disable outline numbering pass a LEVEL of 0."
1438 (if (not (string= org-export-odt-factory-settings (sha1 (current-buffer))))
1439 (org-odt-warn
1440 "org-odt: Using custom styles file? Consider tweaking styles.xml for better output. To suppress this warning update `org-export-odt-factory-settings'")
1441 (goto-char (point-min))
1442 (let ((regex
1443 "<text:outline-level-style\\(.*\\)text:level=\"\\(.*\\)\"\\(.*\\)>")
1444 (replacement
1445 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1446 (while (re-search-forward regex nil t)
1447 (when (> (string-to-number (match-string 1)) level)
1448 (replace-match replacement t nil))))
1449 (save-buffer 0)))
1451 (defun org-odt-warn (msg)
1452 (put-text-property 0 (length msg) 'face 'font-lock-warning-face msg)
1453 (message msg)
1454 (sleep-for 3))
1456 ;;;_ postamble
1458 (provide 'org-odt)