org-e-odt.el: Fix more compilation errors
[org-mode.git] / contrib / lisp / org-e-odt.el
blobdaffe9cb8f59378d50e923fba899d153ff41ae36
1 ;;; org-e-odt.el --- OpenDocument Text exporter for Org-mode
3 ;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
5 ;; Author: Jambunathan K <kjambunathan at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;;; Code:
28 (eval-when-compile
29 (require 'cl)
30 (require 'table))
31 (require 'format-spec)
32 (require 'org-export)
34 ;;; Define Back-End
36 (defvar org-e-odt-translate-alist
37 '((babel-call . org-e-odt-babel-call)
38 (bold . org-e-odt-bold)
39 (center-block . org-e-odt-center-block)
40 (clock . org-e-odt-clock)
41 (code . org-e-odt-code)
42 (comment . org-e-odt-comment)
43 (comment-block . org-e-odt-comment-block)
44 (drawer . org-e-odt-drawer)
45 (dynamic-block . org-e-odt-dynamic-block)
46 (entity . org-e-odt-entity)
47 (example-block . org-e-odt-example-block)
48 (export-block . org-e-odt-export-block)
49 (export-snippet . org-e-odt-export-snippet)
50 (fixed-width . org-e-odt-fixed-width)
51 (footnote-definition . org-e-odt-footnote-definition)
52 (footnote-reference . org-e-odt-footnote-reference)
53 (headline . org-e-odt-headline)
54 (horizontal-rule . org-e-odt-horizontal-rule)
55 (inline-babel-call . org-e-odt-inline-babel-call)
56 (inline-src-block . org-e-odt-inline-src-block)
57 (inlinetask . org-e-odt-inlinetask)
58 (italic . org-e-odt-italic)
59 (item . org-e-odt-item)
60 (keyword . org-e-odt-keyword)
61 (latex-environment . org-e-odt-latex-environment)
62 (latex-fragment . org-e-odt-latex-fragment)
63 (line-break . org-e-odt-line-break)
64 (link . org-e-odt-link)
65 (macro . org-e-odt-macro)
66 (paragraph . org-e-odt-paragraph)
67 (plain-list . org-e-odt-plain-list)
68 (plain-text . org-e-odt-plain-text)
69 (planning . org-e-odt-planning)
70 (property-drawer . org-e-odt-property-drawer)
71 (quote-block . org-e-odt-quote-block)
72 (quote-section . org-e-odt-quote-section)
73 (radio-target . org-e-odt-radio-target)
74 (section . org-e-odt-section)
75 (special-block . org-e-odt-special-block)
76 (src-block . org-e-odt-src-block)
77 (statistics-cookie . org-e-odt-statistics-cookie)
78 (strike-through . org-e-odt-strike-through)
79 (subscript . org-e-odt-subscript)
80 (superscript . org-e-odt-superscript)
81 (table . org-e-odt-table)
82 (table-cell . org-e-odt-table-cell)
83 (table-row . org-e-odt-table-row)
84 (target . org-e-odt-target)
85 (template . org-e-odt-template)
86 (timestamp . org-e-odt-timestamp)
87 (underline . org-e-odt-underline)
88 (verbatim . org-e-odt-verbatim)
89 (verse-block . org-e-odt-verse-block))
90 "Alist between element or object types and translators.")
92 (defconst org-e-odt-options-alist
94 ;; (:agenda-style nil nil org-agenda-export-html-style)
95 ;; ;; (:expand-quoted-html nil "@" org-e-odt-expand)
96 ;; ;; (:timestamp nil nil org-e-odt-with-timestamp)
97 (:odt-styles-file "ODT_STYLES_FILE" nil nil t)
98 (:LaTeX-fragments nil "LaTeX" org-export-with-LaTeX-fragments))
99 "Alist between ODT export properties and ways to set them.
100 See `org-export-options-alist' for more information on the
101 structure of the values.")
103 ;;; Dependencies
105 ;;; Hooks
107 ;;; Function Declarations
109 (declare-function org-id-find-id-file "org-id" (id))
110 (declare-function hfy-face-to-style "htmlfontify" (fn))
111 (declare-function hfy-face-or-def-to-name "htmlfontify" (fn))
112 (declare-function archive-zip-extract "arc-mode.el" (archive name))
113 (declare-function org-create-math-formula "org" (latex-frag &optional mathml-file))
114 (declare-function browse-url-file-url "browse-url" (file))
119 ;;; Internal Variables
121 (defconst org-e-odt-lib-dir
122 (file-name-directory load-file-name)
123 "Location of ODT exporter.
124 Use this to infer values of `org-e-odt-styles-dir' and
125 `org-e-odt-schema-dir'.")
127 (defvar org-e-odt-data-dir
128 (expand-file-name "../../etc/" org-e-odt-lib-dir)
129 "Data directory for ODT exporter.
130 Use this to infer values of `org-e-odt-styles-dir' and
131 `org-e-odt-schema-dir'.")
133 (defconst org-e-odt-special-string-regexps
134 '(("\\\\-" . "&#x00ad;\\1") ; shy
135 ("---\\([^-]\\)" . "&#x2014;\\1") ; mdash
136 ("--\\([^-]\\)" . "&#x2013;\\1") ; ndash
137 ("\\.\\.\\." . "&#x2026;")) ; hellip
138 "Regular expressions for special string conversion.")
140 (defconst org-e-odt-schema-dir-list
141 (list
142 (and org-e-odt-data-dir
143 (expand-file-name "./schema/" org-e-odt-data-dir)) ; bail out
144 (eval-when-compile
145 (and (boundp 'org-e-odt-data-dir) org-e-odt-data-dir ; see make install
146 (expand-file-name "./schema/" org-e-odt-data-dir))))
147 "List of directories to search for OpenDocument schema files.
148 Use this list to set the default value of
149 `org-e-odt-schema-dir'. The entries in this list are
150 populated heuristically based on the values of `org-e-odt-lib-dir'
151 and `org-e-odt-data-dir'.")
153 (defconst org-e-odt-styles-dir-list
154 (list
155 (and org-e-odt-data-dir
156 (expand-file-name "./styles/" org-e-odt-data-dir)) ; bail out
157 (eval-when-compile
158 (and (boundp 'org-e-odt-data-dir) org-e-odt-data-dir ; see make install
159 (expand-file-name "./styles/" org-e-odt-data-dir)))
160 (expand-file-name "../../etc/styles/" org-e-odt-lib-dir) ; git
161 (expand-file-name "./etc/styles/" org-e-odt-lib-dir) ; elpa
162 (expand-file-name "./org/" data-directory) ; system
164 "List of directories to search for OpenDocument styles files.
165 See `org-e-odt-styles-dir'. The entries in this list are populated
166 heuristically based on the values of `org-e-odt-lib-dir' and
167 `org-e-odt-data-dir'.")
169 (defconst org-e-odt-styles-dir
170 (let* ((styles-dir
171 (catch 'styles-dir
172 (message "Debug (org-e-odt): Searching for OpenDocument styles files...")
173 (mapc (lambda (styles-dir)
174 (when styles-dir
175 (message "Debug (org-e-odt): Trying %s..." styles-dir)
176 (when (and (file-readable-p
177 (expand-file-name
178 "OrgOdtContentTemplate.xml" styles-dir))
179 (file-readable-p
180 (expand-file-name
181 "OrgOdtStyles.xml" styles-dir)))
182 (message "Debug (org-e-odt): Using styles under %s"
183 styles-dir)
184 (throw 'styles-dir styles-dir))))
185 org-e-odt-styles-dir-list)
186 nil)))
187 (unless styles-dir
188 (error "Error (org-e-odt): Cannot find factory styles files. Aborting."))
189 styles-dir)
190 "Directory that holds auxiliary XML files used by the ODT exporter.
192 This directory contains the following XML files -
193 \"OrgOdtStyles.xml\" and \"OrgOdtContentTemplate.xml\". These
194 XML files are used as the default values of
195 `org-e-odt-styles-file' and
196 `org-e-odt-content-template-file'.
198 The default value of this variable varies depending on the
199 version of org in use and is initialized from
200 `org-e-odt-styles-dir-list'. Note that the user could be using org
201 from one of: org's own private git repository, GNU ELPA tar or
202 standard Emacs.")
204 (defconst org-e-odt-bookmark-prefix "OrgXref.")
206 (defconst org-e-odt-manifest-file-entry-tag
207 "\n<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
209 (defconst org-e-odt-file-extensions
210 '(("odt" . "OpenDocument Text")
211 ("ott" . "OpenDocument Text Template")
212 ("odm" . "OpenDocument Master Document")
213 ("ods" . "OpenDocument Spreadsheet")
214 ("ots" . "OpenDocument Spreadsheet Template")
215 ("odg" . "OpenDocument Drawing (Graphics)")
216 ("otg" . "OpenDocument Drawing Template")
217 ("odp" . "OpenDocument Presentation")
218 ("otp" . "OpenDocument Presentation Template")
219 ("odi" . "OpenDocument Image")
220 ("odf" . "OpenDocument Formula")
221 ("odc" . "OpenDocument Chart")))
223 (defvar org-e-odt-table-style-format
225 <style:style style:name=\"%s\" style:family=\"table\">
226 <style:table-properties style:rel-width=\"%d%%\" fo:margin-top=\"0cm\" fo:margin-bottom=\"0.20cm\" table:align=\"center\"/>
227 </style:style>
229 "Template for auto-generated Table styles.")
231 (defvar org-e-odt-automatic-styles '()
232 "Registry of automatic styles for various OBJECT-TYPEs.
233 The variable has the following form:
234 \(\(OBJECT-TYPE-A
235 \(\(OBJECT-NAME-A.1 OBJECT-PROPS-A.1\)
236 \(OBJECT-NAME-A.2 OBJECT-PROPS-A.2\) ...\)\)
237 \(OBJECT-TYPE-B
238 \(\(OBJECT-NAME-B.1 OBJECT-PROPS-B.1\)
239 \(OBJECT-NAME-B.2 OBJECT-PROPS-B.2\) ...\)\)
240 ...\).
242 OBJECT-TYPEs could be \"Section\", \"Table\", \"Figure\" etc.
243 OBJECT-PROPS is (typically) a plist created by passing
244 \"#+ATTR_ODT: \" option to `org-e-odt-parse-block-attributes'.
246 Use `org-e-odt-add-automatic-style' to add update this variable.'")
248 (defvar org-e-odt-object-counters nil
249 "Running counters for various OBJECT-TYPEs.
250 Use this to generate automatic names and style-names. See
251 `org-e-odt-add-automatic-style'.")
253 (defvar org-e-odt-src-block-paragraph-format
254 "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
255 <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
256 <style:background-image/>
257 </style:paragraph-properties>
258 <style:text-properties fo:color=\"%s\"/>
259 </style:style>"
260 "Custom paragraph style for colorized source and example blocks.
261 This style is much the same as that of \"OrgFixedWidthBlock\"
262 except that the foreground and background colors are set
263 according to the default face identified by the `htmlfontify'.")
265 (defvar hfy-optimisations)
266 (defvar org-e-odt-embedded-formulas-count 0)
267 (defvar org-e-odt-entity-frame-styles
268 '(("As-CharImage" "__Figure__" ("OrgInlineImage" nil "as-char"))
269 ("ParagraphImage" "__Figure__" ("OrgDisplayImage" nil "paragraph"))
270 ("PageImage" "__Figure__" ("OrgPageImage" nil "page"))
271 ("CaptionedAs-CharImage" "__Figure__"
272 ("OrgCaptionedImage"
273 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
274 ("OrgInlineImage" nil "as-char"))
275 ("CaptionedParagraphImage" "__Figure__"
276 ("OrgCaptionedImage"
277 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
278 ("OrgImageCaptionFrame" nil "paragraph"))
279 ("CaptionedPageImage" "__Figure__"
280 ("OrgCaptionedImage"
281 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
282 ("OrgPageImageCaptionFrame" nil "page"))
283 ("InlineFormula" "__MathFormula__" ("OrgInlineFormula" nil "as-char"))
284 ("DisplayFormula" "__MathFormula__" ("OrgDisplayFormula" nil "as-char"))
285 ("CaptionedDisplayFormula" "__MathFormula__"
286 ("OrgCaptionedFormula" nil "paragraph")
287 ("OrgFormulaCaptionFrame" nil "as-char"))))
289 (defvar org-e-odt-embedded-images-count 0)
290 (defvar org-e-odt-image-size-probe-method
291 (append (and (executable-find "identify") '(imagemagick)) ; See Bug#10675
292 '(emacs fixed))
293 "Ordered list of methods for determining image sizes.")
295 (defvar org-e-odt-default-image-sizes-alist
296 '(("as-char" . (5 . 0.4))
297 ("paragraph" . (5 . 5)))
298 "Hardcoded image dimensions one for each of the anchor
299 methods.")
301 ;; A4 page size is 21.0 by 29.7 cms
302 ;; The default page settings has 2cm margin on each of the sides. So
303 ;; the effective text area is 17.0 by 25.7 cm
304 (defvar org-e-odt-max-image-size '(17.0 . 20.0)
305 "Limiting dimensions for an embedded image.")
307 (defvar org-e-odt-label-styles
308 '(("math-formula" "%c" "text" "(%n)")
309 ("math-label" "(%n)" "text" "(%n)")
310 ("category-and-value" "%e %n: %c" "category-and-value" "%e %n")
311 ("value" "%e %n: %c" "value" "%n"))
312 "Specify how labels are applied and referenced.
313 This is an alist where each element is of the
314 form (LABEL-STYLE-NAME LABEL-ATTACH-FMT LABEL-REF-MODE
315 LABEL-REF-FMT).
317 LABEL-ATTACH-FMT controls how labels and captions are attached to
318 an entity. It may contain following specifiers - %e, %n and %c.
319 %e is replaced with the CATEGORY-NAME. %n is replaced with
320 \"<text:sequence ...> SEQNO </text:sequence>\". %c is replaced
321 with CAPTION. See `org-e-odt-format-label-definition'.
323 LABEL-REF-MODE and LABEL-REF-FMT controls how label references
324 are generated. The following XML is generated for a label
325 reference - \"<text:sequence-ref
326 text:reference-format=\"LABEL-REF-MODE\" ...> LABEL-REF-FMT
327 </text:sequence-ref>\". LABEL-REF-FMT may contain following
328 specifiers - %e and %n. %e is replaced with the CATEGORY-NAME.
329 %n is replaced with SEQNO. See
330 `org-e-odt-format-label-reference'.")
332 (defvar org-e-odt-category-map-alist
333 '(("__Table__" "Table" "value" "Table")
334 ("__Figure__" "Illustration" "value" "Figure")
335 ("__MathFormula__" "Text" "math-formula" "Equation")
336 ("__DvipngImage__" "Equation" "value" "Equation")
337 ("__Listing__" "Listing" "value" "Listing")
338 ;; ("__Table__" "Table" "category-and-value")
339 ;; ("__Figure__" "Figure" "category-and-value")
340 ;; ("__DvipngImage__" "Equation" "category-and-value")
342 "Map a CATEGORY-HANDLE to OD-VARIABLE and LABEL-STYLE.
343 This is a list where each entry is of the form \\(CATEGORY-HANDLE
344 OD-VARIABLE LABEL-STYLE CATEGORY-NAME\\). CATEGORY_HANDLE
345 identifies the captionable entity in question. OD-VARIABLE is
346 the OpenDocument sequence counter associated with the entity.
347 These counters are declared within
348 \"<text:sequence-decls>...</text:sequence-decls>\" block of
349 `org-e-odt-content-template-file'. LABEL-STYLE is a key into
350 `org-e-odt-label-styles' and specifies how a given entity should
351 be captioned and referenced. CATEGORY-NAME is used for
352 qualifying captions on export. You can modify the CATEGORY-NAME
353 used in the exported document by modifying
354 `org-export-dictionary'. For example, an embedded image in an
355 English document is captioned as \"Figure 1: Orgmode Logo\", by
356 default. If you want the image to be captioned as \"Illustration
357 1: Orgmode Logo\" instead, install an entry in
358 `org-export-dictionary' which translates \"Figure\" to
359 \"Illustration\" when the language is \"en\" and encoding is
360 `:utf-8'.")
362 (defvar org-e-odt-manifest-file-entries nil)
363 (defvar hfy-user-sheet-assoc)
365 (defvar org-e-odt-zip-dir nil
366 "Temporary work directory for OpenDocument exporter.")
370 ;;; User Configuration Variables
372 (defgroup org-export-e-odt nil
373 "Options for exporting Org mode files to ODT."
374 :tag "Org Export ODT"
375 :group 'org-export)
378 ;;;; Debugging
380 (defcustom org-e-odt-prettify-xml nil
381 "Specify whether or not the xml output should be prettified.
382 When this option is turned on, `indent-region' is run on all
383 component xml buffers before they are saved. Turn this off for
384 regular use. Turn this on if you need to examine the xml
385 visually."
386 :group 'org-export-e-odt
387 :version "24.1"
388 :type 'boolean)
391 ;;;; Document schema
393 (defcustom org-e-odt-schema-dir
394 (let* ((schema-dir
395 (catch 'schema-dir
396 (message "Debug (org-e-odt): Searching for OpenDocument schema files...")
397 (mapc
398 (lambda (schema-dir)
399 (when schema-dir
400 (message "Debug (org-e-odt): Trying %s..." schema-dir)
401 (when (and (file-readable-p
402 (expand-file-name "od-manifest-schema-v1.2-cs01.rnc"
403 schema-dir))
404 (file-readable-p
405 (expand-file-name "od-schema-v1.2-cs01.rnc"
406 schema-dir))
407 (file-readable-p
408 (expand-file-name "schemas.xml" schema-dir)))
409 (message "Debug (org-e-odt): Using schema files under %s"
410 schema-dir)
411 (throw 'schema-dir schema-dir))))
412 org-e-odt-schema-dir-list)
413 (message "Debug (org-e-odt): No OpenDocument schema files installed")
414 nil)))
415 schema-dir)
416 "Directory that contains OpenDocument schema files.
418 This directory contains:
419 1. rnc files for OpenDocument schema
420 2. a \"schemas.xml\" file that specifies locating rules needed
421 for auto validation of OpenDocument XML files.
423 Use the customize interface to set this variable. This ensures
424 that `rng-schema-locating-files' is updated and auto-validation
425 of OpenDocument XML takes place based on the value
426 `rng-nxml-auto-validate-flag'.
428 The default value of this variable varies depending on the
429 version of org in use and is initialized from
430 `org-e-odt-schema-dir-list'. The OASIS schema files are available
431 only in the org's private git repository. It is *not* bundled
432 with GNU ELPA tar or standard Emacs distribution."
433 :type '(choice
434 (const :tag "Not set" nil)
435 (directory :tag "Schema directory"))
436 :group 'org-export-e-odt
437 :version "24.1"
438 :set
439 (lambda (var value)
440 "Set `org-e-odt-schema-dir'.
441 Also add it to `rng-schema-locating-files'."
442 (let ((schema-dir value))
443 (set var
444 (if (and
445 (file-readable-p
446 (expand-file-name "od-manifest-schema-v1.2-cs01.rnc" schema-dir))
447 (file-readable-p
448 (expand-file-name "od-schema-v1.2-cs01.rnc" schema-dir))
449 (file-readable-p
450 (expand-file-name "schemas.xml" schema-dir)))
451 schema-dir
452 (when value
453 (message "Error (org-e-odt): %s has no OpenDocument schema files"
454 value))
455 nil)))
456 (when org-e-odt-schema-dir
457 (eval-after-load 'rng-loc
458 '(add-to-list 'rng-schema-locating-files
459 (expand-file-name "schemas.xml"
460 org-e-odt-schema-dir))))))
463 ;;;; Document styles
465 (defcustom org-e-odt-content-template-file nil
466 "Template file for \"content.xml\".
467 The exporter embeds the exported content just before
468 \"</office:text>\" element.
470 If unspecified, the file named \"OrgOdtContentTemplate.xml\"
471 under `org-e-odt-styles-dir' is used."
472 :type 'file
473 :group 'org-export-e-odt
474 :version "24.1")
476 (defcustom org-e-odt-styles-file nil
477 "Default styles file for use with ODT export.
478 Valid values are one of:
479 1. nil
480 2. path to a styles.xml file
481 3. path to a *.odt or a *.ott file
482 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
483 ...))
485 In case of option 1, an in-built styles.xml is used. See
486 `org-e-odt-styles-dir' for more information.
488 In case of option 3, the specified file is unzipped and the
489 styles.xml embedded therein is used.
491 In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
492 and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
493 generated odt file. Use relative path for specifying the
494 FILE-MEMBERS. styles.xml must be specified as one of the
495 FILE-MEMBERS.
497 Use options 1, 2 or 3 only if styles.xml alone suffices for
498 achieving the desired formatting. Use option 4, if the styles.xml
499 references additional files like header and footer images for
500 achieving the desired formatting.
502 Use \"#+ODT_STYLES_FILE: ...\" directive to set this variable on
503 a per-file basis. For example,
505 #+ODT_STYLES_FILE: \"/path/to/styles.xml\" or
506 #+ODT_STYLES_FILE: (\"/path/to/file.ott\" (\"styles.xml\" \"image/hdr.png\"))."
507 :group 'org-export-e-odt
508 :version "24.1"
509 :type
510 '(choice
511 (const :tag "Factory settings" nil)
512 (file :must-match t :tag "styles.xml")
513 (file :must-match t :tag "ODT or OTT file")
514 (list :tag "ODT or OTT file + Members"
515 (file :must-match t :tag "ODF Text or Text Template file")
516 (cons :tag "Members"
517 (file :tag " Member" "styles.xml")
518 (repeat (file :tag "Member"))))))
520 (defcustom org-e-odt-display-outline-level 2
521 "Outline levels considered for enumerating captioned entities."
522 :group 'org-export-e-odt
523 :version "24.2"
524 :type 'integer)
526 ;;;; Document conversion
528 (defcustom org-e-odt-convert-processes
529 '(("LibreOffice"
530 "soffice --headless --convert-to %f%x --outdir %d %i")
531 ("unoconv"
532 "unoconv -f %f -o %d %i"))
533 "Specify a list of document converters and their usage.
534 The converters in this list are offered as choices while
535 customizing `org-e-odt-convert-process'.
537 This variable is a list where each element is of the
538 form (CONVERTER-NAME CONVERTER-CMD). CONVERTER-NAME is the name
539 of the converter. CONVERTER-CMD is the shell command for the
540 converter and can contain format specifiers. These format
541 specifiers are interpreted as below:
543 %i input file name in full
544 %I input file name as a URL
545 %f format of the output file
546 %o output file name in full
547 %O output file name as a URL
548 %d output dir in full
549 %D output dir as a URL.
550 %x extra options as set in `org-e-odt-convert-capabilities'."
551 :group 'org-export-e-odt
552 :version "24.1"
553 :type
554 '(choice
555 (const :tag "None" nil)
556 (alist :tag "Converters"
557 :key-type (string :tag "Converter Name")
558 :value-type (group (string :tag "Command line")))))
560 (defcustom org-e-odt-convert-process "LibreOffice"
561 "Use this converter to convert from \"odt\" format to other formats.
562 During customization, the list of converter names are populated
563 from `org-e-odt-convert-processes'."
564 :group 'org-export-e-odt
565 :version "24.1"
566 :type '(choice :convert-widget
567 (lambda (w)
568 (apply 'widget-convert (widget-type w)
569 (eval (car (widget-get w :args)))))
570 `((const :tag "None" nil)
571 ,@(mapcar (lambda (c)
572 `(const :tag ,(car c) ,(car c)))
573 org-e-odt-convert-processes))))
575 (defcustom org-e-odt-convert-capabilities
576 '(("Text"
577 ("odt" "ott" "doc" "rtf" "docx")
578 (("pdf" "pdf") ("odt" "odt") ("rtf" "rtf") ("ott" "ott")
579 ("doc" "doc" ":\"MS Word 97\"") ("docx" "docx") ("html" "html")))
580 ("Web"
581 ("html")
582 (("pdf" "pdf") ("odt" "odt") ("html" "html")))
583 ("Spreadsheet"
584 ("ods" "ots" "xls" "csv" "xlsx")
585 (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv") ("ods" "ods")
586 ("xls" "xls") ("xlsx" "xlsx")))
587 ("Presentation"
588 ("odp" "otp" "ppt" "pptx")
589 (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("otp" "otp") ("ppt" "ppt")
590 ("pptx" "pptx") ("odg" "odg"))))
591 "Specify input and output formats of `org-e-odt-convert-process'.
592 More correctly, specify the set of input and output formats that
593 the user is actually interested in.
595 This variable is an alist where each element is of the
596 form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
597 INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
598 alist where each element is of the form (OUTPUT-FMT
599 OUTPUT-FILE-EXTENSION EXTRA-OPTIONS).
601 The variable is interpreted as follows:
602 `org-e-odt-convert-process' can take any document that is in
603 INPUT-FMT-LIST and produce any document that is in the
604 OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
605 OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
606 serves dual purposes:
607 - It is used for populating completion candidates during
608 `org-e-odt-convert' commands.
609 - It is used as the value of \"%f\" specifier in
610 `org-e-odt-convert-process'.
612 EXTRA-OPTIONS is used as the value of \"%x\" specifier in
613 `org-e-odt-convert-process'.
615 DOCUMENT-CLASS is used to group a set of file formats in
616 INPUT-FMT-LIST in to a single class.
618 Note that this variable inherently captures how LibreOffice based
619 converters work. LibreOffice maps documents of various formats
620 to classes like Text, Web, Spreadsheet, Presentation etc and
621 allow document of a given class (irrespective of it's source
622 format) to be converted to any of the export formats associated
623 with that class.
625 See default setting of this variable for an typical
626 configuration."
627 :group 'org-export-e-odt
628 :version "24.1"
629 :type
630 '(choice
631 (const :tag "None" nil)
632 (alist :tag "Capabilities"
633 :key-type (string :tag "Document Class")
634 :value-type
635 (group (repeat :tag "Input formats" (string :tag "Input format"))
636 (alist :tag "Output formats"
637 :key-type (string :tag "Output format")
638 :value-type
639 (group (string :tag "Output file extension")
640 (choice
641 (const :tag "None" nil)
642 (string :tag "Extra options"))))))))
644 (defcustom org-e-odt-preferred-output-format nil
645 "Automatically post-process to this format after exporting to \"odt\".
646 Interactive commands `org-export-as-e-odt' and
647 `org-export-as-e-odt-and-open' export first to \"odt\" format and
648 then use `org-e-odt-convert-process' to convert the
649 resulting document to this format. During customization of this
650 variable, the list of valid values are populated based on
651 `org-e-odt-convert-capabilities'."
652 :group 'org-export-e-odt
653 :version "24.1"
654 :type '(choice :convert-widget
655 (lambda (w)
656 (apply 'widget-convert (widget-type w)
657 (eval (car (widget-get w :args)))))
658 `((const :tag "None" nil)
659 ,@(mapcar (lambda (c)
660 `(const :tag ,c ,c))
661 (org-e-odt-reachable-formats "odt")))))
664 ;;;; Drawers
666 (defcustom org-e-odt-format-drawer-function nil
667 "Function called to format a drawer in HTML code.
669 The function must accept two parameters:
670 NAME the drawer name, like \"LOGBOOK\"
671 CONTENTS the contents of the drawer.
673 The function should return the string to be exported.
675 For example, the variable could be set to the following function
676 in order to mimic default behaviour:
678 \(defun org-e-odt-format-drawer-default \(name contents\)
679 \"Format a drawer element for HTML export.\"
680 contents\)"
681 :group 'org-export-e-odt
682 :type 'function)
685 ;;;; Headline
687 (defcustom org-e-odt-format-headline-function nil
688 "Function to format headline text.
690 This function will be called with 5 arguments:
691 TODO the todo keyword \(string or nil\).
692 TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
693 PRIORITY the priority of the headline \(integer or nil\)
694 TEXT the main headline text \(string\).
695 TAGS the tags string, separated with colons \(string or nil\).
697 The function result will be used in the section format string.
699 As an example, one could set the variable to the following, in
700 order to reproduce the default set-up:
702 \(defun org-e-odt-format-headline \(todo todo-type priority text tags\)
703 \"Default format function for an headline.\"
704 \(concat \(when todo
705 \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo\)\)
706 \(when priority
707 \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
708 text
709 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
710 :group 'org-export-e-odt
711 :type 'function)
714 ;;;; Inlinetasks
716 (defcustom org-e-odt-format-inlinetask-function nil
717 "Function called to format an inlinetask in HTML code.
719 The function must accept six parameters:
720 TODO the todo keyword, as a string
721 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
722 PRIORITY the inlinetask priority, as a string
723 NAME the inlinetask name, as a string.
724 TAGS the inlinetask tags, as a string.
725 CONTENTS the contents of the inlinetask, as a string.
727 The function should return the string to be exported.
729 For example, the variable could be set to the following function
730 in order to mimic default behaviour:
732 \(defun org-e-odt-format-inlinetask \(todo type priority name tags contents\)
733 \"Format an inline task element for HTML export.\"
734 \(let \(\(full-title
735 \(concat
736 \(when todo
737 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
738 \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
739 title
740 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
741 \(format \(concat \"\\\\begin{center}\\n\"
742 \"\\\\fbox{\\n\"
743 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
744 \"%s\\n\\n\"
745 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
746 \"%s\"
747 \"\\\\end{minipage}}\"
748 \"\\\\end{center}\"\)
749 full-title contents\)\)"
750 :group 'org-export-e-odt
751 :type 'function)
754 ;;;; Links
756 (defcustom org-e-odt-inline-image-rules
757 '(("file" . "\\.\\(jpeg\\|jpg\\|png\\|gif\\)\\'"))
758 "Rules characterizing image files that can be inlined into HTML.
760 A rule consists in an association whose key is the type of link
761 to consider, and value is a regexp that will be matched against
762 link's path.
764 Note that, by default, the image extension *actually* allowed
765 depend on the way the HTML file is processed. When used with
766 pdflatex, pdf, jpg and png images are OK. When processing
767 through dvi to Postscript, only ps and eps are allowed. The
768 default we use here encompasses both."
769 :group 'org-export-e-odt
770 :type '(alist :key-type (string :tag "Type")
771 :value-type (regexp :tag "Path")))
773 (defcustom org-e-odt-pixels-per-inch display-pixels-per-inch
774 "Scaling factor for converting images pixels to inches.
775 Use this for sizing of embedded images. See Info node `(org)
776 Images in ODT export' for more information."
777 :type 'float
778 :group 'org-export-e-odt
779 :version "24.1")
782 ;;;; Plain text
784 (defcustom org-e-odt-quotes
785 '(("fr"
786 ("\\(\\s-\\|[[(]\\|^\\)\"" . "« ")
787 ("\\(\\S-\\)\"" . "» ")
788 ("\\(\\s-\\|(\\|^\\)'" . "'"))
789 ("en"
790 ("\\(\\s-\\|[[(]\\|^\\)\"" . "“")
791 ("\\(\\S-\\)\"" . "”")
792 ("\\(\\s-\\|(\\|^\\)'" . "‘")
793 ("\\(\\S-\\)'" . "’")))
794 "Alist for quotes to use when converting english double-quotes.
796 The CAR of each item in this alist is the language code.
797 The CDR of each item in this alist is a list of three CONS:
798 - the first CONS defines the opening quote;
799 - the second CONS defines the closing quote;
800 - the last CONS defines single quotes.
802 For each item in a CONS, the first string is a regexp
803 for allowed characters before/after the quote, the second
804 string defines the replacement string for this quote."
805 :group 'org-export-e-odt
806 :type '(list
807 (cons :tag "Opening quote"
808 (string :tag "Regexp for char before")
809 (string :tag "Replacement quote "))
810 (cons :tag "Closing quote"
811 (string :tag "Regexp for char after ")
812 (string :tag "Replacement quote "))
813 (cons :tag "Single quote"
814 (string :tag "Regexp for char before")
815 (string :tag "Replacement quote "))))
818 ;;;; Src Block
820 (defcustom org-e-odt-create-custom-styles-for-srcblocks t
821 "Whether custom styles for colorized source blocks be automatically created.
822 When this option is turned on, the exporter creates custom styles
823 for source blocks based on the advice of `htmlfontify'. Creation
824 of custom styles happen as part of `org-e-odt-hfy-face-to-css'.
826 When this option is turned off exporter does not create such
827 styles.
829 Use the latter option if you do not want the custom styles to be
830 based on your current display settings. It is necessary that the
831 styles.xml already contains needed styles for colorizing to work.
833 This variable is effective only if
834 `org-e-odt-fontify-srcblocks' is turned on."
835 :group 'org-export-e-odt
836 :version "24.1"
837 :type 'boolean)
839 (defcustom org-e-odt-fontify-srcblocks t
840 "Specify whether or not source blocks need to be fontified.
841 Turn this option on if you want to colorize the source code
842 blocks in the exported file. For colorization to work, you need
843 to make available an enhanced version of `htmlfontify' library."
844 :type 'boolean
845 :group 'org-export-e-odt
846 :version "24.1")
849 ;;;; Table
851 (defcustom org-e-odt-table-caption-above t
852 "When non-nil, place caption string at the beginning of the table.
853 Otherwise, place it near the end."
854 :group 'org-export-e-odt
855 :type 'boolean)
857 (defcustom org-e-odt-table-styles
858 '(("OrgEquation" "OrgEquation"
859 ((use-first-column-styles . t)
860 (use-last-column-styles . t))))
861 "Specify how Table Styles should be derived from a Table Template.
862 This is a list where each element is of the
863 form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
865 TABLE-STYLE-NAME is the style associated with the table through
866 \"#+ATTR_ODT: :style TABLE-STYLE-NAME\" line.
868 TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
869 TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
870 below) that is included in
871 `org-e-odt-content-template-file'.
873 TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
874 \"TableCell\"
875 PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
876 \"TableParagraph\"
877 TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
878 \"FirstRow\" | \"LastRow\" |
879 \"EvenRow\" | \"OddRow\" |
880 \"EvenColumn\" | \"OddColumn\" | \"\"
881 where \"+\" above denotes string concatenation.
883 TABLE-CELL-OPTIONS is an alist where each element is of the
884 form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
885 TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
886 `use-last-row-styles' |
887 `use-first-column-styles' |
888 `use-last-column-styles' |
889 `use-banding-rows-styles' |
890 `use-banding-columns-styles' |
891 `use-first-row-styles'
892 ON-OR-OFF := `t' | `nil'
894 For example, with the following configuration
896 \(setq org-e-odt-table-styles
897 '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
898 \(\(use-first-row-styles . t\)
899 \(use-first-column-styles . t\)\)\)
900 \(\"TableWithHeaderColumns\" \"Custom\"
901 \(\(use-first-column-styles . t\)\)\)\)\)
903 1. A table associated with \"TableWithHeaderRowsAndColumns\"
904 style will use the following table-cell styles -
905 \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
906 \"CustomTableCell\" and the following paragraph styles
907 \"CustomFirstRowTableParagraph\",
908 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
909 as appropriate.
911 2. A table associated with \"TableWithHeaderColumns\" style will
912 use the following table-cell styles -
913 \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
914 following paragraph styles
915 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
916 as appropriate..
918 Note that TABLE-TEMPLATE-NAME corresponds to the
919 \"<table:table-template>\" elements contained within
920 \"<office:styles>\". The entries (TABLE-STYLE-NAME
921 TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
922 \"table:template-name\" and \"table:use-first-row-styles\" etc
923 attributes of \"<table:table>\" element. Refer ODF-1.2
924 specification for more information. Also consult the
925 implementation filed under `org-e-odt-get-table-cell-styles'.
927 The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
928 formatting of numbered display equations. Do not delete this
929 style from the list."
930 :group 'org-export-e-odt
931 :version "24.1"
932 :type '(choice
933 (const :tag "None" nil)
934 (repeat :tag "Table Styles"
935 (list :tag "Table Style Specification"
936 (string :tag "Table Style Name")
937 (string :tag "Table Template Name")
938 (alist :options (use-first-row-styles
939 use-last-row-styles
940 use-first-column-styles
941 use-last-column-styles
942 use-banding-rows-styles
943 use-banding-columns-styles)
944 :key-type symbol
945 :value-type (const :tag "True" t))))))
949 ;;; Internal functions
951 ;;;; Date
953 (defun org-e-odt--date (&optional org-ts fmt)
954 (save-match-data
955 (let* ((time
956 (and (stringp org-ts)
957 (string-match org-ts-regexp0 org-ts)
958 (apply 'encode-time
959 (org-fix-decoded-time
960 (org-parse-time-string (match-string 0 org-ts) t)))))
961 date)
962 (cond
963 (fmt (format-time-string fmt time))
964 (t (setq date (format-time-string "%Y-%m-%dT%H:%M:%S%z" time))
965 (format "%s:%s" (substring date 0 -2) (substring date -2)))))))
967 ;;;; Frame
969 (defun org-e-odt--frame (text width height style &optional extra
970 anchor-type)
971 (let ((frame-attrs
972 (concat
973 (if width (format " svg:width=\"%0.2fcm\"" width) "")
974 (if height (format " svg:height=\"%0.2fcm\"" height) "")
975 extra
976 (format " text:anchor-type=\"%s\"" (or anchor-type "paragraph")))))
977 (format
978 "\n<draw:frame draw:style-name=\"%s\"%s>\n%s\n</draw:frame>"
979 style frame-attrs
980 (concat text
981 (let ((title (get-text-property 0 :title text))
982 (desc (get-text-property 0 :description text)))
983 (concat (and title
984 (format "<svg:title>%s</svg:title>"
985 (org-e-odt-encode-plain-text title t)))
986 (and desc
987 (format "<svg:desc>%s</svg:desc>"
988 (org-e-odt-encode-plain-text desc t)))))))))
990 ;;;; Library wrappers
992 (defun org-e-odt--adopt-elements (parent &rest children)
993 (prog1 parent
994 (mapc (lambda (child)
995 (let ((parent-1 (org-element-adopt-element parent child nil)))
996 (assert (eq parent-1 parent))))
997 children)))
999 (defun org-e-odt--zip-extract (archive members target)
1000 (when (atom members) (setq members (list members)))
1001 (mapc (lambda (archive member target)
1002 (require 'arc-mode)
1003 (let* ((--quote-file-name
1004 ;; This is shamelessly stolen from `archive-zip-extract'.
1005 (lambda (name)
1006 (if (or (not (memq system-type '(windows-nt ms-dos)))
1007 (and (boundp 'w32-quote-process-args)
1008 (null w32-quote-process-args)))
1009 (shell-quote-argument name)
1010 name)))
1011 (target (funcall --quote-file-name target))
1012 (archive (expand-file-name archive))
1013 (archive-zip-extract
1014 (list "unzip" "-qq" "-o" "-d" target))
1015 exit-code command-output)
1016 (setq command-output
1017 (with-temp-buffer
1018 (setq exit-code (archive-zip-extract archive member))
1019 (buffer-string)))
1020 (unless (zerop exit-code)
1021 (message command-output)
1022 (error "Extraction failed"))))
1023 members))
1025 ;;;; Textbox
1027 (defun org-e-odt--textbox (text width height style &optional
1028 extra anchor-type)
1029 (org-e-odt--frame
1030 (format "\n<draw:text-box %s>%s\n</draw:text-box>"
1031 (concat (format " fo:min-height=\"%0.2fcm\"" (or height .2))
1032 (and (not width)
1033 (format " fo:min-width=\"%0.2fcm\"" (or width .2))))
1034 text)
1035 width nil style extra anchor-type))
1039 ;;;; Table of Contents
1041 (defun org-e-odt-begin-toc (index-title depth)
1042 (concat
1043 (format "
1044 <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
1045 <text:table-of-content-source text:outline-level=\"%d\">
1046 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
1047 " depth index-title)
1049 (let ((levels (number-sequence 1 10)))
1050 (mapconcat
1051 (lambda (level)
1052 (format
1054 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
1055 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
1056 <text:index-entry-chapter/>
1057 <text:index-entry-text/>
1058 <text:index-entry-link-end/>
1059 </text:table-of-content-entry-template>
1060 " level level)) levels ""))
1062 (format "
1063 </text:table-of-content-source>
1065 <text:index-body>
1066 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
1067 <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
1068 </text:index-title>
1069 " index-title)))
1071 (defun org-e-odt-end-toc ()
1072 (format "
1073 </text:index-body>
1074 </text:table-of-content>
1079 (defun* org-e-odt-format-toc-headline
1080 (todo todo-type priority text tags
1081 &key level section-number headline-label &allow-other-keys)
1082 (setq text (concat
1083 (and org-export-with-section-numbers
1084 (concat section-number ". "))
1085 text
1086 (and tags
1087 (concat
1088 "<text:tab/>"
1089 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1090 "OrgTag" tags)))))
1091 (when todo
1092 (setq text (format "<text:span text:style-name=\"%s\">%s</text:span>"
1093 "OrgTodo" text)))
1094 (org-e-odt-format-link text (concat "#" headline-label) t))
1096 (defun org-e-odt-toc (depth info)
1097 (assert (wholenump depth))
1098 (let* ((title (org-export-translate "Table of Contents" :utf-8 info))
1099 (headlines (org-export-collect-headlines info depth)))
1101 (when headlines
1102 (concat
1103 (org-e-odt-begin-toc title depth)
1105 (mapconcat
1106 (lambda (headline)
1107 (let* ((entry (org-e-odt-format-headline--wrap
1108 headline info 'org-e-odt-format-toc-headline))
1109 (level (org-export-get-relative-level headline info))
1110 (style (format "Contents_20_%d" level)))
1111 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1112 style entry)))
1113 headlines "\n")
1115 (org-e-odt-end-toc)))))
1118 ;;;; Document styles
1120 (defun org-e-odt-add-automatic-style (object-type &optional object-props)
1121 "Create an automatic style of type OBJECT-TYPE with param OBJECT-PROPS.
1122 OBJECT-PROPS is (typically) a plist created by passing
1123 \"#+ATTR_ODT: \" option of the object in question to
1124 `org-e-odt-parse-block-attributes'.
1126 Use `org-e-odt-object-counters' to generate an automatic
1127 OBJECT-NAME and STYLE-NAME. If OBJECT-PROPS is non-nil, add a
1128 new entry in `org-e-odt-automatic-styles'. Return (OBJECT-NAME
1129 . STYLE-NAME)."
1130 (assert (stringp object-type))
1131 (let* ((object (intern object-type))
1132 (seqvar object)
1133 (seqno (1+ (or (plist-get org-e-odt-object-counters seqvar) 0)))
1134 (object-name (format "%s%d" object-type seqno)) style-name)
1135 (setq org-e-odt-object-counters
1136 (plist-put org-e-odt-object-counters seqvar seqno))
1137 (when object-props
1138 (setq style-name (format "Org%s" object-name))
1139 (setq org-e-odt-automatic-styles
1140 (plist-put org-e-odt-automatic-styles object
1141 (append (list (list style-name object-props))
1142 (plist-get org-e-odt-automatic-styles object)))))
1143 (cons object-name style-name)))
1146 ;;;; Caption and Labels
1149 (defun org-e-odt--wrap-label (element output)
1150 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
1151 This function shouldn't be used for floats. See
1152 `org-e-odt--caption/label-string'."
1153 ;; (let ((label (org-element-property :name element)))
1154 ;; (if (or (not output) (not label) (string= output "") (string= label ""))
1155 ;; output
1156 ;; (concat (format "\\label{%s}\n" label) output)))
1157 output)
1160 (defun org-e-odt--caption/label-string (caption label info)
1161 "Return caption and label HTML string for floats.
1163 CAPTION is a cons cell of secondary strings, the car being the
1164 standard caption and the cdr its short form. LABEL is a string
1165 representing the label. INFO is a plist holding contextual
1166 information.
1168 If there's no caption nor label, return the empty string.
1170 For non-floats, see `org-e-odt--wrap-label'."
1171 (setq label nil) ;; FIXME
1173 (let ((label-str (if label (format "\\label{%s}" label) "")))
1174 (cond
1175 ((and (not caption) (not label)) "")
1176 ((not caption) (format "\\label{%s}\n" label))
1177 ;; Option caption format with short name.
1178 ((cdr caption)
1179 (format "\\caption[%s]{%s%s}\n"
1180 (org-export-data (cdr caption) info)
1181 label-str
1182 (org-export-data (car caption) info)))
1183 ;; Standard caption format.
1184 ;; (t (format "\\caption{%s%s}\n"
1185 ;; label-str
1186 ;; (org-export-data (car caption) info)))
1187 (t (org-export-data (car caption) info)))))
1189 ;;;; Checkbox
1191 (defun org-e-odt--checkbox (item)
1192 "Return check-box string associated to ITEM."
1193 (let ((checkbox (org-element-property :checkbox item)))
1194 (if (not checkbox) ""
1195 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1196 "OrgCode" (case checkbox
1197 (on "[&#x2713;] ") ; CHECK MARK
1198 (off "[ ] ")
1199 (trans "[-] "))))))
1203 ;;; Template
1205 (defun org-e-odt-template (contents info)
1206 "Return complete document string after HTML conversion.
1207 CONTENTS is the transcoded contents string. RAW-DATA is the
1208 original parsed data. INFO is a plist holding export options."
1209 ;; Write meta file.
1210 (let ((title (org-export-data (plist-get info :title) info))
1211 (author (let ((author (plist-get info :author)))
1212 (if (not author) "" (org-export-data author info))))
1213 (date (org-e-odt--date
1214 (org-export-data (plist-get info :date) info)))
1215 (email (plist-get info :email))
1216 (keywords (plist-get info :keywords))
1217 (description (plist-get info :description)))
1218 (write-region
1219 (concat
1220 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
1221 <office:document-meta
1222 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
1223 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
1224 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
1225 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
1226 xmlns:ooo=\"http://openoffice.org/2004/office\"
1227 office:version=\"1.2\">
1228 <office:meta>\n"
1229 (format "<dc:creator>%s</dc:creator>\n" author)
1230 (format "<meta:initial-creator>%s</meta:initial-creator>\n" author)
1231 (format "<dc:date>%s</dc:date>\n" date)
1232 (format "<meta:creation-date>%s</meta:creation-date>\n" date)
1233 (format "<meta:generator>%s</meta:generator>\n"
1234 (let ((creator-info (plist-get info :with-creator)))
1235 (if (or (not creator-info) (eq creator-info 'comment)) ""
1236 (plist-get info :creator))))
1237 (format "<meta:keyword>%s</meta:keyword>\n" keywords)
1238 (format "<dc:subject>%s</dc:subject>\n" description)
1239 (format "<dc:title>%s</dc:title>\n" title)
1240 "\n"
1241 " </office:meta>\n" "</office:document-meta>")
1242 nil (concat org-e-odt-zip-dir "meta.xml"))
1243 ;; Add meta.xml in to manifest.
1244 (org-e-odt-create-manifest-file-entry "text/xml" "meta.xml"))
1246 ;; Update styles file.
1247 ;; Copy styles.xml. Also dump htmlfontify styles, if there is any.
1248 ;; Write styles file.
1249 (let* ((styles-file (plist-get info :odt-styles-file))
1250 (styles-file (and styles-file (read (org-trim styles-file))))
1251 ;; Non-availability of styles.xml is not a critical
1252 ;; error. For now throw an error purely for aesthetic
1253 ;; reasons.
1254 (styles-file (or styles-file
1255 org-e-odt-styles-file
1256 (expand-file-name "OrgOdtStyles.xml"
1257 org-e-odt-styles-dir)
1258 (error "org-e-odt: Missing styles file?"))))
1259 (cond
1260 ((listp styles-file)
1261 (let ((archive (nth 0 styles-file))
1262 (members (nth 1 styles-file)))
1263 (org-e-odt--zip-extract archive members org-e-odt-zip-dir)
1264 (mapc
1265 (lambda (member)
1266 (when (org-file-image-p member)
1267 (let* ((image-type (file-name-extension member))
1268 (media-type (format "image/%s" image-type)))
1269 (org-e-odt-create-manifest-file-entry media-type member))))
1270 members)))
1271 ((and (stringp styles-file) (file-exists-p styles-file))
1272 (let ((styles-file-type (file-name-extension styles-file)))
1273 (cond
1274 ((string= styles-file-type "xml")
1275 (copy-file styles-file (concat org-e-odt-zip-dir "styles.xml") t))
1276 ((member styles-file-type '("odt" "ott"))
1277 (org-e-odt--zip-extract styles-file "styles.xml" org-e-odt-zip-dir)))))
1279 (error (format "Invalid specification of styles.xml file: %S"
1280 org-e-odt-styles-file))))
1282 ;; create a manifest entry for styles.xml
1283 (org-e-odt-create-manifest-file-entry "text/xml" "styles.xml")
1285 ;; FIXME: Who is opening an empty styles.xml before this point?
1286 (with-current-buffer
1287 (find-file-noselect (concat org-e-odt-zip-dir "styles.xml") t)
1288 (revert-buffer t t)
1290 ;; Write custom styles for source blocks
1291 ;; Save STYLES used for colorizing of source blocks.
1292 ;; Update styles.xml with styles that were collected as part of
1293 ;; `org-e-odt-hfy-face-to-css' callbacks.
1294 (let ((styles (mapconcat (lambda (style) (format " %s\n" (cddr style)))
1295 hfy-user-sheet-assoc "")))
1296 (when styles
1297 (goto-char (point-min))
1298 (when (re-search-forward "</office:styles>" nil t)
1299 (goto-char (match-beginning 0))
1300 (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n"))))
1302 ;; Update styles.xml - take care of outline numbering
1304 ;; Don't make automatic backup of styles.xml file. This setting
1305 ;; prevents the backed-up styles.xml file from being zipped in to
1306 ;; odt file. This is more of a hackish fix. Better alternative
1307 ;; would be to fix the zip command so that the output odt file
1308 ;; includes only the needed files and excludes any auto-generated
1309 ;; extra files like backups and auto-saves etc etc. Note that
1310 ;; currently the zip command zips up the entire temp directory so
1311 ;; that any auto-generated files created under the hood ends up in
1312 ;; the resulting odt file.
1313 (set (make-local-variable 'backup-inhibited) t)
1315 ;; Outline numbering is retained only upto LEVEL.
1316 ;; To disable outline numbering pass a LEVEL of 0.
1318 (goto-char (point-min))
1319 (let ((regex
1320 "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
1321 (replacement
1322 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1323 (while (re-search-forward regex nil t)
1324 (unless (let ((sec-num (plist-get info :section-numbers))
1325 (level (string-to-number (match-string 2))))
1326 (if (wholenump sec-num) (<= level sec-num) sec-num))
1327 (replace-match replacement t nil))))
1328 (save-buffer 0)))
1329 ;; Update content.xml.
1330 (with-temp-buffer
1331 (insert-file-contents
1332 (or org-e-odt-content-template-file
1333 (expand-file-name "OrgOdtContentTemplate.xml"
1334 org-e-odt-styles-dir)))
1335 ;; Write automatic styles.
1336 ;; - Position the cursor.
1337 (goto-char (point-min))
1338 (re-search-forward " </office:automatic-styles>" nil t)
1339 (goto-char (match-beginning 0))
1340 ;; - Dump automatic table styles
1341 (loop for (style-name props) in
1342 (plist-get org-e-odt-automatic-styles 'Table) do
1343 (when (setq props (or (plist-get props :rel-width) 96))
1344 (insert (format org-e-odt-table-style-format style-name props))))
1345 ;; Update display level.
1346 ;; - Remove existing sequence decls. Also position the cursor.
1347 (goto-char (point-min))
1348 (when (re-search-forward "<text:sequence-decls" nil t)
1349 (delete-region (match-beginning 0)
1350 (re-search-forward "</text:sequence-decls>" nil nil)))
1351 ;; Update sequence decls according to user preference.
1352 (insert
1353 (format
1354 "\n<text:sequence-decls>\n%s\n</text:sequence-decls>"
1355 (mapconcat
1356 (lambda (x)
1357 (format
1358 "<text:sequence-decl text:display-outline-level=\"%d\" text:name=\"%s\"/>"
1359 org-e-odt-display-outline-level (nth 1 x)))
1360 org-e-odt-category-map-alist "\n")))
1361 ;; Position the cursor to document body.
1362 (goto-char (point-min))
1363 (re-search-forward "</office:text>" nil nil)
1364 (goto-char (match-beginning 0))
1366 ;; Preamble - Title, Author, Date etc.
1367 (insert
1368 (let* ((title (org-export-data (plist-get info :title) info))
1369 (author (and (plist-get info :with-author)
1370 (let ((auth (plist-get info :author)))
1371 (and auth (org-export-data auth info)))))
1372 (date (org-export-data (plist-get info :date) info))
1373 (iso-date (org-e-odt--date date))
1374 (date (org-e-odt--date date "%d %b %Y"))
1375 (email (plist-get info :email))
1376 ;; switch on or off above vars based on user settings
1377 (author (and (plist-get info :with-author) (or author email)))
1378 ;; (date (and (plist-get info :time-stamp-file) date))
1379 (email (and (plist-get info :with-email) email)))
1380 (concat
1381 ;; title
1382 (when title
1383 (concat
1384 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1385 "OrgTitle" (format "\n<text:title>%s</text:title>" title))
1386 ;; separator
1387 "\n<text:p text:style-name=\"OrgTitle\"/>"))
1388 (cond
1389 ((and author (not email))
1390 ;; author only
1391 (concat
1392 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1393 "OrgSubtitle"
1394 (format "<text:initial-creator>%s</text:initial-creator>" author))
1395 ;; separator
1396 "\n<text:p text:style-name=\"OrgSubtitle\"/>"))
1397 ((and author email)
1398 ;; author and email
1399 (concat
1400 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1401 "OrgSubtitle"
1402 (org-e-odt-format-link
1403 (format "<text:initial-creator>%s</text:initial-creator>" author)
1404 (concat "mailto:" email)))
1405 ;; separator
1406 "\n<text:p text:style-name=\"OrgSubtitle\"/>")))
1407 ;; date
1408 (when date
1409 (concat
1410 (format
1411 "\n<text:p text:style-name=\"%s\">%s</text:p>"
1412 "OrgSubtitle"
1413 (format
1414 "\n<text:date style:data-style-name=\"%s\" text:date-value=\"%s\">%s</text:date>"
1416 "N75" iso-date date))
1417 ;; separator
1418 "<text:p text:style-name=\"OrgSubtitle\"/>")))))
1420 ;; Table of Contents
1421 (let ((depth (plist-get info :with-toc)))
1422 (when (wholenump depth) (insert (org-e-odt-toc depth info))))
1423 ;; Contents.
1424 (insert contents)
1425 ;; Return contents.
1426 (buffer-substring-no-properties (point-min) (point-max))))
1430 ;;; Transcode Functions
1432 ;;;; Bold
1434 (defun org-e-odt-bold (bold contents info)
1435 "Transcode BOLD from Org to ODT.
1436 CONTENTS is the text with bold markup. INFO is a plist holding
1437 contextual information."
1438 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1439 "Bold" contents))
1442 ;;;; Center Block
1444 (defun org-e-odt-center-block (center-block contents info)
1445 "Transcode a CENTER-BLOCK element from Org to ODT.
1446 CONTENTS holds the contents of the center block. INFO is a plist
1447 holding contextual information."
1448 (org-e-odt--wrap-label center-block contents))
1451 ;;;; Clock
1453 (defun org-e-odt-clock (clock contents info)
1454 "Transcode a CLOCK element from Org to ODT.
1455 CONTENTS is nil. INFO is a plist used as a communication
1456 channel."
1457 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1458 "OrgTimestampWrapper"
1459 (concat
1460 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1461 "OrgTimestampKeyword" org-clock-string)
1462 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1463 "OrgTimestamp"
1464 (concat (org-translate-time
1465 (org-element-property :value clock))
1466 (let ((time (org-element-property :time clock)))
1467 (and time (format " (%s)" time))))))))
1470 ;;;; Code
1472 (defun org-e-odt-code (code contents info)
1473 "Transcode a CODE object from Org to ODT.
1474 CONTENTS is nil. INFO is a plist used as a communication
1475 channel."
1476 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1477 "OrgCode" (org-element-property :value code)))
1480 ;;;; Comment
1482 ;; Comments are ignored.
1485 ;;;; Comment Block
1487 ;; Comment Blocks are ignored.
1490 ;;;; Drawer
1492 (defun org-e-odt-drawer (drawer contents info)
1493 "Transcode a DRAWER element from Org to ODT.
1494 CONTENTS holds the contents of the block. INFO is a plist
1495 holding contextual information."
1496 (let* ((name (org-element-property :drawer-name drawer))
1497 (output (if (functionp org-e-odt-format-drawer-function)
1498 (funcall org-e-odt-format-drawer-function
1499 name contents)
1500 ;; If there's no user defined function: simply
1501 ;; display contents of the drawer.
1502 contents)))
1503 (org-e-odt--wrap-label drawer output)))
1506 ;;;; Dynamic Block
1508 (defun org-e-odt-dynamic-block (dynamic-block contents info)
1509 "Transcode a DYNAMIC-BLOCK element from Org to ODT.
1510 CONTENTS holds the contents of the block. INFO is a plist
1511 holding contextual information. See `org-export-data'."
1512 (org-e-odt--wrap-label dynamic-block contents))
1515 ;;;; Entity
1517 (defun org-e-odt-entity (entity contents info)
1518 "Transcode an ENTITY object from Org to ODT.
1519 CONTENTS are the definition itself. INFO is a plist holding
1520 contextual information."
1521 ;; (let ((ent (org-element-property :latex entity)))
1522 ;; (if (org-element-property :latex-math-p entity)
1523 ;; (format "$%s$" ent)
1524 ;; ent))
1525 (org-element-property :utf-8 entity))
1528 ;;;; Example Block
1530 (defun org-e-odt-example-block (example-block contents info)
1531 "Transcode a EXAMPLE-BLOCK element from Org to ODT.
1532 CONTENTS is nil. INFO is a plist holding contextual information."
1533 (org-e-odt--wrap-label
1534 example-block (org-e-odt-format-code example-block info)))
1537 ;;;; Export Snippet
1539 (defun org-e-odt-export-snippet (export-snippet contents info)
1540 "Transcode a EXPORT-SNIPPET object from Org to ODT.
1541 CONTENTS is nil. INFO is a plist holding contextual information."
1542 (when (eq (org-export-snippet-backend export-snippet) 'e-odt)
1543 (org-element-property :value export-snippet)))
1546 ;;;; Export Block
1548 (defun org-e-odt-export-block (export-block contents info)
1549 "Transcode a EXPORT-BLOCK element from Org to ODT.
1550 CONTENTS is nil. INFO is a plist holding contextual information."
1551 (when (string= (org-element-property :type export-block) "ODT")
1552 (org-remove-indentation (org-element-property :value export-block))))
1555 ;;;; Fixed Width
1557 (defun org-e-odt-fixed-width (fixed-width contents info)
1558 "Transcode a FIXED-WIDTH element from Org to ODT.
1559 CONTENTS is nil. INFO is a plist holding contextual information."
1560 (org-e-odt--wrap-label
1561 fixed-width (org-e-odt-do-format-code
1562 (org-element-property :value fixed-width))))
1565 ;;;; Footnote Definition
1567 ;; Footnote Definitions are ignored.
1570 ;;;; Footnote Reference
1572 (defun org-e-odt-footnote-reference (footnote-reference contents info)
1573 "Transcode a FOOTNOTE-REFERENCE element from Org to ODT.
1574 CONTENTS is nil. INFO is a plist holding contextual information."
1575 (let ((--format-footnote-definition
1576 (function
1577 (lambda (n def)
1578 (setq n (format "%d" n))
1579 (let ((id (concat "fn" n))
1580 (note-class "footnote")
1581 (par-style "Footnote"))
1582 (format
1583 "<text:note text:id=\"%s\" text:note-class=\"%s\">%s</text:note>"
1584 id note-class
1585 (concat
1586 (format "<text:note-citation>%s</text:note-citation>" n)
1587 (format "<text:note-body>%s</text:note-body>" def)))))))
1588 (--format-footnote-reference
1589 (function
1590 (lambda (n)
1591 (setq n (format "%d" n))
1592 (let ((note-class "footnote")
1593 (ref-format "text")
1594 (ref-name (concat "fn" n)))
1595 (format
1596 "<text:span text:style-name=\"%s\">%s</text:span>"
1597 "OrgSuperscript"
1598 (format "<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:note-ref>"
1599 note-class ref-format ref-name n)))))))
1600 (concat
1601 ;; Insert separator between two footnotes in a row.
1602 (let ((prev (org-export-get-previous-element footnote-reference)))
1603 (and (eq (org-element-type prev) 'footnote-reference)
1604 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1605 "OrgSuperscript" ",")))
1606 ;; Trancode footnote reference.
1607 (let ((n (org-export-get-footnote-number footnote-reference info)))
1608 (cond
1609 ((not (org-export-footnote-first-reference-p footnote-reference info))
1610 (funcall --format-footnote-reference n))
1611 ;; Inline definitions are secondary strings.
1612 ;; Non-inline footnotes definitions are full Org data.
1614 (let* ((raw (org-export-get-footnote-definition footnote-reference
1615 info))
1616 (def (let ((def (org-trim (org-export-data raw info))))
1617 (if (eq (org-element-type raw) 'org-data) def
1618 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1619 "Footnote" def)))))
1620 (funcall --format-footnote-definition n def))))))))
1623 ;;;; Headline
1625 (defun* org-e-odt-format-headline
1626 (todo todo-type priority text tags
1627 &key level section-number headline-label &allow-other-keys)
1628 (concat
1629 ;; Todo.
1630 (and todo
1631 (concat
1632 (let ((style (if (member todo org-done-keywords) "OrgDone" "OrgTodo")))
1633 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1634 style todo)) " "))
1635 ;; Title.
1636 text
1637 ;; Tags.
1638 (and tags
1639 (concat "<text:tab/>"
1640 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1641 "OrgTag" (mapconcat 'org-trim tags " : "))))))
1643 (defun org-e-odt-format-headline--wrap (headline info
1644 &optional format-function
1645 &rest extra-keys)
1646 "Transcode an HEADLINE element from Org to ODT.
1647 CONTENTS holds the contents of the headline. INFO is a plist
1648 holding contextual information."
1649 (let* ((level (+ (org-export-get-relative-level headline info)))
1650 (headline-number (org-export-get-headline-number headline info))
1651 (section-number (and (org-export-numbered-headline-p headline info)
1652 (mapconcat 'number-to-string
1653 headline-number ".")))
1654 (todo (and (plist-get info :with-todo-keywords)
1655 (let ((todo (org-element-property :todo-keyword headline)))
1656 (and todo (org-export-data todo info)))))
1657 (todo-type (and todo (org-element-property :todo-type headline)))
1658 (priority (and (plist-get info :with-priority)
1659 (org-element-property :priority headline)))
1660 (text (org-export-data (org-element-property :title headline) info))
1661 (tags (and (plist-get info :with-tags)
1662 (org-export-get-tags headline info)))
1663 (headline-label (concat "sec-" (mapconcat 'number-to-string
1664 headline-number "-")))
1665 (format-function (cond
1666 ((functionp format-function) format-function)
1667 ((functionp org-e-odt-format-headline-function)
1668 (function*
1669 (lambda (todo todo-type priority text tags
1670 &allow-other-keys)
1671 (funcall org-e-odt-format-headline-function
1672 todo todo-type priority text tags))))
1673 (t 'org-e-odt-format-headline))))
1674 (apply format-function
1675 todo todo-type priority text tags
1676 :headline-label headline-label :level level
1677 :section-number section-number extra-keys)))
1680 (defun org-e-odt-begin-plain-list (ltype &optional continue-numbering)
1681 (unless (member ltype '(ordered unordered descriptive))
1682 (error "Unknown list type: %s" ltype))
1683 (let ((style-name (assoc-default ltype
1684 '((ordered . "OrgNumberedList")
1685 (unordered . "OrgBulletedList")
1686 (descriptive . "OrgDescriptionList")))))
1687 (format "<text:list text:style-name=\"%s\" text:continue-numbering=\"%s\">"
1688 style-name (if continue-numbering "true" "false"))))
1690 (defun org-e-odt-headline (headline contents info)
1691 "Transcode an HEADLINE element from Org to ODT.
1692 CONTENTS holds the contents of the headline. INFO is a plist
1693 holding contextual information."
1694 (let* ((numberedp (org-export-numbered-headline-p headline info))
1695 ;; Get level relative to current parsed data.
1696 (level (org-export-get-relative-level headline info))
1697 (text (org-export-data (org-element-property :title headline) info))
1698 ;; Create the headline text.
1699 (full-text (org-e-odt-format-headline--wrap headline info)))
1700 (cond
1701 ;; Case 1: This is a footnote section: ignore it.
1702 ((org-element-property :footnote-section-p headline) nil)
1703 ;; Case 2. This is a deep sub-tree: export it as a list item.
1704 ;; Also export as items headlines for which no section
1705 ;; format has been found.
1706 ;; FIXME
1707 ;; ((org-export-low-level-p headline info)
1708 ;; ;; Build the real contents of the sub-tree.
1709 ;; (let* ((type (if numberedp 'unordered 'unordered)) ; FIXME
1710 ;; (itemized-body (org-e-odt-format-list-item
1711 ;; contents type nil nil full-text)))
1712 ;; (concat
1713 ;; (and (org-export-first-sibling-p headline)
1714 ;; (org-e-odt-begin-plain-list type))
1715 ;; itemized-body
1716 ;; (and (org-export-last-sibling-p headline)
1717 ;; "</text:list>"))))
1718 ;; Case 3. Standard headline. Export it as a section.
1720 (let* ((extra-ids (list (org-element-property :custom-id headline)
1721 (org-element-property :id headline)))
1722 (extra-ids nil) ; FIXME
1723 (id (concat "sec-" (mapconcat 'number-to-string
1724 (org-export-get-headline-number
1725 headline info) "-"))))
1726 (concat
1727 (format
1728 "\n<text:h text:style-name=\"%s\" text:outline-level=\"%s\">%s%s</text:h>"
1729 (format "Heading_20_%s" level)
1730 level
1731 ;; Extra targets.
1732 (mapconcat (lambda (x)
1733 (when x
1734 (let ((x (if (org-uuidgen-p x) (concat "ID-" x) x)))
1735 (org-e-odt-format-target
1736 "" (org-export-solidify-link-text x)))))
1737 extra-ids "")
1738 ;; Title.
1739 (org-e-odt-format-target full-text id))
1740 contents))))))
1743 ;;;; Horizontal Rule
1745 (defun org-e-odt-horizontal-rule (horizontal-rule contents info)
1746 "Transcode an HORIZONTAL-RULE object from Org to ODT.
1747 CONTENTS is nil. INFO is a plist holding contextual information."
1748 (org-e-odt--wrap-label
1749 horizontal-rule
1750 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1751 "Horizontal_20_Line" "")))
1754 ;;;; Inline Babel Call
1756 ;; Inline Babel Calls are ignored.
1759 ;;;; Inline Src Block
1761 (defun org-e-odt--find-verb-separator (s)
1762 "Return a character not used in string S.
1763 This is used to choose a separator for constructs like \\verb."
1764 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1765 (loop for c across ll
1766 when (not (string-match (regexp-quote (char-to-string c)) s))
1767 return (char-to-string c))))
1769 (defun org-e-odt-inline-src-block (inline-src-block contents info)
1770 "Transcode an INLINE-SRC-BLOCK element from Org to ODT.
1771 CONTENTS holds the contents of the item. INFO is a plist holding
1772 contextual information."
1773 (let* ((org-lang (org-element-property :language inline-src-block))
1774 (code (org-element-property :value inline-src-block))
1775 (separator (org-e-odt--find-verb-separator code)))
1776 (error "FIXME")))
1779 ;;;; Inlinetask
1781 (defun org-e-odt-inlinetask (inlinetask contents info)
1782 "Transcode an INLINETASK element from Org to ODT.
1783 CONTENTS holds the contents of the block. INFO is a plist
1784 holding contextual information."
1785 (cond
1786 ;; If `org-e-odt-format-inlinetask-function' is provided, call it
1787 ;; with appropriate arguments.
1788 ((functionp org-e-odt-format-inlinetask-function)
1789 (let ((format-function
1790 (function*
1791 (lambda (todo todo-type priority text tags
1792 &key contents &allow-other-keys)
1793 (funcall org-e-odt-format-inlinetask-function
1794 todo todo-type priority text tags contents)))))
1795 (org-e-odt-format-headline--wrap
1796 inlinetask info format-function :contents contents)))
1797 ;; Otherwise, use a default template.
1798 (t (org-e-odt--wrap-label
1799 inlinetask
1800 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1801 "Text_20_body"
1802 (org-e-odt--textbox
1803 (concat
1804 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1805 "OrgInlineTaskHeading"
1806 (org-e-odt-format-headline--wrap
1807 inlinetask info))
1808 contents)
1809 nil nil "OrgInlineTaskFrame" " style:rel-width=\"100%\""))))))
1811 ;;;; Italic
1813 (defun org-e-odt-italic (italic contents info)
1814 "Transcode ITALIC from Org to ODT.
1815 CONTENTS is the text with italic markup. INFO is a plist holding
1816 contextual information."
1817 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1818 "Emphasis" contents))
1821 ;;;; Item
1823 (defun org-e-odt-item (item contents info)
1824 "Transcode an ITEM element from Org to ODT.
1825 CONTENTS holds the contents of the item. INFO is a plist holding
1826 contextual information."
1827 (let* ((plain-list (org-export-get-parent item))
1828 (type (org-element-property :type plain-list))
1829 (counter (org-element-property :counter item))
1830 (tag (let ((tag (org-element-property :tag item)))
1831 (and tag
1832 (concat (org-e-odt--checkbox item)
1833 (org-export-data tag info))))))
1834 (case type
1835 ((ordered unordered)
1836 (format "\n<text:list-item>\n%s\n%s"
1837 contents
1838 (let* ((--element-has-a-table-p
1839 (function
1840 (lambda (element info)
1841 (loop for el in (org-element-contents element)
1842 thereis (eq (org-element-type el) 'table))))))
1843 (cond
1844 ((funcall --element-has-a-table-p item info)
1845 "</text:list-header>")
1846 (t "</text:list-item>")))))
1847 (descriptive
1848 (concat
1849 (let ((term (or tag "(no term)")))
1850 (concat
1851 (format "\n<text:list-item>\n%s\n</text:list-item>"
1852 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1853 "Text_20_body_20_bold" term))
1854 (format
1855 "\n<text:list-item>\n%s\n</text:list-item>"
1856 (format "\n<text:list text:style-name=\"%s\" %s>\n%s\n</text:list>"
1857 "OrgDescriptionList"
1858 "text:continue-numbering=\"false\""
1859 (format "\n<text:list-item>\n%s\n</text:list-item>"
1860 contents)))))))
1861 (t (error "Unknown list type: %S" type)))))
1864 ;;;; Keyword
1866 (defun org-e-odt-keyword (keyword contents info)
1867 "Transcode a KEYWORD element from Org to ODT.
1868 CONTENTS is nil. INFO is a plist holding contextual information."
1869 (let ((key (org-element-property :key keyword))
1870 (value (org-element-property :value keyword)))
1871 (cond
1872 ((string= key "LATEX") value)
1873 ((string= key "INDEX") (format "\\index{%s}" value))
1874 ((string= key "TARGET") nil ; FIXME
1875 ;; (format "\\label{%s}" (org-export-solidify-link-text value))
1877 ((string= key "toc")
1878 (let ((value (downcase value)))
1879 (cond
1880 ((string-match "\\<headlines\\>" value)
1881 (let ((depth (or (and (string-match "[0-9]+" value)
1882 (string-to-number (match-string 0 value)))
1883 (plist-get info :with-toc))))
1884 (when (wholenump depth) (org-e-odt-toc depth info))))
1885 ((string= "tables" value) "FIXME")
1886 ((string= "figures" value) "FIXME")
1887 ((string= "listings" value)
1888 (cond
1889 ;; At the moment, src blocks with a caption are wrapped
1890 ;; into a figure environment.
1891 (t "FIXME")))))))))
1894 ;;;; Latex Environment
1897 (eval-after-load 'org-odt
1898 '(ad-deactivate 'org-format-latex-as-mathml))
1900 ;; (defadvice org-format-latex-as-mathml ; FIXME
1901 ;; (after org-e-odt-protect-latex-fragment activate)
1902 ;; "Encode LaTeX fragment as XML.
1903 ;; Do this when translation to MathML fails."
1904 ;; (when (or (not (> (length ad-return-value) 0))
1905 ;; (get-text-property 0 'org-protected ad-return-value))
1906 ;; (setq ad-return-value
1907 ;; (org-propertize (org-e-odt-encode-plain-text (ad-get-arg 0))
1908 ;; 'org-protected t))))
1910 (defun org-e-odt-format-latex (latex-frag processing-type info)
1911 (let* ((prefix (case processing-type
1912 (dvipng "ltxpng/")
1913 (mathml "ltxmathml/")))
1914 (input-file (plist-get info :input-file))
1915 (cache-subdir
1916 (concat prefix (file-name-sans-extension
1917 (file-name-nondirectory input-file))))
1918 (cache-dir (file-name-directory input-file))
1919 (display-msg (case processing-type
1920 (dvipng "Creating LaTeX Image...")
1921 (mathml "Creating MathML snippet..."))))
1922 (with-temp-buffer
1923 (insert latex-frag)
1924 (org-format-latex cache-subdir cache-dir nil display-msg
1925 nil nil processing-type)
1926 (buffer-string))))
1928 (defun org-e-odt-latex-environment (latex-environment contents info)
1929 "Transcode a LATEX-ENVIRONMENT element from Org to ODT.
1930 CONTENTS is nil. INFO is a plist holding contextual information."
1931 (org-e-odt--wrap-label
1932 latex-environment
1933 (let* ((latex-frag
1934 (org-remove-indentation
1935 (org-element-property :value latex-environment)))
1936 (processing-type (plist-get info :LaTeX-fragments))
1937 (caption (org-element-property :caption latex-environment))
1938 (short-caption (and (cdr caption)
1939 (org-export-data (cdr caption) info)))
1940 (caption (and (car caption) (org-export-data (car caption) info)))
1941 (label (org-element-property :name latex-environment))
1942 (attr nil) ; FIXME
1943 (label (org-element-property :name latex-environment)))
1945 (when (memq processing-type '(t mathjax))
1946 (unless (and (fboundp 'org-format-latex-mathml-available-p)
1947 (org-format-latex-mathml-available-p))
1948 (message "LaTeX to MathML converter not available. Trying dvinpng...")
1949 (setq processing-type 'dvipng)))
1951 (when (eq processing-type 'dvipng)
1952 (unless (and (org-check-external-command "latex" "" t)
1953 (org-check-external-command "dvipng" "" t))
1954 (message "LaTeX to PNG converter not available. Using verbatim.")
1955 (setq processing-type 'verbatim)))
1957 (case processing-type
1958 ((t mathjax)
1959 (org-e-odt-format-formula latex-environment info))
1960 (dvipng
1961 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1962 "Text_20_body"
1963 (org-e-odt-link--inline-image latex-environment info)))
1964 (t (org-e-odt-do-format-code latex-frag))))))
1967 ;;;; Latex Fragment
1970 ;; (when latex-frag ; FIXME
1971 ;; (setq href (org-propertize href :title "LaTeX Fragment"
1972 ;; :description latex-frag)))
1973 ;; handle verbatim
1974 ;; provide descriptions
1976 (defun org-e-odt-latex-fragment (latex-fragment contents info)
1977 "Transcode a LATEX-FRAGMENT object from Org to ODT.
1978 CONTENTS is nil. INFO is a plist holding contextual information."
1979 (let* ((latex-frag (org-element-property :value latex-fragment))
1980 (processing-type (plist-get info :LaTeX-fragments)))
1981 (cond
1982 ((member processing-type '(t mathjax))
1983 (org-e-odt-format-formula latex-fragment info))
1984 ((eq processing-type 'dvipng)
1985 (org-e-odt-link--inline-image latex-fragment info))
1986 (t (org-e-odt-encode-plain-text latex-frag t)))))
1989 ;;;; Line Break
1991 (defun org-e-odt-line-break (line-break contents info)
1992 "Transcode a LINE-BREAK object from Org to ODT.
1993 CONTENTS is nil. INFO is a plist holding contextual information."
1994 "<text:line-break/>\n")
1997 ;;;; Link
2001 ;;;; Links :: Generic
2003 (defun org-e-odt-format-link (desc href &optional suppress-xref)
2004 (cond
2005 ((and (= (string-to-char href) ?#) (not suppress-xref))
2006 (setq href (substring href 1))
2007 (let ((xref-format "text"))
2008 (when (numberp desc)
2009 (setq desc (format "%d" desc) xref-format "number"))
2010 (when (listp desc)
2011 (setq desc (mapconcat 'number-to-string desc ".") xref-format "chapter"))
2012 (setq href (concat org-e-odt-bookmark-prefix href))
2013 (format
2014 "<text:bookmark-ref text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:bookmark-ref>"
2015 xref-format href desc)))
2016 ;; (org-lparse-link-description-is-image
2017 ;; (format "\n<draw:a xlink:type=\"simple\" xlink:href=\"%s\">\n%s\n</draw:a>"
2018 ;; href desc))
2019 (t (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
2020 href desc))))
2022 (defun org-e-odt-format-internal-link (text href)
2023 (org-e-odt-format-link text (concat "#" href)))
2025 ;;;; Links :: Label references
2027 (defun org-e-odt-enumerate-element (element info &optional predicate n)
2028 (let* ((--numbered-parent-headline-at-<=-n
2029 (function
2030 (lambda (element n info)
2031 (loop for x in (org-export-get-genealogy element)
2032 thereis (and (eq (org-element-type x) 'headline)
2033 (<= (org-export-get-relative-level x info) n)
2034 (org-export-numbered-headline-p x info)
2035 x)))))
2036 (--enumerate
2037 (function
2038 (lambda (element scope info &optional predicate)
2039 (let ((counter 0))
2040 (org-element-map
2041 (or scope (plist-get info :parse-tree))
2042 (org-element-type element)
2043 (lambda (el)
2044 (and (or (not predicate) (funcall predicate el info))
2045 (incf counter)
2046 (eq element el)
2047 counter))
2048 info 'first-match)))))
2049 (scope (funcall --numbered-parent-headline-at-<=-n
2050 element (or n org-e-odt-display-outline-level) info))
2051 (ordinal (funcall --enumerate element scope info predicate))
2052 (tag
2053 (concat
2054 ;; Section number.
2055 (and scope
2056 (mapconcat 'number-to-string
2057 (org-export-get-headline-number scope info) "."))
2058 ;; Separator.
2059 (and scope ".")
2060 ;; Ordinal.
2061 (number-to-string ordinal))))
2062 tag))
2064 (defun org-e-odt-format-label (element info op)
2065 (let* ((caption-from
2066 (case (org-element-type element)
2067 (link (org-export-get-parent-element element))
2068 (t element)))
2069 ;; get label and caption.
2070 (label (org-element-property :name caption-from))
2071 (caption (org-element-property :caption caption-from))
2072 (short-caption (cdr caption))
2073 ;; transcode captions.
2074 (caption (and (car caption) (org-export-data (car caption) info)))
2075 (short-caption (and short-caption
2076 (org-export-data short-caption info))))
2077 (when (or label caption)
2078 (let* ((default-category
2079 (cond
2080 ((eq (org-element-type element) 'table)
2081 "__Table__")
2082 ((org-e-odt-standalone-image-p element info)
2083 "__Figure__")
2084 ((member (org-element-type element)
2085 '(latex-environment latex-fragment))
2086 (let ((processing-type (plist-get info :LaTeX-fragments)))
2087 (cond
2088 ((eq processing-type 'dvipng) "__DvipngImage__")
2089 ((eq processing-type 'mathjax) "__MathFormula__")
2090 ((eq processing-type 't) "__MathFormula__")
2091 (t (error "Handle LaTeX:verbatim")))))
2092 ((eq (org-element-type element) 'src-block)
2093 "__Listing__")
2094 (t (error "Handle enumeration of %S" element))))
2095 (predicate
2096 (cond
2097 ((member (org-element-type element)
2098 '(table latex-environment src-block))
2099 nil)
2100 ((org-e-odt-standalone-image-p element info)
2101 'org-e-odt-standalone-image-p)
2102 (t (error "Handle enumeration of %S" element))))
2103 (seqno (org-e-odt-enumerate-element
2104 element info predicate)) ; FIXME
2105 ;; handle label props.
2106 (label-props (assoc default-category org-e-odt-category-map-alist))
2107 ;; identify opendocument counter
2108 (counter (nth 1 label-props))
2109 ;; identify label style
2110 (label-style (nth 2 label-props))
2111 ;; retrieve localized category sting
2112 (category (org-export-translate (nth 3 label-props) :utf-8 info)))
2113 (case op
2114 (definition
2115 ;; assign an internal label, if user has not provided one
2116 (setq label (or label (format "%s-%s" default-category seqno)))
2117 (setq label (org-export-solidify-link-text label))
2119 (cons
2120 (format-spec
2121 (cadr (assoc-string label-style org-e-odt-label-styles t))
2122 `((?e . ,category)
2123 (?n . ,(format
2124 "<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">%s</text:sequence>"
2125 label counter counter seqno))
2126 (?c . ,(or caption ""))))
2127 short-caption))
2128 (reference
2129 (assert label)
2130 (setq label (org-export-solidify-link-text label))
2131 (let* ((fmt (cddr (assoc-string label-style org-e-odt-label-styles t)))
2132 (fmt1 (car fmt))
2133 (fmt2 (cadr fmt)))
2134 (format "<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:sequence-ref>"
2135 fmt1 label (format-spec fmt2 `((?e . ,category)
2136 (?n . ,seqno))))))
2137 (t (error "Unknow %S on label" op)))))))
2139 ;;;; Links :: Embedded images
2141 (defun org-e-odt-copy-image-file (path)
2142 "Returns the internal name of the file"
2143 (let* ((image-type (file-name-extension path))
2144 (media-type (format "image/%s" image-type))
2145 (target-dir "Images/")
2146 (target-file
2147 (format "%s%04d.%s" target-dir
2148 (incf org-e-odt-embedded-images-count) image-type)))
2149 (message "Embedding %s as %s ..."
2150 (substring-no-properties path) target-file)
2152 (when (= 1 org-e-odt-embedded-images-count)
2153 (make-directory (concat org-e-odt-zip-dir target-dir))
2154 (org-e-odt-create-manifest-file-entry "" target-dir))
2156 (copy-file path (concat org-e-odt-zip-dir target-file) 'overwrite)
2157 (org-e-odt-create-manifest-file-entry media-type target-file)
2158 target-file))
2160 (defun org-e-odt-image-size-from-file (file &optional user-width
2161 user-height scale dpi embed-as)
2162 (let* ((--pixels-to-cms
2163 (function (lambda (pixels dpi)
2164 (let ((cms-per-inch 2.54)
2165 (inches (/ pixels dpi)))
2166 (* cms-per-inch inches)))))
2167 (--size-in-cms
2168 (function
2169 (lambda (size-in-pixels dpi)
2170 (and size-in-pixels
2171 (cons (funcall --pixels-to-cms (car size-in-pixels) dpi)
2172 (funcall --pixels-to-cms (cdr size-in-pixels) dpi))))))
2173 (dpi (or dpi org-e-odt-pixels-per-inch))
2174 (anchor-type (or embed-as "paragraph"))
2175 (user-width (and (not scale) user-width))
2176 (user-height (and (not scale) user-height))
2177 (size
2178 (and
2179 (not (and user-height user-width))
2181 ;; Use Imagemagick.
2182 (and (executable-find "identify")
2183 (let ((size-in-pixels
2184 (let ((dim (shell-command-to-string
2185 (format "identify -format \"%%w:%%h\" \"%s\""
2186 file))))
2187 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
2188 (cons (string-to-number (match-string 1 dim))
2189 (string-to-number (match-string 2 dim)))))))
2190 (funcall --size-in-cms size-in-pixels dpi)))
2191 ;; Use Emacs.
2192 (let ((size-in-pixels
2193 (ignore-errors ; Emacs could be in batch mode
2194 (clear-image-cache)
2195 (image-size (create-image file) 'pixels))))
2196 (funcall --size-in-cms size-in-pixels dpi))
2197 ;; Use hard-coded values.
2198 (cdr (assoc-string anchor-type
2199 org-e-odt-default-image-sizes-alist))
2200 ;; Error out.
2201 (error "Cannot determine Image size. Aborting ..."))))
2202 (width (car size)) (height (cdr size)))
2203 (cond
2204 (scale
2205 (setq width (* width scale) height (* height scale)))
2206 ((and user-height user-width)
2207 (setq width user-width height user-height))
2208 (user-height
2209 (setq width (* user-height (/ width height)) height user-height))
2210 (user-width
2211 (setq height (* user-width (/ height width)) width user-width))
2212 (t (ignore)))
2213 ;; ensure that an embedded image fits comfortably within a page
2214 (let ((max-width (car org-e-odt-max-image-size))
2215 (max-height (cdr org-e-odt-max-image-size)))
2216 (when (or (> width max-width) (> height max-height))
2217 (let* ((scale1 (/ max-width width))
2218 (scale2 (/ max-height height))
2219 (scale (min scale1 scale2)))
2220 (setq width (* scale width) height (* scale height)))))
2221 (cons width height)))
2223 ;;;; Links :: Math formula
2225 (defun org-e-odt-format-formula (element info)
2226 (let* ((src (cond
2227 ((eq (org-element-type element) 'link) ; FIXME
2228 (let* ((type (org-element-property :type element))
2229 (raw-path (org-element-property :path element)))
2230 (cond
2231 ((file-name-absolute-p raw-path)
2232 (expand-file-name raw-path))
2233 (t raw-path))))
2234 ((member (org-element-type element)
2235 '(latex-fragment latex-environment))
2236 (let* ((latex-frag (org-remove-indentation
2237 (org-element-property :value element)))
2238 (formula-link (org-e-odt-format-latex
2239 latex-frag 'mathml info)))
2240 (and formula-link
2241 (string-match "file:\\([^]]*\\)" formula-link)
2242 (match-string 1 formula-link))))
2243 (t (error "what is this?"))))
2244 (full-src (if (file-name-absolute-p src) src
2245 (expand-file-name src (file-name-directory
2246 (plist-get info :input-file)))))
2247 (caption-from
2248 (case (org-element-type element)
2249 (link (org-export-get-parent-element element))
2250 (t element)))
2251 (captions (org-e-odt-format-label caption-from info 'definition))
2252 (caption (car captions))
2253 (href
2254 (format "\n<draw:object %s xlink:href=\"%s\" xlink:type=\"simple\"/>"
2255 " xlink:show=\"embed\" xlink:actuate=\"onLoad\""
2256 (file-name-directory (org-e-odt-copy-formula-file full-src))))
2257 (embed-as (if caption 'paragraph 'character))
2258 width height)
2259 (cond
2260 ((eq embed-as 'character)
2261 (org-e-odt-format-entity "InlineFormula" href width height))
2263 (let* ((equation (org-e-odt-format-entity
2264 "CaptionedDisplayFormula" href width height captions))
2265 (label
2266 (let* ((org-e-odt-category-map-alist
2267 '(("__Table__" "Table" "value")
2268 ("__Figure__" "Illustration" "value")
2269 ("__MathFormula__" "Text" "math-label")
2270 ("__DvipngImage__" "Equation" "value")
2271 ("__Listing__" "Listing" "value"))))
2272 (car (org-e-odt-format-label caption-from info 'definition))))
2273 (formula-tree
2274 (org-e-odt--adopt-elements
2275 `(table (:type org :attr_odt (":style \"OrgEquation\"")))
2276 (org-e-odt--adopt-elements
2277 `(table-row (:type standard))
2278 `(table-cell nil "<c8>") `(table-cell nil "<c1>"))
2279 (org-e-odt--adopt-elements
2280 `(table-row (:type standard))
2281 (org-e-odt--adopt-elements
2282 `(table-cell nil) `(export-block
2283 (:type "ODT" :value ,equation)))
2284 (org-e-odt--adopt-elements
2285 `(table-cell nil) `(export-block
2286 (:type "ODT" :value ,label))))))
2287 (formula-info
2288 (org-export-collect-tree-properties
2289 formula-tree (org-export-get-environment 'e-odt))))
2290 (org-export-data formula-tree formula-info))))))
2292 (defun org-e-odt-copy-formula-file (src-file)
2293 "Returns the internal name of the file"
2294 (let* ((target-dir (format "Formula-%04d/"
2295 (incf org-e-odt-embedded-formulas-count)))
2296 (target-file (concat target-dir "content.xml")))
2297 ;; Create a directory for holding formula file. Also enter it in
2298 ;; to manifest.
2299 (make-directory (concat org-e-odt-zip-dir target-dir))
2300 (org-e-odt-create-manifest-file-entry
2301 "application/vnd.oasis.opendocument.formula" target-dir "1.2")
2302 ;; Copy over the formula file from user directory to zip
2303 ;; directory.
2304 (message "Embedding %s as %s ..." src-file target-file)
2305 (let ((case-fold-search nil))
2306 (cond
2307 ;; Case 1: Mathml.
2308 ((string-match "\\.\\(mathml\\|mml\\)\\'" src-file)
2309 (copy-file src-file (concat org-e-odt-zip-dir target-file) 'overwrite))
2310 ;; Case 2: OpenDocument formula.
2311 ((string-match "\\.odf\\'" src-file)
2312 (org-e-odt--zip-extract src-file "content.xml"
2313 (concat org-e-odt-zip-dir target-dir)))
2314 (t (error "%s is not a formula file" src-file))))
2315 ;; Enter the formula file in to manifest.
2316 (org-e-odt-create-manifest-file-entry "text/xml" target-file)
2317 target-file))
2319 ;;;; Targets
2321 (defun org-e-odt-format-target (text id)
2322 (let ((name (concat org-e-odt-bookmark-prefix id)))
2323 (concat
2324 (and id (format "\n<text:bookmark-start text:name=\"%s\"/>" name))
2325 (concat (and id (format "\n<text:bookmark text:name=\"%s\"/>" id)) text)
2326 (and id (format "\n<text:bookmark-end text:name=\"%s\"/>" name)))))
2328 (defun org-e-odt-link--inline-image (element info)
2329 "Return HTML code for an inline image.
2330 LINK is the link pointing to the inline image. INFO is a plist
2331 used as a communication channel."
2332 (let* ((src (cond
2333 ((eq (org-element-type element) 'link)
2334 (let* ((type (org-element-property :type element))
2335 (raw-path (org-element-property :path element)))
2336 (cond ((member type '("http" "https"))
2337 (concat type ":" raw-path))
2338 ((file-name-absolute-p raw-path)
2339 (expand-file-name raw-path))
2340 (t raw-path))))
2341 ((member (org-element-type element)
2342 '(latex-fragment latex-environment))
2343 (let* ((latex-frag (org-remove-indentation
2344 (org-element-property :value element)))
2345 (formula-link (org-e-odt-format-latex
2346 latex-frag 'dvipng info)))
2347 (and formula-link
2348 (string-match "file:\\([^]]*\\)" formula-link)
2349 (match-string 1 formula-link))))
2350 (t (error "what is this?"))))
2351 (src-expanded (if (file-name-absolute-p src) src
2352 (expand-file-name src (file-name-directory
2353 (plist-get info :input-file)))))
2354 (href (format
2355 "\n<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>"
2356 (org-e-odt-copy-image-file src-expanded)))
2357 ;; extract attributes from #+ATTR_ODT line.
2358 (attr-from (case (org-element-type element)
2359 (link (org-export-get-parent-element element))
2360 (t element)))
2361 ;; convert attributes to a plist.
2362 (attr-plist (org-export-read-attribute :attr_odt attr-from))
2363 ;; handle `:anchor', `:style' and `:attributes' properties.
2364 (user-frame-anchor
2365 (car (assoc-string (plist-get attr-plist :anchor)
2366 '(("as-char") ("paragraph") ("page")) t)))
2367 (user-frame-style
2368 (and user-frame-anchor (plist-get attr-plist :style)))
2369 (user-frame-attrs
2370 (and user-frame-anchor (plist-get attr-plist :attributes)))
2371 (user-frame-params
2372 (list user-frame-style user-frame-attrs user-frame-anchor))
2373 ;; (embed-as (or embed-as user-frame-anchor "paragraph"))
2374 ;; extrac
2375 ;; handle `:width', `:height' and `:scale' properties.
2376 (size (org-e-odt-image-size-from-file
2377 src-expanded (plist-get attr-plist :width)
2378 (plist-get attr-plist :height)
2379 (plist-get attr-plist :scale) nil ;; embed-as
2380 "paragraph" ; FIXME
2382 (width (car size)) (height (cdr size))
2383 (embed-as
2384 (case (org-element-type element)
2385 ((org-e-odt-standalone-image-p element info) "paragraph")
2386 (latex-fragment "as-char")
2387 (latex-environment "paragraph")
2388 (t "paragraph")))
2389 (captions (org-e-odt-format-label element info 'definition))
2390 (caption (car captions)) (short-caption (cdr captions))
2391 (entity (concat (and caption "Captioned") embed-as "Image")))
2392 (org-e-odt-format-entity entity href width height
2393 captions user-frame-params )))
2395 (defun org-e-odt-format-entity (entity href width height &optional
2396 captions user-frame-params)
2397 (let* ((caption (car captions)) (short-caption (cdr captions))
2398 (entity-style (assoc-string entity org-e-odt-entity-frame-styles t))
2399 default-frame-params frame-params
2400 (--merge-frame-params
2401 (function
2402 (lambda (default-frame-params user-frame-params)
2403 (if (not user-frame-params) default-frame-params
2404 (assert (= (length default-frame-params) 3))
2405 (assert (= (length user-frame-params) 3))
2406 (loop for user-frame-param in user-frame-params
2407 for default-frame-param in default-frame-params
2408 collect (or user-frame-param default-frame-param)))))))
2409 (cond
2410 ((not caption)
2411 (setq default-frame-params (nth 2 entity-style))
2412 (setq frame-params (funcall --merge-frame-params
2413 default-frame-params user-frame-params))
2414 (apply 'org-e-odt--frame href width height frame-params))
2416 (setq default-frame-params (nth 3 entity-style))
2417 (setq frame-params (funcall --merge-frame-params
2418 default-frame-params user-frame-params))
2419 (apply 'org-e-odt--textbox
2420 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2421 "Illustration"
2422 (concat
2423 (apply 'org-e-odt--frame href width height
2424 (let ((entity-style-1 (copy-sequence
2425 (nth 2 entity-style))))
2426 (setcar (cdr entity-style-1)
2427 (concat
2428 (cadr entity-style-1)
2429 (and short-caption
2430 (format " draw:name=\"%s\" "
2431 short-caption))))
2432 entity-style-1))
2433 caption))
2434 width height frame-params)))))
2436 (defun org-e-odt-standalone-image-p (element info)
2437 "Test if ELEMENT is a standalone image for the purpose ODT export.
2438 INFO is a plist holding contextual information.
2440 Return non-nil, if ELEMENT is of type paragraph and it's sole
2441 content, save for whitespaces, is a link that qualifies as an
2442 inline image.
2444 Return non-nil, if ELEMENT is of type link and it's containing
2445 paragraph has no other content save for leading and trailing
2446 whitespaces.
2448 Return nil, otherwise.
2450 Bind `org-e-odt-standalone-image-predicate' to constrain
2451 paragraph further. For example, to check for only captioned
2452 standalone images, do the following.
2454 \(setq org-e-odt-standalone-image-predicate
2455 \(lambda \(paragraph\)
2456 \(org-element-property :caption paragraph\)\)\)
2458 (let ((--standalone-image-predicate
2459 (function (lambda (paragraph)
2460 (or (org-element-property :caption paragraph)
2461 (org-element-property :name paragraph)))))
2462 (paragraph (case (org-element-type element)
2463 (paragraph element)
2464 (link (and (org-export-inline-image-p
2465 element org-e-odt-inline-image-rules)
2466 (org-export-get-parent element)))
2467 (t nil))))
2468 (when paragraph
2469 (assert (eq (org-element-type paragraph) 'paragraph))
2470 (when (funcall --standalone-image-predicate paragraph)
2471 (let ((contents (org-element-contents paragraph)))
2472 (loop for x in contents
2473 with inline-image-count = 0
2474 always (cond
2475 ((eq (org-element-type x) 'plain-text)
2476 (not (org-string-nw-p x)))
2477 ((eq (org-element-type x) 'link)
2478 (when (org-export-inline-image-p
2479 x org-e-odt-inline-image-rules)
2480 (= (incf inline-image-count) 1)))
2481 (t nil))))))))
2483 (defun org-e-odt-link (link desc info)
2484 "Transcode a LINK object from Org to ODT.
2486 DESC is the description part of the link, or the empty string.
2487 INFO is a plist holding contextual information. See
2488 `org-export-data'."
2489 (let* ((type (org-element-property :type link))
2490 (raw-path (org-element-property :path link))
2491 ;; Ensure DESC really exists, or set it to nil.
2492 (desc (and (not (string= desc "")) desc))
2493 (imagep (org-export-inline-image-p
2494 link org-e-odt-inline-image-rules))
2495 (path (cond
2496 ((member type '("http" "https" "ftp" "mailto"))
2497 (concat type ":" raw-path))
2498 ((string= type "file")
2499 (when (string-match "\\(.+\\)::.+" raw-path)
2500 (setq raw-path (match-string 1 raw-path)))
2501 (if (file-name-absolute-p raw-path)
2502 (concat "file://" (expand-file-name raw-path))
2503 (concat "file://" raw-path)))
2504 (t raw-path)))
2505 protocol)
2506 (cond
2507 ;; Image file.
2508 ((and (not desc) (org-export-inline-image-p
2509 link org-e-odt-inline-image-rules))
2510 (org-e-odt-link--inline-image link info))
2511 ;; Radio target: Transcode target's contents and use them as
2512 ;; link's description.
2513 ((string= type "radio")
2514 (let ((destination (org-export-resolve-radio-link link info)))
2515 (when destination
2516 (org-e-odt-format-internal-link
2517 (org-export-data (org-element-contents destination) info)
2518 (org-export-solidify-link-text path)))))
2519 ;; Links pointing to an headline: Find destination and build
2520 ;; appropriate referencing command.
2521 ((member type '("custom-id" "fuzzy" "id"))
2522 (let ((destination (if (string= type "fuzzy")
2523 (org-export-resolve-fuzzy-link link info)
2524 (org-export-resolve-id-link link info))))
2525 (case (org-element-type destination)
2526 ;; Fuzzy link points nowhere.
2527 ('nil
2528 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2529 "Emphasis" (or desc (org-export-data
2530 (org-element-property
2531 :raw-link link) info))))
2532 ;; Fuzzy link points to an invisible target.
2533 (keyword nil)
2534 ;; LINK points to an headline. Check if LINK should display
2535 ;; section numbers.
2536 (headline
2537 (let* ((headline-no (org-export-get-headline-number destination info))
2538 (label (format "sec-%s" (mapconcat 'number-to-string
2539 headline-no "-")))
2540 (desc
2541 ;; Case 1: Headline is numbered and LINK has no
2542 ;; description or LINK's description matches
2543 ;; headline's title. Display section number.
2544 (if (and (org-export-numbered-headline-p destination info)
2545 (or (not desc)
2546 (string= desc (org-element-property
2547 :raw-value destination))))
2548 headline-no
2549 ;; Case 2: Either the headline is un-numbered or
2550 ;; LINK has a custom description. Display LINK's
2551 ;; description or headline's title.
2552 (or desc (org-export-data (org-element-property
2553 :title destination) info)))))
2554 (org-e-odt-format-internal-link desc label)))
2555 ;; Fuzzy link points to a target. Do as above.
2556 (otherwise
2557 ;; (unless desc
2558 ;; (setq number (cond
2559 ;; ((org-e-odt-standalone-image-p destination info)
2560 ;; (org-export-get-ordinal
2561 ;; (assoc 'link (org-element-contents destination))
2562 ;; info 'link 'org-e-odt-standalone-image-p))
2563 ;; (t (org-export-get-ordinal destination info))))
2564 ;; (setq desc (when number
2565 ;; (if (atom number) (number-to-string number)
2566 ;; (mapconcat 'number-to-string number ".")))))
2568 (let ((label-reference
2569 (org-e-odt-format-label destination info 'reference)))
2570 (assert label-reference)
2571 label-reference)))))
2572 ;; Coderef: replace link with the reference name or the
2573 ;; equivalent line number.
2574 ((string= type "coderef")
2575 (let* ((fmt (org-export-get-coderef-format path desc))
2576 (res (org-export-resolve-coderef path info))
2577 (href (concat "#coderef-" path)))
2578 (format fmt (org-e-odt-format-link res href))))
2579 ;; Link type is handled by a special function.
2580 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
2581 (funcall protocol (org-link-unescape path) desc 'odt))
2582 ;; External link with a description part.
2583 ((and path desc) (org-e-odt-format-link desc path))
2584 ;; External link without a description part.
2585 (path (org-e-odt-format-link path path))
2586 ;; No path, only description. Try to do something useful.
2587 (t (format "<text:span text:style-name=\"%s\">%s</text:span>"
2588 "Emphasis" desc)))))
2591 ;;;; Babel Call
2593 ;; Babel Calls are ignored.
2596 ;;;; Macro
2598 (defun org-e-odt-macro (macro contents info)
2599 "Transcode a MACRO element from Org to ODT.
2600 CONTENTS is nil. INFO is a plist holding contextual information."
2601 ;; Use available tools.
2602 (org-export-expand-macro macro info))
2605 ;;;; Paragraph
2607 (defun org-e-odt-paragraph (paragraph contents info)
2608 "Transcode a PARAGRAPH element from Org to ODT.
2609 CONTENTS is the contents of the paragraph, as a string. INFO is
2610 the plist used as a communication channel."
2611 (let* ((parent (org-export-get-parent paragraph))
2612 (parent-type (org-element-type parent))
2613 (style (case parent-type
2614 (quote-block "Quotations")
2615 (center-block "OrgCenter")
2616 (footnote-definition "Footnote")
2617 (t "Text_20_body"))))
2618 ;; If this paragraph is a leading paragraph in a non-descriptive
2619 ;; item and the item has a checkbox, splice the checkbox and
2620 ;; paragraph contents together.
2621 (when (and (eq (org-element-type parent) 'item)
2622 (not (eq (org-element-property :type
2623 (org-export-get-parent parent))
2624 'descriptive))
2625 (eq paragraph (car (org-element-contents parent))))
2626 (setq contents (concat (org-e-odt--checkbox parent) contents)))
2627 (assert style)
2628 (format "\n<text:p text:style-name=\"%s\">%s</text:p>" style contents)))
2631 ;;;; Plain List
2633 (defun org-e-odt-plain-list (plain-list contents info)
2634 "Transcode a PLAIN-LIST element from Org to ODT.
2635 CONTENTS is the contents of the list. INFO is a plist holding
2636 contextual information."
2637 (let* ((type (org-element-property :type plain-list))
2638 (continue-numbering nil))
2639 (assert (member type '(ordered unordered descriptive)))
2640 (org-e-odt--wrap-label
2641 plain-list
2642 (format "\n<text:list text:style-name=\"%s\" %s>\n%s</text:list>"
2643 (assoc-default type '((ordered . "OrgNumberedList")
2644 (unordered . "OrgBulletedList")
2645 (descriptive . "OrgDescriptionList")))
2646 ;; If top-level list, re-start numbering. Otherwise,
2647 ;; continue numbering.
2648 (format "text:continue-numbering=\"%s\""
2649 (let* ((parent (org-export-get-parent plain-list)))
2650 (if (and parent (eq (org-element-type parent) 'item))
2651 "true" "false")))
2652 contents))))
2654 ;;;; Plain Text
2656 (defun org-e-odt-fill-tabs-and-spaces (line)
2657 (replace-regexp-in-string
2658 "\\([\t]\\|\\([ ]+\\)\\)"
2659 (lambda (s)
2660 (cond
2661 ((string= s "\t") "<text:tab/>")
2662 (t (let ((n (length s)))
2663 (cond
2664 ((= n 1) " ")
2665 ((> n 1) (concat " " (format "<text:s text:c=\"%d\"/>" (1- n))))
2666 (t ""))))))
2667 line))
2669 (defun org-e-odt-encode-plain-text (text &optional no-whitespace-filling)
2670 (mapc
2671 (lambda (pair)
2672 (setq text (replace-regexp-in-string (car pair) (cdr pair) text t t)))
2673 '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
2674 (if no-whitespace-filling text
2675 (org-e-odt-fill-tabs-and-spaces text)))
2677 (defun org-e-odt--quotation-marks (text info)
2678 "Export quotation marks depending on language conventions.
2679 TEXT is a string containing quotation marks to be replaced. INFO
2680 is a plist used as a communication channel."
2681 (mapc (lambda(l)
2682 (let ((start 0))
2683 (while (setq start (string-match (car l) text start))
2684 (let ((new-quote (concat (match-string 1 text) (cdr l))))
2685 (setq text (replace-match new-quote t t text))))))
2686 (cdr (or (assoc (plist-get info :language) org-e-odt-quotes)
2687 ;; Falls back on English.
2688 (assoc "en" org-e-odt-quotes))))
2689 text)
2691 (defun org-e-odt-plain-text (text info)
2692 "Transcode a TEXT string from Org to ODT.
2693 TEXT is the string to transcode. INFO is a plist holding
2694 contextual information."
2695 ;; Protect &, < and >.
2696 (setq text (org-e-odt-encode-plain-text text t))
2697 ;; Handle quotation marks
2698 (setq text (org-e-odt--quotation-marks text info))
2699 ;; Convert special strings.
2700 (when (plist-get info :with-special-strings)
2701 (mapc
2702 (lambda (pair)
2703 (setq text (replace-regexp-in-string (car pair) (cdr pair) text t nil)))
2704 org-e-odt-special-string-regexps))
2705 ;; Handle break preservation if required.
2706 (when (plist-get info :preserve-breaks)
2707 (setq text (replace-regexp-in-string
2708 "\\(\\\\\\\\\\)?[ \t]*\n" "<text:line-break/>\n" text t)))
2709 ;; Return value.
2710 text)
2713 ;;;; Planning
2715 (defun org-e-odt-planning (planning contents info)
2716 "Transcode a PLANNING element from Org to ODT.
2717 CONTENTS is nil. INFO is a plist used as a communication
2718 channel."
2719 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2720 "OrgTimestampWrapper"
2721 (concat
2722 (let ((closed (org-element-property :closed planning)))
2723 (when closed
2724 (concat
2725 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2726 "OrgTimestampKeyword" org-closed-string)
2727 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2728 "OrgTimestamp" (org-translate-time closed)))))
2729 (let ((deadline (org-element-property :deadline planning)))
2730 (when deadline
2731 (concat
2732 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2733 "OrgTimestampKeyword" org-deadline-string)
2734 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2735 "OrgTimestamp" (org-translate-time deadline)))))
2736 (let ((scheduled (org-element-property :scheduled planning)))
2737 (when scheduled
2738 (concat
2739 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2740 "OrgTimestampKeyword" org-scheduled-string)
2741 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2742 "OrgTimestamp" (org-translate-time scheduled))))))))
2745 ;;;; Property Drawer
2747 (defun org-e-odt-property-drawer (property-drawer contents info)
2748 "Transcode a PROPERTY-DRAWER element from Org to ODT.
2749 CONTENTS is nil. INFO is a plist holding contextual
2750 information."
2751 ;; The property drawer isn't exported but we want separating blank
2752 ;; lines nonetheless.
2756 ;;;; Quote Block
2758 (defun org-e-odt-quote-block (quote-block contents info)
2759 "Transcode a QUOTE-BLOCK element from Org to ODT.
2760 CONTENTS holds the contents of the block. INFO is a plist
2761 holding contextual information."
2762 (org-e-odt--wrap-label quote-block contents))
2765 ;;;; Quote Section
2767 (defun org-e-odt-quote-section (quote-section contents info)
2768 "Transcode a QUOTE-SECTION element from Org to ODT.
2769 CONTENTS is nil. INFO is a plist holding contextual information."
2770 (let ((value (org-remove-indentation
2771 (org-element-property :value quote-section))))
2772 (when value (org-e-odt-do-format-code value))))
2775 ;;;; Section
2778 (defun org-e-odt-format-section (text style &optional name)
2779 (let ((default-name (car (org-e-odt-add-automatic-style "Section"))))
2780 (format "\n<text:section text:style-name=\"%s\" %s>\n%s</text:section>"
2781 style
2782 (format "text:name=\"%s\"" (or name default-name))
2783 text)))
2786 (defun org-e-odt-section (section contents info) ; FIXME
2787 "Transcode a SECTION element from Org to ODT.
2788 CONTENTS holds the contents of the section. INFO is a plist
2789 holding contextual information."
2790 contents)
2792 ;;;; Radio Target
2794 (defun org-e-odt-radio-target (radio-target text info)
2795 "Transcode a RADIO-TARGET object from Org to ODT.
2796 TEXT is the text of the target. INFO is a plist holding
2797 contextual information."
2798 (org-e-odt-format-target
2799 text (org-export-solidify-link-text
2800 (org-element-property :value radio-target))))
2803 ;;;; Special Block
2805 (defun org-e-odt-special-block (special-block contents info)
2806 "Transcode a SPECIAL-BLOCK element from Org to ODT.
2807 CONTENTS holds the contents of the block. INFO is a plist
2808 holding contextual information."
2809 (let ((type (downcase (org-element-property :type special-block)))
2810 (attributes (org-export-read-attribute :attr_odt special-block)))
2811 (org-e-odt--wrap-label
2812 special-block
2813 (cond
2814 ;; Annotation.
2815 ((string= type "annotation")
2816 (let ((author (or (plist-get attributes :author)
2817 (let ((author (plist-get info :author)))
2818 (and author (org-export-data author info)))))
2819 (date (or (plist-get attributes :date)
2820 (plist-get info :date))))
2822 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2823 "Text_20_body"
2824 (format "<office:annotation>\n%s\n</office:annotation>"
2825 (concat
2826 (and author
2827 (format "<dc:creator>%s</dc:creator>" author))
2828 (and date
2829 (format "<dc:date>%s</dc:date>"
2830 (org-e-odt--date date)))
2831 contents)))))
2832 ;; Textbox.
2833 ((string= type "textbox")
2834 (let ((width (plist-get attributes :width))
2835 (height (plist-get attributes :height))
2836 (style (plist-get attributes :style))
2837 (extra (plist-get attributes :extra))
2838 (anchor (plist-get attributes :anchor)))
2839 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2840 "Text_20_body" (org-e-odt--textbox contents width height
2841 style extra anchor))))
2842 (t contents)))))
2845 ;;;; Src Block
2848 (defun org-e-odt-hfy-face-to-css (fn)
2849 "Create custom style for face FN.
2850 When FN is the default face, use it's foreground and background
2851 properties to create \"OrgSrcBlock\" paragraph style. Otherwise
2852 use it's color attribute to create a character style whose name
2853 is obtained from FN. Currently all attributes of FN other than
2854 color are ignored.
2856 The style name for a face FN is derived using the following
2857 operations on the face name in that order - de-dash, CamelCase
2858 and prefix with \"OrgSrc\". For example,
2859 `font-lock-function-name-face' is associated with
2860 \"OrgSrcFontLockFunctionNameFace\"."
2861 (let* ((css-list (hfy-face-to-style fn))
2862 (style-name ((lambda (fn)
2863 (concat "OrgSrc"
2864 (mapconcat
2865 'capitalize (split-string
2866 (hfy-face-or-def-to-name fn) "-")
2867 ""))) fn))
2868 (color-val (cdr (assoc "color" css-list)))
2869 (background-color-val (cdr (assoc "background" css-list)))
2870 (style (and org-e-odt-create-custom-styles-for-srcblocks
2871 (cond
2872 ((eq fn 'default)
2873 (format org-e-odt-src-block-paragraph-format
2874 background-color-val color-val))
2876 (format
2878 <style:style style:name=\"%s\" style:family=\"text\">
2879 <style:text-properties fo:color=\"%s\"/>
2880 </style:style>" style-name color-val))))))
2881 (cons style-name style)))
2883 (defun org-e-odt-htmlfontify-string (line)
2884 (let* ((hfy-html-quote-regex "\\([<\"&> ]\\)")
2885 (hfy-html-quote-map '(("\"" "&quot;")
2886 ("<" "&lt;")
2887 ("&" "&amp;")
2888 (">" "&gt;")
2889 (" " "<text:s/>")
2890 (" " "<text:tab/>")))
2891 (hfy-face-to-css 'org-e-odt-hfy-face-to-css)
2892 (hfy-optimisations-1 (copy-seq hfy-optimisations))
2893 (hfy-optimisations (add-to-list 'hfy-optimisations-1
2894 'body-text-only))
2895 (hfy-begin-span-handler
2896 (lambda (style text-block text-id text-begins-block-p)
2897 (insert (format "<text:span text:style-name=\"%s\">" style))))
2898 (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
2899 (with-no-warnings (htmlfontify-string line))))
2901 (defun org-e-odt-do-format-code
2902 (code &optional lang refs retain-labels num-start)
2903 (let* ((lang (or (assoc-default lang org-src-lang-modes) lang))
2904 (lang-mode (and lang (intern (format "%s-mode" lang))))
2905 (code-lines (org-split-string code "\n"))
2906 (code-length (length code-lines))
2907 (use-htmlfontify-p (and (functionp lang-mode)
2908 org-e-odt-fontify-srcblocks
2909 (require 'htmlfontify nil t)
2910 (fboundp 'htmlfontify-string)))
2911 (code (if (not use-htmlfontify-p) code
2912 (with-temp-buffer
2913 (insert code)
2914 (funcall lang-mode)
2915 (font-lock-fontify-buffer)
2916 (buffer-string))))
2917 (fontifier (if use-htmlfontify-p 'org-e-odt-htmlfontify-string
2918 'org-e-odt-encode-plain-text))
2919 (par-style (if use-htmlfontify-p "OrgSrcBlock"
2920 "OrgFixedWidthBlock"))
2921 (i 0))
2922 (assert (= code-length (length (org-split-string code "\n"))))
2923 (setq code
2924 (org-export-format-code
2925 code
2926 (lambda (loc line-num ref)
2927 (setq par-style
2928 (concat par-style (and (= (incf i) code-length) "LastLine")))
2930 (setq loc (concat loc (and ref retain-labels (format " (%s)" ref))))
2931 (setq loc (funcall fontifier loc))
2932 (when ref
2933 (setq loc (org-e-odt-format-target loc (concat "coderef-" ref))))
2934 (assert par-style)
2935 (setq loc (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2936 par-style loc))
2937 (if (not line-num) loc
2938 (format "\n<text:list-item>%s\n</text:list-item>" loc)))
2939 num-start refs))
2940 (cond
2941 ((not num-start) code)
2942 ((= num-start 0)
2943 (format
2944 "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
2945 " text:continue-numbering=\"false\"" code))
2947 (format
2948 "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
2949 " text:continue-numbering=\"true\"" code)))))
2951 (defun org-e-odt-format-code (element info)
2952 (let* ((lang (org-element-property :language element))
2953 ;; Extract code and references.
2954 (code-info (org-export-unravel-code element))
2955 (code (car code-info))
2956 (refs (cdr code-info))
2957 ;; Does the src block contain labels?
2958 (retain-labels (org-element-property :retain-labels element))
2959 ;; Does it have line numbers?
2960 (num-start (case (org-element-property :number-lines element)
2961 (continued (org-export-get-loc element info))
2962 (new 0))))
2963 (org-e-odt-do-format-code code lang refs retain-labels num-start)))
2965 (defun org-e-odt-src-block (src-block contents info)
2966 "Transcode a SRC-BLOCK element from Org to ODT.
2967 CONTENTS holds the contents of the item. INFO is a plist holding
2968 contextual information."
2969 (let* ((lang (org-element-property :language src-block))
2970 (caption (org-element-property :caption src-block))
2971 (short-caption (and (cdr caption)
2972 (org-export-data (cdr caption) info)))
2973 (caption (and (car caption) (org-export-data (car caption) info)))
2974 (label (org-element-property :name src-block))
2975 (attributes (org-export-read-attribute :attr_odt src-block)))
2976 ;; FIXME: Handle caption
2977 ;; caption-str (when caption)
2978 ;; (main (org-export-data (car caption) info))
2979 ;; (secondary (org-export-data (cdr caption) info))
2980 ;; (caption-str (org-e-odt--caption/label-string caption label info))
2981 (let* ((captions (org-e-odt-format-label src-block info 'definition))
2982 (caption (car captions)) (short-caption (cdr captions)))
2983 (concat
2984 (and caption
2985 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2986 "Listing" caption))
2987 (let ((--src-block (org-e-odt-format-code src-block info)))
2988 (if (not (plist-get attributes :textbox)) --src-block
2989 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2990 "Text_20_body"
2991 (org-e-odt--textbox --src-block nil nil nil))))))))
2994 ;;;; Statistics Cookie
2996 (defun org-e-odt-statistics-cookie (statistics-cookie contents info)
2997 "Transcode a STATISTICS-COOKIE object from Org to ODT.
2998 CONTENTS is nil. INFO is a plist holding contextual information."
2999 (let ((cookie-value (org-element-property :value statistics-cookie)))
3000 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3001 "OrgCode" cookie-value)))
3004 ;;;; Strike-Through
3006 (defun org-e-odt-strike-through (strike-through contents info)
3007 "Transcode STRIKE-THROUGH from Org to ODT.
3008 CONTENTS is the text with strike-through markup. INFO is a plist
3009 holding contextual information."
3010 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3011 "Strikethrough" contents))
3014 ;;;; Subscript
3016 (defun org-e-odt-subscript (subscript contents info)
3017 "Transcode a SUBSCRIPT object from Org to ODT.
3018 CONTENTS is the contents of the object. INFO is a plist holding
3019 contextual information."
3020 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3021 "OrgSubscript" contents))
3024 ;;;; Superscript
3026 (defun org-e-odt-superscript (superscript contents info)
3027 "Transcode a SUPERSCRIPT object from Org to ODT.
3028 CONTENTS is the contents of the object. INFO is a plist holding
3029 contextual information."
3030 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3031 "OrgSuperscript" contents))
3034 ;;;; Table Cell
3036 (defun org-e-odt-table-style-spec (element info)
3037 (let* ((table (org-export-get-parent-table element))
3038 (table-attributes (org-export-read-attribute :attr_odt table))
3039 (table-style (plist-get table-attributes :style)))
3040 (assoc table-style org-e-odt-table-styles)))
3042 (defun org-e-odt-get-table-cell-styles (table-cell info)
3043 "Retrieve styles applicable to a table cell.
3044 R and C are (zero-based) row and column numbers of the table
3045 cell. STYLE-SPEC is an entry in `org-e-odt-table-styles'
3046 applicable to the current table. It is `nil' if the table is not
3047 associated with any style attributes.
3049 Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
3051 When STYLE-SPEC is nil, style the table cell the conventional way
3052 - choose cell borders based on row and column groupings and
3053 choose paragraph alignment based on `org-col-cookies' text
3054 property. See also
3055 `org-e-odt-get-paragraph-style-cookie-for-table-cell'.
3057 When STYLE-SPEC is non-nil, ignore the above cookie and return
3058 styles congruent with the ODF-1.2 specification."
3059 (let* ((table-cell-address (org-export-table-cell-address table-cell info))
3060 (r (car table-cell-address)) (c (cdr table-cell-address))
3061 (style-spec (org-e-odt-table-style-spec table-cell info))
3062 (table-dimensions (org-export-table-dimensions
3063 (org-export-get-parent-table table-cell)
3064 info)))
3065 (when style-spec
3066 ;; LibreOffice - particularly the Writer - honors neither table
3067 ;; templates nor custom table-cell styles. Inorder to retain
3068 ;; inter-operability with LibreOffice, only automatic styles are
3069 ;; used for styling of table-cells. The current implementation is
3070 ;; congruent with ODF-1.2 specification and hence is
3071 ;; future-compatible.
3073 ;; Additional Note: LibreOffice's AutoFormat facility for tables -
3074 ;; which recognizes as many as 16 different cell types - is much
3075 ;; richer. Unfortunately it is NOT amenable to easy configuration
3076 ;; by hand.
3077 (let* ((template-name (nth 1 style-spec))
3078 (cell-style-selectors (nth 2 style-spec))
3079 (cell-type
3080 (cond
3081 ((and (cdr (assoc 'use-first-column-styles cell-style-selectors))
3082 (= c 0)) "FirstColumn")
3083 ((and (cdr (assoc 'use-last-column-styles cell-style-selectors))
3084 (= (1+ c) (cdr table-dimensions)))
3085 "LastColumn")
3086 ((and (cdr (assoc 'use-first-row-styles cell-style-selectors))
3087 (= r 0)) "FirstRow")
3088 ((and (cdr (assoc 'use-last-row-styles cell-style-selectors))
3089 (= (1+ r) (car table-dimensions)))
3090 "LastRow")
3091 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
3092 (= (% r 2) 1)) "EvenRow")
3093 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
3094 (= (% r 2) 0)) "OddRow")
3095 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
3096 (= (% c 2) 1)) "EvenColumn")
3097 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
3098 (= (% c 2) 0)) "OddColumn")
3099 (t ""))))
3100 (concat template-name cell-type)))))
3102 (defun org-e-odt-table-cell (table-cell contents info)
3103 "Transcode a TABLE-CELL element from Org to ODT.
3104 CONTENTS is nil. INFO is a plist used as a communication
3105 channel."
3106 (let* ((table-cell-address (org-export-table-cell-address table-cell info))
3107 (r (car table-cell-address))
3108 (c (cdr table-cell-address))
3109 (horiz-span (or (org-export-table-cell-width table-cell info) 0))
3110 (table-row (org-export-get-parent table-cell))
3111 (custom-style-prefix (org-e-odt-get-table-cell-styles
3112 table-cell info))
3113 (paragraph-style
3115 (and custom-style-prefix
3116 (format "%sTableParagraph" custom-style-prefix))
3117 (concat
3118 (cond
3119 ((and (= 1 (org-export-table-row-group table-row info))
3120 (org-export-table-has-header-p
3121 (org-export-get-parent-table table-row) info))
3122 "OrgTableHeading")
3123 ((let* ((table (org-export-get-parent-table table-cell))
3124 (table-attrs (org-export-read-attribute :attr_odt table))
3125 (table-header-columns (plist-get table-attrs
3126 :header-columns)))
3127 (<= c (cond ((wholenump table-header-columns)
3128 (- table-header-columns 1))
3129 (table-header-columns 0)
3130 (t -1))))
3131 "OrgTableHeading")
3132 (t "OrgTableContents"))
3133 (capitalize (symbol-name (org-export-table-cell-alignment
3134 table-cell info))))))
3135 (cell-style-name
3137 (and custom-style-prefix (format "%sTableCell"
3138 custom-style-prefix))
3139 (concat
3140 "OrgTblCell"
3141 (when (or (org-export-table-row-starts-rowgroup-p table-row info)
3142 (zerop r)) "T")
3143 (when (org-export-table-row-ends-rowgroup-p table-row info) "B")
3144 (when (and (org-export-table-cell-starts-colgroup-p table-cell info)
3145 (not (zerop c)) ) "L"))))
3146 (cell-attributes
3147 (concat
3148 (format " table:style-name=\"%s\"" cell-style-name)
3149 (and (> horiz-span 0)
3150 (format " table:number-columns-spanned=\"%d\""
3151 (1+ horiz-span))))))
3152 (unless contents (setq contents ""))
3153 (concat
3154 (assert paragraph-style)
3155 (format "\n<table:table-cell%s>\n%s\n</table:table-cell>"
3156 cell-attributes
3157 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3158 paragraph-style contents))
3159 (let (s)
3160 (dotimes (i horiz-span s)
3161 (setq s (concat s "\n<table:covered-table-cell/>"))))
3162 "\n")))
3165 ;;;; Table Row
3167 (defun org-e-odt-table-row (table-row contents info)
3168 "Transcode a TABLE-ROW element from Org to ODT.
3169 CONTENTS is the contents of the row. INFO is a plist used as a
3170 communication channel."
3171 ;; Rules are ignored since table separators are deduced from
3172 ;; borders of the current row.
3173 (when (eq (org-element-property :type table-row) 'standard)
3174 (let* ((rowgroup-tags
3175 (if (and (= 1 (org-export-table-row-group table-row info))
3176 (org-export-table-has-header-p
3177 (org-export-get-parent-table table-row) info))
3178 ;; If the row belongs to the first rowgroup and the
3179 ;; table has more than one row groups, then this row
3180 ;; belongs to the header row group.
3181 '("\n<table:table-header-rows>" . "\n</table:table-header-rows>")
3182 ;; Otherwise, it belongs to non-header row group.
3183 '("\n<table:table-rows>" . "\n</table:table-rows>"))))
3184 (concat
3185 ;; Does this row begin a rowgroup?
3186 (when (org-export-table-row-starts-rowgroup-p table-row info)
3187 (car rowgroup-tags))
3188 ;; Actual table row
3189 (format "\n<table:table-row>\n%s\n</table:table-row>" contents)
3190 ;; Does this row end a rowgroup?
3191 (when (org-export-table-row-ends-rowgroup-p table-row info)
3192 (cdr rowgroup-tags))))))
3195 ;;;; Table
3197 (defun org-e-odt-table-first-row-data-cells (table info)
3198 (let ((table-row
3199 (org-element-map
3200 table 'table-row
3201 (lambda (row)
3202 (unless (eq (org-element-property :type row) 'rule) row))
3203 info 'first-match))
3204 (special-column-p (org-export-table-has-special-column-p table)))
3205 (if (not special-column-p) (org-element-contents table-row)
3206 (cdr (org-element-contents table-row)))))
3208 (defun org-e-odt--table (table contents info)
3209 "Transcode a TABLE element from Org to ODT.
3210 CONTENTS is the contents of the table. INFO is a plist holding
3211 contextual information."
3212 (case (org-element-property :type table)
3213 ;; Case 1: table.el doesn't support export to OD format. Strip
3214 ;; such tables from export.
3215 (table.el
3216 (prog1 nil
3217 (message
3218 (concat
3219 "(org-e-odt): Found table.el-type table in the source Org file."
3220 " table.el doesn't support export to ODT format."
3221 " Stripping the table from export."))))
3222 ;; Case 2: Native Org tables.
3223 (otherwise
3224 (let* ((captions (org-e-odt-format-label table info 'definition))
3225 (caption (car captions)) (short-caption (cdr captions))
3226 (attributes (org-export-read-attribute :attr_odt table))
3227 (custom-table-style (nth 1 (org-e-odt-table-style-spec table info)))
3228 (table-column-specs
3229 (function
3230 (lambda (table info)
3231 (let* ((table-style (or custom-table-style "OrgTable"))
3232 (column-style (format "%sColumn" table-style)))
3233 (mapconcat
3234 (lambda (table-cell)
3235 (let ((width (1+ (or (org-export-table-cell-width
3236 table-cell info) 0)))
3237 (s (format
3238 "\n<table:table-column table:style-name=\"%s\"/>"
3239 column-style))
3240 out)
3241 (dotimes (i width out) (setq out (concat s out)))))
3242 (org-e-odt-table-first-row-data-cells table info) "\n"))))))
3243 (concat
3244 ;; caption.
3245 (when caption
3246 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3247 "Table" caption))
3248 ;; begin table.
3249 (let* ((automatic-name
3250 (org-e-odt-add-automatic-style "Table" attributes)))
3251 (format
3252 "\n<table:table table:name=\"%s\" table:style-name=\"%s\">"
3253 (or short-caption (car automatic-name))
3254 (or custom-table-style (cdr automatic-name) "OrgTable")))
3255 ;; column specification.
3256 (funcall table-column-specs table info)
3257 ;; actual contents.
3258 "\n" contents
3259 ;; end table.
3260 "</table:table>")))))
3262 (defun org-e-odt-table (table contents info)
3263 "Transcode a TABLE element from Org to ODT.
3264 CONTENTS is the contents of the table. INFO is a plist holding
3265 contextual information."
3266 (let* ((--get-previous-elements
3267 (function
3268 (lambda (blob info)
3269 (let ((parent (org-export-get-parent blob)))
3270 (cdr (member blob (reverse (org-element-contents parent))))))))
3271 (--element-preceded-by-table-p
3272 (function
3273 (lambda (element info)
3274 (loop for el in (funcall --get-previous-elements element info)
3275 thereis (eq (org-element-type el) 'table)))))
3276 (--walk-list-genealogy-and-collect-tags
3277 (function
3278 (lambda (table info)
3279 (let* ((genealogy (org-export-get-genealogy table))
3280 (list-genealogy
3281 (when (eq (org-element-type (car genealogy)) 'item)
3282 (loop for el in genealogy
3283 when (member (org-element-type el)
3284 '(item plain-list))
3285 collect el))))
3286 (loop for el in list-genealogy
3287 with parent-list collect
3288 (case (org-element-type el)
3289 (plain-list
3290 (setq parent-list el)
3291 `("</text:list>"
3292 . ,(let ((type (org-element-property :type el)))
3293 (format
3294 "<text:list text:style-name=\"%s\" %s>"
3295 (assoc-default
3296 type '((ordered . "OrgNumberedList")
3297 (unordered . "OrgBulletedList")
3298 (descriptive . "OrgDescriptionList")))
3299 "text:continue-numbering=\"true\""))))
3300 (item
3301 (cond
3302 ((not parent-list)
3303 (if (funcall --element-preceded-by-table-p table info)
3304 '("</text:list-header>" . "<text:list-header>")
3305 '("</text:list-item>" . "<text:list-header>")))
3306 ((funcall --element-preceded-by-table-p
3307 parent-list info)
3308 '("</text:list-header>" . "<text:list-header>"))
3309 (t '("</text:list-item>" . "<text:list-item>"))))))))))
3310 (close-open-tags (funcall --walk-list-genealogy-and-collect-tags
3311 table info)))
3312 ;; OpenDocument schema does not permit table to occur within a
3313 ;; list item. So, to typeset an indented table, we make use of
3314 ;; list continuations.
3315 (concat "\n"
3316 ;; Discontinue the list.
3317 (mapconcat 'car close-open-tags "\n")
3318 ;; Put the table in an indented section.
3319 (let* ((table (org-e-odt--table table contents info))
3320 (level (/ (length (mapcar 'car close-open-tags)) 2))
3321 (style (format "OrgIndentedSection-Level-%d" level)))
3322 (when table (org-e-odt-format-section table style)))
3323 ;; Continue the list.
3324 (mapconcat 'cdr (nreverse close-open-tags) "\n"))))
3327 ;;;; Target
3329 (defun org-e-odt-target (target contents info)
3330 "Transcode a TARGET object from Org to ODT.
3331 CONTENTS is nil. INFO is a plist holding contextual
3332 information."
3333 (org-e-odt-format-target
3334 "" (org-export-solidify-link-text (org-element-property :value target))))
3337 ;;;; Timestamp
3339 (defun org-e-odt-timestamp (timestamp contents info)
3340 "Transcode a TIMESTAMP object from Org to ODT.
3341 CONTENTS is nil. INFO is a plist used as a communication
3342 channel."
3343 (let ((timestamp-1 (org-element-property :value timestamp))
3344 (timestamp-2 (org-element-property :range-end timestamp)))
3345 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3346 "OrgTimestampWrapper"
3347 (concat
3348 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3349 "OrgTimestamp" (org-translate-time timestamp-1))
3350 (and timestamp-2
3351 "&#x2013;"
3352 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3353 "OrgTimestamp" (org-translate-time timestamp-2)))))))
3356 ;;;; Underline
3358 (defun org-e-odt-underline (underline contents info)
3359 "Transcode UNDERLINE from Org to ODT.
3360 CONTENTS is the text with underline markup. INFO is a plist
3361 holding contextual information."
3362 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3363 "Underline" contents))
3366 ;;;; Verbatim
3368 (defun org-e-odt-verbatim (verbatim contents info)
3369 "Transcode a VERBATIM object from Org to ODT.
3370 CONTENTS is nil. INFO is a plist used as a communication
3371 channel."
3372 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3373 "OrgCode" (org-element-property :value verbatim)))
3376 ;;;; Verse Block
3378 (defun org-e-odt-verse-block (verse-block contents info)
3379 "Transcode a VERSE-BLOCK element from Org to ODT.
3380 CONTENTS is verse block contents. INFO is a plist holding
3381 contextual information."
3382 ;; Add line breaks to each line of verse.
3383 (setq contents (replace-regexp-in-string
3384 "\\(<text:line-break/>\\)?[ \t]*\n"
3385 "<text:line-break/>" contents))
3386 ;; Replace tabs and spaces.
3387 (setq contents (org-e-odt-fill-tabs-and-spaces contents))
3388 ;; Surround it in a verse environment.
3389 (org-e-odt--wrap-label
3390 verse-block
3391 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3392 "OrgVerse" contents)))
3398 ;;; Interactive functions
3400 (defun org-e-odt-create-manifest-file-entry (&rest args)
3401 (push args org-e-odt-manifest-file-entries))
3403 (defun org-e-odt-write-manifest-file ()
3404 (make-directory (concat org-e-odt-zip-dir "META-INF"))
3405 (let ((manifest-file (concat org-e-odt-zip-dir "META-INF/manifest.xml")))
3406 (with-current-buffer
3407 (let ((nxml-auto-insert-xml-declaration-flag nil))
3408 (find-file-noselect manifest-file t))
3409 (insert
3410 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
3411 <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
3412 (mapc
3413 (lambda (file-entry)
3414 (let* ((version (nth 2 file-entry))
3415 (extra (if (not version) ""
3416 (format " manifest:version=\"%s\"" version))))
3417 (insert
3418 (format org-e-odt-manifest-file-entry-tag
3419 (nth 0 file-entry) (nth 1 file-entry) extra))))
3420 org-e-odt-manifest-file-entries)
3421 (insert "\n</manifest:manifest>"))))
3423 (defmacro org-e-odt--export-wrap (out-file &rest body)
3424 `(let* ((--out-file ,out-file)
3425 (out-file-type (file-name-extension --out-file))
3426 (org-e-odt-xml-files '("META-INF/manifest.xml" "content.xml"
3427 "meta.xml" "styles.xml"))
3428 ;; Initialize workarea. All files that end up in the
3429 ;; exported get created here.
3430 (org-e-odt-zip-dir (file-name-as-directory
3431 (make-temp-file (format "%s-" out-file-type) t)))
3432 (org-e-odt-manifest-file-entries nil)
3433 (--cleanup-xml-buffers
3434 (function
3435 (lambda nil
3436 ;; Kill all XML buffers.
3437 (mapc (lambda (file)
3438 (let ((buf (get-file-buffer
3439 (concat org-e-odt-zip-dir file))))
3440 (when buf
3441 (set-buffer-modified-p nil)
3442 (kill-buffer buf))))
3443 org-e-odt-xml-files)
3444 ;; Delete temporary directory and also other embedded
3445 ;; files that get copied there.
3446 (delete-directory org-e-odt-zip-dir t)))))
3447 (org-condition-case-unless-debug
3449 (progn
3450 (unless (executable-find "zip")
3451 ;; Not at all OSes ship with zip by default
3452 (error "Executable \"zip\" needed for creating OpenDocument files"))
3453 ;; Do export. This creates a bunch of xml files ready to be
3454 ;; saved and zipped.
3455 (progn ,@body)
3456 ;; Create a manifest entry for content.xml.
3457 (org-e-odt-create-manifest-file-entry "text/xml" "content.xml")
3459 ;; Write mimetype file
3460 (let* ((mimetypes
3461 '(("odt" . "application/vnd.oasis.opendocument.text")
3462 ("odf" . "application/vnd.oasis.opendocument.formula")))
3463 (mimetype (cdr (assoc-string out-file-type mimetypes t))))
3464 (unless mimetype
3465 (error "Unknown OpenDocument backend %S" out-file-type))
3466 (write-region mimetype nil (concat org-e-odt-zip-dir "mimetype"))
3467 (org-e-odt-create-manifest-file-entry mimetype "/" "1.2"))
3468 ;; Write out the manifest entries before zipping
3469 (org-e-odt-write-manifest-file)
3470 ;; Save all XML files.
3471 (mapc (lambda (file)
3472 (let ((buf (get-file-buffer (concat org-e-odt-zip-dir file))))
3473 (when buf
3474 (with-current-buffer buf
3475 ;; Prettify output if needed.
3476 (when org-e-odt-prettify-xml
3477 (indent-region (point-min) (point-max)))
3478 (save-buffer 0)))))
3479 org-e-odt-xml-files)
3480 ;; Run zip.
3481 (let* ((target --out-file)
3482 (target-name (file-name-nondirectory target))
3483 (target-dir (file-name-directory target))
3484 (cmds `(("zip" "-mX0" ,target-name "mimetype")
3485 ("zip" "-rmTq" ,target-name "."))))
3486 ;; If a file with same name as the desired output file
3487 ;; exists, remove it.
3488 (when (file-exists-p target)
3489 (delete-file target))
3490 ;; Zip up the xml files.
3491 (let ((coding-system-for-write 'no-conversion) exitcode err-string)
3492 (message "Creating ODT file...")
3493 ;; Switch temporarily to content.xml. This way Zip
3494 ;; process will inherit `org-e-odt-zip-dir' as the current
3495 ;; directory.
3496 (with-current-buffer
3497 (find-file-noselect (concat org-e-odt-zip-dir "content.xml") t)
3498 (mapc
3499 (lambda (cmd)
3500 (message "Running %s" (mapconcat 'identity cmd " "))
3501 (setq err-string
3502 (with-output-to-string
3503 (setq exitcode
3504 (apply 'call-process (car cmd)
3505 nil standard-output nil (cdr cmd)))))
3506 (or (zerop exitcode)
3507 (error (concat "Unable to create OpenDocument file."
3508 (format " Zip failed with error (%s)"
3509 err-string)))))
3510 cmds)
3511 ;; Zip file is now in the rightful place.
3512 (rename-file target-name target)))
3513 (message "Created %s" target)
3514 ;; Cleanup work directory and work files.
3515 (funcall --cleanup-xml-buffers)
3516 ;; Open the OpenDocument file in archive-mode for
3517 ;; examination.
3518 (find-file-noselect target t)
3519 ;; Return exported file.
3520 (cond
3521 ;; Case 1: Conversion desired on exported file. Run the
3522 ;; converter on the OpenDocument file. Return the
3523 ;; converted file.
3524 (org-e-odt-preferred-output-format
3525 (or (org-e-odt-convert target org-e-odt-preferred-output-format)
3526 target))
3527 ;; Case 2: No further conversion. Return exported
3528 ;; OpenDocument file.
3529 (t target))))
3530 ((quit error)
3531 ;; Cleanup work directory and work files.
3532 (funcall --cleanup-xml-buffers)
3533 (message "OpenDocument export failed: %s"
3534 (error-message-string err))))))
3538 ;;;###autoload
3539 (defun org-e-odt-export-as-odf (latex-frag &optional odf-file)
3540 "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
3541 Use `org-create-math-formula' to convert LATEX-FRAG first to
3542 MathML. When invoked as an interactive command, use
3543 `org-latex-regexps' to infer LATEX-FRAG from currently active
3544 region. If no LaTeX fragments are found, prompt for it. Push
3545 MathML source to kill ring, if `org-export-copy-to-kill-ring' is
3546 non-nil."
3547 (interactive
3548 `(,(let (frag)
3549 (setq frag (and (setq frag (and (region-active-p)
3550 (buffer-substring (region-beginning)
3551 (region-end))))
3552 (loop for e in org-latex-regexps
3553 thereis (when (string-match (nth 1 e) frag)
3554 (match-string (nth 2 e) frag)))))
3555 (read-string "LaTeX Fragment: " frag nil frag))
3556 ,(let ((odf-filename (expand-file-name
3557 (concat
3558 (file-name-sans-extension
3559 (or (file-name-nondirectory buffer-file-name)))
3560 "." "odf")
3561 (file-name-directory buffer-file-name))))
3562 (read-file-name "ODF filename: " nil odf-filename nil
3563 (file-name-nondirectory odf-filename)))))
3564 (let ((filename (or odf-file
3565 (expand-file-name
3566 (concat
3567 (file-name-sans-extension
3568 (or (file-name-nondirectory buffer-file-name)))
3569 "." "odf")
3570 (file-name-directory buffer-file-name)))))
3571 (org-e-odt--export-wrap
3572 filename
3573 (let* ((buffer (progn
3574 (require 'nxml-mode)
3575 (let ((nxml-auto-insert-xml-declaration-flag nil))
3576 (find-file-noselect (concat org-e-odt-zip-dir
3577 "content.xml") t))))
3578 (coding-system-for-write 'utf-8)
3579 (save-buffer-coding-system 'utf-8))
3580 (set-buffer buffer)
3581 (set-buffer-file-coding-system coding-system-for-write)
3582 (let ((mathml (org-create-math-formula latex-frag)))
3583 (unless mathml (error "No Math formula created"))
3584 (insert mathml)
3585 ;; Add MathML to kill ring, if needed.
3586 (when org-export-copy-to-kill-ring
3587 (org-kill-new (buffer-string))))))))
3589 ;;;###autoload
3590 (defun org-e-odt-export-as-odf-and-open ()
3591 "Export LaTeX fragment as OpenDocument formula and immediately open it.
3592 Use `org-e-odt-export-as-odf' to read LaTeX fragment and OpenDocument
3593 formula file."
3594 (interactive)
3595 (org-open-file (call-interactively 'org-e-odt-export-as-odf)))
3597 ;;;###autoload
3598 (defun org-e-odt-export-to-odt
3599 (&optional subtreep visible-only body-only ext-plist pub-dir)
3600 "Export current buffer to a HTML file.
3602 If narrowing is active in the current buffer, only export its
3603 narrowed part.
3605 If a region is active, export that region.
3607 When optional argument SUBTREEP is non-nil, export the sub-tree
3608 at point, extracting information from the headline properties
3609 first.
3611 When optional argument VISIBLE-ONLY is non-nil, don't export
3612 contents of hidden elements.
3614 When optional argument BODY-ONLY is non-nil, only write code
3615 between \"\\begin{document}\" and \"\\end{document}\".
3617 EXT-PLIST, when provided, is a property list with external
3618 parameters overriding Org default settings, but still inferior to
3619 file-local settings.
3621 When optional argument PUB-DIR is set, use it as the publishing
3622 directory.
3624 Return output file's name."
3625 (interactive)
3626 (org-e-odt--export-wrap
3627 (org-export-output-file-name ".odt" subtreep pub-dir)
3628 (let* ((org-e-odt-embedded-images-count 0)
3629 (org-e-odt-embedded-formulas-count 0)
3630 (org-e-odt-automatic-styles nil)
3631 (org-e-odt-object-counters nil)
3632 ;; Let `htmlfontify' know that we are interested in collecting
3633 ;; styles.
3634 (hfy-user-sheet-assoc nil))
3635 ;; Initialize content.xml and kick-off the export process.
3636 (let ((out-buf (progn
3637 (require 'nxml-mode)
3638 (let ((nxml-auto-insert-xml-declaration-flag nil))
3639 (find-file-noselect
3640 (concat org-e-odt-zip-dir "content.xml") t)))))
3641 (org-export-to-buffer 'e-odt out-buf subtreep visible-only body-only)))))
3646 (defun org-e-odt-reachable-p (in-fmt out-fmt)
3647 "Return non-nil if IN-FMT can be converted to OUT-FMT."
3648 (catch 'done
3649 (let ((reachable-formats (org-e-odt-do-reachable-formats in-fmt)))
3650 (dolist (e reachable-formats)
3651 (let ((out-fmt-spec (assoc out-fmt (cdr e))))
3652 (when out-fmt-spec
3653 (throw 'done (cons (car e) out-fmt-spec))))))))
3655 (defun org-e-odt-do-convert (in-file out-fmt &optional prefix-arg)
3656 "Workhorse routine for `org-e-odt-convert'."
3657 (require 'browse-url)
3658 (let* ((in-file (expand-file-name (or in-file buffer-file-name)))
3659 (dummy (or (file-readable-p in-file)
3660 (error "Cannot read %s" in-file)))
3661 (in-fmt (file-name-extension in-file))
3662 (out-fmt (or out-fmt (error "Output format unspecified")))
3663 (how (or (org-e-odt-reachable-p in-fmt out-fmt)
3664 (error "Cannot convert from %s format to %s format?"
3665 in-fmt out-fmt)))
3666 (convert-process (car how))
3667 (out-file (concat (file-name-sans-extension in-file) "."
3668 (nth 1 (or (cdr how) out-fmt))))
3669 (extra-options (or (nth 2 (cdr how)) ""))
3670 (out-dir (file-name-directory in-file))
3671 (cmd (format-spec convert-process
3672 `((?i . ,(shell-quote-argument in-file))
3673 (?I . ,(browse-url-file-url in-file))
3674 (?f . ,out-fmt)
3675 (?o . ,out-file)
3676 (?O . ,(browse-url-file-url out-file))
3677 (?d . , (shell-quote-argument out-dir))
3678 (?D . ,(browse-url-file-url out-dir))
3679 (?x . ,extra-options)))))
3680 (when (file-exists-p out-file)
3681 (delete-file out-file))
3683 (message "Executing %s" cmd)
3684 (let ((cmd-output (shell-command-to-string cmd)))
3685 (message "%s" cmd-output))
3687 (cond
3688 ((file-exists-p out-file)
3689 (message "Exported to %s" out-file)
3690 (when prefix-arg
3691 (message "Opening %s..." out-file)
3692 (org-open-file out-file))
3693 out-file)
3695 (message "Export to %s failed" out-file)
3696 nil))))
3698 (defun org-e-odt-do-reachable-formats (in-fmt)
3699 "Return verbose info about formats to which IN-FMT can be converted.
3700 Return a list where each element is of the
3701 form (CONVERTER-PROCESS . OUTPUT-FMT-ALIST). See
3702 `org-e-odt-convert-processes' for CONVERTER-PROCESS and see
3703 `org-e-odt-convert-capabilities' for OUTPUT-FMT-ALIST."
3704 (let* ((converter
3705 (and org-e-odt-convert-process
3706 (cadr (assoc-string org-e-odt-convert-process
3707 org-e-odt-convert-processes t))))
3708 (capabilities
3709 (and org-e-odt-convert-process
3710 (cadr (assoc-string org-e-odt-convert-process
3711 org-e-odt-convert-processes t))
3712 org-e-odt-convert-capabilities))
3713 reachable-formats)
3714 (when converter
3715 (dolist (c capabilities)
3716 (when (member in-fmt (nth 1 c))
3717 (push (cons converter (nth 2 c)) reachable-formats))))
3718 reachable-formats))
3720 (defun org-e-odt-reachable-formats (in-fmt)
3721 "Return list of formats to which IN-FMT can be converted.
3722 The list of the form (OUTPUT-FMT-1 OUTPUT-FMT-2 ...)."
3723 (let (l)
3724 (mapc (lambda (e) (add-to-list 'l e))
3725 (apply 'append (mapcar
3726 (lambda (e) (mapcar 'car (cdr e)))
3727 (org-e-odt-do-reachable-formats in-fmt))))
3730 (defun org-e-odt-convert-read-params ()
3731 "Return IN-FILE and OUT-FMT params for `org-e-odt-do-convert'.
3732 This is a helper routine for interactive use."
3733 (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
3734 (in-file (read-file-name "File to be converted: "
3735 nil buffer-file-name t))
3736 (in-fmt (file-name-extension in-file))
3737 (out-fmt-choices (org-e-odt-reachable-formats in-fmt))
3738 (out-fmt
3739 (or (and out-fmt-choices
3740 (funcall input "Output format: "
3741 out-fmt-choices nil nil nil))
3742 (error
3743 "No known converter or no known output formats for %s files"
3744 in-fmt))))
3745 (list in-file out-fmt)))
3747 ;;;###autoload
3748 (defun org-e-odt-convert (&optional in-file out-fmt prefix-arg)
3749 "Convert IN-FILE to format OUT-FMT using a command line converter.
3750 IN-FILE is the file to be converted. If unspecified, it defaults
3751 to variable `buffer-file-name'. OUT-FMT is the desired output
3752 format. Use `org-e-odt-convert-process' as the converter.
3753 If PREFIX-ARG is non-nil then the newly converted file is opened
3754 using `org-open-file'."
3755 (interactive
3756 (append (org-e-odt-convert-read-params) current-prefix-arg))
3757 (org-e-odt-do-convert in-file out-fmt prefix-arg))
3759 ;;; Library Initializations
3761 (mapc
3762 (lambda (desc)
3763 ;; Let Org open all OpenDocument files using system-registered app
3764 (add-to-list 'org-file-apps
3765 (cons (concat "\\." (car desc) "\\'") 'system))
3766 ;; Let Emacs open all OpenDocument files in archive mode
3767 (add-to-list 'auto-mode-alist
3768 (cons (concat "\\." (car desc) "\\'") 'archive-mode)))
3769 org-e-odt-file-extensions)
3771 (provide 'org-e-odt)
3773 ;;; org-e-odt.el ends here