1 ;;; org-odt.el --- OpenDocumentText export for Org-mode
3 ;; Copyright (C) 2010-2011 Jambunathan <kjambunathan at gmail dot com>
5 ;; Author: Jambunathan K <kjambunathan at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
10 ;; This file is not (yet) part of GNU Emacs.
11 ;; However, it is distributed under the same license.
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 (eval-when-compile (require 'cl
))
33 (defun org-odt-end-export ()
34 ;; remove empty paragraphs
35 (goto-char (point-min))
36 (while (re-search-forward
37 "<text:p\\( text:style-name=\"Text_20_body\"\\)?>[ \r\n\t]*</text:p>"
40 (goto-char (point-min))
42 ;; Convert whitespace place holders
43 (goto-char (point-min))
45 (while (setq beg
(next-single-property-change (point) 'org-whitespace
))
46 (setq n
(get-text-property beg
'org-whitespace
)
47 end
(next-single-property-change beg
'org-whitespace
))
49 (delete-region beg end
)
50 (insert (format "<span style=\"visibility:hidden;\">%s</span>"
51 (make-string n ?x
)))))
53 ;; Remove empty lines at the beginning of the file.
54 (goto-char (point-min))
55 (when (looking-at "\\s-+\n") (replace-match ""))
57 ;; Remove display properties
58 (remove-text-properties (point-min) (point-max) '(display t
)))
60 (defvar org-odt-suppress-xref nil
)
61 (defconst org-export-odt-special-string-regexps
62 '(("\\\\-" .
"­\\1") ; shy
63 ("---\\([^-]\\)" .
"—\\1") ; mdash
64 ("--\\([^-]\\)" .
"–\\1") ; ndash
65 ("\\.\\.\\." .
"…")) ; hellip
66 "Regular expressions for special string conversion.")
68 (defconst org-odt-lib-dir
(file-name-directory load-file-name
))
69 (defconst org-odt-data-dir
70 (let ((dir1 (expand-file-name "../odt" org-odt-lib-dir
)) ; git
71 (dir2 (expand-file-name "./contrib/odt" org-odt-lib-dir
))) ; elpa
73 ((file-directory-p dir1
) dir1
)
74 ((file-directory-p dir2
) dir2
)
75 (t (error "Cannot find factory styles file. Check package dir layout"))))
76 "Directory that holds auxiliary files used by the ODT exporter.
78 The 'styles' subdir contains the following xml files -
79 'OrgOdtStyles.xml' and 'OrgOdtAutomaticStyles.xml' - which are
80 used as factory settings of `org-export-odt-styles-file' and
81 `org-export-odt-automatic-styles-file'.
83 The 'etc/schema' subdir contains rnc files for validating of
84 OpenDocument xml files.")
86 (defvar org-odt-file-extensions
87 '(("odt" .
"OpenDocument Text")
88 ("ott" .
"OpenDocument Text Template")
89 ("odm" .
"OpenDocument Master Document")
90 ("ods" .
"OpenDocument Spreadsheet")
91 ("ots" .
"OpenDocument Spreadsheet Template")
92 ("odg" .
"OpenDocument Drawing (Graphics)")
93 ("otg" .
"OpenDocument Drawing Template")
94 ("odp" .
"OpenDocument Presentation")
95 ("otp" .
"OpenDocument Presentation Template")
96 ("odi" .
"OpenDocument Image")
97 ("odf" .
"OpenDocument Formula")
98 ("odc" .
"OpenDocument Chart")
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 (defvar org-odt-ms-file-extensions
107 '(("doc" .
"Microsoft Text")
108 ("docx" .
"Microsoft Text")
109 ("xls" .
"Microsoft Spreadsheet")
110 ("xlsx" .
"Microsoft Spreadsheet")
111 ("ppt" .
"Microsoft Presentation")
112 ("pptx" .
"Microsoft Presentation")))
114 ;; RelaxNG validation of OpenDocument xml files
115 (eval-after-load 'rng-nxml
116 '(setq rng-nxml-auto-validate-flag t
))
118 (eval-after-load 'rng-loc
119 '(add-to-list 'rng-schema-locating-files
120 (expand-file-name "etc/schema/schemas.xml" org-odt-data-dir
)))
124 ;; Let Org open all OpenDocument files using system-registered app
125 (add-to-list 'org-file-apps
126 (cons (concat "\\." (car desc
) "\\'") 'system
))
127 ;; Let Emacs open all OpenDocument files in archive mode
128 (add-to-list 'auto-mode-alist
129 (cons (concat "\\." (car desc
) "\\'") 'archive-mode
)))
130 org-odt-file-extensions
)
134 ;; Let Org open all Microsoft files using system-registered app
135 (add-to-list 'org-file-apps
136 (cons (concat "\\." (car desc
) "\\'") 'system
)))
137 org-odt-ms-file-extensions
)
139 ;; register the odt exporter with the pre-processor
140 (add-to-list 'org-export-backends
'odt
)
142 ;; register the odt exporter with org-lparse library
143 (org-lparse-register-backend 'odt
)
145 (defun org-odt-unload-function ()
146 ;; notify org-lparse library on unload
147 (org-lparse-unregister-backend 'odt
)
150 (defcustom org-export-odt-automatic-styles-file nil
151 "Automatic styles for use with ODT exporter.
152 If unspecified, the file under `org-odt-data-dir' is used."
154 :group
'org-export-odt
)
156 (defcustom org-export-odt-styles-file nil
157 "Default styles file for use with ODT export.
158 Valid values are one of:
160 2. path to a styles.xml file
161 3. path to a *.odt or a *.ott file
162 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
165 In case of option 1, an in-built styles.xml is used. See
166 `org-odt-data-dir' for more information.
168 In case of option 3, the specified file is unzipped and the
169 styles.xml embedded therein is used.
171 In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
172 and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
173 generated odt file. Use relative path for specifying the
174 FILE-MEMBERS. styles.xml must be specified as one of the
177 Use options 1, 2 or 3 only if styles.xml alone suffices for
178 achieving the desired formatting. Use option 4, if the styles.xml
179 references additional files like header and footer images for
180 achieving the desired formattting."
181 :group
'org-export-odt
184 (const :tag
"Factory settings" nil
)
185 (file :must-match t
:tag
"styles.xml")
186 (file :must-match t
:tag
"ODT or OTT file")
187 (list :tag
"ODT or OTT file + Members"
188 (file :must-match t
:tag
"ODF Text or Text Template file")
190 (file :tag
" Member" "styles.xml")
191 (repeat (file :tag
"Member"))))))
193 (defconst org-export-odt-tmpdir-prefix
"odt-")
194 (defconst org-export-odt-bookmark-prefix
"OrgXref.")
195 (defcustom org-export-odt-use-bookmarks-for-internal-links t
196 "Export Internal links as bookmarks?."
198 :group
'org-export-odt
)
200 (defcustom org-export-odt-embed-images t
201 "Should the images be copied in to the odt file or just linked?"
203 :group
'org-export-odt
)
205 (defcustom org-odt-export-inline-images
'maybe
206 "Non-nil means inline images into exported HTML pages.
207 This is done using an <img> tag. When nil, an anchor with href is used to
208 link to the image. If this option is `maybe', then images in links with
209 an empty description will be inlined, while images with a description will
211 :group
'org-odt-export
212 :type
'(choice (const :tag
"Never" nil
)
213 (const :tag
"Always" t
)
214 (const :tag
"When there is no description" maybe
)))
216 (defcustom org-odt-export-inline-image-extensions
217 '("png" "jpeg" "jpg" "gif")
218 "Extensions of image files that can be inlined into HTML."
219 :type
'(repeat (string :tag
"Extension"))
220 :group
'org-odt-export
)
222 (defcustom org-export-odt-pixels-per-inch display-pixels-per-inch
223 ;; FIXME add docstring
226 :group
'org-export-odt
)
228 (defvar org-export-odt-default-org-styles-alist
229 '((paragraph .
((default .
"Text_20_body")
230 (fixedwidth .
"OrgSourceBlock")
232 (quote .
"Quotations")
233 (blockquote .
"Quotations")
234 (center .
"OrgCenter")
237 (title .
"Heading_20_1.title")
238 (footnote .
"Footnote")
239 (src .
"OrgSourceBlock")
240 (illustration .
"Illustration")
242 (definition-term .
"Text_20_body_20_bold")
243 (horizontal-line .
"Horizontal_20_Line")))
244 (character .
((bold .
"Bold")
245 (emphasis .
"Emphasis")
247 (verbatim .
"OrgCode")
248 (strike .
"Strikethrough")
249 (underline .
"Underline")
250 (subscript .
"OrgSubscript")
251 (superscript .
"OrgSuperscript")))
252 (list .
((ordered .
"OrgNumberedList")
253 (unordered .
"OrgBulletedList")
254 (description .
"OrgDescriptionList"))))
255 "Default styles for various entities.")
257 (defvar org-export-odt-org-styles-alist org-export-odt-default-org-styles-alist
)
258 (defun org-odt-get-style-name-for-entity (category &optional entity
)
259 (let ((entity (or entity
'default
)))
261 (cdr (assoc entity
(cdr (assoc category
262 org-export-odt-org-styles-alist
))))
263 (cdr (assoc entity
(cdr (assoc category
264 org-export-odt-default-org-styles-alist
))))
265 (error "Cannot determine style name for entity %s of type %s"
268 (defcustom org-export-odt-preferred-output-format nil
269 "Automatically post-process to this format after exporting to \"odt\".
270 Interactive commands `org-export-as-odt' and
271 `org-export-as-odt-and-open' export first to \"odt\" format and
272 then use an external converter to convert the resulting document
275 The converter used is that specified with CONVERT-METHOD option
276 in `org-odt-get'. If the above option is unspecified then
277 `org-lparse-convert-process' is used.
279 The format specified here should be listed in OTHER-BACKENDS
280 option of `org-odt-get' or `org-lparse-convert-capabilities' as
283 :type
'(choice :convert-widget
285 (apply 'widget-convert
(widget-type w
)
286 (eval (car (widget-get w
:args
)))))
287 `((const :tag
"None" nil
)
288 ,@(mapcar (lambda (c)
289 `(const :tag
,(car c
) ,(car c
)))
290 (org-lparse-get-other-backends "odt")))))
293 (defun org-export-as-odt-and-open (arg)
294 "Export the outline as ODT and immediately open it with a browser.
295 If there is an active region, export only the region.
296 The prefix ARG specifies how many levels of the outline should become
297 headlines. The default is 3. Lower levels will become bulleted lists."
300 (or org-export-odt-preferred-output-format
"odt") "odt" arg
))
303 (defun org-export-as-odt-batch ()
304 "Call the function `org-lparse-batch'.
305 This function can be used in batch processing as:
307 --load=$HOME/lib/emacs/org.el
308 --eval \"(setq org-export-headline-levels 2)\"
309 --visit=MyFile --funcall org-export-as-odt-batch"
310 (org-lparse-batch "odt"))
313 (defun org-export-as-odt-to-buffer (arg)
314 "Call `org-lparse-odt` with output to a temporary buffer.
315 No file is created. The prefix ARG is passed through to `org-lparse-to-buffer'."
317 (org-lparse-to-buffer "odt" arg
))
320 (defun org-replace-region-by-odt (beg end
)
321 "Assume the current region has org-mode syntax, and convert it to ODT.
322 This can be used in any buffer. For example, you could write an
323 itemized list in org-mode syntax in an ODT buffer and then use this
324 command to convert it."
326 (org-replace-region-by "odt" beg end
))
329 (defun org-export-region-as-odt (beg end
&optional body-only buffer
)
330 "Convert region from BEG to END in org-mode buffer to ODT.
331 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
332 contents, and only produce the region of converted text, useful for
333 cut-and-paste operations.
334 If BUFFER is a buffer or a string, use/create that buffer as a target
335 of the converted ODT. If BUFFER is the symbol `string', return the
336 produced ODT as a string and leave not buffer behind. For example,
337 a Lisp program could call this function in the following way:
339 (setq odt (org-export-region-as-odt beg end t 'string))
341 When called interactively, the output buffer is selected, and shown
342 in a window. A non-interactive call will only return the buffer."
344 (org-lparse-region "odt" beg end body-only buffer
))
346 ;;; org-export-as-odt
348 (defun org-export-as-odt (arg &optional hidden ext-plist
349 to-buffer body-only pub-dir
)
350 "Export the outline as a OpenDocumentText file.
351 If there is an active region, export only the region. The prefix
352 ARG specifies how many levels of the outline should become
353 headlines. The default is 3. Lower levels will become bulleted
354 lists. HIDDEN is obsolete and does nothing.
355 EXT-PLIST is a property list with external parameters overriding
356 org-mode's default settings, but still inferior to file-local
357 settings. When TO-BUFFER is non-nil, create a buffer with that
358 name and export to that buffer. If TO-BUFFER is the symbol
359 `string', don't leave any buffer behind but just return the
360 resulting XML as a string. When BODY-ONLY is set, don't produce
361 the file header and footer, simply return the content of
362 <body>...</body>, without even the body tags themselves. When
363 PUB-DIR is set, use this as the publishing directory."
365 (org-lparse (or org-export-odt-preferred-output-format
"odt")
366 "odt" arg hidden ext-plist to-buffer body-only pub-dir
))
368 (defvar org-odt-entity-control-callbacks-alist
370 .
(org-odt-begin-export org-odt-end-export
))
372 .
(org-odt-begin-document-content org-odt-end-document-content
))
374 .
(org-odt-begin-document-body org-odt-end-document-body
))
376 .
(org-odt-begin-toc org-odt-end-toc
))
378 .
(org-odt-begin-environment org-odt-end-environment
))
380 .
(org-odt-begin-footnote-definition org-odt-end-footnote-definition
))
382 .
(org-odt-begin-table org-odt-end-table
))
384 .
(org-odt-begin-table-rowgroup org-odt-end-table-rowgroup
))
386 .
(org-odt-begin-list org-odt-end-list
))
388 .
(org-odt-begin-list-item org-odt-end-list-item
))
390 .
(org-odt-begin-outline org-odt-end-outline
))
392 .
(org-odt-begin-outline-text org-odt-end-outline-text
))
394 .
(org-odt-begin-paragraph org-odt-end-paragraph
)))
397 (defvar org-odt-entity-format-callbacks-alist
398 `((EXTRA-TARGETS . org-lparse-format-extra-targets
)
399 (ORG-TAGS . org-lparse-format-org-tags
)
400 (SECTION-NUMBER . org-lparse-format-section-number
)
401 (HEADLINE . org-odt-format-headline
)
402 (TOC-ENTRY . org-odt-format-toc-entry
)
403 (TOC-ITEM . org-odt-format-toc-item
)
404 (TAGS . org-odt-format-tags
)
405 (SPACES . org-odt-format-spaces
)
406 (TABS . org-odt-format-tabs
)
407 (LINE-BREAK . org-odt-format-line-break
)
408 (FONTIFY . org-odt-format-fontify
)
409 (TODO . org-lparse-format-todo
)
410 (LINK . org-odt-format-link
)
411 (INLINE-IMAGE . org-odt-format-inline-image
)
412 (ORG-LINK . org-odt-format-org-link
)
413 (HEADING . org-odt-format-heading
)
414 (ANCHOR . org-odt-format-anchor
)
415 (TABLE . org-lparse-format-table
)
416 (TABLE-ROW . org-odt-format-table-row
)
417 (TABLE-CELL . org-odt-format-table-cell
)
418 (FOOTNOTES-SECTION . ignore
)
419 (FOOTNOTE-REFERENCE . org-odt-format-footnote-reference
)
420 (HORIZONTAL-LINE . org-odt-format-horizontal-line
)
421 (COMMENT . org-odt-format-comment
)
422 (LINE . org-odt-format-line
)
423 (ORG-ENTITY . org-odt-format-org-entity
))
427 ;;;_. control callbacks
429 (defun org-odt-begin-office-body ()
433 <text:sequence-decls>
434 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Illustration\"/>
435 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Table\"/>
436 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Text\"/>
437 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Drawing\"/>
438 </text:sequence-decls>"))
440 ;; Following variable is let bound when `org-do-lparse' is in
441 ;; progress. See org-html.el.
442 (defvar org-lparse-toc
)
443 (defun org-odt-begin-document-body (opt-plist)
444 (org-odt-begin-office-body)
445 (let ((title (plist-get opt-plist
:title
)))
448 (org-odt-format-stylized-paragraph 'title title
))))
452 (insert "\n" org-lparse-toc
"\n")))
454 (defvar org-lparse-body-only
) ; let bound during org-do-lparse
455 (defvar org-lparse-to-buffer
) ; let bound during org-do-lparse
456 (defun org-odt-end-document-body (opt-plist)
457 (unless org-lparse-body-only
458 (org-lparse-insert-tag "</office:text>")
459 (org-lparse-insert-tag "</office:body>")))
461 (defconst org-odt-document-content-header
462 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
463 <office:document-content
464 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
465 xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\"
466 xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\"
467 xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\"
468 xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\"
469 xmlns:fo=\"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0\"
470 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
471 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
472 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
473 xmlns:number=\"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0\"
474 xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\"
475 xmlns:chart=\"urn:oasis:names:tc:opendocument:xmlns:chart:1.0\"
476 xmlns:dr3d=\"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0\"
477 xmlns:math=\"http://www.w3.org/1998/Math/MathML\"
478 xmlns:form=\"urn:oasis:names:tc:opendocument:xmlns:form:1.0\"
479 xmlns:script=\"urn:oasis:names:tc:opendocument:xmlns:script:1.0\"
480 xmlns:ooo=\"http://openoffice.org/2004/office\"
481 xmlns:ooow=\"http://openoffice.org/2004/writer\"
482 xmlns:oooc=\"http://openoffice.org/2004/calc\"
483 xmlns:dom=\"http://www.w3.org/2001/xml-events\"
484 xmlns:xforms=\"http://www.w3.org/2002/xforms\"
485 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
486 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
487 xmlns:rpt=\"http://openoffice.org/2005/report\"
488 xmlns:of=\"urn:oasis:names:tc:opendocument:xmlns:of:1.2\"
489 xmlns:xodt=\"http://www.w3.org/1999/xodt\"
490 xmlns:field=\"urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0\" office:version=\"1.2\">
493 (defun org-odt-begin-document-content (opt-plist)
495 (insert org-odt-document-content-header
)
498 (insert-file-contents
499 (or org-export-odt-automatic-styles-file
500 (expand-file-name "styles/OrgOdtAutomaticStyles.xml"
502 (goto-char (point-max)))
504 (defun org-odt-end-document-content ()
505 (org-lparse-insert-tag "</office:document-content>"))
507 (defun org-odt-begin-outline (level1 snumber title tags
508 target extra-targets class
)
510 'HEADING
(org-lparse-format
511 'HEADLINE title extra-targets tags snumber level1
)
514 (defun org-odt-end-outline ()
517 (defun org-odt-begin-outline-text (level1 snumber class
)
520 (defun org-odt-end-outline-text ()
523 (defun org-odt-begin-paragraph (&optional style
)
524 (org-lparse-insert-tag
525 "<text:p%s>" (org-odt-get-extra-attrs-for-paragraph-style style
)))
527 (defun org-odt-end-paragraph ()
528 (org-lparse-insert-tag "</text:p>"))
530 (defun org-odt-get-extra-attrs-for-paragraph-style (style)
534 ((stringp style
) style
)
535 ((symbolp style
) (org-odt-get-style-name-for-entity
538 (error "Don't know how to handle paragraph style %s" style
))
539 (format " text:style-name=\"%s\"" style-name
)))
541 (defun org-odt-format-stylized-paragraph (style text
)
543 '("<text:p%s>" .
"</text:p>") text
544 (org-odt-get-extra-attrs-for-paragraph-style style
)))
546 (defun org-odt-begin-environment (style)
548 ((blockquote verse center quote
)
549 (org-lparse-begin-paragraph style
)
552 (org-lparse-end-paragraph)
554 (t (error "Unknown environment %s" style
))))
556 (defun org-odt-end-environment (style)
558 ((blockquote verse center quote
)
559 (org-lparse-end-paragraph)
562 (org-lparse-begin-paragraph)
564 (t (error "Unknown environment %s" style
))))
566 (defvar org-lparse-list-level
) ; dynamically bound in org-do-lparse
567 (defun org-odt-begin-list (ltype)
568 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
570 (let* ((style-name (org-odt-get-style-name-for-entity 'list ltype
))
571 (extra (concat (when (= org-lparse-list-level
1)
572 " text:continue-numbering=\"false\"")
574 (format " text:style-name=\"%s\"" style-name
)))))
576 ((ordered unordered description
)
577 (org-lparse-end-paragraph)
578 (org-lparse-insert-tag "<text:list%s>" extra
))
579 (t (error "Unknown list type: %s" ltype
)))))
581 (defun org-odt-end-list (ltype)
582 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
585 (org-lparse-insert-tag "</text:list>")
586 (error "Unknown list type: %s" ltype
)))
588 (defun org-odt-begin-list-item (ltype &optional arg headline
)
589 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
593 (assert (not headline
) t
)
594 (let* ((counter arg
) (extra ""))
595 (org-lparse-insert-tag "<text:list-item>")
596 (org-lparse-begin-paragraph)))
598 (let* ((id arg
) (extra ""))
599 (org-lparse-insert-tag "<text:list-item>")
600 (org-lparse-begin-paragraph)
601 (insert (if headline
(org-odt-format-target headline id
)
602 (org-odt-format-bookmark "" id
)))))
604 (assert (not headline
) t
)
605 (let ((term (or arg
"(no term)")))
608 '("<text:list-item>" .
"</text:list-item>")
609 (org-odt-format-stylized-paragraph 'definition-term term
)))
610 (org-lparse-begin 'LIST-ITEM
'unordered
)
611 (org-lparse-begin-list 'description
)
612 (org-lparse-begin 'LIST-ITEM
'unordered
)))
613 (t (error "Unknown list type"))))
615 (defun org-odt-end-list-item (ltype)
616 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
620 (org-lparse-insert-tag "</text:list-item>"))
622 (org-lparse-end-list-item)
623 (org-lparse-end-list 'description
)
624 (org-lparse-end-list-item))
625 (t (error "Unknown list type"))))
627 ;; Following variables are let bound when table emission is in
628 ;; progress. See org-lparse.el.
629 (defvar org-lparse-table-begin-marker
)
630 (defvar org-lparse-table-ncols
)
631 (defvar org-lparse-table-rowgrp-open
)
632 (defvar org-lparse-table-rownum
)
633 (defvar org-lparse-table-cur-rowgrp-is-hdr
)
634 (defvar org-lparse-table-is-styled
)
635 (defvar org-lparse-table-rowgrp-info
)
636 (defvar org-lparse-table-colalign-vector
)
637 (defun org-odt-begin-table (caption label attributes
)
640 (org-odt-format-stylized-paragraph
641 'table
(org-odt-format-entity-caption label caption
"Table"))))
643 (org-lparse-insert-tag
644 "<table:table table:name=\"%s\" table:style-name=\"%s\">"
645 (or label
"") "OrgTable")
646 (setq org-lparse-table-begin-marker
(point)))
648 (defun org-odt-end-table ()
649 (goto-char org-lparse-table-begin-marker
)
650 (loop for level from
0 below org-lparse-table-ncols
651 do
(insert (org-odt-format-tags "<table:table-column/>" "")))
653 ;; fill style attributes for table cells
654 (when org-lparse-table-is-styled
655 (while (re-search-forward "@@\\(table-cell:p\\|table-cell:style-name\\)@@\\([0-9]+\\)@@\\([0-9]+\\)@@" nil t
)
656 (let ((spec (match-string 1))
657 (r (string-to-number (match-string 2)))
658 (c (string-to-number (match-string 3))))
660 ((equal spec
"table-cell:p")
661 (let ((style-name (org-odt-get-paragraph-style-for-table-cell r c
)))
662 (replace-match style-name t t
)))
663 ((equal spec
"table-cell:style-name")
664 (let ((style-name (org-odt-get-style-name-for-table-cell r c
)))
665 (replace-match style-name t t
)))))))
667 (goto-char (point-max))
668 (org-lparse-insert-tag "</table:table>"))
670 (defun org-odt-begin-table-rowgroup (&optional is-header-row
)
671 (when org-lparse-table-rowgrp-open
672 (org-lparse-end 'TABLE-ROWGROUP
))
673 (org-lparse-insert-tag (if is-header-row
674 "<table:table-header-rows>"
675 "<table:table-rows>"))
676 (setq org-lparse-table-rowgrp-open t
)
677 (setq org-lparse-table-cur-rowgrp-is-hdr is-header-row
))
679 (defun org-odt-end-table-rowgroup ()
680 (when org-lparse-table-rowgrp-open
681 (setq org-lparse-table-rowgrp-open nil
)
682 (org-lparse-insert-tag
683 (if org-lparse-table-cur-rowgrp-is-hdr
684 "</table:table-header-rows>" "</table:table-rows>"))))
686 (defun org-odt-format-table-row (row)
688 '("<table:table-row>" .
"</table:table-row>") row
))
690 (defun org-odt-get-style-name-for-table-cell (r c
)
695 ((eq (cdr (assoc r org-lparse-table-rowgrp-info
)) :start
) "T")
697 (when (= r org-lparse-table-rownum
) "B")
700 ((or (memq (nth c org-table-colgroup-info
) '(:start
:startend
))
701 (memq (nth (1- c
) org-table-colgroup-info
) '(:end
:startend
))) "L")
704 (defun org-odt-get-paragraph-style-for-table-cell (r c
)
705 (capitalize (aref org-lparse-table-colalign-vector c
)))
707 (defun org-odt-format-table-cell (data r c
)
708 (if (not org-lparse-table-is-styled
)
710 '("<table:table-cell>" .
"</table:table-cell>")
711 (org-odt-format-stylized-paragraph
713 (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
714 ((and (= c
0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS
))
716 (t "OrgTableContents"))
718 (let* ((style-name-cookie
719 (format "@@table-cell:style-name@@%03d@@%03d@@" r c
))
720 (paragraph-style-cookie
723 (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
724 ((and (= c
0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS
))
726 (t "OrgTableContents"))
727 (format "@@table-cell:p@@%03d@@%03d@@" r c
))))
729 '("<table:table-cell table:style-name=\"%s\">" .
730 "</table:table-cell>")
731 (org-odt-format-stylized-paragraph paragraph-style-cookie data
)
732 style-name-cookie
))))
734 (defun org-odt-begin-footnote-definition (n)
735 (org-lparse-begin-paragraph 'footnote
))
737 (defun org-odt-end-footnote-definition (n)
738 (org-lparse-end-paragraph))
740 (defun org-odt-begin-toc (lang-specific-heading)
743 <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
744 <text:table-of-content-source text:outline-level=\"10\">
745 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
746 " lang-specific-heading
))
748 (loop for level from
1 upto
10
751 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
752 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
753 <text:index-entry-chapter/>
754 <text:index-entry-text/>
755 <text:index-entry-link-end/>
756 </text:table-of-content-entry-template>
761 </text:table-of-content-source>
764 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
765 <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
767 " lang-specific-heading
)))
769 (defun org-odt-end-toc ()
772 </text:table-of-content>
775 (defun org-odt-format-toc-entry (snumber todo headline tags href
)
776 (setq headline
(concat
777 (and org-export-with-section-numbers
778 (concat snumber
". "))
782 (org-lparse-format 'SPACES
3)
783 (org-lparse-format 'FONTIFY tags
"tag")))))
785 (setq headline
(org-lparse-format 'FONTIFY headline
"todo")))
787 (let ((org-odt-suppress-xref t
))
788 (org-odt-format-link headline
(concat "#" href
))))
790 (defun org-odt-format-toc-item (toc-entry level org-last-level
)
791 (let ((style (format "Contents_20_%d"
792 (+ level
(or (org-lparse-get 'TOPLEVEL-HLEVEL
) 1) -
1))))
793 (insert "\n" (org-odt-format-stylized-paragraph style toc-entry
) "\n")))
795 ;; Following variable is let bound during 'ORG-LINK callback. See
797 (defvar org-lparse-link-description-is-image nil
)
798 (defun org-odt-format-link (desc href
&optional attr
)
800 ((and (= (string-to-char href
) ?
#) (not org-odt-suppress-xref
))
801 (setq href
(concat org-export-odt-bookmark-prefix
(substring href
1)))
803 '("<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"%s\">" .
804 "</text:bookmark-ref>")
806 (org-lparse-link-description-is-image
808 '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" .
"</draw:a>")
809 desc href
(or attr
"")))
812 '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" .
"</text:a>")
813 desc href
(or attr
"")))))
815 (defun org-odt-format-spaces (n)
819 " " (org-odt-format-tags "<text:s text:c=\"%d\"/>" "" (1- n
))))
822 (defun org-odt-format-tabs (&optional n
)
823 (let ((tab "<text:tab/>")
827 (defun org-odt-format-line-break ()
828 (org-odt-format-tags "<text:line-break/>" ""))
830 (defun org-odt-format-horizontal-line ()
831 (org-odt-format-stylized-paragraph 'horizontal-line
""))
833 (defun org-odt-format-line (line)
834 (case org-lparse-dyn-current-environment
836 (org-odt-format-stylized-paragraph
837 'src
(org-odt-fill-tabs-and-spaces
838 (org-xml-encode-plain-text line
))) "\n"))
839 (t (concat line
"\n"))))
841 (defun org-odt-format-comment (fmt &rest args
)
842 (let ((comment (apply 'format fmt args
)))
843 (format "\n<!-- %s -->\n" comment
)))
845 (defun org-odt-format-org-entity (wd)
846 (org-entity-get-representation wd
'utf8
))
848 (defun org-odt-fill-tabs-and-spaces (line)
849 (replace-regexp-in-string
850 "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
852 ((string= s
"\t") (org-odt-format-tabs))
853 (t (org-odt-format-spaces (length s
))))) line
))
855 (defun org-odt-format-source-code-or-example
856 (lines lang caption textareap cols rows num cont rpllbl fmt
)
857 (setq lines
(org-export-number-lines (org-xml-encode-plain-text-lines lines
)
858 0 0 num cont rpllbl fmt
))
861 (org-odt-format-stylized-paragraph
862 'src
(org-odt-fill-tabs-and-spaces line
)))
863 (org-split-string lines
"[\r\n]") "\n"))
865 (defun org-xml-encode-plain-text-lines (rtn)
866 (mapconcat 'org-xml-encode-plain-text
(org-split-string rtn
"[\r\n]") "\n"))
868 (defun org-odt-remap-stylenames (style-name)
870 (cdr (assoc style-name
'(("timestamp-wrapper" .
"OrgTimestampWrapper")
871 ("timestamp" .
"OrgTimestamp")
872 ("timestamp-kwd" .
"OrgTimestampKeyword")
876 ("target" .
"OrgTarget"))))
879 (defun org-odt-format-fontify (text style
&optional id
)
883 (org-odt-remap-stylenames style
))
885 (org-odt-get-style-name-for-entity 'character style
))
887 (assert (< 1 (length style
)))
888 (let ((parent-style (pop style
)))
889 (mapconcat (lambda (s)
890 ;; (assert (stringp s) t)
891 (org-odt-remap-stylenames s
)) style
"")
892 (org-odt-remap-stylenames parent-style
)))
893 (t (error "Don't how to handle style %s" style
)))))
895 '("<text:span text:style-name=\"%s\">" .
"</text:span>")
898 (defun org-odt-relocate-relative-path (path dir
)
899 (if (file-name-absolute-p path
) path
900 (file-relative-name (expand-file-name path dir
)
901 (expand-file-name "eyecandy" dir
))))
903 (defun org-odt-format-inline-image (thefile)
904 (let* ((thelink (if (file-name-absolute-p thefile
) thefile
906 (org-odt-relocate-relative-path
907 thefile org-current-export-file
))))
910 "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
911 (if org-export-odt-embed-images
912 (org-odt-copy-image-file thefile
) thelink
))))
913 (org-export-odt-format-image thefile href
)))
915 (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
918 OPT-PLIST is an options list.
919 TYPE is the device-type of the link (THIS://foo.html)
920 PATH is the path of the link (http://THIS#locationx)
921 FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
922 DESC is the link description, if any.
923 ATTR is a string of other attributes of the a element.
924 MAY-INLINE-P allows inlining it as an image."
926 (declare (special org-lparse-par-open
))
929 (and (member type-1
'("http" "https" "file"))
930 (org-lparse-should-inline-p path descp
)
932 (type (if (equal type-1
"id") "file" type-1
))
937 ;; check for inlined images
938 ((and (member type
'("file"))
941 filename org-odt-export-inline-image-extensions
)
942 (or (eq t org-odt-export-inline-images
)
943 (and org-odt-export-inline-images
(not descp
))))
945 ;; (when (and (string= type "file") (file-name-absolute-p path))
946 ;; (setq thefile (concat "file://" (expand-file-name path))))
947 ;; (setq thefile (org-xml-format-href thefile))
948 ;; (org-export-html-format-image thefile)
949 (org-odt-format-inline-image thefile
))
951 (when (string= type
"file")
954 ((file-name-absolute-p path
)
955 (concat "file://" (expand-file-name path
)))
956 (t (org-odt-relocate-relative-path
957 thefile org-current-export-file
)))))
959 (when (and (member type
'("" "http" "https" "file" "coderef"))
961 (setq thefile
(concat thefile
"#" fragment
)))
963 (setq thefile
(org-xml-format-href thefile
))
965 (when (not (member type
'("" "file" "coderef")))
966 (setq thefile
(concat type
":" thefile
)))
968 (let ((org-odt-suppress-xref (string= type
"coderef")))
970 (org-xml-format-desc desc
) thefile attr
)))))))
972 (defun org-odt-format-heading (text level
&optional id
)
973 (let* ((text (if id
(org-odt-format-target text id
) text
)))
975 '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
976 "</text:h>") text level level
)))
978 (defun org-odt-format-headline (title extra-targets tags
979 &optional snumber level
)
981 (org-lparse-format 'EXTRA-TARGETS extra-targets
)
983 ;; No need to generate section numbers. They are auto-generated by
986 ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
988 (and tags
(concat (org-lparse-format 'SPACES
3)
989 (org-lparse-format 'ORG-TAGS tags
)))))
991 (defun org-odt-format-anchor (text name
&optional class
)
992 (org-odt-format-target text name
))
994 (defun org-odt-format-bookmark (text id
)
996 (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id
)
999 (defun org-odt-format-target (text id
)
1000 (let ((name (concat org-export-odt-bookmark-prefix id
)))
1002 (and id
(org-odt-format-tags
1003 "<text:bookmark-start text:name=\"%s\"/>" "" name
))
1004 (org-odt-format-bookmark text id
)
1005 (and id
(org-odt-format-tags
1006 "<text:bookmark-end text:name=\"%s\"/>" "" name
)))))
1008 (defun org-odt-format-footnote (n def
)
1009 (let ((id (concat "fn" n
))
1010 (note-class "footnote")
1011 (par-style "Footnote"))
1012 (org-odt-format-tags
1013 '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
1016 (org-odt-format-tags
1017 '("<text:note-citation>" .
"</text:note-citation>")
1019 (org-odt-format-tags
1020 '("<text:note-body>" .
"</text:note-body>")
1024 (defun org-odt-format-footnote-reference (n def refcnt
)
1026 (org-odt-format-footnote n def
)
1027 (org-odt-format-footnote-ref n
)))
1029 (defun org-odt-format-footnote-ref (n)
1030 (let ((note-class "footnote")
1032 (ref-name (concat "fn" n
)))
1033 (org-odt-format-tags
1034 '("<text:span text:style-name=\"%s\">" .
"</text:span>")
1035 (org-odt-format-tags
1036 '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" .
"</text:note-ref>")
1037 n note-class ref-format ref-name
)
1040 (defun org-odt-get-image-name (file-name)
1044 (concat (sha1 file-name
) "." (file-name-extension file-name
)) "Pictures")))
1046 (defun org-export-odt-format-image (src href
1049 "Create image tag with source and attributes."
1052 (let (embed-as caption attr label attr-plist size width height
)
1055 ((string-match "^ltxpng/" src
)
1056 ;; FIXME: Anyway the latex src can be embedded as an
1059 ;; (org-find-text-property-in-string 'org-latex-src src)
1060 (setq caption nil attr nil label nil embed-as
'character
))
1063 (setq caption
(org-find-text-property-in-string 'org-caption src
)
1064 caption
(and caption
(org-xml-format-desc caption
))
1065 attr
(org-find-text-property-in-string 'org-attributes src
)
1066 label
(org-find-text-property-in-string 'org-label src
)
1067 embed-as
'paragraph
)))
1069 (setq attr-plist
(when attr
(read attr
)))
1070 (setq size
(org-odt-image-size-from-file
1071 src
(plist-get attr-plist
:width
)
1072 (plist-get attr-plist
:height
)
1073 (plist-get attr-plist
:scale
) nil embed-as
))
1075 (org-export-odt-do-format-image embed-as caption attr label
1077 (defun org-odt-format-textbox (text style
)
1078 (let ((draw-frame-pair
1079 '("<draw:frame draw:style-name=\"%s\"
1080 text:anchor-type=\"paragraph\"
1081 style:rel-width=\"100%%\"
1082 draw:z-index=\"0\">" .
"</draw:frame>")))
1083 (org-odt-format-tags
1085 (org-odt-format-tags
1086 '("<draw:text-box fo:min-height=\"%dcm\">" .
"</draw:text-box>")
1089 (defun org-odt-format-inlinetask (heading content
1090 &optional todo priority tags
)
1091 (org-odt-format-stylized-paragraph
1092 nil
(org-odt-format-textbox
1093 (concat (org-odt-format-stylized-paragraph
1094 "OrgInlineTaskHeading"
1096 'HEADLINE
(concat (org-lparse-format-todo todo
) " " heading
)
1098 content
) "OrgInlineTaskFrame")))
1100 (defun org-export-odt-do-format-image (embed-as caption attr label
1102 "Create image tag with source and attributes."
1104 (let ((width (car size
)) (height (cdr size
))
1106 '("<draw:frame draw:style-name=\"%s\"
1107 text:anchor-type=\"%s\"
1108 draw:z-index=\"%d\" %s>" .
"</draw:frame>")))
1110 ((and (not caption
) (not label
))
1111 (let (style-name anchor-type
)
1113 ((eq embed-as
'paragraph
)
1114 (setq style-name
"OrgGraphicsParagraph" anchor-type
"paragraph"))
1115 ((eq embed-as
'character
)
1116 (setq style-name
"OrgGraphicsBaseline" anchor-type
"as-char")))
1117 (org-odt-format-tags
1118 draw-frame-pair href style-name anchor-type
0
1119 (org-odt-image-attrs-from-size width height
))))
1123 ;; (when par-open (org-odt-close-par))
1124 (org-odt-format-tags
1126 (org-odt-format-tags
1127 '("<draw:text-box fo:min-height=\"%dcm\">" .
"</draw:text-box>")
1128 (org-odt-format-stylized-paragraph
1131 (let ((extra " style:rel-width=\"100%\" style:rel-height=\"scale\""))
1132 (org-odt-format-tags
1133 draw-frame-pair href
"OrgGraphicsParagraphContent" "paragraph" 2
1134 (concat (org-odt-image-attrs-from-size width height
) extra
)))
1135 (org-odt-format-entity-caption label caption
)))
1137 "OrgFrame" "paragraph" 1
1138 (org-odt-image-attrs-from-size width
))
1140 ;; (when par-open (org-odt-open-par))
1143 ;; xml files generated on-the-fly
1144 (defconst org-export-odt-save-list
1145 '("mimetype" "META-INF/manifest.xml" "content.xml" "meta.xml" "styles.xml"))
1147 ;; xml files that are copied
1148 (defconst org-export-odt-nosave-list
'())
1150 ;; xml files that contribute to the final odt file
1151 (defvar org-export-odt-file-list nil
)
1153 (defconst org-export-odt-manifest-lines
1154 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1155 "<manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">"
1156 "<manifest:file-entry manifest:media-type=\"application/vnd.oasis.opendocument.text\" manifest:version=\"1.2\" manifest:full-path=\"/\"/>"
1157 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"content.xml\"/>"
1158 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"styles.xml\"/>"
1159 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"meta.xml\"/>"
1160 "<manifest:file-entry manifest:media-type=\"\" manifest:full-path=\"Pictures/\"/>") .
("</manifest:manifest>")))
1162 (defconst org-export-odt-meta-lines
1163 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1164 "<office:document-meta"
1165 " xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\""
1166 " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
1167 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\""
1168 " xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\""
1169 " xmlns:ooo=\"http://openoffice.org/2004/office\" "
1170 " office:version=\"1.2\">"
1171 " <office:meta>") .
(" </office:meta>" "</office:document-meta>")))
1173 (defun org-odt-copy-image-file (path &optional target-file
)
1174 "Returns the internal name of the file"
1175 (let* ((image-type (file-name-extension path
))
1176 (media-type (format "image/%s" image-type
))
1177 (src-file (expand-file-name
1178 path
(file-name-directory org-current-export-file
)))
1179 (target-file (or target-file
(org-odt-get-image-name src-file
)))
1183 (when (not org-lparse-to-buffer
)
1184 (message "Embedding %s as %s ..."
1185 (substring-no-properties path
) target-file
)
1186 (copy-file src-file target-file
'overwrite
)
1187 (org-odt-update-manifest-file media-type target-file
)
1188 (push target-file org-export-odt-file-list
)) target-file
))
1190 (defun org-odt-image-attrs-from-size (&optional width height
)
1192 (when width
(format "svg:width=\"%0.2fcm\"" width
))
1194 (when height
(format "svg:height=\"%0.2fcm\"" height
))))
1196 (defvar org-export-odt-image-size-probe-method
1197 '(emacs imagemagick force
)
1198 "Ordered list of methods by for determining size of an embedded
1201 (defvar org-export-odt-default-image-sizes-alist
1202 '(("character" .
(5 .
0.4))
1203 ("paragraph" .
(5 .
5)))
1204 "Hardcoded image dimensions one for each of the anchor
1207 (defun org-odt-do-image-size (probe-method file
&optional dpi anchor-type
)
1208 (setq dpi
(or dpi org-export-odt-pixels-per-inch
))
1209 (setq anchor-type
(or anchor-type
"paragraph"))
1210 (flet ((size-in-cms (size-in-pixels)
1211 (flet ((pixels-to-cms (pixels)
1212 (let* ((cms-per-inch 2.54)
1213 (inches (/ pixels dpi
)))
1214 (* cms-per-inch inches
))))
1216 (cons (pixels-to-cms (car size-in-pixels
))
1217 (pixels-to-cms (cdr size-in-pixels
)))))))
1220 (size-in-cms (ignore-errors (image-size (create-image file
) 'pixels
))))
1223 (let ((dim (shell-command-to-string
1224 (format "identify -format \"%%w:%%h\" \"%s\"" file
))))
1225 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim
)
1226 (cons (string-to-number (match-string 1 dim
))
1227 (string-to-number (match-string 2 dim
)))))))
1229 (cdr (assoc-string anchor-type
1230 org-export-odt-default-image-sizes-alist
))))))
1232 (defun org-odt-image-size-from-file (file &optional user-width
1233 user-height scale dpi embed-as
)
1234 (unless (file-name-absolute-p file
)
1235 (setq file
(expand-file-name
1236 file
(file-name-directory org-current-export-file
))))
1237 (let* (size width height
)
1238 (unless (and user-height user-width
)
1239 (loop for probe-method in org-export-odt-image-size-probe-method
1241 do
(setq size
(org-odt-do-image-size
1242 probe-method file dpi embed-as
)))
1243 (or size
(error "Cannot determine Image size. Aborting ..."))
1244 (setq width
(car size
) height
(cdr size
)))
1247 (setq width
(* width scale
) height
(* height scale
)))
1248 ((and user-height user-width
)
1249 (setq width user-width height user-height
))
1251 (setq width
(* user-height
(/ width height
)) height user-height
))
1253 (setq height
(* user-width
(/ height width
)) width user-width
))
1255 (cons width height
)))
1257 (defvar org-odt-default-entity
"Illustration")
1258 (defun org-odt-format-entity-caption (label caption
&optional default-entity
)
1259 (if (not label
) (or caption
"")
1260 (let* ((label-components (org-odt-parse-label label
))
1261 (entity (car label-components
))
1262 (seqno (cdr label-components
))
1263 (caption (and caption
(concat ": " caption
))))
1266 entity
(or default-entity org-odt-default-entity
)))
1269 (org-odt-format-tags
1270 '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" .
"</text:sequence>")
1271 seqno label entity entity
)
1274 (defun org-odt-format-tags (tag text
&rest args
)
1275 (let ((prefix (when org-lparse-encode-pending
"@"))
1276 (suffix (when org-lparse-encode-pending
"@")))
1277 (apply 'org-lparse-format-tags tag text prefix suffix args
)))
1279 (defun org-odt-init-outfile (filename)
1280 (unless (executable-find "zip")
1281 ;; Not at all OSes ship with zip by default
1282 (error "Executable \"zip\" needed for creating OpenDocument files. Aborting."))
1284 (let* ((outdir (make-temp-file org-export-odt-tmpdir-prefix t
))
1285 (mimetype-file (expand-file-name "mimetype" outdir
))
1286 (content-file (expand-file-name "content.xml" outdir
))
1287 (manifest-file (expand-file-name "META-INF/manifest.xml" outdir
))
1288 (meta-file (expand-file-name "meta.xml" outdir
))
1289 (styles-file (expand-file-name "styles.xml" outdir
))
1290 (pictures-dir (expand-file-name "Pictures" outdir
))
1294 (with-current-buffer (find-file-noselect content-file t
)
1297 ;; FIXME: How to factor in body-only here
1300 (make-directory (file-name-directory manifest-file
))
1301 (with-current-buffer (find-file-noselect manifest-file t
)
1303 (insert (mapconcat 'identity
(car org-export-odt-manifest-lines
) "\n"))
1306 (insert (mapconcat 'identity
(cdr org-export-odt-manifest-lines
) "\n"))))
1309 (with-current-buffer (find-file-noselect meta-file t
)
1311 (insert (mapconcat 'identity
(car org-export-odt-meta-lines
) "\n"))
1314 (insert (mapconcat 'identity
(cdr org-export-odt-meta-lines
) "\n"))))
1317 (with-current-buffer (find-file-noselect mimetype-file t
)
1318 (insert "application/vnd.oasis.opendocument.text"))
1321 ;; (copy-file org-export-odt-styles-file styles-file t)
1324 (make-directory pictures-dir
)
1326 ;; initialize list of files that contribute to the odt file
1327 (setq org-export-odt-file-list
1328 (append org-export-odt-save-list org-export-odt-nosave-list
)))
1331 (defconst org-odt-manifest-file-entry-tag
1332 "<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"/>")
1334 (defcustom org-export-odt-prettify-xml nil
1335 "Specify whether or not the xml output should be prettified.
1336 When this option is turned on, `indent-region' is run on all
1337 component xml buffers before they are saved. Turn this off for
1338 regular use. Turn this on if you need to examine the xml
1340 :group
'org-export-odt
1343 (defun org-odt-save-as-outfile (target opt-plist
)
1345 (org-odt-update-meta-file opt-plist
)
1347 ;; write styles file
1348 (org-odt-copy-styles-file)
1350 ;; Update styles.xml - take care of outline numbering
1351 (with-current-buffer
1352 (find-file-noselect (expand-file-name "styles.xml") t
)
1353 ;; Don't make automatic backup of styles.xml file. This setting
1354 ;; prevents the backedup styles.xml file from being zipped in to
1355 ;; odt file. This is more of a hackish fix. Better alternative
1356 ;; would be to fix the zip command so that the output odt file
1357 ;; includes only the needed files and excludes any auto-generated
1358 ;; extra files like backups and auto-saves etc etc. Note that
1359 ;; currently the zip command zips up the entire temp directory so
1360 ;; that any auto-generated files created under the hood ends up in
1361 ;; the resulting odt file.
1362 (set (make-local-variable 'backup-inhibited
) t
)
1364 ;; Import local setting of `org-export-with-section-numbers'
1365 (org-lparse-bind-local-variables opt-plist
)
1366 (org-odt-configure-outline-numbering
1367 (if org-export-with-section-numbers org-export-headline-levels
0)))
1369 (let ((zipdir default-directory
))
1370 (message "Switching to directory %s" (expand-file-name zipdir
))
1372 ;; save all xml files
1373 (mapc (lambda (file)
1374 (with-current-buffer
1375 (find-file-noselect (expand-file-name file
) t
)
1376 ;; prettify output if needed
1377 (when org-export-odt-prettify-xml
1378 (indent-region (point-min) (point-max)))
1380 org-export-odt-save-list
)
1382 (let* ((target-name (file-name-nondirectory target
))
1383 (target-dir (file-name-directory target
))
1384 (cmds `(("zip" "-mX0" ,target-name
"mimetype")
1385 ("zip" "-rmTq" ,target-name
"."))))
1386 (when (file-exists-p target
)
1387 ;; FIXME: If the file is locked this throws a cryptic error
1388 (delete-file target
))
1390 (let ((coding-system-for-write 'no-conversion
) exitcode
)
1391 (message "Creating odt file...")
1394 (message "Running %s" (mapconcat 'identity cmd
" "))
1396 (apply 'call-process
(car cmd
) nil nil nil
(cdr cmd
)))
1397 (or (zerop exitcode
)
1398 (error "Unable to create odt file (%S)" exitcode
)))
1401 ;; move the file from outdir to target-dir
1402 (rename-file target-name target-dir
)
1404 ;; kill all xml buffers
1405 (mapc (lambda (file)
1407 (find-file-noselect (expand-file-name file zipdir
) t
)))
1408 org-export-odt-save-list
)
1410 (delete-directory zipdir
)))
1412 (message "Created %s" target
)
1413 (set-buffer (find-file-noselect target t
)))
1415 (defun org-odt-format-date (date)
1417 "OpenDocument files require that dates be in ISO-8601 format. Please review your DATE options for compatibility."))
1418 ;; If the user is not careful with the date specification, an
1419 ;; invalid meta.xml will be emitted.
1421 ;; For now honor user's diktat and let him off with a warning
1422 ;; message. This is OK as LibreOffice (and possibly other
1423 ;; apps) doesn't deem this deviation as critical and continue
1424 ;; to load the file.
1426 ;; FIXME: Surely there a better way to handle this. Revisit this
1429 ((and date
(string-match "%" date
))
1430 ;; Honor user's diktat. See comments above
1431 (org-lparse-warn warning-msg
)
1432 (format-time-string date
))
1434 ;; Honor user's diktat. See comments above
1435 (org-lparse-warn warning-msg
)
1439 (format-time-string "%Y-%m-%dT%T%:z")))))
1441 (defun org-odt-update-meta-file (opt-plist)
1442 (with-current-buffer
1443 (find-file-noselect (expand-file-name "meta.xml") t
)
1444 (let ((date (org-odt-format-date (plist-get opt-plist
:date
)))
1445 (author (or (plist-get opt-plist
:author
) ""))
1446 (email (plist-get opt-plist
:email
))
1447 (keywords (plist-get opt-plist
:keywords
))
1448 (description (plist-get opt-plist
:description
))
1449 (title (plist-get opt-plist
:title
)))
1453 (org-odt-format-tags '("<dc:creator>" .
"</dc:creator>") author
)
1454 (org-odt-format-tags
1455 '("\n<meta:initial-creator>" .
"</meta:initial-creator>") author
)
1456 (org-odt-format-tags '("\n<dc:date>" .
"</dc:date>") date
)
1457 (org-odt-format-tags
1458 '("\n<meta:creation-date>" .
"</meta:creation-date>") date
)
1459 (org-odt-format-tags '("\n<meta:generator>" .
"</meta:generator>")
1460 (when org-export-creator-info
1461 (format "Org-%s/Emacs-%s"
1462 org-version emacs-version
)))
1463 (org-odt-format-tags '("\n<meta:keyword>" .
"</meta:keyword>") keywords
)
1464 (org-odt-format-tags '("\n<dc:subject>" .
"</dc:subject>") description
)
1465 (org-odt-format-tags '("\n<dc:title>" .
"</dc:title>") title
)
1468 (defun org-odt-update-manifest-file (media-type full-path
)
1469 (with-current-buffer
1470 (find-file-noselect (expand-file-name "META-INF/manifest.xml") t
)
1471 (insert (format org-odt-manifest-file-entry-tag media-type full-path
))))
1473 (defun org-odt-finalize-outfile ()
1474 (org-odt-delete-empty-paragraphs))
1476 (defun org-odt-delete-empty-paragraphs ()
1477 (goto-char (point-min))
1478 (let ((open "<text:p[^>]*>")
1479 (close "</text:p>"))
1480 (while (re-search-forward (format "%s[ \r\n\t]*%s" open close
) nil t
)
1481 (replace-match ""))))
1483 (defun org-odt-get (what &optional opt-plist
)
1486 (EXPORT-DIR (org-export-directory :html opt-plist
))
1487 (FILE-NAME-EXTENSION "odt")
1488 (EXPORT-BUFFER-NAME "*Org ODT Export*")
1489 (ENTITY-CONTROL org-odt-entity-control-callbacks-alist
)
1490 (ENTITY-FORMAT org-odt-entity-format-callbacks-alist
)
1491 (INIT-METHOD 'org-odt-init-outfile
)
1492 (FINAL-METHOD 'org-odt-finalize-outfile
)
1493 (SAVE-METHOD 'org-odt-save-as-outfile
)
1494 ;; (OTHER-BACKENDS) ; see note in `org-xhtml-get'
1495 ;; (CONVERT-METHOD) ; see note in `org-xhtml-get'
1497 (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps
)
1498 (INLINE-IMAGES 'maybe
)
1499 (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
1500 (PLAIN-TEXT-MAP '(("&" .
"&") ("<" .
"<") (">" .
">")))
1501 (TABLE-FIRST-COLUMN-AS-LABELS nil
)
1502 (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY
"," 'superscript
))
1503 (CODING-SYSTEM-FOR-WRITE 'utf-8
)
1504 (CODING-SYSTEM-FOR-SAVE 'utf-8
)
1505 (t (error "Unknown property: %s" what
))))
1507 (defun org-odt-parse-label (label)
1509 (if (not (string-match "\\`[a-zA-Z]+:\\(.+\\)" label
))
1512 (capitalize (substring label
0 (1- (match-beginning 1))))
1513 (substring label
(match-beginning 1))))))
1515 (defvar org-lparse-latex-fragment-fallback
) ; set by org-do-lparse
1516 (defun org-export-odt-preprocess (parameters)
1517 "Convert LaTeX fragments to images."
1518 (when (and org-current-export-file
1519 (plist-get parameters
:LaTeX-fragments
))
1521 (concat "ltxpng/" (file-name-sans-extension
1522 (file-name-nondirectory
1523 org-current-export-file
)))
1524 org-current-export-dir nil
"Creating LaTeX image %s"
1527 ((eq (plist-get parameters
:LaTeX-fragments
) 'verbatim
) 'verbatim
)
1528 ;; Investigate MathToWeb for converting TeX equations to MathML
1529 ;; See http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01755.html
1530 ((or (eq (plist-get parameters
:LaTeX-fragments
) 'mathjax
)
1531 (eq (plist-get parameters
:LaTeX-fragments
) t
))
1534 "Use of MathJax is incompatible with ODT exporter. "
1535 (format "Using %S instead." org-lparse-latex-fragment-fallback
)))
1536 org-lparse-latex-fragment-fallback
)
1537 ((eq (plist-get parameters
:LaTeX-fragments
) 'dvipng
) 'dvipng
)
1539 (goto-char (point-min))
1540 (let (label label-components category value pretty-label
)
1541 (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t
)
1542 (org-if-unprotected-at (match-beginning 1)
1543 (setq label
(match-string 1)
1544 label-components
(org-odt-parse-label label
)
1545 category
(car label-components
)
1546 value
(cdr label-components
)
1547 pretty-label
(if value
(concat category
" " value
) label
))
1549 (let ((org-lparse-encode-pending t
))
1550 (org-odt-format-tags
1551 '("<text:sequence-ref text:reference-format=\"category-and-value\" text:ref-name=\"%s\">"
1552 .
"</text:sequence-ref>") pretty-label label
)) t t
)))))
1554 (declare-function archive-zip-extract
"arc-mode.el" (archive name
))
1555 (defun org-odt-zip-extract-one (archive member
&optional target
)
1557 (let* ((target (or target default-directory
))
1558 (archive (expand-file-name archive
))
1559 (archive-zip-extract
1560 (list "unzip" "-qq" "-o" "-d" target
))
1561 exit-code command-output
)
1562 (setq command-output
1564 (setq exit-code
(archive-zip-extract archive member
))
1566 (unless (zerop exit-code
)
1567 (message command-output
)
1568 (error "Extraction failed"))))
1570 (defun org-odt-zip-extract (archive members
&optional target
)
1571 (when (atom members
) (setq members
(list members
)))
1572 (mapc (lambda (member)
1573 (org-odt-zip-extract-one archive member target
))
1576 (defun org-odt-copy-styles-file (&optional styles-file
)
1577 ;; Non-availability of styles.xml is not a critical error. For now
1578 ;; throw an error purely for aesthetic reasons.
1579 (setq styles-file
(or styles-file
1580 org-export-odt-styles-file
1581 (expand-file-name "styles/OrgOdtStyles.xml"
1583 (error "org-odt: Missing styles file?")))
1585 ((listp styles-file
)
1586 (let ((archive (nth 0 styles-file
))
1587 (members (nth 1 styles-file
)))
1588 (org-odt-zip-extract archive members
)
1591 (when (org-file-image-p member
)
1592 (let* ((image-type (file-name-extension member
))
1593 (media-type (format "image/%s" image-type
)))
1594 (org-odt-update-manifest-file media-type member
))))
1596 ((and (stringp styles-file
) (file-exists-p styles-file
))
1597 (let ((styles-file-type (file-name-extension styles-file
)))
1599 ((string= styles-file-type
"xml")
1600 (copy-file styles-file
"styles.xml" t
))
1601 ((member styles-file-type
'("odt" "ott"))
1602 (org-odt-zip-extract styles-file
"styles.xml")))))
1604 (error (format "Invalid specification of styles.xml file: %S"
1605 org-export-odt-styles-file
)))))
1607 (defvar org-export-odt-factory-settings
1608 "d4328fb9d1b6cb211d4320ff546829f26700dc5e"
1609 "SHA1 hash of OrgOdtStyles.xml.")
1611 (defun org-odt-configure-outline-numbering (level)
1612 "Outline numbering is retained only upto LEVEL.
1613 To disable outline numbering pass a LEVEL of 0."
1614 (if (not (string= org-export-odt-factory-settings
(sha1 (current-buffer))))
1616 "org-odt: Using custom styles file? Consider tweaking styles.xml for better output. To suppress this warning update `org-export-odt-factory-settings'")
1617 (goto-char (point-min))
1619 "<text:outline-level-style\\(.*\\)text:level=\"\\(.*\\)\"\\(.*\\)>")
1621 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1622 (while (re-search-forward regex nil t
)
1623 (when (> (string-to-number (match-string 1)) level
)
1624 (replace-match replacement t nil
))))
1629 ;;; org-odt.el ends here