1 ;;; ox-odt.el --- OpenDocument Text Exporter for Org Mode -*- lexical-binding: t; -*-
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/>.
30 (require 'table nil
'noerror
))
31 (require 'format-spec
)
37 (org-export-define-backend 'odt
38 '((bold . org-odt-bold
)
39 (center-block . org-odt-center-block
)
40 (clock . org-odt-clock
)
42 (drawer . org-odt-drawer
)
43 (dynamic-block . org-odt-dynamic-block
)
44 (entity . org-odt-entity
)
45 (example-block . org-odt-example-block
)
46 (export-block . org-odt-export-block
)
47 (export-snippet . org-odt-export-snippet
)
48 (fixed-width . org-odt-fixed-width
)
49 (footnote-definition . org-odt-footnote-definition
)
50 (footnote-reference . org-odt-footnote-reference
)
51 (headline . org-odt-headline
)
52 (horizontal-rule . org-odt-horizontal-rule
)
53 (inline-src-block . org-odt-inline-src-block
)
54 (inlinetask . org-odt-inlinetask
)
55 (italic . org-odt-italic
)
57 (keyword . org-odt-keyword
)
58 (latex-environment . org-odt-latex-environment
)
59 (latex-fragment . org-odt-latex-fragment
)
60 (line-break . org-odt-line-break
)
62 (node-property . org-odt-node-property
)
63 (paragraph . org-odt-paragraph
)
64 (plain-list . org-odt-plain-list
)
65 (plain-text . org-odt-plain-text
)
66 (planning . org-odt-planning
)
67 (property-drawer . org-odt-property-drawer
)
68 (quote-block . org-odt-quote-block
)
69 (radio-target . org-odt-radio-target
)
70 (section . org-odt-section
)
71 (special-block . org-odt-special-block
)
72 (src-block . org-odt-src-block
)
73 (statistics-cookie . org-odt-statistics-cookie
)
74 (strike-through . org-odt-strike-through
)
75 (subscript . org-odt-subscript
)
76 (superscript . org-odt-superscript
)
77 (table . org-odt-table
)
78 (table-cell . org-odt-table-cell
)
79 (table-row . org-odt-table-row
)
80 (target . org-odt-target
)
81 (template . org-odt-template
)
82 (timestamp . org-odt-timestamp
)
83 (underline . org-odt-underline
)
84 (verbatim . org-odt-verbatim
)
85 (verse-block . org-odt-verse-block
))
87 :filters-alist
'((:filter-parse-tree
88 .
(org-odt--translate-latex-fragments
89 org-odt--translate-description-lists
90 org-odt--translate-list-tables
)))
93 ((?o
"As ODT file" org-odt-export-to-odt
)
94 (?O
"As ODT file and open"
96 (if a
(org-odt-export-to-odt t s v
)
97 (org-open-file (org-odt-export-to-odt nil s v
) 'system
))))))
99 '((:odt-styles-file
"ODT_STYLES_FILE" nil nil t
)
100 (:description
"DESCRIPTION" nil nil newline
)
101 (:keywords
"KEYWORDS" nil nil space
)
102 (:subtitle
"SUBTITLE" nil nil parse
)
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
)))
124 ;;; Function and Dynamically Scoped Variables 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))
132 (defvar nxml-auto-insert-xml-declaration-flag
) ; nxml-mode.el
133 (defvar archive-zip-extract
) ; arc-mode.el
134 (defvar hfy-end-span-handler
) ; htmlfontify.el
135 (defvar hfy-begin-span-handler
) ; htmlfontify.el
136 (defvar hfy-face-to-css
) ; htmlfontify.el
137 (defvar hfy-html-quote-map
) ; htmlfontify.el
138 (defvar hfy-html-quote-regex
) ; htmlfontify.el
141 ;;; Internal Variables
143 (defconst org-odt-lib-dir
144 (file-name-directory (or load-file-name
(buffer-file-name)))
145 "Location of ODT exporter.
146 Use this to infer values of `org-odt-styles-dir' and
147 `org-odt-schema-dir'.")
149 (defvar org-odt-data-dir
150 (expand-file-name "../../etc/" org-odt-lib-dir
)
151 "Data directory for ODT exporter.
152 Use this to infer values of `org-odt-styles-dir' and
153 `org-odt-schema-dir'.")
155 (defconst org-odt-special-string-regexps
156 '(("\\\\-" .
"­\\1") ; shy
157 ("---\\([^-]\\)" .
"—\\1") ; mdash
158 ("--\\([^-]\\)" .
"–\\1") ; ndash
159 ("\\.\\.\\." .
"…")) ; hellip
160 "Regular expressions for special string conversion.")
162 (defconst org-odt-schema-dir-list
164 (and org-odt-data-dir
165 (expand-file-name "./schema/" org-odt-data-dir
)) ; bail out
167 (and (boundp 'org-odt-data-dir
) org-odt-data-dir
; see make install
168 (expand-file-name "./schema/" org-odt-data-dir
))))
169 "List of directories to search for OpenDocument schema files.
170 Use this list to set the default value of
171 `org-odt-schema-dir'. The entries in this list are
172 populated heuristically based on the values of `org-odt-lib-dir'
173 and `org-odt-data-dir'.")
175 (defconst org-odt-styles-dir-list
177 (and org-odt-data-dir
178 (expand-file-name "./styles/" org-odt-data-dir
)) ; bail out
180 (and (boundp 'org-odt-data-dir
) org-odt-data-dir
; see make install
181 (expand-file-name "./styles/" org-odt-data-dir
)))
182 (expand-file-name "../../etc/styles/" org-odt-lib-dir
) ; git
183 (expand-file-name "./etc/styles/" org-odt-lib-dir
) ; elpa
184 (expand-file-name "./org/" data-directory
) ; system
186 "List of directories to search for OpenDocument styles files.
187 See `org-odt-styles-dir'. The entries in this list are populated
188 heuristically based on the values of `org-odt-lib-dir' and
189 `org-odt-data-dir'.")
191 (defconst org-odt-styles-dir
194 (message "Debug (ox-odt): Searching for OpenDocument styles files...")
195 (dolist (styles-dir org-odt-styles-dir-list
)
197 (message "Debug (ox-odt): Trying %s..." styles-dir
)
198 (when (and (file-readable-p
200 "OrgOdtContentTemplate.xml" styles-dir
))
203 "OrgOdtStyles.xml" styles-dir
)))
204 (message "Debug (ox-odt): Using styles under %s"
206 (throw 'styles-dir styles-dir
)))))))
208 (error "Error (ox-odt): Cannot find factory styles files, aborting"))
210 "Directory that holds auxiliary XML files used by the ODT exporter.
212 This directory contains the following XML files -
213 \"OrgOdtStyles.xml\" and \"OrgOdtContentTemplate.xml\". These
214 XML files are used as the default values of
215 `org-odt-styles-file' and `org-odt-content-template-file'.
217 The default value of this variable varies depending on the
218 version of org in use and is initialized from
219 `org-odt-styles-dir-list'. Note that the user could be using org
220 from one of: org's own private git repository, GNU ELPA tar or
223 (defconst org-odt-bookmark-prefix
"OrgXref.")
225 (defconst org-odt-manifest-file-entry-tag
226 "\n<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
228 (defconst org-odt-file-extensions
229 '(("odt" .
"OpenDocument Text")
230 ("ott" .
"OpenDocument Text Template")
231 ("odm" .
"OpenDocument Master Document")
232 ("ods" .
"OpenDocument Spreadsheet")
233 ("ots" .
"OpenDocument Spreadsheet Template")
234 ("odg" .
"OpenDocument Drawing (Graphics)")
235 ("otg" .
"OpenDocument Drawing Template")
236 ("odp" .
"OpenDocument Presentation")
237 ("otp" .
"OpenDocument Presentation Template")
238 ("odi" .
"OpenDocument Image")
239 ("odf" .
"OpenDocument Formula")
240 ("odc" .
"OpenDocument Chart")))
242 (defconst org-odt-table-style-format
244 <style:style style:name=\"%s\" style:family=\"table\">
245 <style:table-properties style:rel-width=\"%s%%\" fo:margin-top=\"0cm\" fo:margin-bottom=\"0.20cm\" table:align=\"center\"/>
248 "Template for auto-generated Table styles.")
250 (defvar org-odt-automatic-styles
'()
251 "Registry of automatic styles for various OBJECT-TYPEs.
252 The variable has the following form:
254 ((OBJECT-NAME-A.1 OBJECT-PROPS-A.1)
255 (OBJECT-NAME-A.2 OBJECT-PROPS-A.2) ...))
257 ((OBJECT-NAME-B.1 OBJECT-PROPS-B.1)
258 (OBJECT-NAME-B.2 OBJECT-PROPS-B.2) ...))
261 OBJECT-TYPEs could be \"Section\", \"Table\", \"Figure\" etc.
262 OBJECT-PROPS is (typically) a plist created by passing
263 \"#+ATTR_ODT: \" option to `org-odt-parse-block-attributes'.
265 Use `org-odt-add-automatic-style' to add update this variable.'")
267 (defvar org-odt-object-counters nil
268 "Running counters for various OBJECT-TYPEs.
269 Use this to generate automatic names and style-names. See
270 `org-odt-add-automatic-style'.")
272 (defvar org-odt-src-block-paragraph-format
273 "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
274 <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
275 <style:background-image/>
276 </style:paragraph-properties>
277 <style:text-properties fo:color=\"%s\"/>
279 "Custom paragraph style for colorized source and example blocks.
280 This style is much the same as that of \"OrgFixedWidthBlock\"
281 except that the foreground and background colors are set
282 according to the default face identified by the `htmlfontify'.")
284 (defvar hfy-optimizations
)
285 (define-obsolete-variable-alias 'hfy-optimisations
'hfy-optimizations
"25.1")
286 (defvar org-odt-embedded-formulas-count
0)
287 (defvar org-odt-embedded-images-count
0)
288 (defvar org-odt-image-size-probe-method
289 (append (and (executable-find "identify") '(imagemagick)) ; See Bug#10675
291 "Ordered list of methods for determining image sizes.")
293 (defvar org-odt-default-image-sizes-alist
294 '(("as-char" .
(5 .
0.4))
295 ("paragraph" .
(5 .
5)))
296 "Hardcoded image dimensions one for each of the anchor
299 ;; A4 page size is 21.0 by 29.7 cms
300 ;; The default page settings has 2cm margin on each of the sides. So
301 ;; the effective text area is 17.0 by 25.7 cm
302 (defvar org-odt-max-image-size
'(17.0 .
20.0)
303 "Limiting dimensions for an embedded image.")
305 (defconst org-odt-label-styles
306 '(("math-formula" "%c" "text" "(%n)")
307 ("math-label" "(%n)" "text" "(%n)")
308 ("category-and-value" "%e %n: %c" "category-and-value" "%e %n")
309 ("value" "%e %n: %c" "value" "%n"))
310 "Specify how labels are applied and referenced.
312 This is an alist where each element is of the form:
314 (STYLE-NAME ATTACH-FMT REF-MODE REF-FMT)
316 ATTACH-FMT controls how labels and captions are attached to an
317 entity. It may contain following specifiers - %e and %c. %e is
318 replaced with the CATEGORY-NAME. %n is replaced with
319 \"<text:sequence ...> SEQNO </text:sequence>\". %c is replaced
322 REF-MODE and REF-FMT controls how label references are generated.
323 The following XML is generated for a label reference -
324 \"<text:sequence-ref text:reference-format=\"REF-MODE\" ...>
325 REF-FMT </text:sequence-ref>\". REF-FMT may contain following
326 specifiers - %e and %n. %e is replaced with the CATEGORY-NAME.
327 %n is replaced with SEQNO.
329 See also `org-odt-format-label'.")
331 (defvar org-odt-category-map-alist
332 '(("__Table__" "Table" "value" "Table" org-odt--enumerable-p
)
333 ("__Figure__" "Illustration" "value" "Figure" org-odt--enumerable-image-p
)
334 ("__MathFormula__" "Text" "math-formula" "Equation" org-odt--enumerable-formula-p
)
335 ("__DvipngImage__" "Equation" "value" "Equation" org-odt--enumerable-latex-image-p
)
336 ("__Listing__" "Listing" "value" "Listing" org-odt--enumerable-p
))
337 "Map a CATEGORY-HANDLE to OD-VARIABLE and LABEL-STYLE.
339 This is a list where each entry is of the form:
341 (CATEGORY-HANDLE OD-VARIABLE LABEL-STYLE CATEGORY-NAME ENUMERATOR-PREDICATE)
343 CATEGORY_HANDLE identifies the captionable entity in question.
345 OD-VARIABLE is the OpenDocument sequence counter associated with
346 the entity. These counters are declared within
347 \"<text:sequence-decls>...</text:sequence-decls>\" block of
348 `org-odt-content-template-file'.
350 LABEL-STYLE is a key into `org-odt-label-styles' and specifies
351 how a given entity should be captioned and referenced.
353 CATEGORY-NAME is used for qualifying captions on export.
355 ENUMERATOR-PREDICATE is used for assigning a sequence number to
356 the entity. See `org-odt--enumerate'.")
358 (defvar org-odt-manifest-file-entries nil
)
359 (defvar hfy-user-sheet-assoc
)
361 (defvar org-odt-zip-dir nil
362 "Temporary work directory for OpenDocument exporter.")
366 ;;; User Configuration Variables
368 (defgroup org-export-odt nil
369 "Options for exporting Org mode files to ODT."
370 :tag
"Org Export ODT"
376 (defcustom org-odt-prettify-xml nil
377 "Specify whether or not the xml output should be prettified.
378 When this option is turned on, `indent-region' is run on all
379 component xml buffers before they are saved. Turn this off for
380 regular use. Turn this on if you need to examine the xml
382 :group
'org-export-odt
390 (defcustom org-odt-schema-dir
393 (message "Debug (ox-odt): Searching for OpenDocument schema files...")
394 (dolist (schema-dir org-odt-schema-dir-list
)
396 (message "Debug (ox-odt): Trying %s..." schema-dir
)
397 (when (and (file-expand-wildcards
398 (expand-file-name "od-manifest-schema*.rnc"
400 (file-expand-wildcards
401 (expand-file-name "od-schema*.rnc"
404 (expand-file-name "schemas.xml" schema-dir
)))
405 (message "Debug (ox-odt): Using schema files under %s"
407 (throw 'schema-dir schema-dir
))))
408 (message "Debug (ox-odt): No OpenDocument schema files installed")
411 "Directory that contains OpenDocument schema files.
413 This directory contains:
414 1. rnc files for OpenDocument schema
415 2. a \"schemas.xml\" file that specifies locating rules needed
416 for auto validation of OpenDocument XML files.
418 Use the customize interface to set this variable. This ensures
419 that `rng-schema-locating-files' is updated and auto-validation
420 of OpenDocument XML takes place based on the value
421 `rng-nxml-auto-validate-flag'.
423 The default value of this variable varies depending on the
424 version of org in use and is initialized from
425 `org-odt-schema-dir-list'. The OASIS schema files are available
426 only in the org's private git repository. It is *not* bundled
427 with GNU ELPA tar or standard Emacs distribution."
429 (const :tag
"Not set" nil
)
430 (directory :tag
"Schema directory"))
431 :group
'org-export-odt
435 "Set `org-odt-schema-dir'.
436 Also add it to `rng-schema-locating-files'."
437 (let ((schema-dir value
))
440 (file-expand-wildcards
441 (expand-file-name "od-manifest-schema*.rnc" schema-dir
))
442 (file-expand-wildcards
443 (expand-file-name "od-schema*.rnc" schema-dir
))
445 (expand-file-name "schemas.xml" schema-dir
)))
448 (message "Error (ox-odt): %s has no OpenDocument schema files"
451 (when org-odt-schema-dir
452 (eval-after-load 'rng-loc
453 '(add-to-list 'rng-schema-locating-files
454 (expand-file-name "schemas.xml"
455 org-odt-schema-dir
))))))
460 (defcustom org-odt-content-template-file nil
461 "Template file for \"content.xml\".
462 The exporter embeds the exported content just before
463 \"</office:text>\" element.
465 If unspecified, the file named \"OrgOdtContentTemplate.xml\"
466 under `org-odt-styles-dir' is used."
467 :type
'(choice (const nil
)
469 :group
'org-export-odt
472 (defcustom org-odt-styles-file nil
473 "Default styles file for use with ODT export.
474 Valid values are one of:
476 2. path to a styles.xml file
477 3. path to a *.odt or a *.ott file
478 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
481 In case of option 1, an in-built styles.xml is used. See
482 `org-odt-styles-dir' for more information.
484 In case of option 3, the specified file is unzipped and the
485 styles.xml embedded therein is used.
487 In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
488 and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
489 generated odt file. Use relative path for specifying the
490 FILE-MEMBERS. styles.xml must be specified as one of the
493 Use options 1, 2 or 3 only if styles.xml alone suffices for
494 achieving the desired formatting. Use option 4, if the styles.xml
495 references additional files like header and footer images for
496 achieving the desired formatting.
498 Use \"#+ODT_STYLES_FILE: ...\" directive to set this variable on
499 a per-file basis. For example,
501 #+ODT_STYLES_FILE: \"/path/to/styles.xml\" or
502 #+ODT_STYLES_FILE: (\"/path/to/file.ott\" (\"styles.xml\" \"image/hdr.png\"))."
503 :group
'org-export-odt
507 (const :tag
"Factory settings" nil
)
508 (file :must-match t
:tag
"styles.xml")
509 (file :must-match t
:tag
"ODT or OTT file")
510 (list :tag
"ODT or OTT file + Members"
511 (file :must-match t
:tag
"ODF Text or Text Template file")
513 (file :tag
" Member" "styles.xml")
514 (repeat (file :tag
"Member"))))))
516 (defcustom org-odt-display-outline-level
2
517 "Outline levels considered for enumerating captioned entities."
518 :group
'org-export-odt
520 :package-version
'(Org .
"8.0")
523 ;;;; Document conversion
525 (defcustom org-odt-convert-processes
527 "soffice --headless --convert-to %f%x --outdir %d %i")
529 "unoconv -f %f -o %d %i"))
530 "Specify a list of document converters and their usage.
531 The converters in this list are offered as choices while
532 customizing `org-odt-convert-process'.
534 This variable is a list where each element is of the
535 form (CONVERTER-NAME CONVERTER-CMD). CONVERTER-NAME is the name
536 of the converter. CONVERTER-CMD is the shell command for the
537 converter and can contain format specifiers. These format
538 specifiers are interpreted as below:
540 %i input file name in full
541 %I input file name as a URL
542 %f format of the output file
543 %o output file name in full
544 %O output file name as a URL
545 %d output dir in full
546 %D output dir as a URL.
547 %x extra options as set in `org-odt-convert-capabilities'."
548 :group
'org-export-odt
552 (const :tag
"None" nil
)
553 (alist :tag
"Converters"
554 :key-type
(string :tag
"Converter Name")
555 :value-type
(group (string :tag
"Command line")))))
557 (defcustom org-odt-convert-process
"LibreOffice"
558 "Use this converter to convert from \"odt\" format to other formats.
559 During customization, the list of converter names are populated
560 from `org-odt-convert-processes'."
561 :group
'org-export-odt
563 :type
'(choice :convert-widget
565 (apply 'widget-convert
(widget-type w
)
566 (eval (car (widget-get w
:args
)))))
567 `((const :tag
"None" nil
)
568 ,@(mapcar (lambda (c)
569 `(const :tag
,(car c
) ,(car c
)))
570 org-odt-convert-processes
))))
572 (defcustom org-odt-convert-capabilities
574 ("odt" "ott" "doc" "rtf" "docx")
575 (("pdf" "pdf") ("odt" "odt") ("rtf" "rtf") ("ott" "ott")
576 ("doc" "doc" ":\"MS Word 97\"") ("docx" "docx") ("html" "html")))
579 (("pdf" "pdf") ("odt" "odt") ("html" "html")))
581 ("ods" "ots" "xls" "csv" "xlsx")
582 (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv") ("ods" "ods")
583 ("xls" "xls") ("xlsx" "xlsx")))
585 ("odp" "otp" "ppt" "pptx")
586 (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("otp" "otp") ("ppt" "ppt")
587 ("pptx" "pptx") ("odg" "odg"))))
588 "Specify input and output formats of `org-odt-convert-process'.
589 More correctly, specify the set of input and output formats that
590 the user is actually interested in.
592 This variable is an alist where each element is of the
593 form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
594 INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
595 alist where each element is of the form (OUTPUT-FMT
596 OUTPUT-FILE-EXTENSION EXTRA-OPTIONS).
598 The variable is interpreted as follows:
599 `org-odt-convert-process' can take any document that is in
600 INPUT-FMT-LIST and produce any document that is in the
601 OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
602 OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
603 serves dual purposes:
604 - It is used for populating completion candidates during
605 `org-odt-convert' commands.
606 - It is used as the value of \"%f\" specifier in
607 `org-odt-convert-process'.
609 EXTRA-OPTIONS is used as the value of \"%x\" specifier in
610 `org-odt-convert-process'.
612 DOCUMENT-CLASS is used to group a set of file formats in
613 INPUT-FMT-LIST in to a single class.
615 Note that this variable inherently captures how LibreOffice based
616 converters work. LibreOffice maps documents of various formats
617 to classes like Text, Web, Spreadsheet, Presentation etc and
618 allow document of a given class (irrespective of its source
619 format) to be converted to any of the export formats associated
622 See default setting of this variable for an typical
624 :group
'org-export-odt
628 (const :tag
"None" nil
)
629 (alist :tag
"Capabilities"
630 :key-type
(string :tag
"Document Class")
632 (group (repeat :tag
"Input formats" (string :tag
"Input format"))
633 (alist :tag
"Output formats"
634 :key-type
(string :tag
"Output format")
636 (group (string :tag
"Output file extension")
638 (const :tag
"None" nil
)
639 (string :tag
"Extra options"))))))))
641 (defcustom org-odt-preferred-output-format nil
642 "Automatically post-process to this format after exporting to \"odt\".
643 Command `org-odt-export-to-odt' exports first to \"odt\" format
644 and then uses `org-odt-convert-process' to convert the
645 resulting document to this format. During customization of this
646 variable, the list of valid values are populated based on
647 `org-odt-convert-capabilities'.
649 You can set this option on per-file basis using file local
650 values. See Info node `(emacs) File Variables'."
651 :group
'org-export-odt
653 :type
'(choice :convert-widget
655 (apply 'widget-convert
(widget-type w
)
656 (eval (car (widget-get w
:args
)))))
657 `((const :tag
"None" nil
)
658 ,@(mapcar (lambda (c)
660 (org-odt-reachable-formats "odt")))))
662 (put 'org-odt-preferred-output-format
'safe-local-variable
'stringp
)
667 (defcustom org-odt-format-drawer-function
(lambda (_name contents
) contents
)
668 "Function called to format a drawer in ODT code.
670 The function must accept two parameters:
671 NAME the drawer name, like \"LOGBOOK\"
672 CONTENTS the contents of the drawer.
674 The function should return the string to be exported.
676 The default value simply returns the value of CONTENTS."
677 :group
'org-export-odt
679 :package-version
'(Org .
"8.3")
685 (defcustom org-odt-format-headline-function
686 'org-odt-format-headline-default-function
687 "Function to format headline text.
689 This function will be called with 5 arguments:
690 TODO the todo keyword (string or nil).
691 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
692 PRIORITY the priority of the headline (integer or nil)
693 TEXT the main headline text (string).
694 TAGS the tags string, separated with colons (string or nil).
696 The function result will be used as headline text."
697 :group
'org-export-odt
699 :package-version
'(Org .
"8.3")
705 (defcustom org-odt-format-inlinetask-function
706 'org-odt-format-inlinetask-default-function
707 "Function called to format an inlinetask in ODT code.
709 The function must accept six parameters:
710 TODO the todo keyword, as a string
711 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
712 PRIORITY the inlinetask priority, as a string
713 NAME the inlinetask name, as a string.
714 TAGS the inlinetask tags, as a string.
715 CONTENTS the contents of the inlinetask, as a string.
717 The function should return the string to be exported."
718 :group
'org-export-odt
720 :package-version
'(Org .
"8.3")
726 (defcustom org-odt-with-latex org-export-with-latex
727 "Non-nil means process LaTeX math snippets.
729 When set, the exporter will process LaTeX environments and
732 This option can also be set with the +OPTIONS line,
733 e.g. \"tex:mathjax\". Allowed values are:
735 nil Ignore math snippets.
736 `verbatim' Keep everything in verbatim
737 `dvipng' Process the LaTeX fragments to images. This will also
738 include processing of non-math environments.
739 `imagemagick' Convert the LaTeX fragments to pdf files and use
740 imagemagick to convert pdf files to png files.
741 `mathjax' Do MathJax preprocessing and arrange for MathJax.js to
743 t Synonym for `mathjax'."
744 :group
'org-export-odt
746 :package-version
'(Org .
"8.0")
748 (const :tag
"Do not process math in any way" nil
)
749 (const :tag
"Use dvipng to make images" dvipng
)
750 (const :tag
"Use imagemagick to make images" imagemagick
)
751 (const :tag
"Use MathJax to display math" mathjax
)
752 (const :tag
"Leave math verbatim" verbatim
)))
757 (defcustom org-odt-inline-formula-rules
758 '(("file" .
"\\.\\(mathml\\|mml\\|odf\\)\\'"))
759 "Rules characterizing formula files that can be inlined into ODT.
761 A rule consists in an association whose key is the type of link
762 to consider, and value is a regexp that will be matched against
764 :group
'org-export-odt
766 :package-version
'(Org .
"8.0")
767 :type
'(alist :key-type
(string :tag
"Type")
768 :value-type
(regexp :tag
"Path")))
770 (defcustom org-odt-inline-image-rules
771 '(("file" .
"\\.\\(jpeg\\|jpg\\|png\\|gif\\|svg\\)\\'"))
772 "Rules characterizing image files that can be inlined into ODT.
774 A rule consists in an association whose key is the type of link
775 to consider, and value is a regexp that will be matched against
777 :group
'org-export-odt
779 :package-version
'(Org .
"8.3")
780 :type
'(alist :key-type
(string :tag
"Type")
781 :value-type
(regexp :tag
"Path")))
783 (defcustom org-odt-pixels-per-inch
96.0
784 "Scaling factor for converting images pixels to inches.
785 Use this for sizing of embedded images. See Info node `(org)
786 Images in ODT export' for more information."
788 :group
'org-export-odt
790 :package-version
'(Org .
"8.1"))
795 (defcustom org-odt-create-custom-styles-for-srcblocks t
796 "Whether custom styles for colorized source blocks be automatically created.
797 When this option is turned on, the exporter creates custom styles
798 for source blocks based on the advice of `htmlfontify'. Creation
799 of custom styles happen as part of `org-odt-hfy-face-to-css'.
801 When this option is turned off exporter does not create such
804 Use the latter option if you do not want the custom styles to be
805 based on your current display settings. It is necessary that the
806 styles.xml already contains needed styles for colorizing to work.
808 This variable is effective only if `org-odt-fontify-srcblocks' is
810 :group
'org-export-odt
814 (defcustom org-odt-fontify-srcblocks t
815 "Specify whether or not source blocks need to be fontified.
816 Turn this option on if you want to colorize the source code
817 blocks in the exported file. For colorization to work, you need
818 to make available an enhanced version of `htmlfontify' library."
820 :group
'org-export-odt
826 (defcustom org-odt-table-styles
827 '(("OrgEquation" "OrgEquation"
828 ((use-first-column-styles . t
)
829 (use-last-column-styles . t
)))
830 ("TableWithHeaderRowAndColumn" "Custom"
831 ((use-first-row-styles . t
)
832 (use-first-column-styles . t
)))
833 ("TableWithFirstRowandLastRow" "Custom"
834 ((use-first-row-styles . t
)
835 (use-last-row-styles . t
)))
836 ("GriddedTable" "Custom" nil
))
837 "Specify how Table Styles should be derived from a Table Template.
838 This is a list where each element is of the
839 form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
841 TABLE-STYLE-NAME is the style associated with the table through
842 \"#+ATTR_ODT: :style TABLE-STYLE-NAME\" line.
844 TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
845 TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
846 below) that is included in `org-odt-content-template-file'.
848 TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
850 PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
852 TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
853 \"FirstRow\" | \"LastRow\" |
854 \"EvenRow\" | \"OddRow\" |
855 \"EvenColumn\" | \"OddColumn\" | \"\"
856 where \"+\" above denotes string concatenation.
858 TABLE-CELL-OPTIONS is an alist where each element is of the
859 form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
860 TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
861 `use-last-row-styles' |
862 `use-first-column-styles' |
863 `use-last-column-styles' |
864 `use-banding-rows-styles' |
865 `use-banding-columns-styles' |
866 `use-first-row-styles'
869 For example, with the following configuration
871 \(setq org-odt-table-styles
872 '((\"TableWithHeaderRowsAndColumns\" \"Custom\"
873 ((use-first-row-styles . t)
874 (use-first-column-styles . t)))
875 (\"TableWithHeaderColumns\" \"Custom\"
876 ((use-first-column-styles . t)))))
878 1. A table associated with \"TableWithHeaderRowsAndColumns\"
879 style will use the following table-cell styles -
880 \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
881 \"CustomTableCell\" and the following paragraph styles
882 \"CustomFirstRowTableParagraph\",
883 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
886 2. A table associated with \"TableWithHeaderColumns\" style will
887 use the following table-cell styles -
888 \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
889 following paragraph styles
890 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
893 Note that TABLE-TEMPLATE-NAME corresponds to the
894 \"<table:table-template>\" elements contained within
895 \"<office:styles>\". The entries (TABLE-STYLE-NAME
896 TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
897 \"table:template-name\" and \"table:use-first-row-styles\" etc
898 attributes of \"<table:table>\" element. Refer ODF-1.2
899 specification for more information. Also consult the
900 implementation filed under `org-odt-get-table-cell-styles'.
902 The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
903 formatting of numbered display equations. Do not delete this
904 style from the list."
905 :group
'org-export-odt
908 (const :tag
"None" nil
)
909 (repeat :tag
"Table Styles"
910 (list :tag
"Table Style Specification"
911 (string :tag
"Table Style Name")
912 (string :tag
"Table Template Name")
913 (alist :options
(use-first-row-styles
915 use-first-column-styles
916 use-last-column-styles
917 use-banding-rows-styles
918 use-banding-columns-styles
)
920 :value-type
(const :tag
"True" t
))))))
924 (defcustom org-odt-use-date-fields nil
925 "Non-nil, if timestamps should be exported as date fields.
927 When nil, export timestamps as plain text.
929 When non-nil, map `org-time-stamp-custom-formats' to a pair of
930 OpenDocument date-styles with names \"OrgDate1\" and \"OrgDate2\"
931 respectively. A timestamp with no time component is formatted
932 with style \"OrgDate1\" while one with explicit hour and minutes
933 is formatted with style \"OrgDate2\".
935 This feature is experimental. Most (but not all) of the common
936 %-specifiers in `format-time-string' are supported.
937 Specifically, locale-dependent specifiers like \"%c\", \"%x\" are
938 formatted as canonical Org timestamps. For finer control, avoid
941 Textual specifiers like \"%b\", \"%h\", \"%B\", \"%a\", \"%A\"
942 etc., are displayed by the application in the default language
943 and country specified in `org-odt-styles-file'. Note that the
944 default styles file uses language \"en\" and country \"GB\". You
945 can localize the week day and month strings in the exported
946 document by setting the default language and country either using
947 the application UI or through a custom styles file.
949 See `org-odt--build-date-styles' for implementation details."
950 :group
'org-export-odt
952 :package-version
'(Org .
"8.0")
957 ;;; Internal functions
961 (defun org-odt--format-timestamp (timestamp &optional end iso-date-p
)
962 (let* ((format-timestamp
963 (lambda (timestamp format
&optional end utc
)
965 (org-timestamp-format timestamp format end utc
)
966 (format-time-string format nil utc
))))
967 (has-time-p (or (not timestamp
)
968 (org-timestamp-has-time-p timestamp
)))
969 (iso-date (let ((format (if has-time-p
"%Y-%m-%dT%H:%M:%S"
970 "%Y-%m-%dT%H:%M:%S")))
971 (funcall format-timestamp timestamp format end
))))
972 (if iso-date-p iso-date
973 (let* ((style (if has-time-p
"OrgDate2" "OrgDate1"))
974 ;; LibreOffice does not care about end goes as content
975 ;; within the "<text:date>...</text:date>" field. The
976 ;; displayed date is automagically corrected to match the
977 ;; format requested by "style:data-style-name" attribute. So
978 ;; don't bother about formatting the date contents to be
979 ;; compatible with "OrgDate1" and "OrgDateTime" styles. A
980 ;; simple Org-style date should suffice.
981 (date (let* ((formats
982 (if org-display-custom-times
984 (car org-time-stamp-custom-formats
) 1 -
1)
986 (cdr org-time-stamp-custom-formats
) 1 -
1))
987 '("%Y-%m-%d %a" .
"%Y-%m-%d %a %H:%M")))
988 (format (if has-time-p
(cdr formats
) (car formats
))))
989 (funcall format-timestamp timestamp format end
)))
990 (repeater (let ((repeater-type (org-element-property
991 :repeater-type timestamp
))
992 (repeater-value (org-element-property
993 :repeater-value timestamp
))
994 (repeater-unit (org-element-property
995 :repeater-unit timestamp
)))
998 (catchup "++") (restart ".+") (cumulate "+"))
1000 (number-to-string repeater-value
))
1002 (hour "h") (day "d") (week "w") (month "m")
1005 (format "<text:date text:date-value=\"%s\" style:data-style-name=\"%s\" text:fixed=\"true\">%s</text:date>"
1006 iso-date style date
)
1007 (and (not (string= repeater
"")) " ")
1012 (defun org-odt--frame (text width height style
&optional extra
1013 anchor-type
&rest title-and-desc
)
1016 (if width
(format " svg:width=\"%0.2fcm\"" width
) "")
1017 (if height
(format " svg:height=\"%0.2fcm\"" height
) "")
1019 (format " text:anchor-type=\"%s\"" (or anchor-type
"paragraph"))
1020 (format " draw:name=\"%s\""
1021 (car (org-odt-add-automatic-style "Frame"))))))
1023 "\n<draw:frame draw:style-name=\"%s\"%s>\n%s\n</draw:frame>"
1026 (let ((title (car title-and-desc
))
1027 (desc (cadr title-and-desc
)))
1029 (format "<svg:title>%s</svg:title>"
1030 (org-odt--encode-plain-text title t
)))
1032 (format "<svg:desc>%s</svg:desc>"
1033 (org-odt--encode-plain-text desc t
)))))))))
1036 ;;;; Library wrappers
1038 (defun org-odt--zip-extract (archive members target
)
1039 (when (atom members
) (setq members
(list members
)))
1041 (dolist (member members
)
1042 (let* ((--quote-file-name
1043 ;; This is shamelessly stolen from `archive-zip-extract'.
1045 (if (or (not (memq system-type
'(windows-nt ms-dos
)))
1046 (and (boundp 'w32-quote-process-args
)
1047 (null w32-quote-process-args
)))
1048 (shell-quote-argument name
)
1050 (target (funcall --quote-file-name target
))
1051 (archive (expand-file-name archive
))
1052 (archive-zip-extract
1053 (list "unzip" "-qq" "-o" "-d" target
))
1054 exit-code command-output
)
1055 (setq command-output
1057 (setq exit-code
(archive-zip-extract archive member
))
1059 (unless (zerop exit-code
)
1060 (message command-output
)
1061 (error "Extraction failed")))))
1065 (defun org-odt--target (text id
)
1068 (format "\n<text:bookmark-start text:name=\"OrgXref.%s\"/>" id
)
1069 (format "\n<text:bookmark text:name=\"%s\"/>" id
) text
1070 (format "\n<text:bookmark-end text:name=\"OrgXref.%s\"/>" id
))))
1074 (defun org-odt--textbox (text width height style
&optional
1077 (format "\n<draw:text-box %s>%s\n</draw:text-box>"
1078 (concat (format " fo:min-height=\"%0.2fcm\"" (or height
.2))
1080 (format " fo:min-width=\"%0.2fcm\"" (or width
.2))))
1082 width nil style extra anchor-type
))
1086 ;;;; Table of Contents
1088 (defun org-odt--format-toc (title entries depth
)
1089 "Return a table of contents.
1090 TITLE is the title of the table, as a string, or nil. ENTRIES is
1091 the contents of the table, as a string. DEPTH is an integer
1092 specifying the depth of the table."
1095 <text:table-of-content text:style-name=\"OrgIndexSection\" text:protected=\"true\" text:name=\"Table of Contents\">\n"
1096 (format " <text:table-of-content-source text:outline-level=\"%d\">" depth
)
1099 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
1103 (let ((levels (number-sequence 1 10)))
1108 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
1109 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
1110 <text:index-entry-chapter/>
1111 <text:index-entry-text/>
1112 <text:index-entry-link-end/>
1113 </text:table-of-content-entry-template>\n"
1114 level level
)) levels
""))
1116 </text:table-of-content-source>
1120 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
1121 <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
1122 </text:index-title>\n"
1127 </text:table-of-content>"))
1129 (cl-defun org-odt-format-toc-headline
1130 (todo _todo-type priority text tags
1131 &key _level section-number headline-label
&allow-other-keys
)
1132 (format "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
1136 (and section-number
(concat section-number
". "))
1139 (let ((style (if (member todo org-done-keywords
)
1140 "OrgDone" "OrgTodo")))
1141 (format "<text:span text:style-name=\"%s\">%s</text:span> "
1144 (let* ((style (format "OrgPriority-%s" priority
))
1145 (priority (format "[#%c]" priority
)))
1146 (format "<text:span text:style-name=\"%s\">%s</text:span> "
1153 (format " <text:span text:style-name=\"%s\">[%s]</text:span>"
1158 "<text:span text:style-name=\"%s\">%s</text:span>"
1159 "OrgTag" tag
)) tags
" : ")))))))
1161 (defun org-odt-toc (depth info
&optional scope
)
1162 "Build a table of contents.
1163 DEPTH is an integer specifying the depth of the table. INFO is
1164 a plist containing current export properties. Optional argument
1165 SCOPE, when non-nil, defines the scope of the table. Return the
1166 table of contents as a string, or nil."
1167 (assert (wholenump depth
))
1168 ;; When a headline is marked as a radio target, as in the example below:
1170 ;; ** <<<Some Heading>>>
1173 ;; suppress generation of radio targets. i.e., Radio targets are to
1174 ;; be marked as targets within /document body/ and *not* within
1175 ;; /TOC/, as otherwise there will be duplicated anchors one in TOC
1176 ;; and one in the document body.
1178 ;; Likewise, links, footnote references and regular targets are also
1180 (let* ((headlines (org-export-collect-headlines info depth scope
))
1181 (backend (org-export-create-backend
1182 :parent
(org-export-backend-name (plist-get info
:back-end
))
1183 :transcoders
'((footnote-reference . ignore
)
1184 (link .
(lambda (object c i
) c
))
1185 (radio-target .
(lambda (object c i
) c
))
1186 (target . ignore
)))))
1188 (org-odt--format-toc
1189 (and (not scope
) (org-export-translate "Table of Contents" :utf-8 info
))
1192 (let* ((entry (org-odt-format-headline--wrap
1193 headline backend info
'org-odt-format-toc-headline
))
1194 (level (org-export-get-relative-level headline info
))
1195 (style (format "Contents_20_%d" level
)))
1196 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1202 ;;;; Document styles
1204 (defun org-odt-add-automatic-style (object-type &optional object-props
)
1205 "Create an automatic style of type OBJECT-TYPE with param OBJECT-PROPS.
1206 OBJECT-PROPS is (typically) a plist created by passing
1207 \"#+ATTR_ODT: \" option of the object in question to
1208 `org-odt-parse-block-attributes'.
1210 Use `org-odt-object-counters' to generate an automatic
1211 OBJECT-NAME and STYLE-NAME. If OBJECT-PROPS is non-nil, add a
1212 new entry in `org-odt-automatic-styles'. Return (OBJECT-NAME
1214 (assert (stringp object-type
))
1215 (let* ((object (intern object-type
))
1217 (seqno (1+ (or (plist-get org-odt-object-counters seqvar
) 0)))
1218 (object-name (format "%s%d" object-type seqno
)) style-name
)
1219 (setq org-odt-object-counters
1220 (plist-put org-odt-object-counters seqvar seqno
))
1222 (setq style-name
(format "Org%s" object-name
))
1223 (setq org-odt-automatic-styles
1224 (plist-put org-odt-automatic-styles object
1225 (append (list (list style-name object-props
))
1226 (plist-get org-odt-automatic-styles object
)))))
1227 (cons object-name style-name
)))
1231 (defun org-odt--checkbox (item)
1232 "Return check-box string associated to ITEM."
1233 (let ((checkbox (org-element-property :checkbox item
)))
1234 (if (not checkbox
) ""
1235 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1236 "OrgCode" (case checkbox
1237 (on "[✓] ") ; CHECK MARK
1243 (defun org-odt--build-date-styles (fmt style
)
1244 ;; In LibreOffice 3.4.6, there doesn't seem to be a convenient way
1245 ;; to modify the date fields. A date could be modified by
1246 ;; offsetting in days. That's about it. Also, date and time may
1247 ;; have to be emitted as two fields - a date field and a time field
1250 ;; One can add Form Controls to date and time fields so that they
1251 ;; can be easily modified. But then, the exported document will
1252 ;; become tightly coupled with LibreOffice and may not function
1253 ;; properly with other OpenDocument applications.
1255 ;; I have a strange feeling that Date styles are a bit flaky at the
1258 ;; The feature is experimental.
1259 (when (and fmt style
)
1261 '(("%A" .
"<number:day-of-week number:style=\"long\"/>")
1262 ("%B" .
"<number:month number:textual=\"true\" number:style=\"long\"/>")
1263 ("%H" .
"<number:hours number:style=\"long\"/>")
1264 ("%M" .
"<number:minutes number:style=\"long\"/>")
1265 ("%S" .
"<number:seconds number:style=\"long\"/>")
1266 ("%V" .
"<number:week-of-year/>")
1267 ("%Y" .
"<number:year number:style=\"long\"/>")
1268 ("%a" .
"<number:day-of-week number:style=\"short\"/>")
1269 ("%b" .
"<number:month number:textual=\"true\" number:style=\"short\"/>")
1270 ("%d" .
"<number:day number:style=\"long\"/>")
1271 ("%e" .
"<number:day number:style=\"short\"/>")
1272 ("%h" .
"<number:month number:textual=\"true\" number:style=\"short\"/>")
1273 ("%k" .
"<number:hours number:style=\"short\"/>")
1274 ("%m" .
"<number:month number:style=\"long\"/>")
1275 ("%p" .
"<number:am-pm/>")
1276 ("%y" .
"<number:year number:style=\"short\"/>")))
1277 (case-fold-search nil
)
1278 (re (mapconcat 'identity
(mapcar 'car fmt-alist
) "\\|"))
1279 match rpl
(start 0) (filler-beg 0) filler-end filler output
)
1281 '(("\\(?:%[[:digit:]]*N\\)" .
"") ; strip ns, us and ns
1282 ("%C" .
"Y") ; replace century with year
1284 ("%G" .
"Y") ; year corresponding to iso week
1285 ("%I" .
"%H") ; hour on a 12-hour clock
1288 ("%U\\|%W" .
"%V") ; week no. starting on Sun./Mon.
1289 ("%Z" .
"") ; time zone name
1290 ("%c" .
"%Y-%M-%d %a %H:%M" ) ; locale's date and time format
1292 ("%X" .
"%x" ) ; locale's pref. time format
1293 ("%j" .
"") ; day of the year
1294 ("%l" .
"%k") ; like %I blank-padded
1295 ("%s" .
"") ; no. of secs since 1970-01-01 00:00:00 +0000
1296 ("%n" .
"<text:line-break/>")
1297 ("%r" .
"%I:%M:%S %p")
1298 ("%t" .
"<text:tab/>")
1299 ("%u\\|%w" .
"") ; numeric day of week - Mon (1-7), Sun(0-6)
1300 ("%x" .
"%Y-%M-%d %a") ; locale's pref. time format
1301 ("%z" .
"") ; time zone in numeric form
1303 (setq fmt
(replace-regexp-in-string (car pair
) (cdr pair
) fmt t t
)))
1304 (while (string-match re fmt start
)
1305 (setq match
(match-string 0 fmt
))
1306 (setq rpl
(assoc-default match fmt-alist
))
1307 (setq start
(match-end 0))
1308 (setq filler-end
(match-beginning 0))
1309 (setq filler
(substring fmt
(prog1 filler-beg
1310 (setq filler-beg
(match-end 0)))
1312 (setq filler
(and (not (string= filler
""))
1313 (format "<number:text>%s</number:text>"
1314 (org-odt--encode-plain-text filler
))))
1315 (setq output
(concat output
"\n" filler
"\n" rpl
)))
1316 (setq filler
(substring fmt filler-beg
))
1317 (unless (string= filler
"")
1318 (setq output
(concat output
1319 (format "\n<number:text>%s</number:text>"
1320 (org-odt--encode-plain-text filler
)))))
1321 (format "\n<number:date-style style:name=\"%s\" %s>%s\n</number:date-style>"
1323 (concat " number:automatic-order=\"true\""
1324 " number:format-source=\"fixed\"")
1327 (defun org-odt-template (contents info
)
1328 "Return complete document string after ODT conversion.
1329 CONTENTS is the transcoded contents string. RAW-DATA is the
1330 original parsed data. INFO is a plist holding export options."
1332 (let ((title (org-export-data (plist-get info
:title
) info
))
1333 (subtitle (org-export-data (plist-get info
:subtitle
) info
))
1334 (author (let ((author (plist-get info
:author
)))
1335 (if (not author
) "" (org-export-data author info
))))
1336 (keywords (or (plist-get info
:keywords
) ""))
1337 (description (or (plist-get info
:description
) "")))
1340 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
1341 <office:document-meta
1342 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
1343 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
1344 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
1345 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
1346 xmlns:ooo=\"http://openoffice.org/2004/office\"
1347 office:version=\"1.2\">
1349 (format "<dc:creator>%s</dc:creator>\n" author
)
1350 (format "<meta:initial-creator>%s</meta:initial-creator>\n" author
)
1351 ;; Date, if required.
1352 (when (plist-get info
:with-date
)
1353 ;; Check if DATE is specified as an Org-timestamp. If yes,
1354 ;; include it as meta information. Otherwise, just use
1356 (let* ((date (let ((date (plist-get info
:date
)))
1357 (and (not (cdr date
))
1358 (eq (org-element-type (car date
)) 'timestamp
)
1360 (let ((iso-date (org-odt--format-timestamp date nil
'iso-date
)))
1362 (format "<dc:date>%s</dc:date>\n" iso-date
)
1363 (format "<meta:creation-date>%s</meta:creation-date>\n"
1365 (format "<meta:generator>%s</meta:generator>\n"
1366 (plist-get info
:creator
))
1367 (format "<meta:keyword>%s</meta:keyword>\n" keywords
)
1368 (format "<dc:subject>%s</dc:subject>\n" description
)
1369 (format "<dc:title>%s</dc:title>\n" title
)
1370 (when (org-string-nw-p subtitle
)
1372 "<meta:user-defined meta:name=\"subtitle\">%s</meta:user-defined>\n"
1375 " </office:meta>\n" "</office:document-meta>")
1376 nil
(concat org-odt-zip-dir
"meta.xml"))
1377 ;; Add meta.xml in to manifest.
1378 (org-odt-create-manifest-file-entry "text/xml" "meta.xml"))
1380 ;; Update styles file.
1381 ;; Copy styles.xml. Also dump htmlfontify styles, if there is any.
1382 ;; Write styles file.
1383 (let* ((styles-file (plist-get info
:odt-styles-file
))
1384 (styles-file (and styles-file
(read (org-trim styles-file
))))
1385 ;; Non-availability of styles.xml is not a critical
1386 ;; error. For now, throw an error.
1387 (styles-file (or styles-file
1388 (plist-get info
:odt-styles-file
)
1389 (expand-file-name "OrgOdtStyles.xml"
1391 (error "org-odt: Missing styles file?"))))
1393 ((listp styles-file
)
1394 (let ((archive (nth 0 styles-file
))
1395 (members (nth 1 styles-file
)))
1396 (org-odt--zip-extract archive members org-odt-zip-dir
)
1397 (dolist (member members
)
1398 (when (org-file-image-p member
)
1399 (let* ((image-type (file-name-extension member
))
1400 (media-type (format "image/%s" image-type
)))
1401 (org-odt-create-manifest-file-entry media-type member
))))))
1402 ((and (stringp styles-file
) (file-exists-p styles-file
))
1403 (let ((styles-file-type (file-name-extension styles-file
)))
1405 ((string= styles-file-type
"xml")
1406 (copy-file styles-file
(concat org-odt-zip-dir
"styles.xml") t
))
1407 ((member styles-file-type
'("odt" "ott"))
1408 (org-odt--zip-extract styles-file
"styles.xml" org-odt-zip-dir
)))))
1410 (error "Invalid specification of styles.xml file: %S"
1411 (plist-get info
:odt-styles-file
))))
1413 ;; create a manifest entry for styles.xml
1414 (org-odt-create-manifest-file-entry "text/xml" "styles.xml")
1416 ;; FIXME: Who is opening an empty styles.xml before this point?
1417 (with-current-buffer
1418 (find-file-noselect (concat org-odt-zip-dir
"styles.xml") t
)
1421 ;; Write custom styles for source blocks
1422 ;; Save STYLES used for colorizing of source blocks.
1423 ;; Update styles.xml with styles that were collected as part of
1424 ;; `org-odt-hfy-face-to-css' callbacks.
1425 (let ((styles (mapconcat (lambda (style) (format " %s\n" (cddr style
)))
1426 hfy-user-sheet-assoc
"")))
1428 (goto-char (point-min))
1429 (when (re-search-forward "</office:styles>" nil t
)
1430 (goto-char (match-beginning 0))
1431 (insert "\n<!-- Org Htmlfontify Styles -->\n" styles
"\n"))))
1433 ;; Update styles.xml - take care of outline numbering
1435 ;; Don't make automatic backup of styles.xml file. This setting
1436 ;; prevents the backed-up styles.xml file from being zipped in to
1437 ;; odt file. This is more of a hackish fix. Better alternative
1438 ;; would be to fix the zip command so that the output odt file
1439 ;; includes only the needed files and excludes any auto-generated
1440 ;; extra files like backups and auto-saves etc etc. Note that
1441 ;; currently the zip command zips up the entire temp directory so
1442 ;; that any auto-generated files created under the hood ends up in
1443 ;; the resulting odt file.
1444 (setq-local backup-inhibited t
)
1446 ;; Outline numbering is retained only upto LEVEL.
1447 ;; To disable outline numbering pass a LEVEL of 0.
1449 (goto-char (point-min))
1451 "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
1453 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1454 (while (re-search-forward regex nil t
)
1455 (unless (let ((sec-num (plist-get info
:section-numbers
))
1456 (level (string-to-number (match-string 2))))
1457 (if (wholenump sec-num
) (<= level sec-num
) sec-num
))
1458 (replace-match replacement t nil
))))
1460 ;; Update content.xml.
1462 (let* ( ;; `org-display-custom-times' should be accessed right
1463 ;; within the context of the Org buffer. So obtain its
1464 ;; value before moving on to temp-buffer context down below.
1466 (if org-display-custom-times
1467 (cons (substring (car org-time-stamp-custom-formats
) 1 -
1)
1468 (substring (cdr org-time-stamp-custom-formats
) 1 -
1))
1469 '("%Y-%M-%d %a" .
"%Y-%M-%d %a %H:%M"))))
1471 (insert-file-contents
1472 (or (plist-get info
:odt-content-template-file
)
1473 (expand-file-name "OrgOdtContentTemplate.xml"
1474 org-odt-styles-dir
)))
1475 ;; Write automatic styles.
1476 ;; - Position the cursor.
1477 (goto-char (point-min))
1478 (re-search-forward " </office:automatic-styles>" nil t
)
1479 (goto-char (match-beginning 0))
1480 ;; - Dump automatic table styles.
1481 (loop for
(style-name props
) in
1482 (plist-get org-odt-automatic-styles
'Table
) do
1483 (when (setq props
(or (plist-get props
:rel-width
) "96"))
1484 (insert (format org-odt-table-style-format style-name props
))))
1485 ;; - Dump date-styles.
1486 (when (plist-get info
:odt-use-date-fields
)
1487 (insert (org-odt--build-date-styles (car custom-time-fmts
)
1489 (org-odt--build-date-styles (cdr custom-time-fmts
)
1491 ;; Update display level.
1492 ;; - Remove existing sequence decls. Also position the cursor.
1493 (goto-char (point-min))
1494 (when (re-search-forward "<text:sequence-decls" nil t
)
1495 (delete-region (match-beginning 0)
1496 (re-search-forward "</text:sequence-decls>" nil nil
)))
1497 ;; Update sequence decls according to user preference.
1500 "\n<text:sequence-decls>\n%s\n</text:sequence-decls>"
1504 "<text:sequence-decl text:display-outline-level=\"%d\" text:name=\"%s\"/>"
1505 (plist-get info
:odt-display-outline-level
)
1507 org-odt-category-map-alist
"\n")))
1508 ;; Position the cursor to document body.
1509 (goto-char (point-min))
1510 (re-search-forward "</office:text>" nil nil
)
1511 (goto-char (match-beginning 0))
1513 ;; Preamble - Title, Author, Date etc.
1515 (let* ((title (and (plist-get info
:with-title
)
1516 (org-export-data (plist-get info
:title
) info
)))
1517 (subtitle (when title
1518 (org-export-data (plist-get info
:subtitle
) info
)))
1519 (author (and (plist-get info
:with-author
)
1520 (let ((auth (plist-get info
:author
)))
1521 (and auth
(org-export-data auth info
)))))
1522 (email (plist-get info
:email
))
1523 ;; Switch on or off above vars based on user settings
1524 (author (and (plist-get info
:with-author
) (or author email
)))
1525 (email (and (plist-get info
:with-email
) email
)))
1528 (when (org-string-nw-p title
)
1530 (format "\n<text:p text:style-name=\"%s\">%s</text:p>\n"
1531 "OrgTitle" (format "\n<text:title>%s</text:title>" title
))
1533 "\n<text:p text:style-name=\"OrgTitle\"/>\n"
1535 (when (org-string-nw-p subtitle
)
1537 (format "<text:p text:style-name=\"OrgSubtitle\">\n%s\n</text:p>\n"
1539 "<text:user-defined style:data-style-name=\"N0\" text:name=\"subtitle\">\n"
1541 "</text:user-defined>\n"))
1543 "<text:p text:style-name=\"OrgSubtitle\"/>\n"))))
1545 ((and author
(not email
))
1548 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1550 (format "<text:initial-creator>%s</text:initial-creator>" author
))
1552 "\n<text:p text:style-name=\"OrgSubtitle\"/>"))
1554 ;; Author and E-mail.
1557 "\n<text:p text:style-name=\"%s\">%s</text:p>"
1560 "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
1561 (concat "mailto:" email
)
1562 (format "<text:initial-creator>%s</text:initial-creator>" author
)))
1564 "\n<text:p text:style-name=\"OrgSubtitle\"/>")))
1565 ;; Date, if required.
1566 (when (plist-get info
:with-date
)
1567 (let* ((date (plist-get info
:date
))
1568 ;; Check if DATE is specified as a timestamp.
1569 (timestamp (and (not (cdr date
))
1570 (eq (org-element-type (car date
)) 'timestamp
)
1574 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1576 (if (and (plist-get info
:odt-use-date-fields
) timestamp
)
1577 (org-odt--format-timestamp (car date
))
1578 (org-export-data date info
)))
1580 "<text:p text:style-name=\"OrgSubtitle\"/>")))))))
1581 ;; Table of Contents
1582 (let* ((with-toc (plist-get info
:with-toc
))
1583 (depth (and with-toc
(if (wholenump with-toc
)
1585 (plist-get info
:headline-levels
)))))
1586 (when depth
(insert (or (org-odt-toc depth info
) ""))))
1590 (buffer-substring-no-properties (point-min) (point-max)))))
1594 ;;; Transcode Functions
1598 (defun org-odt-bold (_bold contents _info
)
1599 "Transcode BOLD from Org to ODT.
1600 CONTENTS is the text with bold markup. INFO is a plist holding
1601 contextual information."
1602 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1608 (defun org-odt-center-block (_center-block contents _info
)
1609 "Transcode a CENTER-BLOCK element from Org to ODT.
1610 CONTENTS holds the contents of the center block. INFO is a plist
1611 holding contextual information."
1617 (defun org-odt-clock (clock contents info
)
1618 "Transcode a CLOCK element from Org to ODT.
1619 CONTENTS is nil. INFO is a plist used as a communication
1621 (let ((timestamp (org-element-property :value clock
))
1622 (duration (org-element-property :duration clock
)))
1623 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1624 (if (eq (org-element-type (org-export-get-next-element clock info
))
1625 'clock
) "OrgClock" "OrgClockLastLine")
1627 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1628 "OrgClockKeyword" org-clock-string
)
1629 (org-odt-timestamp timestamp contents info
)
1630 (and duration
(format " (%s)" duration
))))))
1635 (defun org-odt-code (code _contents _info
)
1636 "Transcode a CODE object from Org to ODT.
1637 CONTENTS is nil. INFO is a plist used as a communication
1639 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1640 "OrgCode" (org-odt--encode-plain-text
1641 (org-element-property :value code
))))
1646 (defun org-odt-drawer (drawer contents info
)
1647 "Transcode a DRAWER element from Org to ODT.
1648 CONTENTS holds the contents of the block. INFO is a plist
1649 holding contextual information."
1650 (let* ((name (org-element-property :drawer-name drawer
))
1651 (output (funcall (plist-get info
:odt-format-drawer-function
)
1658 (defun org-odt-dynamic-block (_dynamic-block contents _info
)
1659 "Transcode a DYNAMIC-BLOCK element from Org to ODT.
1660 CONTENTS holds the contents of the block. INFO is a plist
1661 holding contextual information. See `org-export-data'."
1667 (defun org-odt-entity (entity _contents _info
)
1668 "Transcode an ENTITY object from Org to ODT.
1669 CONTENTS are the definition itself. INFO is a plist holding
1670 contextual information."
1671 (org-element-property :utf-8 entity
))
1676 (defun org-odt-example-block (example-block _contents info
)
1677 "Transcode a EXAMPLE-BLOCK element from Org to ODT.
1678 CONTENTS is nil. INFO is a plist holding contextual information."
1679 (org-odt-format-code example-block info
))
1684 (defun org-odt-export-snippet (export-snippet _contents _info
)
1685 "Transcode a EXPORT-SNIPPET object from Org to ODT.
1686 CONTENTS is nil. INFO is a plist holding contextual information."
1687 (when (eq (org-export-snippet-backend export-snippet
) 'odt
)
1688 (org-element-property :value export-snippet
)))
1693 (defun org-odt-export-block (export-block _contents _info
)
1694 "Transcode a EXPORT-BLOCK element from Org to ODT.
1695 CONTENTS is nil. INFO is a plist holding contextual information."
1696 (when (string= (org-element-property :type export-block
) "ODT")
1697 (org-remove-indentation (org-element-property :value export-block
))))
1702 (defun org-odt-fixed-width (fixed-width _contents info
)
1703 "Transcode a FIXED-WIDTH element from Org to ODT.
1704 CONTENTS is nil. INFO is a plist holding contextual information."
1705 (org-odt-do-format-code (org-element-property :value fixed-width
) info
))
1708 ;;;; Footnote Definition
1710 ;; Footnote Definitions are ignored.
1713 ;;;; Footnote Reference
1715 (defun org-odt-footnote-reference (footnote-reference _contents info
)
1716 "Transcode a FOOTNOTE-REFERENCE element from Org to ODT.
1717 CONTENTS is nil. INFO is a plist holding contextual information."
1718 (let ((--format-footnote-definition
1720 (setq n
(format "%d" n
))
1721 (let ((id (concat "fn" n
))
1722 (note-class "footnote"))
1724 "<text:note text:id=\"%s\" text:note-class=\"%s\">%s</text:note>"
1727 (format "<text:note-citation>%s</text:note-citation>" n
)
1728 (format "<text:note-body>%s</text:note-body>" def
))))))
1729 (--format-footnote-reference
1731 (setq n
(format "%d" n
))
1732 (let ((note-class "footnote")
1734 (ref-name (concat "fn" n
)))
1736 "<text:span text:style-name=\"%s\">%s</text:span>"
1738 (format "<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:note-ref>"
1739 note-class ref-format ref-name n
))))))
1741 ;; Insert separator between two footnotes in a row.
1742 (let ((prev (org-export-get-previous-element footnote-reference info
)))
1743 (and (eq (org-element-type prev
) 'footnote-reference
)
1744 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1745 "OrgSuperscript" ",")))
1746 ;; Transcode footnote reference.
1747 (let ((n (org-export-get-footnote-number footnote-reference info nil t
)))
1750 (org-export-footnote-first-reference-p footnote-reference info nil t
))
1751 (funcall --format-footnote-reference n
))
1753 (let* ((raw (org-export-get-footnote-definition
1754 footnote-reference info
))
1756 (let ((def (org-trim
1757 (org-export-data-with-backend
1759 (org-export-create-backend
1762 '((paragraph .
(lambda (p c i
)
1763 (org-odt--format-paragraph
1767 "OrgFootnoteQuotations")))))
1769 ;; Inline definitions are secondary strings. We
1770 ;; need to wrap them within a paragraph.
1771 (if (memq (org-element-type (car (org-element-contents raw
)))
1772 org-element-all-elements
)
1775 "\n<text:p text:style-name=\"Footnote\">%s</text:p>"
1777 (funcall --format-footnote-definition n def
))))))))
1782 (defun org-odt-format-headline--wrap (headline backend info
1783 &optional format-function
1785 "Transcode a HEADLINE element using BACKEND.
1786 INFO is a plist holding contextual information."
1787 (setq backend
(or backend
(plist-get info
:back-end
)))
1788 (let* ((level (+ (org-export-get-relative-level headline info
)))
1789 (headline-number (org-export-get-headline-number headline info
))
1790 (section-number (and (org-export-numbered-headline-p headline info
)
1791 (mapconcat 'number-to-string
1792 headline-number
".")))
1793 (todo (and (plist-get info
:with-todo-keywords
)
1794 (let ((todo (org-element-property :todo-keyword headline
)))
1796 (org-export-data-with-backend todo backend info
)))))
1797 (todo-type (and todo
(org-element-property :todo-type headline
)))
1798 (priority (and (plist-get info
:with-priority
)
1799 (org-element-property :priority headline
)))
1800 (text (org-export-data-with-backend
1801 (org-element-property :title headline
) backend info
))
1802 (tags (and (plist-get info
:with-tags
)
1803 (org-export-get-tags headline info
)))
1804 (headline-label (org-export-get-reference headline info
))
1806 (if (functionp format-function
) format-function
1808 (lambda (todo todo-type priority text tags
1809 &key _level _section-number _headline-label
1811 (funcall (plist-get info
:odt-format-headline-function
)
1812 todo todo-type priority text tags
))))))
1813 (apply format-function
1814 todo todo-type priority text tags
1815 :headline-label headline-label
1817 :section-number section-number extra-keys
)))
1819 (defun org-odt-headline (headline contents info
)
1820 "Transcode a HEADLINE element from Org to ODT.
1821 CONTENTS holds the contents of the headline. INFO is a plist
1822 holding contextual information."
1823 ;; Case 1: This is a footnote section: ignore it.
1824 (unless (org-element-property :footnote-section-p headline
)
1825 (let* ((full-text (org-odt-format-headline--wrap headline nil info
))
1826 ;; Get level relative to current parsed data.
1827 (level (org-export-get-relative-level headline info
))
1828 (numbered (org-export-numbered-headline-p headline info
))
1829 ;; Get canonical label for the headline.
1830 (id (org-export-get-reference headline info
))
1833 (let ((id (org-element-property :ID headline
)))
1834 (if id
(org-odt--target "" (concat "ID-" id
)) "")))
1836 (anchored-title (org-odt--target full-text id
)))
1838 ;; Case 2. This is a deep sub-tree: export it as a list item.
1839 ;; Also export as items headlines for which no section
1840 ;; format has been found.
1841 ((org-export-low-level-p headline info
)
1842 ;; Build the real contents of the sub-tree.
1844 (and (org-export-first-sibling-p headline info
)
1845 (format "\n<text:list text:style-name=\"%s\" %s>"
1846 ;; Choose style based on list type.
1847 (if numbered
"OrgNumberedList" "OrgBulletedList")
1848 ;; If top-level list, re-start numbering. Otherwise,
1849 ;; continue numbering.
1850 (format "text:continue-numbering=\"%s\""
1851 (let* ((parent (org-export-get-parent-headline
1854 (org-export-low-level-p parent info
))
1856 (let ((headline-has-table-p
1857 (let ((section (assq 'section
(org-element-contents headline
))))
1858 (assq 'table
(and section
(org-element-contents section
))))))
1859 (format "\n<text:list-item>\n%s\n%s"
1861 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1863 (concat extra-targets anchored-title
))
1865 (if headline-has-table-p
1866 "</text:list-header>"
1867 "</text:list-item>")))
1868 (and (org-export-last-sibling-p headline info
)
1870 ;; Case 3. Standard headline. Export it as a section.
1874 "\n<text:h text:style-name=\"%s\" text:outline-level=\"%s\" text:is-list-header=\"%s\">%s</text:h>"
1875 (format "Heading_20_%s%s"
1876 level
(if numbered
"" "_unnumbered"))
1878 (if numbered
"false" "true")
1879 (concat extra-targets anchored-title
))
1882 (defun org-odt-format-headline-default-function
1883 (todo todo-type priority text tags
)
1884 "Default format function for a headline.
1885 See `org-odt-format-headline-function' for details."
1889 (let ((style (if (eq todo-type
'done
) "OrgDone" "OrgTodo")))
1890 (format "<text:span text:style-name=\"%s\">%s</text:span> " style todo
)))
1892 (let* ((style (format "OrgPriority-%s" priority
))
1893 (priority (format "[#%c]" priority
)))
1894 (format "<text:span text:style-name=\"%s\">%s</text:span> "
1902 (format "<text:span text:style-name=\"%s\">[%s]</text:span>"
1903 "OrgTags" (mapconcat
1906 "<text:span text:style-name=\"%s\">%s</text:span>"
1907 "OrgTag" tag
)) tags
" : "))))))
1910 ;;;; Horizontal Rule
1912 (defun org-odt-horizontal-rule (_horizontal-rule _contents _info
)
1913 "Transcode an HORIZONTAL-RULE object from Org to ODT.
1914 CONTENTS is nil. INFO is a plist holding contextual information."
1915 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1916 "Horizontal_20_Line" ""))
1919 ;;;; Inline Babel Call
1921 ;; Inline Babel Calls are ignored.
1924 ;;;; Inline Src Block
1926 (defun org-odt--find-verb-separator (s)
1927 "Return a character not used in string S.
1928 This is used to choose a separator for constructs like \\verb."
1929 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1930 (loop for c across ll
1931 when
(not (string-match (regexp-quote (char-to-string c
)) s
))
1932 return
(char-to-string c
))))
1934 (defun org-odt-inline-src-block (_inline-src-block _contents _info
)
1935 "Transcode an INLINE-SRC-BLOCK element from Org to ODT.
1936 CONTENTS holds the contents of the item. INFO is a plist holding
1937 contextual information."
1943 (defun org-odt-inlinetask (inlinetask contents info
)
1944 "Transcode an INLINETASK element from Org to ODT.
1945 CONTENTS holds the contents of the block. INFO is a plist
1946 holding contextual information."
1948 (and (plist-get info
:with-todo-keywords
)
1949 (let ((todo (org-element-property :todo-keyword inlinetask
)))
1950 (and todo
(org-export-data todo info
)))))
1951 (todo-type (and todo
(org-element-property :todo-type inlinetask
)))
1952 (priority (and (plist-get info
:with-priority
)
1953 (org-element-property :priority inlinetask
)))
1954 (text (org-export-data (org-element-property :title inlinetask
) info
))
1955 (tags (and (plist-get info
:with-tags
)
1956 (org-export-get-tags inlinetask info
))))
1957 (funcall (plist-get info
:odt-format-inlinetask-function
)
1958 todo todo-type priority text tags contents
)))
1960 (defun org-odt-format-inlinetask-default-function
1961 (todo todo-type priority name tags contents
)
1962 "Default format function for a inlinetasks.
1963 See `org-odt-format-inlinetask-function' for details."
1964 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1968 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1969 "OrgInlineTaskHeading"
1970 (org-odt-format-headline-default-function
1971 todo todo-type priority name tags
))
1973 nil nil
"OrgInlineTaskFrame" " style:rel-width=\"100%\"")))
1977 (defun org-odt-italic (_italic contents _info
)
1978 "Transcode ITALIC from Org to ODT.
1979 CONTENTS is the text with italic markup. INFO is a plist holding
1980 contextual information."
1981 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1982 "Emphasis" contents
))
1987 (defun org-odt-item (item contents info
)
1988 "Transcode an ITEM element from Org to ODT.
1989 CONTENTS holds the contents of the item. INFO is a plist holding
1990 contextual information."
1991 (let* ((plain-list (org-export-get-parent item
))
1992 (type (org-element-property :type plain-list
)))
1993 (unless (memq type
'(ordered unordered descriptive-1 descriptive-2
))
1994 (error "Unknown list type: %S" type
))
1995 (format "\n<text:list-item>\n%s\n%s"
1997 (if (org-element-map item
'table
#'identity info
'first-match
)
1998 "</text:list-header>"
1999 "</text:list-item>"))))
2003 (defun org-odt-keyword (keyword _contents info
)
2004 "Transcode a KEYWORD element from Org to ODT.
2005 CONTENTS is nil. INFO is a plist holding contextual
2007 (let ((key (org-element-property :key keyword
))
2008 (value (org-element-property :value keyword
)))
2010 ((string= key
"ODT") value
)
2011 ((string= key
"INDEX")
2014 ((string= key
"TOC")
2015 (let ((case-fold-search t
))
2017 ((org-string-match-p "\\<headlines\\>" value
)
2018 (let ((depth (or (and (string-match "\\<[0-9]+\\>" value
)
2019 (string-to-number (match-string 0 value
)))
2020 (plist-get info
:headline-levels
)))
2021 (localp (org-string-match-p "\\<local\\>" value
)))
2022 (org-odt-toc depth info
(and localp keyword
))))
2023 ((org-string-match-p "tables\\|figures\\|listings" value
)
2028 ;;;; Latex Environment
2031 ;; (eval-after-load 'ox-odt '(ad-deactivate 'org-format-latex-as-mathml))
2032 ;; (defadvice org-format-latex-as-mathml ; FIXME
2033 ;; (after org-odt-protect-latex-fragment activate)
2034 ;; "Encode LaTeX fragment as XML.
2035 ;; Do this when translation to MathML fails."
2036 ;; (unless (> (length ad-return-value) 0)
2037 ;; (setq ad-return-value (org-odt--encode-plain-text (ad-get-arg 0)))))
2039 (defun org-odt-latex-environment (latex-environment _contents info
)
2040 "Transcode a LATEX-ENVIRONMENT element from Org to ODT.
2041 CONTENTS is nil. INFO is a plist holding contextual information."
2042 (let* ((latex-frag (org-remove-indentation
2043 (org-element-property :value latex-environment
))))
2044 (org-odt-do-format-code latex-frag info
)))
2049 ;; (when latex-frag ; FIXME
2050 ;; (setq href (org-propertize href :title "LaTeX Fragment"
2051 ;; :description latex-frag)))
2053 ;; provide descriptions
2055 (defun org-odt-latex-fragment (latex-fragment _contents _info
)
2056 "Transcode a LATEX-FRAGMENT object from Org to ODT.
2057 CONTENTS is nil. INFO is a plist holding contextual information."
2058 (let ((latex-frag (org-element-property :value latex-fragment
)))
2059 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2060 "OrgCode" (org-odt--encode-plain-text latex-frag t
))))
2065 (defun org-odt-line-break (_line-break _contents _info
)
2066 "Transcode a LINE-BREAK object from Org to ODT.
2067 CONTENTS is nil. INFO is a plist holding contextual information."
2068 "<text:line-break/>")
2073 ;;;; Links :: Label references
2075 (defun org-odt--enumerate (element info
&optional predicate n
)
2076 (when predicate
(assert (funcall predicate element info
)))
2077 (let* ((--numbered-parent-headline-at-<=-n
2078 (lambda (element n info
)
2079 (loop for x in
(org-element-lineage element
)
2080 thereis
(and (eq (org-element-type x
) 'headline
)
2081 (<= (org-export-get-relative-level x info
) n
)
2082 (org-export-numbered-headline-p x info
)
2085 (lambda (element scope info
&optional predicate
)
2087 (org-element-map (or scope
(plist-get info
:parse-tree
))
2088 (org-element-type element
)
2090 (and (or (not predicate
) (funcall predicate el info
))
2094 info
'first-match
))))
2095 (scope (funcall --numbered-parent-headline-at-
<=-n
2097 (or n
(plist-get info
:odt-display-outline-level
))
2099 (ordinal (funcall --enumerate element scope info predicate
))
2104 (mapconcat 'number-to-string
2105 (org-export-get-headline-number scope info
) "."))
2109 (number-to-string ordinal
))))
2112 (defun org-odt-format-label (element info op
)
2113 "Return a label for ELEMENT.
2115 ELEMENT is a `link', `table', `src-block' or `paragraph' type
2116 element. INFO is a plist used as a communication channel. OP is
2117 either `definition' or `reference', depending on the purpose of
2118 the generated string.
2120 Return value is a string if OP is set to `reference' or a cons
2121 cell like CAPTION . SHORT-CAPTION) where CAPTION and
2122 SHORT-CAPTION are strings."
2123 (assert (memq (org-element-type element
) '(link table src-block paragraph
)))
2124 (let* ((element-or-parent
2125 (case (org-element-type element
)
2126 (link (org-export-get-parent-element element
))
2128 ;; Get label and caption.
2129 (label (and (or (org-element-property :name element
)
2130 (org-element-property :name element-or-parent
))
2131 (org-export-get-reference element-or-parent info
)))
2132 (caption (let ((c (org-export-get-caption element-or-parent
)))
2133 (and c
(org-export-data c info
))))
2134 ;; FIXME: We don't use short-caption for now
2135 (short-caption nil
))
2136 (when (or label caption
)
2137 (let* ((default-category
2138 (case (org-element-type element
)
2140 (src-block "__Listing__")
2143 ((org-odt--enumerable-latex-image-p element info
)
2145 ((org-odt--enumerable-image-p element info
)
2147 ((org-odt--enumerable-formula-p element info
)
2149 (t (error "Don't know how to format label for link: %S"
2151 (t (error "Don't know how to format label for element type: %s"
2152 (org-element-type element
)))))
2154 (assert default-category
)
2155 (destructuring-bind (counter label-style category predicate
)
2156 (assoc-default default-category org-odt-category-map-alist
)
2157 ;; Compute sequence number of the element.
2158 (setq seqno
(org-odt--enumerate element info predicate
))
2159 ;; Localize category string.
2160 (setq category
(org-export-translate category
:utf-8 info
))
2162 ;; Case 1: Handle Label definition.
2166 ;; Sneak in a bookmark. The bookmark is used when the
2167 ;; labeled element is referenced with a link that
2168 ;; provides its own description.
2169 (format "\n<text:bookmark text:name=\"%s\"/>" label
)
2170 ;; Label definition: Typically formatted as below:
2171 ;; CATEGORY SEQ-NO: LONG CAPTION
2172 ;; with translation for correct punctuation.
2174 (org-export-translate
2175 (cadr (assoc-string label-style org-odt-label-styles t
))
2179 "<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">%s</text:sequence>"
2180 label counter counter seqno
))
2181 (?c .
,(or caption
"")))))
2183 ;; Case 2: Handle Label reference.
2185 (let* ((fmt (cddr (assoc-string label-style org-odt-label-styles t
)))
2188 (format "<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:sequence-ref>"
2191 (format-spec fmt2
`((?e .
,category
) (?n .
,seqno
))))))
2192 (t (error "Unknown %S on label" op
))))))))
2195 ;;;; Links :: Inline Images
2197 (defun org-odt--copy-image-file (path)
2198 "Returns the internal name of the file"
2199 (let* ((image-type (file-name-extension path
))
2200 (media-type (format "image/%s" image-type
))
2201 (target-dir "Images/")
2203 (format "%s%04d.%s" target-dir
2204 (incf org-odt-embedded-images-count
) image-type
)))
2205 (message "Embedding %s as %s..."
2206 (substring-no-properties path
) target-file
)
2208 (when (= 1 org-odt-embedded-images-count
)
2209 (make-directory (concat org-odt-zip-dir target-dir
))
2210 (org-odt-create-manifest-file-entry "" target-dir
))
2212 (copy-file path
(concat org-odt-zip-dir target-file
) 'overwrite
)
2213 (org-odt-create-manifest-file-entry media-type target-file
)
2216 (defun org-odt--image-size
2217 (file info
&optional user-width user-height scale dpi embed-as
)
2218 (let* ((--pixels-to-cms
2219 (function (lambda (pixels dpi
)
2220 (let ((cms-per-inch 2.54)
2221 (inches (/ pixels dpi
)))
2222 (* cms-per-inch inches
)))))
2225 (lambda (size-in-pixels dpi
)
2227 (cons (funcall --pixels-to-cms
(car size-in-pixels
) dpi
)
2228 (funcall --pixels-to-cms
(cdr size-in-pixels
) dpi
))))))
2229 (dpi (or dpi
(plist-get info
:odt-pixels-per-inch
)))
2230 (anchor-type (or embed-as
"paragraph"))
2231 (user-width (and (not scale
) user-width
))
2232 (user-height (and (not scale
) user-height
))
2235 (not (and user-height user-width
))
2238 (and (executable-find "identify")
2239 (let ((size-in-pixels
2240 (let ((dim (shell-command-to-string
2241 (format "identify -format \"%%w:%%h\" \"%s\""
2243 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim
)
2244 (cons (string-to-number (match-string 1 dim
))
2245 (string-to-number (match-string 2 dim
)))))))
2246 (funcall --size-in-cms size-in-pixels dpi
)))
2248 (let ((size-in-pixels
2249 (ignore-errors ; Emacs could be in batch mode
2251 (image-size (create-image file
) 'pixels
))))
2252 (funcall --size-in-cms size-in-pixels dpi
))
2253 ;; Use hard-coded values.
2254 (cdr (assoc-string anchor-type
2255 org-odt-default-image-sizes-alist
))
2257 (error "Cannot determine image size, aborting"))))
2258 (width (car size
)) (height (cdr size
)))
2261 (setq width
(* width scale
) height
(* height scale
)))
2262 ((and user-height user-width
)
2263 (setq width user-width height user-height
))
2265 (setq width
(* user-height
(/ width height
)) height user-height
))
2267 (setq height
(* user-width
(/ height width
)) width user-width
))
2269 ;; ensure that an embedded image fits comfortably within a page
2270 (let ((max-width (car org-odt-max-image-size
))
2271 (max-height (cdr org-odt-max-image-size
)))
2272 (when (or (> width max-width
) (> height max-height
))
2273 (let* ((scale1 (/ max-width width
))
2274 (scale2 (/ max-height height
))
2275 (scale (min scale1 scale2
)))
2276 (setq width
(* scale width
) height
(* scale height
)))))
2277 (cons width height
)))
2279 (defun org-odt-link--inline-image (element info
)
2280 "Return ODT code for an inline image.
2281 LINK is the link pointing to the inline image. INFO is a plist
2282 used as a communication channel."
2283 (assert (eq (org-element-type element
) 'link
))
2284 (let* ((src (let* ((type (org-element-property :type element
))
2285 (raw-path (org-element-property :path element
)))
2286 (cond ((member type
'("http" "https"))
2287 (concat type
":" raw-path
))
2288 ((file-name-absolute-p raw-path
)
2289 (expand-file-name raw-path
))
2291 (src-expanded (if (file-name-absolute-p src
) src
2292 (expand-file-name src
(file-name-directory
2293 (plist-get info
:input-file
)))))
2295 "\n<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>"
2296 (org-odt--copy-image-file src-expanded
)))
2297 ;; Extract attributes from #+ATTR_ODT line.
2298 (attr-from (case (org-element-type element
)
2299 (link (org-export-get-parent-element element
))
2301 ;; Convert attributes to a plist.
2302 (attr-plist (org-export-read-attribute :attr_odt attr-from
))
2303 ;; Handle `:anchor', `:style' and `:attributes' properties.
2305 (car (assoc-string (plist-get attr-plist
:anchor
)
2306 '(("as-char") ("paragraph") ("page")) t
)))
2308 (and user-frame-anchor
(plist-get attr-plist
:style
)))
2310 (and user-frame-anchor
(plist-get attr-plist
:attributes
)))
2312 (list user-frame-style user-frame-attrs user-frame-anchor
))
2313 ;; (embed-as (or embed-as user-frame-anchor "paragraph"))
2315 ;; Handle `:width', `:height' and `:scale' properties. Read
2316 ;; them as numbers since we need them for computations.
2317 (size (org-odt--image-size
2319 (let ((width (plist-get attr-plist
:width
)))
2320 (and width
(read width
)))
2321 (let ((length (plist-get attr-plist
:length
)))
2322 (and length
(read length
)))
2323 (let ((scale (plist-get attr-plist
:scale
)))
2324 (and scale
(read scale
)))
2328 (width (car size
)) (height (cdr size
))
2329 (standalone-link-p (org-odt--standalone-link-p element info
))
2330 (embed-as (if standalone-link-p
"paragraph" "as-char"))
2331 (captions (org-odt-format-label element info
'definition
))
2332 (caption (car captions
))
2333 (entity (concat (and caption
"Captioned") embed-as
"Image"))
2334 ;; Check if this link was created by LaTeX-to-PNG converter.
2335 (replaces (org-element-property
2336 :replaces
(if (not standalone-link-p
) element
2337 (org-export-get-parent-element element
))))
2338 ;; If yes, note down the type of the element - LaTeX Fragment
2339 ;; or LaTeX environment. It will go in to frame title.
2340 (title (and replaces
(capitalize
2341 (symbol-name (org-element-type replaces
)))))
2343 ;; If yes, note down its contents. It will go in to frame
2344 ;; description. This quite useful for debugging.
2345 (desc (and replaces
(org-element-property :value replaces
))))
2346 (org-odt--render-image/formula entity href width height
2347 captions user-frame-params title desc
)))
2350 ;;;; Links :: Math formula
2352 (defun org-odt-link--inline-formula (element info
)
2353 (let* ((src (let ((raw-path (org-element-property :path element
)))
2355 ((file-name-absolute-p raw-path
)
2356 (expand-file-name raw-path
))
2358 (src-expanded (if (file-name-absolute-p src
) src
2359 (expand-file-name src
(file-name-directory
2360 (plist-get info
:input-file
)))))
2363 "\n<draw:object %s xlink:href=\"%s\" xlink:type=\"simple\"/>"
2364 " xlink:show=\"embed\" xlink:actuate=\"onLoad\""
2365 (file-name-directory (org-odt--copy-formula-file src-expanded
))))
2366 (standalone-link-p (org-odt--standalone-link-p element info
))
2367 (embed-as (if standalone-link-p
'paragraph
'character
))
2368 (captions (org-odt-format-label element info
'definition
))
2369 ;; Check if this link was created by LaTeX-to-MathML
2371 (replaces (org-element-property
2372 :replaces
(if (not standalone-link-p
) element
2373 (org-export-get-parent-element element
))))
2374 ;; If yes, note down the type of the element - LaTeX Fragment
2375 ;; or LaTeX environment. It will go in to frame title.
2376 (title (and replaces
(capitalize
2377 (symbol-name (org-element-type replaces
)))))
2379 ;; If yes, note down its contents. It will go in to frame
2380 ;; description. This quite useful for debugging.
2381 (desc (and replaces
(org-element-property :value replaces
)))
2384 ((eq embed-as
'character
)
2385 (org-odt--render-image/formula
"InlineFormula" href width height
2386 nil nil title desc
))
2388 (let* ((equation (org-odt--render-image/formula
2389 "CaptionedDisplayFormula" href width height
2390 captions nil title desc
))
2392 (let* ((org-odt-category-map-alist
2393 '(("__MathFormula__" "Text" "math-label" "Equation"
2394 org-odt--enumerable-formula-p
))))
2395 (car (org-odt-format-label element info
'definition
)))))
2396 (concat equation
"<text:tab/>" label
))))))
2398 (defun org-odt--copy-formula-file (src-file)
2399 "Returns the internal name of the file"
2400 (let* ((target-dir (format "Formula-%04d/"
2401 (incf org-odt-embedded-formulas-count
)))
2402 (target-file (concat target-dir
"content.xml")))
2403 ;; Create a directory for holding formula file. Also enter it in
2405 (make-directory (concat org-odt-zip-dir target-dir
))
2406 (org-odt-create-manifest-file-entry
2407 "application/vnd.oasis.opendocument.formula" target-dir
"1.2")
2408 ;; Copy over the formula file from user directory to zip
2410 (message "Embedding %s as %s..." src-file target-file
)
2411 (let ((case-fold-search nil
))
2414 ((string-match "\\.\\(mathml\\|mml\\)\\'" src-file
)
2415 (copy-file src-file
(concat org-odt-zip-dir target-file
) 'overwrite
))
2416 ;; Case 2: OpenDocument formula.
2417 ((string-match "\\.odf\\'" src-file
)
2418 (org-odt--zip-extract src-file
"content.xml"
2419 (concat org-odt-zip-dir target-dir
)))
2420 (t (error "%s is not a formula file" src-file
))))
2421 ;; Enter the formula file in to manifest.
2422 (org-odt-create-manifest-file-entry "text/xml" target-file
)
2427 (defun org-odt--render-image/formula
(cfg-key href width height
&optional
2428 captions user-frame-params
2429 &rest title-and-desc
)
2430 (let* ((frame-cfg-alist
2431 ;; Each element of this alist is of the form (CFG-HANDLE
2432 ;; INNER-FRAME-PARAMS OUTER-FRAME-PARAMS).
2434 ;; CFG-HANDLE is the key to the alist.
2436 ;; INNER-FRAME-PARAMS and OUTER-FRAME-PARAMS specify the
2437 ;; frame params for INNER-FRAME and OUTER-FRAME
2438 ;; respectively. See below.
2440 ;; Configurations that are meant to be applied to
2441 ;; non-captioned image/formula specifies no
2442 ;; OUTER-FRAME-PARAMS.
2446 ;; INNER-FRAME :: Frame that directly surrounds an
2449 ;; OUTER-FRAME :: Frame that encloses the INNER-FRAME. This
2450 ;; frame also contains the caption, if any.
2452 ;; FRAME-PARAMS :: List of the form (FRAME-STYLE-NAME
2453 ;; FRAME-ATTRIBUTES FRAME-ANCHOR). Note
2454 ;; that these are the last three arguments
2455 ;; to `org-odt--frame'.
2457 ;; Note that an un-captioned image/formula requires just an
2458 ;; INNER-FRAME, while a captioned image/formula requires
2459 ;; both an INNER and an OUTER-FRAME.
2460 '(("As-CharImage" ("OrgInlineImage" nil
"as-char"))
2461 ("ParagraphImage" ("OrgDisplayImage" nil
"paragraph"))
2462 ("PageImage" ("OrgPageImage" nil
"page"))
2463 ("CaptionedAs-CharImage"
2464 ("OrgCaptionedImage"
2465 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
2466 ("OrgInlineImage" nil
"as-char"))
2467 ("CaptionedParagraphImage"
2468 ("OrgCaptionedImage"
2469 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
2470 ("OrgImageCaptionFrame" nil
"paragraph"))
2471 ("CaptionedPageImage"
2472 ("OrgCaptionedImage"
2473 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
2474 ("OrgPageImageCaptionFrame" nil
"page"))
2475 ("InlineFormula" ("OrgInlineFormula" nil
"as-char"))
2476 ("DisplayFormula" ("OrgDisplayFormula" nil
"as-char"))
2477 ("CaptionedDisplayFormula"
2478 ("OrgCaptionedFormula" nil
"paragraph")
2479 ("OrgFormulaCaptionFrame" nil
"paragraph"))))
2480 (caption (car captions
)) (short-caption (cdr captions
))
2481 ;; Retrieve inner and outer frame params, from configuration.
2482 (frame-cfg (assoc-string cfg-key frame-cfg-alist t
))
2483 (inner (nth 1 frame-cfg
))
2484 (outer (nth 2 frame-cfg
))
2485 ;; User-specified frame params (from #+ATTR_ODT spec)
2486 (user user-frame-params
)
2487 (--merge-frame-params (function
2488 (lambda (default user
)
2489 "Merge default and user frame params."
2490 (if (not user
) default
2491 (assert (= (length default
) 3))
2492 (assert (= (length user
) 3))
2495 collect
(or u d
)))))))
2497 ;; Case 1: Image/Formula has no caption.
2498 ;; There is only one frame, one that surrounds the image
2501 ;; Merge user frame params with that from configuration.
2502 (setq inner
(funcall --merge-frame-params inner user
))
2503 (apply 'org-odt--frame href width height
2504 (append inner title-and-desc
)))
2505 ;; Case 2: Image/Formula is captioned or labeled.
2506 ;; There are two frames: The inner one surrounds the
2507 ;; image or formula. The outer one contains the
2508 ;; caption/sequence number.
2510 ;; Merge user frame params with outer frame params.
2511 (setq outer
(funcall --merge-frame-params outer user
))
2512 ;; Short caption, if specified, goes as part of inner frame.
2513 (setq inner
(let ((frame-params (copy-sequence inner
)))
2514 (setcar (cdr frame-params
)
2518 (format " draw:name=\"%s\" " short-caption
))))
2520 (apply 'org-odt--textbox
2521 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2524 (apply 'org-odt--frame href width height
2525 (append inner title-and-desc
))
2527 width height outer
)))))
2529 (defun org-odt--enumerable-p (element _info
)
2530 ;; Element should have a caption or label.
2531 (or (org-element-property :caption element
)
2532 (org-element-property :name element
)))
2534 (defun org-odt--enumerable-image-p (element info
)
2535 (org-odt--standalone-link-p
2537 ;; Paragraph should have a caption or label. It SHOULD NOT be a
2538 ;; replacement element. (i.e., It SHOULD NOT be a result of LaTeX
2541 (and (not (org-element-property :replaces p
))
2542 (or (org-element-property :caption p
)
2543 (org-element-property :name p
))))
2544 ;; Link should point to an image file.
2546 (assert (eq (org-element-type l
) 'link
))
2547 (org-export-inline-image-p l
(plist-get info
:odt-inline-image-rules
)))))
2549 (defun org-odt--enumerable-latex-image-p (element info
)
2550 (org-odt--standalone-link-p
2552 ;; Paragraph should have a caption or label. It SHOULD also be a
2553 ;; replacement element. (i.e., It SHOULD be a result of LaTeX
2556 (and (org-element-property :replaces p
)
2557 (or (org-element-property :caption p
)
2558 (org-element-property :name p
))))
2559 ;; Link should point to an image file.
2561 (assert (eq (org-element-type l
) 'link
))
2562 (org-export-inline-image-p l
(plist-get info
:odt-inline-image-rules
)))))
2564 (defun org-odt--enumerable-formula-p (element info
)
2565 (org-odt--standalone-link-p
2567 ;; Paragraph should have a caption or label.
2569 (or (org-element-property :caption p
)
2570 (org-element-property :name p
)))
2571 ;; Link should point to a MathML or ODF file.
2573 (assert (eq (org-element-type l
) 'link
))
2574 (org-export-inline-image-p l
(plist-get info
:odt-inline-formula-rules
)))))
2576 (defun org-odt--standalone-link-p (element _info
&optional
2579 "Test if ELEMENT is a standalone link for the purpose ODT export.
2580 INFO is a plist holding contextual information.
2582 Return non-nil, if ELEMENT is of type paragraph satisfying
2583 PARAGRAPH-PREDICATE and its sole content, save for whitespaces,
2584 is a link that satisfies LINK-PREDICATE.
2586 Return non-nil, if ELEMENT is of type link satisfying
2587 LINK-PREDICATE and its containing paragraph satisfies
2588 PARAGRAPH-PREDICATE in addition to having no other content save for
2589 leading and trailing whitespaces.
2591 Return nil, otherwise."
2592 (let ((p (case (org-element-type element
)
2594 (link (and (or (not link-predicate
)
2595 (funcall link-predicate element
))
2596 (org-export-get-parent element
)))
2598 (when (and p
(eq (org-element-type p
) 'paragraph
))
2599 (when (or (not paragraph-predicate
)
2600 (funcall paragraph-predicate p
))
2601 (let ((contents (org-element-contents p
)))
2602 (loop for x in contents
2603 with inline-image-count
= 0
2604 always
(case (org-element-type x
)
2606 (not (org-string-nw-p x
)))
2608 (and (or (not link-predicate
)
2609 (funcall link-predicate x
))
2610 (= (incf inline-image-count
) 1)))
2613 (defun org-odt-link--infer-description (destination info
)
2614 ;; DESTINATION is a headline or an element (like paragraph,
2615 ;; verse-block etc) to which a "#+NAME: label" can be attached.
2617 ;; Note that labels that are attached to captioned entities - inline
2618 ;; images, math formulae and tables - get resolved as part of
2619 ;; `org-odt-format-label' and `org-odt--enumerate'.
2621 ;; Create a cross-reference to DESTINATION but make best-efforts to
2622 ;; create a *meaningful* description. Check item numbers, section
2623 ;; number and section title in that order.
2625 ;; NOTE: Counterpart of `org-export-get-ordinal'.
2626 ;; FIXME: Handle footnote-definition footnote-reference?
2627 (let* ((genealogy (org-element-lineage destination
))
2628 (data (reverse genealogy
))
2629 (label (let ((type (org-element-type destination
)))
2630 (if (memq type
'(headline target
))
2631 (org-export-get-reference destination info
)
2632 (error "FIXME: Unable to resolve %S" destination
)))))
2634 (let* ( ;; Locate top-level list.
2637 when
(eq (org-element-type (car x
)) 'plain-list
)
2639 ;; Get list item nos.
2641 (loop for
(plain-list item . rest
) on top-level-list by
#'cddr
2642 until
(not (eq (org-element-type plain-list
) 'plain-list
))
2643 collect
(when (eq (org-element-property :type
2646 (1+ (length (org-export-get-previous-element
2648 ;; Locate top-most listified headline.
2649 (listified-headlines
2651 when
(and (eq (org-element-type (car x
)) 'headline
)
2652 (org-export-low-level-p (car x
) info
))
2654 ;; Get listified headline numbers.
2655 (listified-headline-nos
2656 (loop for el in listified-headlines
2657 when
(eq (org-element-type el
) 'headline
)
2658 collect
(when (org-export-numbered-headline-p el info
)
2659 (1+ (length (org-export-get-previous-element
2661 ;; Combine item numbers from both the listified headlines and
2662 ;; regular list items.
2664 ;; Case 1: Check if all the parents of list item are numbered.
2665 ;; If yes, link to the item proper.
2666 (let ((item-numbers (append listified-headline-nos item-numbers
)))
2667 (when (and item-numbers
(not (memq nil item-numbers
)))
2668 (format "<text:bookmark-ref text:reference-format=\"number-all-superior\" text:ref-name=\"%s\">%s</text:bookmark-ref>"
2670 (mapconcat (lambda (n) (if (not n
) " "
2671 (concat (number-to-string n
) ".")))
2672 item-numbers
"")))))
2673 ;; Case 2: Locate a regular and numbered headline in the
2674 ;; hierarchy. Display its section number.
2677 ;; Test if destination is a numbered headline.
2678 (org-export-numbered-headline-p destination info
)
2679 (loop for el in
(cons destination genealogy
)
2680 when
(and (eq (org-element-type el
) 'headline
)
2681 (not (org-export-low-level-p el info
))
2682 (org-export-numbered-headline-p el info
))
2686 (format "<text:bookmark-ref text:reference-format=\"chapter\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
2688 (mapconcat 'number-to-string
(org-export-get-headline-number
2689 headline info
) "."))))
2690 ;; Case 4: Locate a regular headline in the hierarchy. Display
2692 (let ((headline (loop for el in
(cons destination genealogy
)
2693 when
(and (eq (org-element-type el
) 'headline
)
2694 (not (org-export-low-level-p el info
)))
2698 (format "<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
2700 (let ((title (org-element-property :title headline
)))
2701 (org-export-data title info
)))))
2704 (defun org-odt-link (link desc info
)
2705 "Transcode a LINK object from Org to ODT.
2707 DESC is the description part of the link, or the empty string.
2708 INFO is a plist holding contextual information. See
2710 (let* ((type (org-element-property :type link
))
2711 (raw-path (org-element-property :path link
))
2712 ;; Ensure DESC really exists, or set it to nil.
2713 (desc (and (not (string= desc
"")) desc
))
2714 (imagep (org-export-inline-image-p
2715 link
(plist-get info
:odt-inline-image-rules
)))
2717 ((member type
'("http" "https" "ftp" "mailto"))
2718 (concat type
":" raw-path
))
2719 ((string= type
"file") (org-export-file-uri raw-path
))
2721 ;; Convert & to & for correct XML representation
2722 (path (replace-regexp-in-string "&" "&" path
)))
2724 ;; Link type is handled by a special function.
2725 ((org-export-custom-protocol-maybe link desc
'odt
))
2727 ((and (not desc
) imagep
) (org-odt-link--inline-image link info
))
2730 (org-export-inline-image-p
2731 link
(plist-get info
:odt-inline-formula-rules
)))
2732 (org-odt-link--inline-formula link info
))
2733 ;; Radio target: Transcode target's contents and use them as
2734 ;; link's description.
2735 ((string= type
"radio")
2736 (let ((destination (org-export-resolve-radio-link link info
)))
2737 (if (not destination
) desc
2739 "<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
2740 (org-export-get-reference destination info
)
2742 ;; Links pointing to a headline: Find destination and build
2743 ;; appropriate referencing command.
2744 ((member type
'("custom-id" "fuzzy" "id"))
2745 (let ((destination (if (string= type
"fuzzy")
2746 (org-export-resolve-fuzzy-link link info
)
2747 (org-export-resolve-id-link link info
))))
2748 (case (org-element-type destination
)
2749 ;; Fuzzy link points to a headline. If there's
2750 ;; a description, create a hyperlink. Otherwise, try to
2751 ;; provide a meaningful description.
2753 (if (not desc
) (org-odt-link--infer-description destination info
)
2755 (or (and (string= type
"custom-id")
2756 (org-element-property :CUSTOM_ID destination
))
2757 (org-export-get-reference destination info
))))
2759 "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
2761 ;; Fuzzy link points to a target. If there's a description,
2762 ;; create a hyperlink. Otherwise, try to provide
2763 ;; a meaningful description.
2765 (format "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
2766 (org-export-get-reference destination info
)
2767 (or desc
(org-export-get-ordinal destination info
))))
2768 ;; Fuzzy link points to some element (e.g., an inline image,
2769 ;; a math formula or a table).
2771 (let ((label-reference
2773 (org-odt-format-label destination info
'reference
))))
2775 ((not label-reference
)
2776 (org-odt-link--infer-description destination info
))
2777 ;; LINK has no description. Create
2778 ;; a cross-reference showing entity's sequence
2780 ((not desc
) label-reference
)
2781 ;; LINK has description. Insert a hyperlink with
2782 ;; user-provided description.
2785 "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
2786 (org-export-get-reference destination info
)
2788 ;; Coderef: replace link with the reference name or the
2789 ;; equivalent line number.
2790 ((string= type
"coderef")
2791 (let* ((line-no (format "%d" (org-export-resolve-coderef path info
)))
2792 (href (concat "coderef-" path
)))
2794 (org-export-get-coderef-format path desc
)
2796 "<text:bookmark-ref text:reference-format=\"number\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
2798 ;; External link with a description part.
2800 (let ((link-contents (org-element-contents link
)))
2801 ;; Check if description is a link to an inline image.
2802 (if (and (not (cdr link-contents
))
2803 (let ((desc-element (car link-contents
)))
2804 (and (eq (org-element-type desc-element
) 'link
)
2805 (org-export-inline-image-p
2807 (plist-get info
:odt-inline-image-rules
)))))
2808 ;; Format link as a clickable image.
2809 (format "\n<draw:a xlink:type=\"simple\" xlink:href=\"%s\">\n%s\n</draw:a>"
2811 ;; Otherwise, format it as a regular link.
2812 (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
2814 ;; External link without a description part.
2816 (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
2818 ;; No path, only description. Try to do something useful.
2819 (t (format "<text:span text:style-name=\"%s\">%s</text:span>"
2820 "Emphasis" desc
)))))
2825 (defun org-odt-node-property (node-property _contents _info
)
2826 "Transcode a NODE-PROPERTY element from Org to ODT.
2827 CONTENTS is nil. INFO is a plist holding contextual
2829 (org-odt--encode-plain-text
2831 (org-element-property :key node-property
)
2832 (let ((value (org-element-property :value node-property
)))
2833 (if value
(concat " " value
) "")))))
2837 (defun org-odt--paragraph-style (paragraph)
2838 "Return style of PARAGRAPH.
2839 Style is a symbol among `quoted', `centered' and nil."
2840 (let ((up paragraph
))
2841 (while (and (setq up
(org-element-property :parent up
))
2842 (not (memq (org-element-type up
)
2843 '(center-block quote-block section
)))))
2844 (case (org-element-type up
)
2845 (center-block 'centered
)
2846 (quote-block 'quoted
))))
2848 (defun org-odt--format-paragraph (paragraph contents info default center quote
)
2849 "Format paragraph according to given styles.
2850 PARAGRAPH is a paragraph type element. CONTENTS is the
2851 transcoded contents of that paragraph, as a string. INFO is
2852 a plist used as a communication channel. DEFAULT, CENTER and
2853 QUOTE are, respectively, style to use when paragraph belongs to
2854 no special environment, a center block, or a quote block."
2855 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2856 (case (org-odt--paragraph-style paragraph
)
2859 (otherwise default
))
2860 ;; If PARAGRAPH is a leading paragraph in an item that has
2861 ;; a checkbox, splice checkbox and paragraph contents
2863 (concat (let ((parent (org-element-property :parent paragraph
)))
2864 (and (eq (org-element-type parent
) 'item
)
2865 (not (org-export-get-previous-element paragraph info
))
2866 (org-odt--checkbox parent
)))
2869 (defun org-odt-paragraph (paragraph contents info
)
2870 "Transcode a PARAGRAPH element from Org to ODT.
2871 CONTENTS is the contents of the paragraph, as a string. INFO is
2872 the plist used as a communication channel."
2873 (org-odt--format-paragraph
2874 paragraph contents info
2875 (or (org-element-property :style paragraph
) "Text_20_body")
2882 (defun org-odt-plain-list (plain-list contents _info
)
2883 "Transcode a PLAIN-LIST element from Org to ODT.
2884 CONTENTS is the contents of the list. INFO is a plist holding
2885 contextual information."
2886 (format "\n<text:list text:style-name=\"%s\" %s>\n%s</text:list>"
2887 ;; Choose style based on list type.
2888 (case (org-element-property :type plain-list
)
2889 (ordered "OrgNumberedList")
2890 (unordered "OrgBulletedList")
2891 (descriptive-1 "OrgDescriptionList")
2892 (descriptive-2 "OrgDescriptionList"))
2893 ;; If top-level list, re-start numbering. Otherwise,
2894 ;; continue numbering.
2895 (format "text:continue-numbering=\"%s\""
2896 (let* ((parent (org-export-get-parent plain-list
)))
2897 (if (and parent
(eq (org-element-type parent
) 'item
))
2903 (defun org-odt--encode-tabs-and-spaces (line)
2904 (replace-regexp-in-string
2905 "\\([\t]\\|\\([ ]+\\)\\)"
2908 ((string= s
"\t") "<text:tab/>")
2909 (t (let ((n (length s
)))
2912 ((> n
1) (concat " " (format "<text:s text:c=\"%d\"/>" (1- n
))))
2916 (defun org-odt--encode-plain-text (text &optional no-whitespace-filling
)
2917 (dolist (pair '(("&" .
"&") ("<" .
"<") (">" .
">")))
2918 (setq text
(replace-regexp-in-string (car pair
) (cdr pair
) text t t
)))
2919 (if no-whitespace-filling text
2920 (org-odt--encode-tabs-and-spaces text
)))
2922 (defun org-odt-plain-text (text info
)
2923 "Transcode a TEXT string from Org to ODT.
2924 TEXT is the string to transcode. INFO is a plist holding
2925 contextual information."
2926 (let ((output text
))
2927 ;; Protect &, < and >.
2928 (setq output
(org-odt--encode-plain-text output t
))
2929 ;; Handle smart quotes. Be sure to provide original string since
2930 ;; OUTPUT may have been modified.
2931 (when (plist-get info
:with-smart-quotes
)
2932 (setq output
(org-export-activate-smart-quotes output
:utf-8 info text
)))
2933 ;; Convert special strings.
2934 (when (plist-get info
:with-special-strings
)
2935 (dolist (pair org-odt-special-string-regexps
)
2937 (replace-regexp-in-string (car pair
) (cdr pair
) output t nil
))))
2938 ;; Handle break preservation if required.
2939 (when (plist-get info
:preserve-breaks
)
2940 (setq output
(replace-regexp-in-string
2941 "\\(\\\\\\\\\\)?[ \t]*\n" "<text:line-break/>" output t
)))
2948 (defun org-odt-planning (planning contents info
)
2949 "Transcode a PLANNING element from Org to ODT.
2950 CONTENTS is nil. INFO is a plist used as a communication
2952 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2955 (let ((closed (org-element-property :closed planning
)))
2958 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2959 "OrgClosedKeyword" org-closed-string
)
2960 (org-odt-timestamp closed contents info
))))
2961 (let ((deadline (org-element-property :deadline planning
)))
2964 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2965 "OrgDeadlineKeyword" org-deadline-string
)
2966 (org-odt-timestamp deadline contents info
))))
2967 (let ((scheduled (org-element-property :scheduled planning
)))
2970 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2971 "OrgScheduledKeyword" org-deadline-string
)
2972 (org-odt-timestamp scheduled contents info
)))))))
2975 ;;;; Property Drawer
2977 (defun org-odt-property-drawer (_property-drawer contents _info
)
2978 "Transcode a PROPERTY-DRAWER element from Org to ODT.
2979 CONTENTS holds the contents of the drawer. INFO is a plist
2980 holding contextual information."
2981 (and (org-string-nw-p contents
)
2982 (format "<text:p text:style-name=\"OrgFixedWidthBlock\">%s</text:p>"
2988 (defun org-odt-quote-block (_quote-block contents _info
)
2989 "Transcode a QUOTE-BLOCK element from Org to ODT.
2990 CONTENTS holds the contents of the block. INFO is a plist
2991 holding contextual information."
2997 (defun org-odt-format-section (text style
&optional name
)
2998 (let ((default-name (car (org-odt-add-automatic-style "Section"))))
2999 (format "\n<text:section text:style-name=\"%s\" %s>\n%s\n</text:section>"
3001 (format "text:name=\"%s\"" (or name default-name
))
3005 (defun org-odt-section (_section contents _info
) ; FIXME
3006 "Transcode a SECTION element from Org to ODT.
3007 CONTENTS holds the contents of the section. INFO is a plist
3008 holding contextual information."
3013 (defun org-odt-radio-target (radio-target text info
)
3014 "Transcode a RADIO-TARGET object from Org to ODT.
3015 TEXT is the text of the target. INFO is a plist holding
3016 contextual information."
3017 (org-odt--target text
(org-export-get-reference radio-target info
)))
3022 (defun org-odt-special-block (special-block contents info
)
3023 "Transcode a SPECIAL-BLOCK element from Org to ODT.
3024 CONTENTS holds the contents of the block. INFO is a plist
3025 holding contextual information."
3026 (let ((type (org-element-property :type special-block
))
3027 (attributes (org-export-read-attribute :attr_odt special-block
)))
3030 ((string= type
"annotation")
3031 (let* ((author (or (plist-get attributes
:author
)
3032 (let ((author (plist-get info
:author
)))
3033 (and author
(org-export-data author info
)))))
3034 (date (or (plist-get attributes
:date
)
3035 ;; FIXME: Is `car' right thing to do below?
3036 (car (plist-get info
:date
)))))
3037 (format "\n<text:p>%s</text:p>"
3038 (format "<office:annotation>\n%s\n</office:annotation>"
3041 (format "<dc:creator>%s</dc:creator>" author
))
3043 (format "<dc:date>%s</dc:date>"
3044 (org-odt--format-timestamp date nil
'iso-date
)))
3047 ((string= type
"textbox")
3048 (let ((width (plist-get attributes
:width
))
3049 (height (plist-get attributes
:height
))
3050 (style (plist-get attributes
:style
))
3051 (extra (plist-get attributes
:extra
))
3052 (anchor (plist-get attributes
:anchor
)))
3053 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3054 "Text_20_body" (org-odt--textbox contents width height
3055 style extra anchor
))))
3061 (defun org-odt-hfy-face-to-css (fn)
3062 "Create custom style for face FN.
3063 When FN is the default face, use its foreground and background
3064 properties to create \"OrgSrcBlock\" paragraph style. Otherwise
3065 use its color attribute to create a character style whose name
3066 is obtained from FN. Currently all attributes of FN other than
3069 The style name for a face FN is derived using the following
3070 operations on the face name in that order - de-dash, CamelCase
3071 and prefix with \"OrgSrc\". For example,
3072 `font-lock-function-name-face' is associated with
3073 \"OrgSrcFontLockFunctionNameFace\"."
3074 (let* ((css-list (hfy-face-to-style fn
))
3075 (style-name (concat "OrgSrc"
3077 'capitalize
(split-string
3078 (hfy-face-or-def-to-name fn
) "-")
3080 (color-val (cdr (assoc "color" css-list
)))
3081 (background-color-val (cdr (assoc "background" css-list
)))
3082 (style (and org-odt-create-custom-styles-for-srcblocks
3085 (format org-odt-src-block-paragraph-format
3086 background-color-val color-val
))
3090 <style:style style:name=\"%s\" style:family=\"text\">
3091 <style:text-properties fo:color=\"%s\"/>
3092 </style:style>" style-name color-val
))))))
3093 (cons style-name style
)))
3095 (defun org-odt-htmlfontify-string (line)
3096 (let* ((hfy-html-quote-regex "\\([<\"&> \t]\\)")
3097 (hfy-html-quote-map '(("\"" """)
3102 ("\t" "<text:tab/>")))
3103 (hfy-face-to-css 'org-odt-hfy-face-to-css
)
3104 (hfy-optimizations-1 (copy-sequence hfy-optimizations
))
3105 (hfy-optimizations (cl-pushnew 'body-text-only hfy-optimizations-1
))
3106 (hfy-begin-span-handler
3107 (lambda (style _text-block _text-id _text-begins-block-p
)
3108 (insert (format "<text:span text:style-name=\"%s\">" style
))))
3109 (hfy-end-span-handler (lambda () (insert "</text:span>"))))
3110 (org-no-warnings (htmlfontify-string line
))))
3112 (defun org-odt-do-format-code
3113 (code info
&optional lang refs retain-labels num-start
)
3114 (let* ((lang (or (assoc-default lang org-src-lang-modes
) lang
))
3115 (lang-mode (and lang
(intern (format "%s-mode" lang
))))
3116 (code-lines (org-split-string code
"\n"))
3117 (code-length (length code-lines
))
3118 (use-htmlfontify-p (and (functionp lang-mode
)
3119 (plist-get info
:odt-fontify-srcblocks
)
3120 (require 'htmlfontify nil t
)
3121 (fboundp 'htmlfontify-string
)))
3122 (code (if (not use-htmlfontify-p
) code
3128 (fontifier (if use-htmlfontify-p
'org-odt-htmlfontify-string
3129 'org-odt--encode-plain-text
))
3130 (par-style (if use-htmlfontify-p
"OrgSrcBlock"
3131 "OrgFixedWidthBlock"))
3133 (assert (= code-length
(length (org-split-string code
"\n"))))
3135 (org-export-format-code
3137 (lambda (loc line-num ref
)
3139 (concat par-style
(and (= (incf i
) code-length
) "LastLine")))
3141 (setq loc
(concat loc
(and ref retain-labels
(format " (%s)" ref
))))
3142 (setq loc
(funcall fontifier loc
))
3144 (setq loc
(org-odt--target loc
(concat "coderef-" ref
))))
3146 (setq loc
(format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3148 (if (not line-num
) loc
3149 (format "\n<text:list-item>%s\n</text:list-item>" loc
)))
3152 ((not num-start
) code
)
3155 "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
3156 " text:continue-numbering=\"false\"" code
))
3159 "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
3160 " text:continue-numbering=\"true\"" code
)))))
3162 (defun org-odt-format-code (element info
)
3163 (let* ((lang (org-element-property :language element
))
3164 ;; Extract code and references.
3165 (code-info (org-export-unravel-code element
))
3166 (code (car code-info
))
3167 (refs (cdr code-info
))
3168 ;; Does the src block contain labels?
3169 (retain-labels (org-element-property :retain-labels element
))
3170 ;; Does it have line numbers?
3171 (num-start (case (org-element-property :number-lines element
)
3172 (continued (org-export-get-loc element info
))
3174 (org-odt-do-format-code code info lang refs retain-labels num-start
)))
3176 (defun org-odt-src-block (src-block _contents info
)
3177 "Transcode a SRC-BLOCK element from Org to ODT.
3178 CONTENTS holds the contents of the item. INFO is a plist holding
3179 contextual information."
3180 (let* ((attributes (org-export-read-attribute :attr_odt src-block
))
3181 (caption (car (org-odt-format-label src-block info
'definition
))))
3184 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3186 (let ((--src-block (org-odt-format-code src-block info
)))
3187 (if (not (plist-get attributes
:textbox
)) --src-block
3188 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3190 (org-odt--textbox --src-block nil nil nil
)))))))
3193 ;;;; Statistics Cookie
3195 (defun org-odt-statistics-cookie (statistics-cookie _contents _info
)
3196 "Transcode a STATISTICS-COOKIE object from Org to ODT.
3197 CONTENTS is nil. INFO is a plist holding contextual information."
3198 (let ((cookie-value (org-element-property :value statistics-cookie
)))
3199 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3200 "OrgCode" cookie-value
)))
3205 (defun org-odt-strike-through (_strike-through contents _info
)
3206 "Transcode STRIKE-THROUGH from Org to ODT.
3207 CONTENTS is the text with strike-through markup. INFO is a plist
3208 holding contextual information."
3209 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3210 "Strikethrough" contents
))
3215 (defun org-odt-subscript (_subscript contents _info
)
3216 "Transcode a SUBSCRIPT object from Org to ODT.
3217 CONTENTS is the contents of the object. INFO is a plist holding
3218 contextual information."
3219 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3220 "OrgSubscript" contents
))
3225 (defun org-odt-superscript (_superscript contents _info
)
3226 "Transcode a SUPERSCRIPT object from Org to ODT.
3227 CONTENTS is the contents of the object. INFO is a plist holding
3228 contextual information."
3229 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3230 "OrgSuperscript" contents
))
3235 (defun org-odt-table-style-spec (element info
)
3236 (let* ((table (org-export-get-parent-table element
))
3237 (table-attributes (org-export-read-attribute :attr_odt table
))
3238 (table-style (plist-get table-attributes
:style
)))
3239 (assoc table-style
(plist-get info
:odt-table-styles
))))
3241 (defun org-odt-get-table-cell-styles (table-cell info
)
3242 "Retrieve styles applicable to a table cell.
3243 R and C are (zero-based) row and column numbers of the table
3244 cell. STYLE-SPEC is an entry in `org-odt-table-styles'
3245 applicable to the current table. It is nil if the table is not
3246 associated with any style attributes.
3248 Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
3250 When STYLE-SPEC is nil, style the table cell the conventional way
3251 - choose cell borders based on row and column groupings and
3252 choose paragraph alignment based on `org-col-cookies' text
3254 `org-odt-get-paragraph-style-cookie-for-table-cell'.
3256 When STYLE-SPEC is non-nil, ignore the above cookie and return
3257 styles congruent with the ODF-1.2 specification."
3258 (let* ((table-cell-address (org-export-table-cell-address table-cell info
))
3259 (r (car table-cell-address
)) (c (cdr table-cell-address
))
3260 (style-spec (org-odt-table-style-spec table-cell info
))
3261 (table-dimensions (org-export-table-dimensions
3262 (org-export-get-parent-table table-cell
)
3265 ;; LibreOffice - particularly the Writer - honors neither table
3266 ;; templates nor custom table-cell styles. Inorder to retain
3267 ;; inter-operability with LibreOffice, only automatic styles are
3268 ;; used for styling of table-cells. The current implementation is
3269 ;; congruent with ODF-1.2 specification and hence is
3270 ;; future-compatible.
3272 ;; Additional Note: LibreOffice's AutoFormat facility for tables -
3273 ;; which recognizes as many as 16 different cell types - is much
3274 ;; richer. Unfortunately it is NOT amenable to easy configuration
3276 (let* ((template-name (nth 1 style-spec
))
3277 (cell-style-selectors (nth 2 style-spec
))
3280 ((and (cdr (assoc 'use-first-column-styles cell-style-selectors
))
3281 (= c
0)) "FirstColumn")
3282 ((and (cdr (assoc 'use-last-column-styles cell-style-selectors
))
3283 (= (1+ c
) (cdr table-dimensions
)))
3285 ((and (cdr (assoc 'use-first-row-styles cell-style-selectors
))
3286 (= r
0)) "FirstRow")
3287 ((and (cdr (assoc 'use-last-row-styles cell-style-selectors
))
3288 (= (1+ r
) (car table-dimensions
)))
3290 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors
))
3291 (= (% r
2) 1)) "EvenRow")
3292 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors
))
3293 (= (% r
2) 0)) "OddRow")
3294 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors
))
3295 (= (% c
2) 1)) "EvenColumn")
3296 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors
))
3297 (= (% c
2) 0)) "OddColumn")
3299 (concat template-name cell-type
)))))
3301 (defun org-odt-table-cell (table-cell contents info
)
3302 "Transcode a TABLE-CELL element from Org to ODT.
3303 CONTENTS is nil. INFO is a plist used as a communication
3305 (let* ((table-cell-address (org-export-table-cell-address table-cell info
))
3306 (r (car table-cell-address
))
3307 (c (cdr table-cell-address
))
3308 (horiz-span (or (org-export-table-cell-width table-cell info
) 0))
3309 (table-row (org-export-get-parent table-cell
))
3310 (custom-style-prefix (org-odt-get-table-cell-styles
3314 (and custom-style-prefix
3315 (format "%sTableParagraph" custom-style-prefix
))
3318 ((and (= 1 (org-export-table-row-group table-row info
))
3319 (org-export-table-has-header-p
3320 (org-export-get-parent-table table-row
) info
))
3322 ((let* ((table (org-export-get-parent-table table-cell
))
3323 (table-attrs (org-export-read-attribute :attr_odt table
))
3324 (table-header-columns
3325 (let ((cols (plist-get table-attrs
:header-columns
)))
3326 (and cols
(read cols
)))))
3327 (<= c
(cond ((wholenump table-header-columns
)
3328 (- table-header-columns
1))
3329 (table-header-columns 0)
3332 (t "OrgTableContents"))
3333 (capitalize (symbol-name (org-export-table-cell-alignment
3334 table-cell info
))))))
3337 (and custom-style-prefix
(format "%sTableCell"
3338 custom-style-prefix
))
3341 (when (or (org-export-table-row-starts-rowgroup-p table-row info
)
3343 (when (org-export-table-row-ends-rowgroup-p table-row info
) "B")
3344 (when (and (org-export-table-cell-starts-colgroup-p table-cell info
)
3345 (not (zerop c
)) ) "L"))))
3348 (format " table:style-name=\"%s\"" cell-style-name
)
3349 (and (> horiz-span
0)
3350 (format " table:number-columns-spanned=\"%d\""
3351 (1+ horiz-span
))))))
3352 (unless contents
(setq contents
""))
3354 (assert paragraph-style
)
3355 (format "\n<table:table-cell%s>\n%s\n</table:table-cell>"
3357 (let ((table-cell-contents (org-element-contents table-cell
)))
3358 (if (memq (org-element-type (car table-cell-contents
))
3359 org-element-all-elements
)
3361 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3362 paragraph-style contents
))))
3364 (dotimes (_ horiz-span s
)
3365 (setq s
(concat s
"\n<table:covered-table-cell/>"))))
3371 (defun org-odt-table-row (table-row contents info
)
3372 "Transcode a TABLE-ROW element from Org to ODT.
3373 CONTENTS is the contents of the row. INFO is a plist used as a
3374 communication channel."
3375 ;; Rules are ignored since table separators are deduced from
3376 ;; borders of the current row.
3377 (when (eq (org-element-property :type table-row
) 'standard
)
3378 (let* ((rowgroup-tags
3379 (if (and (= 1 (org-export-table-row-group table-row info
))
3380 (org-export-table-has-header-p
3381 (org-export-get-parent-table table-row
) info
))
3382 ;; If the row belongs to the first rowgroup and the
3383 ;; table has more than one row groups, then this row
3384 ;; belongs to the header row group.
3385 '("\n<table:table-header-rows>" .
"\n</table:table-header-rows>")
3386 ;; Otherwise, it belongs to non-header row group.
3387 '("\n<table:table-rows>" .
"\n</table:table-rows>"))))
3389 ;; Does this row begin a rowgroup?
3390 (when (org-export-table-row-starts-rowgroup-p table-row info
)
3391 (car rowgroup-tags
))
3393 (format "\n<table:table-row>\n%s\n</table:table-row>" contents
)
3394 ;; Does this row end a rowgroup?
3395 (when (org-export-table-row-ends-rowgroup-p table-row info
)
3396 (cdr rowgroup-tags
))))))
3401 (defun org-odt-table-first-row-data-cells (table info
)
3403 (org-element-map table
'table-row
3405 (unless (eq (org-element-property :type row
) 'rule
) row
))
3407 (special-column-p (org-export-table-has-special-column-p table
)))
3408 (if (not special-column-p
) (org-element-contents table-row
)
3409 (cdr (org-element-contents table-row
)))))
3411 (defun org-odt--table (table contents info
)
3412 "Transcode a TABLE element from Org to ODT.
3413 CONTENTS is the contents of the table. INFO is a plist holding
3414 contextual information."
3415 (case (org-element-property :type table
)
3416 ;; Case 1: table.el doesn't support export to OD format. Strip
3417 ;; such tables from export.
3422 "(ox-odt): Found table.el-type table in the source Org file."
3423 " table.el doesn't support export to ODT format."
3424 " Stripping the table from export."))))
3425 ;; Case 2: Native Org tables.
3427 (let* ((captions (org-odt-format-label table info
'definition
))
3428 (caption (car captions
)) (short-caption (cdr captions
))
3429 (attributes (org-export-read-attribute :attr_odt table
))
3430 (custom-table-style (nth 1 (org-odt-table-style-spec table info
)))
3432 (lambda (table info
)
3433 (let* ((table-style (or custom-table-style
"OrgTable"))
3434 (column-style (format "%sColumn" table-style
)))
3436 (lambda (table-cell)
3437 (let ((width (1+ (or (org-export-table-cell-width
3438 table-cell info
) 0)))
3440 "\n<table:table-column table:style-name=\"%s\"/>"
3443 (dotimes (_ width out
) (setq out
(concat s out
)))))
3444 (org-odt-table-first-row-data-cells table info
) "\n")))))
3448 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3451 (let* ((automatic-name
3452 (org-odt-add-automatic-style "Table" attributes
)))
3454 "\n<table:table table:style-name=\"%s\"%s>"
3455 (or custom-table-style
(cdr automatic-name
) "OrgTable")
3456 (concat (when short-caption
3457 (format " table:name=\"%s\"" short-caption
)))))
3458 ;; column specification.
3459 (funcall table-column-specs table info
)
3463 "</table:table>")))))
3465 (defun org-odt-table (table contents info
)
3466 "Transcode a TABLE element from Org to ODT.
3467 CONTENTS is the contents of the table. INFO is a plist holding
3468 contextual information.
3470 Use `org-odt--table' to typeset the table. Handle details
3471 pertaining to indentation here."
3472 (let* ((--element-preceded-by-table-p
3474 (lambda (element info
)
3475 (loop for el in
(org-export-get-previous-element element info t
)
3476 thereis
(eq (org-element-type el
) 'table
)))))
3477 (--walk-list-genealogy-and-collect-tags
3479 (lambda (table info
)
3480 (let* ((genealogy (org-element-lineage table
))
3482 (when (eq (org-element-type (car genealogy
)) 'item
)
3483 (loop for el in genealogy
3484 when
(memq (org-element-type el
)
3489 (loop for el in genealogy
3490 when
(and (eq (org-element-type el
) 'headline
)
3491 (org-export-low-level-p el info
))
3495 (org-element-contents
3496 (org-export-get-parent el
)))))))
3499 ;; Handle list genealogy.
3500 (loop for el in list-genealogy collect
3501 (case (org-element-type el
)
3503 (setq parent-list el
)
3504 (cons "</text:list>"
3505 (format "\n<text:list text:style-name=\"%s\" %s>"
3506 (case (org-element-property :type el
)
3507 (ordered "OrgNumberedList")
3508 (unordered "OrgBulletedList")
3509 (descriptive-1 "OrgDescriptionList")
3510 (descriptive-2 "OrgDescriptionList"))
3511 "text:continue-numbering=\"true\"")))
3515 (if (funcall --element-preceded-by-table-p table info
)
3516 '("</text:list-header>" .
"<text:list-header>")
3517 '("</text:list-item>" .
"<text:list-header>")))
3518 ((funcall --element-preceded-by-table-p
3520 '("</text:list-header>" .
"<text:list-header>"))
3521 (t '("</text:list-item>" .
"<text:list-item>"))))))
3522 ;; Handle low-level headlines.
3523 (loop for el in llh-genealogy
3524 with step
= 'item collect
3527 (setq step
'item
) ; Flip-flop
3528 (setq parent-list el
)
3529 (cons "</text:list>"
3530 (format "\n<text:list text:style-name=\"%s\" %s>"
3531 (if (org-export-numbered-headline-p
3535 "text:continue-numbering=\"true\"")))
3537 (setq step
'plain-list
) ; Flip-flop
3540 (if (funcall --element-preceded-by-table-p table info
)
3541 '("</text:list-header>" .
"<text:list-header>")
3542 '("</text:list-item>" .
"<text:list-header>")))
3543 ((let ((section?
(org-export-get-previous-element
3546 (eq (org-element-type section?
) 'section
)
3547 (assq 'table
(org-element-contents section?
))))
3548 '("</text:list-header>" .
"<text:list-header>"))
3550 '("</text:list-item>" .
"<text:list-item>")))))))))))
3551 (close-open-tags (funcall --walk-list-genealogy-and-collect-tags
3553 ;; OpenDocument schema does not permit table to occur within a
3556 ;; One solution - the easiest and lightweight, in terms of
3557 ;; implementation - is to put the table in an indented text box
3558 ;; and make the text box part of the list-item. Unfortunately if
3559 ;; the table is big and spans multiple pages, the text box could
3560 ;; overflow. In this case, the following attribute will come
3563 ;; ,---- From OpenDocument-v1.1.pdf
3564 ;; | 15.27.28 Overflow behavior
3566 ;; | For text boxes contained within text document, the
3567 ;; | style:overflow-behavior property specifies the behavior of text
3568 ;; | boxes where the containing text does not fit into the text
3571 ;; | If the attribute's value is clip, the text that does not fit
3572 ;; | into the text box is not displayed.
3574 ;; | If the attribute value is auto-create-new-frame, a new frame
3575 ;; | will be created on the next page, with the same position and
3576 ;; | dimensions of the original frame.
3578 ;; | If the style:overflow-behavior property's value is
3579 ;; | auto-create-new-frame and the text box has a minimum width or
3580 ;; | height specified, then the text box will grow until the page
3581 ;; | bounds are reached before a new frame is created.
3584 ;; Unfortunately, LibreOffice-3.4.6 doesn't honor
3585 ;; auto-create-new-frame property and always resorts to clipping
3586 ;; the text box. This results in table being truncated.
3588 ;; So we solve the problem the hard (and fun) way using list
3591 ;; The problem only becomes more interesting if you take in to
3592 ;; account the following facts:
3594 ;; - Description lists are simulated as plain lists.
3595 ;; - Low-level headlines can be listified.
3596 ;; - In Org-mode, a table can occur not only as a regular list
3597 ;; item, but also within description lists and low-level
3600 ;; See `org-odt-translate-description-lists' and
3601 ;; `org-odt-translate-low-level-headlines' for how this is
3605 ;; Discontinue the list.
3606 (mapconcat 'car close-open-tags
"\n")
3607 ;; Put the table in an indented section.
3608 (let* ((table (org-odt--table table contents info
))
3609 (level (/ (length (mapcar 'car close-open-tags
)) 2))
3610 (style (format "OrgIndentedSection-Level-%d" level
)))
3611 (when table
(org-odt-format-section table style
)))
3612 ;; Continue the list.
3613 (mapconcat 'cdr
(nreverse close-open-tags
) "\n"))))
3618 (defun org-odt-target (target _contents info
)
3619 "Transcode a TARGET object from Org to ODT.
3620 CONTENTS is nil. INFO is a plist holding contextual
3622 (org-odt--target "" (org-export-get-reference target info
)))
3627 (defun org-odt-timestamp (timestamp _contents info
)
3628 "Transcode a TIMESTAMP object from Org to ODT.
3629 CONTENTS is nil. INFO is a plist used as a communication
3631 (let ((type (org-element-property :type timestamp
)))
3632 (if (not (plist-get info
:odt-use-date-fields
))
3633 (let ((value (org-odt-plain-text
3634 (org-timestamp-translate timestamp
) info
)))
3635 (case (org-element-property :type timestamp
)
3636 ((active active-range
)
3637 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3638 "OrgActiveTimestamp" value
))
3639 ((inactive inactive-range
)
3640 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3641 "OrgInactiveTimestamp" value
))
3645 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3646 "OrgActiveTimestamp"
3647 (format "<%s>" (org-odt--format-timestamp timestamp
))))
3649 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3650 "OrgInactiveTimestamp"
3651 (format "[%s]" (org-odt--format-timestamp timestamp
))))
3653 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3654 "OrgActiveTimestamp"
3655 (format "<%s>–<%s>"
3656 (org-odt--format-timestamp timestamp
)
3657 (org-odt--format-timestamp timestamp
'end
))))
3659 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3660 "OrgInactiveTimestamp"
3661 (format "[%s]–[%s]"
3662 (org-odt--format-timestamp timestamp
)
3663 (org-odt--format-timestamp timestamp
'end
))))
3665 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3667 (org-odt-plain-text (org-timestamp-translate timestamp
)
3673 (defun org-odt-underline (_underline contents _info
)
3674 "Transcode UNDERLINE from Org to ODT.
3675 CONTENTS is the text with underline markup. INFO is a plist
3676 holding contextual information."
3677 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3678 "Underline" contents
))
3683 (defun org-odt-verbatim (verbatim _contents _info
)
3684 "Transcode a VERBATIM object from Org to ODT.
3685 CONTENTS is nil. INFO is a plist used as a communication
3687 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3688 "OrgCode" (org-odt--encode-plain-text
3689 (org-element-property :value verbatim
))))
3694 (defun org-odt-verse-block (_verse-block contents _info
)
3695 "Transcode a VERSE-BLOCK element from Org to ODT.
3696 CONTENTS is verse block contents. INFO is a plist holding
3697 contextual information."
3698 ;; Add line breaks to each line of verse.
3699 (setq contents
(replace-regexp-in-string
3700 "\\(<text:line-break/>\\)?[ \t]*\n"
3701 "<text:line-break/>" contents
))
3702 ;; Replace tabs and spaces.
3703 (setq contents
(org-odt--encode-tabs-and-spaces contents
))
3704 ;; Surround it in a verse environment.
3705 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3706 "OrgVerse" contents
))
3712 ;;;; LaTeX fragments
3714 (defun org-odt--translate-latex-fragments (tree _backend info
)
3715 (let ((processing-type (plist-get info
:with-latex
))
3717 ;; Normalize processing-type to one of dvipng, mathml or verbatim.
3718 ;; If the desired converter is not available, force verbatim
3720 (case processing-type
3722 (if (and (fboundp 'org-format-latex-mathml-available-p
)
3723 (org-format-latex-mathml-available-p))
3724 (setq processing-type
'mathml
)
3725 (message "LaTeX to MathML converter not available.")
3726 (setq processing-type
'verbatim
)))
3727 ((dvipng imagemagick
)
3728 (unless (and (org-check-external-command "latex" "" t
)
3729 (org-check-external-command
3730 (if (eq processing-type
'dvipng
) "dvipng" "convert") "" t
))
3731 (message "LaTeX to PNG converter not available.")
3732 (setq processing-type
'verbatim
)))
3734 (message "Unknown LaTeX option. Forcing verbatim.")
3735 (setq processing-type
'verbatim
)))
3737 ;; Store normalized value for later use.
3738 (when (plist-get info
:with-latex
)
3739 (plist-put info
:with-latex processing-type
))
3740 (message "Formatting LaTeX using %s" processing-type
)
3742 ;; Convert `latex-fragment's and `latex-environment's.
3743 (when (memq processing-type
'(mathml dvipng imagemagick
))
3744 (org-element-map tree
'(latex-fragment latex-environment
)
3747 (let* ((latex-frag (org-element-property :value latex-
*))
3748 (input-file (plist-get info
:input-file
))
3749 (cache-dir (file-name-directory input-file
))
3750 (cache-subdir (concat
3751 (case processing-type
3752 ((dvipng imagemagick
) "ltxpng/")
3753 (mathml "ltxmathml/"))
3754 (file-name-sans-extension
3755 (file-name-nondirectory input-file
))))
3757 (case processing-type
3758 ((dvipng imagemagick
)
3759 (format "Creating LaTeX Image %d..." count
))
3760 (mathml (format "Creating MathML snippet %d..." count
))))
3761 ;; Get an Org-style link to PNG image or the MathML
3764 (let ((link (with-temp-buffer
3766 (org-format-latex cache-subdir cache-dir
3768 nil processing-type
)
3769 (buffer-substring-no-properties
3770 (point-min) (point-max)))))
3771 (if (org-string-match-p "file:\\([^]]*\\)" link
) link
3772 (message "LaTeX Conversion failed.")
3775 ;; Conversion succeeded. Parse above Org-style link to
3779 (org-element-parse-secondary-string org-link
'(link))
3780 'link
#'identity info t
))
3782 (case (org-element-type latex-
*)
3783 ;; Case 1: LaTeX environment. Mimic
3784 ;; a "standalone image or formula" by
3785 ;; enclosing the `link' in a `paragraph'.
3786 ;; Copy over original attributes, captions to
3787 ;; the enclosing paragraph.
3789 (org-element-adopt-elements
3791 (list :style
"OrgFormula"
3793 (org-element-property :name latex-
*)
3795 (org-element-property :caption latex-
*)))
3797 ;; Case 2: LaTeX fragment. No special action.
3798 (latex-fragment link
))))
3799 ;; Note down the object that link replaces.
3800 (org-element-put-property replacement
:replaces
3801 (list (org-element-type latex-
*)
3802 (list :value latex-frag
)))
3803 ;; Restore blank after initial element or object.
3804 (org-element-put-property
3805 replacement
:post-blank
3806 (org-element-property :post-blank latex-
*))
3808 (org-element-set-element latex-
* replacement
)))))
3813 ;;;; Description lists
3815 ;; This translator is necessary to handle indented tables in a uniform
3816 ;; manner. See comment in `org-odt--table'.
3818 (defun org-odt--translate-description-lists (tree _backend info
)
3819 ;; OpenDocument has no notion of a description list. So simulate it
3820 ;; using plain lists. Description lists in the exported document
3821 ;; are typeset in the same manner as they are in a typical HTML
3824 ;; Specifically, a description list like this:
3827 ;; | - term-1 :: definition-1
3828 ;; | - term-2 :: definition-2
3831 ;; gets translated in to the following form:
3840 ;; Further effect is achieved by fixing the OD styles as below:
3842 ;; 1. Set the :type property of the simulated lists to
3843 ;; `descriptive-1' and `descriptive-2'. Map these to list-styles
3844 ;; that has *no* bullets whatsoever.
3846 ;; 2. The paragraph containing the definition term is styled to be
3849 (org-element-map tree
'plain-list
3851 (when (equal (org-element-property :type el
) 'descriptive
)
3852 (org-element-set-element
3854 (apply 'org-element-adopt-elements
3855 (list 'plain-list
(list :type
'descriptive-1
))
3858 (org-element-adopt-elements
3859 (list 'item
(list :checkbox
(org-element-property
3861 (list 'paragraph
(list :style
"Text_20_body_20_bold")
3862 (or (org-element-property :tag item
) "(no term)"))
3863 (org-element-adopt-elements
3864 (list 'plain-list
(list :type
'descriptive-2
))
3865 (apply 'org-element-adopt-elements
3867 (org-element-contents item
)))))
3868 (org-element-contents el
)))))
3875 ;; Lists that are marked with attribute `:list-table' are called as
3876 ;; list tables. They will be rendered as a table within the exported
3879 ;; Consider an example. The following list table
3881 ;; #+attr_odt :list-table t
3891 ;; will be exported as though it were an Org table like the one show
3894 ;; | Row 1 | 1.1 | 1.2 | 1.3 |
3895 ;; | Row 2 | 2.1 | 2.2 | 2.3 |
3897 ;; Note that org-tables are NOT multi-line and each line is mapped to
3898 ;; a unique row in the exported document. So if an exported table
3899 ;; needs to contain a single paragraph (with copious text) it needs to
3900 ;; be typed up in a single line. Editing such long lines using the
3901 ;; table editor will be a cumbersome task. Furthermore inclusion of
3902 ;; multi-paragraph text in a table cell is well-nigh impossible.
3904 ;; A LIST-TABLE circumvents above problems.
3906 ;; Note that in the example above the list items could be paragraphs
3907 ;; themselves and the list can be arbitrarily deep.
3909 ;; Inspired by following thread:
3910 ;; https://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01101.html
3912 ;; Translate lists to tables
3914 (defun org-odt--translate-list-tables (tree _backend info
)
3915 (org-element-map tree
'plain-list
3917 (when (org-export-read-attribute :attr_odt l1-list
:list-table
)
3918 ;; Replace list with table.
3919 (org-element-set-element
3921 ;; Build replacement table.
3922 (apply 'org-element-adopt-elements
3923 (list 'table
'(:type org
:attr_odt
(":style \"GriddedTable\"")))
3924 (org-element-map l1-list
'item
3926 (let* ((l1-item-contents (org-element-contents l1-item
))
3927 l1-item-leading-text l2-list
)
3928 ;; Remove Level-2 list from the Level-item. It
3929 ;; will be subsequently attached as table-cells.
3930 (let ((cur l1-item-contents
) prev
)
3931 (while (and cur
(not (eq (org-element-type (car cur
))
3934 (setq cur
(cdr cur
)))
3937 (setq l2-list
(car cur
)))
3938 (setq l1-item-leading-text l1-item-contents
))
3939 ;; Level-1 items start a table row.
3940 (apply 'org-element-adopt-elements
3941 (list 'table-row
(list :type
'standard
))
3942 ;; Leading text of level-1 item define
3943 ;; the first table-cell.
3944 (apply 'org-element-adopt-elements
3945 (list 'table-cell nil
)
3946 l1-item-leading-text
)
3947 ;; Level-2 items define subsequent
3948 ;; table-cells of the row.
3949 (org-element-map l2-list
'item
3951 (apply 'org-element-adopt-elements
3952 (list 'table-cell nil
)
3953 (org-element-contents l2-item
)))
3961 ;;; Interactive functions
3963 (defun org-odt-create-manifest-file-entry (&rest args
)
3964 (push args org-odt-manifest-file-entries
))
3966 (defun org-odt-write-manifest-file ()
3967 (make-directory (concat org-odt-zip-dir
"META-INF"))
3968 (let ((manifest-file (concat org-odt-zip-dir
"META-INF/manifest.xml")))
3969 (with-current-buffer
3970 (let ((nxml-auto-insert-xml-declaration-flag nil
))
3971 (find-file-noselect manifest-file t
))
3973 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
3974 <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
3975 (dolist (file-entry org-odt-manifest-file-entries
)
3976 (let* ((version (nth 2 file-entry
))
3977 (extra (if (not version
) ""
3978 (format " manifest:version=\"%s\"" version
))))
3980 (format org-odt-manifest-file-entry-tag
3981 (nth 0 file-entry
) (nth 1 file-entry
) extra
))))
3982 (insert "\n</manifest:manifest>"))))
3984 (defmacro org-odt--export-wrap
(out-file &rest body
)
3985 `(let* ((--out-file ,out-file
)
3986 (out-file-type (file-name-extension --out-file
))
3987 (org-odt-xml-files '("META-INF/manifest.xml" "content.xml"
3988 "meta.xml" "styles.xml"))
3989 ;; Initialize temporary workarea. All files that end up in
3990 ;; the exported document get parked/created here.
3991 (org-odt-zip-dir (file-name-as-directory
3992 (make-temp-file (format "%s-" out-file-type
) t
)))
3993 (org-odt-manifest-file-entries nil
)
3994 (--cleanup-xml-buffers
3996 ;; Kill all XML buffers.
3997 (dolist (file org-odt-xml-files
)
3998 (let ((buf (find-buffer-visiting
3999 (concat org-odt-zip-dir file
))))
4001 (with-current-buffer buf
4002 (set-buffer-modified-p nil
)
4003 (kill-buffer buf
)))))
4004 ;; Delete temporary directory and also other embedded
4005 ;; files that get copied there.
4006 (delete-directory org-odt-zip-dir t
))))
4009 (unless (executable-find "zip")
4010 ;; Not at all OSes ship with zip by default
4011 (error "Executable \"zip\" needed for creating OpenDocument files"))
4012 ;; Do export. This creates a bunch of xml files ready to be
4013 ;; saved and zipped.
4015 ;; Create a manifest entry for content.xml.
4016 (org-odt-create-manifest-file-entry "text/xml" "content.xml")
4017 ;; Write mimetype file
4019 '(("odt" .
"application/vnd.oasis.opendocument.text")
4020 ("odf" .
"application/vnd.oasis.opendocument.formula")))
4021 (mimetype (cdr (assoc-string out-file-type mimetypes t
))))
4023 (error "Unknown OpenDocument backend %S" out-file-type
))
4024 (write-region mimetype nil
(concat org-odt-zip-dir
"mimetype"))
4025 (org-odt-create-manifest-file-entry mimetype
"/" "1.2"))
4026 ;; Write out the manifest entries before zipping
4027 (org-odt-write-manifest-file)
4028 ;; Save all XML files.
4029 (dolist (file org-odt-xml-files
)
4030 (let ((buf (find-buffer-visiting
4031 (concat org-odt-zip-dir file
))))
4033 (with-current-buffer buf
4034 ;; Prettify output if needed.
4035 (when org-odt-prettify-xml
4036 (indent-region (point-min) (point-max)))
4039 (let* ((target --out-file
)
4040 (target-name (file-name-nondirectory target
))
4041 (cmds `(("zip" "-mX0" ,target-name
"mimetype")
4042 ("zip" "-rmTq" ,target-name
"."))))
4043 ;; If a file with same name as the desired output file
4044 ;; exists, remove it.
4045 (when (file-exists-p target
)
4046 (delete-file target
))
4047 ;; Zip up the xml files.
4048 (let ((coding-system-for-write 'no-conversion
) exitcode err-string
)
4049 (message "Creating ODT file...")
4050 ;; Switch temporarily to content.xml. This way Zip
4051 ;; process will inherit `org-odt-zip-dir' as the current
4053 (with-current-buffer
4054 (find-file-noselect (concat org-odt-zip-dir
"content.xml") t
)
4056 (message "Running %s" (mapconcat 'identity cmd
" "))
4058 (with-output-to-string
4060 (apply 'call-process
(car cmd
)
4061 nil standard-output nil
(cdr cmd
)))))
4062 (or (zerop exitcode
)
4063 (error (concat "Unable to create OpenDocument file."
4064 " Zip failed with error (%s)")
4066 ;; Move the zip file from temporary work directory to
4067 ;; user-mandated location.
4068 (rename-file (concat org-odt-zip-dir target-name
) target
)
4069 (message "Created %s" (expand-file-name target
))
4070 ;; Cleanup work directory and work files.
4071 (funcall --cleanup-xml-buffers
)
4072 ;; Open the OpenDocument file in archive-mode for
4074 (find-file-noselect target t
)
4075 ;; Return exported file.
4077 ;; Case 1: Conversion desired on exported file. Run the
4078 ;; converter on the OpenDocument file. Return the
4080 (org-odt-preferred-output-format
4081 (or (org-odt-convert target org-odt-preferred-output-format
)
4083 ;; Case 2: No further conversion. Return exported
4084 ;; OpenDocument file.
4087 ;; Cleanup work directory and work files.
4088 (funcall --cleanup-xml-buffers
)
4089 (message "OpenDocument export failed: %s"
4090 (error-message-string err
))))))
4093 ;;;; Export to OpenDocument formula
4096 (defun org-odt-export-as-odf (latex-frag &optional odf-file
)
4097 "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
4098 Use `org-create-math-formula' to convert LATEX-FRAG first to
4099 MathML. When invoked as an interactive command, use
4100 `org-latex-regexps' to infer LATEX-FRAG from currently active
4101 region. If no LaTeX fragments are found, prompt for it. Push
4102 MathML source to kill ring depending on the value of
4103 `org-export-copy-to-kill-ring'."
4106 (setq frag
(and (setq frag
(and (region-active-p)
4107 (buffer-substring (region-beginning)
4109 (loop for e in org-latex-regexps
4110 thereis
(when (string-match (nth 1 e
) frag
)
4111 (match-string (nth 2 e
) frag
)))))
4112 (read-string "LaTeX Fragment: " frag nil frag
))
4113 ,(let ((odf-filename (expand-file-name
4115 (file-name-sans-extension
4116 (or (file-name-nondirectory buffer-file-name
)))
4118 (file-name-directory buffer-file-name
))))
4119 (read-file-name "ODF filename: " nil odf-filename nil
4120 (file-name-nondirectory odf-filename
)))))
4121 (let ((filename (or odf-file
4124 (file-name-sans-extension
4125 (or (file-name-nondirectory buffer-file-name
)))
4127 (file-name-directory buffer-file-name
)))))
4128 (org-odt--export-wrap
4130 (let* ((buffer (progn
4131 (require 'nxml-mode
)
4132 (let ((nxml-auto-insert-xml-declaration-flag nil
))
4133 (find-file-noselect (concat org-odt-zip-dir
4134 "content.xml") t
))))
4135 (coding-system-for-write 'utf-8
)
4136 (save-buffer-coding-system 'utf-8
))
4138 (set-buffer-file-coding-system coding-system-for-write
)
4139 (let ((mathml (org-create-math-formula latex-frag
)))
4140 (unless mathml
(error "No Math formula created"))
4142 ;; Add MathML to kill ring, if needed.
4143 (when (org-export--copy-to-kill-ring-p)
4144 (org-kill-new (buffer-string))))))))
4147 (defun org-odt-export-as-odf-and-open ()
4148 "Export LaTeX fragment as OpenDocument formula and immediately open it.
4149 Use `org-odt-export-as-odf' to read LaTeX fragment and OpenDocument
4152 (org-open-file (call-interactively 'org-odt-export-as-odf
) 'system
))
4155 ;;;; Export to OpenDocument Text
4158 (defun org-odt-export-to-odt (&optional async subtreep visible-only ext-plist
)
4159 "Export current buffer to a ODT file.
4161 If narrowing is active in the current buffer, only export its
4164 If a region is active, export that region.
4166 A non-nil optional argument ASYNC means the process should happen
4167 asynchronously. The resulting file should be accessible through
4168 the `org-export-stack' interface.
4170 When optional argument SUBTREEP is non-nil, export the sub-tree
4171 at point, extracting information from the headline properties
4174 When optional argument VISIBLE-ONLY is non-nil, don't export
4175 contents of hidden elements.
4177 EXT-PLIST, when provided, is a property list with external
4178 parameters overriding Org default settings, but still inferior to
4179 file-local settings.
4181 Return output file's name."
4183 (let ((outfile (org-export-output-file-name ".odt" subtreep
)))
4185 (org-export-async-start (lambda (f) (org-export-add-to-stack f
'odt
))
4187 (org-odt--export-wrap
4189 (let* ((org-odt-embedded-images-count 0)
4190 (org-odt-embedded-formulas-count 0)
4191 (org-odt-automatic-styles nil
)
4192 (org-odt-object-counters nil
)
4193 ;; Let `htmlfontify' know that we are interested in
4194 ;; collecting styles.
4195 (hfy-user-sheet-assoc nil
))
4196 ;; Initialize content.xml and kick-off the export
4200 (require 'nxml-mode
)
4201 (let ((nxml-auto-insert-xml-declaration-flag nil
))
4203 (concat org-odt-zip-dir
"content.xml") t
))))
4204 (output (org-export-as
4205 'odt
,subtreep
,visible-only nil
,ext-plist
)))
4206 (with-current-buffer out-buf
4208 (insert output
)))))))
4209 (org-odt--export-wrap
4211 (let* ((org-odt-embedded-images-count 0)
4212 (org-odt-embedded-formulas-count 0)
4213 (org-odt-automatic-styles nil
)
4214 (org-odt-object-counters nil
)
4215 ;; Let `htmlfontify' know that we are interested in collecting
4217 (hfy-user-sheet-assoc nil
))
4218 ;; Initialize content.xml and kick-off the export process.
4219 (let ((output (org-export-as 'odt subtreep visible-only nil ext-plist
))
4221 (require 'nxml-mode
)
4222 (let ((nxml-auto-insert-xml-declaration-flag nil
))
4224 (concat org-odt-zip-dir
"content.xml") t
)))))
4225 (with-current-buffer out-buf
(erase-buffer) (insert output
))))))))
4228 ;;;; Convert between OpenDocument and other formats
4230 (defun org-odt-reachable-p (in-fmt out-fmt
)
4231 "Return non-nil if IN-FMT can be converted to OUT-FMT."
4233 (let ((reachable-formats (org-odt-do-reachable-formats in-fmt
)))
4234 (dolist (e reachable-formats
)
4235 (let ((out-fmt-spec (assoc out-fmt
(cdr e
))))
4237 (throw 'done
(cons (car e
) out-fmt-spec
))))))))
4239 (defun org-odt-do-convert (in-file out-fmt
&optional open
)
4240 "Workhorse routine for `org-odt-convert'."
4241 (require 'browse-url
)
4242 (let* ((in-file (let ((f (expand-file-name (or in-file buffer-file-name
))))
4243 (if (file-readable-p f
) f
4244 (error "Cannot read %s" in-file
))))
4245 (in-fmt (file-name-extension in-file
))
4246 (out-fmt (or out-fmt
(error "Output format unspecified")))
4247 (how (or (org-odt-reachable-p in-fmt out-fmt
)
4248 (error "Cannot convert from %s format to %s format?"
4250 (convert-process (car how
))
4251 (out-file (concat (file-name-sans-extension in-file
) "."
4252 (nth 1 (or (cdr how
) out-fmt
))))
4253 (extra-options (or (nth 2 (cdr how
)) ""))
4254 (out-dir (file-name-directory in-file
))
4255 (cmd (format-spec convert-process
4256 `((?i .
,(shell-quote-argument in-file
))
4257 (?I .
,(browse-url-file-url in-file
))
4260 (?O .
,(browse-url-file-url out-file
))
4261 (?d .
, (shell-quote-argument out-dir
))
4262 (?D .
,(browse-url-file-url out-dir
))
4263 (?x .
,extra-options
)))))
4264 (when (file-exists-p out-file
)
4265 (delete-file out-file
))
4267 (message "Executing %s" cmd
)
4268 (let ((cmd-output (shell-command-to-string cmd
)))
4269 (message "%s" cmd-output
))
4272 ((file-exists-p out-file
)
4273 (message "Exported to %s" out-file
)
4275 (message "Opening %s..." out-file
)
4276 (org-open-file out-file
'system
))
4279 (message "Export to %s failed" out-file
)
4282 (defun org-odt-do-reachable-formats (in-fmt)
4283 "Return verbose info about formats to which IN-FMT can be converted.
4284 Return a list where each element is of the
4285 form (CONVERTER-PROCESS . OUTPUT-FMT-ALIST). See
4286 `org-odt-convert-processes' for CONVERTER-PROCESS and see
4287 `org-odt-convert-capabilities' for OUTPUT-FMT-ALIST."
4289 (and org-odt-convert-process
4290 (cadr (assoc-string org-odt-convert-process
4291 org-odt-convert-processes t
))))
4293 (and org-odt-convert-process
4294 (cadr (assoc-string org-odt-convert-process
4295 org-odt-convert-processes t
))
4296 org-odt-convert-capabilities
))
4299 (dolist (c capabilities
)
4300 (when (member in-fmt
(nth 1 c
))
4301 (push (cons converter
(nth 2 c
)) reachable-formats
))))
4304 (defun org-odt-reachable-formats (in-fmt)
4305 "Return list of formats to which IN-FMT can be converted.
4306 The list of the form (OUTPUT-FMT-1 OUTPUT-FMT-2 ...)."
4308 (apply #'append
(mapcar
4309 (lambda (e) (mapcar #'car
(cdr e
)))
4310 (org-odt-do-reachable-formats in-fmt
)))))
4312 (defun org-odt-convert-read-params ()
4313 "Return IN-FILE and OUT-FMT params for `org-odt-do-convert'.
4314 This is a helper routine for interactive use."
4315 (let* ((input (if (featurep 'ido
) 'ido-completing-read
'completing-read
))
4316 (in-file (read-file-name "File to be converted: "
4317 nil buffer-file-name t
))
4318 (in-fmt (file-name-extension in-file
))
4319 (out-fmt-choices (org-odt-reachable-formats in-fmt
))
4321 (or (and out-fmt-choices
4322 (funcall input
"Output format: "
4323 out-fmt-choices nil nil nil
))
4325 "No known converter or no known output formats for %s files"
4327 (list in-file out-fmt
)))
4330 (defun org-odt-convert (&optional in-file out-fmt open
)
4331 "Convert IN-FILE to format OUT-FMT using a command line converter.
4332 IN-FILE is the file to be converted. If unspecified, it defaults
4333 to variable `buffer-file-name'. OUT-FMT is the desired output
4334 format. Use `org-odt-convert-process' as the converter. If OPEN
4335 is non-nil then the newly converted file is opened using
4338 (append (org-odt-convert-read-params) current-prefix-arg
))
4339 (org-odt-do-convert in-file out-fmt open
))
4341 ;;; Library Initializations
4343 (dolist (desc org-odt-file-extensions
)
4344 ;; Let Emacs open all OpenDocument files in archive mode.
4345 (add-to-list 'auto-mode-alist
4346 (cons (concat "\\." (car desc
) "\\'") 'archive-mode
)))
4351 ;; generated-autoload-file: "org-loaddefs.el"
4354 ;;; ox-odt.el ends here