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
835 (fixedwidth (concat (org-odt-format-source-code-or-example-line
836 (org-xml-encode-plain-text line
)) "\n"))
837 (t (concat line
"\n"))))
839 (defun org-odt-format-comment (fmt &rest args
)
840 (let ((comment (apply 'format fmt args
)))
841 (format "\n<!-- %s -->\n" comment
)))
843 (defun org-odt-format-org-entity (wd)
844 (org-entity-get-representation wd
'utf8
))
846 (defun org-odt-fill-tabs-and-spaces (line)
847 (replace-regexp-in-string
848 "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
850 ((string= s
"\t") (org-odt-format-tabs))
851 (t (org-odt-format-spaces (length s
))))) line
))
853 (defun org-odt-format-source-code-or-example-line (line)
854 (org-odt-format-stylized-paragraph 'src
(org-odt-fill-tabs-and-spaces line
)))
856 (defun org-odt-format-example (lines)
859 (org-odt-format-source-code-or-example-line line
))
860 (org-split-string lines
"[\r\n]") "\n"))
862 (defun org-odt-format-source-code-or-example (lines lang caption textareap
865 (org-odt-format-example (org-export-number-lines
866 (org-xml-encode-plain-text-lines lines
)
867 0 0 num cont rpllbl fmt
)))
869 (defun org-xml-encode-plain-text-lines (rtn)
870 (mapconcat 'org-xml-encode-plain-text
(org-split-string rtn
"[\r\n]") "\n"))
872 (defun org-odt-remap-stylenames (style-name)
874 (cdr (assoc style-name
'(("timestamp-wrapper" .
"OrgTimestampWrapper")
875 ("timestamp" .
"OrgTimestamp")
876 ("timestamp-kwd" .
"OrgTimestampKeyword")
880 ("target" .
"OrgTarget"))))
883 (defun org-odt-format-fontify (text style
&optional id
)
887 (org-odt-remap-stylenames style
))
889 (org-odt-get-style-name-for-entity 'character style
))
891 (assert (< 1 (length style
)))
892 (let ((parent-style (pop style
)))
893 (mapconcat (lambda (s)
894 ;; (assert (stringp s) t)
895 (org-odt-remap-stylenames s
)) style
"")
896 (org-odt-remap-stylenames parent-style
)))
897 (t (error "Don't how to handle style %s" style
)))))
899 '("<text:span text:style-name=\"%s\">" .
"</text:span>")
902 (defun org-odt-relocate-relative-path (path dir
)
903 (if (file-name-absolute-p path
) path
904 (file-relative-name (expand-file-name path dir
)
905 (expand-file-name "eyecandy" dir
))))
907 (defun org-odt-format-inline-image (thefile)
908 (let* ((thelink (if (file-name-absolute-p thefile
) thefile
910 (org-odt-relocate-relative-path
911 thefile org-current-export-file
))))
914 "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
915 (if org-export-odt-embed-images
916 (org-odt-copy-image-file thefile
) thelink
))))
917 (org-export-odt-format-image thefile href
)))
919 (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
922 OPT-PLIST is an options list.
923 TYPE is the device-type of the link (THIS://foo.html)
924 PATH is the path of the link (http://THIS#locationx)
925 FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
926 DESC is the link description, if any.
927 ATTR is a string of other attributes of the a element.
928 MAY-INLINE-P allows inlining it as an image."
930 (declare (special org-lparse-par-open
))
933 (and (member type-1
'("http" "https" "file"))
934 (org-lparse-should-inline-p path descp
)
936 (type (if (equal type-1
"id") "file" type-1
))
941 ;; check for inlined images
942 ((and (member type
'("file"))
945 filename org-odt-export-inline-image-extensions
)
946 (or (eq t org-odt-export-inline-images
)
947 (and org-odt-export-inline-images
(not descp
))))
949 ;; (when (and (string= type "file") (file-name-absolute-p path))
950 ;; (setq thefile (concat "file://" (expand-file-name path))))
951 ;; (setq thefile (org-xml-format-href thefile))
952 ;; (org-export-html-format-image thefile)
953 (org-odt-format-inline-image thefile
))
955 (when (string= type
"file")
958 ((file-name-absolute-p path
)
959 (concat "file://" (expand-file-name path
)))
960 (t (org-odt-relocate-relative-path
961 thefile org-current-export-file
)))))
963 (when (and (member type
'("" "http" "https" "file" "coderef"))
965 (setq thefile
(concat thefile
"#" fragment
)))
967 (setq thefile
(org-xml-format-href thefile
))
969 (when (not (member type
'("" "file" "coderef")))
970 (setq thefile
(concat type
":" thefile
)))
972 (let ((org-odt-suppress-xref (string= type
"coderef")))
974 (org-xml-format-desc desc
) thefile attr
)))))))
976 (defun org-odt-format-heading (text level
&optional id
)
977 (let* ((text (if id
(org-odt-format-target text id
) text
)))
979 '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
980 "</text:h>") text level level
)))
982 (defun org-odt-format-headline (title extra-targets tags
983 &optional snumber level
)
985 (org-lparse-format 'EXTRA-TARGETS extra-targets
)
987 ;; No need to generate section numbers. They are auto-generated by
990 ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
992 (and tags
(concat (org-lparse-format 'SPACES
3)
993 (org-lparse-format 'ORG-TAGS tags
)))))
995 (defun org-odt-format-anchor (text name
&optional class
)
996 (org-odt-format-target text name
))
998 (defun org-odt-format-bookmark (text id
)
1000 (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id
)
1003 (defun org-odt-format-target (text id
)
1004 (let ((name (concat org-export-odt-bookmark-prefix id
)))
1006 (and id
(org-odt-format-tags
1007 "<text:bookmark-start text:name=\"%s\"/>" "" name
))
1008 (org-odt-format-bookmark text id
)
1009 (and id
(org-odt-format-tags
1010 "<text:bookmark-end text:name=\"%s\"/>" "" name
)))))
1012 (defun org-odt-format-footnote (n def
)
1013 (let ((id (concat "fn" n
))
1014 (note-class "footnote")
1015 (par-style "Footnote"))
1016 (org-odt-format-tags
1017 '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
1020 (org-odt-format-tags
1021 '("<text:note-citation>" .
"</text:note-citation>")
1023 (org-odt-format-tags
1024 '("<text:note-body>" .
"</text:note-body>")
1028 (defun org-odt-format-footnote-reference (n def refcnt
)
1030 (org-odt-format-footnote n def
)
1031 (org-odt-format-footnote-ref n
)))
1033 (defun org-odt-format-footnote-ref (n)
1034 (let ((note-class "footnote")
1036 (ref-name (concat "fn" n
)))
1037 (org-odt-format-tags
1038 '("<text:span text:style-name=\"%s\">" .
"</text:span>")
1039 (org-odt-format-tags
1040 '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" .
"</text:note-ref>")
1041 n note-class ref-format ref-name
)
1044 (defun org-odt-get-image-name (file-name)
1048 (concat (sha1 file-name
) "." (file-name-extension file-name
)) "Pictures")))
1050 (defun org-export-odt-format-image (src href
1053 "Create image tag with source and attributes."
1056 (let (embed-as caption attr label attr-plist size width height
)
1059 ((string-match "^ltxpng/" src
)
1060 ;; FIXME: Anyway the latex src can be embedded as an
1063 ;; (org-find-text-property-in-string 'org-latex-src src)
1064 (setq caption nil attr nil label nil embed-as
'character
))
1067 (setq caption
(org-find-text-property-in-string 'org-caption src
)
1068 caption
(and caption
(org-xml-format-desc caption
))
1069 attr
(org-find-text-property-in-string 'org-attributes src
)
1070 label
(org-find-text-property-in-string 'org-label src
)
1071 embed-as
'paragraph
)))
1073 (setq attr-plist
(when attr
(read attr
)))
1074 (setq size
(org-odt-image-size-from-file
1075 src
(plist-get attr-plist
:width
)
1076 (plist-get attr-plist
:height
)
1077 (plist-get attr-plist
:scale
) nil embed-as
))
1079 (org-export-odt-do-format-image embed-as caption attr label
1081 (defun org-odt-format-textbox (text style
)
1082 (let ((draw-frame-pair
1083 '("<draw:frame draw:style-name=\"%s\"
1084 text:anchor-type=\"paragraph\"
1085 style:rel-width=\"100%%\"
1086 draw:z-index=\"0\">" .
"</draw:frame>")))
1087 (org-odt-format-tags
1089 (org-odt-format-tags
1090 '("<draw:text-box fo:min-height=\"%dcm\">" .
"</draw:text-box>")
1093 (defun org-odt-format-inlinetask (heading content
1094 &optional todo priority tags
)
1095 (org-odt-format-stylized-paragraph
1096 nil
(org-odt-format-textbox
1097 (concat (org-odt-format-stylized-paragraph
1098 "OrgInlineTaskHeading"
1100 'HEADLINE
(concat (org-lparse-format-todo todo
) " " heading
)
1102 content
) "OrgInlineTaskFrame")))
1104 (defun org-export-odt-do-format-image (embed-as caption attr label
1106 "Create image tag with source and attributes."
1108 (let ((width (car size
)) (height (cdr size
))
1110 '("<draw:frame draw:style-name=\"%s\"
1111 text:anchor-type=\"%s\"
1112 draw:z-index=\"%d\" %s>" .
"</draw:frame>")))
1114 ((and (not caption
) (not label
))
1115 (let (style-name anchor-type
)
1117 ((eq embed-as
'paragraph
)
1118 (setq style-name
"OrgGraphicsParagraph" anchor-type
"paragraph"))
1119 ((eq embed-as
'character
)
1120 (setq style-name
"OrgGraphicsBaseline" anchor-type
"as-char")))
1121 (org-odt-format-tags
1122 draw-frame-pair href style-name anchor-type
0
1123 (org-odt-image-attrs-from-size width height
))))
1127 ;; (when par-open (org-odt-close-par))
1128 (org-odt-format-tags
1130 (org-odt-format-tags
1131 '("<draw:text-box fo:min-height=\"%dcm\">" .
"</draw:text-box>")
1132 (org-odt-format-stylized-paragraph
1135 (let ((extra " style:rel-width=\"100%\" style:rel-height=\"scale\""))
1136 (org-odt-format-tags
1137 draw-frame-pair href
"OrgGraphicsParagraphContent" "paragraph" 2
1138 (concat (org-odt-image-attrs-from-size width height
) extra
)))
1139 (org-odt-format-entity-caption label caption
)))
1141 "OrgFrame" "paragraph" 1
1142 (org-odt-image-attrs-from-size width
))
1144 ;; (when par-open (org-odt-open-par))
1147 ;; xml files generated on-the-fly
1148 (defconst org-export-odt-save-list
1149 '("mimetype" "META-INF/manifest.xml" "content.xml" "meta.xml" "styles.xml"))
1151 ;; xml files that are copied
1152 (defconst org-export-odt-nosave-list
'())
1154 ;; xml files that contribute to the final odt file
1155 (defvar org-export-odt-file-list nil
)
1157 (defconst org-export-odt-manifest-lines
1158 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1159 "<manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">"
1160 "<manifest:file-entry manifest:media-type=\"application/vnd.oasis.opendocument.text\" manifest:version=\"1.2\" manifest:full-path=\"/\"/>"
1161 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"content.xml\"/>"
1162 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"styles.xml\"/>"
1163 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"meta.xml\"/>"
1164 "<manifest:file-entry manifest:media-type=\"\" manifest:full-path=\"Pictures/\"/>") .
("</manifest:manifest>")))
1166 (defconst org-export-odt-meta-lines
1167 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1168 "<office:document-meta"
1169 " xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\""
1170 " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
1171 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\""
1172 " xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\""
1173 " xmlns:ooo=\"http://openoffice.org/2004/office\" "
1174 " office:version=\"1.2\">"
1175 " <office:meta>") .
(" </office:meta>" "</office:document-meta>")))
1177 (defun org-odt-copy-image-file (path &optional target-file
)
1178 "Returns the internal name of the file"
1179 (let* ((image-type (file-name-extension path
))
1180 (media-type (format "image/%s" image-type
))
1181 (src-file (expand-file-name
1182 path
(file-name-directory org-current-export-file
)))
1183 (target-file (or target-file
(org-odt-get-image-name src-file
)))
1187 (when (not org-lparse-to-buffer
)
1188 (message "Embedding %s as %s ..."
1189 (substring-no-properties path
) target-file
)
1190 (copy-file src-file target-file
'overwrite
)
1191 (org-odt-update-manifest-file media-type target-file
)
1192 (push target-file org-export-odt-file-list
)) target-file
))
1194 (defun org-odt-image-attrs-from-size (&optional width height
)
1196 (when width
(format "svg:width=\"%0.2fcm\"" width
))
1198 (when height
(format "svg:height=\"%0.2fcm\"" height
))))
1200 (defvar org-export-odt-image-size-probe-method
1201 '(emacs imagemagick force
)
1202 "Ordered list of methods by for determining size of an embedded
1205 (defvar org-export-odt-default-image-sizes-alist
1206 '(("character" .
(5 .
0.4))
1207 ("paragraph" .
(5 .
5)))
1208 "Hardcoded image dimensions one for each of the anchor
1211 (defun org-odt-do-image-size (probe-method file
&optional dpi anchor-type
)
1212 (setq dpi
(or dpi org-export-odt-pixels-per-inch
))
1213 (setq anchor-type
(or anchor-type
"paragraph"))
1214 (flet ((size-in-cms (size-in-pixels)
1215 (flet ((pixels-to-cms (pixels)
1216 (let* ((cms-per-inch 2.54)
1217 (inches (/ pixels dpi
)))
1218 (* cms-per-inch inches
))))
1220 (cons (pixels-to-cms (car size-in-pixels
))
1221 (pixels-to-cms (cdr size-in-pixels
)))))))
1224 (size-in-cms (ignore-errors (image-size (create-image file
) 'pixels
))))
1227 (let ((dim (shell-command-to-string
1228 (format "identify -format \"%%w:%%h\" \"%s\"" file
))))
1229 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim
)
1230 (cons (string-to-number (match-string 1 dim
))
1231 (string-to-number (match-string 2 dim
)))))))
1233 (cdr (assoc-string anchor-type
1234 org-export-odt-default-image-sizes-alist
))))))
1236 (defun org-odt-image-size-from-file (file &optional user-width
1237 user-height scale dpi embed-as
)
1238 (unless (file-name-absolute-p file
)
1239 (setq file
(expand-file-name
1240 file
(file-name-directory org-current-export-file
))))
1241 (let* (size width height
)
1242 (unless (and user-height user-width
)
1243 (loop for probe-method in org-export-odt-image-size-probe-method
1245 do
(setq size
(org-odt-do-image-size
1246 probe-method file dpi embed-as
)))
1247 (or size
(error "Cannot determine Image size. Aborting ..."))
1248 (setq width
(car size
) height
(cdr size
)))
1251 (setq width
(* width scale
) height
(* height scale
)))
1252 ((and user-height user-width
)
1253 (setq width user-width height user-height
))
1255 (setq width
(* user-height
(/ width height
)) height user-height
))
1257 (setq height
(* user-width
(/ height width
)) width user-width
))
1259 (cons width height
)))
1261 (defvar org-odt-default-entity
"Illustration")
1262 (defun org-odt-format-entity-caption (label caption
&optional default-entity
)
1263 (if (not label
) (or caption
"")
1264 (let* ((label-components (org-odt-parse-label label
))
1265 (entity (car label-components
))
1266 (seqno (cdr label-components
))
1267 (caption (and caption
(concat ": " caption
))))
1270 entity
(or default-entity org-odt-default-entity
)))
1273 (org-odt-format-tags
1274 '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" .
"</text:sequence>")
1275 seqno label entity entity
)
1278 (defun org-odt-format-tags (tag text
&rest args
)
1279 (let ((prefix (when org-lparse-encode-pending
"@"))
1280 (suffix (when org-lparse-encode-pending
"@")))
1281 (apply 'org-lparse-format-tags tag text prefix suffix args
)))
1283 (defun org-odt-init-outfile (filename)
1284 (unless (executable-find "zip")
1285 ;; Not at all OSes ship with zip by default
1286 (error "Executable \"zip\" needed for creating OpenDocument files. Aborting."))
1288 (let* ((outdir (make-temp-file org-export-odt-tmpdir-prefix t
))
1289 (mimetype-file (expand-file-name "mimetype" outdir
))
1290 (content-file (expand-file-name "content.xml" outdir
))
1291 (manifest-file (expand-file-name "META-INF/manifest.xml" outdir
))
1292 (meta-file (expand-file-name "meta.xml" outdir
))
1293 (styles-file (expand-file-name "styles.xml" outdir
))
1294 (pictures-dir (expand-file-name "Pictures" outdir
))
1298 (with-current-buffer (find-file-noselect content-file t
)
1301 ;; FIXME: How to factor in body-only here
1304 (make-directory (file-name-directory manifest-file
))
1305 (with-current-buffer (find-file-noselect manifest-file t
)
1307 (insert (mapconcat 'identity
(car org-export-odt-manifest-lines
) "\n"))
1310 (insert (mapconcat 'identity
(cdr org-export-odt-manifest-lines
) "\n"))))
1313 (with-current-buffer (find-file-noselect meta-file t
)
1315 (insert (mapconcat 'identity
(car org-export-odt-meta-lines
) "\n"))
1318 (insert (mapconcat 'identity
(cdr org-export-odt-meta-lines
) "\n"))))
1321 (with-current-buffer (find-file-noselect mimetype-file t
)
1322 (insert "application/vnd.oasis.opendocument.text"))
1325 ;; (copy-file org-export-odt-styles-file styles-file t)
1328 (make-directory pictures-dir
)
1330 ;; initialize list of files that contribute to the odt file
1331 (setq org-export-odt-file-list
1332 (append org-export-odt-save-list org-export-odt-nosave-list
)))
1335 (defconst org-odt-manifest-file-entry-tag
1336 "<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"/>")
1338 (defcustom org-export-odt-prettify-xml nil
1339 "Specify whether or not the xml output should be prettified.
1340 When this option is turned on, `indent-region' is run on all
1341 component xml buffers before they are saved. Turn this off for
1342 regular use. Turn this on if you need to examine the xml
1344 :group
'org-export-odt
1347 (defun org-odt-save-as-outfile (target opt-plist
)
1349 (org-odt-update-meta-file opt-plist
)
1351 ;; write styles file
1352 (org-odt-copy-styles-file)
1354 ;; Update styles.xml - take care of outline numbering
1355 (with-current-buffer
1356 (find-file-noselect (expand-file-name "styles.xml") t
)
1357 ;; Don't make automatic backup of styles.xml file. This setting
1358 ;; prevents the backedup styles.xml file from being zipped in to
1359 ;; odt file. This is more of a hackish fix. Better alternative
1360 ;; would be to fix the zip command so that the output odt file
1361 ;; includes only the needed files and excludes any auto-generated
1362 ;; extra files like backups and auto-saves etc etc. Note that
1363 ;; currently the zip command zips up the entire temp directory so
1364 ;; that any auto-generated files created under the hood ends up in
1365 ;; the resulting odt file.
1366 (set (make-local-variable 'backup-inhibited
) t
)
1368 ;; Import local setting of `org-export-with-section-numbers'
1369 (org-lparse-bind-local-variables opt-plist
)
1370 (org-odt-configure-outline-numbering
1371 (if org-export-with-section-numbers org-export-headline-levels
0)))
1373 (let ((zipdir default-directory
))
1374 (message "Switching to directory %s" (expand-file-name zipdir
))
1376 ;; save all xml files
1377 (mapc (lambda (file)
1378 (with-current-buffer
1379 (find-file-noselect (expand-file-name file
) t
)
1380 ;; prettify output if needed
1381 (when org-export-odt-prettify-xml
1382 (indent-region (point-min) (point-max)))
1384 org-export-odt-save-list
)
1386 (let* ((target-name (file-name-nondirectory target
))
1387 (target-dir (file-name-directory target
))
1388 (cmds `(("zip" "-mX0" ,target-name
"mimetype")
1389 ("zip" "-rmTq" ,target-name
"."))))
1390 (when (file-exists-p target
)
1391 ;; FIXME: If the file is locked this throws a cryptic error
1392 (delete-file target
))
1394 (let ((coding-system-for-write 'no-conversion
) exitcode
)
1395 (message "Creating odt file...")
1398 (message "Running %s" (mapconcat 'identity cmd
" "))
1400 (apply 'call-process
(car cmd
) nil nil nil
(cdr cmd
)))
1401 (or (zerop exitcode
)
1402 (error "Unable to create odt file (%S)" exitcode
)))
1405 ;; move the file from outdir to target-dir
1406 (rename-file target-name target-dir
)
1408 ;; kill all xml buffers
1409 (mapc (lambda (file)
1411 (find-file-noselect (expand-file-name file zipdir
) t
)))
1412 org-export-odt-save-list
)
1414 (delete-directory zipdir
)))
1416 (message "Created %s" target
)
1417 (set-buffer (find-file-noselect target t
)))
1419 (defun org-odt-format-date (date)
1421 "OpenDocument files require that dates be in ISO-8601 format. Please review your DATE options for compatibility."))
1422 ;; If the user is not careful with the date specification, an
1423 ;; invalid meta.xml will be emitted.
1425 ;; For now honor user's diktat and let him off with a warning
1426 ;; message. This is OK as LibreOffice (and possibly other
1427 ;; apps) doesn't deem this deviation as critical and continue
1428 ;; to load the file.
1430 ;; FIXME: Surely there a better way to handle this. Revisit this
1433 ((and date
(string-match "%" date
))
1434 ;; Honor user's diktat. See comments above
1435 (org-lparse-warn warning-msg
)
1436 (format-time-string date
))
1438 ;; Honor user's diktat. See comments above
1439 (org-lparse-warn warning-msg
)
1443 (format-time-string "%Y-%m-%dT%T%:z")))))
1445 (defun org-odt-update-meta-file (opt-plist)
1446 (with-current-buffer
1447 (find-file-noselect (expand-file-name "meta.xml") t
)
1448 (let ((date (org-odt-format-date (plist-get opt-plist
:date
)))
1449 (author (or (plist-get opt-plist
:author
) ""))
1450 (email (plist-get opt-plist
:email
))
1451 (keywords (plist-get opt-plist
:keywords
))
1452 (description (plist-get opt-plist
:description
))
1453 (title (plist-get opt-plist
:title
)))
1457 (org-odt-format-tags '("<dc:creator>" .
"</dc:creator>") author
)
1458 (org-odt-format-tags
1459 '("\n<meta:initial-creator>" .
"</meta:initial-creator>") author
)
1460 (org-odt-format-tags '("\n<dc:date>" .
"</dc:date>") date
)
1461 (org-odt-format-tags
1462 '("\n<meta:creation-date>" .
"</meta:creation-date>") date
)
1463 (org-odt-format-tags '("\n<meta:generator>" .
"</meta:generator>")
1464 (when org-export-creator-info
1465 (format "Org-%s/Emacs-%s"
1466 org-version emacs-version
)))
1467 (org-odt-format-tags '("\n<meta:keyword>" .
"</meta:keyword>") keywords
)
1468 (org-odt-format-tags '("\n<dc:subject>" .
"</dc:subject>") description
)
1469 (org-odt-format-tags '("\n<dc:title>" .
"</dc:title>") title
)
1472 (defun org-odt-update-manifest-file (media-type full-path
)
1473 (with-current-buffer
1474 (find-file-noselect (expand-file-name "META-INF/manifest.xml") t
)
1475 (insert (format org-odt-manifest-file-entry-tag media-type full-path
))))
1477 (defun org-odt-finalize-outfile ()
1478 (org-odt-delete-empty-paragraphs))
1480 (defun org-odt-delete-empty-paragraphs ()
1481 (goto-char (point-min))
1482 (let ((open "<text:p[^>]*>")
1483 (close "</text:p>"))
1484 (while (re-search-forward (format "%s[ \r\n\t]*%s" open close
) nil t
)
1485 (replace-match ""))))
1487 (defun org-odt-get (what &optional opt-plist
)
1490 (EXPORT-DIR (org-export-directory :html opt-plist
))
1491 (FILE-NAME-EXTENSION "odt")
1492 (EXPORT-BUFFER-NAME "*Org ODT Export*")
1493 (ENTITY-CONTROL org-odt-entity-control-callbacks-alist
)
1494 (ENTITY-FORMAT org-odt-entity-format-callbacks-alist
)
1495 (INIT-METHOD 'org-odt-init-outfile
)
1496 (FINAL-METHOD 'org-odt-finalize-outfile
)
1497 (SAVE-METHOD 'org-odt-save-as-outfile
)
1498 ;; (OTHER-BACKENDS) ; see note in `org-xhtml-get'
1499 ;; (CONVERT-METHOD) ; see note in `org-xhtml-get'
1501 (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps
)
1502 (INLINE-IMAGES 'maybe
)
1503 (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
1504 (PLAIN-TEXT-MAP '(("&" .
"&") ("<" .
"<") (">" .
">")))
1505 (TABLE-FIRST-COLUMN-AS-LABELS nil
)
1506 (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY
"," 'superscript
))
1507 (CODING-SYSTEM-FOR-WRITE 'utf-8
)
1508 (CODING-SYSTEM-FOR-SAVE 'utf-8
)
1509 (t (error "Unknown property: %s" what
))))
1511 (defun org-odt-parse-label (label)
1513 (if (not (string-match "\\`[a-zA-Z]+:\\(.+\\)" label
))
1516 (capitalize (substring label
0 (1- (match-beginning 1))))
1517 (substring label
(match-beginning 1))))))
1519 (defvar org-lparse-latex-fragment-fallback
) ; set by org-do-lparse
1520 (defun org-export-odt-preprocess (parameters)
1521 "Convert LaTeX fragments to images."
1522 (when (and org-current-export-file
1523 (plist-get parameters
:LaTeX-fragments
))
1525 (concat "ltxpng/" (file-name-sans-extension
1526 (file-name-nondirectory
1527 org-current-export-file
)))
1528 org-current-export-dir nil
"Creating LaTeX image %s"
1531 ((eq (plist-get parameters
:LaTeX-fragments
) 'verbatim
) 'verbatim
)
1532 ;; Investigate MathToWeb for converting TeX equations to MathML
1533 ;; See http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01755.html
1534 ((or (eq (plist-get parameters
:LaTeX-fragments
) 'mathjax
)
1535 (eq (plist-get parameters
:LaTeX-fragments
) t
))
1538 "Use of MathJax is incompatible with ODT exporter. "
1539 (format "Using %S instead." org-lparse-latex-fragment-fallback
)))
1540 org-lparse-latex-fragment-fallback
)
1541 ((eq (plist-get parameters
:LaTeX-fragments
) 'dvipng
) 'dvipng
)
1543 (goto-char (point-min))
1544 (let (label label-components category value pretty-label
)
1545 (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t
)
1546 (org-if-unprotected-at (match-beginning 1)
1547 (setq label
(match-string 1)
1548 label-components
(org-odt-parse-label label
)
1549 category
(car label-components
)
1550 value
(cdr label-components
)
1551 pretty-label
(if value
(concat category
" " value
) label
))
1553 (let ((org-lparse-encode-pending t
))
1554 (org-odt-format-tags
1555 '("<text:sequence-ref text:reference-format=\"category-and-value\" text:ref-name=\"%s\">"
1556 .
"</text:sequence-ref>") pretty-label label
)) t t
)))))
1558 (declare-function archive-zip-extract
"arc-mode.el" (archive name
))
1559 (defun org-odt-zip-extract-one (archive member
&optional target
)
1561 (let* ((target (or target default-directory
))
1562 (archive (expand-file-name archive
))
1563 (archive-zip-extract
1564 (list "unzip" "-qq" "-o" "-d" target
))
1565 exit-code command-output
)
1566 (setq command-output
1568 (setq exit-code
(archive-zip-extract archive member
))
1570 (unless (zerop exit-code
)
1571 (message command-output
)
1572 (error "Extraction failed"))))
1574 (defun org-odt-zip-extract (archive members
&optional target
)
1575 (when (atom members
) (setq members
(list members
)))
1576 (mapc (lambda (member)
1577 (org-odt-zip-extract-one archive member target
))
1580 (defun org-odt-copy-styles-file (&optional styles-file
)
1581 ;; Non-availability of styles.xml is not a critical error. For now
1582 ;; throw an error purely for aesthetic reasons.
1583 (setq styles-file
(or styles-file
1584 org-export-odt-styles-file
1585 (expand-file-name "styles/OrgOdtStyles.xml"
1587 (error "org-odt: Missing styles file?")))
1589 ((listp styles-file
)
1590 (let ((archive (nth 0 styles-file
))
1591 (members (nth 1 styles-file
)))
1592 (org-odt-zip-extract archive members
)
1595 (when (org-file-image-p member
)
1596 (let* ((image-type (file-name-extension member
))
1597 (media-type (format "image/%s" image-type
)))
1598 (org-odt-update-manifest-file media-type member
))))
1600 ((and (stringp styles-file
) (file-exists-p styles-file
))
1601 (let ((styles-file-type (file-name-extension styles-file
)))
1603 ((string= styles-file-type
"xml")
1604 (copy-file styles-file
"styles.xml" t
))
1605 ((member styles-file-type
'("odt" "ott"))
1606 (org-odt-zip-extract styles-file
"styles.xml")))))
1608 (error (format "Invalid specification of styles.xml file: %S"
1609 org-export-odt-styles-file
)))))
1611 (defvar org-export-odt-factory-settings
1612 "d4328fb9d1b6cb211d4320ff546829f26700dc5e"
1613 "SHA1 hash of OrgOdtStyles.xml.")
1615 (defun org-odt-configure-outline-numbering (level)
1616 "Outline numbering is retained only upto LEVEL.
1617 To disable outline numbering pass a LEVEL of 0."
1618 (if (not (string= org-export-odt-factory-settings
(sha1 (current-buffer))))
1620 "org-odt: Using custom styles file? Consider tweaking styles.xml for better output. To suppress this warning update `org-export-odt-factory-settings'")
1621 (goto-char (point-min))
1623 "<text:outline-level-style\\(.*\\)text:level=\"\\(.*\\)\"\\(.*\\)>")
1625 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1626 (while (re-search-forward regex nil t
)
1627 (when (> (string-to-number (match-string 1)) level
)
1628 (replace-match replacement t nil
))))
1633 ;;; org-odt.el ends here