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")))))
78 (defvar org-odt-file-extensions
79 '(("odt" .
"OpenDocument Text")
80 ("ott" .
"OpenDocument Text Template")
81 ("odm" .
"OpenDocument Master Document")
82 ("ods" .
"OpenDocument Spreadsheet")
83 ("ots" .
"OpenDocument Spreadsheet Template")
84 ("odg" .
"OpenDocument Drawing (Graphics)")
85 ("otg" .
"OpenDocument Drawing Template")
86 ("odp" .
"OpenDocument Presentation")
87 ("otp" .
"OpenDocument Presentation Template")
88 ("odi" .
"OpenDocument Image")
89 ("odf" .
"OpenDocument Formula")
90 ("odc" .
"OpenDocument Chart")
91 ("doc" .
"Microsoft Text")
92 ("docx" .
"Microsoft Text")
93 ("xls" .
"Microsoft Spreadsheet")
94 ("xlsx" .
"Microsoft Spreadsheet")
95 ("ppt" .
"Microsoft Presentation")
96 ("pptx" .
"Microsoft Presentation")))
98 (defvar org-odt-ms-file-extensions
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 ;; RelaxNG validation of OpenDocument xml files
107 (eval-after-load 'rng-nxml
108 '(setq rng-nxml-auto-validate-flag t
))
110 (eval-after-load 'rng-loc
111 '(add-to-list 'rng-schema-locating-files
112 (expand-file-name "etc/schema/schemas.xml" org-odt-data-dir
)))
116 ;; Let Org open all OpenDocument files using system-registered app
117 (add-to-list 'org-file-apps
118 (cons (concat "\\." (car desc
) "\\'") 'system
))
119 ;; Let Emacs open all OpenDocument files in archive mode
120 (add-to-list 'auto-mode-alist
121 (cons (concat "\\." (car desc
) "\\'") 'archive-mode
)))
122 org-odt-file-extensions
)
126 ;; Let Org open all Microsoft files using system-registered app
127 (add-to-list 'org-file-apps
128 (cons (concat "\\." (car desc
) "\\'") 'system
)))
129 org-odt-ms-file-extensions
)
131 ;; register the odt exporter with the pre-processor
132 (add-to-list 'org-export-backends
'odt
)
134 ;; register the odt exporter with org-lparse library
135 (org-lparse-register-backend 'odt
)
137 (defcustom org-export-odt-automatic-styles-file nil
138 "Default style file for use with ODT exporter."
140 :group
'org-export-odt
)
142 ;; TODO: Make configuration user-friendly.
143 (defcustom org-export-odt-styles-file nil
144 "Default style file for use with ODT exporter.
145 Valid values are path to an styles.xml file or a path to a valid
146 *.odt or a *.ott file or a list of the form (FILE (MEMBER1
147 MEMBER2 ...)). In the last case, the specified FILE is unzipped
148 and MEMBER1, MEMBER2 etc are copied in to the generated odt
149 file. The last form is particularly useful if the styles.xml has
150 reference to additional files like header and footer images.
153 :group
'org-export-odt
)
155 (defconst org-export-odt-tmpdir-prefix
"odt-")
156 (defconst org-export-odt-bookmark-prefix
"OrgXref.")
157 (defcustom org-export-odt-use-bookmarks-for-internal-links t
158 "Export Internal links as bookmarks?."
160 :group
'org-export-odt
)
162 (defcustom org-export-odt-embed-images t
163 "Should the images be copied in to the odt file or just linked?"
165 :group
'org-export-odt
)
167 (defcustom org-odt-export-inline-images
'maybe
168 "Non-nil means inline images into exported HTML pages.
169 This is done using an <img> tag. When nil, an anchor with href is used to
170 link to the image. If this option is `maybe', then images in links with
171 an empty description will be inlined, while images with a description will
173 :group
'org-odt-export
174 :type
'(choice (const :tag
"Never" nil
)
175 (const :tag
"Always" t
)
176 (const :tag
"When there is no description" maybe
)))
178 (defcustom org-odt-export-inline-image-extensions
179 '("png" "jpeg" "jpg" "gif")
180 "Extensions of image files that can be inlined into HTML."
181 :type
'(repeat (string :tag
"Extension"))
182 :group
'org-odt-export
)
184 (defcustom org-export-odt-pixels-per-inch display-pixels-per-inch
185 ;; FIXME add docstring
188 :group
'org-export-odt
)
190 (defvar org-export-odt-default-org-styles-alist
191 '((paragraph .
((default .
"Text_20_body")
192 (fixedwidth .
"OrgSourceBlock")
194 (quote .
"Quotations")
195 (blockquote .
"Quotations")
196 (center .
"OrgCenter")
199 (title .
"Heading_20_1.title")
200 (footnote .
"Footnote")
201 (src .
"OrgSourceBlock")
202 (illustration .
"Illustration")
204 (definition-term .
"Text_20_body_20_bold")
205 (horizontal-line .
"Horizontal_20_Line")))
206 (character .
((bold .
"Bold")
207 (emphasis .
"Emphasis")
209 (verbatim .
"OrgCode")
210 (strike .
"Strikethrough")
211 (underline .
"Underline")
212 (subscript .
"OrgSubscript")
213 (superscript .
"OrgSuperscript")))
214 (list .
((ordered .
"OrgNumberedList")
215 (unordered .
"OrgBulletedList")
216 (description .
"OrgDescriptionList"))))
217 "Default styles for various entities.")
219 (defvar org-export-odt-org-styles-alist org-export-odt-default-org-styles-alist
)
220 (defun org-odt-get-style-name-for-entity (category &optional entity
)
221 (let ((entity (or entity
'default
)))
223 (cdr (assoc entity
(cdr (assoc category
224 org-export-odt-org-styles-alist
))))
225 (cdr (assoc entity
(cdr (assoc category
226 org-export-odt-default-org-styles-alist
))))
227 (error "Cannot determine style name for entity %s of type %s"
231 (defun org-export-as-odt-and-open (arg)
232 "Export the outline as ODT and immediately open it with a browser.
233 If there is an active region, export only the region.
234 The prefix ARG specifies how many levels of the outline should become
235 headlines. The default is 3. Lower levels will become bulleted lists."
237 (org-lparse-and-open "odt" "odt" arg
))
240 (defun org-export-as-odt-batch ()
241 "Call the function `org-lparse-batch'.
242 This function can be used in batch processing as:
244 --load=$HOME/lib/emacs/org.el
245 --eval \"(setq org-export-headline-levels 2)\"
246 --visit=MyFile --funcall org-export-as-odt-batch"
247 (org-lparse-batch "odt"))
250 (defun org-export-as-odt-to-buffer (arg)
251 "Call `org-lparse-odt` with output to a temporary buffer.
252 No file is created. The prefix ARG is passed through to `org-lparse-to-buffer'."
254 (org-lparse-to-buffer "odt" arg
))
257 (defun org-replace-region-by-odt (beg end
)
258 "Assume the current region has org-mode syntax, and convert it to ODT.
259 This can be used in any buffer. For example, you could write an
260 itemized list in org-mode syntax in an ODT buffer and then use this
261 command to convert it."
263 (org-replace-region-by "odt" beg end
))
266 (defun org-export-region-as-odt (beg end
&optional body-only buffer
)
267 "Convert region from BEG to END in org-mode buffer to ODT.
268 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
269 contents, and only produce the region of converted text, useful for
270 cut-and-paste operations.
271 If BUFFER is a buffer or a string, use/create that buffer as a target
272 of the converted ODT. If BUFFER is the symbol `string', return the
273 produced ODT as a string and leave not buffer behind. For example,
274 a Lisp program could call this function in the following way:
276 (setq odt (org-export-region-as-odt beg end t 'string))
278 When called interactively, the output buffer is selected, and shown
279 in a window. A non-interactive call will only return the buffer."
281 (org-lparse-region "odt" beg end body-only buffer
))
283 ;;; org-export-as-odt
285 (defun org-export-as-odt (arg &optional hidden ext-plist
286 to-buffer body-only pub-dir
)
287 "Export the outline as a OpenDocumentText file.
288 If there is an active region, export only the region. The prefix
289 ARG specifies how many levels of the outline should become
290 headlines. The default is 3. Lower levels will become bulleted
291 lists. HIDDEN is obsolete and does nothing.
292 EXT-PLIST is a property list with external parameters overriding
293 org-mode's default settings, but still inferior to file-local
294 settings. When TO-BUFFER is non-nil, create a buffer with that
295 name and export to that buffer. If TO-BUFFER is the symbol
296 `string', don't leave any buffer behind but just return the
297 resulting XML as a string. When BODY-ONLY is set, don't produce
298 the file header and footer, simply return the content of
299 <body>...</body>, without even the body tags themselves. When
300 PUB-DIR is set, use this as the publishing directory."
302 (org-lparse "odt" "odt" arg hidden ext-plist to-buffer body-only pub-dir
))
304 (defvar org-odt-entity-control-callbacks-alist
306 .
(org-odt-begin-export org-odt-end-export
))
308 .
(org-odt-begin-document-content org-odt-end-document-content
))
310 .
(org-odt-begin-document-body org-odt-end-document-body
))
312 .
(org-odt-begin-toc org-odt-end-toc
))
314 .
(org-odt-begin-environment org-odt-end-environment
))
316 .
(org-odt-begin-footnote-definition org-odt-end-footnote-definition
))
318 .
(org-odt-begin-table org-odt-end-table
))
320 .
(org-odt-begin-table-rowgroup org-odt-end-table-rowgroup
))
322 .
(org-odt-begin-list org-odt-end-list
))
324 .
(org-odt-begin-list-item org-odt-end-list-item
))
326 .
(org-odt-begin-outline org-odt-end-outline
))
328 .
(org-odt-begin-outline-text org-odt-end-outline-text
))
330 .
(org-odt-begin-paragraph org-odt-end-paragraph
)))
333 (defvar org-odt-entity-format-callbacks-alist
334 `((EXTRA-TARGETS . org-lparse-format-extra-targets
)
335 (ORG-TAGS . org-lparse-format-org-tags
)
336 (SECTION-NUMBER . org-lparse-format-section-number
)
337 (HEADLINE . org-odt-format-headline
)
338 (TOC-ENTRY . org-odt-format-toc-entry
)
339 (TOC-ITEM . org-odt-format-toc-item
)
340 (TAGS . org-odt-format-tags
)
341 (SPACES . org-odt-format-spaces
)
342 (TABS . org-odt-format-tabs
)
343 (LINE-BREAK . org-odt-format-line-break
)
344 (FONTIFY . org-odt-format-fontify
)
345 (TODO . org-lparse-format-todo
)
346 (LINK . org-odt-format-link
)
347 (INLINE-IMAGE . org-odt-format-inline-image
)
348 (ORG-LINK . org-odt-format-org-link
)
349 (HEADING . org-odt-format-heading
)
350 (ANCHOR . org-odt-format-anchor
)
351 (TABLE . org-lparse-format-table
)
352 (TABLE-ROW . org-odt-format-table-row
)
353 (TABLE-CELL . org-odt-format-table-cell
)
354 (FOOTNOTES-SECTION . ignore
)
355 (FOOTNOTE-REFERENCE . org-odt-format-footnote-reference
)
356 (HORIZONTAL-LINE . org-odt-format-horizontal-line
)
357 (COMMENT . org-odt-format-comment
)
358 (LINE . org-odt-format-line
)
359 (ORG-ENTITY . org-odt-format-org-entity
))
363 ;;;_. control callbacks
365 (defun org-odt-begin-office-body ()
369 <text:sequence-decls>
370 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Illustration\"/>
371 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Table\"/>
372 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Text\"/>
373 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Drawing\"/>
374 </text:sequence-decls>"))
376 ;; Following variable is let bound when `org-do-lparse' is in
377 ;; progress. See org-html.el.
378 (defvar org-lparse-toc
)
379 (defun org-odt-begin-document-body (opt-plist)
380 (org-odt-begin-office-body)
381 (let ((title (plist-get opt-plist
:title
)))
384 (org-odt-format-stylized-paragraph 'title title
))))
388 (insert "\n" org-lparse-toc
"\n")))
390 (defvar org-lparse-body-only
) ; let bound during org-do-lparse
391 (defvar org-lparse-to-buffer
) ; let bound during org-do-lparse
392 (defun org-odt-end-document-body (opt-plist)
393 (unless org-lparse-body-only
394 (org-lparse-insert-tag "</office:text>")
395 (org-lparse-insert-tag "</office:body>")))
397 (defconst org-odt-document-content-header
398 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
399 <office:document-content
400 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
401 xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\"
402 xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\"
403 xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\"
404 xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\"
405 xmlns:fo=\"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0\"
406 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
407 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
408 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
409 xmlns:number=\"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0\"
410 xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\"
411 xmlns:chart=\"urn:oasis:names:tc:opendocument:xmlns:chart:1.0\"
412 xmlns:dr3d=\"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0\"
413 xmlns:math=\"http://www.w3.org/1998/Math/MathML\"
414 xmlns:form=\"urn:oasis:names:tc:opendocument:xmlns:form:1.0\"
415 xmlns:script=\"urn:oasis:names:tc:opendocument:xmlns:script:1.0\"
416 xmlns:ooo=\"http://openoffice.org/2004/office\"
417 xmlns:ooow=\"http://openoffice.org/2004/writer\"
418 xmlns:oooc=\"http://openoffice.org/2004/calc\"
419 xmlns:dom=\"http://www.w3.org/2001/xml-events\"
420 xmlns:xforms=\"http://www.w3.org/2002/xforms\"
421 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
422 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
423 xmlns:rpt=\"http://openoffice.org/2005/report\"
424 xmlns:of=\"urn:oasis:names:tc:opendocument:xmlns:of:1.2\"
425 xmlns:xodt=\"http://www.w3.org/1999/xodt\"
426 xmlns:field=\"urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0\" office:version=\"1.2\">
429 (defun org-odt-begin-document-content (opt-plist)
431 (insert org-odt-document-content-header
)
434 (insert-file-contents
435 (or org-export-odt-automatic-styles-file
436 (expand-file-name "styles/OrgOdtAutomaticStyles.xml"
438 (goto-char (point-max)))
440 (defun org-odt-end-document-content ()
441 (org-lparse-insert-tag "</office:document-content>"))
443 (defun org-odt-begin-outline (level1 snumber title tags
444 target extra-targets class
)
446 'HEADING
(org-lparse-format
447 'HEADLINE title extra-targets tags snumber level1
)
450 (defun org-odt-end-outline ()
453 (defun org-odt-begin-outline-text (level1 snumber class
)
456 (defun org-odt-end-outline-text ()
459 (defun org-odt-begin-paragraph (&optional style
)
460 (org-lparse-insert-tag
461 "<text:p%s>" (org-odt-get-extra-attrs-for-paragraph-style style
)))
463 (defun org-odt-end-paragraph ()
464 (org-lparse-insert-tag "</text:p>"))
466 (defun org-odt-get-extra-attrs-for-paragraph-style (style)
470 ((stringp style
) style
)
471 ((symbolp style
) (org-odt-get-style-name-for-entity
474 (error "Don't know how to handle paragraph style %s" style
))
475 (format " text:style-name=\"%s\"" style-name
)))
477 (defun org-odt-format-stylized-paragraph (style text
)
479 '("<text:p%s>" .
"</text:p>") text
480 (org-odt-get-extra-attrs-for-paragraph-style style
)))
482 (defun org-odt-begin-environment (style)
484 ((blockquote verse center quote
)
485 (org-lparse-begin-paragraph style
)
488 (org-lparse-end-paragraph)
490 (t (error "Unknown environment %s" style
))))
492 (defun org-odt-end-environment (style)
494 ((blockquote verse center quote
)
495 (org-lparse-end-paragraph)
498 (org-lparse-begin-paragraph)
500 (t (error "Unknown environment %s" style
))))
502 (defun org-odt-begin-list (ltype &optional arg1
)
503 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
505 (let* ((style-name (org-odt-get-style-name-for-entity 'list ltype
))
506 (extra (if style-name
507 (format " text:style-name=\"%s\"" style-name
) "")))
509 ;; FIXME: Handle arg1 incase of ordered lists.
511 ((ordered unordered description
)
512 (org-lparse-end-paragraph)
513 (org-lparse-insert-tag "<text:list%s>" extra
))
514 (t (error "Unknown list type: %s" ltype
)))))
516 (defun org-odt-end-list (ltype)
517 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
520 (org-lparse-insert-tag "</text:list>")
521 (error "Unknown list type: %s" ltype
)))
523 (defun org-odt-begin-list-item (ltype &optional arg headline
)
524 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
528 (assert (not headline
) t
)
529 (let* ((counter arg
) (extra ""))
530 (org-lparse-insert-tag "<text:list-item>")
531 (org-lparse-begin-paragraph)))
533 (let* ((id arg
) (extra ""))
534 (org-lparse-insert-tag "<text:list-item>")
535 (org-lparse-begin-paragraph)
536 (insert (if headline
(org-odt-format-target headline id
)
537 (org-odt-format-bookmark "" id
)))))
539 (assert (not headline
) t
)
540 (let ((term (or arg
"(no term)")))
543 '("<text:list-item>" .
"</text:list-item>")
544 (org-odt-format-stylized-paragraph 'definition-term term
)))
545 (org-lparse-begin 'LIST-ITEM
'unordered
)
546 (org-lparse-begin 'LIST
'description
)
547 (org-lparse-begin 'LIST-ITEM
'unordered
)))
548 (t (error "Unknown list type"))))
550 (defun org-odt-end-list-item (ltype)
551 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
555 (org-lparse-insert-tag "</text:list-item>"))
557 (org-lparse-end-list-item)
558 (org-lparse-end 'LIST
'description
)
559 (org-lparse-end-list-item))
560 (t (error "Unknown list type"))))
562 ;; Following variables are let bound when table emission is in
563 ;; progress. See org-lparse.el.
564 (defvar org-lparse-table-begin-marker
)
565 (defvar org-lparse-table-ncols
)
566 (defvar org-lparse-table-rowgrp-open
)
567 (defvar org-lparse-table-rownum
)
568 (defvar org-lparse-table-cur-rowgrp-is-hdr
)
569 (defvar org-lparse-table-is-styled
)
570 (defvar org-lparse-table-rowgrp-info
)
571 (defvar org-lparse-table-colalign-vector
)
572 (defun org-odt-begin-table (caption label attributes
)
575 (org-odt-format-stylized-paragraph
576 'table
(org-odt-format-entity-caption label caption
"Table"))))
578 (org-lparse-insert-tag
579 "<table:table table:name=\"%s\" table:style-name=\"%s\">"
580 (or label
"") "OrgTable")
581 (setq org-lparse-table-begin-marker
(point)))
583 (defun org-odt-end-table ()
584 (goto-char org-lparse-table-begin-marker
)
585 (loop for level from
0 below org-lparse-table-ncols
586 do
(insert (org-odt-format-tags "<table:table-column/>" "")))
588 ;; fill style attributes for table cells
589 (when org-lparse-table-is-styled
590 (while (re-search-forward "@@\\(table-cell:p\\|table-cell:style-name\\)@@\\([0-9]+\\)@@\\([0-9]+\\)@@" nil t
)
591 (let ((spec (match-string 1))
592 (r (string-to-number (match-string 2)))
593 (c (string-to-number (match-string 3))))
595 ((equal spec
"table-cell:p")
596 (let ((style-name (org-odt-get-paragraph-style-for-table-cell r c
)))
597 (replace-match style-name t t
)))
598 ((equal spec
"table-cell:style-name")
599 (let ((style-name (org-odt-get-style-name-for-table-cell r c
)))
600 (replace-match style-name t t
)))))))
602 (goto-char (point-max))
603 (org-lparse-insert-tag "</table:table>"))
605 (defun org-odt-begin-table-rowgroup (&optional is-header-row
)
606 (when org-lparse-table-rowgrp-open
607 (org-lparse-end 'TABLE-ROWGROUP
))
608 (org-lparse-insert-tag (if is-header-row
609 "<table:table-header-rows>"
610 "<table:table-rows>"))
611 (setq org-lparse-table-rowgrp-open t
)
612 (setq org-lparse-table-cur-rowgrp-is-hdr is-header-row
))
614 (defun org-odt-end-table-rowgroup ()
615 (when org-lparse-table-rowgrp-open
616 (setq org-lparse-table-rowgrp-open nil
)
617 (org-lparse-insert-tag
618 (if org-lparse-table-cur-rowgrp-is-hdr
619 "</table:table-header-rows>" "</table:table-rows>"))))
621 (defun org-odt-format-table-row (row)
623 '("<table:table-row>" .
"</table:table-row>") row
))
625 (defun org-odt-get-style-name-for-table-cell (r c
)
630 ((eq (cdr (assoc r org-lparse-table-rowgrp-info
)) :start
) "T")
632 (when (= r org-lparse-table-rownum
) "B")
635 ((or (memq (nth c org-table-colgroup-info
) '(:start
:startend
))
636 (memq (nth (1- c
) org-table-colgroup-info
) '(:end
:startend
))) "L")
639 (defun org-odt-get-paragraph-style-for-table-cell (r c
)
640 (capitalize (aref org-lparse-table-colalign-vector c
)))
642 (defun org-odt-format-table-cell (data r c
)
643 (if (not org-lparse-table-is-styled
)
645 '("<table:table-cell>" .
"</table:table-cell>")
646 (org-odt-format-stylized-paragraph
648 (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
649 ((and (= c
0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS
))
651 (t "OrgTableContents"))
653 (let* ((style-name-cookie
654 (format "@@table-cell:style-name@@%03d@@%03d@@" r c
))
655 (paragraph-style-cookie
658 (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
659 ((and (= c
0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS
))
661 (t "OrgTableContents"))
662 (format "@@table-cell:p@@%03d@@%03d@@" r c
))))
664 '("<table:table-cell table:style-name=\"%s\">" .
665 "</table:table-cell>")
666 (org-odt-format-stylized-paragraph paragraph-style-cookie data
)
667 style-name-cookie
))))
669 (defun org-odt-begin-footnote-definition (n)
670 (org-lparse-begin-paragraph 'footnote
))
672 (defun org-odt-end-footnote-definition (n)
673 (org-lparse-end-paragraph))
675 (defun org-odt-begin-toc (lang-specific-heading)
678 <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
679 <text:table-of-content-source text:outline-level=\"10\">
680 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
681 " lang-specific-heading
))
683 (loop for level from
1 upto
10
686 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
687 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
688 <text:index-entry-chapter/>
689 <text:index-entry-text/>
690 <text:index-entry-link-end/>
691 </text:table-of-content-entry-template>
695 </text:table-of-content-source>
698 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
699 <text:p text:style-name=\"Contents_20_Heading\">Table of Contents</text:p>
703 (defun org-odt-end-toc ()
706 </text:table-of-content>
709 (defun org-odt-format-toc-entry (snumber todo headline tags href
)
710 (setq headline
(concat
711 (and org-export-with-section-numbers
712 (concat snumber
". "))
716 (org-lparse-format 'SPACES
3)
717 (org-lparse-format 'FONTIFY tags
"tag")))))
719 (setq headline
(org-lparse-format 'FONTIFY headline
"todo")))
721 (let ((org-odt-suppress-xref t
))
722 (org-odt-format-link headline
(concat "#" href
))))
724 (defun org-odt-format-toc-item (toc-entry level org-last-level
)
725 (let ((style (format "Contents_20_%d"
726 (+ level
(or (org-lparse-get 'TOPLEVEL-HLEVEL
) 1) -
1))))
727 (insert "\n" (org-odt-format-stylized-paragraph style toc-entry
) "\n")))
729 ;; Following variable is let bound during 'ORG-LINK callback. See
731 (defvar org-lparse-link-description-is-image nil
)
732 (defun org-odt-format-link (desc href
&optional attr
)
734 ((and (= (string-to-char href
) ?
#) (not org-odt-suppress-xref
))
735 (setq href
(concat org-export-odt-bookmark-prefix
(substring href
1)))
737 '("<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"%s\">" .
738 "</text:bookmark-ref>")
740 (org-lparse-link-description-is-image
742 '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" .
"</draw:a>")
743 desc href
(or attr
"")))
746 '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" .
"</text:a>")
747 desc href
(or attr
"")))))
749 (defun org-odt-format-spaces (n)
750 (org-odt-format-tags "<text:s text:c=\"%d\"/>" "" n
))
752 (defun org-odt-format-tabs (&optional n
)
753 (let ((tab "<text:tab/>")
757 (defun org-odt-format-line-break ()
758 (org-odt-format-tags "<text:line-break/>" ""))
760 (defun org-odt-format-horizontal-line ()
761 (org-odt-format-stylized-paragraph 'horizontal-line
""))
763 (defun org-odt-format-line (line)
764 (case org-lparse-dyn-current-environment
765 (fixedwidth (concat (org-odt-format-source-code-or-example-line
766 (org-xml-encode-plain-text line
)) "\n"))
767 (t (concat line
"\n"))))
769 (defun org-odt-format-comment (fmt &rest args
)
770 (let ((comment (apply 'format fmt args
)))
771 (format "\n<!-- %s -->\n" comment
)))
773 (defun org-odt-format-org-entity (wd)
774 (org-entity-get-representation wd
'utf8
))
776 (defun org-odt-fill-tabs-and-spaces (line)
777 (replace-regexp-in-string
778 "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
780 ((string= s
"\t") (org-odt-format-tabs))
782 (org-odt-format-spaces (length s
)))
785 (defun org-odt-format-source-code-or-example-line (line)
786 (org-odt-format-stylized-paragraph 'src
(org-odt-fill-tabs-and-spaces line
)))
788 (defun org-odt-format-example (lines)
791 (org-odt-format-source-code-or-example-line line
))
792 (org-split-string lines
"[\r\n]") "\n"))
794 (defun org-odt-format-source-code-or-example (lines lang caption textareap
797 (org-odt-format-example (org-export-number-lines
798 (org-xml-encode-plain-text-lines lines
)
799 0 0 num cont rpllbl fmt
)))
801 (defun org-xml-encode-plain-text-lines (rtn)
802 (mapconcat 'org-xml-encode-plain-text
(org-split-string rtn
"[\r\n]") "\n"))
804 (defun org-odt-remap-stylenames (style-name)
806 (cdr (assoc style-name
'(("timestamp-wrapper" .
"OrgTimestampWrapper")
807 ("timestamp" .
"OrgTimestamp")
808 ("timestamp-kwd" .
"OrgTimestampKeyword")
812 ("target" .
"OrgTarget"))))
815 (defun org-odt-format-fontify (text style
&optional id
)
819 (org-odt-remap-stylenames style
))
821 (org-odt-get-style-name-for-entity 'character style
))
823 (assert (< 1 (length style
)))
824 (let ((parent-style (pop style
)))
825 (mapconcat (lambda (s)
826 ;; (assert (stringp s) t)
827 (org-odt-remap-stylenames s
)) style
"")
828 (org-odt-remap-stylenames parent-style
)))
829 (t (error "Don't how to handle style %s" style
)))))
831 '("<text:span text:style-name=\"%s\">" .
"</text:span>")
834 (defun org-odt-relocate-relative-path (path dir
)
835 (if (file-name-absolute-p path
) path
836 (file-relative-name (expand-file-name path dir
)
837 (expand-file-name "eyecandy" dir
))))
839 (defun org-odt-format-inline-image (thefile)
840 (let* ((thelink (if (file-name-absolute-p thefile
) thefile
842 (org-odt-relocate-relative-path
843 thefile org-current-export-file
))))
846 "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
847 (if org-export-odt-embed-images
848 (org-odt-copy-image-file thefile
) thelink
))))
849 (org-export-odt-format-image thefile href
)))
851 (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
854 OPT-PLIST is an options list.
855 TYPE is the device-type of the link (THIS://foo.html)
856 PATH is the path of the link (http://THIS#locationx)
857 FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
858 DESC is the link description, if any.
859 ATTR is a string of other attributes of the a element.
860 MAY-INLINE-P allows inlining it as an image."
862 (declare (special org-lparse-par-open
))
865 (and (member type-1
'("http" "https" "file"))
866 (org-lparse-should-inline-p path descp
)
868 (type (if (equal type-1
"id") "file" type-1
))
873 ;; check for inlined images
874 ((and (member type
'("file"))
877 filename org-odt-export-inline-image-extensions
)
878 (or (eq t org-odt-export-inline-images
)
879 (and org-odt-export-inline-images
(not descp
))))
881 ;; (when (and (string= type "file") (file-name-absolute-p path))
882 ;; (setq thefile (concat "file://" (expand-file-name path))))
883 ;; (setq thefile (org-xml-format-href thefile))
884 ;; (org-export-html-format-image thefile)
885 (org-odt-format-inline-image thefile
))
887 (when (string= type
"file")
890 ((file-name-absolute-p path
)
891 (concat "file://" (expand-file-name path
)))
892 (t (org-odt-relocate-relative-path
893 thefile org-current-export-file
)))))
895 (when (and (member type
'("" "http" "https" "file" "coderef"))
897 (setq thefile
(concat thefile
"#" fragment
)))
899 (setq thefile
(org-xml-format-href thefile
))
901 (when (not (member type
'("" "file" "coderef")))
902 (setq thefile
(concat type
":" thefile
)))
904 (let ((org-odt-suppress-xref (string= type
"coderef")))
906 (org-xml-format-desc desc
) thefile attr
)))))))
908 (defun org-odt-format-heading (text level
&optional id
)
909 (let* ((text (if id
(org-odt-format-target text id
) text
)))
911 '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
912 "</text:h>") text level level
)))
914 (defun org-odt-format-headline (title extra-targets tags
915 &optional snumber level
)
917 (org-lparse-format 'EXTRA-TARGETS extra-targets
)
919 ;; No need to generate section numbers. They are auto-generated by
922 ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
924 (and tags
(concat (org-lparse-format 'SPACES
3)
925 (org-lparse-format 'ORG-TAGS tags
)))))
927 (defun org-odt-format-anchor (text name
&optional class
)
928 (org-odt-format-target text name
))
930 (defun org-odt-format-bookmark (text id
)
932 (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id
)
935 (defun org-odt-format-target (text id
)
936 (let ((name (concat org-export-odt-bookmark-prefix id
)))
938 (and id
(org-odt-format-tags
939 "<text:bookmark-start text:name=\"%s\"/>" "" name
))
940 (org-odt-format-bookmark text id
)
941 (and id
(org-odt-format-tags
942 "<text:bookmark-end text:name=\"%s\"/>" "" name
)))))
944 (defun org-odt-format-footnote (n def
)
945 (let ((id (concat "fn" n
))
946 (note-class "footnote")
947 (par-style "Footnote"))
949 '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
953 '("<text:note-citation>" .
"</text:note-citation>")
956 '("<text:note-body>" .
"</text:note-body>")
960 (defun org-odt-format-footnote-reference (n def refcnt
)
962 (org-odt-format-footnote n def
)
963 (org-odt-format-footnote-ref n
)))
965 (defun org-odt-format-footnote-ref (n)
966 (let ((note-class "footnote")
968 (ref-name (concat "fn" n
)))
970 '("<text:span text:style-name=\"%s\">" .
"</text:span>")
972 '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" .
"</text:note-ref>")
973 n note-class ref-format ref-name
)
976 (defun org-odt-get-image-name (file-name)
980 (concat (sha1 file-name
) "." (file-name-extension file-name
)) "Pictures")))
982 (defun org-export-odt-format-image (src href
985 "Create image tag with source and attributes."
988 (let (embed-as caption attr label attr-plist size width height
)
991 ((string-match "^ltxpng/" src
)
992 ;; FIXME: Anyway the latex src can be embedded as an
995 ;; (org-find-text-property-in-string 'org-latex-src src)
996 (setq caption nil attr nil label nil embed-as
'character
))
999 (setq caption
(org-find-text-property-in-string 'org-caption src
)
1000 caption
(and caption
(org-xml-format-desc caption
))
1001 attr
(org-find-text-property-in-string 'org-attributes src
)
1002 label
(org-find-text-property-in-string 'org-label src
)
1003 embed-as
'paragraph
)))
1005 (setq attr-plist
(when attr
(read attr
)))
1006 (setq size
(org-odt-image-size-from-file
1007 src
(plist-get attr-plist
:width
)
1008 (plist-get attr-plist
:height
)
1009 (plist-get attr-plist
:scale
) nil embed-as
))
1011 (org-export-odt-do-format-image embed-as caption attr label
1014 (defun org-export-odt-do-format-image (embed-as caption attr label
1016 "Create image tag with source and attributes."
1018 (let ((width (car size
)) (height (cdr size
))
1020 '("<draw:frame draw:style-name=\"%s\"
1021 text:anchor-type=\"%s\"
1022 draw:z-index=\"%d\" %s>" .
"</draw:frame>")))
1024 ((and (not caption
) (not label
))
1025 (let (style-name anchor-type
)
1027 ((eq embed-as
'paragraph
)
1028 (setq style-name
"OrgGraphicsParagraph" anchor-type
"paragraph"))
1029 ((eq embed-as
'character
)
1030 (setq style-name
"OrgGraphicsBaseline" anchor-type
"as-char")))
1031 (org-odt-format-tags
1032 draw-frame-pair href style-name anchor-type
0
1033 (org-odt-image-attrs-from-size width height
))))
1037 ;; (when par-open (org-odt-close-par))
1038 (org-odt-format-tags
1040 (org-odt-format-tags
1041 '("<draw:text-box fo:min-height=\"%dcm\">" .
"</draw:text-box>")
1042 (org-odt-format-stylized-paragraph
1045 (let ((extra " style:rel-width=\"100%\" style:rel-height=\"scale\""))
1046 (org-odt-format-tags
1047 draw-frame-pair href
"OrgGraphicsParagraphContent" "paragraph" 2
1048 (concat (org-odt-image-attrs-from-size width height
) extra
)))
1049 (org-odt-format-entity-caption label caption
)))
1051 "OrgFrame" "paragraph" 1
1052 (org-odt-image-attrs-from-size width
))
1054 ;; (when par-open (org-odt-open-par))
1057 ;; xml files generated on-the-fly
1058 (defconst org-export-odt-save-list
1059 '("META-INF/manifest.xml" "content.xml" "meta.xml" "styles.xml"))
1061 ;; xml files that are copied
1062 (defconst org-export-odt-nosave-list
'())
1064 ;; xml files that contribute to the final odt file
1065 (defvar org-export-odt-file-list nil
)
1067 (defconst org-export-odt-manifest-lines
1068 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1069 "<manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">"
1070 "<manifest:file-entry manifest:media-type=\"application/vnd.oasis.opendocument.text\" manifest:version=\"1.2\" manifest:full-path=\"/\"/>"
1071 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"content.xml\"/>"
1072 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"styles.xml\"/>"
1073 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"meta.xml\"/>"
1074 "<manifest:file-entry manifest:media-type=\"\" manifest:full-path=\"Pictures/\"/>") .
("</manifest:manifest>")))
1076 (defconst org-export-odt-meta-lines
1077 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1078 "<office:document-meta"
1079 " xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\""
1080 " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
1081 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\""
1082 " xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\""
1083 " xmlns:ooo=\"http://openoffice.org/2004/office\" "
1084 " office:version=\"1.2\">"
1085 " <office:meta>") .
(" </office:meta>" "</office:document-meta>")))
1087 (defun org-odt-copy-image-file (path &optional target-file
)
1088 "Returns the internal name of the file"
1089 (let* ((image-type (file-name-extension path
))
1090 (media-type (format "image/%s" image-type
))
1091 (src-file (expand-file-name
1092 path
(file-name-directory org-current-export-file
)))
1093 (target-file (or target-file
(org-odt-get-image-name src-file
)))
1097 (when (not org-lparse-to-buffer
)
1098 (message "Embedding %s as %s ..."
1099 (substring-no-properties path
) target-file
)
1100 (copy-file src-file target-file
'overwrite
)
1101 (org-odt-update-manifest-file media-type target-file
)
1102 (push target-file org-export-odt-file-list
)) target-file
))
1104 (defun org-odt-image-attrs-from-size (&optional width height
)
1106 (when width
(format "svg:width=\"%0.2fcm\"" width
))
1108 (when height
(format "svg:height=\"%0.2fcm\"" height
))))
1110 (defvar org-export-odt-image-size-probe-method
1111 '(emacs imagemagick force
)
1112 "Ordered list of methods by for determining size of an embedded
1115 (defvar org-export-odt-default-image-sizes-alist
1116 '(("character" .
(5 .
0.4))
1117 ("paragraph" .
(5 .
5)))
1118 "Hardcoded image dimensions one for each of the anchor
1121 (defun org-odt-do-image-size (probe-method file
&optional dpi anchor-type
)
1122 (setq dpi
(or dpi org-export-odt-pixels-per-inch
))
1123 (setq anchor-type
(or anchor-type
"paragraph"))
1124 (flet ((size-in-cms (size-in-pixels)
1125 (flet ((pixels-to-cms (pixels)
1126 (let* ((cms-per-inch 2.54)
1127 (inches (/ pixels dpi
)))
1128 (* cms-per-inch inches
))))
1130 (cons (pixels-to-cms (car size-in-pixels
))
1131 (pixels-to-cms (cdr size-in-pixels
)))))))
1134 (size-in-cms (ignore-errors (image-size (create-image file
) 'pixels
))))
1137 (let ((dim (shell-command-to-string
1138 (format "identify -format \"%%w:%%h\" \"%s\"" file
))))
1139 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim
)
1140 (cons (string-to-number (match-string 1 dim
))
1141 (string-to-number (match-string 2 dim
)))))))
1143 (cdr (assoc-string anchor-type
1144 org-export-odt-default-image-sizes-alist
))))))
1146 (defun org-odt-image-size-from-file (file &optional user-width
1147 user-height scale dpi embed-as
)
1148 (unless (file-name-absolute-p file
)
1149 (setq file
(expand-file-name
1150 file
(file-name-directory org-current-export-file
))))
1151 (let* (size width height
)
1152 (unless (and user-height user-width
)
1153 (loop for probe-method in org-export-odt-image-size-probe-method
1155 do
(setq size
(org-odt-do-image-size
1156 probe-method file dpi embed-as
)))
1157 (or size
(error "Cannot determine Image size. Aborting ..."))
1158 (setq width
(car size
) height
(cdr size
)))
1161 (setq width
(* width scale
) height
(* height scale
)))
1162 ((and user-height user-width
)
1163 (setq width user-width height user-height
))
1165 (setq width
(* user-height
(/ width height
)) height user-height
))
1167 (setq height
(* user-width
(/ height width
)) width user-width
))
1169 (cons width height
)))
1171 (defvar org-odt-default-entity
"Illustration")
1172 (defun org-odt-format-entity-caption (label caption
&optional default-entity
)
1173 (if (not label
) (or caption
"")
1174 (let* ((label-components (org-odt-parse-label label
))
1175 (entity (car label-components
))
1176 (seqno (cdr label-components
))
1177 (caption (and caption
(concat ": " caption
))))
1180 entity
(or default-entity org-odt-default-entity
)))
1183 (org-odt-format-tags
1184 '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" .
"</text:sequence>")
1185 seqno label entity entity
)
1188 (defun org-odt-format-tags (tag text
&rest args
)
1189 (let ((prefix (when org-lparse-encode-pending
"@"))
1190 (suffix (when org-lparse-encode-pending
"@")))
1191 (apply 'org-lparse-format-tags tag text prefix suffix args
)))
1193 (defun org-odt-init-outfile (filename)
1194 (let* ((outdir (make-temp-file org-export-odt-tmpdir-prefix t
))
1195 (mimetype-file (expand-file-name "mimetype" outdir
))
1196 (content-file (expand-file-name "content.xml" outdir
))
1197 (manifest-file (expand-file-name "META-INF/manifest.xml" outdir
))
1198 (meta-file (expand-file-name "meta.xml" outdir
))
1199 (styles-file (expand-file-name "styles.xml" outdir
))
1200 (pictures-dir (expand-file-name "Pictures" outdir
))
1204 (with-current-buffer (find-file-noselect content-file t
)
1207 ;; FIXME: How to factor in body-only here
1210 (make-directory (file-name-directory manifest-file
))
1211 (with-current-buffer (find-file-noselect manifest-file t
)
1213 (insert (mapconcat 'identity
(car org-export-odt-manifest-lines
) "\n"))
1216 (insert (mapconcat 'identity
(cdr org-export-odt-manifest-lines
) "\n"))))
1219 (with-current-buffer (find-file-noselect meta-file t
)
1221 (insert (mapconcat 'identity
(car org-export-odt-meta-lines
) "\n"))
1224 (insert (mapconcat 'identity
(cdr org-export-odt-meta-lines
) "\n"))))
1227 ;; (copy-file org-export-odt-styles-file styles-file t)
1230 (make-directory pictures-dir
)
1232 ;; initialize list of files that contribute to the odt file
1233 (setq org-export-odt-file-list
1234 (append org-export-odt-save-list org-export-odt-nosave-list
)))
1237 (defconst org-export-odt-mimetype-lines
1238 '("application/vnd.oasis.opendocument.text"))
1240 (defconst org-odt-manifest-file-entry-tag
1241 "<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"/>")
1243 (defun org-odt-save-as-outfile (target opt-plist
)
1245 (org-odt-update-meta-file opt-plist
)
1247 ;; write styles file
1248 (org-odt-copy-styles-file)
1250 ;; Update styles.xml - take care of outline numbering
1251 (with-current-buffer
1252 (find-file-noselect (expand-file-name "styles.xml") t
)
1253 ;; Don't make automatic backup of styles.xml file. This setting
1254 ;; prevents the backedup styles.xml file from being zipped in to
1255 ;; odt file. This is more of a hackish fix. Better alternative
1256 ;; would be to fix the zip command so that the output odt file
1257 ;; includes only the needed files and excludes any auto-generated
1258 ;; extra files like backups and auto-saves etc etc. Note that
1259 ;; currently the zip command zips up the entire temp directory so
1260 ;; that any auto-generated files created under the hood ends up in
1261 ;; the resulting odt file.
1262 (set (make-local-variable 'backup-inhibited
) t
)
1264 ;; Import local setting of `org-export-with-section-numbers'
1265 (org-lparse-bind-local-variables opt-plist
)
1266 (org-odt-configure-outline-numbering
1267 (if org-export-with-section-numbers org-export-headline-levels
0)))
1269 (let ((zipdir default-directory
))
1270 (message "Switching to directory %s" (expand-file-name zipdir
))
1272 ;; save all xml files
1273 (mapc (lambda (file)
1274 (with-current-buffer
1275 (find-file-noselect (expand-file-name file
) t
)
1277 (indent-region (point-min) (point-max))
1279 org-export-odt-save-list
)
1281 (let* ((target-name (file-name-nondirectory target
))
1282 (target-dir (file-name-directory target
))
1283 (cmd (format "zip -rmTq %s %s" target-name
".")))
1284 (when (file-exists-p target
)
1285 ;; FIXME: If the file is locked this throws a cryptic error
1286 (delete-file target
))
1288 (let ((coding-system-for-write 'no-conversion
) exitcode
)
1289 (message "Creating odt file using \"%s\"" cmd
)
1291 (apply 'call-process
1296 (append (list "-rmTq") (list target-name
"."))))
1298 (or (zerop exitcode
)
1299 (error "Unable to create odt file (%S)" exitcode
)))
1301 ;; move the file from outdir to target-dir
1302 (rename-file target-name target-dir
)
1304 ;; kill all xml buffers
1305 (mapc (lambda (file)
1307 (find-file-noselect (expand-file-name file zipdir
) t
)))
1308 org-export-odt-save-list
)
1310 (delete-directory zipdir
)))
1312 (message "Created %s" target
)
1313 (set-buffer (find-file-noselect target t
)))
1315 (defun org-odt-update-meta-file (opt-plist)
1316 (with-current-buffer
1317 (find-file-noselect (expand-file-name "meta.xml") t
)
1318 (let ((date (or (plist-get opt-plist
:effective-date
) ""))
1319 (author (or (plist-get opt-plist
:author
) ""))
1320 (email (plist-get opt-plist
:email
))
1321 (keywords (plist-get opt-plist
:keywords
))
1322 (description (plist-get opt-plist
:description
))
1323 (title (plist-get opt-plist
:title
)))
1327 (org-odt-format-tags '("<dc:creator>" .
"</dc:creator>") author
)
1328 (org-odt-format-tags
1329 '("\n<meta:initial-creator>" .
"</meta:initial-creator>") author
)
1330 (org-odt-format-tags '("\n<dc:date>" .
"</dc:date>") date
)
1331 (org-odt-format-tags
1332 '("\n<meta:creation-date>" .
"</meta:creation-date>") date
)
1333 (org-odt-format-tags '("\n<meta:generator>" .
"</meta:generator>")
1334 (when org-export-creator-info
1335 (format "Org-%s/Emacs-%s"
1336 org-version emacs-version
)))
1337 (org-odt-format-tags '("\n<meta:keyword>" .
"</meta:keyword>") keywords
)
1338 (org-odt-format-tags '("\n<dc:subject>" .
"</dc:subject>") description
)
1339 (org-odt-format-tags '("\n<dc:title>" .
"</dc:title>") title
)
1342 (defun org-odt-update-manifest-file (media-type full-path
)
1343 (with-current-buffer
1344 (find-file-noselect (expand-file-name "META-INF/manifest.xml") t
)
1345 (insert (format org-odt-manifest-file-entry-tag media-type full-path
))))
1347 (defun org-odt-finalize-outfile ()
1348 (message "org-newodt: Finalizing outfile")
1349 (org-odt-delete-empty-paragraphs))
1351 (defun org-odt-delete-empty-paragraphs ()
1352 (goto-char (point-min))
1353 (let ((open "<text:p[^>]*>")
1354 (close "</text:p>"))
1355 (while (re-search-forward (format "%s[ \r\n\t]*%s" open close
) nil t
)
1356 (replace-match ""))))
1358 (defun org-odt-get (what &optional opt-plist
)
1361 (EXPORT-DIR (org-export-directory :html opt-plist
))
1362 (FILE-NAME-EXTENSION "odt")
1363 (EXPORT-BUFFER-NAME "*Org ODT Export*")
1364 (ENTITY-CONTROL org-odt-entity-control-callbacks-alist
)
1365 (ENTITY-FORMAT org-odt-entity-format-callbacks-alist
)
1366 (INIT-METHOD 'org-odt-init-outfile
)
1367 (FINAL-METHOD 'org-odt-finalize-outfile
)
1368 (SAVE-METHOD 'org-odt-save-as-outfile
)
1370 '("bib" "doc" "doc6" "doc95" "html" "xhtml" "latex" "odt" "ott" "pdf" "rtf"
1371 "sdw" "sdw3" "sdw4" "stw " "sxw" "mediawiki" "text" "txt" "uot" "vor"
1372 "vor3" "vor4" "docbook" "ooxml" "ppt" "odp"))
1373 (CONVERT-METHOD org-lparse-convert-process
)
1375 (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps
)
1376 (INLINE-IMAGES 'maybe
)
1377 (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
1378 (PLAIN-TEXT-MAP '(("&" .
"&") ("<" .
"<") (">" .
">")))
1379 (TABLE-FIRST-COLUMN-AS-LABELS nil
)
1380 (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY
"," 'superscript
))
1381 (CODING-SYSTEM-FOR-WRITE 'utf-8
)
1382 (CODING-SYSTEM-FOR-SAVE 'utf-8
)
1383 (t (error "Unknown property: %s" what
))))
1385 (defun org-odt-parse-label (label)
1387 (if (not (string-match "\\`[a-zA-Z]+:\\(.+\\)" label
))
1390 (capitalize (substring label
0 (1- (match-beginning 1))))
1391 (substring label
(match-beginning 1))))))
1393 (defvar org-lparse-latex-fragment-fallback
) ; set by org-do-lparse
1394 (defun org-export-odt-preprocess (parameters)
1395 "Convert LaTeX fragments to images."
1396 (when (and org-current-export-file
1397 (plist-get parameters
:LaTeX-fragments
))
1399 (concat "ltxpng/" (file-name-sans-extension
1400 (file-name-nondirectory
1401 org-current-export-file
)))
1402 org-current-export-dir nil
"Creating LaTeX image %s"
1405 ((eq (plist-get parameters
:LaTeX-fragments
) 'verbatim
) 'verbatim
)
1406 ;; Investigate MathToWeb for converting TeX equations to MathML
1407 ;; See http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01755.html
1408 ((or (eq (plist-get parameters
:LaTeX-fragments
) 'mathjax
)
1409 (eq (plist-get parameters
:LaTeX-fragments
) t
))
1412 "Use of MathJax is incompatible with ODT exporter. "
1413 (format "Using %S instead." org-lparse-latex-fragment-fallback
)))
1414 org-lparse-latex-fragment-fallback
)
1415 ((eq (plist-get parameters
:LaTeX-fragments
) 'dvipng
) 'dvipng
)
1417 (goto-char (point-min))
1418 (let (label label-components category value pretty-label
)
1419 (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t
)
1420 (org-if-unprotected-at (match-beginning 1)
1421 (setq label
(match-string 1)
1422 label-components
(org-odt-parse-label label
)
1423 category
(car label-components
)
1424 value
(cdr label-components
)
1425 pretty-label
(if value
(concat category
" " value
) label
))
1427 (let ((org-lparse-encode-pending t
))
1428 (org-odt-format-tags
1429 '("<text:sequence-ref text:reference-format=\"category-and-value\" text:ref-name=\"%s\">"
1430 .
"</text:sequence-ref>") pretty-label label
)) t t
)))))
1432 (declare-function archive-zip-extract
"arc-mode.el" (archive name
))
1433 (defun org-odt-zip-extract-one (archive member
&optional target
)
1435 (let* ((target (or target default-directory
))
1436 (archive (expand-file-name archive
))
1437 (archive-zip-extract
1438 (list "unzip" "-qq" "-o" "-d" target
))
1439 exit-code command-output
)
1440 (setq command-output
1442 (setq exit-code
(archive-zip-extract archive member
))
1444 (unless (zerop exit-code
)
1445 (message command-output
)
1446 (error "Extraction failed"))))
1448 (defun org-odt-zip-extract (archive members
&optional target
)
1449 (when (atom members
) (setq members
(list members
)))
1450 (mapc (lambda (member)
1451 (org-odt-zip-extract-one archive member target
))
1454 (defun org-odt-copy-styles-file (&optional styles-file
)
1455 ;; Non-availability of styles.xml is not a critical error. For now
1456 ;; throw an error purely for aesthetic reasons.
1457 (setq styles-file
(or styles-file
1458 org-export-odt-styles-file
1459 (expand-file-name "styles/OrgOdtStyles.xml"
1461 (error "org-odt: Missing styles file?")))
1463 ((listp styles-file
)
1464 (let ((archive (nth 0 styles-file
))
1465 (members (nth 1 styles-file
)))
1466 (org-odt-zip-extract archive members
)
1469 (when (org-file-image-p member
)
1470 (let* ((image-type (file-name-extension member
))
1471 (media-type (format "image/%s" image-type
)))
1472 (org-odt-update-manifest-file media-type member
))))
1474 ((and (stringp styles-file
) (file-exists-p styles-file
))
1475 (let ((styles-file-type (file-name-extension styles-file
)))
1477 ((string= styles-file-type
"xml")
1478 (copy-file styles-file
"styles.xml" t
))
1479 ((member styles-file-type
'("odt" "ott"))
1480 (org-odt-zip-extract styles-file
"styles.xml")))))
1482 (error (format "Invalid specification of styles.xml file: %S"
1483 org-export-odt-styles-file
)))))
1485 (defvar org-export-odt-factory-settings
1486 "d4328fb9d1b6cb211d4320ff546829f26700dc5e"
1487 "SHA1 hash of OrgOdtStyles.xml.")
1489 (defun org-odt-configure-outline-numbering (level)
1490 "Outline numbering is retained only upto LEVEL.
1491 To disable outline numbering pass a LEVEL of 0."
1492 (if (not (string= org-export-odt-factory-settings
(sha1 (current-buffer))))
1494 "org-odt: Using custom styles file? Consider tweaking styles.xml for better output. To suppress this warning update `org-export-odt-factory-settings'")
1495 (goto-char (point-min))
1497 "<text:outline-level-style\\(.*\\)text:level=\"\\(.*\\)\"\\(.*\\)>")
1499 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1500 (while (re-search-forward regex nil t
)
1501 (when (> (string-to-number (match-string 1)) level
)
1502 (replace-match replacement t nil
))))
1507 ;;; org-odt.el ends here