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-and-open "odt" arg
))
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:
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"))
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'."
203 (org-export-as-to-buffer "odt" arg
))
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."
212 (org-replace-region-by "odt" beg end
))
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."
230 (org-export-region-as "odt" beg end body-only buffer
))
232 ;;; org-export-as-odt
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."
251 (org-export-as "odt" arg hidden ext-plist to-buffer body-only pub-dir
))
255 (defvar org-odt-entity-control-callbacks-alist
257 .
(org-odt-begin-export org-odt-end-export
))
259 .
(org-odt-begin-document-content org-odt-end-document-content
))
261 .
(org-odt-begin-document-body org-odt-end-document-body
))
263 .
(org-odt-begin-toc org-odt-end-toc
))
265 .
(org-odt-begin-environment org-odt-end-environment
))
267 .
(org-html-begin-level org-html-end-level
))
269 .
(org-odt-begin-footnote-definition org-odt-end-footnote-definition
))
271 .
(org-odt-begin-table org-odt-end-table
))
273 .
(org-odt-begin-table-rowgroup org-odt-end-table-rowgroup
))
275 .
(org-odt-begin-list org-odt-end-list
))
277 .
(org-odt-begin-list-item org-odt-end-list-item
))
279 .
(org-odt-begin-section org-odt-end-section
))
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
))
313 ;;;_. control callbacks
315 (defun org-odt-begin-office-body ()
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
)))
335 (org-odt-format-stylized-paragraph 'title title
))))
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)
385 (insert org-odt-document-content-header
)
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
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>"))
404 (defun org-odt-begin-section (&optional id class
405 extra-class
; ignored for now
409 (when id
(format " text:name=\"%s\"" id
))
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>"))
420 (defun org-odt-begin-paragraph (&optional style
)
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)
432 ((stringp style
) style
)
433 ((symbolp style
) (org-odt-get-style-name-for-entity
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
)))
446 (defun org-odt-begin-environment (style)
448 ((blockquote verse center quote
)
449 (org-parse-begin-paragraph style
)
452 (org-parse-end-paragraph)
454 (t (error "Unknown environment %s" style
))))
456 (defun org-odt-end-environment (style)
458 ((blockquote verse center quote
)
459 (org-parse-end-paragraph)
462 (org-parse-begin-paragraph)
464 (t (error "Unknown environment %s" style
))))
468 (defun org-odt-begin-list (ltype &optional arg1
)
469 (setq ltype
(or (org-html-html-list-type-to-canonical-list-type 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.
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
)
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
)
494 (assert (not headline
) t
)
495 (let* ((counter arg
) (extra ""))
496 (org-html-insert-tag "<text:list-item>")
497 (org-parse-begin-paragraph)))
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
)))))
505 (assert (not headline
) t
)
506 (let ((term (or arg
"(no term)")))
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
)
521 (org-html-insert-tag "</text:list-item>"))
524 (org-parse-end 'LIST
'description
)
526 (t (error "Unknown list type"))))
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
)
543 (org-odt-format-stylized-paragraph
544 'table
(org-odt-format-entity-caption label caption
"Table"))))
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))))
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
)
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
)
598 ((eq (cdr (assoc r org-table-rowgroup-info
)) :start
) "T")
600 (when (= r org-table-rownum
) "B")
603 ((or (memq (nth c org-table-colgroup-info
) '(:start
:startend
))
604 (memq (nth (1- c
) org-table-colgroup-info
) '(:end
:startend
))) "L")
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
616 (org-table-current-rowgroup-is-header "OrgTableHeading")
617 ((and (= c
0) org-export-html-table-use-header-tags-for-first-column
)
619 (t "OrgTableContents"))
621 (let* ((style-name-cookie
622 (format "@@table-cell:style-name@@%03d@@%03d@@" r c
))
623 (paragraph-style-cookie
626 (org-table-current-rowgroup-is-header "OrgTableHeading")
627 ((and (= c
0) org-export-html-table-use-header-tags-for-first-column
)
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)
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
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>
668 </text:table-of-content-source>
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>
676 (defun org-odt-end-toc ()
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
". "))
689 (org-parse-format 'SPACES
3)
690 (org-parse-format 'FONTIFY tags
"tag")))))
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
704 (defvar org-parse-link-description-is-image nil
)
705 (defun org-odt-format-link (desc href
&optional attr
)
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>")
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
"")))))
723 (defun org-odt-format-spaces (n)
724 (org-html-format-tags "<text:s text:c=\"%d\"/>" "" n
))
727 (defun org-odt-format-tabs (&optional n
)
728 (let ((tab "<text:tab/>")
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
""))
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)
751 ((string= s
"\t") (org-odt-format-tabs))
753 (org-odt-format-spaces (length s
)))
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)
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)
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")
779 ("target" .
"OrgTarget"))))
783 (defun org-odt-format-fontify (text style
&optional id
)
787 (org-odt-remap-stylenames style
))
789 (org-odt-get-style-name-for-entity 'character 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>")
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
))))
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
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
))
834 (and (member type-1
'("http" "https" "file"))
835 (org-html-should-inline-p path descp
)
837 (type (if (equal type-1
"id") "file" type-1
))
842 ;; check for inlined images
843 ((and (member type
'("file"))
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")
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"))
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")))
875 (org-export-html-format-desc desc
) thefile attr
)))))))
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
)))
887 (defun org-odt-format-headline (title extra-targets tags
888 &optional snumber level
)
890 (org-parse-format 'EXTRA-TARGETS extra-targets
)
892 ;; No need to generate section numbers. They are auto-generated by
895 ;; (concat (org-parse-format 'SECTION-NUMBER snumber level) " ")
897 (and tags
(concat (org-parse-format 'SPACES
3)
898 (org-parse-format 'ORG-TAGS tags
)))))
902 (defun org-odt-format-anchor (text name
&optional class
)
903 (org-odt-format-target text name
))
906 (defun org-odt-format-bookmark (text id
)
908 (org-html-format-tags "<text:bookmark text:name=\"%s\"/>" text id
)
911 (defun org-odt-format-target (text id
)
912 (let ((name (concat org-export-odt-bookmark-prefix id
)))
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\">" .
929 (org-html-format-tags
930 '("<text:note-citation>" .
"</text:note-citation>")
932 (org-html-format-tags
933 '("<text:note-body>" .
"</text:note-body>")
938 (defun org-odt-format-footnote-reference (n def refcnt
)
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")
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
)
957 (defun org-odt-get-image-name (file-name)
961 (concat (sha1 file-name
) "." (file-name-extension file-name
)) "Pictures")))
963 (defun org-export-odt-format-image (src href
966 "Create image tag with source and attributes."
969 (let (embed-as caption attr label attr-plist size width height
)
972 ((string-match "^ltxpng/" src
)
973 ;; FIXME: Anyway the latex src can be embedded as an
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
996 (defun org-export-odt-do-format-image (embed-as caption attr label
998 "Create image tag with source and attributes."
1000 (let ((width (car size
)) (height (cdr size
))
1002 '("<draw:frame draw:style-name=\"%s\"
1003 text:anchor-type=\"%s\"
1004 draw:z-index=\"%d\" %s>" .
"</draw:frame>")))
1006 ((and (not caption
) (not label
))
1007 (let (style-name anchor-type
)
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
))))
1019 ;; (when par-open (org-odt-close-par))
1020 (org-html-format-tags
1022 (org-html-format-tags
1023 '("<draw:text-box fo:min-height=\"%dcm\">" .
"</draw:text-box>")
1024 (org-odt-format-stylized-paragraph
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
)))
1033 "OrgFrame" "paragraph" 1
1034 (org-odt-image-attrs-from-size width
))
1036 ;; (when par-open (org-odt-open-par))
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
)))
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
)
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
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
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
))))
1112 (cons (pixels-to-cms (car size-in-pixels
))
1113 (pixels-to-cms (cdr size-in-pixels
)))))))
1116 (size-in-cms (ignore-errors (image-size (create-image file
) 'pixels
))))
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
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
)))
1143 (setq width
(* width scale
) height
(* height scale
)))
1144 ((and user-height user-width
)
1145 (setq width user-width height user-height
))
1147 (setq width
(* user-height
(/ width height
)) height user-height
))
1149 (setq height
(* user-width
(/ height width
)) width user-width
))
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
))))
1162 entity
(or default-entity org-odt-default-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
)
1170 ;;;_. maintenance callbacks
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
))
1184 (with-current-buffer (find-file-noselect content-file
) (erase-buffer))
1186 ;; FIXME: How to factor in body-only here
1189 (make-directory (file-name-directory manifest-file
))
1190 (with-current-buffer (find-file-noselect manifest-file
)
1192 (insert (mapconcat 'identity
(car org-export-odt-manifest-lines
) "\n"))
1195 (insert (mapconcat 'identity
(cdr org-export-odt-manifest-lines
) "\n"))))
1198 (with-current-buffer (find-file-noselect meta-file
)
1200 (insert (mapconcat 'identity
(car org-export-odt-meta-lines
) "\n"))
1203 (insert (mapconcat 'identity
(cdr org-export-odt-meta-lines
) "\n"))))
1206 ;; (copy-file org-export-odt-styles-file styles-file t)
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
)))
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
)
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
))
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
)
1259 (apply 'call-process
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
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
)))
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
)
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 ""))))
1330 (defun org-odt-get (what &optional opt-plist
)
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
)
1342 (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps
)
1343 (t (error "Unknown property: %s" what
))))
1347 (defun org-odt-parse-label (label)
1349 (if (not (string-match "\\`[a-zA-Z]+:\\(.+\\)" label
))
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
))
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"
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")
1373 ((eq (plist-get parameters
:LaTeX-fragments
) 'dvipng
) 'dvipng
)
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
))
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
)
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
1400 (setq exit-code
(archive-zip-extract archive member
))
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
))
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
1421 "../contrib/odt/OrgOdtStyles.xml"
1422 (file-name-directory (locate-library "org-odt")))
1423 (error "org-odt: Missing styles file?")))
1425 ((listp styles-file
)
1426 (let ((archive (nth 0 styles-file
))
1427 (members (nth 1 styles-file
)))
1428 (org-odt-zip-extract archive members
)
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
))))
1436 ((and (stringp styles-file
) (file-exists-p styles-file
))
1437 (let ((styles-file-type (file-name-extension styles-file
)))
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))))
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))
1462 "<text:outline-level-style\\(.*\\)text:level=\"\\(.*\\)\"\\(.*\\)>")
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
))))