1 ;;; org-odt.el --- OpenDocumentText export for Org-mode
3 ;; Copyright (C) 2010, 2011
4 ;; Jambunathan <kjambunathan at gmail dot com>
6 ;; Author: Jambunathan K <kjambunathan at gmail dot com>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
11 ;; This file is not (yet) part of GNU Emacs.
12 ;; However, it is distributed under the same license.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
31 (eval-when-compile (require 'cl
))
34 (defun org-odt-end-export ()
35 ;; remove empty paragraphs
36 (goto-char (point-min))
37 (while (re-search-forward
38 "<text:p\\( text:style-name=\"Text_20_body\"\\)?>[ \r\n\t]*</text:p>"
41 (goto-char (point-min))
43 ;; Convert whitespace place holders
44 (goto-char (point-min))
46 (while (setq beg
(next-single-property-change (point) 'org-whitespace
))
47 (setq n
(get-text-property beg
'org-whitespace
)
48 end
(next-single-property-change beg
'org-whitespace
))
50 (delete-region beg end
)
51 (insert (format "<span style=\"visibility:hidden;\">%s</span>"
52 (make-string n ?x
)))))
54 ;; Remove empty lines at the beginning of the file.
55 (goto-char (point-min))
56 (when (looking-at "\\s-+\n") (replace-match ""))
58 ;; Remove display properties
59 (remove-text-properties (point-min) (point-max) '(display t
)))
61 (defvar org-odt-suppress-xref nil
)
62 (defconst org-export-odt-special-string-regexps
63 '(("\\\\-" .
"­\\1") ; shy
64 ("---\\([^-]\\)" .
"—\\1") ; mdash
65 ("--\\([^-]\\)" .
"–\\1") ; ndash
66 ("\\.\\.\\." .
"…")) ; hellip
67 "Regular expressions for special string conversion.")
69 (defconst org-odt-lib-dir
(file-name-directory load-file-name
))
70 (defconst org-odt-data-dir
71 (let ((dir1 (expand-file-name "../odt" org-odt-lib-dir
)) ; git
72 (dir2 (expand-file-name "./contrib/odt" org-odt-lib-dir
))) ; elpa
74 ((file-directory-p dir1
) dir1
)
75 ((file-directory-p dir2
) dir2
)
76 (t (error "Cannot find factory styles file. Check package dir layout"))))
77 "Directory that holds auxiliary files used by the ODT exporter.
79 The 'styles' subdir contains the following xml files -
80 'OrgOdtStyles.xml' and 'OrgOdtAutomaticStyles.xml' - which are
81 used as factory settings of `org-export-odt-styles-file' and
82 `org-export-odt-automatic-styles-file'.
84 The 'etc/schema' subdir contains rnc files for validating of
85 OpenDocument xml files.")
87 (defvar org-odt-file-extensions
88 '(("odt" .
"OpenDocument Text")
89 ("ott" .
"OpenDocument Text Template")
90 ("odm" .
"OpenDocument Master Document")
91 ("ods" .
"OpenDocument Spreadsheet")
92 ("ots" .
"OpenDocument Spreadsheet Template")
93 ("odg" .
"OpenDocument Drawing (Graphics)")
94 ("otg" .
"OpenDocument Drawing Template")
95 ("odp" .
"OpenDocument Presentation")
96 ("otp" .
"OpenDocument Presentation Template")
97 ("odi" .
"OpenDocument Image")
98 ("odf" .
"OpenDocument Formula")
99 ("odc" .
"OpenDocument Chart")
100 ("doc" .
"Microsoft Text")
101 ("docx" .
"Microsoft Text")
102 ("xls" .
"Microsoft Spreadsheet")
103 ("xlsx" .
"Microsoft Spreadsheet")
104 ("ppt" .
"Microsoft Presentation")
105 ("pptx" .
"Microsoft Presentation")))
107 (defvar org-odt-ms-file-extensions
108 '(("doc" .
"Microsoft Text")
109 ("docx" .
"Microsoft Text")
110 ("xls" .
"Microsoft Spreadsheet")
111 ("xlsx" .
"Microsoft Spreadsheet")
112 ("ppt" .
"Microsoft Presentation")
113 ("pptx" .
"Microsoft Presentation")))
115 ;; RelaxNG validation of OpenDocument xml files
116 (eval-after-load 'rng-nxml
117 '(setq rng-nxml-auto-validate-flag t
))
119 (eval-after-load 'rng-loc
120 '(add-to-list 'rng-schema-locating-files
121 (expand-file-name "etc/schema/schemas.xml" org-odt-data-dir
)))
125 ;; Let Org open all OpenDocument files using system-registered app
126 (add-to-list 'org-file-apps
127 (cons (concat "\\." (car desc
) "\\'") 'system
))
128 ;; Let Emacs open all OpenDocument files in archive mode
129 (add-to-list 'auto-mode-alist
130 (cons (concat "\\." (car desc
) "\\'") 'archive-mode
)))
131 org-odt-file-extensions
)
135 ;; Let Org open all Microsoft files using system-registered app
136 (add-to-list 'org-file-apps
137 (cons (concat "\\." (car desc
) "\\'") 'system
)))
138 org-odt-ms-file-extensions
)
140 ;; register the odt exporter with the pre-processor
141 (add-to-list 'org-export-backends
'odt
)
143 ;; register the odt exporter with org-lparse library
144 (org-lparse-register-backend 'odt
)
146 (defcustom org-export-odt-automatic-styles-file nil
147 "Automatic styles for use with ODT exporter.
148 If unspecified, the file under `org-odt-data-dir' is used."
150 :group
'org-export-odt
)
152 (defcustom org-export-odt-styles-file nil
153 "Default styles file for use with ODT export.
154 Valid values are one of:
156 2. path to a styles.xml file
157 3. path to a *.odt or a *.ott file
158 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
161 In case of option 1, an in-built styles.xml is used. See
162 `org-odt-data-dir' for more information.
164 In case of option 3, the specified file is unzipped and the
165 styles.xml embedded therein is used.
167 In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
168 and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
169 generated odt file. Use relative path for specifying the
170 FILE-MEMBERS. styles.xml must be specified as one of the
173 Use options 1, 2 or 3 only if styles.xml alone suffices for
174 achieving the desired formatting. Use option 4, if the styles.xml
175 references additional files like header and footer images for
176 achieving the desired formattting."
177 :group
'org-export-odt
180 (const :tag
"Factory settings" nil
)
181 (file :must-match t
:tag
"styles.xml")
182 (file :must-match t
:tag
"ODT or OTT file")
183 (list :tag
"ODT or OTT file + Members"
184 (file :must-match t
:tag
"ODF Text or Text Template file")
186 (file :tag
" Member" "styles.xml")
187 (repeat (file :tag
"Member"))))))
189 (defconst org-export-odt-tmpdir-prefix
"odt-")
190 (defconst org-export-odt-bookmark-prefix
"OrgXref.")
191 (defcustom org-export-odt-use-bookmarks-for-internal-links t
192 "Export Internal links as bookmarks?."
194 :group
'org-export-odt
)
196 (defcustom org-export-odt-embed-images t
197 "Should the images be copied in to the odt file or just linked?"
199 :group
'org-export-odt
)
201 (defcustom org-odt-export-inline-images
'maybe
202 "Non-nil means inline images into exported HTML pages.
203 This is done using an <img> tag. When nil, an anchor with href is used to
204 link to the image. If this option is `maybe', then images in links with
205 an empty description will be inlined, while images with a description will
207 :group
'org-odt-export
208 :type
'(choice (const :tag
"Never" nil
)
209 (const :tag
"Always" t
)
210 (const :tag
"When there is no description" maybe
)))
212 (defcustom org-odt-export-inline-image-extensions
213 '("png" "jpeg" "jpg" "gif")
214 "Extensions of image files that can be inlined into HTML."
215 :type
'(repeat (string :tag
"Extension"))
216 :group
'org-odt-export
)
218 (defcustom org-export-odt-pixels-per-inch display-pixels-per-inch
219 ;; FIXME add docstring
222 :group
'org-export-odt
)
224 (defvar org-export-odt-default-org-styles-alist
225 '((paragraph .
((default .
"Text_20_body")
226 (fixedwidth .
"OrgSourceBlock")
228 (quote .
"Quotations")
229 (blockquote .
"Quotations")
230 (center .
"OrgCenter")
233 (title .
"Heading_20_1.title")
234 (footnote .
"Footnote")
235 (src .
"OrgSourceBlock")
236 (illustration .
"Illustration")
238 (definition-term .
"Text_20_body_20_bold")
239 (horizontal-line .
"Horizontal_20_Line")))
240 (character .
((bold .
"Bold")
241 (emphasis .
"Emphasis")
243 (verbatim .
"OrgCode")
244 (strike .
"Strikethrough")
245 (underline .
"Underline")
246 (subscript .
"OrgSubscript")
247 (superscript .
"OrgSuperscript")))
248 (list .
((ordered .
"OrgNumberedList")
249 (unordered .
"OrgBulletedList")
250 (description .
"OrgDescriptionList"))))
251 "Default styles for various entities.")
253 (defvar org-export-odt-org-styles-alist org-export-odt-default-org-styles-alist
)
254 (defun org-odt-get-style-name-for-entity (category &optional entity
)
255 (let ((entity (or entity
'default
)))
257 (cdr (assoc entity
(cdr (assoc category
258 org-export-odt-org-styles-alist
))))
259 (cdr (assoc entity
(cdr (assoc category
260 org-export-odt-default-org-styles-alist
))))
261 (error "Cannot determine style name for entity %s of type %s"
265 (defun org-export-as-odt-and-open (arg)
266 "Export the outline as ODT and immediately open it with a browser.
267 If there is an active region, export only the region.
268 The prefix ARG specifies how many levels of the outline should become
269 headlines. The default is 3. Lower levels will become bulleted lists."
271 (org-lparse-and-open "odt" "odt" arg
))
274 (defun org-export-as-odt-batch ()
275 "Call the function `org-lparse-batch'.
276 This function can be used in batch processing as:
278 --load=$HOME/lib/emacs/org.el
279 --eval \"(setq org-export-headline-levels 2)\"
280 --visit=MyFile --funcall org-export-as-odt-batch"
281 (org-lparse-batch "odt"))
284 (defun org-export-as-odt-to-buffer (arg)
285 "Call `org-lparse-odt` with output to a temporary buffer.
286 No file is created. The prefix ARG is passed through to `org-lparse-to-buffer'."
288 (org-lparse-to-buffer "odt" arg
))
291 (defun org-replace-region-by-odt (beg end
)
292 "Assume the current region has org-mode syntax, and convert it to ODT.
293 This can be used in any buffer. For example, you could write an
294 itemized list in org-mode syntax in an ODT buffer and then use this
295 command to convert it."
297 (org-replace-region-by "odt" beg end
))
300 (defun org-export-region-as-odt (beg end
&optional body-only buffer
)
301 "Convert region from BEG to END in org-mode buffer to ODT.
302 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
303 contents, and only produce the region of converted text, useful for
304 cut-and-paste operations.
305 If BUFFER is a buffer or a string, use/create that buffer as a target
306 of the converted ODT. If BUFFER is the symbol `string', return the
307 produced ODT as a string and leave not buffer behind. For example,
308 a Lisp program could call this function in the following way:
310 (setq odt (org-export-region-as-odt beg end t 'string))
312 When called interactively, the output buffer is selected, and shown
313 in a window. A non-interactive call will only return the buffer."
315 (org-lparse-region "odt" beg end body-only buffer
))
317 ;;; org-export-as-odt
319 (defun org-export-as-odt (arg &optional hidden ext-plist
320 to-buffer body-only pub-dir
)
321 "Export the outline as a OpenDocumentText file.
322 If there is an active region, export only the region. The prefix
323 ARG specifies how many levels of the outline should become
324 headlines. The default is 3. Lower levels will become bulleted
325 lists. HIDDEN is obsolete and does nothing.
326 EXT-PLIST is a property list with external parameters overriding
327 org-mode's default settings, but still inferior to file-local
328 settings. When TO-BUFFER is non-nil, create a buffer with that
329 name and export to that buffer. If TO-BUFFER is the symbol
330 `string', don't leave any buffer behind but just return the
331 resulting XML as a string. When BODY-ONLY is set, don't produce
332 the file header and footer, simply return the content of
333 <body>...</body>, without even the body tags themselves. When
334 PUB-DIR is set, use this as the publishing directory."
336 (org-lparse "odt" "odt" arg hidden ext-plist to-buffer body-only pub-dir
))
338 (defvar org-odt-entity-control-callbacks-alist
340 .
(org-odt-begin-export org-odt-end-export
))
342 .
(org-odt-begin-document-content org-odt-end-document-content
))
344 .
(org-odt-begin-document-body org-odt-end-document-body
))
346 .
(org-odt-begin-toc org-odt-end-toc
))
348 .
(org-odt-begin-environment org-odt-end-environment
))
350 .
(org-odt-begin-footnote-definition org-odt-end-footnote-definition
))
352 .
(org-odt-begin-table org-odt-end-table
))
354 .
(org-odt-begin-table-rowgroup org-odt-end-table-rowgroup
))
356 .
(org-odt-begin-list org-odt-end-list
))
358 .
(org-odt-begin-list-item org-odt-end-list-item
))
360 .
(org-odt-begin-outline org-odt-end-outline
))
362 .
(org-odt-begin-outline-text org-odt-end-outline-text
))
364 .
(org-odt-begin-paragraph org-odt-end-paragraph
)))
367 (defvar org-odt-entity-format-callbacks-alist
368 `((EXTRA-TARGETS . org-lparse-format-extra-targets
)
369 (ORG-TAGS . org-lparse-format-org-tags
)
370 (SECTION-NUMBER . org-lparse-format-section-number
)
371 (HEADLINE . org-odt-format-headline
)
372 (TOC-ENTRY . org-odt-format-toc-entry
)
373 (TOC-ITEM . org-odt-format-toc-item
)
374 (TAGS . org-odt-format-tags
)
375 (SPACES . org-odt-format-spaces
)
376 (TABS . org-odt-format-tabs
)
377 (LINE-BREAK . org-odt-format-line-break
)
378 (FONTIFY . org-odt-format-fontify
)
379 (TODO . org-lparse-format-todo
)
380 (LINK . org-odt-format-link
)
381 (INLINE-IMAGE . org-odt-format-inline-image
)
382 (ORG-LINK . org-odt-format-org-link
)
383 (HEADING . org-odt-format-heading
)
384 (ANCHOR . org-odt-format-anchor
)
385 (TABLE . org-lparse-format-table
)
386 (TABLE-ROW . org-odt-format-table-row
)
387 (TABLE-CELL . org-odt-format-table-cell
)
388 (FOOTNOTES-SECTION . ignore
)
389 (FOOTNOTE-REFERENCE . org-odt-format-footnote-reference
)
390 (HORIZONTAL-LINE . org-odt-format-horizontal-line
)
391 (COMMENT . org-odt-format-comment
)
392 (LINE . org-odt-format-line
)
393 (ORG-ENTITY . org-odt-format-org-entity
))
397 ;;;_. control callbacks
399 (defun org-odt-begin-office-body ()
403 <text:sequence-decls>
404 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Illustration\"/>
405 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Table\"/>
406 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Text\"/>
407 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Drawing\"/>
408 </text:sequence-decls>"))
410 ;; Following variable is let bound when `org-do-lparse' is in
411 ;; progress. See org-html.el.
412 (defvar org-lparse-toc
)
413 (defun org-odt-begin-document-body (opt-plist)
414 (org-odt-begin-office-body)
415 (let ((title (plist-get opt-plist
:title
)))
418 (org-odt-format-stylized-paragraph 'title title
))))
422 (insert "\n" org-lparse-toc
"\n")))
424 (defvar org-lparse-body-only
) ; let bound during org-do-lparse
425 (defvar org-lparse-to-buffer
) ; let bound during org-do-lparse
426 (defun org-odt-end-document-body (opt-plist)
427 (unless org-lparse-body-only
428 (org-lparse-insert-tag "</office:text>")
429 (org-lparse-insert-tag "</office:body>")))
431 (defconst org-odt-document-content-header
432 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
433 <office:document-content
434 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
435 xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\"
436 xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\"
437 xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\"
438 xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\"
439 xmlns:fo=\"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0\"
440 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
441 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
442 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
443 xmlns:number=\"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0\"
444 xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\"
445 xmlns:chart=\"urn:oasis:names:tc:opendocument:xmlns:chart:1.0\"
446 xmlns:dr3d=\"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0\"
447 xmlns:math=\"http://www.w3.org/1998/Math/MathML\"
448 xmlns:form=\"urn:oasis:names:tc:opendocument:xmlns:form:1.0\"
449 xmlns:script=\"urn:oasis:names:tc:opendocument:xmlns:script:1.0\"
450 xmlns:ooo=\"http://openoffice.org/2004/office\"
451 xmlns:ooow=\"http://openoffice.org/2004/writer\"
452 xmlns:oooc=\"http://openoffice.org/2004/calc\"
453 xmlns:dom=\"http://www.w3.org/2001/xml-events\"
454 xmlns:xforms=\"http://www.w3.org/2002/xforms\"
455 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
456 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
457 xmlns:rpt=\"http://openoffice.org/2005/report\"
458 xmlns:of=\"urn:oasis:names:tc:opendocument:xmlns:of:1.2\"
459 xmlns:xodt=\"http://www.w3.org/1999/xodt\"
460 xmlns:field=\"urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0\" office:version=\"1.2\">
463 (defun org-odt-begin-document-content (opt-plist)
465 (insert org-odt-document-content-header
)
468 (insert-file-contents
469 (or org-export-odt-automatic-styles-file
470 (expand-file-name "styles/OrgOdtAutomaticStyles.xml"
472 (goto-char (point-max)))
474 (defun org-odt-end-document-content ()
475 (org-lparse-insert-tag "</office:document-content>"))
477 (defun org-odt-begin-outline (level1 snumber title tags
478 target extra-targets class
)
480 'HEADING
(org-lparse-format
481 'HEADLINE title extra-targets tags snumber level1
)
484 (defun org-odt-end-outline ()
487 (defun org-odt-begin-outline-text (level1 snumber class
)
490 (defun org-odt-end-outline-text ()
493 (defun org-odt-begin-paragraph (&optional style
)
494 (org-lparse-insert-tag
495 "<text:p%s>" (org-odt-get-extra-attrs-for-paragraph-style style
)))
497 (defun org-odt-end-paragraph ()
498 (org-lparse-insert-tag "</text:p>"))
500 (defun org-odt-get-extra-attrs-for-paragraph-style (style)
504 ((stringp style
) style
)
505 ((symbolp style
) (org-odt-get-style-name-for-entity
508 (error "Don't know how to handle paragraph style %s" style
))
509 (format " text:style-name=\"%s\"" style-name
)))
511 (defun org-odt-format-stylized-paragraph (style text
)
513 '("<text:p%s>" .
"</text:p>") text
514 (org-odt-get-extra-attrs-for-paragraph-style style
)))
516 (defun org-odt-begin-environment (style)
518 ((blockquote verse center quote
)
519 (org-lparse-begin-paragraph style
)
522 (org-lparse-end-paragraph)
524 (t (error "Unknown environment %s" style
))))
526 (defun org-odt-end-environment (style)
528 ((blockquote verse center quote
)
529 (org-lparse-end-paragraph)
532 (org-lparse-begin-paragraph)
534 (t (error "Unknown environment %s" style
))))
536 (defun org-odt-begin-list (ltype &optional arg1
)
537 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
539 (let* ((style-name (org-odt-get-style-name-for-entity 'list ltype
))
540 (extra (if style-name
541 (format " text:style-name=\"%s\"" style-name
) "")))
543 ;; FIXME: Handle arg1 incase of ordered lists.
545 ((ordered unordered description
)
546 (org-lparse-end-paragraph)
547 (org-lparse-insert-tag "<text:list%s>" extra
))
548 (t (error "Unknown list type: %s" ltype
)))))
550 (defun org-odt-end-list (ltype)
551 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
554 (org-lparse-insert-tag "</text:list>")
555 (error "Unknown list type: %s" ltype
)))
557 (defun org-odt-begin-list-item (ltype &optional arg headline
)
558 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
562 (assert (not headline
) t
)
563 (let* ((counter arg
) (extra ""))
564 (org-lparse-insert-tag "<text:list-item>")
565 (org-lparse-begin-paragraph)))
567 (let* ((id arg
) (extra ""))
568 (org-lparse-insert-tag "<text:list-item>")
569 (org-lparse-begin-paragraph)
570 (insert (if headline
(org-odt-format-target headline id
)
571 (org-odt-format-bookmark "" id
)))))
573 (assert (not headline
) t
)
574 (let ((term (or arg
"(no term)")))
577 '("<text:list-item>" .
"</text:list-item>")
578 (org-odt-format-stylized-paragraph 'definition-term term
)))
579 (org-lparse-begin 'LIST-ITEM
'unordered
)
580 (org-lparse-begin 'LIST
'description
)
581 (org-lparse-begin 'LIST-ITEM
'unordered
)))
582 (t (error "Unknown list type"))))
584 (defun org-odt-end-list-item (ltype)
585 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
589 (org-lparse-insert-tag "</text:list-item>"))
591 (org-lparse-end-list-item)
592 (org-lparse-end 'LIST
'description
)
593 (org-lparse-end-list-item))
594 (t (error "Unknown list type"))))
596 ;; Following variables are let bound when table emission is in
597 ;; progress. See org-lparse.el.
598 (defvar org-lparse-table-begin-marker
)
599 (defvar org-lparse-table-ncols
)
600 (defvar org-lparse-table-rowgrp-open
)
601 (defvar org-lparse-table-rownum
)
602 (defvar org-lparse-table-cur-rowgrp-is-hdr
)
603 (defvar org-lparse-table-is-styled
)
604 (defvar org-lparse-table-rowgrp-info
)
605 (defvar org-lparse-table-colalign-vector
)
606 (defun org-odt-begin-table (caption label attributes
)
609 (org-odt-format-stylized-paragraph
610 'table
(org-odt-format-entity-caption label caption
"Table"))))
612 (org-lparse-insert-tag
613 "<table:table table:name=\"%s\" table:style-name=\"%s\">"
614 (or label
"") "OrgTable")
615 (setq org-lparse-table-begin-marker
(point)))
617 (defun org-odt-end-table ()
618 (goto-char org-lparse-table-begin-marker
)
619 (loop for level from
0 below org-lparse-table-ncols
620 do
(insert (org-odt-format-tags "<table:table-column/>" "")))
622 ;; fill style attributes for table cells
623 (when org-lparse-table-is-styled
624 (while (re-search-forward "@@\\(table-cell:p\\|table-cell:style-name\\)@@\\([0-9]+\\)@@\\([0-9]+\\)@@" nil t
)
625 (let ((spec (match-string 1))
626 (r (string-to-number (match-string 2)))
627 (c (string-to-number (match-string 3))))
629 ((equal spec
"table-cell:p")
630 (let ((style-name (org-odt-get-paragraph-style-for-table-cell r c
)))
631 (replace-match style-name t t
)))
632 ((equal spec
"table-cell:style-name")
633 (let ((style-name (org-odt-get-style-name-for-table-cell r c
)))
634 (replace-match style-name t t
)))))))
636 (goto-char (point-max))
637 (org-lparse-insert-tag "</table:table>"))
639 (defun org-odt-begin-table-rowgroup (&optional is-header-row
)
640 (when org-lparse-table-rowgrp-open
641 (org-lparse-end 'TABLE-ROWGROUP
))
642 (org-lparse-insert-tag (if is-header-row
643 "<table:table-header-rows>"
644 "<table:table-rows>"))
645 (setq org-lparse-table-rowgrp-open t
)
646 (setq org-lparse-table-cur-rowgrp-is-hdr is-header-row
))
648 (defun org-odt-end-table-rowgroup ()
649 (when org-lparse-table-rowgrp-open
650 (setq org-lparse-table-rowgrp-open nil
)
651 (org-lparse-insert-tag
652 (if org-lparse-table-cur-rowgrp-is-hdr
653 "</table:table-header-rows>" "</table:table-rows>"))))
655 (defun org-odt-format-table-row (row)
657 '("<table:table-row>" .
"</table:table-row>") row
))
659 (defun org-odt-get-style-name-for-table-cell (r c
)
664 ((eq (cdr (assoc r org-lparse-table-rowgrp-info
)) :start
) "T")
666 (when (= r org-lparse-table-rownum
) "B")
669 ((or (memq (nth c org-table-colgroup-info
) '(:start
:startend
))
670 (memq (nth (1- c
) org-table-colgroup-info
) '(:end
:startend
))) "L")
673 (defun org-odt-get-paragraph-style-for-table-cell (r c
)
674 (capitalize (aref org-lparse-table-colalign-vector c
)))
676 (defun org-odt-format-table-cell (data r c
)
677 (if (not org-lparse-table-is-styled
)
679 '("<table:table-cell>" .
"</table:table-cell>")
680 (org-odt-format-stylized-paragraph
682 (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
683 ((and (= c
0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS
))
685 (t "OrgTableContents"))
687 (let* ((style-name-cookie
688 (format "@@table-cell:style-name@@%03d@@%03d@@" r c
))
689 (paragraph-style-cookie
692 (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
693 ((and (= c
0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS
))
695 (t "OrgTableContents"))
696 (format "@@table-cell:p@@%03d@@%03d@@" r c
))))
698 '("<table:table-cell table:style-name=\"%s\">" .
699 "</table:table-cell>")
700 (org-odt-format-stylized-paragraph paragraph-style-cookie data
)
701 style-name-cookie
))))
703 (defun org-odt-begin-footnote-definition (n)
704 (org-lparse-begin-paragraph 'footnote
))
706 (defun org-odt-end-footnote-definition (n)
707 (org-lparse-end-paragraph))
709 (defun org-odt-begin-toc (lang-specific-heading)
712 <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
713 <text:table-of-content-source text:outline-level=\"10\">
714 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
715 " lang-specific-heading
))
717 (loop for level from
1 upto
10
720 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
721 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
722 <text:index-entry-chapter/>
723 <text:index-entry-text/>
724 <text:index-entry-link-end/>
725 </text:table-of-content-entry-template>
730 </text:table-of-content-source>
733 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
734 <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
736 " lang-specific-heading
)))
738 (defun org-odt-end-toc ()
741 </text:table-of-content>
744 (defun org-odt-format-toc-entry (snumber todo headline tags href
)
745 (setq headline
(concat
746 (and org-export-with-section-numbers
747 (concat snumber
". "))
751 (org-lparse-format 'SPACES
3)
752 (org-lparse-format 'FONTIFY tags
"tag")))))
754 (setq headline
(org-lparse-format 'FONTIFY headline
"todo")))
756 (let ((org-odt-suppress-xref t
))
757 (org-odt-format-link headline
(concat "#" href
))))
759 (defun org-odt-format-toc-item (toc-entry level org-last-level
)
760 (let ((style (format "Contents_20_%d"
761 (+ level
(or (org-lparse-get 'TOPLEVEL-HLEVEL
) 1) -
1))))
762 (insert "\n" (org-odt-format-stylized-paragraph style toc-entry
) "\n")))
764 ;; Following variable is let bound during 'ORG-LINK callback. See
766 (defvar org-lparse-link-description-is-image nil
)
767 (defun org-odt-format-link (desc href
&optional attr
)
769 ((and (= (string-to-char href
) ?
#) (not org-odt-suppress-xref
))
770 (setq href
(concat org-export-odt-bookmark-prefix
(substring href
1)))
772 '("<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"%s\">" .
773 "</text:bookmark-ref>")
775 (org-lparse-link-description-is-image
777 '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" .
"</draw:a>")
778 desc href
(or attr
"")))
781 '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" .
"</text:a>")
782 desc href
(or attr
"")))))
784 (defun org-odt-format-spaces (n)
785 (org-odt-format-tags "<text:s text:c=\"%d\"/>" "" n
))
787 (defun org-odt-format-tabs (&optional n
)
788 (let ((tab "<text:tab/>")
792 (defun org-odt-format-line-break ()
793 (org-odt-format-tags "<text:line-break/>" ""))
795 (defun org-odt-format-horizontal-line ()
796 (org-odt-format-stylized-paragraph 'horizontal-line
""))
798 (defun org-odt-format-line (line)
799 (case org-lparse-dyn-current-environment
800 (fixedwidth (concat (org-odt-format-source-code-or-example-line
801 (org-xml-encode-plain-text line
)) "\n"))
802 (t (concat line
"\n"))))
804 (defun org-odt-format-comment (fmt &rest args
)
805 (let ((comment (apply 'format fmt args
)))
806 (format "\n<!-- %s -->\n" comment
)))
808 (defun org-odt-format-org-entity (wd)
809 (org-entity-get-representation wd
'utf8
))
811 (defun org-odt-fill-tabs-and-spaces (line)
812 (replace-regexp-in-string
813 "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
815 ((string= s
"\t") (org-odt-format-tabs))
817 (org-odt-format-spaces (length s
)))
820 (defun org-odt-format-source-code-or-example-line (line)
821 (org-odt-format-stylized-paragraph 'src
(org-odt-fill-tabs-and-spaces line
)))
823 (defun org-odt-format-example (lines)
826 (org-odt-format-source-code-or-example-line line
))
827 (org-split-string lines
"[\r\n]") "\n"))
829 (defun org-odt-format-source-code-or-example (lines lang caption textareap
832 (org-odt-format-example (org-export-number-lines
833 (org-xml-encode-plain-text-lines lines
)
834 0 0 num cont rpllbl fmt
)))
836 (defun org-xml-encode-plain-text-lines (rtn)
837 (mapconcat 'org-xml-encode-plain-text
(org-split-string rtn
"[\r\n]") "\n"))
839 (defun org-odt-remap-stylenames (style-name)
841 (cdr (assoc style-name
'(("timestamp-wrapper" .
"OrgTimestampWrapper")
842 ("timestamp" .
"OrgTimestamp")
843 ("timestamp-kwd" .
"OrgTimestampKeyword")
847 ("target" .
"OrgTarget"))))
850 (defun org-odt-format-fontify (text style
&optional id
)
854 (org-odt-remap-stylenames style
))
856 (org-odt-get-style-name-for-entity 'character style
))
858 (assert (< 1 (length style
)))
859 (let ((parent-style (pop style
)))
860 (mapconcat (lambda (s)
861 ;; (assert (stringp s) t)
862 (org-odt-remap-stylenames s
)) style
"")
863 (org-odt-remap-stylenames parent-style
)))
864 (t (error "Don't how to handle style %s" style
)))))
866 '("<text:span text:style-name=\"%s\">" .
"</text:span>")
869 (defun org-odt-relocate-relative-path (path dir
)
870 (if (file-name-absolute-p path
) path
871 (file-relative-name (expand-file-name path dir
)
872 (expand-file-name "eyecandy" dir
))))
874 (defun org-odt-format-inline-image (thefile)
875 (let* ((thelink (if (file-name-absolute-p thefile
) thefile
877 (org-odt-relocate-relative-path
878 thefile org-current-export-file
))))
881 "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
882 (if org-export-odt-embed-images
883 (org-odt-copy-image-file thefile
) thelink
))))
884 (org-export-odt-format-image thefile href
)))
886 (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
889 OPT-PLIST is an options list.
890 TYPE is the device-type of the link (THIS://foo.html)
891 PATH is the path of the link (http://THIS#locationx)
892 FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
893 DESC is the link description, if any.
894 ATTR is a string of other attributes of the a element.
895 MAY-INLINE-P allows inlining it as an image."
897 (declare (special org-lparse-par-open
))
900 (and (member type-1
'("http" "https" "file"))
901 (org-lparse-should-inline-p path descp
)
903 (type (if (equal type-1
"id") "file" type-1
))
908 ;; check for inlined images
909 ((and (member type
'("file"))
912 filename org-odt-export-inline-image-extensions
)
913 (or (eq t org-odt-export-inline-images
)
914 (and org-odt-export-inline-images
(not descp
))))
916 ;; (when (and (string= type "file") (file-name-absolute-p path))
917 ;; (setq thefile (concat "file://" (expand-file-name path))))
918 ;; (setq thefile (org-xml-format-href thefile))
919 ;; (org-export-html-format-image thefile)
920 (org-odt-format-inline-image thefile
))
922 (when (string= type
"file")
925 ((file-name-absolute-p path
)
926 (concat "file://" (expand-file-name path
)))
927 (t (org-odt-relocate-relative-path
928 thefile org-current-export-file
)))))
930 (when (and (member type
'("" "http" "https" "file" "coderef"))
932 (setq thefile
(concat thefile
"#" fragment
)))
934 (setq thefile
(org-xml-format-href thefile
))
936 (when (not (member type
'("" "file" "coderef")))
937 (setq thefile
(concat type
":" thefile
)))
939 (let ((org-odt-suppress-xref (string= type
"coderef")))
941 (org-xml-format-desc desc
) thefile attr
)))))))
943 (defun org-odt-format-heading (text level
&optional id
)
944 (let* ((text (if id
(org-odt-format-target text id
) text
)))
946 '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
947 "</text:h>") text level level
)))
949 (defun org-odt-format-headline (title extra-targets tags
950 &optional snumber level
)
952 (org-lparse-format 'EXTRA-TARGETS extra-targets
)
954 ;; No need to generate section numbers. They are auto-generated by
957 ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
959 (and tags
(concat (org-lparse-format 'SPACES
3)
960 (org-lparse-format 'ORG-TAGS tags
)))))
962 (defun org-odt-format-anchor (text name
&optional class
)
963 (org-odt-format-target text name
))
965 (defun org-odt-format-bookmark (text id
)
967 (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id
)
970 (defun org-odt-format-target (text id
)
971 (let ((name (concat org-export-odt-bookmark-prefix id
)))
973 (and id
(org-odt-format-tags
974 "<text:bookmark-start text:name=\"%s\"/>" "" name
))
975 (org-odt-format-bookmark text id
)
976 (and id
(org-odt-format-tags
977 "<text:bookmark-end text:name=\"%s\"/>" "" name
)))))
979 (defun org-odt-format-footnote (n def
)
980 (let ((id (concat "fn" n
))
981 (note-class "footnote")
982 (par-style "Footnote"))
984 '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
988 '("<text:note-citation>" .
"</text:note-citation>")
991 '("<text:note-body>" .
"</text:note-body>")
995 (defun org-odt-format-footnote-reference (n def refcnt
)
997 (org-odt-format-footnote n def
)
998 (org-odt-format-footnote-ref n
)))
1000 (defun org-odt-format-footnote-ref (n)
1001 (let ((note-class "footnote")
1003 (ref-name (concat "fn" n
)))
1004 (org-odt-format-tags
1005 '("<text:span text:style-name=\"%s\">" .
"</text:span>")
1006 (org-odt-format-tags
1007 '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" .
"</text:note-ref>")
1008 n note-class ref-format ref-name
)
1011 (defun org-odt-get-image-name (file-name)
1015 (concat (sha1 file-name
) "." (file-name-extension file-name
)) "Pictures")))
1017 (defun org-export-odt-format-image (src href
1020 "Create image tag with source and attributes."
1023 (let (embed-as caption attr label attr-plist size width height
)
1026 ((string-match "^ltxpng/" src
)
1027 ;; FIXME: Anyway the latex src can be embedded as an
1030 ;; (org-find-text-property-in-string 'org-latex-src src)
1031 (setq caption nil attr nil label nil embed-as
'character
))
1034 (setq caption
(org-find-text-property-in-string 'org-caption src
)
1035 caption
(and caption
(org-xml-format-desc caption
))
1036 attr
(org-find-text-property-in-string 'org-attributes src
)
1037 label
(org-find-text-property-in-string 'org-label src
)
1038 embed-as
'paragraph
)))
1040 (setq attr-plist
(when attr
(read attr
)))
1041 (setq size
(org-odt-image-size-from-file
1042 src
(plist-get attr-plist
:width
)
1043 (plist-get attr-plist
:height
)
1044 (plist-get attr-plist
:scale
) nil embed-as
))
1046 (org-export-odt-do-format-image embed-as caption attr label
1049 (defun org-export-odt-do-format-image (embed-as caption attr label
1051 "Create image tag with source and attributes."
1053 (let ((width (car size
)) (height (cdr size
))
1055 '("<draw:frame draw:style-name=\"%s\"
1056 text:anchor-type=\"%s\"
1057 draw:z-index=\"%d\" %s>" .
"</draw:frame>")))
1059 ((and (not caption
) (not label
))
1060 (let (style-name anchor-type
)
1062 ((eq embed-as
'paragraph
)
1063 (setq style-name
"OrgGraphicsParagraph" anchor-type
"paragraph"))
1064 ((eq embed-as
'character
)
1065 (setq style-name
"OrgGraphicsBaseline" anchor-type
"as-char")))
1066 (org-odt-format-tags
1067 draw-frame-pair href style-name anchor-type
0
1068 (org-odt-image-attrs-from-size width height
))))
1072 ;; (when par-open (org-odt-close-par))
1073 (org-odt-format-tags
1075 (org-odt-format-tags
1076 '("<draw:text-box fo:min-height=\"%dcm\">" .
"</draw:text-box>")
1077 (org-odt-format-stylized-paragraph
1080 (let ((extra " style:rel-width=\"100%\" style:rel-height=\"scale\""))
1081 (org-odt-format-tags
1082 draw-frame-pair href
"OrgGraphicsParagraphContent" "paragraph" 2
1083 (concat (org-odt-image-attrs-from-size width height
) extra
)))
1084 (org-odt-format-entity-caption label caption
)))
1086 "OrgFrame" "paragraph" 1
1087 (org-odt-image-attrs-from-size width
))
1089 ;; (when par-open (org-odt-open-par))
1092 ;; xml files generated on-the-fly
1093 (defconst org-export-odt-save-list
1094 '("mimetype" "META-INF/manifest.xml" "content.xml" "meta.xml" "styles.xml"))
1096 ;; xml files that are copied
1097 (defconst org-export-odt-nosave-list
'())
1099 ;; xml files that contribute to the final odt file
1100 (defvar org-export-odt-file-list nil
)
1102 (defconst org-export-odt-manifest-lines
1103 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1104 "<manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">"
1105 "<manifest:file-entry manifest:media-type=\"application/vnd.oasis.opendocument.text\" manifest:version=\"1.2\" manifest:full-path=\"/\"/>"
1106 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"content.xml\"/>"
1107 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"styles.xml\"/>"
1108 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"meta.xml\"/>"
1109 "<manifest:file-entry manifest:media-type=\"\" manifest:full-path=\"Pictures/\"/>") .
("</manifest:manifest>")))
1111 (defconst org-export-odt-meta-lines
1112 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1113 "<office:document-meta"
1114 " xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\""
1115 " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
1116 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\""
1117 " xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\""
1118 " xmlns:ooo=\"http://openoffice.org/2004/office\" "
1119 " office:version=\"1.2\">"
1120 " <office:meta>") .
(" </office:meta>" "</office:document-meta>")))
1122 (defun org-odt-copy-image-file (path &optional target-file
)
1123 "Returns the internal name of the file"
1124 (let* ((image-type (file-name-extension path
))
1125 (media-type (format "image/%s" image-type
))
1126 (src-file (expand-file-name
1127 path
(file-name-directory org-current-export-file
)))
1128 (target-file (or target-file
(org-odt-get-image-name src-file
)))
1132 (when (not org-lparse-to-buffer
)
1133 (message "Embedding %s as %s ..."
1134 (substring-no-properties path
) target-file
)
1135 (copy-file src-file target-file
'overwrite
)
1136 (org-odt-update-manifest-file media-type target-file
)
1137 (push target-file org-export-odt-file-list
)) target-file
))
1139 (defun org-odt-image-attrs-from-size (&optional width height
)
1141 (when width
(format "svg:width=\"%0.2fcm\"" width
))
1143 (when height
(format "svg:height=\"%0.2fcm\"" height
))))
1145 (defvar org-export-odt-image-size-probe-method
1146 '(emacs imagemagick force
)
1147 "Ordered list of methods by for determining size of an embedded
1150 (defvar org-export-odt-default-image-sizes-alist
1151 '(("character" .
(5 .
0.4))
1152 ("paragraph" .
(5 .
5)))
1153 "Hardcoded image dimensions one for each of the anchor
1156 (defun org-odt-do-image-size (probe-method file
&optional dpi anchor-type
)
1157 (setq dpi
(or dpi org-export-odt-pixels-per-inch
))
1158 (setq anchor-type
(or anchor-type
"paragraph"))
1159 (flet ((size-in-cms (size-in-pixels)
1160 (flet ((pixels-to-cms (pixels)
1161 (let* ((cms-per-inch 2.54)
1162 (inches (/ pixels dpi
)))
1163 (* cms-per-inch inches
))))
1165 (cons (pixels-to-cms (car size-in-pixels
))
1166 (pixels-to-cms (cdr size-in-pixels
)))))))
1169 (size-in-cms (ignore-errors (image-size (create-image file
) 'pixels
))))
1172 (let ((dim (shell-command-to-string
1173 (format "identify -format \"%%w:%%h\" \"%s\"" file
))))
1174 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim
)
1175 (cons (string-to-number (match-string 1 dim
))
1176 (string-to-number (match-string 2 dim
)))))))
1178 (cdr (assoc-string anchor-type
1179 org-export-odt-default-image-sizes-alist
))))))
1181 (defun org-odt-image-size-from-file (file &optional user-width
1182 user-height scale dpi embed-as
)
1183 (unless (file-name-absolute-p file
)
1184 (setq file
(expand-file-name
1185 file
(file-name-directory org-current-export-file
))))
1186 (let* (size width height
)
1187 (unless (and user-height user-width
)
1188 (loop for probe-method in org-export-odt-image-size-probe-method
1190 do
(setq size
(org-odt-do-image-size
1191 probe-method file dpi embed-as
)))
1192 (or size
(error "Cannot determine Image size. Aborting ..."))
1193 (setq width
(car size
) height
(cdr size
)))
1196 (setq width
(* width scale
) height
(* height scale
)))
1197 ((and user-height user-width
)
1198 (setq width user-width height user-height
))
1200 (setq width
(* user-height
(/ width height
)) height user-height
))
1202 (setq height
(* user-width
(/ height width
)) width user-width
))
1204 (cons width height
)))
1206 (defvar org-odt-default-entity
"Illustration")
1207 (defun org-odt-format-entity-caption (label caption
&optional default-entity
)
1208 (if (not label
) (or caption
"")
1209 (let* ((label-components (org-odt-parse-label label
))
1210 (entity (car label-components
))
1211 (seqno (cdr label-components
))
1212 (caption (and caption
(concat ": " caption
))))
1215 entity
(or default-entity org-odt-default-entity
)))
1218 (org-odt-format-tags
1219 '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" .
"</text:sequence>")
1220 seqno label entity entity
)
1223 (defun org-odt-format-tags (tag text
&rest args
)
1224 (let ((prefix (when org-lparse-encode-pending
"@"))
1225 (suffix (when org-lparse-encode-pending
"@")))
1226 (apply 'org-lparse-format-tags tag text prefix suffix args
)))
1228 (defun org-odt-init-outfile (filename)
1229 (unless (executable-find "zip")
1230 ;; Not at all OSes ship with zip by default
1231 (error "Executable \"zip\" needed for creating OpenDocument files. Aborting."))
1233 (let* ((outdir (make-temp-file org-export-odt-tmpdir-prefix t
))
1234 (mimetype-file (expand-file-name "mimetype" outdir
))
1235 (content-file (expand-file-name "content.xml" outdir
))
1236 (manifest-file (expand-file-name "META-INF/manifest.xml" outdir
))
1237 (meta-file (expand-file-name "meta.xml" outdir
))
1238 (styles-file (expand-file-name "styles.xml" outdir
))
1239 (pictures-dir (expand-file-name "Pictures" outdir
))
1243 (with-current-buffer (find-file-noselect content-file t
)
1246 ;; FIXME: How to factor in body-only here
1249 (make-directory (file-name-directory manifest-file
))
1250 (with-current-buffer (find-file-noselect manifest-file t
)
1252 (insert (mapconcat 'identity
(car org-export-odt-manifest-lines
) "\n"))
1255 (insert (mapconcat 'identity
(cdr org-export-odt-manifest-lines
) "\n"))))
1258 (with-current-buffer (find-file-noselect meta-file t
)
1260 (insert (mapconcat 'identity
(car org-export-odt-meta-lines
) "\n"))
1263 (insert (mapconcat 'identity
(cdr org-export-odt-meta-lines
) "\n"))))
1266 (with-current-buffer (find-file-noselect mimetype-file t
)
1267 (insert "application/vnd.oasis.opendocument.text"))
1270 ;; (copy-file org-export-odt-styles-file styles-file t)
1273 (make-directory pictures-dir
)
1275 ;; initialize list of files that contribute to the odt file
1276 (setq org-export-odt-file-list
1277 (append org-export-odt-save-list org-export-odt-nosave-list
)))
1280 (defconst org-odt-manifest-file-entry-tag
1281 "<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"/>")
1283 (defun org-odt-save-as-outfile (target opt-plist
)
1285 (org-odt-update-meta-file opt-plist
)
1287 ;; write styles file
1288 (org-odt-copy-styles-file)
1290 ;; Update styles.xml - take care of outline numbering
1291 (with-current-buffer
1292 (find-file-noselect (expand-file-name "styles.xml") t
)
1293 ;; Don't make automatic backup of styles.xml file. This setting
1294 ;; prevents the backedup styles.xml file from being zipped in to
1295 ;; odt file. This is more of a hackish fix. Better alternative
1296 ;; would be to fix the zip command so that the output odt file
1297 ;; includes only the needed files and excludes any auto-generated
1298 ;; extra files like backups and auto-saves etc etc. Note that
1299 ;; currently the zip command zips up the entire temp directory so
1300 ;; that any auto-generated files created under the hood ends up in
1301 ;; the resulting odt file.
1302 (set (make-local-variable 'backup-inhibited
) t
)
1304 ;; Import local setting of `org-export-with-section-numbers'
1305 (org-lparse-bind-local-variables opt-plist
)
1306 (org-odt-configure-outline-numbering
1307 (if org-export-with-section-numbers org-export-headline-levels
0)))
1309 (let ((zipdir default-directory
))
1310 (message "Switching to directory %s" (expand-file-name zipdir
))
1312 ;; save all xml files
1313 (mapc (lambda (file)
1314 (with-current-buffer
1315 (find-file-noselect (expand-file-name file
) t
)
1317 (indent-region (point-min) (point-max))
1319 org-export-odt-save-list
)
1321 (let* ((target-name (file-name-nondirectory target
))
1322 (target-dir (file-name-directory target
))
1323 (cmds `(("zip" "-mX0" ,target-name
"mimetype")
1324 ("zip" "-rmTq" ,target-name
"."))))
1325 (when (file-exists-p target
)
1326 ;; FIXME: If the file is locked this throws a cryptic error
1327 (delete-file target
))
1329 (let ((coding-system-for-write 'no-conversion
) exitcode
)
1330 (message "Creating odt file...")
1333 (message "Running %s" (mapconcat 'identity cmd
" "))
1335 (apply 'call-process
(car cmd
) nil nil nil
(cdr cmd
)))
1336 (or (zerop exitcode
)
1337 (error "Unable to create odt file (%S)" exitcode
)))
1340 ;; move the file from outdir to target-dir
1341 (rename-file target-name target-dir
)
1343 ;; kill all xml buffers
1344 (mapc (lambda (file)
1346 (find-file-noselect (expand-file-name file zipdir
) t
)))
1347 org-export-odt-save-list
)
1349 (delete-directory zipdir
)))
1351 (message "Created %s" target
)
1352 (set-buffer (find-file-noselect target t
)))
1354 (defun org-odt-format-date (date)
1356 "OpenDocument files require that dates be in ISO-8601 format. Please review your DATE options for compatibility."))
1357 ;; If the user is not careful with the date specification, an
1358 ;; invalid meta.xml will be emitted.
1360 ;; For now honor user's diktat and let him off with a warning
1361 ;; message. This is OK as LibreOffice (and possibly other
1362 ;; apps) doesn't deem this deviation as critical and continue
1363 ;; to load the file.
1365 ;; FIXME: Surely there a better way to handle this. Revisit this
1368 ((and date
(string-match "%" date
))
1369 ;; Honor user's diktat. See comments above
1370 (org-lparse-warn warning-msg
)
1371 (format-time-string date
))
1373 ;; Honor user's diktat. See comments above
1374 (org-lparse-warn warning-msg
)
1378 (format-time-string "%Y-%m-%dT%T%:z")))))
1380 (defun org-odt-update-meta-file (opt-plist)
1381 (with-current-buffer
1382 (find-file-noselect (expand-file-name "meta.xml") t
)
1383 (let ((date (org-odt-format-date (plist-get opt-plist
:date
)))
1384 (author (or (plist-get opt-plist
:author
) ""))
1385 (email (plist-get opt-plist
:email
))
1386 (keywords (plist-get opt-plist
:keywords
))
1387 (description (plist-get opt-plist
:description
))
1388 (title (plist-get opt-plist
:title
)))
1392 (org-odt-format-tags '("<dc:creator>" .
"</dc:creator>") author
)
1393 (org-odt-format-tags
1394 '("\n<meta:initial-creator>" .
"</meta:initial-creator>") author
)
1395 (org-odt-format-tags '("\n<dc:date>" .
"</dc:date>") date
)
1396 (org-odt-format-tags
1397 '("\n<meta:creation-date>" .
"</meta:creation-date>") date
)
1398 (org-odt-format-tags '("\n<meta:generator>" .
"</meta:generator>")
1399 (when org-export-creator-info
1400 (format "Org-%s/Emacs-%s"
1401 org-version emacs-version
)))
1402 (org-odt-format-tags '("\n<meta:keyword>" .
"</meta:keyword>") keywords
)
1403 (org-odt-format-tags '("\n<dc:subject>" .
"</dc:subject>") description
)
1404 (org-odt-format-tags '("\n<dc:title>" .
"</dc:title>") title
)
1407 (defun org-odt-update-manifest-file (media-type full-path
)
1408 (with-current-buffer
1409 (find-file-noselect (expand-file-name "META-INF/manifest.xml") t
)
1410 (insert (format org-odt-manifest-file-entry-tag media-type full-path
))))
1412 (defun org-odt-finalize-outfile ()
1413 (message "org-newodt: Finalizing outfile")
1414 (org-odt-delete-empty-paragraphs))
1416 (defun org-odt-delete-empty-paragraphs ()
1417 (goto-char (point-min))
1418 (let ((open "<text:p[^>]*>")
1419 (close "</text:p>"))
1420 (while (re-search-forward (format "%s[ \r\n\t]*%s" open close
) nil t
)
1421 (replace-match ""))))
1423 (defun org-odt-get (what &optional opt-plist
)
1426 (EXPORT-DIR (org-export-directory :html opt-plist
))
1427 (FILE-NAME-EXTENSION "odt")
1428 (EXPORT-BUFFER-NAME "*Org ODT Export*")
1429 (ENTITY-CONTROL org-odt-entity-control-callbacks-alist
)
1430 (ENTITY-FORMAT org-odt-entity-format-callbacks-alist
)
1431 (INIT-METHOD 'org-odt-init-outfile
)
1432 (FINAL-METHOD 'org-odt-finalize-outfile
)
1433 (SAVE-METHOD 'org-odt-save-as-outfile
)
1435 '("bib" "doc" "doc6" "doc95" "html" "xhtml" "latex" "odt" "ott" "pdf" "rtf"
1436 "sdw" "sdw3" "sdw4" "stw " "sxw" "mediawiki" "text" "txt" "uot" "vor"
1437 "vor3" "vor4" "docbook" "ooxml" "ppt" "odp"))
1438 (CONVERT-METHOD org-lparse-convert-process
)
1440 (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps
)
1441 (INLINE-IMAGES 'maybe
)
1442 (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
1443 (PLAIN-TEXT-MAP '(("&" .
"&") ("<" .
"<") (">" .
">")))
1444 (TABLE-FIRST-COLUMN-AS-LABELS nil
)
1445 (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY
"," 'superscript
))
1446 (CODING-SYSTEM-FOR-WRITE 'utf-8
)
1447 (CODING-SYSTEM-FOR-SAVE 'utf-8
)
1448 (t (error "Unknown property: %s" what
))))
1450 (defun org-odt-parse-label (label)
1452 (if (not (string-match "\\`[a-zA-Z]+:\\(.+\\)" label
))
1455 (capitalize (substring label
0 (1- (match-beginning 1))))
1456 (substring label
(match-beginning 1))))))
1458 (defvar org-lparse-latex-fragment-fallback
) ; set by org-do-lparse
1459 (defun org-export-odt-preprocess (parameters)
1460 "Convert LaTeX fragments to images."
1461 (when (and org-current-export-file
1462 (plist-get parameters
:LaTeX-fragments
))
1464 (concat "ltxpng/" (file-name-sans-extension
1465 (file-name-nondirectory
1466 org-current-export-file
)))
1467 org-current-export-dir nil
"Creating LaTeX image %s"
1470 ((eq (plist-get parameters
:LaTeX-fragments
) 'verbatim
) 'verbatim
)
1471 ;; Investigate MathToWeb for converting TeX equations to MathML
1472 ;; See http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01755.html
1473 ((or (eq (plist-get parameters
:LaTeX-fragments
) 'mathjax
)
1474 (eq (plist-get parameters
:LaTeX-fragments
) t
))
1477 "Use of MathJax is incompatible with ODT exporter. "
1478 (format "Using %S instead." org-lparse-latex-fragment-fallback
)))
1479 org-lparse-latex-fragment-fallback
)
1480 ((eq (plist-get parameters
:LaTeX-fragments
) 'dvipng
) 'dvipng
)
1482 (goto-char (point-min))
1483 (let (label label-components category value pretty-label
)
1484 (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t
)
1485 (org-if-unprotected-at (match-beginning 1)
1486 (setq label
(match-string 1)
1487 label-components
(org-odt-parse-label label
)
1488 category
(car label-components
)
1489 value
(cdr label-components
)
1490 pretty-label
(if value
(concat category
" " value
) label
))
1492 (let ((org-lparse-encode-pending t
))
1493 (org-odt-format-tags
1494 '("<text:sequence-ref text:reference-format=\"category-and-value\" text:ref-name=\"%s\">"
1495 .
"</text:sequence-ref>") pretty-label label
)) t t
)))))
1497 (declare-function archive-zip-extract
"arc-mode.el" (archive name
))
1498 (defun org-odt-zip-extract-one (archive member
&optional target
)
1500 (let* ((target (or target default-directory
))
1501 (archive (expand-file-name archive
))
1502 (archive-zip-extract
1503 (list "unzip" "-qq" "-o" "-d" target
))
1504 exit-code command-output
)
1505 (setq command-output
1507 (setq exit-code
(archive-zip-extract archive member
))
1509 (unless (zerop exit-code
)
1510 (message command-output
)
1511 (error "Extraction failed"))))
1513 (defun org-odt-zip-extract (archive members
&optional target
)
1514 (when (atom members
) (setq members
(list members
)))
1515 (mapc (lambda (member)
1516 (org-odt-zip-extract-one archive member target
))
1519 (defun org-odt-copy-styles-file (&optional styles-file
)
1520 ;; Non-availability of styles.xml is not a critical error. For now
1521 ;; throw an error purely for aesthetic reasons.
1522 (setq styles-file
(or styles-file
1523 org-export-odt-styles-file
1524 (expand-file-name "styles/OrgOdtStyles.xml"
1526 (error "org-odt: Missing styles file?")))
1528 ((listp styles-file
)
1529 (let ((archive (nth 0 styles-file
))
1530 (members (nth 1 styles-file
)))
1531 (org-odt-zip-extract archive members
)
1534 (when (org-file-image-p member
)
1535 (let* ((image-type (file-name-extension member
))
1536 (media-type (format "image/%s" image-type
)))
1537 (org-odt-update-manifest-file media-type member
))))
1539 ((and (stringp styles-file
) (file-exists-p styles-file
))
1540 (let ((styles-file-type (file-name-extension styles-file
)))
1542 ((string= styles-file-type
"xml")
1543 (copy-file styles-file
"styles.xml" t
))
1544 ((member styles-file-type
'("odt" "ott"))
1545 (org-odt-zip-extract styles-file
"styles.xml")))))
1547 (error (format "Invalid specification of styles.xml file: %S"
1548 org-export-odt-styles-file
)))))
1550 (defvar org-export-odt-factory-settings
1551 "d4328fb9d1b6cb211d4320ff546829f26700dc5e"
1552 "SHA1 hash of OrgOdtStyles.xml.")
1554 (defun org-odt-configure-outline-numbering (level)
1555 "Outline numbering is retained only upto LEVEL.
1556 To disable outline numbering pass a LEVEL of 0."
1557 (if (not (string= org-export-odt-factory-settings
(sha1 (current-buffer))))
1559 "org-odt: Using custom styles file? Consider tweaking styles.xml for better output. To suppress this warning update `org-export-odt-factory-settings'")
1560 (goto-char (point-min))
1562 "<text:outline-level-style\\(.*\\)text:level=\"\\(.*\\)\"\\(.*\\)>")
1564 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1565 (while (re-search-forward regex nil t
)
1566 (when (> (string-to-number (match-string 1)) level
)
1567 (replace-match replacement t nil
))))
1572 ;;; org-odt.el ends here