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