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 .
"OrgFixedWidthBlock")
232 (quote .
"Quotations")
233 (blockquote .
"Quotations")
234 (center .
"OrgCenter")
237 (title .
"Heading_20_1.title")
238 (footnote .
"Footnote")
239 (src .
"OrgSrcBlock")
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 'fixedwidth
(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 (defcustom org-export-odt-use-htmlfontify t
856 "Specify whether or not source blocks need to be fontified.
857 Turn this option on if you want to colorize the source code
858 blocks in the exported file. For colorization to work, you need
859 to make available an enhanced version of `htmlfontify' library."
861 :group
'org-export-odt
)
863 (defun org-odt-format-source-code-or-example-plain
864 (lines lang caption textareap cols rows num cont rpllbl fmt
)
865 "Format source or example blocks much like fixedwidth blocks.
866 Use this when `org-export-odt-use-htmlfontify' option is turned
868 (setq lines
(org-export-number-lines (org-xml-encode-plain-text-lines lines
)
869 0 0 num cont rpllbl fmt
))
872 (org-odt-format-stylized-paragraph
873 'fixedwidth
(org-odt-fill-tabs-and-spaces line
)))
874 (org-split-string lines
"[\r\n]") "\n"))
876 (defvar org-src-block-paragraph-format
877 "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
878 <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
879 <style:background-image/>
880 </style:paragraph-properties>
881 <style:text-properties fo:color=\"%s\"/>
883 "Custom paragraph style for colorized source and example blocks.
884 This style is much the same as that of \"OrgFixedWidthBlock\"
885 except that the foreground and background colors are set
886 according to the default face identified by the `htmlfontify'.")
888 (defun org-odt-hfy-face-to-css (fn)
889 "Create custom style for face FN.
890 When FN is the default face, use it's foreground and background
891 properties to create \"OrgSrcBlock\" paragraph style. Otherwise
892 use it's color attribute to create a character style whose name
893 is obtained from FN. Currently all attributes of FN other than
896 The style name for a face FN is derived using the following
897 operations on the face name in that order - de-dash, CamelCase
898 and prefix with \"OrgSrc\". For example,
899 `font-lock-function-name-face' is associated with
900 \"OrgSrcFontLockFunctionNameFace\"."
901 (let* ((css-list (hfy-face-to-style fn
))
902 (style-name ((lambda (fn)
905 'capitalize
(split-string
906 (hfy-face-or-def-to-name fn
) "-")
908 (color-val (cdr (assoc "color" css-list
)))
909 (background-color-val (cdr (assoc "background" css-list
)))
910 (style (and org-export-odt-create-custom-styles-for-srcblocks
913 (format org-src-block-paragraph-format
914 background-color-val color-val
))
918 <style:style style:name=\"%s\" style:family=\"text\">
919 <style:text-properties fo:color=\"%s\"/>
920 </style:style>" style-name color-val
))))))
921 (cons style-name style
)))
923 (defcustom org-export-odt-create-custom-styles-for-srcblocks t
924 "Whether custom styles for colorized source blocks be automatically created.
925 When this option is turned on, the exporter creates custom styles
926 for source blocks based on the advice of `htmlfontify'. Creation
927 of custom styles happen as part of `org-odt-hfy-face-to-css'.
929 When this option is turned off exporter does not create such
932 Use the latter option if you do not want the custom styles to be
933 based on your current display settings. It is necessary that the
934 styles.xml already contains needed styles for colorizing to work.
936 This variable is effective only if
937 `org-export-odt-use-htmlfontify' is turned on."
938 :group
'org-export-odt
941 (defun org-odt-insert-custom-styles-for-srcblocks (styles)
942 "Save STYLES used for colorizing of source blocks.
943 Update styles.xml with styles that were collected as part of
944 `org-odt-hfy-face-to-css' callbacks."
947 (find-file-noselect (expand-file-name "styles.xml") t
)
948 (goto-char (point-min))
949 (when (re-search-forward "</office:styles>" nil t
)
950 (goto-char (match-beginning 0))
951 (insert "\n<!-- Org Htmlfontify Styles -->\n" styles
"\n")))))
953 (defun org-odt-format-source-code-or-example-colored
954 (lines lang caption textareap cols rows num cont rpllbl fmt
)
955 "Format source or example blocks using `htmlfontify-string'.
956 Use this routine when `org-export-odt-use-htmlfontify' option is
958 (let* ((lang-m (and lang
(or (cdr (assoc lang org-src-lang-modes
)) lang
)))
959 (mode (and lang-m
(intern (concat (if (symbolp lang-m
)
962 (org-inhibit-startup t
)
963 (org-startup-folded nil
)
964 (lines (with-temp-buffer
966 (if (functionp mode
) (funcall mode
) (fundamental-mode))
967 (font-lock-fontify-buffer)
969 (hfy-html-quote-regex "\\([<\"&> ]\\)")
970 (hfy-html-quote-map '(("\"" """)
975 (" " "<text:tab/>")))
976 (hfy-face-to-css 'org-odt-hfy-face-to-css
)
977 (hfy-optimisations-1 (copy-seq hfy-optimisations
))
978 (hfy-optimisations (add-to-list 'hfy-optimisations-1
980 (hfy-begin-span-handler
981 (lambda (style text-block text-id text-begins-block-p
)
982 (insert (format "<text:span text:style-name=\"%s\">" style
))))
983 (hfy-end-span-handler (lambda nil
(insert "</text:span>"))))
986 (org-odt-format-stylized-paragraph 'src
(htmlfontify-string line
)))
987 (org-split-string lines
"[\r\n]") "\n")))
989 (defun org-odt-format-source-code-or-example (lines lang caption textareap
992 "Format source or example blocks for export.
993 Use `org-odt-format-source-code-or-example-plain' or
994 `org-odt-format-source-code-or-example-colored' depending on the
995 value of `org-export-odt-use-htmlfontify."
997 (if (and org-export-odt-use-htmlfontify
998 (or (featurep 'htmlfontify
) (require 'htmlfontify
))
999 (fboundp 'htmlfontify-string
))
1000 'org-odt-format-source-code-or-example-colored
1001 'org-odt-format-source-code-or-example-plain
)
1002 lines lang caption textareap cols rows num cont rpllbl fmt
))
1004 (defun org-xml-encode-plain-text-lines (rtn)
1005 (mapconcat 'org-xml-encode-plain-text
(org-split-string rtn
"[\r\n]") "\n"))
1007 (defun org-odt-remap-stylenames (style-name)
1009 (cdr (assoc style-name
'(("timestamp-wrapper" .
"OrgTimestampWrapper")
1010 ("timestamp" .
"OrgTimestamp")
1011 ("timestamp-kwd" .
"OrgTimestampKeyword")
1013 ("todo" .
"OrgTodo")
1014 ("done" .
"OrgDone")
1015 ("target" .
"OrgTarget"))))
1018 (defun org-odt-format-fontify (text style
&optional id
)
1022 (org-odt-remap-stylenames style
))
1024 (org-odt-get-style-name-for-entity 'character style
))
1026 (assert (< 1 (length style
)))
1027 (let ((parent-style (pop style
)))
1028 (mapconcat (lambda (s)
1029 ;; (assert (stringp s) t)
1030 (org-odt-remap-stylenames s
)) style
"")
1031 (org-odt-remap-stylenames parent-style
)))
1032 (t (error "Don't how to handle style %s" style
)))))
1033 (org-odt-format-tags
1034 '("<text:span text:style-name=\"%s\">" .
"</text:span>")
1037 (defun org-odt-relocate-relative-path (path dir
)
1038 (if (file-name-absolute-p path
) path
1039 (file-relative-name (expand-file-name path dir
)
1040 (expand-file-name "eyecandy" dir
))))
1042 (defun org-odt-format-inline-image (thefile)
1043 (let* ((thelink (if (file-name-absolute-p thefile
) thefile
1044 (org-xml-format-href
1045 (org-odt-relocate-relative-path
1046 thefile org-current-export-file
))))
1048 (org-odt-format-tags
1049 "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
1050 (if org-export-odt-embed-images
1051 (org-odt-copy-image-file thefile
) thelink
))))
1052 (org-export-odt-format-image thefile href
)))
1054 (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
1057 OPT-PLIST is an options list.
1058 TYPE is the device-type of the link (THIS://foo.html)
1059 PATH is the path of the link (http://THIS#locationx)
1060 FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
1061 DESC is the link description, if any.
1062 ATTR is a string of other attributes of the a element.
1063 MAY-INLINE-P allows inlining it as an image."
1065 (declare (special org-lparse-par-open
))
1067 (let* ((may-inline-p
1068 (and (member type-1
'("http" "https" "file"))
1069 (org-lparse-should-inline-p path descp
)
1071 (type (if (equal type-1
"id") "file" type-1
))
1076 ;; check for inlined images
1077 ((and (member type
'("file"))
1080 filename org-odt-export-inline-image-extensions
)
1081 (or (eq t org-odt-export-inline-images
)
1082 (and org-odt-export-inline-images
(not descp
))))
1084 ;; (when (and (string= type "file") (file-name-absolute-p path))
1085 ;; (setq thefile (concat "file://" (expand-file-name path))))
1086 ;; (setq thefile (org-xml-format-href thefile))
1087 ;; (org-export-html-format-image thefile)
1088 (org-odt-format-inline-image thefile
))
1090 (when (string= type
"file")
1093 ((file-name-absolute-p path
)
1094 (concat "file://" (expand-file-name path
)))
1095 (t (org-odt-relocate-relative-path
1096 thefile org-current-export-file
)))))
1098 (when (and (member type
'("" "http" "https" "file" "coderef"))
1100 (setq thefile
(concat thefile
"#" fragment
)))
1102 (setq thefile
(org-xml-format-href thefile
))
1104 (when (not (member type
'("" "file" "coderef")))
1105 (setq thefile
(concat type
":" thefile
)))
1107 (let ((org-odt-suppress-xref (string= type
"coderef")))
1108 (org-odt-format-link
1109 (org-xml-format-desc desc
) thefile attr
)))))))
1111 (defun org-odt-format-heading (text level
&optional id
)
1112 (let* ((text (if id
(org-odt-format-target text id
) text
)))
1113 (org-odt-format-tags
1114 '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
1115 "</text:h>") text level level
)))
1117 (defun org-odt-format-headline (title extra-targets tags
1118 &optional snumber level
)
1120 (org-lparse-format 'EXTRA-TARGETS extra-targets
)
1122 ;; No need to generate section numbers. They are auto-generated by
1125 ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
1127 (and tags
(concat (org-lparse-format 'SPACES
3)
1128 (org-lparse-format 'ORG-TAGS tags
)))))
1130 (defun org-odt-format-anchor (text name
&optional class
)
1131 (org-odt-format-target text name
))
1133 (defun org-odt-format-bookmark (text id
)
1135 (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id
)
1138 (defun org-odt-format-target (text id
)
1139 (let ((name (concat org-export-odt-bookmark-prefix id
)))
1141 (and id
(org-odt-format-tags
1142 "<text:bookmark-start text:name=\"%s\"/>" "" name
))
1143 (org-odt-format-bookmark text id
)
1144 (and id
(org-odt-format-tags
1145 "<text:bookmark-end text:name=\"%s\"/>" "" name
)))))
1147 (defun org-odt-format-footnote (n def
)
1148 (let ((id (concat "fn" n
))
1149 (note-class "footnote")
1150 (par-style "Footnote"))
1151 (org-odt-format-tags
1152 '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
1155 (org-odt-format-tags
1156 '("<text:note-citation>" .
"</text:note-citation>")
1158 (org-odt-format-tags
1159 '("<text:note-body>" .
"</text:note-body>")
1163 (defun org-odt-format-footnote-reference (n def refcnt
)
1165 (org-odt-format-footnote n def
)
1166 (org-odt-format-footnote-ref n
)))
1168 (defun org-odt-format-footnote-ref (n)
1169 (let ((note-class "footnote")
1171 (ref-name (concat "fn" n
)))
1172 (org-odt-format-tags
1173 '("<text:span text:style-name=\"%s\">" .
"</text:span>")
1174 (org-odt-format-tags
1175 '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" .
"</text:note-ref>")
1176 n note-class ref-format ref-name
)
1179 (defun org-odt-get-image-name (file-name)
1183 (concat (sha1 file-name
) "." (file-name-extension file-name
)) "Pictures")))
1185 (defun org-export-odt-format-image (src href
1188 "Create image tag with source and attributes."
1191 (let (embed-as caption attr label attr-plist size width height
)
1194 ((string-match "^ltxpng/" src
)
1195 ;; FIXME: Anyway the latex src can be embedded as an
1198 ;; (org-find-text-property-in-string 'org-latex-src src)
1199 (setq caption nil attr nil label nil embed-as
'character
))
1202 (setq caption
(org-find-text-property-in-string 'org-caption src
)
1203 caption
(and caption
(org-xml-format-desc caption
))
1204 attr
(org-find-text-property-in-string 'org-attributes src
)
1205 label
(org-find-text-property-in-string 'org-label src
)
1206 embed-as
'paragraph
)))
1208 (setq attr-plist
(when attr
(read attr
)))
1209 (setq size
(org-odt-image-size-from-file
1210 src
(plist-get attr-plist
:width
)
1211 (plist-get attr-plist
:height
)
1212 (plist-get attr-plist
:scale
) nil embed-as
))
1214 (org-export-odt-do-format-image embed-as caption attr label
1216 (defun org-odt-format-textbox (text style
)
1217 (let ((draw-frame-pair
1218 '("<draw:frame draw:style-name=\"%s\"
1219 text:anchor-type=\"paragraph\"
1220 style:rel-width=\"100%%\"
1221 draw:z-index=\"0\">" .
"</draw:frame>")))
1222 (org-odt-format-tags
1224 (org-odt-format-tags
1225 '("<draw:text-box fo:min-height=\"%dcm\">" .
"</draw:text-box>")
1228 (defun org-odt-format-inlinetask (heading content
1229 &optional todo priority tags
)
1230 (org-odt-format-stylized-paragraph
1231 nil
(org-odt-format-textbox
1232 (concat (org-odt-format-stylized-paragraph
1233 "OrgInlineTaskHeading"
1235 'HEADLINE
(concat (org-lparse-format-todo todo
) " " heading
)
1237 content
) "OrgInlineTaskFrame")))
1239 (defun org-export-odt-do-format-image (embed-as caption attr label
1241 "Create image tag with source and attributes."
1243 (let ((width (car size
)) (height (cdr size
))
1245 '("<draw:frame draw:style-name=\"%s\"
1246 text:anchor-type=\"%s\"
1247 draw:z-index=\"%d\" %s>" .
"</draw:frame>")))
1249 ((and (not caption
) (not label
))
1250 (let (style-name anchor-type
)
1252 ((eq embed-as
'paragraph
)
1253 (setq style-name
"OrgGraphicsParagraph" anchor-type
"paragraph"))
1254 ((eq embed-as
'character
)
1255 (setq style-name
"OrgGraphicsBaseline" anchor-type
"as-char")))
1256 (org-odt-format-tags
1257 draw-frame-pair href style-name anchor-type
0
1258 (org-odt-image-attrs-from-size width height
))))
1262 ;; (when par-open (org-odt-close-par))
1263 (org-odt-format-tags
1265 (org-odt-format-tags
1266 '("<draw:text-box fo:min-height=\"%dcm\">" .
"</draw:text-box>")
1267 (org-odt-format-stylized-paragraph
1270 (let ((extra " style:rel-width=\"100%\" style:rel-height=\"scale\""))
1271 (org-odt-format-tags
1272 draw-frame-pair href
"OrgGraphicsParagraphContent" "paragraph" 2
1273 (concat (org-odt-image-attrs-from-size width height
) extra
)))
1274 (org-odt-format-entity-caption label caption
)))
1276 "OrgFrame" "paragraph" 1
1277 (org-odt-image-attrs-from-size width
))
1279 ;; (when par-open (org-odt-open-par))
1282 ;; xml files generated on-the-fly
1283 (defconst org-export-odt-save-list
1284 '("mimetype" "META-INF/manifest.xml" "content.xml" "meta.xml" "styles.xml"))
1286 ;; xml files that are copied
1287 (defconst org-export-odt-nosave-list
'())
1289 ;; xml files that contribute to the final odt file
1290 (defvar org-export-odt-file-list nil
)
1292 (defconst org-export-odt-manifest-lines
1293 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1294 "<manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">"
1295 "<manifest:file-entry manifest:media-type=\"application/vnd.oasis.opendocument.text\" manifest:version=\"1.2\" manifest:full-path=\"/\"/>"
1296 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"content.xml\"/>"
1297 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"styles.xml\"/>"
1298 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"meta.xml\"/>"
1299 "<manifest:file-entry manifest:media-type=\"\" manifest:full-path=\"Pictures/\"/>") .
("</manifest:manifest>")))
1301 (defconst org-export-odt-meta-lines
1302 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1303 "<office:document-meta"
1304 " xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\""
1305 " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
1306 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\""
1307 " xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\""
1308 " xmlns:ooo=\"http://openoffice.org/2004/office\" "
1309 " office:version=\"1.2\">"
1310 " <office:meta>") .
(" </office:meta>" "</office:document-meta>")))
1312 (defun org-odt-copy-image-file (path &optional target-file
)
1313 "Returns the internal name of the file"
1314 (let* ((image-type (file-name-extension path
))
1315 (media-type (format "image/%s" image-type
))
1316 (src-file (expand-file-name
1317 path
(file-name-directory org-current-export-file
)))
1318 (target-file (or target-file
(org-odt-get-image-name src-file
)))
1322 (when (not org-lparse-to-buffer
)
1323 (message "Embedding %s as %s ..."
1324 (substring-no-properties path
) target-file
)
1325 (copy-file src-file target-file
'overwrite
)
1326 (org-odt-update-manifest-file media-type target-file
)
1327 (push target-file org-export-odt-file-list
)) target-file
))
1329 (defun org-odt-image-attrs-from-size (&optional width height
)
1331 (when width
(format "svg:width=\"%0.2fcm\"" width
))
1333 (when height
(format "svg:height=\"%0.2fcm\"" height
))))
1335 (defvar org-export-odt-image-size-probe-method
1336 '(emacs imagemagick force
)
1337 "Ordered list of methods by for determining size of an embedded
1340 (defvar org-export-odt-default-image-sizes-alist
1341 '(("character" .
(5 .
0.4))
1342 ("paragraph" .
(5 .
5)))
1343 "Hardcoded image dimensions one for each of the anchor
1346 (defun org-odt-do-image-size (probe-method file
&optional dpi anchor-type
)
1347 (setq dpi
(or dpi org-export-odt-pixels-per-inch
))
1348 (setq anchor-type
(or anchor-type
"paragraph"))
1349 (flet ((size-in-cms (size-in-pixels)
1350 (flet ((pixels-to-cms (pixels)
1351 (let* ((cms-per-inch 2.54)
1352 (inches (/ pixels dpi
)))
1353 (* cms-per-inch inches
))))
1355 (cons (pixels-to-cms (car size-in-pixels
))
1356 (pixels-to-cms (cdr size-in-pixels
)))))))
1359 (size-in-cms (ignore-errors (image-size (create-image file
) 'pixels
))))
1362 (let ((dim (shell-command-to-string
1363 (format "identify -format \"%%w:%%h\" \"%s\"" file
))))
1364 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim
)
1365 (cons (string-to-number (match-string 1 dim
))
1366 (string-to-number (match-string 2 dim
)))))))
1368 (cdr (assoc-string anchor-type
1369 org-export-odt-default-image-sizes-alist
))))))
1371 (defun org-odt-image-size-from-file (file &optional user-width
1372 user-height scale dpi embed-as
)
1373 (unless (file-name-absolute-p file
)
1374 (setq file
(expand-file-name
1375 file
(file-name-directory org-current-export-file
))))
1376 (let* (size width height
)
1377 (unless (and user-height user-width
)
1378 (loop for probe-method in org-export-odt-image-size-probe-method
1380 do
(setq size
(org-odt-do-image-size
1381 probe-method file dpi embed-as
)))
1382 (or size
(error "Cannot determine Image size. Aborting ..."))
1383 (setq width
(car size
) height
(cdr size
)))
1386 (setq width
(* width scale
) height
(* height scale
)))
1387 ((and user-height user-width
)
1388 (setq width user-width height user-height
))
1390 (setq width
(* user-height
(/ width height
)) height user-height
))
1392 (setq height
(* user-width
(/ height width
)) width user-width
))
1394 (cons width height
)))
1396 (defvar org-odt-default-entity
"Illustration")
1397 (defun org-odt-format-entity-caption (label caption
&optional default-entity
)
1398 (if (not label
) (or caption
"")
1399 (let* ((label-components (org-odt-parse-label label
))
1400 (entity (car label-components
))
1401 (seqno (cdr label-components
))
1402 (caption (and caption
(concat ": " caption
))))
1405 entity
(or default-entity org-odt-default-entity
)))
1408 (org-odt-format-tags
1409 '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" .
"</text:sequence>")
1410 seqno label entity entity
)
1413 (defun org-odt-format-tags (tag text
&rest args
)
1414 (let ((prefix (when org-lparse-encode-pending
"@"))
1415 (suffix (when org-lparse-encode-pending
"@")))
1416 (apply 'org-lparse-format-tags tag text prefix suffix args
)))
1418 (defun org-odt-init-outfile (filename)
1419 (unless (executable-find "zip")
1420 ;; Not at all OSes ship with zip by default
1421 (error "Executable \"zip\" needed for creating OpenDocument files. Aborting."))
1423 (let* ((outdir (make-temp-file org-export-odt-tmpdir-prefix t
))
1424 (mimetype-file (expand-file-name "mimetype" outdir
))
1425 (content-file (expand-file-name "content.xml" outdir
))
1426 (manifest-file (expand-file-name "META-INF/manifest.xml" outdir
))
1427 (meta-file (expand-file-name "meta.xml" outdir
))
1428 (styles-file (expand-file-name "styles.xml" outdir
))
1429 (pictures-dir (expand-file-name "Pictures" outdir
))
1433 (with-current-buffer (find-file-noselect content-file t
)
1436 ;; FIXME: How to factor in body-only here
1439 (make-directory (file-name-directory manifest-file
))
1440 (with-current-buffer (find-file-noselect manifest-file t
)
1442 (insert (mapconcat 'identity
(car org-export-odt-manifest-lines
) "\n"))
1445 (insert (mapconcat 'identity
(cdr org-export-odt-manifest-lines
) "\n"))))
1448 (with-current-buffer (find-file-noselect meta-file t
)
1450 (insert (mapconcat 'identity
(car org-export-odt-meta-lines
) "\n"))
1453 (insert (mapconcat 'identity
(cdr org-export-odt-meta-lines
) "\n"))))
1456 (with-current-buffer (find-file-noselect mimetype-file t
)
1457 (insert "application/vnd.oasis.opendocument.text"))
1460 ;; (copy-file org-export-odt-styles-file styles-file t)
1463 (make-directory pictures-dir
)
1465 ;; initialize list of files that contribute to the odt file
1466 (setq org-export-odt-file-list
1467 (append org-export-odt-save-list org-export-odt-nosave-list
)))
1470 (defconst org-odt-manifest-file-entry-tag
1471 "<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"/>")
1473 (defcustom org-export-odt-prettify-xml nil
1474 "Specify whether or not the xml output should be prettified.
1475 When this option is turned on, `indent-region' is run on all
1476 component xml buffers before they are saved. Turn this off for
1477 regular use. Turn this on if you need to examine the xml
1479 :group
'org-export-odt
1482 (defun org-odt-save-as-outfile (target opt-plist
)
1484 (org-odt-update-meta-file opt-plist
)
1486 ;; write styles file
1487 (org-odt-copy-styles-file)
1489 ;; Update styles.xml - take care of outline numbering
1490 (with-current-buffer
1491 (find-file-noselect (expand-file-name "styles.xml") t
)
1492 ;; Don't make automatic backup of styles.xml file. This setting
1493 ;; prevents the backedup styles.xml file from being zipped in to
1494 ;; odt file. This is more of a hackish fix. Better alternative
1495 ;; would be to fix the zip command so that the output odt file
1496 ;; includes only the needed files and excludes any auto-generated
1497 ;; extra files like backups and auto-saves etc etc. Note that
1498 ;; currently the zip command zips up the entire temp directory so
1499 ;; that any auto-generated files created under the hood ends up in
1500 ;; the resulting odt file.
1501 (set (make-local-variable 'backup-inhibited
) t
)
1503 ;; Import local setting of `org-export-with-section-numbers'
1504 (org-lparse-bind-local-variables opt-plist
)
1505 (org-odt-configure-outline-numbering
1506 (if org-export-with-section-numbers org-export-headline-levels
0)))
1508 ;; Write custom stlyes for source blocks
1509 (org-odt-insert-custom-styles-for-srcblocks
1512 (format " %s\n" (cddr style
)))
1513 hfy-user-sheet-assoc
""))
1515 (let ((zipdir default-directory
))
1516 (message "Switching to directory %s" (expand-file-name zipdir
))
1518 ;; save all xml files
1519 (mapc (lambda (file)
1520 (with-current-buffer
1521 (find-file-noselect (expand-file-name file
) t
)
1522 ;; prettify output if needed
1523 (when org-export-odt-prettify-xml
1524 (indent-region (point-min) (point-max)))
1526 org-export-odt-save-list
)
1528 (let* ((target-name (file-name-nondirectory target
))
1529 (target-dir (file-name-directory target
))
1530 (cmds `(("zip" "-mX0" ,target-name
"mimetype")
1531 ("zip" "-rmTq" ,target-name
"."))))
1532 (when (file-exists-p target
)
1533 ;; FIXME: If the file is locked this throws a cryptic error
1534 (delete-file target
))
1536 (let ((coding-system-for-write 'no-conversion
) exitcode
)
1537 (message "Creating odt file...")
1540 (message "Running %s" (mapconcat 'identity cmd
" "))
1542 (apply 'call-process
(car cmd
) nil nil nil
(cdr cmd
)))
1543 (or (zerop exitcode
)
1544 (error "Unable to create odt file (%S)" exitcode
)))
1547 ;; move the file from outdir to target-dir
1548 (rename-file target-name target-dir
)
1550 ;; kill all xml buffers
1551 (mapc (lambda (file)
1553 (find-file-noselect (expand-file-name file zipdir
) t
)))
1554 org-export-odt-save-list
)
1556 (delete-directory zipdir
)))
1558 (message "Created %s" target
)
1559 (set-buffer (find-file-noselect target t
)))
1561 (defun org-odt-format-date (date)
1563 "OpenDocument files require that dates be in ISO-8601 format. Please review your DATE options for compatibility."))
1564 ;; If the user is not careful with the date specification, an
1565 ;; invalid meta.xml will be emitted.
1567 ;; For now honor user's diktat and let him off with a warning
1568 ;; message. This is OK as LibreOffice (and possibly other
1569 ;; apps) doesn't deem this deviation as critical and continue
1570 ;; to load the file.
1572 ;; FIXME: Surely there a better way to handle this. Revisit this
1575 ((and date
(string-match "%" date
))
1576 ;; Honor user's diktat. See comments above
1577 (org-lparse-warn warning-msg
)
1578 (format-time-string date
))
1580 ;; Honor user's diktat. See comments above
1581 (org-lparse-warn warning-msg
)
1585 (format-time-string "%Y-%m-%dT%T%:z")))))
1587 (defun org-odt-update-meta-file (opt-plist)
1588 (with-current-buffer
1589 (find-file-noselect (expand-file-name "meta.xml") t
)
1590 (let ((date (org-odt-format-date (plist-get opt-plist
:date
)))
1591 (author (or (plist-get opt-plist
:author
) ""))
1592 (email (plist-get opt-plist
:email
))
1593 (keywords (plist-get opt-plist
:keywords
))
1594 (description (plist-get opt-plist
:description
))
1595 (title (plist-get opt-plist
:title
)))
1599 (org-odt-format-tags '("<dc:creator>" .
"</dc:creator>") author
)
1600 (org-odt-format-tags
1601 '("\n<meta:initial-creator>" .
"</meta:initial-creator>") author
)
1602 (org-odt-format-tags '("\n<dc:date>" .
"</dc:date>") date
)
1603 (org-odt-format-tags
1604 '("\n<meta:creation-date>" .
"</meta:creation-date>") date
)
1605 (org-odt-format-tags '("\n<meta:generator>" .
"</meta:generator>")
1606 (when org-export-creator-info
1607 (format "Org-%s/Emacs-%s"
1608 org-version emacs-version
)))
1609 (org-odt-format-tags '("\n<meta:keyword>" .
"</meta:keyword>") keywords
)
1610 (org-odt-format-tags '("\n<dc:subject>" .
"</dc:subject>") description
)
1611 (org-odt-format-tags '("\n<dc:title>" .
"</dc:title>") title
)
1614 (defun org-odt-update-manifest-file (media-type full-path
)
1615 (with-current-buffer
1616 (find-file-noselect (expand-file-name "META-INF/manifest.xml") t
)
1617 (insert (format org-odt-manifest-file-entry-tag media-type full-path
))))
1619 (defun org-odt-finalize-outfile ()
1620 (org-odt-delete-empty-paragraphs))
1622 (defun org-odt-delete-empty-paragraphs ()
1623 (goto-char (point-min))
1624 (let ((open "<text:p[^>]*>")
1625 (close "</text:p>"))
1626 (while (re-search-forward (format "%s[ \r\n\t]*%s" open close
) nil t
)
1627 (replace-match ""))))
1629 (defun org-odt-get (what &optional opt-plist
)
1632 (EXPORT-DIR (org-export-directory :html opt-plist
))
1633 (FILE-NAME-EXTENSION "odt")
1634 (EXPORT-BUFFER-NAME "*Org ODT Export*")
1635 (ENTITY-CONTROL org-odt-entity-control-callbacks-alist
)
1636 (ENTITY-FORMAT org-odt-entity-format-callbacks-alist
)
1637 (INIT-METHOD 'org-odt-init-outfile
)
1638 (FINAL-METHOD 'org-odt-finalize-outfile
)
1639 (SAVE-METHOD 'org-odt-save-as-outfile
)
1640 ;; (OTHER-BACKENDS) ; see note in `org-xhtml-get'
1641 ;; (CONVERT-METHOD) ; see note in `org-xhtml-get'
1643 (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps
)
1644 (INLINE-IMAGES 'maybe
)
1645 (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
1646 (PLAIN-TEXT-MAP '(("&" .
"&") ("<" .
"<") (">" .
">")))
1647 (TABLE-FIRST-COLUMN-AS-LABELS nil
)
1648 (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY
"," 'superscript
))
1649 (CODING-SYSTEM-FOR-WRITE 'utf-8
)
1650 (CODING-SYSTEM-FOR-SAVE 'utf-8
)
1651 (t (error "Unknown property: %s" what
))))
1653 (defun org-odt-parse-label (label)
1655 (if (not (string-match "\\`[a-zA-Z]+:\\(.+\\)" label
))
1658 (capitalize (substring label
0 (1- (match-beginning 1))))
1659 (substring label
(match-beginning 1))))))
1661 (defvar org-lparse-latex-fragment-fallback
) ; set by org-do-lparse
1662 (defun org-export-odt-preprocess (parameters)
1663 "Convert LaTeX fragments to images."
1664 (when (and org-current-export-file
1665 (plist-get parameters
:LaTeX-fragments
))
1667 (concat "ltxpng/" (file-name-sans-extension
1668 (file-name-nondirectory
1669 org-current-export-file
)))
1670 org-current-export-dir nil
"Creating LaTeX image %s"
1673 ((eq (plist-get parameters
:LaTeX-fragments
) 'verbatim
) 'verbatim
)
1674 ;; Investigate MathToWeb for converting TeX equations to MathML
1675 ;; See http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01755.html
1676 ((or (eq (plist-get parameters
:LaTeX-fragments
) 'mathjax
)
1677 (eq (plist-get parameters
:LaTeX-fragments
) t
))
1680 "Use of MathJax is incompatible with ODT exporter. "
1681 (format "Using %S instead." org-lparse-latex-fragment-fallback
)))
1682 org-lparse-latex-fragment-fallback
)
1683 ((eq (plist-get parameters
:LaTeX-fragments
) 'dvipng
) 'dvipng
)
1685 (goto-char (point-min))
1686 (let (label label-components category value pretty-label
)
1687 (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t
)
1688 (org-if-unprotected-at (match-beginning 1)
1689 (setq label
(match-string 1)
1690 label-components
(org-odt-parse-label label
)
1691 category
(car label-components
)
1692 value
(cdr label-components
)
1693 pretty-label
(if value
(concat category
" " value
) label
))
1695 (let ((org-lparse-encode-pending t
))
1696 (org-odt-format-tags
1697 '("<text:sequence-ref text:reference-format=\"category-and-value\" text:ref-name=\"%s\">"
1698 .
"</text:sequence-ref>") pretty-label label
)) t t
)))))
1700 (declare-function archive-zip-extract
"arc-mode.el" (archive name
))
1701 (defun org-odt-zip-extract-one (archive member
&optional target
)
1703 (let* ((target (or target default-directory
))
1704 (archive (expand-file-name archive
))
1705 (archive-zip-extract
1706 (list "unzip" "-qq" "-o" "-d" target
))
1707 exit-code command-output
)
1708 (setq command-output
1710 (setq exit-code
(archive-zip-extract archive member
))
1712 (unless (zerop exit-code
)
1713 (message command-output
)
1714 (error "Extraction failed"))))
1716 (defun org-odt-zip-extract (archive members
&optional target
)
1717 (when (atom members
) (setq members
(list members
)))
1718 (mapc (lambda (member)
1719 (org-odt-zip-extract-one archive member target
))
1722 (defun org-odt-copy-styles-file (&optional styles-file
)
1723 ;; Non-availability of styles.xml is not a critical error. For now
1724 ;; throw an error purely for aesthetic reasons.
1725 (setq styles-file
(or styles-file
1726 org-export-odt-styles-file
1727 (expand-file-name "styles/OrgOdtStyles.xml"
1729 (error "org-odt: Missing styles file?")))
1731 ((listp styles-file
)
1732 (let ((archive (nth 0 styles-file
))
1733 (members (nth 1 styles-file
)))
1734 (org-odt-zip-extract archive members
)
1737 (when (org-file-image-p member
)
1738 (let* ((image-type (file-name-extension member
))
1739 (media-type (format "image/%s" image-type
)))
1740 (org-odt-update-manifest-file media-type member
))))
1742 ((and (stringp styles-file
) (file-exists-p styles-file
))
1743 (let ((styles-file-type (file-name-extension styles-file
)))
1745 ((string= styles-file-type
"xml")
1746 (copy-file styles-file
"styles.xml" t
))
1747 ((member styles-file-type
'("odt" "ott"))
1748 (org-odt-zip-extract styles-file
"styles.xml")))))
1750 (error (format "Invalid specification of styles.xml file: %S"
1751 org-export-odt-styles-file
)))))
1753 (defvar org-export-odt-factory-settings
1754 "d4328fb9d1b6cb211d4320ff546829f26700dc5e"
1755 "SHA1 hash of OrgOdtStyles.xml.")
1757 (defun org-odt-configure-outline-numbering (level)
1758 "Outline numbering is retained only upto LEVEL.
1759 To disable outline numbering pass a LEVEL of 0."
1760 (if (not (string= org-export-odt-factory-settings
(sha1 (current-buffer))))
1762 "org-odt: Using custom styles file? Consider tweaking styles.xml for better output. To suppress this warning update `org-export-odt-factory-settings'")
1763 (goto-char (point-min))
1765 "<text:outline-level-style\\(.*\\)text:level=\"\\(.*\\)\"\\(.*\\)>")
1767 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1768 (while (re-search-forward regex nil t
)
1769 (when (> (string-to-number (match-string 1)) level
)
1770 (replace-match replacement t nil
))))
1775 ;;; org-odt.el ends here