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
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
32 (eval-when-compile (require 'cl
))
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>"
44 (goto-char (point-min))
46 ;; Convert whitespace place holders
47 (goto-char (point-min))
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
))
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 '(("\\\\-" .
"­\\1")
73 ("---\\([^-]\\)" .
"—\\1")
74 ("--\\([^-]\\)" .
"–\\1")
75 ("\\.\\.\\." .
"…"))
76 "Regular expressions for special string conversion.")
81 (defcustom org-export-odt-automatic-styles-file nil
82 "Default style file for use with ODT exporter."
83 :group
'org-export-odt
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
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
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
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
135 ;;;_ , potential custom settings
136 (defvar org-export-odt-default-org-styles-alist
137 '((paragraph .
((default .
"Text_20_body")
138 (fixedwidth .
"OrgSourceBlock")
140 (quote .
"Quotations")
141 (blockquote .
"Quotations")
142 (center .
"OrgCenter")
145 (title .
"Heading_20_1.title")
146 (footnote .
"Footnote")
147 (src .
"OrgSourceBlock")
148 (illustration .
"Illustration")
150 (definition-term .
"Text_20_body_20_bold")
151 (horizontal-line .
"Horizontal_20_Line")))
152 (character .
((bold .
"Bold")
153 (emphasis .
"Emphasis")
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"
177 ;;;_. interactive commands
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."
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))))
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:
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
))
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'."
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*")))
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."
217 (let (reg odt buf pop-up-frames
)
218 (save-window-excursion
220 (setq odt
(org-export-region-as-odt
222 (setq reg
(buffer-substring beg end
)
223 buf
(get-buffer-create "*Org tmp*"))
224 (with-current-buffer buf
228 (setq odt
(org-export-region-as-odt
229 (point-min) (point-max) t
'string
)))
231 (delete-region beg end
)
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."
250 (when (interactive-p)
251 (setq buffer
"*Org ODT Export*"))
252 (let ((transient-mark-mode t
) (zmacs-regions t
)
254 (setq ext-plist
(plist-put ext-plist
:ignore-subtree-p t
))
256 (set-mark (point)) ;; to activate the region
258 (setq rtn
(org-export-as-odt
261 (if (fboundp 'deactivate-mark
) (deactivate-mark))
262 (if (and (interactive-p) (bufferp rtn
))
263 (switch-to-buffer-other-window rtn
)
266 ;;; org-export-as-odt
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."
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
)))
294 (defvar org-odt-entity-control-callbacks-alist
296 .
(org-odt-begin-export org-odt-end-export
))
298 .
(org-odt-begin-document-content org-odt-end-document-content
))
300 .
(org-odt-begin-document-body org-odt-end-document-body
))
302 .
(org-odt-begin-toc org-odt-end-toc
))
304 .
(org-odt-begin-environment org-odt-end-environment
))
306 .
(org-html-begin-level org-html-end-level
))
308 .
(org-odt-begin-footnote-definition org-odt-end-footnote-definition
))
310 .
(org-odt-begin-table org-odt-end-table
))
312 .
(org-odt-begin-table-rowgroup org-odt-end-table-rowgroup
))
314 .
(org-odt-begin-list org-odt-end-list
))
316 .
(org-odt-begin-list-item org-odt-end-list-item
))
318 .
(org-odt-begin-section org-odt-end-section
))
320 .
(org-odt-begin-paragraph org-odt-end-paragraph
)))
323 (defvar org-odt-entity-format-callbacks-alist
324 `((EXTRA-TARGETS . org-html-format-extra-targets
)
325 (ORG-TAGS . org-html-format-org-tags
)
326 (SECTION-NUMBER . org-html-format-section-number
)
327 (HEADLINE . org-odt-format-headline
)
328 (TOC-ENTRY . org-odt-format-toc-entry
)
329 (TOC-ITEM . org-odt-format-toc-item
)
330 (TAGS . org-odt-format-tags
)
331 (SPACES . org-odt-format-spaces
)
332 (TABS . org-odt-format-tabs
)
333 (LINE-BREAK . org-odt-format-line-break
)
334 (FONTIFY . org-odt-format-fontify
)
335 (TODO . org-html-format-todo
)
336 (LINK . org-odt-format-link
)
337 (INLINE-IMAGE . org-odt-format-inline-image
)
338 (ORG-LINK . org-odt-format-org-link
)
339 (HEADING . org-odt-format-heading
)
340 (ANCHOR . org-odt-format-anchor
)
341 (TABLE-ROW . org-odt-format-table-row
)
342 (TABLE-CELL . org-odt-format-table-cell
)
343 (FOOTNOTES-SECTION . ignore
)
344 (FOOTNOTE-REFERENCE . org-odt-format-footnote-reference
)
345 (HORIZONTAL-LINE . org-odt-format-horizontal-line
)
346 (COMMENT . org-html-format-comment
)
347 (PLAIN . org-odt-format-plain
))
351 ;;;_. control callbacks
353 (defun org-odt-begin-office-body ()
357 <text:sequence-decls>
358 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Illustration\"/>
359 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Table\"/>
360 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Text\"/>
361 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Drawing\"/>
362 </text:sequence-decls>"))
365 ;; Following variable is let bound when `org-do-export' is in
366 ;; progress. See org-html.el.
367 (defvar org-parse-table-of-contents
)
368 (defun org-odt-begin-document-body (opt-plist)
369 (org-odt-begin-office-body)
370 (let ((title (plist-get opt-plist
:title
)))
373 (org-odt-format-stylized-paragraph 'title title
))))
376 (when org-parse-table-of-contents
377 (insert "\n" org-parse-table-of-contents
"\n")))
379 (defun org-odt-end-document-body (opt-plist)
380 (org-html-insert-tag "</office:text>")
381 (org-html-insert-tag "</office:body>"))
383 (defconst org-odt-document-content-header
384 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
385 <office:document-content
386 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
387 xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\"
388 xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\"
389 xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\"
390 xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\"
391 xmlns:fo=\"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0\"
392 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
393 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
394 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
395 xmlns:number=\"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0\"
396 xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\"
397 xmlns:chart=\"urn:oasis:names:tc:opendocument:xmlns:chart:1.0\"
398 xmlns:dr3d=\"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0\"
399 xmlns:math=\"http://www.w3.org/1998/Math/MathML\"
400 xmlns:form=\"urn:oasis:names:tc:opendocument:xmlns:form:1.0\"
401 xmlns:script=\"urn:oasis:names:tc:opendocument:xmlns:script:1.0\"
402 xmlns:ooo=\"http://openoffice.org/2004/office\"
403 xmlns:ooow=\"http://openoffice.org/2004/writer\"
404 xmlns:oooc=\"http://openoffice.org/2004/calc\"
405 xmlns:dom=\"http://www.w3.org/2001/xml-events\"
406 xmlns:xforms=\"http://www.w3.org/2002/xforms\"
407 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
408 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
409 xmlns:rpt=\"http://openoffice.org/2005/report\"
410 xmlns:of=\"urn:oasis:names:tc:opendocument:xmlns:of:1.2\"
411 xmlns:xodt=\"http://www.w3.org/1999/xodt\"
412 xmlns:field=\"urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0\" office:version=\"1.2\">
417 ;;;_ , document content
418 (defun org-odt-begin-document-content (opt-plist)
420 (org-odt-update-meta-file opt-plist
)
423 (insert org-odt-document-content-header
)
426 (insert-file-contents org-export-odt-automatic-styles-file
)
427 (goto-char (point-max)))
429 (defun org-odt-end-document-content ()
430 (org-html-insert-tag "</office:document-content>"))
435 (defun org-odt-begin-section (&optional id class
436 extra-class
; ignored for now
440 (when id
(format " text:name=\"%s\"" id
))
442 (format " text:style-name=\"%s\"" class
)))))
443 (org-html-insert-tag "<text:section%s>" extra
)))
445 (defun org-odt-end-section ()
446 (org-html-insert-tag "</text:section>"))
451 (defun org-odt-begin-paragraph (&optional style
)
453 "<text:p%s>" (org-odt-get-extra-attrs-for-paragraph-style style
)))
455 (defun org-odt-end-paragraph ()
456 (org-html-insert-tag "</text:p>"))
459 (defun org-odt-get-extra-attrs-for-paragraph-style (style)
463 ((stringp style
) style
)
464 ((symbolp style
) (org-odt-get-style-name-for-entity
467 (error "Don't know how to handle paragraph style %s" style
))
468 (format " text:style-name=\"%s\"" style-name
)))
470 (defun org-odt-format-stylized-paragraph (style text
)
471 (org-html-format-tags
472 '("<text:p%s>" .
"</text:p>") text
473 (org-odt-get-extra-attrs-for-paragraph-style style
)))
477 (defun org-odt-begin-environment (style)
479 ((blockquote verse center quote
)
480 (org-parse-begin-paragraph style
)
483 (org-parse-end-paragraph)
485 (t (error "Unknown environment %s" style
))))
487 (defun org-odt-end-environment (style)
489 ((blockquote verse center quote
)
490 (org-parse-end-paragraph)
493 (org-parse-begin-paragraph)
495 (t (error "Unknown environment %s" style
))))
499 (defun org-odt-begin-list (ltype &optional arg1
)
500 (setq ltype
(or (org-html-html-list-type-to-canonical-list-type ltype
)
502 (let* ((style-name (org-odt-get-style-name-for-entity 'list ltype
))
503 (extra (if style-name
504 (format " text:style-name=\"%s\"" style-name
) "")))
506 ;; FIXME: Handle arg1 incase of ordered lists.
508 ((ordered unordered description
)
509 (org-parse-end-paragraph)
510 (org-html-insert-tag "<text:list%s>" extra
))
511 (t (error "Unknown list type: %s" ltype
)))))
513 (defun org-odt-end-list (ltype)
514 (setq ltype
(or (org-html-html-list-type-to-canonical-list-type ltype
)
517 (org-html-insert-tag "</text:list>")
518 (error "Unknown list type: %s" ltype
)))
520 (defun org-odt-begin-list-item (ltype &optional arg headline
)
521 (setq ltype
(or (org-html-html-list-type-to-canonical-list-type ltype
)
525 (assert (not headline
) t
)
526 (let* ((counter arg
) (extra ""))
527 (org-html-insert-tag "<text:list-item>")
528 (org-parse-begin-paragraph)))
530 (let* ((id arg
) (extra ""))
531 (org-html-insert-tag "<text:list-item>")
532 (org-parse-begin-paragraph)
534 (if headline
(if id
(org-odt-format-target headline id
) headline
)
535 (org-odt-format-bookmark "" id
)))))
537 (assert (not headline
) t
)
538 (let ((term (or arg
"(no term)")))
540 (org-html-format-tags
541 '("<text:list-item>" .
"</text:list-item>")
542 (org-odt-format-stylized-paragraph 'definition-term term
)))
543 (org-parse-begin 'LIST-ITEM
'unordered
)
544 (org-parse-begin 'LIST
'description
)
545 (org-parse-begin 'LIST-ITEM
'unordered
)))
546 (t (error "Unknown list type"))))
548 (defun org-odt-end-list-item (ltype)
549 (setq ltype
(or (org-html-html-list-type-to-canonical-list-type ltype
)
553 (org-html-insert-tag "</text:list-item>"))
556 (org-parse-end 'LIST
'description
)
558 (t (error "Unknown list type"))))
562 ;; Following variables are let bound when table emission is in
563 ;; progress. See org-html.el.
564 (defvar org-table-begin-marker
)
565 (defvar org-table-ncols
)
566 (defvar org-table-rowgroup-open
)
567 (defvar org-table-rownum
)
568 (defvar org-table-current-rowgroup-is-header
)
569 (defvar org-table-is-styled
)
570 (defvar org-table-rowgroup-info
)
571 (defvar org-table-colalign-vector
)
572 (defun org-odt-begin-table (caption label attributes
)
575 (org-odt-format-stylized-paragraph
576 'table
(org-odt-format-entity-caption label caption
"Table"))))
579 "<table:table table:name=\"%s\" table:style-name=\"%s\">"
580 (or label
"") "OrgTable")
581 (setq org-table-begin-marker
(point)))
583 (defun org-odt-end-table ()
584 (goto-char org-table-begin-marker
)
585 (loop for level from
0 below org-table-ncols
586 do
(insert (org-html-format-tags "<table:table-column/>" "")))
588 ;; fill style attributes for table cells
589 (when org-table-is-styled
590 (while (re-search-forward "@@\\(table-cell:p\\|table-cell:style-name\\)@@\\([0-9]+\\)@@\\([0-9]+\\)@@" nil t
)
591 (let ((spec (match-string 1))
592 (r (string-to-number (match-string 2)))
593 (c (string-to-number (match-string 3))))
595 ((equal spec
"table-cell:p")
596 (let ((style-name (org-odt-get-paragraph-style-for-table-cell r c
)))
597 (replace-match style-name t t
)))
598 ((equal spec
"table-cell:style-name")
599 (let ((style-name (org-odt-get-style-name-for-table-cell r c
)))
600 (replace-match style-name t t
)))))))
602 (goto-char (point-max))
603 (org-html-insert-tag "</table:table>"))
605 (defun org-odt-begin-table-rowgroup (&optional is-header-row
)
606 (when org-table-rowgroup-open
607 (org-parse-end 'TABLE-ROWGROUP
))
608 (org-html-insert-tag (if is-header-row
609 "<table:table-header-rows>"
610 "<table:table-rows>"))
611 (setq org-table-rowgroup-open t
)
612 (setq org-table-current-rowgroup-is-header is-header-row
))
614 (defun org-odt-end-table-rowgroup ()
615 (when org-table-rowgroup-open
616 (setq org-table-rowgroup-open nil
)
618 (if org-table-current-rowgroup-is-header
619 "</table:table-header-rows>" "</table:table-rows>"))))
621 (defun org-odt-format-table-row (row)
622 (org-html-format-tags
623 '("<table:table-row>" .
"</table:table-row>") row
))
625 (defun org-odt-get-style-name-for-table-cell (r c
)
630 ((eq (cdr (assoc r org-table-rowgroup-info
)) :start
) "T")
632 (when (= r org-table-rownum
) "B")
635 ((or (memq (nth c org-table-colgroup-info
) '(:start
:startend
))
636 (memq (nth (1- c
) org-table-colgroup-info
) '(:end
:startend
))) "L")
639 (defun org-odt-get-paragraph-style-for-table-cell (r c
)
640 (capitalize (aref org-table-colalign-vector c
)))
642 (defun org-odt-format-table-cell (data r c
)
643 (if (not org-table-is-styled
)
644 (org-html-format-tags
645 '("<table:table-cell>" .
"</table:table-cell>")
646 (org-odt-format-stylized-paragraph
648 (org-table-current-rowgroup-is-header "OrgTableHeading")
649 ((and (= c
0) org-export-html-table-use-header-tags-for-first-column
)
651 (t "OrgTableContents"))
653 (let* ((style-name-cookie
654 (format "@@table-cell:style-name@@%03d@@%03d@@" r c
))
655 (paragraph-style-cookie
658 (org-table-current-rowgroup-is-header "OrgTableHeading")
659 ((and (= c
0) org-export-html-table-use-header-tags-for-first-column
)
661 (t "OrgTableContents"))
662 (format "@@table-cell:p@@%03d@@%03d@@" r c
))))
663 (org-html-format-tags
664 '("<table:table-cell table:style-name=\"%s\">" .
665 "</table:table-cell>")
666 (org-odt-format-stylized-paragraph paragraph-style-cookie data
)
667 style-name-cookie
))))
670 ;;;_ , footnote definition
671 (defun org-odt-begin-footnote-definition (n)
672 (org-parse-begin-paragraph 'footnote
))
674 (defun org-odt-end-footnote-definition (n)
675 (org-parse-end-paragraph))
678 ;;;_. format callbacks
680 (defun org-odt-begin-toc (lang-specific-heading)
683 <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
684 <text:table-of-content-source text:outline-level=\"10\">
685 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
686 " lang-specific-heading
))
688 (loop for level from
1 upto
10
691 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
692 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
693 <text:index-entry-chapter/>
694 <text:index-entry-text/>
695 <text:index-entry-link-end/>
696 </text:table-of-content-entry-template>
700 </text:table-of-content-source>
703 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
704 <text:p text:style-name=\"Contents_20_Heading\">Table of Contents</text:p>
708 (defun org-odt-end-toc ()
711 </text:table-of-content>
714 (defun org-odt-format-toc-entry (snumber todo headline tags href
)
715 (setq headline
(concat
716 (and org-export-with-section-numbers
717 (concat snumber
". "))
721 (org-parse-format 'SPACES
3)
722 (org-parse-format 'FONTIFY tags
"tag")))))
724 (setq headline
(org-parse-format 'FONTIFY headline
"todo")))
726 (let ((org-odt-suppress-xref t
))
727 (org-odt-format-link headline
(concat "#" href
))))
729 (defun org-odt-format-toc-item (toc-entry level org-last-level
)
730 (let ((style (format "Contents_20_%d"
731 (+ level
(or (org-parse-get 'TOPLEVEL-HLEVEL
) 1) -
1))))
732 (insert "\n" (org-odt-format-stylized-paragraph style toc-entry
) "\n")))
734 ;; Following variable is let bound during 'ORG-LINK callback. See
736 (defvar org-parse-link-description-is-image nil
)
737 (defun org-odt-format-link (desc href
&optional attr
)
739 ((and (= (string-to-char href
) ?
#) (not org-odt-suppress-xref
))
740 (setq href
(concat org-export-odt-bookmark-prefix
(substring href
1)))
741 (org-html-format-tags
742 '("<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"%s\">" .
743 "</text:bookmark-ref>")
745 (org-parse-link-description-is-image
746 (org-html-format-tags
747 '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" .
"</draw:a>")
748 desc href
(or attr
"")))
750 (org-html-format-tags
751 '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" .
"</text:a>")
752 desc href
(or attr
"")))))
755 (defun org-odt-format-spaces (n)
756 (org-html-format-tags "<text:s text:c=\"%d\"/>" "" n
))
759 (defun org-odt-format-tabs (&optional n
)
760 (let ((tab "<text:tab/>")
765 (defun org-odt-format-line-break ()
766 (org-html-format-tags "<text:line-break/>" ""))
768 ;;;_ , horizontal line
769 (defun org-odt-format-horizontal-line ()
770 (org-odt-format-stylized-paragraph 'horizontal-line
""))
773 (defun org-odt-format-plain (line)
774 (case org-parse-dyn-current-environment
775 (fixedwidth (concat (org-odt-format-source-code-or-example-line
776 (org-html-protect line
)) "\n"))
777 (t (concat line
"\n"))))
779 (defun org-odt-fill-tabs-and-spaces (line)
780 (replace-regexp-in-string
781 "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
783 ((string= s
"\t") (org-odt-format-tabs))
785 (org-odt-format-spaces (length s
)))
788 (defun org-odt-format-source-code-or-example-line (line)
789 (org-odt-format-stylized-paragraph 'src
(org-odt-fill-tabs-and-spaces line
)))
791 (defun org-odt-format-example (lines)
794 (org-odt-format-source-code-or-example-line line
))
795 (org-split-string lines
"[\r\n]") "\n"))
797 (defun org-html-protect-lines (rtn)
799 (lambda (line) (org-html-protect line
))
800 (org-split-string rtn
"[\r\n]") "\n"))
802 ;;;_ , character styles
803 (defun org-odt-remap-stylenames (style-name)
805 (cdr (assoc style-name
'(("timestamp-wrapper" .
"OrgTimestampWrapper")
806 ("timestamp" .
"OrgTimestamp")
807 ("timestamp-kwd" .
"OrgTimestampKeyword")
811 ("target" .
"OrgTarget"))))
815 (defun org-odt-format-fontify (text style
&optional id
)
819 (org-odt-remap-stylenames style
))
821 (org-odt-get-style-name-for-entity 'character style
))
823 (assert (< 1 (length style
)))
824 (let ((parent-style (pop style
)))
825 (mapconcat (lambda (s)
826 ;; (assert (stringp s) t)
827 (org-odt-remap-stylenames s
)) style
"")
828 (org-odt-remap-stylenames parent-style
)))
829 (t (error "Don't how to handle style %s" style
)))))
830 (org-html-format-tags
831 '("<text:span text:style-name=\"%s\">" .
"</text:span>")
835 (defun org-odt-relocate-relative-path (path dir
)
836 (if (file-name-absolute-p path
) path
837 (file-relative-name (expand-file-name path dir
)
838 (expand-file-name "eyecandy" dir
))))
840 (defun org-odt-format-inline-image (thefile)
841 (let* ((thelink (if (file-name-absolute-p thefile
) thefile
842 (org-export-html-format-href
843 (org-odt-relocate-relative-path
844 thefile org-current-export-file
))))
846 (org-html-format-tags
847 "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
848 (if org-export-odt-embed-images
849 (org-odt-copy-image-file thefile
) thelink
))))
850 (org-export-odt-format-image thefile href
)))
852 (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
855 OPT-PLIST is an options list.
856 TYPE is the device-type of the link (THIS://foo.html)
857 PATH is the path of the link (http://THIS#locationx)
858 FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
859 DESC is the link description, if any.
860 ATTR is a string of other attributes of the a element.
861 MAY-INLINE-P allows inlining it as an image."
863 (declare (special org-par-open
))
866 (and (member type-1
'("http" "https" "file"))
867 (org-html-should-inline-p path descp
)
869 (type (if (equal type-1
"id") "file" type-1
))
874 ;; check for inlined images
875 ((and (member type
'("file"))
878 filename org-odt-export-inline-image-extensions
)
879 (or (eq t org-odt-export-inline-images
)
880 (and org-odt-export-inline-images
(not descp
))))
882 ;; (when (and (string= type "file") (file-name-absolute-p path))
883 ;; (setq thefile (concat "file://" (expand-file-name path))))
884 ;; (setq thefile (org-export-html-format-href thefile))
885 ;; (org-export-html-format-image thefile)
886 (org-odt-format-inline-image thefile
))
888 (when (string= type
"file")
891 ((file-name-absolute-p path
)
892 (concat "file://" (expand-file-name path
)))
893 (t (org-odt-relocate-relative-path
894 thefile org-current-export-file
)))))
896 (when (and (member type
'("" "http" "https" "file" "coderef"))
898 (setq thefile
(concat thefile
"#" fragment
)))
900 (setq thefile
(org-export-html-format-href thefile
))
902 (when (not (member type
'("" "file" "coderef")))
903 (setq thefile
(concat type
":" thefile
)))
905 (let ((org-odt-suppress-xref (string= type
"coderef")))
907 (org-export-html-format-desc desc
) thefile attr
)))))))
911 (defun org-odt-format-heading (text level
&optional id
)
912 (let* ((text (if id
(org-odt-format-target text id
) text
)))
913 (org-html-format-tags
914 '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
915 "</text:h>") text level level
)))
919 (defun org-odt-format-headline (title extra-targets tags
920 &optional snumber level
)
922 (org-parse-format 'EXTRA-TARGETS extra-targets
)
924 ;; No need to generate section numbers. They are auto-generated by
927 ;; (concat (org-parse-format 'SECTION-NUMBER snumber level) " ")
929 (and tags
(concat (org-parse-format 'SPACES
3)
930 (org-parse-format 'ORG-TAGS tags
)))))
934 (defun org-odt-format-anchor (text name
&optional class
)
935 (org-odt-format-target text name
))
938 (defun org-odt-format-bookmark (text id
)
939 (org-html-format-tags "<text:bookmark text:name=\"%s\"/>" text id
))
941 (defun org-odt-format-target (text id
)
942 (let ((name (concat org-export-odt-bookmark-prefix id
)))
944 (org-html-format-tags "<text:bookmark-start text:name=\"%s\"/>" "" name
)
945 (org-odt-format-bookmark text id
)
946 (org-html-format-tags "<text:bookmark-end text:name=\"%s\"/>" "" name
))))
948 ;;;_ , footnote reference
949 (defun org-odt-format-footnote (n def
)
950 (let ((id (concat "fn" n
))
951 (note-class "footnote")
952 (par-style "Footnote"))
953 (org-html-format-tags
954 '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
957 (org-html-format-tags
958 '("<text:note-citation>" .
"</text:note-citation>")
960 (org-html-format-tags
961 '("<text:note-body>" .
"</text:note-body>")
966 (defun org-odt-format-footnote-reference (n def refcnt
)
968 (org-odt-format-footnote n def
)
969 (org-odt-format-footnote-ref n
)))
972 (defun org-odt-format-footnote-ref (n)
973 (let ((note-class "footnote")
975 (ref-name (concat "fn" n
)))
976 (org-html-format-tags
977 '("<text:span text:style-name=\"%s\">" .
"</text:span>")
978 (org-html-format-tags
979 '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" .
"</text:note-ref>")
980 n note-class ref-format ref-name
)
985 (defun org-odt-get-image-name (file-name)
989 (concat (sha1 file-name
) "." (file-name-extension file-name
)) "Pictures")))
991 (defun org-export-odt-format-image (src href
994 "Create image tag with source and attributes."
997 (let (embed-as caption attr label attr-plist size width height
)
1000 ((string-match "^ltxpng/" src
)
1001 ;; FIXME: Anyway the latex src can be embedded as an
1004 ;; (org-find-text-property-in-string 'org-latex-src src)
1005 (setq caption nil attr nil label nil embed-as
'character
))
1008 (setq caption
(org-find-text-property-in-string 'org-caption src
)
1009 caption
(and caption
(org-export-html-format-desc caption
))
1010 attr
(org-find-text-property-in-string 'org-attributes src
)
1011 label
(org-find-text-property-in-string 'org-label src
)
1012 embed-as
'paragraph
)))
1014 (setq attr-plist
(when attr
(read attr
)))
1015 (setq size
(org-odt-image-size-from-file
1016 src
(plist-get attr-plist
:width
)
1017 (plist-get attr-plist
:height
)
1018 (plist-get attr-plist
:scale
)))
1020 (org-export-odt-do-format-image embed-as caption attr label
1024 (defun org-export-odt-do-format-image (embed-as caption attr label
1026 "Create image tag with source and attributes."
1028 (let ((width (car size
)) (height (cdr size
))
1030 '("<draw:frame draw:style-name=\"%s\"
1031 text:anchor-type=\"%s\"
1032 draw:z-index=\"%d\" %s>" .
"</draw:frame>")))
1034 ((and (not caption
) (not label
))
1035 (let (style-name anchor-type
)
1037 ((eq embed-as
'paragraph
)
1038 (setq style-name
"OrgGraphicsParagraph" anchor-type
"paragraph"))
1039 ((eq embed-as
'character
)
1040 (setq style-name
"OrgGraphicsBaseline" anchor-type
"as-char")))
1041 (org-html-format-tags
1042 draw-frame-pair href style-name anchor-type
0
1043 (org-odt-image-attrs-from-size width height
))))
1047 ;; (when par-open (org-odt-close-par))
1048 (org-html-format-tags
1050 (org-html-format-tags
1051 '("<draw:text-box fo:min-height=\"%dcm\">" .
"</draw:text-box>")
1052 (org-odt-format-stylized-paragraph
1055 (let ((extra " style:rel-width=\"100%\" style:rel-height=\"scale\""))
1056 (org-html-format-tags
1057 draw-frame-pair href
"OrgGraphicsParagraphContent" "paragraph" 2
1058 (concat (org-odt-image-attrs-from-size width height
) extra
)))
1059 (org-odt-format-entity-caption label caption
)))
1061 "OrgFrame" "paragraph" 1
1062 (org-odt-image-attrs-from-size width
))
1064 ;; (when par-open (org-odt-open-par))
1067 ;; xml files generated on-the-fly
1068 (defconst org-export-odt-save-list
1069 '("META-INF/manifest.xml" "content.xml" "meta.xml"))
1071 ;; xml files that are copied
1072 (defconst org-export-odt-nosave-list
'("styles.xml"))
1074 ;; xml files that contribute to the final odt file
1075 (defvar org-export-odt-file-list nil
)
1077 (defconst org-export-odt-manifest-lines
1078 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1079 "<manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">"
1080 "<manifest:file-entry manifest:media-type=\"application/vnd.oasis.opendocument.text\" manifest:version=\"1.2\" manifest:full-path=\"/\"/>"
1081 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"content.xml\"/>"
1082 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"styles.xml\"/>"
1083 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"meta.xml\"/>"
1084 "<manifest:file-entry manifest:media-type=\"\" manifest:full-path=\"Pictures/\"/>") .
("</manifest:manifest>")))
1086 (defconst org-export-odt-meta-lines
1087 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1088 "<office:document-meta"
1089 " xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\""
1090 " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
1091 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\""
1092 " xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\""
1093 " xmlns:ooo=\"http://openoffice.org/2004/office\" "
1094 " office:version=\"1.2\">"
1095 " <office:meta>") .
(" </office:meta>" "</office:document-meta>")))
1097 (defun org-odt-copy-image-file (path &optional target-file
)
1098 "Returns the internal name of the file"
1099 (let* ((image-type (file-name-extension path
))
1100 (media-type (format "image/%s" image-type
))
1101 (src-file (expand-file-name
1102 path
(file-name-directory org-current-export-file
)))
1103 (target-file (or target-file
(org-odt-get-image-name src-file
)))
1108 (message "Embedding %s as %s ..."
1109 (substring-no-properties path
) target-file
)
1110 (copy-file src-file target-file
'overwrite
)
1111 (org-odt-update-manifest-file media-type target-file
)
1112 (push target-file org-export-odt-file-list
)) target-file
))
1114 (defun org-odt-image-attrs-from-size (&optional width height
)
1116 (when width
(format "svg:width=\"%0.2fcm\"" width
))
1118 (when height
(format "svg:height=\"%0.2fcm\"" height
))))
1120 (defun org-odt-image-size-from-file (file &optional user-width
1121 user-height scale dpi
)
1122 (unless (file-name-absolute-p file
)
1123 (setq file
(expand-file-name
1124 file
(file-name-directory org-current-export-file
))))
1126 (setq dpi
(or dpi org-export-odt-pixels-per-inch
))
1127 (let* ((size (image-size (create-image file
) 'pixels
))
1128 (width-in-pixels (car size
)) (height-in-pixels (cdr size
))
1129 (width-in-inches (/ width-in-pixels dpi
))
1130 (height-in-inches (/ height-in-pixels dpi
))
1132 (width-in-cms (* cms-per-inch width-in-inches
))
1133 (height-in-cms (* cms-per-inch height-in-inches
))
1134 (width width-in-cms
) (height height-in-cms
))
1135 (cond (scale (setq width
(* width scale
) height
(* height scale
)))
1136 ((and user-height user-width
)
1137 (setq width user-width height user-height
))
1138 (user-height (setq width
(* user-height
(/ width height
))
1139 height user-height
))
1140 (user-width (setq height
(* user-width
(/ height width
))
1143 (cons width height
)))
1145 (defvar org-odt-default-entity
"Illustration")
1146 (defun org-odt-format-entity-caption (label caption
&optional default-entity
)
1147 (if (not label
) (or caption
"")
1148 (let* ((label-components (org-odt-parse-label label
))
1149 (entity (car label-components
))
1150 (seqno (cdr label-components
))
1151 (caption (and caption
(concat ": " caption
))))
1154 entity
(or default-entity org-odt-default-entity
)))
1157 (org-html-format-tags
1158 '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" .
"</text:sequence>")
1159 seqno label entity entity
)
1162 ;;;_. maintenance callbacks
1165 (defun org-odt-init-outfile (filename)
1166 (let* ((outdir (make-temp-file org-export-odt-tmpdir-prefix t
))
1167 (mimetype-file (expand-file-name "mimetype" outdir
))
1168 (content-file (expand-file-name "content.xml" outdir
))
1169 (manifest-file (expand-file-name "META-INF/manifest.xml" outdir
))
1170 (meta-file (expand-file-name "meta.xml" outdir
))
1171 (styles-file (expand-file-name "styles.xml" outdir
))
1172 (pictures-dir (expand-file-name "Pictures" outdir
))
1176 (with-current-buffer (find-file-noselect content-file
) (erase-buffer))
1178 ;; FIXME: How to factor in body-only here
1181 (make-directory (file-name-directory manifest-file
))
1182 (with-current-buffer (find-file-noselect manifest-file
)
1184 (insert (mapconcat 'identity
(car org-export-odt-manifest-lines
) "\n"))
1187 (insert (mapconcat 'identity
(cdr org-export-odt-manifest-lines
) "\n"))))
1190 (with-current-buffer (find-file-noselect meta-file
)
1192 (insert (mapconcat 'identity
(car org-export-odt-meta-lines
) "\n"))
1195 (insert (mapconcat 'identity
(cdr org-export-odt-meta-lines
) "\n"))))
1198 ;; (copy-file org-export-odt-styles-file styles-file t)
1201 (make-directory pictures-dir
)
1203 ;; initialize list of files that contribute to the odt file
1204 (setq org-export-odt-file-list
1205 (append org-export-odt-save-list org-export-odt-nosave-list
)))
1209 (defconst org-export-odt-mimetype-lines
1210 '("application/vnd.oasis.opendocument.text"))
1212 (defconst org-odt-manifest-file-entry-tag
1213 "<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"/>")
1215 (defun org-odt-save-as-outfile (target)
1216 (org-odt-copy-styles-file)
1218 ;; Update styles.xml - take care of outline numbering
1219 (with-current-buffer
1220 (find-file-noselect "styles.xml" (file-name-directory buffer-file-name
))
1222 ;; Import local setting of `org-export-with-section-numbers'
1223 (org-html-bind-local-variables opt-plist
)
1224 (org-odt-configure-outline-numbering
1225 (if org-export-with-section-numbers org-export-headline-levels
0)))
1227 (let ((zipdir default-directory
))
1228 (message "Switching to directory %s" (expand-file-name zipdir
))
1230 ;; save all xml files
1231 (mapc (lambda (file)
1232 (with-current-buffer (find-file-noselect (expand-file-name file
))
1234 (indent-region (point-min) (point-max)) (save-buffer)))
1235 org-export-odt-save-list
)
1237 (let* ((target-name (file-name-nondirectory target
))
1238 (target-dir (file-name-directory target
))
1239 (cmd (format "zip -rmTq %s %s" target-name
".")))
1240 (when (file-exists-p target
)
1241 ;; FIXME: If the file is locked this throws a cryptic error
1242 (delete-file target
))
1244 (let ((coding-system-for-write 'no-conversion
) exitcode
)
1245 (message "Creating odt file using \"%s\"" cmd
)
1247 (apply 'call-process
1252 (append (list "-rmTq") (list target-name
"."))))
1254 (or (zerop exitcode
)
1255 (error "Unable to create odt file (%S)" exitcode
)))
1257 ;; move the file from outdir to target-dir
1258 (rename-file target-name target-dir
)
1260 ;; kill all xml buffers
1261 (mapc (lambda (file)
1262 (kill-buffer (find-file-noselect (expand-file-name file zipdir
))))
1263 org-export-odt-save-list
)
1265 (delete-directory zipdir
)))
1267 ;; FIXME: How to open the file without visiting
1270 (defun org-odt-update-meta-file (opt-plist)
1271 (with-current-buffer
1272 (find-file-noselect "meta.xml" (file-name-directory buffer-file-name
))
1273 (let ((date (or (plist-get opt-plist
:effective-date
) ""))
1274 (author (or (plist-get opt-plist
:author
) ""))
1275 (email (plist-get opt-plist
:email
))
1276 (keywords (plist-get opt-plist
:keywords
))
1277 (description (plist-get opt-plist
:description
))
1278 (title (plist-get opt-plist
:title
)))
1282 (org-html-format-tags '("<dc:creator>" .
"</dc:creator>") author
)
1283 (org-html-format-tags
1284 '("\n<meta:initial-creator>" .
"</meta:initial-creator>") author
)
1285 (org-html-format-tags '("\n<dc:date>" .
"</dc:date>") date
)
1286 (org-html-format-tags
1287 '("\n<meta:creation-date>" .
"</meta:creation-date>") date
)
1288 (org-html-format-tags '("\n<meta:generator>" .
"</meta:generator>")
1289 (when org-export-creator-info
1290 (format "Org-%s/Emacs-%s"
1291 org-version emacs-version
)))
1292 (org-html-format-tags '("\n<meta:keyword>" .
"</meta:keyword>") keywords
)
1293 (org-html-format-tags '("\n<dc:subject>" .
"</dc:subject>") description
)
1294 (org-html-format-tags '("\n<dc:title>" .
"</dc:title>") title
)
1298 (defun org-odt-update-manifest-file (media-type full-path
)
1299 (with-current-buffer
1300 (find-file-noselect "META-INF/manifest.xml")
1301 (insert (format org-odt-manifest-file-entry-tag media-type full-path
))))
1304 ;;;_ , cleanup method
1306 (defun org-odt-finalize-outfile ()
1307 (message "org-newodt: Finalizing outfile")
1308 (org-odt-delete-empty-paragraphs))
1310 (defun org-odt-delete-empty-paragraphs ()
1311 (goto-char (point-min))
1312 (let ((open "<text:p[^>]*>")
1313 (close "</text:p>"))
1314 (while (re-search-forward (format "%s[ \r\n\t]*%s" open close
) nil t
)
1315 (replace-match ""))))
1318 (defun org-odt-get (what &optional opt-plist
)
1321 (EXPORT-DIR (org-export-directory :html opt-plist
))
1322 (FILE-NAME-EXTENSION "odt")
1323 (EXPORT-BUFFER-NAME "*Org ODT Export*")
1324 (ENTITY-CONTROL org-odt-entity-control-callbacks-alist
)
1325 (ENTITY-FORMAT org-odt-entity-format-callbacks-alist
)
1326 (INIT-METHOD 'org-odt-init-outfile
)
1327 (FINAL-METHOD 'org-odt-finalize-outfile
)
1328 (SAVE-METHOD 'org-odt-save-as-outfile
)
1330 (t (error "Unknown property: %s" what
))))
1334 (defun org-odt-parse-label (label)
1336 (if (not (string-match "\\`[a-zA-Z]+:\\(.+\\)" label
))
1339 (capitalize (substring label
0 (1- (match-beginning 1))))
1340 (substring label
(match-beginning 1))))))
1342 (defun org-export-odt-preprocess (parameters)
1343 "Convert LaTeX fragments to images."
1344 (when (and org-current-export-file
1345 (plist-get parameters
:LaTeX-fragments
))
1347 (concat "ltxpng/" (file-name-sans-extension
1348 (file-name-nondirectory
1349 org-current-export-file
)))
1350 org-current-export-dir nil
"Creating LaTeX image %s"
1353 ((eq (plist-get parameters
:LaTeX-fragments
) 'verbatim
) 'verbatim
)
1354 ;; Investigate MathToWeb for converting TeX equations to MathML
1355 ;; See http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01755.html
1356 ((or (eq (plist-get parameters
:LaTeX-fragments
) 'mathjax
)
1357 (eq (plist-get parameters
:LaTeX-fragments
) t
))
1358 (org-odt-warn "MathJax not supported. Falling back to dvipng")
1360 ((eq (plist-get parameters
:LaTeX-fragments
) 'dvipng
) 'dvipng
)
1362 (goto-char (point-min))
1363 (let (label label-components category value pretty-label
)
1364 (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t
)
1365 (org-if-unprotected-at (match-beginning 1)
1366 (setq label
(match-string 1)
1367 label-components
(org-odt-parse-label label
)
1368 category
(car label-components
)
1369 value
(cdr label-components
)
1370 pretty-label
(if value
(concat category
" " value
) label
))
1372 (let ((org-html-protect t
))
1373 (org-html-format-tags
1374 '("<text:sequence-ref text:reference-format=\"category-and-value\" text:ref-name=\"%s\">"
1375 .
"</text:sequence-ref>") pretty-label label
)) t t
)))))
1377 (declare-function archive-zip-extract
"arc-mode.el" (archive name
))
1378 (defun org-odt-zip-extract-one (archive member
&optional target
)
1380 (let* ((target (or target default-directory
))
1381 (archive (expand-file-name archive
))
1382 (archive-zip-extract
1383 (list "unzip" "-qq" "-o" "-d" target
))
1384 exit-code command-output
)
1385 (setq command-output
1387 (setq exit-code
(archive-zip-extract archive member
))
1389 (unless (zerop exit-code
)
1390 (message command-output
)
1391 (error "Extraction failed"))))
1393 (defun org-odt-zip-extract (archive members
&optional target
)
1394 (when (atom members
) (setq members
(list members
)))
1395 (mapc (lambda (member)
1396 (org-odt-zip-extract-one archive member target
))
1399 (defun org-odt-copy-styles-file (&optional styles-file
)
1400 ;; Non-availability of styles.xml is not a critical error. For now
1401 ;; throw an error purely for aesthetic reasons.
1402 (setq styles-file
(or styles-file
1403 org-export-odt-styles-file
1404 (error "org-odt: Missing styles file?")))
1406 ((listp styles-file
)
1407 (let ((archive (nth 0 styles-file
))
1408 (members (nth 1 styles-file
)))
1409 (org-odt-zip-extract archive members
)
1412 (when (org-file-image-p member
)
1413 (let* ((image-type (file-name-extension member
))
1414 (media-type (format "image/%s" image-type
)))
1415 (org-odt-update-manifest-file media-type member
))))
1417 ((and (stringp styles-file
) (file-exists-p styles-file
))
1418 (let ((styles-file-type (file-name-extension styles-file
)))
1420 ((string= styles-file-type
"xml")
1421 (copy-file styles-file
"styles.xml" t
))
1422 ((member styles-file-type
'("odt" "ott"))
1423 (org-odt-zip-extract styles-file
"styles.xml")))))
1425 (error (format "Invalid specification of styles.xml file: %S"
1426 org-export-odt-styles-file
)))))
1429 ;;; convenience routines
1431 (defvar org-export-odt-factory-settings
1432 "b629fe72d6620ffb0fcf684be1412330bed74b14"
1433 "SHA1 hash of OrgOdtStyles.xml.")
1435 (defun org-odt-configure-outline-numbering (level)
1436 "Outline numbering is retained only upto LEVEL.
1437 To disable outline numbering pass a LEVEL of 0."
1438 (if (not (string= org-export-odt-factory-settings
(sha1 (current-buffer))))
1440 "org-odt: Using custom styles file? Consider tweaking styles.xml for better output. To suppress this warning update `org-export-odt-factory-settings'")
1441 (goto-char (point-min))
1443 "<text:outline-level-style\\(.*\\)text:level=\"\\(.*\\)\"\\(.*\\)>")
1445 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1446 (while (re-search-forward regex nil t
)
1447 (when (> (string-to-number (match-string 1)) level
)
1448 (replace-match replacement t nil
))))
1451 (defun org-odt-warn (msg)
1452 (put-text-property 0 (length msg
) 'face
'font-lock-warning-face msg
)