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 (org-lparse-unregister-backend 'odt
)
147 (remove-hook 'org-export-preprocess-after-blockquote-hook
148 'org-export-odt-preprocess-latex-fragments
)
151 (defcustom org-export-odt-automatic-styles-file nil
152 "Automatic styles for use with ODT exporter.
153 If unspecified, the file under `org-odt-data-dir' is used."
155 :group
'org-export-odt
)
157 (defcustom org-export-odt-styles-file nil
158 "Default styles file for use with ODT export.
159 Valid values are one of:
161 2. path to a styles.xml file
162 3. path to a *.odt or a *.ott file
163 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
166 In case of option 1, an in-built styles.xml is used. See
167 `org-odt-data-dir' for more information.
169 In case of option 3, the specified file is unzipped and the
170 styles.xml embedded therein is used.
172 In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
173 and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
174 generated odt file. Use relative path for specifying the
175 FILE-MEMBERS. styles.xml must be specified as one of the
178 Use options 1, 2 or 3 only if styles.xml alone suffices for
179 achieving the desired formatting. Use option 4, if the styles.xml
180 references additional files like header and footer images for
181 achieving the desired formattting."
182 :group
'org-export-odt
185 (const :tag
"Factory settings" nil
)
186 (file :must-match t
:tag
"styles.xml")
187 (file :must-match t
:tag
"ODT or OTT file")
188 (list :tag
"ODT or OTT file + Members"
189 (file :must-match t
:tag
"ODF Text or Text Template file")
191 (file :tag
" Member" "styles.xml")
192 (repeat (file :tag
"Member"))))))
194 (defconst org-export-odt-tmpdir-prefix
"odt-")
195 (defconst org-export-odt-bookmark-prefix
"OrgXref.")
196 (defcustom org-export-odt-use-bookmarks-for-internal-links t
197 "Export Internal links as bookmarks?."
199 :group
'org-export-odt
)
201 (defcustom org-export-odt-embed-images t
202 "Should the images be copied in to the odt file or just linked?"
204 :group
'org-export-odt
)
206 (defcustom org-odt-export-inline-images
'maybe
207 "Non-nil means inline images into exported HTML pages.
208 This is done using an <img> tag. When nil, an anchor with href is used to
209 link to the image. If this option is `maybe', then images in links with
210 an empty description will be inlined, while images with a description will
212 :group
'org-odt-export
213 :type
'(choice (const :tag
"Never" nil
)
214 (const :tag
"Always" t
)
215 (const :tag
"When there is no description" maybe
)))
217 (defcustom org-odt-export-inline-image-extensions
218 '("png" "jpeg" "jpg" "gif")
219 "Extensions of image files that can be inlined into HTML."
220 :type
'(repeat (string :tag
"Extension"))
221 :group
'org-odt-export
)
223 (defcustom org-export-odt-pixels-per-inch display-pixels-per-inch
224 ;; FIXME add docstring
227 :group
'org-export-odt
)
229 (defvar org-export-odt-default-org-styles-alist
230 '((paragraph .
((default .
"Text_20_body")
231 (fixedwidth .
"OrgFixedWidthBlock")
233 (quote .
"Quotations")
234 (blockquote .
"Quotations")
235 (center .
"OrgCenter")
238 (title .
"Heading_20_1.title")
239 (footnote .
"Footnote")
240 (src .
"OrgSrcBlock")
241 (illustration .
"Illustration")
243 (definition-term .
"Text_20_body_20_bold")
244 (horizontal-line .
"Horizontal_20_Line")))
245 (character .
((bold .
"Bold")
246 (emphasis .
"Emphasis")
248 (verbatim .
"OrgCode")
249 (strike .
"Strikethrough")
250 (underline .
"Underline")
251 (subscript .
"OrgSubscript")
252 (superscript .
"OrgSuperscript")))
253 (list .
((ordered .
"OrgNumberedList")
254 (unordered .
"OrgBulletedList")
255 (description .
"OrgDescriptionList"))))
256 "Default styles for various entities.")
258 (defvar org-export-odt-org-styles-alist org-export-odt-default-org-styles-alist
)
259 (defun org-odt-get-style-name-for-entity (category &optional entity
)
260 (let ((entity (or entity
'default
)))
262 (cdr (assoc entity
(cdr (assoc category
263 org-export-odt-org-styles-alist
))))
264 (cdr (assoc entity
(cdr (assoc category
265 org-export-odt-default-org-styles-alist
))))
266 (error "Cannot determine style name for entity %s of type %s"
269 (defcustom org-export-odt-preferred-output-format nil
270 "Automatically post-process to this format after exporting to \"odt\".
271 Interactive commands `org-export-as-odt' and
272 `org-export-as-odt-and-open' export first to \"odt\" format and
273 then use an external converter to convert the resulting document
276 The converter used is that specified with CONVERT-METHOD option
277 in `org-odt-get'. If the above option is unspecified then
278 `org-lparse-convert-process' is used.
280 The format specified here should be listed in OTHER-BACKENDS
281 option of `org-odt-get' or `org-lparse-convert-capabilities' as
284 :type
'(choice :convert-widget
286 (apply 'widget-convert
(widget-type w
)
287 (eval (car (widget-get w
:args
)))))
288 `((const :tag
"None" nil
)
289 ,@(mapcar (lambda (c)
290 `(const :tag
,(car c
) ,(car c
)))
291 (org-lparse-get-other-backends "odt")))))
294 (defun org-export-as-odt-and-open (arg)
295 "Export the outline as ODT and immediately open it with a browser.
296 If there is an active region, export only the region.
297 The prefix ARG specifies how many levels of the outline should become
298 headlines. The default is 3. Lower levels will become bulleted lists."
301 (or org-export-odt-preferred-output-format
"odt") "odt" arg
))
304 (defun org-export-as-odt-batch ()
305 "Call the function `org-lparse-batch'.
306 This function can be used in batch processing as:
308 --load=$HOME/lib/emacs/org.el
309 --eval \"(setq org-export-headline-levels 2)\"
310 --visit=MyFile --funcall org-export-as-odt-batch"
311 (org-lparse-batch "odt"))
314 (defun org-export-as-odt-to-buffer (arg)
315 "Call `org-lparse-odt` with output to a temporary buffer.
316 No file is created. The prefix ARG is passed through to `org-lparse-to-buffer'."
318 (org-lparse-to-buffer "odt" arg
))
321 (defun org-replace-region-by-odt (beg end
)
322 "Assume the current region has org-mode syntax, and convert it to ODT.
323 This can be used in any buffer. For example, you could write an
324 itemized list in org-mode syntax in an ODT buffer and then use this
325 command to convert it."
327 (org-replace-region-by "odt" beg end
))
330 (defun org-export-region-as-odt (beg end
&optional body-only buffer
)
331 "Convert region from BEG to END in org-mode buffer to ODT.
332 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
333 contents, and only produce the region of converted text, useful for
334 cut-and-paste operations.
335 If BUFFER is a buffer or a string, use/create that buffer as a target
336 of the converted ODT. If BUFFER is the symbol `string', return the
337 produced ODT as a string and leave not buffer behind. For example,
338 a Lisp program could call this function in the following way:
340 (setq odt (org-export-region-as-odt beg end t 'string))
342 When called interactively, the output buffer is selected, and shown
343 in a window. A non-interactive call will only return the buffer."
345 (org-lparse-region "odt" beg end body-only buffer
))
347 ;;; org-export-as-odt
349 (defun org-export-as-odt (arg &optional hidden ext-plist
350 to-buffer body-only pub-dir
)
351 "Export the outline as a OpenDocumentText file.
352 If there is an active region, export only the region. The prefix
353 ARG specifies how many levels of the outline should become
354 headlines. The default is 3. Lower levels will become bulleted
355 lists. HIDDEN is obsolete and does nothing.
356 EXT-PLIST is a property list with external parameters overriding
357 org-mode's default settings, but still inferior to file-local
358 settings. When TO-BUFFER is non-nil, create a buffer with that
359 name and export to that buffer. If TO-BUFFER is the symbol
360 `string', don't leave any buffer behind but just return the
361 resulting XML as a string. When BODY-ONLY is set, don't produce
362 the file header and footer, simply return the content of
363 <body>...</body>, without even the body tags themselves. When
364 PUB-DIR is set, use this as the publishing directory."
366 (org-lparse (or org-export-odt-preferred-output-format
"odt")
367 "odt" arg hidden ext-plist to-buffer body-only pub-dir
))
369 (defvar org-odt-entity-control-callbacks-alist
371 .
(org-odt-begin-export org-odt-end-export
))
373 .
(org-odt-begin-document-content org-odt-end-document-content
))
375 .
(org-odt-begin-document-body org-odt-end-document-body
))
377 .
(org-odt-begin-toc org-odt-end-toc
))
379 .
(org-odt-begin-environment org-odt-end-environment
))
381 .
(org-odt-begin-footnote-definition org-odt-end-footnote-definition
))
383 .
(org-odt-begin-table org-odt-end-table
))
385 .
(org-odt-begin-table-rowgroup org-odt-end-table-rowgroup
))
387 .
(org-odt-begin-list org-odt-end-list
))
389 .
(org-odt-begin-list-item org-odt-end-list-item
))
391 .
(org-odt-begin-outline org-odt-end-outline
))
393 .
(org-odt-begin-outline-text org-odt-end-outline-text
))
395 .
(org-odt-begin-paragraph org-odt-end-paragraph
)))
398 (defvar org-odt-entity-format-callbacks-alist
399 `((EXTRA-TARGETS . org-lparse-format-extra-targets
)
400 (ORG-TAGS . org-lparse-format-org-tags
)
401 (SECTION-NUMBER . org-lparse-format-section-number
)
402 (HEADLINE . org-odt-format-headline
)
403 (TOC-ENTRY . org-odt-format-toc-entry
)
404 (TOC-ITEM . org-odt-format-toc-item
)
405 (TAGS . org-odt-format-tags
)
406 (SPACES . org-odt-format-spaces
)
407 (TABS . org-odt-format-tabs
)
408 (LINE-BREAK . org-odt-format-line-break
)
409 (FONTIFY . org-odt-format-fontify
)
410 (TODO . org-lparse-format-todo
)
411 (LINK . org-odt-format-link
)
412 (INLINE-IMAGE . org-odt-format-inline-image
)
413 (ORG-LINK . org-odt-format-org-link
)
414 (HEADING . org-odt-format-heading
)
415 (ANCHOR . org-odt-format-anchor
)
416 (TABLE . org-lparse-format-table
)
417 (TABLE-ROW . org-odt-format-table-row
)
418 (TABLE-CELL . org-odt-format-table-cell
)
419 (FOOTNOTES-SECTION . ignore
)
420 (FOOTNOTE-REFERENCE . org-odt-format-footnote-reference
)
421 (HORIZONTAL-LINE . org-odt-format-horizontal-line
)
422 (COMMENT . org-odt-format-comment
)
423 (LINE . org-odt-format-line
)
424 (ORG-ENTITY . org-odt-format-org-entity
))
428 ;;;_. control callbacks
430 (defun org-odt-begin-office-body ()
434 <text:sequence-decls>
435 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Illustration\"/>
436 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Table\"/>
437 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Text\"/>
438 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Drawing\"/>
439 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Equation\"/>
440 </text:sequence-decls>"))
442 ;; Following variable is let bound when `org-do-lparse' is in
443 ;; progress. See org-html.el.
444 (defvar org-lparse-toc
)
445 (defun org-odt-begin-document-body (opt-plist)
446 (org-odt-begin-office-body)
447 (let ((title (plist-get opt-plist
:title
)))
450 (org-odt-format-stylized-paragraph 'title title
))))
454 (insert "\n" org-lparse-toc
"\n")))
456 (defvar org-lparse-body-only
) ; let bound during org-do-lparse
457 (defvar org-lparse-to-buffer
) ; let bound during org-do-lparse
458 (defun org-odt-end-document-body (opt-plist)
459 (unless org-lparse-body-only
460 (org-lparse-insert-tag "</office:text>")
461 (org-lparse-insert-tag "</office:body>")))
463 (defconst org-odt-document-content-header
464 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
465 <office:document-content
466 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
467 xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\"
468 xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\"
469 xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\"
470 xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\"
471 xmlns:fo=\"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0\"
472 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
473 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
474 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
475 xmlns:number=\"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0\"
476 xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\"
477 xmlns:chart=\"urn:oasis:names:tc:opendocument:xmlns:chart:1.0\"
478 xmlns:dr3d=\"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0\"
479 xmlns:math=\"http://www.w3.org/1998/Math/MathML\"
480 xmlns:form=\"urn:oasis:names:tc:opendocument:xmlns:form:1.0\"
481 xmlns:script=\"urn:oasis:names:tc:opendocument:xmlns:script:1.0\"
482 xmlns:ooo=\"http://openoffice.org/2004/office\"
483 xmlns:ooow=\"http://openoffice.org/2004/writer\"
484 xmlns:oooc=\"http://openoffice.org/2004/calc\"
485 xmlns:dom=\"http://www.w3.org/2001/xml-events\"
486 xmlns:xforms=\"http://www.w3.org/2002/xforms\"
487 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
488 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
489 xmlns:rpt=\"http://openoffice.org/2005/report\"
490 xmlns:of=\"urn:oasis:names:tc:opendocument:xmlns:of:1.2\"
491 xmlns:xodt=\"http://www.w3.org/1999/xodt\"
492 xmlns:field=\"urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0\" office:version=\"1.2\">
495 (defun org-odt-begin-document-content (opt-plist)
497 (insert org-odt-document-content-header
)
500 (insert-file-contents
501 (or org-export-odt-automatic-styles-file
502 (expand-file-name "styles/OrgOdtAutomaticStyles.xml"
504 (goto-char (point-max)))
506 (defun org-odt-end-document-content ()
507 (org-lparse-insert-tag "</office:document-content>"))
509 (defun org-odt-begin-outline (level1 snumber title tags
510 target extra-targets class
)
512 'HEADING
(org-lparse-format
513 'HEADLINE title extra-targets tags snumber level1
)
516 (defun org-odt-end-outline ()
519 (defun org-odt-begin-outline-text (level1 snumber class
)
522 (defun org-odt-end-outline-text ()
525 (defun org-odt-begin-paragraph (&optional style
)
526 (org-lparse-insert-tag
527 "<text:p%s>" (org-odt-get-extra-attrs-for-paragraph-style style
)))
529 (defun org-odt-end-paragraph ()
530 (org-lparse-insert-tag "</text:p>"))
532 (defun org-odt-get-extra-attrs-for-paragraph-style (style)
536 ((stringp style
) style
)
537 ((symbolp style
) (org-odt-get-style-name-for-entity
540 (error "Don't know how to handle paragraph style %s" style
))
541 (format " text:style-name=\"%s\"" style-name
)))
543 (defun org-odt-format-stylized-paragraph (style text
)
545 '("<text:p%s>" .
"</text:p>") text
546 (org-odt-get-extra-attrs-for-paragraph-style style
)))
548 (defun org-odt-begin-environment (style)
550 ((blockquote verse center quote
)
551 (org-lparse-begin-paragraph style
)
554 (org-lparse-end-paragraph)
556 (t (error "Unknown environment %s" style
))))
558 (defun org-odt-end-environment (style)
560 ((blockquote verse center quote
)
561 (org-lparse-end-paragraph)
564 (org-lparse-begin-paragraph)
566 (t (error "Unknown environment %s" style
))))
568 (defvar org-lparse-list-level
) ; dynamically bound in org-do-lparse
569 (defun org-odt-begin-list (ltype)
570 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
572 (let* ((style-name (org-odt-get-style-name-for-entity 'list ltype
))
573 (extra (concat (when (= org-lparse-list-level
1)
574 " text:continue-numbering=\"false\"")
576 (format " text:style-name=\"%s\"" style-name
)))))
578 ((ordered unordered description
)
579 (org-lparse-end-paragraph)
580 (org-lparse-insert-tag "<text:list%s>" extra
))
581 (t (error "Unknown list type: %s" ltype
)))))
583 (defun org-odt-end-list (ltype)
584 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
587 (org-lparse-insert-tag "</text:list>")
588 (error "Unknown list type: %s" ltype
)))
590 (defun org-odt-begin-list-item (ltype &optional arg headline
)
591 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
595 (assert (not headline
) t
)
596 (let* ((counter arg
) (extra ""))
597 (org-lparse-insert-tag "<text:list-item>")
598 (org-lparse-begin-paragraph)))
600 (let* ((id arg
) (extra ""))
601 (org-lparse-insert-tag "<text:list-item>")
602 (org-lparse-begin-paragraph)
603 (insert (if headline
(org-odt-format-target headline id
)
604 (org-odt-format-bookmark "" id
)))))
606 (assert (not headline
) t
)
607 (let ((term (or arg
"(no term)")))
610 '("<text:list-item>" .
"</text:list-item>")
611 (org-odt-format-stylized-paragraph 'definition-term term
)))
612 (org-lparse-begin-list-item 'unordered
)
613 (org-lparse-begin-list 'description
)
614 (org-lparse-begin-list-item 'unordered
)))
615 (t (error "Unknown list type"))))
617 (defun org-odt-end-list-item (ltype)
618 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
622 (org-lparse-insert-tag "</text:list-item>"))
624 (org-lparse-end-list-item-1)
625 (org-lparse-end-list 'description
)
626 (org-lparse-end-list-item-1))
627 (t (error "Unknown list type"))))
629 ;; Following variables are let bound when table emission is in
630 ;; progress. See org-lparse.el.
631 (defvar org-lparse-table-begin-marker
)
632 (defvar org-lparse-table-ncols
)
633 (defvar org-lparse-table-rowgrp-open
)
634 (defvar org-lparse-table-rownum
)
635 (defvar org-lparse-table-cur-rowgrp-is-hdr
)
636 (defvar org-lparse-table-is-styled
)
637 (defvar org-lparse-table-rowgrp-info
)
638 (defvar org-lparse-table-colalign-vector
)
639 (defun org-odt-begin-table (caption label attributes
)
642 (org-odt-format-stylized-paragraph
643 'table
(org-odt-format-entity-caption label caption
"Table"))))
645 (org-lparse-insert-tag
646 "<table:table table:name=\"%s\" table:style-name=\"%s\">"
647 (or label
"") "OrgTable")
648 (setq org-lparse-table-begin-marker
(point)))
650 (defun org-odt-end-table ()
651 (goto-char org-lparse-table-begin-marker
)
652 (loop for level from
0 below org-lparse-table-ncols
655 "<table:table-column table:style-name=\"OrgTableColumn\"/>" "")))
657 ;; fill style attributes for table cells
658 (when org-lparse-table-is-styled
659 (while (re-search-forward "@@\\(table-cell:p\\|table-cell:style-name\\)@@\\([0-9]+\\)@@\\([0-9]+\\)@@" nil t
)
660 (let ((spec (match-string 1))
661 (r (string-to-number (match-string 2)))
662 (c (string-to-number (match-string 3))))
664 ((equal spec
"table-cell:p")
665 (let ((style-name (org-odt-get-paragraph-style-for-table-cell r c
)))
666 (replace-match style-name t t
)))
667 ((equal spec
"table-cell:style-name")
668 (let ((style-name (org-odt-get-style-name-for-table-cell r c
)))
669 (replace-match style-name t t
)))))))
671 (goto-char (point-max))
672 (org-lparse-insert-tag "</table:table>"))
674 (defun org-odt-begin-table-rowgroup (&optional is-header-row
)
675 (when org-lparse-table-rowgrp-open
676 (org-lparse-end 'TABLE-ROWGROUP
))
677 (org-lparse-insert-tag (if is-header-row
678 "<table:table-header-rows>"
679 "<table:table-rows>"))
680 (setq org-lparse-table-rowgrp-open t
)
681 (setq org-lparse-table-cur-rowgrp-is-hdr is-header-row
))
683 (defun org-odt-end-table-rowgroup ()
684 (when org-lparse-table-rowgrp-open
685 (setq org-lparse-table-rowgrp-open nil
)
686 (org-lparse-insert-tag
687 (if org-lparse-table-cur-rowgrp-is-hdr
688 "</table:table-header-rows>" "</table:table-rows>"))))
690 (defun org-odt-format-table-row (row)
692 '("<table:table-row>" .
"</table:table-row>") row
))
694 (defun org-odt-get-style-name-for-table-cell (r c
)
699 ((eq (cdr (assoc r org-lparse-table-rowgrp-info
)) :start
) "T")
701 (when (= r org-lparse-table-rownum
) "B")
704 ((or (memq (nth c org-table-colgroup-info
) '(:start
:startend
))
705 (memq (nth (1- c
) org-table-colgroup-info
) '(:end
:startend
))) "L")
708 (defun org-odt-get-paragraph-style-for-table-cell (r c
)
709 (capitalize (aref org-lparse-table-colalign-vector c
)))
711 (defun org-odt-get-paragraph-style-cookie-for-table-cell (r c
)
714 (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
715 ((and (= c
0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS
))
717 (t "OrgTableContents"))
718 (and org-lparse-table-is-styled
719 (format "@@table-cell:p@@%03d@@%03d@@" r c
))))
721 (defun org-odt-get-style-name-cookie-for-table-cell (r c
)
722 (when org-lparse-table-is-styled
723 (format "@@table-cell:style-name@@%03d@@%03d@@" r c
)))
725 (defun org-odt-format-table-cell (data r c
)
726 (let* ((paragraph-style-cookie
727 (org-odt-get-paragraph-style-cookie-for-table-cell r c
))
729 (org-odt-get-style-name-cookie-for-table-cell r c
))
730 (extra (if style-name-cookie
731 (format " table:style-name=\"%s\"" style-name-cookie
) "")))
733 '("<table:table-cell%s>" .
"</table:table-cell>")
734 (if org-lparse-list-table-p data
735 (org-odt-format-stylized-paragraph paragraph-style-cookie data
)) extra
)))
737 (defun org-odt-begin-footnote-definition (n)
738 (org-lparse-begin-paragraph 'footnote
))
740 (defun org-odt-end-footnote-definition (n)
741 (org-lparse-end-paragraph))
743 (defun org-odt-begin-toc (lang-specific-heading)
746 <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
747 <text:table-of-content-source text:outline-level=\"10\">
748 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
749 " lang-specific-heading
))
751 (loop for level from
1 upto
10
754 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
755 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
756 <text:index-entry-chapter/>
757 <text:index-entry-text/>
758 <text:index-entry-link-end/>
759 </text:table-of-content-entry-template>
764 </text:table-of-content-source>
767 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
768 <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
770 " lang-specific-heading
)))
772 (defun org-odt-end-toc ()
775 </text:table-of-content>
778 (defun org-odt-format-toc-entry (snumber todo headline tags href
)
779 (setq headline
(concat
780 (and org-export-with-section-numbers
781 (concat snumber
". "))
785 (org-lparse-format 'SPACES
3)
786 (org-lparse-format 'FONTIFY tags
"tag")))))
788 (setq headline
(org-lparse-format 'FONTIFY headline
"todo")))
790 (let ((org-odt-suppress-xref t
))
791 (org-odt-format-link headline
(concat "#" href
))))
793 (defun org-odt-format-toc-item (toc-entry level org-last-level
)
794 (let ((style (format "Contents_20_%d"
795 (+ level
(or (org-lparse-get 'TOPLEVEL-HLEVEL
) 1) -
1))))
796 (insert "\n" (org-odt-format-stylized-paragraph style toc-entry
) "\n")))
798 ;; Following variable is let bound during 'ORG-LINK callback. See
800 (defvar org-lparse-link-description-is-image nil
)
801 (defun org-odt-format-link (desc href
&optional attr
)
803 ((and (= (string-to-char href
) ?
#) (not org-odt-suppress-xref
))
804 (setq href
(concat org-export-odt-bookmark-prefix
(substring href
1)))
806 '("<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"%s\">" .
807 "</text:bookmark-ref>")
809 (org-lparse-link-description-is-image
811 '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" .
"</draw:a>")
812 desc href
(or attr
"")))
815 '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" .
"</text:a>")
816 desc href
(or attr
"")))))
818 (defun org-odt-format-spaces (n)
822 " " (org-odt-format-tags "<text:s text:c=\"%d\"/>" "" (1- n
))))
825 (defun org-odt-format-tabs (&optional n
)
826 (let ((tab "<text:tab/>")
830 (defun org-odt-format-line-break ()
831 (org-odt-format-tags "<text:line-break/>" ""))
833 (defun org-odt-format-horizontal-line ()
834 (org-odt-format-stylized-paragraph 'horizontal-line
""))
836 (defun org-odt-format-line (line)
837 (case org-lparse-dyn-current-environment
839 (org-odt-format-stylized-paragraph
840 'fixedwidth
(org-odt-fill-tabs-and-spaces
841 (org-xml-encode-plain-text line
))) "\n"))
842 (t (concat line
"\n"))))
844 (defun org-odt-format-comment (fmt &rest args
)
845 (let ((comment (apply 'format fmt args
)))
846 (format "\n<!-- %s -->\n" comment
)))
848 (defun org-odt-format-org-entity (wd)
849 (org-entity-get-representation wd
'utf8
))
851 (defun org-odt-fill-tabs-and-spaces (line)
852 (replace-regexp-in-string
853 "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
855 ((string= s
"\t") (org-odt-format-tabs))
856 (t (org-odt-format-spaces (length s
))))) line
))
858 (defcustom org-export-odt-use-htmlfontify t
859 "Specify whether or not source blocks need to be fontified.
860 Turn this option on if you want to colorize the source code
861 blocks in the exported file. For colorization to work, you need
862 to make available an enhanced version of `htmlfontify' library."
864 :group
'org-export-odt
)
866 (defun org-odt-format-source-code-or-example-plain
867 (lines lang caption textareap cols rows num cont rpllbl fmt
)
868 "Format source or example blocks much like fixedwidth blocks.
869 Use this when `org-export-odt-use-htmlfontify' option is turned
871 (setq lines
(org-export-number-lines (org-xml-encode-plain-text-lines lines
)
872 0 0 num cont rpllbl fmt
))
875 (org-odt-format-stylized-paragraph
876 'fixedwidth
(org-odt-fill-tabs-and-spaces line
)))
877 (org-split-string lines
"[\r\n]") "\n"))
879 (defvar org-src-block-paragraph-format
880 "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
881 <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
882 <style:background-image/>
883 </style:paragraph-properties>
884 <style:text-properties fo:color=\"%s\"/>
886 "Custom paragraph style for colorized source and example blocks.
887 This style is much the same as that of \"OrgFixedWidthBlock\"
888 except that the foreground and background colors are set
889 according to the default face identified by the `htmlfontify'.")
891 (defun org-odt-hfy-face-to-css (fn)
892 "Create custom style for face FN.
893 When FN is the default face, use it's foreground and background
894 properties to create \"OrgSrcBlock\" paragraph style. Otherwise
895 use it's color attribute to create a character style whose name
896 is obtained from FN. Currently all attributes of FN other than
899 The style name for a face FN is derived using the following
900 operations on the face name in that order - de-dash, CamelCase
901 and prefix with \"OrgSrc\". For example,
902 `font-lock-function-name-face' is associated with
903 \"OrgSrcFontLockFunctionNameFace\"."
904 (let* ((css-list (hfy-face-to-style fn
))
905 (style-name ((lambda (fn)
908 'capitalize
(split-string
909 (hfy-face-or-def-to-name fn
) "-")
911 (color-val (cdr (assoc "color" css-list
)))
912 (background-color-val (cdr (assoc "background" css-list
)))
913 (style (and org-export-odt-create-custom-styles-for-srcblocks
916 (format org-src-block-paragraph-format
917 background-color-val color-val
))
921 <style:style style:name=\"%s\" style:family=\"text\">
922 <style:text-properties fo:color=\"%s\"/>
923 </style:style>" style-name color-val
))))))
924 (cons style-name style
)))
926 (defcustom org-export-odt-create-custom-styles-for-srcblocks t
927 "Whether custom styles for colorized source blocks be automatically created.
928 When this option is turned on, the exporter creates custom styles
929 for source blocks based on the advice of `htmlfontify'. Creation
930 of custom styles happen as part of `org-odt-hfy-face-to-css'.
932 When this option is turned off exporter does not create such
935 Use the latter option if you do not want the custom styles to be
936 based on your current display settings. It is necessary that the
937 styles.xml already contains needed styles for colorizing to work.
939 This variable is effective only if
940 `org-export-odt-use-htmlfontify' is turned on."
941 :group
'org-export-odt
944 (defun org-odt-insert-custom-styles-for-srcblocks (styles)
945 "Save STYLES used for colorizing of source blocks.
946 Update styles.xml with styles that were collected as part of
947 `org-odt-hfy-face-to-css' callbacks."
950 (find-file-noselect (expand-file-name "styles.xml") t
)
951 (goto-char (point-min))
952 (when (re-search-forward "</office:styles>" nil t
)
953 (goto-char (match-beginning 0))
954 (insert "\n<!-- Org Htmlfontify Styles -->\n" styles
"\n")))))
956 (defun org-odt-format-source-code-or-example-colored
957 (lines lang caption textareap cols rows num cont rpllbl fmt
)
958 "Format source or example blocks using `htmlfontify-string'.
959 Use this routine when `org-export-odt-use-htmlfontify' option is
961 (let* ((lang-m (and lang
(or (cdr (assoc lang org-src-lang-modes
)) lang
)))
962 (mode (and lang-m
(intern (concat (if (symbolp lang-m
)
965 (org-inhibit-startup t
)
966 (org-startup-folded nil
)
967 (lines (with-temp-buffer
969 (if (functionp mode
) (funcall mode
) (fundamental-mode))
970 (font-lock-fontify-buffer)
972 (hfy-html-quote-regex "\\([<\"&> ]\\)")
973 (hfy-html-quote-map '(("\"" """)
978 (" " "<text:tab/>")))
979 (hfy-face-to-css 'org-odt-hfy-face-to-css
)
980 (hfy-optimisations-1 (copy-seq hfy-optimisations
))
981 (hfy-optimisations (add-to-list 'hfy-optimisations-1
983 (hfy-begin-span-handler
984 (lambda (style text-block text-id text-begins-block-p
)
985 (insert (format "<text:span text:style-name=\"%s\">" style
))))
986 (hfy-end-span-handler (lambda nil
(insert "</text:span>"))))
987 (when (fboundp 'htmlfontify-string
)
990 (org-odt-format-stylized-paragraph 'src
(htmlfontify-string line
)))
991 (org-split-string lines
"[\r\n]") "\n"))))
993 (defun org-odt-format-source-code-or-example (lines lang caption textareap
996 "Format source or example blocks for export.
997 Use `org-odt-format-source-code-or-example-plain' or
998 `org-odt-format-source-code-or-example-colored' depending on the
999 value of `org-export-odt-use-htmlfontify."
1001 (if (and org-export-odt-use-htmlfontify
1002 (or (featurep 'htmlfontify
) (require 'htmlfontify
))
1003 (fboundp 'htmlfontify-string
))
1004 'org-odt-format-source-code-or-example-colored
1005 'org-odt-format-source-code-or-example-plain
)
1006 lines lang caption textareap cols rows num cont rpllbl fmt
))
1008 (defun org-xml-encode-plain-text-lines (rtn)
1009 (mapconcat 'org-xml-encode-plain-text
(org-split-string rtn
"[\r\n]") "\n"))
1011 (defun org-odt-remap-stylenames (style-name)
1013 (cdr (assoc style-name
'(("timestamp-wrapper" .
"OrgTimestampWrapper")
1014 ("timestamp" .
"OrgTimestamp")
1015 ("timestamp-kwd" .
"OrgTimestampKeyword")
1017 ("todo" .
"OrgTodo")
1018 ("done" .
"OrgDone")
1019 ("target" .
"OrgTarget"))))
1022 (defun org-odt-format-fontify (text style
&optional id
)
1026 (org-odt-remap-stylenames style
))
1028 (org-odt-get-style-name-for-entity 'character style
))
1030 (assert (< 1 (length style
)))
1031 (let ((parent-style (pop style
)))
1032 (mapconcat (lambda (s)
1033 ;; (assert (stringp s) t)
1034 (org-odt-remap-stylenames s
)) style
"")
1035 (org-odt-remap-stylenames parent-style
)))
1036 (t (error "Don't how to handle style %s" style
)))))
1037 (org-odt-format-tags
1038 '("<text:span text:style-name=\"%s\">" .
"</text:span>")
1041 (defun org-odt-relocate-relative-path (path dir
)
1042 (if (file-name-absolute-p path
) path
1043 (file-relative-name (expand-file-name path dir
)
1044 (expand-file-name "eyecandy" dir
))))
1046 (defun org-odt-format-inline-image (thefile)
1047 (let* ((thelink (if (file-name-absolute-p thefile
) thefile
1048 (org-xml-format-href
1049 (org-odt-relocate-relative-path
1050 thefile org-current-export-file
))))
1052 (org-odt-format-tags
1053 "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
1054 (if org-export-odt-embed-images
1055 (org-odt-copy-image-file thefile
) thelink
))))
1056 (org-export-odt-format-image thefile href
)))
1058 (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
1061 OPT-PLIST is an options list.
1062 TYPE is the device-type of the link (THIS://foo.html)
1063 PATH is the path of the link (http://THIS#locationx)
1064 FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
1065 DESC is the link description, if any.
1066 ATTR is a string of other attributes of the a element.
1067 MAY-INLINE-P allows inlining it as an image."
1069 (declare (special org-lparse-par-open
))
1071 (let* ((may-inline-p
1072 (and (member type-1
'("http" "https" "file"))
1073 (org-lparse-should-inline-p path descp
)
1075 (type (if (equal type-1
"id") "file" type-1
))
1080 ;; check for inlined images
1081 ((and (member type
'("file"))
1084 filename org-odt-export-inline-image-extensions
)
1085 (or (eq t org-odt-export-inline-images
)
1086 (and org-odt-export-inline-images
(not descp
))))
1088 ;; (when (and (string= type "file") (file-name-absolute-p path))
1089 ;; (setq thefile (concat "file://" (expand-file-name path))))
1090 ;; (setq thefile (org-xml-format-href thefile))
1091 ;; (org-export-html-format-image thefile)
1092 (org-odt-format-inline-image thefile
))
1094 (when (string= type
"file")
1097 ((file-name-absolute-p path
)
1098 (concat "file://" (expand-file-name path
)))
1099 (t (org-odt-relocate-relative-path
1100 thefile org-current-export-file
)))))
1102 (when (and (member type
'("" "http" "https" "file" "coderef"))
1104 (setq thefile
(concat thefile
"#" fragment
)))
1106 (setq thefile
(org-xml-format-href thefile
))
1108 (when (not (member type
'("" "file" "coderef")))
1109 (setq thefile
(concat type
":" thefile
)))
1111 (let ((org-odt-suppress-xref (string= type
"coderef")))
1112 (org-odt-format-link
1113 (org-xml-format-desc desc
) thefile attr
)))))))
1115 (defun org-odt-format-heading (text level
&optional id
)
1116 (let* ((text (if id
(org-odt-format-target text id
) text
)))
1117 (org-odt-format-tags
1118 '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
1119 "</text:h>") text level level
)))
1121 (defun org-odt-format-headline (title extra-targets tags
1122 &optional snumber level
)
1124 (org-lparse-format 'EXTRA-TARGETS extra-targets
)
1126 ;; No need to generate section numbers. They are auto-generated by
1129 ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
1131 (and tags
(concat (org-lparse-format 'SPACES
3)
1132 (org-lparse-format 'ORG-TAGS tags
)))))
1134 (defun org-odt-format-anchor (text name
&optional class
)
1135 (org-odt-format-target text name
))
1137 (defun org-odt-format-bookmark (text id
)
1139 (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id
)
1142 (defun org-odt-format-target (text id
)
1143 (let ((name (concat org-export-odt-bookmark-prefix id
)))
1145 (and id
(org-odt-format-tags
1146 "<text:bookmark-start text:name=\"%s\"/>" "" name
))
1147 (org-odt-format-bookmark text id
)
1148 (and id
(org-odt-format-tags
1149 "<text:bookmark-end text:name=\"%s\"/>" "" name
)))))
1151 (defun org-odt-format-footnote (n def
)
1152 (let ((id (concat "fn" n
))
1153 (note-class "footnote")
1154 (par-style "Footnote"))
1155 (org-odt-format-tags
1156 '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
1159 (org-odt-format-tags
1160 '("<text:note-citation>" .
"</text:note-citation>")
1162 (org-odt-format-tags
1163 '("<text:note-body>" .
"</text:note-body>")
1167 (defun org-odt-format-footnote-reference (n def refcnt
)
1169 (org-odt-format-footnote n def
)
1170 (org-odt-format-footnote-ref n
)))
1172 (defun org-odt-format-footnote-ref (n)
1173 (let ((note-class "footnote")
1175 (ref-name (concat "fn" n
)))
1176 (org-odt-format-tags
1177 '("<text:span text:style-name=\"%s\">" .
"</text:span>")
1178 (org-odt-format-tags
1179 '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" .
"</text:note-ref>")
1180 n note-class ref-format ref-name
)
1183 (defun org-odt-get-image-name (file-name)
1187 (concat (sha1 file-name
) "." (file-name-extension file-name
)) "Pictures")))
1189 (defun org-export-odt-format-image (src href
)
1190 "Create image tag with source and attributes."
1192 (let* ((caption (org-find-text-property-in-string 'org-caption src
))
1193 (caption (and caption
(org-xml-format-desc caption
)))
1194 (attr (org-find-text-property-in-string 'org-attributes src
))
1195 (label (org-find-text-property-in-string 'org-label src
))
1196 (embed-as (if (string-match "^ltxpng/" src
) 'character
'paragraph
))
1197 (attr-plist (when attr
(read attr
)))
1198 (size (org-odt-image-size-from-file
1199 src
(plist-get attr-plist
:width
)
1200 (plist-get attr-plist
:height
)
1201 (plist-get attr-plist
:scale
) nil embed-as
)))
1202 (org-export-odt-do-format-image
1203 embed-as caption attr label size href
))))
1205 (defun org-odt-format-textbox (text style
)
1206 (let ((draw-frame-pair
1207 '("<draw:frame draw:style-name=\"%s\"
1208 text:anchor-type=\"paragraph\"
1209 style:rel-width=\"100%%\"
1210 draw:z-index=\"0\">" .
"</draw:frame>")))
1211 (org-odt-format-tags
1213 (org-odt-format-tags
1214 '("<draw:text-box fo:min-height=\"%dcm\">" .
"</draw:text-box>")
1217 (defun org-odt-format-inlinetask (heading content
1218 &optional todo priority tags
)
1219 (org-odt-format-stylized-paragraph
1220 nil
(org-odt-format-textbox
1221 (concat (org-odt-format-stylized-paragraph
1222 "OrgInlineTaskHeading"
1224 'HEADLINE
(concat (org-lparse-format-todo todo
) " " heading
)
1226 content
) "OrgInlineTaskFrame")))
1228 (defun org-export-odt-do-format-image (embed-as caption attr label
1230 "Create image tag with source and attributes."
1232 (let ((width (car size
)) (height (cdr size
))
1234 '("<draw:frame draw:style-name=\"%s\"
1235 text:anchor-type=\"%s\"
1236 draw:z-index=\"%d\" %s>" .
"</draw:frame>")))
1238 ((and (not caption
) (not label
))
1239 (let (style-name anchor-type
)
1241 ((eq embed-as
'paragraph
)
1242 (setq style-name
"OrgGraphicsParagraph" anchor-type
"paragraph"))
1243 ((eq embed-as
'character
)
1244 (setq style-name
"OrgGraphicsBaseline" anchor-type
"as-char")))
1245 (org-odt-format-tags
1246 draw-frame-pair href style-name anchor-type
0
1247 (org-odt-image-attrs-from-size width height
))))
1251 ;; (when par-open (org-odt-close-par))
1252 (org-odt-format-tags
1254 (org-odt-format-tags
1255 '("<draw:text-box fo:min-height=\"%dcm\">" .
"</draw:text-box>")
1256 (org-odt-format-stylized-paragraph
1259 (let ((extra " style:rel-width=\"100%\" style:rel-height=\"scale\""))
1260 (org-odt-format-tags
1261 draw-frame-pair href
"OrgGraphicsParagraphContent" "paragraph" 2
1262 (concat (org-odt-image-attrs-from-size width height
) extra
)))
1263 (org-odt-format-entity-caption label caption
)))
1265 "OrgFrame" "paragraph" 1
1266 (org-odt-image-attrs-from-size width
))
1268 ;; (when par-open (org-odt-open-par))
1271 ;; xml files generated on-the-fly
1272 (defconst org-export-odt-save-list
1273 '("mimetype" "META-INF/manifest.xml" "content.xml" "meta.xml" "styles.xml"))
1275 ;; xml files that are copied
1276 (defconst org-export-odt-nosave-list
'())
1278 ;; xml files that contribute to the final odt file
1279 (defvar org-export-odt-file-list nil
)
1281 (defconst org-export-odt-manifest-lines
1282 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1283 "<manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">"
1284 "<manifest:file-entry manifest:media-type=\"application/vnd.oasis.opendocument.text\" manifest:version=\"1.2\" manifest:full-path=\"/\"/>"
1285 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"content.xml\"/>"
1286 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"styles.xml\"/>"
1287 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"meta.xml\"/>"
1288 "<manifest:file-entry manifest:media-type=\"\" manifest:full-path=\"Pictures/\"/>") .
("</manifest:manifest>")))
1290 (defun org-odt-copy-image-file (path &optional target-file
)
1291 "Returns the internal name of the file"
1292 (let* ((image-type (file-name-extension path
))
1293 (media-type (format "image/%s" image-type
))
1294 (src-file (expand-file-name
1295 path
(file-name-directory org-current-export-file
)))
1296 (target-file (or target-file
(org-odt-get-image-name src-file
)))
1300 (when (not org-lparse-to-buffer
)
1301 (message "Embedding %s as %s ..."
1302 (substring-no-properties path
) target-file
)
1303 (copy-file src-file target-file
'overwrite
)
1304 (org-odt-update-manifest-file media-type target-file
)
1305 (push target-file org-export-odt-file-list
)) target-file
))
1307 (defun org-odt-image-attrs-from-size (&optional width height
)
1309 (when width
(format "svg:width=\"%0.2fcm\"" width
))
1311 (when height
(format "svg:height=\"%0.2fcm\"" height
))))
1313 (defvar org-export-odt-image-size-probe-method
1314 '(emacs imagemagick force
)
1315 "Ordered list of methods by for determining size of an embedded
1318 (defvar org-export-odt-default-image-sizes-alist
1319 '(("character" .
(5 .
0.4))
1320 ("paragraph" .
(5 .
5)))
1321 "Hardcoded image dimensions one for each of the anchor
1324 (defun org-odt-do-image-size (probe-method file
&optional dpi anchor-type
)
1325 (setq dpi
(or dpi org-export-odt-pixels-per-inch
))
1326 (setq anchor-type
(or anchor-type
"paragraph"))
1327 (flet ((size-in-cms (size-in-pixels)
1328 (flet ((pixels-to-cms (pixels)
1329 (let* ((cms-per-inch 2.54)
1330 (inches (/ pixels dpi
)))
1331 (* cms-per-inch inches
))))
1333 (cons (pixels-to-cms (car size-in-pixels
))
1334 (pixels-to-cms (cdr size-in-pixels
)))))))
1337 (size-in-cms (ignore-errors (image-size (create-image file
) 'pixels
))))
1340 (let ((dim (shell-command-to-string
1341 (format "identify -format \"%%w:%%h\" \"%s\"" file
))))
1342 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim
)
1343 (cons (string-to-number (match-string 1 dim
))
1344 (string-to-number (match-string 2 dim
)))))))
1346 (cdr (assoc-string anchor-type
1347 org-export-odt-default-image-sizes-alist
))))))
1349 (defun org-odt-image-size-from-file (file &optional user-width
1350 user-height scale dpi embed-as
)
1351 (unless (file-name-absolute-p file
)
1352 (setq file
(expand-file-name
1353 file
(file-name-directory org-current-export-file
))))
1354 (let* (size width height
)
1355 (unless (and user-height user-width
)
1356 (loop for probe-method in org-export-odt-image-size-probe-method
1358 do
(setq size
(org-odt-do-image-size
1359 probe-method file dpi embed-as
)))
1360 (or size
(error "Cannot determine Image size. Aborting ..."))
1361 (setq width
(car size
) height
(cdr size
)))
1364 (setq width
(* width scale
) height
(* height scale
)))
1365 ((and user-height user-width
)
1366 (setq width user-width height user-height
))
1368 (setq width
(* user-height
(/ width height
)) height user-height
))
1370 (setq height
(* user-width
(/ height width
)) width user-width
))
1372 (cons width height
)))
1374 (defvar org-odt-default-entity
"Illustration")
1375 (defun org-odt-format-entity-caption (label caption
&optional default-entity
)
1376 (if (not label
) (or caption
"")
1377 (let* ((label-components (org-odt-parse-label label
))
1378 (entity (car label-components
))
1379 (seqno (cdr label-components
))
1380 (caption (and caption
(concat ": " caption
))))
1383 entity
(or default-entity org-odt-default-entity
)))
1386 (org-odt-format-tags
1387 '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" .
"</text:sequence>")
1388 seqno label entity entity
)
1391 (defun org-odt-format-tags (tag text
&rest args
)
1392 (let ((prefix (when org-lparse-encode-pending
"@"))
1393 (suffix (when org-lparse-encode-pending
"@")))
1394 (apply 'org-lparse-format-tags tag text prefix suffix args
)))
1396 (defun org-odt-init-outfile (filename)
1397 (unless (executable-find "zip")
1398 ;; Not at all OSes ship with zip by default
1399 (error "Executable \"zip\" needed for creating OpenDocument files. Aborting."))
1401 (let* ((outdir (make-temp-file org-export-odt-tmpdir-prefix t
))
1402 (mimetype-file (expand-file-name "mimetype" outdir
))
1403 (content-file (expand-file-name "content.xml" outdir
))
1404 (manifest-file (expand-file-name "META-INF/manifest.xml" outdir
))
1405 (meta-file (expand-file-name "meta.xml" outdir
))
1406 (styles-file (expand-file-name "styles.xml" outdir
))
1407 (pictures-dir (expand-file-name "Pictures" outdir
))
1411 (with-current-buffer (find-file-noselect content-file t
)
1414 ;; FIXME: How to factor in body-only here
1417 (make-directory (file-name-directory manifest-file
))
1418 (with-current-buffer (find-file-noselect manifest-file t
)
1420 (insert (mapconcat 'identity
(car org-export-odt-manifest-lines
) "\n"))
1423 (insert (mapconcat 'identity
(cdr org-export-odt-manifest-lines
) "\n"))))
1426 ;; (copy-file org-export-odt-styles-file styles-file t)
1429 (make-directory pictures-dir
)
1431 ;; initialize list of files that contribute to the odt file
1432 (setq org-export-odt-file-list
1433 (append org-export-odt-save-list org-export-odt-nosave-list
)))
1436 (defconst org-odt-manifest-file-entry-tag
1437 "<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"/>")
1439 (defcustom org-export-odt-prettify-xml nil
1440 "Specify whether or not the xml output should be prettified.
1441 When this option is turned on, `indent-region' is run on all
1442 component xml buffers before they are saved. Turn this off for
1443 regular use. Turn this on if you need to examine the xml
1445 :group
'org-export-odt
1448 (defvar hfy-user-sheet-assoc
) ; bound during org-do-lparse
1449 (defun org-odt-save-as-outfile (target opt-plist
)
1450 ;; create mimetype file
1451 (write-region "application/vnd.oasis.opendocument.text" nil
1452 (expand-file-name "mimetype"))
1455 (org-odt-update-meta-file opt-plist
)
1457 ;; write styles file
1458 (org-odt-copy-styles-file)
1460 ;; Update styles.xml - take care of outline numbering
1461 (with-current-buffer
1462 (find-file-noselect (expand-file-name "styles.xml") t
)
1463 ;; Don't make automatic backup of styles.xml file. This setting
1464 ;; prevents the backedup styles.xml file from being zipped in to
1465 ;; odt file. This is more of a hackish fix. Better alternative
1466 ;; would be to fix the zip command so that the output odt file
1467 ;; includes only the needed files and excludes any auto-generated
1468 ;; extra files like backups and auto-saves etc etc. Note that
1469 ;; currently the zip command zips up the entire temp directory so
1470 ;; that any auto-generated files created under the hood ends up in
1471 ;; the resulting odt file.
1472 (set (make-local-variable 'backup-inhibited
) t
)
1474 ;; Import local setting of `org-export-with-section-numbers'
1475 (org-lparse-bind-local-variables opt-plist
)
1476 (org-odt-configure-outline-numbering
1477 (if org-export-with-section-numbers org-export-headline-levels
0)))
1479 ;; Write custom stlyes for source blocks
1480 (org-odt-insert-custom-styles-for-srcblocks
1483 (format " %s\n" (cddr style
)))
1484 hfy-user-sheet-assoc
""))
1486 (let ((zipdir default-directory
))
1487 (message "Switching to directory %s" (expand-file-name zipdir
))
1489 ;; save all xml files
1490 (mapc (lambda (file)
1491 (with-current-buffer
1492 (find-file-noselect (expand-file-name file
) t
)
1493 ;; prettify output if needed
1494 (when org-export-odt-prettify-xml
1495 (indent-region (point-min) (point-max)))
1497 org-export-odt-save-list
)
1499 (let* ((target-name (file-name-nondirectory target
))
1500 (target-dir (file-name-directory target
))
1501 (cmds `(("zip" "-mX0" ,target-name
"mimetype")
1502 ("zip" "-rmTq" ,target-name
"."))))
1503 (when (file-exists-p target
)
1504 ;; FIXME: If the file is locked this throws a cryptic error
1505 (delete-file target
))
1507 (let ((coding-system-for-write 'no-conversion
) exitcode
)
1508 (message "Creating odt file...")
1511 (message "Running %s" (mapconcat 'identity cmd
" "))
1513 (apply 'call-process
(car cmd
) nil nil nil
(cdr cmd
)))
1514 (or (zerop exitcode
)
1515 (error "Unable to create odt file (%S)" exitcode
)))
1518 ;; move the file from outdir to target-dir
1519 (rename-file target-name target-dir
)
1521 ;; kill all xml buffers
1522 (mapc (lambda (file)
1524 (find-file-noselect (expand-file-name file zipdir
) t
)))
1525 org-export-odt-save-list
)
1527 (delete-directory zipdir
)))
1529 (message "Created %s" target
)
1530 (set-buffer (find-file-noselect target t
)))
1532 (defun org-odt-format-date (date)
1534 "OpenDocument files require that dates be in ISO-8601 format. Please review your DATE options for compatibility."))
1535 ;; If the user is not careful with the date specification, an
1536 ;; invalid meta.xml will be emitted.
1538 ;; For now honor user's diktat and let him off with a warning
1539 ;; message. This is OK as LibreOffice (and possibly other
1540 ;; apps) doesn't deem this deviation as critical and continue
1541 ;; to load the file.
1543 ;; FIXME: Surely there a better way to handle this. Revisit this
1546 ((and date
(string-match "%" date
))
1547 ;; Honor user's diktat. See comments above
1548 (org-lparse-warn warning-msg
)
1549 (format-time-string date
))
1551 ;; Honor user's diktat. See comments above
1552 (org-lparse-warn warning-msg
)
1556 (format-time-string "%Y-%m-%dT%T%:z")))))
1558 (defun org-odt-update-meta-file (opt-plist)
1559 (let ((date (org-odt-format-date (plist-get opt-plist
:date
)))
1560 (author (or (plist-get opt-plist
:author
) ""))
1561 (email (plist-get opt-plist
:email
))
1562 (keywords (plist-get opt-plist
:keywords
))
1563 (description (plist-get opt-plist
:description
))
1564 (title (plist-get opt-plist
:title
)))
1568 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
1569 <office:document-meta
1570 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
1571 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
1572 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
1573 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
1574 xmlns:ooo=\"http://openoffice.org/2004/office\"
1575 office:version=\"1.2\">
1577 (org-odt-format-tags '("<dc:creator>" .
"</dc:creator>") author
)
1578 (org-odt-format-tags
1579 '("\n<meta:initial-creator>" .
"</meta:initial-creator>") author
)
1580 (org-odt-format-tags '("\n<dc:date>" .
"</dc:date>") date
)
1581 (org-odt-format-tags
1582 '("\n<meta:creation-date>" .
"</meta:creation-date>") date
)
1583 (org-odt-format-tags '("\n<meta:generator>" .
"</meta:generator>")
1584 (when org-export-creator-info
1585 (format "Org-%s/Emacs-%s"
1586 org-version emacs-version
)))
1587 (org-odt-format-tags '("\n<meta:keyword>" .
"</meta:keyword>") keywords
)
1588 (org-odt-format-tags '("\n<dc:subject>" .
"</dc:subject>") description
)
1589 (org-odt-format-tags '("\n<dc:title>" .
"</dc:title>") title
)
1591 " </office:meta>" "</office:document-meta>")
1592 nil
(expand-file-name "meta.xml"))))
1594 (defun org-odt-update-manifest-file (media-type full-path
)
1595 (with-current-buffer
1596 (find-file-noselect (expand-file-name "META-INF/manifest.xml") t
)
1597 (insert (format org-odt-manifest-file-entry-tag media-type full-path
))))
1599 (defun org-odt-finalize-outfile ()
1600 (org-odt-delete-empty-paragraphs))
1602 (defun org-odt-delete-empty-paragraphs ()
1603 (goto-char (point-min))
1604 (let ((open "<text:p[^>]*>")
1605 (close "</text:p>"))
1606 (while (re-search-forward (format "%s[ \r\n\t]*%s" open close
) nil t
)
1607 (replace-match ""))))
1609 (defun org-odt-get (what &optional opt-plist
)
1612 (EXPORT-DIR (org-export-directory :html opt-plist
))
1613 (FILE-NAME-EXTENSION "odt")
1614 (EXPORT-BUFFER-NAME "*Org ODT Export*")
1615 (ENTITY-CONTROL org-odt-entity-control-callbacks-alist
)
1616 (ENTITY-FORMAT org-odt-entity-format-callbacks-alist
)
1617 (INIT-METHOD 'org-odt-init-outfile
)
1618 (FINAL-METHOD 'org-odt-finalize-outfile
)
1619 (SAVE-METHOD 'org-odt-save-as-outfile
)
1620 ;; (OTHER-BACKENDS) ; see note in `org-xhtml-get'
1621 ;; (CONVERT-METHOD) ; see note in `org-xhtml-get'
1623 (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps
)
1624 (INLINE-IMAGES 'maybe
)
1625 (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
1626 (PLAIN-TEXT-MAP '(("&" .
"&") ("<" .
"<") (">" .
">")))
1627 (TABLE-FIRST-COLUMN-AS-LABELS nil
)
1628 (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY
"," 'superscript
))
1629 (CODING-SYSTEM-FOR-WRITE 'utf-8
)
1630 (CODING-SYSTEM-FOR-SAVE 'utf-8
)
1631 (t (error "Unknown property: %s" what
))))
1633 (defun org-odt-parse-label (label)
1635 (if (not (string-match "\\`[a-zA-Z]+:\\(.+\\)" label
))
1638 (capitalize (substring label
0 (1- (match-beginning 1))))
1639 (substring label
(match-beginning 1))))))
1641 (defvar org-lparse-latex-fragment-fallback
) ; set by org-do-lparse
1642 (defvar org-lparse-opt-plist
) ; bound during org-do-lparse
1643 (defun org-export-odt-do-preprocess-latex-fragments ()
1644 "Convert LaTeX fragments to images."
1645 (let* ((latex-frag-opt (plist-get org-lparse-opt-plist
:LaTeX-fragments
))
1646 (latex-frag-opt-1 ; massage the options
1647 (or (and (member latex-frag-opt
'(mathjax t
))
1648 (prog1 org-lparse-latex-fragment-fallback
1651 "Use of MathJax is incompatible with ODT exporter. "
1652 (format "Using %S instead."
1653 org-lparse-latex-fragment-fallback
)))))
1655 (when (and org-current-export-file latex-frag-opt-1
)
1656 ;; Investigate MathToWeb for converting TeX equations to MathML
1657 ;; http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01755.html
1659 (concat "ltxpng/" (file-name-sans-extension
1660 (file-name-nondirectory
1661 org-current-export-file
)))
1662 org-current-export-dir nil
"Creating LaTeX image %s"
1663 nil nil latex-frag-opt-1
))))
1665 (defun org-export-odt-preprocess-latex-fragments ()
1666 (when (equal org-export-current-backend
'odt
)
1667 (org-export-odt-do-preprocess-latex-fragments)))
1669 (defun org-export-odt-preprocess-label-references ()
1670 (goto-char (point-min))
1671 (let (label label-components category value pretty-label
)
1672 (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t
)
1673 (org-if-unprotected-at (match-beginning 1)
1674 (setq label
(match-string 1)
1675 label-components
(org-odt-parse-label label
)
1676 category
(car label-components
)
1677 value
(cdr label-components
)
1678 pretty-label
(if value
(concat category
" " value
) label
))
1680 (let ((org-lparse-encode-pending t
))
1681 (org-odt-format-tags
1682 '("<text:sequence-ref text:reference-format=\"category-and-value\" text:ref-name=\"%s\">"
1683 .
"</text:sequence-ref>") pretty-label label
)) t t
)))))
1685 ;; process latex fragments as part of
1686 ;; `org-export-preprocess-after-blockquote-hook'. Note that this hook
1687 ;; is the one that is closest and well before the call to
1688 ;; `org-export-attach-captions-and-attributes' in
1689 ;; `org-export-preprocess-stirng'. The above arrangement permits
1690 ;; captions, labels and attributes to be attached to png images
1691 ;; generated out of latex equations.
1692 (add-hook 'org-export-preprocess-after-blockquote-hook
1693 'org-export-odt-preprocess-latex-fragments
)
1695 (defun org-export-odt-preprocess (parameters)
1696 (org-export-odt-preprocess-label-references))
1698 (declare-function archive-zip-extract
"arc-mode.el" (archive name
))
1699 (defun org-odt-zip-extract-one (archive member
&optional target
)
1701 (let* ((target (or target default-directory
))
1702 (archive (expand-file-name archive
))
1703 (archive-zip-extract
1704 (list "unzip" "-qq" "-o" "-d" target
))
1705 exit-code command-output
)
1706 (setq command-output
1708 (setq exit-code
(archive-zip-extract archive member
))
1710 (unless (zerop exit-code
)
1711 (message command-output
)
1712 (error "Extraction failed"))))
1714 (defun org-odt-zip-extract (archive members
&optional target
)
1715 (when (atom members
) (setq members
(list members
)))
1716 (mapc (lambda (member)
1717 (org-odt-zip-extract-one archive member target
))
1720 (defun org-odt-copy-styles-file (&optional styles-file
)
1721 ;; Non-availability of styles.xml is not a critical error. For now
1722 ;; throw an error purely for aesthetic reasons.
1723 (setq styles-file
(or styles-file
1724 org-export-odt-styles-file
1725 (expand-file-name "styles/OrgOdtStyles.xml"
1727 (error "org-odt: Missing styles file?")))
1729 ((listp styles-file
)
1730 (let ((archive (nth 0 styles-file
))
1731 (members (nth 1 styles-file
)))
1732 (org-odt-zip-extract archive members
)
1735 (when (org-file-image-p member
)
1736 (let* ((image-type (file-name-extension member
))
1737 (media-type (format "image/%s" image-type
)))
1738 (org-odt-update-manifest-file media-type member
))))
1740 ((and (stringp styles-file
) (file-exists-p styles-file
))
1741 (let ((styles-file-type (file-name-extension styles-file
)))
1743 ((string= styles-file-type
"xml")
1744 (copy-file styles-file
"styles.xml" t
))
1745 ((member styles-file-type
'("odt" "ott"))
1746 (org-odt-zip-extract styles-file
"styles.xml")))))
1748 (error (format "Invalid specification of styles.xml file: %S"
1749 org-export-odt-styles-file
)))))
1751 (defvar org-export-odt-factory-settings
1752 "d4328fb9d1b6cb211d4320ff546829f26700dc5e"
1753 "SHA1 hash of OrgOdtStyles.xml.")
1755 (defun org-odt-configure-outline-numbering (level)
1756 "Outline numbering is retained only upto LEVEL.
1757 To disable outline numbering pass a LEVEL of 0."
1758 (goto-char (point-min))
1760 "<text:outline-level-style\\(.*\\)text:level=\"\\([^\"]*\\)\"\\(.*\\)>")
1762 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1763 (while (re-search-forward regex nil t
)
1764 (when (> (string-to-number (match-string 2)) level
)
1765 (replace-match replacement t nil
))))
1770 ;;; org-odt.el ends here