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