export back-ends: Activate asynchronous export
[org-mode.git] / contrib / lisp / org-e-odt.el
blob5904fadfd2bac1954fd936d3f9681df71c7b5f55
1 ;;; org-e-odt.el --- OpenDocument Text exporter for Org-mode
3 ;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
5 ;; Author: Jambunathan K <kjambunathan at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
9 ;; This file is 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)
34 ;;; Define Back-End
36 (org-export-define-backend e-odt
37 ((bold . org-e-odt-bold)
38 (center-block . org-e-odt-center-block)
39 (clock . org-e-odt-clock)
40 (code . org-e-odt-code)
41 (drawer . org-e-odt-drawer)
42 (dynamic-block . org-e-odt-dynamic-block)
43 (entity . org-e-odt-entity)
44 (example-block . org-e-odt-example-block)
45 (export-block . org-e-odt-export-block)
46 (export-snippet . org-e-odt-export-snippet)
47 (fixed-width . org-e-odt-fixed-width)
48 (footnote-definition . org-e-odt-footnote-definition)
49 (footnote-reference . org-e-odt-footnote-reference)
50 (headline . org-e-odt-headline)
51 (horizontal-rule . org-e-odt-horizontal-rule)
52 (inline-src-block . org-e-odt-inline-src-block)
53 (inlinetask . org-e-odt-inlinetask)
54 (italic . org-e-odt-italic)
55 (item . org-e-odt-item)
56 (keyword . org-e-odt-keyword)
57 (latex-environment . org-e-odt-latex-environment)
58 (latex-fragment . org-e-odt-latex-fragment)
59 (line-break . org-e-odt-line-break)
60 (link . org-e-odt-link)
61 (paragraph . org-e-odt-paragraph)
62 (plain-list . org-e-odt-plain-list)
63 (plain-text . org-e-odt-plain-text)
64 (planning . org-e-odt-planning)
65 (property-drawer . org-e-odt-property-drawer)
66 (quote-block . org-e-odt-quote-block)
67 (quote-section . org-e-odt-quote-section)
68 (radio-target . org-e-odt-radio-target)
69 (section . org-e-odt-section)
70 (special-block . org-e-odt-special-block)
71 (src-block . org-e-odt-src-block)
72 (statistics-cookie . org-e-odt-statistics-cookie)
73 (strike-through . org-e-odt-strike-through)
74 (subscript . org-e-odt-subscript)
75 (superscript . org-e-odt-superscript)
76 (table . org-e-odt-table)
77 (table-cell . org-e-odt-table-cell)
78 (table-row . org-e-odt-table-row)
79 (target . org-e-odt-target)
80 (template . org-e-odt-template)
81 (timestamp . org-e-odt-timestamp)
82 (underline . org-e-odt-underline)
83 (verbatim . org-e-odt-verbatim)
84 (verse-block . org-e-odt-verse-block))
85 :export-block "ODT"
86 :filters-alist ((:filter-parse-tree
87 . (org-e-odt--translate-latex-fragments
88 org-e-odt--translate-description-lists
89 org-e-odt--translate-list-tables)))
90 :menu-entry
91 (?o "Export to ODT"
92 ((?o "As ODT file" org-e-odt-export-to-odt)
93 (?O "As ODT file and open"
94 (lambda (a s v b)
95 (if a (org-e-odt-export-to-odt t s v)
96 (org-open-file (org-e-odt-export-to-odt nil s v) 'system))))))
97 :options-alist
98 ((:odt-styles-file "ODT_STYLES_FILE" nil nil t)
99 (:LaTeX-fragments nil "LaTeX" org-export-with-LaTeX-fragments)))
102 ;;; Dependencies
104 ;;; Hooks
106 ;;; Function Declarations
108 (declare-function org-id-find-id-file "org-id" (id))
109 (declare-function hfy-face-to-style "htmlfontify" (fn))
110 (declare-function hfy-face-or-def-to-name "htmlfontify" (fn))
111 (declare-function archive-zip-extract "arc-mode" (archive name))
112 (declare-function org-create-math-formula "org" (latex-frag &optional mathml-file))
113 (declare-function browse-url-file-url "browse-url" (file))
117 ;;; Internal Variables
119 (defconst org-e-odt-lib-dir
120 (file-name-directory load-file-name)
121 "Location of ODT exporter.
122 Use this to infer values of `org-e-odt-styles-dir' and
123 `org-e-odt-schema-dir'.")
125 (defvar org-e-odt-data-dir
126 (expand-file-name "../../etc/" org-e-odt-lib-dir)
127 "Data directory for ODT exporter.
128 Use this to infer values of `org-e-odt-styles-dir' and
129 `org-e-odt-schema-dir'.")
131 (defconst org-e-odt-special-string-regexps
132 '(("\\\\-" . "&#x00ad;\\1") ; shy
133 ("---\\([^-]\\)" . "&#x2014;\\1") ; mdash
134 ("--\\([^-]\\)" . "&#x2013;\\1") ; ndash
135 ("\\.\\.\\." . "&#x2026;")) ; hellip
136 "Regular expressions for special string conversion.")
138 (defconst org-e-odt-schema-dir-list
139 (list
140 (and org-e-odt-data-dir
141 (expand-file-name "./schema/" org-e-odt-data-dir)) ; bail out
142 (eval-when-compile
143 (and (boundp 'org-e-odt-data-dir) org-e-odt-data-dir ; see make install
144 (expand-file-name "./schema/" org-e-odt-data-dir))))
145 "List of directories to search for OpenDocument schema files.
146 Use this list to set the default value of
147 `org-e-odt-schema-dir'. The entries in this list are
148 populated heuristically based on the values of `org-e-odt-lib-dir'
149 and `org-e-odt-data-dir'.")
151 (defconst org-e-odt-styles-dir-list
152 (list
153 (and org-e-odt-data-dir
154 (expand-file-name "./styles/" org-e-odt-data-dir)) ; bail out
155 (eval-when-compile
156 (and (boundp 'org-e-odt-data-dir) org-e-odt-data-dir ; see make install
157 (expand-file-name "./styles/" org-e-odt-data-dir)))
158 (expand-file-name "../../etc/styles/" org-e-odt-lib-dir) ; git
159 (expand-file-name "./etc/styles/" org-e-odt-lib-dir) ; elpa
160 (expand-file-name "./org/" data-directory) ; system
162 "List of directories to search for OpenDocument styles files.
163 See `org-e-odt-styles-dir'. The entries in this list are populated
164 heuristically based on the values of `org-e-odt-lib-dir' and
165 `org-e-odt-data-dir'.")
167 (defconst org-e-odt-styles-dir
168 (let* ((styles-dir
169 (catch 'styles-dir
170 (message "Debug (org-e-odt): Searching for OpenDocument styles files...")
171 (mapc (lambda (styles-dir)
172 (when styles-dir
173 (message "Debug (org-e-odt): Trying %s..." styles-dir)
174 (when (and (file-readable-p
175 (expand-file-name
176 "OrgOdtContentTemplate.xml" styles-dir))
177 (file-readable-p
178 (expand-file-name
179 "OrgOdtStyles.xml" styles-dir)))
180 (message "Debug (org-e-odt): Using styles under %s"
181 styles-dir)
182 (throw 'styles-dir styles-dir))))
183 org-e-odt-styles-dir-list)
184 nil)))
185 (unless styles-dir
186 (error "Error (org-e-odt): Cannot find factory styles files, aborting"))
187 styles-dir)
188 "Directory that holds auxiliary XML files used by the ODT exporter.
190 This directory contains the following XML files -
191 \"OrgOdtStyles.xml\" and \"OrgOdtContentTemplate.xml\". These
192 XML files are used as the default values of
193 `org-e-odt-styles-file' and
194 `org-e-odt-content-template-file'.
196 The default value of this variable varies depending on the
197 version of org in use and is initialized from
198 `org-e-odt-styles-dir-list'. Note that the user could be using org
199 from one of: org's own private git repository, GNU ELPA tar or
200 standard Emacs.")
202 (defconst org-e-odt-bookmark-prefix "OrgXref.")
204 (defconst org-e-odt-manifest-file-entry-tag
205 "\n<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
207 (defconst org-e-odt-file-extensions
208 '(("odt" . "OpenDocument Text")
209 ("ott" . "OpenDocument Text Template")
210 ("odm" . "OpenDocument Master Document")
211 ("ods" . "OpenDocument Spreadsheet")
212 ("ots" . "OpenDocument Spreadsheet Template")
213 ("odg" . "OpenDocument Drawing (Graphics)")
214 ("otg" . "OpenDocument Drawing Template")
215 ("odp" . "OpenDocument Presentation")
216 ("otp" . "OpenDocument Presentation Template")
217 ("odi" . "OpenDocument Image")
218 ("odf" . "OpenDocument Formula")
219 ("odc" . "OpenDocument Chart")))
221 (defconst org-e-odt-table-style-format
223 <style:style style:name=\"%s\" style:family=\"table\">
224 <style:table-properties style:rel-width=\"%d%%\" fo:margin-top=\"0cm\" fo:margin-bottom=\"0.20cm\" table:align=\"center\"/>
225 </style:style>
227 "Template for auto-generated Table styles.")
229 (defvar org-e-odt-automatic-styles '()
230 "Registry of automatic styles for various OBJECT-TYPEs.
231 The variable has the following form:
232 \(\(OBJECT-TYPE-A
233 \(\(OBJECT-NAME-A.1 OBJECT-PROPS-A.1\)
234 \(OBJECT-NAME-A.2 OBJECT-PROPS-A.2\) ...\)\)
235 \(OBJECT-TYPE-B
236 \(\(OBJECT-NAME-B.1 OBJECT-PROPS-B.1\)
237 \(OBJECT-NAME-B.2 OBJECT-PROPS-B.2\) ...\)\)
238 ...\).
240 OBJECT-TYPEs could be \"Section\", \"Table\", \"Figure\" etc.
241 OBJECT-PROPS is (typically) a plist created by passing
242 \"#+ATTR_ODT: \" option to `org-e-odt-parse-block-attributes'.
244 Use `org-e-odt-add-automatic-style' to add update this variable.'")
246 (defvar org-e-odt-object-counters nil
247 "Running counters for various OBJECT-TYPEs.
248 Use this to generate automatic names and style-names. See
249 `org-e-odt-add-automatic-style'.")
251 (defvar org-e-odt-src-block-paragraph-format
252 "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
253 <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
254 <style:background-image/>
255 </style:paragraph-properties>
256 <style:text-properties fo:color=\"%s\"/>
257 </style:style>"
258 "Custom paragraph style for colorized source and example blocks.
259 This style is much the same as that of \"OrgFixedWidthBlock\"
260 except that the foreground and background colors are set
261 according to the default face identified by the `htmlfontify'.")
263 (defvar hfy-optimisations)
264 (defvar org-e-odt-embedded-formulas-count 0)
265 (defvar org-e-odt-embedded-images-count 0)
266 (defvar org-e-odt-image-size-probe-method
267 (append (and (executable-find "identify") '(imagemagick)) ; See Bug#10675
268 '(emacs fixed))
269 "Ordered list of methods for determining image sizes.")
271 (defvar org-e-odt-default-image-sizes-alist
272 '(("as-char" . (5 . 0.4))
273 ("paragraph" . (5 . 5)))
274 "Hardcoded image dimensions one for each of the anchor
275 methods.")
277 ;; A4 page size is 21.0 by 29.7 cms
278 ;; The default page settings has 2cm margin on each of the sides. So
279 ;; the effective text area is 17.0 by 25.7 cm
280 (defvar org-e-odt-max-image-size '(17.0 . 20.0)
281 "Limiting dimensions for an embedded image.")
283 (defconst org-e-odt-label-styles
284 '(("math-formula" "%c" "text" "(%n)")
285 ("math-label" "(%n)" "text" "(%n)")
286 ("category-and-value" "%e %n: %c" "category-and-value" "%e %n")
287 ("value" "%e %n: %c" "value" "%n"))
288 "Specify how labels are applied and referenced.
289 This is an alist where each element is of the
290 form (LABEL-STYLE-NAME LABEL-ATTACH-FMT LABEL-REF-MODE
291 LABEL-REF-FMT).
293 LABEL-ATTACH-FMT controls how labels and captions are attached to
294 an entity. It may contain following specifiers - %e, %n and %c.
295 %e is replaced with the CATEGORY-NAME. %n is replaced with
296 \"<text:sequence ...> SEQNO </text:sequence>\". %c is replaced
297 with CAPTION. See `org-e-odt-format-label-definition'.
299 LABEL-REF-MODE and LABEL-REF-FMT controls how label references
300 are generated. The following XML is generated for a label
301 reference - \"<text:sequence-ref
302 text:reference-format=\"LABEL-REF-MODE\" ...> LABEL-REF-FMT
303 </text:sequence-ref>\". LABEL-REF-FMT may contain following
304 specifiers - %e and %n. %e is replaced with the CATEGORY-NAME.
305 %n is replaced with SEQNO. See
306 `org-e-odt-format-label-reference'.")
308 (defvar org-e-odt-category-map-alist
309 '(("__Table__" "Table" "value" "Table" org-e-odt--enumerable-p)
310 ("__Figure__" "Illustration" "value" "Figure" org-e-odt--enumerable-image-p)
311 ("__MathFormula__" "Text" "math-formula" "Equation" org-e-odt--enumerable-formula-p)
312 ("__DvipngImage__" "Equation" "value" "Equation" org-e-odt--enumerable-latex-image-p)
313 ("__Listing__" "Listing" "value" "Listing" org-e-odt--enumerable-p)
314 ;; ("__Table__" "Table" "category-and-value")
315 ;; ("__Figure__" "Figure" "category-and-value")
316 ;; ("__DvipngImage__" "Equation" "category-and-value")
318 "Map a CATEGORY-HANDLE to OD-VARIABLE and LABEL-STYLE.
319 This is a list where each entry is of the form \\(CATEGORY-HANDLE
320 OD-VARIABLE LABEL-STYLE CATEGORY-NAME ENUMERATOR-PREDICATE\\).
322 CATEGORY_HANDLE identifies the captionable entity in question.
324 OD-VARIABLE is the OpenDocument sequence counter associated with
325 the entity. These counters are declared within
326 \"<text:sequence-decls>...</text:sequence-decls>\" block of
327 `org-e-odt-content-template-file'.
329 LABEL-STYLE is a key into `org-e-odt-label-styles' and specifies
330 how a given entity should be captioned and referenced.
332 CATEGORY-NAME is used for qualifying captions on export. You can
333 modify the CATEGORY-NAME used in the exported document by
334 modifying `org-export-dictionary'. For example, an embedded
335 image in an English document is captioned as \"Figure 1: Orgmode
336 Logo\", by default. If you want the image to be captioned as
337 \"Illustration 1: Orgmode Logo\" instead, install an entry in
338 `org-export-dictionary' which translates \"Figure\" to
339 \"Illustration\" when the language is \"en\" and encoding is
340 `:utf-8'.
342 ENUMERATOR-PREDICATE is used for assigning a sequence number to
343 the entity. See `org-e-odt--enumerate'.")
345 (defvar org-e-odt-manifest-file-entries nil)
346 (defvar hfy-user-sheet-assoc)
348 (defvar org-e-odt-zip-dir nil
349 "Temporary work directory for OpenDocument exporter.")
353 ;;; User Configuration Variables
355 (defgroup org-export-e-odt nil
356 "Options for exporting Org mode files to ODT."
357 :tag "Org Export ODT"
358 :group 'org-export)
361 ;;;; Debugging
363 (defcustom org-e-odt-prettify-xml nil
364 "Specify whether or not the xml output should be prettified.
365 When this option is turned on, `indent-region' is run on all
366 component xml buffers before they are saved. Turn this off for
367 regular use. Turn this on if you need to examine the xml
368 visually."
369 :group 'org-export-e-odt
370 :version "24.1"
371 :type 'boolean)
374 ;;;; Document schema
376 (defcustom org-e-odt-schema-dir
377 (let* ((schema-dir
378 (catch 'schema-dir
379 (message "Debug (org-e-odt): Searching for OpenDocument schema files...")
380 (mapc
381 (lambda (schema-dir)
382 (when schema-dir
383 (message "Debug (org-e-odt): Trying %s..." schema-dir)
384 (when (and (file-readable-p
385 (expand-file-name "od-manifest-schema-v1.2-cs01.rnc"
386 schema-dir))
387 (file-readable-p
388 (expand-file-name "od-schema-v1.2-cs01.rnc"
389 schema-dir))
390 (file-readable-p
391 (expand-file-name "schemas.xml" schema-dir)))
392 (message "Debug (org-e-odt): Using schema files under %s"
393 schema-dir)
394 (throw 'schema-dir schema-dir))))
395 org-e-odt-schema-dir-list)
396 (message "Debug (org-e-odt): No OpenDocument schema files installed")
397 nil)))
398 schema-dir)
399 "Directory that contains OpenDocument schema files.
401 This directory contains:
402 1. rnc files for OpenDocument schema
403 2. a \"schemas.xml\" file that specifies locating rules needed
404 for auto validation of OpenDocument XML files.
406 Use the customize interface to set this variable. This ensures
407 that `rng-schema-locating-files' is updated and auto-validation
408 of OpenDocument XML takes place based on the value
409 `rng-nxml-auto-validate-flag'.
411 The default value of this variable varies depending on the
412 version of org in use and is initialized from
413 `org-e-odt-schema-dir-list'. The OASIS schema files are available
414 only in the org's private git repository. It is *not* bundled
415 with GNU ELPA tar or standard Emacs distribution."
416 :type '(choice
417 (const :tag "Not set" nil)
418 (directory :tag "Schema directory"))
419 :group 'org-export-e-odt
420 :version "24.1"
421 :set
422 (lambda (var value)
423 "Set `org-e-odt-schema-dir'.
424 Also add it to `rng-schema-locating-files'."
425 (let ((schema-dir value))
426 (set var
427 (if (and
428 (file-readable-p
429 (expand-file-name "od-manifest-schema-v1.2-cs01.rnc" schema-dir))
430 (file-readable-p
431 (expand-file-name "od-schema-v1.2-cs01.rnc" schema-dir))
432 (file-readable-p
433 (expand-file-name "schemas.xml" schema-dir)))
434 schema-dir
435 (when value
436 (message "Error (org-e-odt): %s has no OpenDocument schema files"
437 value))
438 nil)))
439 (when org-e-odt-schema-dir
440 (eval-after-load 'rng-loc
441 '(add-to-list 'rng-schema-locating-files
442 (expand-file-name "schemas.xml"
443 org-e-odt-schema-dir))))))
446 ;;;; Document styles
448 (defcustom org-e-odt-content-template-file nil
449 "Template file for \"content.xml\".
450 The exporter embeds the exported content just before
451 \"</office:text>\" element.
453 If unspecified, the file named \"OrgOdtContentTemplate.xml\"
454 under `org-e-odt-styles-dir' is used."
455 :type 'file
456 :group 'org-export-e-odt
457 :version "24.1")
459 (defcustom org-e-odt-styles-file nil
460 "Default styles file for use with ODT export.
461 Valid values are one of:
462 1. nil
463 2. path to a styles.xml file
464 3. path to a *.odt or a *.ott file
465 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
466 ...))
468 In case of option 1, an in-built styles.xml is used. See
469 `org-e-odt-styles-dir' for more information.
471 In case of option 3, the specified file is unzipped and the
472 styles.xml embedded therein is used.
474 In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
475 and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
476 generated odt file. Use relative path for specifying the
477 FILE-MEMBERS. styles.xml must be specified as one of the
478 FILE-MEMBERS.
480 Use options 1, 2 or 3 only if styles.xml alone suffices for
481 achieving the desired formatting. Use option 4, if the styles.xml
482 references additional files like header and footer images for
483 achieving the desired formatting.
485 Use \"#+ODT_STYLES_FILE: ...\" directive to set this variable on
486 a per-file basis. For example,
488 #+ODT_STYLES_FILE: \"/path/to/styles.xml\" or
489 #+ODT_STYLES_FILE: (\"/path/to/file.ott\" (\"styles.xml\" \"image/hdr.png\"))."
490 :group 'org-export-e-odt
491 :version "24.1"
492 :type
493 '(choice
494 (const :tag "Factory settings" nil)
495 (file :must-match t :tag "styles.xml")
496 (file :must-match t :tag "ODT or OTT file")
497 (list :tag "ODT or OTT file + Members"
498 (file :must-match t :tag "ODF Text or Text Template file")
499 (cons :tag "Members"
500 (file :tag " Member" "styles.xml")
501 (repeat (file :tag "Member"))))))
503 (defcustom org-e-odt-display-outline-level 2
504 "Outline levels considered for enumerating captioned entities."
505 :group 'org-export-e-odt
506 :version "24.2"
507 :type 'integer)
509 ;;;; Document conversion
511 (defcustom org-e-odt-convert-processes
512 '(("LibreOffice"
513 "soffice --headless --convert-to %f%x --outdir %d %i")
514 ("unoconv"
515 "unoconv -f %f -o %d %i"))
516 "Specify a list of document converters and their usage.
517 The converters in this list are offered as choices while
518 customizing `org-e-odt-convert-process'.
520 This variable is a list where each element is of the
521 form (CONVERTER-NAME CONVERTER-CMD). CONVERTER-NAME is the name
522 of the converter. CONVERTER-CMD is the shell command for the
523 converter and can contain format specifiers. These format
524 specifiers are interpreted as below:
526 %i input file name in full
527 %I input file name as a URL
528 %f format of the output file
529 %o output file name in full
530 %O output file name as a URL
531 %d output dir in full
532 %D output dir as a URL.
533 %x extra options as set in `org-e-odt-convert-capabilities'."
534 :group 'org-export-e-odt
535 :version "24.1"
536 :type
537 '(choice
538 (const :tag "None" nil)
539 (alist :tag "Converters"
540 :key-type (string :tag "Converter Name")
541 :value-type (group (string :tag "Command line")))))
543 (defcustom org-e-odt-convert-process "LibreOffice"
544 "Use this converter to convert from \"odt\" format to other formats.
545 During customization, the list of converter names are populated
546 from `org-e-odt-convert-processes'."
547 :group 'org-export-e-odt
548 :version "24.1"
549 :type '(choice :convert-widget
550 (lambda (w)
551 (apply 'widget-convert (widget-type w)
552 (eval (car (widget-get w :args)))))
553 `((const :tag "None" nil)
554 ,@(mapcar (lambda (c)
555 `(const :tag ,(car c) ,(car c)))
556 org-e-odt-convert-processes))))
558 (defcustom org-e-odt-convert-capabilities
559 '(("Text"
560 ("odt" "ott" "doc" "rtf" "docx")
561 (("pdf" "pdf") ("odt" "odt") ("rtf" "rtf") ("ott" "ott")
562 ("doc" "doc" ":\"MS Word 97\"") ("docx" "docx") ("html" "html")))
563 ("Web"
564 ("html")
565 (("pdf" "pdf") ("odt" "odt") ("html" "html")))
566 ("Spreadsheet"
567 ("ods" "ots" "xls" "csv" "xlsx")
568 (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv") ("ods" "ods")
569 ("xls" "xls") ("xlsx" "xlsx")))
570 ("Presentation"
571 ("odp" "otp" "ppt" "pptx")
572 (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("otp" "otp") ("ppt" "ppt")
573 ("pptx" "pptx") ("odg" "odg"))))
574 "Specify input and output formats of `org-e-odt-convert-process'.
575 More correctly, specify the set of input and output formats that
576 the user is actually interested in.
578 This variable is an alist where each element is of the
579 form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
580 INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
581 alist where each element is of the form (OUTPUT-FMT
582 OUTPUT-FILE-EXTENSION EXTRA-OPTIONS).
584 The variable is interpreted as follows:
585 `org-e-odt-convert-process' can take any document that is in
586 INPUT-FMT-LIST and produce any document that is in the
587 OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
588 OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
589 serves dual purposes:
590 - It is used for populating completion candidates during
591 `org-e-odt-convert' commands.
592 - It is used as the value of \"%f\" specifier in
593 `org-e-odt-convert-process'.
595 EXTRA-OPTIONS is used as the value of \"%x\" specifier in
596 `org-e-odt-convert-process'.
598 DOCUMENT-CLASS is used to group a set of file formats in
599 INPUT-FMT-LIST in to a single class.
601 Note that this variable inherently captures how LibreOffice based
602 converters work. LibreOffice maps documents of various formats
603 to classes like Text, Web, Spreadsheet, Presentation etc and
604 allow document of a given class (irrespective of it's source
605 format) to be converted to any of the export formats associated
606 with that class.
608 See default setting of this variable for an typical
609 configuration."
610 :group 'org-export-e-odt
611 :version "24.1"
612 :type
613 '(choice
614 (const :tag "None" nil)
615 (alist :tag "Capabilities"
616 :key-type (string :tag "Document Class")
617 :value-type
618 (group (repeat :tag "Input formats" (string :tag "Input format"))
619 (alist :tag "Output formats"
620 :key-type (string :tag "Output format")
621 :value-type
622 (group (string :tag "Output file extension")
623 (choice
624 (const :tag "None" nil)
625 (string :tag "Extra options"))))))))
627 (defcustom org-e-odt-preferred-output-format nil
628 "Automatically post-process to this format after exporting to \"odt\".
629 Command `org-e-odt-export-to-odt' exports first to \"odt\" format
630 and then uses `org-e-odt-convert-process' to convert the
631 resulting document to this format. During customization of this
632 variable, the list of valid values are populated based on
633 `org-e-odt-convert-capabilities'.
635 You can set this option on per-file basis using file local
636 values. See Info node `(emacs) File Variables'."
637 :group 'org-export-e-odt
638 :version "24.1"
639 :type '(choice :convert-widget
640 (lambda (w)
641 (apply 'widget-convert (widget-type w)
642 (eval (car (widget-get w :args)))))
643 `((const :tag "None" nil)
644 ,@(mapcar (lambda (c)
645 `(const :tag ,c ,c))
646 (org-e-odt-reachable-formats "odt")))))
647 ;;;###autoload
648 (put 'org-e-odt-preferred-output-format 'safe-local-variable 'stringp)
651 ;;;; Drawers
653 (defcustom org-e-odt-format-drawer-function nil
654 "Function called to format a drawer in HTML code.
656 The function must accept two parameters:
657 NAME the drawer name, like \"LOGBOOK\"
658 CONTENTS the contents of the drawer.
660 The function should return the string to be exported.
662 For example, the variable could be set to the following function
663 in order to mimic default behaviour:
665 \(defun org-e-odt-format-drawer-default \(name contents\)
666 \"Format a drawer element for HTML export.\"
667 contents\)"
668 :group 'org-export-e-odt
669 :type 'function)
672 ;;;; Headline
674 (defcustom org-e-odt-format-headline-function nil
675 "Function to format headline text.
677 This function will be called with 5 arguments:
678 TODO the todo keyword \(string or nil\).
679 TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
680 PRIORITY the priority of the headline \(integer or nil\)
681 TEXT the main headline text \(string\).
682 TAGS the tags string, separated with colons \(string or nil\).
684 The function result will be used in the section format string.
686 As an example, one could set the variable to the following, in
687 order to reproduce the default set-up:
689 \(defun org-e-odt-format-headline \(todo todo-type priority text tags\)
690 \"Default format function for an headline.\"
691 \(concat \(when todo
692 \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo\)\)
693 \(when priority
694 \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
695 text
696 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
697 :group 'org-export-e-odt
698 :type 'function)
701 ;;;; Inlinetasks
703 (defcustom org-e-odt-format-inlinetask-function nil
704 "Function called to format an inlinetask in HTML code.
706 The function must accept six parameters:
707 TODO the todo keyword, as a string
708 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
709 PRIORITY the inlinetask priority, as a string
710 NAME the inlinetask name, as a string.
711 TAGS the inlinetask tags, as a string.
712 CONTENTS the contents of the inlinetask, as a string.
714 The function should return the string to be exported.
716 For example, the variable could be set to the following function
717 in order to mimic default behaviour:
719 \(defun org-e-odt-format-inlinetask \(todo type priority name tags contents\)
720 \"Format an inline task element for HTML export.\"
721 \(let \(\(full-title
722 \(concat
723 \(when todo
724 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
725 \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
726 title
727 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
728 \(format \(concat \"\\\\begin{center}\\n\"
729 \"\\\\fbox{\\n\"
730 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
731 \"%s\\n\\n\"
732 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
733 \"%s\"
734 \"\\\\end{minipage}}\"
735 \"\\\\end{center}\"\)
736 full-title contents\)\)"
737 :group 'org-export-e-odt
738 :type 'function)
741 ;;;; Links
743 (defcustom org-e-odt-inline-formula-rules
744 '(("file" . "\\.\\(mathml\\|mml\\)\\'"))
745 "Rules characterizing formula files that can be inlined into HTML.
747 A rule consists in an association whose key is the type of link
748 to consider, and value is a regexp that will be matched against
749 link's path."
750 :group 'org-export-e-odt
751 :type '(alist :key-type (string :tag "Type")
752 :value-type (regexp :tag "Path")))
754 (defcustom org-e-odt-inline-image-rules
755 '(("file" . "\\.\\(jpeg\\|jpg\\|png\\|gif\\)\\'"))
756 "Rules characterizing image files that can be inlined into HTML.
758 A rule consists in an association whose key is the type of link
759 to consider, and value is a regexp that will be matched against
760 link's path."
761 :group 'org-export-e-odt
762 :type '(alist :key-type (string :tag "Type")
763 :value-type (regexp :tag "Path")))
765 (defcustom org-e-odt-pixels-per-inch display-pixels-per-inch
766 "Scaling factor for converting images pixels to inches.
767 Use this for sizing of embedded images. See Info node `(org)
768 Images in ODT export' for more information."
769 :type 'float
770 :group 'org-export-e-odt
771 :version "24.1")
774 ;;;; Src Block
776 (defcustom org-e-odt-create-custom-styles-for-srcblocks t
777 "Whether custom styles for colorized source blocks be automatically created.
778 When this option is turned on, the exporter creates custom styles
779 for source blocks based on the advice of `htmlfontify'. Creation
780 of custom styles happen as part of `org-e-odt-hfy-face-to-css'.
782 When this option is turned off exporter does not create such
783 styles.
785 Use the latter option if you do not want the custom styles to be
786 based on your current display settings. It is necessary that the
787 styles.xml already contains needed styles for colorizing to work.
789 This variable is effective only if
790 `org-e-odt-fontify-srcblocks' is turned on."
791 :group 'org-export-e-odt
792 :version "24.1"
793 :type 'boolean)
795 (defcustom org-e-odt-fontify-srcblocks t
796 "Specify whether or not source blocks need to be fontified.
797 Turn this option on if you want to colorize the source code
798 blocks in the exported file. For colorization to work, you need
799 to make available an enhanced version of `htmlfontify' library."
800 :type 'boolean
801 :group 'org-export-e-odt
802 :version "24.1")
805 ;;;; Table
807 (defcustom org-e-odt-table-caption-above t
808 "When non-nil, place caption string at the beginning of the table.
809 Otherwise, place it near the end."
810 :group 'org-export-e-odt
811 :type 'boolean)
813 (defcustom org-e-odt-table-styles
814 '(("OrgEquation" "OrgEquation"
815 ((use-first-column-styles . t)
816 (use-last-column-styles . t)))
817 ("TableWithHeaderRowAndColumn" "Custom"
818 ((use-first-row-styles . t)
819 (use-first-column-styles . t)))
820 ("TableWithFirstRowandLastRow" "Custom"
821 ((use-first-row-styles . t)
822 (use-last-row-styles . t)))
823 ("GriddedTable" "Custom" nil))
824 "Specify how Table Styles should be derived from a Table Template.
825 This is a list where each element is of the
826 form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
828 TABLE-STYLE-NAME is the style associated with the table through
829 \"#+ATTR_ODT: :style TABLE-STYLE-NAME\" line.
831 TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
832 TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
833 below) that is included in
834 `org-e-odt-content-template-file'.
836 TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
837 \"TableCell\"
838 PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
839 \"TableParagraph\"
840 TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
841 \"FirstRow\" | \"LastRow\" |
842 \"EvenRow\" | \"OddRow\" |
843 \"EvenColumn\" | \"OddColumn\" | \"\"
844 where \"+\" above denotes string concatenation.
846 TABLE-CELL-OPTIONS is an alist where each element is of the
847 form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
848 TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
849 `use-last-row-styles' |
850 `use-first-column-styles' |
851 `use-last-column-styles' |
852 `use-banding-rows-styles' |
853 `use-banding-columns-styles' |
854 `use-first-row-styles'
855 ON-OR-OFF := `t' | `nil'
857 For example, with the following configuration
859 \(setq org-e-odt-table-styles
860 '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
861 \(\(use-first-row-styles . t\)
862 \(use-first-column-styles . t\)\)\)
863 \(\"TableWithHeaderColumns\" \"Custom\"
864 \(\(use-first-column-styles . t\)\)\)\)\)
866 1. A table associated with \"TableWithHeaderRowsAndColumns\"
867 style will use the following table-cell styles -
868 \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
869 \"CustomTableCell\" and the following paragraph styles
870 \"CustomFirstRowTableParagraph\",
871 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
872 as appropriate.
874 2. A table associated with \"TableWithHeaderColumns\" style will
875 use the following table-cell styles -
876 \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
877 following paragraph styles
878 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
879 as appropriate..
881 Note that TABLE-TEMPLATE-NAME corresponds to the
882 \"<table:table-template>\" elements contained within
883 \"<office:styles>\". The entries (TABLE-STYLE-NAME
884 TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
885 \"table:template-name\" and \"table:use-first-row-styles\" etc
886 attributes of \"<table:table>\" element. Refer ODF-1.2
887 specification for more information. Also consult the
888 implementation filed under `org-e-odt-get-table-cell-styles'.
890 The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
891 formatting of numbered display equations. Do not delete this
892 style from the list."
893 :group 'org-export-e-odt
894 :version "24.1"
895 :type '(choice
896 (const :tag "None" nil)
897 (repeat :tag "Table Styles"
898 (list :tag "Table Style Specification"
899 (string :tag "Table Style Name")
900 (string :tag "Table Template Name")
901 (alist :options (use-first-row-styles
902 use-last-row-styles
903 use-first-column-styles
904 use-last-column-styles
905 use-banding-rows-styles
906 use-banding-columns-styles)
907 :key-type symbol
908 :value-type (const :tag "True" t))))))
912 ;;; Internal functions
914 ;;;; Date
916 (defun org-e-odt--date (&optional org-ts fmt)
917 (save-match-data
918 (let* ((time
919 (and (stringp org-ts)
920 (string-match org-ts-regexp0 org-ts)
921 (apply 'encode-time
922 (org-fix-decoded-time
923 (org-parse-time-string (match-string 0 org-ts) t)))))
924 date)
925 (cond
926 (fmt (format-time-string fmt time))
927 (t (setq date (format-time-string "%Y-%m-%dT%H:%M:%S%z" time))
928 (format "%s:%s" (substring date 0 -2) (substring date -2)))))))
930 ;;;; Frame
932 (defun org-e-odt--frame (text width height style &optional extra
933 anchor-type)
934 (let ((frame-attrs
935 (concat
936 (if width (format " svg:width=\"%0.2fcm\"" width) "")
937 (if height (format " svg:height=\"%0.2fcm\"" height) "")
938 extra
939 (format " text:anchor-type=\"%s\"" (or anchor-type "paragraph")))))
940 (format
941 "\n<draw:frame draw:style-name=\"%s\"%s>\n%s\n</draw:frame>"
942 style frame-attrs
943 (concat text
944 (let ((title (get-text-property 0 :title text))
945 (desc (get-text-property 0 :description text)))
946 (concat (and title
947 (format "<svg:title>%s</svg:title>"
948 (org-e-odt--encode-plain-text title t)))
949 (and desc
950 (format "<svg:desc>%s</svg:desc>"
951 (org-e-odt--encode-plain-text desc t)))))))))
954 ;;;; Library wrappers
956 (defun org-e-odt--zip-extract (archive members target)
957 (when (atom members) (setq members (list members)))
958 (mapc (lambda (archive member target)
959 (require 'arc-mode)
960 (let* ((--quote-file-name
961 ;; This is shamelessly stolen from `archive-zip-extract'.
962 (lambda (name)
963 (if (or (not (memq system-type '(windows-nt ms-dos)))
964 (and (boundp 'w32-quote-process-args)
965 (null w32-quote-process-args)))
966 (shell-quote-argument name)
967 name)))
968 (target (funcall --quote-file-name target))
969 (archive (expand-file-name archive))
970 (archive-zip-extract
971 (list "unzip" "-qq" "-o" "-d" target))
972 exit-code command-output)
973 (setq command-output
974 (with-temp-buffer
975 (setq exit-code (archive-zip-extract archive member))
976 (buffer-string)))
977 (unless (zerop exit-code)
978 (message command-output)
979 (error "Extraction failed"))))
980 members))
983 ;;;; Target
985 (defun org-e-odt--target (text id)
986 (if (not id) text
987 (concat
988 (format "\n<text:bookmark-start text:name=\"OrgXref.%s\"/>" id)
989 (format "\n<text:bookmark text:name=\"%s\"/>" id) text
990 (format "\n<text:bookmark-end text:name=\"OrgXref.%s\"/>" id))))
992 ;;;; Textbox
994 (defun org-e-odt--textbox (text width height style &optional
995 extra anchor-type)
996 (org-e-odt--frame
997 (format "\n<draw:text-box %s>%s\n</draw:text-box>"
998 (concat (format " fo:min-height=\"%0.2fcm\"" (or height .2))
999 (and (not width)
1000 (format " fo:min-width=\"%0.2fcm\"" (or width .2))))
1001 text)
1002 width nil style extra anchor-type))
1006 ;;;; Table of Contents
1008 (defun org-e-odt-begin-toc (index-title depth)
1009 (concat
1010 (format "
1011 <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
1012 <text:table-of-content-source text:outline-level=\"%d\">
1013 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
1014 " depth index-title)
1016 (let ((levels (number-sequence 1 10)))
1017 (mapconcat
1018 (lambda (level)
1019 (format
1021 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
1022 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
1023 <text:index-entry-chapter/>
1024 <text:index-entry-text/>
1025 <text:index-entry-link-end/>
1026 </text:table-of-content-entry-template>
1027 " level level)) levels ""))
1029 (format "
1030 </text:table-of-content-source>
1032 <text:index-body>
1033 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
1034 <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
1035 </text:index-title>
1036 " index-title)))
1038 (defun org-e-odt-end-toc ()
1039 (format "
1040 </text:index-body>
1041 </text:table-of-content>
1046 (defun* org-e-odt-format-toc-headline
1047 (todo todo-type priority text tags
1048 &key level section-number headline-label &allow-other-keys)
1049 (setq text (concat
1050 (and org-export-with-section-numbers
1051 (concat section-number ". "))
1052 text
1053 (and tags
1054 (concat
1055 "<text:tab/>"
1056 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1057 "OrgTag" tags)))))
1058 (when todo
1059 (setq text (format "<text:span text:style-name=\"%s\">%s</text:span>"
1060 "OrgTodo" text)))
1061 (format "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
1062 headline-label text))
1064 (defun org-e-odt-toc (depth info)
1065 (assert (wholenump depth))
1066 (let* ((title (org-export-translate "Table of Contents" :utf-8 info))
1067 (headlines (org-export-collect-headlines
1068 info (and (wholenump depth) depth))))
1069 (when headlines
1070 (concat
1071 (org-e-odt-begin-toc title depth)
1072 (mapconcat
1073 (lambda (headline)
1074 (let* ((entry (org-e-odt-format-headline--wrap
1075 headline info 'org-e-odt-format-toc-headline))
1076 (level (org-export-get-relative-level headline info))
1077 (style (format "Contents_20_%d" level)))
1078 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1079 style entry)))
1080 headlines "\n")
1081 (org-e-odt-end-toc)))))
1084 ;;;; Document styles
1086 (defun org-e-odt-add-automatic-style (object-type &optional object-props)
1087 "Create an automatic style of type OBJECT-TYPE with param OBJECT-PROPS.
1088 OBJECT-PROPS is (typically) a plist created by passing
1089 \"#+ATTR_ODT: \" option of the object in question to
1090 `org-e-odt-parse-block-attributes'.
1092 Use `org-e-odt-object-counters' to generate an automatic
1093 OBJECT-NAME and STYLE-NAME. If OBJECT-PROPS is non-nil, add a
1094 new entry in `org-e-odt-automatic-styles'. Return (OBJECT-NAME
1095 . STYLE-NAME)."
1096 (assert (stringp object-type))
1097 (let* ((object (intern object-type))
1098 (seqvar object)
1099 (seqno (1+ (or (plist-get org-e-odt-object-counters seqvar) 0)))
1100 (object-name (format "%s%d" object-type seqno)) style-name)
1101 (setq org-e-odt-object-counters
1102 (plist-put org-e-odt-object-counters seqvar seqno))
1103 (when object-props
1104 (setq style-name (format "Org%s" object-name))
1105 (setq org-e-odt-automatic-styles
1106 (plist-put org-e-odt-automatic-styles object
1107 (append (list (list style-name object-props))
1108 (plist-get org-e-odt-automatic-styles object)))))
1109 (cons object-name style-name)))
1111 ;;;; Checkbox
1113 (defun org-e-odt--checkbox (item)
1114 "Return check-box string associated to ITEM."
1115 (let ((checkbox (org-element-property :checkbox item)))
1116 (if (not checkbox) ""
1117 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1118 "OrgCode" (case checkbox
1119 (on "[&#x2713;] ") ; CHECK MARK
1120 (off "[ ] ")
1121 (trans "[-] "))))))
1123 ;;; Template
1125 (defun org-e-odt-template (contents info)
1126 "Return complete document string after HTML conversion.
1127 CONTENTS is the transcoded contents string. RAW-DATA is the
1128 original parsed data. INFO is a plist holding export options."
1129 ;; Write meta file.
1130 (let ((title (org-export-data (plist-get info :title) info))
1131 (author (let ((author (plist-get info :author)))
1132 (if (not author) "" (org-export-data author info))))
1133 (date (org-e-odt--date
1134 (org-export-data (plist-get info :date) info)))
1135 (email (plist-get info :email))
1136 (keywords (plist-get info :keywords))
1137 (description (plist-get info :description)))
1138 (write-region
1139 (concat
1140 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
1141 <office:document-meta
1142 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
1143 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
1144 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
1145 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
1146 xmlns:ooo=\"http://openoffice.org/2004/office\"
1147 office:version=\"1.2\">
1148 <office:meta>\n"
1149 (format "<dc:creator>%s</dc:creator>\n" author)
1150 (format "<meta:initial-creator>%s</meta:initial-creator>\n" author)
1151 (format "<dc:date>%s</dc:date>\n" date)
1152 (format "<meta:creation-date>%s</meta:creation-date>\n" date)
1153 (format "<meta:generator>%s</meta:generator>\n"
1154 (let ((creator-info (plist-get info :with-creator)))
1155 (if (or (not creator-info) (eq creator-info 'comment)) ""
1156 (plist-get info :creator))))
1157 (format "<meta:keyword>%s</meta:keyword>\n" keywords)
1158 (format "<dc:subject>%s</dc:subject>\n" description)
1159 (format "<dc:title>%s</dc:title>\n" title)
1160 "\n"
1161 " </office:meta>\n" "</office:document-meta>")
1162 nil (concat org-e-odt-zip-dir "meta.xml"))
1163 ;; Add meta.xml in to manifest.
1164 (org-e-odt-create-manifest-file-entry "text/xml" "meta.xml"))
1166 ;; Update styles file.
1167 ;; Copy styles.xml. Also dump htmlfontify styles, if there is any.
1168 ;; Write styles file.
1169 (let* ((styles-file (plist-get info :odt-styles-file))
1170 (styles-file (and styles-file (read (org-trim styles-file))))
1171 ;; Non-availability of styles.xml is not a critical
1172 ;; error. For now throw an error purely for aesthetic
1173 ;; reasons.
1174 (styles-file (or styles-file
1175 org-e-odt-styles-file
1176 (expand-file-name "OrgOdtStyles.xml"
1177 org-e-odt-styles-dir)
1178 (error "org-e-odt: Missing styles file?"))))
1179 (cond
1180 ((listp styles-file)
1181 (let ((archive (nth 0 styles-file))
1182 (members (nth 1 styles-file)))
1183 (org-e-odt--zip-extract archive members org-e-odt-zip-dir)
1184 (mapc
1185 (lambda (member)
1186 (when (org-file-image-p member)
1187 (let* ((image-type (file-name-extension member))
1188 (media-type (format "image/%s" image-type)))
1189 (org-e-odt-create-manifest-file-entry media-type member))))
1190 members)))
1191 ((and (stringp styles-file) (file-exists-p styles-file))
1192 (let ((styles-file-type (file-name-extension styles-file)))
1193 (cond
1194 ((string= styles-file-type "xml")
1195 (copy-file styles-file (concat org-e-odt-zip-dir "styles.xml") t))
1196 ((member styles-file-type '("odt" "ott"))
1197 (org-e-odt--zip-extract styles-file "styles.xml" org-e-odt-zip-dir)))))
1199 (error (format "Invalid specification of styles.xml file: %S"
1200 org-e-odt-styles-file))))
1202 ;; create a manifest entry for styles.xml
1203 (org-e-odt-create-manifest-file-entry "text/xml" "styles.xml")
1205 ;; FIXME: Who is opening an empty styles.xml before this point?
1206 (with-current-buffer
1207 (find-file-noselect (concat org-e-odt-zip-dir "styles.xml") t)
1208 (revert-buffer t t)
1210 ;; Write custom styles for source blocks
1211 ;; Save STYLES used for colorizing of source blocks.
1212 ;; Update styles.xml with styles that were collected as part of
1213 ;; `org-e-odt-hfy-face-to-css' callbacks.
1214 (let ((styles (mapconcat (lambda (style) (format " %s\n" (cddr style)))
1215 hfy-user-sheet-assoc "")))
1216 (when styles
1217 (goto-char (point-min))
1218 (when (re-search-forward "</office:styles>" nil t)
1219 (goto-char (match-beginning 0))
1220 (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n"))))
1222 ;; Update styles.xml - take care of outline numbering
1224 ;; Don't make automatic backup of styles.xml file. This setting
1225 ;; prevents the backed-up styles.xml file from being zipped in to
1226 ;; odt file. This is more of a hackish fix. Better alternative
1227 ;; would be to fix the zip command so that the output odt file
1228 ;; includes only the needed files and excludes any auto-generated
1229 ;; extra files like backups and auto-saves etc etc. Note that
1230 ;; currently the zip command zips up the entire temp directory so
1231 ;; that any auto-generated files created under the hood ends up in
1232 ;; the resulting odt file.
1233 (set (make-local-variable 'backup-inhibited) t)
1235 ;; Outline numbering is retained only upto LEVEL.
1236 ;; To disable outline numbering pass a LEVEL of 0.
1238 (goto-char (point-min))
1239 (let ((regex
1240 "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
1241 (replacement
1242 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1243 (while (re-search-forward regex nil t)
1244 (unless (let ((sec-num (plist-get info :section-numbers))
1245 (level (string-to-number (match-string 2))))
1246 (if (wholenump sec-num) (<= level sec-num) sec-num))
1247 (replace-match replacement t nil))))
1248 (save-buffer 0)))
1249 ;; Update content.xml.
1250 (with-temp-buffer
1251 (insert-file-contents
1252 (or org-e-odt-content-template-file
1253 (expand-file-name "OrgOdtContentTemplate.xml"
1254 org-e-odt-styles-dir)))
1255 ;; Write automatic styles.
1256 ;; - Position the cursor.
1257 (goto-char (point-min))
1258 (re-search-forward " </office:automatic-styles>" nil t)
1259 (goto-char (match-beginning 0))
1260 ;; - Dump automatic table styles
1261 (loop for (style-name props) in
1262 (plist-get org-e-odt-automatic-styles 'Table) do
1263 (when (setq props (or (plist-get props :rel-width) 96))
1264 (insert (format org-e-odt-table-style-format style-name props))))
1265 ;; Update display level.
1266 ;; - Remove existing sequence decls. Also position the cursor.
1267 (goto-char (point-min))
1268 (when (re-search-forward "<text:sequence-decls" nil t)
1269 (delete-region (match-beginning 0)
1270 (re-search-forward "</text:sequence-decls>" nil nil)))
1271 ;; Update sequence decls according to user preference.
1272 (insert
1273 (format
1274 "\n<text:sequence-decls>\n%s\n</text:sequence-decls>"
1275 (mapconcat
1276 (lambda (x)
1277 (format
1278 "<text:sequence-decl text:display-outline-level=\"%d\" text:name=\"%s\"/>"
1279 org-e-odt-display-outline-level (nth 1 x)))
1280 org-e-odt-category-map-alist "\n")))
1281 ;; Position the cursor to document body.
1282 (goto-char (point-min))
1283 (re-search-forward "</office:text>" nil nil)
1284 (goto-char (match-beginning 0))
1286 ;; Preamble - Title, Author, Date etc.
1287 (insert
1288 (let* ((title (org-export-data (plist-get info :title) info))
1289 (author (and (plist-get info :with-author)
1290 (let ((auth (plist-get info :author)))
1291 (and auth (org-export-data auth info)))))
1292 (date (org-export-data (plist-get info :date) info))
1293 (iso-date (org-e-odt--date date))
1294 (date (org-e-odt--date date "%d %b %Y"))
1295 (email (plist-get info :email))
1296 ;; switch on or off above vars based on user settings
1297 (author (and (plist-get info :with-author) (or author email)))
1298 ;; (date (and (plist-get info :time-stamp-file) date))
1299 (email (and (plist-get info :with-email) email)))
1300 (concat
1301 ;; title
1302 (when title
1303 (concat
1304 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1305 "OrgTitle" (format "\n<text:title>%s</text:title>" title))
1306 ;; separator
1307 "\n<text:p text:style-name=\"OrgTitle\"/>"))
1308 (cond
1309 ((and author (not email))
1310 ;; author only
1311 (concat
1312 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1313 "OrgSubtitle"
1314 (format "<text:initial-creator>%s</text:initial-creator>" author))
1315 ;; separator
1316 "\n<text:p text:style-name=\"OrgSubtitle\"/>"))
1317 ((and author email)
1318 ;; author and email
1319 (concat
1320 (format
1321 "\n<text:p text:style-name=\"%s\">%s</text:p>"
1322 "OrgSubtitle"
1323 (format
1324 "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
1325 (concat "mailto:" email)
1326 (format "<text:initial-creator>%s</text:initial-creator>" author)))
1327 ;; separator
1328 "\n<text:p text:style-name=\"OrgSubtitle\"/>")))
1329 ;; date
1330 (when date
1331 (concat
1332 (format
1333 "\n<text:p text:style-name=\"%s\">%s</text:p>"
1334 "OrgSubtitle"
1335 (format
1336 "\n<text:date style:data-style-name=\"%s\" text:date-value=\"%s\">%s</text:date>"
1338 "OrgDate" iso-date date))
1339 ;; separator
1340 "<text:p text:style-name=\"OrgSubtitle\"/>")))))
1341 ;; Table of Contents
1342 (let* ((with-toc (plist-get info :with-toc))
1343 (depth (and with-toc (if (wholenump with-toc)
1344 with-toc
1345 (plist-get info :headline-levels)))))
1346 (when depth (insert (or (org-e-odt-toc depth info) ""))))
1347 ;; Contents.
1348 (insert contents)
1349 ;; Return contents.
1350 (buffer-substring-no-properties (point-min) (point-max))))
1354 ;;; Transcode Functions
1356 ;;;; Bold
1358 (defun org-e-odt-bold (bold contents info)
1359 "Transcode BOLD from Org to ODT.
1360 CONTENTS is the text with bold markup. INFO is a plist holding
1361 contextual information."
1362 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1363 "Bold" contents))
1366 ;;;; Center Block
1368 (defun org-e-odt-center-block (center-block contents info)
1369 "Transcode a CENTER-BLOCK element from Org to ODT.
1370 CONTENTS holds the contents of the center block. INFO is a plist
1371 holding contextual information."
1372 contents)
1375 ;;;; Clock
1377 (defun org-e-odt-clock (clock contents info)
1378 "Transcode a CLOCK element from Org to ODT.
1379 CONTENTS is nil. INFO is a plist used as a communication
1380 channel."
1381 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1382 "OrgTimestampWrapper"
1383 (concat
1384 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1385 "OrgTimestampKeyword" org-clock-string)
1386 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1387 "OrgTimestamp"
1388 (concat
1389 (org-translate-time
1390 (org-element-property :raw-value
1391 (org-element-property :value clock)))
1392 (let ((time (org-element-property :duration clock)))
1393 (and time (format " (%s)" time))))))))
1396 ;;;; Code
1398 (defun org-e-odt-code (code contents info)
1399 "Transcode a CODE object from Org to ODT.
1400 CONTENTS is nil. INFO is a plist used as a communication
1401 channel."
1402 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1403 "OrgCode" (org-element-property :value code)))
1406 ;;;; Comment
1408 ;; Comments are ignored.
1411 ;;;; Comment Block
1413 ;; Comment Blocks are ignored.
1416 ;;;; Drawer
1418 (defun org-e-odt-drawer (drawer contents info)
1419 "Transcode a DRAWER element from Org to ODT.
1420 CONTENTS holds the contents of the block. INFO is a plist
1421 holding contextual information."
1422 (let* ((name (org-element-property :drawer-name drawer))
1423 (output (if (functionp org-e-odt-format-drawer-function)
1424 (funcall org-e-odt-format-drawer-function
1425 name contents)
1426 ;; If there's no user defined function: simply
1427 ;; display contents of the drawer.
1428 contents)))
1429 output))
1432 ;;;; Dynamic Block
1434 (defun org-e-odt-dynamic-block (dynamic-block contents info)
1435 "Transcode a DYNAMIC-BLOCK element from Org to ODT.
1436 CONTENTS holds the contents of the block. INFO is a plist
1437 holding contextual information. See `org-export-data'."
1438 contents)
1441 ;;;; Entity
1443 (defun org-e-odt-entity (entity contents info)
1444 "Transcode an ENTITY object from Org to ODT.
1445 CONTENTS are the definition itself. INFO is a plist holding
1446 contextual information."
1447 (org-element-property :utf-8 entity))
1450 ;;;; Example Block
1452 (defun org-e-odt-example-block (example-block contents info)
1453 "Transcode a EXAMPLE-BLOCK element from Org to ODT.
1454 CONTENTS is nil. INFO is a plist holding contextual information."
1455 (org-e-odt-format-code example-block info))
1458 ;;;; Export Snippet
1460 (defun org-e-odt-export-snippet (export-snippet contents info)
1461 "Transcode a EXPORT-SNIPPET object from Org to ODT.
1462 CONTENTS is nil. INFO is a plist holding contextual information."
1463 (when (eq (org-export-snippet-backend export-snippet) 'e-odt)
1464 (org-element-property :value export-snippet)))
1467 ;;;; Export Block
1469 (defun org-e-odt-export-block (export-block contents info)
1470 "Transcode a EXPORT-BLOCK element from Org to ODT.
1471 CONTENTS is nil. INFO is a plist holding contextual information."
1472 (when (string= (org-element-property :type export-block) "ODT")
1473 (org-remove-indentation (org-element-property :value export-block))))
1476 ;;;; Fixed Width
1478 (defun org-e-odt-fixed-width (fixed-width contents info)
1479 "Transcode a FIXED-WIDTH element from Org to ODT.
1480 CONTENTS is nil. INFO is a plist holding contextual information."
1481 (org-e-odt-do-format-code (org-element-property :value fixed-width)))
1484 ;;;; Footnote Definition
1486 ;; Footnote Definitions are ignored.
1489 ;;;; Footnote Reference
1491 (defun org-e-odt-footnote-reference (footnote-reference contents info)
1492 "Transcode a FOOTNOTE-REFERENCE element from Org to ODT.
1493 CONTENTS is nil. INFO is a plist holding contextual information."
1494 (let ((--format-footnote-definition
1495 (function
1496 (lambda (n def)
1497 (setq n (format "%d" n))
1498 (let ((id (concat "fn" n))
1499 (note-class "footnote")
1500 (par-style "Footnote"))
1501 (format
1502 "<text:note text:id=\"%s\" text:note-class=\"%s\">%s</text:note>"
1503 id note-class
1504 (concat
1505 (format "<text:note-citation>%s</text:note-citation>" n)
1506 (format "<text:note-body>%s</text:note-body>" def)))))))
1507 (--format-footnote-reference
1508 (function
1509 (lambda (n)
1510 (setq n (format "%d" n))
1511 (let ((note-class "footnote")
1512 (ref-format "text")
1513 (ref-name (concat "fn" n)))
1514 (format
1515 "<text:span text:style-name=\"%s\">%s</text:span>"
1516 "OrgSuperscript"
1517 (format "<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:note-ref>"
1518 note-class ref-format ref-name n)))))))
1519 (concat
1520 ;; Insert separator between two footnotes in a row.
1521 (let ((prev (org-export-get-previous-element footnote-reference info)))
1522 (and (eq (org-element-type prev) 'footnote-reference)
1523 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1524 "OrgSuperscript" ",")))
1525 ;; Trancode footnote reference.
1526 (let ((n (org-export-get-footnote-number footnote-reference info)))
1527 (cond
1528 ((not (org-export-footnote-first-reference-p footnote-reference info))
1529 (funcall --format-footnote-reference n))
1530 ;; Inline definitions are secondary strings.
1531 ;; Non-inline footnotes definitions are full Org data.
1533 (let* ((raw (org-export-get-footnote-definition footnote-reference
1534 info))
1535 (def (let ((def (org-trim (org-export-data raw info))))
1536 (if (eq (org-element-type raw) 'org-data) def
1537 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1538 "Footnote" def)))))
1539 (funcall --format-footnote-definition n def))))))))
1542 ;;;; Headline
1544 (defun* org-e-odt-format-headline
1545 (todo todo-type priority text tags
1546 &key level section-number headline-label &allow-other-keys)
1547 (concat
1548 ;; Todo.
1549 (and todo
1550 (concat
1551 (let ((style (if (member todo org-done-keywords) "OrgDone" "OrgTodo")))
1552 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1553 style todo)) " "))
1554 ;; Title.
1555 text
1556 ;; Tags.
1557 (and tags
1558 (concat "<text:tab/>"
1559 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1560 "OrgTag" (mapconcat 'org-trim tags " : "))))))
1562 (defun org-e-odt-format-headline--wrap (headline info
1563 &optional format-function
1564 &rest extra-keys)
1565 "Transcode an HEADLINE element from Org to ODT.
1566 CONTENTS holds the contents of the headline. INFO is a plist
1567 holding contextual information."
1568 (let* ((level (+ (org-export-get-relative-level headline info)))
1569 (headline-number (org-export-get-headline-number headline info))
1570 (section-number (and (org-export-numbered-headline-p headline info)
1571 (mapconcat 'number-to-string
1572 headline-number ".")))
1573 (todo (and (plist-get info :with-todo-keywords)
1574 (let ((todo (org-element-property :todo-keyword headline)))
1575 (and todo (org-export-data todo info)))))
1576 (todo-type (and todo (org-element-property :todo-type headline)))
1577 (priority (and (plist-get info :with-priority)
1578 (org-element-property :priority headline)))
1579 (text (org-export-data (org-element-property :title headline) info))
1580 (tags (and (plist-get info :with-tags)
1581 (org-export-get-tags headline info)))
1582 (headline-label (concat "sec-" (mapconcat 'number-to-string
1583 headline-number "-")))
1584 (format-function (cond
1585 ((functionp format-function) format-function)
1586 ((functionp org-e-odt-format-headline-function)
1587 (function*
1588 (lambda (todo todo-type priority text tags
1589 &allow-other-keys)
1590 (funcall org-e-odt-format-headline-function
1591 todo todo-type priority text tags))))
1592 (t 'org-e-odt-format-headline))))
1593 (apply format-function
1594 todo todo-type priority text tags
1595 :headline-label headline-label :level level
1596 :section-number section-number extra-keys)))
1598 (defun org-e-odt-headline (headline contents info)
1599 "Transcode an HEADLINE element from Org to ODT.
1600 CONTENTS holds the contents of the headline. INFO is a plist
1601 holding contextual information."
1602 ;; Case 1: This is a footnote section: ignore it.
1603 (unless (org-element-property :footnote-section-p headline)
1604 (let* ((text (org-export-data (org-element-property :title headline) info))
1605 ;; Create the headline text.
1606 (full-text (org-e-odt-format-headline--wrap headline info))
1607 ;; Get level relative to current parsed data.
1608 (level (org-export-get-relative-level headline info))
1609 ;; Get canonical label for the headline.
1610 (id (concat "sec-" (mapconcat 'number-to-string
1611 (org-export-get-headline-number
1612 headline info) "-")))
1613 ;; Get user-specified labels for the headline.
1614 (extra-ids (list (org-element-property :custom-id headline)
1615 (org-element-property :id headline)))
1616 ;; Extra targets.
1617 (extra-targets
1618 (mapconcat (lambda (x)
1619 (when x
1620 (let ((x (if (org-uuidgen-p x) (concat "ID-" x) x)))
1621 (org-e-odt--target
1622 "" (org-export-solidify-link-text x)))))
1623 extra-ids ""))
1624 ;; Title.
1625 (anchored-title (org-e-odt--target full-text id)))
1626 (cond
1627 ;; Case 2. This is a deep sub-tree: export it as a list item.
1628 ;; Also export as items headlines for which no section
1629 ;; format has been found.
1630 ((org-export-low-level-p headline info)
1631 ;; Build the real contents of the sub-tree.
1632 (concat
1633 (and (org-export-first-sibling-p headline info)
1634 (format "\n<text:list text:style-name=\"%s\" %s>"
1635 ;; Choose style based on list type.
1636 (if (org-export-numbered-headline-p headline info)
1637 "OrgNumberedList" "OrgBulletedList")
1638 ;; If top-level list, re-start numbering. Otherwise,
1639 ;; continue numbering.
1640 (format "text:continue-numbering=\"%s\""
1641 (let* ((parent (org-export-get-parent-headline
1642 headline)))
1643 (if (and parent
1644 (org-export-low-level-p parent info))
1645 "true" "false")))))
1646 (let ((headline-has-table-p
1647 (let ((section (assq 'section (org-element-contents headline))))
1648 (assq 'table (and section (org-element-contents section))))))
1649 (format "\n<text:list-item>\n%s\n%s"
1650 (concat
1651 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1652 "Text_20_body"
1653 (concat extra-targets anchored-title))
1654 contents)
1655 (if headline-has-table-p
1656 "</text:list-header>"
1657 "</text:list-item>")))
1658 (and (org-export-last-sibling-p headline info)
1659 "</text:list>")))
1660 ;; Case 3. Standard headline. Export it as a section.
1662 (concat
1663 (format
1664 "\n<text:h text:style-name=\"%s\" text:outline-level=\"%s\">%s</text:h>"
1665 (format "Heading_20_%s" level)
1666 level
1667 (concat extra-targets anchored-title))
1668 contents))))))
1671 ;;;; Horizontal Rule
1673 (defun org-e-odt-horizontal-rule (horizontal-rule contents info)
1674 "Transcode an HORIZONTAL-RULE object from Org to ODT.
1675 CONTENTS is nil. INFO is a plist holding contextual information."
1676 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1677 "Horizontal_20_Line" ""))
1680 ;;;; Inline Babel Call
1682 ;; Inline Babel Calls are ignored.
1685 ;;;; Inline Src Block
1687 (defun org-e-odt--find-verb-separator (s)
1688 "Return a character not used in string S.
1689 This is used to choose a separator for constructs like \\verb."
1690 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1691 (loop for c across ll
1692 when (not (string-match (regexp-quote (char-to-string c)) s))
1693 return (char-to-string c))))
1695 (defun org-e-odt-inline-src-block (inline-src-block contents info)
1696 "Transcode an INLINE-SRC-BLOCK element from Org to ODT.
1697 CONTENTS holds the contents of the item. INFO is a plist holding
1698 contextual information."
1699 (let* ((org-lang (org-element-property :language inline-src-block))
1700 (code (org-element-property :value inline-src-block))
1701 (separator (org-e-odt--find-verb-separator code)))
1702 (error "FIXME")))
1705 ;;;; Inlinetask
1707 (defun org-e-odt-inlinetask (inlinetask contents info)
1708 "Transcode an INLINETASK element from Org to ODT.
1709 CONTENTS holds the contents of the block. INFO is a plist
1710 holding contextual information."
1711 (cond
1712 ;; If `org-e-odt-format-inlinetask-function' is provided, call it
1713 ;; with appropriate arguments.
1714 ((functionp org-e-odt-format-inlinetask-function)
1715 (let ((format-function
1716 (function*
1717 (lambda (todo todo-type priority text tags
1718 &key contents &allow-other-keys)
1719 (funcall org-e-odt-format-inlinetask-function
1720 todo todo-type priority text tags contents)))))
1721 (org-e-odt-format-headline--wrap
1722 inlinetask info format-function :contents contents)))
1723 ;; Otherwise, use a default template.
1725 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1726 "Text_20_body"
1727 (org-e-odt--textbox
1728 (concat
1729 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1730 "OrgInlineTaskHeading"
1731 (org-e-odt-format-headline--wrap
1732 inlinetask info))
1733 contents)
1734 nil nil "OrgInlineTaskFrame" " style:rel-width=\"100%\"")))))
1736 ;;;; Italic
1738 (defun org-e-odt-italic (italic contents info)
1739 "Transcode ITALIC from Org to ODT.
1740 CONTENTS is the text with italic markup. INFO is a plist holding
1741 contextual information."
1742 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1743 "Emphasis" contents))
1746 ;;;; Item
1748 (defun org-e-odt-item (item contents info)
1749 "Transcode an ITEM element from Org to ODT.
1750 CONTENTS holds the contents of the item. INFO is a plist holding
1751 contextual information."
1752 (let* ((plain-list (org-export-get-parent item))
1753 (type (org-element-property :type plain-list))
1754 (counter (org-element-property :counter item))
1755 (tag (let ((tag (org-element-property :tag item)))
1756 (and tag
1757 (concat (org-e-odt--checkbox item)
1758 (org-export-data tag info))))))
1759 (case type
1760 ((ordered unordered descriptive-1 descriptive-2)
1761 (format "\n<text:list-item>\n%s\n%s"
1762 contents
1763 (let* ((--element-has-a-table-p
1764 (function
1765 (lambda (element info)
1766 (loop for el in (org-element-contents element)
1767 thereis (eq (org-element-type el) 'table))))))
1768 (cond
1769 ((funcall --element-has-a-table-p item info)
1770 "</text:list-header>")
1771 (t "</text:list-item>")))))
1772 (t (error "Unknown list type: %S" type)))))
1774 ;;;; Keyword
1776 (defun org-e-odt-keyword (keyword contents info)
1777 "Transcode a KEYWORD element from Org to ODT.
1778 CONTENTS is nil. INFO is a plist holding contextual information."
1779 (let ((key (org-element-property :key keyword))
1780 (value (org-element-property :value keyword)))
1781 (cond
1782 ((string= key "ODT") value)
1783 ((string= key "INDEX") (format "\\index{%s}" value))
1784 ((string= key "TARGET") nil ; FIXME
1785 ;; (format "\\label{%s}" (org-export-solidify-link-text value))
1787 ((string= key "toc")
1788 (let ((value (downcase value)))
1789 (cond
1790 ((string-match "\\<headlines\\>" value)
1791 (let ((depth (or (and (string-match "[0-9]+" value)
1792 (string-to-number (match-string 0 value)))
1793 (plist-get info :with-toc))))
1794 (when (wholenump depth) (org-e-odt-toc depth info))))
1795 ((string= "tables" value) "FIXME")
1796 ((string= "figures" value) "FIXME")
1797 ((string= "listings" value)
1798 (cond
1799 ;; At the moment, src blocks with a caption are wrapped
1800 ;; into a figure environment.
1801 (t "FIXME")))))))))
1804 ;;;; Latex Environment
1807 (eval-after-load 'org-odt
1808 '(ad-deactivate 'org-format-latex-as-mathml))
1810 ;; (defadvice org-format-latex-as-mathml ; FIXME
1811 ;; (after org-e-odt-protect-latex-fragment activate)
1812 ;; "Encode LaTeX fragment as XML.
1813 ;; Do this when translation to MathML fails."
1814 ;; (when (or (not (> (length ad-return-value) 0))
1815 ;; (get-text-property 0 'org-protected ad-return-value))
1816 ;; (setq ad-return-value
1817 ;; (org-propertize (org-e-odt--encode-plain-text (ad-get-arg 0))
1818 ;; 'org-protected t))))
1820 (defun org-e-odt-latex-environment (latex-environment contents info)
1821 "Transcode a LATEX-ENVIRONMENT element from Org to ODT.
1822 CONTENTS is nil. INFO is a plist holding contextual information."
1823 (let* ((latex-frag (org-remove-indentation
1824 (org-element-property :value latex-environment))))
1825 (org-e-odt-do-format-code latex-frag)))
1828 ;;;; Latex Fragment
1830 ;; (when latex-frag ; FIXME
1831 ;; (setq href (org-propertize href :title "LaTeX Fragment"
1832 ;; :description latex-frag)))
1833 ;; handle verbatim
1834 ;; provide descriptions
1836 (defun org-e-odt-latex-fragment (latex-fragment contents info)
1837 "Transcode a LATEX-FRAGMENT object from Org to ODT.
1838 CONTENTS is nil. INFO is a plist holding contextual information."
1839 (let* ((latex-frag (org-element-property :value latex-fragment))
1840 (processing-type (plist-get info :LaTeX-fragments)))
1841 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1842 "OrgCode" (org-e-odt--encode-plain-text latex-frag t))))
1845 ;;;; Line Break
1847 (defun org-e-odt-line-break (line-break contents info)
1848 "Transcode a LINE-BREAK object from Org to ODT.
1849 CONTENTS is nil. INFO is a plist holding contextual information."
1850 "<text:line-break/>\n")
1853 ;;;; Link
1855 ;;;; Links :: Label references
1857 (defun org-e-odt--enumerate (element info &optional predicate n)
1858 (when predicate (assert (funcall predicate element info)))
1859 (let* ((--numbered-parent-headline-at-<=-n
1860 (function
1861 (lambda (element n info)
1862 (loop for x in (org-export-get-genealogy element)
1863 thereis (and (eq (org-element-type x) 'headline)
1864 (<= (org-export-get-relative-level x info) n)
1865 (org-export-numbered-headline-p x info)
1866 x)))))
1867 (--enumerate
1868 (function
1869 (lambda (element scope info &optional predicate)
1870 (let ((counter 0))
1871 (org-element-map
1872 (or scope (plist-get info :parse-tree))
1873 (org-element-type element)
1874 (lambda (el)
1875 (and (or (not predicate) (funcall predicate el info))
1876 (incf counter)
1877 (eq element el)
1878 counter))
1879 info 'first-match)))))
1880 (scope (funcall --numbered-parent-headline-at-<=-n
1881 element (or n org-e-odt-display-outline-level) info))
1882 (ordinal (funcall --enumerate element scope info predicate))
1883 (tag
1884 (concat
1885 ;; Section number.
1886 (and scope
1887 (mapconcat 'number-to-string
1888 (org-export-get-headline-number scope info) "."))
1889 ;; Separator.
1890 (and scope ".")
1891 ;; Ordinal.
1892 (number-to-string ordinal))))
1893 tag))
1895 (defun org-e-odt-format-label (element info op)
1896 (assert (memq (org-element-type element) '(link table src-block paragraph)))
1897 (let* ((caption-from
1898 (case (org-element-type element)
1899 (link (org-export-get-parent-element element))
1900 (t element)))
1901 ;; Get label and caption.
1902 (label (org-element-property :name caption-from))
1903 (caption (org-export-get-caption caption-from))
1904 (short-caption (org-export-get-caption caption-from t))
1905 ;; Transcode captions.
1906 (caption (and caption (org-export-data caption info)))
1907 (short-caption (and short-caption
1908 (org-export-data short-caption info))))
1909 (when (or label caption)
1910 (let* ((default-category
1911 (case (org-element-type element)
1912 (table "__Table__")
1913 (src-block "__Listing__")
1914 ((link paragraph)
1915 (cond
1916 ((org-e-odt--enumerable-latex-image-p element info)
1917 "__DvipngImage__")
1918 ((org-e-odt--enumerable-image-p element info)
1919 "__Figure__")
1920 ((org-e-odt--enumerable-formula-p element info)
1921 "__MathFormula__")
1922 (t (error "Don't know how to format label for link: %S"
1923 element))))
1924 (t (error "Don't know how to format label for element type: %s"
1925 (org-element-type element)))))
1926 seqno)
1927 (assert default-category)
1928 (destructuring-bind (counter label-style category predicate)
1929 (assoc-default default-category org-e-odt-category-map-alist)
1930 ;; Compute sequence number of the element.
1931 (setq seqno (org-e-odt--enumerate element info predicate))
1932 ;; Localize category string.
1933 (setq category (org-export-translate category :utf-8 info))
1934 (case op
1935 ;; Case 1: Handle Label definition.
1936 (definition
1937 ;; Assign an internal label, if user has not provided one
1938 (setq label (or label (format "%s-%s" default-category seqno)))
1939 (setq label (org-export-solidify-link-text label))
1940 (cons
1941 (format-spec
1942 (cadr (assoc-string label-style org-e-odt-label-styles t))
1943 `((?e . ,category)
1944 (?n . ,(format
1945 "<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">%s</text:sequence>"
1946 label counter counter seqno))
1947 (?c . ,(or caption ""))))
1948 short-caption))
1949 ;; Case 2: Handle Label reference.
1950 (reference
1951 (assert label)
1952 (setq label (org-export-solidify-link-text label))
1953 (let* ((fmt (cddr (assoc-string label-style org-e-odt-label-styles t)))
1954 (fmt1 (car fmt))
1955 (fmt2 (cadr fmt)))
1956 (format "<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:sequence-ref>"
1957 fmt1 label (format-spec fmt2 `((?e . ,category)
1958 (?n . ,seqno))))))
1959 (t (error "Unknow %S on label" op))))))))
1962 ;;;; Links :: Inline Images
1964 (defun org-e-odt--copy-image-file (path)
1965 "Returns the internal name of the file"
1966 (let* ((image-type (file-name-extension path))
1967 (media-type (format "image/%s" image-type))
1968 (target-dir "Images/")
1969 (target-file
1970 (format "%s%04d.%s" target-dir
1971 (incf org-e-odt-embedded-images-count) image-type)))
1972 (message "Embedding %s as %s ..."
1973 (substring-no-properties path) target-file)
1975 (when (= 1 org-e-odt-embedded-images-count)
1976 (make-directory (concat org-e-odt-zip-dir target-dir))
1977 (org-e-odt-create-manifest-file-entry "" target-dir))
1979 (copy-file path (concat org-e-odt-zip-dir target-file) 'overwrite)
1980 (org-e-odt-create-manifest-file-entry media-type target-file)
1981 target-file))
1983 (defun org-e-odt--image-size (file &optional user-width
1984 user-height scale dpi embed-as)
1985 (let* ((--pixels-to-cms
1986 (function (lambda (pixels dpi)
1987 (let ((cms-per-inch 2.54)
1988 (inches (/ pixels dpi)))
1989 (* cms-per-inch inches)))))
1990 (--size-in-cms
1991 (function
1992 (lambda (size-in-pixels dpi)
1993 (and size-in-pixels
1994 (cons (funcall --pixels-to-cms (car size-in-pixels) dpi)
1995 (funcall --pixels-to-cms (cdr size-in-pixels) dpi))))))
1996 (dpi (or dpi org-e-odt-pixels-per-inch))
1997 (anchor-type (or embed-as "paragraph"))
1998 (user-width (and (not scale) user-width))
1999 (user-height (and (not scale) user-height))
2000 (size
2001 (and
2002 (not (and user-height user-width))
2004 ;; Use Imagemagick.
2005 (and (executable-find "identify")
2006 (let ((size-in-pixels
2007 (let ((dim (shell-command-to-string
2008 (format "identify -format \"%%w:%%h\" \"%s\""
2009 file))))
2010 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
2011 (cons (string-to-number (match-string 1 dim))
2012 (string-to-number (match-string 2 dim)))))))
2013 (funcall --size-in-cms size-in-pixels dpi)))
2014 ;; Use Emacs.
2015 (let ((size-in-pixels
2016 (ignore-errors ; Emacs could be in batch mode
2017 (clear-image-cache)
2018 (image-size (create-image file) 'pixels))))
2019 (funcall --size-in-cms size-in-pixels dpi))
2020 ;; Use hard-coded values.
2021 (cdr (assoc-string anchor-type
2022 org-e-odt-default-image-sizes-alist))
2023 ;; Error out.
2024 (error "Cannot determine image size, aborting"))))
2025 (width (car size)) (height (cdr size)))
2026 (cond
2027 (scale
2028 (setq width (* width scale) height (* height scale)))
2029 ((and user-height user-width)
2030 (setq width user-width height user-height))
2031 (user-height
2032 (setq width (* user-height (/ width height)) height user-height))
2033 (user-width
2034 (setq height (* user-width (/ height width)) width user-width))
2035 (t (ignore)))
2036 ;; ensure that an embedded image fits comfortably within a page
2037 (let ((max-width (car org-e-odt-max-image-size))
2038 (max-height (cdr org-e-odt-max-image-size)))
2039 (when (or (> width max-width) (> height max-height))
2040 (let* ((scale1 (/ max-width width))
2041 (scale2 (/ max-height height))
2042 (scale (min scale1 scale2)))
2043 (setq width (* scale width) height (* scale height)))))
2044 (cons width height)))
2046 (defun org-e-odt-link--inline-image (element info)
2047 "Return HTML code for an inline image.
2048 LINK is the link pointing to the inline image. INFO is a plist
2049 used as a communication channel."
2050 (assert (eq (org-element-type element) 'link))
2051 (let* ((src (let* ((type (org-element-property :type element))
2052 (raw-path (org-element-property :path element)))
2053 (cond ((member type '("http" "https"))
2054 (concat type ":" raw-path))
2055 ((file-name-absolute-p raw-path)
2056 (expand-file-name raw-path))
2057 (t raw-path))))
2058 (src-expanded (if (file-name-absolute-p src) src
2059 (expand-file-name src (file-name-directory
2060 (plist-get info :input-file)))))
2061 (href (format
2062 "\n<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>"
2063 (org-e-odt--copy-image-file src-expanded)))
2064 ;; Extract attributes from #+ATTR_ODT line.
2065 (attr-from (case (org-element-type element)
2066 (link (org-export-get-parent-element element))
2067 (t element)))
2068 ;; Convert attributes to a plist.
2069 (attr-plist (org-export-read-attribute :attr_odt attr-from))
2070 ;; Handle `:anchor', `:style' and `:attributes' properties.
2071 (user-frame-anchor
2072 (car (assoc-string (plist-get attr-plist :anchor)
2073 '(("as-char") ("paragraph") ("page")) t)))
2074 (user-frame-style
2075 (and user-frame-anchor (plist-get attr-plist :style)))
2076 (user-frame-attrs
2077 (and user-frame-anchor (plist-get attr-plist :attributes)))
2078 (user-frame-params
2079 (list user-frame-style user-frame-attrs user-frame-anchor))
2080 ;; (embed-as (or embed-as user-frame-anchor "paragraph"))
2081 ;; extrac
2082 ;; handle `:width', `:height' and `:scale' properties.
2083 (size (org-e-odt--image-size
2084 src-expanded (plist-get attr-plist :width)
2085 (plist-get attr-plist :height)
2086 (plist-get attr-plist :scale) nil ;; embed-as
2087 "paragraph" ; FIXME
2089 (width (car size)) (height (cdr size))
2090 (embed-as (if (org-e-odt--standalone-link-p element info) "paragraph"
2091 "as-char"))
2092 (captions (org-e-odt-format-label element info 'definition))
2093 (caption (car captions)) (short-caption (cdr captions))
2094 (entity (concat (and caption "Captioned") embed-as "Image")))
2095 (org-e-odt--render-image/formula entity href width height
2096 captions user-frame-params )))
2099 ;;;; Links :: Math formula
2101 (defun org-e-odt-link--inline-formula (element info)
2102 (let* ((src (let* ((type (org-element-property :type element))
2103 (raw-path (org-element-property :path element)))
2104 (cond
2105 ((file-name-absolute-p raw-path)
2106 (expand-file-name raw-path))
2107 (t raw-path))))
2108 (full-src (if (file-name-absolute-p src) src
2109 (expand-file-name src (file-name-directory
2110 (plist-get info :input-file)))))
2111 (caption-from
2112 (case (org-element-type element)
2113 (link (org-export-get-parent-element element))
2114 (t element)))
2115 (captions (org-e-odt-format-label caption-from info 'definition))
2116 (caption (car captions))
2117 (href
2118 (format "\n<draw:object %s xlink:href=\"%s\" xlink:type=\"simple\"/>"
2119 " xlink:show=\"embed\" xlink:actuate=\"onLoad\""
2120 (file-name-directory (org-e-odt--copy-formula-file full-src))))
2121 (embed-as (if caption 'paragraph 'character))
2122 width height)
2123 (cond
2124 ((eq embed-as 'character)
2125 (org-e-odt--render-image/formula "InlineFormula" href width height))
2127 (let* ((equation (org-e-odt--render-image/formula
2128 "CaptionedDisplayFormula" href width height captions))
2129 (label
2130 (let* ((org-e-odt-category-map-alist
2131 '(("__MathFormula__" "Text" "math-label" "Equation"
2132 org-e-odt--enumerable-formula-p))))
2133 (car (org-e-odt-format-label caption-from info 'definition)))))
2134 (concat equation "<text:tab/>" label))))))
2136 (defun org-e-odt--copy-formula-file (src-file)
2137 "Returns the internal name of the file"
2138 (let* ((target-dir (format "Formula-%04d/"
2139 (incf org-e-odt-embedded-formulas-count)))
2140 (target-file (concat target-dir "content.xml")))
2141 ;; Create a directory for holding formula file. Also enter it in
2142 ;; to manifest.
2143 (make-directory (concat org-e-odt-zip-dir target-dir))
2144 (org-e-odt-create-manifest-file-entry
2145 "application/vnd.oasis.opendocument.formula" target-dir "1.2")
2146 ;; Copy over the formula file from user directory to zip
2147 ;; directory.
2148 (message "Embedding %s as %s ..." src-file target-file)
2149 (let ((case-fold-search nil))
2150 (cond
2151 ;; Case 1: Mathml.
2152 ((string-match "\\.\\(mathml\\|mml\\)\\'" src-file)
2153 (copy-file src-file (concat org-e-odt-zip-dir target-file) 'overwrite))
2154 ;; Case 2: OpenDocument formula.
2155 ((string-match "\\.odf\\'" src-file)
2156 (org-e-odt--zip-extract src-file "content.xml"
2157 (concat org-e-odt-zip-dir target-dir)))
2158 (t (error "%s is not a formula file" src-file))))
2159 ;; Enter the formula file in to manifest.
2160 (org-e-odt-create-manifest-file-entry "text/xml" target-file)
2161 target-file))
2163 ;;;; Targets
2165 (defun org-e-odt--render-image/formula (cfg-key href width height &optional
2166 captions user-frame-params)
2167 (let* ((frame-cfg-alist
2168 ;; Each element of this alist is of the form (CFG-HANDLE
2169 ;; INNER-FRAME-PARAMS OUTER-FRAME-PARAMS).
2171 ;; CFG-HANDLE is the key to the alist.
2173 ;; INNER-FRAME-PARAMS and OUTER-FRAME-PARAMS specify the
2174 ;; frame params for INNER-FRAME and OUTER-FRAME
2175 ;; respectively. See below.
2177 ;; Configurations that are meant to be applied to
2178 ;; non-captioned image/formula specifies no
2179 ;; OUTER-FRAME-PARAMS.
2181 ;; TERMINOLOGY
2182 ;; ===========
2183 ;; INNER-FRAME :: Frame that directly surrounds an
2184 ;; image/formula.
2186 ;; OUTER-FRAME :: Frame that encloses the INNER-FRAME. This
2187 ;; frame also contains the caption, if any.
2189 ;; FRAME-PARAMS :: List of the form (FRAME-STYLE-NAME
2190 ;; FRAME-ATTRIBUTES FRAME-ANCHOR). Note
2191 ;; that these are the last three arguments
2192 ;; to `org-e-odt--frame'.
2194 ;; Note that an un-captioned image/formula requires just an
2195 ;; INNER-FRAME, while a captioned image/formula requires
2196 ;; both an INNER and an OUTER-FRAME.
2197 '(("As-CharImage" ("OrgInlineImage" nil "as-char"))
2198 ("ParagraphImage" ("OrgDisplayImage" nil "paragraph"))
2199 ("PageImage" ("OrgPageImage" nil "page"))
2200 ("CaptionedAs-CharImage"
2201 ("OrgCaptionedImage"
2202 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
2203 ("OrgInlineImage" nil "as-char"))
2204 ("CaptionedParagraphImage"
2205 ("OrgCaptionedImage"
2206 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
2207 ("OrgImageCaptionFrame" nil "paragraph"))
2208 ("CaptionedPageImage"
2209 ("OrgCaptionedImage"
2210 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
2211 ("OrgPageImageCaptionFrame" nil "page"))
2212 ("InlineFormula" ("OrgInlineFormula" nil "as-char"))
2213 ("DisplayFormula" ("OrgDisplayFormula" nil "as-char"))
2214 ("CaptionedDisplayFormula"
2215 ("OrgCaptionedFormula" nil "paragraph")
2216 ("OrgFormulaCaptionFrame" nil "paragraph"))))
2217 (caption (car captions)) (short-caption (cdr captions))
2218 ;; Retrieve inner and outer frame params, from configuration.
2219 (frame-cfg (assoc-string cfg-key frame-cfg-alist t))
2220 (inner (nth 1 frame-cfg))
2221 (outer (nth 2 frame-cfg))
2222 ;; User-specified frame params (from #+ATTR_ODT spec)
2223 (user user-frame-params)
2224 (--merge-frame-params (function
2225 (lambda (default user)
2226 "Merge default and user frame params."
2227 (if (not user) default
2228 (assert (= (length default) 3))
2229 (assert (= (length user) 3))
2230 (loop for u in user
2231 for d in default
2232 collect (or u d)))))))
2233 (cond
2234 ;; Case 1: Image/Formula has no caption.
2235 ;; There is only one frame, one that surrounds the image
2236 ;; or formula.
2237 ((not caption)
2238 ;; Merge user frame params with that from configuration.
2239 (setq inner (funcall --merge-frame-params inner user))
2240 (apply 'org-e-odt--frame href width height inner))
2241 ;; Case 2: Image/Formula is captioned or labeled.
2242 ;; There are two frames: The inner one surrounds the
2243 ;; image or formula. The outer one contains the
2244 ;; caption/sequence number.
2246 ;; Merge user frame params with outer frame params.
2247 (setq outer (funcall --merge-frame-params outer user))
2248 ;; Short caption, if specified, goes as part of inner frame.
2249 (setq inner (let ((frame-params (copy-sequence inner)))
2250 (setcar (cdr frame-params)
2251 (concat
2252 (cadr frame-params)
2253 (when short-caption
2254 (format " draw:name=\"%s\" " short-caption))))
2255 frame-params))
2256 (apply 'org-e-odt--textbox
2257 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2258 "Illustration"
2259 (concat
2260 (apply 'org-e-odt--frame href width height inner)
2261 caption))
2262 width height outer)))))
2264 (defun org-e-odt--enumerable-p (element info)
2265 ;; Element should have a caption or label.
2266 (or (org-element-property :caption element)
2267 (org-element-property :name element)))
2269 (defun org-e-odt--enumerable-image-p (element info)
2270 (org-e-odt--standalone-link-p
2271 element info
2272 ;; Paragraph should have a caption or label. It SHOULD NOT be a
2273 ;; replacement element. (i.e., It SHOULD NOT be a result of LaTeX
2274 ;; processing.)
2275 (lambda (p)
2276 (and (not (org-element-property :replaces p))
2277 (or (org-element-property :caption p)
2278 (org-element-property :name p))))
2279 ;; Link should point to an image file.
2280 (lambda (l)
2281 (assert (eq (org-element-type l) 'link))
2282 (org-export-inline-image-p l org-e-odt-inline-image-rules))))
2284 (defun org-e-odt--enumerable-latex-image-p (element info)
2285 (org-e-odt--standalone-link-p
2286 element info
2287 ;; Paragraph should have a caption or label. It SHOULD also be a
2288 ;; replacement element. (i.e., It SHOULD be a result of LaTeX
2289 ;; processing.)
2290 (lambda (p)
2291 (and (org-element-property :replaces p)
2292 (or (org-element-property :caption p)
2293 (org-element-property :name p))))
2294 ;; Link should point to an image file.
2295 (lambda (l)
2296 (assert (eq (org-element-type l) 'link))
2297 (org-export-inline-image-p l org-e-odt-inline-image-rules))))
2299 (defun org-e-odt--enumerable-formula-p (element info)
2300 (org-e-odt--standalone-link-p
2301 element info
2302 ;; Paragraph should have a caption or label.
2303 (lambda (p)
2304 (or (org-element-property :caption p)
2305 (org-element-property :name p)))
2306 ;; Link should point to a MathML or ODF file.
2307 (lambda (l)
2308 (assert (eq (org-element-type l) 'link))
2309 (org-export-inline-image-p l org-e-odt-inline-formula-rules))))
2311 (defun org-e-odt--standalone-link-p (element info &optional
2312 paragraph-predicate
2313 link-predicate)
2314 "Test if ELEMENT is a standalone link for the purpose ODT export.
2315 INFO is a plist holding contextual information.
2317 Return non-nil, if ELEMENT is of type paragraph satisfying
2318 PARAGRAPH-PREDICATE and it's sole content, save for whitespaces,
2319 is a link that satisfies LINK-PREDICATE.
2321 Return non-nil, if ELEMENT is of type link satisfying
2322 LINK-PREDICATE and it's containing paragraph satisfies
2323 PARAGRAPH-PREDICATE inaddtion to having no other content save for
2324 leading and trailing whitespaces.
2326 Return nil, otherwise."
2327 (let ((p (case (org-element-type element)
2328 (paragraph element)
2329 (link (and (or (not link-predicate)
2330 (funcall link-predicate element))
2331 (org-export-get-parent element)))
2332 (t nil))))
2333 (when p
2334 (assert (eq (org-element-type p) 'paragraph))
2335 (when (or (not paragraph-predicate)
2336 (funcall paragraph-predicate p))
2337 (let ((contents (org-element-contents p)))
2338 (loop for x in contents
2339 with inline-image-count = 0
2340 always (case (org-element-type x)
2341 (plain-text
2342 (not (org-string-nw-p x)))
2343 (link
2344 (and (or (not link-predicate)
2345 (funcall link-predicate x))
2346 (= (incf inline-image-count) 1)))
2347 (t nil))))))))
2349 (defun org-e-odt-get-previous-elements (blob info)
2350 (let ((parent (org-export-get-parent blob)))
2351 (cdr (memq blob (reverse (org-element-contents parent))))))
2353 (defun org-e-odt-resolve-numbered-paragraph (element info)
2354 (when (eq (org-element-type element) 'item)
2355 (let ((el element) ordinal)
2356 (while (eq (org-element-type el) 'item)
2357 (push (1+ (length (org-e-odt-get-previous-elements el info))) ordinal)
2358 (setq el (org-export-get-parent (org-export-get-parent el))))
2359 ordinal)))
2361 (defun org-e-odt-link (link desc info)
2362 "Transcode a LINK object from Org to ODT.
2364 DESC is the description part of the link, or the empty string.
2365 INFO is a plist holding contextual information. See
2366 `org-export-data'."
2367 (let* ((type (org-element-property :type link))
2368 (raw-path (org-element-property :path link))
2369 ;; Ensure DESC really exists, or set it to nil.
2370 (desc (and (not (string= desc "")) desc))
2371 (imagep (org-export-inline-image-p
2372 link org-e-odt-inline-image-rules))
2373 (path (cond
2374 ((member type '("http" "https" "ftp" "mailto"))
2375 (concat type ":" raw-path))
2376 ((string= type "file")
2377 (if (file-name-absolute-p raw-path)
2378 (concat "file://" (expand-file-name raw-path))
2379 (concat "file://" raw-path)))
2380 (t raw-path)))
2381 protocol)
2382 (cond
2383 ;; Image file.
2384 ((and (not desc) (org-export-inline-image-p
2385 link org-e-odt-inline-image-rules))
2386 (org-e-odt-link--inline-image link info))
2387 ;; Formula file.
2388 ((and (not desc) (org-export-inline-image-p
2389 link org-e-odt-inline-formula-rules))
2390 (org-e-odt-link--inline-formula link info))
2391 ;; Radio target: Transcode target's contents and use them as
2392 ;; link's description.
2393 ((string= type "radio")
2394 (let ((destination (org-export-resolve-radio-link link info)))
2395 (when destination
2396 (let ((desc (org-export-data (org-element-contents destination) info))
2397 (href (org-export-solidify-link-text path)))
2398 (format
2399 "<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
2400 href desc)))))
2401 ;; Links pointing to an headline: Find destination and build
2402 ;; appropriate referencing command.
2403 ((member type '("custom-id" "fuzzy" "id"))
2404 (let ((destination (if (string= type "fuzzy")
2405 (org-export-resolve-fuzzy-link link info)
2406 (org-export-resolve-id-link link info))))
2407 (case (org-element-type destination)
2408 ;; Fuzzy link points nowhere.
2409 ('nil
2410 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2411 "Emphasis" (or desc (org-export-data
2412 (org-element-property
2413 :raw-link link) info))))
2414 ;; Fuzzy link points to an invisible target.
2415 (keyword nil)
2416 ;; LINK points to an headline. Check if LINK should display
2417 ;; section numbers.
2418 (headline
2419 (let* ((headline-no (org-export-get-headline-number destination info))
2420 (label (format "sec-%s" (mapconcat 'number-to-string
2421 headline-no "-"))))
2422 (cond
2423 ;; Case 1: Headline is numbered and LINK has no
2424 ;; description or LINK's description matches headline's
2425 ;; title. Display section number.
2426 ((and (org-export-numbered-headline-p destination info)
2427 (or (not desc) (string= desc (org-element-property
2428 :raw-value destination))))
2429 (format
2430 "<text:bookmark-ref text:reference-format=\"chapter\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
2431 label (mapconcat 'number-to-string headline-no ".")))
2432 ;; Case 2: Either the headline is un-numbered or
2433 ;; LINK has a custom description. Display LINK's
2434 ;; description or headline's title.
2436 (let ((desc (or desc (org-export-data
2437 (org-element-property :title destination)
2438 info))))
2439 (format
2440 "<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
2441 label desc))))))
2442 ;; Fuzzy link points to a target. Do as above.
2443 (target
2444 ;; Identify nearest meaningful container
2445 (let ((container
2446 (loop for parent in (org-export-get-genealogy destination)
2447 when
2448 (memq
2449 (org-element-type parent)
2450 '(footnote-definition footnote-reference headline item
2451 table))
2452 return parent)))
2453 ;; There is a meaningful container
2454 (when container
2455 (case (org-element-type container)
2456 ;; Container is item
2457 (item
2458 (format
2459 "<text:bookmark-ref text:reference-format=\"number-all-superior\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
2460 (org-export-solidify-link-text path)
2462 (mapconcat 'number-to-string
2463 (org-e-odt-resolve-numbered-paragraph
2464 container info) ".")))))))
2465 (otherwise
2466 ;; (unless desc
2467 ;; (setq number (cond
2468 ;; ((org-e-odt--standalone-link-p destination info)
2469 ;; (org-export-get-ordinal
2470 ;; (assoc 'link (org-element-contents destination))
2471 ;; info 'link 'org-e-odt--standalone-link-p))
2472 ;; (t (org-export-get-ordinal destination info))))
2473 ;; (setq desc (when number
2474 ;; (if (atom number) (number-to-string number)
2475 ;; (mapconcat 'number-to-string number ".")))))
2477 (let ((label-reference
2478 (org-e-odt-format-label destination info 'reference)))
2479 (assert label-reference)
2480 label-reference)))))
2481 ;; Coderef: replace link with the reference name or the
2482 ;; equivalent line number.
2483 ((string= type "coderef")
2484 (let* ((line-no (format "%d" (org-export-resolve-coderef path info)))
2485 (href (concat "coderef-" path)))
2486 (format
2487 (org-export-get-coderef-format path desc)
2488 (format
2489 "<text:bookmark-ref text:reference-format=\"number\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
2490 href line-no))))
2491 ;; Link type is handled by a special function.
2492 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
2493 (funcall protocol (org-link-unescape path) desc 'odt))
2494 ;; External link with a description part.
2495 ((and path desc)
2496 (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
2497 path desc))
2498 ;; External link without a description part.
2499 (path
2500 (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
2501 path path))
2502 ;; No path, only description. Try to do something useful.
2503 (t (format "<text:span text:style-name=\"%s\">%s</text:span>"
2504 "Emphasis" desc)))))
2507 ;;;; Paragraph
2509 (defun org-e-odt-paragraph (paragraph contents info)
2510 "Transcode a PARAGRAPH element from Org to ODT.
2511 CONTENTS is the contents of the paragraph, as a string. INFO is
2512 the plist used as a communication channel."
2513 (let* ((parent (org-export-get-parent paragraph))
2514 (parent-type (org-element-type parent))
2515 (style (case parent-type
2516 (quote-block "Quotations")
2517 (center-block "OrgCenter")
2518 (footnote-definition "Footnote")
2519 (t (or (org-element-property :style paragraph)
2520 "Text_20_body")))))
2521 ;; If this paragraph is a leading paragraph in an item and the
2522 ;; item has a checkbox, splice the checkbox and paragraph contents
2523 ;; together.
2524 (when (and (eq (org-element-type parent) 'item)
2525 (eq paragraph (car (org-element-contents parent))))
2526 (setq contents (concat (org-e-odt--checkbox parent) contents)))
2527 (assert style)
2528 (format "\n<text:p text:style-name=\"%s\">%s</text:p>" style contents)))
2531 ;;;; Plain List
2533 (defun org-e-odt-plain-list (plain-list contents info)
2534 "Transcode a PLAIN-LIST element from Org to ODT.
2535 CONTENTS is the contents of the list. INFO is a plist holding
2536 contextual information."
2537 (format "\n<text:list text:style-name=\"%s\" %s>\n%s</text:list>"
2538 ;; Choose style based on list type.
2539 (case (org-element-property :type plain-list)
2540 (ordered "OrgNumberedList")
2541 (unordered "OrgBulletedList")
2542 (descriptive-1 "OrgDescriptionList")
2543 (descriptive-2 "OrgDescriptionList"))
2544 ;; If top-level list, re-start numbering. Otherwise,
2545 ;; continue numbering.
2546 (format "text:continue-numbering=\"%s\""
2547 (let* ((parent (org-export-get-parent plain-list)))
2548 (if (and parent (eq (org-element-type parent) 'item))
2549 "true" "false")))
2550 contents))
2552 ;;;; Plain Text
2554 (defun org-e-odt--encode-tabs-and-spaces (line)
2555 (replace-regexp-in-string
2556 "\\([\t]\\|\\([ ]+\\)\\)"
2557 (lambda (s)
2558 (cond
2559 ((string= s "\t") "<text:tab/>")
2560 (t (let ((n (length s)))
2561 (cond
2562 ((= n 1) " ")
2563 ((> n 1) (concat " " (format "<text:s text:c=\"%d\"/>" (1- n))))
2564 (t ""))))))
2565 line))
2567 (defun org-e-odt--encode-plain-text (text &optional no-whitespace-filling)
2568 (mapc
2569 (lambda (pair)
2570 (setq text (replace-regexp-in-string (car pair) (cdr pair) text t t)))
2571 '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
2572 (if no-whitespace-filling text
2573 (org-e-odt--encode-tabs-and-spaces text)))
2575 (defun org-e-odt-plain-text (text info)
2576 "Transcode a TEXT string from Org to ODT.
2577 TEXT is the string to transcode. INFO is a plist holding
2578 contextual information."
2579 (let ((output text))
2580 ;; Protect &, < and >.
2581 (setq output (org-e-odt--encode-plain-text output t))
2582 ;; Handle smart quotes. Be sure to provide original string since
2583 ;; OUTPUT may have been modified.
2584 (setq output (org-export-activate-smart-quotes output :utf-8 info text))
2585 ;; Convert special strings.
2586 (when (plist-get info :with-special-strings)
2587 (mapc
2588 (lambda (pair)
2589 (setq output
2590 (replace-regexp-in-string (car pair) (cdr pair) output t nil)))
2591 org-e-odt-special-string-regexps))
2592 ;; Handle break preservation if required.
2593 (when (plist-get info :preserve-breaks)
2594 (setq output (replace-regexp-in-string
2595 "\\(\\\\\\\\\\)?[ \t]*\n" "<text:line-break/>\n" output t)))
2596 ;; Return value.
2597 output))
2600 ;;;; Planning
2602 (defun org-e-odt-planning (planning contents info)
2603 "Transcode a PLANNING element from Org to ODT.
2604 CONTENTS is nil. INFO is a plist used as a communication
2605 channel."
2606 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2607 "OrgTimestampWrapper"
2608 (concat
2609 (let ((closed (org-element-property :closed planning)))
2610 (when closed
2611 (concat
2612 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2613 "OrgTimestampKeyword" org-closed-string)
2614 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2615 "OrgTimestamp"
2616 (org-translate-time
2617 (org-element-property :raw-value closed))))))
2618 (let ((deadline (org-element-property :deadline planning)))
2619 (when deadline
2620 (concat
2621 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2622 "OrgTimestampKeyword" org-deadline-string)
2623 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2624 "OrgTimestamp"
2625 (org-translate-time
2626 (org-element-property :raw-value deadline))))))
2627 (let ((scheduled (org-element-property :scheduled planning)))
2628 (when scheduled
2629 (concat
2630 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2631 "OrgTimestampKeyword" org-scheduled-string)
2632 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2633 "OrgTimestamp"
2634 (org-translate-time
2635 (org-element-property :raw-value scheduled)))))))))
2638 ;;;; Property Drawer
2640 (defun org-e-odt-property-drawer (property-drawer contents info)
2641 "Transcode a PROPERTY-DRAWER element from Org to ODT.
2642 CONTENTS is nil. INFO is a plist holding contextual
2643 information."
2644 ;; The property drawer isn't exported but we want separating blank
2645 ;; lines nonetheless.
2649 ;;;; Quote Block
2651 (defun org-e-odt-quote-block (quote-block contents info)
2652 "Transcode a QUOTE-BLOCK element from Org to ODT.
2653 CONTENTS holds the contents of the block. INFO is a plist
2654 holding contextual information."
2655 contents)
2658 ;;;; Quote Section
2660 (defun org-e-odt-quote-section (quote-section contents info)
2661 "Transcode a QUOTE-SECTION element from Org to ODT.
2662 CONTENTS is nil. INFO is a plist holding contextual information."
2663 (let ((value (org-remove-indentation
2664 (org-element-property :value quote-section))))
2665 (when value (org-e-odt-do-format-code value))))
2668 ;;;; Section
2670 (defun org-e-odt-format-section (text style &optional name)
2671 (let ((default-name (car (org-e-odt-add-automatic-style "Section"))))
2672 (format "\n<text:section text:style-name=\"%s\" %s>\n%s\n</text:section>"
2673 style
2674 (format "text:name=\"%s\"" (or name default-name))
2675 text)))
2678 (defun org-e-odt-section (section contents info) ; FIXME
2679 "Transcode a SECTION element from Org to ODT.
2680 CONTENTS holds the contents of the section. INFO is a plist
2681 holding contextual information."
2682 contents)
2684 ;;;; Radio Target
2686 (defun org-e-odt-radio-target (radio-target text info)
2687 "Transcode a RADIO-TARGET object from Org to ODT.
2688 TEXT is the text of the target. INFO is a plist holding
2689 contextual information."
2690 (org-e-odt--target
2691 text (org-export-solidify-link-text
2692 (org-element-property :value radio-target))))
2695 ;;;; Special Block
2697 (defun org-e-odt-special-block (special-block contents info)
2698 "Transcode a SPECIAL-BLOCK element from Org to ODT.
2699 CONTENTS holds the contents of the block. INFO is a plist
2700 holding contextual information."
2701 (let ((type (downcase (org-element-property :type special-block)))
2702 (attributes (org-export-read-attribute :attr_odt special-block)))
2703 (cond
2704 ;; Annotation.
2705 ((string= type "annotation")
2706 (let ((author (or (plist-get attributes :author)
2707 (let ((author (plist-get info :author)))
2708 (and author (org-export-data author info)))))
2709 (date (or (plist-get attributes :date)
2710 (plist-get info :date))))
2712 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2713 "Text_20_body"
2714 (format "<office:annotation>\n%s\n</office:annotation>"
2715 (concat
2716 (and author
2717 (format "<dc:creator>%s</dc:creator>" author))
2718 (and date
2719 (format "<dc:date>%s</dc:date>"
2720 (org-e-odt--date date)))
2721 contents)))))
2722 ;; Textbox.
2723 ((string= type "textbox")
2724 (let ((width (plist-get attributes :width))
2725 (height (plist-get attributes :height))
2726 (style (plist-get attributes :style))
2727 (extra (plist-get attributes :extra))
2728 (anchor (plist-get attributes :anchor)))
2729 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2730 "Text_20_body" (org-e-odt--textbox contents width height
2731 style extra anchor))))
2732 (t contents))))
2735 ;;;; Src Block
2737 (defun org-e-odt-hfy-face-to-css (fn)
2738 "Create custom style for face FN.
2739 When FN is the default face, use it's foreground and background
2740 properties to create \"OrgSrcBlock\" paragraph style. Otherwise
2741 use it's color attribute to create a character style whose name
2742 is obtained from FN. Currently all attributes of FN other than
2743 color are ignored.
2745 The style name for a face FN is derived using the following
2746 operations on the face name in that order - de-dash, CamelCase
2747 and prefix with \"OrgSrc\". For example,
2748 `font-lock-function-name-face' is associated with
2749 \"OrgSrcFontLockFunctionNameFace\"."
2750 (let* ((css-list (hfy-face-to-style fn))
2751 (style-name ((lambda (fn)
2752 (concat "OrgSrc"
2753 (mapconcat
2754 'capitalize (split-string
2755 (hfy-face-or-def-to-name fn) "-")
2756 ""))) fn))
2757 (color-val (cdr (assoc "color" css-list)))
2758 (background-color-val (cdr (assoc "background" css-list)))
2759 (style (and org-e-odt-create-custom-styles-for-srcblocks
2760 (cond
2761 ((eq fn 'default)
2762 (format org-e-odt-src-block-paragraph-format
2763 background-color-val color-val))
2765 (format
2767 <style:style style:name=\"%s\" style:family=\"text\">
2768 <style:text-properties fo:color=\"%s\"/>
2769 </style:style>" style-name color-val))))))
2770 (cons style-name style)))
2772 (defun org-e-odt-htmlfontify-string (line)
2773 (let* ((hfy-html-quote-regex "\\([<\"&> ]\\)")
2774 (hfy-html-quote-map '(("\"" "&quot;")
2775 ("<" "&lt;")
2776 ("&" "&amp;")
2777 (">" "&gt;")
2778 (" " "<text:s/>")
2779 (" " "<text:tab/>")))
2780 (hfy-face-to-css 'org-e-odt-hfy-face-to-css)
2781 (hfy-optimisations-1 (copy-sequence hfy-optimisations))
2782 (hfy-optimisations (add-to-list 'hfy-optimisations-1
2783 'body-text-only))
2784 (hfy-begin-span-handler
2785 (lambda (style text-block text-id text-begins-block-p)
2786 (insert (format "<text:span text:style-name=\"%s\">" style))))
2787 (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
2788 (with-no-warnings (htmlfontify-string line))))
2790 (defun org-e-odt-do-format-code
2791 (code &optional lang refs retain-labels num-start)
2792 (let* ((lang (or (assoc-default lang org-src-lang-modes) lang))
2793 (lang-mode (and lang (intern (format "%s-mode" lang))))
2794 (code-lines (org-split-string code "\n"))
2795 (code-length (length code-lines))
2796 (use-htmlfontify-p (and (functionp lang-mode)
2797 org-e-odt-fontify-srcblocks
2798 (require 'htmlfontify nil t)
2799 (fboundp 'htmlfontify-string)))
2800 (code (if (not use-htmlfontify-p) code
2801 (with-temp-buffer
2802 (insert code)
2803 (funcall lang-mode)
2804 (font-lock-fontify-buffer)
2805 (buffer-string))))
2806 (fontifier (if use-htmlfontify-p 'org-e-odt-htmlfontify-string
2807 'org-e-odt--encode-plain-text))
2808 (par-style (if use-htmlfontify-p "OrgSrcBlock"
2809 "OrgFixedWidthBlock"))
2810 (i 0))
2811 (assert (= code-length (length (org-split-string code "\n"))))
2812 (setq code
2813 (org-export-format-code
2814 code
2815 (lambda (loc line-num ref)
2816 (setq par-style
2817 (concat par-style (and (= (incf i) code-length) "LastLine")))
2819 (setq loc (concat loc (and ref retain-labels (format " (%s)" ref))))
2820 (setq loc (funcall fontifier loc))
2821 (when ref
2822 (setq loc (org-e-odt--target loc (concat "coderef-" ref))))
2823 (assert par-style)
2824 (setq loc (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2825 par-style loc))
2826 (if (not line-num) loc
2827 (format "\n<text:list-item>%s\n</text:list-item>" loc)))
2828 num-start refs))
2829 (cond
2830 ((not num-start) code)
2831 ((= num-start 0)
2832 (format
2833 "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
2834 " text:continue-numbering=\"false\"" code))
2836 (format
2837 "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
2838 " text:continue-numbering=\"true\"" code)))))
2840 (defun org-e-odt-format-code (element info)
2841 (let* ((lang (org-element-property :language element))
2842 ;; Extract code and references.
2843 (code-info (org-export-unravel-code element))
2844 (code (car code-info))
2845 (refs (cdr code-info))
2846 ;; Does the src block contain labels?
2847 (retain-labels (org-element-property :retain-labels element))
2848 ;; Does it have line numbers?
2849 (num-start (case (org-element-property :number-lines element)
2850 (continued (org-export-get-loc element info))
2851 (new 0))))
2852 (org-e-odt-do-format-code code lang refs retain-labels num-start)))
2854 (defun org-e-odt-src-block (src-block contents info)
2855 "Transcode a SRC-BLOCK element from Org to ODT.
2856 CONTENTS holds the contents of the item. INFO is a plist holding
2857 contextual information."
2858 (let* ((lang (org-element-property :language src-block))
2859 (attributes (org-export-read-attribute :attr_odt src-block))
2860 (captions (org-e-odt-format-label src-block info 'definition))
2861 (caption (car captions)) (short-caption (cdr captions)))
2862 (concat
2863 (and caption
2864 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2865 "Listing" caption))
2866 (let ((--src-block (org-e-odt-format-code src-block info)))
2867 (if (not (plist-get attributes :textbox)) --src-block
2868 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2869 "Text_20_body"
2870 (org-e-odt--textbox --src-block nil nil nil)))))))
2873 ;;;; Statistics Cookie
2875 (defun org-e-odt-statistics-cookie (statistics-cookie contents info)
2876 "Transcode a STATISTICS-COOKIE object from Org to ODT.
2877 CONTENTS is nil. INFO is a plist holding contextual information."
2878 (let ((cookie-value (org-element-property :value statistics-cookie)))
2879 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2880 "OrgCode" cookie-value)))
2883 ;;;; Strike-Through
2885 (defun org-e-odt-strike-through (strike-through contents info)
2886 "Transcode STRIKE-THROUGH from Org to ODT.
2887 CONTENTS is the text with strike-through markup. INFO is a plist
2888 holding contextual information."
2889 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2890 "Strikethrough" contents))
2893 ;;;; Subscript
2895 (defun org-e-odt-subscript (subscript contents info)
2896 "Transcode a SUBSCRIPT object from Org to ODT.
2897 CONTENTS is the contents of the object. INFO is a plist holding
2898 contextual information."
2899 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2900 "OrgSubscript" contents))
2903 ;;;; Superscript
2905 (defun org-e-odt-superscript (superscript contents info)
2906 "Transcode a SUPERSCRIPT object from Org to ODT.
2907 CONTENTS is the contents of the object. INFO is a plist holding
2908 contextual information."
2909 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2910 "OrgSuperscript" contents))
2913 ;;;; Table Cell
2915 (defun org-e-odt-table-style-spec (element info)
2916 (let* ((table (org-export-get-parent-table element))
2917 (table-attributes (org-export-read-attribute :attr_odt table))
2918 (table-style (plist-get table-attributes :style)))
2919 (assoc table-style org-e-odt-table-styles)))
2921 (defun org-e-odt-get-table-cell-styles (table-cell info)
2922 "Retrieve styles applicable to a table cell.
2923 R and C are (zero-based) row and column numbers of the table
2924 cell. STYLE-SPEC is an entry in `org-e-odt-table-styles'
2925 applicable to the current table. It is `nil' if the table is not
2926 associated with any style attributes.
2928 Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
2930 When STYLE-SPEC is nil, style the table cell the conventional way
2931 - choose cell borders based on row and column groupings and
2932 choose paragraph alignment based on `org-col-cookies' text
2933 property. See also
2934 `org-e-odt-get-paragraph-style-cookie-for-table-cell'.
2936 When STYLE-SPEC is non-nil, ignore the above cookie and return
2937 styles congruent with the ODF-1.2 specification."
2938 (let* ((table-cell-address (org-export-table-cell-address table-cell info))
2939 (r (car table-cell-address)) (c (cdr table-cell-address))
2940 (style-spec (org-e-odt-table-style-spec table-cell info))
2941 (table-dimensions (org-export-table-dimensions
2942 (org-export-get-parent-table table-cell)
2943 info)))
2944 (when style-spec
2945 ;; LibreOffice - particularly the Writer - honors neither table
2946 ;; templates nor custom table-cell styles. Inorder to retain
2947 ;; inter-operability with LibreOffice, only automatic styles are
2948 ;; used for styling of table-cells. The current implementation is
2949 ;; congruent with ODF-1.2 specification and hence is
2950 ;; future-compatible.
2952 ;; Additional Note: LibreOffice's AutoFormat facility for tables -
2953 ;; which recognizes as many as 16 different cell types - is much
2954 ;; richer. Unfortunately it is NOT amenable to easy configuration
2955 ;; by hand.
2956 (let* ((template-name (nth 1 style-spec))
2957 (cell-style-selectors (nth 2 style-spec))
2958 (cell-type
2959 (cond
2960 ((and (cdr (assoc 'use-first-column-styles cell-style-selectors))
2961 (= c 0)) "FirstColumn")
2962 ((and (cdr (assoc 'use-last-column-styles cell-style-selectors))
2963 (= (1+ c) (cdr table-dimensions)))
2964 "LastColumn")
2965 ((and (cdr (assoc 'use-first-row-styles cell-style-selectors))
2966 (= r 0)) "FirstRow")
2967 ((and (cdr (assoc 'use-last-row-styles cell-style-selectors))
2968 (= (1+ r) (car table-dimensions)))
2969 "LastRow")
2970 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
2971 (= (% r 2) 1)) "EvenRow")
2972 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
2973 (= (% r 2) 0)) "OddRow")
2974 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
2975 (= (% c 2) 1)) "EvenColumn")
2976 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
2977 (= (% c 2) 0)) "OddColumn")
2978 (t ""))))
2979 (concat template-name cell-type)))))
2981 (defun org-e-odt-table-cell (table-cell contents info)
2982 "Transcode a TABLE-CELL element from Org to ODT.
2983 CONTENTS is nil. INFO is a plist used as a communication
2984 channel."
2985 (let* ((table-cell-address (org-export-table-cell-address table-cell info))
2986 (r (car table-cell-address))
2987 (c (cdr table-cell-address))
2988 (horiz-span (or (org-export-table-cell-width table-cell info) 0))
2989 (table-row (org-export-get-parent table-cell))
2990 (custom-style-prefix (org-e-odt-get-table-cell-styles
2991 table-cell info))
2992 (paragraph-style
2994 (and custom-style-prefix
2995 (format "%sTableParagraph" custom-style-prefix))
2996 (concat
2997 (cond
2998 ((and (= 1 (org-export-table-row-group table-row info))
2999 (org-export-table-has-header-p
3000 (org-export-get-parent-table table-row) info))
3001 "OrgTableHeading")
3002 ((let* ((table (org-export-get-parent-table table-cell))
3003 (table-attrs (org-export-read-attribute :attr_odt table))
3004 (table-header-columns (plist-get table-attrs
3005 :header-columns)))
3006 (<= c (cond ((wholenump table-header-columns)
3007 (- table-header-columns 1))
3008 (table-header-columns 0)
3009 (t -1))))
3010 "OrgTableHeading")
3011 (t "OrgTableContents"))
3012 (capitalize (symbol-name (org-export-table-cell-alignment
3013 table-cell info))))))
3014 (cell-style-name
3016 (and custom-style-prefix (format "%sTableCell"
3017 custom-style-prefix))
3018 (concat
3019 "OrgTblCell"
3020 (when (or (org-export-table-row-starts-rowgroup-p table-row info)
3021 (zerop r)) "T")
3022 (when (org-export-table-row-ends-rowgroup-p table-row info) "B")
3023 (when (and (org-export-table-cell-starts-colgroup-p table-cell info)
3024 (not (zerop c)) ) "L"))))
3025 (cell-attributes
3026 (concat
3027 (format " table:style-name=\"%s\"" cell-style-name)
3028 (and (> horiz-span 0)
3029 (format " table:number-columns-spanned=\"%d\""
3030 (1+ horiz-span))))))
3031 (unless contents (setq contents ""))
3032 (concat
3033 (assert paragraph-style)
3034 (format "\n<table:table-cell%s>\n%s\n</table:table-cell>"
3035 cell-attributes
3036 (let ((table-cell-contents (org-element-contents table-cell)))
3037 (if (memq (org-element-type (car table-cell-contents))
3038 org-element-all-elements)
3039 contents
3040 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3041 paragraph-style contents))))
3042 (let (s)
3043 (dotimes (i horiz-span s)
3044 (setq s (concat s "\n<table:covered-table-cell/>"))))
3045 "\n")))
3048 ;;;; Table Row
3050 (defun org-e-odt-table-row (table-row contents info)
3051 "Transcode a TABLE-ROW element from Org to ODT.
3052 CONTENTS is the contents of the row. INFO is a plist used as a
3053 communication channel."
3054 ;; Rules are ignored since table separators are deduced from
3055 ;; borders of the current row.
3056 (when (eq (org-element-property :type table-row) 'standard)
3057 (let* ((rowgroup-tags
3058 (if (and (= 1 (org-export-table-row-group table-row info))
3059 (org-export-table-has-header-p
3060 (org-export-get-parent-table table-row) info))
3061 ;; If the row belongs to the first rowgroup and the
3062 ;; table has more than one row groups, then this row
3063 ;; belongs to the header row group.
3064 '("\n<table:table-header-rows>" . "\n</table:table-header-rows>")
3065 ;; Otherwise, it belongs to non-header row group.
3066 '("\n<table:table-rows>" . "\n</table:table-rows>"))))
3067 (concat
3068 ;; Does this row begin a rowgroup?
3069 (when (org-export-table-row-starts-rowgroup-p table-row info)
3070 (car rowgroup-tags))
3071 ;; Actual table row
3072 (format "\n<table:table-row>\n%s\n</table:table-row>" contents)
3073 ;; Does this row end a rowgroup?
3074 (when (org-export-table-row-ends-rowgroup-p table-row info)
3075 (cdr rowgroup-tags))))))
3078 ;;;; Table
3080 (defun org-e-odt-table-first-row-data-cells (table info)
3081 (let ((table-row
3082 (org-element-map
3083 table 'table-row
3084 (lambda (row)
3085 (unless (eq (org-element-property :type row) 'rule) row))
3086 info 'first-match))
3087 (special-column-p (org-export-table-has-special-column-p table)))
3088 (if (not special-column-p) (org-element-contents table-row)
3089 (cdr (org-element-contents table-row)))))
3091 (defun org-e-odt--table (table contents info)
3092 "Transcode a TABLE element from Org to ODT.
3093 CONTENTS is the contents of the table. INFO is a plist holding
3094 contextual information."
3095 (case (org-element-property :type table)
3096 ;; Case 1: table.el doesn't support export to OD format. Strip
3097 ;; such tables from export.
3098 (table.el
3099 (prog1 nil
3100 (message
3101 (concat
3102 "(org-e-odt): Found table.el-type table in the source Org file."
3103 " table.el doesn't support export to ODT format."
3104 " Stripping the table from export."))))
3105 ;; Case 2: Native Org tables.
3106 (otherwise
3107 (let* ((captions (org-e-odt-format-label table info 'definition))
3108 (caption (car captions)) (short-caption (cdr captions))
3109 (attributes (org-export-read-attribute :attr_odt table))
3110 (custom-table-style (nth 1 (org-e-odt-table-style-spec table info)))
3111 (table-column-specs
3112 (function
3113 (lambda (table info)
3114 (let* ((table-style (or custom-table-style "OrgTable"))
3115 (column-style (format "%sColumn" table-style)))
3116 (mapconcat
3117 (lambda (table-cell)
3118 (let ((width (1+ (or (org-export-table-cell-width
3119 table-cell info) 0)))
3120 (s (format
3121 "\n<table:table-column table:style-name=\"%s\"/>"
3122 column-style))
3123 out)
3124 (dotimes (i width out) (setq out (concat s out)))))
3125 (org-e-odt-table-first-row-data-cells table info) "\n"))))))
3126 (concat
3127 ;; caption.
3128 (when caption
3129 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3130 "Table" caption))
3131 ;; begin table.
3132 (let* ((automatic-name
3133 (org-e-odt-add-automatic-style "Table" attributes)))
3134 (format
3135 "\n<table:table table:name=\"%s\" table:style-name=\"%s\">"
3136 (or short-caption (car automatic-name))
3137 (or custom-table-style (cdr automatic-name) "OrgTable")))
3138 ;; column specification.
3139 (funcall table-column-specs table info)
3140 ;; actual contents.
3141 "\n" contents
3142 ;; end table.
3143 "</table:table>")))))
3145 (defun org-e-odt-table (table contents info)
3146 "Transcode a TABLE element from Org to ODT.
3147 CONTENTS is the contents of the table. INFO is a plist holding
3148 contextual information.
3150 Use `org-e-odt--table' to typeset the table. Handle details
3151 pertaining to indentation here."
3152 (let* ((--get-previous-elements
3153 (function
3154 (lambda (blob info)
3155 (let ((parent (org-export-get-parent blob)))
3156 (cdr (memq blob (reverse (org-element-contents parent))))))))
3157 (--element-preceded-by-table-p
3158 (function
3159 (lambda (element info)
3160 (loop for el in (funcall --get-previous-elements element info)
3161 thereis (eq (org-element-type el) 'table)))))
3162 (--walk-list-genealogy-and-collect-tags
3163 (function
3164 (lambda (table info)
3165 (let* ((genealogy (org-export-get-genealogy table))
3166 (list-genealogy
3167 (when (eq (org-element-type (car genealogy)) 'item)
3168 (loop for el in genealogy
3169 when (memq (org-element-type el)
3170 '(item plain-list))
3171 collect el)))
3172 (llh-genealogy
3173 (apply 'nconc
3174 (loop for el in genealogy
3175 when (and (eq (org-element-type el) 'headline)
3176 (org-export-low-level-p el info))
3177 collect
3178 (list el
3179 (assq 'headline
3180 (org-element-contents
3181 (org-export-get-parent el)))))))
3182 parent-list)
3183 (nconc
3184 ;; Handle list genealogy.
3185 (loop for el in list-genealogy collect
3186 (case (org-element-type el)
3187 (plain-list
3188 (setq parent-list el)
3189 (cons "</text:list>"
3190 (format "\n<text:list text:style-name=\"%s\" %s>"
3191 (case (org-element-property :type el)
3192 (ordered "OrgNumberedList")
3193 (unordered "OrgBulletedList")
3194 (descriptive-1 "OrgDescriptionList")
3195 (descriptive-2 "OrgDescriptionList"))
3196 "text:continue-numbering=\"true\"")))
3197 (item
3198 (cond
3199 ((not parent-list)
3200 (if (funcall --element-preceded-by-table-p table info)
3201 '("</text:list-header>" . "<text:list-header>")
3202 '("</text:list-item>" . "<text:list-header>")))
3203 ((funcall --element-preceded-by-table-p
3204 parent-list info)
3205 '("</text:list-header>" . "<text:list-header>"))
3206 (t '("</text:list-item>" . "<text:list-item>"))))))
3207 ;; Handle low-level headlines.
3208 (loop for el in llh-genealogy
3209 with step = 'item collect
3210 (case step
3211 (plain-list
3212 (setq step 'item) ; Flip-flop
3213 (setq parent-list el)
3214 (cons "</text:list>"
3215 (format "\n<text:list text:style-name=\"%s\" %s>"
3216 (if (org-export-numbered-headline-p
3217 el info)
3218 "OrgNumberedList"
3219 "OrgBulletedList")
3220 "text:continue-numbering=\"true\"")))
3221 (item
3222 (setq step 'plain-list) ; Flip-flop
3223 (cond
3224 ((not parent-list)
3225 (if (funcall --element-preceded-by-table-p table info)
3226 '("</text:list-header>" . "<text:list-header>")
3227 '("</text:list-item>" . "<text:list-header>")))
3228 ((let ((section? (org-export-get-previous-element
3229 parent-list info)))
3230 (and section?
3231 (eq (org-element-type section?) 'section)
3232 (assq 'table (org-element-contents section?))))
3233 '("</text:list-header>" . "<text:list-header>"))
3235 '("</text:list-item>" . "<text:list-item>")))))))))))
3236 (close-open-tags (funcall --walk-list-genealogy-and-collect-tags
3237 table info)))
3238 ;; OpenDocument schema does not permit table to occur within a
3239 ;; list item.
3241 ;; One solution - the easiest and lightweight, in terms of
3242 ;; implementation - is to put the table in an indented text box
3243 ;; and make the text box part of the list-item. Unfortunately if
3244 ;; the table is big and spans multiple pages, the text box could
3245 ;; overflow. In this case, the following attribute will come
3246 ;; handy.
3248 ;; ,---- From OpenDocument-v1.1.pdf
3249 ;; | 15.27.28 Overflow behavior
3250 ;; |
3251 ;; | For text boxes contained within text document, the
3252 ;; | style:overflow-behavior property specifies the behavior of text
3253 ;; | boxes where the containing text does not fit into the text
3254 ;; | box.
3255 ;; |
3256 ;; | If the attribute's value is clip, the text that does not fit
3257 ;; | into the text box is not displayed.
3258 ;; |
3259 ;; | If the attribute value is auto-create-new-frame, a new frame
3260 ;; | will be created on the next page, with the same position and
3261 ;; | dimensions of the original frame.
3262 ;; |
3263 ;; | If the style:overflow-behavior property's value is
3264 ;; | auto-create-new-frame and the text box has a minimum width or
3265 ;; | height specified, then the text box will grow until the page
3266 ;; | bounds are reached before a new frame is created.
3267 ;; `----
3269 ;; Unfortunately, LibreOffice-3.4.6 doesn't honor
3270 ;; auto-create-new-frame property and always resorts to clipping
3271 ;; the text box. This results in table being truncated.
3273 ;; So we solve the problem the hard (and fun) way using list
3274 ;; continuations.
3276 ;; The problem only becomes more interesting if you take in to
3277 ;; account the following facts:
3279 ;; - Description lists are simulated as plain lists.
3280 ;; - Low-level headlines can be listified.
3281 ;; - In Org-mode, a table can occur not only as a regular list
3282 ;; item, but also within description lists and low-level
3283 ;; headlines.
3285 ;; See `org-e-odt-translate-description-lists' and
3286 ;; `org-e-odt-translate-low-level-headlines' for how this is
3287 ;; tackled.
3289 (concat "\n"
3290 ;; Discontinue the list.
3291 (mapconcat 'car close-open-tags "\n")
3292 ;; Put the table in an indented section.
3293 (let* ((table (org-e-odt--table table contents info))
3294 (level (/ (length (mapcar 'car close-open-tags)) 2))
3295 (style (format "OrgIndentedSection-Level-%d" level)))
3296 (when table (org-e-odt-format-section table style)))
3297 ;; Continue the list.
3298 (mapconcat 'cdr (nreverse close-open-tags) "\n"))))
3301 ;;;; Target
3303 (defun org-e-odt-target (target contents info)
3304 "Transcode a TARGET object from Org to ODT.
3305 CONTENTS is nil. INFO is a plist holding contextual
3306 information."
3307 (let ((value (org-element-property :value target)))
3308 (org-e-odt--target "" (org-export-solidify-link-text value))))
3311 ;;;; Timestamp
3313 (defun org-e-odt-timestamp (timestamp contents info)
3314 "Transcode a TIMESTAMP object from Org to ODT.
3315 CONTENTS is nil. INFO is a plist used as a communication
3316 channel."
3317 (let ((value (org-translate-time
3318 (org-element-property :raw-value timestamp))))
3319 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3320 "OrgTimestampWrapper"
3321 (if (not (memq (org-element-property :type timestamp)
3322 '(active-range inactive-range)))
3323 value
3324 (let ((timestamps (org-split-string value "--")))
3325 (concat
3326 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3327 "OrgTimestamp"
3328 (car timestamps))
3329 "&#x2013;"
3330 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3331 "OrgTimestamp"
3332 (cdr timestamps))))))))
3335 ;;;; Underline
3337 (defun org-e-odt-underline (underline contents info)
3338 "Transcode UNDERLINE from Org to ODT.
3339 CONTENTS is the text with underline markup. INFO is a plist
3340 holding contextual information."
3341 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3342 "Underline" contents))
3345 ;;;; Verbatim
3347 (defun org-e-odt-verbatim (verbatim contents info)
3348 "Transcode a VERBATIM object from Org to ODT.
3349 CONTENTS is nil. INFO is a plist used as a communication
3350 channel."
3351 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3352 "OrgCode" (org-element-property :value verbatim)))
3355 ;;;; Verse Block
3357 (defun org-e-odt-verse-block (verse-block contents info)
3358 "Transcode a VERSE-BLOCK element from Org to ODT.
3359 CONTENTS is verse block contents. INFO is a plist holding
3360 contextual information."
3361 ;; Add line breaks to each line of verse.
3362 (setq contents (replace-regexp-in-string
3363 "\\(<text:line-break/>\\)?[ \t]*\n"
3364 "<text:line-break/>" contents))
3365 ;; Replace tabs and spaces.
3366 (setq contents (org-e-odt--encode-tabs-and-spaces contents))
3367 ;; Surround it in a verse environment.
3368 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3369 "OrgVerse" contents))
3373 ;;; Filters
3375 ;;;; LaTeX fragments
3377 (defun org-e-odt--translate-latex-fragments (tree backend info)
3378 (let ((processing-type (plist-get info :LaTeX-fragments))
3379 (count 0))
3380 ;; Normalize processing-type to one of dvipng, mathml or verbatim.
3381 ;; If the desired converter is not available, force verbatim
3382 ;; processing.
3383 (case processing-type
3384 ((t mathml)
3385 (if (and (fboundp 'org-format-latex-mathml-available-p)
3386 (org-format-latex-mathml-available-p))
3387 (setq processing-type 'mathml)
3388 (message "LaTeX to MathML converter not available.")
3389 (setq processing-type 'verbatim)))
3390 (dvipng
3391 (unless (and (org-check-external-command "latex" "" t)
3392 (org-check-external-command "dvipng" "" t))
3393 (message "LaTeX to PNG converter not available.")
3394 (setq processing-type 'verbatim)))
3395 (otherwise
3396 (message "Unknown LaTeX option. Forcing verbatim.")
3397 (setq processing-type 'verbatim)))
3399 ;; Store normalized value for later use.
3400 (when (plist-get info :LaTeX-fragments)
3401 (plist-put info :LaTeX-fragments processing-type))
3402 (message "Formatting LaTeX using %s" processing-type)
3404 ;; Convert `latex-fragment's and `latex-environment's.
3405 (when (memq processing-type '(mathml dvipng))
3406 (org-element-map
3407 tree '(latex-fragment latex-environment)
3408 (lambda (latex-*)
3409 (incf count)
3410 (let* ((latex-frag (org-element-property :value latex-*))
3411 (input-file (plist-get info :input-file))
3412 (cache-dir (file-name-directory input-file))
3413 (cache-subdir (concat
3414 (case processing-type
3415 (dvipng "ltxpng/")
3416 (mathml "ltxmathml/"))
3417 (file-name-sans-extension
3418 (file-name-nondirectory input-file))))
3419 (display-msg
3420 (case processing-type
3421 (dvipng (format "Creating LaTeX Image %d..." count))
3422 (mathml (format "Creating MathML snippet %d..." count))))
3423 ;; Get an Org-style link to PNG image or the MathML
3424 ;; file.
3425 (org-link
3426 (let ((link (with-temp-buffer
3427 (insert latex-frag)
3428 (org-format-latex cache-subdir cache-dir
3429 nil display-msg
3430 nil nil processing-type)
3431 (buffer-substring-no-properties
3432 (point-min) (point-max)))))
3433 (if (not (string-match "file:\\([^]]*\\)" link))
3434 (prog1 nil (message "LaTeX Conversion failed."))
3435 link))))
3436 (when org-link
3437 ;; Conversion succeeded. Parse above Org-style link to a
3438 ;; `link' object.
3439 (let* ((link (car (org-element-map (with-temp-buffer
3440 (org-mode)
3441 (insert org-link)
3442 (org-element-parse-buffer))
3443 'link 'identity))))
3444 ;; Orphan the link.
3445 (org-element-put-property link :parent nil)
3446 (let* (
3447 (replacement
3448 (case (org-element-type latex-*)
3449 ;; Case 1: LaTeX environment.
3450 ;; Mimic a "standalone image or formula" by
3451 ;; enclosing the `link' in a `paragraph'.
3452 ;; Copy over original attributes, captions to
3453 ;; the enclosing paragraph.
3454 (latex-environment
3455 (org-element-adopt-elements
3456 (list 'paragraph
3457 (list :style "OrgFormula"
3458 :name (org-element-property :name
3459 latex-*)
3460 :caption (org-element-property :caption
3461 latex-*)))
3462 link))
3463 ;; Case 2: LaTeX fragment.
3464 ;; No special action.
3465 (latex-fragment link))))
3466 ;; Note down the object that link replaces.
3467 (org-element-put-property replacement :replaces
3468 (list (org-element-type latex-*)
3469 (list :value latex-frag)))
3470 ;; Replace now.
3471 (org-element-set-element latex-* replacement))))))
3472 info)))
3473 tree)
3476 ;;;; Description lists
3478 ;; This translator is necessary to handle indented tables in a uniform
3479 ;; manner. See comment in `org-e-odt--table'.
3481 (defun org-e-odt--translate-description-lists (tree backend info)
3482 ;; OpenDocument has no notion of a description list. So simulate it
3483 ;; using plain lists. Description lists in the exported document
3484 ;; are typeset in the same manner as they are in a typical HTML
3485 ;; document.
3487 ;; Specifically, a description list like this:
3489 ;; ,----
3490 ;; | - term-1 :: definition-1
3491 ;; | - term-2 :: definition-2
3492 ;; `----
3494 ;; gets translated in to the following form:
3496 ;; ,----
3497 ;; | - term-1
3498 ;; | - definition-1
3499 ;; | - term-2
3500 ;; | - definition-2
3501 ;; `----
3503 ;; Further effect is achieved by fixing the OD styles as below:
3505 ;; 1. Set the :type property of the simulated lists to
3506 ;; `descriptive-1' and `descriptive-2'. Map these to list-styles
3507 ;; that has *no* bullets whatsoever.
3509 ;; 2. The paragraph containing the definition term is styled to be
3510 ;; in bold.
3512 (org-element-map
3513 tree 'plain-list
3514 (lambda (el)
3515 (when (equal (org-element-property :type el) 'descriptive)
3516 (org-element-set-element
3518 (apply 'org-element-adopt-elements
3519 (list 'plain-list (list :type 'descriptive-1))
3520 (mapcar
3521 (lambda (item)
3522 (org-element-adopt-elements
3523 (list 'item (list :checkbox (org-element-property
3524 :checkbox item)))
3525 (list 'paragraph (list :style "Text_20_body_20_bold")
3526 (or (org-element-property :tag item) "(no term)"))
3527 (org-element-adopt-elements
3528 (list 'plain-list (list :type 'descriptive-2))
3529 (apply 'org-element-adopt-elements
3530 (list 'item nil)
3531 (org-element-contents item)))))
3532 (org-element-contents el)))))
3533 nil)
3534 info)
3535 tree)
3537 ;;;; List tables
3539 ;; Lists that are marked with attribute `:list-table' are called as
3540 ;; list tables. They will be rendered as a table within the exported
3541 ;; document.
3543 ;; Consider an example. The following list table
3545 ;; #+attr_odt :list-table t
3546 ;; - Row 1
3547 ;; - 1.1
3548 ;; - 1.2
3549 ;; - 1.3
3550 ;; - Row 2
3551 ;; - 2.1
3552 ;; - 2.2
3553 ;; - 2.3
3555 ;; will be exported as though it were an Org table like the one show
3556 ;; below.
3558 ;; | Row 1 | 1.1 | 1.2 | 1.3 |
3559 ;; | Row 2 | 2.1 | 2.2 | 2.3 |
3561 ;; Note that org-tables are NOT multi-line and each line is mapped to
3562 ;; a unique row in the exported document. So if an exported table
3563 ;; needs to contain a single paragraph (with copious text) it needs to
3564 ;; be typed up in a single line. Editing such long lines using the
3565 ;; table editor will be a cumbersome task. Furthermore inclusion of
3566 ;; multi-paragraph text in a table cell is well-nigh impossible.
3568 ;; A LIST-TABLE circumvents above problems.
3570 ;; Note that in the example above the list items could be paragraphs
3571 ;; themselves and the list can be arbitrarily deep.
3573 ;; Inspired by following thread:
3574 ;; https://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01101.html
3576 ;; Translate lists to tables
3578 (defun org-e-odt--translate-list-tables (tree backend info)
3579 (org-element-map
3580 tree 'plain-list
3581 (lambda (l1-list)
3582 (when (org-export-read-attribute :attr_odt l1-list :list-table)
3583 ;; Replace list with table.
3584 (org-element-set-element
3585 l1-list
3586 ;; Build replacement table.
3587 (apply 'org-element-adopt-elements
3588 (list 'table '(:type org :attr_odt (":style \"GriddedTable\"")))
3589 (org-element-map
3590 l1-list
3591 'item
3592 (lambda (l1-item)
3593 (let* ((l1-item-contents (org-element-contents l1-item))
3594 l1-item-leading-text l2-list)
3595 ;; Remove Level-2 list from the Level-item. It
3596 ;; will be subsequently attached as table-cells.
3597 (let ((cur l1-item-contents) prev)
3598 (while (and cur (not (eq (org-element-type (car cur))
3599 'plain-list)))
3600 (setq prev cur)
3601 (setq cur (cdr cur)))
3602 (when prev
3603 (setcdr prev nil)
3604 (setq l2-list (car cur)))
3605 (setq l1-item-leading-text l1-item-contents))
3606 ;; Level-1 items start a table row.
3607 (apply 'org-element-adopt-elements
3608 (list 'table-row (list :type 'standard))
3609 ;; Leading text of level-1 item define the
3610 ;; first table-cell.
3611 (apply 'org-element-adopt-elements
3612 (list 'table-cell nil)
3613 l1-item-leading-text)
3614 ;; Level-2 items define subsequent
3615 ;; table-cells of the row.
3616 (org-element-map
3617 l2-list
3618 'item
3619 (lambda (l2-item)
3620 (apply 'org-element-adopt-elements
3621 (list 'table-cell nil)
3622 (org-element-contents l2-item)))
3623 info nil 'item))))
3624 info nil 'item))))
3625 nil)
3626 info)
3627 tree)
3630 ;;; Interactive functions
3632 (defun org-e-odt-create-manifest-file-entry (&rest args)
3633 (push args org-e-odt-manifest-file-entries))
3635 (defun org-e-odt-write-manifest-file ()
3636 (make-directory (concat org-e-odt-zip-dir "META-INF"))
3637 (let ((manifest-file (concat org-e-odt-zip-dir "META-INF/manifest.xml")))
3638 (with-current-buffer
3639 (let ((nxml-auto-insert-xml-declaration-flag nil))
3640 (find-file-noselect manifest-file t))
3641 (insert
3642 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
3643 <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
3644 (mapc
3645 (lambda (file-entry)
3646 (let* ((version (nth 2 file-entry))
3647 (extra (if (not version) ""
3648 (format " manifest:version=\"%s\"" version))))
3649 (insert
3650 (format org-e-odt-manifest-file-entry-tag
3651 (nth 0 file-entry) (nth 1 file-entry) extra))))
3652 org-e-odt-manifest-file-entries)
3653 (insert "\n</manifest:manifest>"))))
3655 (defmacro org-e-odt--export-wrap (out-file &rest body)
3656 `(let* ((--out-file ,out-file)
3657 (out-file-type (file-name-extension --out-file))
3658 (org-e-odt-xml-files '("META-INF/manifest.xml" "content.xml"
3659 "meta.xml" "styles.xml"))
3660 ;; Initialize temporary workarea. All files that end up in
3661 ;; the exported document get parked/created here.
3662 (org-e-odt-zip-dir (file-name-as-directory
3663 (make-temp-file (format "%s-" out-file-type) t)))
3664 (org-e-odt-manifest-file-entries nil)
3665 (--cleanup-xml-buffers
3666 (function
3667 (lambda nil
3668 ;; Kill all XML buffers.
3669 (mapc (lambda (file)
3670 (let ((buf (get-file-buffer
3671 (concat org-e-odt-zip-dir file))))
3672 (when buf
3673 (set-buffer-modified-p nil)
3674 (kill-buffer buf))))
3675 org-e-odt-xml-files)
3676 ;; Delete temporary directory and also other embedded
3677 ;; files that get copied there.
3678 (delete-directory org-e-odt-zip-dir t)))))
3679 (org-condition-case-unless-debug err
3680 (progn
3681 (unless (executable-find "zip")
3682 ;; Not at all OSes ship with zip by default
3683 (error "Executable \"zip\" needed for creating OpenDocument files"))
3684 ;; Do export. This creates a bunch of xml files ready to be
3685 ;; saved and zipped.
3686 (progn ,@body)
3687 ;; Create a manifest entry for content.xml.
3688 (org-e-odt-create-manifest-file-entry "text/xml" "content.xml")
3689 ;; Write mimetype file
3690 (let* ((mimetypes
3691 '(("odt" . "application/vnd.oasis.opendocument.text")
3692 ("odf" . "application/vnd.oasis.opendocument.formula")))
3693 (mimetype (cdr (assoc-string out-file-type mimetypes t))))
3694 (unless mimetype
3695 (error "Unknown OpenDocument backend %S" out-file-type))
3696 (write-region mimetype nil (concat org-e-odt-zip-dir "mimetype"))
3697 (org-e-odt-create-manifest-file-entry mimetype "/" "1.2"))
3698 ;; Write out the manifest entries before zipping
3699 (org-e-odt-write-manifest-file)
3700 ;; Save all XML files.
3701 (mapc (lambda (file)
3702 (let ((buf (get-file-buffer (concat org-e-odt-zip-dir file))))
3703 (when buf
3704 (with-current-buffer buf
3705 ;; Prettify output if needed.
3706 (when org-e-odt-prettify-xml
3707 (indent-region (point-min) (point-max)))
3708 (save-buffer 0)))))
3709 org-e-odt-xml-files)
3710 ;; Run zip.
3711 (let* ((target --out-file)
3712 (target-name (file-name-nondirectory target))
3713 (cmds `(("zip" "-mX0" ,target-name "mimetype")
3714 ("zip" "-rmTq" ,target-name "."))))
3715 ;; If a file with same name as the desired output file
3716 ;; exists, remove it.
3717 (when (file-exists-p target)
3718 (delete-file target))
3719 ;; Zip up the xml files.
3720 (let ((coding-system-for-write 'no-conversion) exitcode err-string)
3721 (message "Creating ODT file...")
3722 ;; Switch temporarily to content.xml. This way Zip
3723 ;; process will inherit `org-e-odt-zip-dir' as the current
3724 ;; directory.
3725 (with-current-buffer
3726 (find-file-noselect (concat org-e-odt-zip-dir "content.xml") t)
3727 (mapc
3728 (lambda (cmd)
3729 (message "Running %s" (mapconcat 'identity cmd " "))
3730 (setq err-string
3731 (with-output-to-string
3732 (setq exitcode
3733 (apply 'call-process (car cmd)
3734 nil standard-output nil (cdr cmd)))))
3735 (or (zerop exitcode)
3736 (error (concat "Unable to create OpenDocument file."
3737 (format " Zip failed with error (%s)"
3738 err-string)))))
3739 cmds)))
3740 ;; Move the zip file from temporary work directory to
3741 ;; user-mandated location.
3742 (rename-file (concat org-e-odt-zip-dir target-name) target)
3743 (message "Created %s" (expand-file-name target))
3744 ;; Cleanup work directory and work files.
3745 (funcall --cleanup-xml-buffers)
3746 ;; Open the OpenDocument file in archive-mode for
3747 ;; examination.
3748 (find-file-noselect target t)
3749 ;; Return exported file.
3750 (cond
3751 ;; Case 1: Conversion desired on exported file. Run the
3752 ;; converter on the OpenDocument file. Return the
3753 ;; converted file.
3754 (org-e-odt-preferred-output-format
3755 (or (org-e-odt-convert target org-e-odt-preferred-output-format)
3756 target))
3757 ;; Case 2: No further conversion. Return exported
3758 ;; OpenDocument file.
3759 (t target))))
3760 (error
3761 ;; Cleanup work directory and work files.
3762 (funcall --cleanup-xml-buffers)
3763 (message "OpenDocument export failed: %s"
3764 (error-message-string err))))))
3767 ;;;; Export to OpenDocument formula
3769 ;;;###autoload
3770 (defun org-e-odt-export-as-odf (latex-frag &optional odf-file)
3771 "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
3772 Use `org-create-math-formula' to convert LATEX-FRAG first to
3773 MathML. When invoked as an interactive command, use
3774 `org-latex-regexps' to infer LATEX-FRAG from currently active
3775 region. If no LaTeX fragments are found, prompt for it. Push
3776 MathML source to kill ring, if `org-export-copy-to-kill-ring' is
3777 non-nil."
3778 (interactive
3779 `(,(let (frag)
3780 (setq frag (and (setq frag (and (region-active-p)
3781 (buffer-substring (region-beginning)
3782 (region-end))))
3783 (loop for e in org-latex-regexps
3784 thereis (when (string-match (nth 1 e) frag)
3785 (match-string (nth 2 e) frag)))))
3786 (read-string "LaTeX Fragment: " frag nil frag))
3787 ,(let ((odf-filename (expand-file-name
3788 (concat
3789 (file-name-sans-extension
3790 (or (file-name-nondirectory buffer-file-name)))
3791 "." "odf")
3792 (file-name-directory buffer-file-name))))
3793 (read-file-name "ODF filename: " nil odf-filename nil
3794 (file-name-nondirectory odf-filename)))))
3795 (let ((filename (or odf-file
3796 (expand-file-name
3797 (concat
3798 (file-name-sans-extension
3799 (or (file-name-nondirectory buffer-file-name)))
3800 "." "odf")
3801 (file-name-directory buffer-file-name)))))
3802 (org-e-odt--export-wrap
3803 filename
3804 (let* ((buffer (progn
3805 (require 'nxml-mode)
3806 (let ((nxml-auto-insert-xml-declaration-flag nil))
3807 (find-file-noselect (concat org-e-odt-zip-dir
3808 "content.xml") t))))
3809 (coding-system-for-write 'utf-8)
3810 (save-buffer-coding-system 'utf-8))
3811 (set-buffer buffer)
3812 (set-buffer-file-coding-system coding-system-for-write)
3813 (let ((mathml (org-create-math-formula latex-frag)))
3814 (unless mathml (error "No Math formula created"))
3815 (insert mathml)
3816 ;; Add MathML to kill ring, if needed.
3817 (when org-export-copy-to-kill-ring
3818 (org-kill-new (buffer-string))))))))
3820 ;;;###autoload
3821 (defun org-e-odt-export-as-odf-and-open ()
3822 "Export LaTeX fragment as OpenDocument formula and immediately open it.
3823 Use `org-e-odt-export-as-odf' to read LaTeX fragment and OpenDocument
3824 formula file."
3825 (interactive)
3826 (org-open-file (call-interactively 'org-e-odt-export-as-odf) 'system))
3829 ;;;; Export to OpenDocument Text
3831 ;;;###autoload
3832 (defun org-e-odt-export-to-odt (&optional async subtreep visible-only ext-plist)
3833 "Export current buffer to a HTML file.
3835 If narrowing is active in the current buffer, only export its
3836 narrowed part.
3838 If a region is active, export that region.
3840 A non-nil optional argument ASYNC means the process should happen
3841 asynchronously. The resulting file should be accessible through
3842 the `org-export-stack' interface.
3844 When optional argument SUBTREEP is non-nil, export the sub-tree
3845 at point, extracting information from the headline properties
3846 first.
3848 When optional argument VISIBLE-ONLY is non-nil, don't export
3849 contents of hidden elements.
3851 EXT-PLIST, when provided, is a property list with external
3852 parameters overriding Org default settings, but still inferior to
3853 file-local settings.
3855 Return output file's name."
3856 (interactive)
3857 (let ((outfile (org-export-output-file-name ".odt" subtreep)))
3858 (if async
3859 (org-export-async-start (lambda (f) (org-export-add-to-stack f 'e-odt))
3860 `(expand-file-name
3861 (org-e-odt--export-wrap
3862 ,outfile
3863 (let* ((org-e-odt-embedded-images-count 0)
3864 (org-e-odt-embedded-formulas-count 0)
3865 (org-e-odt-automatic-styles nil)
3866 (org-e-odt-object-counters nil)
3867 ;; Let `htmlfontify' know that we are interested in
3868 ;; collecting styles.
3869 (hfy-user-sheet-assoc nil))
3870 ;; Initialize content.xml and kick-off the export
3871 ;; process.
3872 (let ((out-buf
3873 (progn
3874 (require 'nxml-mode)
3875 (let ((nxml-auto-insert-xml-declaration-flag nil))
3876 (find-file-noselect
3877 (concat org-e-odt-zip-dir "content.xml") t)))))
3878 (org-export-to-buffer
3879 'e-odt out-buf ,subtreep ,visible-only nil ',ext-plist))))))
3880 (org-e-odt--export-wrap
3881 outfile
3882 (let* ((org-e-odt-embedded-images-count 0)
3883 (org-e-odt-embedded-formulas-count 0)
3884 (org-e-odt-automatic-styles nil)
3885 (org-e-odt-object-counters nil)
3886 ;; Let `htmlfontify' know that we are interested in collecting
3887 ;; styles.
3888 (hfy-user-sheet-assoc nil))
3889 ;; Initialize content.xml and kick-off the export process.
3890 (let ((out-buf (progn
3891 (require 'nxml-mode)
3892 (let ((nxml-auto-insert-xml-declaration-flag nil))
3893 (find-file-noselect
3894 (concat org-e-odt-zip-dir "content.xml") t)))))
3895 (org-export-to-buffer
3896 'e-odt out-buf subtreep visible-only nil ext-plist)))))))
3899 ;;;; Convert between OpenDocument and other formats
3901 (defun org-e-odt-reachable-p (in-fmt out-fmt)
3902 "Return non-nil if IN-FMT can be converted to OUT-FMT."
3903 (catch 'done
3904 (let ((reachable-formats (org-e-odt-do-reachable-formats in-fmt)))
3905 (dolist (e reachable-formats)
3906 (let ((out-fmt-spec (assoc out-fmt (cdr e))))
3907 (when out-fmt-spec
3908 (throw 'done (cons (car e) out-fmt-spec))))))))
3910 (defun org-e-odt-do-convert (in-file out-fmt &optional prefix-arg)
3911 "Workhorse routine for `org-e-odt-convert'."
3912 (require 'browse-url)
3913 (let* ((in-file (expand-file-name (or in-file buffer-file-name)))
3914 (dummy (or (file-readable-p in-file)
3915 (error "Cannot read %s" in-file)))
3916 (in-fmt (file-name-extension in-file))
3917 (out-fmt (or out-fmt (error "Output format unspecified")))
3918 (how (or (org-e-odt-reachable-p in-fmt out-fmt)
3919 (error "Cannot convert from %s format to %s format?"
3920 in-fmt out-fmt)))
3921 (convert-process (car how))
3922 (out-file (concat (file-name-sans-extension in-file) "."
3923 (nth 1 (or (cdr how) out-fmt))))
3924 (extra-options (or (nth 2 (cdr how)) ""))
3925 (out-dir (file-name-directory in-file))
3926 (cmd (format-spec convert-process
3927 `((?i . ,(shell-quote-argument in-file))
3928 (?I . ,(browse-url-file-url in-file))
3929 (?f . ,out-fmt)
3930 (?o . ,out-file)
3931 (?O . ,(browse-url-file-url out-file))
3932 (?d . , (shell-quote-argument out-dir))
3933 (?D . ,(browse-url-file-url out-dir))
3934 (?x . ,extra-options)))))
3935 (when (file-exists-p out-file)
3936 (delete-file out-file))
3938 (message "Executing %s" cmd)
3939 (let ((cmd-output (shell-command-to-string cmd)))
3940 (message "%s" cmd-output))
3942 (cond
3943 ((file-exists-p out-file)
3944 (message "Exported to %s" out-file)
3945 (when prefix-arg
3946 (message "Opening %s..." out-file)
3947 (org-open-file out-file 'system))
3948 out-file)
3950 (message "Export to %s failed" out-file)
3951 nil))))
3953 (defun org-e-odt-do-reachable-formats (in-fmt)
3954 "Return verbose info about formats to which IN-FMT can be converted.
3955 Return a list where each element is of the
3956 form (CONVERTER-PROCESS . OUTPUT-FMT-ALIST). See
3957 `org-e-odt-convert-processes' for CONVERTER-PROCESS and see
3958 `org-e-odt-convert-capabilities' for OUTPUT-FMT-ALIST."
3959 (let* ((converter
3960 (and org-e-odt-convert-process
3961 (cadr (assoc-string org-e-odt-convert-process
3962 org-e-odt-convert-processes t))))
3963 (capabilities
3964 (and org-e-odt-convert-process
3965 (cadr (assoc-string org-e-odt-convert-process
3966 org-e-odt-convert-processes t))
3967 org-e-odt-convert-capabilities))
3968 reachable-formats)
3969 (when converter
3970 (dolist (c capabilities)
3971 (when (member in-fmt (nth 1 c))
3972 (push (cons converter (nth 2 c)) reachable-formats))))
3973 reachable-formats))
3975 (defun org-e-odt-reachable-formats (in-fmt)
3976 "Return list of formats to which IN-FMT can be converted.
3977 The list of the form (OUTPUT-FMT-1 OUTPUT-FMT-2 ...)."
3978 (let (l)
3979 (mapc (lambda (e) (add-to-list 'l e))
3980 (apply 'append (mapcar
3981 (lambda (e) (mapcar 'car (cdr e)))
3982 (org-e-odt-do-reachable-formats in-fmt))))
3985 (defun org-e-odt-convert-read-params ()
3986 "Return IN-FILE and OUT-FMT params for `org-e-odt-do-convert'.
3987 This is a helper routine for interactive use."
3988 (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
3989 (in-file (read-file-name "File to be converted: "
3990 nil buffer-file-name t))
3991 (in-fmt (file-name-extension in-file))
3992 (out-fmt-choices (org-e-odt-reachable-formats in-fmt))
3993 (out-fmt
3994 (or (and out-fmt-choices
3995 (funcall input "Output format: "
3996 out-fmt-choices nil nil nil))
3997 (error
3998 "No known converter or no known output formats for %s files"
3999 in-fmt))))
4000 (list in-file out-fmt)))
4002 ;;;###autoload
4003 (defun org-e-odt-convert (&optional in-file out-fmt prefix-arg)
4004 "Convert IN-FILE to format OUT-FMT using a command line converter.
4005 IN-FILE is the file to be converted. If unspecified, it defaults
4006 to variable `buffer-file-name'. OUT-FMT is the desired output
4007 format. Use `org-e-odt-convert-process' as the converter.
4008 If PREFIX-ARG is non-nil then the newly converted file is opened
4009 using `org-open-file'."
4010 (interactive
4011 (append (org-e-odt-convert-read-params) current-prefix-arg))
4012 (org-e-odt-do-convert in-file out-fmt prefix-arg))
4014 ;;; Library Initializations
4016 (mapc
4017 (lambda (desc)
4018 ;; Let Emacs open all OpenDocument files in archive mode
4019 (add-to-list 'auto-mode-alist
4020 (cons (concat "\\." (car desc) "\\'") 'archive-mode)))
4021 org-e-odt-file-extensions)
4024 ;;; FIXME
4026 ;;;; Links whose description is image
4028 ;; (org-lparse-link-description-is-image
4029 ;; (format "\n<draw:a xlink:type=\"simple\" xlink:href=\"%s\">\n%s\n</draw:a>"
4030 ;; href desc))
4033 (provide 'org-e-odt)
4035 ;;; org-e-odt.el ends here