Description lists were ill-formatted. Fix it.
[org-mode/org-jambu.git] / lisp / org-odt.el
blob3e1a5806d0c23bc855d48cd115b0434206f0c590
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-and-open "odt" arg))
188 ;;;###autoload
189 (defun org-export-as-odt-batch ()
190 "Call the function `org-export-as-odt'.
191 This function can be used in batch processing as:
192 emacs --batch
193 --load=$HOME/lib/emacs/org.el
194 --eval \"(setq org-export-headline-levels 2)\"
195 --visit=MyFile --funcall org-export-as-odt-batch"
196 (org-export-as-batch "odt"))
198 ;;;###autoload
199 (defun org-export-as-odt-to-buffer (arg)
200 "Call `org-export-as-odt` with output to a temporary buffer.
201 No file is created. The prefix ARG is passed through to `org-export-as-odt'."
202 (interactive "P")
203 (org-export-as-to-buffer "odt" arg))
205 ;;;###autoload
206 (defun org-replace-region-by-odt (beg end)
207 "Assume the current region has org-mode syntax, and convert it to ODT.
208 This can be used in any buffer. For example, you could write an
209 itemized list in org-mode syntax in an ODT buffer and then use this
210 command to convert it."
211 (interactive "r")
212 (org-replace-region-by "odt" beg end))
214 ;;;###autoload
215 (defun org-export-region-as-odt (beg end &optional body-only buffer)
216 "Convert region from BEG to END in org-mode buffer to ODT.
217 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
218 contents, and only produce the region of converted text, useful for
219 cut-and-paste operations.
220 If BUFFER is a buffer or a string, use/create that buffer as a target
221 of the converted ODT. If BUFFER is the symbol `string', return the
222 produced ODT as a string and leave not buffer behind. For example,
223 a Lisp program could call this function in the following way:
225 (setq odt (org-export-region-as-odt beg end t 'string))
227 When called interactively, the output buffer is selected, and shown
228 in a window. A non-interactive call will only return the buffer."
229 (interactive "r\nP")
230 (org-export-region-as "odt" beg end body-only buffer))
232 ;;; org-export-as-odt
233 ;;;###autoload
234 (defun org-export-as-odt (arg &optional hidden ext-plist
235 to-buffer body-only pub-dir)
236 "Export the outline as a OpenDocumentText file.
237 If there is an active region, export only the region. The prefix
238 ARG specifies how many levels of the outline should become
239 headlines. The default is 3. Lower levels will become bulleted
240 lists. HIDDEN is obsolete and does nothing.
241 EXT-PLIST is a property list with external parameters overriding
242 org-mode's default settings, but still inferior to file-local
243 settings. When TO-BUFFER is non-nil, create a buffer with that
244 name and export to that buffer. If TO-BUFFER is the symbol
245 `string', don't leave any buffer behind but just return the
246 resulting XML as a string. When BODY-ONLY is set, don't produce
247 the file header and footer, simply return the content of
248 <body>...</body>, without even the body tags themselves. When
249 PUB-DIR is set, use this as the publishing directory."
250 (interactive "P")
251 (org-export-as "odt" arg hidden ext-plist to-buffer body-only pub-dir))
253 ;;;_ parser
254 ;;;_. initialization
255 (defvar org-odt-entity-control-callbacks-alist
256 `((EXPORT
257 . (org-odt-begin-export org-odt-end-export))
258 (DOCUMENT-CONTENT
259 . (org-odt-begin-document-content org-odt-end-document-content))
260 (DOCUMENT-BODY
261 . (org-odt-begin-document-body org-odt-end-document-body))
262 (TOC
263 . (org-odt-begin-toc org-odt-end-toc))
264 (ENVIRONMENT
265 . (org-odt-begin-environment org-odt-end-environment))
266 (LEVEL
267 . (org-html-begin-level org-html-end-level))
268 (FOOTNOTE-DEFINITION
269 . (org-odt-begin-footnote-definition org-odt-end-footnote-definition))
270 (TABLE
271 . (org-odt-begin-table org-odt-end-table))
272 (TABLE-ROWGROUP
273 . (org-odt-begin-table-rowgroup org-odt-end-table-rowgroup))
274 (LIST
275 . (org-odt-begin-list org-odt-end-list))
276 (LIST-ITEM
277 . (org-odt-begin-list-item org-odt-end-list-item))
278 (SECTION
279 . (org-odt-begin-section org-odt-end-section))
280 (PARAGRAPH
281 . (org-odt-begin-paragraph org-odt-end-paragraph)))
284 (defvar org-odt-entity-format-callbacks-alist
285 `((EXTRA-TARGETS . org-html-format-extra-targets)
286 (ORG-TAGS . org-html-format-org-tags)
287 (SECTION-NUMBER . org-html-format-section-number)
288 (HEADLINE . org-odt-format-headline)
289 (TOC-ENTRY . org-odt-format-toc-entry)
290 (TOC-ITEM . org-odt-format-toc-item)
291 (TAGS . org-odt-format-tags)
292 (SPACES . org-odt-format-spaces)
293 (TABS . org-odt-format-tabs)
294 (LINE-BREAK . org-odt-format-line-break)
295 (FONTIFY . org-odt-format-fontify)
296 (TODO . org-html-format-todo)
297 (LINK . org-odt-format-link)
298 (INLINE-IMAGE . org-odt-format-inline-image)
299 (ORG-LINK . org-odt-format-org-link)
300 (HEADING . org-odt-format-heading)
301 (ANCHOR . org-odt-format-anchor)
302 (TABLE . org-parse-format-table)
303 (TABLE-ROW . org-odt-format-table-row)
304 (TABLE-CELL . org-odt-format-table-cell)
305 (FOOTNOTES-SECTION . ignore)
306 (FOOTNOTE-REFERENCE . org-odt-format-footnote-reference)
307 (HORIZONTAL-LINE . org-odt-format-horizontal-line)
308 (COMMENT . org-html-format-comment)
309 (PLAIN . org-odt-format-plain))
312 ;;;_. callbacks
313 ;;;_. control callbacks
314 ;;;_ , document body
315 (defun org-odt-begin-office-body ()
316 (insert "
317 <office:body>
318 <office:text>
319 <text:sequence-decls>
320 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Illustration\"/>
321 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Table\"/>
322 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Text\"/>
323 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Drawing\"/>
324 </text:sequence-decls>"))
327 ;; Following variable is let bound when `org-do-export' is in
328 ;; progress. See org-html.el.
329 (defvar org-parse-table-of-contents)
330 (defun org-odt-begin-document-body (opt-plist)
331 (org-odt-begin-office-body)
332 (let ((title (plist-get opt-plist :title)))
333 (when title
334 (insert
335 (org-odt-format-stylized-paragraph 'title title))))
337 ;; insert toc
338 (when org-parse-table-of-contents
339 (insert "\n" org-parse-table-of-contents "\n")))
341 (defvar org-parse-body-only) ; let bound during org-do-export
342 (defvar org-parse-to-buffer) ; let bound during org-do-export
343 (defun org-odt-end-document-body (opt-plist)
344 (unless org-parse-body-only
345 (org-html-insert-tag "</office:text>")
346 (org-html-insert-tag "</office:body>")))
348 (defconst org-odt-document-content-header
349 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
350 <office:document-content
351 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
352 xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\"
353 xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\"
354 xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\"
355 xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\"
356 xmlns:fo=\"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0\"
357 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
358 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
359 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
360 xmlns:number=\"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0\"
361 xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\"
362 xmlns:chart=\"urn:oasis:names:tc:opendocument:xmlns:chart:1.0\"
363 xmlns:dr3d=\"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0\"
364 xmlns:math=\"http://www.w3.org/1998/Math/MathML\"
365 xmlns:form=\"urn:oasis:names:tc:opendocument:xmlns:form:1.0\"
366 xmlns:script=\"urn:oasis:names:tc:opendocument:xmlns:script:1.0\"
367 xmlns:ooo=\"http://openoffice.org/2004/office\"
368 xmlns:ooow=\"http://openoffice.org/2004/writer\"
369 xmlns:oooc=\"http://openoffice.org/2004/calc\"
370 xmlns:dom=\"http://www.w3.org/2001/xml-events\"
371 xmlns:xforms=\"http://www.w3.org/2002/xforms\"
372 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
373 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
374 xmlns:rpt=\"http://openoffice.org/2005/report\"
375 xmlns:of=\"urn:oasis:names:tc:opendocument:xmlns:of:1.2\"
376 xmlns:xodt=\"http://www.w3.org/1999/xodt\"
377 xmlns:field=\"urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0\" office:version=\"1.2\">
382 ;;;_ , document content
383 (defun org-odt-begin-document-content (opt-plist)
384 ;; document header
385 (insert org-odt-document-content-header)
387 ;; automatic styles
388 (insert-file-contents
389 (or org-export-odt-automatic-styles-file
390 ;; use a sensible default
391 ;; FIXME: The code below assumes a specific dir layout for
392 ;; locating the automatic styles file
393 (expand-file-name
394 "../contrib/odt/OrgOdtAutomaticStyles.xml"
395 (file-name-directory (locate-library "org-odt")))))
396 (goto-char (point-max)))
398 (defun org-odt-end-document-content ()
399 (org-html-insert-tag "</office:document-content>"))
402 ;;;_ , level
403 ;;;_ , section
404 (defun org-odt-begin-section (&optional id class
405 extra-class ; ignored for now
407 (let* ((extra
408 (concat
409 (when id (format " text:name=\"%s\"" id))
410 (when class
411 (format " text:style-name=\"%s\"" class)))))
412 (org-html-insert-tag "<text:section%s>" extra)))
414 (defun org-odt-end-section ()
415 (org-html-insert-tag "</text:section>"))
418 ;;;_ , heading
419 ;;;_ , paragraph
420 (defun org-odt-begin-paragraph (&optional style)
421 (org-html-insert-tag
422 "<text:p%s>" (org-odt-get-extra-attrs-for-paragraph-style style)))
424 (defun org-odt-end-paragraph ()
425 (org-html-insert-tag "</text:p>"))
428 (defun org-odt-get-extra-attrs-for-paragraph-style (style)
429 (let (style-name)
430 (setq style-name
431 (cond
432 ((stringp style) style)
433 ((symbolp style) (org-odt-get-style-name-for-entity
434 'paragraph style))))
435 (unless style-name
436 (error "Don't know how to handle paragraph style %s" style))
437 (format " text:style-name=\"%s\"" style-name)))
439 (defun org-odt-format-stylized-paragraph (style text)
440 (org-html-format-tags
441 '("<text:p%s>" . "</text:p>") text
442 (org-odt-get-extra-attrs-for-paragraph-style style)))
445 ;;;_ , environment
446 (defun org-odt-begin-environment (style)
447 (case style
448 ((blockquote verse center quote)
449 (org-parse-begin-paragraph style)
450 (list))
451 ((fixedwidth native)
452 (org-parse-end-paragraph)
453 (list))
454 (t (error "Unknown environment %s" style))))
456 (defun org-odt-end-environment (style)
457 (case style
458 ((blockquote verse center quote)
459 (org-parse-end-paragraph)
460 (list))
461 ((fixedwidth native)
462 (org-parse-begin-paragraph)
463 (list))
464 (t (error "Unknown environment %s" style))))
467 ;;;_ , list
468 (defun org-odt-begin-list (ltype &optional arg1)
469 (setq ltype (or (org-html-html-list-type-to-canonical-list-type ltype)
470 ltype))
471 (let* ((style-name (org-odt-get-style-name-for-entity 'list ltype))
472 (extra (if style-name
473 (format " text:style-name=\"%s\"" style-name) "")))
475 ;; FIXME: Handle arg1 incase of ordered lists.
476 (case ltype
477 ((ordered unordered description)
478 (org-parse-end-paragraph)
479 (org-html-insert-tag "<text:list%s>" extra))
480 (t (error "Unknown list type: %s" ltype)))))
482 (defun org-odt-end-list (ltype)
483 (setq ltype (or (org-html-html-list-type-to-canonical-list-type ltype)
484 ltype))
485 (if ltype
486 (org-html-insert-tag "</text:list>")
487 (error "Unknown list type: %s" ltype)))
489 (defun org-odt-begin-list-item (ltype &optional arg headline)
490 (setq ltype (or (org-html-html-list-type-to-canonical-list-type ltype)
491 ltype))
492 (case ltype
493 (ordered
494 (assert (not headline) t)
495 (let* ((counter arg) (extra ""))
496 (org-html-insert-tag "<text:list-item>")
497 (org-parse-begin-paragraph)))
498 (unordered
499 (let* ((id arg) (extra ""))
500 (org-html-insert-tag "<text:list-item>")
501 (org-parse-begin-paragraph)
502 (insert (if headline (org-odt-format-target headline id)
503 (org-odt-format-bookmark "" id)))))
504 (description
505 (assert (not headline) t)
506 (let ((term (or arg "(no term)")))
507 (insert
508 (org-html-format-tags
509 '("<text:list-item>" . "</text:list-item>")
510 (org-odt-format-stylized-paragraph 'definition-term term)))
511 (org-parse-begin 'LIST-ITEM 'unordered)
512 (org-parse-begin 'LIST 'description)
513 (org-parse-begin 'LIST-ITEM 'unordered)))
514 (t (error "Unknown list type"))))
516 (defun org-odt-end-list-item (ltype)
517 (setq ltype (or (org-html-html-list-type-to-canonical-list-type ltype)
518 ltype))
519 (case ltype
520 ((ordered unordered)
521 (org-html-insert-tag "</text:list-item>"))
522 (description
523 (org-close-li)
524 (org-parse-end 'LIST 'description)
525 (org-close-li))
526 (t (error "Unknown list type"))))
528 ;;;_ , table
530 ;; Following variables are let bound when table emission is in
531 ;; progress. See org-html.el.
532 (defvar org-table-begin-marker)
533 (defvar org-table-ncols)
534 (defvar org-table-rowgroup-open)
535 (defvar org-table-rownum)
536 (defvar org-table-current-rowgroup-is-header)
537 (defvar org-table-is-styled)
538 (defvar org-table-rowgroup-info)
539 (defvar org-table-colalign-vector)
540 (defun org-odt-begin-table (caption label attributes)
541 (when label
542 (insert
543 (org-odt-format-stylized-paragraph
544 'table (org-odt-format-entity-caption label caption "Table"))))
546 (org-html-insert-tag
547 "<table:table table:name=\"%s\" table:style-name=\"%s\">"
548 (or label "") "OrgTable")
549 (setq org-table-begin-marker (point)))
551 (defun org-odt-end-table ()
552 (goto-char org-table-begin-marker)
553 (loop for level from 0 below org-table-ncols
554 do (insert (org-html-format-tags "<table:table-column/>" "")))
556 ;; fill style attributes for table cells
557 (when org-table-is-styled
558 (while (re-search-forward "@@\\(table-cell:p\\|table-cell:style-name\\)@@\\([0-9]+\\)@@\\([0-9]+\\)@@" nil t)
559 (let ((spec (match-string 1))
560 (r (string-to-number (match-string 2)))
561 (c (string-to-number (match-string 3))))
562 (cond
563 ((equal spec "table-cell:p")
564 (let ((style-name (org-odt-get-paragraph-style-for-table-cell r c)))
565 (replace-match style-name t t)))
566 ((equal spec "table-cell:style-name")
567 (let ((style-name (org-odt-get-style-name-for-table-cell r c)))
568 (replace-match style-name t t)))))))
570 (goto-char (point-max))
571 (org-html-insert-tag "</table:table>"))
573 (defun org-odt-begin-table-rowgroup (&optional is-header-row)
574 (when org-table-rowgroup-open
575 (org-parse-end 'TABLE-ROWGROUP))
576 (org-html-insert-tag (if is-header-row
577 "<table:table-header-rows>"
578 "<table:table-rows>"))
579 (setq org-table-rowgroup-open t)
580 (setq org-table-current-rowgroup-is-header is-header-row))
582 (defun org-odt-end-table-rowgroup ()
583 (when org-table-rowgroup-open
584 (setq org-table-rowgroup-open nil)
585 (org-html-insert-tag
586 (if org-table-current-rowgroup-is-header
587 "</table:table-header-rows>" "</table:table-rows>"))))
589 (defun org-odt-format-table-row (row)
590 (org-html-format-tags
591 '("<table:table-row>" . "</table:table-row>") row))
593 (defun org-odt-get-style-name-for-table-cell (r c)
594 (concat
595 "OrgTblCell"
596 (cond
597 ((= r 0) "T")
598 ((eq (cdr (assoc r org-table-rowgroup-info)) :start) "T")
599 (t ""))
600 (when (= r org-table-rownum) "B")
601 (cond
602 ((= c 0) "")
603 ((or (memq (nth c org-table-colgroup-info) '(:start :startend))
604 (memq (nth (1- c) org-table-colgroup-info) '(:end :startend))) "L")
605 (t ""))))
607 (defun org-odt-get-paragraph-style-for-table-cell (r c)
608 (capitalize (aref org-table-colalign-vector c)))
610 (defun org-odt-format-table-cell (data r c)
611 (if (not org-table-is-styled)
612 (org-html-format-tags
613 '("<table:table-cell>" . "</table:table-cell>")
614 (org-odt-format-stylized-paragraph
615 (cond
616 (org-table-current-rowgroup-is-header "OrgTableHeading")
617 ((and (= c 0) org-export-html-table-use-header-tags-for-first-column)
618 "OrgTableHeading")
619 (t "OrgTableContents"))
620 data))
621 (let* ((style-name-cookie
622 (format "@@table-cell:style-name@@%03d@@%03d@@" r c))
623 (paragraph-style-cookie
624 (concat
625 (cond
626 (org-table-current-rowgroup-is-header "OrgTableHeading")
627 ((and (= c 0) org-export-html-table-use-header-tags-for-first-column)
628 "OrgTableHeading")
629 (t "OrgTableContents"))
630 (format "@@table-cell:p@@%03d@@%03d@@" r c))))
631 (org-html-format-tags
632 '("<table:table-cell table:style-name=\"%s\">" .
633 "</table:table-cell>")
634 (org-odt-format-stylized-paragraph paragraph-style-cookie data)
635 style-name-cookie))))
638 ;;;_ , footnote definition
639 (defun org-odt-begin-footnote-definition (n)
640 (org-parse-begin-paragraph 'footnote))
642 (defun org-odt-end-footnote-definition (n)
643 (org-parse-end-paragraph))
646 ;;;_. format callbacks
648 (defun org-odt-begin-toc (lang-specific-heading)
649 (insert
650 (format "
651 <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
652 <text:table-of-content-source text:outline-level=\"10\">
653 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
654 " lang-specific-heading))
656 (loop for level from 1 upto 10
657 do (insert (format
659 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
660 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
661 <text:index-entry-chapter/>
662 <text:index-entry-text/>
663 <text:index-entry-link-end/>
664 </text:table-of-content-entry-template>
665 " level level)))
667 (insert "
668 </text:table-of-content-source>
670 <text:index-body>
671 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
672 <text:p text:style-name=\"Contents_20_Heading\">Table of Contents</text:p>
673 </text:index-title>
676 (defun org-odt-end-toc ()
677 (insert "
678 </text:index-body>
679 </text:table-of-content>
682 (defun org-odt-format-toc-entry (snumber todo headline tags href)
683 (setq headline (concat
684 (and org-export-with-section-numbers
685 (concat snumber ". "))
686 headline
687 (and tags
688 (concat
689 (org-parse-format 'SPACES 3)
690 (org-parse-format 'FONTIFY tags "tag")))))
691 (when todo
692 (setq headline (org-parse-format 'FONTIFY headline "todo")))
694 (let ((org-odt-suppress-xref t))
695 (org-odt-format-link headline (concat "#" href))))
697 (defun org-odt-format-toc-item (toc-entry level org-last-level)
698 (let ((style (format "Contents_20_%d"
699 (+ level (or (org-parse-get 'TOPLEVEL-HLEVEL) 1) -1))))
700 (insert "\n" (org-odt-format-stylized-paragraph style toc-entry) "\n")))
702 ;; Following variable is let bound during 'ORG-LINK callback. See
703 ;; org-html.el
704 (defvar org-parse-link-description-is-image nil)
705 (defun org-odt-format-link (desc href &optional attr)
706 (cond
707 ((and (= (string-to-char href) ?#) (not org-odt-suppress-xref))
708 (setq href (concat org-export-odt-bookmark-prefix (substring href 1)))
709 (org-html-format-tags
710 '("<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"%s\">" .
711 "</text:bookmark-ref>")
712 desc href))
713 (org-parse-link-description-is-image
714 (org-html-format-tags
715 '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</draw:a>")
716 desc href (or attr "")))
718 (org-html-format-tags
719 '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</text:a>")
720 desc href (or attr "")))))
722 ;;;_ , spaces
723 (defun org-odt-format-spaces (n)
724 (org-html-format-tags "<text:s text:c=\"%d\"/>" "" n))
726 ;;;_ , tabs
727 (defun org-odt-format-tabs (&optional n)
728 (let ((tab "<text:tab/>")
729 (n (or n 1)))
730 (insert tab)))
732 ;;;_ , line break
733 (defun org-odt-format-line-break ()
734 (org-html-format-tags "<text:line-break/>" ""))
736 ;;;_ , horizontal line
737 (defun org-odt-format-horizontal-line ()
738 (org-odt-format-stylized-paragraph 'horizontal-line ""))
740 ;;;_ , line
741 (defun org-odt-format-plain (line)
742 (case org-parse-dyn-current-environment
743 (fixedwidth (concat (org-odt-format-source-code-or-example-line
744 (org-html-protect line)) "\n"))
745 (t (concat line "\n"))))
747 (defun org-odt-fill-tabs-and-spaces (line)
748 (replace-regexp-in-string
749 "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
750 (cond
751 ((string= s "\t") (org-odt-format-tabs))
752 ((> (length s) 1)
753 (org-odt-format-spaces (length s)))
754 (t " "))) line))
756 (defun org-odt-format-source-code-or-example-line (line)
757 (org-odt-format-stylized-paragraph 'src (org-odt-fill-tabs-and-spaces line)))
759 (defun org-odt-format-example (lines)
760 (mapconcat
761 (lambda (line)
762 (org-odt-format-source-code-or-example-line line))
763 (org-split-string lines "[\r\n]") "\n"))
765 (defun org-html-protect-lines (rtn)
766 (mapconcat
767 (lambda (line) (org-html-protect line))
768 (org-split-string rtn "[\r\n]") "\n"))
770 ;;;_ , character styles
771 (defun org-odt-remap-stylenames (style-name)
773 (cdr (assoc style-name '(("timestamp-wrapper" . "OrgTimestampWrapper")
774 ("timestamp" . "OrgTimestamp")
775 ("timestamp-kwd" . "OrgTimestampKeyword")
776 ("tag" . "OrgTag")
777 ("todo" . "OrgTodo")
778 ("done" . "OrgDone")
779 ("target" . "OrgTarget"))))
780 style-name))
783 (defun org-odt-format-fontify (text style &optional id)
784 (let* ((style-name
785 (cond
786 ((stringp style)
787 (org-odt-remap-stylenames style))
788 ((symbolp style)
789 (org-odt-get-style-name-for-entity 'character style))
790 ((listp style)
791 (assert (< 1 (length style)))
792 (let ((parent-style (pop style)))
793 (mapconcat (lambda (s)
794 ;; (assert (stringp s) t)
795 (org-odt-remap-stylenames s)) style "")
796 (org-odt-remap-stylenames parent-style)))
797 (t (error "Don't how to handle style %s" style)))))
798 (org-html-format-tags
799 '("<text:span text:style-name=\"%s\">" . "</text:span>")
800 text style-name)))
802 ;;;_ , link
803 (defun org-odt-relocate-relative-path (path dir)
804 (if (file-name-absolute-p path) path
805 (file-relative-name (expand-file-name path dir)
806 (expand-file-name "eyecandy" dir))))
808 (defun org-odt-format-inline-image (thefile)
809 (let* ((thelink (if (file-name-absolute-p thefile) thefile
810 (org-export-html-format-href
811 (org-odt-relocate-relative-path
812 thefile org-current-export-file))))
813 (href
814 (org-html-format-tags
815 "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
816 (if org-export-odt-embed-images
817 (org-odt-copy-image-file thefile) thelink))))
818 (org-export-odt-format-image thefile href)))
820 (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
821 descp)
822 "Make an HTML link.
823 OPT-PLIST is an options list.
824 TYPE is the device-type of the link (THIS://foo.html)
825 PATH is the path of the link (http://THIS#locationx)
826 FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
827 DESC is the link description, if any.
828 ATTR is a string of other attributes of the a element.
829 MAY-INLINE-P allows inlining it as an image."
831 (declare (special org-par-open))
832 (save-match-data
833 (let* ((may-inline-p
834 (and (member type-1 '("http" "https" "file"))
835 (org-html-should-inline-p path descp)
836 (not fragment)))
837 (type (if (equal type-1 "id") "file" type-1))
838 (filename path)
839 (thefile path))
841 (cond
842 ;; check for inlined images
843 ((and (member type '("file"))
844 (not fragment)
845 (org-file-image-p
846 filename org-odt-export-inline-image-extensions)
847 (or (eq t org-odt-export-inline-images)
848 (and org-odt-export-inline-images (not descp))))
850 ;; (when (and (string= type "file") (file-name-absolute-p path))
851 ;; (setq thefile (concat "file://" (expand-file-name path))))
852 ;; (setq thefile (org-export-html-format-href thefile))
853 ;; (org-export-html-format-image thefile)
854 (org-odt-format-inline-image thefile))
856 (when (string= type "file")
857 (setq thefile
858 (cond
859 ((file-name-absolute-p path)
860 (concat "file://" (expand-file-name path)))
861 (t (org-odt-relocate-relative-path
862 thefile org-current-export-file)))))
864 (when (and (member type '("" "http" "https" "file" "coderef"))
865 fragment)
866 (setq thefile (concat thefile "#" fragment)))
868 (setq thefile (org-export-html-format-href thefile))
870 (when (not (member type '("" "file" "coderef")))
871 (setq thefile (concat type ":" thefile)))
873 (let ((org-odt-suppress-xref (string= type "coderef")))
874 (org-odt-format-link
875 (org-export-html-format-desc desc) thefile attr)))))))
878 ;;;_ , heading
879 (defun org-odt-format-heading (text level &optional id)
880 (let* ((text (if id (org-odt-format-target text id) text)))
881 (org-html-format-tags
882 '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
883 "</text:h>") text level level)))
885 ;;;_ , headline
887 (defun org-odt-format-headline (title extra-targets tags
888 &optional snumber level)
889 (concat
890 (org-parse-format 'EXTRA-TARGETS extra-targets)
892 ;; No need to generate section numbers. They are auto-generated by
893 ;; the application
895 ;; (concat (org-parse-format 'SECTION-NUMBER snumber level) " ")
896 title
897 (and tags (concat (org-parse-format 'SPACES 3)
898 (org-parse-format 'ORG-TAGS tags)))))
900 ;;;_ , anchor
902 (defun org-odt-format-anchor (text name &optional class)
903 (org-odt-format-target text name))
905 ;;;_ , target
906 (defun org-odt-format-bookmark (text id)
907 (if id
908 (org-html-format-tags "<text:bookmark text:name=\"%s\"/>" text id)
909 text))
911 (defun org-odt-format-target (text id)
912 (let ((name (concat org-export-odt-bookmark-prefix id)))
913 (concat
914 (and id (org-html-format-tags
915 "<text:bookmark-start text:name=\"%s\"/>" "" name))
916 (org-odt-format-bookmark text id)
917 (and id (org-html-format-tags
918 "<text:bookmark-end text:name=\"%s\"/>" "" name)))))
920 ;;;_ , footnote reference
921 (defun org-odt-format-footnote (n def)
922 (let ((id (concat "fn" n))
923 (note-class "footnote")
924 (par-style "Footnote"))
925 (org-html-format-tags
926 '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
927 "</text:note>")
928 (concat
929 (org-html-format-tags
930 '("<text:note-citation>" . "</text:note-citation>")
932 (org-html-format-tags
933 '("<text:note-body>" . "</text:note-body>")
934 def))
935 id note-class)))
938 (defun org-odt-format-footnote-reference (n def refcnt)
939 (if (= refcnt 1)
940 (org-odt-format-footnote n def)
941 (org-odt-format-footnote-ref n)))
944 (defun org-odt-format-footnote-ref (n)
945 (let ((note-class "footnote")
946 (ref-format "text")
947 (ref-name (concat "fn" n)))
948 (org-html-format-tags
949 '("<text:span text:style-name=\"%s\">" . "</text:span>")
950 (org-html-format-tags
951 '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" . "</text:note-ref>")
952 n note-class ref-format ref-name)
953 "OrgSuperscript")))
956 ;;;_ , image
957 (defun org-odt-get-image-name (file-name)
958 (require 'sha1)
959 (file-relative-name
960 (expand-file-name
961 (concat (sha1 file-name) "." (file-name-extension file-name)) "Pictures")))
963 (defun org-export-odt-format-image (src href
964 ;; par-open
966 "Create image tag with source and attributes."
967 (save-match-data
969 (let (embed-as caption attr label attr-plist size width height)
971 (cond
972 ((string-match "^ltxpng/" src)
973 ;; FIXME: Anyway the latex src can be embedded as an
974 ;; annotation
976 ;; (org-find-text-property-in-string 'org-latex-src src)
977 (setq caption nil attr nil label nil embed-as 'character))
980 (setq caption (org-find-text-property-in-string 'org-caption src)
981 caption (and caption (org-export-html-format-desc caption))
982 attr (org-find-text-property-in-string 'org-attributes src)
983 label (org-find-text-property-in-string 'org-label src)
984 embed-as 'paragraph)))
986 (setq attr-plist (when attr (read attr)))
987 (setq size (org-odt-image-size-from-file
988 src (plist-get attr-plist :width)
989 (plist-get attr-plist :height)
990 (plist-get attr-plist :scale) nil embed-as))
992 (org-export-odt-do-format-image embed-as caption attr label
993 size href))))
996 (defun org-export-odt-do-format-image (embed-as caption attr label
997 size href)
998 "Create image tag with source and attributes."
999 (save-match-data
1000 (let ((width (car size)) (height (cdr size))
1001 (draw-frame-pair
1002 '("<draw:frame draw:style-name=\"%s\"
1003 text:anchor-type=\"%s\"
1004 draw:z-index=\"%d\" %s>" . "</draw:frame>")))
1005 (cond
1006 ((and (not caption) (not label))
1007 (let (style-name anchor-type)
1008 (cond
1009 ((eq embed-as 'paragraph)
1010 (setq style-name "OrgGraphicsParagraph" anchor-type "paragraph"))
1011 ((eq embed-as 'character)
1012 (setq style-name "OrgGraphicsBaseline" anchor-type "as-char")))
1013 (org-html-format-tags
1014 draw-frame-pair href style-name anchor-type 0
1015 (org-odt-image-attrs-from-size width height))))
1018 (concat
1019 ;; (when par-open (org-odt-close-par))
1020 (org-html-format-tags
1021 draw-frame-pair
1022 (org-html-format-tags
1023 '("<draw:text-box fo:min-height=\"%dcm\">" . "</draw:text-box>")
1024 (org-odt-format-stylized-paragraph
1025 'illustration
1026 (concat
1027 (let ((extra " style:rel-width=\"100%\" style:rel-height=\"scale\""))
1028 (org-html-format-tags
1029 draw-frame-pair href "OrgGraphicsParagraphContent" "paragraph" 2
1030 (concat (org-odt-image-attrs-from-size width height) extra)))
1031 (org-odt-format-entity-caption label caption)))
1032 height)
1033 "OrgFrame" "paragraph" 1
1034 (org-odt-image-attrs-from-size width))
1036 ;; (when par-open (org-odt-open-par))
1037 ))))))
1039 ;; xml files generated on-the-fly
1040 (defconst org-export-odt-save-list
1041 '("META-INF/manifest.xml" "content.xml" "meta.xml"))
1043 ;; xml files that are copied
1044 (defconst org-export-odt-nosave-list '("styles.xml"))
1046 ;; xml files that contribute to the final odt file
1047 (defvar org-export-odt-file-list nil)
1049 (defconst org-export-odt-manifest-lines
1050 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1051 "<manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">"
1052 "<manifest:file-entry manifest:media-type=\"application/vnd.oasis.opendocument.text\" manifest:version=\"1.2\" manifest:full-path=\"/\"/>"
1053 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"content.xml\"/>"
1054 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"styles.xml\"/>"
1055 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"meta.xml\"/>"
1056 "<manifest:file-entry manifest:media-type=\"\" manifest:full-path=\"Pictures/\"/>") . ("</manifest:manifest>")))
1058 (defconst org-export-odt-meta-lines
1059 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1060 "<office:document-meta"
1061 " xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\""
1062 " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
1063 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\""
1064 " xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\""
1065 " xmlns:ooo=\"http://openoffice.org/2004/office\" "
1066 " office:version=\"1.2\">"
1067 " <office:meta>") . (" </office:meta>" "</office:document-meta>")))
1069 (defun org-odt-copy-image-file (path &optional target-file)
1070 "Returns the internal name of the file"
1071 (let* ((image-type (file-name-extension path))
1072 (media-type (format "image/%s" image-type))
1073 (src-file (expand-file-name
1074 path (file-name-directory org-current-export-file)))
1075 (target-file (or target-file (org-odt-get-image-name src-file)))
1076 ;; FIXME
1077 (body-only nil))
1079 (when (not org-parse-to-buffer)
1080 (message "Embedding %s as %s ..."
1081 (substring-no-properties path) target-file)
1082 (copy-file src-file target-file 'overwrite)
1083 (org-odt-update-manifest-file media-type target-file)
1084 (push target-file org-export-odt-file-list)) target-file))
1086 (defun org-odt-image-attrs-from-size (&optional width height)
1087 (concat
1088 (when width (format "svg:width=\"%0.2fcm\"" width))
1090 (when height (format "svg:height=\"%0.2fcm\"" height))))
1092 (defvar org-export-odt-image-size-probe-method
1093 '(emacs imagemagick force)
1094 "Ordered list of methods by for determining size of an embedded
1095 image.")
1097 (defvar org-export-odt-default-image-sizes-alist
1098 '(("character" . (5 . 0.4))
1099 ("paragraph" . (5 . 5)))
1100 "Hardcoded image dimensions one for each of the anchor
1101 methods.")
1103 (defun org-odt-do-image-size (probe-method file &optional dpi anchor-type)
1104 (setq dpi (or dpi org-export-odt-pixels-per-inch))
1105 (setq anchor-type (or anchor-type "paragraph"))
1106 (flet ((size-in-cms (size-in-pixels)
1107 (flet ((pixels-to-cms (pixels)
1108 (let* ((cms-per-inch 2.54)
1109 (inches (/ pixels dpi)))
1110 (* cms-per-inch inches))))
1111 (and size-in-pixels
1112 (cons (pixels-to-cms (car size-in-pixels))
1113 (pixels-to-cms (cdr size-in-pixels)))))))
1114 (case probe-method
1115 (emacs
1116 (size-in-cms (ignore-errors (image-size (create-image file) 'pixels))))
1117 (imagemagick
1118 (size-in-cms
1119 (let ((dim (shell-command-to-string
1120 (format "identify -format \"%%w:%%h\" \"%s\"" file))))
1121 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
1122 (cons (string-to-number (match-string 1 dim))
1123 (string-to-number (match-string 2 dim)))))))
1125 (cdr (assoc-string anchor-type
1126 org-export-odt-default-image-sizes-alist))))))
1128 (defun org-odt-image-size-from-file (file &optional user-width
1129 user-height scale dpi embed-as)
1130 (unless (file-name-absolute-p file)
1131 (setq file (expand-file-name
1132 file (file-name-directory org-current-export-file))))
1133 (let* (size width height)
1134 (unless (and user-height user-width)
1135 (loop for probe-method in org-export-odt-image-size-probe-method
1136 until size
1137 do (setq size (org-odt-do-image-size
1138 probe-method file dpi embed-as)))
1139 (or size (error "Cannot determine Image size. Aborting ..."))
1140 (setq width (car size) height (cdr size)))
1141 (cond
1142 (scale
1143 (setq width (* width scale) height (* height scale)))
1144 ((and user-height user-width)
1145 (setq width user-width height user-height))
1146 (user-height
1147 (setq width (* user-height (/ width height)) height user-height))
1148 (user-width
1149 (setq height (* user-width (/ height width)) width user-width))
1150 (t (ignore)))
1151 (cons width height)))
1153 (defvar org-odt-default-entity "Illustration")
1154 (defun org-odt-format-entity-caption (label caption &optional default-entity)
1155 (if (not label) (or caption "")
1156 (let* ((label-components (org-odt-parse-label label))
1157 (entity (car label-components))
1158 (seqno (cdr label-components))
1159 (caption (and caption (concat ": " caption))))
1160 (unless seqno
1161 (setq seqno label
1162 entity (or default-entity org-odt-default-entity)))
1163 (concat
1164 entity " "
1165 (org-html-format-tags
1166 '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" . "</text:sequence>")
1167 seqno label entity entity)
1168 caption))))
1170 ;;;_. maintenance callbacks
1171 ;;;_ , init method
1173 (defun org-odt-init-outfile (filename)
1174 (let* ((outdir (make-temp-file org-export-odt-tmpdir-prefix t))
1175 (mimetype-file (expand-file-name "mimetype" outdir))
1176 (content-file (expand-file-name "content.xml" outdir))
1177 (manifest-file (expand-file-name "META-INF/manifest.xml" outdir))
1178 (meta-file (expand-file-name "meta.xml" outdir))
1179 (styles-file (expand-file-name "styles.xml" outdir))
1180 (pictures-dir (expand-file-name "Pictures" outdir))
1181 (body-only nil))
1183 ;; content file
1184 (with-current-buffer (find-file-noselect content-file) (erase-buffer))
1186 ;; FIXME: How to factor in body-only here
1187 (unless body-only
1188 ;; manifest file
1189 (make-directory (file-name-directory manifest-file))
1190 (with-current-buffer (find-file-noselect manifest-file)
1191 (erase-buffer)
1192 (insert (mapconcat 'identity (car org-export-odt-manifest-lines) "\n"))
1193 (insert "\n")
1194 (save-excursion
1195 (insert (mapconcat 'identity (cdr org-export-odt-manifest-lines) "\n"))))
1197 ;; meta file
1198 (with-current-buffer (find-file-noselect meta-file)
1199 (erase-buffer)
1200 (insert (mapconcat 'identity (car org-export-odt-meta-lines) "\n"))
1201 (insert "\n")
1202 (save-excursion
1203 (insert (mapconcat 'identity (cdr org-export-odt-meta-lines) "\n"))))
1205 ;; styles file
1206 ;; (copy-file org-export-odt-styles-file styles-file t)
1208 ;; Pictures dir
1209 (make-directory pictures-dir)
1211 ;; initialize list of files that contribute to the odt file
1212 (setq org-export-odt-file-list
1213 (append org-export-odt-save-list org-export-odt-nosave-list)))
1214 content-file))
1216 ;;;_ , save metod
1217 (defconst org-export-odt-mimetype-lines
1218 '("application/vnd.oasis.opendocument.text"))
1220 (defconst org-odt-manifest-file-entry-tag
1221 "<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"/>")
1223 (defun org-odt-save-as-outfile (target opt-plist)
1224 ;; write meta file
1225 (org-odt-update-meta-file opt-plist)
1227 ;; write styles file
1228 (org-odt-copy-styles-file)
1230 ;; Update styles.xml - take care of outline numbering
1231 (with-current-buffer
1232 (find-file-noselect "styles.xml" (file-name-directory buffer-file-name))
1234 ;; Import local setting of `org-export-with-section-numbers'
1235 (org-html-bind-local-variables opt-plist)
1236 (org-odt-configure-outline-numbering
1237 (if org-export-with-section-numbers org-export-headline-levels 0)))
1239 (let ((zipdir default-directory))
1240 (message "Switching to directory %s" (expand-file-name zipdir))
1242 ;; save all xml files
1243 (mapc (lambda (file)
1244 (with-current-buffer (find-file-noselect (expand-file-name file))
1245 ;; prettify output
1246 (indent-region (point-min) (point-max)) (save-buffer)))
1247 org-export-odt-save-list)
1249 (let* ((target-name (file-name-nondirectory target))
1250 (target-dir (file-name-directory target))
1251 (cmd (format "zip -rmTq %s %s" target-name ".")))
1252 (when (file-exists-p target)
1253 ;; FIXME: If the file is locked this throws a cryptic error
1254 (delete-file target))
1256 (let ((coding-system-for-write 'no-conversion) exitcode)
1257 (message "Creating odt file using \"%s\"" cmd)
1258 (setq exitcode
1259 (apply 'call-process
1260 "zip"
1264 (append (list "-rmTq") (list target-name "."))))
1266 (or (zerop exitcode)
1267 (error "Unable to create odt file (%S)" exitcode)))
1269 ;; move the file from outdir to target-dir
1270 (rename-file target-name target-dir)
1272 ;; kill all xml buffers
1273 (mapc (lambda (file)
1274 (kill-buffer (find-file-noselect (expand-file-name file zipdir))))
1275 org-export-odt-save-list)
1277 (delete-directory zipdir)))
1279 ;; FIXME: How to open the file without visiting
1280 (find-file target))
1282 (defun org-odt-update-meta-file (opt-plist)
1283 (with-current-buffer
1284 (find-file-noselect "meta.xml" (file-name-directory buffer-file-name))
1285 (let ((date (or (plist-get opt-plist :effective-date) ""))
1286 (author (or (plist-get opt-plist :author) ""))
1287 (email (plist-get opt-plist :email))
1288 (keywords (plist-get opt-plist :keywords))
1289 (description (plist-get opt-plist :description))
1290 (title (plist-get opt-plist :title)))
1292 (insert
1293 "\n"
1294 (org-html-format-tags '("<dc:creator>" . "</dc:creator>") author)
1295 (org-html-format-tags
1296 '("\n<meta:initial-creator>" . "</meta:initial-creator>") author)
1297 (org-html-format-tags '("\n<dc:date>" . "</dc:date>") date)
1298 (org-html-format-tags
1299 '("\n<meta:creation-date>" . "</meta:creation-date>") date)
1300 (org-html-format-tags '("\n<meta:generator>" . "</meta:generator>")
1301 (when org-export-creator-info
1302 (format "Org-%s/Emacs-%s"
1303 org-version emacs-version)))
1304 (org-html-format-tags '("\n<meta:keyword>" . "</meta:keyword>") keywords)
1305 (org-html-format-tags '("\n<dc:subject>" . "</dc:subject>") description)
1306 (org-html-format-tags '("\n<dc:title>" . "</dc:title>") title)
1307 "\n"))))
1310 (defun org-odt-update-manifest-file (media-type full-path)
1311 (with-current-buffer
1312 (find-file-noselect "META-INF/manifest.xml")
1313 (insert (format org-odt-manifest-file-entry-tag media-type full-path))))
1316 ;;;_ , cleanup method
1318 (defun org-odt-finalize-outfile ()
1319 (message "org-newodt: Finalizing outfile")
1320 (org-odt-delete-empty-paragraphs))
1322 (defun org-odt-delete-empty-paragraphs ()
1323 (goto-char (point-min))
1324 (let ((open "<text:p[^>]*>")
1325 (close "</text:p>"))
1326 (while (re-search-forward (format "%s[ \r\n\t]*%s" open close) nil t)
1327 (replace-match ""))))
1329 ;;;_. get callback
1330 (defun org-odt-get (what &optional opt-plist)
1331 (case what
1332 (BACKEND 'odt)
1333 (EXPORT-DIR (org-export-directory :html opt-plist))
1334 (FILE-NAME-EXTENSION "odt")
1335 (EXPORT-BUFFER-NAME "*Org ODT Export*")
1336 (ENTITY-CONTROL org-odt-entity-control-callbacks-alist)
1337 (ENTITY-FORMAT org-odt-entity-format-callbacks-alist)
1338 (INIT-METHOD 'org-odt-init-outfile)
1339 (FINAL-METHOD 'org-odt-finalize-outfile)
1340 (SAVE-METHOD 'org-odt-save-as-outfile)
1341 (TOPLEVEL-HLEVEL 1)
1342 (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps)
1343 (t (error "Unknown property: %s" what))))
1345 ;;;_ preprocessor
1347 (defun org-odt-parse-label (label)
1348 (save-match-data
1349 (if (not (string-match "\\`[a-zA-Z]+:\\(.+\\)" label))
1350 (cons label nil)
1351 (cons
1352 (capitalize (substring label 0 (1- (match-beginning 1))))
1353 (substring label (match-beginning 1))))))
1355 (defun org-export-odt-preprocess (parameters)
1356 "Convert LaTeX fragments to images."
1357 (when (and org-current-export-file
1358 (plist-get parameters :LaTeX-fragments))
1359 (org-format-latex
1360 (concat "ltxpng/" (file-name-sans-extension
1361 (file-name-nondirectory
1362 org-current-export-file)))
1363 org-current-export-dir nil "Creating LaTeX image %s"
1364 nil nil
1365 (cond
1366 ((eq (plist-get parameters :LaTeX-fragments) 'verbatim) 'verbatim)
1367 ;; Investigate MathToWeb for converting TeX equations to MathML
1368 ;; See http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01755.html
1369 ((or (eq (plist-get parameters :LaTeX-fragments) 'mathjax )
1370 (eq (plist-get parameters :LaTeX-fragments) t ))
1371 (org-parse-warn "MathJax not supported. Falling back to dvipng")
1372 'dvipng)
1373 ((eq (plist-get parameters :LaTeX-fragments) 'dvipng ) 'dvipng)
1374 (t nil))))
1375 (goto-char (point-min))
1376 (let (label label-components category value pretty-label)
1377 (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t)
1378 (org-if-unprotected-at (match-beginning 1)
1379 (setq label (match-string 1)
1380 label-components (org-odt-parse-label label)
1381 category (car label-components)
1382 value (cdr label-components)
1383 pretty-label (if value (concat category " " value) label))
1384 (replace-match
1385 (let ((org-html-protect t))
1386 (org-html-format-tags
1387 '("<text:sequence-ref text:reference-format=\"category-and-value\" text:ref-name=\"%s\">"
1388 . "</text:sequence-ref>") pretty-label label)) t t)))))
1390 (declare-function archive-zip-extract "arc-mode.el" (archive name))
1391 (defun org-odt-zip-extract-one (archive member &optional target)
1392 (require 'arc-mode)
1393 (let* ((target (or target default-directory))
1394 (archive (expand-file-name archive))
1395 (archive-zip-extract
1396 (list "unzip" "-qq" "-o" "-d" target))
1397 exit-code command-output)
1398 (setq command-output
1399 (with-temp-buffer
1400 (setq exit-code (archive-zip-extract archive member))
1401 (buffer-string)))
1402 (unless (zerop exit-code)
1403 (message command-output)
1404 (error "Extraction failed"))))
1406 (defun org-odt-zip-extract (archive members &optional target)
1407 (when (atom members) (setq members (list members)))
1408 (mapc (lambda (member)
1409 (org-odt-zip-extract-one archive member target))
1410 members))
1412 (defun org-odt-copy-styles-file (&optional styles-file)
1413 ;; Non-availability of styles.xml is not a critical error. For now
1414 ;; throw an error purely for aesthetic reasons.
1415 (setq styles-file (or styles-file
1416 org-export-odt-styles-file
1417 ;; use a sensible default
1418 ;; FIXME: The code below assumes a specific
1419 ;; dir layout for locating the styles file
1420 (expand-file-name
1421 "../contrib/odt/OrgOdtStyles.xml"
1422 (file-name-directory (locate-library "org-odt")))
1423 (error "org-odt: Missing styles file?")))
1424 (cond
1425 ((listp styles-file)
1426 (let ((archive (nth 0 styles-file))
1427 (members (nth 1 styles-file)))
1428 (org-odt-zip-extract archive members)
1429 (mapc
1430 (lambda (member)
1431 (when (org-file-image-p member)
1432 (let* ((image-type (file-name-extension member))
1433 (media-type (format "image/%s" image-type)))
1434 (org-odt-update-manifest-file media-type member))))
1435 members)))
1436 ((and (stringp styles-file) (file-exists-p styles-file))
1437 (let ((styles-file-type (file-name-extension styles-file)))
1438 (cond
1439 ((string= styles-file-type "xml")
1440 (copy-file styles-file "styles.xml" t))
1441 ((member styles-file-type '("odt" "ott"))
1442 (org-odt-zip-extract styles-file "styles.xml")))))
1444 (error (format "Invalid specification of styles.xml file: %S"
1445 org-export-odt-styles-file)))))
1448 ;;; convenience routines
1450 (defvar org-export-odt-factory-settings
1451 "b629fe72d6620ffb0fcf684be1412330bed74b14"
1452 "SHA1 hash of OrgOdtStyles.xml.")
1454 (defun org-odt-configure-outline-numbering (level)
1455 "Outline numbering is retained only upto LEVEL.
1456 To disable outline numbering pass a LEVEL of 0."
1457 (if (not (string= org-export-odt-factory-settings (sha1 (current-buffer))))
1458 (org-parse-warn
1459 "org-odt: Using custom styles file? Consider tweaking styles.xml for better output. To suppress this warning update `org-export-odt-factory-settings'")
1460 (goto-char (point-min))
1461 (let ((regex
1462 "<text:outline-level-style\\(.*\\)text:level=\"\\(.*\\)\"\\(.*\\)>")
1463 (replacement
1464 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1465 (while (re-search-forward regex nil t)
1466 (when (> (string-to-number (match-string 1)) level)
1467 (replace-match replacement t nil))))
1468 (save-buffer 0)))
1470 ;;;_ postamble
1472 (provide 'org-odt)