7ba2d9d871ad326baddb87c4c6495da55b528168
[org-mode.git] / contrib / lisp / org-e-odt.el
blob7ba2d9d871ad326baddb87c4c6495da55b528168
1 ;;; org-e-odt.el --- OpenDocument Text exporter for Org-mode
3 ;; Copyright (C) 2010-2013 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 not 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)
33 (require 'org-compat)
35 ;;; Define Back-End
37 (org-export-define-backend e-odt
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 (drawer . org-e-odt-drawer)
43 (dynamic-block . org-e-odt-dynamic-block)
44 (entity . org-e-odt-entity)
45 (example-block . org-e-odt-example-block)
46 (export-block . org-e-odt-export-block)
47 (export-snippet . org-e-odt-export-snippet)
48 (fixed-width . org-e-odt-fixed-width)
49 (footnote-definition . org-e-odt-footnote-definition)
50 (footnote-reference . org-e-odt-footnote-reference)
51 (headline . org-e-odt-headline)
52 (horizontal-rule . org-e-odt-horizontal-rule)
53 (inline-src-block . org-e-odt-inline-src-block)
54 (inlinetask . org-e-odt-inlinetask)
55 (italic . org-e-odt-italic)
56 (item . org-e-odt-item)
57 (keyword . org-e-odt-keyword)
58 (latex-environment . org-e-odt-latex-environment)
59 (latex-fragment . org-e-odt-latex-fragment)
60 (line-break . org-e-odt-line-break)
61 (link . org-e-odt-link)
62 (paragraph . org-e-odt-paragraph)
63 (plain-list . org-e-odt-plain-list)
64 (plain-text . org-e-odt-plain-text)
65 (planning . org-e-odt-planning)
66 (property-drawer . org-e-odt-property-drawer)
67 (quote-block . org-e-odt-quote-block)
68 (quote-section . org-e-odt-quote-section)
69 (radio-target . org-e-odt-radio-target)
70 (section . org-e-odt-section)
71 (special-block . org-e-odt-special-block)
72 (src-block . org-e-odt-src-block)
73 (statistics-cookie . org-e-odt-statistics-cookie)
74 (strike-through . org-e-odt-strike-through)
75 (subscript . org-e-odt-subscript)
76 (superscript . org-e-odt-superscript)
77 (table . org-e-odt-table)
78 (table-cell . org-e-odt-table-cell)
79 (table-row . org-e-odt-table-row)
80 (target . org-e-odt-target)
81 (template . org-e-odt-template)
82 (timestamp . org-e-odt-timestamp)
83 (underline . org-e-odt-underline)
84 (verbatim . org-e-odt-verbatim)
85 (verse-block . org-e-odt-verse-block))
86 :export-block "ODT"
87 :filters-alist ((:filter-parse-tree
88 . (org-e-odt--translate-latex-fragments
89 org-e-odt--translate-description-lists
90 org-e-odt--translate-list-tables)))
91 :menu-entry
92 (?o "Export to ODT"
93 ((?o "As ODT file" org-e-odt-export-to-odt)
94 (?O "As ODT file and open"
95 (lambda (a s v b)
96 (if a (org-e-odt-export-to-odt t s v)
97 (org-open-file (org-e-odt-export-to-odt nil s v) 'system))))))
98 :options-alist
99 ((:odt-styles-file "ODT_STYLES_FILE" nil nil t)
100 (:LaTeX-fragments nil "LaTeX" org-export-with-LaTeX-fragments)))
103 ;;; Dependencies
105 ;;; Hooks
107 ;;; Function Declarations
109 (declare-function org-id-find-id-file "org-id" (id))
110 (declare-function hfy-face-to-style "htmlfontify" (fn))
111 (declare-function hfy-face-or-def-to-name "htmlfontify" (fn))
112 (declare-function archive-zip-extract "arc-mode" (archive name))
113 (declare-function org-create-math-formula "org" (latex-frag &optional mathml-file))
114 (declare-function browse-url-file-url "browse-url" (file))
118 ;;; Internal Variables
120 (defconst org-e-odt-lib-dir
121 (file-name-directory load-file-name)
122 "Location of ODT exporter.
123 Use this to infer values of `org-e-odt-styles-dir' and
124 `org-e-odt-schema-dir'.")
126 (defvar org-e-odt-data-dir
127 (expand-file-name "../../etc/" org-e-odt-lib-dir)
128 "Data directory for ODT exporter.
129 Use this to infer values of `org-e-odt-styles-dir' and
130 `org-e-odt-schema-dir'.")
132 (defconst org-e-odt-special-string-regexps
133 '(("\\\\-" . "&#x00ad;\\1") ; shy
134 ("---\\([^-]\\)" . "&#x2014;\\1") ; mdash
135 ("--\\([^-]\\)" . "&#x2013;\\1") ; ndash
136 ("\\.\\.\\." . "&#x2026;")) ; hellip
137 "Regular expressions for special string conversion.")
139 (defconst org-e-odt-schema-dir-list
140 (list
141 (and org-e-odt-data-dir
142 (expand-file-name "./schema/" org-e-odt-data-dir)) ; bail out
143 (eval-when-compile
144 (and (boundp 'org-e-odt-data-dir) org-e-odt-data-dir ; see make install
145 (expand-file-name "./schema/" org-e-odt-data-dir))))
146 "List of directories to search for OpenDocument schema files.
147 Use this list to set the default value of
148 `org-e-odt-schema-dir'. The entries in this list are
149 populated heuristically based on the values of `org-e-odt-lib-dir'
150 and `org-e-odt-data-dir'.")
152 (defconst org-e-odt-styles-dir-list
153 (list
154 (and org-e-odt-data-dir
155 (expand-file-name "./styles/" org-e-odt-data-dir)) ; bail out
156 (eval-when-compile
157 (and (boundp 'org-e-odt-data-dir) org-e-odt-data-dir ; see make install
158 (expand-file-name "./styles/" org-e-odt-data-dir)))
159 (expand-file-name "../../etc/styles/" org-e-odt-lib-dir) ; git
160 (expand-file-name "./etc/styles/" org-e-odt-lib-dir) ; elpa
161 (expand-file-name "./org/" data-directory) ; system
163 "List of directories to search for OpenDocument styles files.
164 See `org-e-odt-styles-dir'. The entries in this list are populated
165 heuristically based on the values of `org-e-odt-lib-dir' and
166 `org-e-odt-data-dir'.")
168 (defconst org-e-odt-styles-dir
169 (let* ((styles-dir
170 (catch 'styles-dir
171 (message "Debug (org-e-odt): Searching for OpenDocument styles files...")
172 (mapc (lambda (styles-dir)
173 (when styles-dir
174 (message "Debug (org-e-odt): Trying %s..." styles-dir)
175 (when (and (file-readable-p
176 (expand-file-name
177 "OrgOdtContentTemplate.xml" styles-dir))
178 (file-readable-p
179 (expand-file-name
180 "OrgOdtStyles.xml" styles-dir)))
181 (message "Debug (org-e-odt): Using styles under %s"
182 styles-dir)
183 (throw 'styles-dir styles-dir))))
184 org-e-odt-styles-dir-list)
185 nil)))
186 (unless styles-dir
187 (error "Error (org-e-odt): Cannot find factory styles files, aborting"))
188 styles-dir)
189 "Directory that holds auxiliary XML files used by the ODT exporter.
191 This directory contains the following XML files -
192 \"OrgOdtStyles.xml\" and \"OrgOdtContentTemplate.xml\". These
193 XML files are used as the default values of
194 `org-e-odt-styles-file' and
195 `org-e-odt-content-template-file'.
197 The default value of this variable varies depending on the
198 version of org in use and is initialized from
199 `org-e-odt-styles-dir-list'. Note that the user could be using org
200 from one of: org's own private git repository, GNU ELPA tar or
201 standard Emacs.")
203 (defconst org-e-odt-bookmark-prefix "OrgXref.")
205 (defconst org-e-odt-manifest-file-entry-tag
206 "\n<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
208 (defconst org-e-odt-file-extensions
209 '(("odt" . "OpenDocument Text")
210 ("ott" . "OpenDocument Text Template")
211 ("odm" . "OpenDocument Master Document")
212 ("ods" . "OpenDocument Spreadsheet")
213 ("ots" . "OpenDocument Spreadsheet Template")
214 ("odg" . "OpenDocument Drawing (Graphics)")
215 ("otg" . "OpenDocument Drawing Template")
216 ("odp" . "OpenDocument Presentation")
217 ("otp" . "OpenDocument Presentation Template")
218 ("odi" . "OpenDocument Image")
219 ("odf" . "OpenDocument Formula")
220 ("odc" . "OpenDocument Chart")))
222 (defconst org-e-odt-table-style-format
224 <style:style style:name=\"%s\" style:family=\"table\">
225 <style:table-properties style:rel-width=\"%d%%\" fo:margin-top=\"0cm\" fo:margin-bottom=\"0.20cm\" table:align=\"center\"/>
226 </style:style>
228 "Template for auto-generated Table styles.")
230 (defvar org-e-odt-automatic-styles '()
231 "Registry of automatic styles for various OBJECT-TYPEs.
232 The variable has the following form:
233 \(\(OBJECT-TYPE-A
234 \(\(OBJECT-NAME-A.1 OBJECT-PROPS-A.1\)
235 \(OBJECT-NAME-A.2 OBJECT-PROPS-A.2\) ...\)\)
236 \(OBJECT-TYPE-B
237 \(\(OBJECT-NAME-B.1 OBJECT-PROPS-B.1\)
238 \(OBJECT-NAME-B.2 OBJECT-PROPS-B.2\) ...\)\)
239 ...\).
241 OBJECT-TYPEs could be \"Section\", \"Table\", \"Figure\" etc.
242 OBJECT-PROPS is (typically) a plist created by passing
243 \"#+ATTR_ODT: \" option to `org-e-odt-parse-block-attributes'.
245 Use `org-e-odt-add-automatic-style' to add update this variable.'")
247 (defvar org-e-odt-object-counters nil
248 "Running counters for various OBJECT-TYPEs.
249 Use this to generate automatic names and style-names. See
250 `org-e-odt-add-automatic-style'.")
252 (defvar org-e-odt-src-block-paragraph-format
253 "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
254 <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
255 <style:background-image/>
256 </style:paragraph-properties>
257 <style:text-properties fo:color=\"%s\"/>
258 </style:style>"
259 "Custom paragraph style for colorized source and example blocks.
260 This style is much the same as that of \"OrgFixedWidthBlock\"
261 except that the foreground and background colors are set
262 according to the default face identified by the `htmlfontify'.")
264 (defvar hfy-optimisations)
265 (defvar org-e-odt-embedded-formulas-count 0)
266 (defvar org-e-odt-embedded-images-count 0)
267 (defvar org-e-odt-image-size-probe-method
268 (append (and (executable-find "identify") '(imagemagick)) ; See Bug#10675
269 '(emacs fixed))
270 "Ordered list of methods for determining image sizes.")
272 (defvar org-e-odt-default-image-sizes-alist
273 '(("as-char" . (5 . 0.4))
274 ("paragraph" . (5 . 5)))
275 "Hardcoded image dimensions one for each of the anchor
276 methods.")
278 ;; A4 page size is 21.0 by 29.7 cms
279 ;; The default page settings has 2cm margin on each of the sides. So
280 ;; the effective text area is 17.0 by 25.7 cm
281 (defvar org-e-odt-max-image-size '(17.0 . 20.0)
282 "Limiting dimensions for an embedded image.")
284 (defconst org-e-odt-label-styles
285 '(("math-formula" "%c" "text" "(%n)")
286 ("math-label" "(%n)" "text" "(%n)")
287 ("category-and-value" "%e %n: %c" "category-and-value" "%e %n")
288 ("value" "%e %n: %c" "value" "%n"))
289 "Specify how labels are applied and referenced.
290 This is an alist where each element is of the
291 form (LABEL-STYLE-NAME LABEL-ATTACH-FMT LABEL-REF-MODE
292 LABEL-REF-FMT).
294 LABEL-ATTACH-FMT controls how labels and captions are attached to
295 an entity. It may contain following specifiers - %e, %n and %c.
296 %e is replaced with the CATEGORY-NAME. %n is replaced with
297 \"<text:sequence ...> SEQNO </text:sequence>\". %c is replaced
298 with CAPTION. See `org-e-odt-format-label-definition'.
300 LABEL-REF-MODE and LABEL-REF-FMT controls how label references
301 are generated. The following XML is generated for a label
302 reference - \"<text:sequence-ref
303 text:reference-format=\"LABEL-REF-MODE\" ...> LABEL-REF-FMT
304 </text:sequence-ref>\". LABEL-REF-FMT may contain following
305 specifiers - %e and %n. %e is replaced with the CATEGORY-NAME.
306 %n is replaced with SEQNO. See
307 `org-e-odt-format-label-reference'.")
309 (defvar org-e-odt-category-map-alist
310 '(("__Table__" "Table" "value" "Table" org-e-odt--enumerable-p)
311 ("__Figure__" "Illustration" "value" "Figure" org-e-odt--enumerable-image-p)
312 ("__MathFormula__" "Text" "math-formula" "Equation" org-e-odt--enumerable-formula-p)
313 ("__DvipngImage__" "Equation" "value" "Equation" org-e-odt--enumerable-latex-image-p)
314 ("__Listing__" "Listing" "value" "Listing" org-e-odt--enumerable-p)
315 ;; ("__Table__" "Table" "category-and-value")
316 ;; ("__Figure__" "Figure" "category-and-value")
317 ;; ("__DvipngImage__" "Equation" "category-and-value")
319 "Map a CATEGORY-HANDLE to OD-VARIABLE and LABEL-STYLE.
320 This is a list where each entry is of the form \\(CATEGORY-HANDLE
321 OD-VARIABLE LABEL-STYLE CATEGORY-NAME ENUMERATOR-PREDICATE\\).
323 CATEGORY_HANDLE identifies the captionable entity in question.
325 OD-VARIABLE is the OpenDocument sequence counter associated with
326 the entity. These counters are declared within
327 \"<text:sequence-decls>...</text:sequence-decls>\" block of
328 `org-e-odt-content-template-file'.
330 LABEL-STYLE is a key into `org-e-odt-label-styles' and specifies
331 how a given entity should be captioned and referenced.
333 CATEGORY-NAME is used for qualifying captions on export. You can
334 modify the CATEGORY-NAME used in the exported document by
335 modifying `org-export-dictionary'. For example, an embedded
336 image in an English document is captioned as \"Figure 1: Orgmode
337 Logo\", by default. If you want the image to be captioned as
338 \"Illustration 1: Orgmode Logo\" instead, install an entry in
339 `org-export-dictionary' which translates \"Figure\" to
340 \"Illustration\" when the language is \"en\" and encoding is
341 `:utf-8'.
343 ENUMERATOR-PREDICATE is used for assigning a sequence number to
344 the entity. See `org-e-odt--enumerate'.")
346 (defvar org-e-odt-manifest-file-entries nil)
347 (defvar hfy-user-sheet-assoc)
349 (defvar org-e-odt-zip-dir nil
350 "Temporary work directory for OpenDocument exporter.")
354 ;;; User Configuration Variables
356 (defgroup org-export-e-odt nil
357 "Options for exporting Org mode files to ODT."
358 :tag "Org Export ODT"
359 :group 'org-export)
362 ;;;; Debugging
364 (defcustom org-e-odt-prettify-xml nil
365 "Specify whether or not the xml output should be prettified.
366 When this option is turned on, `indent-region' is run on all
367 component xml buffers before they are saved. Turn this off for
368 regular use. Turn this on if you need to examine the xml
369 visually."
370 :group 'org-export-e-odt
371 :version "24.1"
372 :type 'boolean)
375 ;;;; Document schema
377 (defcustom org-e-odt-schema-dir
378 (let* ((schema-dir
379 (catch 'schema-dir
380 (message "Debug (org-e-odt): Searching for OpenDocument schema files...")
381 (mapc
382 (lambda (schema-dir)
383 (when schema-dir
384 (message "Debug (org-e-odt): Trying %s..." schema-dir)
385 (when (and (file-expand-wildcards
386 (expand-file-name "od-manifest-schema*.rnc"
387 schema-dir))
388 (file-expand-wildcards
389 (expand-file-name "od-schema*.rnc"
390 schema-dir))
391 (file-readable-p
392 (expand-file-name "schemas.xml" schema-dir)))
393 (message "Debug (org-e-odt): Using schema files under %s"
394 schema-dir)
395 (throw 'schema-dir schema-dir))))
396 org-e-odt-schema-dir-list)
397 (message "Debug (org-e-odt): No OpenDocument schema files installed")
398 nil)))
399 schema-dir)
400 "Directory that contains OpenDocument schema files.
402 This directory contains:
403 1. rnc files for OpenDocument schema
404 2. a \"schemas.xml\" file that specifies locating rules needed
405 for auto validation of OpenDocument XML files.
407 Use the customize interface to set this variable. This ensures
408 that `rng-schema-locating-files' is updated and auto-validation
409 of OpenDocument XML takes place based on the value
410 `rng-nxml-auto-validate-flag'.
412 The default value of this variable varies depending on the
413 version of org in use and is initialized from
414 `org-e-odt-schema-dir-list'. The OASIS schema files are available
415 only in the org's private git repository. It is *not* bundled
416 with GNU ELPA tar or standard Emacs distribution."
417 :type '(choice
418 (const :tag "Not set" nil)
419 (directory :tag "Schema directory"))
420 :group 'org-export-e-odt
421 :version "24.1"
422 :set
423 (lambda (var value)
424 "Set `org-e-odt-schema-dir'.
425 Also add it to `rng-schema-locating-files'."
426 (let ((schema-dir value))
427 (set var
428 (if (and
429 (file-expand-wildcards
430 (expand-file-name "od-manifest-schema*.rnc" schema-dir))
431 (file-expand-wildcards
432 (expand-file-name "od-schema*.rnc" schema-dir))
433 (file-readable-p
434 (expand-file-name "schemas.xml" schema-dir)))
435 schema-dir
436 (when value
437 (message "Error (org-e-odt): %s has no OpenDocument schema files"
438 value))
439 nil)))
440 (when org-e-odt-schema-dir
441 (eval-after-load 'rng-loc
442 '(add-to-list 'rng-schema-locating-files
443 (expand-file-name "schemas.xml"
444 org-e-odt-schema-dir))))))
447 ;;;; Document styles
449 (defcustom org-e-odt-content-template-file nil
450 "Template file for \"content.xml\".
451 The exporter embeds the exported content just before
452 \"</office:text>\" element.
454 If unspecified, the file named \"OrgOdtContentTemplate.xml\"
455 under `org-e-odt-styles-dir' is used."
456 :type 'file
457 :group 'org-export-e-odt
458 :version "24.1")
460 (defcustom org-e-odt-styles-file nil
461 "Default styles file for use with ODT export.
462 Valid values are one of:
463 1. nil
464 2. path to a styles.xml file
465 3. path to a *.odt or a *.ott file
466 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
467 ...))
469 In case of option 1, an in-built styles.xml is used. See
470 `org-e-odt-styles-dir' for more information.
472 In case of option 3, the specified file is unzipped and the
473 styles.xml embedded therein is used.
475 In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
476 and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
477 generated odt file. Use relative path for specifying the
478 FILE-MEMBERS. styles.xml must be specified as one of the
479 FILE-MEMBERS.
481 Use options 1, 2 or 3 only if styles.xml alone suffices for
482 achieving the desired formatting. Use option 4, if the styles.xml
483 references additional files like header and footer images for
484 achieving the desired formatting.
486 Use \"#+ODT_STYLES_FILE: ...\" directive to set this variable on
487 a per-file basis. For example,
489 #+ODT_STYLES_FILE: \"/path/to/styles.xml\" or
490 #+ODT_STYLES_FILE: (\"/path/to/file.ott\" (\"styles.xml\" \"image/hdr.png\"))."
491 :group 'org-export-e-odt
492 :version "24.1"
493 :type
494 '(choice
495 (const :tag "Factory settings" nil)
496 (file :must-match t :tag "styles.xml")
497 (file :must-match t :tag "ODT or OTT file")
498 (list :tag "ODT or OTT file + Members"
499 (file :must-match t :tag "ODF Text or Text Template file")
500 (cons :tag "Members"
501 (file :tag " Member" "styles.xml")
502 (repeat (file :tag "Member"))))))
504 (defcustom org-e-odt-display-outline-level 2
505 "Outline levels considered for enumerating captioned entities."
506 :group 'org-export-e-odt
507 :version "24.2"
508 :type 'integer)
510 ;;;; Document conversion
512 (defcustom org-e-odt-convert-processes
513 '(("LibreOffice"
514 "soffice --headless --convert-to %f%x --outdir %d %i")
515 ("unoconv"
516 "unoconv -f %f -o %d %i"))
517 "Specify a list of document converters and their usage.
518 The converters in this list are offered as choices while
519 customizing `org-e-odt-convert-process'.
521 This variable is a list where each element is of the
522 form (CONVERTER-NAME CONVERTER-CMD). CONVERTER-NAME is the name
523 of the converter. CONVERTER-CMD is the shell command for the
524 converter and can contain format specifiers. These format
525 specifiers are interpreted as below:
527 %i input file name in full
528 %I input file name as a URL
529 %f format of the output file
530 %o output file name in full
531 %O output file name as a URL
532 %d output dir in full
533 %D output dir as a URL.
534 %x extra options as set in `org-e-odt-convert-capabilities'."
535 :group 'org-export-e-odt
536 :version "24.1"
537 :type
538 '(choice
539 (const :tag "None" nil)
540 (alist :tag "Converters"
541 :key-type (string :tag "Converter Name")
542 :value-type (group (string :tag "Command line")))))
544 (defcustom org-e-odt-convert-process "LibreOffice"
545 "Use this converter to convert from \"odt\" format to other formats.
546 During customization, the list of converter names are populated
547 from `org-e-odt-convert-processes'."
548 :group 'org-export-e-odt
549 :version "24.1"
550 :type '(choice :convert-widget
551 (lambda (w)
552 (apply 'widget-convert (widget-type w)
553 (eval (car (widget-get w :args)))))
554 `((const :tag "None" nil)
555 ,@(mapcar (lambda (c)
556 `(const :tag ,(car c) ,(car c)))
557 org-e-odt-convert-processes))))
559 (defcustom org-e-odt-convert-capabilities
560 '(("Text"
561 ("odt" "ott" "doc" "rtf" "docx")
562 (("pdf" "pdf") ("odt" "odt") ("rtf" "rtf") ("ott" "ott")
563 ("doc" "doc" ":\"MS Word 97\"") ("docx" "docx") ("html" "html")))
564 ("Web"
565 ("html")
566 (("pdf" "pdf") ("odt" "odt") ("html" "html")))
567 ("Spreadsheet"
568 ("ods" "ots" "xls" "csv" "xlsx")
569 (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv") ("ods" "ods")
570 ("xls" "xls") ("xlsx" "xlsx")))
571 ("Presentation"
572 ("odp" "otp" "ppt" "pptx")
573 (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("otp" "otp") ("ppt" "ppt")
574 ("pptx" "pptx") ("odg" "odg"))))
575 "Specify input and output formats of `org-e-odt-convert-process'.
576 More correctly, specify the set of input and output formats that
577 the user is actually interested in.
579 This variable is an alist where each element is of the
580 form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
581 INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
582 alist where each element is of the form (OUTPUT-FMT
583 OUTPUT-FILE-EXTENSION EXTRA-OPTIONS).
585 The variable is interpreted as follows:
586 `org-e-odt-convert-process' can take any document that is in
587 INPUT-FMT-LIST and produce any document that is in the
588 OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
589 OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
590 serves dual purposes:
591 - It is used for populating completion candidates during
592 `org-e-odt-convert' commands.
593 - It is used as the value of \"%f\" specifier in
594 `org-e-odt-convert-process'.
596 EXTRA-OPTIONS is used as the value of \"%x\" specifier in
597 `org-e-odt-convert-process'.
599 DOCUMENT-CLASS is used to group a set of file formats in
600 INPUT-FMT-LIST in to a single class.
602 Note that this variable inherently captures how LibreOffice based
603 converters work. LibreOffice maps documents of various formats
604 to classes like Text, Web, Spreadsheet, Presentation etc and
605 allow document of a given class (irrespective of it's source
606 format) to be converted to any of the export formats associated
607 with that class.
609 See default setting of this variable for an typical
610 configuration."
611 :group 'org-export-e-odt
612 :version "24.1"
613 :type
614 '(choice
615 (const :tag "None" nil)
616 (alist :tag "Capabilities"
617 :key-type (string :tag "Document Class")
618 :value-type
619 (group (repeat :tag "Input formats" (string :tag "Input format"))
620 (alist :tag "Output formats"
621 :key-type (string :tag "Output format")
622 :value-type
623 (group (string :tag "Output file extension")
624 (choice
625 (const :tag "None" nil)
626 (string :tag "Extra options"))))))))
628 (defcustom org-e-odt-preferred-output-format nil
629 "Automatically post-process to this format after exporting to \"odt\".
630 Command `org-e-odt-export-to-odt' exports first to \"odt\" format
631 and then uses `org-e-odt-convert-process' to convert the
632 resulting document to this format. During customization of this
633 variable, the list of valid values are populated based on
634 `org-e-odt-convert-capabilities'.
636 You can set this option on per-file basis using file local
637 values. See Info node `(emacs) File Variables'."
638 :group 'org-export-e-odt
639 :version "24.1"
640 :type '(choice :convert-widget
641 (lambda (w)
642 (apply 'widget-convert (widget-type w)
643 (eval (car (widget-get w :args)))))
644 `((const :tag "None" nil)
645 ,@(mapcar (lambda (c)
646 `(const :tag ,c ,c))
647 (org-e-odt-reachable-formats "odt")))))
648 ;;;###autoload
649 (put 'org-e-odt-preferred-output-format 'safe-local-variable 'stringp)
652 ;;;; Drawers
654 (defcustom org-e-odt-format-drawer-function nil
655 "Function called to format a drawer in ODT code.
657 The function must accept two parameters:
658 NAME the drawer name, like \"LOGBOOK\"
659 CONTENTS the contents of the drawer.
661 The function should return the string to be exported.
663 For example, the variable could be set to the following function
664 in order to mimic default behaviour:
666 \(defun org-e-odt-format-drawer-default \(name contents\)
667 \"Format a drawer element for ODT export.\"
668 contents\)"
669 :group 'org-export-e-odt
670 :type 'function)
673 ;;;; Headline
675 (defcustom org-e-odt-format-headline-function nil
676 "Function to format headline text.
678 This function will be called with 5 arguments:
679 TODO the todo keyword \(string or nil\).
680 TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
681 PRIORITY the priority of the headline \(integer or nil\)
682 TEXT the main headline text \(string\).
683 TAGS the tags string, separated with colons \(string or nil\).
685 The function result will be used as headline text."
686 :group 'org-export-e-odt
687 :type 'function)
690 ;;;; Inlinetasks
692 (defcustom org-e-odt-format-inlinetask-function nil
693 "Function called to format an inlinetask in ODT code.
695 The function must accept six parameters:
696 TODO the todo keyword, as a string
697 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
698 PRIORITY the inlinetask priority, as a string
699 NAME the inlinetask name, as a string.
700 TAGS the inlinetask tags, as a string.
701 CONTENTS the contents of the inlinetask, as a string.
703 The function should return the string to be exported."
704 :group 'org-export-e-odt
705 :type 'function)
708 ;;;; Links
710 (defcustom org-e-odt-inline-formula-rules
711 '(("file" . "\\.\\(mathml\\|mml\\|odf\\)\\'"))
712 "Rules characterizing formula files that can be inlined into ODT.
714 A rule consists in an association whose key is the type of link
715 to consider, and value is a regexp that will be matched against
716 link's path."
717 :group 'org-export-e-odt
718 :type '(alist :key-type (string :tag "Type")
719 :value-type (regexp :tag "Path")))
721 (defcustom org-e-odt-inline-image-rules
722 '(("file" . "\\.\\(jpeg\\|jpg\\|png\\|gif\\)\\'"))
723 "Rules characterizing image files that can be inlined into ODT.
725 A rule consists in an association whose key is the type of link
726 to consider, and value is a regexp that will be matched against
727 link's path."
728 :group 'org-export-e-odt
729 :type '(alist :key-type (string :tag "Type")
730 :value-type (regexp :tag "Path")))
732 (defcustom org-e-odt-pixels-per-inch display-pixels-per-inch
733 "Scaling factor for converting images pixels to inches.
734 Use this for sizing of embedded images. See Info node `(org)
735 Images in ODT export' for more information."
736 :type 'float
737 :group 'org-export-e-odt
738 :version "24.1")
741 ;;;; Src Block
743 (defcustom org-e-odt-create-custom-styles-for-srcblocks t
744 "Whether custom styles for colorized source blocks be automatically created.
745 When this option is turned on, the exporter creates custom styles
746 for source blocks based on the advice of `htmlfontify'. Creation
747 of custom styles happen as part of `org-e-odt-hfy-face-to-css'.
749 When this option is turned off exporter does not create such
750 styles.
752 Use the latter option if you do not want the custom styles to be
753 based on your current display settings. It is necessary that the
754 styles.xml already contains needed styles for colorizing to work.
756 This variable is effective only if
757 `org-e-odt-fontify-srcblocks' is turned on."
758 :group 'org-export-e-odt
759 :version "24.1"
760 :type 'boolean)
762 (defcustom org-e-odt-fontify-srcblocks t
763 "Specify whether or not source blocks need to be fontified.
764 Turn this option on if you want to colorize the source code
765 blocks in the exported file. For colorization to work, you need
766 to make available an enhanced version of `htmlfontify' library."
767 :type 'boolean
768 :group 'org-export-e-odt
769 :version "24.1")
772 ;;;; Table
774 (defcustom org-e-odt-table-styles
775 '(("OrgEquation" "OrgEquation"
776 ((use-first-column-styles . t)
777 (use-last-column-styles . t)))
778 ("TableWithHeaderRowAndColumn" "Custom"
779 ((use-first-row-styles . t)
780 (use-first-column-styles . t)))
781 ("TableWithFirstRowandLastRow" "Custom"
782 ((use-first-row-styles . t)
783 (use-last-row-styles . t)))
784 ("GriddedTable" "Custom" nil))
785 "Specify how Table Styles should be derived from a Table Template.
786 This is a list where each element is of the
787 form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
789 TABLE-STYLE-NAME is the style associated with the table through
790 \"#+ATTR_ODT: :style TABLE-STYLE-NAME\" line.
792 TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
793 TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
794 below) that is included in
795 `org-e-odt-content-template-file'.
797 TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
798 \"TableCell\"
799 PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
800 \"TableParagraph\"
801 TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
802 \"FirstRow\" | \"LastRow\" |
803 \"EvenRow\" | \"OddRow\" |
804 \"EvenColumn\" | \"OddColumn\" | \"\"
805 where \"+\" above denotes string concatenation.
807 TABLE-CELL-OPTIONS is an alist where each element is of the
808 form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
809 TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
810 `use-last-row-styles' |
811 `use-first-column-styles' |
812 `use-last-column-styles' |
813 `use-banding-rows-styles' |
814 `use-banding-columns-styles' |
815 `use-first-row-styles'
816 ON-OR-OFF := `t' | `nil'
818 For example, with the following configuration
820 \(setq org-e-odt-table-styles
821 '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
822 \(\(use-first-row-styles . t\)
823 \(use-first-column-styles . t\)\)\)
824 \(\"TableWithHeaderColumns\" \"Custom\"
825 \(\(use-first-column-styles . t\)\)\)\)\)
827 1. A table associated with \"TableWithHeaderRowsAndColumns\"
828 style will use the following table-cell styles -
829 \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
830 \"CustomTableCell\" and the following paragraph styles
831 \"CustomFirstRowTableParagraph\",
832 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
833 as appropriate.
835 2. A table associated with \"TableWithHeaderColumns\" style will
836 use the following table-cell styles -
837 \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
838 following paragraph styles
839 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
840 as appropriate..
842 Note that TABLE-TEMPLATE-NAME corresponds to the
843 \"<table:table-template>\" elements contained within
844 \"<office:styles>\". The entries (TABLE-STYLE-NAME
845 TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
846 \"table:template-name\" and \"table:use-first-row-styles\" etc
847 attributes of \"<table:table>\" element. Refer ODF-1.2
848 specification for more information. Also consult the
849 implementation filed under `org-e-odt-get-table-cell-styles'.
851 The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
852 formatting of numbered display equations. Do not delete this
853 style from the list."
854 :group 'org-export-e-odt
855 :version "24.1"
856 :type '(choice
857 (const :tag "None" nil)
858 (repeat :tag "Table Styles"
859 (list :tag "Table Style Specification"
860 (string :tag "Table Style Name")
861 (string :tag "Table Template Name")
862 (alist :options (use-first-row-styles
863 use-last-row-styles
864 use-first-column-styles
865 use-last-column-styles
866 use-banding-rows-styles
867 use-banding-columns-styles)
868 :key-type symbol
869 :value-type (const :tag "True" t))))))
871 ;;;; Timestamps
873 (defcustom org-e-odt-use-date-fields nil
874 "Non-nil, if timestamps should be exported as date fields.
876 When nil, export timestamps as plain text.
878 When non-nil, map `org-time-stamp-custom-formats' to a pair of
879 OpenDocument date-styles with names \"OrgDate1\" and \"OrgDate2\"
880 respectively. A timestamp with no time component is formatted
881 with style \"OrgDate1\" while one with explicit hour and minutes
882 is formatted with style \"OrgDate2\".
884 This feature is experimental. Most (but not all) of the common
885 %-specifiers in `format-time-string' are supported.
886 Specifically, locale-dependent specifiers like \"%c\", \"%x\" are
887 formatted as canonical Org timestamps. For finer control, avoid
888 these %-specifiers.
890 Textutal specifiers like \"%b\", \"%h\", \"%B\", \"%a\", \"%A\"
891 etc., are displayed by the application in the default language
892 and country specified in `org-e-odt-styles-file'. Note that the
893 default styles file uses language \"en\" and country \"GB\". You
894 can localize the week day and month strings in the exported
895 document by setting the default language and country either using
896 the application UI or through a custom styles file.
898 See `org-e-odt--build-date-styles' for implementation details."
899 :group 'org-export-e-odt
900 :type 'boolean)
904 ;;; Internal functions
906 ;;;; Date
908 (defun org-e-odt--format-timestamp (timestamp &optional end iso-date-p)
909 (let* ((format-timestamp
910 (lambda (timestamp format &optional end utc)
911 (if timestamp
912 (org-timestamp-format timestamp format end utc)
913 (format-time-string format nil utc))))
914 (has-time-p (or (not timestamp)
915 (org-timestamp-has-time-p timestamp)))
916 (iso-date (let ((format (if has-time-p "%Y-%m-%dT%H:%M:%S"
917 "%Y-%m-%dT%H:%M:%S")))
918 (funcall format-timestamp timestamp format end))))
919 (if iso-date-p iso-date
920 (let* ((style (if has-time-p "OrgDate2" "OrgDate1"))
921 ;; LibreOffice does not care about end goes as content
922 ;; within the "<text:date>...</text:date>" field. The
923 ;; displayed date is automagically corrected to match the
924 ;; format requested by "style:data-style-name" attribute. So
925 ;; don't bother about formatting the date contents to be
926 ;; compatible with "OrgDate1" and "OrgDateTime" styles. A
927 ;; simple Org-style date should suffice.
928 (date (let* ((formats
929 (if org-display-custom-times
930 (cons (substring
931 (car org-time-stamp-custom-formats) 1 -1)
932 (substring
933 (cdr org-time-stamp-custom-formats) 1 -1))
934 '("%Y-%m-%d %a" . "%Y-%m-%d %a %H:%M")))
935 (format (if has-time-p (cdr formats) (car formats))))
936 (funcall format-timestamp timestamp format end)))
937 (repeater (let ((repeater-type (org-element-property
938 :repeater-type timestamp))
939 (repeater-value (org-element-property
940 :repeater-value timestamp))
941 (repeater-unit (org-element-property
942 :repeater-unit timestamp)))
943 (concat
944 (case repeater-type
945 (catchup "++") (restart ".+") (cumulate "+"))
946 (when repeater-value
947 (number-to-string repeater-value))
948 (case repeater-unit
949 (hour "h") (day "d") (week "w") (month "m")
950 (year "y"))))))
951 (concat
952 (format "<text:date text:date-value=\"%s\" style:data-style-name=\"%s\" text:fixed=\"true\">%s</text:date>"
953 iso-date style date)
954 (and (not (string= repeater "")) " ")
955 repeater)))))
957 ;;;; Frame
959 (defun org-e-odt--frame (text width height style &optional extra
960 anchor-type &rest title-and-desc)
961 (let ((frame-attrs
962 (concat
963 (if width (format " svg:width=\"%0.2fcm\"" width) "")
964 (if height (format " svg:height=\"%0.2fcm\"" height) "")
965 extra
966 (format " text:anchor-type=\"%s\"" (or anchor-type "paragraph")))))
967 (format
968 "\n<draw:frame draw:style-name=\"%s\"%s>\n%s\n</draw:frame>"
969 style frame-attrs
970 (concat text
971 (let ((title (car title-and-desc))
972 (desc (cadr title-and-desc)))
973 (concat (when title
974 (format "<svg:title>%s</svg:title>"
975 (org-e-odt--encode-plain-text title t)))
976 (when desc
977 (format "<svg:desc>%s</svg:desc>"
978 (org-e-odt--encode-plain-text desc t)))))))))
981 ;;;; Library wrappers
983 (defun org-e-odt--zip-extract (archive members target)
984 (when (atom members) (setq members (list members)))
985 (mapc (lambda (member)
986 (require 'arc-mode)
987 (let* ((--quote-file-name
988 ;; This is shamelessly stolen from `archive-zip-extract'.
989 (lambda (name)
990 (if (or (not (memq system-type '(windows-nt ms-dos)))
991 (and (boundp 'w32-quote-process-args)
992 (null w32-quote-process-args)))
993 (shell-quote-argument name)
994 name)))
995 (target (funcall --quote-file-name target))
996 (archive (expand-file-name archive))
997 (archive-zip-extract
998 (list "unzip" "-qq" "-o" "-d" target))
999 exit-code command-output)
1000 (setq command-output
1001 (with-temp-buffer
1002 (setq exit-code (archive-zip-extract archive member))
1003 (buffer-string)))
1004 (unless (zerop exit-code)
1005 (message command-output)
1006 (error "Extraction failed"))))
1007 members))
1009 (defun org-e-odt--suppress-some-translators (info types)
1010 ;; See comments in `org-e-odt-format-label' and `org-e-odt-toc'.
1011 (org-combine-plists
1012 info (list
1013 ;; Override translators.
1014 :translate-alist
1015 (nconc (mapcar (lambda (type) (cons type (lambda (data contents info)
1016 contents))) types)
1017 (plist-get info :translate-alist))
1018 ;; Reset data translation cache. FIXME.
1019 ;; :exported-data nil
1023 ;;;; Target
1025 (defun org-e-odt--target (text id)
1026 (if (not id) text
1027 (concat
1028 (format "\n<text:bookmark-start text:name=\"OrgXref.%s\"/>" id)
1029 (format "\n<text:bookmark text:name=\"%s\"/>" id) text
1030 (format "\n<text:bookmark-end text:name=\"OrgXref.%s\"/>" id))))
1032 ;;;; Textbox
1034 (defun org-e-odt--textbox (text width height style &optional
1035 extra anchor-type)
1036 (org-e-odt--frame
1037 (format "\n<draw:text-box %s>%s\n</draw:text-box>"
1038 (concat (format " fo:min-height=\"%0.2fcm\"" (or height .2))
1039 (and (not width)
1040 (format " fo:min-width=\"%0.2fcm\"" (or width .2))))
1041 text)
1042 width nil style extra anchor-type))
1046 ;;;; Table of Contents
1048 (defun org-e-odt-begin-toc (index-title depth)
1049 (concat
1050 (format "
1051 <text:table-of-content text:style-name=\"OrgIndexSection\" text:protected=\"true\" text:name=\"Table of Contents\">
1052 <text:table-of-content-source text:outline-level=\"%d\">
1053 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
1054 " depth index-title)
1056 (let ((levels (number-sequence 1 10)))
1057 (mapconcat
1058 (lambda (level)
1059 (format
1061 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
1062 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
1063 <text:index-entry-chapter/>
1064 <text:index-entry-text/>
1065 <text:index-entry-link-end/>
1066 </text:table-of-content-entry-template>
1067 " level level)) levels ""))
1069 (format "
1070 </text:table-of-content-source>
1072 <text:index-body>
1073 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
1074 <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
1075 </text:index-title>
1076 " index-title)))
1078 (defun org-e-odt-end-toc ()
1079 (format "
1080 </text:index-body>
1081 </text:table-of-content>
1084 (defun* org-e-odt-format-toc-headline
1085 (todo todo-type priority text tags
1086 &key level section-number headline-label &allow-other-keys)
1087 (setq text
1088 (concat
1089 ;; Section number.
1090 (when section-number (concat section-number ". "))
1091 ;; Todo.
1092 (when todo
1093 (let ((style (if (member todo org-done-keywords)
1094 "OrgDone" "OrgTodo")))
1095 (format "<text:span text:style-name=\"%s\">%s</text:span> "
1096 style todo)))
1097 (when priority
1098 (let* ((style (format "OrgPriority-%s" priority))
1099 (priority (format "[#%c]" priority)))
1100 (format "<text:span text:style-name=\"%s\">%s</text:span> "
1101 style priority)))
1102 ;; Title.
1103 text
1104 ;; Tags.
1105 (when tags
1106 (concat
1107 (format " <text:span text:style-name=\"%s\">[%s]</text:span>"
1108 "OrgTags"
1109 (mapconcat
1110 (lambda (tag)
1111 (format
1112 "<text:span text:style-name=\"%s\">%s</text:span>"
1113 "OrgTag" tag)) tags " : "))))))
1114 (format "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
1115 headline-label text))
1117 (defun org-e-odt-toc (depth info)
1118 (assert (wholenump depth))
1119 ;; When a headline is marked as a radio target, as in the example below:
1121 ;; ** <<<Some Heading>>>
1122 ;; Some text.
1124 ;; suppress generation of radio targets. i.e., Radio targets are to
1125 ;; be marked as targets within /document body/ and *not* within
1126 ;; /TOC/, as otherwise there will be duplicated anchors one in TOC
1127 ;; and one in the document body.
1129 ;; FIXME-1: Currently exported headings are memoized. `org-export.el'
1130 ;; doesn't provide a way to disable memoization. So this doesn't
1131 ;; work.
1133 ;; FIXME-2: Are there any other objects that need to be suppressed
1134 ;; within TOC?
1135 (let* ((title (org-export-translate "Table of Contents" :utf-8 info))
1136 (headlines (org-export-collect-headlines
1137 info (and (wholenump depth) depth)))
1138 (translations (nconc (mapcar
1139 (lambda (type)
1140 (cons type (lambda (data contents info)
1141 contents)))
1142 (list 'radio-target))
1143 (plist-get info :translate-alist))))
1144 (when headlines
1145 (concat
1146 (org-e-odt-begin-toc title depth)
1147 (mapconcat
1148 (lambda (headline)
1149 (let* ((entry (org-e-odt-format-headline--wrap
1150 headline translations info
1151 'org-e-odt-format-toc-headline))
1152 (level (org-export-get-relative-level headline info))
1153 (style (format "Contents_20_%d" level)))
1154 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1155 style entry)))
1156 headlines "\n")
1157 (org-e-odt-end-toc)))))
1160 ;;;; Document styles
1162 (defun org-e-odt-add-automatic-style (object-type &optional object-props)
1163 "Create an automatic style of type OBJECT-TYPE with param OBJECT-PROPS.
1164 OBJECT-PROPS is (typically) a plist created by passing
1165 \"#+ATTR_ODT: \" option of the object in question to
1166 `org-e-odt-parse-block-attributes'.
1168 Use `org-e-odt-object-counters' to generate an automatic
1169 OBJECT-NAME and STYLE-NAME. If OBJECT-PROPS is non-nil, add a
1170 new entry in `org-e-odt-automatic-styles'. Return (OBJECT-NAME
1171 . STYLE-NAME)."
1172 (assert (stringp object-type))
1173 (let* ((object (intern object-type))
1174 (seqvar object)
1175 (seqno (1+ (or (plist-get org-e-odt-object-counters seqvar) 0)))
1176 (object-name (format "%s%d" object-type seqno)) style-name)
1177 (setq org-e-odt-object-counters
1178 (plist-put org-e-odt-object-counters seqvar seqno))
1179 (when object-props
1180 (setq style-name (format "Org%s" object-name))
1181 (setq org-e-odt-automatic-styles
1182 (plist-put org-e-odt-automatic-styles object
1183 (append (list (list style-name object-props))
1184 (plist-get org-e-odt-automatic-styles object)))))
1185 (cons object-name style-name)))
1187 ;;;; Checkbox
1189 (defun org-e-odt--checkbox (item)
1190 "Return check-box string associated to ITEM."
1191 (let ((checkbox (org-element-property :checkbox item)))
1192 (if (not checkbox) ""
1193 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1194 "OrgCode" (case checkbox
1195 (on "[&#x2713;] ") ; CHECK MARK
1196 (off "[ ] ")
1197 (trans "[-] "))))))
1199 ;;; Template
1201 (defun org-e-odt--build-date-styles (fmt style)
1202 ;; In LibreOffice 3.4.6, there doesn't seem to be a convenient way
1203 ;; to modify the date fields. A date could be modified by
1204 ;; offsetting in days. That's about it. Also, date and time may
1205 ;; have to be emitted as two fields - a date field and a time field
1206 ;; - separately.
1208 ;; One can add Form Controls to date and time fields so that they
1209 ;; can be easily modified. But then, the exported document will
1210 ;; become tightly coupled with LibreOffice and may not function
1211 ;; properly with other OpenDocument applications.
1213 ;; I have a strange feeling that Date styles are a bit flaky at the
1214 ;; moment.
1216 ;; The feature is experimental.
1217 (when (and fmt style)
1218 (let* ((fmt-alist
1219 '(("%A" . "<number:day-of-week number:style=\"long\"/>")
1220 ("%B" . "<number:month number:textual=\"true\" number:style=\"long\"/>")
1221 ("%H" . "<number:hours number:style=\"long\"/>")
1222 ("%M" . "<number:minutes number:style=\"long\"/>")
1223 ("%S" . "<number:seconds number:style=\"long\"/>")
1224 ("%V" . "<number:week-of-year/>")
1225 ("%Y" . "<number:year number:style=\"long\"/>")
1226 ("%a" . "<number:day-of-week number:style=\"short\"/>")
1227 ("%b" . "<number:month number:textual=\"true\" number:style=\"short\"/>")
1228 ("%d" . "<number:day number:style=\"long\"/>")
1229 ("%e" . "<number:day number:style=\"short\"/>")
1230 ("%h" . "<number:month number:textual=\"true\" number:style=\"short\"/>")
1231 ("%k" . "<number:hours number:style=\"short\"/>")
1232 ("%m" . "<number:month number:style=\"long\"/>")
1233 ("%p" . "<number:am-pm/>")
1234 ("%y" . "<number:year number:style=\"short\"/>")))
1235 (case-fold-search nil)
1236 (re (mapconcat 'identity (mapcar 'car fmt-alist) "\\|"))
1237 match rpl (start 0) (filler-beg 0) filler-end filler output)
1238 (mapc
1239 (lambda (pair)
1240 (setq fmt (replace-regexp-in-string (car pair) (cdr pair) fmt t t)))
1241 '(("\\(?:%[[:digit:]]*N\\)" . "") ; strip ns, us and ns
1242 ("%C" . "Y") ; replace century with year
1243 ("%D" . "%m/%d/%y")
1244 ("%G" . "Y") ; year corresponding to iso week
1245 ("%I" . "%H") ; hour on a 12-hour clock
1246 ("%R" . "%H:%M")
1247 ("%T" . "%H:%M:%S")
1248 ("%U\\|%W" . "%V") ; week no. starting on Sun./Mon.
1249 ("%Z" . "") ; time zone name
1250 ("%c" . "%Y-%M-%d %a %H:%M" ) ; locale's date and time format
1251 ("%g" . "%y")
1252 ("%X" . "%x" ) ; locale's pref. time format
1253 ("%j" . "") ; day of the year
1254 ("%l" . "%k") ; like %I blank-padded
1255 ("%s" . "") ; no. of secs since 1970-01-01 00:00:00 +0000
1256 ("%n" . "<text:line-break/>")
1257 ("%r" . "%I:%M:%S %p")
1258 ("%t" . "<text:tab/>")
1259 ("%u\\|%w" . "") ; numeric day of week - Mon (1-7), Sun(0-6)
1260 ("%x" . "%Y-%M-%d %a") ; locale's pref. time format
1261 ("%z" . "") ; time zone in numeric form
1263 (while (string-match re fmt start)
1264 (setq match (match-string 0 fmt))
1265 (setq rpl (assoc-default match fmt-alist))
1266 (setq start (match-end 0))
1267 (setq filler-end (match-beginning 0))
1268 (setq filler (substring fmt (prog1 filler-beg
1269 (setq filler-beg (match-end 0)))
1270 filler-end))
1271 (setq filler (and (not (string= filler ""))
1272 (format "<number:text>%s</number:text>"
1273 (org-e-odt--encode-plain-text filler))))
1274 (setq output (concat output "\n" filler "\n" rpl)))
1275 (setq filler (substring fmt filler-beg))
1276 (unless (string= filler "")
1277 (setq output (concat output
1278 (format "\n<number:text>%s</number:text>"
1279 (org-e-odt--encode-plain-text filler)))))
1280 (format "\n<number:date-style style:name=\"%s\" %s>%s\n</number:date-style>"
1281 style
1282 (concat " number:automatic-order=\"true\""
1283 " number:format-source=\"fixed\"")
1284 output ))))
1286 (defun org-e-odt-template (contents info)
1287 "Return complete document string after ODT conversion.
1288 CONTENTS is the transcoded contents string. RAW-DATA is the
1289 original parsed data. INFO is a plist holding export options."
1290 ;; Write meta file.
1291 (let ((title (org-export-data (plist-get info :title) info))
1292 (author (let ((author (plist-get info :author)))
1293 (if (not author) "" (org-export-data author info))))
1294 (email (plist-get info :email))
1295 (keywords (plist-get info :keywords))
1296 (description (plist-get info :description)))
1297 (write-region
1298 (concat
1299 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
1300 <office:document-meta
1301 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
1302 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
1303 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
1304 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
1305 xmlns:ooo=\"http://openoffice.org/2004/office\"
1306 office:version=\"1.2\">
1307 <office:meta>\n"
1308 (format "<dc:creator>%s</dc:creator>\n" author)
1309 (format "<meta:initial-creator>%s</meta:initial-creator>\n" author)
1310 ;; Date, if required.
1311 (when (plist-get info :with-date)
1312 ;; Check if DATE is specified as an Org-timestamp. If yes,
1313 ;; include it as meta information. Otherwise, just use
1314 ;; today's date.
1315 (let* ((date (let ((date (plist-get info :date)))
1316 (and (not (cdr date))
1317 (eq (org-element-type (car date)) 'timestamp)
1318 (car date)))))
1319 (let ((iso-date (org-e-odt--format-timestamp date nil 'iso-date)))
1320 (concat
1321 (format "<dc:date>%s</dc:date>\n" iso-date)
1322 (format "<meta:creation-date>%s</meta:creation-date>\n"
1323 iso-date)))))
1324 (format "<meta:generator>%s</meta:generator>\n"
1325 (let ((creator-info (plist-get info :with-creator)))
1326 (if (or (not creator-info) (eq creator-info 'comment)) ""
1327 (plist-get info :creator))))
1328 (format "<meta:keyword>%s</meta:keyword>\n" keywords)
1329 (format "<dc:subject>%s</dc:subject>\n" description)
1330 (format "<dc:title>%s</dc:title>\n" title)
1331 "\n"
1332 " </office:meta>\n" "</office:document-meta>")
1333 nil (concat org-e-odt-zip-dir "meta.xml"))
1334 ;; Add meta.xml in to manifest.
1335 (org-e-odt-create-manifest-file-entry "text/xml" "meta.xml"))
1337 ;; Update styles file.
1338 ;; Copy styles.xml. Also dump htmlfontify styles, if there is any.
1339 ;; Write styles file.
1340 (let* ((styles-file (plist-get info :odt-styles-file))
1341 (styles-file (and styles-file (read (org-trim styles-file))))
1342 ;; Non-availability of styles.xml is not a critical
1343 ;; error. For now, throw an error.
1344 (styles-file (or styles-file
1345 org-e-odt-styles-file
1346 (expand-file-name "OrgOdtStyles.xml"
1347 org-e-odt-styles-dir)
1348 (error "org-e-odt: Missing styles file?"))))
1349 (cond
1350 ((listp styles-file)
1351 (let ((archive (nth 0 styles-file))
1352 (members (nth 1 styles-file)))
1353 (org-e-odt--zip-extract archive members org-e-odt-zip-dir)
1354 (mapc
1355 (lambda (member)
1356 (when (org-file-image-p member)
1357 (let* ((image-type (file-name-extension member))
1358 (media-type (format "image/%s" image-type)))
1359 (org-e-odt-create-manifest-file-entry media-type member))))
1360 members)))
1361 ((and (stringp styles-file) (file-exists-p styles-file))
1362 (let ((styles-file-type (file-name-extension styles-file)))
1363 (cond
1364 ((string= styles-file-type "xml")
1365 (copy-file styles-file (concat org-e-odt-zip-dir "styles.xml") t))
1366 ((member styles-file-type '("odt" "ott"))
1367 (org-e-odt--zip-extract styles-file "styles.xml" org-e-odt-zip-dir)))))
1369 (error (format "Invalid specification of styles.xml file: %S"
1370 org-e-odt-styles-file))))
1372 ;; create a manifest entry for styles.xml
1373 (org-e-odt-create-manifest-file-entry "text/xml" "styles.xml")
1375 ;; FIXME: Who is opening an empty styles.xml before this point?
1376 (with-current-buffer
1377 (find-file-noselect (concat org-e-odt-zip-dir "styles.xml") t)
1378 (revert-buffer t t)
1380 ;; Write custom styles for source blocks
1381 ;; Save STYLES used for colorizing of source blocks.
1382 ;; Update styles.xml with styles that were collected as part of
1383 ;; `org-e-odt-hfy-face-to-css' callbacks.
1384 (let ((styles (mapconcat (lambda (style) (format " %s\n" (cddr style)))
1385 hfy-user-sheet-assoc "")))
1386 (when styles
1387 (goto-char (point-min))
1388 (when (re-search-forward "</office:styles>" nil t)
1389 (goto-char (match-beginning 0))
1390 (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n"))))
1392 ;; Update styles.xml - take care of outline numbering
1394 ;; Don't make automatic backup of styles.xml file. This setting
1395 ;; prevents the backed-up styles.xml file from being zipped in to
1396 ;; odt file. This is more of a hackish fix. Better alternative
1397 ;; would be to fix the zip command so that the output odt file
1398 ;; includes only the needed files and excludes any auto-generated
1399 ;; extra files like backups and auto-saves etc etc. Note that
1400 ;; currently the zip command zips up the entire temp directory so
1401 ;; that any auto-generated files created under the hood ends up in
1402 ;; the resulting odt file.
1403 (set (make-local-variable 'backup-inhibited) t)
1405 ;; Outline numbering is retained only upto LEVEL.
1406 ;; To disable outline numbering pass a LEVEL of 0.
1408 (goto-char (point-min))
1409 (let ((regex
1410 "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
1411 (replacement
1412 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1413 (while (re-search-forward regex nil t)
1414 (unless (let ((sec-num (plist-get info :section-numbers))
1415 (level (string-to-number (match-string 2))))
1416 (if (wholenump sec-num) (<= level sec-num) sec-num))
1417 (replace-match replacement t nil))))
1418 (save-buffer 0)))
1419 ;; Update content.xml.
1421 (let* ( ;; `org-display-custom-times' should be accessed right
1422 ;; within the context of the Org buffer. So obtain it's
1423 ;; value before moving on to temp-buffer context down below.
1424 (custom-time-fmts
1425 (if org-display-custom-times
1426 (cons (substring (car org-time-stamp-custom-formats) 1 -1)
1427 (substring (cdr org-time-stamp-custom-formats) 1 -1))
1428 '("%Y-%M-%d %a" . "%Y-%M-%d %a %H:%M"))))
1429 (with-temp-buffer
1430 (insert-file-contents
1431 (or org-e-odt-content-template-file
1432 (expand-file-name "OrgOdtContentTemplate.xml"
1433 org-e-odt-styles-dir)))
1434 ;; Write automatic styles.
1435 ;; - Position the cursor.
1436 (goto-char (point-min))
1437 (re-search-forward " </office:automatic-styles>" nil t)
1438 (goto-char (match-beginning 0))
1439 ;; - Dump automatic table styles.
1440 (loop for (style-name props) in
1441 (plist-get org-e-odt-automatic-styles 'Table) do
1442 (when (setq props (or (plist-get props :rel-width) 96))
1443 (insert (format org-e-odt-table-style-format style-name props))))
1444 ;; - Dump date-styles.
1445 (when org-e-odt-use-date-fields
1446 (insert (org-e-odt--build-date-styles (car custom-time-fmts)
1447 "OrgDate1")
1448 (org-e-odt--build-date-styles (cdr custom-time-fmts)
1449 "OrgDate2")))
1450 ;; Update display level.
1451 ;; - Remove existing sequence decls. Also position the cursor.
1452 (goto-char (point-min))
1453 (when (re-search-forward "<text:sequence-decls" nil t)
1454 (delete-region (match-beginning 0)
1455 (re-search-forward "</text:sequence-decls>" nil nil)))
1456 ;; Update sequence decls according to user preference.
1457 (insert
1458 (format
1459 "\n<text:sequence-decls>\n%s\n</text:sequence-decls>"
1460 (mapconcat
1461 (lambda (x)
1462 (format
1463 "<text:sequence-decl text:display-outline-level=\"%d\" text:name=\"%s\"/>"
1464 org-e-odt-display-outline-level (nth 1 x)))
1465 org-e-odt-category-map-alist "\n")))
1466 ;; Position the cursor to document body.
1467 (goto-char (point-min))
1468 (re-search-forward "</office:text>" nil nil)
1469 (goto-char (match-beginning 0))
1471 ;; Preamble - Title, Author, Date etc.
1472 (insert
1473 (let* ((title (org-export-data (plist-get info :title) info))
1474 (author (and (plist-get info :with-author)
1475 (let ((auth (plist-get info :author)))
1476 (and auth (org-export-data auth info)))))
1477 (email (plist-get info :email))
1478 ;; Switch on or off above vars based on user settings
1479 (author (and (plist-get info :with-author) (or author email)))
1480 (email (and (plist-get info :with-email) email)))
1481 (concat
1482 ;; Title.
1483 (when title
1484 (concat
1485 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1486 "OrgTitle" (format "\n<text:title>%s</text:title>" title))
1487 ;; Separator.
1488 "\n<text:p text:style-name=\"OrgTitle\"/>"))
1489 (cond
1490 ((and author (not email))
1491 ;; Author only.
1492 (concat
1493 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1494 "OrgSubtitle"
1495 (format "<text:initial-creator>%s</text:initial-creator>" author))
1496 ;; Separator.
1497 "\n<text:p text:style-name=\"OrgSubtitle\"/>"))
1498 ((and author email)
1499 ;; Author and E-mail.
1500 (concat
1501 (format
1502 "\n<text:p text:style-name=\"%s\">%s</text:p>"
1503 "OrgSubtitle"
1504 (format
1505 "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
1506 (concat "mailto:" email)
1507 (format "<text:initial-creator>%s</text:initial-creator>" author)))
1508 ;; Separator.
1509 "\n<text:p text:style-name=\"OrgSubtitle\"/>")))
1510 ;; Date, if required.
1511 (when (plist-get info :with-date)
1512 (let* ((date (plist-get info :date))
1513 ;; Check if DATE is specified as a timestamp.
1514 (timestamp (and (not (cdr date))
1515 (eq (org-element-type (car date)) 'timestamp)
1516 (car date))))
1517 (concat
1518 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1519 "OrgSubtitle"
1520 (if (and org-e-odt-use-date-fields timestamp)
1521 (org-e-odt--format-timestamp (car date))
1522 (org-export-data (plist-get info :date) info)))
1523 ;; Separator
1524 "<text:p text:style-name=\"OrgSubtitle\"/>"))))))
1525 ;; Table of Contents
1526 (let* ((with-toc (plist-get info :with-toc))
1527 (depth (and with-toc (if (wholenump with-toc)
1528 with-toc
1529 (plist-get info :headline-levels)))))
1530 (when depth (insert (or (org-e-odt-toc depth info) ""))))
1531 ;; Contents.
1532 (insert contents)
1533 ;; Return contents.
1534 (buffer-substring-no-properties (point-min) (point-max)))))
1538 ;;; Transcode Functions
1540 ;;;; Bold
1542 (defun org-e-odt-bold (bold contents info)
1543 "Transcode BOLD from Org to ODT.
1544 CONTENTS is the text with bold markup. INFO is a plist holding
1545 contextual information."
1546 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1547 "Bold" contents))
1550 ;;;; Center Block
1552 (defun org-e-odt-center-block (center-block contents info)
1553 "Transcode a CENTER-BLOCK element from Org to ODT.
1554 CONTENTS holds the contents of the center block. INFO is a plist
1555 holding contextual information."
1556 contents)
1559 ;;;; Clock
1561 (defun org-e-odt-clock (clock contents info)
1562 "Transcode a CLOCK element from Org to ODT.
1563 CONTENTS is nil. INFO is a plist used as a communication
1564 channel."
1565 (let ((timestamp (org-element-property :value clock))
1566 (duration (org-element-property :duration clock)))
1567 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1568 (if (eq (org-element-type (org-export-get-next-element clock info))
1569 'clock) "OrgClock" "OrgClockLastLine")
1570 (concat
1571 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1572 "OrgClockKeyword" org-clock-string)
1573 (org-e-odt-timestamp timestamp contents info)
1574 (and duration (format " (%s)" duration))))))
1577 ;;;; Code
1579 (defun org-e-odt-code (code contents info)
1580 "Transcode a CODE object from Org to ODT.
1581 CONTENTS is nil. INFO is a plist used as a communication
1582 channel."
1583 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1584 "OrgCode" (org-element-property :value code)))
1587 ;;;; Comment
1589 ;; Comments are ignored.
1592 ;;;; Comment Block
1594 ;; Comment Blocks are ignored.
1597 ;;;; Drawer
1599 (defun org-e-odt-drawer (drawer contents info)
1600 "Transcode a DRAWER element from Org to ODT.
1601 CONTENTS holds the contents of the block. INFO is a plist
1602 holding contextual information."
1603 (let* ((name (org-element-property :drawer-name drawer))
1604 (output (if (functionp org-e-odt-format-drawer-function)
1605 (funcall org-e-odt-format-drawer-function
1606 name contents)
1607 ;; If there's no user defined function: simply
1608 ;; display contents of the drawer.
1609 contents)))
1610 output))
1613 ;;;; Dynamic Block
1615 (defun org-e-odt-dynamic-block (dynamic-block contents info)
1616 "Transcode a DYNAMIC-BLOCK element from Org to ODT.
1617 CONTENTS holds the contents of the block. INFO is a plist
1618 holding contextual information. See `org-export-data'."
1619 contents)
1622 ;;;; Entity
1624 (defun org-e-odt-entity (entity contents info)
1625 "Transcode an ENTITY object from Org to ODT.
1626 CONTENTS are the definition itself. INFO is a plist holding
1627 contextual information."
1628 (org-element-property :utf-8 entity))
1631 ;;;; Example Block
1633 (defun org-e-odt-example-block (example-block contents info)
1634 "Transcode a EXAMPLE-BLOCK element from Org to ODT.
1635 CONTENTS is nil. INFO is a plist holding contextual information."
1636 (org-e-odt-format-code example-block info))
1639 ;;;; Export Snippet
1641 (defun org-e-odt-export-snippet (export-snippet contents info)
1642 "Transcode a EXPORT-SNIPPET object from Org to ODT.
1643 CONTENTS is nil. INFO is a plist holding contextual information."
1644 (when (eq (org-export-snippet-backend export-snippet) 'e-odt)
1645 (org-element-property :value export-snippet)))
1648 ;;;; Export Block
1650 (defun org-e-odt-export-block (export-block contents info)
1651 "Transcode a EXPORT-BLOCK element from Org to ODT.
1652 CONTENTS is nil. INFO is a plist holding contextual information."
1653 (when (string= (org-element-property :type export-block) "ODT")
1654 (org-remove-indentation (org-element-property :value export-block))))
1657 ;;;; Fixed Width
1659 (defun org-e-odt-fixed-width (fixed-width contents info)
1660 "Transcode a FIXED-WIDTH element from Org to ODT.
1661 CONTENTS is nil. INFO is a plist holding contextual information."
1662 (org-e-odt-do-format-code (org-element-property :value fixed-width)))
1665 ;;;; Footnote Definition
1667 ;; Footnote Definitions are ignored.
1670 ;;;; Footnote Reference
1672 (defun org-e-odt-footnote-reference (footnote-reference contents info)
1673 "Transcode a FOOTNOTE-REFERENCE element from Org to ODT.
1674 CONTENTS is nil. INFO is a plist holding contextual information."
1675 (let ((--format-footnote-definition
1676 (function
1677 (lambda (n def)
1678 (setq n (format "%d" n))
1679 (let ((id (concat "fn" n))
1680 (note-class "footnote")
1681 (par-style "Footnote"))
1682 (format
1683 "<text:note text:id=\"%s\" text:note-class=\"%s\">%s</text:note>"
1684 id note-class
1685 (concat
1686 (format "<text:note-citation>%s</text:note-citation>" n)
1687 (format "<text:note-body>%s</text:note-body>" def)))))))
1688 (--format-footnote-reference
1689 (function
1690 (lambda (n)
1691 (setq n (format "%d" n))
1692 (let ((note-class "footnote")
1693 (ref-format "text")
1694 (ref-name (concat "fn" n)))
1695 (format
1696 "<text:span text:style-name=\"%s\">%s</text:span>"
1697 "OrgSuperscript"
1698 (format "<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:note-ref>"
1699 note-class ref-format ref-name n)))))))
1700 (concat
1701 ;; Insert separator between two footnotes in a row.
1702 (let ((prev (org-export-get-previous-element footnote-reference info)))
1703 (and (eq (org-element-type prev) 'footnote-reference)
1704 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1705 "OrgSuperscript" ",")))
1706 ;; Trancode footnote reference.
1707 (let ((n (org-export-get-footnote-number footnote-reference info)))
1708 (cond
1709 ((not (org-export-footnote-first-reference-p footnote-reference info))
1710 (funcall --format-footnote-reference n))
1711 ;; Inline definitions are secondary strings.
1712 ;; Non-inline footnotes definitions are full Org data.
1714 (let* ((raw (org-export-get-footnote-definition footnote-reference
1715 info))
1716 (def (let ((def (org-trim (org-export-data raw info))))
1717 (if (eq (org-element-type raw) 'org-data) def
1718 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1719 "Footnote" def)))))
1720 (funcall --format-footnote-definition n def))))))))
1723 ;;;; Headline
1725 (defun* org-e-odt-format-headline
1726 (todo todo-type priority text tags
1727 &key level section-number headline-label &allow-other-keys)
1728 (concat
1729 ;; Todo.
1730 (when todo
1731 (let ((style (if (member todo org-done-keywords) "OrgDone" "OrgTodo")))
1732 (format "<text:span text:style-name=\"%s\">%s</text:span> "
1733 style todo)))
1734 (when priority
1735 (let* ((style (format "OrgPriority-%s" priority))
1736 (priority (format "[#%c]" priority)))
1737 (format "<text:span text:style-name=\"%s\">%s</text:span> "
1738 style priority)))
1739 ;; Title.
1740 text
1741 ;; Tags.
1742 (when tags
1743 (concat
1744 "<text:tab/>"
1745 (format "<text:span text:style-name=\"%s\">[%s]</text:span>"
1746 "OrgTags" (mapconcat
1747 (lambda (tag)
1748 (format
1749 "<text:span text:style-name=\"%s\">%s</text:span>"
1750 "OrgTag" tag)) tags " : "))))))
1752 (defun org-e-odt-format-headline--wrap (headline translations info
1753 &optional format-function
1754 &rest extra-keys)
1755 "Transcode an HEADLINE element from Org to ODT.
1756 CONTENTS holds the contents of the headline. INFO is a plist
1757 holding contextual information."
1758 (setq translations (or translations (plist-get info :translate-alist)))
1759 (let* ((level (+ (org-export-get-relative-level headline info)))
1760 (headline-number (org-export-get-headline-number headline info))
1761 (section-number (and (org-export-numbered-headline-p headline info)
1762 (mapconcat 'number-to-string
1763 headline-number ".")))
1764 (todo (and (plist-get info :with-todo-keywords)
1765 (let ((todo (org-element-property :todo-keyword headline)))
1766 (and todo (org-export-data-with-translations
1767 todo translations info)))))
1768 (todo-type (and todo (org-element-property :todo-type headline)))
1769 (priority (and (plist-get info :with-priority)
1770 (org-element-property :priority headline)))
1771 (text (org-export-data-with-translations
1772 (org-element-property :title headline) translations info))
1773 (tags (and (plist-get info :with-tags)
1774 (org-export-get-tags headline info)))
1775 (headline-label (concat "sec-" (mapconcat 'number-to-string
1776 headline-number "-")))
1777 (format-function (cond
1778 ((functionp format-function) format-function)
1779 ((functionp org-e-odt-format-headline-function)
1780 (function*
1781 (lambda (todo todo-type priority text tags
1782 &allow-other-keys)
1783 (funcall org-e-odt-format-headline-function
1784 todo todo-type priority text tags))))
1785 (t 'org-e-odt-format-headline))))
1786 (apply format-function
1787 todo todo-type priority text tags
1788 :headline-label headline-label :level level
1789 :section-number section-number extra-keys)))
1791 (defun org-e-odt-headline (headline contents info)
1792 "Transcode an HEADLINE element from Org to ODT.
1793 CONTENTS holds the contents of the headline. INFO is a plist
1794 holding contextual information."
1795 ;; Case 1: This is a footnote section: ignore it.
1796 (unless (org-element-property :footnote-section-p headline)
1797 (let* ((text (org-export-data (org-element-property :title headline) info))
1798 ;; Create the headline text.
1799 (full-text (org-e-odt-format-headline--wrap headline nil info))
1800 ;; Get level relative to current parsed data.
1801 (level (org-export-get-relative-level headline info))
1802 ;; Get canonical label for the headline.
1803 (id (concat "sec-" (mapconcat 'number-to-string
1804 (org-export-get-headline-number
1805 headline info) "-")))
1806 ;; Get user-specified labels for the headline.
1807 (extra-ids (list (org-element-property :custom-id headline)
1808 (org-element-property :id headline)))
1809 ;; Extra targets.
1810 (extra-targets
1811 (mapconcat (lambda (x)
1812 (when x
1813 (let ((x (if (org-uuidgen-p x) (concat "ID-" x) x)))
1814 (org-e-odt--target
1815 "" (org-export-solidify-link-text x)))))
1816 extra-ids ""))
1817 ;; Title.
1818 (anchored-title (org-e-odt--target full-text id)))
1819 (cond
1820 ;; Case 2. This is a deep sub-tree: export it as a list item.
1821 ;; Also export as items headlines for which no section
1822 ;; format has been found.
1823 ((org-export-low-level-p headline info)
1824 ;; Build the real contents of the sub-tree.
1825 (concat
1826 (and (org-export-first-sibling-p headline info)
1827 (format "\n<text:list text:style-name=\"%s\" %s>"
1828 ;; Choose style based on list type.
1829 (if (org-export-numbered-headline-p headline info)
1830 "OrgNumberedList" "OrgBulletedList")
1831 ;; If top-level list, re-start numbering. Otherwise,
1832 ;; continue numbering.
1833 (format "text:continue-numbering=\"%s\""
1834 (let* ((parent (org-export-get-parent-headline
1835 headline)))
1836 (if (and parent
1837 (org-export-low-level-p parent info))
1838 "true" "false")))))
1839 (let ((headline-has-table-p
1840 (let ((section (assq 'section (org-element-contents headline))))
1841 (assq 'table (and section (org-element-contents section))))))
1842 (format "\n<text:list-item>\n%s\n%s"
1843 (concat
1844 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1845 "Text_20_body"
1846 (concat extra-targets anchored-title))
1847 contents)
1848 (if headline-has-table-p
1849 "</text:list-header>"
1850 "</text:list-item>")))
1851 (and (org-export-last-sibling-p headline info)
1852 "</text:list>")))
1853 ;; Case 3. Standard headline. Export it as a section.
1855 (concat
1856 (format
1857 "\n<text:h text:style-name=\"%s\" text:outline-level=\"%s\">%s</text:h>"
1858 (format "Heading_20_%s" level)
1859 level
1860 (concat extra-targets anchored-title))
1861 contents))))))
1864 ;;;; Horizontal Rule
1866 (defun org-e-odt-horizontal-rule (horizontal-rule contents info)
1867 "Transcode an HORIZONTAL-RULE object from Org to ODT.
1868 CONTENTS is nil. INFO is a plist holding contextual information."
1869 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1870 "Horizontal_20_Line" ""))
1873 ;;;; Inline Babel Call
1875 ;; Inline Babel Calls are ignored.
1878 ;;;; Inline Src Block
1880 (defun org-e-odt--find-verb-separator (s)
1881 "Return a character not used in string S.
1882 This is used to choose a separator for constructs like \\verb."
1883 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1884 (loop for c across ll
1885 when (not (string-match (regexp-quote (char-to-string c)) s))
1886 return (char-to-string c))))
1888 (defun org-e-odt-inline-src-block (inline-src-block contents info)
1889 "Transcode an INLINE-SRC-BLOCK element from Org to ODT.
1890 CONTENTS holds the contents of the item. INFO is a plist holding
1891 contextual information."
1892 (let* ((org-lang (org-element-property :language inline-src-block))
1893 (code (org-element-property :value inline-src-block))
1894 (separator (org-e-odt--find-verb-separator code)))
1895 (error "FIXME")))
1898 ;;;; Inlinetask
1900 (defun org-e-odt-inlinetask (inlinetask contents info)
1901 "Transcode an INLINETASK element from Org to ODT.
1902 CONTENTS holds the contents of the block. INFO is a plist
1903 holding contextual information."
1904 (cond
1905 ;; If `org-e-odt-format-inlinetask-function' is provided, call it
1906 ;; with appropriate arguments.
1907 ((functionp org-e-odt-format-inlinetask-function)
1908 (let ((format-function
1909 (function*
1910 (lambda (todo todo-type priority text tags
1911 &key contents &allow-other-keys)
1912 (funcall org-e-odt-format-inlinetask-function
1913 todo todo-type priority text tags contents)))))
1914 (org-e-odt-format-headline--wrap
1915 inlinetask nil info format-function :contents contents)))
1916 ;; Otherwise, use a default template.
1918 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1919 "Text_20_body"
1920 (org-e-odt--textbox
1921 (concat
1922 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1923 "OrgInlineTaskHeading"
1924 (org-e-odt-format-headline--wrap inlinetask nil info))
1925 contents)
1926 nil nil "OrgInlineTaskFrame" " style:rel-width=\"100%\"")))))
1928 ;;;; Italic
1930 (defun org-e-odt-italic (italic contents info)
1931 "Transcode ITALIC from Org to ODT.
1932 CONTENTS is the text with italic markup. INFO is a plist holding
1933 contextual information."
1934 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1935 "Emphasis" contents))
1938 ;;;; Item
1940 (defun org-e-odt-item (item contents info)
1941 "Transcode an ITEM element from Org to ODT.
1942 CONTENTS holds the contents of the item. INFO is a plist holding
1943 contextual information."
1944 (let* ((plain-list (org-export-get-parent item))
1945 (type (org-element-property :type plain-list))
1946 (counter (org-element-property :counter item))
1947 (tag (let ((tag (org-element-property :tag item)))
1948 (and tag
1949 (concat (org-e-odt--checkbox item)
1950 (org-export-data tag info))))))
1951 (case type
1952 ((ordered unordered descriptive-1 descriptive-2)
1953 (format "\n<text:list-item>\n%s\n%s"
1954 contents
1955 (let* ((--element-has-a-table-p
1956 (function
1957 (lambda (element info)
1958 (loop for el in (org-element-contents element)
1959 thereis (eq (org-element-type el) 'table))))))
1960 (cond
1961 ((funcall --element-has-a-table-p item info)
1962 "</text:list-header>")
1963 (t "</text:list-item>")))))
1964 (t (error "Unknown list type: %S" type)))))
1966 ;;;; Keyword
1968 (defun org-e-odt-keyword (keyword contents info)
1969 "Transcode a KEYWORD element from Org to ODT.
1970 CONTENTS is nil. INFO is a plist holding contextual information."
1971 (let ((key (org-element-property :key keyword))
1972 (value (org-element-property :value keyword)))
1973 (cond
1974 ((string= key "ODT") value)
1975 ((string= key "INDEX")
1976 ;; FIXME
1977 (ignore))
1978 ((string= key "TARGET") nil)
1979 ((string= key "toc")
1980 (let ((value (downcase value)))
1981 (cond
1982 ((string-match "\\<headlines\\>" value)
1983 (let ((depth (or (and (string-match "[0-9]+" value)
1984 (string-to-number (match-string 0 value)))
1985 (plist-get info :with-toc))))
1986 (when (wholenump depth) (org-e-odt-toc depth info))))
1987 ((member value '("tables" "figures" "listings"))
1988 ;; FIXME
1989 (ignore))))))))
1992 ;;;; Latex Environment
1995 (eval-after-load 'org-odt
1996 '(ad-deactivate 'org-format-latex-as-mathml))
1998 ;; (defadvice org-format-latex-as-mathml ; FIXME
1999 ;; (after org-e-odt-protect-latex-fragment activate)
2000 ;; "Encode LaTeX fragment as XML.
2001 ;; Do this when translation to MathML fails."
2002 ;; (when (or (not (> (length ad-return-value) 0))
2003 ;; (get-text-property 0 'org-protected ad-return-value))
2004 ;; (setq ad-return-value
2005 ;; (org-propertize (org-e-odt--encode-plain-text (ad-get-arg 0))
2006 ;; 'org-protected t))))
2008 (defun org-e-odt-latex-environment (latex-environment contents info)
2009 "Transcode a LATEX-ENVIRONMENT element from Org to ODT.
2010 CONTENTS is nil. INFO is a plist holding contextual information."
2011 (let* ((latex-frag (org-remove-indentation
2012 (org-element-property :value latex-environment))))
2013 (org-e-odt-do-format-code latex-frag)))
2016 ;;;; Latex Fragment
2018 ;; (when latex-frag ; FIXME
2019 ;; (setq href (org-propertize href :title "LaTeX Fragment"
2020 ;; :description latex-frag)))
2021 ;; handle verbatim
2022 ;; provide descriptions
2024 (defun org-e-odt-latex-fragment (latex-fragment contents info)
2025 "Transcode a LATEX-FRAGMENT object from Org to ODT.
2026 CONTENTS is nil. INFO is a plist holding contextual information."
2027 (let* ((latex-frag (org-element-property :value latex-fragment))
2028 (processing-type (plist-get info :LaTeX-fragments)))
2029 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2030 "OrgCode" (org-e-odt--encode-plain-text latex-frag t))))
2033 ;;;; Line Break
2035 (defun org-e-odt-line-break (line-break contents info)
2036 "Transcode a LINE-BREAK object from Org to ODT.
2037 CONTENTS is nil. INFO is a plist holding contextual information."
2038 "<text:line-break/>\n")
2041 ;;;; Link
2043 ;;;; Links :: Label references
2045 (defun org-e-odt--enumerate (element info &optional predicate n)
2046 (when predicate (assert (funcall predicate element info)))
2047 (let* ((--numbered-parent-headline-at-<=-n
2048 (function
2049 (lambda (element n info)
2050 (loop for x in (org-export-get-genealogy element)
2051 thereis (and (eq (org-element-type x) 'headline)
2052 (<= (org-export-get-relative-level x info) n)
2053 (org-export-numbered-headline-p x info)
2054 x)))))
2055 (--enumerate
2056 (function
2057 (lambda (element scope info &optional predicate)
2058 (let ((counter 0))
2059 (org-element-map
2060 (or scope (plist-get info :parse-tree))
2061 (org-element-type element)
2062 (lambda (el)
2063 (and (or (not predicate) (funcall predicate el info))
2064 (incf counter)
2065 (eq element el)
2066 counter))
2067 info 'first-match)))))
2068 (scope (funcall --numbered-parent-headline-at-<=-n
2069 element (or n org-e-odt-display-outline-level) info))
2070 (ordinal (funcall --enumerate element scope info predicate))
2071 (tag
2072 (concat
2073 ;; Section number.
2074 (and scope
2075 (mapconcat 'number-to-string
2076 (org-export-get-headline-number scope info) "."))
2077 ;; Separator.
2078 (and scope ".")
2079 ;; Ordinal.
2080 (number-to-string ordinal))))
2081 tag))
2083 (defun org-e-odt-format-label (element info op)
2084 (assert (memq (org-element-type element) '(link table src-block paragraph)))
2085 (let* ((caption-from
2086 (case (org-element-type element)
2087 (link (org-export-get-parent-element element))
2088 (t element)))
2089 ;; Get label and caption.
2090 (label (org-element-property :name caption-from))
2091 (caption (org-export-get-caption caption-from))
2092 (short-caption (org-export-get-caption caption-from t))
2093 ;; Transcode captions.
2094 (caption (and caption (org-export-data caption info)))
2095 ;; Currently short caption are sneaked in as object names.
2097 ;; The advantages are:
2099 ;; - Table Of Contents: Currently, there is no support for
2100 ;; building TOC for figures, listings and tables. See
2101 ;; `org-e-odt-keyword'. User instead has to rely on
2102 ;; external application for building such indices. Within
2103 ;; LibreOffice, building an "Illustration Index" or "Index
2104 ;; of Tables" will create a table with long captions (only)
2105 ;; and building a table with "Object names" will create a
2106 ;; table with short captions.
2108 ;; - Easy navigation: In LibreOffice, object names are
2109 ;; offered via the navigation bar. This way one can
2110 ;; quickly locate and jump to object of his choice in the
2111 ;; exported document.
2113 ;; The main disadvantage is that there cannot be any markups
2114 ;; within object names i.e., one cannot embolden, italicize
2115 ;; or underline text within short caption. So suppress
2116 ;; generation of <text:span >...</text:span> and other
2117 ;; markups by overriding the default translators. We
2118 ;; probably shouldn't be suppressing translators for all
2119 ;; elements in `org-element-all-objects', but for now this
2120 ;; will do.
2121 (short-caption
2122 (let ((short-caption (or short-caption caption))
2123 (translations (nconc (mapcar
2124 (lambda (type)
2125 (cons type (lambda (data contents info)
2126 contents)))
2127 org-element-all-objects)
2128 (plist-get info :translate-alist))))
2129 (when short-caption
2130 (org-export-data-with-translations short-caption
2131 translations info)))))
2132 (when (or label caption)
2133 (let* ((default-category
2134 (case (org-element-type element)
2135 (table "__Table__")
2136 (src-block "__Listing__")
2137 ((link paragraph)
2138 (cond
2139 ((org-e-odt--enumerable-latex-image-p element info)
2140 "__DvipngImage__")
2141 ((org-e-odt--enumerable-image-p element info)
2142 "__Figure__")
2143 ((org-e-odt--enumerable-formula-p element info)
2144 "__MathFormula__")
2145 (t (error "Don't know how to format label for link: %S"
2146 element))))
2147 (t (error "Don't know how to format label for element type: %s"
2148 (org-element-type element)))))
2149 seqno)
2150 (assert default-category)
2151 (destructuring-bind (counter label-style category predicate)
2152 (assoc-default default-category org-e-odt-category-map-alist)
2153 ;; Compute sequence number of the element.
2154 (setq seqno (org-e-odt--enumerate element info predicate))
2155 ;; Localize category string.
2156 (setq category (org-export-translate category :utf-8 info))
2157 (case op
2158 ;; Case 1: Handle Label definition.
2159 (definition
2160 ;; Assign an internal label, if user has not provided one
2161 (setq label (or label (format "%s-%s" default-category seqno)))
2162 (setq label (org-export-solidify-link-text label))
2163 (cons
2164 (concat
2165 ;; Sneak in a bookmark. The bookmark is used when the
2166 ;; labeled element is referenced with a link that
2167 ;; provides it's own description.
2168 (format "\n<text:bookmark text:name=\"%s\"/>" label)
2169 ;; Label definition: Typically formatted as below:
2170 ;; CATEGORY SEQ-NO: LONG CAPTION
2171 (format-spec
2172 (cadr (assoc-string label-style org-e-odt-label-styles t))
2173 `((?e . ,category)
2174 (?n . ,(format
2175 "<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">%s</text:sequence>"
2176 label counter counter seqno))
2177 (?c . ,(or caption "")))))
2178 short-caption))
2179 ;; Case 2: Handle Label reference.
2180 (reference
2181 (assert label)
2182 (setq label (org-export-solidify-link-text label))
2183 (let* ((fmt (cddr (assoc-string label-style org-e-odt-label-styles t)))
2184 (fmt1 (car fmt))
2185 (fmt2 (cadr fmt)))
2186 (format "<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:sequence-ref>"
2187 fmt1 label (format-spec fmt2 `((?e . ,category)
2188 (?n . ,seqno))))))
2189 (t (error "Unknown %S on label" op))))))))
2192 ;;;; Links :: Inline Images
2194 (defun org-e-odt--copy-image-file (path)
2195 "Returns the internal name of the file"
2196 (let* ((image-type (file-name-extension path))
2197 (media-type (format "image/%s" image-type))
2198 (target-dir "Images/")
2199 (target-file
2200 (format "%s%04d.%s" target-dir
2201 (incf org-e-odt-embedded-images-count) image-type)))
2202 (message "Embedding %s as %s ..."
2203 (substring-no-properties path) target-file)
2205 (when (= 1 org-e-odt-embedded-images-count)
2206 (make-directory (concat org-e-odt-zip-dir target-dir))
2207 (org-e-odt-create-manifest-file-entry "" target-dir))
2209 (copy-file path (concat org-e-odt-zip-dir target-file) 'overwrite)
2210 (org-e-odt-create-manifest-file-entry media-type target-file)
2211 target-file))
2213 (defun org-e-odt--image-size (file &optional user-width
2214 user-height scale dpi embed-as)
2215 (let* ((--pixels-to-cms
2216 (function (lambda (pixels dpi)
2217 (let ((cms-per-inch 2.54)
2218 (inches (/ pixels dpi)))
2219 (* cms-per-inch inches)))))
2220 (--size-in-cms
2221 (function
2222 (lambda (size-in-pixels dpi)
2223 (and size-in-pixels
2224 (cons (funcall --pixels-to-cms (car size-in-pixels) dpi)
2225 (funcall --pixels-to-cms (cdr size-in-pixels) dpi))))))
2226 (dpi (or dpi org-e-odt-pixels-per-inch))
2227 (anchor-type (or embed-as "paragraph"))
2228 (user-width (and (not scale) user-width))
2229 (user-height (and (not scale) user-height))
2230 (size
2231 (and
2232 (not (and user-height user-width))
2234 ;; Use Imagemagick.
2235 (and (executable-find "identify")
2236 (let ((size-in-pixels
2237 (let ((dim (shell-command-to-string
2238 (format "identify -format \"%%w:%%h\" \"%s\""
2239 file))))
2240 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
2241 (cons (string-to-number (match-string 1 dim))
2242 (string-to-number (match-string 2 dim)))))))
2243 (funcall --size-in-cms size-in-pixels dpi)))
2244 ;; Use Emacs.
2245 (let ((size-in-pixels
2246 (ignore-errors ; Emacs could be in batch mode
2247 (clear-image-cache)
2248 (image-size (create-image file) 'pixels))))
2249 (funcall --size-in-cms size-in-pixels dpi))
2250 ;; Use hard-coded values.
2251 (cdr (assoc-string anchor-type
2252 org-e-odt-default-image-sizes-alist))
2253 ;; Error out.
2254 (error "Cannot determine image size, aborting"))))
2255 (width (car size)) (height (cdr size)))
2256 (cond
2257 (scale
2258 (setq width (* width scale) height (* height scale)))
2259 ((and user-height user-width)
2260 (setq width user-width height user-height))
2261 (user-height
2262 (setq width (* user-height (/ width height)) height user-height))
2263 (user-width
2264 (setq height (* user-width (/ height width)) width user-width))
2265 (t (ignore)))
2266 ;; ensure that an embedded image fits comfortably within a page
2267 (let ((max-width (car org-e-odt-max-image-size))
2268 (max-height (cdr org-e-odt-max-image-size)))
2269 (when (or (> width max-width) (> height max-height))
2270 (let* ((scale1 (/ max-width width))
2271 (scale2 (/ max-height height))
2272 (scale (min scale1 scale2)))
2273 (setq width (* scale width) height (* scale height)))))
2274 (cons width height)))
2276 (defun org-e-odt-link--inline-image (element info)
2277 "Return ODT code for an inline image.
2278 LINK is the link pointing to the inline image. INFO is a plist
2279 used as a communication channel."
2280 (assert (eq (org-element-type element) 'link))
2281 (let* ((src (let* ((type (org-element-property :type element))
2282 (raw-path (org-element-property :path element)))
2283 (cond ((member type '("http" "https"))
2284 (concat type ":" raw-path))
2285 ((file-name-absolute-p raw-path)
2286 (expand-file-name raw-path))
2287 (t raw-path))))
2288 (src-expanded (if (file-name-absolute-p src) src
2289 (expand-file-name src (file-name-directory
2290 (plist-get info :input-file)))))
2291 (href (format
2292 "\n<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>"
2293 (org-e-odt--copy-image-file src-expanded)))
2294 ;; Extract attributes from #+ATTR_ODT line.
2295 (attr-from (case (org-element-type element)
2296 (link (org-export-get-parent-element element))
2297 (t element)))
2298 ;; Convert attributes to a plist.
2299 (attr-plist (org-export-read-attribute :attr_odt attr-from))
2300 ;; Handle `:anchor', `:style' and `:attributes' properties.
2301 (user-frame-anchor
2302 (car (assoc-string (plist-get attr-plist :anchor)
2303 '(("as-char") ("paragraph") ("page")) t)))
2304 (user-frame-style
2305 (and user-frame-anchor (plist-get attr-plist :style)))
2306 (user-frame-attrs
2307 (and user-frame-anchor (plist-get attr-plist :attributes)))
2308 (user-frame-params
2309 (list user-frame-style user-frame-attrs user-frame-anchor))
2310 ;; (embed-as (or embed-as user-frame-anchor "paragraph"))
2311 ;; extrac
2312 ;; handle `:width', `:height' and `:scale' properties.
2313 (size (org-e-odt--image-size
2314 src-expanded (plist-get attr-plist :width)
2315 (plist-get attr-plist :height)
2316 (plist-get attr-plist :scale) nil ;; embed-as
2317 "paragraph" ; FIXME
2319 (width (car size)) (height (cdr size))
2320 (standalone-link-p (org-e-odt--standalone-link-p element info))
2321 (embed-as (if standalone-link-p "paragraph" "as-char"))
2322 (captions (org-e-odt-format-label element info 'definition))
2323 (caption (car captions)) (short-caption (cdr captions))
2324 (entity (concat (and caption "Captioned") embed-as "Image"))
2325 ;; Check if this link was created by LaTeX-to-PNG converter.
2326 (replaces (org-element-property
2327 :replaces (if (not standalone-link-p) element
2328 (org-export-get-parent-element element))))
2329 ;; If yes, note down the type of the element - LaTeX Fragment
2330 ;; or LaTeX environment. It will go in to frame title.
2331 (title (and replaces (capitalize
2332 (symbol-name (org-element-type replaces)))))
2334 ;; If yes, note down it's contents. It will go in to frame
2335 ;; description. This quite useful for debugging.
2336 (desc (and replaces (org-element-property :value replaces))))
2337 (org-e-odt--render-image/formula entity href width height
2338 captions user-frame-params title desc)))
2341 ;;;; Links :: Math formula
2343 (defun org-e-odt-link--inline-formula (element info)
2344 (let* ((src (let* ((type (org-element-property :type element))
2345 (raw-path (org-element-property :path element)))
2346 (cond
2347 ((file-name-absolute-p raw-path)
2348 (expand-file-name raw-path))
2349 (t raw-path))))
2350 (src-expanded (if (file-name-absolute-p src) src
2351 (expand-file-name src (file-name-directory
2352 (plist-get info :input-file)))))
2353 (href
2354 (format
2355 "\n<draw:object %s xlink:href=\"%s\" xlink:type=\"simple\"/>"
2356 " xlink:show=\"embed\" xlink:actuate=\"onLoad\""
2357 (file-name-directory (org-e-odt--copy-formula-file src-expanded))))
2358 (standalone-link-p (org-e-odt--standalone-link-p element info))
2359 (embed-as (if standalone-link-p 'paragraph 'character))
2360 (captions (org-e-odt-format-label element info 'definition))
2361 (caption (car captions)) (short-caption (cdr captions))
2362 ;; Check if this link was created by LaTeX-to-MathML
2363 ;; converter.
2364 (replaces (org-element-property
2365 :replaces (if (not standalone-link-p) element
2366 (org-export-get-parent-element element))))
2367 ;; If yes, note down the type of the element - LaTeX Fragment
2368 ;; or LaTeX environment. It will go in to frame title.
2369 (title (and replaces (capitalize
2370 (symbol-name (org-element-type replaces)))))
2372 ;; If yes, note down it's contents. It will go in to frame
2373 ;; description. This quite useful for debugging.
2374 (desc (and replaces (org-element-property :value replaces)))
2375 width height)
2376 (cond
2377 ((eq embed-as 'character)
2378 (org-e-odt--render-image/formula "InlineFormula" href width height
2379 nil nil title desc))
2381 (let* ((equation (org-e-odt--render-image/formula
2382 "CaptionedDisplayFormula" href width height
2383 captions nil title desc))
2384 (label
2385 (let* ((org-e-odt-category-map-alist
2386 '(("__MathFormula__" "Text" "math-label" "Equation"
2387 org-e-odt--enumerable-formula-p))))
2388 (car (org-e-odt-format-label element info 'definition)))))
2389 (concat equation "<text:tab/>" label))))))
2391 (defun org-e-odt--copy-formula-file (src-file)
2392 "Returns the internal name of the file"
2393 (let* ((target-dir (format "Formula-%04d/"
2394 (incf org-e-odt-embedded-formulas-count)))
2395 (target-file (concat target-dir "content.xml")))
2396 ;; Create a directory for holding formula file. Also enter it in
2397 ;; to manifest.
2398 (make-directory (concat org-e-odt-zip-dir target-dir))
2399 (org-e-odt-create-manifest-file-entry
2400 "application/vnd.oasis.opendocument.formula" target-dir "1.2")
2401 ;; Copy over the formula file from user directory to zip
2402 ;; directory.
2403 (message "Embedding %s as %s ..." src-file target-file)
2404 (let ((case-fold-search nil))
2405 (cond
2406 ;; Case 1: Mathml.
2407 ((string-match "\\.\\(mathml\\|mml\\)\\'" src-file)
2408 (copy-file src-file (concat org-e-odt-zip-dir target-file) 'overwrite))
2409 ;; Case 2: OpenDocument formula.
2410 ((string-match "\\.odf\\'" src-file)
2411 (org-e-odt--zip-extract src-file "content.xml"
2412 (concat org-e-odt-zip-dir target-dir)))
2413 (t (error "%s is not a formula file" src-file))))
2414 ;; Enter the formula file in to manifest.
2415 (org-e-odt-create-manifest-file-entry "text/xml" target-file)
2416 target-file))
2418 ;;;; Targets
2420 (defun org-e-odt--render-image/formula (cfg-key href width height &optional
2421 captions user-frame-params
2422 &rest title-and-desc)
2423 (let* ((frame-cfg-alist
2424 ;; Each element of this alist is of the form (CFG-HANDLE
2425 ;; INNER-FRAME-PARAMS OUTER-FRAME-PARAMS).
2427 ;; CFG-HANDLE is the key to the alist.
2429 ;; INNER-FRAME-PARAMS and OUTER-FRAME-PARAMS specify the
2430 ;; frame params for INNER-FRAME and OUTER-FRAME
2431 ;; respectively. See below.
2433 ;; Configurations that are meant to be applied to
2434 ;; non-captioned image/formula specifies no
2435 ;; OUTER-FRAME-PARAMS.
2437 ;; TERMINOLOGY
2438 ;; ===========
2439 ;; INNER-FRAME :: Frame that directly surrounds an
2440 ;; image/formula.
2442 ;; OUTER-FRAME :: Frame that encloses the INNER-FRAME. This
2443 ;; frame also contains the caption, if any.
2445 ;; FRAME-PARAMS :: List of the form (FRAME-STYLE-NAME
2446 ;; FRAME-ATTRIBUTES FRAME-ANCHOR). Note
2447 ;; that these are the last three arguments
2448 ;; to `org-e-odt--frame'.
2450 ;; Note that an un-captioned image/formula requires just an
2451 ;; INNER-FRAME, while a captioned image/formula requires
2452 ;; both an INNER and an OUTER-FRAME.
2453 '(("As-CharImage" ("OrgInlineImage" nil "as-char"))
2454 ("ParagraphImage" ("OrgDisplayImage" nil "paragraph"))
2455 ("PageImage" ("OrgPageImage" nil "page"))
2456 ("CaptionedAs-CharImage"
2457 ("OrgCaptionedImage"
2458 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
2459 ("OrgInlineImage" nil "as-char"))
2460 ("CaptionedParagraphImage"
2461 ("OrgCaptionedImage"
2462 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
2463 ("OrgImageCaptionFrame" nil "paragraph"))
2464 ("CaptionedPageImage"
2465 ("OrgCaptionedImage"
2466 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
2467 ("OrgPageImageCaptionFrame" nil "page"))
2468 ("InlineFormula" ("OrgInlineFormula" nil "as-char"))
2469 ("DisplayFormula" ("OrgDisplayFormula" nil "as-char"))
2470 ("CaptionedDisplayFormula"
2471 ("OrgCaptionedFormula" nil "paragraph")
2472 ("OrgFormulaCaptionFrame" nil "paragraph"))))
2473 (caption (car captions)) (short-caption (cdr captions))
2474 ;; Retrieve inner and outer frame params, from configuration.
2475 (frame-cfg (assoc-string cfg-key frame-cfg-alist t))
2476 (inner (nth 1 frame-cfg))
2477 (outer (nth 2 frame-cfg))
2478 ;; User-specified frame params (from #+ATTR_ODT spec)
2479 (user user-frame-params)
2480 (--merge-frame-params (function
2481 (lambda (default user)
2482 "Merge default and user frame params."
2483 (if (not user) default
2484 (assert (= (length default) 3))
2485 (assert (= (length user) 3))
2486 (loop for u in user
2487 for d in default
2488 collect (or u d)))))))
2489 (cond
2490 ;; Case 1: Image/Formula has no caption.
2491 ;; There is only one frame, one that surrounds the image
2492 ;; or formula.
2493 ((not caption)
2494 ;; Merge user frame params with that from configuration.
2495 (setq inner (funcall --merge-frame-params inner user))
2496 (apply 'org-e-odt--frame href width height
2497 (append inner title-and-desc)))
2498 ;; Case 2: Image/Formula is captioned or labeled.
2499 ;; There are two frames: The inner one surrounds the
2500 ;; image or formula. The outer one contains the
2501 ;; caption/sequence number.
2503 ;; Merge user frame params with outer frame params.
2504 (setq outer (funcall --merge-frame-params outer user))
2505 ;; Short caption, if specified, goes as part of inner frame.
2506 (setq inner (let ((frame-params (copy-sequence inner)))
2507 (setcar (cdr frame-params)
2508 (concat
2509 (cadr frame-params)
2510 (when short-caption
2511 (format " draw:name=\"%s\" " short-caption))))
2512 frame-params))
2513 (apply 'org-e-odt--textbox
2514 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2515 "Illustration"
2516 (concat
2517 (apply 'org-e-odt--frame href width height
2518 (append inner title-and-desc))
2519 caption))
2520 width height outer)))))
2522 (defun org-e-odt--enumerable-p (element info)
2523 ;; Element should have a caption or label.
2524 (or (org-element-property :caption element)
2525 (org-element-property :name element)))
2527 (defun org-e-odt--enumerable-image-p (element info)
2528 (org-e-odt--standalone-link-p
2529 element info
2530 ;; Paragraph should have a caption or label. It SHOULD NOT be a
2531 ;; replacement element. (i.e., It SHOULD NOT be a result of LaTeX
2532 ;; processing.)
2533 (lambda (p)
2534 (and (not (org-element-property :replaces p))
2535 (or (org-element-property :caption p)
2536 (org-element-property :name p))))
2537 ;; Link should point to an image file.
2538 (lambda (l)
2539 (assert (eq (org-element-type l) 'link))
2540 (org-export-inline-image-p l org-e-odt-inline-image-rules))))
2542 (defun org-e-odt--enumerable-latex-image-p (element info)
2543 (org-e-odt--standalone-link-p
2544 element info
2545 ;; Paragraph should have a caption or label. It SHOULD also be a
2546 ;; replacement element. (i.e., It SHOULD be a result of LaTeX
2547 ;; processing.)
2548 (lambda (p)
2549 (and (org-element-property :replaces p)
2550 (or (org-element-property :caption p)
2551 (org-element-property :name p))))
2552 ;; Link should point to an image file.
2553 (lambda (l)
2554 (assert (eq (org-element-type l) 'link))
2555 (org-export-inline-image-p l org-e-odt-inline-image-rules))))
2557 (defun org-e-odt--enumerable-formula-p (element info)
2558 (org-e-odt--standalone-link-p
2559 element info
2560 ;; Paragraph should have a caption or label.
2561 (lambda (p)
2562 (or (org-element-property :caption p)
2563 (org-element-property :name p)))
2564 ;; Link should point to a MathML or ODF file.
2565 (lambda (l)
2566 (assert (eq (org-element-type l) 'link))
2567 (org-export-inline-image-p l org-e-odt-inline-formula-rules))))
2569 (defun org-e-odt--standalone-link-p (element info &optional
2570 paragraph-predicate
2571 link-predicate)
2572 "Test if ELEMENT is a standalone link for the purpose ODT export.
2573 INFO is a plist holding contextual information.
2575 Return non-nil, if ELEMENT is of type paragraph satisfying
2576 PARAGRAPH-PREDICATE and it's sole content, save for whitespaces,
2577 is a link that satisfies LINK-PREDICATE.
2579 Return non-nil, if ELEMENT is of type link satisfying
2580 LINK-PREDICATE and it's containing paragraph satisfies
2581 PARAGRAPH-PREDICATE inaddtion to having no other content save for
2582 leading and trailing whitespaces.
2584 Return nil, otherwise."
2585 (let ((p (case (org-element-type element)
2586 (paragraph element)
2587 (link (and (or (not link-predicate)
2588 (funcall link-predicate element))
2589 (org-export-get-parent element)))
2590 (t nil))))
2591 (when (and p (eq (org-element-type p) 'paragraph))
2592 (when (or (not paragraph-predicate)
2593 (funcall paragraph-predicate p))
2594 (let ((contents (org-element-contents p)))
2595 (loop for x in contents
2596 with inline-image-count = 0
2597 always (case (org-element-type x)
2598 (plain-text
2599 (not (org-string-nw-p x)))
2600 (link
2601 (and (or (not link-predicate)
2602 (funcall link-predicate x))
2603 (= (incf inline-image-count) 1)))
2604 (t nil))))))))
2606 (defun org-e-odt-link--infer-description (destination info)
2607 ;; DESTINATION is a HEADLINE, a "<<target>>" or an element (like
2608 ;; paragraph, verse-block etc) to which a "#+NAME: label" can be
2609 ;; attached. Note that labels that are attached to captioned
2610 ;; entities - inline images, math formulae and tables - get resolved
2611 ;; as part of `org-e-odt-format-label' and `org-e-odt--enumerate'.
2613 ;; Create a cross-reference to DESTINATION but make best-efforts to
2614 ;; create a *meaningful* description. Check item numbers, section
2615 ;; number and section title in that order.
2617 ;; NOTE: Counterpart of `org-export-get-ordinal'.
2618 ;; FIXME: Handle footnote-definition footnote-reference?
2619 (let* ((genealogy (org-export-get-genealogy destination))
2620 (data (reverse genealogy))
2621 (label (case (org-element-type destination)
2622 (headline
2623 (format "sec-%s" (mapconcat 'number-to-string
2624 (org-export-get-headline-number
2625 destination info) "-")))
2626 (target
2627 (org-element-property :value destination))
2628 (t (error "FIXME: Resolve %S" destination)))))
2630 (let* ( ;; Locate top-level list.
2631 (top-level-list
2632 (loop for x on data
2633 when (eq (org-element-type (car x)) 'plain-list)
2634 return x))
2635 ;; Get list item nos.
2636 (item-numbers
2637 (loop for (plain-list item . rest) on top-level-list by #'cddr
2638 until (not (eq (org-element-type plain-list) 'plain-list))
2639 collect (when (eq (org-element-property :type
2640 plain-list)
2641 'ordered)
2642 (1+ (length (org-export-get-previous-element
2643 item info t))))))
2644 ;; Locate top-most listified headline.
2645 (listified-headlines
2646 (loop for x on data
2647 when (and (eq (org-element-type (car x)) 'headline)
2648 (org-export-low-level-p (car x) info))
2649 return x))
2650 ;; Get listified headline numbers.
2651 (listified-headline-nos
2652 (loop for el in listified-headlines
2653 when (eq (org-element-type el) 'headline)
2654 collect (when (org-export-numbered-headline-p el info)
2655 (1+ (length (org-export-get-previous-element
2656 el info t)))))))
2657 ;; Combine item numbers from both the listified headlines and
2658 ;; regular list items.
2660 ;; Case 1: Check if all the parents of list item are numbered.
2661 ;; If yes, link to the item proper.
2662 (let ((item-numbers (append listified-headline-nos item-numbers)))
2663 (when (and item-numbers (not (memq nil item-numbers)))
2664 (format "<text:bookmark-ref text:reference-format=\"number-all-superior\" text:ref-name=\"%s\">%s</text:bookmark-ref>"
2665 (org-export-solidify-link-text label)
2666 (mapconcat (lambda (n) (if (not n) " "
2667 (concat (number-to-string n) ".")))
2668 item-numbers "")))))
2669 ;; Case 2: Locate a regular and numbered headline in the
2670 ;; hierarchy. Display it's section number.
2671 (let ((headline (loop for el in (cons destination genealogy)
2672 when (and (eq (org-element-type el) 'headline)
2673 (not (org-export-low-level-p el info))
2674 (org-export-numbered-headline-p el info))
2675 return el)))
2676 ;; We found one.
2677 (when headline
2678 (format "<text:bookmark-ref text:reference-format=\"chapter\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
2679 (org-export-solidify-link-text label)
2680 (mapconcat 'number-to-string (org-export-get-headline-number
2681 headline info) "."))))
2682 ;; Case 4: Locate a regular headline in the hierarchy. Display
2683 ;; it's title.
2684 (let ((headline (loop for el in (cons destination genealogy)
2685 when (and (eq (org-element-type el) 'headline)
2686 (not (org-export-low-level-p el info)))
2687 return el)))
2688 ;; We found one.
2689 (when headline
2690 (format "<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
2691 (org-export-solidify-link-text label)
2692 (let ((title (org-element-property :title headline)))
2693 (org-export-data title info)))))
2694 (error "FIXME?"))))
2696 (defun org-e-odt-link (link desc info)
2697 "Transcode a LINK object from Org to ODT.
2699 DESC is the description part of the link, or the empty string.
2700 INFO is a plist holding contextual information. See
2701 `org-export-data'."
2702 (let* ((type (org-element-property :type link))
2703 (raw-path (org-element-property :path link))
2704 ;; Ensure DESC really exists, or set it to nil.
2705 (desc (and (not (string= desc "")) desc))
2706 (imagep (org-export-inline-image-p
2707 link org-e-odt-inline-image-rules))
2708 (path (cond
2709 ((member type '("http" "https" "ftp" "mailto"))
2710 (concat type ":" raw-path))
2711 ((string= type "file")
2712 (if (file-name-absolute-p raw-path)
2713 (concat "file://" (expand-file-name raw-path))
2714 (concat "file://" raw-path)))
2715 (t raw-path)))
2716 protocol)
2717 (cond
2718 ;; Image file.
2719 ((and (not desc) (org-export-inline-image-p
2720 link org-e-odt-inline-image-rules))
2721 (org-e-odt-link--inline-image link info))
2722 ;; Formula file.
2723 ((and (not desc) (org-export-inline-image-p
2724 link org-e-odt-inline-formula-rules))
2725 (org-e-odt-link--inline-formula link info))
2726 ;; Radio target: Transcode target's contents and use them as
2727 ;; link's description.
2728 ((string= type "radio")
2729 (let ((destination (org-export-resolve-radio-link link info)))
2730 (when destination
2731 (let ((desc (org-export-data (org-element-contents destination) info))
2732 (href (org-export-solidify-link-text path)))
2733 (format
2734 "<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
2735 href desc)))))
2736 ;; Links pointing to an headline: Find destination and build
2737 ;; appropriate referencing command.
2738 ((member type '("custom-id" "fuzzy" "id"))
2739 (let* ((destination (if (string= type "fuzzy")
2740 (org-export-resolve-fuzzy-link link info)
2741 (org-export-resolve-id-link link info))))
2743 ;; Case 1: Fuzzy link points nowhere.
2744 (when (null (org-element-type destination))
2745 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2746 "Emphasis" (or desc (org-export-data
2747 (org-element-property
2748 :raw-link link) info))))
2749 ;; Case 2: Fuzzy link points to an invisible target. Strip it.
2750 (when (eq (org-element-type destination) 'keyword) "")
2751 ;; Case 3: LINK points to an headline.
2752 (when (eq (org-element-type destination) 'headline)
2753 ;; Case 3.1: LINK has a custom description that is
2754 ;; different from headline's title. Create a hyperlink.
2755 (when (and desc
2756 (let ((link-desc (org-element-contents link)))
2757 (not (string= (org-element-interpret-data link-desc)
2758 (org-element-property :raw-value
2759 destination)))))
2760 (let* ((headline-no (org-export-get-headline-number
2761 destination info))
2762 (label (format "sec-%s" (mapconcat 'number-to-string
2763 headline-no "-"))))
2764 (format "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
2765 label desc))))
2766 ;; Case 4: LINK points to an Inline image, Math formula or a Table.
2767 (let ((label-reference (ignore-errors (org-e-odt-format-label
2768 destination info 'reference))))
2769 (when label-reference
2770 (cond
2771 ;; Case 4.1: LINK has no description. Create a
2772 ;; cross-reference showing entity's sequence number.
2773 ((not desc) label-reference)
2774 ;; Case 4.2: LINK has description. Insert a hyperlink
2775 ;; with user-provided description.
2776 (t (let* ((caption-from (case (org-element-type destination)
2777 (link (org-export-get-parent-element
2778 destination))
2779 (t destination)))
2780 ;; Get label and caption.
2781 (label (org-element-property :name caption-from)))
2782 (format "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
2783 (org-export-solidify-link-text label) desc))))))
2784 ;; Case 5: Fuzzy link points to a TARGET.
2785 (when (eq (org-element-type destination) 'target)
2786 ;; Case 5.1: LINK has description. Create a hyperlink.
2787 (when desc
2788 (let ((label (org-element-property :value destination)))
2789 (format "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
2790 (org-export-solidify-link-text label) desc))))
2791 ;; LINK has no description. It points to either a HEADLINE, a
2792 ;; TARGET or an ELEMENT with a #+NAME: LABEL attached to it.
2793 ;; LINK to DESTINATION, but make a best effort to provide a
2794 ;; *meaningful* description.
2795 (org-e-odt-link--infer-description destination info))))
2796 ;; Coderef: replace link with the reference name or the
2797 ;; equivalent line number.
2798 ((string= type "coderef")
2799 (let* ((line-no (format "%d" (org-export-resolve-coderef path info)))
2800 (href (concat "coderef-" path)))
2801 (format
2802 (org-export-get-coderef-format path desc)
2803 (format
2804 "<text:bookmark-ref text:reference-format=\"number\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
2805 href line-no))))
2806 ;; Link type is handled by a special function.
2807 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
2808 (funcall protocol (org-link-unescape path) desc 'odt))
2809 ;; External link with a description part.
2810 ((and path desc)
2811 (let ((link-contents (org-element-contents link)))
2812 ;; Check if description is a link to an inline image.
2813 (if (and (not (cdr link-contents))
2814 (let ((desc-element (car link-contents)))
2815 (and (eq (org-element-type desc-element) 'link)
2816 (org-export-inline-image-p
2817 desc-element org-e-odt-inline-image-rules))))
2818 ;; Format link as a clickable image.
2819 (format "\n<draw:a xlink:type=\"simple\" xlink:href=\"%s\">\n%s\n</draw:a>"
2820 path desc)
2821 ;; Otherwise, format it as a regular link.
2822 (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
2823 path desc))))
2824 ;; External link without a description part.
2825 (path
2826 (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
2827 path path))
2828 ;; No path, only description. Try to do something useful.
2829 (t (format "<text:span text:style-name=\"%s\">%s</text:span>"
2830 "Emphasis" desc)))))
2833 ;;;; Paragraph
2835 (defun org-e-odt-paragraph (paragraph contents info)
2836 "Transcode a PARAGRAPH element from Org to ODT.
2837 CONTENTS is the contents of the paragraph, as a string. INFO is
2838 the plist used as a communication channel."
2839 (let* ((parent (org-export-get-parent paragraph))
2840 (parent-type (org-element-type parent))
2841 (style (case parent-type
2842 (quote-block "Quotations")
2843 (center-block "OrgCenter")
2844 (footnote-definition "Footnote")
2845 (t (or (org-element-property :style paragraph)
2846 "Text_20_body")))))
2847 ;; If this paragraph is a leading paragraph in an item and the
2848 ;; item has a checkbox, splice the checkbox and paragraph contents
2849 ;; together.
2850 (when (and (eq (org-element-type parent) 'item)
2851 (eq paragraph (car (org-element-contents parent))))
2852 (setq contents (concat (org-e-odt--checkbox parent) contents)))
2853 (assert style)
2854 (format "\n<text:p text:style-name=\"%s\">%s</text:p>" style contents)))
2857 ;;;; Plain List
2859 (defun org-e-odt-plain-list (plain-list contents info)
2860 "Transcode a PLAIN-LIST element from Org to ODT.
2861 CONTENTS is the contents of the list. INFO is a plist holding
2862 contextual information."
2863 (format "\n<text:list text:style-name=\"%s\" %s>\n%s</text:list>"
2864 ;; Choose style based on list type.
2865 (case (org-element-property :type plain-list)
2866 (ordered "OrgNumberedList")
2867 (unordered "OrgBulletedList")
2868 (descriptive-1 "OrgDescriptionList")
2869 (descriptive-2 "OrgDescriptionList"))
2870 ;; If top-level list, re-start numbering. Otherwise,
2871 ;; continue numbering.
2872 (format "text:continue-numbering=\"%s\""
2873 (let* ((parent (org-export-get-parent plain-list)))
2874 (if (and parent (eq (org-element-type parent) 'item))
2875 "true" "false")))
2876 contents))
2878 ;;;; Plain Text
2880 (defun org-e-odt--encode-tabs-and-spaces (line)
2881 (replace-regexp-in-string
2882 "\\([\t]\\|\\([ ]+\\)\\)"
2883 (lambda (s)
2884 (cond
2885 ((string= s "\t") "<text:tab/>")
2886 (t (let ((n (length s)))
2887 (cond
2888 ((= n 1) " ")
2889 ((> n 1) (concat " " (format "<text:s text:c=\"%d\"/>" (1- n))))
2890 (t ""))))))
2891 line))
2893 (defun org-e-odt--encode-plain-text (text &optional no-whitespace-filling)
2894 (mapc
2895 (lambda (pair)
2896 (setq text (replace-regexp-in-string (car pair) (cdr pair) text t t)))
2897 '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
2898 (if no-whitespace-filling text
2899 (org-e-odt--encode-tabs-and-spaces text)))
2901 (defun org-e-odt-plain-text (text info)
2902 "Transcode a TEXT string from Org to ODT.
2903 TEXT is the string to transcode. INFO is a plist holding
2904 contextual information."
2905 (let ((output text))
2906 ;; Protect &, < and >.
2907 (setq output (org-e-odt--encode-plain-text output t))
2908 ;; Handle smart quotes. Be sure to provide original string since
2909 ;; OUTPUT may have been modified.
2910 (setq output (org-export-activate-smart-quotes output :utf-8 info text))
2911 ;; Convert special strings.
2912 (when (plist-get info :with-special-strings)
2913 (mapc
2914 (lambda (pair)
2915 (setq output
2916 (replace-regexp-in-string (car pair) (cdr pair) output t nil)))
2917 org-e-odt-special-string-regexps))
2918 ;; Handle break preservation if required.
2919 (when (plist-get info :preserve-breaks)
2920 (setq output (replace-regexp-in-string
2921 "\\(\\\\\\\\\\)?[ \t]*\n" "<text:line-break/>\n" output t)))
2922 ;; Return value.
2923 output))
2926 ;;;; Planning
2928 (defun org-e-odt-planning (planning contents info)
2929 "Transcode a PLANNING element from Org to ODT.
2930 CONTENTS is nil. INFO is a plist used as a communication
2931 channel."
2932 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2933 "OrgPlanning"
2934 (concat
2935 (let ((closed (org-element-property :closed planning)))
2936 (when closed
2937 (concat
2938 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2939 "OrgClosedKeyword" org-closed-string)
2940 (org-e-odt-timestamp closed contents info))))
2941 (let ((deadline (org-element-property :deadline planning)))
2942 (when deadline
2943 (concat
2944 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2945 "OrgDeadlineKeyword" org-deadline-string)
2946 (org-e-odt-timestamp deadline contents info))))
2947 (let ((scheduled (org-element-property :scheduled planning)))
2948 (when scheduled
2949 (concat
2950 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2951 "OrgScheduledKeyword" org-deadline-string)
2952 (org-e-odt-timestamp scheduled contents info)))))))
2955 ;;;; Property Drawer
2957 (defun org-e-odt-property-drawer (property-drawer contents info)
2958 "Transcode a PROPERTY-DRAWER element from Org to ODT.
2959 CONTENTS is nil. INFO is a plist holding contextual
2960 information."
2961 ;; The property drawer isn't exported but we want separating blank
2962 ;; lines nonetheless.
2966 ;;;; Quote Block
2968 (defun org-e-odt-quote-block (quote-block contents info)
2969 "Transcode a QUOTE-BLOCK element from Org to ODT.
2970 CONTENTS holds the contents of the block. INFO is a plist
2971 holding contextual information."
2972 contents)
2975 ;;;; Quote Section
2977 (defun org-e-odt-quote-section (quote-section contents info)
2978 "Transcode a QUOTE-SECTION element from Org to ODT.
2979 CONTENTS is nil. INFO is a plist holding contextual information."
2980 (let ((value (org-remove-indentation
2981 (org-element-property :value quote-section))))
2982 (when value (org-e-odt-do-format-code value))))
2985 ;;;; Section
2987 (defun org-e-odt-format-section (text style &optional name)
2988 (let ((default-name (car (org-e-odt-add-automatic-style "Section"))))
2989 (format "\n<text:section text:style-name=\"%s\" %s>\n%s\n</text:section>"
2990 style
2991 (format "text:name=\"%s\"" (or name default-name))
2992 text)))
2995 (defun org-e-odt-section (section contents info) ; FIXME
2996 "Transcode a SECTION element from Org to ODT.
2997 CONTENTS holds the contents of the section. INFO is a plist
2998 holding contextual information."
2999 contents)
3001 ;;;; Radio Target
3003 (defun org-e-odt-radio-target (radio-target text info)
3004 "Transcode a RADIO-TARGET object from Org to ODT.
3005 TEXT is the text of the target. INFO is a plist holding
3006 contextual information."
3007 (org-e-odt--target
3008 text (org-export-solidify-link-text
3009 (org-element-property :value radio-target))))
3012 ;;;; Special Block
3014 (defun org-e-odt-special-block (special-block contents info)
3015 "Transcode a SPECIAL-BLOCK element from Org to ODT.
3016 CONTENTS holds the contents of the block. INFO is a plist
3017 holding contextual information."
3018 (let ((type (downcase (org-element-property :type special-block)))
3019 (attributes (org-export-read-attribute :attr_odt special-block)))
3020 (cond
3021 ;; Annotation.
3022 ((string= type "annotation")
3023 (let* ((author (or (plist-get attributes :author)
3024 (let ((author (plist-get info :author)))
3025 (and author (org-export-data author info)))))
3026 (date (or (plist-get attributes :date)
3027 ;; FIXME: Is `car' right thing to do below?
3028 (car (plist-get info :date)))))
3030 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3031 "Text_20_body"
3032 (format "<office:annotation>\n%s\n</office:annotation>"
3033 (concat
3034 (and author
3035 (format "<dc:creator>%s</dc:creator>" author))
3036 (and date
3037 (format "<dc:date>%s</dc:date>"
3038 (org-e-odt--format-timestamp date nil 'iso-date)))
3039 contents)))))
3040 ;; Textbox.
3041 ((string= type "textbox")
3042 (let ((width (plist-get attributes :width))
3043 (height (plist-get attributes :height))
3044 (style (plist-get attributes :style))
3045 (extra (plist-get attributes :extra))
3046 (anchor (plist-get attributes :anchor)))
3047 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3048 "Text_20_body" (org-e-odt--textbox contents width height
3049 style extra anchor))))
3050 (t contents))))
3053 ;;;; Src Block
3055 (defun org-e-odt-hfy-face-to-css (fn)
3056 "Create custom style for face FN.
3057 When FN is the default face, use it's foreground and background
3058 properties to create \"OrgSrcBlock\" paragraph style. Otherwise
3059 use it's color attribute to create a character style whose name
3060 is obtained from FN. Currently all attributes of FN other than
3061 color are ignored.
3063 The style name for a face FN is derived using the following
3064 operations on the face name in that order - de-dash, CamelCase
3065 and prefix with \"OrgSrc\". For example,
3066 `font-lock-function-name-face' is associated with
3067 \"OrgSrcFontLockFunctionNameFace\"."
3068 (let* ((css-list (hfy-face-to-style fn))
3069 (style-name ((lambda (fn)
3070 (concat "OrgSrc"
3071 (mapconcat
3072 'capitalize (split-string
3073 (hfy-face-or-def-to-name fn) "-")
3074 ""))) fn))
3075 (color-val (cdr (assoc "color" css-list)))
3076 (background-color-val (cdr (assoc "background" css-list)))
3077 (style (and org-e-odt-create-custom-styles-for-srcblocks
3078 (cond
3079 ((eq fn 'default)
3080 (format org-e-odt-src-block-paragraph-format
3081 background-color-val color-val))
3083 (format
3085 <style:style style:name=\"%s\" style:family=\"text\">
3086 <style:text-properties fo:color=\"%s\"/>
3087 </style:style>" style-name color-val))))))
3088 (cons style-name style)))
3090 (defun org-e-odt-htmlfontify-string (line)
3091 (let* ((hfy-html-quote-regex "\\([<\"&> ]\\)")
3092 (hfy-html-quote-map '(("\"" "&quot;")
3093 ("<" "&lt;")
3094 ("&" "&amp;")
3095 (">" "&gt;")
3096 (" " "<text:s/>")
3097 (" " "<text:tab/>")))
3098 (hfy-face-to-css 'org-e-odt-hfy-face-to-css)
3099 (hfy-optimisations-1 (copy-sequence hfy-optimisations))
3100 (hfy-optimisations (add-to-list 'hfy-optimisations-1
3101 'body-text-only))
3102 (hfy-begin-span-handler
3103 (lambda (style text-block text-id text-begins-block-p)
3104 (insert (format "<text:span text:style-name=\"%s\">" style))))
3105 (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
3106 (org-no-warnings (htmlfontify-string line))))
3108 (defun org-e-odt-do-format-code
3109 (code &optional lang refs retain-labels num-start)
3110 (let* ((lang (or (assoc-default lang org-src-lang-modes) lang))
3111 (lang-mode (and lang (intern (format "%s-mode" lang))))
3112 (code-lines (org-split-string code "\n"))
3113 (code-length (length code-lines))
3114 (use-htmlfontify-p (and (functionp lang-mode)
3115 org-e-odt-fontify-srcblocks
3116 (require 'htmlfontify nil t)
3117 (fboundp 'htmlfontify-string)))
3118 (code (if (not use-htmlfontify-p) code
3119 (with-temp-buffer
3120 (insert code)
3121 (funcall lang-mode)
3122 (font-lock-fontify-buffer)
3123 (buffer-string))))
3124 (fontifier (if use-htmlfontify-p 'org-e-odt-htmlfontify-string
3125 'org-e-odt--encode-plain-text))
3126 (par-style (if use-htmlfontify-p "OrgSrcBlock"
3127 "OrgFixedWidthBlock"))
3128 (i 0))
3129 (assert (= code-length (length (org-split-string code "\n"))))
3130 (setq code
3131 (org-export-format-code
3132 code
3133 (lambda (loc line-num ref)
3134 (setq par-style
3135 (concat par-style (and (= (incf i) code-length) "LastLine")))
3137 (setq loc (concat loc (and ref retain-labels (format " (%s)" ref))))
3138 (setq loc (funcall fontifier loc))
3139 (when ref
3140 (setq loc (org-e-odt--target loc (concat "coderef-" ref))))
3141 (assert par-style)
3142 (setq loc (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3143 par-style loc))
3144 (if (not line-num) loc
3145 (format "\n<text:list-item>%s\n</text:list-item>" loc)))
3146 num-start refs))
3147 (cond
3148 ((not num-start) code)
3149 ((= num-start 0)
3150 (format
3151 "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
3152 " text:continue-numbering=\"false\"" code))
3154 (format
3155 "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
3156 " text:continue-numbering=\"true\"" code)))))
3158 (defun org-e-odt-format-code (element info)
3159 (let* ((lang (org-element-property :language element))
3160 ;; Extract code and references.
3161 (code-info (org-export-unravel-code element))
3162 (code (car code-info))
3163 (refs (cdr code-info))
3164 ;; Does the src block contain labels?
3165 (retain-labels (org-element-property :retain-labels element))
3166 ;; Does it have line numbers?
3167 (num-start (case (org-element-property :number-lines element)
3168 (continued (org-export-get-loc element info))
3169 (new 0))))
3170 (org-e-odt-do-format-code code lang refs retain-labels num-start)))
3172 (defun org-e-odt-src-block (src-block contents info)
3173 "Transcode a SRC-BLOCK element from Org to ODT.
3174 CONTENTS holds the contents of the item. INFO is a plist holding
3175 contextual information."
3176 (let* ((lang (org-element-property :language src-block))
3177 (attributes (org-export-read-attribute :attr_odt src-block))
3178 (captions (org-e-odt-format-label src-block info 'definition))
3179 (caption (car captions)) (short-caption (cdr captions)))
3180 (concat
3181 (and caption
3182 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3183 "Listing" caption))
3184 (let ((--src-block (org-e-odt-format-code src-block info)))
3185 (if (not (plist-get attributes :textbox)) --src-block
3186 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3187 "Text_20_body"
3188 (org-e-odt--textbox --src-block nil nil nil)))))))
3191 ;;;; Statistics Cookie
3193 (defun org-e-odt-statistics-cookie (statistics-cookie contents info)
3194 "Transcode a STATISTICS-COOKIE object from Org to ODT.
3195 CONTENTS is nil. INFO is a plist holding contextual information."
3196 (let ((cookie-value (org-element-property :value statistics-cookie)))
3197 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3198 "OrgCode" cookie-value)))
3201 ;;;; Strike-Through
3203 (defun org-e-odt-strike-through (strike-through contents info)
3204 "Transcode STRIKE-THROUGH from Org to ODT.
3205 CONTENTS is the text with strike-through markup. INFO is a plist
3206 holding contextual information."
3207 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3208 "Strikethrough" contents))
3211 ;;;; Subscript
3213 (defun org-e-odt-subscript (subscript contents info)
3214 "Transcode a SUBSCRIPT object from Org to ODT.
3215 CONTENTS is the contents of the object. INFO is a plist holding
3216 contextual information."
3217 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3218 "OrgSubscript" contents))
3221 ;;;; Superscript
3223 (defun org-e-odt-superscript (superscript contents info)
3224 "Transcode a SUPERSCRIPT object from Org to ODT.
3225 CONTENTS is the contents of the object. INFO is a plist holding
3226 contextual information."
3227 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3228 "OrgSuperscript" contents))
3231 ;;;; Table Cell
3233 (defun org-e-odt-table-style-spec (element info)
3234 (let* ((table (org-export-get-parent-table element))
3235 (table-attributes (org-export-read-attribute :attr_odt table))
3236 (table-style (plist-get table-attributes :style)))
3237 (assoc table-style org-e-odt-table-styles)))
3239 (defun org-e-odt-get-table-cell-styles (table-cell info)
3240 "Retrieve styles applicable to a table cell.
3241 R and C are (zero-based) row and column numbers of the table
3242 cell. STYLE-SPEC is an entry in `org-e-odt-table-styles'
3243 applicable to the current table. It is `nil' if the table is not
3244 associated with any style attributes.
3246 Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
3248 When STYLE-SPEC is nil, style the table cell the conventional way
3249 - choose cell borders based on row and column groupings and
3250 choose paragraph alignment based on `org-col-cookies' text
3251 property. See also
3252 `org-e-odt-get-paragraph-style-cookie-for-table-cell'.
3254 When STYLE-SPEC is non-nil, ignore the above cookie and return
3255 styles congruent with the ODF-1.2 specification."
3256 (let* ((table-cell-address (org-export-table-cell-address table-cell info))
3257 (r (car table-cell-address)) (c (cdr table-cell-address))
3258 (style-spec (org-e-odt-table-style-spec table-cell info))
3259 (table-dimensions (org-export-table-dimensions
3260 (org-export-get-parent-table table-cell)
3261 info)))
3262 (when style-spec
3263 ;; LibreOffice - particularly the Writer - honors neither table
3264 ;; templates nor custom table-cell styles. Inorder to retain
3265 ;; inter-operability with LibreOffice, only automatic styles are
3266 ;; used for styling of table-cells. The current implementation is
3267 ;; congruent with ODF-1.2 specification and hence is
3268 ;; future-compatible.
3270 ;; Additional Note: LibreOffice's AutoFormat facility for tables -
3271 ;; which recognizes as many as 16 different cell types - is much
3272 ;; richer. Unfortunately it is NOT amenable to easy configuration
3273 ;; by hand.
3274 (let* ((template-name (nth 1 style-spec))
3275 (cell-style-selectors (nth 2 style-spec))
3276 (cell-type
3277 (cond
3278 ((and (cdr (assoc 'use-first-column-styles cell-style-selectors))
3279 (= c 0)) "FirstColumn")
3280 ((and (cdr (assoc 'use-last-column-styles cell-style-selectors))
3281 (= (1+ c) (cdr table-dimensions)))
3282 "LastColumn")
3283 ((and (cdr (assoc 'use-first-row-styles cell-style-selectors))
3284 (= r 0)) "FirstRow")
3285 ((and (cdr (assoc 'use-last-row-styles cell-style-selectors))
3286 (= (1+ r) (car table-dimensions)))
3287 "LastRow")
3288 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
3289 (= (% r 2) 1)) "EvenRow")
3290 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
3291 (= (% r 2) 0)) "OddRow")
3292 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
3293 (= (% c 2) 1)) "EvenColumn")
3294 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
3295 (= (% c 2) 0)) "OddColumn")
3296 (t ""))))
3297 (concat template-name cell-type)))))
3299 (defun org-e-odt-table-cell (table-cell contents info)
3300 "Transcode a TABLE-CELL element from Org to ODT.
3301 CONTENTS is nil. INFO is a plist used as a communication
3302 channel."
3303 (let* ((table-cell-address (org-export-table-cell-address table-cell info))
3304 (r (car table-cell-address))
3305 (c (cdr table-cell-address))
3306 (horiz-span (or (org-export-table-cell-width table-cell info) 0))
3307 (table-row (org-export-get-parent table-cell))
3308 (custom-style-prefix (org-e-odt-get-table-cell-styles
3309 table-cell info))
3310 (paragraph-style
3312 (and custom-style-prefix
3313 (format "%sTableParagraph" custom-style-prefix))
3314 (concat
3315 (cond
3316 ((and (= 1 (org-export-table-row-group table-row info))
3317 (org-export-table-has-header-p
3318 (org-export-get-parent-table table-row) info))
3319 "OrgTableHeading")
3320 ((let* ((table (org-export-get-parent-table table-cell))
3321 (table-attrs (org-export-read-attribute :attr_odt table))
3322 (table-header-columns (plist-get table-attrs
3323 :header-columns)))
3324 (<= c (cond ((wholenump table-header-columns)
3325 (- table-header-columns 1))
3326 (table-header-columns 0)
3327 (t -1))))
3328 "OrgTableHeading")
3329 (t "OrgTableContents"))
3330 (capitalize (symbol-name (org-export-table-cell-alignment
3331 table-cell info))))))
3332 (cell-style-name
3334 (and custom-style-prefix (format "%sTableCell"
3335 custom-style-prefix))
3336 (concat
3337 "OrgTblCell"
3338 (when (or (org-export-table-row-starts-rowgroup-p table-row info)
3339 (zerop r)) "T")
3340 (when (org-export-table-row-ends-rowgroup-p table-row info) "B")
3341 (when (and (org-export-table-cell-starts-colgroup-p table-cell info)
3342 (not (zerop c)) ) "L"))))
3343 (cell-attributes
3344 (concat
3345 (format " table:style-name=\"%s\"" cell-style-name)
3346 (and (> horiz-span 0)
3347 (format " table:number-columns-spanned=\"%d\""
3348 (1+ horiz-span))))))
3349 (unless contents (setq contents ""))
3350 (concat
3351 (assert paragraph-style)
3352 (format "\n<table:table-cell%s>\n%s\n</table:table-cell>"
3353 cell-attributes
3354 (let ((table-cell-contents (org-element-contents table-cell)))
3355 (if (memq (org-element-type (car table-cell-contents))
3356 org-element-all-elements)
3357 contents
3358 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3359 paragraph-style contents))))
3360 (let (s)
3361 (dotimes (i horiz-span s)
3362 (setq s (concat s "\n<table:covered-table-cell/>"))))
3363 "\n")))
3366 ;;;; Table Row
3368 (defun org-e-odt-table-row (table-row contents info)
3369 "Transcode a TABLE-ROW element from Org to ODT.
3370 CONTENTS is the contents of the row. INFO is a plist used as a
3371 communication channel."
3372 ;; Rules are ignored since table separators are deduced from
3373 ;; borders of the current row.
3374 (when (eq (org-element-property :type table-row) 'standard)
3375 (let* ((rowgroup-tags
3376 (if (and (= 1 (org-export-table-row-group table-row info))
3377 (org-export-table-has-header-p
3378 (org-export-get-parent-table table-row) info))
3379 ;; If the row belongs to the first rowgroup and the
3380 ;; table has more than one row groups, then this row
3381 ;; belongs to the header row group.
3382 '("\n<table:table-header-rows>" . "\n</table:table-header-rows>")
3383 ;; Otherwise, it belongs to non-header row group.
3384 '("\n<table:table-rows>" . "\n</table:table-rows>"))))
3385 (concat
3386 ;; Does this row begin a rowgroup?
3387 (when (org-export-table-row-starts-rowgroup-p table-row info)
3388 (car rowgroup-tags))
3389 ;; Actual table row
3390 (format "\n<table:table-row>\n%s\n</table:table-row>" contents)
3391 ;; Does this row end a rowgroup?
3392 (when (org-export-table-row-ends-rowgroup-p table-row info)
3393 (cdr rowgroup-tags))))))
3396 ;;;; Table
3398 (defun org-e-odt-table-first-row-data-cells (table info)
3399 (let ((table-row
3400 (org-element-map
3401 table 'table-row
3402 (lambda (row)
3403 (unless (eq (org-element-property :type row) 'rule) row))
3404 info 'first-match))
3405 (special-column-p (org-export-table-has-special-column-p table)))
3406 (if (not special-column-p) (org-element-contents table-row)
3407 (cdr (org-element-contents table-row)))))
3409 (defun org-e-odt--table (table contents info)
3410 "Transcode a TABLE element from Org to ODT.
3411 CONTENTS is the contents of the table. INFO is a plist holding
3412 contextual information."
3413 (case (org-element-property :type table)
3414 ;; Case 1: table.el doesn't support export to OD format. Strip
3415 ;; such tables from export.
3416 (table.el
3417 (prog1 nil
3418 (message
3419 (concat
3420 "(org-e-odt): Found table.el-type table in the source Org file."
3421 " table.el doesn't support export to ODT format."
3422 " Stripping the table from export."))))
3423 ;; Case 2: Native Org tables.
3424 (otherwise
3425 (let* ((captions (org-e-odt-format-label table info 'definition))
3426 (caption (car captions)) (short-caption (cdr captions))
3427 (attributes (org-export-read-attribute :attr_odt table))
3428 (custom-table-style (nth 1 (org-e-odt-table-style-spec table info)))
3429 (table-column-specs
3430 (function
3431 (lambda (table info)
3432 (let* ((table-style (or custom-table-style "OrgTable"))
3433 (column-style (format "%sColumn" table-style)))
3434 (mapconcat
3435 (lambda (table-cell)
3436 (let ((width (1+ (or (org-export-table-cell-width
3437 table-cell info) 0)))
3438 (s (format
3439 "\n<table:table-column table:style-name=\"%s\"/>"
3440 column-style))
3441 out)
3442 (dotimes (i width out) (setq out (concat s out)))))
3443 (org-e-odt-table-first-row-data-cells table info) "\n"))))))
3444 (concat
3445 ;; caption.
3446 (when caption
3447 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3448 "Table" caption))
3449 ;; begin table.
3450 (let* ((automatic-name
3451 (org-e-odt-add-automatic-style "Table" attributes)))
3452 (format
3453 "\n<table:table table:style-name=\"%s\"%s>"
3454 (or custom-table-style (cdr automatic-name) "OrgTable")
3455 (concat (when short-caption
3456 (format " table:name=\"%s\"" short-caption)))))
3457 ;; column specification.
3458 (funcall table-column-specs table info)
3459 ;; actual contents.
3460 "\n" contents
3461 ;; end table.
3462 "</table:table>")))))
3464 (defun org-e-odt-table (table contents info)
3465 "Transcode a TABLE element from Org to ODT.
3466 CONTENTS is the contents of the table. INFO is a plist holding
3467 contextual information.
3469 Use `org-e-odt--table' to typeset the table. Handle details
3470 pertaining to indentation here."
3471 (let* ((--element-preceded-by-table-p
3472 (function
3473 (lambda (element info)
3474 (loop for el in (org-export-get-previous-element element info t)
3475 thereis (eq (org-element-type el) 'table)))))
3476 (--walk-list-genealogy-and-collect-tags
3477 (function
3478 (lambda (table info)
3479 (let* ((genealogy (org-export-get-genealogy table))
3480 (list-genealogy
3481 (when (eq (org-element-type (car genealogy)) 'item)
3482 (loop for el in genealogy
3483 when (memq (org-element-type el)
3484 '(item plain-list))
3485 collect el)))
3486 (llh-genealogy
3487 (apply 'nconc
3488 (loop for el in genealogy
3489 when (and (eq (org-element-type el) 'headline)
3490 (org-export-low-level-p el info))
3491 collect
3492 (list el
3493 (assq 'headline
3494 (org-element-contents
3495 (org-export-get-parent el)))))))
3496 parent-list)
3497 (nconc
3498 ;; Handle list genealogy.
3499 (loop for el in list-genealogy collect
3500 (case (org-element-type el)
3501 (plain-list
3502 (setq parent-list el)
3503 (cons "</text:list>"
3504 (format "\n<text:list text:style-name=\"%s\" %s>"
3505 (case (org-element-property :type el)
3506 (ordered "OrgNumberedList")
3507 (unordered "OrgBulletedList")
3508 (descriptive-1 "OrgDescriptionList")
3509 (descriptive-2 "OrgDescriptionList"))
3510 "text:continue-numbering=\"true\"")))
3511 (item
3512 (cond
3513 ((not parent-list)
3514 (if (funcall --element-preceded-by-table-p table info)
3515 '("</text:list-header>" . "<text:list-header>")
3516 '("</text:list-item>" . "<text:list-header>")))
3517 ((funcall --element-preceded-by-table-p
3518 parent-list info)
3519 '("</text:list-header>" . "<text:list-header>"))
3520 (t '("</text:list-item>" . "<text:list-item>"))))))
3521 ;; Handle low-level headlines.
3522 (loop for el in llh-genealogy
3523 with step = 'item collect
3524 (case step
3525 (plain-list
3526 (setq step 'item) ; Flip-flop
3527 (setq parent-list el)
3528 (cons "</text:list>"
3529 (format "\n<text:list text:style-name=\"%s\" %s>"
3530 (if (org-export-numbered-headline-p
3531 el info)
3532 "OrgNumberedList"
3533 "OrgBulletedList")
3534 "text:continue-numbering=\"true\"")))
3535 (item
3536 (setq step 'plain-list) ; Flip-flop
3537 (cond
3538 ((not parent-list)
3539 (if (funcall --element-preceded-by-table-p table info)
3540 '("</text:list-header>" . "<text:list-header>")
3541 '("</text:list-item>" . "<text:list-header>")))
3542 ((let ((section? (org-export-get-previous-element
3543 parent-list info)))
3544 (and section?
3545 (eq (org-element-type section?) 'section)
3546 (assq 'table (org-element-contents section?))))
3547 '("</text:list-header>" . "<text:list-header>"))
3549 '("</text:list-item>" . "<text:list-item>")))))))))))
3550 (close-open-tags (funcall --walk-list-genealogy-and-collect-tags
3551 table info)))
3552 ;; OpenDocument schema does not permit table to occur within a
3553 ;; list item.
3555 ;; One solution - the easiest and lightweight, in terms of
3556 ;; implementation - is to put the table in an indented text box
3557 ;; and make the text box part of the list-item. Unfortunately if
3558 ;; the table is big and spans multiple pages, the text box could
3559 ;; overflow. In this case, the following attribute will come
3560 ;; handy.
3562 ;; ,---- From OpenDocument-v1.1.pdf
3563 ;; | 15.27.28 Overflow behavior
3564 ;; |
3565 ;; | For text boxes contained within text document, the
3566 ;; | style:overflow-behavior property specifies the behavior of text
3567 ;; | boxes where the containing text does not fit into the text
3568 ;; | box.
3569 ;; |
3570 ;; | If the attribute's value is clip, the text that does not fit
3571 ;; | into the text box is not displayed.
3572 ;; |
3573 ;; | If the attribute value is auto-create-new-frame, a new frame
3574 ;; | will be created on the next page, with the same position and
3575 ;; | dimensions of the original frame.
3576 ;; |
3577 ;; | If the style:overflow-behavior property's value is
3578 ;; | auto-create-new-frame and the text box has a minimum width or
3579 ;; | height specified, then the text box will grow until the page
3580 ;; | bounds are reached before a new frame is created.
3581 ;; `----
3583 ;; Unfortunately, LibreOffice-3.4.6 doesn't honor
3584 ;; auto-create-new-frame property and always resorts to clipping
3585 ;; the text box. This results in table being truncated.
3587 ;; So we solve the problem the hard (and fun) way using list
3588 ;; continuations.
3590 ;; The problem only becomes more interesting if you take in to
3591 ;; account the following facts:
3593 ;; - Description lists are simulated as plain lists.
3594 ;; - Low-level headlines can be listified.
3595 ;; - In Org-mode, a table can occur not only as a regular list
3596 ;; item, but also within description lists and low-level
3597 ;; headlines.
3599 ;; See `org-e-odt-translate-description-lists' and
3600 ;; `org-e-odt-translate-low-level-headlines' for how this is
3601 ;; tackled.
3603 (concat "\n"
3604 ;; Discontinue the list.
3605 (mapconcat 'car close-open-tags "\n")
3606 ;; Put the table in an indented section.
3607 (let* ((table (org-e-odt--table table contents info))
3608 (level (/ (length (mapcar 'car close-open-tags)) 2))
3609 (style (format "OrgIndentedSection-Level-%d" level)))
3610 (when table (org-e-odt-format-section table style)))
3611 ;; Continue the list.
3612 (mapconcat 'cdr (nreverse close-open-tags) "\n"))))
3615 ;;;; Target
3617 (defun org-e-odt-target (target contents info)
3618 "Transcode a TARGET object from Org to ODT.
3619 CONTENTS is nil. INFO is a plist holding contextual
3620 information."
3621 (let ((value (org-element-property :value target)))
3622 (org-e-odt--target "" (org-export-solidify-link-text value))))
3625 ;;;; Timestamp
3627 (defun org-e-odt-timestamp (timestamp contents info)
3628 "Transcode a TIMESTAMP object from Org to ODT.
3629 CONTENTS is nil. INFO is a plist used as a communication
3630 channel."
3631 (let* ((raw-value (org-element-property :raw-value timestamp))
3632 (type (org-element-property :type timestamp)))
3633 (if (not org-e-odt-use-date-fields)
3634 (let ((value (org-e-odt-plain-text
3635 (org-timestamp-translate timestamp) info)))
3636 (case (org-element-property :type timestamp)
3637 ((active active-range)
3638 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3639 "OrgActiveTimestamp" value))
3640 ((inactive inactive-range)
3641 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3642 "OrgInactiveTimestamp" value))
3643 (otherwise value)))
3644 (case type
3645 (active
3646 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3647 "OrgActiveTimestamp"
3648 (format "&lt;%s&gt;" (org-e-odt--format-timestamp timestamp))))
3649 (inactive
3650 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3651 "OrgInactiveTimestamp"
3652 (format "[%s]" (org-e-odt--format-timestamp timestamp))))
3653 (active-range
3654 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3655 "OrgActiveTimestamp"
3656 (format "&lt;%s&gt;&#x2013;&lt;%s&gt;"
3657 (org-e-odt--format-timestamp timestamp)
3658 (org-e-odt--format-timestamp timestamp 'end))))
3659 (inactive-range
3660 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3661 "OrgInactiveTimestamp"
3662 (format "[%s]&#x2013;[%s]"
3663 (org-e-odt--format-timestamp timestamp)
3664 (org-e-odt--format-timestamp timestamp 'end))))
3665 (otherwise
3666 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3667 "OrgDiaryTimestamp"
3668 (org-e-odt-plain-text (org-timestamp-translate timestamp)
3669 info)))))))
3672 ;;;; Underline
3674 (defun org-e-odt-underline (underline contents info)
3675 "Transcode UNDERLINE from Org to ODT.
3676 CONTENTS is the text with underline markup. INFO is a plist
3677 holding contextual information."
3678 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3679 "Underline" contents))
3682 ;;;; Verbatim
3684 (defun org-e-odt-verbatim (verbatim contents info)
3685 "Transcode a VERBATIM object from Org to ODT.
3686 CONTENTS is nil. INFO is a plist used as a communication
3687 channel."
3688 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3689 "OrgCode" (org-element-property :value verbatim)))
3692 ;;;; Verse Block
3694 (defun org-e-odt-verse-block (verse-block contents info)
3695 "Transcode a VERSE-BLOCK element from Org to ODT.
3696 CONTENTS is verse block contents. INFO is a plist holding
3697 contextual information."
3698 ;; Add line breaks to each line of verse.
3699 (setq contents (replace-regexp-in-string
3700 "\\(<text:line-break/>\\)?[ \t]*\n"
3701 "<text:line-break/>" contents))
3702 ;; Replace tabs and spaces.
3703 (setq contents (org-e-odt--encode-tabs-and-spaces contents))
3704 ;; Surround it in a verse environment.
3705 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3706 "OrgVerse" contents))
3710 ;;; Filters
3712 ;;;; LaTeX fragments
3714 (defun org-e-odt--translate-latex-fragments (tree backend info)
3715 (let ((processing-type (plist-get info :LaTeX-fragments))
3716 (count 0))
3717 ;; Normalize processing-type to one of dvipng, mathml or verbatim.
3718 ;; If the desired converter is not available, force verbatim
3719 ;; processing.
3720 (case processing-type
3721 ((t mathml)
3722 (if (and (fboundp 'org-format-latex-mathml-available-p)
3723 (org-format-latex-mathml-available-p))
3724 (setq processing-type 'mathml)
3725 (message "LaTeX to MathML converter not available.")
3726 (setq processing-type 'verbatim)))
3727 (dvipng
3728 (unless (and (org-check-external-command "latex" "" t)
3729 (org-check-external-command "dvipng" "" t))
3730 (message "LaTeX to PNG converter not available.")
3731 (setq processing-type 'verbatim)))
3732 (otherwise
3733 (message "Unknown LaTeX option. Forcing verbatim.")
3734 (setq processing-type 'verbatim)))
3736 ;; Store normalized value for later use.
3737 (when (plist-get info :LaTeX-fragments)
3738 (plist-put info :LaTeX-fragments processing-type))
3739 (message "Formatting LaTeX using %s" processing-type)
3741 ;; Convert `latex-fragment's and `latex-environment's.
3742 (when (memq processing-type '(mathml dvipng))
3743 (org-element-map
3744 tree '(latex-fragment latex-environment)
3745 (lambda (latex-*)
3746 (incf count)
3747 (let* ((latex-frag (org-element-property :value latex-*))
3748 (input-file (plist-get info :input-file))
3749 (cache-dir (file-name-directory input-file))
3750 (cache-subdir (concat
3751 (case processing-type
3752 (dvipng "ltxpng/")
3753 (mathml "ltxmathml/"))
3754 (file-name-sans-extension
3755 (file-name-nondirectory input-file))))
3756 (display-msg
3757 (case processing-type
3758 (dvipng (format "Creating LaTeX Image %d..." count))
3759 (mathml (format "Creating MathML snippet %d..." count))))
3760 ;; Get an Org-style link to PNG image or the MathML
3761 ;; file.
3762 (org-link
3763 (let ((link (with-temp-buffer
3764 (insert latex-frag)
3765 (org-format-latex cache-subdir cache-dir
3766 nil display-msg
3767 nil nil processing-type)
3768 (buffer-substring-no-properties
3769 (point-min) (point-max)))))
3770 (if (not (string-match "file:\\([^]]*\\)" link))
3771 (prog1 nil (message "LaTeX Conversion failed."))
3772 link))))
3773 (when org-link
3774 ;; Conversion succeeded. Parse above Org-style link to a
3775 ;; `link' object.
3776 (let* ((link (car (org-element-map (with-temp-buffer
3777 (org-mode)
3778 (insert org-link)
3779 (org-element-parse-buffer))
3780 'link 'identity))))
3781 ;; Orphan the link.
3782 (org-element-put-property link :parent nil)
3783 (let* (
3784 (replacement
3785 (case (org-element-type latex-*)
3786 ;; Case 1: LaTeX environment.
3787 ;; Mimic a "standalone image or formula" by
3788 ;; enclosing the `link' in a `paragraph'.
3789 ;; Copy over original attributes, captions to
3790 ;; the enclosing paragraph.
3791 (latex-environment
3792 (org-element-adopt-elements
3793 (list 'paragraph
3794 (list :style "OrgFormula"
3795 :name (org-element-property :name
3796 latex-*)
3797 :caption (org-element-property :caption
3798 latex-*)))
3799 link))
3800 ;; Case 2: LaTeX fragment.
3801 ;; No special action.
3802 (latex-fragment link))))
3803 ;; Note down the object that link replaces.
3804 (org-element-put-property replacement :replaces
3805 (list (org-element-type latex-*)
3806 (list :value latex-frag)))
3807 ;; Replace now.
3808 (org-element-set-element latex-* replacement))))))
3809 info)))
3810 tree)
3813 ;;;; Description lists
3815 ;; This translator is necessary to handle indented tables in a uniform
3816 ;; manner. See comment in `org-e-odt--table'.
3818 (defun org-e-odt--translate-description-lists (tree backend info)
3819 ;; OpenDocument has no notion of a description list. So simulate it
3820 ;; using plain lists. Description lists in the exported document
3821 ;; are typeset in the same manner as they are in a typical HTML
3822 ;; document.
3824 ;; Specifically, a description list like this:
3826 ;; ,----
3827 ;; | - term-1 :: definition-1
3828 ;; | - term-2 :: definition-2
3829 ;; `----
3831 ;; gets translated in to the following form:
3833 ;; ,----
3834 ;; | - term-1
3835 ;; | - definition-1
3836 ;; | - term-2
3837 ;; | - definition-2
3838 ;; `----
3840 ;; Further effect is achieved by fixing the OD styles as below:
3842 ;; 1. Set the :type property of the simulated lists to
3843 ;; `descriptive-1' and `descriptive-2'. Map these to list-styles
3844 ;; that has *no* bullets whatsoever.
3846 ;; 2. The paragraph containing the definition term is styled to be
3847 ;; in bold.
3849 (org-element-map
3850 tree 'plain-list
3851 (lambda (el)
3852 (when (equal (org-element-property :type el) 'descriptive)
3853 (org-element-set-element
3855 (apply 'org-element-adopt-elements
3856 (list 'plain-list (list :type 'descriptive-1))
3857 (mapcar
3858 (lambda (item)
3859 (org-element-adopt-elements
3860 (list 'item (list :checkbox (org-element-property
3861 :checkbox item)))
3862 (list 'paragraph (list :style "Text_20_body_20_bold")
3863 (or (org-element-property :tag item) "(no term)"))
3864 (org-element-adopt-elements
3865 (list 'plain-list (list :type 'descriptive-2))
3866 (apply 'org-element-adopt-elements
3867 (list 'item nil)
3868 (org-element-contents item)))))
3869 (org-element-contents el)))))
3870 nil)
3871 info)
3872 tree)
3874 ;;;; List tables
3876 ;; Lists that are marked with attribute `:list-table' are called as
3877 ;; list tables. They will be rendered as a table within the exported
3878 ;; document.
3880 ;; Consider an example. The following list table
3882 ;; #+attr_odt :list-table t
3883 ;; - Row 1
3884 ;; - 1.1
3885 ;; - 1.2
3886 ;; - 1.3
3887 ;; - Row 2
3888 ;; - 2.1
3889 ;; - 2.2
3890 ;; - 2.3
3892 ;; will be exported as though it were an Org table like the one show
3893 ;; below.
3895 ;; | Row 1 | 1.1 | 1.2 | 1.3 |
3896 ;; | Row 2 | 2.1 | 2.2 | 2.3 |
3898 ;; Note that org-tables are NOT multi-line and each line is mapped to
3899 ;; a unique row in the exported document. So if an exported table
3900 ;; needs to contain a single paragraph (with copious text) it needs to
3901 ;; be typed up in a single line. Editing such long lines using the
3902 ;; table editor will be a cumbersome task. Furthermore inclusion of
3903 ;; multi-paragraph text in a table cell is well-nigh impossible.
3905 ;; A LIST-TABLE circumvents above problems.
3907 ;; Note that in the example above the list items could be paragraphs
3908 ;; themselves and the list can be arbitrarily deep.
3910 ;; Inspired by following thread:
3911 ;; https://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01101.html
3913 ;; Translate lists to tables
3915 (defun org-e-odt--translate-list-tables (tree backend info)
3916 (org-element-map
3917 tree 'plain-list
3918 (lambda (l1-list)
3919 (when (org-export-read-attribute :attr_odt l1-list :list-table)
3920 ;; Replace list with table.
3921 (org-element-set-element
3922 l1-list
3923 ;; Build replacement table.
3924 (apply 'org-element-adopt-elements
3925 (list 'table '(:type org :attr_odt (":style \"GriddedTable\"")))
3926 (org-element-map
3927 l1-list
3928 'item
3929 (lambda (l1-item)
3930 (let* ((l1-item-contents (org-element-contents l1-item))
3931 l1-item-leading-text l2-list)
3932 ;; Remove Level-2 list from the Level-item. It
3933 ;; will be subsequently attached as table-cells.
3934 (let ((cur l1-item-contents) prev)
3935 (while (and cur (not (eq (org-element-type (car cur))
3936 'plain-list)))
3937 (setq prev cur)
3938 (setq cur (cdr cur)))
3939 (when prev
3940 (setcdr prev nil)
3941 (setq l2-list (car cur)))
3942 (setq l1-item-leading-text l1-item-contents))
3943 ;; Level-1 items start a table row.
3944 (apply 'org-element-adopt-elements
3945 (list 'table-row (list :type 'standard))
3946 ;; Leading text of level-1 item define the
3947 ;; first table-cell.
3948 (apply 'org-element-adopt-elements
3949 (list 'table-cell nil)
3950 l1-item-leading-text)
3951 ;; Level-2 items define subsequent
3952 ;; table-cells of the row.
3953 (org-element-map
3954 l2-list
3955 'item
3956 (lambda (l2-item)
3957 (apply 'org-element-adopt-elements
3958 (list 'table-cell nil)
3959 (org-element-contents l2-item)))
3960 info nil 'item))))
3961 info nil 'item))))
3962 nil)
3963 info)
3964 tree)
3967 ;;; Interactive functions
3969 (defun org-e-odt-create-manifest-file-entry (&rest args)
3970 (push args org-e-odt-manifest-file-entries))
3972 (defun org-e-odt-write-manifest-file ()
3973 (make-directory (concat org-e-odt-zip-dir "META-INF"))
3974 (let ((manifest-file (concat org-e-odt-zip-dir "META-INF/manifest.xml")))
3975 (with-current-buffer
3976 (let ((nxml-auto-insert-xml-declaration-flag nil))
3977 (find-file-noselect manifest-file t))
3978 (insert
3979 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
3980 <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
3981 (mapc
3982 (lambda (file-entry)
3983 (let* ((version (nth 2 file-entry))
3984 (extra (if (not version) ""
3985 (format " manifest:version=\"%s\"" version))))
3986 (insert
3987 (format org-e-odt-manifest-file-entry-tag
3988 (nth 0 file-entry) (nth 1 file-entry) extra))))
3989 org-e-odt-manifest-file-entries)
3990 (insert "\n</manifest:manifest>"))))
3992 (defmacro org-e-odt--export-wrap (out-file &rest body)
3993 `(let* ((--out-file ,out-file)
3994 (out-file-type (file-name-extension --out-file))
3995 (org-e-odt-xml-files '("META-INF/manifest.xml" "content.xml"
3996 "meta.xml" "styles.xml"))
3997 ;; Initialize temporary workarea. All files that end up in
3998 ;; the exported document get parked/created here.
3999 (org-e-odt-zip-dir (file-name-as-directory
4000 (make-temp-file (format "%s-" out-file-type) t)))
4001 (org-e-odt-manifest-file-entries nil)
4002 (--cleanup-xml-buffers
4003 (function
4004 (lambda nil
4005 ;; Kill all XML buffers.
4006 (mapc (lambda (file)
4007 (let ((buf (find-buffer-visiting
4008 (concat org-e-odt-zip-dir file))))
4009 (when buf
4010 (with-current-buffer buf
4011 (set-buffer-modified-p nil)
4012 (kill-buffer buf)))))
4013 org-e-odt-xml-files)
4014 ;; Delete temporary directory and also other embedded
4015 ;; files that get copied there.
4016 (delete-directory org-e-odt-zip-dir t)))))
4017 (org-condition-case-unless-debug err
4018 (progn
4019 (unless (executable-find "zip")
4020 ;; Not at all OSes ship with zip by default
4021 (error "Executable \"zip\" needed for creating OpenDocument files"))
4022 ;; Do export. This creates a bunch of xml files ready to be
4023 ;; saved and zipped.
4024 (progn ,@body)
4025 ;; Create a manifest entry for content.xml.
4026 (org-e-odt-create-manifest-file-entry "text/xml" "content.xml")
4027 ;; Write mimetype file
4028 (let* ((mimetypes
4029 '(("odt" . "application/vnd.oasis.opendocument.text")
4030 ("odf" . "application/vnd.oasis.opendocument.formula")))
4031 (mimetype (cdr (assoc-string out-file-type mimetypes t))))
4032 (unless mimetype
4033 (error "Unknown OpenDocument backend %S" out-file-type))
4034 (write-region mimetype nil (concat org-e-odt-zip-dir "mimetype"))
4035 (org-e-odt-create-manifest-file-entry mimetype "/" "1.2"))
4036 ;; Write out the manifest entries before zipping
4037 (org-e-odt-write-manifest-file)
4038 ;; Save all XML files.
4039 (mapc (lambda (file)
4040 (let ((buf (find-buffer-visiting
4041 (concat org-e-odt-zip-dir file))))
4042 (when buf
4043 (with-current-buffer buf
4044 ;; Prettify output if needed.
4045 (when org-e-odt-prettify-xml
4046 (indent-region (point-min) (point-max)))
4047 (save-buffer 0)))))
4048 org-e-odt-xml-files)
4049 ;; Run zip.
4050 (let* ((target --out-file)
4051 (target-name (file-name-nondirectory target))
4052 (cmds `(("zip" "-mX0" ,target-name "mimetype")
4053 ("zip" "-rmTq" ,target-name "."))))
4054 ;; If a file with same name as the desired output file
4055 ;; exists, remove it.
4056 (when (file-exists-p target)
4057 (delete-file target))
4058 ;; Zip up the xml files.
4059 (let ((coding-system-for-write 'no-conversion) exitcode err-string)
4060 (message "Creating ODT file...")
4061 ;; Switch temporarily to content.xml. This way Zip
4062 ;; process will inherit `org-e-odt-zip-dir' as the current
4063 ;; directory.
4064 (with-current-buffer
4065 (find-file-noselect (concat org-e-odt-zip-dir "content.xml") t)
4066 (mapc
4067 (lambda (cmd)
4068 (message "Running %s" (mapconcat 'identity cmd " "))
4069 (setq err-string
4070 (with-output-to-string
4071 (setq exitcode
4072 (apply 'call-process (car cmd)
4073 nil standard-output nil (cdr cmd)))))
4074 (or (zerop exitcode)
4075 (error (concat "Unable to create OpenDocument file."
4076 (format " Zip failed with error (%s)"
4077 err-string)))))
4078 cmds)))
4079 ;; Move the zip file from temporary work directory to
4080 ;; user-mandated location.
4081 (rename-file (concat org-e-odt-zip-dir target-name) target)
4082 (message "Created %s" (expand-file-name target))
4083 ;; Cleanup work directory and work files.
4084 (funcall --cleanup-xml-buffers)
4085 ;; Open the OpenDocument file in archive-mode for
4086 ;; examination.
4087 (find-file-noselect target t)
4088 ;; Return exported file.
4089 (cond
4090 ;; Case 1: Conversion desired on exported file. Run the
4091 ;; converter on the OpenDocument file. Return the
4092 ;; converted file.
4093 (org-e-odt-preferred-output-format
4094 (or (org-e-odt-convert target org-e-odt-preferred-output-format)
4095 target))
4096 ;; Case 2: No further conversion. Return exported
4097 ;; OpenDocument file.
4098 (t target))))
4099 (error
4100 ;; Cleanup work directory and work files.
4101 (funcall --cleanup-xml-buffers)
4102 (message "OpenDocument export failed: %s"
4103 (error-message-string err))))))
4106 ;;;; Export to OpenDocument formula
4108 ;;;###autoload
4109 (defun org-e-odt-export-as-odf (latex-frag &optional odf-file)
4110 "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
4111 Use `org-create-math-formula' to convert LATEX-FRAG first to
4112 MathML. When invoked as an interactive command, use
4113 `org-latex-regexps' to infer LATEX-FRAG from currently active
4114 region. If no LaTeX fragments are found, prompt for it. Push
4115 MathML source to kill ring, if `org-export-copy-to-kill-ring' is
4116 non-nil."
4117 (interactive
4118 `(,(let (frag)
4119 (setq frag (and (setq frag (and (region-active-p)
4120 (buffer-substring (region-beginning)
4121 (region-end))))
4122 (loop for e in org-latex-regexps
4123 thereis (when (string-match (nth 1 e) frag)
4124 (match-string (nth 2 e) frag)))))
4125 (read-string "LaTeX Fragment: " frag nil frag))
4126 ,(let ((odf-filename (expand-file-name
4127 (concat
4128 (file-name-sans-extension
4129 (or (file-name-nondirectory buffer-file-name)))
4130 "." "odf")
4131 (file-name-directory buffer-file-name))))
4132 (read-file-name "ODF filename: " nil odf-filename nil
4133 (file-name-nondirectory odf-filename)))))
4134 (let ((filename (or odf-file
4135 (expand-file-name
4136 (concat
4137 (file-name-sans-extension
4138 (or (file-name-nondirectory buffer-file-name)))
4139 "." "odf")
4140 (file-name-directory buffer-file-name)))))
4141 (org-e-odt--export-wrap
4142 filename
4143 (let* ((buffer (progn
4144 (require 'nxml-mode)
4145 (let ((nxml-auto-insert-xml-declaration-flag nil))
4146 (find-file-noselect (concat org-e-odt-zip-dir
4147 "content.xml") t))))
4148 (coding-system-for-write 'utf-8)
4149 (save-buffer-coding-system 'utf-8))
4150 (set-buffer buffer)
4151 (set-buffer-file-coding-system coding-system-for-write)
4152 (let ((mathml (org-create-math-formula latex-frag)))
4153 (unless mathml (error "No Math formula created"))
4154 (insert mathml)
4155 ;; Add MathML to kill ring, if needed.
4156 (when org-export-copy-to-kill-ring
4157 (org-kill-new (buffer-string))))))))
4159 ;;;###autoload
4160 (defun org-e-odt-export-as-odf-and-open ()
4161 "Export LaTeX fragment as OpenDocument formula and immediately open it.
4162 Use `org-e-odt-export-as-odf' to read LaTeX fragment and OpenDocument
4163 formula file."
4164 (interactive)
4165 (org-open-file (call-interactively 'org-e-odt-export-as-odf) 'system))
4168 ;;;; Export to OpenDocument Text
4170 ;;;###autoload
4171 (defun org-e-odt-export-to-odt (&optional async subtreep visible-only ext-plist)
4172 "Export current buffer to a ODT file.
4174 If narrowing is active in the current buffer, only export its
4175 narrowed part.
4177 If a region is active, export that region.
4179 A non-nil optional argument ASYNC means the process should happen
4180 asynchronously. The resulting file should be accessible through
4181 the `org-export-stack' interface.
4183 When optional argument SUBTREEP is non-nil, export the sub-tree
4184 at point, extracting information from the headline properties
4185 first.
4187 When optional argument VISIBLE-ONLY is non-nil, don't export
4188 contents of hidden elements.
4190 EXT-PLIST, when provided, is a property list with external
4191 parameters overriding Org default settings, but still inferior to
4192 file-local settings.
4194 Return output file's name."
4195 (interactive)
4196 (let ((outfile (org-export-output-file-name ".odt" subtreep)))
4197 (if async
4198 (org-export-async-start (lambda (f) (org-export-add-to-stack f 'e-odt))
4199 `(expand-file-name
4200 (org-e-odt--export-wrap
4201 ,outfile
4202 (let* ((org-e-odt-embedded-images-count 0)
4203 (org-e-odt-embedded-formulas-count 0)
4204 (org-e-odt-automatic-styles nil)
4205 (org-e-odt-object-counters nil)
4206 ;; Let `htmlfontify' know that we are interested in
4207 ;; collecting styles.
4208 (hfy-user-sheet-assoc nil))
4209 ;; Initialize content.xml and kick-off the export
4210 ;; process.
4211 (let ((out-buf
4212 (progn
4213 (require 'nxml-mode)
4214 (let ((nxml-auto-insert-xml-declaration-flag nil))
4215 (find-file-noselect
4216 (concat org-e-odt-zip-dir "content.xml") t)))))
4217 (org-export-to-buffer
4218 'e-odt out-buf ,subtreep ,visible-only nil ',ext-plist))))))
4219 (org-e-odt--export-wrap
4220 outfile
4221 (let* ((org-e-odt-embedded-images-count 0)
4222 (org-e-odt-embedded-formulas-count 0)
4223 (org-e-odt-automatic-styles nil)
4224 (org-e-odt-object-counters nil)
4225 ;; Let `htmlfontify' know that we are interested in collecting
4226 ;; styles.
4227 (hfy-user-sheet-assoc nil))
4228 ;; Initialize content.xml and kick-off the export process.
4229 (let ((out-buf (progn
4230 (require 'nxml-mode)
4231 (let ((nxml-auto-insert-xml-declaration-flag nil))
4232 (find-file-noselect
4233 (concat org-e-odt-zip-dir "content.xml") t)))))
4234 (org-export-to-buffer
4235 'e-odt out-buf subtreep visible-only nil ext-plist)))))))
4238 ;;;; Convert between OpenDocument and other formats
4240 (defun org-e-odt-reachable-p (in-fmt out-fmt)
4241 "Return non-nil if IN-FMT can be converted to OUT-FMT."
4242 (catch 'done
4243 (let ((reachable-formats (org-e-odt-do-reachable-formats in-fmt)))
4244 (dolist (e reachable-formats)
4245 (let ((out-fmt-spec (assoc out-fmt (cdr e))))
4246 (when out-fmt-spec
4247 (throw 'done (cons (car e) out-fmt-spec))))))))
4249 (defun org-e-odt-do-convert (in-file out-fmt &optional prefix-arg)
4250 "Workhorse routine for `org-e-odt-convert'."
4251 (require 'browse-url)
4252 (let* ((in-file (expand-file-name (or in-file buffer-file-name)))
4253 (dummy (or (file-readable-p in-file)
4254 (error "Cannot read %s" in-file)))
4255 (in-fmt (file-name-extension in-file))
4256 (out-fmt (or out-fmt (error "Output format unspecified")))
4257 (how (or (org-e-odt-reachable-p in-fmt out-fmt)
4258 (error "Cannot convert from %s format to %s format?"
4259 in-fmt out-fmt)))
4260 (convert-process (car how))
4261 (out-file (concat (file-name-sans-extension in-file) "."
4262 (nth 1 (or (cdr how) out-fmt))))
4263 (extra-options (or (nth 2 (cdr how)) ""))
4264 (out-dir (file-name-directory in-file))
4265 (cmd (format-spec convert-process
4266 `((?i . ,(shell-quote-argument in-file))
4267 (?I . ,(browse-url-file-url in-file))
4268 (?f . ,out-fmt)
4269 (?o . ,out-file)
4270 (?O . ,(browse-url-file-url out-file))
4271 (?d . , (shell-quote-argument out-dir))
4272 (?D . ,(browse-url-file-url out-dir))
4273 (?x . ,extra-options)))))
4274 (when (file-exists-p out-file)
4275 (delete-file out-file))
4277 (message "Executing %s" cmd)
4278 (let ((cmd-output (shell-command-to-string cmd)))
4279 (message "%s" cmd-output))
4281 (cond
4282 ((file-exists-p out-file)
4283 (message "Exported to %s" out-file)
4284 (when prefix-arg
4285 (message "Opening %s..." out-file)
4286 (org-open-file out-file 'system))
4287 out-file)
4289 (message "Export to %s failed" out-file)
4290 nil))))
4292 (defun org-e-odt-do-reachable-formats (in-fmt)
4293 "Return verbose info about formats to which IN-FMT can be converted.
4294 Return a list where each element is of the
4295 form (CONVERTER-PROCESS . OUTPUT-FMT-ALIST). See
4296 `org-e-odt-convert-processes' for CONVERTER-PROCESS and see
4297 `org-e-odt-convert-capabilities' for OUTPUT-FMT-ALIST."
4298 (let* ((converter
4299 (and org-e-odt-convert-process
4300 (cadr (assoc-string org-e-odt-convert-process
4301 org-e-odt-convert-processes t))))
4302 (capabilities
4303 (and org-e-odt-convert-process
4304 (cadr (assoc-string org-e-odt-convert-process
4305 org-e-odt-convert-processes t))
4306 org-e-odt-convert-capabilities))
4307 reachable-formats)
4308 (when converter
4309 (dolist (c capabilities)
4310 (when (member in-fmt (nth 1 c))
4311 (push (cons converter (nth 2 c)) reachable-formats))))
4312 reachable-formats))
4314 (defun org-e-odt-reachable-formats (in-fmt)
4315 "Return list of formats to which IN-FMT can be converted.
4316 The list of the form (OUTPUT-FMT-1 OUTPUT-FMT-2 ...)."
4317 (let (l)
4318 (mapc (lambda (e) (add-to-list 'l e))
4319 (apply 'append (mapcar
4320 (lambda (e) (mapcar 'car (cdr e)))
4321 (org-e-odt-do-reachable-formats in-fmt))))
4324 (defun org-e-odt-convert-read-params ()
4325 "Return IN-FILE and OUT-FMT params for `org-e-odt-do-convert'.
4326 This is a helper routine for interactive use."
4327 (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
4328 (in-file (read-file-name "File to be converted: "
4329 nil buffer-file-name t))
4330 (in-fmt (file-name-extension in-file))
4331 (out-fmt-choices (org-e-odt-reachable-formats in-fmt))
4332 (out-fmt
4333 (or (and out-fmt-choices
4334 (funcall input "Output format: "
4335 out-fmt-choices nil nil nil))
4336 (error
4337 "No known converter or no known output formats for %s files"
4338 in-fmt))))
4339 (list in-file out-fmt)))
4341 ;;;###autoload
4342 (defun org-e-odt-convert (&optional in-file out-fmt prefix-arg)
4343 "Convert IN-FILE to format OUT-FMT using a command line converter.
4344 IN-FILE is the file to be converted. If unspecified, it defaults
4345 to variable `buffer-file-name'. OUT-FMT is the desired output
4346 format. Use `org-e-odt-convert-process' as the converter.
4347 If PREFIX-ARG is non-nil then the newly converted file is opened
4348 using `org-open-file'."
4349 (interactive
4350 (append (org-e-odt-convert-read-params) current-prefix-arg))
4351 (org-e-odt-do-convert in-file out-fmt prefix-arg))
4353 ;;; Library Initializations
4355 (mapc
4356 (lambda (desc)
4357 ;; Let Emacs open all OpenDocument files in archive mode
4358 (add-to-list 'auto-mode-alist
4359 (cons (concat "\\." (car desc) "\\'") 'archive-mode)))
4360 org-e-odt-file-extensions)
4362 (provide 'org-e-odt)
4364 ;;; org-e-odt.el ends here