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 (org-odt-format-stylized-paragraph paragraph-style-cookie data
) extra
)))
736 (defun org-odt-begin-footnote-definition (n)
737 (org-lparse-begin-paragraph 'footnote
))
739 (defun org-odt-end-footnote-definition (n)
740 (org-lparse-end-paragraph))
742 (defun org-odt-begin-toc (lang-specific-heading)
745 <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
746 <text:table-of-content-source text:outline-level=\"10\">
747 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
748 " lang-specific-heading
))
750 (loop for level from
1 upto
10
753 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
754 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
755 <text:index-entry-chapter/>
756 <text:index-entry-text/>
757 <text:index-entry-link-end/>
758 </text:table-of-content-entry-template>
763 </text:table-of-content-source>
766 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
767 <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
769 " lang-specific-heading
)))
771 (defun org-odt-end-toc ()
774 </text:table-of-content>
777 (defun org-odt-format-toc-entry (snumber todo headline tags href
)
778 (setq headline
(concat
779 (and org-export-with-section-numbers
780 (concat snumber
". "))
784 (org-lparse-format 'SPACES
3)
785 (org-lparse-format 'FONTIFY tags
"tag")))))
787 (setq headline
(org-lparse-format 'FONTIFY headline
"todo")))
789 (let ((org-odt-suppress-xref t
))
790 (org-odt-format-link headline
(concat "#" href
))))
792 (defun org-odt-format-toc-item (toc-entry level org-last-level
)
793 (let ((style (format "Contents_20_%d"
794 (+ level
(or (org-lparse-get 'TOPLEVEL-HLEVEL
) 1) -
1))))
795 (insert "\n" (org-odt-format-stylized-paragraph style toc-entry
) "\n")))
797 ;; Following variable is let bound during 'ORG-LINK callback. See
799 (defvar org-lparse-link-description-is-image nil
)
800 (defun org-odt-format-link (desc href
&optional attr
)
802 ((and (= (string-to-char href
) ?
#) (not org-odt-suppress-xref
))
803 (setq href
(concat org-export-odt-bookmark-prefix
(substring href
1)))
805 '("<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"%s\">" .
806 "</text:bookmark-ref>")
808 (org-lparse-link-description-is-image
810 '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" .
"</draw:a>")
811 desc href
(or attr
"")))
814 '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" .
"</text:a>")
815 desc href
(or attr
"")))))
817 (defun org-odt-format-spaces (n)
821 " " (org-odt-format-tags "<text:s text:c=\"%d\"/>" "" (1- n
))))
824 (defun org-odt-format-tabs (&optional n
)
825 (let ((tab "<text:tab/>")
829 (defun org-odt-format-line-break ()
830 (org-odt-format-tags "<text:line-break/>" ""))
832 (defun org-odt-format-horizontal-line ()
833 (org-odt-format-stylized-paragraph 'horizontal-line
""))
835 (defun org-odt-format-line (line)
836 (case org-lparse-dyn-current-environment
838 (org-odt-format-stylized-paragraph
839 'fixedwidth
(org-odt-fill-tabs-and-spaces
840 (org-xml-encode-plain-text line
))) "\n"))
841 (t (concat line
"\n"))))
843 (defun org-odt-format-comment (fmt &rest args
)
844 (let ((comment (apply 'format fmt args
)))
845 (format "\n<!-- %s -->\n" comment
)))
847 (defun org-odt-format-org-entity (wd)
848 (org-entity-get-representation wd
'utf8
))
850 (defun org-odt-fill-tabs-and-spaces (line)
851 (replace-regexp-in-string
852 "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
854 ((string= s
"\t") (org-odt-format-tabs))
855 (t (org-odt-format-spaces (length s
))))) line
))
857 (defcustom org-export-odt-use-htmlfontify t
858 "Specify whether or not source blocks need to be fontified.
859 Turn this option on if you want to colorize the source code
860 blocks in the exported file. For colorization to work, you need
861 to make available an enhanced version of `htmlfontify' library."
863 :group
'org-export-odt
)
865 (defun org-odt-format-source-code-or-example-plain
866 (lines lang caption textareap cols rows num cont rpllbl fmt
)
867 "Format source or example blocks much like fixedwidth blocks.
868 Use this when `org-export-odt-use-htmlfontify' option is turned
870 (setq lines
(org-export-number-lines (org-xml-encode-plain-text-lines lines
)
871 0 0 num cont rpllbl fmt
))
874 (org-odt-format-stylized-paragraph
875 'fixedwidth
(org-odt-fill-tabs-and-spaces line
)))
876 (org-split-string lines
"[\r\n]") "\n"))
878 (defvar org-src-block-paragraph-format
879 "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
880 <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
881 <style:background-image/>
882 </style:paragraph-properties>
883 <style:text-properties fo:color=\"%s\"/>
885 "Custom paragraph style for colorized source and example blocks.
886 This style is much the same as that of \"OrgFixedWidthBlock\"
887 except that the foreground and background colors are set
888 according to the default face identified by the `htmlfontify'.")
890 (defun org-odt-hfy-face-to-css (fn)
891 "Create custom style for face FN.
892 When FN is the default face, use it's foreground and background
893 properties to create \"OrgSrcBlock\" paragraph style. Otherwise
894 use it's color attribute to create a character style whose name
895 is obtained from FN. Currently all attributes of FN other than
898 The style name for a face FN is derived using the following
899 operations on the face name in that order - de-dash, CamelCase
900 and prefix with \"OrgSrc\". For example,
901 `font-lock-function-name-face' is associated with
902 \"OrgSrcFontLockFunctionNameFace\"."
903 (let* ((css-list (hfy-face-to-style fn
))
904 (style-name ((lambda (fn)
907 'capitalize
(split-string
908 (hfy-face-or-def-to-name fn
) "-")
910 (color-val (cdr (assoc "color" css-list
)))
911 (background-color-val (cdr (assoc "background" css-list
)))
912 (style (and org-export-odt-create-custom-styles-for-srcblocks
915 (format org-src-block-paragraph-format
916 background-color-val color-val
))
920 <style:style style:name=\"%s\" style:family=\"text\">
921 <style:text-properties fo:color=\"%s\"/>
922 </style:style>" style-name color-val
))))))
923 (cons style-name style
)))
925 (defcustom org-export-odt-create-custom-styles-for-srcblocks t
926 "Whether custom styles for colorized source blocks be automatically created.
927 When this option is turned on, the exporter creates custom styles
928 for source blocks based on the advice of `htmlfontify'. Creation
929 of custom styles happen as part of `org-odt-hfy-face-to-css'.
931 When this option is turned off exporter does not create such
934 Use the latter option if you do not want the custom styles to be
935 based on your current display settings. It is necessary that the
936 styles.xml already contains needed styles for colorizing to work.
938 This variable is effective only if
939 `org-export-odt-use-htmlfontify' is turned on."
940 :group
'org-export-odt
943 (defun org-odt-insert-custom-styles-for-srcblocks (styles)
944 "Save STYLES used for colorizing of source blocks.
945 Update styles.xml with styles that were collected as part of
946 `org-odt-hfy-face-to-css' callbacks."
949 (find-file-noselect (expand-file-name "styles.xml") t
)
950 (goto-char (point-min))
951 (when (re-search-forward "</office:styles>" nil t
)
952 (goto-char (match-beginning 0))
953 (insert "\n<!-- Org Htmlfontify Styles -->\n" styles
"\n")))))
955 (defun org-odt-format-source-code-or-example-colored
956 (lines lang caption textareap cols rows num cont rpllbl fmt
)
957 "Format source or example blocks using `htmlfontify-string'.
958 Use this routine when `org-export-odt-use-htmlfontify' option is
960 (let* ((lang-m (and lang
(or (cdr (assoc lang org-src-lang-modes
)) lang
)))
961 (mode (and lang-m
(intern (concat (if (symbolp lang-m
)
964 (org-inhibit-startup t
)
965 (org-startup-folded nil
)
966 (lines (with-temp-buffer
968 (if (functionp mode
) (funcall mode
) (fundamental-mode))
969 (font-lock-fontify-buffer)
971 (hfy-html-quote-regex "\\([<\"&> ]\\)")
972 (hfy-html-quote-map '(("\"" """)
977 (" " "<text:tab/>")))
978 (hfy-face-to-css 'org-odt-hfy-face-to-css
)
979 (hfy-optimisations-1 (copy-seq hfy-optimisations
))
980 (hfy-optimisations (add-to-list 'hfy-optimisations-1
982 (hfy-begin-span-handler
983 (lambda (style text-block text-id text-begins-block-p
)
984 (insert (format "<text:span text:style-name=\"%s\">" style
))))
985 (hfy-end-span-handler (lambda nil
(insert "</text:span>"))))
986 (when (fboundp 'htmlfontify-string
)
989 (org-odt-format-stylized-paragraph 'src
(htmlfontify-string line
)))
990 (org-split-string lines
"[\r\n]") "\n"))))
992 (defun org-odt-format-source-code-or-example (lines lang caption textareap
995 "Format source or example blocks for export.
996 Use `org-odt-format-source-code-or-example-plain' or
997 `org-odt-format-source-code-or-example-colored' depending on the
998 value of `org-export-odt-use-htmlfontify."
1000 (if (and org-export-odt-use-htmlfontify
1001 (or (featurep 'htmlfontify
) (require 'htmlfontify
))
1002 (fboundp 'htmlfontify-string
))
1003 'org-odt-format-source-code-or-example-colored
1004 'org-odt-format-source-code-or-example-plain
)
1005 lines lang caption textareap cols rows num cont rpllbl fmt
))
1007 (defun org-xml-encode-plain-text-lines (rtn)
1008 (mapconcat 'org-xml-encode-plain-text
(org-split-string rtn
"[\r\n]") "\n"))
1010 (defun org-odt-remap-stylenames (style-name)
1012 (cdr (assoc style-name
'(("timestamp-wrapper" .
"OrgTimestampWrapper")
1013 ("timestamp" .
"OrgTimestamp")
1014 ("timestamp-kwd" .
"OrgTimestampKeyword")
1016 ("todo" .
"OrgTodo")
1017 ("done" .
"OrgDone")
1018 ("target" .
"OrgTarget"))))
1021 (defun org-odt-format-fontify (text style
&optional id
)
1025 (org-odt-remap-stylenames style
))
1027 (org-odt-get-style-name-for-entity 'character style
))
1029 (assert (< 1 (length style
)))
1030 (let ((parent-style (pop style
)))
1031 (mapconcat (lambda (s)
1032 ;; (assert (stringp s) t)
1033 (org-odt-remap-stylenames s
)) style
"")
1034 (org-odt-remap-stylenames parent-style
)))
1035 (t (error "Don't how to handle style %s" style
)))))
1036 (org-odt-format-tags
1037 '("<text:span text:style-name=\"%s\">" .
"</text:span>")
1040 (defun org-odt-relocate-relative-path (path dir
)
1041 (if (file-name-absolute-p path
) path
1042 (file-relative-name (expand-file-name path dir
)
1043 (expand-file-name "eyecandy" dir
))))
1045 (defun org-odt-format-inline-image (thefile)
1046 (let* ((thelink (if (file-name-absolute-p thefile
) thefile
1047 (org-xml-format-href
1048 (org-odt-relocate-relative-path
1049 thefile org-current-export-file
))))
1051 (org-odt-format-tags
1052 "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
1053 (if org-export-odt-embed-images
1054 (org-odt-copy-image-file thefile
) thelink
))))
1055 (org-export-odt-format-image thefile href
)))
1057 (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
1060 OPT-PLIST is an options list.
1061 TYPE is the device-type of the link (THIS://foo.html)
1062 PATH is the path of the link (http://THIS#locationx)
1063 FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
1064 DESC is the link description, if any.
1065 ATTR is a string of other attributes of the a element.
1066 MAY-INLINE-P allows inlining it as an image."
1068 (declare (special org-lparse-par-open
))
1070 (let* ((may-inline-p
1071 (and (member type-1
'("http" "https" "file"))
1072 (org-lparse-should-inline-p path descp
)
1074 (type (if (equal type-1
"id") "file" type-1
))
1079 ;; check for inlined images
1080 ((and (member type
'("file"))
1083 filename org-odt-export-inline-image-extensions
)
1084 (or (eq t org-odt-export-inline-images
)
1085 (and org-odt-export-inline-images
(not descp
))))
1087 ;; (when (and (string= type "file") (file-name-absolute-p path))
1088 ;; (setq thefile (concat "file://" (expand-file-name path))))
1089 ;; (setq thefile (org-xml-format-href thefile))
1090 ;; (org-export-html-format-image thefile)
1091 (org-odt-format-inline-image thefile
))
1093 (when (string= type
"file")
1096 ((file-name-absolute-p path
)
1097 (concat "file://" (expand-file-name path
)))
1098 (t (org-odt-relocate-relative-path
1099 thefile org-current-export-file
)))))
1101 (when (and (member type
'("" "http" "https" "file" "coderef"))
1103 (setq thefile
(concat thefile
"#" fragment
)))
1105 (setq thefile
(org-xml-format-href thefile
))
1107 (when (not (member type
'("" "file" "coderef")))
1108 (setq thefile
(concat type
":" thefile
)))
1110 (let ((org-odt-suppress-xref (string= type
"coderef")))
1111 (org-odt-format-link
1112 (org-xml-format-desc desc
) thefile attr
)))))))
1114 (defun org-odt-format-heading (text level
&optional id
)
1115 (let* ((text (if id
(org-odt-format-target text id
) text
)))
1116 (org-odt-format-tags
1117 '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
1118 "</text:h>") text level level
)))
1120 (defun org-odt-format-headline (title extra-targets tags
1121 &optional snumber level
)
1123 (org-lparse-format 'EXTRA-TARGETS extra-targets
)
1125 ;; No need to generate section numbers. They are auto-generated by
1128 ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
1130 (and tags
(concat (org-lparse-format 'SPACES
3)
1131 (org-lparse-format 'ORG-TAGS tags
)))))
1133 (defun org-odt-format-anchor (text name
&optional class
)
1134 (org-odt-format-target text name
))
1136 (defun org-odt-format-bookmark (text id
)
1138 (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id
)
1141 (defun org-odt-format-target (text id
)
1142 (let ((name (concat org-export-odt-bookmark-prefix id
)))
1144 (and id
(org-odt-format-tags
1145 "<text:bookmark-start text:name=\"%s\"/>" "" name
))
1146 (org-odt-format-bookmark text id
)
1147 (and id
(org-odt-format-tags
1148 "<text:bookmark-end text:name=\"%s\"/>" "" name
)))))
1150 (defun org-odt-format-footnote (n def
)
1151 (let ((id (concat "fn" n
))
1152 (note-class "footnote")
1153 (par-style "Footnote"))
1154 (org-odt-format-tags
1155 '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
1158 (org-odt-format-tags
1159 '("<text:note-citation>" .
"</text:note-citation>")
1161 (org-odt-format-tags
1162 '("<text:note-body>" .
"</text:note-body>")
1166 (defun org-odt-format-footnote-reference (n def refcnt
)
1168 (org-odt-format-footnote n def
)
1169 (org-odt-format-footnote-ref n
)))
1171 (defun org-odt-format-footnote-ref (n)
1172 (let ((note-class "footnote")
1174 (ref-name (concat "fn" n
)))
1175 (org-odt-format-tags
1176 '("<text:span text:style-name=\"%s\">" .
"</text:span>")
1177 (org-odt-format-tags
1178 '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" .
"</text:note-ref>")
1179 n note-class ref-format ref-name
)
1182 (defun org-odt-get-image-name (file-name)
1186 (concat (sha1 file-name
) "." (file-name-extension file-name
)) "Pictures")))
1188 (defun org-export-odt-format-image (src href
)
1189 "Create image tag with source and attributes."
1191 (let* ((caption (org-find-text-property-in-string 'org-caption src
))
1192 (caption (and caption
(org-xml-format-desc caption
)))
1193 (attr (org-find-text-property-in-string 'org-attributes src
))
1194 (label (org-find-text-property-in-string 'org-label src
))
1195 (embed-as (if (string-match "^ltxpng/" src
) 'character
'paragraph
))
1196 (attr-plist (when attr
(read attr
)))
1197 (size (org-odt-image-size-from-file
1198 src
(plist-get attr-plist
:width
)
1199 (plist-get attr-plist
:height
)
1200 (plist-get attr-plist
:scale
) nil embed-as
)))
1201 (org-export-odt-do-format-image
1202 embed-as caption attr label size href
))))
1204 (defun org-odt-format-textbox (text style
)
1205 (let ((draw-frame-pair
1206 '("<draw:frame draw:style-name=\"%s\"
1207 text:anchor-type=\"paragraph\"
1208 style:rel-width=\"100%%\"
1209 draw:z-index=\"0\">" .
"</draw:frame>")))
1210 (org-odt-format-tags
1212 (org-odt-format-tags
1213 '("<draw:text-box fo:min-height=\"%dcm\">" .
"</draw:text-box>")
1216 (defun org-odt-format-inlinetask (heading content
1217 &optional todo priority tags
)
1218 (org-odt-format-stylized-paragraph
1219 nil
(org-odt-format-textbox
1220 (concat (org-odt-format-stylized-paragraph
1221 "OrgInlineTaskHeading"
1223 'HEADLINE
(concat (org-lparse-format-todo todo
) " " heading
)
1225 content
) "OrgInlineTaskFrame")))
1227 (defun org-export-odt-do-format-image (embed-as caption attr label
1229 "Create image tag with source and attributes."
1231 (let ((width (car size
)) (height (cdr size
))
1233 '("<draw:frame draw:style-name=\"%s\"
1234 text:anchor-type=\"%s\"
1235 draw:z-index=\"%d\" %s>" .
"</draw:frame>")))
1237 ((and (not caption
) (not label
))
1238 (let (style-name anchor-type
)
1240 ((eq embed-as
'paragraph
)
1241 (setq style-name
"OrgGraphicsParagraph" anchor-type
"paragraph"))
1242 ((eq embed-as
'character
)
1243 (setq style-name
"OrgGraphicsBaseline" anchor-type
"as-char")))
1244 (org-odt-format-tags
1245 draw-frame-pair href style-name anchor-type
0
1246 (org-odt-image-attrs-from-size width height
))))
1250 ;; (when par-open (org-odt-close-par))
1251 (org-odt-format-tags
1253 (org-odt-format-tags
1254 '("<draw:text-box fo:min-height=\"%dcm\">" .
"</draw:text-box>")
1255 (org-odt-format-stylized-paragraph
1258 (let ((extra " style:rel-width=\"100%\" style:rel-height=\"scale\""))
1259 (org-odt-format-tags
1260 draw-frame-pair href
"OrgGraphicsParagraphContent" "paragraph" 2
1261 (concat (org-odt-image-attrs-from-size width height
) extra
)))
1262 (org-odt-format-entity-caption label caption
)))
1264 "OrgFrame" "paragraph" 1
1265 (org-odt-image-attrs-from-size width
))
1267 ;; (when par-open (org-odt-open-par))
1270 ;; xml files generated on-the-fly
1271 (defconst org-export-odt-save-list
1272 '("mimetype" "META-INF/manifest.xml" "content.xml" "meta.xml" "styles.xml"))
1274 ;; xml files that are copied
1275 (defconst org-export-odt-nosave-list
'())
1277 ;; xml files that contribute to the final odt file
1278 (defvar org-export-odt-file-list nil
)
1280 (defconst org-export-odt-manifest-lines
1281 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1282 "<manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">"
1283 "<manifest:file-entry manifest:media-type=\"application/vnd.oasis.opendocument.text\" manifest:version=\"1.2\" manifest:full-path=\"/\"/>"
1284 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"content.xml\"/>"
1285 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"styles.xml\"/>"
1286 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"meta.xml\"/>"
1287 "<manifest:file-entry manifest:media-type=\"\" manifest:full-path=\"Pictures/\"/>") .
("</manifest:manifest>")))
1289 (defconst org-export-odt-meta-lines
1290 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1291 "<office:document-meta"
1292 " xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\""
1293 " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
1294 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\""
1295 " xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\""
1296 " xmlns:ooo=\"http://openoffice.org/2004/office\" "
1297 " office:version=\"1.2\">"
1298 " <office:meta>") .
(" </office:meta>" "</office:document-meta>")))
1300 (defun org-odt-copy-image-file (path &optional target-file
)
1301 "Returns the internal name of the file"
1302 (let* ((image-type (file-name-extension path
))
1303 (media-type (format "image/%s" image-type
))
1304 (src-file (expand-file-name
1305 path
(file-name-directory org-current-export-file
)))
1306 (target-file (or target-file
(org-odt-get-image-name src-file
)))
1310 (when (not org-lparse-to-buffer
)
1311 (message "Embedding %s as %s ..."
1312 (substring-no-properties path
) target-file
)
1313 (copy-file src-file target-file
'overwrite
)
1314 (org-odt-update-manifest-file media-type target-file
)
1315 (push target-file org-export-odt-file-list
)) target-file
))
1317 (defun org-odt-image-attrs-from-size (&optional width height
)
1319 (when width
(format "svg:width=\"%0.2fcm\"" width
))
1321 (when height
(format "svg:height=\"%0.2fcm\"" height
))))
1323 (defvar org-export-odt-image-size-probe-method
1324 '(emacs imagemagick force
)
1325 "Ordered list of methods by for determining size of an embedded
1328 (defvar org-export-odt-default-image-sizes-alist
1329 '(("character" .
(5 .
0.4))
1330 ("paragraph" .
(5 .
5)))
1331 "Hardcoded image dimensions one for each of the anchor
1334 (defun org-odt-do-image-size (probe-method file
&optional dpi anchor-type
)
1335 (setq dpi
(or dpi org-export-odt-pixels-per-inch
))
1336 (setq anchor-type
(or anchor-type
"paragraph"))
1337 (flet ((size-in-cms (size-in-pixels)
1338 (flet ((pixels-to-cms (pixels)
1339 (let* ((cms-per-inch 2.54)
1340 (inches (/ pixels dpi
)))
1341 (* cms-per-inch inches
))))
1343 (cons (pixels-to-cms (car size-in-pixels
))
1344 (pixels-to-cms (cdr size-in-pixels
)))))))
1347 (size-in-cms (ignore-errors (image-size (create-image file
) 'pixels
))))
1350 (let ((dim (shell-command-to-string
1351 (format "identify -format \"%%w:%%h\" \"%s\"" file
))))
1352 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim
)
1353 (cons (string-to-number (match-string 1 dim
))
1354 (string-to-number (match-string 2 dim
)))))))
1356 (cdr (assoc-string anchor-type
1357 org-export-odt-default-image-sizes-alist
))))))
1359 (defun org-odt-image-size-from-file (file &optional user-width
1360 user-height scale dpi embed-as
)
1361 (unless (file-name-absolute-p file
)
1362 (setq file
(expand-file-name
1363 file
(file-name-directory org-current-export-file
))))
1364 (let* (size width height
)
1365 (unless (and user-height user-width
)
1366 (loop for probe-method in org-export-odt-image-size-probe-method
1368 do
(setq size
(org-odt-do-image-size
1369 probe-method file dpi embed-as
)))
1370 (or size
(error "Cannot determine Image size. Aborting ..."))
1371 (setq width
(car size
) height
(cdr size
)))
1374 (setq width
(* width scale
) height
(* height scale
)))
1375 ((and user-height user-width
)
1376 (setq width user-width height user-height
))
1378 (setq width
(* user-height
(/ width height
)) height user-height
))
1380 (setq height
(* user-width
(/ height width
)) width user-width
))
1382 (cons width height
)))
1384 (defvar org-odt-default-entity
"Illustration")
1385 (defun org-odt-format-entity-caption (label caption
&optional default-entity
)
1386 (if (not label
) (or caption
"")
1387 (let* ((label-components (org-odt-parse-label label
))
1388 (entity (car label-components
))
1389 (seqno (cdr label-components
))
1390 (caption (and caption
(concat ": " caption
))))
1393 entity
(or default-entity org-odt-default-entity
)))
1396 (org-odt-format-tags
1397 '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" .
"</text:sequence>")
1398 seqno label entity entity
)
1401 (defun org-odt-format-tags (tag text
&rest args
)
1402 (let ((prefix (when org-lparse-encode-pending
"@"))
1403 (suffix (when org-lparse-encode-pending
"@")))
1404 (apply 'org-lparse-format-tags tag text prefix suffix args
)))
1406 (defun org-odt-init-outfile (filename)
1407 (unless (executable-find "zip")
1408 ;; Not at all OSes ship with zip by default
1409 (error "Executable \"zip\" needed for creating OpenDocument files. Aborting."))
1411 (let* ((outdir (make-temp-file org-export-odt-tmpdir-prefix t
))
1412 (mimetype-file (expand-file-name "mimetype" outdir
))
1413 (content-file (expand-file-name "content.xml" outdir
))
1414 (manifest-file (expand-file-name "META-INF/manifest.xml" outdir
))
1415 (meta-file (expand-file-name "meta.xml" outdir
))
1416 (styles-file (expand-file-name "styles.xml" outdir
))
1417 (pictures-dir (expand-file-name "Pictures" outdir
))
1421 (with-current-buffer (find-file-noselect content-file t
)
1424 ;; FIXME: How to factor in body-only here
1427 (make-directory (file-name-directory manifest-file
))
1428 (with-current-buffer (find-file-noselect manifest-file t
)
1430 (insert (mapconcat 'identity
(car org-export-odt-manifest-lines
) "\n"))
1433 (insert (mapconcat 'identity
(cdr org-export-odt-manifest-lines
) "\n"))))
1436 (with-current-buffer (find-file-noselect meta-file t
)
1438 (insert (mapconcat 'identity
(car org-export-odt-meta-lines
) "\n"))
1441 (insert (mapconcat 'identity
(cdr org-export-odt-meta-lines
) "\n"))))
1444 (with-current-buffer (find-file-noselect mimetype-file t
)
1445 (insert "application/vnd.oasis.opendocument.text"))
1448 ;; (copy-file org-export-odt-styles-file styles-file t)
1451 (make-directory pictures-dir
)
1453 ;; initialize list of files that contribute to the odt file
1454 (setq org-export-odt-file-list
1455 (append org-export-odt-save-list org-export-odt-nosave-list
)))
1458 (defconst org-odt-manifest-file-entry-tag
1459 "<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"/>")
1461 (defcustom org-export-odt-prettify-xml nil
1462 "Specify whether or not the xml output should be prettified.
1463 When this option is turned on, `indent-region' is run on all
1464 component xml buffers before they are saved. Turn this off for
1465 regular use. Turn this on if you need to examine the xml
1467 :group
'org-export-odt
1470 (defvar hfy-user-sheet-assoc
) ; bound during org-do-lparse
1471 (defun org-odt-save-as-outfile (target opt-plist
)
1473 (org-odt-update-meta-file opt-plist
)
1475 ;; write styles file
1476 (org-odt-copy-styles-file)
1478 ;; Update styles.xml - take care of outline numbering
1479 (with-current-buffer
1480 (find-file-noselect (expand-file-name "styles.xml") t
)
1481 ;; Don't make automatic backup of styles.xml file. This setting
1482 ;; prevents the backedup styles.xml file from being zipped in to
1483 ;; odt file. This is more of a hackish fix. Better alternative
1484 ;; would be to fix the zip command so that the output odt file
1485 ;; includes only the needed files and excludes any auto-generated
1486 ;; extra files like backups and auto-saves etc etc. Note that
1487 ;; currently the zip command zips up the entire temp directory so
1488 ;; that any auto-generated files created under the hood ends up in
1489 ;; the resulting odt file.
1490 (set (make-local-variable 'backup-inhibited
) t
)
1492 ;; Import local setting of `org-export-with-section-numbers'
1493 (org-lparse-bind-local-variables opt-plist
)
1494 (org-odt-configure-outline-numbering
1495 (if org-export-with-section-numbers org-export-headline-levels
0)))
1497 ;; Write custom stlyes for source blocks
1498 (org-odt-insert-custom-styles-for-srcblocks
1501 (format " %s\n" (cddr style
)))
1502 hfy-user-sheet-assoc
""))
1504 (let ((zipdir default-directory
))
1505 (message "Switching to directory %s" (expand-file-name zipdir
))
1507 ;; save all xml files
1508 (mapc (lambda (file)
1509 (with-current-buffer
1510 (find-file-noselect (expand-file-name file
) t
)
1511 ;; prettify output if needed
1512 (when org-export-odt-prettify-xml
1513 (indent-region (point-min) (point-max)))
1515 org-export-odt-save-list
)
1517 (let* ((target-name (file-name-nondirectory target
))
1518 (target-dir (file-name-directory target
))
1519 (cmds `(("zip" "-mX0" ,target-name
"mimetype")
1520 ("zip" "-rmTq" ,target-name
"."))))
1521 (when (file-exists-p target
)
1522 ;; FIXME: If the file is locked this throws a cryptic error
1523 (delete-file target
))
1525 (let ((coding-system-for-write 'no-conversion
) exitcode
)
1526 (message "Creating odt file...")
1529 (message "Running %s" (mapconcat 'identity cmd
" "))
1531 (apply 'call-process
(car cmd
) nil nil nil
(cdr cmd
)))
1532 (or (zerop exitcode
)
1533 (error "Unable to create odt file (%S)" exitcode
)))
1536 ;; move the file from outdir to target-dir
1537 (rename-file target-name target-dir
)
1539 ;; kill all xml buffers
1540 (mapc (lambda (file)
1542 (find-file-noselect (expand-file-name file zipdir
) t
)))
1543 org-export-odt-save-list
)
1545 (delete-directory zipdir
)))
1547 (message "Created %s" target
)
1548 (set-buffer (find-file-noselect target t
)))
1550 (defun org-odt-format-date (date)
1552 "OpenDocument files require that dates be in ISO-8601 format. Please review your DATE options for compatibility."))
1553 ;; If the user is not careful with the date specification, an
1554 ;; invalid meta.xml will be emitted.
1556 ;; For now honor user's diktat and let him off with a warning
1557 ;; message. This is OK as LibreOffice (and possibly other
1558 ;; apps) doesn't deem this deviation as critical and continue
1559 ;; to load the file.
1561 ;; FIXME: Surely there a better way to handle this. Revisit this
1564 ((and date
(string-match "%" date
))
1565 ;; Honor user's diktat. See comments above
1566 (org-lparse-warn warning-msg
)
1567 (format-time-string date
))
1569 ;; Honor user's diktat. See comments above
1570 (org-lparse-warn warning-msg
)
1574 (format-time-string "%Y-%m-%dT%T%:z")))))
1576 (defun org-odt-update-meta-file (opt-plist)
1577 (with-current-buffer
1578 (find-file-noselect (expand-file-name "meta.xml") t
)
1579 (let ((date (org-odt-format-date (plist-get opt-plist
:date
)))
1580 (author (or (plist-get opt-plist
:author
) ""))
1581 (email (plist-get opt-plist
:email
))
1582 (keywords (plist-get opt-plist
:keywords
))
1583 (description (plist-get opt-plist
:description
))
1584 (title (plist-get opt-plist
:title
)))
1588 (org-odt-format-tags '("<dc:creator>" .
"</dc:creator>") author
)
1589 (org-odt-format-tags
1590 '("\n<meta:initial-creator>" .
"</meta:initial-creator>") author
)
1591 (org-odt-format-tags '("\n<dc:date>" .
"</dc:date>") date
)
1592 (org-odt-format-tags
1593 '("\n<meta:creation-date>" .
"</meta:creation-date>") date
)
1594 (org-odt-format-tags '("\n<meta:generator>" .
"</meta:generator>")
1595 (when org-export-creator-info
1596 (format "Org-%s/Emacs-%s"
1597 org-version emacs-version
)))
1598 (org-odt-format-tags '("\n<meta:keyword>" .
"</meta:keyword>") keywords
)
1599 (org-odt-format-tags '("\n<dc:subject>" .
"</dc:subject>") description
)
1600 (org-odt-format-tags '("\n<dc:title>" .
"</dc:title>") title
)
1603 (defun org-odt-update-manifest-file (media-type full-path
)
1604 (with-current-buffer
1605 (find-file-noselect (expand-file-name "META-INF/manifest.xml") t
)
1606 (insert (format org-odt-manifest-file-entry-tag media-type full-path
))))
1608 (defun org-odt-finalize-outfile ()
1609 (org-odt-delete-empty-paragraphs))
1611 (defun org-odt-delete-empty-paragraphs ()
1612 (goto-char (point-min))
1613 (let ((open "<text:p[^>]*>")
1614 (close "</text:p>"))
1615 (while (re-search-forward (format "%s[ \r\n\t]*%s" open close
) nil t
)
1616 (replace-match ""))))
1618 (defun org-odt-get (what &optional opt-plist
)
1621 (EXPORT-DIR (org-export-directory :html opt-plist
))
1622 (FILE-NAME-EXTENSION "odt")
1623 (EXPORT-BUFFER-NAME "*Org ODT Export*")
1624 (ENTITY-CONTROL org-odt-entity-control-callbacks-alist
)
1625 (ENTITY-FORMAT org-odt-entity-format-callbacks-alist
)
1626 (INIT-METHOD 'org-odt-init-outfile
)
1627 (FINAL-METHOD 'org-odt-finalize-outfile
)
1628 (SAVE-METHOD 'org-odt-save-as-outfile
)
1629 ;; (OTHER-BACKENDS) ; see note in `org-xhtml-get'
1630 ;; (CONVERT-METHOD) ; see note in `org-xhtml-get'
1632 (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps
)
1633 (INLINE-IMAGES 'maybe
)
1634 (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
1635 (PLAIN-TEXT-MAP '(("&" .
"&") ("<" .
"<") (">" .
">")))
1636 (TABLE-FIRST-COLUMN-AS-LABELS nil
)
1637 (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY
"," 'superscript
))
1638 (CODING-SYSTEM-FOR-WRITE 'utf-8
)
1639 (CODING-SYSTEM-FOR-SAVE 'utf-8
)
1640 (t (error "Unknown property: %s" what
))))
1642 (defun org-odt-parse-label (label)
1644 (if (not (string-match "\\`[a-zA-Z]+:\\(.+\\)" label
))
1647 (capitalize (substring label
0 (1- (match-beginning 1))))
1648 (substring label
(match-beginning 1))))))
1650 (defvar org-lparse-latex-fragment-fallback
) ; set by org-do-lparse
1651 (defvar org-lparse-opt-plist
) ; bound during org-do-lparse
1652 (defun org-export-odt-do-preprocess-latex-fragments ()
1653 "Convert LaTeX fragments to images."
1654 (let* ((latex-frag-opt (plist-get org-lparse-opt-plist
:LaTeX-fragments
))
1655 (latex-frag-opt-1 ; massage the options
1656 (or (and (member latex-frag-opt
'(mathjax t
))
1657 (prog1 org-lparse-latex-fragment-fallback
1660 "Use of MathJax is incompatible with ODT exporter. "
1661 (format "Using %S instead."
1662 org-lparse-latex-fragment-fallback
)))))
1664 (when (and org-current-export-file latex-frag-opt-1
)
1665 ;; Investigate MathToWeb for converting TeX equations to MathML
1666 ;; http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01755.html
1668 (concat "ltxpng/" (file-name-sans-extension
1669 (file-name-nondirectory
1670 org-current-export-file
)))
1671 org-current-export-dir nil
"Creating LaTeX image %s"
1672 nil nil latex-frag-opt-1
))))
1674 (defun org-export-odt-preprocess-latex-fragments ()
1675 (when (equal org-export-current-backend
'odt
)
1676 (org-export-odt-do-preprocess-latex-fragments)))
1678 (defun org-export-odt-preprocess-label-references ()
1679 (goto-char (point-min))
1680 (let (label label-components category value pretty-label
)
1681 (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t
)
1682 (org-if-unprotected-at (match-beginning 1)
1683 (setq label
(match-string 1)
1684 label-components
(org-odt-parse-label label
)
1685 category
(car label-components
)
1686 value
(cdr label-components
)
1687 pretty-label
(if value
(concat category
" " value
) label
))
1689 (let ((org-lparse-encode-pending t
))
1690 (org-odt-format-tags
1691 '("<text:sequence-ref text:reference-format=\"category-and-value\" text:ref-name=\"%s\">"
1692 .
"</text:sequence-ref>") pretty-label label
)) t t
)))))
1694 ;; process latex fragments as part of
1695 ;; `org-export-preprocess-after-blockquote-hook'. Note that this hook
1696 ;; is the one that is closest and well before the call to
1697 ;; `org-export-attach-captions-and-attributes' in
1698 ;; `org-export-preprocess-stirng'. The above arrangement permits
1699 ;; captions, labels and attributes to be attached to png images
1700 ;; generated out of latex equations.
1701 (add-hook 'org-export-preprocess-after-blockquote-hook
1702 'org-export-odt-preprocess-latex-fragments
)
1704 (defun org-export-odt-preprocess (parameters)
1705 (org-export-odt-preprocess-label-references))
1707 (declare-function archive-zip-extract
"arc-mode.el" (archive name
))
1708 (defun org-odt-zip-extract-one (archive member
&optional target
)
1710 (let* ((target (or target default-directory
))
1711 (archive (expand-file-name archive
))
1712 (archive-zip-extract
1713 (list "unzip" "-qq" "-o" "-d" target
))
1714 exit-code command-output
)
1715 (setq command-output
1717 (setq exit-code
(archive-zip-extract archive member
))
1719 (unless (zerop exit-code
)
1720 (message command-output
)
1721 (error "Extraction failed"))))
1723 (defun org-odt-zip-extract (archive members
&optional target
)
1724 (when (atom members
) (setq members
(list members
)))
1725 (mapc (lambda (member)
1726 (org-odt-zip-extract-one archive member target
))
1729 (defun org-odt-copy-styles-file (&optional styles-file
)
1730 ;; Non-availability of styles.xml is not a critical error. For now
1731 ;; throw an error purely for aesthetic reasons.
1732 (setq styles-file
(or styles-file
1733 org-export-odt-styles-file
1734 (expand-file-name "styles/OrgOdtStyles.xml"
1736 (error "org-odt: Missing styles file?")))
1738 ((listp styles-file
)
1739 (let ((archive (nth 0 styles-file
))
1740 (members (nth 1 styles-file
)))
1741 (org-odt-zip-extract archive members
)
1744 (when (org-file-image-p member
)
1745 (let* ((image-type (file-name-extension member
))
1746 (media-type (format "image/%s" image-type
)))
1747 (org-odt-update-manifest-file media-type member
))))
1749 ((and (stringp styles-file
) (file-exists-p styles-file
))
1750 (let ((styles-file-type (file-name-extension styles-file
)))
1752 ((string= styles-file-type
"xml")
1753 (copy-file styles-file
"styles.xml" t
))
1754 ((member styles-file-type
'("odt" "ott"))
1755 (org-odt-zip-extract styles-file
"styles.xml")))))
1757 (error (format "Invalid specification of styles.xml file: %S"
1758 org-export-odt-styles-file
)))))
1760 (defvar org-export-odt-factory-settings
1761 "d4328fb9d1b6cb211d4320ff546829f26700dc5e"
1762 "SHA1 hash of OrgOdtStyles.xml.")
1764 (defun org-odt-configure-outline-numbering (level)
1765 "Outline numbering is retained only upto LEVEL.
1766 To disable outline numbering pass a LEVEL of 0."
1767 (goto-char (point-min))
1769 "<text:outline-level-style\\(.*\\)text:level=\"\\([^\"]*\\)\"\\(.*\\)>")
1771 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1772 (while (re-search-forward regex nil t
)
1773 (when (> (string-to-number (match-string 2)) level
)
1774 (replace-match replacement t nil
))))
1779 ;;; org-odt.el ends here