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