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