1 ;;; org-odt.el --- OpenDocumentText export for Org-mode
3 ;; Copyright (C) 2010, 2011
4 ;; Jambunathan <kjambunathan at gmail dot com>
6 ;; Author: Jambunathan K <kjambunathan at gmail dot com>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
11 ;; This file is not (yet) part of GNU Emacs.
12 ;; However, it is distributed under the same license.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
31 (eval-when-compile (require 'cl
))
34 (defun org-odt-end-export ()
35 ;; remove empty paragraphs
36 (goto-char (point-min))
37 (while (re-search-forward
38 "<text:p\\( text:style-name=\"Text_20_body\"\\)?>[ \r\n\t]*</text:p>"
41 (goto-char (point-min))
43 ;; Convert whitespace place holders
44 (goto-char (point-min))
46 (while (setq beg
(next-single-property-change (point) 'org-whitespace
))
47 (setq n
(get-text-property beg
'org-whitespace
)
48 end
(next-single-property-change beg
'org-whitespace
))
50 (delete-region beg end
)
51 (insert (format "<span style=\"visibility:hidden;\">%s</span>"
52 (make-string n ?x
)))))
54 ;; Remove empty lines at the beginning of the file.
55 (goto-char (point-min))
56 (when (looking-at "\\s-+\n") (replace-match ""))
58 ;; Remove display properties
59 (remove-text-properties (point-min) (point-max) '(display t
)))
61 (defvar org-odt-suppress-xref nil
)
62 (defconst org-export-odt-special-string-regexps
63 '(("\\\\-" .
"­\\1") ; shy
64 ("---\\([^-]\\)" .
"—\\1") ; mdash
65 ("--\\([^-]\\)" .
"–\\1") ; ndash
66 ("\\.\\.\\." .
"…")) ; hellip
67 "Regular expressions for special string conversion.")
69 (defconst org-odt-lib-dir
(file-name-directory load-file-name
))
70 (defconst org-odt-data-dir
71 (let ((dir1 (expand-file-name "../odt" org-odt-lib-dir
)) ; git
72 (dir2 (expand-file-name "./contrib/odt" org-odt-lib-dir
))) ; elpa
74 ((file-directory-p dir1
) dir1
)
75 ((file-directory-p dir2
) dir2
)
76 (t (error "Cannot find factory styles file. Check package dir layout")))))
78 (defvar org-odt-file-extensions
79 '(("odt" .
"OpenDocument Text")
80 ("ott" .
"OpenDocument Text Template")
81 ("odm" .
"OpenDocument Master Document")
82 ("ods" .
"OpenDocument Spreadsheet")
83 ("ots" .
"OpenDocument Spreadsheet Template")
84 ("odg" .
"OpenDocument Drawing (Graphics)")
85 ("otg" .
"OpenDocument Drawing Template")
86 ("odp" .
"OpenDocument Presentation")
87 ("otp" .
"OpenDocument Presentation Template")
88 ("odi" .
"OpenDocument Image")
89 ("odf" .
"OpenDocument Formula")
90 ("odc" .
"OpenDocument Chart")
91 ("doc" .
"Microsoft Text")
92 ("docx" .
"Microsoft Text")
93 ("xls" .
"Microsoft Spreadsheet")
94 ("xlsx" .
"Microsoft Spreadsheet")
95 ("ppt" .
"Microsoft Presentation")
96 ("pptx" .
"Microsoft Presentation")))
98 (defvar org-odt-ms-file-extensions
99 '(("doc" .
"Microsoft Text")
100 ("docx" .
"Microsoft Text")
101 ("xls" .
"Microsoft Spreadsheet")
102 ("xlsx" .
"Microsoft Spreadsheet")
103 ("ppt" .
"Microsoft Presentation")
104 ("pptx" .
"Microsoft Presentation")))
106 ;; RelaxNG validation of OpenDocument xml files
107 (eval-after-load 'rng-nxml
108 '(setq rng-nxml-auto-validate-flag t
))
110 (eval-after-load 'rng-loc
111 '(add-to-list 'rng-schema-locating-files
112 (expand-file-name "etc/schema/schemas.xml" org-odt-data-dir
)))
116 ;; Let Org open all OpenDocument files using system-registered app
117 (add-to-list 'org-file-apps
118 (cons (concat "\\." (car desc
) "\\'") 'system
))
119 ;; Let Emacs open all OpenDocument files in archive mode
120 (add-to-list 'auto-mode-alist
121 (cons (concat "\\." (car desc
) "\\'") 'archive-mode
)))
122 org-odt-file-extensions
)
126 ;; Let Org open all Microsoft files using system-registered app
127 (add-to-list 'org-file-apps
128 (cons (concat "\\." (car desc
) "\\'") 'system
)))
129 org-odt-ms-file-extensions
)
131 ;; register the odt exporter with the pre-processor
132 (add-to-list 'org-export-backends
'odt
)
134 ;; register the odt exporter with org-lparse library
135 (org-lparse-register-backend 'odt
)
137 (defcustom org-export-odt-automatic-styles-file nil
138 "Default style file for use with ODT exporter."
140 :group
'org-export-odt
)
142 ;; TODO: Make configuration user-friendly.
143 (defcustom org-export-odt-styles-file nil
144 "Default style file for use with ODT exporter.
145 Valid values are path to an styles.xml file or a path to a valid
146 *.odt or a *.ott file or a list of the form (FILE (MEMBER1
147 MEMBER2 ...)). In the last case, the specified FILE is unzipped
148 and MEMBER1, MEMBER2 etc are copied in to the generated odt
149 file. The last form is particularly useful if the styles.xml has
150 reference to additional files like header and footer images.
153 :group
'org-export-odt
)
155 (defconst org-export-odt-tmpdir-prefix
"odt-")
156 (defconst org-export-odt-bookmark-prefix
"OrgXref.")
157 (defcustom org-export-odt-use-bookmarks-for-internal-links t
158 "Export Internal links as bookmarks?."
160 :group
'org-export-odt
)
162 (defcustom org-export-odt-embed-images t
163 "Should the images be copied in to the odt file or just linked?"
165 :group
'org-export-odt
)
167 (defcustom org-odt-export-inline-images
'maybe
168 "Non-nil means inline images into exported HTML pages.
169 This is done using an <img> tag. When nil, an anchor with href is used to
170 link to the image. If this option is `maybe', then images in links with
171 an empty description will be inlined, while images with a description will
173 :group
'org-odt-export
174 :type
'(choice (const :tag
"Never" nil
)
175 (const :tag
"Always" t
)
176 (const :tag
"When there is no description" maybe
)))
178 (defcustom org-odt-export-inline-image-extensions
179 '("png" "jpeg" "jpg" "gif")
180 "Extensions of image files that can be inlined into HTML."
181 :type
'(repeat (string :tag
"Extension"))
182 :group
'org-odt-export
)
184 (defcustom org-export-odt-pixels-per-inch display-pixels-per-inch
185 ;; FIXME add docstring
188 :group
'org-export-odt
)
190 (defvar org-export-odt-default-org-styles-alist
191 '((paragraph .
((default .
"Text_20_body")
192 (fixedwidth .
"OrgSourceBlock")
194 (quote .
"Quotations")
195 (blockquote .
"Quotations")
196 (center .
"OrgCenter")
199 (title .
"Heading_20_1.title")
200 (footnote .
"Footnote")
201 (src .
"OrgSourceBlock")
202 (illustration .
"Illustration")
204 (definition-term .
"Text_20_body_20_bold")
205 (horizontal-line .
"Horizontal_20_Line")))
206 (character .
((bold .
"Bold")
207 (emphasis .
"Emphasis")
209 (verbatim .
"OrgCode")
210 (strike .
"Strikethrough")
211 (underline .
"Underline")
212 (subscript .
"OrgSubscript")
213 (superscript .
"OrgSuperscript")))
214 (list .
((ordered .
"OrgNumberedList")
215 (unordered .
"OrgBulletedList")
216 (description .
"OrgDescriptionList"))))
217 "Default styles for various entities.")
219 (defvar org-export-odt-org-styles-alist org-export-odt-default-org-styles-alist
)
220 (defun org-odt-get-style-name-for-entity (category &optional entity
)
221 (let ((entity (or entity
'default
)))
223 (cdr (assoc entity
(cdr (assoc category
224 org-export-odt-org-styles-alist
))))
225 (cdr (assoc entity
(cdr (assoc category
226 org-export-odt-default-org-styles-alist
))))
227 (error "Cannot determine style name for entity %s of type %s"
231 (defun org-export-as-odt-and-open (arg)
232 "Export the outline as ODT and immediately open it with a browser.
233 If there is an active region, export only the region.
234 The prefix ARG specifies how many levels of the outline should become
235 headlines. The default is 3. Lower levels will become bulleted lists."
237 (org-lparse-and-open "odt" "odt" arg
))
240 (defun org-export-as-odt-batch ()
241 "Call the function `org-lparse-batch'.
242 This function can be used in batch processing as:
244 --load=$HOME/lib/emacs/org.el
245 --eval \"(setq org-export-headline-levels 2)\"
246 --visit=MyFile --funcall org-export-as-odt-batch"
247 (org-lparse-batch "odt"))
250 (defun org-export-as-odt-to-buffer (arg)
251 "Call `org-lparse-odt` with output to a temporary buffer.
252 No file is created. The prefix ARG is passed through to `org-lparse-to-buffer'."
254 (org-lparse-to-buffer "odt" arg
))
257 (defun org-replace-region-by-odt (beg end
)
258 "Assume the current region has org-mode syntax, and convert it to ODT.
259 This can be used in any buffer. For example, you could write an
260 itemized list in org-mode syntax in an ODT buffer and then use this
261 command to convert it."
263 (org-replace-region-by "odt" beg end
))
266 (defun org-export-region-as-odt (beg end
&optional body-only buffer
)
267 "Convert region from BEG to END in org-mode buffer to ODT.
268 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
269 contents, and only produce the region of converted text, useful for
270 cut-and-paste operations.
271 If BUFFER is a buffer or a string, use/create that buffer as a target
272 of the converted ODT. If BUFFER is the symbol `string', return the
273 produced ODT as a string and leave not buffer behind. For example,
274 a Lisp program could call this function in the following way:
276 (setq odt (org-export-region-as-odt beg end t 'string))
278 When called interactively, the output buffer is selected, and shown
279 in a window. A non-interactive call will only return the buffer."
281 (org-lparse-region "odt" beg end body-only buffer
))
283 ;;; org-export-as-odt
285 (defun org-export-as-odt (arg &optional hidden ext-plist
286 to-buffer body-only pub-dir
)
287 "Export the outline as a OpenDocumentText file.
288 If there is an active region, export only the region. The prefix
289 ARG specifies how many levels of the outline should become
290 headlines. The default is 3. Lower levels will become bulleted
291 lists. HIDDEN is obsolete and does nothing.
292 EXT-PLIST is a property list with external parameters overriding
293 org-mode's default settings, but still inferior to file-local
294 settings. When TO-BUFFER is non-nil, create a buffer with that
295 name and export to that buffer. If TO-BUFFER is the symbol
296 `string', don't leave any buffer behind but just return the
297 resulting XML as a string. When BODY-ONLY is set, don't produce
298 the file header and footer, simply return the content of
299 <body>...</body>, without even the body tags themselves. When
300 PUB-DIR is set, use this as the publishing directory."
302 (org-lparse "odt" "odt" arg hidden ext-plist to-buffer body-only pub-dir
))
304 (defvar org-odt-entity-control-callbacks-alist
306 .
(org-odt-begin-export org-odt-end-export
))
308 .
(org-odt-begin-document-content org-odt-end-document-content
))
310 .
(org-odt-begin-document-body org-odt-end-document-body
))
312 .
(org-odt-begin-toc org-odt-end-toc
))
314 .
(org-odt-begin-environment org-odt-end-environment
))
316 .
(org-odt-begin-footnote-definition org-odt-end-footnote-definition
))
318 .
(org-odt-begin-table org-odt-end-table
))
320 .
(org-odt-begin-table-rowgroup org-odt-end-table-rowgroup
))
322 .
(org-odt-begin-list org-odt-end-list
))
324 .
(org-odt-begin-list-item org-odt-end-list-item
))
326 .
(org-odt-begin-outline org-odt-end-outline
))
328 .
(org-odt-begin-outline-text org-odt-end-outline-text
))
330 .
(org-odt-begin-paragraph org-odt-end-paragraph
)))
333 (defvar org-odt-entity-format-callbacks-alist
334 `((EXTRA-TARGETS . org-lparse-format-extra-targets
)
335 (ORG-TAGS . org-lparse-format-org-tags
)
336 (SECTION-NUMBER . org-lparse-format-section-number
)
337 (HEADLINE . org-odt-format-headline
)
338 (TOC-ENTRY . org-odt-format-toc-entry
)
339 (TOC-ITEM . org-odt-format-toc-item
)
340 (TAGS . org-odt-format-tags
)
341 (SPACES . org-odt-format-spaces
)
342 (TABS . org-odt-format-tabs
)
343 (LINE-BREAK . org-odt-format-line-break
)
344 (FONTIFY . org-odt-format-fontify
)
345 (TODO . org-lparse-format-todo
)
346 (LINK . org-odt-format-link
)
347 (INLINE-IMAGE . org-odt-format-inline-image
)
348 (ORG-LINK . org-odt-format-org-link
)
349 (HEADING . org-odt-format-heading
)
350 (ANCHOR . org-odt-format-anchor
)
351 (TABLE . org-lparse-format-table
)
352 (TABLE-ROW . org-odt-format-table-row
)
353 (TABLE-CELL . org-odt-format-table-cell
)
354 (FOOTNOTES-SECTION . ignore
)
355 (FOOTNOTE-REFERENCE . org-odt-format-footnote-reference
)
356 (HORIZONTAL-LINE . org-odt-format-horizontal-line
)
357 (COMMENT . org-odt-format-comment
)
358 (LINE . org-odt-format-line
)
359 (ORG-ENTITY . org-odt-format-org-entity
))
363 ;;;_. control callbacks
365 (defun org-odt-begin-office-body ()
369 <text:sequence-decls>
370 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Illustration\"/>
371 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Table\"/>
372 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Text\"/>
373 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Drawing\"/>
374 </text:sequence-decls>"))
376 ;; Following variable is let bound when `org-do-lparse' is in
377 ;; progress. See org-html.el.
378 (defvar org-lparse-toc
)
379 (defun org-odt-begin-document-body (opt-plist)
380 (org-odt-begin-office-body)
381 (let ((title (plist-get opt-plist
:title
)))
384 (org-odt-format-stylized-paragraph 'title title
))))
388 (insert "\n" org-lparse-toc
"\n")))
390 (defvar org-lparse-body-only
) ; let bound during org-do-lparse
391 (defvar org-lparse-to-buffer
) ; let bound during org-do-lparse
392 (defun org-odt-end-document-body (opt-plist)
393 (unless org-lparse-body-only
394 (org-lparse-insert-tag "</office:text>")
395 (org-lparse-insert-tag "</office:body>")))
397 (defconst org-odt-document-content-header
398 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
399 <office:document-content
400 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
401 xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\"
402 xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\"
403 xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\"
404 xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\"
405 xmlns:fo=\"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0\"
406 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
407 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
408 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
409 xmlns:number=\"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0\"
410 xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\"
411 xmlns:chart=\"urn:oasis:names:tc:opendocument:xmlns:chart:1.0\"
412 xmlns:dr3d=\"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0\"
413 xmlns:math=\"http://www.w3.org/1998/Math/MathML\"
414 xmlns:form=\"urn:oasis:names:tc:opendocument:xmlns:form:1.0\"
415 xmlns:script=\"urn:oasis:names:tc:opendocument:xmlns:script:1.0\"
416 xmlns:ooo=\"http://openoffice.org/2004/office\"
417 xmlns:ooow=\"http://openoffice.org/2004/writer\"
418 xmlns:oooc=\"http://openoffice.org/2004/calc\"
419 xmlns:dom=\"http://www.w3.org/2001/xml-events\"
420 xmlns:xforms=\"http://www.w3.org/2002/xforms\"
421 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
422 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
423 xmlns:rpt=\"http://openoffice.org/2005/report\"
424 xmlns:of=\"urn:oasis:names:tc:opendocument:xmlns:of:1.2\"
425 xmlns:xodt=\"http://www.w3.org/1999/xodt\"
426 xmlns:field=\"urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0\" office:version=\"1.2\">
429 (defun org-odt-begin-document-content (opt-plist)
431 (insert org-odt-document-content-header
)
434 (insert-file-contents
435 (or org-export-odt-automatic-styles-file
436 (expand-file-name "styles/OrgOdtAutomaticStyles.xml"
438 (goto-char (point-max)))
440 (defun org-odt-end-document-content ()
441 (org-lparse-insert-tag "</office:document-content>"))
443 (defun org-odt-begin-outline (level1 snumber title tags
444 target extra-targets class
)
446 'HEADING
(org-lparse-format
447 'HEADLINE title extra-targets tags snumber level1
)
450 (defun org-odt-end-outline ()
453 (defun org-odt-begin-outline-text (level1 snumber class
)
456 (defun org-odt-end-outline-text ()
459 (defun org-odt-begin-paragraph (&optional style
)
460 (org-lparse-insert-tag
461 "<text:p%s>" (org-odt-get-extra-attrs-for-paragraph-style style
)))
463 (defun org-odt-end-paragraph ()
464 (org-lparse-insert-tag "</text:p>"))
466 (defun org-odt-get-extra-attrs-for-paragraph-style (style)
470 ((stringp style
) style
)
471 ((symbolp style
) (org-odt-get-style-name-for-entity
474 (error "Don't know how to handle paragraph style %s" style
))
475 (format " text:style-name=\"%s\"" style-name
)))
477 (defun org-odt-format-stylized-paragraph (style text
)
479 '("<text:p%s>" .
"</text:p>") text
480 (org-odt-get-extra-attrs-for-paragraph-style style
)))
482 (defun org-odt-begin-environment (style)
484 ((blockquote verse center quote
)
485 (org-lparse-begin-paragraph style
)
488 (org-lparse-end-paragraph)
490 (t (error "Unknown environment %s" style
))))
492 (defun org-odt-end-environment (style)
494 ((blockquote verse center quote
)
495 (org-lparse-end-paragraph)
498 (org-lparse-begin-paragraph)
500 (t (error "Unknown environment %s" style
))))
502 (defun org-odt-begin-list (ltype &optional arg1
)
503 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
505 (let* ((style-name (org-odt-get-style-name-for-entity 'list ltype
))
506 (extra (if style-name
507 (format " text:style-name=\"%s\"" style-name
) "")))
509 ;; FIXME: Handle arg1 incase of ordered lists.
511 ((ordered unordered description
)
512 (org-lparse-end-paragraph)
513 (org-lparse-insert-tag "<text:list%s>" extra
))
514 (t (error "Unknown list type: %s" ltype
)))))
516 (defun org-odt-end-list (ltype)
517 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
520 (org-lparse-insert-tag "</text:list>")
521 (error "Unknown list type: %s" ltype
)))
523 (defun org-odt-begin-list-item (ltype &optional arg headline
)
524 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
528 (assert (not headline
) t
)
529 (let* ((counter arg
) (extra ""))
530 (org-lparse-insert-tag "<text:list-item>")
531 (org-lparse-begin-paragraph)))
533 (let* ((id arg
) (extra ""))
534 (org-lparse-insert-tag "<text:list-item>")
535 (org-lparse-begin-paragraph)
536 (insert (if headline
(org-odt-format-target headline id
)
537 (org-odt-format-bookmark "" id
)))))
539 (assert (not headline
) t
)
540 (let ((term (or arg
"(no term)")))
543 '("<text:list-item>" .
"</text:list-item>")
544 (org-odt-format-stylized-paragraph 'definition-term term
)))
545 (org-lparse-begin 'LIST-ITEM
'unordered
)
546 (org-lparse-begin 'LIST
'description
)
547 (org-lparse-begin 'LIST-ITEM
'unordered
)))
548 (t (error "Unknown list type"))))
550 (defun org-odt-end-list-item (ltype)
551 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
555 (org-lparse-insert-tag "</text:list-item>"))
557 (org-lparse-end-list-item)
558 (org-lparse-end 'LIST
'description
)
559 (org-lparse-end-list-item))
560 (t (error "Unknown list type"))))
562 ;; Following variables are let bound when table emission is in
563 ;; progress. See org-lparse.el.
564 (defvar org-lparse-table-begin-marker
)
565 (defvar org-lparse-table-ncols
)
566 (defvar org-lparse-table-rowgrp-open
)
567 (defvar org-lparse-table-rownum
)
568 (defvar org-lparse-table-cur-rowgrp-is-hdr
)
569 (defvar org-lparse-table-is-styled
)
570 (defvar org-lparse-table-rowgrp-info
)
571 (defvar org-lparse-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"))))
578 (org-lparse-insert-tag
579 "<table:table table:name=\"%s\" table:style-name=\"%s\">"
580 (or label
"") "OrgTable")
581 (setq org-lparse-table-begin-marker
(point)))
583 (defun org-odt-end-table ()
584 (goto-char org-lparse-table-begin-marker
)
585 (loop for level from
0 below org-lparse-table-ncols
586 do
(insert (org-odt-format-tags "<table:table-column/>" "")))
588 ;; fill style attributes for table cells
589 (when org-lparse-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-lparse-insert-tag "</table:table>"))
605 (defun org-odt-begin-table-rowgroup (&optional is-header-row
)
606 (when org-lparse-table-rowgrp-open
607 (org-lparse-end 'TABLE-ROWGROUP
))
608 (org-lparse-insert-tag (if is-header-row
609 "<table:table-header-rows>"
610 "<table:table-rows>"))
611 (setq org-lparse-table-rowgrp-open t
)
612 (setq org-lparse-table-cur-rowgrp-is-hdr is-header-row
))
614 (defun org-odt-end-table-rowgroup ()
615 (when org-lparse-table-rowgrp-open
616 (setq org-lparse-table-rowgrp-open nil
)
617 (org-lparse-insert-tag
618 (if org-lparse-table-cur-rowgrp-is-hdr
619 "</table:table-header-rows>" "</table:table-rows>"))))
621 (defun org-odt-format-table-row (row)
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-lparse-table-rowgrp-info
)) :start
) "T")
632 (when (= r org-lparse-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-lparse-table-colalign-vector c
)))
642 (defun org-odt-format-table-cell (data r c
)
643 (if (not org-lparse-table-is-styled
)
645 '("<table:table-cell>" .
"</table:table-cell>")
646 (org-odt-format-stylized-paragraph
648 (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
649 ((and (= c
0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS
))
651 (t "OrgTableContents"))
653 (let* ((style-name-cookie
654 (format "@@table-cell:style-name@@%03d@@%03d@@" r c
))
655 (paragraph-style-cookie
658 (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
659 ((and (= c
0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS
))
661 (t "OrgTableContents"))
662 (format "@@table-cell:p@@%03d@@%03d@@" r c
))))
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
))))
669 (defun org-odt-begin-footnote-definition (n)
670 (org-lparse-begin-paragraph 'footnote
))
672 (defun org-odt-end-footnote-definition (n)
673 (org-lparse-end-paragraph))
675 (defun org-odt-begin-toc (lang-specific-heading)
678 <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
679 <text:table-of-content-source text:outline-level=\"10\">
680 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
681 " lang-specific-heading
))
683 (loop for level from
1 upto
10
686 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
687 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
688 <text:index-entry-chapter/>
689 <text:index-entry-text/>
690 <text:index-entry-link-end/>
691 </text:table-of-content-entry-template>
695 </text:table-of-content-source>
698 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
699 <text:p text:style-name=\"Contents_20_Heading\">Table of Contents</text:p>
703 (defun org-odt-end-toc ()
706 </text:table-of-content>
709 (defun org-odt-format-toc-entry (snumber todo headline tags href
)
710 (setq headline
(concat
711 (and org-export-with-section-numbers
712 (concat snumber
". "))
716 (org-lparse-format 'SPACES
3)
717 (org-lparse-format 'FONTIFY tags
"tag")))))
719 (setq headline
(org-lparse-format 'FONTIFY headline
"todo")))
721 (let ((org-odt-suppress-xref t
))
722 (org-odt-format-link headline
(concat "#" href
))))
724 (defun org-odt-format-toc-item (toc-entry level org-last-level
)
725 (let ((style (format "Contents_20_%d"
726 (+ level
(or (org-lparse-get 'TOPLEVEL-HLEVEL
) 1) -
1))))
727 (insert "\n" (org-odt-format-stylized-paragraph style toc-entry
) "\n")))
729 ;; Following variable is let bound during 'ORG-LINK callback. See
731 (defvar org-lparse-link-description-is-image nil
)
732 (defun org-odt-format-link (desc href
&optional attr
)
734 ((and (= (string-to-char href
) ?
#) (not org-odt-suppress-xref
))
735 (setq href
(concat org-export-odt-bookmark-prefix
(substring href
1)))
737 '("<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"%s\">" .
738 "</text:bookmark-ref>")
740 (org-lparse-link-description-is-image
742 '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" .
"</draw:a>")
743 desc href
(or attr
"")))
746 '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" .
"</text:a>")
747 desc href
(or attr
"")))))
749 (defun org-odt-format-spaces (n)
750 (org-odt-format-tags "<text:s text:c=\"%d\"/>" "" n
))
752 (defun org-odt-format-tabs (&optional n
)
753 (let ((tab "<text:tab/>")
757 (defun org-odt-format-line-break ()
758 (org-odt-format-tags "<text:line-break/>" ""))
760 (defun org-odt-format-horizontal-line ()
761 (org-odt-format-stylized-paragraph 'horizontal-line
""))
763 (defun org-odt-format-line (line)
764 (case org-lparse-dyn-current-environment
765 (fixedwidth (concat (org-odt-format-source-code-or-example-line
766 (org-xml-encode-plain-text line
)) "\n"))
767 (t (concat line
"\n"))))
769 (defun org-odt-format-comment (fmt &rest args
)
770 (let ((comment (apply 'format fmt args
)))
771 (format "\n<!-- %s -->\n" comment
)))
773 (defun org-odt-format-org-entity (wd)
774 ;; FIXME: Seems to work. But is this correct?
775 (let ((s (org-entity-get-representation wd
'utf8
)))
776 (and s
(format "&#x%x;" (string-to-char s
)))))
778 (defun org-odt-fill-tabs-and-spaces (line)
779 (replace-regexp-in-string
780 "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
782 ((string= s
"\t") (org-odt-format-tabs))
784 (org-odt-format-spaces (length s
)))
787 (defun org-odt-format-source-code-or-example-line (line)
788 (org-odt-format-stylized-paragraph 'src
(org-odt-fill-tabs-and-spaces line
)))
790 (defun org-odt-format-example (lines)
793 (org-odt-format-source-code-or-example-line line
))
794 (org-split-string lines
"[\r\n]") "\n"))
796 (defun org-odt-format-source-code-or-example (lines lang caption textareap
799 (org-odt-format-example (org-export-number-lines
800 (org-xml-encode-plain-text-lines lines
)
801 0 0 num cont rpllbl fmt
)))
803 (defun org-xml-encode-plain-text-lines (rtn)
804 (mapconcat 'org-xml-encode-plain-text
(org-split-string rtn
"[\r\n]") "\n"))
806 (defun org-odt-remap-stylenames (style-name)
808 (cdr (assoc style-name
'(("timestamp-wrapper" .
"OrgTimestampWrapper")
809 ("timestamp" .
"OrgTimestamp")
810 ("timestamp-kwd" .
"OrgTimestampKeyword")
814 ("target" .
"OrgTarget"))))
817 (defun org-odt-format-fontify (text style
&optional id
)
821 (org-odt-remap-stylenames style
))
823 (org-odt-get-style-name-for-entity 'character style
))
825 (assert (< 1 (length style
)))
826 (let ((parent-style (pop style
)))
827 (mapconcat (lambda (s)
828 ;; (assert (stringp s) t)
829 (org-odt-remap-stylenames s
)) style
"")
830 (org-odt-remap-stylenames parent-style
)))
831 (t (error "Don't how to handle style %s" style
)))))
833 '("<text:span text:style-name=\"%s\">" .
"</text:span>")
836 (defun org-odt-relocate-relative-path (path dir
)
837 (if (file-name-absolute-p path
) path
838 (file-relative-name (expand-file-name path dir
)
839 (expand-file-name "eyecandy" dir
))))
841 (defun org-odt-format-inline-image (thefile)
842 (let* ((thelink (if (file-name-absolute-p thefile
) thefile
844 (org-odt-relocate-relative-path
845 thefile org-current-export-file
))))
848 "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
849 (if org-export-odt-embed-images
850 (org-odt-copy-image-file thefile
) thelink
))))
851 (org-export-odt-format-image thefile href
)))
853 (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
856 OPT-PLIST is an options list.
857 TYPE is the device-type of the link (THIS://foo.html)
858 PATH is the path of the link (http://THIS#locationx)
859 FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
860 DESC is the link description, if any.
861 ATTR is a string of other attributes of the a element.
862 MAY-INLINE-P allows inlining it as an image."
864 (declare (special org-lparse-par-open
))
867 (and (member type-1
'("http" "https" "file"))
868 (org-lparse-should-inline-p path descp
)
870 (type (if (equal type-1
"id") "file" type-1
))
875 ;; check for inlined images
876 ((and (member type
'("file"))
879 filename org-odt-export-inline-image-extensions
)
880 (or (eq t org-odt-export-inline-images
)
881 (and org-odt-export-inline-images
(not descp
))))
883 ;; (when (and (string= type "file") (file-name-absolute-p path))
884 ;; (setq thefile (concat "file://" (expand-file-name path))))
885 ;; (setq thefile (org-xml-format-href thefile))
886 ;; (org-export-html-format-image thefile)
887 (org-odt-format-inline-image thefile
))
889 (when (string= type
"file")
892 ((file-name-absolute-p path
)
893 (concat "file://" (expand-file-name path
)))
894 (t (org-odt-relocate-relative-path
895 thefile org-current-export-file
)))))
897 (when (and (member type
'("" "http" "https" "file" "coderef"))
899 (setq thefile
(concat thefile
"#" fragment
)))
901 (setq thefile
(org-xml-format-href thefile
))
903 (when (not (member type
'("" "file" "coderef")))
904 (setq thefile
(concat type
":" thefile
)))
906 (let ((org-odt-suppress-xref (string= type
"coderef")))
908 (org-xml-format-desc desc
) thefile attr
)))))))
910 (defun org-odt-format-heading (text level
&optional id
)
911 (let* ((text (if id
(org-odt-format-target text id
) text
)))
913 '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
914 "</text:h>") text level level
)))
916 (defun org-odt-format-headline (title extra-targets tags
917 &optional snumber level
)
919 (org-lparse-format 'EXTRA-TARGETS extra-targets
)
921 ;; No need to generate section numbers. They are auto-generated by
924 ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
926 (and tags
(concat (org-lparse-format 'SPACES
3)
927 (org-lparse-format 'ORG-TAGS tags
)))))
929 (defun org-odt-format-anchor (text name
&optional class
)
930 (org-odt-format-target text name
))
932 (defun org-odt-format-bookmark (text id
)
934 (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id
)
937 (defun org-odt-format-target (text id
)
938 (let ((name (concat org-export-odt-bookmark-prefix id
)))
940 (and id
(org-odt-format-tags
941 "<text:bookmark-start text:name=\"%s\"/>" "" name
))
942 (org-odt-format-bookmark text id
)
943 (and id
(org-odt-format-tags
944 "<text:bookmark-end text:name=\"%s\"/>" "" name
)))))
946 (defun org-odt-format-footnote (n def
)
947 (let ((id (concat "fn" n
))
948 (note-class "footnote")
949 (par-style "Footnote"))
951 '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
955 '("<text:note-citation>" .
"</text:note-citation>")
958 '("<text:note-body>" .
"</text:note-body>")
962 (defun org-odt-format-footnote-reference (n def refcnt
)
964 (org-odt-format-footnote n def
)
965 (org-odt-format-footnote-ref n
)))
967 (defun org-odt-format-footnote-ref (n)
968 (let ((note-class "footnote")
970 (ref-name (concat "fn" n
)))
972 '("<text:span text:style-name=\"%s\">" .
"</text:span>")
974 '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" .
"</text:note-ref>")
975 n note-class ref-format ref-name
)
978 (defun org-odt-get-image-name (file-name)
982 (concat (sha1 file-name
) "." (file-name-extension file-name
)) "Pictures")))
984 (defun org-export-odt-format-image (src href
987 "Create image tag with source and attributes."
990 (let (embed-as caption attr label attr-plist size width height
)
993 ((string-match "^ltxpng/" src
)
994 ;; FIXME: Anyway the latex src can be embedded as an
997 ;; (org-find-text-property-in-string 'org-latex-src src)
998 (setq caption nil attr nil label nil embed-as
'character
))
1001 (setq caption
(org-find-text-property-in-string 'org-caption src
)
1002 caption
(and caption
(org-xml-format-desc caption
))
1003 attr
(org-find-text-property-in-string 'org-attributes src
)
1004 label
(org-find-text-property-in-string 'org-label src
)
1005 embed-as
'paragraph
)))
1007 (setq attr-plist
(when attr
(read attr
)))
1008 (setq size
(org-odt-image-size-from-file
1009 src
(plist-get attr-plist
:width
)
1010 (plist-get attr-plist
:height
)
1011 (plist-get attr-plist
:scale
) nil embed-as
))
1013 (org-export-odt-do-format-image embed-as caption attr label
1016 (defun org-export-odt-do-format-image (embed-as caption attr label
1018 "Create image tag with source and attributes."
1020 (let ((width (car size
)) (height (cdr size
))
1022 '("<draw:frame draw:style-name=\"%s\"
1023 text:anchor-type=\"%s\"
1024 draw:z-index=\"%d\" %s>" .
"</draw:frame>")))
1026 ((and (not caption
) (not label
))
1027 (let (style-name anchor-type
)
1029 ((eq embed-as
'paragraph
)
1030 (setq style-name
"OrgGraphicsParagraph" anchor-type
"paragraph"))
1031 ((eq embed-as
'character
)
1032 (setq style-name
"OrgGraphicsBaseline" anchor-type
"as-char")))
1033 (org-odt-format-tags
1034 draw-frame-pair href style-name anchor-type
0
1035 (org-odt-image-attrs-from-size width height
))))
1039 ;; (when par-open (org-odt-close-par))
1040 (org-odt-format-tags
1042 (org-odt-format-tags
1043 '("<draw:text-box fo:min-height=\"%dcm\">" .
"</draw:text-box>")
1044 (org-odt-format-stylized-paragraph
1047 (let ((extra " style:rel-width=\"100%\" style:rel-height=\"scale\""))
1048 (org-odt-format-tags
1049 draw-frame-pair href
"OrgGraphicsParagraphContent" "paragraph" 2
1050 (concat (org-odt-image-attrs-from-size width height
) extra
)))
1051 (org-odt-format-entity-caption label caption
)))
1053 "OrgFrame" "paragraph" 1
1054 (org-odt-image-attrs-from-size width
))
1056 ;; (when par-open (org-odt-open-par))
1059 ;; xml files generated on-the-fly
1060 (defconst org-export-odt-save-list
1061 '("META-INF/manifest.xml" "content.xml" "meta.xml" "styles.xml"))
1063 ;; xml files that are copied
1064 (defconst org-export-odt-nosave-list
'())
1066 ;; xml files that contribute to the final odt file
1067 (defvar org-export-odt-file-list nil
)
1069 (defconst org-export-odt-manifest-lines
1070 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1071 "<manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">"
1072 "<manifest:file-entry manifest:media-type=\"application/vnd.oasis.opendocument.text\" manifest:version=\"1.2\" manifest:full-path=\"/\"/>"
1073 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"content.xml\"/>"
1074 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"styles.xml\"/>"
1075 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"meta.xml\"/>"
1076 "<manifest:file-entry manifest:media-type=\"\" manifest:full-path=\"Pictures/\"/>") .
("</manifest:manifest>")))
1078 (defconst org-export-odt-meta-lines
1079 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1080 "<office:document-meta"
1081 " xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\""
1082 " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
1083 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\""
1084 " xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\""
1085 " xmlns:ooo=\"http://openoffice.org/2004/office\" "
1086 " office:version=\"1.2\">"
1087 " <office:meta>") .
(" </office:meta>" "</office:document-meta>")))
1089 (defun org-odt-copy-image-file (path &optional target-file
)
1090 "Returns the internal name of the file"
1091 (let* ((image-type (file-name-extension path
))
1092 (media-type (format "image/%s" image-type
))
1093 (src-file (expand-file-name
1094 path
(file-name-directory org-current-export-file
)))
1095 (target-file (or target-file
(org-odt-get-image-name src-file
)))
1099 (when (not org-lparse-to-buffer
)
1100 (message "Embedding %s as %s ..."
1101 (substring-no-properties path
) target-file
)
1102 (copy-file src-file target-file
'overwrite
)
1103 (org-odt-update-manifest-file media-type target-file
)
1104 (push target-file org-export-odt-file-list
)) target-file
))
1106 (defun org-odt-image-attrs-from-size (&optional width height
)
1108 (when width
(format "svg:width=\"%0.2fcm\"" width
))
1110 (when height
(format "svg:height=\"%0.2fcm\"" height
))))
1112 (defvar org-export-odt-image-size-probe-method
1113 '(emacs imagemagick force
)
1114 "Ordered list of methods by for determining size of an embedded
1117 (defvar org-export-odt-default-image-sizes-alist
1118 '(("character" .
(5 .
0.4))
1119 ("paragraph" .
(5 .
5)))
1120 "Hardcoded image dimensions one for each of the anchor
1123 (defun org-odt-do-image-size (probe-method file
&optional dpi anchor-type
)
1124 (setq dpi
(or dpi org-export-odt-pixels-per-inch
))
1125 (setq anchor-type
(or anchor-type
"paragraph"))
1126 (flet ((size-in-cms (size-in-pixels)
1127 (flet ((pixels-to-cms (pixels)
1128 (let* ((cms-per-inch 2.54)
1129 (inches (/ pixels dpi
)))
1130 (* cms-per-inch inches
))))
1132 (cons (pixels-to-cms (car size-in-pixels
))
1133 (pixels-to-cms (cdr size-in-pixels
)))))))
1136 (size-in-cms (ignore-errors (image-size (create-image file
) 'pixels
))))
1139 (let ((dim (shell-command-to-string
1140 (format "identify -format \"%%w:%%h\" \"%s\"" file
))))
1141 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim
)
1142 (cons (string-to-number (match-string 1 dim
))
1143 (string-to-number (match-string 2 dim
)))))))
1145 (cdr (assoc-string anchor-type
1146 org-export-odt-default-image-sizes-alist
))))))
1148 (defun org-odt-image-size-from-file (file &optional user-width
1149 user-height scale dpi embed-as
)
1150 (unless (file-name-absolute-p file
)
1151 (setq file
(expand-file-name
1152 file
(file-name-directory org-current-export-file
))))
1153 (let* (size width height
)
1154 (unless (and user-height user-width
)
1155 (loop for probe-method in org-export-odt-image-size-probe-method
1157 do
(setq size
(org-odt-do-image-size
1158 probe-method file dpi embed-as
)))
1159 (or size
(error "Cannot determine Image size. Aborting ..."))
1160 (setq width
(car size
) height
(cdr size
)))
1163 (setq width
(* width scale
) height
(* height scale
)))
1164 ((and user-height user-width
)
1165 (setq width user-width height user-height
))
1167 (setq width
(* user-height
(/ width height
)) height user-height
))
1169 (setq height
(* user-width
(/ height width
)) width user-width
))
1171 (cons width height
)))
1173 (defvar org-odt-default-entity
"Illustration")
1174 (defun org-odt-format-entity-caption (label caption
&optional default-entity
)
1175 (if (not label
) (or caption
"")
1176 (let* ((label-components (org-odt-parse-label label
))
1177 (entity (car label-components
))
1178 (seqno (cdr label-components
))
1179 (caption (and caption
(concat ": " caption
))))
1182 entity
(or default-entity org-odt-default-entity
)))
1185 (org-odt-format-tags
1186 '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" .
"</text:sequence>")
1187 seqno label entity entity
)
1190 (defun org-odt-format-tags (tag text
&rest args
)
1191 (let ((prefix (when org-lparse-encode-pending
"@"))
1192 (suffix (when org-lparse-encode-pending
"@")))
1193 (apply 'org-lparse-format-tags tag text prefix suffix args
)))
1195 (defun org-odt-init-outfile (filename)
1196 (let* ((outdir (make-temp-file org-export-odt-tmpdir-prefix t
))
1197 (mimetype-file (expand-file-name "mimetype" outdir
))
1198 (content-file (expand-file-name "content.xml" outdir
))
1199 (manifest-file (expand-file-name "META-INF/manifest.xml" outdir
))
1200 (meta-file (expand-file-name "meta.xml" outdir
))
1201 (styles-file (expand-file-name "styles.xml" outdir
))
1202 (pictures-dir (expand-file-name "Pictures" outdir
))
1206 (with-current-buffer (find-file-noselect content-file t
)
1209 ;; FIXME: How to factor in body-only here
1212 (make-directory (file-name-directory manifest-file
))
1213 (with-current-buffer (find-file-noselect manifest-file t
)
1215 (insert (mapconcat 'identity
(car org-export-odt-manifest-lines
) "\n"))
1218 (insert (mapconcat 'identity
(cdr org-export-odt-manifest-lines
) "\n"))))
1221 (with-current-buffer (find-file-noselect meta-file t
)
1223 (insert (mapconcat 'identity
(car org-export-odt-meta-lines
) "\n"))
1226 (insert (mapconcat 'identity
(cdr org-export-odt-meta-lines
) "\n"))))
1229 ;; (copy-file org-export-odt-styles-file styles-file t)
1232 (make-directory pictures-dir
)
1234 ;; initialize list of files that contribute to the odt file
1235 (setq org-export-odt-file-list
1236 (append org-export-odt-save-list org-export-odt-nosave-list
)))
1239 (defconst org-export-odt-mimetype-lines
1240 '("application/vnd.oasis.opendocument.text"))
1242 (defconst org-odt-manifest-file-entry-tag
1243 "<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"/>")
1245 (defun org-odt-save-as-outfile (target opt-plist
)
1247 (org-odt-update-meta-file opt-plist
)
1249 ;; write styles file
1250 (org-odt-copy-styles-file)
1252 ;; Update styles.xml - take care of outline numbering
1253 (with-current-buffer
1254 (find-file-noselect (expand-file-name "styles.xml") t
)
1255 ;; Don't make automatic backup of styles.xml file. This setting
1256 ;; prevents the backedup styles.xml file from being zipped in to
1257 ;; odt file. This is more of a hackish fix. Better alternative
1258 ;; would be to fix the zip command so that the output odt file
1259 ;; includes only the needed files and excludes any auto-generated
1260 ;; extra files like backups and auto-saves etc etc. Note that
1261 ;; currently the zip command zips up the entire temp directory so
1262 ;; that any auto-generated files created under the hood ends up in
1263 ;; the resulting odt file.
1264 (set (make-local-variable 'backup-inhibited
) t
)
1266 ;; Import local setting of `org-export-with-section-numbers'
1267 (org-lparse-bind-local-variables opt-plist
)
1268 (org-odt-configure-outline-numbering
1269 (if org-export-with-section-numbers org-export-headline-levels
0)))
1271 (let ((zipdir default-directory
))
1272 (message "Switching to directory %s" (expand-file-name zipdir
))
1274 ;; save all xml files
1275 (mapc (lambda (file)
1276 (with-current-buffer
1277 (find-file-noselect (expand-file-name file
) t
)
1279 (indent-region (point-min) (point-max))
1281 org-export-odt-save-list
)
1283 (let* ((target-name (file-name-nondirectory target
))
1284 (target-dir (file-name-directory target
))
1285 (cmd (format "zip -rmTq %s %s" target-name
".")))
1286 (when (file-exists-p target
)
1287 ;; FIXME: If the file is locked this throws a cryptic error
1288 (delete-file target
))
1290 (let ((coding-system-for-write 'no-conversion
) exitcode
)
1291 (message "Creating odt file using \"%s\"" cmd
)
1293 (apply 'call-process
1298 (append (list "-rmTq") (list target-name
"."))))
1300 (or (zerop exitcode
)
1301 (error "Unable to create odt file (%S)" exitcode
)))
1303 ;; move the file from outdir to target-dir
1304 (rename-file target-name target-dir
)
1306 ;; kill all xml buffers
1307 (mapc (lambda (file)
1309 (find-file-noselect (expand-file-name file zipdir
) t
)))
1310 org-export-odt-save-list
)
1312 (delete-directory zipdir
)))
1314 (message "Created %s" target
)
1315 (set-buffer (find-file-noselect target t
)))
1317 (defun org-odt-update-meta-file (opt-plist)
1318 (with-current-buffer
1319 (find-file-noselect (expand-file-name "meta.xml") t
)
1320 (let ((date (or (plist-get opt-plist
:effective-date
) ""))
1321 (author (or (plist-get opt-plist
:author
) ""))
1322 (email (plist-get opt-plist
:email
))
1323 (keywords (plist-get opt-plist
:keywords
))
1324 (description (plist-get opt-plist
:description
))
1325 (title (plist-get opt-plist
:title
)))
1329 (org-odt-format-tags '("<dc:creator>" .
"</dc:creator>") author
)
1330 (org-odt-format-tags
1331 '("\n<meta:initial-creator>" .
"</meta:initial-creator>") author
)
1332 (org-odt-format-tags '("\n<dc:date>" .
"</dc:date>") date
)
1333 (org-odt-format-tags
1334 '("\n<meta:creation-date>" .
"</meta:creation-date>") date
)
1335 (org-odt-format-tags '("\n<meta:generator>" .
"</meta:generator>")
1336 (when org-export-creator-info
1337 (format "Org-%s/Emacs-%s"
1338 org-version emacs-version
)))
1339 (org-odt-format-tags '("\n<meta:keyword>" .
"</meta:keyword>") keywords
)
1340 (org-odt-format-tags '("\n<dc:subject>" .
"</dc:subject>") description
)
1341 (org-odt-format-tags '("\n<dc:title>" .
"</dc:title>") title
)
1344 (defun org-odt-update-manifest-file (media-type full-path
)
1345 (with-current-buffer
1346 (find-file-noselect (expand-file-name "META-INF/manifest.xml") t
)
1347 (insert (format org-odt-manifest-file-entry-tag media-type full-path
))))
1349 (defun org-odt-finalize-outfile ()
1350 (message "org-newodt: Finalizing outfile")
1351 (org-odt-delete-empty-paragraphs))
1353 (defun org-odt-delete-empty-paragraphs ()
1354 (goto-char (point-min))
1355 (let ((open "<text:p[^>]*>")
1356 (close "</text:p>"))
1357 (while (re-search-forward (format "%s[ \r\n\t]*%s" open close
) nil t
)
1358 (replace-match ""))))
1360 (defun org-odt-get (what &optional opt-plist
)
1363 (EXPORT-DIR (org-export-directory :html opt-plist
))
1364 (FILE-NAME-EXTENSION "odt")
1365 (EXPORT-BUFFER-NAME "*Org ODT Export*")
1366 (ENTITY-CONTROL org-odt-entity-control-callbacks-alist
)
1367 (ENTITY-FORMAT org-odt-entity-format-callbacks-alist
)
1368 (INIT-METHOD 'org-odt-init-outfile
)
1369 (FINAL-METHOD 'org-odt-finalize-outfile
)
1370 (SAVE-METHOD 'org-odt-save-as-outfile
)
1372 '("bib" "doc" "doc6" "doc95" "html" "xhtml" "latex" "odt" "ott" "pdf" "rtf"
1373 "sdw" "sdw3" "sdw4" "stw " "sxw" "mediawiki" "text" "txt" "uot" "vor"
1374 "vor3" "vor4" "docbook" "ooxml" "ppt" "odp"))
1375 (CONVERT-METHOD org-lparse-convert-process
)
1377 (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps
)
1378 (INLINE-IMAGES 'maybe
)
1379 (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
1380 (PLAIN-TEXT-MAP '(("&" .
"&") ("<" .
"<") (">" .
">")))
1381 (TABLE-FIRST-COLUMN-AS-LABELS nil
)
1382 (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY
"," 'superscript
))
1383 (CODING-SYSTEM-FOR-WRITE 'utf-8
)
1384 (CODING-SYSTEM-FOR-SAVE 'utf-8
)
1385 (t (error "Unknown property: %s" what
))))
1387 (defun org-odt-parse-label (label)
1389 (if (not (string-match "\\`[a-zA-Z]+:\\(.+\\)" label
))
1392 (capitalize (substring label
0 (1- (match-beginning 1))))
1393 (substring label
(match-beginning 1))))))
1395 (defvar org-lparse-latex-fragment-fallback
) ; set by org-do-lparse
1396 (defun org-export-odt-preprocess (parameters)
1397 "Convert LaTeX fragments to images."
1398 (when (and org-current-export-file
1399 (plist-get parameters
:LaTeX-fragments
))
1401 (concat "ltxpng/" (file-name-sans-extension
1402 (file-name-nondirectory
1403 org-current-export-file
)))
1404 org-current-export-dir nil
"Creating LaTeX image %s"
1407 ((eq (plist-get parameters
:LaTeX-fragments
) 'verbatim
) 'verbatim
)
1408 ;; Investigate MathToWeb for converting TeX equations to MathML
1409 ;; See http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01755.html
1410 ((or (eq (plist-get parameters
:LaTeX-fragments
) 'mathjax
)
1411 (eq (plist-get parameters
:LaTeX-fragments
) t
))
1414 "Use of MathJax is incompatible with ODT exporter. "
1415 (format "Using %S instead." org-lparse-latex-fragment-fallback
)))
1416 org-lparse-latex-fragment-fallback
)
1417 ((eq (plist-get parameters
:LaTeX-fragments
) 'dvipng
) 'dvipng
)
1419 (goto-char (point-min))
1420 (let (label label-components category value pretty-label
)
1421 (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t
)
1422 (org-if-unprotected-at (match-beginning 1)
1423 (setq label
(match-string 1)
1424 label-components
(org-odt-parse-label label
)
1425 category
(car label-components
)
1426 value
(cdr label-components
)
1427 pretty-label
(if value
(concat category
" " value
) label
))
1429 (let ((org-lparse-encode-pending t
))
1430 (org-odt-format-tags
1431 '("<text:sequence-ref text:reference-format=\"category-and-value\" text:ref-name=\"%s\">"
1432 .
"</text:sequence-ref>") pretty-label label
)) t t
)))))
1434 (declare-function archive-zip-extract
"arc-mode.el" (archive name
))
1435 (defun org-odt-zip-extract-one (archive member
&optional target
)
1437 (let* ((target (or target default-directory
))
1438 (archive (expand-file-name archive
))
1439 (archive-zip-extract
1440 (list "unzip" "-qq" "-o" "-d" target
))
1441 exit-code command-output
)
1442 (setq command-output
1444 (setq exit-code
(archive-zip-extract archive member
))
1446 (unless (zerop exit-code
)
1447 (message command-output
)
1448 (error "Extraction failed"))))
1450 (defun org-odt-zip-extract (archive members
&optional target
)
1451 (when (atom members
) (setq members
(list members
)))
1452 (mapc (lambda (member)
1453 (org-odt-zip-extract-one archive member target
))
1456 (defun org-odt-copy-styles-file (&optional styles-file
)
1457 ;; Non-availability of styles.xml is not a critical error. For now
1458 ;; throw an error purely for aesthetic reasons.
1459 (setq styles-file
(or styles-file
1460 org-export-odt-styles-file
1461 (expand-file-name "styles/OrgOdtStyles.xml"
1463 (error "org-odt: Missing styles file?")))
1465 ((listp styles-file
)
1466 (let ((archive (nth 0 styles-file
))
1467 (members (nth 1 styles-file
)))
1468 (org-odt-zip-extract archive members
)
1471 (when (org-file-image-p member
)
1472 (let* ((image-type (file-name-extension member
))
1473 (media-type (format "image/%s" image-type
)))
1474 (org-odt-update-manifest-file media-type member
))))
1476 ((and (stringp styles-file
) (file-exists-p styles-file
))
1477 (let ((styles-file-type (file-name-extension styles-file
)))
1479 ((string= styles-file-type
"xml")
1480 (copy-file styles-file
"styles.xml" t
))
1481 ((member styles-file-type
'("odt" "ott"))
1482 (org-odt-zip-extract styles-file
"styles.xml")))))
1484 (error (format "Invalid specification of styles.xml file: %S"
1485 org-export-odt-styles-file
)))))
1487 (defvar org-export-odt-factory-settings
1488 "d4328fb9d1b6cb211d4320ff546829f26700dc5e"
1489 "SHA1 hash of OrgOdtStyles.xml.")
1491 (defun org-odt-configure-outline-numbering (level)
1492 "Outline numbering is retained only upto LEVEL.
1493 To disable outline numbering pass a LEVEL of 0."
1494 (if (not (string= org-export-odt-factory-settings
(sha1 (current-buffer))))
1496 "org-odt: Using custom styles file? Consider tweaking styles.xml for better output. To suppress this warning update `org-export-odt-factory-settings'")
1497 (goto-char (point-min))
1499 "<text:outline-level-style\\(.*\\)text:level=\"\\(.*\\)\"\\(.*\\)>")
1501 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1502 (while (re-search-forward regex nil t
)
1503 (when (> (string-to-number (match-string 1)) level
)
1504 (replace-match replacement t nil
))))
1509 ;;; org-odt.el ends here