org-html.el: Emit backend specific line breaks.
[org-mode/org-jambu.git] / lisp / org-odt.el
blob88b0e9c633dcca7e903b70f4fd557f86fc3a8dbd
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 (ENVIRONMENT
302 . (org-odt-begin-environment org-odt-end-environment))
303 (LEVEL
304 . (org-html-begin-level org-html-end-level))
305 (FOOTNOTE-DEFINITION
306 . (org-odt-begin-footnote-definition org-odt-end-footnote-definition))
307 (TABLE
308 . (org-odt-begin-table org-odt-end-table))
309 (TABLE-ROWGROUP
310 . (org-odt-begin-table-rowgroup org-odt-end-table-rowgroup))
311 (LIST
312 . (org-odt-begin-list org-odt-end-list))
313 (LIST-ITEM
314 . (org-odt-begin-list-item org-odt-end-list-item))
315 (SECTION
316 . (org-odt-begin-section org-odt-end-section))
317 (PARAGRAPH
318 . (org-odt-begin-paragraph org-odt-end-paragraph)))
321 (defvar org-odt-entity-format-callbacks-alist
322 `((EXTRA-TARGETS . org-html-format-extra-targets)
323 (ORG-TAGS . org-html-format-org-tags)
324 (SECTION-NUMBER . org-html-format-section-number)
325 (HEADLINE . org-html-format-headline)
326 (TOC-ENTRY . org-html-format-toc-entry)
327 (TOC-ITEM . org-html-format-toc-item)
328 (TAGS . org-odt-format-tags)
329 (SPACES . org-odt-format-spaces)
330 (TABS . org-odt-format-tabs)
331 (LINE-BREAK . org-odt-format-line-break)
332 (FONTIFY . org-odt-format-fontify)
333 (TODO . org-html-format-todo)
334 (LINK . org-odt-format-link)
335 (INLINE-IMAGE . org-odt-format-inline-image)
336 (ORG-LINK . org-odt-format-org-link)
337 (HEADING . org-odt-format-heading)
338 (ANCHOR . org-odt-format-anchor)
339 (TABLE-ROW . org-odt-format-table-row)
340 (TABLE-CELL . org-odt-format-table-cell)
341 (FOOTNOTES-SECTION . ignore)
342 (FOOTNOTE-REFERENCE . org-odt-format-footnote-reference)
343 (HORIZONTAL-LINE . org-odt-format-horizontal-line)
344 (COMMENT . org-html-format-comment)
345 (PLAIN . org-odt-format-plain))
348 ;;;_. callbacks
349 ;;;_. control callbacks
350 ;;;_ , document body
351 (defun org-odt-begin-office-body ()
352 (insert "
353 <office:body>
354 <office:text>
355 <text:sequence-decls>
356 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Illustration\"/>
357 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Table\"/>
358 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Text\"/>
359 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Drawing\"/>
360 </text:sequence-decls>"))
363 ;; Following variable is let bound when `org-do-export' is in
364 ;; progress. See org-html.el.
365 (defvar org-parse-table-of-contents)
366 (defun org-odt-begin-document-body (opt-plist)
367 (org-odt-begin-office-body)
368 (let ((title (plist-get opt-plist :title)))
369 (when title
370 (insert
371 (org-odt-format-stylized-paragraph 'title title))))
373 ;; insert toc
374 (when org-parse-table-of-contents
375 (insert "\n" org-parse-table-of-contents "\n")))
377 (defun org-odt-end-document-body (opt-plist)
378 (org-html-insert-tag "</office:text>")
379 (org-html-insert-tag "</office:body>"))
381 (defconst org-odt-document-content-header
382 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
383 <office:document-content
384 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
385 xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\"
386 xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\"
387 xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\"
388 xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\"
389 xmlns:fo=\"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0\"
390 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
391 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
392 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
393 xmlns:number=\"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0\"
394 xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\"
395 xmlns:chart=\"urn:oasis:names:tc:opendocument:xmlns:chart:1.0\"
396 xmlns:dr3d=\"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0\"
397 xmlns:math=\"http://www.w3.org/1998/Math/MathML\"
398 xmlns:form=\"urn:oasis:names:tc:opendocument:xmlns:form:1.0\"
399 xmlns:script=\"urn:oasis:names:tc:opendocument:xmlns:script:1.0\"
400 xmlns:ooo=\"http://openoffice.org/2004/office\"
401 xmlns:ooow=\"http://openoffice.org/2004/writer\"
402 xmlns:oooc=\"http://openoffice.org/2004/calc\"
403 xmlns:dom=\"http://www.w3.org/2001/xml-events\"
404 xmlns:xforms=\"http://www.w3.org/2002/xforms\"
405 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
406 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
407 xmlns:rpt=\"http://openoffice.org/2005/report\"
408 xmlns:of=\"urn:oasis:names:tc:opendocument:xmlns:of:1.2\"
409 xmlns:xodt=\"http://www.w3.org/1999/xodt\"
410 xmlns:field=\"urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0\" office:version=\"1.2\">
415 ;;;_ , document content
416 (defun org-odt-begin-document-content (opt-plist)
417 ;; update meta-file
418 (org-odt-update-meta-file opt-plist)
420 ;; document header
421 (insert org-odt-document-content-header)
423 ;; automatic styles
424 (insert-file-contents org-export-odt-automatic-styles-file)
425 (goto-char (point-max)))
427 (defun org-odt-end-document-content ()
428 (org-html-insert-tag "</office:document-content>"))
431 ;;;_ , level
432 ;;;_ , section
433 (defun org-odt-begin-section (&optional id class)
434 (let* ((extra
435 (concat
436 (when id (format " text:name=\"%s\"" id))
437 (when class
438 (format " text:style-name=\"%s\"" class)))))
439 (org-html-insert-tag "<text:section%s>" extra)))
441 (defun org-odt-end-section ()
442 (org-html-insert-tag "</text:section>"))
445 ;;;_ , heading
446 ;;;_ , paragraph
447 (defun org-odt-begin-paragraph (&optional style)
448 (org-html-insert-tag
449 "<text:p%s>" (org-odt-get-extra-attrs-for-paragraph-style style)))
451 (defun org-odt-end-paragraph ()
452 (org-html-insert-tag "</text:p>"))
455 (defun org-odt-get-extra-attrs-for-paragraph-style (style)
456 (let (style-name)
457 (setq style-name
458 (cond
459 ((stringp style) style)
460 ((symbolp style) (org-odt-get-style-name-for-entity
461 'paragraph style))))
462 (unless style-name
463 (error "Don't know how to handle paragraph style %s" style))
464 (format " text:style-name=\"%s\"" style-name)))
466 (defun org-odt-format-stylized-paragraph (style text)
467 (org-html-format-tags
468 '("<text:p%s>" . "</text:p>") text
469 (org-odt-get-extra-attrs-for-paragraph-style style)))
472 ;;;_ , environment
473 (defun org-odt-begin-environment (style)
474 (case style
475 ((blockquote verse center quote)
476 (org-parse-begin-paragraph style)
477 (list))
478 ((fixedwidth)
479 (org-parse-end-paragraph)
480 (list))
481 (t (error "Unknown environment %s" style))))
483 (defun org-odt-end-environment (style)
484 (case style
485 ((blockquote verse center quote)
486 (org-parse-end-paragraph)
487 (list))
488 ((fixedwidth)
489 (org-parse-begin-paragraph)
490 (list))
491 (t (error "Unknown environment %s" style))))
494 ;;;_ , list
495 (defun org-odt-begin-list (ltype &optional arg1)
496 (setq ltype (or (org-html-html-list-type-to-canonical-list-type ltype)
497 ltype))
498 (let* ((style-name (org-odt-get-style-name-for-entity 'list ltype))
499 (extra (if style-name
500 (format " text:style-name=\"%s\"" style-name) "")))
502 ;; FIXME: Handle arg1 incase of ordered lists.
503 (case ltype
504 ((ordered unordered description)
505 (org-parse-end-paragraph)
506 (org-html-insert-tag "<text:list%s>" extra))
507 (t (error "Unknown list type: %s" ltype)))))
509 (defun org-odt-end-list (ltype)
510 (setq ltype (or (org-html-html-list-type-to-canonical-list-type ltype)
511 ltype))
512 (if ltype
513 (org-html-insert-tag "</text:list>")
514 (error "Unknown list type: %s" ltype)))
516 (defun org-odt-begin-list-item (ltype &optional arg)
517 (setq ltype (or (org-html-html-list-type-to-canonical-list-type ltype)
518 ltype))
519 (case ltype
520 (ordered
521 (let* ((counter arg) (extra ""))
522 (org-html-insert-tag "<text:list-item>")
523 (org-parse-begin-paragraph)))
524 (unordered
525 (let* ((id arg) (extra ""))
526 (org-html-insert-tag "<text:list-item>")
527 (org-parse-begin-paragraph)))
528 (description
529 (let ((term (or arg "(no term)")))
530 (insert
531 (org-html-format-tags
532 '("<text:list-item>" . "</text:list-item>")
533 (org-odt-format-stylized-paragraph 'definition-term term)))
534 (org-parse-begin 'LIST-ITEM 'unordered)
535 (org-parse-begin 'LIST 'description)
536 (org-parse-begin 'LIST-ITEM 'unordered)))
537 (t (error "Unknown list type"))))
539 (defun org-odt-end-list-item (ltype)
540 (setq ltype (or (org-html-html-list-type-to-canonical-list-type ltype)
541 ltype))
542 (case ltype
543 ((ordered unordered)
544 (org-html-insert-tag "</text:list-item>"))
545 (description
546 (org-close-li)
547 (org-parse-end 'LIST 'description)
548 (org-close-li))
549 (t (error "Unknown list type"))))
551 ;;;_ , table
553 ;; Following variables are let bound when table emission is in
554 ;; progress. See org-html.el.
555 (defvar org-table-begin-marker)
556 (defvar org-table-ncols)
557 (defvar org-table-rowgroup-open)
558 (defvar org-table-rownum)
559 (defvar org-table-current-rowgroup-is-header)
560 (defvar org-table-is-styled)
561 (defvar org-table-rowgroup-info)
562 (defvar org-table-colalign-vector)
563 (defun org-odt-begin-table (caption label attributes)
564 (when label
565 (let* ((seqno label) (id label))
566 (insert
567 (org-odt-format-stylized-paragraph
568 'table (org-odt-format-entity-caption
569 "Table" id seqno (or caption ""))))))
571 (org-html-insert-tag
572 "<table:table table:name=\"%s\" table:style-name=\"%s\">"
573 (or label "") "OrgTable")
574 (setq org-table-begin-marker (point)))
576 (defun org-odt-end-table ()
577 (goto-char org-table-begin-marker)
578 (let ((i -1))
579 (while (< (incf i) org-table-ncols)
580 (insert
581 (org-html-format-tags
582 '("<table:table-column >" . "</table:table-column>") ""))))
584 ;; fill style attributes for table cells
585 (when org-table-is-styled
586 (while (re-search-forward "@@\\(table-cell:p\\|table-cell:style-name\\)@@\\([0-9]+\\)@@\\([0-9]+\\)@@" nil t)
587 (let ((spec (match-string 1))
588 (r (string-to-number (match-string 2)))
589 (c (string-to-number (match-string 3))))
590 (cond
591 ((equal spec "table-cell:p")
592 (let ((style-name (org-odt-get-paragraph-style-for-table-cell r c)))
593 (replace-match style-name t t)))
594 ((equal spec "table-cell:style-name")
595 (let ((style-name (org-odt-get-style-name-for-table-cell r c)))
596 (replace-match style-name t t)))))))
598 (goto-char (point-max))
599 (org-html-insert-tag "</table:table>"))
601 (defun org-odt-begin-table-rowgroup (&optional is-header-row)
602 (when org-table-rowgroup-open
603 (org-parse-end 'TABLE-ROWGROUP))
604 (org-html-insert-tag (if is-header-row
605 "<table:table-header-rows>"
606 "<table:table-rows>"))
607 (setq org-table-rowgroup-open t)
608 (setq org-table-current-rowgroup-is-header is-header-row))
610 (defun org-odt-end-table-rowgroup ()
611 (when org-table-rowgroup-open
612 (setq org-table-rowgroup-open nil)
613 (org-html-insert-tag
614 (if org-table-current-rowgroup-is-header
615 "</table:table-header-rows>" "</table:table-rows>"))))
617 (defun org-odt-format-table-row (row)
618 (org-html-format-tags
619 '("<table:table-row>" . "</table:table-row>") row))
621 (defun org-odt-get-style-name-for-table-cell (r c)
622 (concat
623 "OrgTblCell"
624 (cond
625 ((= r 0) "T")
626 ((eq (cdr (assoc r org-table-rowgroup-info)) :start) "T")
627 (t ""))
628 (when (= r org-table-rownum) "B")
629 (cond
630 ((= c 0) "")
631 ((or (memq (nth c org-table-colgroup-info) '(:start :startend))
632 (memq (nth (1- c) org-table-colgroup-info) '(:end :startend))) "L")
633 (t ""))))
635 (defun org-odt-get-paragraph-style-for-table-cell (r c)
636 (capitalize (aref org-table-colalign-vector c)))
638 (defun org-odt-format-table-cell (data r c)
639 (if (not org-table-is-styled)
640 (org-html-format-tags
641 '("<table:table-cell>" . "</table:table-cell>")
642 (org-odt-format-stylized-paragraph
643 (cond
644 (org-table-current-rowgroup-is-header "OrgTableHeading")
645 ((and (= c 0) org-export-html-table-use-header-tags-for-first-column)
646 "OrgTableHeading")
647 (t "OrgTableContents"))
648 data))
649 (let* ((style-name-cookie
650 (format "@@table-cell:style-name@@%03d@@%03d@@" r c))
651 (paragraph-style-cookie
652 (concat
653 (cond
654 (org-table-current-rowgroup-is-header "OrgTableHeading")
655 ((and (= c 0) org-export-html-table-use-header-tags-for-first-column)
656 "OrgTableHeading")
657 (t "OrgTableContents"))
658 (format "@@table-cell:p@@%03d@@%03d@@" r c))))
659 (org-html-format-tags
660 '("<table:table-cell table:style-name=\"%s\">" .
661 "</table:table-cell>")
662 (org-odt-format-stylized-paragraph paragraph-style-cookie data)
663 style-name-cookie))))
666 ;;;_ , footnote definition
667 (defun org-odt-begin-footnote-definition (n)
668 (org-parse-begin-paragraph 'footnote))
670 (defun org-odt-end-footnote-definition (n)
671 (org-parse-end-paragraph))
674 ;;;_. format callbacks
676 ;; Following variable is let bound during 'ORG-LINK callback. See
677 ;; org-html.el
678 (defvar org-parse-link-description-is-image)
679 (defun org-odt-format-link (desc href &optional attr)
680 (cond
681 ((and (= (string-to-char href) ?#) (not org-odt-suppress-xref))
682 (setq href (concat org-export-odt-bookmark-prefix (substring href 1)))
683 (org-html-format-tags
684 '("<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"%s\">" .
685 "</text:bookmark-ref>")
686 desc href))
687 (org-parse-link-description-is-image
688 (org-html-format-tags
689 '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</draw:a>")
690 desc href (or attr "")))
692 (org-html-format-tags
693 '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</text:a>")
694 desc href (or attr "")))))
696 ;;;_ , spaces
697 (defun org-odt-format-spaces (n)
698 (org-html-format-tags
699 '("<text:s text:c=\"%d\">" . "</text:s>") "" n))
701 ;;;_ , tabs
702 (defun org-odt-format-tabs (&optional n)
703 (let ((tab "<text:tab/>")
704 (n (or n 1)))
705 (insert tab)))
707 ;;;_ , line break
708 (defun org-odt-format-line-break ()
709 (org-html-format-tags
710 '("<text:line-break>" . "</text:line-break>") ""))
712 ;;;_ , horizontal line
713 (defun org-odt-format-horizontal-line ()
714 (org-odt-format-stylized-paragraph 'horizontal-line ""))
716 ;;;_ , line
717 (defun org-odt-format-plain (line)
718 (case org-parse-dyn-current-environment
719 (fixedwidth (concat (org-odt-format-source-code-or-example-line
720 (org-html-protect line)) "\n"))
721 (t (concat line "\n"))))
723 (defun org-odt-fill-tabs-and-spaces (line)
724 (replace-regexp-in-string
725 "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
726 (cond
727 ((string= s "\t") (org-odt-format-tabs))
728 ((> (length s) 1)
729 (org-odt-format-spaces (length s)))
730 (t " "))) line))
732 (defun org-odt-format-source-code-or-example-line (line)
733 (org-odt-format-stylized-paragraph 'src (org-odt-fill-tabs-and-spaces line)))
735 (defun org-odt-format-example (rtn)
736 (let ((lines (org-split-string rtn "[\r\n]")) out)
737 (while lines
738 (setq out
739 (concat out (org-odt-format-source-code-or-example-line
740 (org-html-protect (pop lines))))))
741 (concat "\n" out "\n")))
744 ;;;_ , character styles
745 (defun org-odt-remap-stylenames (style-name)
747 (cdr (assoc style-name '(("timestamp-wrapper" . "OrgTimestampWrapper")
748 ("timestamp" . "OrgTimestamp")
749 ("timestamp-kwd" . "OrgTimestampKeyword")
750 ("tag" . "OrgTag")
751 ("todo" . "OrgTodo")
752 ("done" . "OrgDone")
753 ("target" . "OrgTarget"))))
754 style-name))
757 (defun org-odt-format-fontify (text style &optional id)
758 (let* ((style-name
759 (cond
760 ((stringp style)
761 (org-odt-remap-stylenames style))
762 ((symbolp style)
763 (org-odt-get-style-name-for-entity 'character style))
764 ((listp style)
765 (assert (< 1 (length style)))
766 (let ((parent-style (pop style)))
767 (mapconcat (lambda (s)
768 ;; (assert (stringp s) t)
769 (org-odt-remap-stylenames s)) style "")
770 (org-odt-remap-stylenames parent-style)))
771 (t (error "Don't how to handle style %s" style)))))
772 (org-html-format-tags
773 '("<text:span text:style-name=\"%s\">" . "</text:span>")
774 text style-name)))
776 ;;;_ , link
777 (defun org-odt-relocate-relative-path (path dir)
778 (if (file-name-absolute-p path) path
779 (file-relative-name (expand-file-name path dir)
780 (expand-file-name "eyecandy" dir))))
782 (defun org-odt-format-inline-image (thefile)
783 (let* ((thelink (if (file-name-absolute-p thefile) thefile
784 (org-export-html-format-href
785 (org-odt-relocate-relative-path
786 thefile org-current-export-file))))
787 (href
788 (org-html-format-tags
789 '("<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\">" . "</draw:image>")
791 (if org-export-odt-embed-images
792 (org-odt-copy-image-file thefile) thelink))))
793 (org-export-odt-format-image thefile href)))
795 (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
796 descp)
797 "Make an HTML link.
798 OPT-PLIST is an options list.
799 TYPE is the device-type of the link (THIS://foo.html)
800 PATH is the path of the link (http://THIS#locationx)
801 FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
802 DESC is the link description, if any.
803 ATTR is a string of other attributes of the a element.
804 MAY-INLINE-P allows inlining it as an image."
806 (declare (special org-par-open))
807 (save-match-data
808 (let* ((may-inline-p
809 (and (member type-1 '("http" "https" "file"))
810 (org-html-should-inline-p path descp)
811 (not fragment)))
812 (type (if (equal type-1 "id") "file" type-1))
813 (filename path)
814 (thefile path))
816 (cond
817 ;; check for inlined images
818 ((and (member type '("file"))
819 (not fragment)
820 (org-file-image-p
821 filename org-odt-export-inline-image-extensions)
822 (or (eq t org-odt-export-inline-images)
823 (and org-odt-export-inline-images (not descp))))
825 ;; (when (and (string= type "file") (file-name-absolute-p path))
826 ;; (setq thefile (concat "file://" (expand-file-name path))))
827 ;; (setq thefile (org-export-html-format-href thefile))
828 ;; (org-export-html-format-image thefile)
829 (org-odt-format-inline-image thefile))
831 (when (string= type "file")
832 (setq thefile
833 (cond
834 ((file-name-absolute-p path)
835 (concat "file://" (expand-file-name path)))
836 (t (org-odt-relocate-relative-path
837 thefile org-current-export-file)))))
839 (when (and (member type '("" "http" "https" "file"))
840 fragment)
841 (setq thefile (concat thefile "#" fragment)))
843 (setq thefile (org-export-html-format-href thefile))
845 (when (not (member type '("" "file")))
846 (setq thefile (concat type ":" thefile)))
848 (org-parse-format
849 'LINK (org-export-html-format-desc desc) thefile attr))))))
852 ;;;_ , heading
853 (defun org-odt-format-heading (text level &optional id class)
854 (let* ((text (if id (org-odt-format-target text id) text)))
855 ;; FIXME: handle class?
857 (org-html-format-tags
858 '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
859 "</text:h>")
860 (org-odt-format-target text id) level level)))
864 ;;;_ , anchor
866 (defun org-odt-format-anchor (text name &optional class)
867 (org-odt-format-bookmark text name))
869 ;;;_ , target
870 ;; ("<text:bookmark text:name=\"%s\"/>" . "")
871 (defun org-odt-format-bookmark (text id)
872 (concat
873 (org-html-format-tags
874 '("<text:bookmark text:name=\"%s\">" . "</text:bookmark>") "" id) text))
876 ;; ("<text:bookmark-start text:name=\"%s\"/>" .
877 ;; "<text:bookmark-end text:name=\"%s\"/>")
881 (defun org-odt-format-target (text id)
882 (let ((name (concat org-export-odt-bookmark-prefix id)))
883 (concat
884 (format "<text:bookmark-start text:name=\"%s\"/>" name)
885 (concat (org-odt-format-bookmark "" id) text)
886 (format "<text:bookmark-end text:name=\"%s\"/>" name))))
889 ;;;_ , footnote reference
890 (defun org-odt-format-footnote (n def)
891 (let ((id (concat "fn" n))
892 (note-class "footnote")
893 (par-style "Footnote"))
894 (org-html-format-tags
895 '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
896 "</text:note>")
897 (concat
898 (org-html-format-tags
899 '("<text:note-citation>" . "</text:note-citation>")
901 (org-html-format-tags
902 '("<text:note-body>" . "</text:note-body>")
903 def))
904 id note-class)))
907 (defun org-odt-format-footnote-reference (n def refcnt)
908 (if (= refcnt 1)
909 (org-odt-format-footnote n def)
910 (org-odt-format-footnote-ref n)))
913 (defun org-odt-format-footnote-ref (n)
914 (let ((note-class "footnote")
915 (ref-format "text")
916 (ref-name (concat "fn" n)))
917 (org-html-format-tags
918 '("<text:span text:style-name=\"%s\">" . "</text:span>")
919 (org-html-format-tags
920 '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" . "</text:note-ref>")
921 n note-class ref-format ref-name)
922 "OrgSuperscript")))
925 ;;;_ , image
926 (defun org-odt-get-image-name (file-name)
927 (require 'sha1)
928 (file-relative-name
929 (expand-file-name
930 (concat (sha1 file-name) "." (file-name-extension file-name)) "Pictures")))
932 (defun org-export-odt-format-image (src href
933 ;; par-open
935 "Create image tag with source and attributes."
936 (save-match-data
938 (let (embed-as caption attr label attr-plist size width height)
940 (cond
941 ((string-match "^ltxpng/" src)
942 ;; FIXME: Anyway the latex src can be embedded as an
943 ;; annotation
945 ;; (org-find-text-property-in-string 'org-latex-src src)
946 (setq caption nil attr nil label nil embed-as 'character))
949 (setq caption (org-find-text-property-in-string 'org-caption src)
950 caption (and caption (org-export-html-format-desc caption))
951 attr (org-find-text-property-in-string 'org-attributes src)
952 label (org-find-text-property-in-string 'org-label src)
953 embed-as 'paragraph)))
955 (setq attr-plist (when attr (read attr)))
956 (setq size (org-odt-image-size-from-file
957 src (plist-get attr-plist :width)
958 (plist-get attr-plist :height)
959 (plist-get attr-plist :scale)))
961 (org-export-odt-do-format-image embed-as caption attr label
962 size href))))
965 (defun org-export-odt-do-format-image (embed-as caption attr label
966 size href)
967 "Create image tag with source and attributes."
968 (save-match-data
969 (let ((width (car size)) (height (cdr size))
970 (draw-frame-pair
971 '("<draw:frame draw:style-name=\"%s\"
972 text:anchor-type=\"%s\"
973 draw:z-index=\"%d\" %s>" . "</draw:frame>")))
974 (cond
975 ((and (not caption) (not label))
976 (let (style-name anchor-type)
977 (cond
978 ((eq embed-as 'paragraph)
979 (setq style-name "OrgGraphicsParagraph" anchor-type "paragraph"))
980 ((eq embed-as 'character)
981 (setq style-name "OrgGraphicsBaseline" anchor-type "as-char")))
982 (org-html-format-tags
983 draw-frame-pair href style-name anchor-type 0
984 (org-odt-image-attrs-from-size width height))))
986 (caption
987 (concat
988 ;; (when par-open (org-odt-close-par))
989 (org-html-format-tags
990 draw-frame-pair
991 (org-html-format-tags
992 '("<draw:text-box fo:min-height=\"%dcm\">" . "</draw:text-box>")
993 (org-odt-format-stylized-paragraph
994 'illustration
995 (concat
996 (let ((extra " style:rel-width=\"100%\" style:rel-height=\"scale\""))
997 (org-html-format-tags
998 draw-frame-pair href "OrgGraphicsParagraphContent" "paragraph" 2
999 (concat (org-odt-image-attrs-from-size width height) extra)))
1000 (let ((seqno label))
1001 (org-odt-format-entity-caption
1002 "Illustration" label seqno caption))))
1003 height)
1004 "OrgFrame" "paragraph" 1
1005 (org-odt-image-attrs-from-size width))
1007 ;; (when par-open (org-odt-open-par))
1008 ))))))
1011 (defun org-odt-copy-image-file (path &optional target-file)
1012 "Returns the internal name of the file"
1013 (let* ((image-type (file-name-extension path))
1014 (media-type (format "image/%s" image-type))
1015 (src-file (expand-file-name
1016 path (file-name-directory org-current-export-file)))
1017 (target-file (or target-file (org-odt-get-image-name src-file)))
1018 ;; FIXME
1019 (body-only nil))
1021 (unless body-only
1022 (message "Embedding %s as %s ..."
1023 (substring-no-properties path) target-file)
1024 (copy-file src-file target-file 'overwrite)
1025 (org-odt-update-manifest-file media-type target-file)
1026 (push target-file org-export-odt-file-list)) target-file))
1028 (defun org-odt-image-attrs-from-size (&optional width height)
1029 (concat
1030 (when width (format "svg:width=\"%0.2fcm\"" width))
1032 (when height (format "svg:height=\"%0.2fcm\"" height))))
1034 (defun org-odt-image-size-from-file (file &optional user-width
1035 user-height scale dpi)
1036 (unless (file-name-absolute-p file)
1037 (setq file (expand-file-name
1038 file (file-name-directory org-current-export-file))))
1040 (setq dpi (or dpi org-export-odt-pixels-per-inch))
1041 (let* ((size (image-size (create-image file) 'pixels))
1042 (width-in-pixels (car size)) (height-in-pixels (cdr size))
1043 (width-in-inches (/ width-in-pixels dpi))
1044 (height-in-inches (/ height-in-pixels dpi))
1045 (cms-per-inch 2.54)
1046 (width-in-cms (* cms-per-inch width-in-inches))
1047 (height-in-cms (* cms-per-inch height-in-inches))
1048 (width width-in-cms) (height height-in-cms))
1049 (cond (scale (setq width (* width scale) height (* height scale)))
1050 ((and user-height user-width)
1051 (setq width user-width height user-height))
1052 (user-height (setq width (* user-height (/ width height))
1053 height user-height))
1054 (user-width (setq height (* user-width (/ height width))
1055 width user-width))
1056 (t (ignore)))
1057 (cons width height)))
1059 (defun org-odt-format-entity-caption (entity label seqno caption)
1060 (concat
1061 entity " "
1062 (org-html-format-tags
1063 '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" . "</text:sequence>")
1064 (format "%s " seqno) label entity entity)
1065 " " caption))
1067 ;;;_. maintenance callbacks
1068 ;;;_ , init method
1070 (defun org-odt-init-outfile (filename)
1071 (let* ((outdir (make-temp-file org-export-odt-tmpdir-prefix t))
1072 (mimetype-file (expand-file-name "mimetype" outdir))
1073 (content-file (expand-file-name "content.xml" outdir))
1074 (manifest-file (expand-file-name "META-INF/manifest.xml" outdir))
1075 (meta-file (expand-file-name "meta.xml" outdir))
1076 (styles-file (expand-file-name "styles.xml" outdir))
1077 (pictures-dir (expand-file-name "Pictures" outdir))
1078 (body-only nil))
1080 ;; content file
1081 (with-current-buffer (find-file-noselect content-file) (erase-buffer))
1083 ;; FIXME: How to factor in body-only here
1084 (unless body-only
1085 ;; manifest file
1086 (make-directory (file-name-directory manifest-file))
1087 (with-current-buffer (find-file-noselect manifest-file)
1088 (erase-buffer)
1089 (insert (mapconcat 'identity (car org-export-odt-manifest-lines) "\n"))
1090 (insert "\n")
1091 (save-excursion
1092 (insert (mapconcat 'identity (cdr org-export-odt-manifest-lines) "\n"))))
1094 ;; meta file
1095 (with-current-buffer (find-file-noselect meta-file)
1096 (erase-buffer)
1097 (insert (mapconcat 'identity (car org-export-odt-meta-lines) "\n"))
1098 (insert "\n")
1099 (save-excursion
1100 (insert (mapconcat 'identity (cdr org-export-odt-meta-lines) "\n"))))
1102 ;; styles file
1103 ;; (copy-file org-export-odt-styles-file styles-file t)
1105 ;; Pictures dir
1106 (make-directory pictures-dir)
1108 ;; initialize list of files that contribute to the odt file
1109 (setq org-export-odt-file-list
1110 (append org-export-odt-save-list org-export-odt-nosave-list)))
1111 content-file))
1113 ;;;_ , save metod
1115 ;; xml files generated on-the-fly
1116 (defconst org-export-odt-save-list
1117 '("META-INF/manifest.xml" "content.xml" "meta.xml"))
1119 ;; xml files that are copied
1120 (defconst org-export-odt-nosave-list '("styles.xml"))
1122 ;; xml files that contribute to the final odt file
1123 (defvar org-export-odt-file-list nil)
1125 (defconst org-export-odt-manifest-lines
1126 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1127 "<manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\">"
1128 "<manifest:file-entry manifest:media-type=\"application/vnd.oasis.opendocument.text\" manifest:version=\"1.2\" manifest:full-path=\"/\"/>"
1129 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"content.xml\"/>"
1130 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"styles.xml\"/>"
1131 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"meta.xml\"/>"
1132 "<manifest:file-entry manifest:media-type=\"\" manifest:full-path=\"Pictures/\"/>") . ("</manifest:manifest>")))
1134 (defconst org-export-odt-meta-lines
1135 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1136 "<office:document-meta"
1137 " xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\""
1138 " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
1139 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\""
1140 " xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\""
1141 " xmlns:ooo=\"http://openoffice.org/2004/office\" "
1142 " office:version=\"1.2\">"
1143 " <office:meta>") . (" </office:meta>" "</office:document-meta>")))
1145 (defconst org-export-odt-mimetype-lines
1146 '("application/vnd.oasis.opendocument.text"))
1148 (defconst org-odt-manifest-file-entry-tag
1149 "<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"/>")
1151 (defun org-odt-save-as-outfile (target)
1152 (org-odt-copy-styles-file)
1153 (let ((zipdir default-directory))
1154 (message "Switching to directory %s" (expand-file-name zipdir))
1156 ;; save all xml files
1157 (mapc (lambda (file)
1158 (with-current-buffer (find-file-noselect (expand-file-name file))
1159 ;; prettify output
1160 (indent-region (point-min) (point-max)) (save-buffer)))
1161 org-export-odt-save-list)
1163 (let* ((target-name (file-name-nondirectory target))
1164 (target-dir (file-name-directory target))
1165 (cmd (format "zip -rmTq %s %s" target-name ".")))
1166 (when (file-exists-p target)
1167 ;; FIXME: If the file is locked this throws a cryptic error
1168 (delete-file target))
1170 (let ((coding-system-for-write 'no-conversion) exitcode)
1171 (message "Creating odt file using \"%s\"" cmd)
1172 (setq exitcode
1173 (apply 'call-process
1174 "zip"
1178 (append (list "-rmTq") (list target-name "."))))
1180 (or (zerop exitcode)
1181 (error "Unable to create odt file (%S)" exitcode)))
1183 ;; move the file from outdir to target-dir
1184 (rename-file target-name target-dir)
1186 ;; kill all xml buffers
1187 (mapc (lambda (file)
1188 (kill-buffer (find-file-noselect (expand-file-name file zipdir))))
1189 org-export-odt-save-list)
1191 (delete-directory zipdir)))
1193 ;; FIXME: How to open the file without visiting
1194 (find-file target))
1196 (defun org-odt-update-meta-file (opt-plist)
1197 (with-current-buffer
1198 (find-file-noselect "meta.xml" (file-name-directory buffer-file-name))
1199 (let ((date (or (plist-get opt-plist :effective-date) ""))
1200 (author (or (plist-get opt-plist :author) ""))
1201 (email (plist-get opt-plist :email))
1202 (keywords (plist-get opt-plist :keywords))
1203 (description (plist-get opt-plist :description))
1204 (title (plist-get opt-plist :title)))
1206 (insert
1207 "\n"
1208 (org-html-format-tags '("<dc:creator>" . "</dc:creator>") author)
1209 (org-html-format-tags
1210 '("\n<dc:initial-creator>" . "</dc:initial-creator>") author)
1211 (org-html-format-tags '("\n<dc:date>" . "</dc:date>") date)
1212 (org-html-format-tags
1213 '("\n<meta:creation-date>" . "</meta:creation-date>") date)
1214 (org-html-format-tags '("\n<meta:generator>" . "</meta:generator>")
1215 (when org-export-creator-info
1216 (format "Org-%s/Emacs-%s"
1217 org-version emacs-version)))
1218 (org-html-format-tags '("\n<meta:keyword>" . "</meta:keyword>") keywords)
1219 (org-html-format-tags '("\n<dc:subject>" . "</dc:subject>") description)
1220 (org-html-format-tags '("\n<dc:title>" . "</dc:title>") title)
1221 "\n"))))
1224 (defun org-odt-update-manifest-file (media-type full-path)
1225 (with-current-buffer
1226 (find-file-noselect "META-INF/manifest.xml")
1227 (insert (format org-odt-manifest-file-entry-tag media-type full-path))))
1230 ;;;_ , cleanup method
1232 (defun org-odt-finalize-outfile ()
1233 (message "org-newodt: Finalizing outfile")
1234 (org-odt-delete-empty-paragraphs))
1236 (defun org-odt-delete-empty-paragraphs ()
1237 (goto-char (point-min))
1238 (let ((open "<text:p[^>]*>")
1239 (close "</text:p>"))
1240 (while (re-search-forward (format "%s[ \r\n\t]*%s" open close) nil t)
1241 (replace-match ""))))
1243 ;;;_. get callback
1244 (defun org-odt-get (what &optional opt-plist)
1245 (case what
1246 (BACKEND 'odt)
1247 (EXPORT-DIR (org-export-directory :html opt-plist))
1248 (FILE-NAME-EXTENSION "odt")
1249 (EXPORT-BUFFER-NAME "*Org ODT Export*")
1250 (ENTITY-CONTROL org-odt-entity-control-callbacks-alist)
1251 (ENTITY-FORMAT org-odt-entity-format-callbacks-alist)
1252 (INIT-METHOD 'org-odt-init-outfile)
1253 (FINAL-METHOD 'org-odt-finalize-outfile)
1254 (SAVE-METHOD 'org-odt-save-as-outfile)
1255 (t (error "Unknown property: %s" what))))
1257 ;;;_ preprocessor
1259 (defun org-export-odt-format-source-code-or-example
1260 (lang code &optional opts indent caption)
1261 "Format CODE from language LANG and return it formatted for export.
1262 If LANG is nil, do not add any fontification.
1263 OPTS contains formatting options, like `-n' for triggering numbering lines,
1264 and `+n' for continuing previous numbering.
1265 Code formatting according to language currently only works for HTML.
1266 Numbering lines works for all three major backends (html, latex, and ascii).
1267 INDENT was the original indentation of the block."
1268 (save-match-data
1269 (let (num cont rtn rpllbl keepp textareap preserve-indentp cols rows fmt)
1270 (setq opts (or opts "")
1271 num (string-match "[-+]n\\>" opts)
1272 cont (string-match "\\+n\\>" opts)
1273 rpllbl (string-match "-r\\>" opts)
1274 keepp (string-match "-k\\>" opts)
1275 textareap (string-match "-t\\>" opts)
1276 preserve-indentp (or org-src-preserve-indentation
1277 (string-match "-i\\>" opts))
1278 cols (if (string-match "-w[ \t]+\\([0-9]+\\)" opts)
1279 (string-to-number (match-string 1 opts))
1281 rows (if (string-match "-h[ \t]+\\([0-9]+\\)" opts)
1282 (string-to-number (match-string 1 opts))
1283 (org-count-lines code))
1284 fmt (if (string-match "-l[ \t]+\"\\([^\"\n]+\\)\"" opts)
1285 (match-string 1 opts)))
1286 (if keepp (setq rpllbl 'keep))
1287 (setq rtn (if preserve-indentp code (org-remove-indentation code)))
1288 (when (string-match "^," rtn)
1289 (setq rtn (with-temp-buffer
1290 (insert rtn)
1291 ;; Free up the protected lines
1292 (goto-char (point-min))
1293 (while (re-search-forward "^," nil t)
1294 (if (or (equal lang "org")
1295 (save-match-data
1296 (looking-at "\\([*#]\\|[ \t]*#\\+\\)")))
1297 (replace-match ""))
1298 (end-of-line 1))
1299 (buffer-string))))
1301 (org-add-props
1302 (concat "\n#+BEGIN_ODT\n"
1303 (org-add-props
1304 (org-odt-format-example rtn)
1305 '(org-protected t org-example t))
1306 "\n#+END_ODT\n\n")
1307 nil 'original-indentation indent))))
1309 (defun org-export-odt-preprocess (parameters)
1310 "Convert LaTeX fragments to images."
1311 (when (and org-current-export-file
1312 (plist-get parameters :LaTeX-fragments))
1313 (org-format-latex
1314 (concat "ltxpng/" (file-name-sans-extension
1315 (file-name-nondirectory
1316 org-current-export-file)))
1317 org-current-export-dir nil "Creating LaTeX image %s"
1318 nil nil
1319 (cond
1320 ((eq (plist-get parameters :LaTeX-fragments) 'verbatim) 'verbatim)
1321 ((eq (plist-get parameters :LaTeX-fragments) 'mathjax ) 'mathjax)
1322 ((eq (plist-get parameters :LaTeX-fragments) t ) 'mathjax)
1323 ((eq (plist-get parameters :LaTeX-fragments) 'dvipng ) 'dvipng)
1324 (t nil))))
1325 (goto-char (point-min))
1326 (let (label l1)
1327 (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t)
1328 (org-if-unprotected-at (match-beginning 1)
1329 (setq label (match-string 1))
1330 (save-match-data
1331 (if (string-match "\\`[a-z]\\{1,10\\}:\\(.+\\)" label)
1332 (setq l1 (substring label (match-beginning 1)))
1333 (setq l1 label)))
1334 (replace-match
1335 (let ((org-html-protect t))
1336 (org-html-format-tags
1337 '("<text:sequence-ref text:reference-format=\"value\" text:ref-name=\"%s\">"
1338 . "</text:sequence-ref>")
1339 l1 label))
1340 t t)))))
1342 (defun org-odt-zip-extract-one (archive member &optional target)
1343 (let* ((target (or target default-directory))
1344 (archive (expand-file-name archive))
1345 (archive-zip-extract
1346 (list "unzip" "-qq" "-o" "-d" target))
1347 exit-code command-output)
1348 (setq command-output
1349 (with-temp-buffer
1350 (setq exit-code (archive-zip-extract archive member))
1351 (buffer-string)))
1352 (unless (zerop exit-code)
1353 (message command-output)
1354 (error "Extraction failed"))))
1356 (defun org-odt-zip-extract (archive members &optional target)
1357 (when (atom members) (setq members (list members)))
1358 (mapc (lambda (member)
1359 (org-odt-zip-extract-one archive member target))
1360 members))
1362 (defun org-odt-copy-styles-file (&optional styles-file)
1363 ;; Non-availability of styles.xml is not a critical error. For now
1364 ;; throw an error purely for aesthetic reasons.
1365 (setq styles-file (or styles-file
1366 org-export-odt-styles-file
1367 (error "org-odt: Missing styles file?")))
1368 (cond
1369 ((listp styles-file)
1370 (let ((archive (nth 0 styles-file))
1371 (members (nth 1 styles-file)))
1372 (org-odt-zip-extract archive members)
1373 (mapc
1374 (lambda (member)
1375 (when (org-file-image-p member)
1376 (let* ((image-type (file-name-extension member))
1377 (media-type (format "image/%s" image-type)))
1378 (org-odt-update-manifest-file media-type member))))
1379 members)))
1380 ((and (stringp styles-file) (file-exists-p styles-file))
1381 (setq styles-file-type (file-name-extension styles-file))
1382 (cond
1383 ((string= styles-file-type "xml")
1384 (copy-file styles-file "styles.xml" t))
1385 ((member styles-file-type '("odt" "ott"))
1386 (org-odt-zip-extract styles-file "styles.xml"))))
1388 (error (format "Invalid specification of styles.xml file: %S"
1389 org-export-odt-styles-file)))))
1391 ;;;_ postamble
1393 (provide 'org-odt)