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