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