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