ox-odt.el: Add support for JabRef
[org-mode/cv.git] / lisp / ox-odt.el
blob326e8b7e53e2afad3f60b0102a5f5f9b2ec4dbfc
1 ;;; ox-odt.el --- OpenDocument Text Exporter for Org Mode
3 ;; Copyright (C) 2010-2015 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 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 'rx)
31 (require 'table nil 'noerror))
32 (require 'format-spec)
33 (require 'ox)
34 (require 'org-compat)
36 ;;; Define Back-End
38 (org-export-define-backend 'odt
39 '((bold . org-odt-bold)
40 (center-block . org-odt-center-block)
41 (clock . org-odt-clock)
42 (code . org-odt-code)
43 (drawer . org-odt-drawer)
44 (dynamic-block . org-odt-dynamic-block)
45 (entity . org-odt-entity)
46 (example-block . org-odt-example-block)
47 (export-block . org-odt-export-block)
48 (export-snippet . org-odt-export-snippet)
49 (fixed-width . org-odt-fixed-width)
50 (footnote-definition . org-odt-footnote-definition)
51 (footnote-reference . org-odt-footnote-reference)
52 (headline . org-odt-headline)
53 (horizontal-rule . org-odt-horizontal-rule)
54 (inline-src-block . org-odt-inline-src-block)
55 (inlinetask . org-odt-inlinetask)
56 (italic . org-odt-italic)
57 (item . org-odt-item)
58 (keyword . org-odt-keyword)
59 (latex-environment . org-odt-latex-environment)
60 (latex-fragment . org-odt-latex-fragment)
61 (line-break . org-odt-line-break)
62 (link . org-odt-link)
63 (node-property . org-odt-node-property)
64 (paragraph . org-odt-paragraph)
65 (plain-list . org-odt-plain-list)
66 (plain-text . org-odt-plain-text)
67 (planning . org-odt-planning)
68 (property-drawer . org-odt-property-drawer)
69 (quote-block . org-odt-quote-block)
70 (radio-target . org-odt-radio-target)
71 (section . org-odt-section)
72 (special-block . org-odt-special-block)
73 (src-block . org-odt-src-block)
74 (statistics-cookie . org-odt-statistics-cookie)
75 (strike-through . org-odt-strike-through)
76 (subscript . org-odt-subscript)
77 (superscript . org-odt-superscript)
78 (table . org-odt-table)
79 (table-cell . org-odt-table-cell)
80 (table-row . org-odt-table-row)
81 (target . org-odt-target)
82 (template . org-odt-template)
83 (timestamp . org-odt-timestamp)
84 (underline . org-odt-underline)
85 (verbatim . org-odt-verbatim)
86 (verse-block . org-odt-verse-block)
87 (citation . org-odt-citation))
88 :export-block "ODT"
89 :filters-alist '((:filter-parse-tree
90 . (org-odt--collect-cite-keys
91 org-odt--translate-latex-fragments
92 org-odt--translate-description-lists ; Dummy symbol
93 org-odt--translate-list-tables)))
94 :menu-entry
95 '(?o "Export to ODT"
96 ((?o "As ODT file" org-odt-export-to-odt)
97 (?O "As ODT file and open"
98 (lambda (a s v b)
99 (if a (org-odt-export-to-odt t s v)
100 (org-open-file (org-odt-export-to-odt nil s v) 'system))))))
101 :options-alist
102 '((:odt-styles-file "ODT_STYLES_FILE" nil nil t)
103 ;; Other variables.
104 (:odt-content-template-file nil nil org-odt-content-template-file)
105 (:odt-display-outline-level nil nil org-odt-display-outline-level)
106 (:odt-fontify-srcblocks nil nil org-odt-fontify-srcblocks)
107 (:odt-format-drawer-function nil nil org-odt-format-drawer-function)
108 (:odt-format-headline-function nil nil org-odt-format-headline-function)
109 (:odt-format-inlinetask-function nil nil org-odt-format-inlinetask-function)
110 (:odt-inline-formula-rules nil nil org-odt-inline-formula-rules)
111 (:odt-inline-image-rules nil nil org-odt-inline-image-rules)
112 (:odt-pixels-per-inch nil nil org-odt-pixels-per-inch)
113 (:odt-styles-file nil nil org-odt-styles-file)
114 (:odt-table-styles nil nil org-odt-table-styles)
115 (:odt-use-date-fields nil nil org-odt-use-date-fields)
116 ;; Redefine regular option.
117 (:with-latex nil "tex" org-odt-with-latex)))
120 ;;; Dependencies
122 ;;; Hooks
124 ;;; Function Declarations
126 (declare-function hfy-face-to-style "htmlfontify" (fn))
127 (declare-function hfy-face-or-def-to-name "htmlfontify" (fn))
128 (declare-function archive-zip-extract "arc-mode" (archive name))
129 (declare-function org-create-math-formula "org" (latex-frag &optional mathml-file))
130 (declare-function browse-url-file-url "browse-url" (file))
134 ;;; Internal Variables
136 (defconst org-odt-lib-dir
137 (file-name-directory (or load-file-name (buffer-file-name)))
138 "Location of ODT exporter.
139 Use this to infer values of `org-odt-styles-dir' and
140 `org-odt-schema-dir'.")
142 (defvar org-odt-data-dir nil
143 "Data directory for ODT exporter.
144 Use this to infer values of `org-odt-styles-dir' and
145 `org-odt-schema-dir'.")
147 (defconst org-odt-special-string-regexps
148 '(("\\\\-" . "&#x00ad;\\1") ; shy
149 ("---\\([^-]\\)" . "&#x2014;\\1") ; mdash
150 ("--\\([^-]\\)" . "&#x2013;\\1") ; ndash
151 ("\\.\\.\\." . "&#x2026;")) ; hellip
152 "Regular expressions for special string conversion.")
154 (defconst org-odt-schema-dir-list
155 (list
156 (and org-odt-data-dir
157 (expand-file-name "./schema/" org-odt-data-dir)) ; bail out
158 (eval-when-compile
159 (and (boundp 'org-odt-data-dir) org-odt-data-dir ; see make install
160 (expand-file-name "./schema/" org-odt-data-dir)))
161 (expand-file-name "../etc/schema/" org-odt-lib-dir) ; git
162 (expand-file-name "./etc/schema/" org-odt-lib-dir) ; elpa
164 "List of directories to search for OpenDocument schema files.
165 Use this list to set the default value of
166 `org-odt-schema-dir'. The entries in this list are
167 populated heuristically based on the values of `org-odt-lib-dir'
168 and `org-odt-data-dir'.")
170 (defconst org-odt-styles-dir-list
171 (list
172 (and org-odt-data-dir
173 (expand-file-name "./styles/" org-odt-data-dir)) ; bail out
174 (eval-when-compile
175 (and (boundp 'org-odt-data-dir) org-odt-data-dir ; see make install
176 (expand-file-name "./styles/" org-odt-data-dir)))
177 (expand-file-name "../etc/styles/" org-odt-lib-dir) ; git
178 (expand-file-name "./etc/styles/" org-odt-lib-dir) ; elpa
179 (expand-file-name "./org/" data-directory) ; system
181 "List of directories to search for OpenDocument styles files.
182 See `org-odt-styles-dir'. The entries in this list are populated
183 heuristically based on the values of `org-odt-lib-dir' and
184 `org-odt-data-dir'.")
186 (defconst org-odt-styles-dir
187 (let* ((styles-dir
188 (catch 'styles-dir
189 (message "Debug (ox-odt): Searching for OpenDocument styles files...")
190 (mapc (lambda (styles-dir)
191 (when styles-dir
192 (message "Debug (ox-odt): Trying %s..." styles-dir)
193 (when (and (file-readable-p
194 (expand-file-name
195 "OrgOdtContentTemplate.xml" styles-dir))
196 (file-readable-p
197 (expand-file-name
198 "OrgOdtStyles.xml" styles-dir)))
199 (message "Debug (ox-odt): Using styles under %s"
200 styles-dir)
201 (throw 'styles-dir styles-dir))))
202 org-odt-styles-dir-list)
203 nil)))
204 (unless styles-dir
205 (error "Error (ox-odt): Cannot find factory styles files, aborting"))
206 styles-dir)
207 "Directory that holds auxiliary XML files used by the ODT exporter.
209 This directory contains the following XML files -
210 \"OrgOdtStyles.xml\" and \"OrgOdtContentTemplate.xml\". These
211 XML files are used as the default values of
212 `org-odt-styles-file' and `org-odt-content-template-file'.
214 The default value of this variable varies depending on the
215 version of org in use and is initialized from
216 `org-odt-styles-dir-list'. Note that the user could be using org
217 from one of: org's own private git repository, GNU ELPA tar or
218 standard Emacs.")
220 (defconst org-odt-manifest-file-entry-tag
221 "\n<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
223 (defconst org-odt-file-extensions
224 '(("odt" . "OpenDocument Text")
225 ("ott" . "OpenDocument Text Template")
226 ("odm" . "OpenDocument Master Document")
227 ("ods" . "OpenDocument Spreadsheet")
228 ("ots" . "OpenDocument Spreadsheet Template")
229 ("odg" . "OpenDocument Drawing (Graphics)")
230 ("otg" . "OpenDocument Drawing Template")
231 ("odp" . "OpenDocument Presentation")
232 ("otp" . "OpenDocument Presentation Template")
233 ("odi" . "OpenDocument Image")
234 ("odf" . "OpenDocument Formula")
235 ("odc" . "OpenDocument Chart")))
237 (defconst org-odt-table-style-format
239 <style:style style:name=\"%s\" style:family=\"table\">
240 <style:table-properties style:rel-width=\"%d%%\" fo:margin-top=\"0cm\" fo:margin-bottom=\"0.20cm\" table:align=\"center\"/>
241 </style:style>
243 "Template for auto-generated Table styles.")
245 (defvar org-odt-automatic-styles '()
246 "Registry of automatic styles for various OBJECT-TYPEs.
247 The variable has the following form:
248 \(\(OBJECT-TYPE-A
249 \(\(OBJECT-NAME-A.1 OBJECT-PROPS-A.1\)
250 \(OBJECT-NAME-A.2 OBJECT-PROPS-A.2\) ...\)\)
251 \(OBJECT-TYPE-B
252 \(\(OBJECT-NAME-B.1 OBJECT-PROPS-B.1\)
253 \(OBJECT-NAME-B.2 OBJECT-PROPS-B.2\) ...\)\)
254 ...\).
256 OBJECT-TYPEs could be \"Section\", \"Table\", \"Figure\" etc.
257 OBJECT-PROPS is (typically) a plist created by passing
258 \"#+ATTR_ODT: \" option to `org-odt-parse-block-attributes'.
260 Use `org-odt-add-automatic-style' to add update this variable.'")
262 (defvar org-odt-object-counters nil
263 "Running counters for various OBJECT-TYPEs.
264 Use this to generate automatic names and style-names. See
265 `org-odt-add-automatic-style'.")
267 (defvar org-odt-src-block-paragraph-format
268 "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
269 <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
270 <style:background-image/>
271 </style:paragraph-properties>
272 <style:text-properties fo:color=\"%s\"/>
273 </style:style>"
274 "Custom paragraph style for colorized source and example blocks.
275 This style is much the same as that of \"OrgFixedWidthBlock\"
276 except that the foreground and background colors are set
277 according to the default face identified by the `htmlfontify'.")
279 (defvar hfy-optimisations)
280 (defvar org-odt-embedded-formulas-count 0)
281 (defvar org-odt-embedded-images-count 0)
282 (defvar org-odt-image-size-probe-method
283 (append (and (executable-find "identify") '(imagemagick)) ; See Bug#10675
284 '(emacs fixed))
285 "Ordered list of methods for determining image sizes.")
287 (defvar org-odt-default-image-sizes-alist
288 '(("as-char" . (5 . 0.4))
289 ("paragraph" . (5 . 5)))
290 "Hardcoded image dimensions one for each of the anchor
291 methods.")
293 ;; A4 page size is 21.0 by 29.7 cms
294 ;; The default page settings has 2cm margin on each of the sides. So
295 ;; the effective text area is 17.0 by 25.7 cm
296 (defvar org-odt-max-image-size '(17.0 . 20.0)
297 "Limiting dimensions for an embedded image.")
299 (defvar org-odt-category-map-alist
300 '((:TABLE: "Table" "Table" org-odt--enumerable-p )
301 (:FIGURE: "Illustration" "Figure" org-odt--enumerable-image-p )
302 (:MATH-FORMULA: "Text" "Equation" org-odt--enumerable-formula-p )
303 (:DVIPNG-IMAGE: "Equation" "Equation" org-odt--enumerable-latex-image-p)
304 (:LISTING: "Listing" "Listing" org-odt--enumerable-p ))
305 "Map a CATEGORY-HANDLE to OD-VARIABLE and LABEL-STYLE.
307 This is a list where each entry is of the form:
309 \(CATEGORY-HANDLE OD-VARIABLE CATEGORY-NAME ENUMERATOR-PREDICATE)
311 CATEGORY_HANDLE identifies the captionable entity in question.
313 OD-VARIABLE is the OpenDocument sequence counter associated with
314 the entity. These counters are declared within
315 \"<text:sequence-decls>...</text:sequence-decls>\" block of
316 `org-odt-content-template-file'.
318 CATEGORY-NAME is used for qualifying captions on export.
320 ENUMERATOR-PREDICATE is used for assigning a sequence number to
321 the entity. See `org-odt--enumerate'.")
323 (defvar org-odt-manifest-file-entries nil)
324 (defvar hfy-user-sheet-assoc)
326 (defvar org-odt-zip-dir nil
327 "Temporary work directory for OpenDocument exporter.")
331 ;;; User Configuration Variables
333 (defgroup org-export-odt nil
334 "Options for exporting Org mode files to ODT."
335 :tag "Org Export ODT"
336 :group 'org-export)
339 ;;;; Debugging
341 (defcustom org-odt-prettify-xml nil
342 "Specify whether or not the xml output should be prettified.
343 When this option is turned on, `indent-region' is run on all
344 component xml buffers before they are saved. Turn this off for
345 regular use. Turn this on if you need to examine the xml
346 visually."
347 :group 'org-export-odt
348 :version "24.1"
349 :type 'boolean)
352 ;;;; Document schema
354 (require 'rng-loc)
355 (defcustom org-odt-schema-dir
356 (let* ((schema-dir
357 (catch 'schema-dir
358 (message "Debug (ox-odt): Searching for OpenDocument schema files...")
359 (mapc
360 (lambda (schema-dir)
361 (when schema-dir
362 (message "Debug (ox-odt): Trying %s..." schema-dir)
363 (when (and (file-expand-wildcards
364 (expand-file-name "od-manifest-schema*.rnc"
365 schema-dir))
366 (file-expand-wildcards
367 (expand-file-name "od-schema*.rnc"
368 schema-dir))
369 (file-readable-p
370 (expand-file-name "schemas.xml" schema-dir)))
371 (message "Debug (ox-odt): Using schema files under %s"
372 schema-dir)
373 (throw 'schema-dir schema-dir))))
374 org-odt-schema-dir-list)
375 (message "Debug (ox-odt): No OpenDocument schema files installed")
376 nil)))
377 schema-dir)
378 "Directory that contains OpenDocument schema files.
380 This directory contains:
381 1. rnc files for OpenDocument schema
382 2. a \"schemas.xml\" file that specifies locating rules needed
383 for auto validation of OpenDocument XML files.
385 Use the customize interface to set this variable. This ensures
386 that `rng-schema-locating-files' is updated and auto-validation
387 of OpenDocument XML takes place based on the value
388 `rng-nxml-auto-validate-flag'.
390 The default value of this variable varies depending on the
391 version of org in use and is initialized from
392 `org-odt-schema-dir-list'. The OASIS schema files are available
393 only in the org's private git repository. It is *not* bundled
394 with GNU ELPA tar or standard Emacs distribution."
395 :type '(choice
396 (const :tag "Not set" nil)
397 (directory :tag "Schema directory"))
398 :group 'org-export-odt
399 :version "24.1"
400 :set
401 (lambda (var value)
402 "Set `org-odt-schema-dir'.
403 Also add it to `rng-schema-locating-files'."
404 (let ((schema-dir value))
405 (set var
406 (if (and
407 (file-expand-wildcards
408 (expand-file-name "od-manifest-schema*.rnc" schema-dir))
409 (file-expand-wildcards
410 (expand-file-name "od-schema*.rnc" schema-dir))
411 (file-readable-p
412 (expand-file-name "schemas.xml" schema-dir)))
413 schema-dir
414 (when value
415 (message "Error (ox-odt): %s has no OpenDocument schema files"
416 value))
417 nil)))
418 (when org-odt-schema-dir
419 (eval-after-load 'rng-loc
420 '(add-to-list 'rng-schema-locating-files
421 (expand-file-name "schemas.xml"
422 org-odt-schema-dir))))))
425 ;;;; Document styles
427 (defcustom org-odt-content-template-file nil
428 "Template file for \"content.xml\".
429 The exporter embeds the exported content just before
430 \"</office:text>\" element.
432 If unspecified, the file named \"OrgOdtContentTemplate.xml\"
433 under `org-odt-styles-dir' is used."
434 :type '(choice (const nil)
435 (file))
436 :group 'org-export-odt
437 :version "24.3")
439 (defcustom org-odt-styles-file nil
440 "Default styles file for use with ODT export.
441 Valid values are one of:
442 1. nil
443 2. path to a styles.xml file
444 3. path to a *.odt or a *.ott file
445 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
446 ...))
448 In case of option 1, an in-built styles.xml is used. See
449 `org-odt-styles-dir' for more information.
451 In case of option 3, the specified file is unzipped and the
452 styles.xml embedded therein is used.
454 In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
455 and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
456 generated odt file. Use relative path for specifying the
457 FILE-MEMBERS. styles.xml must be specified as one of the
458 FILE-MEMBERS.
460 Use options 1, 2 or 3 only if styles.xml alone suffices for
461 achieving the desired formatting. Use option 4, if the styles.xml
462 references additional files like header and footer images for
463 achieving the desired formatting.
465 Use \"#+ODT_STYLES_FILE: ...\" directive to set this variable on
466 a per-file basis. For example,
468 #+ODT_STYLES_FILE: \"/path/to/styles.xml\" or
469 #+ODT_STYLES_FILE: (\"/path/to/file.ott\" (\"styles.xml\" \"image/hdr.png\"))."
470 :group 'org-export-odt
471 :version "24.1"
472 :type
473 '(choice
474 (const :tag "Factory settings" nil)
475 (file :must-match t :tag "styles.xml")
476 (file :must-match t :tag "ODT or OTT file")
477 (list :tag "ODT or OTT file + Members"
478 (file :must-match t :tag "ODF Text or Text Template file")
479 (cons :tag "Members"
480 (file :tag " Member" "styles.xml")
481 (repeat (file :tag "Member"))))))
483 (defcustom org-odt-display-outline-level 2
484 "Outline levels considered for enumerating captioned entities."
485 :group 'org-export-odt
486 :version "24.4"
487 :package-version '(Org . "8.0")
488 :type 'integer)
490 ;;;; Document conversion
492 (defcustom org-odt-convert-processes
493 '(("LibreOffice"
494 "soffice --headless --convert-to %f%x --outdir %d %i")
495 ("unoconv"
496 "unoconv -f %f -o %d %i"))
497 "Specify a list of document converters and their usage.
498 The converters in this list are offered as choices while
499 customizing `org-odt-convert-process'.
501 This variable is a list where each element is of the
502 form (CONVERTER-NAME CONVERTER-CMD). CONVERTER-NAME is the name
503 of the converter. CONVERTER-CMD is the shell command for the
504 converter and can contain format specifiers. These format
505 specifiers are interpreted as below:
507 %i input file name in full
508 %I input file name as a URL
509 %f format of the output file
510 %o output file name in full
511 %O output file name as a URL
512 %d output dir in full
513 %D output dir as a URL.
514 %x extra options as set in `org-odt-convert-capabilities'."
515 :group 'org-export-odt
516 :version "24.1"
517 :type
518 '(choice
519 (const :tag "None" nil)
520 (alist :tag "Converters"
521 :key-type (string :tag "Converter Name")
522 :value-type (group (string :tag "Command line")))))
524 (defcustom org-odt-convert-process "LibreOffice"
525 "Use this converter to convert from \"odt\" format to other formats.
526 During customization, the list of converter names are populated
527 from `org-odt-convert-processes'."
528 :group 'org-export-odt
529 :version "24.1"
530 :type '(choice :convert-widget
531 (lambda (w)
532 (apply 'widget-convert (widget-type w)
533 (eval (car (widget-get w :args)))))
534 `((const :tag "None" nil)
535 ,@(mapcar (lambda (c)
536 `(const :tag ,(car c) ,(car c)))
537 org-odt-convert-processes))))
539 (defcustom org-odt-convert-capabilities
540 '(("Text"
541 ("odt" "ott" "doc" "rtf" "docx")
542 (("pdf" "pdf") ("odt" "odt") ("rtf" "rtf") ("ott" "ott")
543 ("doc" "doc" ":\"MS Word 97\"") ("docx" "docx") ("html" "html")
544 ("txt" "txt" ":\"Text (encoded)\"")))
545 ("Web"
546 ("html")
547 (("pdf" "pdf") ("odt" "odt") ("html" "html")))
548 ("Spreadsheet"
549 ("ods" "ots" "xls" "csv" "xlsx")
550 (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv") ("ods" "ods")
551 ("xls" "xls") ("xlsx" "xlsx")))
552 ("Presentation"
553 ("odp" "otp" "ppt" "pptx")
554 (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("otp" "otp") ("ppt" "ppt")
555 ("pptx" "pptx") ("odg" "odg"))))
556 "Specify input and output formats of `org-odt-convert-process'.
557 More correctly, specify the set of input and output formats that
558 the user is actually interested in.
560 This variable is an alist where each element is of the
561 form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
562 INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
563 alist where each element is of the form (OUTPUT-FMT
564 OUTPUT-FILE-EXTENSION EXTRA-OPTIONS).
566 The variable is interpreted as follows:
567 `org-odt-convert-process' can take any document that is in
568 INPUT-FMT-LIST and produce any document that is in the
569 OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
570 OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
571 serves dual purposes:
572 - It is used for populating completion candidates during
573 `org-odt-convert' commands.
574 - It is used as the value of \"%f\" specifier in
575 `org-odt-convert-process'.
577 EXTRA-OPTIONS is used as the value of \"%x\" specifier in
578 `org-odt-convert-process'.
580 DOCUMENT-CLASS is used to group a set of file formats in
581 INPUT-FMT-LIST in to a single class.
583 Note that this variable inherently captures how LibreOffice based
584 converters work. LibreOffice maps documents of various formats
585 to classes like Text, Web, Spreadsheet, Presentation etc and
586 allow document of a given class (irrespective of its source
587 format) to be converted to any of the export formats associated
588 with that class.
590 See default setting of this variable for an typical
591 configuration."
592 :group 'org-export-odt
593 :version "24.1"
594 :type
595 '(choice
596 (const :tag "None" nil)
597 (alist :tag "Capabilities"
598 :key-type (string :tag "Document Class")
599 :value-type
600 (group (repeat :tag "Input formats" (string :tag "Input format"))
601 (alist :tag "Output formats"
602 :key-type (string :tag "Output format")
603 :value-type
604 (group (string :tag "Output file extension")
605 (choice
606 (const :tag "None" nil)
607 (string :tag "Extra options"))))))))
609 (defcustom org-odt-preferred-output-format nil
610 "Automatically post-process to this format after exporting to \"odt\".
611 Command `org-odt-export-to-odt' exports first to \"odt\" format
612 and then uses `org-odt-convert-process' to convert the
613 resulting document to this format. During customization of this
614 variable, the list of valid values are populated based on
615 `org-odt-convert-capabilities'.
617 You can set this option on per-file basis using file local
618 values. See Info node `(emacs) File Variables'."
619 :group 'org-export-odt
620 :version "24.1"
621 :type '(choice :convert-widget
622 (lambda (w)
623 (apply 'widget-convert (widget-type w)
624 (eval (car (widget-get w :args)))))
625 `((const :tag "None" nil)
626 ,@(mapcar (lambda (c)
627 `(const :tag ,c ,c))
628 (org-odt-reachable-formats "odt")))))
629 ;;;###autoload
630 (put 'org-odt-preferred-output-format 'safe-local-variable 'stringp)
633 ;;;; Drawers
635 (defcustom org-odt-format-drawer-function
636 (lambda (name contents) contents)
637 "Function called to format a drawer in ODT code.
639 The function must accept two parameters:
640 NAME the drawer name, like \"LOGBOOK\"
641 CONTENTS the contents of the drawer.
643 The function should return the string to be exported.
645 The default value simply returns the value of CONTENTS."
646 :group 'org-export-odt
647 :version "24.4"
648 :package-version '(Org . "8.3")
649 :type 'function)
652 ;;;; Headline
654 (defcustom org-odt-format-headline-function
655 'org-odt-format-headline-default-function
656 "Function to format headline text.
658 This function will be called with 5 arguments:
659 TODO the todo keyword \(string or nil\).
660 TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
661 PRIORITY the priority of the headline \(integer or nil\)
662 TEXT the main headline text \(string\).
663 TAGS the tags string, separated with colons \(string or nil\).
665 The function result will be used as headline text."
666 :group 'org-export-odt
667 :version "25.1"
668 :package-version '(Org . "8.3")
669 :type 'function)
672 ;;;; Inlinetasks
674 (defcustom org-odt-format-inlinetask-function
675 'org-odt-format-inlinetask-default-function
676 "Function called to format an inlinetask in ODT code.
678 The function must accept six parameters:
679 TODO the todo keyword, as a string
680 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
681 PRIORITY the inlinetask priority, as a string
682 NAME the inlinetask name, as a string.
683 TAGS the inlinetask tags, as a string.
684 CONTENTS the contents of the inlinetask, as a string.
686 The function should return the string to be exported."
687 :group 'org-export-odt
688 :version "25.1"
689 :package-version '(Org . "8.3")
690 :type 'function)
693 ;;;; LaTeX
695 (defcustom org-odt-with-latex org-export-with-latex
696 "Non-nil means process LaTeX math snippets.
698 When set, the exporter will process LaTeX environments and
699 fragments.
701 This option can also be set with the +OPTIONS line,
702 e.g. \"tex:mathjax\". Allowed values are:
704 nil Ignore math snippets.
705 `verbatim' Keep everything in verbatim
706 `dvipng' Process the LaTeX fragments to images. This will also
707 include processing of non-math environments.
708 `imagemagick' Convert the LaTeX fragments to pdf files and use
709 imagemagick to convert pdf files to png files.
710 `mathjax' Do MathJax preprocessing and arrange for MathJax.js to
711 be loaded.
712 t Synonym for `mathjax'."
713 :group 'org-export-odt
714 :version "24.4"
715 :package-version '(Org . "8.0")
716 :type '(choice
717 (const :tag "Do not process math in any way" nil)
718 (const :tag "Use dvipng to make images" dvipng)
719 (const :tag "Use imagemagick to make images" imagemagick)
720 (const :tag "Use MathJax to display math" mathjax)
721 (const :tag "Leave math verbatim" verbatim)))
724 ;;;; Links
726 (defcustom org-odt-inline-formula-rules
727 '(("file" . "\\.\\(mathml\\|mml\\|odf\\)\\'"))
728 "Rules characterizing formula files that can be inlined into ODT.
730 A rule consists in an association whose key is the type of link
731 to consider, and value is a regexp that will be matched against
732 link's path."
733 :group 'org-export-odt
734 :version "24.4"
735 :package-version '(Org . "8.0")
736 :type '(alist :key-type (string :tag "Type")
737 :value-type (regexp :tag "Path")))
739 (defcustom org-odt-inline-image-rules
740 '(("file" . "\\.\\(jpeg\\|jpg\\|png\\|gif\\)\\'"))
741 "Rules characterizing image files that can be inlined into ODT.
743 A rule consists in an association whose key is the type of link
744 to consider, and value is a regexp that will be matched against
745 link's path."
746 :group 'org-export-odt
747 :version "24.4"
748 :package-version '(Org . "8.0")
749 :type '(alist :key-type (string :tag "Type")
750 :value-type (regexp :tag "Path")))
752 (defcustom org-odt-pixels-per-inch 96.0
753 "Scaling factor for converting images pixels to inches.
754 Use this for sizing of embedded images. See Info node `(org)
755 Images in ODT export' for more information."
756 :type 'float
757 :group 'org-export-odt
758 :version "24.4"
759 :package-version '(Org . "8.1"))
761 (defcustom org-odt-caption-and-xref-settings
762 '((:LISTING: :caption-position below :caption-format
763 (category " " counter ": " caption)
764 :xref-format
765 (value))
766 (:DVIPNG-IMAGE: :caption-position below :caption-format
767 (category " " counter ": " caption)
768 :xref-format
769 (value))
770 (:MATH-FORMULA: :caption-position below :caption-format
771 (caption)
772 :xref-format
773 (text)
774 :label-format
775 ("(" counter ")"))
776 (:FIGURE: :caption-position below :caption-format
777 (category " " counter ": " caption)
778 :xref-format
779 (value))
780 (:TABLE: :caption-position below :caption-format
781 (category " " counter ": " caption)
782 :xref-format
783 (value)))
784 "Specify how to format caption and cross-references.
786 Use this, for example, to control various aspects of caption (the
787 numbering format, its position etc.) or to generate page numbers
788 as part of cross-references. For a quick overview of this
789 variable, see examples towards the end of this docstring.
791 If you customize this option, the following text—\"[PLS. UPDATE
792 FIELDS]\"—is used as a placeholder for unresolvable
793 cross-reference fields (like page number etc). Use an external
794 application to synchronize these fields to their right values.
795 When using LibreOffice, use Tools -> Update-> Fields / Update
796 All.
798 This variable is an alist of pairs (RULE-TAG . RULE-PLIST).
799 RULE-TAG is a symbol. RULE-PLIST is a property list, the allowed
800 properties of which depend on the value of RULE-TAG. The details
801 are as below.
803 RULE-TAG takes following one of the values:
805 `:TABLE:' `:FIGURE:' `:MATH-FORMULA:' `:DVIPNG-IMAGE:'
806 `:LISTING:' `:TARGET:'.
808 The `:TARGET:' rule specifies how a cross-reference to a
809 HEADLINE, a TARGET or a non-captionable ELEMENT is typeset. Its
810 RULE-PLIST allow a single property `:xref-format'.
812 All RULE-TAGs (except for `:TARGET:') specify how a caption and a
813 cross-reference to the corresponding entity is typeset. Its
814 RULE-PLIST allow following properties.
816 `:caption-position' - a symbol - one of `above' or `below'
817 `:caption-format' - a mixed list of symbols and strings
818 `:xref-format' - a mixed list of symbols and strings
820 `:caption-format' and `:xref-format' are but format
821 specifiers (in disguise) and specify how a caption or a
822 cross-reference is transcoded. Their form and function are
823 better illustrated than described. So, consider the following
824 examples:
826 A `:caption-format' with the following value
828 (category \" \" counter \": \" caption)
830 will result in following caption.
832 Table 1: An Example Table
833 ^^^^^ ^ ^^^^^^^^^^^^^^^^
834 ^ | ^
835 | | |
836 category | caption
837 counter
839 A `:xref-format' with the following value
841 (\"Section \" chapter \" [\" text \"]\", \" page, \" t)
843 will result in following cross-reference.
845 See Section 3.1 [Tropical Storms], page 24.
846 ^ ^^^^^^^^^^^^^^^ ^^^
847 | | |
848 chapter no. chapter title page number
850 See `org-odt-link--infer-description' (specifically
851 `org-odt--xref-target') and `org-odt-format-label' for
852 implementation details."
853 :type
854 `(alist :options
855 ((:TARGET:
856 (plist :options
857 ((:xref-format
858 (choice
859 (const :tag "Simple page number" ("page " t))
860 (const :tag "TexInfo style"
861 ("Section " chapter " [" text "]," " page " t))
862 (repeat :tag "Format string"
863 (choice
864 (const :tag "Chapter" chapter)
865 (const :tag "Direction" direction)
866 (const :tag "Number" number)
867 (const :tag "Number (All superior)" number-all-superior)
868 (const :tag "Number (No superior)" number-no-superior)
869 (const :tag "Page" page)
870 (const :tag "Page style" t)
871 (const :tag "Text" text)
872 (string :tag "String" ""))))))))
873 ,@(mapcar
874 (lambda (dc)
875 `(,dc
876 (plist :options
877 ((:caption-position
878 (choice (const :tag "Below" below)
879 (const :tag "Above" above)))
880 (:caption-format
881 (repeat (choice
882 (const :tag "Category" category)
883 (const :tag "Counter" counter)
884 (const :tag "Caption" caption)
885 (string :tag "String" ""))))
886 (:xref-format
887 (repeat :tag "Format string"
888 (choice
889 (const :tag "Caption" caption)
890 (const :tag "Category & Value" category-and-value)
891 (const :tag "Chapter" chapter)
892 (const :tag "Direction" direction)
893 (const :tag "Page" page)
894 (const :tag "Text" text)
895 (const :tag "Value" value)
896 (string :tag "String" "")))))
898 '(:TABLE: :FIGURE: :MATH-FORMULA: :DVIPNG-IMAGE: :LISTING:))))
899 :group 'org-export-odt
900 :version "24.4")
902 ;;;; Lists
904 (defcustom org-odt-description-list-style #'org-odt--translate-description-lists/html
905 "Specify how description lists are rendered.
906 Choose one of HTML or LaTeX style."
907 :type '(choice
908 (const :tag "Use HTML style" org-odt--translate-description-lists/html )
909 (const :tag "Use LaTeX style" org-odt--translate-description-lists/latex ))
910 :group 'org-export-odt
911 :set (lambda (symbol value)
912 "Alias `org-odt--translate-description-lists'."
913 (set-default symbol value)
914 (fset 'org-odt--translate-description-lists value))
915 :version "24.1")
918 ;;;; Src Block
920 (defcustom org-odt-create-custom-styles-for-srcblocks t
921 "Whether custom styles for colorized source blocks be automatically created.
922 When this option is turned on, the exporter creates custom styles
923 for source blocks based on the advice of `htmlfontify'. Creation
924 of custom styles happen as part of `org-odt-hfy-face-to-css'.
926 When this option is turned off exporter does not create such
927 styles.
929 Use the latter option if you do not want the custom styles to be
930 based on your current display settings. It is necessary that the
931 styles.xml already contains needed styles for colorizing to work.
933 This variable is effective only if `org-odt-fontify-srcblocks' is
934 turned on."
935 :group 'org-export-odt
936 :version "24.1"
937 :type 'boolean)
939 (defcustom org-odt-fontify-srcblocks t
940 "Specify whether or not source blocks need to be fontified.
941 Turn this option on if you want to colorize the source code
942 blocks in the exported file. For colorization to work, you need
943 to make available an enhanced version of `htmlfontify' library."
944 :type 'boolean
945 :group 'org-export-odt
946 :version "24.1")
949 ;;;; Table
951 (defcustom org-odt-table-styles
952 '(("OrgEquation" "OrgEquation"
953 ((use-first-column-styles . t)
954 (use-last-column-styles . t)))
955 ("TableWithHeaderRowAndColumn" "Custom"
956 ((use-first-row-styles . t)
957 (use-first-column-styles . t)))
958 ("TableWithFirstRowandLastRow" "Custom"
959 ((use-first-row-styles . t)
960 (use-last-row-styles . t)))
961 ("GriddedTable" "Custom" nil))
962 "Specify how Table Styles should be derived from a Table Template.
963 This is a list where each element is of the
964 form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
966 TABLE-STYLE-NAME is the style associated with the table through
967 \"#+ATTR_ODT: :style TABLE-STYLE-NAME\" line.
969 TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
970 TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
971 below) that is included in `org-odt-content-template-file'.
973 TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
974 \"TableCell\"
975 PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
976 \"TableParagraph\"
977 TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
978 \"FirstRow\" | \"LastRow\" |
979 \"EvenRow\" | \"OddRow\" |
980 \"EvenColumn\" | \"OddColumn\" | \"\"
981 where \"+\" above denotes string concatenation.
983 TABLE-CELL-OPTIONS is an alist where each element is of the
984 form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
985 TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
986 `use-last-row-styles' |
987 `use-first-column-styles' |
988 `use-last-column-styles' |
989 `use-banding-rows-styles' |
990 `use-banding-columns-styles' |
991 `use-first-row-styles'
992 ON-OR-OFF := `t' | `nil'
994 For example, with the following configuration
996 \(setq org-odt-table-styles
997 '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
998 \(\(use-first-row-styles . t\)
999 \(use-first-column-styles . t\)\)\)
1000 \(\"TableWithHeaderColumns\" \"Custom\"
1001 \(\(use-first-column-styles . t\)\)\)\)\)
1003 1. A table associated with \"TableWithHeaderRowsAndColumns\"
1004 style will use the following table-cell styles -
1005 \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
1006 \"CustomTableCell\" and the following paragraph styles
1007 \"CustomFirstRowTableParagraph\",
1008 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
1009 as appropriate.
1011 2. A table associated with \"TableWithHeaderColumns\" style will
1012 use the following table-cell styles -
1013 \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
1014 following paragraph styles
1015 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
1016 as appropriate..
1018 Note that TABLE-TEMPLATE-NAME corresponds to the
1019 \"<table:table-template>\" elements contained within
1020 \"<office:styles>\". The entries (TABLE-STYLE-NAME
1021 TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
1022 \"table:template-name\" and \"table:use-first-row-styles\" etc
1023 attributes of \"<table:table>\" element. Refer ODF-1.2
1024 specification for more information. Also consult the
1025 implementation filed under `org-odt-get-table-cell-styles'.
1027 The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
1028 formatting of numbered display equations. Do not delete this
1029 style from the list."
1030 :group 'org-export-odt
1031 :version "24.1"
1032 :type '(choice
1033 (const :tag "None" nil)
1034 (repeat :tag "Table Styles"
1035 (list :tag "Table Style Specification"
1036 (string :tag "Table Style Name")
1037 (string :tag "Table Template Name")
1038 (alist :options (use-first-row-styles
1039 use-last-row-styles
1040 use-first-column-styles
1041 use-last-column-styles
1042 use-banding-rows-styles
1043 use-banding-columns-styles)
1044 :key-type symbol
1045 :value-type (const :tag "True" t))))))
1047 ;;;; Timestamps
1049 (defcustom org-odt-use-date-fields nil
1050 "Non-nil, if timestamps should be exported as date fields.
1052 When nil, export timestamps as plain text.
1054 When non-nil, map `org-time-stamp-custom-formats' to a pair of
1055 OpenDocument date-styles with names \"OrgDate1\" and \"OrgDate2\"
1056 respectively. A timestamp with no time component is formatted
1057 with style \"OrgDate1\" while one with explicit hour and minutes
1058 is formatted with style \"OrgDate2\".
1060 This feature is experimental. Most (but not all) of the common
1061 %-specifiers in `format-time-string' are supported.
1062 Specifically, locale-dependent specifiers like \"%c\", \"%x\" are
1063 formatted as canonical Org timestamps. For finer control, avoid
1064 these %-specifiers.
1066 Textual specifiers like \"%b\", \"%h\", \"%B\", \"%a\", \"%A\"
1067 etc., are displayed by the application in the default language
1068 and country specified in `org-odt-styles-file'. Note that the
1069 default styles file uses language \"en\" and country \"GB\". You
1070 can localize the week day and month strings in the exported
1071 document by setting the default language and country either using
1072 the application UI or through a custom styles file.
1074 See `org-odt--build-date-styles' for implementation details."
1075 :group 'org-export-odt
1076 :version "24.4"
1077 :package-version '(Org . "8.0")
1078 :type 'boolean)
1082 ;;; Internal functions
1084 ;;;; Date
1086 (defun org-odt--format-timestamp (timestamp &optional end iso-date-p)
1087 (let* ((format-timestamp
1088 (lambda (timestamp format &optional end utc)
1089 (if timestamp
1090 (org-timestamp-format timestamp format end utc)
1091 (format-time-string format nil utc))))
1092 (has-time-p (or (not timestamp)
1093 (org-timestamp-has-time-p timestamp)))
1094 (iso-date (let ((format (if has-time-p "%Y-%m-%dT%H:%M:%S"
1095 "%Y-%m-%dT%H:%M:%S")))
1096 (funcall format-timestamp timestamp format end))))
1097 (if iso-date-p iso-date
1098 (let* ((style (if has-time-p "OrgDate2" "OrgDate1"))
1099 ;; LibreOffice does not care about end goes as content
1100 ;; within the "<text:date>...</text:date>" field. The
1101 ;; displayed date is automagically corrected to match the
1102 ;; format requested by "style:data-style-name" attribute. So
1103 ;; don't bother about formatting the date contents to be
1104 ;; compatible with "OrgDate1" and "OrgDateTime" styles. A
1105 ;; simple Org-style date should suffice.
1106 (date (let* ((formats
1107 (if org-display-custom-times
1108 (cons (substring
1109 (car org-time-stamp-custom-formats) 1 -1)
1110 (substring
1111 (cdr org-time-stamp-custom-formats) 1 -1))
1112 '("%Y-%m-%d %a" . "%Y-%m-%d %a %H:%M")))
1113 (format (if has-time-p (cdr formats) (car formats))))
1114 (funcall format-timestamp timestamp format end)))
1115 (repeater (let ((repeater-type (org-element-property
1116 :repeater-type timestamp))
1117 (repeater-value (org-element-property
1118 :repeater-value timestamp))
1119 (repeater-unit (org-element-property
1120 :repeater-unit timestamp)))
1121 (concat
1122 (case repeater-type
1123 (catchup "++") (restart ".+") (cumulate "+"))
1124 (when repeater-value
1125 (number-to-string repeater-value))
1126 (case repeater-unit
1127 (hour "h") (day "d") (week "w") (month "m")
1128 (year "y"))))))
1129 (concat
1130 (format "<text:date text:date-value=\"%s\" style:data-style-name=\"%s\" text:fixed=\"true\">%s</text:date>"
1131 iso-date style date)
1132 (and (not (string= repeater "")) " ")
1133 repeater)))))
1135 ;;;; Frame
1137 (defun org-odt--frame (text width height style &optional extra
1138 anchor-type &rest title-and-desc)
1139 (let* ((frame-name (car (org-odt-add-automatic-style "Frame")))
1140 (frame-attrs
1141 (concat
1142 (if width (format " svg:width=\"%0.2fcm\"" width) "")
1143 (if height (format " svg:height=\"%0.2fcm\"" height) "")
1144 extra
1145 (format " text:anchor-type=\"%s\"" (or anchor-type "paragraph"))
1146 (format " draw:name=\"%s\"" frame-name))))
1147 (format
1148 "\n<draw:frame draw:style-name=\"%s\"%s>\n%s\n</draw:frame>"
1149 style frame-attrs
1150 (concat text
1151 (let ((title (car title-and-desc))
1152 (desc (cadr title-and-desc)))
1153 (concat (when title
1154 (format "<svg:title>%s</svg:title>"
1155 (org-odt--encode-plain-text title t)))
1156 (when desc
1157 (format "<svg:desc>%s</svg:desc>"
1158 (org-odt--encode-plain-text desc t)))))))))
1161 ;;;; Library wrappers :: Arc Mode
1163 (defun org-odt--zip-extract (archive members target)
1164 (when (atom members) (setq members (list members)))
1165 (mapc (lambda (member)
1166 (require 'arc-mode)
1167 (let* ((--quote-file-name
1168 ;; This is shamelessly stolen from `archive-zip-extract'.
1169 (lambda (name)
1170 (if (or (not (memq system-type '(windows-nt ms-dos)))
1171 (and (boundp 'w32-quote-process-args)
1172 (null w32-quote-process-args)))
1173 (shell-quote-argument name)
1174 name)))
1175 (target (funcall --quote-file-name target))
1176 (archive (expand-file-name archive))
1177 (archive-zip-extract
1178 (list "unzip" "-qq" "-o" "-d" target))
1179 exit-code command-output)
1180 (setq command-output
1181 (with-temp-buffer
1182 (setq exit-code (archive-zip-extract archive member))
1183 (buffer-string)))
1184 (unless (zerop exit-code)
1185 (message command-output)
1186 (error "Extraction failed"))))
1187 members))
1190 ;;;; Library wrappers :: Ox
1192 (defun org-odt--read-attribute (element property)
1193 (let* ((attrs (org-export-read-attribute :attr_odt element))
1194 (value (plist-get attrs property)))
1195 (and value (ignore-errors (read value)))))
1198 ;;;; Target
1200 (defun org-odt--target (text label)
1201 (cond
1202 ;; Empty label.
1203 ((not (and label (org-string-nw-p label))) text)
1204 ;; Bookmark pointing to a range of text.
1205 ((and text (not (string= text "")))
1206 (concat (format "\n<text:bookmark-start text:name=\"%s\"/>" label) text
1207 (format "\n<text:bookmark-end text:name=\"%s\"/>" label)))
1208 ;; Bookmark at a location.
1209 (t (format "\n<text:bookmark text:name=\"%s\"/>" label))))
1211 (defun org-odt--xref-target (category text label)
1212 (let* ((xref-format (plist-get
1213 (assoc-default category
1214 org-odt-caption-and-xref-settings)
1215 :xref-format)))
1216 (when xref-format
1217 (mapconcat
1218 (lambda (%)
1219 (cond
1220 ((stringp %) %)
1221 ((eq t %)
1222 (format "<text:bookmark-ref text:ref-name=\"%s\">%s</text:bookmark-ref>"
1223 label text))
1224 ((symbolp %)
1225 (format "<text:bookmark-ref text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:bookmark-ref>"
1226 % label text))))
1227 xref-format ""))))
1229 ;;;; Textbox
1231 (defun org-odt--textbox (text width height style &optional
1232 extra anchor-type)
1233 (org-odt--frame
1234 (format "\n<draw:text-box %s>%s\n</draw:text-box>"
1235 (concat (format " fo:min-height=\"%0.2fcm\"" (or height .2))
1236 (and (not width)
1237 (format " fo:min-width=\"%0.2fcm\"" (or width .2))))
1238 text)
1239 width nil style extra anchor-type))
1243 ;;;; Table of Contents
1245 (defun org-odt-begin-toc (index-title depth)
1246 (concat
1247 (format "
1248 <text:table-of-content text:style-name=\"OrgIndexSection\" text:protected=\"true\" text:name=\"Table of Contents\">
1249 <text:table-of-content-source text:outline-level=\"%d\">
1250 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
1251 " depth index-title)
1253 (let ((levels (number-sequence 1 10)))
1254 (mapconcat
1255 (lambda (level)
1256 (format
1258 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
1259 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
1260 <text:index-entry-chapter/>
1261 <text:index-entry-text/>
1262 <text:index-entry-link-end/>
1263 </text:table-of-content-entry-template>
1264 " level level)) levels ""))
1266 (format "
1267 </text:table-of-content-source>
1269 <text:index-body>
1270 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
1271 <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
1272 </text:index-title>
1273 " index-title)))
1275 (defun org-odt-end-toc ()
1276 (format "
1277 </text:index-body>
1278 </text:table-of-content>
1281 (defun* org-odt-format-toc-headline
1282 (todo todo-type priority text tags
1283 &key level section-number headline-label)
1284 (setq text
1285 (concat
1286 ;; Section number.
1287 (when section-number (concat section-number ". "))
1288 ;; Todo.
1289 (when todo
1290 (let ((style (if (member todo org-done-keywords)
1291 "OrgDone" "OrgTodo")))
1292 (format "<text:span text:style-name=\"%s\">%s</text:span> "
1293 style todo)))
1294 (when priority
1295 (let* ((style (format "OrgPriority-%s" priority))
1296 (priority (format "[#%c]" priority)))
1297 (format "<text:span text:style-name=\"%s\">%s</text:span> "
1298 style priority)))
1299 ;; Title.
1300 text
1301 ;; Tags.
1302 (when tags
1303 (concat
1304 (format " <text:span text:style-name=\"%s\">[%s]</text:span>"
1305 "OrgTags"
1306 (mapconcat
1307 (lambda (tag)
1308 (format
1309 "<text:span text:style-name=\"%s\">%s</text:span>"
1310 "OrgTag" tag)) tags " : "))))))
1311 (format "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
1312 headline-label text))
1314 (defun org-odt-toc (depth info)
1315 (assert (wholenump depth))
1316 ;; When a headline is marked as a radio target, as in the example below:
1318 ;; ** <<<Some Heading>>>
1319 ;; Some text.
1321 ;; suppress generation of radio targets. i.e., Radio targets are to
1322 ;; be marked as targets within /document body/ and *not* within
1323 ;; /TOC/, as otherwise there will be duplicated anchors one in TOC
1324 ;; and one in the document body.
1326 ;; FIXME-1: Currently exported headings are memoized. `org-export.el'
1327 ;; doesn't provide a way to disable memoization. So this doesn't
1328 ;; work.
1330 ;; FIXME-2: Are there any other objects that need to be suppressed
1331 ;; within TOC?
1332 (let* ((title (org-export-translate "Table of Contents" :utf-8 info))
1333 (headlines (org-export-collect-headlines
1334 info (and (wholenump depth) depth)))
1335 (backend (org-export-create-backend
1336 :parent (org-export-backend-name
1337 (plist-get info :back-end))
1338 :transcoders (mapcar
1339 (lambda (type) (cons type (lambda (d c i) c)))
1340 (list 'radio-target)))))
1341 (when headlines
1342 (concat
1343 (org-odt-begin-toc title depth)
1344 (mapconcat
1345 (lambda (headline)
1346 (let* ((entry (org-odt-format-headline--wrap
1347 headline backend info 'org-odt-format-toc-headline))
1348 (level (org-export-get-relative-level headline info))
1349 (style (format "Contents_20_%d" level)))
1350 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1351 style entry)))
1352 headlines "\n")
1353 (org-odt-end-toc)))))
1356 ;;;; Document styles
1358 (defun org-odt-add-automatic-style (object-type &optional object-props)
1359 "Create an automatic style of type OBJECT-TYPE with param OBJECT-PROPS.
1360 OBJECT-PROPS is (typically) a plist created by passing
1361 \"#+ATTR_ODT: \" option of the object in question to
1362 `org-odt-parse-block-attributes'.
1364 Use `org-odt-object-counters' to generate an automatic
1365 OBJECT-NAME and STYLE-NAME. If OBJECT-PROPS is non-nil, add a
1366 new entry in `org-odt-automatic-styles'. Return (OBJECT-NAME
1367 . STYLE-NAME)."
1368 (assert (stringp object-type))
1369 (let* ((object (intern object-type))
1370 (seqvar object)
1371 (seqno (1+ (or (plist-get org-odt-object-counters seqvar) 0)))
1372 (object-name (format "%s%d" object-type seqno)) style-name)
1373 (setq org-odt-object-counters
1374 (plist-put org-odt-object-counters seqvar seqno))
1375 (when object-props
1376 (setq style-name (format "Org%s" object-name))
1377 (setq org-odt-automatic-styles
1378 (plist-put org-odt-automatic-styles object
1379 (append (list (list style-name object-props))
1380 (plist-get org-odt-automatic-styles object)))))
1381 (cons object-name style-name)))
1383 ;;;; Checkbox
1385 (defun org-odt--checkbox (item)
1386 "Return check-box string associated to ITEM."
1387 (let ((checkbox (org-element-property :checkbox item)))
1388 (if (not checkbox) ""
1389 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1390 "OrgCode" (case checkbox
1391 (on "[&#x2713;] ") ; CHECK MARK
1392 (off "[ ] ")
1393 (trans "[-] "))))))
1395 ;;; Template
1397 (defun org-odt--build-date-styles (fmt style)
1398 ;; In LibreOffice 3.4.6, there doesn't seem to be a convenient way
1399 ;; to modify the date fields. A date could be modified by
1400 ;; offsetting in days. That's about it. Also, date and time may
1401 ;; have to be emitted as two fields - a date field and a time field
1402 ;; - separately.
1404 ;; One can add Form Controls to date and time fields so that they
1405 ;; can be easily modified. But then, the exported document will
1406 ;; become tightly coupled with LibreOffice and may not function
1407 ;; properly with other OpenDocument applications.
1409 ;; I have a strange feeling that Date styles are a bit flaky at the
1410 ;; moment.
1412 ;; The feature is experimental.
1413 (when (and fmt style)
1414 (let* ((fmt-alist
1415 '(("%A" . "<number:day-of-week number:style=\"long\"/>")
1416 ("%B" . "<number:month number:textual=\"true\" number:style=\"long\"/>")
1417 ("%H" . "<number:hours number:style=\"long\"/>")
1418 ("%M" . "<number:minutes number:style=\"long\"/>")
1419 ("%S" . "<number:seconds number:style=\"long\"/>")
1420 ("%V" . "<number:week-of-year/>")
1421 ("%Y" . "<number:year number:style=\"long\"/>")
1422 ("%a" . "<number:day-of-week number:style=\"short\"/>")
1423 ("%b" . "<number:month number:textual=\"true\" number:style=\"short\"/>")
1424 ("%d" . "<number:day number:style=\"long\"/>")
1425 ("%e" . "<number:day number:style=\"short\"/>")
1426 ("%h" . "<number:month number:textual=\"true\" number:style=\"short\"/>")
1427 ("%k" . "<number:hours number:style=\"short\"/>")
1428 ("%m" . "<number:month number:style=\"long\"/>")
1429 ("%p" . "<number:am-pm/>")
1430 ("%y" . "<number:year number:style=\"short\"/>")))
1431 (case-fold-search nil)
1432 (re (mapconcat 'identity (mapcar 'car fmt-alist) "\\|"))
1433 match rpl (start 0) (filler-beg 0) filler-end filler output)
1434 (mapc
1435 (lambda (pair)
1436 (setq fmt (replace-regexp-in-string (car pair) (cdr pair) fmt t t)))
1437 '(("\\(?:%[[:digit:]]*N\\)" . "") ; strip ns, us and ns
1438 ("%C" . "Y") ; replace century with year
1439 ("%D" . "%m/%d/%y")
1440 ("%G" . "Y") ; year corresponding to iso week
1441 ("%I" . "%H") ; hour on a 12-hour clock
1442 ("%R" . "%H:%M")
1443 ("%T" . "%H:%M:%S")
1444 ("%U\\|%W" . "%V") ; week no. starting on Sun./Mon.
1445 ("%Z" . "") ; time zone name
1446 ("%c" . "%Y-%M-%d %a %H:%M" ) ; locale's date and time format
1447 ("%g" . "%y")
1448 ("%X" . "%x" ) ; locale's pref. time format
1449 ("%j" . "") ; day of the year
1450 ("%l" . "%k") ; like %I blank-padded
1451 ("%s" . "") ; no. of secs since 1970-01-01 00:00:00 +0000
1452 ("%n" . "<text:line-break/>")
1453 ("%r" . "%I:%M:%S %p")
1454 ("%t" . "<text:tab/>")
1455 ("%u\\|%w" . "") ; numeric day of week - Mon (1-7), Sun(0-6)
1456 ("%x" . "%Y-%M-%d %a") ; locale's pref. time format
1457 ("%z" . "") ; time zone in numeric form
1459 (while (string-match re fmt start)
1460 (setq match (match-string 0 fmt))
1461 (setq rpl (assoc-default match fmt-alist))
1462 (setq start (match-end 0))
1463 (setq filler-end (match-beginning 0))
1464 (setq filler (substring fmt (prog1 filler-beg
1465 (setq filler-beg (match-end 0)))
1466 filler-end))
1467 (setq filler (and (not (string= filler ""))
1468 (format "<number:text>%s</number:text>"
1469 (org-odt--encode-plain-text filler))))
1470 (setq output (concat output "\n" filler "\n" rpl)))
1471 (setq filler (substring fmt filler-beg))
1472 (unless (string= filler "")
1473 (setq output (concat output
1474 (format "\n<number:text>%s</number:text>"
1475 (org-odt--encode-plain-text filler)))))
1476 (format "\n<number:date-style style:name=\"%s\" %s>%s\n</number:date-style>"
1477 style
1478 (concat " number:automatic-order=\"true\""
1479 " number:format-source=\"fixed\"")
1480 output ))))
1482 (defun org-odt-template (contents info)
1483 "Return complete document string after ODT conversion.
1484 CONTENTS is the transcoded contents string. RAW-DATA is the
1485 original parsed data. INFO is a plist holding export options."
1486 ;; Write meta file.
1487 (let ((title (org-export-data (plist-get info :title) info))
1488 (author (let ((author (plist-get info :author)))
1489 (if (not author) "" (org-export-data author info))))
1490 (email (plist-get info :email))
1491 (keywords (plist-get info :keywords))
1492 (description (plist-get info :description)))
1493 (write-region
1494 (concat
1495 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
1496 <office:document-meta
1497 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
1498 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
1499 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
1500 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
1501 xmlns:ooo=\"http://openoffice.org/2004/office\"
1502 office:version=\"1.2\">
1503 <office:meta>\n"
1504 (format "<dc:creator>%s</dc:creator>\n" author)
1505 (format "<meta:initial-creator>%s</meta:initial-creator>\n" author)
1506 ;; Date, if required.
1507 (when (plist-get info :with-date)
1508 ;; Check if DATE is specified as an Org-timestamp. If yes,
1509 ;; include it as meta information. Otherwise, just use
1510 ;; today's date.
1511 (let* ((date (let ((date (plist-get info :date)))
1512 (and (not (cdr date))
1513 (eq (org-element-type (car date)) 'timestamp)
1514 (car date)))))
1515 (let ((iso-date (org-odt--format-timestamp date nil 'iso-date)))
1516 (concat
1517 (format "<dc:date>%s</dc:date>\n" iso-date)
1518 (format "<meta:creation-date>%s</meta:creation-date>\n"
1519 iso-date)))))
1520 (format "<meta:generator>%s</meta:generator>\n"
1521 (let ((creator-info (plist-get info :with-creator)))
1522 (if (or (not creator-info) (eq creator-info 'comment)) ""
1523 (plist-get info :creator))))
1524 (format "<meta:keyword>%s</meta:keyword>\n" keywords)
1525 (format "<dc:subject>%s</dc:subject>\n" description)
1526 (when (org-string-nw-p title)
1527 (format "<dc:title>%s</dc:title>\n" title))
1528 "\n"
1529 " </office:meta>\n" "</office:document-meta>")
1530 nil (concat org-odt-zip-dir "meta.xml"))
1531 ;; Add meta.xml in to manifest.
1532 (org-odt-create-manifest-file-entry "text/xml" "meta.xml"))
1534 ;; Update styles file.
1535 ;; Copy styles.xml. Also dump htmlfontify styles, if there is any.
1536 ;; Write styles file.
1537 (let* ((styles-file (plist-get info :odt-styles-file))
1538 (styles-file (and styles-file (read (org-trim styles-file))))
1539 ;; Non-availability of styles.xml is not a critical
1540 ;; error. For now, throw an error.
1541 (styles-file (or styles-file
1542 (plist-get info :odt-styles-file)
1543 (expand-file-name "OrgOdtStyles.xml"
1544 org-odt-styles-dir)
1545 (error "org-odt: Missing styles file?"))))
1546 (cond
1547 ((listp styles-file)
1548 (let ((archive (nth 0 styles-file))
1549 (members (nth 1 styles-file)))
1550 (org-odt--zip-extract archive members org-odt-zip-dir)
1551 (mapc
1552 (lambda (member)
1553 (when (org-file-image-p member)
1554 (let* ((image-type (file-name-extension member))
1555 (media-type (format "image/%s" image-type)))
1556 (org-odt-create-manifest-file-entry media-type member))))
1557 members)))
1558 ((and (stringp styles-file) (file-exists-p styles-file))
1559 (let ((styles-file-type (file-name-extension styles-file)))
1560 (cond
1561 ((string= styles-file-type "xml")
1562 (copy-file styles-file (concat org-odt-zip-dir "styles.xml") t))
1563 ((member styles-file-type '("odt" "ott"))
1564 (org-odt--zip-extract styles-file "styles.xml" org-odt-zip-dir)))))
1566 (error (format "Invalid specification of styles.xml file: %S"
1567 (plist-get info :odt-styles-file)))))
1569 ;; create a manifest entry for styles.xml
1570 (org-odt-create-manifest-file-entry "text/xml" "styles.xml")
1572 ;; FIXME: Who is opening an empty styles.xml before this point?
1573 (with-current-buffer
1574 (find-file-noselect (concat org-odt-zip-dir "styles.xml") t)
1575 (revert-buffer t t)
1577 ;; Write custom styles for source blocks
1578 ;; Save STYLES used for colorizing of source blocks.
1579 ;; Update styles.xml with styles that were collected as part of
1580 ;; `org-odt-hfy-face-to-css' callbacks.
1581 (let ((styles (mapconcat (lambda (style) (format " %s\n" (cddr style)))
1582 hfy-user-sheet-assoc "")))
1583 (when styles
1584 (goto-char (point-min))
1585 (when (re-search-forward "</office:styles>" nil t)
1586 (goto-char (match-beginning 0))
1587 (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n"))))
1589 ;; Update styles.xml - take care of outline numbering
1591 ;; Don't make automatic backup of styles.xml file. This setting
1592 ;; prevents the backed-up styles.xml file from being zipped in to
1593 ;; odt file. This is more of a hackish fix. Better alternative
1594 ;; would be to fix the zip command so that the output odt file
1595 ;; includes only the needed files and excludes any auto-generated
1596 ;; extra files like backups and auto-saves etc etc. Note that
1597 ;; currently the zip command zips up the entire temp directory so
1598 ;; that any auto-generated files created under the hood ends up in
1599 ;; the resulting odt file.
1600 (set (make-local-variable 'backup-inhibited) t)
1602 ;; Outline numbering is retained only upto LEVEL.
1603 ;; To disable outline numbering pass a LEVEL of 0.
1605 (goto-char (point-min))
1606 (let ((regex
1607 "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
1608 (replacement
1609 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1610 (while (re-search-forward regex nil t)
1611 (unless (let ((sec-num (plist-get info :section-numbers))
1612 (level (string-to-number (match-string 2))))
1613 (if (wholenump sec-num) (<= level sec-num) sec-num))
1614 (replace-match replacement t nil))))
1615 (save-buffer 0)))
1616 ;; Update content.xml.
1618 (let* ( ;; `org-display-custom-times' should be accessed right
1619 ;; within the context of the Org buffer. So obtain its
1620 ;; value before moving on to temp-buffer context down below.
1621 (custom-time-fmts
1622 (if org-display-custom-times
1623 (cons (substring (car org-time-stamp-custom-formats) 1 -1)
1624 (substring (cdr org-time-stamp-custom-formats) 1 -1))
1625 '("%Y-%M-%d %a" . "%Y-%M-%d %a %H:%M"))))
1626 (with-temp-buffer
1627 (insert-file-contents
1628 (or (plist-get info :odt-content-template-file)
1629 (expand-file-name "OrgOdtContentTemplate.xml"
1630 org-odt-styles-dir)))
1631 ;; Write automatic styles.
1632 ;; - Position the cursor.
1633 (goto-char (point-min))
1634 (re-search-forward " </office:automatic-styles>" nil t)
1635 (goto-char (match-beginning 0))
1636 ;; - Dump automatic table styles.
1637 (loop for (style-name props) in
1638 (plist-get org-odt-automatic-styles 'Table) do
1639 (when (setq props (or (let ((value (plist-get props :rel-width)))
1640 (and value (ignore-errors (read value)))) 96))
1641 (insert (format org-odt-table-style-format style-name props))))
1642 ;; - Dump date-styles.
1643 (when (plist-get info :odt-use-date-fields)
1644 (insert (org-odt--build-date-styles (car custom-time-fmts)
1645 "OrgDate1")
1646 (org-odt--build-date-styles (cdr custom-time-fmts)
1647 "OrgDate2")))
1648 ;; Update display level.
1649 ;; - Remove existing sequence decls. Also position the cursor.
1650 (goto-char (point-min))
1651 (when (re-search-forward "<text:sequence-decls" nil t)
1652 (delete-region (match-beginning 0)
1653 (re-search-forward "</text:sequence-decls>" nil nil)))
1654 ;; Update sequence decls according to user preference.
1655 (insert
1656 (format
1657 "\n<text:sequence-decls>\n%s\n</text:sequence-decls>"
1658 (mapconcat
1659 (lambda (x)
1660 (format
1661 "<text:sequence-decl text:display-outline-level=\"%d\" text:name=\"%s\"/>"
1662 (plist-get info :odt-display-outline-level) (nth 1 x)))
1663 org-odt-category-map-alist "\n")))
1664 ;; Position the cursor to document body.
1665 (goto-char (point-min))
1666 (re-search-forward "</office:text>" nil nil)
1667 (goto-char (match-beginning 0))
1669 ;; Preamble - Title, Author, Date etc.
1670 (insert
1671 (let* ((title (and (plist-get info :with-title)
1672 (org-export-data (plist-get info :title) info)))
1673 (author (and (plist-get info :with-author)
1674 (let ((auth (plist-get info :author)))
1675 (and auth (org-export-data auth info)))))
1676 (email (plist-get info :email))
1677 ;; Switch on or off above vars based on user settings
1678 (author (and (plist-get info :with-author) (or author email)))
1679 (email (and (plist-get info :with-email) email)))
1680 (concat
1681 ;; Title.
1682 (when (org-string-nw-p title)
1683 (concat
1684 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1685 "OrgTitle" (format "\n<text:title>%s</text:title>" title))
1686 ;; Separator.
1687 "\n<text:p text:style-name=\"OrgTitle\"/>"))
1688 (cond
1689 ((and author (not email))
1690 ;; Author only.
1691 (concat
1692 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1693 "OrgSubtitle"
1694 (format "<text:initial-creator>%s</text:initial-creator>" author))
1695 ;; Separator.
1696 "\n<text:p text:style-name=\"OrgSubtitle\"/>"))
1697 ((and author email)
1698 ;; Author and E-mail.
1699 (concat
1700 (format
1701 "\n<text:p text:style-name=\"%s\">%s</text:p>"
1702 "OrgSubtitle"
1703 (format
1704 "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
1705 (concat "mailto:" email)
1706 (format "<text:initial-creator>%s</text:initial-creator>" author)))
1707 ;; Separator.
1708 "\n<text:p text:style-name=\"OrgSubtitle\"/>")))
1709 ;; Date, if required.
1710 (when (plist-get info :with-date)
1711 (let* ((date (plist-get info :date))
1712 ;; Check if DATE is specified as a timestamp.
1713 (timestamp (and (not (cdr date))
1714 (eq (org-element-type (car date)) 'timestamp)
1715 (car date)))
1716 ;; Use DATE as subtitle.
1717 (subtitle
1718 (if (and (plist-get info :odt-use-date-fields) timestamp)
1719 (org-odt--format-timestamp (car date))
1720 (org-export-data (plist-get info :date) info))))
1721 (concat
1722 (when (org-string-nw-p subtitle)
1723 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1724 "OrgSubtitle" subtitle))
1725 ;; Separator
1726 "<text:p text:style-name=\"OrgSubtitle\"/>"))))))
1727 ;; Table of Contents
1728 (let* ((with-toc (plist-get info :with-toc))
1729 (depth (and with-toc (if (wholenump with-toc)
1730 with-toc
1731 (plist-get info :headline-levels)))))
1732 (when depth (insert (or (org-odt-toc depth info) ""))))
1733 ;; Contents.
1734 (insert contents)
1735 ;; Return contents.
1736 (buffer-substring-no-properties (point-min) (point-max)))))
1740 ;;; Transcode Functions
1742 ;;;; Bold
1744 (defun org-odt-bold (bold contents info)
1745 "Transcode BOLD from Org to ODT.
1746 CONTENTS is the text with bold markup. INFO is a plist holding
1747 contextual information."
1748 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1749 ;; Internally, `org-odt--translate-description-lists/html'
1750 ;; or `org-odt--translate-description-lists/latex' requests
1751 ;; a custom style for bold.
1752 (or (org-element-property :style bold) "Bold")
1753 contents))
1756 ;;;; Center Block
1758 (defun org-odt-center-block (center-block contents info)
1759 "Transcode a CENTER-BLOCK element from Org to ODT.
1760 CONTENTS holds the contents of the center block. INFO is a plist
1761 holding contextual information."
1762 contents)
1765 ;;;; Clock
1767 (defun org-odt-clock (clock contents info)
1768 "Transcode a CLOCK element from Org to ODT.
1769 CONTENTS is nil. INFO is a plist used as a communication
1770 channel."
1771 (let ((timestamp (org-element-property :value clock))
1772 (duration (org-element-property :duration clock)))
1773 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1774 (if (eq (org-element-type (org-export-get-next-element clock info))
1775 'clock) "OrgClock" "OrgClockLastLine")
1776 (concat
1777 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1778 "OrgClockKeyword" org-clock-string)
1779 (org-odt-timestamp timestamp contents info)
1780 (and duration (format " (%s)" duration))))))
1783 ;;;; Code
1785 (defun org-odt-code (code contents info)
1786 "Transcode a CODE object from Org to ODT.
1787 CONTENTS is nil. INFO is a plist used as a communication
1788 channel."
1789 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1790 "OrgCode" (org-odt--encode-plain-text
1791 (org-element-property :value code))))
1794 ;;;; Comment
1796 ;; Comments are ignored.
1799 ;;;; Comment Block
1801 ;; Comment Blocks are ignored.
1804 ;;;; Drawer
1806 (defun org-odt-drawer (drawer contents info)
1807 "Transcode a DRAWER element from Org to ODT.
1808 CONTENTS holds the contents of the block. INFO is a plist
1809 holding contextual information."
1810 (let* ((name (org-element-property :drawer-name drawer))
1811 (output (funcall (plist-get info :odt-format-drawer-function)
1812 name contents)))
1813 output))
1816 ;;;; Dynamic Block
1818 (defun org-odt-dynamic-block (dynamic-block contents info)
1819 "Transcode a DYNAMIC-BLOCK element from Org to ODT.
1820 CONTENTS holds the contents of the block. INFO is a plist
1821 holding contextual information. See `org-export-data'."
1822 contents)
1825 ;;;; Entity
1827 (defun org-odt-entity (entity contents info)
1828 "Transcode an ENTITY object from Org to ODT.
1829 CONTENTS are the definition itself. INFO is a plist holding
1830 contextual information."
1831 (org-element-property :utf-8 entity))
1834 ;;;; Example Block
1836 (defun org-odt-example-block (example-block contents info)
1837 "Transcode a EXAMPLE-BLOCK element from Org to ODT.
1838 CONTENTS is nil. INFO is a plist holding contextual information."
1839 (org-odt-format-code example-block info))
1842 ;;;; Export Snippet
1844 (defun org-odt-export-snippet (export-snippet contents info)
1845 "Transcode a EXPORT-SNIPPET object from Org to ODT.
1846 CONTENTS is nil. INFO is a plist holding contextual information."
1847 (when (eq (org-export-snippet-backend export-snippet) 'odt)
1848 (org-element-property :value export-snippet)))
1851 ;;;; Export Block
1853 (defun org-odt-export-block (export-block contents info)
1854 "Transcode a EXPORT-BLOCK element from Org to ODT.
1855 CONTENTS is nil. INFO is a plist holding contextual information."
1856 (when (string= (org-element-property :type export-block) "ODT")
1857 (org-remove-indentation (org-element-property :value export-block))))
1860 ;;;; Fixed Width
1862 (defun org-odt-fixed-width (fixed-width contents info)
1863 "Transcode a FIXED-WIDTH element from Org to ODT.
1864 CONTENTS is nil. INFO is a plist holding contextual information."
1865 (org-odt-do-format-code (org-element-property :value fixed-width) info))
1868 ;;;; Footnote Definition
1870 ;; Footnote Definitions are ignored.
1873 ;;;; Footnote Reference
1875 (defun org-odt--format-footnote-definition (n def)
1876 (let ((note-class "footnote")
1877 (par-style "Footnote")
1878 (id (if n (format "text:id=\"fn%d\"" n) "")))
1879 (format
1880 "<text:note %s text:note-class=\"%s\">%s</text:note>"
1881 id note-class
1882 (concat
1883 (format "<text:note-citation>%d</text:note-citation>" (or n 0))
1884 (format "<text:note-body>%s</text:note-body>" def)))))
1886 (defun org-odt-footnote-reference (footnote-reference contents info)
1887 "Transcode a FOOTNOTE-REFERENCE element from Org to ODT.
1888 CONTENTS is nil. INFO is a plist holding contextual information."
1889 (let ((--format-footnote-reference
1890 (function
1891 (lambda (n)
1892 (setq n (format "%d" n))
1893 (let ((note-class "footnote")
1894 (ref-format "text")
1895 (ref-name (concat "fn" n)))
1896 (format
1897 "<text:span text:style-name=\"%s\">%s</text:span>"
1898 "OrgSuperscript"
1899 (format "<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:note-ref>"
1900 note-class ref-format ref-name n)))))))
1901 (concat
1902 ;; Insert separator between two footnotes in a row.
1903 (let ((prev (org-export-get-previous-element footnote-reference info)))
1904 (and (eq (org-element-type prev) 'footnote-reference)
1905 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1906 "OrgSuperscript" ",")))
1907 ;; Transcode footnote reference.
1908 (let ((n (org-export-get-footnote-number footnote-reference info nil t)))
1909 (cond
1910 ((not
1911 (org-export-footnote-first-reference-p footnote-reference info nil t))
1912 (funcall --format-footnote-reference n))
1913 ;; Inline definitions are secondary strings.
1914 ;; Non-inline footnotes definitions are full Org data.
1916 (let* ((raw (org-export-get-footnote-definition
1917 footnote-reference info))
1918 (def
1919 (let ((def (org-trim (org-export-data raw info))))
1920 (if (eq (org-element-type raw) 'org-data) def
1921 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1922 "Footnote" def)))))
1923 (org-odt--format-footnote-definition n def))))))))
1926 ;;;; Citation Reference
1928 (defun org-odt-citation (citation contents info)
1929 "Transcode a CITATION element from Org to ODT.
1930 CONTENTS is nil. INFO is a plist holding contextual information."
1931 ;; Just interpret the citation object.
1932 ;; Citation processors (like ox-jabref.el) may handle citation
1933 ;; by registering their own transcoders.
1934 (org-element-interpret-data citation))
1936 ;;;; Headline
1938 (defun org-odt-format-headline-default-function
1939 (todo todo-type priority text tags)
1940 "Default format function for a headline.
1941 See `org-odt-format-headline-function' for details."
1942 (concat
1943 ;; Todo.
1944 (when todo
1945 (let ((style (if (member todo org-done-keywords) "OrgDone" "OrgTodo")))
1946 (format "<text:span text:style-name=\"%s\">%s</text:span> "
1947 style todo)))
1948 (when priority
1949 (let* ((style (format "OrgPriority-%s" priority))
1950 (priority (format "[#%c]" priority)))
1951 (format "<text:span text:style-name=\"%s\">%s</text:span> "
1952 style priority)))
1953 ;; Title.
1954 text
1955 ;; Tags.
1956 (when tags
1957 (concat
1958 "<text:tab/>"
1959 (format "<text:span text:style-name=\"%s\">[%s]</text:span>"
1960 "OrgTags" (mapconcat
1961 (lambda (tag)
1962 (format
1963 "<text:span text:style-name=\"%s\">%s</text:span>"
1964 "OrgTag" tag)) tags " : "))))))
1966 (defun org-odt-format-headline--wrap (headline backend info
1967 &optional format-function)
1968 "Transcode a HEADLINE element using BACKEND.
1969 INFO is a plist holding contextual information."
1970 (setq backend (or backend (plist-get info :back-end)))
1971 (let* ((level (+ (org-export-get-relative-level headline info)))
1972 (headline-number (org-export-get-headline-number headline info))
1973 (section-number (and (org-export-numbered-headline-p headline info)
1974 (mapconcat 'number-to-string
1975 headline-number ".")))
1976 (todo (and (plist-get info :with-todo-keywords)
1977 (let ((todo (org-element-property :todo-keyword headline)))
1978 (and todo
1979 (org-export-data-with-backend todo backend info)))))
1980 (todo-type (and todo (org-element-property :todo-type headline)))
1981 (priority (and (plist-get info :with-priority)
1982 (org-element-property :priority headline)))
1983 (text (org-export-data-with-backend
1984 (org-element-property :title headline) backend info))
1985 (tags (and (plist-get info :with-tags)
1986 (org-export-get-tags headline info)))
1987 (headline-label (concat "sec-" (mapconcat 'number-to-string
1988 headline-number "-")))
1989 (format-function
1990 (if (functionp format-function) format-function
1991 (function*
1992 (lambda (todo todo-type priority text tags
1993 &key level section-number headline-label)
1994 (funcall (plist-get info :odt-format-headline-function)
1995 todo todo-type priority text tags))))))
1996 (funcall format-function
1997 todo todo-type priority text tags
1998 :level level :section-number section-number
1999 :headline-label headline-label)))
2001 (defun org-odt-headline (headline contents info)
2002 "Transcode a HEADLINE element from Org to ODT.
2003 CONTENTS holds the contents of the headline. INFO is a plist
2004 holding contextual information."
2005 ;; Case 1: This is a footnote section: ignore it.
2006 (unless (org-element-property :footnote-section-p headline)
2007 (let* ((text (org-export-data (org-element-property :title headline) info))
2008 ;; Create the headline text.
2009 (full-text (org-odt-format-headline--wrap headline nil info))
2010 ;; Get level relative to current parsed data.
2011 (level (org-export-get-relative-level headline info))
2012 ;; Get canonical label for the headline.
2013 (id (concat "sec-" (mapconcat 'number-to-string
2014 (org-export-get-headline-number
2015 headline info) "-")))
2016 ;; Get user-specified labels for the headline.
2017 (extra-ids (list (org-element-property :CUSTOM_ID headline)
2018 (org-element-property :ID headline)))
2019 ;; Extra targets.
2020 (extra-targets
2021 (mapconcat (lambda (x)
2022 (when x
2023 (let ((x (if (org-uuidgen-p x) (concat "ID-" x) x)))
2024 (org-odt--target
2025 "" (org-export-solidify-link-text x)))))
2026 extra-ids ""))
2027 ;; Title.
2028 (anchored-title (org-odt--target full-text id)))
2029 (cond
2030 ;; Case 2. This is a deep sub-tree: export it as a list item.
2031 ;; Also export as items headlines for which no section
2032 ;; format has been found.
2033 ((org-export-low-level-p headline info)
2034 ;; Build the real contents of the sub-tree.
2035 (concat
2036 (and (org-export-first-sibling-p headline info)
2037 (format "\n<text:list text:style-name=\"%s\" %s>"
2038 ;; Choose style based on list type.
2039 (if (org-export-numbered-headline-p headline info)
2040 "OrgNumberedList" "OrgBulletedList")
2041 ;; If top-level list, re-start numbering. Otherwise,
2042 ;; continue numbering.
2043 (format "text:continue-numbering=\"%s\""
2044 (let* ((parent (org-export-get-parent-headline
2045 headline)))
2046 (if (and parent
2047 (org-export-low-level-p parent info))
2048 "true" "false")))))
2049 (let ((headline-has-table-p
2050 (let ((section (assq 'section (org-element-contents headline))))
2051 (assq 'table (and section (org-element-contents section))))))
2052 (format "\n<text:list-item>\n%s\n%s"
2053 (concat
2054 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2055 "Text_20_body"
2056 (concat extra-targets anchored-title))
2057 contents)
2058 (if headline-has-table-p
2059 "</text:list-header>"
2060 "</text:list-item>")))
2061 (and (org-export-last-sibling-p headline info)
2062 "</text:list>")))
2063 ;; Case 3. Standard headline. Export it as a section.
2065 (concat
2066 (format
2067 "\n<text:h text:style-name=\"%s\" text:outline-level=\"%s\">%s</text:h>"
2068 (format "Heading_20_%s" level)
2069 level
2070 (concat extra-targets anchored-title))
2071 contents))))))
2074 ;;;; Horizontal Rule
2076 (defun org-odt-horizontal-rule (horizontal-rule contents info)
2077 "Transcode an HORIZONTAL-RULE object from Org to ODT.
2078 CONTENTS is nil. INFO is a plist holding contextual information."
2079 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2080 "Horizontal_20_Line" ""))
2083 ;;;; Inline Babel Call
2085 ;; Inline Babel Calls are ignored.
2088 ;;;; Inline Src Block
2090 (defun org-odt--find-verb-separator (s)
2091 "Return a character not used in string S.
2092 This is used to choose a separator for constructs like \\verb."
2093 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
2094 (loop for c across ll
2095 when (not (string-match (regexp-quote (char-to-string c)) s))
2096 return (char-to-string c))))
2098 (defun org-odt-inline-src-block (inline-src-block contents info)
2099 "Transcode an INLINE-SRC-BLOCK element from Org to ODT.
2100 CONTENTS holds the contents of the item. INFO is a plist holding
2101 contextual information."
2102 (let* ((org-lang (org-element-property :language inline-src-block))
2103 (code (org-element-property :value inline-src-block))
2104 (separator (org-odt--find-verb-separator code)))
2105 (error "FIXME")))
2108 ;;;; Inlinetask
2110 (defun org-odt-inlinetask (inlinetask contents info)
2111 "Transcode an INLINETASK element from Org to ODT.
2112 CONTENTS holds the contents of the block. INFO is a plist
2113 holding contextual information."
2114 (let* ((todo (and (plist-get info :with-todo-keywords)
2115 (let ((todo (org-element-property :todo-keyword inlinetask)))
2116 (and todo (org-export-data todo info)))))
2117 (todo-type (and todo (org-element-property :todo-type inlinetask)))
2118 (priority (and (plist-get info :with-priority)
2119 (org-element-property :priority inlinetask)))
2120 (name (org-export-data (org-element-property :title inlinetask) info))
2121 (tags (and (plist-get info :with-tags)
2122 (org-export-get-tags inlinetask info))))
2123 (funcall (plist-get info :odt-format-inlinetask-function)
2124 todo todo-type priority name tags contents)))
2126 (defun org-odt-format-inlinetask-default-function
2127 (todo todo-type priority name tags contents)
2128 "Transcode an INLINETASK element from Org to ODT.
2129 CONTENTS holds the contents of the block. INFO is a plist
2130 holding contextual information."
2131 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2132 "Text_20_body"
2133 (org-odt--textbox
2134 (concat
2135 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2136 "OrgInlineTaskHeading"
2137 (org-odt-format-headline-default-function
2138 todo todo-type priority name tags))
2139 contents)
2140 nil nil "OrgInlineTaskFrame" " style:rel-width=\"100%\"")))
2142 ;;;; Italic
2144 (defun org-odt-italic (italic contents info)
2145 "Transcode ITALIC from Org to ODT.
2146 CONTENTS is the text with italic markup. INFO is a plist holding
2147 contextual information."
2148 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2149 "Emphasis" contents))
2152 ;;;; Item
2154 (defun org-odt-item (item contents info)
2155 "Transcode an ITEM element from Org to ODT.
2156 CONTENTS holds the contents of the item. INFO is a plist holding
2157 contextual information."
2158 (let* ((plain-list (org-export-get-parent item))
2159 (type (org-element-property :type plain-list))
2160 (counter (org-element-property :counter item))
2161 (tag (let ((tag (org-element-property :tag item)))
2162 (and tag
2163 (concat (org-odt--checkbox item)
2164 (org-export-data tag info))))))
2165 (case type
2166 ((ordered unordered descriptive-1 descriptive-2)
2167 (format "\n<text:list-item>\n%s\n%s"
2168 contents
2169 (let* ((--element-has-a-table-p
2170 (function
2171 (lambda (element info)
2172 (loop for el in (org-element-contents element)
2173 thereis (eq (org-element-type el) 'table))))))
2174 (cond
2175 ((funcall --element-has-a-table-p item info)
2176 "</text:list-header>")
2177 (t "</text:list-item>")))))
2178 (t (error "Unknown list type: %S" type)))))
2180 ;;;; Keyword
2182 (defun org-odt-keyword (keyword contents info)
2183 "Transcode a KEYWORD element from Org to ODT.
2184 CONTENTS is nil. INFO is a plist holding contextual information."
2185 (let ((key (org-element-property :key keyword))
2186 (value (org-element-property :value keyword)))
2187 (cond
2188 ((string= key "ODT") value)
2189 ((string= key "INDEX")
2190 ;; FIXME
2191 (ignore))
2192 ((string= key "TOC")
2193 (let ((value (downcase value)))
2194 (cond
2195 ((string-match "\\<headlines\\>" value)
2196 (let ((depth (or (and (string-match "[0-9]+" value)
2197 (string-to-number (match-string 0 value)))
2198 (plist-get info :with-toc))))
2199 (when (wholenump depth) (org-odt-toc depth info))))
2200 ((member value '("tables" "figures" "listings"))
2201 ;; FIXME
2202 (ignore)))))
2203 ;; Handle BIBLIOGRAPHY. Ignore it.
2204 ((string= key "BIBLIOGRAPHY")
2205 ;; Citation Processors (see ox-jabref.el) may handle this by
2206 ;; registering their own transcoders.
2207 (ignore))
2208 ((string= key "PAGEBREAK")
2210 ;; Pagebreaks created this way are a mere expedience. These
2211 ;; create extraneous "empty" paragraphs which take up "extra
2212 ;; space". A typographer will chide you for resorting to such
2213 ;; underhanded means to create pagebreaks.
2215 ;; As an expedience it has it's uses. See
2216 ;; `org-odt-special-block' for a realistic example of how
2217 ;; pagebreak can be to service.
2219 ;; The right way to create pagebreaks is to create new styles -
2220 ;; custom or automatic - that set the "before/after" pagebreak
2221 ;; of an element (a paragraph, table etc).
2223 ;; For example, consider pagebreaks created as below.
2225 ;; Text in first page.
2227 ;; #+ATTR_ODT: :style "OrgPageBreakDefault"
2228 ;; #+PAGEBREAK:
2230 ;; This text goes in next page.
2232 ;; Now look at the page that is introduced with forced page
2233 ;; break. You will realize that the first line of text in that
2234 ;; page is a bit displaced from other pages created by
2235 ;; LibreOffice. A keen eye will definitely catch this
2236 ;; aberration.
2237 (let ((style (org-odt--read-attribute keyword :style)))
2238 (unless (and style (stringp style) (org-string-nw-p style))
2239 (setq style "OrgPageBreakDefault"))
2240 (format "\n<text:p text:style-name=\"%s\"/>" style))))))
2243 ;;;; Latex Environment
2246 ;; (eval-after-load 'ox-odt '(ad-deactivate 'org-format-latex-as-mathml))
2247 ;; (defadvice org-format-latex-as-mathml ; FIXME
2248 ;; (after org-odt-protect-latex-fragment activate)
2249 ;; "Encode LaTeX fragment as XML.
2250 ;; Do this when translation to MathML fails."
2251 ;; (unless (> (length ad-return-value) 0)
2252 ;; (setq ad-return-value (org-odt--encode-plain-text (ad-get-arg 0)))))
2254 (defun org-odt-latex-environment (latex-environment contents info)
2255 "Transcode a LATEX-ENVIRONMENT element from Org to ODT.
2256 CONTENTS is nil. INFO is a plist holding contextual information."
2257 (let* ((latex-frag (org-remove-indentation
2258 (org-element-property :value latex-environment))))
2259 (org-odt-do-format-code latex-frag info)))
2262 ;;;; Latex Fragment
2264 ;; (when latex-frag ; FIXME
2265 ;; (setq href (org-propertize href :title "LaTeX Fragment"
2266 ;; :description latex-frag)))
2267 ;; handle verbatim
2268 ;; provide descriptions
2270 (defun org-odt-latex-fragment (latex-fragment contents info)
2271 "Transcode a LATEX-FRAGMENT object from Org to ODT.
2272 CONTENTS is nil. INFO is a plist holding contextual information."
2273 (let* ((latex-frag (org-element-property :value latex-fragment)))
2274 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2275 "OrgCode" (org-odt--encode-plain-text latex-frag t))))
2278 ;;;; Line Break
2280 (defun org-odt-line-break (line-break contents info)
2281 "Transcode a LINE-BREAK object from Org to ODT.
2282 CONTENTS is nil. INFO is a plist holding contextual information."
2283 "<text:line-break/>")
2286 ;;;; Link
2288 ;;;; Links :: Label references
2290 (defun org-odt--enumerate (element info &optional predicate n)
2291 (when predicate (assert (funcall predicate element info)))
2292 (let* ((--numbered-parent-headline-at-<=-n
2293 (function
2294 (lambda (element n info)
2295 (loop for x in (org-element-lineage element)
2296 thereis (and (eq (org-element-type x) 'headline)
2297 (<= (org-export-get-relative-level x info) n)
2298 (org-export-numbered-headline-p x info)
2299 x)))))
2300 (--enumerate
2301 (function
2302 (lambda (element scope info &optional predicate)
2303 (let ((counter 0))
2304 (org-element-map (or scope (plist-get info :parse-tree))
2305 (org-element-type element)
2306 (lambda (el)
2307 (and (or (not predicate) (funcall predicate el info))
2308 (incf counter)
2309 (eq element el)
2310 counter))
2311 info 'first-match)))))
2312 (scope (funcall --numbered-parent-headline-at-<=-n
2313 element (or n (plist-get info :odt-display-outline-level)) info))
2314 (ordinal (funcall --enumerate element scope info predicate))
2315 (tag
2316 (concat
2317 ;; Section number.
2318 (and scope
2319 (mapconcat 'number-to-string
2320 (org-export-get-headline-number scope info) "."))
2321 ;; Separator.
2322 (and scope ".")
2323 ;; Ordinal.
2324 (number-to-string ordinal))))
2325 tag))
2327 (defun org-odt-format-label (element info op &optional format-prop)
2328 "Return a label for ELEMENT.
2330 ELEMENT is a `link', `table', `src-block' or `paragraph' type
2331 element. INFO is a plist used as a communication channel. OP is
2332 either `definition' or `reference', depending on the purpose of
2333 the generated string.
2335 Return value is a string if OP is set to `reference' or a cons
2336 cell like CAPTION . SHORT-CAPTION) where CAPTION and
2337 SHORT-CAPTION are strings."
2338 (assert (memq (org-element-type element) '(link table src-block paragraph)))
2339 (let* ((caption-from
2340 (case (org-element-type element)
2341 (link (org-export-get-parent-element element))
2342 (t element)))
2343 ;; Get label and caption.
2344 (label (org-element-property :name caption-from))
2345 (caption (org-export-get-caption caption-from))
2346 (short-caption (org-export-get-caption caption-from t))
2347 ;; Transcode captions.
2348 (caption (and caption (org-export-data caption info)))
2349 ;; Currently short caption are sneaked in as object names.
2351 ;; The advantages are:
2353 ;; - Table Of Contents: Currently, there is no support for
2354 ;; building TOC for figures, listings and tables. See
2355 ;; `org-odt-keyword'. User instead has to rely on
2356 ;; external application for building such indices. Within
2357 ;; LibreOffice, building an "Illustration Index" or "Index
2358 ;; of Tables" will create a table with long captions (only)
2359 ;; and building a table with "Object names" will create a
2360 ;; table with short captions.
2362 ;; - Easy navigation: In LibreOffice, object names are
2363 ;; offered via the navigation bar. This way one can
2364 ;; quickly locate and jump to object of his choice in the
2365 ;; exported document.
2367 ;; The main disadvantage is that there cannot be any markups
2368 ;; within object names i.e., one cannot embolden, italicize
2369 ;; or underline text within short caption. So suppress
2370 ;; generation of <text:span >...</text:span> and other
2371 ;; markups by overriding the default translators. We
2372 ;; probably shouldn't be suppressing translators for all
2373 ;; elements in `org-element-all-objects', but for now this
2374 ;; will do.
2375 (short-caption
2376 ;; Sneaking in short-caption as name attribute is
2377 ;; problematic with LibreOffice > 4.0.4.2. So ignore
2378 ;; short-captions. See following thread:
2379 ;; http://lists.gnu.org/archive/html/emacs-orgmode/2013-12/msg00100.html
2380 (ignore
2381 (let ((short-caption (or short-caption caption))
2382 (backend (org-export-create-backend
2383 :parent (org-export-backend-name
2384 (plist-get info :back-end))
2385 :transcoders
2386 (mapcar (lambda (type) (cons type (lambda (o c i) c)))
2387 org-element-all-objects))))
2388 (when short-caption
2389 (org-export-data-with-backend short-caption backend info))))))
2390 (when (or label caption)
2391 (let* ((default-category
2392 (case (org-element-type element)
2393 (table :TABLE:)
2394 (src-block :LISTING:)
2395 ((link paragraph)
2396 (cond
2397 ((org-odt--enumerable-latex-image-p element info)
2398 :DVIPNG-IMAGE:)
2399 ((org-odt--enumerable-image-p element info)
2400 :FIGURE:)
2401 ((org-odt--enumerable-formula-p element info)
2402 :MATH-FORMULA:)
2403 (t (error "Don't know how to format label for link: %S"
2404 element))))
2405 (t (error "Don't know how to format label for element type: %s"
2406 (org-element-type element)))))
2407 seqno)
2408 (assert default-category)
2409 (destructuring-bind (counter category predicate)
2410 (assoc-default default-category org-odt-category-map-alist)
2411 ;; Compute sequence number of the element.
2412 (setq seqno (org-odt--enumerate element info predicate))
2413 ;; Localize category string.
2414 (setq category (org-export-translate category :utf-8 info))
2415 (case op
2416 ;; Case 1: Handle Label definition.
2417 (definition
2418 ;; Assign an internal label, if user has not provided one
2419 (setq label (org-export-solidify-link-text
2420 (or label (format "%s-%s" default-category seqno))))
2421 (cons
2422 (concat
2423 ;; Sneak in a bookmark. The bookmark is used when the
2424 ;; labeled element is referenced with a link that
2425 ;; provides its own description.
2426 (org-odt--target "" label)
2427 ;; Label definition: Typically formatted as below:
2428 ;; CATEGORY SEQ-NO: LONG CAPTION
2429 ;; with translation for correct punctuation.
2430 (let* ((caption-format
2431 (plist-get
2432 (assoc-default default-category
2433 org-odt-caption-and-xref-settings)
2434 (or format-prop :caption-format))))
2435 (mapconcat (lambda (%)
2436 (case %
2437 (category
2438 (org-export-translate category :utf-8 info))
2439 (counter
2440 (format
2441 "<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">%s</text:sequence>"
2442 label counter counter seqno))
2443 (caption (or caption ""))
2444 (otherwise %)))
2445 caption-format "")))
2446 short-caption))
2447 ;; Case 2: Handle Label reference.
2448 (reference
2449 (assert label)
2450 (setq label (org-export-solidify-link-text label))
2451 (let* ((xref-format
2452 (plist-get
2453 (assoc-default default-category
2454 org-odt-caption-and-xref-settings)
2455 (or format-prop :xref-format)))
2456 (standard-value-p
2457 (let ((standard-value
2458 (eval (car (get 'org-odt-caption-and-xref-settings
2459 'standard-value)))))
2460 (equal (assoc-default default-category
2461 org-odt-caption-and-xref-settings)
2462 (assoc-default default-category standard-value))))
2463 (value (if standard-value-p seqno "[PLS. UPDATE FIELDS]")))
2464 (mapconcat (lambda (%)
2465 (cond
2466 ((stringp %) %)
2467 ((symbolp %)
2468 (format "<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:sequence-ref>"
2469 % label value))))
2470 xref-format "")))
2471 (t (error "Unknown %S on label" op))))))))
2474 ;;;; Links :: Inline Images
2476 (defun org-odt--copy-image-file (path)
2477 "Returns the internal name of the file"
2478 (let* ((image-type (file-name-extension path))
2479 (media-type (format "image/%s" image-type))
2480 (target-dir "Images/")
2481 (target-file
2482 (format "%s%04d.%s" target-dir
2483 (incf org-odt-embedded-images-count) image-type)))
2484 (message "Embedding %s as %s..."
2485 (substring-no-properties path) target-file)
2487 (when (= 1 org-odt-embedded-images-count)
2488 (make-directory (concat org-odt-zip-dir target-dir))
2489 (org-odt-create-manifest-file-entry "" target-dir))
2491 (copy-file path (concat org-odt-zip-dir target-file) 'overwrite)
2492 (org-odt-create-manifest-file-entry media-type target-file)
2493 target-file))
2495 (defun org-odt--image-size (file info &optional user-width
2496 user-height scale dpi embed-as)
2497 (let* ((--pixels-to-cms
2498 (function (lambda (pixels dpi)
2499 (let ((cms-per-inch 2.54)
2500 (inches (/ pixels dpi)))
2501 (* cms-per-inch inches)))))
2502 (--size-in-cms
2503 (function
2504 (lambda (size-in-pixels dpi)
2505 (and size-in-pixels
2506 (cons (funcall --pixels-to-cms (car size-in-pixels) dpi)
2507 (funcall --pixels-to-cms (cdr size-in-pixels) dpi))))))
2508 (dpi (or dpi (plist-get info :odt-pixels-per-inch)))
2509 (anchor-type (or embed-as "paragraph"))
2510 (user-width (and (not scale) user-width))
2511 (user-height (and (not scale) user-height))
2512 (size
2513 (and
2514 (not (and user-height user-width))
2516 ;; Use Imagemagick.
2517 (and (executable-find "identify")
2518 (let ((size-in-pixels
2519 (let ((dim (shell-command-to-string
2520 (format "identify -format \"%%w:%%h\" \"%s\""
2521 file))))
2522 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
2523 (cons (string-to-number (match-string 1 dim))
2524 (string-to-number (match-string 2 dim)))))))
2525 (funcall --size-in-cms size-in-pixels dpi)))
2526 ;; Use Emacs.
2527 (let ((size-in-pixels
2528 (ignore-errors ; Emacs could be in batch mode
2529 (clear-image-cache)
2530 (image-size (create-image file) 'pixels))))
2531 (funcall --size-in-cms size-in-pixels dpi))
2532 ;; Use hard-coded values.
2533 (cdr (assoc-string anchor-type
2534 org-odt-default-image-sizes-alist))
2535 ;; Error out.
2536 (error "Cannot determine image size, aborting"))))
2537 (width (car size)) (height (cdr size)))
2538 (cond
2539 (scale
2540 (setq width (* width scale) height (* height scale)))
2541 ((and user-height user-width)
2542 (setq width user-width height user-height))
2543 (user-height
2544 (setq width (* user-height (/ width height)) height user-height))
2545 (user-width
2546 (setq height (* user-width (/ height width)) width user-width))
2547 (t (ignore)))
2548 ;; ensure that an embedded image fits comfortably within a page
2549 (let ((max-width (car org-odt-max-image-size))
2550 (max-height (cdr org-odt-max-image-size)))
2551 (when (or (> width max-width) (> height max-height))
2552 (let* ((scale1 (/ max-width width))
2553 (scale2 (/ max-height height))
2554 (scale (min scale1 scale2)))
2555 (setq width (* scale width) height (* scale height)))))
2556 (cons width height)))
2558 (defun org-odt-link--inline-image (element info)
2559 "Return ODT code for an inline image.
2560 LINK is the link pointing to the inline image. INFO is a plist
2561 used as a communication channel."
2562 (assert (eq (org-element-type element) 'link))
2563 (let* ((src (let* ((type (org-element-property :type element))
2564 (raw-path (org-element-property :path element)))
2565 (cond ((member type '("http" "https"))
2566 (concat type ":" raw-path))
2567 ((file-name-absolute-p raw-path)
2568 (expand-file-name raw-path))
2569 (t raw-path))))
2570 (src-expanded (if (file-name-absolute-p src) src
2571 (expand-file-name src (file-name-directory
2572 (plist-get info :input-file)))))
2573 (href (format
2574 "\n<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>"
2575 (org-odt--copy-image-file src-expanded)))
2576 ;; Extract attributes from #+ATTR_ODT line.
2577 (attr-from (case (org-element-type element)
2578 (link (org-export-get-parent-element element))
2579 (t element)))
2580 ;; Handle `:anchor', `:style' and `:attributes' properties.
2581 (user-frame-anchor
2582 (car (assoc-string (org-odt--read-attribute attr-from :anchor)
2583 '(("as-char") ("paragraph") ("page")) t)))
2584 (user-frame-style
2585 (and user-frame-anchor (org-odt--read-attribute attr-from :style)))
2586 (user-frame-attrs
2587 (and user-frame-anchor (org-odt--read-attribute attr-from :attributes)))
2588 (user-frame-params
2589 (list user-frame-style user-frame-attrs user-frame-anchor))
2590 ;; (embed-as (or embed-as user-frame-anchor "paragraph"))
2592 ;; Handle `:width', `:height' and `:scale' properties. Read
2593 ;; them as numbers since we need them for computations.
2594 (size (org-odt--image-size
2595 src-expanded info
2596 (org-odt--read-attribute attr-from :width)
2597 (org-odt--read-attribute attr-from :height)
2598 (org-odt--read-attribute attr-from :scale)
2599 nil ; embed-as
2600 "paragraph" ; FIXME
2602 (width (car size)) (height (cdr size))
2603 (standalone-link-p (org-odt--standalone-link-p element info))
2604 (embed-as (if standalone-link-p "paragraph" "as-char"))
2605 (captions (org-odt-format-label element info 'definition))
2606 (caption (car captions)) (short-caption (cdr captions))
2607 (entity (concat (and caption "Captioned") embed-as "Image"))
2608 ;; Check if this link was created by LaTeX-to-PNG converter.
2609 (replaces (org-element-property
2610 :replaces (if (not standalone-link-p) element
2611 (org-export-get-parent-element element))))
2612 ;; If yes, note down the type of the element - LaTeX Fragment
2613 ;; or LaTeX environment. It will go in to frame title.
2614 (title (and replaces (capitalize
2615 (symbol-name (org-element-type replaces)))))
2617 ;; If yes, note down its contents. It will go in to frame
2618 ;; description. This quite useful for debugging.
2619 (desc (and replaces (org-element-property :value replaces))))
2620 (org-odt--render-image/formula entity href width height
2621 captions user-frame-params title desc)))
2624 ;;;; Links :: Math formula
2626 (defun org-odt-link--inline-formula (element info)
2627 (let* ((src (let* ((type (org-element-property :type element))
2628 (raw-path (org-element-property :path element)))
2629 (cond
2630 ((file-name-absolute-p raw-path)
2631 (expand-file-name raw-path))
2632 (t raw-path))))
2633 (src-expanded (if (file-name-absolute-p src) src
2634 (expand-file-name src (file-name-directory
2635 (plist-get info :input-file)))))
2636 (href
2637 (format
2638 "\n<draw:object %s xlink:href=\"%s\" xlink:type=\"simple\"/>"
2639 " xlink:show=\"embed\" xlink:actuate=\"onLoad\""
2640 (file-name-directory (org-odt--copy-formula-file src-expanded))))
2641 (standalone-link-p (org-odt--standalone-link-p element info))
2642 (embed-as (if standalone-link-p 'paragraph 'character))
2643 (captions (org-odt-format-label element info 'definition))
2644 (caption (car captions)) (short-caption (cdr captions))
2645 ;; Check if this link was created by LaTeX-to-MathML
2646 ;; converter.
2647 (replaces (org-element-property
2648 :replaces (if (not standalone-link-p) element
2649 (org-export-get-parent-element element))))
2650 ;; If yes, note down the type of the element - LaTeX Fragment
2651 ;; or LaTeX environment. It will go in to frame title.
2652 (title (and replaces (capitalize
2653 (symbol-name (org-element-type replaces)))))
2655 ;; If yes, note down its contents. It will go in to frame
2656 ;; description. This quite useful for debugging.
2657 (desc (and replaces (org-element-property :value replaces)))
2658 width height)
2659 (cond
2660 ((eq embed-as 'character)
2661 (org-odt--render-image/formula "InlineFormula" href width height
2662 nil nil title desc))
2664 (let* ((equation (org-odt--render-image/formula
2665 "CaptionedDisplayFormula" href width height
2666 captions nil title desc))
2667 (label
2668 (car (org-odt-format-label element info 'definition :label-format))))
2669 (concat equation "<text:tab/>" label))))))
2671 (defun org-odt--copy-formula-file (src-file)
2672 "Returns the internal name of the file"
2673 (let* ((target-dir (format "Formula-%04d/"
2674 (incf org-odt-embedded-formulas-count)))
2675 (target-file (concat target-dir "content.xml")))
2676 ;; Create a directory for holding formula file. Also enter it in
2677 ;; to manifest.
2678 (make-directory (concat org-odt-zip-dir target-dir))
2679 (org-odt-create-manifest-file-entry
2680 "application/vnd.oasis.opendocument.formula" target-dir "1.2")
2681 ;; Copy over the formula file from user directory to zip
2682 ;; directory.
2683 (message "Embedding %s as %s..." src-file target-file)
2684 (let ((case-fold-search nil))
2685 (cond
2686 ;; Case 1: Mathml.
2687 ((string-match "\\.\\(mathml\\|mml\\)\\'" src-file)
2688 (copy-file src-file (concat org-odt-zip-dir target-file) 'overwrite))
2689 ;; Case 2: OpenDocument formula.
2690 ((string-match "\\.odf\\'" src-file)
2691 (org-odt--zip-extract src-file "content.xml"
2692 (concat org-odt-zip-dir target-dir)))
2693 (t (error "%s is not a formula file" src-file))))
2694 ;; Enter the formula file in to manifest.
2695 (org-odt-create-manifest-file-entry "text/xml" target-file)
2696 target-file))
2698 ;;;; Targets
2700 (defun org-odt--render-image/formula (cfg-key href width height &optional
2701 captions user-frame-params
2702 &rest title-and-desc)
2703 (let* ((frame-cfg-alist
2704 ;; Each element of this alist is of the form (CFG-HANDLE
2705 ;; INNER-FRAME-PARAMS OUTER-FRAME-PARAMS).
2707 ;; CFG-HANDLE is the key to the alist.
2709 ;; INNER-FRAME-PARAMS and OUTER-FRAME-PARAMS specify the
2710 ;; frame params for INNER-FRAME and OUTER-FRAME
2711 ;; respectively. See below.
2713 ;; Configurations that are meant to be applied to
2714 ;; non-captioned image/formula specifies no
2715 ;; OUTER-FRAME-PARAMS.
2717 ;; TERMINOLOGY
2718 ;; ===========
2719 ;; INNER-FRAME :: Frame that directly surrounds an
2720 ;; image/formula.
2722 ;; OUTER-FRAME :: Frame that encloses the INNER-FRAME. This
2723 ;; frame also contains the caption, if any.
2725 ;; FRAME-PARAMS :: List of the form (FRAME-STYLE-NAME
2726 ;; FRAME-ATTRIBUTES FRAME-ANCHOR). Note
2727 ;; that these are the last three arguments
2728 ;; to `org-odt--frame'.
2730 ;; Note that an un-captioned image/formula requires just an
2731 ;; INNER-FRAME, while a captioned image/formula requires
2732 ;; both an INNER and an OUTER-FRAME.
2733 '(("As-CharImage" ("OrgInlineImage" nil "as-char"))
2734 ("ParagraphImage" ("OrgDisplayImage" nil "paragraph"))
2735 ("PageImage" ("OrgPageImage" nil "page"))
2736 ("CaptionedAs-CharImage"
2737 ("OrgCaptionedImage"
2738 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
2739 ("OrgInlineImage" nil "as-char"))
2740 ("CaptionedParagraphImage"
2741 ("OrgCaptionedImage"
2742 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
2743 ("OrgImageCaptionFrame" nil "paragraph"))
2744 ("CaptionedPageImage"
2745 ("OrgCaptionedImage"
2746 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
2747 ("OrgPageImageCaptionFrame" nil "page"))
2748 ("InlineFormula" ("OrgInlineFormula" nil "as-char"))
2749 ("DisplayFormula" ("OrgDisplayFormula" nil "as-char"))
2750 ("CaptionedDisplayFormula"
2751 ("OrgCaptionedFormula" nil "paragraph")
2752 ("OrgFormulaCaptionFrame" nil "paragraph"))))
2753 (caption (car captions)) (short-caption (cdr captions))
2754 ;; Retrieve inner and outer frame params, from configuration.
2755 (frame-cfg (assoc-string cfg-key frame-cfg-alist t))
2756 (inner (nth 1 frame-cfg))
2757 (outer (nth 2 frame-cfg))
2758 ;; User-specified frame params (from #+ATTR_ODT spec)
2759 (user user-frame-params)
2760 (--merge-frame-params (function
2761 (lambda (default user)
2762 "Merge default and user frame params."
2763 (if (not user) default
2764 (assert (= (length default) 3))
2765 (assert (= (length user) 3))
2766 (loop for u in user
2767 for d in default
2768 collect (or u d)))))))
2769 (cond
2770 ;; Case 1: Image/Formula has no caption.
2771 ;; There is only one frame, one that surrounds the image
2772 ;; or formula.
2773 ((not caption)
2774 ;; Merge user frame params with that from configuration.
2775 (setq inner (funcall --merge-frame-params inner user))
2776 (apply 'org-odt--frame href width height
2777 (append inner title-and-desc)))
2778 ;; Case 2: Image/Formula is captioned or labeled.
2779 ;; There are two frames: The inner one surrounds the
2780 ;; image or formula. The outer one contains the
2781 ;; caption/sequence number.
2783 ;; Merge user frame params with outer frame params.
2784 (setq outer (funcall --merge-frame-params outer user))
2785 ;; Short caption, if specified, goes as part of inner frame.
2786 (setq inner (let ((frame-params (copy-sequence inner)))
2787 (setcar (cdr frame-params)
2788 (concat
2789 (cadr frame-params)
2790 (when short-caption
2791 (format " draw:name=\"%s\" " short-caption))))
2792 frame-params))
2793 (apply 'org-odt--textbox
2794 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2795 "Illustration"
2796 (concat
2797 (apply 'org-odt--frame href width height
2798 (append inner title-and-desc))
2799 caption))
2800 width height outer)))))
2802 (defun org-odt--enumerable-p (element info)
2803 ;; Element should have a caption or label.
2804 (or (org-element-property :caption element)
2805 (org-element-property :name element)))
2807 (defun org-odt--enumerable-image-p (element info)
2808 (org-odt--standalone-link-p
2809 element info
2810 ;; Paragraph should have a caption or label. It SHOULD NOT be a
2811 ;; replacement element. (i.e., It SHOULD NOT be a result of LaTeX
2812 ;; processing.)
2813 (lambda (p)
2814 (and (not (org-element-property :replaces p))
2815 (or (org-element-property :caption p)
2816 (org-element-property :name p))))
2817 ;; Link should point to an image file.
2818 (lambda (l)
2819 (assert (eq (org-element-type l) 'link))
2820 (org-export-inline-image-p l (plist-get info :odt-inline-image-rules)))))
2822 (defun org-odt--enumerable-latex-image-p (element info)
2823 (org-odt--standalone-link-p
2824 element info
2825 ;; Paragraph should have a caption or label. It SHOULD also be a
2826 ;; replacement element. (i.e., It SHOULD be a result of LaTeX
2827 ;; processing.)
2828 (lambda (p)
2829 (and (org-element-property :replaces p)
2830 (or (org-element-property :caption p)
2831 (org-element-property :name p))))
2832 ;; Link should point to an image file.
2833 (lambda (l)
2834 (assert (eq (org-element-type l) 'link))
2835 (org-export-inline-image-p l (plist-get info :odt-inline-image-rules)))))
2837 (defun org-odt--enumerable-formula-p (element info)
2838 (org-odt--standalone-link-p
2839 element info
2840 ;; Paragraph should have a caption or label.
2841 (lambda (p)
2842 (or (org-element-property :caption p)
2843 (org-element-property :name p)))
2844 ;; Link should point to a MathML or ODF file.
2845 (lambda (l)
2846 (assert (eq (org-element-type l) 'link))
2847 (org-export-inline-image-p l (plist-get info :odt-inline-formula-rules)))))
2849 (defun org-odt--standalone-link-p (element info &optional
2850 paragraph-predicate
2851 link-predicate)
2852 "Test if ELEMENT is a standalone link for the purpose ODT export.
2853 INFO is a plist holding contextual information.
2855 Return non-nil, if ELEMENT is of type paragraph satisfying
2856 PARAGRAPH-PREDICATE and its sole content, save for whitespaces,
2857 is a link that satisfies LINK-PREDICATE.
2859 Return non-nil, if ELEMENT is of type link satisfying
2860 LINK-PREDICATE and its containing paragraph satisfies
2861 PARAGRAPH-PREDICATE in addition to having no other content save for
2862 leading and trailing whitespaces.
2864 Return nil, otherwise."
2865 (let ((p (case (org-element-type element)
2866 (paragraph element)
2867 (link (and (or (not link-predicate)
2868 (funcall link-predicate element))
2869 (org-export-get-parent element)))
2870 (t nil))))
2871 (when (and p (eq (org-element-type p) 'paragraph))
2872 (when (or (not paragraph-predicate)
2873 (funcall paragraph-predicate p))
2874 (let ((contents (org-element-contents p)))
2875 (loop for x in contents
2876 with inline-image-count = 0
2877 always (case (org-element-type x)
2878 (plain-text
2879 (not (org-string-nw-p x)))
2880 (link
2881 (and (or (not link-predicate)
2882 (funcall link-predicate x))
2883 (= (incf inline-image-count) 1)))
2884 (t nil))))))))
2886 (defun org-odt-link--infer-description (destination info)
2887 ;; DESTINATION is a HEADLINE, a "<<target>>" or an element (like
2888 ;; paragraph, verse-block etc) to which a "#+NAME: label" can be
2889 ;; attached. Note that labels that are attached to captioned
2890 ;; entities - inline images, math formulae and tables - get resolved
2891 ;; as part of `org-odt-format-label' and `org-odt--enumerate'.
2893 ;; Create a cross-reference to DESTINATION but make best-efforts to
2894 ;; create a *meaningful* description. Check item numbers, section
2895 ;; number and section title in that order.
2897 ;; NOTE: Counterpart of `org-export-get-ordinal'.
2898 ;; FIXME: Handle footnote-definition footnote-reference?
2899 (let* ((genealogy (org-element-lineage destination))
2900 (data (reverse genealogy))
2901 (label (case (org-element-type destination)
2902 (headline
2903 (format "sec-%s" (mapconcat 'number-to-string
2904 (org-export-get-headline-number
2905 destination info) "-")))
2906 (target
2907 (org-element-property :value destination))
2908 (t (org-element-property :name destination)))))
2910 ;; Case 1: Does the user want the cross-references to be typeset
2911 ;; in a custom manner (say for example, generate page numbers
2912 ;; etc.)? If yes, emit the required XML tags but with "???" as
2913 ;; the field value. This (incorrect) field value can be
2914 ;; corrected by using an external Office application. For
2915 ;; example, in case of LibreOffice, the field values can be
2916 ;; synchronized by running Tools->Update->Fields/Update All on
2917 ;; the exported document.
2918 (org-odt--xref-target :TARGET: "[PLS. UPDATE FIELDS]"
2919 (org-export-solidify-link-text label))
2920 ;; Case 2: Is target an item of a numbered list? If yes, use the
2921 ;; item's number as description. The target need not necessarily
2922 ;; be part of a proper numbered list, it can also be part of a
2923 ;; low-level headline that is deemed as a list for purposes of
2924 ;; export.
2925 (let* ( ;; Locate top-level list.
2926 (top-level-list
2927 (loop for x on data
2928 when (eq (org-element-type (car x)) 'plain-list)
2929 return x))
2930 ;; Get list item nos.
2931 (item-numbers
2932 (loop for (plain-list item . rest) on top-level-list by #'cddr
2933 until (not (eq (org-element-type plain-list) 'plain-list))
2934 collect (when (eq (org-element-property :type
2935 plain-list)
2936 'ordered)
2937 (1+ (length (org-export-get-previous-element
2938 item info t))))))
2939 ;; Locate top-most listified headline.
2940 (listified-headlines
2941 (loop for x on data
2942 when (and (eq (org-element-type (car x)) 'headline)
2943 (org-export-low-level-p (car x) info))
2944 return x))
2945 ;; Get listified headline numbers.
2946 (listified-headline-nos
2947 (loop for el in listified-headlines
2948 when (eq (org-element-type el) 'headline)
2949 collect (when (org-export-numbered-headline-p el info)
2950 (1+ (length (org-export-get-previous-element
2951 el info t)))))))
2952 ;; Combine item numbers from both the listified headlines and
2953 ;; regular list items.
2955 ;; Case 2.1: Check if all the parents of list item are
2956 ;; numbered. If yes, link to the item proper.
2957 (let ((item-numbers (append listified-headline-nos item-numbers)))
2958 (when (and item-numbers (not (memq nil item-numbers)))
2959 (format "<text:bookmark-ref text:reference-format=\"number-all-superior\" text:ref-name=\"%s\">%s</text:bookmark-ref>"
2960 (org-export-solidify-link-text label)
2961 (mapconcat (lambda (n) (if (not n) " "
2962 (concat (number-to-string n) ".")))
2963 item-numbers "")))))
2964 ;; Case 3: Is the target part of a regular and numbered headline
2965 ;; in the hierarchy? If yes, use the chapter/section number as
2966 ;; description.
2967 (let ((headline (loop for el in (cons destination genealogy)
2968 when (and (eq (org-element-type el) 'headline)
2969 (not (org-export-low-level-p el info))
2970 (org-export-numbered-headline-p el info))
2971 return el)))
2972 ;; We found one.
2973 (when headline
2974 (format "<text:bookmark-ref text:reference-format=\"chapter\" text:ref-name=\"%s\">%s</text:bookmark-ref>"
2975 (org-export-solidify-link-text label)
2976 (mapconcat 'number-to-string (org-export-get-headline-number
2977 headline info) "."))))
2978 ;; Case 4: Is the target part of any headline. If yes, use the
2979 ;; chapter/section's title description.
2980 (let ((headline (loop for el in (cons destination genealogy)
2981 when (and (eq (org-element-type el) 'headline)
2982 (not (org-export-low-level-p el info)))
2983 return el)))
2984 ;; We found one.
2985 (when headline
2986 (format "<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"%s\">%s</text:bookmark-ref>"
2987 (org-export-solidify-link-text label)
2988 (let ((title (org-element-property :title headline)))
2989 (org-export-data title info)))))
2990 ;; Case 5: The target is part of a document that is outside of
2991 ;; any headline. Use "???" as description. (We can use the
2992 ;; label text itself as the description. But, philosophically
2993 ;; speaking, this is in-appropriate. Targets are just labels and
2994 ;; must not generate any content text. So, it makes sense to
2995 ;; insist that the user provide an explicit description.)
2996 (format "<text:bookmark-ref text:reference-format=\"number-all-superior\" text:ref-name=\"%s\">%s</text:bookmark-ref>"
2997 (org-export-solidify-link-text label) "[PLS. UPDATE FIELDS]"))))
2999 (defun org-odt-link (link desc info)
3000 "Transcode a LINK object from Org to ODT.
3002 DESC is the description part of the link, or the empty string.
3003 INFO is a plist holding contextual information. See
3004 `org-export-data'."
3005 (let* ((type (org-element-property :type link))
3006 (raw-path (org-element-property :path link))
3007 ;; Ensure DESC really exists, or set it to nil.
3008 (desc (and (not (string= desc "")) desc))
3009 (imagep (org-export-inline-image-p
3010 link (plist-get info :odt-inline-image-rules)))
3011 (path (cond
3012 ((member type '("http" "https" "ftp" "mailto"))
3013 (concat type ":" raw-path))
3014 ((and (string= type "file") (file-name-absolute-p raw-path))
3015 (concat "file:" raw-path))
3016 (t raw-path)))
3017 ;; Convert & to &amp; for correct XML representation
3018 (path (replace-regexp-in-string "&" "&amp;" path)))
3019 (cond
3020 ;; Link type is handled by a special function.
3021 ((org-export-custom-protocol-maybe link desc 'odt))
3022 ;; Image file.
3023 ((and (not desc) (org-export-inline-image-p
3024 link (plist-get info :odt-inline-image-rules)))
3025 (org-odt-link--inline-image link info))
3026 ;; Formula file.
3027 ((and (not desc) (org-export-inline-image-p
3028 link (plist-get info :odt-inline-formula-rules)))
3029 (org-odt-link--inline-formula link info))
3030 ;; Radio target: Transcode target's contents and use them as
3031 ;; link's description.
3032 ((string= type "radio")
3033 (let ((destination (org-export-resolve-radio-link link info)))
3034 (if (not destination) desc
3035 (let ((desc (org-export-data (org-element-contents destination) info))
3036 (href (org-export-solidify-link-text
3037 (org-element-property :value destination))))
3039 ;; Case 1: Honour user's customization.
3040 (org-odt--xref-target :TARGET: "[PLS. UPDATE FIELDS]" href)
3041 ;; Case 2: Use the text of the radio target.
3042 (format
3043 "<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"%s\">%s</text:bookmark-ref>"
3044 href desc))))))
3045 ;; Links pointing to a headline: Find destination and build
3046 ;; appropriate referencing command.
3047 ((member type '("custom-id" "fuzzy" "id"))
3048 (let ((destination (if (string= type "fuzzy")
3049 (org-export-resolve-fuzzy-link link info)
3050 (org-export-resolve-id-link link info))))
3051 (case (org-element-type destination)
3052 ;; Case 1: Fuzzy link points nowhere.
3053 ('nil
3054 (user-error
3055 "Link \"%s\" at char position %d-%d points nowhere."
3056 (org-element-property :raw-link link)
3057 (org-element-property :begin link)
3058 (org-element-property :end link)))
3059 ;; Case 2: Fuzzy link points to a headline.
3060 (headline
3061 ;; If there's a description, create a hyperlink.
3062 ;; Otherwise, try to provide a meaningful description.
3063 (if (not desc) (org-odt-link--infer-description destination info)
3064 (let* ((headline-no
3065 (org-export-get-headline-number destination info))
3066 (label
3067 (format "sec-%s"
3068 (mapconcat 'number-to-string headline-no "-"))))
3069 (format
3070 "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
3071 label desc))))
3072 ;; Case 3: Fuzzy link points to a target.
3073 (target
3074 ;; If there's a description, create a hyperlink.
3075 ;; Otherwise, try to provide a meaningful description.
3076 (if (not desc) (org-odt-link--infer-description destination info)
3077 (let ((label (org-element-property :value destination)))
3078 (format "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
3079 (org-export-solidify-link-text label)
3080 desc))))
3081 ;; Case 4: Fuzzy link points to some element (e.g., an
3082 ;; inline image, a math formula or a table).
3083 (otherwise
3084 (let ((label-reference
3085 (ignore-errors (org-odt-format-label
3086 destination info 'reference))))
3087 (cond ((not label-reference)
3088 (org-odt-link--infer-description destination info))
3089 ;; LINK has no description. Create
3090 ;; a cross-reference showing entity's sequence
3091 ;; number.
3092 ((not desc) label-reference)
3093 ;; LINK has description. Insert a hyperlink with
3094 ;; user-provided description.
3096 (let ((label (org-element-property :name destination)))
3097 (format "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
3098 (org-export-solidify-link-text label)
3099 desc)))))))))
3100 ;; Coderef: replace link with the reference name or the
3101 ;; equivalent line number.
3102 ((string= type "coderef")
3103 (let* ((line-no (format "%d" (org-export-resolve-coderef path info)))
3104 (href (concat "coderef-" path)))
3105 (format
3106 (org-export-get-coderef-format path desc)
3107 (format
3108 "<text:bookmark-ref text:reference-format=\"number\" text:ref-name=\"%s\">%s</text:bookmark-ref>"
3109 href line-no))))
3110 ;; External link with a description part.
3111 ((and path desc)
3112 (let ((link-contents (org-element-contents link)))
3113 ;; Check if description is a link to an inline image.
3114 (if (and (not (cdr link-contents))
3115 (let ((desc-element (car link-contents)))
3116 (and (eq (org-element-type desc-element) 'link)
3117 (org-export-inline-image-p
3118 desc-element (plist-get info :odt-inline-image-rules)))))
3119 ;; Format link as a clickable image.
3120 (format "\n<draw:a xlink:type=\"simple\" xlink:href=\"%s\">\n%s\n</draw:a>"
3121 path desc)
3122 ;; Otherwise, format it as a regular link.
3123 (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
3124 path desc))))
3125 ;; External link without a description part.
3126 (path
3127 (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
3128 path path))
3129 ;; No path, only description. Try to do something useful.
3130 (t (format "<text:span text:style-name=\"%s\">%s</text:span>"
3131 "Emphasis" desc)))))
3134 ;;;; Node Property
3136 (defun org-odt-node-property (node-property contents info)
3137 "Transcode a NODE-PROPERTY element from Org to ODT.
3138 CONTENTS is nil. INFO is a plist holding contextual
3139 information."
3140 (org-odt--encode-plain-text
3141 (format "%s:%s"
3142 (org-element-property :key node-property)
3143 (let ((value (org-element-property :value node-property)))
3144 (if value (concat " " value) "")))))
3146 ;;;; Paragraph
3148 ;; You can customize paragraphs - standalone one and those occurring
3149 ;; within lists - using `:style' and `:p-style' attributes. Try out
3150 ;; the following example and see for yourself what you can achieve.
3152 ;; #+ATTR_ODT: :style "OrgBulletedList" :p-style "Text_20_body_20_bold"
3153 ;; 1. N1
3154 ;; 1. N11
3155 ;; 2. N12
3156 ;; 2. N2
3157 ;; #+ATTR_ODT: :style "OrgNumberedList" :p-style "Preformatted_20_Text"
3158 ;; * B21
3160 ;; * B22
3161 ;; - B221
3163 ;; First paragraph.
3165 ;; #+ATTR_ODT: :style "OrgBibliographyList" :p-style "Text_20_body"
3166 ;; 1. one
3167 ;; 2. two
3168 ;; 3. three
3170 ;; #+ATTR_ODT: :style "Text_20_body_20_indent"
3171 ;; Second paragraph.
3173 ;; - B222
3174 ;; * B23
3175 ;; 3. N3
3177 (defun org-odt-paragraph (paragraph contents info)
3178 "Transcode a PARAGRAPH element from Org to ODT.
3179 CONTENTS is the contents of the paragraph, as a string. INFO is
3180 the plist used as a communication channel."
3181 (let* ((parent (org-export-get-parent paragraph))
3182 (parent-type (org-element-type parent))
3183 (genealogy (cons paragraph (org-element-lineage paragraph)))
3184 (data (reverse genealogy))
3185 (style
3186 ;; Traverse the parse-tree from root element to this
3187 ;; paragraph. Use the following rule at each element to
3188 ;; calculate the paragraph style applicable at that element.
3190 ;; Case 1: If an element specifies an EXPLICIT STYLE of it's
3191 ;; own via the #+ATTR_ODT line, use it. PARAGRAPH and
3192 ;; SPECIAL-BLOCK use the `:style' attribute for this
3193 ;; purpose, while PLAIN-LIST uses `:p-style' attribute.
3195 ;; Case 2: If an element does not have an explicit style but
3196 ;; has an IMPLICIT, PRE-CONFIGURE STYLE of it's own, use it.
3197 ;; For example, paragraphs within a FOOTNOTE-DEFINITON,
3198 ;; CENTER-BLOCK or QUOTE-BLOCK get pre-configured styles
3199 ;; like "Footnote", "OrgCenter" or "Quotations" resply.
3201 ;; Case 3: If an element specifies neither an IMPLICIT style
3202 ;; or an EXPLICIT style, use the style from it's parent.
3203 ;; For example, a paragraph within a PLAIN-LIST (that
3204 ;; doesn't specify a `:p-style' of it's own) inherit it's
3205 ;; style from the it's parent.
3207 ;; Case 4: If an element has no parent (i.e., root node),
3208 ;; use the fallback style "Text_20_body".
3209 (loop for el in data
3210 ;; Fallback style.
3211 with style = "Text_20_body"
3212 with footnote-definition-p = nil do
3213 (setq style
3215 ;; Case 1: Does this node IMPLICITLY or
3216 ;; EXPLICITLY specify a style? Use it.
3217 (case (org-element-type el)
3218 (center-block
3219 (or (org-odt--read-attribute el :style)
3220 (if footnote-definition-p "OrgFootnoteCenter"
3221 "OrgCenter")))
3222 (footnote-definition
3223 (setq footnote-definition-p t)
3224 (or (org-odt--read-attribute el :style) "Footnote"))
3225 (paragraph
3227 ;; Case 1: Some paragraphs are "created"
3228 ;; not by the user but by the
3229 ;; pre-processing stage. They use the
3230 ;; `:style' property of the element rather
3231 ;; than the style property from the
3232 ;; attribute line. See
3233 ;; `org-odt--translate-description-lists/latex',
3234 ;; `org-odt--translate-description-lists/html'
3235 ;; `org-odt--translate-latex-fragments'.
3236 (org-element-property :style el)
3237 (org-odt--read-attribute el :style)))
3238 (plain-list
3239 ;; NOTE: ITEMs cannot have #+ATTR_ODT
3240 ;; attached to them. See
3242 ;; http://lists.gnu.org/archive/html/emacs-orgmode/2013-08/msg00586.html
3243 (org-odt--read-attribute el :p-style))
3244 (quote-block
3245 (if footnote-definition-p "OrgFootnoteQuotations"
3246 "Quotations"))
3247 (special-block
3248 (let ((type (downcase (org-element-property :type el))))
3249 (cond
3250 ;; Case 1: Handle SPECIAL-BLOCKs that are
3251 ;; well-known (and treated specially) by
3252 ;; the ODT exporter.
3253 ((string= type "textbox")
3254 (org-odt--read-attribute el :p-style))
3255 ;; Case 2: Handle user-specified
3256 ;; SPECIAL-BLOCKs not known to the
3257 ;; exporter.
3258 (t (org-odt--read-attribute el :style))))))
3259 ;; Case 2: Element doesn't specify a style of
3260 ;; it's own. Use the parent style.
3261 style))
3262 finally return style)))
3263 ;; If this paragraph is a leading paragraph in an item and the
3264 ;; item has a checkbox, splice the checkbox and paragraph contents
3265 ;; together.
3266 (when (and (eq (org-element-type parent) 'item)
3267 (eq paragraph (car (org-element-contents parent))))
3268 (setq contents (concat (org-odt--checkbox parent) contents)))
3269 (format "\n<text:p text:style-name=\"%s\">%s</text:p>" style contents)))
3272 ;;;; Plain List
3274 (defun org-odt-plain-list (plain-list contents info)
3275 "Transcode a PLAIN-LIST element from Org to ODT.
3276 CONTENTS is the contents of the list. INFO is a plist holding
3277 contextual information."
3278 (format "\n<text:list text:style-name=\"%s\" %s>\n%s</text:list>"
3279 ;; Choose style based on list type.
3280 (case (org-element-property :type plain-list)
3281 (ordered (or (org-odt--read-attribute plain-list :style)
3282 "OrgNumberedList"))
3283 (unordered (or (org-odt--read-attribute plain-list :style)
3284 "OrgBulletedList"))
3285 ;; FIXME: Define and handle `:style' attributes for
3286 ;; description lists.
3287 (descriptive-1 "OrgDescriptionList")
3288 (descriptive-2 "OrgDescriptionList"))
3289 ;; If top-level list, re-start numbering. Otherwise,
3290 ;; continue numbering.
3291 (format "text:continue-numbering=\"%s\""
3292 (let* ((parent (org-export-get-parent plain-list)))
3293 (if (and parent (eq (org-element-type parent) 'item))
3294 "true" "false")))
3295 contents))
3297 ;;;; Plain Text
3299 (defun org-odt--encode-tabs-and-spaces (line)
3300 (replace-regexp-in-string
3301 "\\([\t]\\|\\([ ]+\\)\\)"
3302 (lambda (s)
3303 (cond
3304 ((string= s "\t") "<text:tab/>")
3305 (t (let ((n (length s)))
3306 (cond
3307 ((= n 1) " ")
3308 ((> n 1) (concat " " (format "<text:s text:c=\"%d\"/>" (1- n))))
3309 (t ""))))))
3310 line))
3312 (defun org-odt--encode-plain-text (text &optional no-whitespace-filling)
3313 (mapc
3314 (lambda (pair)
3315 (setq text (replace-regexp-in-string (car pair) (cdr pair) text t t)))
3316 '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
3317 (if no-whitespace-filling text
3318 (org-odt--encode-tabs-and-spaces text)))
3320 (defun org-odt-plain-text (text info)
3321 "Transcode a TEXT string from Org to ODT.
3322 TEXT is the string to transcode. INFO is a plist holding
3323 contextual information."
3324 (let ((output text))
3325 ;; Protect &, < and >.
3326 (setq output (org-odt--encode-plain-text output t))
3327 ;; Handle smart quotes. Be sure to provide original string since
3328 ;; OUTPUT may have been modified.
3329 (when (plist-get info :with-smart-quotes)
3330 (setq output (org-export-activate-smart-quotes output :utf-8 info text)))
3331 ;; Convert special strings.
3332 (when (plist-get info :with-special-strings)
3333 (mapc
3334 (lambda (pair)
3335 (setq output
3336 (replace-regexp-in-string (car pair) (cdr pair) output t nil)))
3337 org-odt-special-string-regexps))
3338 ;; Handle break preservation if required.
3339 (when (plist-get info :preserve-breaks)
3340 (setq output (replace-regexp-in-string
3341 "\\(\\\\\\\\\\)?[ \t]*\n" "<text:line-break/>" output t)))
3342 ;; Return value.
3343 output))
3346 ;;;; Planning
3348 (defun org-odt-planning (planning contents info)
3349 "Transcode a PLANNING element from Org to ODT.
3350 CONTENTS is nil. INFO is a plist used as a communication
3351 channel."
3352 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3353 "OrgPlanning"
3354 (concat
3355 (let ((closed (org-element-property :closed planning)))
3356 (when closed
3357 (concat
3358 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3359 "OrgClosedKeyword" org-closed-string)
3360 (org-odt-timestamp closed contents info))))
3361 (let ((deadline (org-element-property :deadline planning)))
3362 (when deadline
3363 (concat
3364 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3365 "OrgDeadlineKeyword" org-deadline-string)
3366 (org-odt-timestamp deadline contents info))))
3367 (let ((scheduled (org-element-property :scheduled planning)))
3368 (when scheduled
3369 (concat
3370 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3371 "OrgScheduledKeyword" org-deadline-string)
3372 (org-odt-timestamp scheduled contents info)))))))
3375 ;;;; Property Drawer
3377 (defun org-odt-property-drawer (property-drawer contents info)
3378 "Transcode a PROPERTY-DRAWER element from Org to ODT.
3379 CONTENTS holds the contents of the drawer. INFO is a plist
3380 holding contextual information."
3381 (and (org-string-nw-p contents)
3382 (format "<text:p text:style-name=\"OrgFixedWidthBlock\">%s</text:p>"
3383 contents)))
3386 ;;;; Quote Block
3388 (defun org-odt-quote-block (quote-block contents info)
3389 "Transcode a QUOTE-BLOCK element from Org to ODT.
3390 CONTENTS holds the contents of the block. INFO is a plist
3391 holding contextual information."
3392 contents)
3395 ;;;; Section
3397 (defun org-odt-format-section (text style &optional name)
3398 (let ((default-name (car (org-odt-add-automatic-style "Section"))))
3399 (format "\n<text:section text:style-name=\"%s\" %s>\n%s\n</text:section>"
3400 style
3401 (format "text:name=\"%s\"" (or name default-name))
3402 text)))
3405 (defun org-odt-section (section contents info) ; FIXME
3406 "Transcode a SECTION element from Org to ODT.
3407 CONTENTS holds the contents of the section. INFO is a plist
3408 holding contextual information."
3409 contents)
3411 ;;;; Radio Target
3413 (defun org-odt-radio-target (radio-target text info)
3414 "Transcode a RADIO-TARGET object from Org to ODT.
3415 TEXT is the text of the target. INFO is a plist holding
3416 contextual information."
3417 (org-odt--target
3418 text (org-export-solidify-link-text
3419 (org-element-property :value radio-target))))
3422 ;;;; Special Block
3424 (defun org-odt-special-block (special-block contents info)
3425 "Transcode a SPECIAL-BLOCK element from Org to ODT.
3426 CONTENTS holds the contents of the block. INFO is a plist
3427 holding contextual information."
3428 (let ((type (org-element-property :type special-block))
3429 (attributes (org-export-read-attribute :attr_odt special-block)))
3430 (cond
3431 ;; Annotation.
3432 ((string= type "annotation")
3433 (let* ((author (or (plist-get attributes :author)
3434 (let ((author (plist-get info :author)))
3435 (and author (org-export-data author info)))))
3436 (date (or (plist-get attributes :date)
3437 ;; FIXME: Is `car' right thing to do below?
3438 (car (plist-get info :date)))))
3439 (format "\n<text:p>%s</text:p>"
3440 (format "<office:annotation>\n%s\n</office:annotation>"
3441 (concat
3442 (and author
3443 (format "<dc:creator>%s</dc:creator>" author))
3444 (and date
3445 (format "<dc:date>%s</dc:date>"
3446 (org-odt--format-timestamp date nil 'iso-date)))
3447 contents)))))
3448 ;; Textbox.
3449 ((string= type "textbox")
3450 ;; Textboxes an be used for centering tables etc horizontally
3451 ;; and vertically within a page.
3453 ;; In the example below, a landscape and centered table is
3454 ;; created in the middle of what is essentially a portrait
3455 ;; document.
3457 ;; Leading text.
3459 ;; #+ATTR_ODT: :style "OrgPageBreakLandscape"
3460 ;; #+PAGEBREAK:
3462 ;; #+ATTR_ODT: :width 5 :style "OrgPageImage" :anchor "page"
3463 ;; #+BEGIN_TEXTBOX
3464 ;; | a | b |
3465 ;; | e | f |
3466 ;; #+END_TEXTBOX
3468 ;; #+ATTR_ODT: :style "OrgPageBreakDefault"
3469 ;; #+PAGEBREAK:
3471 ;; Trailing text.
3472 (let ((width (org-odt--read-attribute special-block :width))
3473 (height (org-odt--read-attribute special-block :height))
3474 (style (org-odt--read-attribute special-block :style))
3475 (extra (org-odt--read-attribute special-block :extra))
3476 (anchor (org-odt--read-attribute special-block :anchor)))
3477 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3478 "Text_20_body" (org-odt--textbox contents width height
3479 style extra anchor))))
3480 (t contents))))
3483 ;;;; Src Block
3485 (defun org-odt-hfy-face-to-css (fn)
3486 "Create custom style for face FN.
3487 When FN is the default face, use its foreground and background
3488 properties to create \"OrgSrcBlock\" paragraph style. Otherwise
3489 use its color attribute to create a character style whose name
3490 is obtained from FN. Currently all attributes of FN other than
3491 color are ignored.
3493 The style name for a face FN is derived using the following
3494 operations on the face name in that order - de-dash, CamelCase
3495 and prefix with \"OrgSrc\". For example,
3496 `font-lock-function-name-face' is associated with
3497 \"OrgSrcFontLockFunctionNameFace\"."
3498 (let* ((css-list (hfy-face-to-style fn))
3499 (style-name (concat "OrgSrc"
3500 (mapconcat
3501 'capitalize (split-string
3502 (hfy-face-or-def-to-name fn) "-")
3503 "")))
3504 (color-val (cdr (assoc "color" css-list)))
3505 (background-color-val (cdr (assoc "background" css-list)))
3506 (style (and org-odt-create-custom-styles-for-srcblocks
3507 (cond
3508 ((eq fn 'default)
3509 (format org-odt-src-block-paragraph-format
3510 background-color-val color-val))
3512 (format
3514 <style:style style:name=\"%s\" style:family=\"text\">
3515 <style:text-properties fo:color=\"%s\"/>
3516 </style:style>" style-name color-val))))))
3517 (cons style-name style)))
3519 (defun org-odt-htmlfontify-string (line)
3520 (let* ((hfy-html-quote-regex "\\([<\"&> ]\\)")
3521 (hfy-html-quote-map '(("\"" "&quot;")
3522 ("<" "&lt;")
3523 ("&" "&amp;")
3524 (">" "&gt;")
3525 (" " "<text:s/>")
3526 (" " "<text:tab/>")))
3527 (hfy-face-to-css 'org-odt-hfy-face-to-css)
3528 (hfy-optimisations-1 (copy-sequence hfy-optimisations))
3529 (hfy-optimisations (add-to-list 'hfy-optimisations-1
3530 'body-text-only))
3531 (hfy-begin-span-handler
3532 (lambda (style text-block text-id text-begins-block-p)
3533 (insert (format "<text:span text:style-name=\"%s\">" style))))
3534 (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
3535 (org-no-warnings (htmlfontify-string line))))
3537 (defun org-odt-do-format-code
3538 (code info &optional lang refs retain-labels num-start)
3539 (let* ((lang (or (assoc-default lang org-src-lang-modes) lang))
3540 (lang-mode (and lang (intern (format "%s-mode" lang))))
3541 (code-lines (org-split-string code "\n"))
3542 (code-length (length code-lines))
3543 (use-htmlfontify-p (and (functionp lang-mode)
3544 (plist-get info :odt-fontify-srcblocks)
3545 (require 'htmlfontify nil t)
3546 (fboundp 'htmlfontify-string)))
3547 (code (if (not use-htmlfontify-p) code
3548 (with-temp-buffer
3549 (insert code)
3550 (funcall lang-mode)
3551 (font-lock-ensure)
3552 (buffer-string))))
3553 (fontifier (if use-htmlfontify-p 'org-odt-htmlfontify-string
3554 'org-odt--encode-plain-text))
3555 (par-style (if use-htmlfontify-p "OrgSrcBlock"
3556 "OrgFixedWidthBlock"))
3557 (i 0))
3558 (assert (= code-length (length (org-split-string code "\n"))))
3559 (setq code
3560 (org-export-format-code
3561 code
3562 (lambda (loc line-num ref)
3563 (setq par-style
3564 (concat par-style (and (= (incf i) code-length) "LastLine")))
3566 (setq loc (concat loc (and ref retain-labels (format " (%s)" ref))))
3567 (setq loc (funcall fontifier loc))
3568 (when ref
3569 (setq loc (org-odt--target loc (concat "coderef-" ref))))
3570 (assert par-style)
3571 (setq loc (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3572 par-style loc))
3573 (if (not line-num) loc
3574 (format "\n<text:list-item>%s\n</text:list-item>" loc)))
3575 num-start refs))
3576 (cond
3577 ((not num-start) code)
3578 ((= num-start 0)
3579 (format
3580 "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
3581 " text:continue-numbering=\"false\"" code))
3583 (format
3584 "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
3585 " text:continue-numbering=\"true\"" code)))))
3587 (defun org-odt-format-code (element info)
3588 (let* ((lang (org-element-property :language element))
3589 ;; Extract code and references.
3590 (code-info (org-export-unravel-code element))
3591 (code (car code-info))
3592 (refs (cdr code-info))
3593 ;; Does the src block contain labels?
3594 (retain-labels (org-element-property :retain-labels element))
3595 ;; Does it have line numbers?
3596 (num-start (case (org-element-property :number-lines element)
3597 (continued (org-export-get-loc element info))
3598 (new 0))))
3599 (org-odt-do-format-code code info lang refs retain-labels num-start)))
3601 (defun org-odt-src-block (src-block contents info)
3602 "Transcode a SRC-BLOCK element from Org to ODT.
3603 CONTENTS holds the contents of the item. INFO is a plist holding
3604 contextual information."
3605 (let* ((lang (org-element-property :language src-block))
3606 (captions (org-odt-format-label src-block info 'definition))
3607 (caption (car captions)) (short-caption (cdr captions)))
3608 (concat
3609 (and caption
3610 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3611 "Listing" caption))
3612 (let ((--src-block (org-odt-format-code src-block info)))
3613 ;; Is `:textbox' property non-nil?
3614 (if (not (org-odt--read-attribute src-block :textbox)) --src-block
3615 ;; Yes. Enclose it in a Text Box.
3616 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3617 "Text_20_body"
3618 (org-odt--textbox --src-block nil nil nil)))))))
3621 ;;;; Statistics Cookie
3623 (defun org-odt-statistics-cookie (statistics-cookie contents info)
3624 "Transcode a STATISTICS-COOKIE object from Org to ODT.
3625 CONTENTS is nil. INFO is a plist holding contextual information."
3626 (let ((cookie-value (org-element-property :value statistics-cookie)))
3627 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3628 "OrgCode" cookie-value)))
3631 ;;;; Strike-Through
3633 (defun org-odt-strike-through (strike-through contents info)
3634 "Transcode STRIKE-THROUGH from Org to ODT.
3635 CONTENTS is the text with strike-through markup. INFO is a plist
3636 holding contextual information."
3637 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3638 "Strikethrough" contents))
3641 ;;;; Subscript
3643 (defun org-odt-subscript (subscript contents info)
3644 "Transcode a SUBSCRIPT object from Org to ODT.
3645 CONTENTS is the contents of the object. INFO is a plist holding
3646 contextual information."
3647 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3648 "OrgSubscript" contents))
3651 ;;;; Superscript
3653 (defun org-odt-superscript (superscript contents info)
3654 "Transcode a SUPERSCRIPT object from Org to ODT.
3655 CONTENTS is the contents of the object. INFO is a plist holding
3656 contextual information."
3657 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3658 "OrgSuperscript" contents))
3661 ;;;; Table Cell
3663 (defun org-odt-table-style-spec (element info)
3664 (let* ((table (org-export-get-parent-table element))
3665 (table-style (org-odt--read-attribute table :style)))
3666 (assoc table-style (plist-get info :odt-table-styles))))
3668 (defun org-odt-get-table-cell-styles (table-cell info)
3669 "Retrieve styles applicable to a table cell.
3670 R and C are (zero-based) row and column numbers of the table
3671 cell. STYLE-SPEC is an entry in `org-odt-table-styles'
3672 applicable to the current table. It is nil if the table is not
3673 associated with any style attributes.
3675 Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
3677 When STYLE-SPEC is nil, style the table cell the conventional way
3678 - choose cell borders based on row and column groupings and
3679 choose paragraph alignment based on `org-col-cookies' text
3680 property. See also
3681 `org-odt-get-paragraph-style-cookie-for-table-cell'.
3683 When STYLE-SPEC is non-nil, ignore the above cookie and return
3684 styles congruent with the ODF-1.2 specification."
3685 (let* ((table-cell-address (org-export-table-cell-address table-cell info))
3686 (r (car table-cell-address)) (c (cdr table-cell-address))
3687 (style-spec (org-odt-table-style-spec table-cell info))
3688 (table-dimensions (org-export-table-dimensions
3689 (org-export-get-parent-table table-cell)
3690 info)))
3691 (when style-spec
3692 ;; LibreOffice - particularly the Writer - honors neither table
3693 ;; templates nor custom table-cell styles. Inorder to retain
3694 ;; inter-operability with LibreOffice, only automatic styles are
3695 ;; used for styling of table-cells. The current implementation is
3696 ;; congruent with ODF-1.2 specification and hence is
3697 ;; future-compatible.
3699 ;; Additional Note: LibreOffice's AutoFormat facility for tables -
3700 ;; which recognizes as many as 16 different cell types - is much
3701 ;; richer. Unfortunately it is NOT amenable to easy configuration
3702 ;; by hand.
3703 (let* ((template-name (nth 1 style-spec))
3704 (cell-style-selectors (nth 2 style-spec))
3705 (cell-type
3706 (cond
3707 ((and (cdr (assoc 'use-first-column-styles cell-style-selectors))
3708 (= c 0)) "FirstColumn")
3709 ((and (cdr (assoc 'use-last-column-styles cell-style-selectors))
3710 (= (1+ c) (cdr table-dimensions)))
3711 "LastColumn")
3712 ((and (cdr (assoc 'use-first-row-styles cell-style-selectors))
3713 (= r 0)) "FirstRow")
3714 ((and (cdr (assoc 'use-last-row-styles cell-style-selectors))
3715 (= (1+ r) (car table-dimensions)))
3716 "LastRow")
3717 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
3718 (= (% r 2) 1)) "EvenRow")
3719 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
3720 (= (% r 2) 0)) "OddRow")
3721 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
3722 (= (% c 2) 1)) "EvenColumn")
3723 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
3724 (= (% c 2) 0)) "OddColumn")
3725 (t ""))))
3726 (concat template-name cell-type)))))
3728 (defun org-odt--table-cell-widths (table info)
3729 (let* ((user-widths (org-export-read-attribute :attr_odt table :widths))
3730 (user-width-p (and user-widths t))
3731 (user-widths (and user-width-p (split-string user-widths ","))))
3732 (org-element-map
3733 (org-element-map table 'table-row
3734 (lambda (row)
3735 (unless (eq (org-element-property :type row) 'rule) row))
3736 info 'first-match)
3737 'table-cell
3738 (lambda (table-cell)
3739 (or (and user-width-p (string-to-number (or (pop user-widths) "0")))
3740 (org-export-table-cell-width table-cell info) 0))
3741 info)))
3743 (defun org-odt-table-cell (table-cell contents info)
3744 "Transcode a TABLE-CELL element from Org to ODT.
3745 CONTENTS is nil. INFO is a plist used as a communication
3746 channel."
3747 (let* ((table-cell-address (org-export-table-cell-address table-cell info))
3748 (table-cell-borders (org-export-table-cell-borders table-cell info))
3749 (r (car table-cell-address))
3750 (c (cdr table-cell-address))
3751 (horiz-span (nth c (org-odt--table-cell-widths
3752 (org-export-get-parent-table table-cell) info)))
3753 (table-row (org-export-get-parent table-cell))
3754 (custom-style-prefix (org-odt-get-table-cell-styles
3755 table-cell info))
3756 (paragraph-style
3758 (and custom-style-prefix
3759 (format "%sTableParagraph" custom-style-prefix))
3760 (concat
3761 (cond
3762 ((and (= 1 (org-export-table-row-group table-row info))
3763 (org-export-table-has-header-p
3764 (org-export-get-parent-table table-row) info))
3765 "OrgTableHeading")
3766 ((let* ((table (org-export-get-parent-table table-cell))
3767 (table-header-columns
3768 (let ((cols (org-odt--read-attribute table :header-columns)))
3769 (and cols (read cols)))))
3770 (<= c (cond ((wholenump table-header-columns)
3771 (- table-header-columns 1))
3772 (table-header-columns 0)
3773 (t -1))))
3774 "OrgTableHeading")
3775 (t "OrgTableContents"))
3776 (capitalize (symbol-name (org-export-table-cell-alignment
3777 table-cell info))))))
3778 (cell-style-name
3780 (and custom-style-prefix (format "%sTableCell"
3781 custom-style-prefix))
3782 (concat
3783 "OrgTblCell"
3784 (when (memq 'above table-cell-borders) "T")
3785 (when (memq 'below table-cell-borders) "B")
3786 (when (memq 'left table-cell-borders) "L")
3787 (when (memq 'right table-cell-borders) "R"))))
3788 (cell-attributes
3789 (concat
3790 (format " table:style-name=\"%s\"" cell-style-name)
3791 (and (> horiz-span 0)
3792 (format " table:number-columns-spanned=\"%d\""
3793 (1+ horiz-span))))))
3794 (unless contents (setq contents ""))
3795 (concat
3796 (assert paragraph-style)
3797 (format "\n<table:table-cell%s>\n%s\n</table:table-cell>"
3798 cell-attributes
3799 (let ((table-cell-contents (org-element-contents table-cell)))
3800 (if (memq (org-element-type (car table-cell-contents))
3801 org-element-all-elements)
3802 contents
3803 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3804 paragraph-style contents))))
3805 (let (s)
3806 (dotimes (i horiz-span s)
3807 (setq s (concat s "\n<table:covered-table-cell/>"))))
3808 "\n")))
3811 ;;;; Table Row
3813 (defun org-odt-table-row (table-row contents info)
3814 "Transcode a TABLE-ROW element from Org to ODT.
3815 CONTENTS is the contents of the row. INFO is a plist used as a
3816 communication channel."
3817 ;; Rules are ignored since table separators are deduced from
3818 ;; borders of the current row.
3819 (when (eq (org-element-property :type table-row) 'standard)
3820 (let* ((rowgroup-tags
3821 (if (and (= 1 (org-export-table-row-group table-row info))
3822 (org-export-table-has-header-p
3823 (org-export-get-parent-table table-row) info))
3824 ;; If the row belongs to the first rowgroup and the
3825 ;; table has more than one row groups, then this row
3826 ;; belongs to the header row group.
3827 '("\n<table:table-header-rows>" . "\n</table:table-header-rows>")
3828 ;; Otherwise, it belongs to non-header row group.
3829 '("\n<table:table-rows>" . "\n</table:table-rows>"))))
3830 (concat
3831 ;; Does this row begin a rowgroup?
3832 (when (org-export-table-row-starts-rowgroup-p table-row info)
3833 (car rowgroup-tags))
3834 ;; Actual table row
3835 (let* ((custom-table-style (nth 1 (org-odt-table-style-spec table-row info)))
3836 (table-style (or custom-table-style "OrgTable"))
3837 (row-style (format "%sRow" table-style)))
3838 (format "\n<table:table-row table:style-name=\"%s\">\n%s\n</table:table-row>"
3839 row-style contents))
3840 ;; Does this row end a rowgroup?
3841 (when (org-export-table-row-ends-rowgroup-p table-row info)
3842 (cdr rowgroup-tags))))))
3845 ;;;; Table
3847 (defun org-odt--table (table contents info)
3848 "Transcode a TABLE element from Org to ODT.
3849 CONTENTS is the contents of the table. INFO is a plist holding
3850 contextual information."
3851 (case (org-element-property :type table)
3852 ;; Case 1: table.el doesn't support export to OD format. Strip
3853 ;; such tables from export.
3854 (table.el
3855 (prog1 nil
3856 (message
3857 (concat
3858 "(ox-odt): Found table.el-type table in the source Org file."
3859 " table.el doesn't support export to ODT format."
3860 " Stripping the table from export."))))
3861 ;; Case 2: Native Org tables.
3862 (otherwise
3863 (let* ((captions (org-odt-format-label table info 'definition))
3864 (caption (car captions)) (short-caption (cdr captions))
3865 (attributes (org-export-read-attribute :attr_odt table))
3866 (custom-table-style (nth 1 (org-odt-table-style-spec table info)))
3867 (table-column-specs
3868 (function
3869 (lambda (table info)
3870 (let* ((table-style (or custom-table-style "OrgTable"))
3871 (column-style (format "%sColumn" table-style)))
3872 (mapconcat
3873 (lambda (width)
3874 (setq width (1+ width))
3875 (let ((s (format
3876 "\n<table:table-column table:style-name=\"%s\"/>"
3877 column-style))
3878 out)
3879 (dotimes (i width out) (setq out (concat s out)))))
3880 (org-odt--table-cell-widths table info) "\n"))))))
3881 (concat
3882 ;; caption.
3883 (when caption
3884 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3885 "Table" caption))
3886 ;; begin table.
3887 (let* ((automatic-name
3888 (org-odt-add-automatic-style "Table" attributes)))
3889 (format
3890 "\n<table:table table:style-name=\"%s\"%s>"
3891 (or custom-table-style (cdr automatic-name) "OrgTable")
3892 (concat (when short-caption
3893 (format " table:name=\"%s\"" short-caption)))))
3894 ;; column specification.
3895 (funcall table-column-specs table info)
3896 ;; actual contents.
3897 "\n" contents
3898 ;; end table.
3899 "</table:table>")))))
3901 (defun org-odt-table (table contents info)
3902 "Transcode a TABLE element from Org to ODT.
3903 CONTENTS is the contents of the table. INFO is a plist holding
3904 contextual information.
3906 Use `org-odt--table' to typeset the table. Handle details
3907 pertaining to indentation here."
3908 (let* ((--element-preceded-by-table-p
3909 (function
3910 (lambda (element info)
3911 (loop for el in (org-export-get-previous-element element info t)
3912 thereis (eq (org-element-type el) 'table)))))
3913 (--walk-list-genealogy-and-collect-tags
3914 (function
3915 (lambda (table info)
3916 (let* ((genealogy (org-element-lineage table))
3917 (list-genealogy
3918 (when (eq (org-element-type (car genealogy)) 'item)
3919 (loop for el in genealogy
3920 when (memq (org-element-type el)
3921 '(item plain-list))
3922 collect el)))
3923 (llh-genealogy
3924 (apply 'nconc
3925 (loop for el in genealogy
3926 when (and (eq (org-element-type el) 'headline)
3927 (org-export-low-level-p el info))
3928 collect
3929 (list el
3930 (assq 'headline
3931 (org-element-contents
3932 (org-export-get-parent el)))))))
3933 parent-list)
3934 (nconc
3935 ;; Handle list genealogy.
3936 (loop for el in list-genealogy collect
3937 (case (org-element-type el)
3938 (plain-list
3939 (setq parent-list el)
3940 (cons "</text:list>"
3941 (format "\n<text:list text:style-name=\"%s\" %s>"
3942 (case (org-element-property :type el)
3943 (ordered "OrgNumberedList")
3944 (unordered "OrgBulletedList")
3945 (descriptive-1 "OrgDescriptionList")
3946 (descriptive-2 "OrgDescriptionList"))
3947 "text:continue-numbering=\"true\"")))
3948 (item
3949 (cond
3950 ((not parent-list)
3951 (if (funcall --element-preceded-by-table-p table info)
3952 '("</text:list-header>" . "<text:list-header>")
3953 '("</text:list-item>" . "<text:list-header>")))
3954 ((funcall --element-preceded-by-table-p
3955 parent-list info)
3956 '("</text:list-header>" . "<text:list-header>"))
3957 (t '("</text:list-item>" . "<text:list-item>"))))))
3958 ;; Handle low-level headlines.
3959 (loop for el in llh-genealogy
3960 with step = 'item collect
3961 (case step
3962 (plain-list
3963 (setq step 'item) ; Flip-flop
3964 (setq parent-list el)
3965 (cons "</text:list>"
3966 (format "\n<text:list text:style-name=\"%s\" %s>"
3967 (if (org-export-numbered-headline-p
3968 el info)
3969 "OrgNumberedList"
3970 "OrgBulletedList")
3971 "text:continue-numbering=\"true\"")))
3972 (item
3973 (setq step 'plain-list) ; Flip-flop
3974 (cond
3975 ((not parent-list)
3976 (if (funcall --element-preceded-by-table-p table info)
3977 '("</text:list-header>" . "<text:list-header>")
3978 '("</text:list-item>" . "<text:list-header>")))
3979 ((let ((section? (org-export-get-previous-element
3980 parent-list info)))
3981 (and section?
3982 (eq (org-element-type section?) 'section)
3983 (assq 'table (org-element-contents section?))))
3984 '("</text:list-header>" . "<text:list-header>"))
3986 '("</text:list-item>" . "<text:list-item>")))))))))))
3987 (close-open-tags (funcall --walk-list-genealogy-and-collect-tags
3988 table info)))
3989 ;; OpenDocument schema does not permit table to occur within a
3990 ;; list item.
3992 ;; One solution - the easiest and lightweight, in terms of
3993 ;; implementation - is to put the table in an indented text box
3994 ;; and make the text box part of the list-item. Unfortunately if
3995 ;; the table is big and spans multiple pages, the text box could
3996 ;; overflow. In this case, the following attribute will come
3997 ;; handy.
3999 ;; ,---- From OpenDocument-v1.1.pdf
4000 ;; | 15.27.28 Overflow behavior
4001 ;; |
4002 ;; | For text boxes contained within text document, the
4003 ;; | style:overflow-behavior property specifies the behavior of text
4004 ;; | boxes where the containing text does not fit into the text
4005 ;; | box.
4006 ;; |
4007 ;; | If the attribute's value is clip, the text that does not fit
4008 ;; | into the text box is not displayed.
4009 ;; |
4010 ;; | If the attribute value is auto-create-new-frame, a new frame
4011 ;; | will be created on the next page, with the same position and
4012 ;; | dimensions of the original frame.
4013 ;; |
4014 ;; | If the style:overflow-behavior property's value is
4015 ;; | auto-create-new-frame and the text box has a minimum width or
4016 ;; | height specified, then the text box will grow until the page
4017 ;; | bounds are reached before a new frame is created.
4018 ;; `----
4020 ;; Unfortunately, LibreOffice-3.4.6 doesn't honor
4021 ;; auto-create-new-frame property and always resorts to clipping
4022 ;; the text box. This results in table being truncated.
4024 ;; So we solve the problem the hard (and fun) way using list
4025 ;; continuations.
4027 ;; The problem only becomes more interesting if you take in to
4028 ;; account the following facts:
4030 ;; - Description lists are simulated as plain lists.
4031 ;; - Low-level headlines can be listified.
4032 ;; - In Org-mode, a table can occur not only as a regular list
4033 ;; item, but also within description lists and low-level
4034 ;; headlines.
4036 ;; See `org-odt--translate-description-lists' and
4037 ;; `org-odt-translate-low-level-headlines' for how this is
4038 ;; tackled.
4040 (concat "\n"
4041 ;; Discontinue the list.
4042 (mapconcat 'car close-open-tags "\n")
4043 ;; Put the table in an indented section.
4044 (let* ((table (org-odt--table table contents info))
4045 (level (/ (length (mapcar 'car close-open-tags)) 2))
4046 (style (format "OrgIndentedSection-Level-%d" level)))
4047 (when table (org-odt-format-section table style)))
4048 ;; Continue the list.
4049 (mapconcat 'cdr (nreverse close-open-tags) "\n"))))
4052 ;;;; Target
4054 (defun org-odt-target (target contents info)
4055 "Transcode a TARGET object from Org to ODT.
4056 CONTENTS is nil. INFO is a plist holding contextual
4057 information."
4058 (let ((value (org-element-property :value target)))
4059 (org-odt--target "" (org-export-solidify-link-text value))))
4062 ;;;; Timestamp
4064 (defun org-odt-timestamp (timestamp contents info)
4065 "Transcode a TIMESTAMP object from Org to ODT.
4066 CONTENTS is nil. INFO is a plist used as a communication
4067 channel."
4068 (let* ((raw-value (org-element-property :raw-value timestamp))
4069 (type (org-element-property :type timestamp)))
4070 (if (not (plist-get info :odt-use-date-fields))
4071 (let ((value (org-odt-plain-text
4072 (org-timestamp-translate timestamp) info)))
4073 (case (org-element-property :type timestamp)
4074 ((active active-range)
4075 (format "<text:span text:style-name=\"%s\">%s</text:span>"
4076 "OrgActiveTimestamp" value))
4077 ((inactive inactive-range)
4078 (format "<text:span text:style-name=\"%s\">%s</text:span>"
4079 "OrgInactiveTimestamp" value))
4080 (otherwise value)))
4081 (case type
4082 (active
4083 (format "<text:span text:style-name=\"%s\">%s</text:span>"
4084 "OrgActiveTimestamp"
4085 (format "&lt;%s&gt;" (org-odt--format-timestamp timestamp))))
4086 (inactive
4087 (format "<text:span text:style-name=\"%s\">%s</text:span>"
4088 "OrgInactiveTimestamp"
4089 (format "[%s]" (org-odt--format-timestamp timestamp))))
4090 (active-range
4091 (format "<text:span text:style-name=\"%s\">%s</text:span>"
4092 "OrgActiveTimestamp"
4093 (format "&lt;%s&gt;&#x2013;&lt;%s&gt;"
4094 (org-odt--format-timestamp timestamp)
4095 (org-odt--format-timestamp timestamp 'end))))
4096 (inactive-range
4097 (format "<text:span text:style-name=\"%s\">%s</text:span>"
4098 "OrgInactiveTimestamp"
4099 (format "[%s]&#x2013;[%s]"
4100 (org-odt--format-timestamp timestamp)
4101 (org-odt--format-timestamp timestamp 'end))))
4102 (otherwise
4103 (format "<text:span text:style-name=\"%s\">%s</text:span>"
4104 "OrgDiaryTimestamp"
4105 (org-odt-plain-text (org-timestamp-translate timestamp)
4106 info)))))))
4109 ;;;; Underline
4111 (defun org-odt-underline (underline contents info)
4112 "Transcode UNDERLINE from Org to ODT.
4113 CONTENTS is the text with underline markup. INFO is a plist
4114 holding contextual information."
4115 (format "<text:span text:style-name=\"%s\">%s</text:span>"
4116 "Underline" contents))
4119 ;;;; Verbatim
4121 (defun org-odt-verbatim (verbatim contents info)
4122 "Transcode a VERBATIM object from Org to ODT.
4123 CONTENTS is nil. INFO is a plist used as a communication
4124 channel."
4125 (format "<text:span text:style-name=\"%s\">%s</text:span>"
4126 "OrgCode" (org-odt--encode-plain-text
4127 (org-element-property :value verbatim))))
4130 ;;;; Verse Block
4132 (defun org-odt-verse-block (verse-block contents info)
4133 "Transcode a VERSE-BLOCK element from Org to ODT.
4134 CONTENTS is verse block contents. INFO is a plist holding
4135 contextual information."
4136 ;; Add line breaks to each line of verse.
4137 (setq contents (replace-regexp-in-string
4138 "\\(<text:line-break/>\\)?[ \t]*\n"
4139 "<text:line-break/>" contents))
4140 ;; Replace tabs and spaces.
4141 (setq contents (org-odt--encode-tabs-and-spaces contents))
4142 ;; Surround it in a verse environment.
4143 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
4144 "OrgVerse" contents))
4148 ;;; Filters
4150 (defun org-odt--collect-cite-keys (tree backend info)
4151 "Collect cite keys (in reverse order) in to INFO.
4153 Modify INFO plist by appending a `:citations-alist' property.
4154 The value of this property is a list where each element is of the
4155 form (CITE-KEY . CITATION), with CITATION
4156 being the first element that references CITE-KEY. The list is
4157 sorted in reverse order of appearance of CITE-KEYs in the
4158 exported file."
4159 (let ((citations-alist nil))
4160 (org-element-map tree 'citation
4161 (lambda (citation)
4162 (let* ((value (org-element-property :key citation)))
4163 (let ((cite-keys (split-string value ",")))
4164 (mapc (lambda (cite-key)
4165 (setq cite-key (org-trim cite-key))
4166 (unless (assoc cite-key citations-alist)
4167 (push (cons cite-key citation) citations-alist)))
4168 cite-keys))))
4169 info)
4170 ;; Modify INFO by side-effects.
4171 (nconc info (list :citations-alist citations-alist)))
4172 tree)
4174 ;;;; LaTeX fragments
4176 (defun org-odt--translate-latex-fragments (tree backend info)
4177 (let ((processing-type (plist-get info :with-latex))
4178 (count 0))
4179 ;; Normalize processing-type to one of dvipng, mathml or verbatim.
4180 ;; If the desired converter is not available, force verbatim
4181 ;; processing.
4182 (case processing-type
4183 ((t mathml)
4184 (if (and (fboundp 'org-format-latex-mathml-available-p)
4185 (org-format-latex-mathml-available-p))
4186 (setq processing-type 'mathml)
4187 (message "LaTeX to MathML converter not available.")
4188 (setq processing-type 'verbatim)))
4189 ((dvipng imagemagick)
4190 (unless (and (org-check-external-command "latex" "" t)
4191 (org-check-external-command
4192 (if (eq processing-type 'dvipng) "dvipng" "convert") "" t))
4193 (message "LaTeX to PNG converter not available.")
4194 (setq processing-type 'verbatim)))
4195 (otherwise
4196 (message "Unknown LaTeX option. Forcing verbatim.")
4197 (setq processing-type 'verbatim)))
4199 ;; Store normalized value for later use.
4200 (when (plist-get info :with-latex)
4201 (plist-put info :with-latex processing-type))
4202 (message "Formatting LaTeX using %s" processing-type)
4204 ;; Convert `latex-fragment's and `latex-environment's.
4205 (when (memq processing-type '(mathml dvipng imagemagick))
4206 (org-element-map tree '(latex-fragment latex-environment)
4207 (lambda (latex-*)
4208 (incf count)
4209 (let* ((latex-frag (org-element-property :value latex-*))
4210 (input-file (plist-get info :input-file))
4211 (cache-dir (file-name-directory input-file))
4212 (cache-subdir (concat
4213 (case processing-type
4214 ((dvipng imagemagick) "ltxpng/")
4215 (mathml "ltxmathml/"))
4216 (file-name-sans-extension
4217 (file-name-nondirectory input-file))))
4218 (display-msg
4219 (case processing-type
4220 ((dvipng imagemagick)
4221 (format "Creating LaTeX Image %d..." count))
4222 (mathml
4223 (format "Creating MathML snippet %d..." count))))
4224 ;; Get an Org-style link to PNG image or the MathML
4225 ;; file.
4226 (org-link
4227 (let ((link (with-temp-buffer
4228 (insert latex-frag)
4229 (org-format-latex cache-subdir cache-dir
4230 nil display-msg
4231 nil processing-type)
4232 (buffer-substring-no-properties
4233 (point-min) (point-max)))))
4234 (if (org-string-match-p "file:\\([^]]*\\)" link) link
4235 (prog1 nil (message "LaTeX Conversion failed."))))))
4236 (when org-link
4237 ;; Conversion succeeded. Parse above Org-style link to a
4238 ;; `link' object.
4239 (let* ((link (car (org-element-map (with-temp-buffer
4240 (org-mode)
4241 (insert org-link)
4242 (org-element-parse-buffer))
4243 'link 'identity))))
4244 ;; Orphan the link.
4245 (org-element-put-property link :parent nil)
4246 (let* (
4247 (replacement
4248 (case (org-element-type latex-*)
4249 ;; Case 1: LaTeX environment.
4250 ;; Mimic a "standalone image or formula" by
4251 ;; enclosing the `link' in a `paragraph'.
4252 ;; Copy over original attributes, captions to
4253 ;; the enclosing paragraph.
4254 (latex-environment
4255 (org-element-adopt-elements
4256 (list 'paragraph
4257 (list :style "OrgFormula"
4258 :name (org-element-property :name
4259 latex-*)
4260 :caption (org-element-property :caption
4261 latex-*)))
4262 link))
4263 ;; Case 2: LaTeX fragment.
4264 ;; No special action.
4265 (latex-fragment link))))
4266 ;; Restore the blanks after the initial element or object.
4267 (org-element-put-property
4268 replacement :post-blank
4269 (org-element-property :post-blank latex-*))
4270 ;; Note down the object that link replaces.
4271 (org-element-put-property replacement :replaces
4272 (list (org-element-type latex-*)
4273 (list :value latex-frag)))
4274 ;; Replace now.
4275 (org-element-set-element latex-* replacement))))))
4276 info)))
4277 tree)
4280 ;;;; Description lists
4282 ;; This translator is necessary to handle indented tables in a uniform
4283 ;; manner. See comment in `org-odt--table'.
4285 ;; Depending on user option `org-odt-description-list-style',
4286 ;; description lists can be typeset either as in HTML documents or as
4287 ;; in LaTeX documents.
4289 (defun org-odt--translate-description-lists/html (tree backend info)
4290 ;; OpenDocument has no notion of a description list. So simulate it
4291 ;; using plain lists. Description lists in the exported document
4292 ;; are typeset in the same manner as they are in a typical HTML
4293 ;; document. See `org-odt--translate-description-lists/latex' for
4294 ;; yet another way of translation.
4296 ;; Specifically, a description list like this:
4298 ;; ,----
4299 ;; | - term-1 :: definition-1
4300 ;; |
4301 ;; | paragraph-1
4302 ;; |
4303 ;; | - term-2 :: definition-2
4304 ;; |
4305 ;; | paragraph-2
4306 ;; `----
4308 ;; gets translated in to the following form:
4310 ;; ,----
4311 ;; | - term-1
4312 ;; |
4313 ;; | - definition-1
4314 ;; |
4315 ;; | paragraph-1
4316 ;; |
4317 ;; | - term-2
4318 ;; |
4319 ;; | - definition-2
4320 ;; |
4321 ;; | paragraph-2
4322 ;; `----
4324 ;; Further effect is achieved by fixing the OD styles as below:
4326 ;; 1. Set the :type property of the simulated lists to
4327 ;; `descriptive-1' and `descriptive-2'. Map these to list-styles
4328 ;; that has *no* bullets whatsoever.
4330 ;; 2. The paragraph containing the definition term is styled to be
4331 ;; in bold.
4333 (org-element-map tree 'plain-list
4334 (lambda (el)
4335 (when (equal (org-element-property :type el) 'descriptive)
4336 (org-element-set-element
4338 (apply 'org-element-adopt-elements
4339 (list 'plain-list (list :type 'descriptive-1))
4340 (mapcar
4341 (lambda (item)
4342 (org-element-adopt-elements
4343 (list 'item (list :checkbox (org-element-property
4344 :checkbox item)))
4345 (list 'paragraph nil
4346 (list 'bold (list :style "OrgDescriptionTerm")
4347 (or (org-element-property :tag item) "(no term)")))
4348 (org-element-adopt-elements
4349 (list 'plain-list (list :type 'descriptive-2))
4350 (apply 'org-element-adopt-elements
4351 (list 'item nil)
4352 (org-element-contents item)))))
4353 (org-element-contents el)))))
4354 nil)
4355 info)
4356 tree)
4358 (defun org-odt--translate-description-lists/latex (tree backend info)
4359 ;; OpenDocument has no notion of a description list. So simulate it
4360 ;; using plain lists. Description lists in the exported document
4361 ;; are typeset in the same manner as they are in a typical LaTeX
4362 ;; style document. See `org-odt--translate-description-lists/html'
4363 ;; for yet another way of translation.
4365 ;; Specifically, a description list like this:
4367 ;; ,----
4368 ;; | - term-1 :: definition-1
4369 ;; |
4370 ;; | paragraph-1
4371 ;; |
4372 ;; | - term-2 :: definition-2
4373 ;; |
4374 ;; | paragraph-2
4375 ;; `----
4377 ;; gets translated in to the following form:
4379 ;; ,----
4380 ;; | - *term-1* definition-1
4381 ;; |
4382 ;; | - paragraph-1
4383 ;; |
4384 ;; | - *term-2* definition-2
4385 ;; |
4386 ;; | - paragraph-2
4387 ;; `----
4389 ;; Further effect is achieved by fixing the OD styles as below:
4391 ;; 1. Set the :type property of the simulated lists to
4392 ;; `descriptive-1' and `descriptive-2'. Map these to list-styles
4393 ;; that has *no* bullets whatsoever.
4395 ;; 2. The paragraph containing the definition term is styled to be
4396 ;; use hanging indent.
4398 (org-element-map tree 'plain-list
4399 (lambda (el)
4400 (when (equal (org-element-property :type el) 'descriptive)
4401 (org-element-set-element
4403 (apply 'org-element-adopt-elements
4404 (list 'plain-list (list :type 'descriptive-1))
4405 (mapcar
4406 (lambda (item)
4407 (let* ((item-contents (org-element-contents item))
4408 (leading-paragraph (car item-contents))
4409 (item-contents (cdr item-contents)))
4410 (org-element-adopt-elements
4411 (list 'item (list :checkbox (org-element-property :checkbox item)))
4412 (apply 'org-element-adopt-elements
4413 (list 'paragraph (list :style "OrgDescriptionDefinition"))
4414 (list 'bold (list :style "OrgDescriptionTerm" :post-blank 1)
4415 (or (org-element-property :tag item) "(no term)"))
4416 (org-element-contents leading-paragraph))
4417 (org-element-adopt-elements
4418 (list 'plain-list (list :type 'descriptive-2))
4419 (apply 'org-element-adopt-elements
4420 (list 'item nil)
4421 item-contents)))))
4422 (org-element-contents el)))))
4423 nil)
4424 info)
4425 tree)
4427 ;;;; List tables
4429 ;; Lists that are marked with attribute `:list-table' are called as
4430 ;; list tables. They will be rendered as a table within the exported
4431 ;; document.
4433 ;; Consider an example. The following list table
4435 ;; #+ATTR_ODT: :rel-width 80
4436 ;; #+ATTR_ODT: :list-table t
4437 ;; - Row 1
4438 ;; - 1.1
4439 ;; - 1.2
4440 ;; - 1.3
4441 ;; - -----
4442 ;; - Row 2
4443 ;; - 2.1
4444 ;; - 2.2
4445 ;; - 2.3
4447 ;; will be exported as though it were an Org table like the one show
4448 ;; below.
4450 ;; | Row 1 | 1.1 | 1.2 | 1.3 |
4451 ;; |-------+-----+-----+-----|
4452 ;; | Row 2 | 2.1 | 2.2 | 2.3 |
4454 ;; List tables can contain hrule (see example above). They can also
4455 ;; contain table specific attributes.
4457 ;; Note that org-tables are NOT multi-line and each line is mapped to
4458 ;; a unique row in the exported document. So if an exported table
4459 ;; needs to contain a single paragraph (with copious text) it needs to
4460 ;; be typed up in a single line. Editing such long lines using the
4461 ;; table editor will be a cumbersome task. Furthermore inclusion of
4462 ;; multi-paragraph text in a table cell is well-nigh impossible.
4464 ;; A LIST-TABLE circumvents above problems.
4466 ;; Note that in the example above the list items could be paragraphs
4467 ;; themselves and the list can be arbitrarily deep.
4469 ;; Inspired by following thread:
4470 ;; https://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01101.html
4472 ;; Translate lists to tables
4474 (defun org-odt--translate-list-tables (tree backend info)
4475 (org-element-map tree 'plain-list
4476 (lambda (l1-list)
4477 (when (org-odt--read-attribute l1-list :list-table)
4478 ;; Replace list with table.
4479 (org-element-set-element
4480 l1-list
4481 ;; Build replacement table.
4482 (apply 'org-element-adopt-elements
4483 (list 'table (list :type 'org :attr_odt
4484 (org-element-property :attr_odt l1-list)))
4485 (org-element-map l1-list 'item
4486 (lambda (l1-item)
4487 (let* ((l1-item-contents (org-element-contents l1-item))
4488 l1-item-leading-text l2-list)
4489 ;; Remove Level-2 list from the Level-item. It
4490 ;; will be subsequently attached as table-cells.
4491 (let ((cur l1-item-contents) prev)
4492 (while (and cur (not (eq (org-element-type (car cur))
4493 'plain-list)))
4494 (setq prev cur)
4495 (setq cur (cdr cur)))
4496 (when prev
4497 (setcdr prev nil)
4498 (setq l2-list (car cur)))
4499 (setq l1-item-leading-text l1-item-contents))
4500 (cond
4501 ;; Check for hrule.
4502 ((and
4503 (null l2-list)
4504 (not (cdr l1-item-leading-text))
4505 (eq (org-element-type (car l1-item-leading-text))
4506 'paragraph)
4507 (string-match "\\`[[:space:]]*-\\{5,\\}[[:space:]]*\\'"
4508 (org-element-interpret-data
4509 (car l1-item-leading-text))))
4510 ;; Level-1 items start a table row.
4511 (org-element-adopt-elements
4512 (list 'table-row (list :type 'rule))))
4513 ;; No hrule.
4515 ;; Level-1 items start a table row.
4516 (apply 'org-element-adopt-elements
4517 (list 'table-row (list :type 'standard))
4518 ;; Leading text of level-1 item define
4519 ;; the first table-cell.
4520 (apply 'org-element-adopt-elements
4521 (list 'table-cell nil)
4522 l1-item-leading-text)
4523 ;; Level-2 items define subsequent
4524 ;; table-cells of the row.
4525 (org-element-map l2-list 'item
4526 (lambda (l2-item)
4527 (apply 'org-element-adopt-elements
4528 (list 'table-cell nil)
4529 (org-element-contents l2-item)))
4530 info nil 'item))))))
4531 info nil 'item))))
4532 nil)
4533 info)
4534 tree)
4538 ;;; Interactive functions
4540 (defun org-odt-create-manifest-file-entry (&rest args)
4541 (push args org-odt-manifest-file-entries))
4543 (defun org-odt-write-manifest-file ()
4544 (make-directory (concat org-odt-zip-dir "META-INF"))
4545 (let ((manifest-file (concat org-odt-zip-dir "META-INF/manifest.xml")))
4546 (with-current-buffer
4547 (let ((nxml-auto-insert-xml-declaration-flag nil))
4548 (find-file-noselect manifest-file t))
4549 (insert
4550 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
4551 <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
4552 (mapc
4553 (lambda (file-entry)
4554 (let* ((version (nth 2 file-entry))
4555 (extra (if (not version) ""
4556 (format " manifest:version=\"%s\"" version))))
4557 (insert
4558 (format org-odt-manifest-file-entry-tag
4559 (nth 0 file-entry) (nth 1 file-entry) extra))))
4560 org-odt-manifest-file-entries)
4561 (insert "\n</manifest:manifest>"))))
4563 (defmacro org-odt--export-wrap (out-file &rest body)
4564 `(let* ((--out-file ,out-file)
4565 (out-file-type (file-name-extension --out-file))
4566 ;; XML files created by the exporter.
4567 (org-odt-xml-files '("META-INF/manifest.xml" "content.xml"
4568 "meta.xml" "styles.xml"))
4569 ;; Encode all the above XML files using utf-8.
4570 (coding-system-for-write 'utf-8)
4571 (save-buffer-coding-system 'utf-8)
4572 ;; Initialize temporary workarea. All files that end up in
4573 ;; the exported document get parked/created here.
4574 (org-odt-zip-dir (file-name-as-directory
4575 (make-temp-file (format "%s-" out-file-type) t)))
4576 (org-odt-manifest-file-entries nil)
4577 (--cleanup-xml-buffers
4578 (function
4579 (lambda nil
4580 ;; Kill all XML buffers.
4581 (mapc (lambda (file)
4582 (let ((buf (find-buffer-visiting
4583 (concat org-odt-zip-dir file))))
4584 (when buf
4585 (with-current-buffer buf
4586 (set-buffer-modified-p nil)
4587 (kill-buffer buf)))))
4588 org-odt-xml-files)
4589 ;; Delete temporary directory and also other embedded
4590 ;; files that get copied there.
4591 (delete-directory org-odt-zip-dir t)))))
4592 (condition-case err
4593 (progn
4594 (unless (executable-find "zip")
4595 ;; Not at all OSes ship with zip by default
4596 (error "Executable \"zip\" needed for creating OpenDocument files"))
4597 ;; Do export. This creates a bunch of xml files ready to be
4598 ;; saved and zipped.
4599 (progn ,@body)
4600 ;; Create a manifest entry for content.xml.
4601 (org-odt-create-manifest-file-entry "text/xml" "content.xml")
4602 ;; Write mimetype file
4603 (let* ((mimetypes
4604 '(("odt" . "application/vnd.oasis.opendocument.text")
4605 ("odf" . "application/vnd.oasis.opendocument.formula")))
4606 (mimetype (cdr (assoc-string out-file-type mimetypes t))))
4607 (unless mimetype
4608 (error "Unknown OpenDocument backend %S" out-file-type))
4609 (write-region mimetype nil (concat org-odt-zip-dir "mimetype"))
4610 (org-odt-create-manifest-file-entry mimetype "/" "1.2"))
4611 ;; Write out the manifest entries before zipping
4612 (org-odt-write-manifest-file)
4613 ;; Save all XML files.
4614 (mapc (lambda (file)
4615 (let ((buf (find-buffer-visiting
4616 (concat org-odt-zip-dir file))))
4617 (when buf
4618 (with-current-buffer buf
4619 ;; Prettify output if needed.
4620 (when org-odt-prettify-xml
4621 (indent-region (point-min) (point-max)))
4622 (save-buffer 0)))))
4623 org-odt-xml-files)
4624 ;; Run zip.
4625 (let* ((target --out-file)
4626 (target-name (file-name-nondirectory target))
4627 (cmds `(("zip" "-mX0" ,target-name "mimetype")
4628 ("zip" "-rmTq" ,target-name "."))))
4629 ;; If a file with same name as the desired output file
4630 ;; exists, remove it.
4631 (when (file-exists-p target)
4632 (delete-file target))
4633 ;; Zip up the xml files.
4634 (let ((coding-system-for-write 'no-conversion) exitcode err-string)
4635 (message "Creating ODT file...")
4636 ;; Switch temporarily to content.xml. This way Zip
4637 ;; process will inherit `org-odt-zip-dir' as the current
4638 ;; directory.
4639 (with-current-buffer
4640 (find-file-noselect (concat org-odt-zip-dir "content.xml") t)
4641 (mapc
4642 (lambda (cmd)
4643 (message "Running %s" (mapconcat 'identity cmd " "))
4644 (setq err-string
4645 (with-output-to-string
4646 (setq exitcode
4647 (apply 'call-process (car cmd)
4648 nil standard-output nil (cdr cmd)))))
4649 (or (zerop exitcode)
4650 (error (concat "Unable to create OpenDocument file."
4651 (format " Zip failed with error (%s)"
4652 err-string)))))
4653 cmds)))
4654 ;; Move the zip file from temporary work directory to
4655 ;; user-mandated location.
4656 (rename-file (concat org-odt-zip-dir target-name) target)
4657 (message "Created %s" (expand-file-name target))
4658 ;; Cleanup work directory and work files.
4659 (funcall --cleanup-xml-buffers)
4660 ;; Open the OpenDocument file in archive-mode for
4661 ;; examination.
4662 (find-file-noselect target t)
4663 ;; Return exported file.
4664 (cond
4665 ;; Case 1: Conversion desired on exported file. Run the
4666 ;; converter on the OpenDocument file. Return the
4667 ;; converted file.
4668 (org-odt-preferred-output-format
4669 (or (org-odt-convert target org-odt-preferred-output-format)
4670 target))
4671 ;; Case 2: No further conversion. Return exported
4672 ;; OpenDocument file.
4673 (t target))))
4674 ((debug error)
4675 ;; Cleanup work directory and work files.
4676 (funcall --cleanup-xml-buffers)
4677 (message "OpenDocument export failed: %s"
4678 (error-message-string err))))))
4681 ;;;; Export to OpenDocument formula
4683 ;;;###autoload
4684 (defun org-odt-export-as-odf (latex-frag &optional odf-file)
4685 "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
4686 Use `org-create-math-formula' to convert LATEX-FRAG first to
4687 MathML. When invoked as an interactive command, use
4688 `org-latex-regexps' to infer LATEX-FRAG from currently active
4689 region. If no LaTeX fragments are found, prompt for it. Push
4690 MathML source to kill ring depending on the value of
4691 `org-export-copy-to-kill-ring'."
4692 (interactive
4693 `(,(let (frag)
4694 (setq frag (and (setq frag (and (region-active-p)
4695 (buffer-substring (region-beginning)
4696 (region-end))))
4697 (loop for e in org-latex-regexps
4698 thereis (when (string-match (nth 1 e) frag)
4699 (match-string (nth 2 e) frag)))))
4700 (read-string "LaTeX Fragment: " frag nil frag))
4701 ,(let ((odf-filename (expand-file-name
4702 (concat
4703 (file-name-sans-extension
4704 (or (file-name-nondirectory buffer-file-name)))
4705 "." "odf")
4706 (file-name-directory buffer-file-name))))
4707 (read-file-name "ODF filename: " nil odf-filename nil
4708 (file-name-nondirectory odf-filename)))))
4709 (let ((filename (or odf-file
4710 (expand-file-name
4711 (concat
4712 (file-name-sans-extension
4713 (or (file-name-nondirectory buffer-file-name)))
4714 "." "odf")
4715 (file-name-directory buffer-file-name)))))
4716 (org-odt--export-wrap
4717 filename
4718 (let* ((buffer (progn
4719 (require 'nxml-mode)
4720 (let ((nxml-auto-insert-xml-declaration-flag nil))
4721 (find-file-noselect (concat org-odt-zip-dir
4722 "content.xml") t)))))
4723 (set-buffer buffer)
4724 (set-buffer-file-coding-system coding-system-for-write)
4725 (let ((mathml (org-create-math-formula latex-frag)))
4726 (unless mathml (error "No Math formula created"))
4727 (insert mathml)
4728 ;; Add MathML to kill ring, if needed.
4729 (when (org-export--copy-to-kill-ring-p)
4730 (org-kill-new (buffer-string))))))))
4732 ;;;###autoload
4733 (defun org-odt-export-as-odf-and-open ()
4734 "Export LaTeX fragment as OpenDocument formula and immediately open it.
4735 Use `org-odt-export-as-odf' to read LaTeX fragment and OpenDocument
4736 formula file."
4737 (interactive)
4738 (org-open-file (call-interactively 'org-odt-export-as-odf) 'system))
4741 ;;;; Export to OpenDocument Text
4743 ;;;###autoload
4744 (defun org-odt-export-to-odt (&optional async subtreep visible-only ext-plist)
4745 "Export current buffer to a ODT file.
4747 If narrowing is active in the current buffer, only export its
4748 narrowed part.
4750 If a region is active, export that region.
4752 A non-nil optional argument ASYNC means the process should happen
4753 asynchronously. The resulting file should be accessible through
4754 the `org-export-stack' interface.
4756 When optional argument SUBTREEP is non-nil, export the sub-tree
4757 at point, extracting information from the headline properties
4758 first.
4760 When optional argument VISIBLE-ONLY is non-nil, don't export
4761 contents of hidden elements.
4763 EXT-PLIST, when provided, is a property list with external
4764 parameters overriding Org default settings, but still inferior to
4765 file-local settings.
4767 Return output file's name."
4768 (interactive)
4769 (let ((outfile (org-export-output-file-name ".odt" subtreep)))
4770 (if async
4771 (org-export-async-start (lambda (f) (org-export-add-to-stack f 'odt))
4772 `(expand-file-name
4773 (org-odt--export-wrap
4774 ,outfile
4775 (let* ((org-odt-embedded-images-count 0)
4776 (org-odt-embedded-formulas-count 0)
4777 (org-odt-automatic-styles nil)
4778 (org-odt-object-counters nil)
4779 ;; Let `htmlfontify' know that we are interested in
4780 ;; collecting styles.
4781 (hfy-user-sheet-assoc nil))
4782 ;; Initialize content.xml and kick-off the export
4783 ;; process.
4784 (let ((out-buf
4785 (progn
4786 (require 'nxml-mode)
4787 (let ((nxml-auto-insert-xml-declaration-flag nil))
4788 (find-file-noselect
4789 (concat org-odt-zip-dir "content.xml") t))))
4790 (output (org-export-as
4791 'odt ,subtreep ,visible-only nil ,ext-plist)))
4792 (with-current-buffer out-buf
4793 (erase-buffer)
4794 (insert output)))))))
4795 (org-odt--export-wrap
4796 outfile
4797 (let* ((org-odt-embedded-images-count 0)
4798 (org-odt-embedded-formulas-count 0)
4799 (org-odt-automatic-styles nil)
4800 (org-odt-object-counters nil)
4801 ;; Let `htmlfontify' know that we are interested in collecting
4802 ;; styles.
4803 (hfy-user-sheet-assoc nil))
4804 ;; Initialize content.xml and kick-off the export process.
4805 (let ((output (org-export-as 'odt subtreep visible-only nil ext-plist))
4806 (out-buf (progn
4807 (require 'nxml-mode)
4808 (let ((nxml-auto-insert-xml-declaration-flag nil))
4809 (find-file-noselect
4810 (concat org-odt-zip-dir "content.xml") t)))))
4811 (with-current-buffer out-buf (erase-buffer) (insert output))))))))
4814 ;;;; Convert between OpenDocument and other formats
4816 (defun org-odt-reachable-p (in-fmt out-fmt)
4817 "Return non-nil if IN-FMT can be converted to OUT-FMT."
4818 (catch 'done
4819 (let ((reachable-formats (org-odt-do-reachable-formats in-fmt)))
4820 (dolist (e reachable-formats)
4821 (let ((out-fmt-spec (assoc out-fmt (cdr e))))
4822 (when out-fmt-spec
4823 (throw 'done (cons (car e) out-fmt-spec))))))))
4825 (defun org-odt-do-convert (in-file out-fmt &optional prefix-arg)
4826 "Workhorse routine for `org-odt-convert'."
4827 (require 'browse-url)
4828 (let* ((in-file (expand-file-name (or in-file buffer-file-name)))
4829 (dummy (or (file-readable-p in-file)
4830 (error "Cannot read %s" in-file)))
4831 (in-fmt (file-name-extension in-file))
4832 (out-fmt (or out-fmt (error "Output format unspecified")))
4833 (how (or (org-odt-reachable-p in-fmt out-fmt)
4834 (error "Cannot convert from %s format to %s format?"
4835 in-fmt out-fmt)))
4836 (convert-process (car how))
4837 (out-file (concat (file-name-sans-extension in-file) "."
4838 (nth 1 (or (cdr how) out-fmt))))
4839 (extra-options (or (nth 2 (cdr how)) ""))
4840 (out-dir (file-name-directory in-file))
4841 (cmd (format-spec convert-process
4842 `((?i . ,(shell-quote-argument in-file))
4843 (?I . ,(browse-url-file-url in-file))
4844 (?f . ,out-fmt)
4845 (?o . ,out-file)
4846 (?O . ,(browse-url-file-url out-file))
4847 (?d . , (shell-quote-argument out-dir))
4848 (?D . ,(browse-url-file-url out-dir))
4849 (?x . ,extra-options)))))
4850 (when (file-exists-p out-file)
4851 (delete-file out-file))
4853 (message "Executing %s" cmd)
4854 (let ((cmd-output (shell-command-to-string cmd)))
4855 (message "%s" cmd-output))
4857 (cond
4858 ((file-exists-p out-file)
4859 (message "Exported to %s" out-file)
4860 (when prefix-arg
4861 (message "Opening %s..." out-file)
4862 (org-open-file out-file 'system))
4863 out-file)
4865 (message "Export to %s failed" out-file)
4866 nil))))
4868 (defun org-odt-do-reachable-formats (in-fmt)
4869 "Return verbose info about formats to which IN-FMT can be converted.
4870 Return a list where each element is of the
4871 form (CONVERTER-PROCESS . OUTPUT-FMT-ALIST). See
4872 `org-odt-convert-processes' for CONVERTER-PROCESS and see
4873 `org-odt-convert-capabilities' for OUTPUT-FMT-ALIST."
4874 (let* ((converter
4875 (and org-odt-convert-process
4876 (cadr (assoc-string org-odt-convert-process
4877 org-odt-convert-processes t))))
4878 (capabilities
4879 (and org-odt-convert-process
4880 (cadr (assoc-string org-odt-convert-process
4881 org-odt-convert-processes t))
4882 org-odt-convert-capabilities))
4883 reachable-formats)
4884 (when converter
4885 (dolist (c capabilities)
4886 (when (member in-fmt (nth 1 c))
4887 (push (cons converter (nth 2 c)) reachable-formats))))
4888 reachable-formats))
4890 (defun org-odt-reachable-formats (in-fmt)
4891 "Return list of formats to which IN-FMT can be converted.
4892 The list of the form (OUTPUT-FMT-1 OUTPUT-FMT-2 ...)."
4893 (let (l)
4894 (mapc (lambda (e) (add-to-list 'l e))
4895 (apply 'append (mapcar
4896 (lambda (e) (mapcar 'car (cdr e)))
4897 (org-odt-do-reachable-formats in-fmt))))
4900 (defun org-odt-convert-read-params ()
4901 "Return IN-FILE and OUT-FMT params for `org-odt-do-convert'.
4902 This is a helper routine for interactive use."
4903 (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
4904 (in-file (read-file-name "File to be converted: "
4905 nil buffer-file-name t))
4906 (in-fmt (file-name-extension in-file))
4907 (out-fmt-choices (org-odt-reachable-formats in-fmt))
4908 (out-fmt
4909 (or (and out-fmt-choices
4910 (funcall input "Output format: "
4911 out-fmt-choices nil nil nil))
4912 (error
4913 "No known converter or no known output formats for %s files"
4914 in-fmt))))
4915 (list in-file out-fmt)))
4917 ;;;###autoload
4918 (defun org-odt-convert (&optional in-file out-fmt prefix-arg)
4919 "Convert IN-FILE to format OUT-FMT using a command line converter.
4920 IN-FILE is the file to be converted. If unspecified, it defaults
4921 to variable `buffer-file-name'. OUT-FMT is the desired output
4922 format. Use `org-odt-convert-process' as the converter.
4923 If PREFIX-ARG is non-nil then the newly converted file is opened
4924 using `org-open-file'."
4925 (interactive
4926 (append (org-odt-convert-read-params) current-prefix-arg))
4927 (org-odt-do-convert in-file out-fmt prefix-arg))
4929 ;;; Library Initializations
4931 (mapc
4932 (lambda (desc)
4933 ;; Let Emacs open all OpenDocument files in archive mode
4934 (add-to-list 'auto-mode-alist
4935 (cons (concat "\\." (car desc) "\\'") 'archive-mode)))
4936 org-odt-file-extensions)
4938 (provide 'ox-odt)
4940 ;;; ox-odt.el ends here