1 ;;; org-e-odt.el --- OpenDocument Text exporter for Org-mode
3 ;; Copyright (C) 2010-2013 Free Software Foundation, Inc.
5 ;; Author: Jambunathan K <kjambunathan at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
9 ;; This file is not part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
31 (require 'format-spec
)
37 (org-export-define-backend e-odt
38 ((bold . org-e-odt-bold
)
39 (center-block . org-e-odt-center-block
)
40 (clock . org-e-odt-clock
)
41 (code . org-e-odt-code
)
42 (drawer . org-e-odt-drawer
)
43 (dynamic-block . org-e-odt-dynamic-block
)
44 (entity . org-e-odt-entity
)
45 (example-block . org-e-odt-example-block
)
46 (export-block . org-e-odt-export-block
)
47 (export-snippet . org-e-odt-export-snippet
)
48 (fixed-width . org-e-odt-fixed-width
)
49 (footnote-definition . org-e-odt-footnote-definition
)
50 (footnote-reference . org-e-odt-footnote-reference
)
51 (headline . org-e-odt-headline
)
52 (horizontal-rule . org-e-odt-horizontal-rule
)
53 (inline-src-block . org-e-odt-inline-src-block
)
54 (inlinetask . org-e-odt-inlinetask
)
55 (italic . org-e-odt-italic
)
56 (item . org-e-odt-item
)
57 (keyword . org-e-odt-keyword
)
58 (latex-environment . org-e-odt-latex-environment
)
59 (latex-fragment . org-e-odt-latex-fragment
)
60 (line-break . org-e-odt-line-break
)
61 (link . org-e-odt-link
)
62 (paragraph . org-e-odt-paragraph
)
63 (plain-list . org-e-odt-plain-list
)
64 (plain-text . org-e-odt-plain-text
)
65 (planning . org-e-odt-planning
)
66 (property-drawer . org-e-odt-property-drawer
)
67 (quote-block . org-e-odt-quote-block
)
68 (quote-section . org-e-odt-quote-section
)
69 (radio-target . org-e-odt-radio-target
)
70 (section . org-e-odt-section
)
71 (special-block . org-e-odt-special-block
)
72 (src-block . org-e-odt-src-block
)
73 (statistics-cookie . org-e-odt-statistics-cookie
)
74 (strike-through . org-e-odt-strike-through
)
75 (subscript . org-e-odt-subscript
)
76 (superscript . org-e-odt-superscript
)
77 (table . org-e-odt-table
)
78 (table-cell . org-e-odt-table-cell
)
79 (table-row . org-e-odt-table-row
)
80 (target . org-e-odt-target
)
81 (template . org-e-odt-template
)
82 (timestamp . org-e-odt-timestamp
)
83 (underline . org-e-odt-underline
)
84 (verbatim . org-e-odt-verbatim
)
85 (verse-block . org-e-odt-verse-block
))
87 :filters-alist
((:filter-parse-tree
88 .
(org-e-odt--translate-latex-fragments
89 org-e-odt--translate-description-lists
90 org-e-odt--translate-list-tables
)))
93 ((?o
"As ODT file" org-e-odt-export-to-odt
)
94 (?O
"As ODT file and open"
96 (if a
(org-e-odt-export-to-odt t s v
)
97 (org-open-file (org-e-odt-export-to-odt nil s v
) 'system
))))))
99 ((:odt-styles-file
"ODT_STYLES_FILE" nil nil t
)
100 (:LaTeX-fragments nil
"LaTeX" org-export-with-LaTeX-fragments
)))
107 ;;; Function Declarations
109 (declare-function org-id-find-id-file
"org-id" (id))
110 (declare-function hfy-face-to-style
"htmlfontify" (fn))
111 (declare-function hfy-face-or-def-to-name
"htmlfontify" (fn))
112 (declare-function archive-zip-extract
"arc-mode" (archive name
))
113 (declare-function org-create-math-formula
"org" (latex-frag &optional mathml-file
))
114 (declare-function browse-url-file-url
"browse-url" (file))
118 ;;; Internal Variables
120 (defconst org-e-odt-lib-dir
121 (file-name-directory load-file-name
)
122 "Location of ODT exporter.
123 Use this to infer values of `org-e-odt-styles-dir' and
124 `org-e-odt-schema-dir'.")
126 (defvar org-e-odt-data-dir
127 (expand-file-name "../../etc/" org-e-odt-lib-dir
)
128 "Data directory for ODT exporter.
129 Use this to infer values of `org-e-odt-styles-dir' and
130 `org-e-odt-schema-dir'.")
132 (defconst org-e-odt-special-string-regexps
133 '(("\\\\-" .
"­\\1") ; shy
134 ("---\\([^-]\\)" .
"—\\1") ; mdash
135 ("--\\([^-]\\)" .
"–\\1") ; ndash
136 ("\\.\\.\\." .
"…")) ; hellip
137 "Regular expressions for special string conversion.")
139 (defconst org-e-odt-schema-dir-list
141 (and org-e-odt-data-dir
142 (expand-file-name "./schema/" org-e-odt-data-dir
)) ; bail out
144 (and (boundp 'org-e-odt-data-dir
) org-e-odt-data-dir
; see make install
145 (expand-file-name "./schema/" org-e-odt-data-dir
))))
146 "List of directories to search for OpenDocument schema files.
147 Use this list to set the default value of
148 `org-e-odt-schema-dir'. The entries in this list are
149 populated heuristically based on the values of `org-e-odt-lib-dir'
150 and `org-e-odt-data-dir'.")
152 (defconst org-e-odt-styles-dir-list
154 (and org-e-odt-data-dir
155 (expand-file-name "./styles/" org-e-odt-data-dir
)) ; bail out
157 (and (boundp 'org-e-odt-data-dir
) org-e-odt-data-dir
; see make install
158 (expand-file-name "./styles/" org-e-odt-data-dir
)))
159 (expand-file-name "../../etc/styles/" org-e-odt-lib-dir
) ; git
160 (expand-file-name "./etc/styles/" org-e-odt-lib-dir
) ; elpa
161 (expand-file-name "./org/" data-directory
) ; system
163 "List of directories to search for OpenDocument styles files.
164 See `org-e-odt-styles-dir'. The entries in this list are populated
165 heuristically based on the values of `org-e-odt-lib-dir' and
166 `org-e-odt-data-dir'.")
168 (defconst org-e-odt-styles-dir
171 (message "Debug (org-e-odt): Searching for OpenDocument styles files...")
172 (mapc (lambda (styles-dir)
174 (message "Debug (org-e-odt): Trying %s..." styles-dir
)
175 (when (and (file-readable-p
177 "OrgOdtContentTemplate.xml" styles-dir
))
180 "OrgOdtStyles.xml" styles-dir
)))
181 (message "Debug (org-e-odt): Using styles under %s"
183 (throw 'styles-dir styles-dir
))))
184 org-e-odt-styles-dir-list
)
187 (error "Error (org-e-odt): Cannot find factory styles files, aborting"))
189 "Directory that holds auxiliary XML files used by the ODT exporter.
191 This directory contains the following XML files -
192 \"OrgOdtStyles.xml\" and \"OrgOdtContentTemplate.xml\". These
193 XML files are used as the default values of
194 `org-e-odt-styles-file' and
195 `org-e-odt-content-template-file'.
197 The default value of this variable varies depending on the
198 version of org in use and is initialized from
199 `org-e-odt-styles-dir-list'. Note that the user could be using org
200 from one of: org's own private git repository, GNU ELPA tar or
203 (defconst org-e-odt-bookmark-prefix
"OrgXref.")
205 (defconst org-e-odt-manifest-file-entry-tag
206 "\n<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
208 (defconst org-e-odt-file-extensions
209 '(("odt" .
"OpenDocument Text")
210 ("ott" .
"OpenDocument Text Template")
211 ("odm" .
"OpenDocument Master Document")
212 ("ods" .
"OpenDocument Spreadsheet")
213 ("ots" .
"OpenDocument Spreadsheet Template")
214 ("odg" .
"OpenDocument Drawing (Graphics)")
215 ("otg" .
"OpenDocument Drawing Template")
216 ("odp" .
"OpenDocument Presentation")
217 ("otp" .
"OpenDocument Presentation Template")
218 ("odi" .
"OpenDocument Image")
219 ("odf" .
"OpenDocument Formula")
220 ("odc" .
"OpenDocument Chart")))
222 (defconst org-e-odt-table-style-format
224 <style:style style:name=\"%s\" style:family=\"table\">
225 <style:table-properties style:rel-width=\"%d%%\" fo:margin-top=\"0cm\" fo:margin-bottom=\"0.20cm\" table:align=\"center\"/>
228 "Template for auto-generated Table styles.")
230 (defvar org-e-odt-automatic-styles
'()
231 "Registry of automatic styles for various OBJECT-TYPEs.
232 The variable has the following form:
234 \(\(OBJECT-NAME-A.1 OBJECT-PROPS-A.1\)
235 \(OBJECT-NAME-A.2 OBJECT-PROPS-A.2\) ...\)\)
237 \(\(OBJECT-NAME-B.1 OBJECT-PROPS-B.1\)
238 \(OBJECT-NAME-B.2 OBJECT-PROPS-B.2\) ...\)\)
241 OBJECT-TYPEs could be \"Section\", \"Table\", \"Figure\" etc.
242 OBJECT-PROPS is (typically) a plist created by passing
243 \"#+ATTR_ODT: \" option to `org-e-odt-parse-block-attributes'.
245 Use `org-e-odt-add-automatic-style' to add update this variable.'")
247 (defvar org-e-odt-object-counters nil
248 "Running counters for various OBJECT-TYPEs.
249 Use this to generate automatic names and style-names. See
250 `org-e-odt-add-automatic-style'.")
252 (defvar org-e-odt-src-block-paragraph-format
253 "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
254 <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
255 <style:background-image/>
256 </style:paragraph-properties>
257 <style:text-properties fo:color=\"%s\"/>
259 "Custom paragraph style for colorized source and example blocks.
260 This style is much the same as that of \"OrgFixedWidthBlock\"
261 except that the foreground and background colors are set
262 according to the default face identified by the `htmlfontify'.")
264 (defvar hfy-optimisations
)
265 (defvar org-e-odt-embedded-formulas-count
0)
266 (defvar org-e-odt-embedded-images-count
0)
267 (defvar org-e-odt-image-size-probe-method
268 (append (and (executable-find "identify") '(imagemagick)) ; See Bug#10675
270 "Ordered list of methods for determining image sizes.")
272 (defvar org-e-odt-default-image-sizes-alist
273 '(("as-char" .
(5 .
0.4))
274 ("paragraph" .
(5 .
5)))
275 "Hardcoded image dimensions one for each of the anchor
278 ;; A4 page size is 21.0 by 29.7 cms
279 ;; The default page settings has 2cm margin on each of the sides. So
280 ;; the effective text area is 17.0 by 25.7 cm
281 (defvar org-e-odt-max-image-size
'(17.0 .
20.0)
282 "Limiting dimensions for an embedded image.")
284 (defconst org-e-odt-label-styles
285 '(("math-formula" "%c" "text" "(%n)")
286 ("math-label" "(%n)" "text" "(%n)")
287 ("category-and-value" "%e %n: %c" "category-and-value" "%e %n")
288 ("value" "%e %n: %c" "value" "%n"))
289 "Specify how labels are applied and referenced.
290 This is an alist where each element is of the
291 form (LABEL-STYLE-NAME LABEL-ATTACH-FMT LABEL-REF-MODE
294 LABEL-ATTACH-FMT controls how labels and captions are attached to
295 an entity. It may contain following specifiers - %e, %n and %c.
296 %e is replaced with the CATEGORY-NAME. %n is replaced with
297 \"<text:sequence ...> SEQNO </text:sequence>\". %c is replaced
298 with CAPTION. See `org-e-odt-format-label-definition'.
300 LABEL-REF-MODE and LABEL-REF-FMT controls how label references
301 are generated. The following XML is generated for a label
302 reference - \"<text:sequence-ref
303 text:reference-format=\"LABEL-REF-MODE\" ...> LABEL-REF-FMT
304 </text:sequence-ref>\". LABEL-REF-FMT may contain following
305 specifiers - %e and %n. %e is replaced with the CATEGORY-NAME.
306 %n is replaced with SEQNO. See
307 `org-e-odt-format-label-reference'.")
309 (defvar org-e-odt-category-map-alist
310 '(("__Table__" "Table" "value" "Table" org-e-odt--enumerable-p
)
311 ("__Figure__" "Illustration" "value" "Figure" org-e-odt--enumerable-image-p
)
312 ("__MathFormula__" "Text" "math-formula" "Equation" org-e-odt--enumerable-formula-p
)
313 ("__DvipngImage__" "Equation" "value" "Equation" org-e-odt--enumerable-latex-image-p
)
314 ("__Listing__" "Listing" "value" "Listing" org-e-odt--enumerable-p
)
315 ;; ("__Table__" "Table" "category-and-value")
316 ;; ("__Figure__" "Figure" "category-and-value")
317 ;; ("__DvipngImage__" "Equation" "category-and-value")
319 "Map a CATEGORY-HANDLE to OD-VARIABLE and LABEL-STYLE.
320 This is a list where each entry is of the form \\(CATEGORY-HANDLE
321 OD-VARIABLE LABEL-STYLE CATEGORY-NAME ENUMERATOR-PREDICATE\\).
323 CATEGORY_HANDLE identifies the captionable entity in question.
325 OD-VARIABLE is the OpenDocument sequence counter associated with
326 the entity. These counters are declared within
327 \"<text:sequence-decls>...</text:sequence-decls>\" block of
328 `org-e-odt-content-template-file'.
330 LABEL-STYLE is a key into `org-e-odt-label-styles' and specifies
331 how a given entity should be captioned and referenced.
333 CATEGORY-NAME is used for qualifying captions on export. You can
334 modify the CATEGORY-NAME used in the exported document by
335 modifying `org-export-dictionary'. For example, an embedded
336 image in an English document is captioned as \"Figure 1: Orgmode
337 Logo\", by default. If you want the image to be captioned as
338 \"Illustration 1: Orgmode Logo\" instead, install an entry in
339 `org-export-dictionary' which translates \"Figure\" to
340 \"Illustration\" when the language is \"en\" and encoding is
343 ENUMERATOR-PREDICATE is used for assigning a sequence number to
344 the entity. See `org-e-odt--enumerate'.")
346 (defvar org-e-odt-manifest-file-entries nil
)
347 (defvar hfy-user-sheet-assoc
)
349 (defvar org-e-odt-zip-dir nil
350 "Temporary work directory for OpenDocument exporter.")
354 ;;; User Configuration Variables
356 (defgroup org-export-e-odt nil
357 "Options for exporting Org mode files to ODT."
358 :tag
"Org Export ODT"
364 (defcustom org-e-odt-prettify-xml nil
365 "Specify whether or not the xml output should be prettified.
366 When this option is turned on, `indent-region' is run on all
367 component xml buffers before they are saved. Turn this off for
368 regular use. Turn this on if you need to examine the xml
370 :group
'org-export-e-odt
377 (defcustom org-e-odt-schema-dir
380 (message "Debug (org-e-odt): Searching for OpenDocument schema files...")
384 (message "Debug (org-e-odt): Trying %s..." schema-dir
)
385 (when (and (file-expand-wildcards
386 (expand-file-name "od-manifest-schema*.rnc"
388 (file-expand-wildcards
389 (expand-file-name "od-schema*.rnc"
392 (expand-file-name "schemas.xml" schema-dir
)))
393 (message "Debug (org-e-odt): Using schema files under %s"
395 (throw 'schema-dir schema-dir
))))
396 org-e-odt-schema-dir-list
)
397 (message "Debug (org-e-odt): No OpenDocument schema files installed")
400 "Directory that contains OpenDocument schema files.
402 This directory contains:
403 1. rnc files for OpenDocument schema
404 2. a \"schemas.xml\" file that specifies locating rules needed
405 for auto validation of OpenDocument XML files.
407 Use the customize interface to set this variable. This ensures
408 that `rng-schema-locating-files' is updated and auto-validation
409 of OpenDocument XML takes place based on the value
410 `rng-nxml-auto-validate-flag'.
412 The default value of this variable varies depending on the
413 version of org in use and is initialized from
414 `org-e-odt-schema-dir-list'. The OASIS schema files are available
415 only in the org's private git repository. It is *not* bundled
416 with GNU ELPA tar or standard Emacs distribution."
418 (const :tag
"Not set" nil
)
419 (directory :tag
"Schema directory"))
420 :group
'org-export-e-odt
424 "Set `org-e-odt-schema-dir'.
425 Also add it to `rng-schema-locating-files'."
426 (let ((schema-dir value
))
429 (file-expand-wildcards
430 (expand-file-name "od-manifest-schema*.rnc" schema-dir
))
431 (file-expand-wildcards
432 (expand-file-name "od-schema*.rnc" schema-dir
))
434 (expand-file-name "schemas.xml" schema-dir
)))
437 (message "Error (org-e-odt): %s has no OpenDocument schema files"
440 (when org-e-odt-schema-dir
441 (eval-after-load 'rng-loc
442 '(add-to-list 'rng-schema-locating-files
443 (expand-file-name "schemas.xml"
444 org-e-odt-schema-dir
))))))
449 (defcustom org-e-odt-content-template-file nil
450 "Template file for \"content.xml\".
451 The exporter embeds the exported content just before
452 \"</office:text>\" element.
454 If unspecified, the file named \"OrgOdtContentTemplate.xml\"
455 under `org-e-odt-styles-dir' is used."
457 :group
'org-export-e-odt
460 (defcustom org-e-odt-styles-file nil
461 "Default styles file for use with ODT export.
462 Valid values are one of:
464 2. path to a styles.xml file
465 3. path to a *.odt or a *.ott file
466 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
469 In case of option 1, an in-built styles.xml is used. See
470 `org-e-odt-styles-dir' for more information.
472 In case of option 3, the specified file is unzipped and the
473 styles.xml embedded therein is used.
475 In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
476 and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
477 generated odt file. Use relative path for specifying the
478 FILE-MEMBERS. styles.xml must be specified as one of the
481 Use options 1, 2 or 3 only if styles.xml alone suffices for
482 achieving the desired formatting. Use option 4, if the styles.xml
483 references additional files like header and footer images for
484 achieving the desired formatting.
486 Use \"#+ODT_STYLES_FILE: ...\" directive to set this variable on
487 a per-file basis. For example,
489 #+ODT_STYLES_FILE: \"/path/to/styles.xml\" or
490 #+ODT_STYLES_FILE: (\"/path/to/file.ott\" (\"styles.xml\" \"image/hdr.png\"))."
491 :group
'org-export-e-odt
495 (const :tag
"Factory settings" nil
)
496 (file :must-match t
:tag
"styles.xml")
497 (file :must-match t
:tag
"ODT or OTT file")
498 (list :tag
"ODT or OTT file + Members"
499 (file :must-match t
:tag
"ODF Text or Text Template file")
501 (file :tag
" Member" "styles.xml")
502 (repeat (file :tag
"Member"))))))
504 (defcustom org-e-odt-display-outline-level
2
505 "Outline levels considered for enumerating captioned entities."
506 :group
'org-export-e-odt
510 ;;;; Document conversion
512 (defcustom org-e-odt-convert-processes
514 "soffice --headless --convert-to %f%x --outdir %d %i")
516 "unoconv -f %f -o %d %i"))
517 "Specify a list of document converters and their usage.
518 The converters in this list are offered as choices while
519 customizing `org-e-odt-convert-process'.
521 This variable is a list where each element is of the
522 form (CONVERTER-NAME CONVERTER-CMD). CONVERTER-NAME is the name
523 of the converter. CONVERTER-CMD is the shell command for the
524 converter and can contain format specifiers. These format
525 specifiers are interpreted as below:
527 %i input file name in full
528 %I input file name as a URL
529 %f format of the output file
530 %o output file name in full
531 %O output file name as a URL
532 %d output dir in full
533 %D output dir as a URL.
534 %x extra options as set in `org-e-odt-convert-capabilities'."
535 :group
'org-export-e-odt
539 (const :tag
"None" nil
)
540 (alist :tag
"Converters"
541 :key-type
(string :tag
"Converter Name")
542 :value-type
(group (string :tag
"Command line")))))
544 (defcustom org-e-odt-convert-process
"LibreOffice"
545 "Use this converter to convert from \"odt\" format to other formats.
546 During customization, the list of converter names are populated
547 from `org-e-odt-convert-processes'."
548 :group
'org-export-e-odt
550 :type
'(choice :convert-widget
552 (apply 'widget-convert
(widget-type w
)
553 (eval (car (widget-get w
:args
)))))
554 `((const :tag
"None" nil
)
555 ,@(mapcar (lambda (c)
556 `(const :tag
,(car c
) ,(car c
)))
557 org-e-odt-convert-processes
))))
559 (defcustom org-e-odt-convert-capabilities
561 ("odt" "ott" "doc" "rtf" "docx")
562 (("pdf" "pdf") ("odt" "odt") ("rtf" "rtf") ("ott" "ott")
563 ("doc" "doc" ":\"MS Word 97\"") ("docx" "docx") ("html" "html")))
566 (("pdf" "pdf") ("odt" "odt") ("html" "html")))
568 ("ods" "ots" "xls" "csv" "xlsx")
569 (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv") ("ods" "ods")
570 ("xls" "xls") ("xlsx" "xlsx")))
572 ("odp" "otp" "ppt" "pptx")
573 (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("otp" "otp") ("ppt" "ppt")
574 ("pptx" "pptx") ("odg" "odg"))))
575 "Specify input and output formats of `org-e-odt-convert-process'.
576 More correctly, specify the set of input and output formats that
577 the user is actually interested in.
579 This variable is an alist where each element is of the
580 form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
581 INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
582 alist where each element is of the form (OUTPUT-FMT
583 OUTPUT-FILE-EXTENSION EXTRA-OPTIONS).
585 The variable is interpreted as follows:
586 `org-e-odt-convert-process' can take any document that is in
587 INPUT-FMT-LIST and produce any document that is in the
588 OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
589 OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
590 serves dual purposes:
591 - It is used for populating completion candidates during
592 `org-e-odt-convert' commands.
593 - It is used as the value of \"%f\" specifier in
594 `org-e-odt-convert-process'.
596 EXTRA-OPTIONS is used as the value of \"%x\" specifier in
597 `org-e-odt-convert-process'.
599 DOCUMENT-CLASS is used to group a set of file formats in
600 INPUT-FMT-LIST in to a single class.
602 Note that this variable inherently captures how LibreOffice based
603 converters work. LibreOffice maps documents of various formats
604 to classes like Text, Web, Spreadsheet, Presentation etc and
605 allow document of a given class (irrespective of it's source
606 format) to be converted to any of the export formats associated
609 See default setting of this variable for an typical
611 :group
'org-export-e-odt
615 (const :tag
"None" nil
)
616 (alist :tag
"Capabilities"
617 :key-type
(string :tag
"Document Class")
619 (group (repeat :tag
"Input formats" (string :tag
"Input format"))
620 (alist :tag
"Output formats"
621 :key-type
(string :tag
"Output format")
623 (group (string :tag
"Output file extension")
625 (const :tag
"None" nil
)
626 (string :tag
"Extra options"))))))))
628 (defcustom org-e-odt-preferred-output-format nil
629 "Automatically post-process to this format after exporting to \"odt\".
630 Command `org-e-odt-export-to-odt' exports first to \"odt\" format
631 and then uses `org-e-odt-convert-process' to convert the
632 resulting document to this format. During customization of this
633 variable, the list of valid values are populated based on
634 `org-e-odt-convert-capabilities'.
636 You can set this option on per-file basis using file local
637 values. See Info node `(emacs) File Variables'."
638 :group
'org-export-e-odt
640 :type
'(choice :convert-widget
642 (apply 'widget-convert
(widget-type w
)
643 (eval (car (widget-get w
:args
)))))
644 `((const :tag
"None" nil
)
645 ,@(mapcar (lambda (c)
647 (org-e-odt-reachable-formats "odt")))))
649 (put 'org-e-odt-preferred-output-format
'safe-local-variable
'stringp
)
654 (defcustom org-e-odt-format-drawer-function nil
655 "Function called to format a drawer in ODT code.
657 The function must accept two parameters:
658 NAME the drawer name, like \"LOGBOOK\"
659 CONTENTS the contents of the drawer.
661 The function should return the string to be exported.
663 For example, the variable could be set to the following function
664 in order to mimic default behaviour:
666 \(defun org-e-odt-format-drawer-default \(name contents\)
667 \"Format a drawer element for ODT export.\"
669 :group
'org-export-e-odt
675 (defcustom org-e-odt-format-headline-function nil
676 "Function to format headline text.
678 This function will be called with 5 arguments:
679 TODO the todo keyword \(string or nil\).
680 TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
681 PRIORITY the priority of the headline \(integer or nil\)
682 TEXT the main headline text \(string\).
683 TAGS the tags string, separated with colons \(string or nil\).
685 The function result will be used as headline text."
686 :group
'org-export-e-odt
692 (defcustom org-e-odt-format-inlinetask-function nil
693 "Function called to format an inlinetask in ODT code.
695 The function must accept six parameters:
696 TODO the todo keyword, as a string
697 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
698 PRIORITY the inlinetask priority, as a string
699 NAME the inlinetask name, as a string.
700 TAGS the inlinetask tags, as a string.
701 CONTENTS the contents of the inlinetask, as a string.
703 The function should return the string to be exported."
704 :group
'org-export-e-odt
710 (defcustom org-e-odt-inline-formula-rules
711 '(("file" .
"\\.\\(mathml\\|mml\\|odf\\)\\'"))
712 "Rules characterizing formula files that can be inlined into ODT.
714 A rule consists in an association whose key is the type of link
715 to consider, and value is a regexp that will be matched against
717 :group
'org-export-e-odt
718 :type
'(alist :key-type
(string :tag
"Type")
719 :value-type
(regexp :tag
"Path")))
721 (defcustom org-e-odt-inline-image-rules
722 '(("file" .
"\\.\\(jpeg\\|jpg\\|png\\|gif\\)\\'"))
723 "Rules characterizing image files that can be inlined into ODT.
725 A rule consists in an association whose key is the type of link
726 to consider, and value is a regexp that will be matched against
728 :group
'org-export-e-odt
729 :type
'(alist :key-type
(string :tag
"Type")
730 :value-type
(regexp :tag
"Path")))
732 (defcustom org-e-odt-pixels-per-inch display-pixels-per-inch
733 "Scaling factor for converting images pixels to inches.
734 Use this for sizing of embedded images. See Info node `(org)
735 Images in ODT export' for more information."
737 :group
'org-export-e-odt
743 (defcustom org-e-odt-create-custom-styles-for-srcblocks t
744 "Whether custom styles for colorized source blocks be automatically created.
745 When this option is turned on, the exporter creates custom styles
746 for source blocks based on the advice of `htmlfontify'. Creation
747 of custom styles happen as part of `org-e-odt-hfy-face-to-css'.
749 When this option is turned off exporter does not create such
752 Use the latter option if you do not want the custom styles to be
753 based on your current display settings. It is necessary that the
754 styles.xml already contains needed styles for colorizing to work.
756 This variable is effective only if
757 `org-e-odt-fontify-srcblocks' is turned on."
758 :group
'org-export-e-odt
762 (defcustom org-e-odt-fontify-srcblocks t
763 "Specify whether or not source blocks need to be fontified.
764 Turn this option on if you want to colorize the source code
765 blocks in the exported file. For colorization to work, you need
766 to make available an enhanced version of `htmlfontify' library."
768 :group
'org-export-e-odt
774 (defcustom org-e-odt-table-styles
775 '(("OrgEquation" "OrgEquation"
776 ((use-first-column-styles . t
)
777 (use-last-column-styles . t
)))
778 ("TableWithHeaderRowAndColumn" "Custom"
779 ((use-first-row-styles . t
)
780 (use-first-column-styles . t
)))
781 ("TableWithFirstRowandLastRow" "Custom"
782 ((use-first-row-styles . t
)
783 (use-last-row-styles . t
)))
784 ("GriddedTable" "Custom" nil
))
785 "Specify how Table Styles should be derived from a Table Template.
786 This is a list where each element is of the
787 form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
789 TABLE-STYLE-NAME is the style associated with the table through
790 \"#+ATTR_ODT: :style TABLE-STYLE-NAME\" line.
792 TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
793 TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
794 below) that is included in
795 `org-e-odt-content-template-file'.
797 TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
799 PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
801 TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
802 \"FirstRow\" | \"LastRow\" |
803 \"EvenRow\" | \"OddRow\" |
804 \"EvenColumn\" | \"OddColumn\" | \"\"
805 where \"+\" above denotes string concatenation.
807 TABLE-CELL-OPTIONS is an alist where each element is of the
808 form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
809 TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
810 `use-last-row-styles' |
811 `use-first-column-styles' |
812 `use-last-column-styles' |
813 `use-banding-rows-styles' |
814 `use-banding-columns-styles' |
815 `use-first-row-styles'
816 ON-OR-OFF := `t' | `nil'
818 For example, with the following configuration
820 \(setq org-e-odt-table-styles
821 '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
822 \(\(use-first-row-styles . t\)
823 \(use-first-column-styles . t\)\)\)
824 \(\"TableWithHeaderColumns\" \"Custom\"
825 \(\(use-first-column-styles . t\)\)\)\)\)
827 1. A table associated with \"TableWithHeaderRowsAndColumns\"
828 style will use the following table-cell styles -
829 \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
830 \"CustomTableCell\" and the following paragraph styles
831 \"CustomFirstRowTableParagraph\",
832 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
835 2. A table associated with \"TableWithHeaderColumns\" style will
836 use the following table-cell styles -
837 \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
838 following paragraph styles
839 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
842 Note that TABLE-TEMPLATE-NAME corresponds to the
843 \"<table:table-template>\" elements contained within
844 \"<office:styles>\". The entries (TABLE-STYLE-NAME
845 TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
846 \"table:template-name\" and \"table:use-first-row-styles\" etc
847 attributes of \"<table:table>\" element. Refer ODF-1.2
848 specification for more information. Also consult the
849 implementation filed under `org-e-odt-get-table-cell-styles'.
851 The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
852 formatting of numbered display equations. Do not delete this
853 style from the list."
854 :group
'org-export-e-odt
857 (const :tag
"None" nil
)
858 (repeat :tag
"Table Styles"
859 (list :tag
"Table Style Specification"
860 (string :tag
"Table Style Name")
861 (string :tag
"Table Template Name")
862 (alist :options
(use-first-row-styles
864 use-first-column-styles
865 use-last-column-styles
866 use-banding-rows-styles
867 use-banding-columns-styles
)
869 :value-type
(const :tag
"True" t
))))))
873 (defcustom org-e-odt-use-date-fields nil
874 "Non-nil, if timestamps should be exported as date fields.
876 When nil, export timestamps as plain text.
878 When non-nil, map `org-time-stamp-custom-formats' to a pair of
879 OpenDocument date-styles with names \"OrgDate1\" and \"OrgDate2\"
880 respectively. A timestamp with no time component is formatted
881 with style \"OrgDate1\" while one with explicit hour and minutes
882 is formatted with style \"OrgDate2\".
884 This feature is experimental. Most (but not all) of the common
885 %-specifiers in `format-time-string' are supported.
886 Specifically, locale-dependent specifiers like \"%c\", \"%x\" are
887 formatted as canonical Org timestamps. For finer control, avoid
890 Textutal specifiers like \"%b\", \"%h\", \"%B\", \"%a\", \"%A\"
891 etc., are displayed by the application in the default language
892 and country specified in `org-e-odt-styles-file'. Note that the
893 default styles file uses language \"en\" and country \"GB\". You
894 can localize the week day and month strings in the exported
895 document by setting the default language and country either using
896 the application UI or through a custom styles file.
898 See `org-e-odt--build-date-styles' for implementation details."
899 :group
'org-export-e-odt
904 ;;; Internal functions
908 (defun org-e-odt--format-timestamp (timestamp &optional end iso-date-p
)
909 (let* ((format-timestamp
910 (lambda (timestamp format
&optional end utc
)
912 (org-export-format-timestamp timestamp format end utc
)
913 (format-time-string format nil utc
))))
914 (has-time-p (or (not timestamp
)
915 (org-export-timestamp-has-time-p timestamp
)))
916 (iso-date (let ((format (if has-time-p
"%Y-%m-%dT%H:%M:%S"
917 "%Y-%m-%dT%H:%M:%S")))
918 (funcall format-timestamp timestamp format end
))))
919 (if iso-date-p iso-date
920 (let* ((style (if has-time-p
"OrgDate2" "OrgDate1"))
921 ;; LibreOffice does not care about end goes as content
922 ;; within the "<text:date>...</text:date>" field. The
923 ;; displayed date is automagically corrected to match the
924 ;; format requested by "style:data-style-name" attribute. So
925 ;; don't bother about formatting the date contents to be
926 ;; compatible with "OrgDate1" and "OrgDateTime" styles. A
927 ;; simple Org-style date should suffice.
928 (date (let* ((formats
929 (if org-display-custom-times
931 (car org-time-stamp-custom-formats
) 1 -
1)
933 (cdr org-time-stamp-custom-formats
) 1 -
1))
934 '("%Y-%m-%d %a" .
"%Y-%m-%d %a %H:%M")))
935 (format (if has-time-p
(cdr formats
) (car formats
))))
936 (funcall format-timestamp timestamp format end
)))
937 (repeater (let ((repeater-type (org-element-property
938 :repeater-type timestamp
))
939 (repeater-value (org-element-property
940 :repeater-value timestamp
))
941 (repeater-unit (org-element-property
942 :repeater-unit timestamp
)))
945 (catchup "++") (restart ".+") (cumulate "+"))
947 (number-to-string repeater-value
))
949 (hour "h") (day "d") (week "w") (month "m")
952 (format "<text:date text:date-value=\"%s\" style:data-style-name=\"%s\" text:fixed=\"true\">%s</text:date>"
954 (and (not (string= repeater
"")) " ")
959 (defun org-e-odt--frame (text width height style
&optional extra
960 anchor-type
&rest title-and-desc
)
963 (if width
(format " svg:width=\"%0.2fcm\"" width
) "")
964 (if height
(format " svg:height=\"%0.2fcm\"" height
) "")
966 (format " text:anchor-type=\"%s\"" (or anchor-type
"paragraph")))))
968 "\n<draw:frame draw:style-name=\"%s\"%s>\n%s\n</draw:frame>"
971 (let ((title (car title-and-desc
))
972 (desc (cadr title-and-desc
)))
974 (format "<svg:title>%s</svg:title>"
975 (org-e-odt--encode-plain-text title t
)))
977 (format "<svg:desc>%s</svg:desc>"
978 (org-e-odt--encode-plain-text desc t
)))))))))
981 ;;;; Library wrappers
983 (defun org-e-odt--zip-extract (archive members target
)
984 (when (atom members
) (setq members
(list members
)))
985 (mapc (lambda (member)
987 (let* ((--quote-file-name
988 ;; This is shamelessly stolen from `archive-zip-extract'.
990 (if (or (not (memq system-type
'(windows-nt ms-dos
)))
991 (and (boundp 'w32-quote-process-args
)
992 (null w32-quote-process-args
)))
993 (shell-quote-argument name
)
995 (target (funcall --quote-file-name target
))
996 (archive (expand-file-name archive
))
998 (list "unzip" "-qq" "-o" "-d" target
))
999 exit-code command-output
)
1000 (setq command-output
1002 (setq exit-code
(archive-zip-extract archive member
))
1004 (unless (zerop exit-code
)
1005 (message command-output
)
1006 (error "Extraction failed"))))
1009 (defun org-e-odt--suppress-some-translators (info types
)
1010 ;; See comments in `org-e-odt-format-label' and `org-e-odt-toc'.
1013 ;; Override translators.
1015 (nconc (mapcar (lambda (type) (cons type
(lambda (data contents info
)
1017 (plist-get info
:translate-alist
))
1018 ;; Reset data translation cache. FIXME.
1019 ;; :exported-data nil
1025 (defun org-e-odt--target (text id
)
1028 (format "\n<text:bookmark-start text:name=\"OrgXref.%s\"/>" id
)
1029 (format "\n<text:bookmark text:name=\"%s\"/>" id
) text
1030 (format "\n<text:bookmark-end text:name=\"OrgXref.%s\"/>" id
))))
1034 (defun org-e-odt--textbox (text width height style
&optional
1037 (format "\n<draw:text-box %s>%s\n</draw:text-box>"
1038 (concat (format " fo:min-height=\"%0.2fcm\"" (or height
.2))
1040 (format " fo:min-width=\"%0.2fcm\"" (or width
.2))))
1042 width nil style extra anchor-type
))
1046 ;;;; Table of Contents
1048 (defun org-e-odt-begin-toc (index-title depth
)
1051 <text:table-of-content text:style-name=\"OrgIndexSection\" text:protected=\"true\" text:name=\"Table of Contents\">
1052 <text:table-of-content-source text:outline-level=\"%d\">
1053 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
1054 " depth index-title
)
1056 (let ((levels (number-sequence 1 10)))
1061 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
1062 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
1063 <text:index-entry-chapter/>
1064 <text:index-entry-text/>
1065 <text:index-entry-link-end/>
1066 </text:table-of-content-entry-template>
1067 " level level
)) levels
""))
1070 </text:table-of-content-source>
1073 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
1074 <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
1078 (defun org-e-odt-end-toc ()
1081 </text:table-of-content>
1084 (defun* org-e-odt-format-toc-headline
1085 (todo todo-type priority text tags
1086 &key level section-number headline-label
&allow-other-keys
)
1090 (when section-number
(concat section-number
". "))
1093 (let ((style (if (member todo org-done-keywords
)
1094 "OrgDone" "OrgTodo")))
1095 (format "<text:span text:style-name=\"%s\">%s</text:span> "
1098 (let* ((style (format "OrgPriority-%s" priority
))
1099 (priority (format "[#%c]" priority
)))
1100 (format "<text:span text:style-name=\"%s\">%s</text:span> "
1107 (format " <text:span text:style-name=\"%s\">[%s]</text:span>"
1112 "<text:span text:style-name=\"%s\">%s</text:span>"
1113 "OrgTag" tag
)) tags
" : "))))))
1114 (format "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
1115 headline-label text
))
1117 (defun org-e-odt-toc (depth info
)
1118 (assert (wholenump depth
))
1119 ;; When a headline is marked as a radio target, as in the example below:
1121 ;; ** <<<Some Heading>>>
1124 ;; suppress generation of radio targets. i.e., Radio targets are to
1125 ;; be marked as targets within /document body/ and *not* within
1126 ;; /TOC/, as otherwise there will be duplicated anchors one in TOC
1127 ;; and one in the document body.
1129 ;; FIXME-1: Currently exported headings are memoized. `org-export.el'
1130 ;; doesn't provide a way to disable memoization. So this doesn't
1133 ;; FIXME-2: Are there any other objects that need to be suppressed
1135 (let* ((title (org-export-translate "Table of Contents" :utf-8 info
))
1136 (headlines (org-export-collect-headlines
1137 info
(and (wholenump depth
) depth
)))
1138 (translations (nconc (mapcar
1140 (cons type
(lambda (data contents info
)
1142 (list 'radio-target
))
1143 (plist-get info
:translate-alist
))))
1146 (org-e-odt-begin-toc title depth
)
1149 (let* ((entry (org-e-odt-format-headline--wrap
1150 headline translations info
1151 'org-e-odt-format-toc-headline
))
1152 (level (org-export-get-relative-level headline info
))
1153 (style (format "Contents_20_%d" level
)))
1154 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1157 (org-e-odt-end-toc)))))
1160 ;;;; Document styles
1162 (defun org-e-odt-add-automatic-style (object-type &optional object-props
)
1163 "Create an automatic style of type OBJECT-TYPE with param OBJECT-PROPS.
1164 OBJECT-PROPS is (typically) a plist created by passing
1165 \"#+ATTR_ODT: \" option of the object in question to
1166 `org-e-odt-parse-block-attributes'.
1168 Use `org-e-odt-object-counters' to generate an automatic
1169 OBJECT-NAME and STYLE-NAME. If OBJECT-PROPS is non-nil, add a
1170 new entry in `org-e-odt-automatic-styles'. Return (OBJECT-NAME
1172 (assert (stringp object-type
))
1173 (let* ((object (intern object-type
))
1175 (seqno (1+ (or (plist-get org-e-odt-object-counters seqvar
) 0)))
1176 (object-name (format "%s%d" object-type seqno
)) style-name
)
1177 (setq org-e-odt-object-counters
1178 (plist-put org-e-odt-object-counters seqvar seqno
))
1180 (setq style-name
(format "Org%s" object-name
))
1181 (setq org-e-odt-automatic-styles
1182 (plist-put org-e-odt-automatic-styles object
1183 (append (list (list style-name object-props
))
1184 (plist-get org-e-odt-automatic-styles object
)))))
1185 (cons object-name style-name
)))
1189 (defun org-e-odt--checkbox (item)
1190 "Return check-box string associated to ITEM."
1191 (let ((checkbox (org-element-property :checkbox item
)))
1192 (if (not checkbox
) ""
1193 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1194 "OrgCode" (case checkbox
1195 (on "[✓] ") ; CHECK MARK
1201 (defun org-e-odt--build-date-styles (fmt style
)
1202 ;; In LibreOffice 3.4.6, there doesn't seem to be a convenient way
1203 ;; to modify the date fields. A date could be modified by
1204 ;; offsetting in days. That's about it. Also, date and time may
1205 ;; have to be emitted as two fields - a date field and a time field
1208 ;; One can add Form Controls to date and time fields so that they
1209 ;; can be easily modified. But then, the exported document will
1210 ;; become tightly coupled with LibreOffice and may not function
1211 ;; properly with other OpenDocument applications.
1213 ;; I have a strange feeling that Date styles are a bit flaky at the
1216 ;; The feature is experimental.
1217 (when (and fmt style
)
1219 '(("%A" .
"<number:day-of-week number:style=\"long\"/>")
1220 ("%B" .
"<number:month number:textual=\"true\" number:style=\"long\"/>")
1221 ("%H" .
"<number:hours number:style=\"long\"/>")
1222 ("%M" .
"<number:minutes number:style=\"long\"/>")
1223 ("%S" .
"<number:seconds number:style=\"long\"/>")
1224 ("%V" .
"<number:week-of-year/>")
1225 ("%Y" .
"<number:year number:style=\"long\"/>")
1226 ("%a" .
"<number:day-of-week number:style=\"short\"/>")
1227 ("%b" .
"<number:month number:textual=\"true\" number:style=\"short\"/>")
1228 ("%d" .
"<number:day number:style=\"long\"/>")
1229 ("%e" .
"<number:day number:style=\"short\"/>")
1230 ("%h" .
"<number:month number:textual=\"true\" number:style=\"short\"/>")
1231 ("%k" .
"<number:hours number:style=\"short\"/>")
1232 ("%m" .
"<number:month number:style=\"long\"/>")
1233 ("%p" .
"<number:am-pm/>")
1234 ("%y" .
"<number:year number:style=\"short\"/>")))
1235 (case-fold-search nil
)
1236 (re (mapconcat 'identity
(mapcar 'car fmt-alist
) "\\|"))
1237 match rpl
(start 0) (filler-beg 0) filler-end filler output
)
1240 (setq fmt
(replace-regexp-in-string (car pair
) (cdr pair
) fmt t t
)))
1241 '(("\\(?:%[[:digit:]]*N\\)" .
"") ; strip ns, us and ns
1242 ("%C" .
"Y") ; replace century with year
1244 ("%G" .
"Y") ; year corresponding to iso week
1245 ("%I" .
"%H") ; hour on a 12-hour clock
1248 ("%U\\|%W" .
"%V") ; week no. starting on Sun./Mon.
1249 ("%Z" .
"") ; time zone name
1250 ("%c" .
"%Y-%M-%d %a %H:%M" ) ; locale's date and time format
1252 ("%X" .
"%x" ) ; locale's pref. time format
1253 ("%j" .
"") ; day of the year
1254 ("%l" .
"%k") ; like %I blank-padded
1255 ("%s" .
"") ; no. of secs since 1970-01-01 00:00:00 +0000
1256 ("%n" .
"<text:line-break/>")
1257 ("%r" .
"%I:%M:%S %p")
1258 ("%t" .
"<text:tab/>")
1259 ("%u\\|%w" .
"") ; numeric day of week - Mon (1-7), Sun(0-6)
1260 ("%x" .
"%Y-%M-%d %a") ; locale's pref. time format
1261 ("%z" .
"") ; time zone in numeric form
1263 (while (string-match re fmt start
)
1264 (setq match
(match-string 0 fmt
))
1265 (setq rpl
(assoc-default match fmt-alist
))
1266 (setq start
(match-end 0))
1267 (setq filler-end
(match-beginning 0))
1268 (setq filler
(substring fmt
(prog1 filler-beg
1269 (setq filler-beg
(match-end 0)))
1271 (setq filler
(and (not (string= filler
""))
1272 (format "<number:text>%s</number:text>"
1273 (org-e-odt--encode-plain-text filler
))))
1274 (setq output
(concat output
"\n" filler
"\n" rpl
)))
1275 (setq filler
(substring fmt filler-beg
))
1276 (unless (string= filler
"")
1277 (setq output
(concat output
1278 (format "\n<number:text>%s</number:text>"
1279 (org-e-odt--encode-plain-text filler
)))))
1280 (format "\n<number:date-style style:name=\"%s\" %s>%s\n</number:date-style>"
1282 (concat " number:automatic-order=\"true\""
1283 " number:format-source=\"fixed\"")
1286 (defun org-e-odt-template (contents info
)
1287 "Return complete document string after ODT conversion.
1288 CONTENTS is the transcoded contents string. RAW-DATA is the
1289 original parsed data. INFO is a plist holding export options."
1291 (let ((title (org-export-data (plist-get info
:title
) info
))
1292 (author (let ((author (plist-get info
:author
)))
1293 (if (not author
) "" (org-export-data author info
))))
1294 (email (plist-get info
:email
))
1295 (keywords (plist-get info
:keywords
))
1296 (description (plist-get info
:description
)))
1299 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
1300 <office:document-meta
1301 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
1302 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
1303 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
1304 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
1305 xmlns:ooo=\"http://openoffice.org/2004/office\"
1306 office:version=\"1.2\">
1308 (format "<dc:creator>%s</dc:creator>\n" author
)
1309 (format "<meta:initial-creator>%s</meta:initial-creator>\n" author
)
1310 ;; Date, if required.
1311 (when (plist-get info
:with-date
)
1312 ;; Check if DATE is specified as an Org-timestamp. If yes,
1313 ;; include it as meta information. Otherwise, just use
1315 (let* ((date (let ((date (plist-get info
:date
)))
1316 (and (not (cdr date
))
1317 (eq (org-element-type (car date
)) 'timestamp
)
1319 (let ((iso-date (org-e-odt--format-timestamp date nil
'iso-date
)))
1321 (format "<dc:date>%s</dc:date>\n" iso-date
)
1322 (format "<meta:creation-date>%s</meta:creation-date>\n"
1324 (format "<meta:generator>%s</meta:generator>\n"
1325 (let ((creator-info (plist-get info
:with-creator
)))
1326 (if (or (not creator-info
) (eq creator-info
'comment
)) ""
1327 (plist-get info
:creator
))))
1328 (format "<meta:keyword>%s</meta:keyword>\n" keywords
)
1329 (format "<dc:subject>%s</dc:subject>\n" description
)
1330 (format "<dc:title>%s</dc:title>\n" title
)
1332 " </office:meta>\n" "</office:document-meta>")
1333 nil
(concat org-e-odt-zip-dir
"meta.xml"))
1334 ;; Add meta.xml in to manifest.
1335 (org-e-odt-create-manifest-file-entry "text/xml" "meta.xml"))
1337 ;; Update styles file.
1338 ;; Copy styles.xml. Also dump htmlfontify styles, if there is any.
1339 ;; Write styles file.
1340 (let* ((styles-file (plist-get info
:odt-styles-file
))
1341 (styles-file (and styles-file
(read (org-trim styles-file
))))
1342 ;; Non-availability of styles.xml is not a critical
1343 ;; error. For now, throw an error.
1344 (styles-file (or styles-file
1345 org-e-odt-styles-file
1346 (expand-file-name "OrgOdtStyles.xml"
1347 org-e-odt-styles-dir
)
1348 (error "org-e-odt: Missing styles file?"))))
1350 ((listp styles-file
)
1351 (let ((archive (nth 0 styles-file
))
1352 (members (nth 1 styles-file
)))
1353 (org-e-odt--zip-extract archive members org-e-odt-zip-dir
)
1356 (when (org-file-image-p member
)
1357 (let* ((image-type (file-name-extension member
))
1358 (media-type (format "image/%s" image-type
)))
1359 (org-e-odt-create-manifest-file-entry media-type member
))))
1361 ((and (stringp styles-file
) (file-exists-p styles-file
))
1362 (let ((styles-file-type (file-name-extension styles-file
)))
1364 ((string= styles-file-type
"xml")
1365 (copy-file styles-file
(concat org-e-odt-zip-dir
"styles.xml") t
))
1366 ((member styles-file-type
'("odt" "ott"))
1367 (org-e-odt--zip-extract styles-file
"styles.xml" org-e-odt-zip-dir
)))))
1369 (error (format "Invalid specification of styles.xml file: %S"
1370 org-e-odt-styles-file
))))
1372 ;; create a manifest entry for styles.xml
1373 (org-e-odt-create-manifest-file-entry "text/xml" "styles.xml")
1375 ;; FIXME: Who is opening an empty styles.xml before this point?
1376 (with-current-buffer
1377 (find-file-noselect (concat org-e-odt-zip-dir
"styles.xml") t
)
1380 ;; Write custom styles for source blocks
1381 ;; Save STYLES used for colorizing of source blocks.
1382 ;; Update styles.xml with styles that were collected as part of
1383 ;; `org-e-odt-hfy-face-to-css' callbacks.
1384 (let ((styles (mapconcat (lambda (style) (format " %s\n" (cddr style
)))
1385 hfy-user-sheet-assoc
"")))
1387 (goto-char (point-min))
1388 (when (re-search-forward "</office:styles>" nil t
)
1389 (goto-char (match-beginning 0))
1390 (insert "\n<!-- Org Htmlfontify Styles -->\n" styles
"\n"))))
1392 ;; Update styles.xml - take care of outline numbering
1394 ;; Don't make automatic backup of styles.xml file. This setting
1395 ;; prevents the backed-up styles.xml file from being zipped in to
1396 ;; odt file. This is more of a hackish fix. Better alternative
1397 ;; would be to fix the zip command so that the output odt file
1398 ;; includes only the needed files and excludes any auto-generated
1399 ;; extra files like backups and auto-saves etc etc. Note that
1400 ;; currently the zip command zips up the entire temp directory so
1401 ;; that any auto-generated files created under the hood ends up in
1402 ;; the resulting odt file.
1403 (set (make-local-variable 'backup-inhibited
) t
)
1405 ;; Outline numbering is retained only upto LEVEL.
1406 ;; To disable outline numbering pass a LEVEL of 0.
1408 (goto-char (point-min))
1410 "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
1412 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1413 (while (re-search-forward regex nil t
)
1414 (unless (let ((sec-num (plist-get info
:section-numbers
))
1415 (level (string-to-number (match-string 2))))
1416 (if (wholenump sec-num
) (<= level sec-num
) sec-num
))
1417 (replace-match replacement t nil
))))
1419 ;; Update content.xml.
1421 (let* ( ;; `org-display-custom-times' should be accessed right
1422 ;; within the context of the Org buffer. So obtain it's
1423 ;; value before moving on to temp-buffer context down below.
1425 (if org-display-custom-times
1426 (cons (substring (car org-time-stamp-custom-formats
) 1 -
1)
1427 (substring (cdr org-time-stamp-custom-formats
) 1 -
1))
1428 '("%Y-%M-%d %a" .
"%Y-%M-%d %a %H:%M"))))
1430 (insert-file-contents
1431 (or org-e-odt-content-template-file
1432 (expand-file-name "OrgOdtContentTemplate.xml"
1433 org-e-odt-styles-dir
)))
1434 ;; Write automatic styles.
1435 ;; - Position the cursor.
1436 (goto-char (point-min))
1437 (re-search-forward " </office:automatic-styles>" nil t
)
1438 (goto-char (match-beginning 0))
1439 ;; - Dump automatic table styles.
1440 (loop for
(style-name props
) in
1441 (plist-get org-e-odt-automatic-styles
'Table
) do
1442 (when (setq props
(or (plist-get props
:rel-width
) 96))
1443 (insert (format org-e-odt-table-style-format style-name props
))))
1444 ;; - Dump date-styles.
1445 (when org-e-odt-use-date-fields
1446 (insert (org-e-odt--build-date-styles (car custom-time-fmts
)
1448 (org-e-odt--build-date-styles (cdr custom-time-fmts
)
1450 ;; Update display level.
1451 ;; - Remove existing sequence decls. Also position the cursor.
1452 (goto-char (point-min))
1453 (when (re-search-forward "<text:sequence-decls" nil t
)
1454 (delete-region (match-beginning 0)
1455 (re-search-forward "</text:sequence-decls>" nil nil
)))
1456 ;; Update sequence decls according to user preference.
1459 "\n<text:sequence-decls>\n%s\n</text:sequence-decls>"
1463 "<text:sequence-decl text:display-outline-level=\"%d\" text:name=\"%s\"/>"
1464 org-e-odt-display-outline-level
(nth 1 x
)))
1465 org-e-odt-category-map-alist
"\n")))
1466 ;; Position the cursor to document body.
1467 (goto-char (point-min))
1468 (re-search-forward "</office:text>" nil nil
)
1469 (goto-char (match-beginning 0))
1471 ;; Preamble - Title, Author, Date etc.
1473 (let* ((title (org-export-data (plist-get info
:title
) info
))
1474 (author (and (plist-get info
:with-author
)
1475 (let ((auth (plist-get info
:author
)))
1476 (and auth
(org-export-data auth info
)))))
1477 (email (plist-get info
:email
))
1478 ;; Switch on or off above vars based on user settings
1479 (author (and (plist-get info
:with-author
) (or author email
)))
1480 (email (and (plist-get info
:with-email
) email
)))
1485 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1486 "OrgTitle" (format "\n<text:title>%s</text:title>" title
))
1488 "\n<text:p text:style-name=\"OrgTitle\"/>"))
1490 ((and author
(not email
))
1493 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1495 (format "<text:initial-creator>%s</text:initial-creator>" author
))
1497 "\n<text:p text:style-name=\"OrgSubtitle\"/>"))
1499 ;; Author and E-mail.
1502 "\n<text:p text:style-name=\"%s\">%s</text:p>"
1505 "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
1506 (concat "mailto:" email
)
1507 (format "<text:initial-creator>%s</text:initial-creator>" author
)))
1509 "\n<text:p text:style-name=\"OrgSubtitle\"/>")))
1510 ;; Date, if required.
1511 (when (plist-get info
:with-date
)
1512 (let* ((date (plist-get info
:date
))
1513 ;; Check if DATE is specified as a timestamp.
1514 (timestamp (and (not (cdr date
))
1515 (eq (org-element-type (car date
)) 'timestamp
)
1518 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1520 (if (and org-e-odt-use-date-fields timestamp
)
1521 (org-e-odt--format-timestamp (car date
))
1522 (org-export-data (plist-get info
:date
) info
)))
1524 "<text:p text:style-name=\"OrgSubtitle\"/>"))))))
1525 ;; Table of Contents
1526 (let* ((with-toc (plist-get info
:with-toc
))
1527 (depth (and with-toc
(if (wholenump with-toc
)
1529 (plist-get info
:headline-levels
)))))
1530 (when depth
(insert (or (org-e-odt-toc depth info
) ""))))
1534 (buffer-substring-no-properties (point-min) (point-max)))))
1538 ;;; Transcode Functions
1542 (defun org-e-odt-bold (bold contents info
)
1543 "Transcode BOLD from Org to ODT.
1544 CONTENTS is the text with bold markup. INFO is a plist holding
1545 contextual information."
1546 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1552 (defun org-e-odt-center-block (center-block contents info
)
1553 "Transcode a CENTER-BLOCK element from Org to ODT.
1554 CONTENTS holds the contents of the center block. INFO is a plist
1555 holding contextual information."
1561 (defun org-e-odt-clock (clock contents info
)
1562 "Transcode a CLOCK element from Org to ODT.
1563 CONTENTS is nil. INFO is a plist used as a communication
1565 (let ((timestamp (org-element-property :value clock
))
1566 (duration (org-element-property :duration clock
)))
1567 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1568 (if (eq (org-element-type (org-export-get-next-element clock info
))
1569 'clock
) "OrgClock" "OrgClockLastLine")
1571 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1572 "OrgClockKeyword" org-clock-string
)
1573 (org-e-odt-timestamp timestamp contents info
)
1574 (and duration
(format " (%s)" duration
))))))
1579 (defun org-e-odt-code (code contents info
)
1580 "Transcode a CODE object from Org to ODT.
1581 CONTENTS is nil. INFO is a plist used as a communication
1583 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1584 "OrgCode" (org-element-property :value code
)))
1589 ;; Comments are ignored.
1594 ;; Comment Blocks are ignored.
1599 (defun org-e-odt-drawer (drawer contents info
)
1600 "Transcode a DRAWER element from Org to ODT.
1601 CONTENTS holds the contents of the block. INFO is a plist
1602 holding contextual information."
1603 (let* ((name (org-element-property :drawer-name drawer
))
1604 (output (if (functionp org-e-odt-format-drawer-function
)
1605 (funcall org-e-odt-format-drawer-function
1607 ;; If there's no user defined function: simply
1608 ;; display contents of the drawer.
1615 (defun org-e-odt-dynamic-block (dynamic-block contents info
)
1616 "Transcode a DYNAMIC-BLOCK element from Org to ODT.
1617 CONTENTS holds the contents of the block. INFO is a plist
1618 holding contextual information. See `org-export-data'."
1624 (defun org-e-odt-entity (entity contents info
)
1625 "Transcode an ENTITY object from Org to ODT.
1626 CONTENTS are the definition itself. INFO is a plist holding
1627 contextual information."
1628 (org-element-property :utf-8 entity
))
1633 (defun org-e-odt-example-block (example-block contents info
)
1634 "Transcode a EXAMPLE-BLOCK element from Org to ODT.
1635 CONTENTS is nil. INFO is a plist holding contextual information."
1636 (org-e-odt-format-code example-block info
))
1641 (defun org-e-odt-export-snippet (export-snippet contents info
)
1642 "Transcode a EXPORT-SNIPPET object from Org to ODT.
1643 CONTENTS is nil. INFO is a plist holding contextual information."
1644 (when (eq (org-export-snippet-backend export-snippet
) 'e-odt
)
1645 (org-element-property :value export-snippet
)))
1650 (defun org-e-odt-export-block (export-block contents info
)
1651 "Transcode a EXPORT-BLOCK element from Org to ODT.
1652 CONTENTS is nil. INFO is a plist holding contextual information."
1653 (when (string= (org-element-property :type export-block
) "ODT")
1654 (org-remove-indentation (org-element-property :value export-block
))))
1659 (defun org-e-odt-fixed-width (fixed-width contents info
)
1660 "Transcode a FIXED-WIDTH element from Org to ODT.
1661 CONTENTS is nil. INFO is a plist holding contextual information."
1662 (org-e-odt-do-format-code (org-element-property :value fixed-width
)))
1665 ;;;; Footnote Definition
1667 ;; Footnote Definitions are ignored.
1670 ;;;; Footnote Reference
1672 (defun org-e-odt-footnote-reference (footnote-reference contents info
)
1673 "Transcode a FOOTNOTE-REFERENCE element from Org to ODT.
1674 CONTENTS is nil. INFO is a plist holding contextual information."
1675 (let ((--format-footnote-definition
1678 (setq n
(format "%d" n
))
1679 (let ((id (concat "fn" n
))
1680 (note-class "footnote")
1681 (par-style "Footnote"))
1683 "<text:note text:id=\"%s\" text:note-class=\"%s\">%s</text:note>"
1686 (format "<text:note-citation>%s</text:note-citation>" n
)
1687 (format "<text:note-body>%s</text:note-body>" def
)))))))
1688 (--format-footnote-reference
1691 (setq n
(format "%d" n
))
1692 (let ((note-class "footnote")
1694 (ref-name (concat "fn" n
)))
1696 "<text:span text:style-name=\"%s\">%s</text:span>"
1698 (format "<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:note-ref>"
1699 note-class ref-format ref-name n
)))))))
1701 ;; Insert separator between two footnotes in a row.
1702 (let ((prev (org-export-get-previous-element footnote-reference info
)))
1703 (and (eq (org-element-type prev
) 'footnote-reference
)
1704 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1705 "OrgSuperscript" ",")))
1706 ;; Trancode footnote reference.
1707 (let ((n (org-export-get-footnote-number footnote-reference info
)))
1709 ((not (org-export-footnote-first-reference-p footnote-reference info
))
1710 (funcall --format-footnote-reference n
))
1711 ;; Inline definitions are secondary strings.
1712 ;; Non-inline footnotes definitions are full Org data.
1714 (let* ((raw (org-export-get-footnote-definition footnote-reference
1716 (def (let ((def (org-trim (org-export-data raw info
))))
1717 (if (eq (org-element-type raw
) 'org-data
) def
1718 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1720 (funcall --format-footnote-definition n def
))))))))
1725 (defun* org-e-odt-format-headline
1726 (todo todo-type priority text tags
1727 &key level section-number headline-label
&allow-other-keys
)
1731 (let ((style (if (member todo org-done-keywords
) "OrgDone" "OrgTodo")))
1732 (format "<text:span text:style-name=\"%s\">%s</text:span> "
1735 (let* ((style (format "OrgPriority-%s" priority
))
1736 (priority (format "[#%c]" priority
)))
1737 (format "<text:span text:style-name=\"%s\">%s</text:span> "
1745 (format "<text:span text:style-name=\"%s\">[%s]</text:span>"
1746 "OrgTags" (mapconcat
1749 "<text:span text:style-name=\"%s\">%s</text:span>"
1750 "OrgTag" tag
)) tags
" : "))))))
1752 (defun org-e-odt-format-headline--wrap (headline translations info
1753 &optional format-function
1755 "Transcode an HEADLINE element from Org to ODT.
1756 CONTENTS holds the contents of the headline. INFO is a plist
1757 holding contextual information."
1758 (setq translations
(or translations
(plist-get info
:translate-alist
)))
1759 (let* ((level (+ (org-export-get-relative-level headline info
)))
1760 (headline-number (org-export-get-headline-number headline info
))
1761 (section-number (and (org-export-numbered-headline-p headline info
)
1762 (mapconcat 'number-to-string
1763 headline-number
".")))
1764 (todo (and (plist-get info
:with-todo-keywords
)
1765 (let ((todo (org-element-property :todo-keyword headline
)))
1766 (and todo
(org-export-data-with-translations
1767 todo translations info
)))))
1768 (todo-type (and todo
(org-element-property :todo-type headline
)))
1769 (priority (and (plist-get info
:with-priority
)
1770 (org-element-property :priority headline
)))
1771 (text (org-export-data-with-translations
1772 (org-element-property :title headline
) translations info
))
1773 (tags (and (plist-get info
:with-tags
)
1774 (org-export-get-tags headline info
)))
1775 (headline-label (concat "sec-" (mapconcat 'number-to-string
1776 headline-number
"-")))
1777 (format-function (cond
1778 ((functionp format-function
) format-function
)
1779 ((functionp org-e-odt-format-headline-function
)
1781 (lambda (todo todo-type priority text tags
1783 (funcall org-e-odt-format-headline-function
1784 todo todo-type priority text tags
))))
1785 (t 'org-e-odt-format-headline
))))
1786 (apply format-function
1787 todo todo-type priority text tags
1788 :headline-label headline-label
:level level
1789 :section-number section-number extra-keys
)))
1791 (defun org-e-odt-headline (headline contents info
)
1792 "Transcode an HEADLINE element from Org to ODT.
1793 CONTENTS holds the contents of the headline. INFO is a plist
1794 holding contextual information."
1795 ;; Case 1: This is a footnote section: ignore it.
1796 (unless (org-element-property :footnote-section-p headline
)
1797 (let* ((text (org-export-data (org-element-property :title headline
) info
))
1798 ;; Create the headline text.
1799 (full-text (org-e-odt-format-headline--wrap headline nil info
))
1800 ;; Get level relative to current parsed data.
1801 (level (org-export-get-relative-level headline info
))
1802 ;; Get canonical label for the headline.
1803 (id (concat "sec-" (mapconcat 'number-to-string
1804 (org-export-get-headline-number
1805 headline info
) "-")))
1806 ;; Get user-specified labels for the headline.
1807 (extra-ids (list (org-element-property :custom-id headline
)
1808 (org-element-property :id headline
)))
1811 (mapconcat (lambda (x)
1813 (let ((x (if (org-uuidgen-p x
) (concat "ID-" x
) x
)))
1815 "" (org-export-solidify-link-text x
)))))
1818 (anchored-title (org-e-odt--target full-text id
)))
1820 ;; Case 2. This is a deep sub-tree: export it as a list item.
1821 ;; Also export as items headlines for which no section
1822 ;; format has been found.
1823 ((org-export-low-level-p headline info
)
1824 ;; Build the real contents of the sub-tree.
1826 (and (org-export-first-sibling-p headline info
)
1827 (format "\n<text:list text:style-name=\"%s\" %s>"
1828 ;; Choose style based on list type.
1829 (if (org-export-numbered-headline-p headline info
)
1830 "OrgNumberedList" "OrgBulletedList")
1831 ;; If top-level list, re-start numbering. Otherwise,
1832 ;; continue numbering.
1833 (format "text:continue-numbering=\"%s\""
1834 (let* ((parent (org-export-get-parent-headline
1837 (org-export-low-level-p parent info
))
1839 (let ((headline-has-table-p
1840 (let ((section (assq 'section
(org-element-contents headline
))))
1841 (assq 'table
(and section
(org-element-contents section
))))))
1842 (format "\n<text:list-item>\n%s\n%s"
1844 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1846 (concat extra-targets anchored-title
))
1848 (if headline-has-table-p
1849 "</text:list-header>"
1850 "</text:list-item>")))
1851 (and (org-export-last-sibling-p headline info
)
1853 ;; Case 3. Standard headline. Export it as a section.
1857 "\n<text:h text:style-name=\"%s\" text:outline-level=\"%s\">%s</text:h>"
1858 (format "Heading_20_%s" level
)
1860 (concat extra-targets anchored-title
))
1864 ;;;; Horizontal Rule
1866 (defun org-e-odt-horizontal-rule (horizontal-rule contents info
)
1867 "Transcode an HORIZONTAL-RULE object from Org to ODT.
1868 CONTENTS is nil. INFO is a plist holding contextual information."
1869 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1870 "Horizontal_20_Line" ""))
1873 ;;;; Inline Babel Call
1875 ;; Inline Babel Calls are ignored.
1878 ;;;; Inline Src Block
1880 (defun org-e-odt--find-verb-separator (s)
1881 "Return a character not used in string S.
1882 This is used to choose a separator for constructs like \\verb."
1883 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1884 (loop for c across ll
1885 when
(not (string-match (regexp-quote (char-to-string c
)) s
))
1886 return
(char-to-string c
))))
1888 (defun org-e-odt-inline-src-block (inline-src-block contents info
)
1889 "Transcode an INLINE-SRC-BLOCK element from Org to ODT.
1890 CONTENTS holds the contents of the item. INFO is a plist holding
1891 contextual information."
1892 (let* ((org-lang (org-element-property :language inline-src-block
))
1893 (code (org-element-property :value inline-src-block
))
1894 (separator (org-e-odt--find-verb-separator code
)))
1900 (defun org-e-odt-inlinetask (inlinetask contents info
)
1901 "Transcode an INLINETASK element from Org to ODT.
1902 CONTENTS holds the contents of the block. INFO is a plist
1903 holding contextual information."
1905 ;; If `org-e-odt-format-inlinetask-function' is provided, call it
1906 ;; with appropriate arguments.
1907 ((functionp org-e-odt-format-inlinetask-function
)
1908 (let ((format-function
1910 (lambda (todo todo-type priority text tags
1911 &key contents
&allow-other-keys
)
1912 (funcall org-e-odt-format-inlinetask-function
1913 todo todo-type priority text tags contents
)))))
1914 (org-e-odt-format-headline--wrap
1915 inlinetask nil info format-function
:contents contents
)))
1916 ;; Otherwise, use a default template.
1918 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1922 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1923 "OrgInlineTaskHeading"
1924 (org-e-odt-format-headline--wrap inlinetask nil info
))
1926 nil nil
"OrgInlineTaskFrame" " style:rel-width=\"100%\"")))))
1930 (defun org-e-odt-italic (italic contents info
)
1931 "Transcode ITALIC from Org to ODT.
1932 CONTENTS is the text with italic markup. INFO is a plist holding
1933 contextual information."
1934 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1935 "Emphasis" contents
))
1940 (defun org-e-odt-item (item contents info
)
1941 "Transcode an ITEM element from Org to ODT.
1942 CONTENTS holds the contents of the item. INFO is a plist holding
1943 contextual information."
1944 (let* ((plain-list (org-export-get-parent item
))
1945 (type (org-element-property :type plain-list
))
1946 (counter (org-element-property :counter item
))
1947 (tag (let ((tag (org-element-property :tag item
)))
1949 (concat (org-e-odt--checkbox item
)
1950 (org-export-data tag info
))))))
1952 ((ordered unordered descriptive-1 descriptive-2
)
1953 (format "\n<text:list-item>\n%s\n%s"
1955 (let* ((--element-has-a-table-p
1957 (lambda (element info
)
1958 (loop for el in
(org-element-contents element
)
1959 thereis
(eq (org-element-type el
) 'table
))))))
1961 ((funcall --element-has-a-table-p item info
)
1962 "</text:list-header>")
1963 (t "</text:list-item>")))))
1964 (t (error "Unknown list type: %S" type
)))))
1968 (defun org-e-odt-keyword (keyword contents info
)
1969 "Transcode a KEYWORD element from Org to ODT.
1970 CONTENTS is nil. INFO is a plist holding contextual information."
1971 (let ((key (org-element-property :key keyword
))
1972 (value (org-element-property :value keyword
)))
1974 ((string= key
"ODT") value
)
1975 ((string= key
"INDEX")
1978 ((string= key
"TARGET") nil
)
1979 ((string= key
"toc")
1980 (let ((value (downcase value
)))
1982 ((string-match "\\<headlines\\>" value
)
1983 (let ((depth (or (and (string-match "[0-9]+" value
)
1984 (string-to-number (match-string 0 value
)))
1985 (plist-get info
:with-toc
))))
1986 (when (wholenump depth
) (org-e-odt-toc depth info
))))
1987 ((member value
'("tables" "figures" "listings"))
1992 ;;;; Latex Environment
1995 (eval-after-load 'org-odt
1996 '(ad-deactivate 'org-format-latex-as-mathml
))
1998 ;; (defadvice org-format-latex-as-mathml ; FIXME
1999 ;; (after org-e-odt-protect-latex-fragment activate)
2000 ;; "Encode LaTeX fragment as XML.
2001 ;; Do this when translation to MathML fails."
2002 ;; (when (or (not (> (length ad-return-value) 0))
2003 ;; (get-text-property 0 'org-protected ad-return-value))
2004 ;; (setq ad-return-value
2005 ;; (org-propertize (org-e-odt--encode-plain-text (ad-get-arg 0))
2006 ;; 'org-protected t))))
2008 (defun org-e-odt-latex-environment (latex-environment contents info
)
2009 "Transcode a LATEX-ENVIRONMENT element from Org to ODT.
2010 CONTENTS is nil. INFO is a plist holding contextual information."
2011 (let* ((latex-frag (org-remove-indentation
2012 (org-element-property :value latex-environment
))))
2013 (org-e-odt-do-format-code latex-frag
)))
2018 ;; (when latex-frag ; FIXME
2019 ;; (setq href (org-propertize href :title "LaTeX Fragment"
2020 ;; :description latex-frag)))
2022 ;; provide descriptions
2024 (defun org-e-odt-latex-fragment (latex-fragment contents info
)
2025 "Transcode a LATEX-FRAGMENT object from Org to ODT.
2026 CONTENTS is nil. INFO is a plist holding contextual information."
2027 (let* ((latex-frag (org-element-property :value latex-fragment
))
2028 (processing-type (plist-get info
:LaTeX-fragments
)))
2029 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2030 "OrgCode" (org-e-odt--encode-plain-text latex-frag t
))))
2035 (defun org-e-odt-line-break (line-break contents info
)
2036 "Transcode a LINE-BREAK object from Org to ODT.
2037 CONTENTS is nil. INFO is a plist holding contextual information."
2038 "<text:line-break/>\n")
2043 ;;;; Links :: Label references
2045 (defun org-e-odt--enumerate (element info
&optional predicate n
)
2046 (when predicate
(assert (funcall predicate element info
)))
2047 (let* ((--numbered-parent-headline-at-<=-n
2049 (lambda (element n info
)
2050 (loop for x in
(org-export-get-genealogy element
)
2051 thereis
(and (eq (org-element-type x
) 'headline
)
2052 (<= (org-export-get-relative-level x info
) n
)
2053 (org-export-numbered-headline-p x info
)
2057 (lambda (element scope info
&optional predicate
)
2060 (or scope
(plist-get info
:parse-tree
))
2061 (org-element-type element
)
2063 (and (or (not predicate
) (funcall predicate el info
))
2067 info
'first-match
)))))
2068 (scope (funcall --numbered-parent-headline-at-
<=-n
2069 element
(or n org-e-odt-display-outline-level
) info
))
2070 (ordinal (funcall --enumerate element scope info predicate
))
2075 (mapconcat 'number-to-string
2076 (org-export-get-headline-number scope info
) "."))
2080 (number-to-string ordinal
))))
2083 (defun org-e-odt-format-label (element info op
)
2084 (assert (memq (org-element-type element
) '(link table src-block paragraph
)))
2085 (let* ((caption-from
2086 (case (org-element-type element
)
2087 (link (org-export-get-parent-element element
))
2089 ;; Get label and caption.
2090 (label (org-element-property :name caption-from
))
2091 (caption (org-export-get-caption caption-from
))
2092 (short-caption (org-export-get-caption caption-from t
))
2093 ;; Transcode captions.
2094 (caption (and caption
(org-export-data caption info
)))
2095 ;; Currently short caption are sneaked in as object names.
2097 ;; The advantages are:
2099 ;; - Table Of Contents: Currently, there is no support for
2100 ;; building TOC for figures, listings and tables. See
2101 ;; `org-e-odt-keyword'. User instead has to rely on
2102 ;; external application for building such indices. Within
2103 ;; LibreOffice, building an "Illustration Index" or "Index
2104 ;; of Tables" will create a table with long captions (only)
2105 ;; and building a table with "Object names" will create a
2106 ;; table with short captions.
2108 ;; - Easy navigation: In LibreOffice, object names are
2109 ;; offered via the navigation bar. This way one can
2110 ;; quickly locate and jump to object of his choice in the
2111 ;; exported document.
2113 ;; The main disadvantage is that there cannot be any markups
2114 ;; within object names i.e., one cannot embolden, italicize
2115 ;; or underline text within short caption. So suppress
2116 ;; generation of <text:span >...</text:span> and other
2117 ;; markups by overriding the default translators. We
2118 ;; probably shouldn't be suppressing translators for all
2119 ;; elements in `org-element-all-objects', but for now this
2122 (let ((short-caption (or short-caption caption
))
2123 (translations (nconc (mapcar
2125 (cons type
(lambda (data contents info
)
2127 org-element-all-objects
)
2128 (plist-get info
:translate-alist
))))
2130 (org-export-data-with-translations short-caption
2131 translations info
)))))
2132 (when (or label caption
)
2133 (let* ((default-category
2134 (case (org-element-type element
)
2136 (src-block "__Listing__")
2139 ((org-e-odt--enumerable-latex-image-p element info
)
2141 ((org-e-odt--enumerable-image-p element info
)
2143 ((org-e-odt--enumerable-formula-p element info
)
2145 (t (error "Don't know how to format label for link: %S"
2147 (t (error "Don't know how to format label for element type: %s"
2148 (org-element-type element
)))))
2150 (assert default-category
)
2151 (destructuring-bind (counter label-style category predicate
)
2152 (assoc-default default-category org-e-odt-category-map-alist
)
2153 ;; Compute sequence number of the element.
2154 (setq seqno
(org-e-odt--enumerate element info predicate
))
2155 ;; Localize category string.
2156 (setq category
(org-export-translate category
:utf-8 info
))
2158 ;; Case 1: Handle Label definition.
2160 ;; Assign an internal label, if user has not provided one
2161 (setq label
(or label
(format "%s-%s" default-category seqno
)))
2162 (setq label
(org-export-solidify-link-text label
))
2165 ;; Sneak in a bookmark. The bookmark is used when the
2166 ;; labeled element is referenced with a link that
2167 ;; provides it's own description.
2168 (format "\n<text:bookmark text:name=\"%s\"/>" label
)
2169 ;; Label definition: Typically formatted as below:
2170 ;; CATEGORY SEQ-NO: LONG CAPTION
2172 (cadr (assoc-string label-style org-e-odt-label-styles t
))
2175 "<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">%s</text:sequence>"
2176 label counter counter seqno
))
2177 (?c .
,(or caption
"")))))
2179 ;; Case 2: Handle Label reference.
2182 (setq label
(org-export-solidify-link-text label
))
2183 (let* ((fmt (cddr (assoc-string label-style org-e-odt-label-styles t
)))
2186 (format "<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:sequence-ref>"
2187 fmt1 label
(format-spec fmt2
`((?e .
,category
)
2189 (t (error "Unknown %S on label" op
))))))))
2192 ;;;; Links :: Inline Images
2194 (defun org-e-odt--copy-image-file (path)
2195 "Returns the internal name of the file"
2196 (let* ((image-type (file-name-extension path
))
2197 (media-type (format "image/%s" image-type
))
2198 (target-dir "Images/")
2200 (format "%s%04d.%s" target-dir
2201 (incf org-e-odt-embedded-images-count
) image-type
)))
2202 (message "Embedding %s as %s ..."
2203 (substring-no-properties path
) target-file
)
2205 (when (= 1 org-e-odt-embedded-images-count
)
2206 (make-directory (concat org-e-odt-zip-dir target-dir
))
2207 (org-e-odt-create-manifest-file-entry "" target-dir
))
2209 (copy-file path
(concat org-e-odt-zip-dir target-file
) 'overwrite
)
2210 (org-e-odt-create-manifest-file-entry media-type target-file
)
2213 (defun org-e-odt--image-size (file &optional user-width
2214 user-height scale dpi embed-as
)
2215 (let* ((--pixels-to-cms
2216 (function (lambda (pixels dpi
)
2217 (let ((cms-per-inch 2.54)
2218 (inches (/ pixels dpi
)))
2219 (* cms-per-inch inches
)))))
2222 (lambda (size-in-pixels dpi
)
2224 (cons (funcall --pixels-to-cms
(car size-in-pixels
) dpi
)
2225 (funcall --pixels-to-cms
(cdr size-in-pixels
) dpi
))))))
2226 (dpi (or dpi org-e-odt-pixels-per-inch
))
2227 (anchor-type (or embed-as
"paragraph"))
2228 (user-width (and (not scale
) user-width
))
2229 (user-height (and (not scale
) user-height
))
2232 (not (and user-height user-width
))
2235 (and (executable-find "identify")
2236 (let ((size-in-pixels
2237 (let ((dim (shell-command-to-string
2238 (format "identify -format \"%%w:%%h\" \"%s\""
2240 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim
)
2241 (cons (string-to-number (match-string 1 dim
))
2242 (string-to-number (match-string 2 dim
)))))))
2243 (funcall --size-in-cms size-in-pixels dpi
)))
2245 (let ((size-in-pixels
2246 (ignore-errors ; Emacs could be in batch mode
2248 (image-size (create-image file
) 'pixels
))))
2249 (funcall --size-in-cms size-in-pixels dpi
))
2250 ;; Use hard-coded values.
2251 (cdr (assoc-string anchor-type
2252 org-e-odt-default-image-sizes-alist
))
2254 (error "Cannot determine image size, aborting"))))
2255 (width (car size
)) (height (cdr size
)))
2258 (setq width
(* width scale
) height
(* height scale
)))
2259 ((and user-height user-width
)
2260 (setq width user-width height user-height
))
2262 (setq width
(* user-height
(/ width height
)) height user-height
))
2264 (setq height
(* user-width
(/ height width
)) width user-width
))
2266 ;; ensure that an embedded image fits comfortably within a page
2267 (let ((max-width (car org-e-odt-max-image-size
))
2268 (max-height (cdr org-e-odt-max-image-size
)))
2269 (when (or (> width max-width
) (> height max-height
))
2270 (let* ((scale1 (/ max-width width
))
2271 (scale2 (/ max-height height
))
2272 (scale (min scale1 scale2
)))
2273 (setq width
(* scale width
) height
(* scale height
)))))
2274 (cons width height
)))
2276 (defun org-e-odt-link--inline-image (element info
)
2277 "Return ODT code for an inline image.
2278 LINK is the link pointing to the inline image. INFO is a plist
2279 used as a communication channel."
2280 (assert (eq (org-element-type element
) 'link
))
2281 (let* ((src (let* ((type (org-element-property :type element
))
2282 (raw-path (org-element-property :path element
)))
2283 (cond ((member type
'("http" "https"))
2284 (concat type
":" raw-path
))
2285 ((file-name-absolute-p raw-path
)
2286 (expand-file-name raw-path
))
2288 (src-expanded (if (file-name-absolute-p src
) src
2289 (expand-file-name src
(file-name-directory
2290 (plist-get info
:input-file
)))))
2292 "\n<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>"
2293 (org-e-odt--copy-image-file src-expanded
)))
2294 ;; Extract attributes from #+ATTR_ODT line.
2295 (attr-from (case (org-element-type element
)
2296 (link (org-export-get-parent-element element
))
2298 ;; Convert attributes to a plist.
2299 (attr-plist (org-export-read-attribute :attr_odt attr-from
))
2300 ;; Handle `:anchor', `:style' and `:attributes' properties.
2302 (car (assoc-string (plist-get attr-plist
:anchor
)
2303 '(("as-char") ("paragraph") ("page")) t
)))
2305 (and user-frame-anchor
(plist-get attr-plist
:style
)))
2307 (and user-frame-anchor
(plist-get attr-plist
:attributes
)))
2309 (list user-frame-style user-frame-attrs user-frame-anchor
))
2310 ;; (embed-as (or embed-as user-frame-anchor "paragraph"))
2312 ;; handle `:width', `:height' and `:scale' properties.
2313 (size (org-e-odt--image-size
2314 src-expanded
(plist-get attr-plist
:width
)
2315 (plist-get attr-plist
:height
)
2316 (plist-get attr-plist
:scale
) nil
;; embed-as
2319 (width (car size
)) (height (cdr size
))
2320 (standalone-link-p (org-e-odt--standalone-link-p element info
))
2321 (embed-as (if standalone-link-p
"paragraph" "as-char"))
2322 (captions (org-e-odt-format-label element info
'definition
))
2323 (caption (car captions
)) (short-caption (cdr captions
))
2324 (entity (concat (and caption
"Captioned") embed-as
"Image"))
2325 ;; Check if this link was created by LaTeX-to-PNG converter.
2326 (replaces (org-element-property
2327 :replaces
(if (not standalone-link-p
) element
2328 (org-export-get-parent-element element
))))
2329 ;; If yes, note down the type of the element - LaTeX Fragment
2330 ;; or LaTeX environment. It will go in to frame title.
2331 (title (and replaces
(capitalize
2332 (symbol-name (org-element-type replaces
)))))
2334 ;; If yes, note down it's contents. It will go in to frame
2335 ;; description. This quite useful for debugging.
2336 (desc (and replaces
(org-element-property :value replaces
))))
2337 (org-e-odt--render-image/formula entity href width height
2338 captions user-frame-params title desc
)))
2341 ;;;; Links :: Math formula
2343 (defun org-e-odt-link--inline-formula (element info
)
2344 (let* ((src (let* ((type (org-element-property :type element
))
2345 (raw-path (org-element-property :path element
)))
2347 ((file-name-absolute-p raw-path
)
2348 (expand-file-name raw-path
))
2350 (src-expanded (if (file-name-absolute-p src
) src
2351 (expand-file-name src
(file-name-directory
2352 (plist-get info
:input-file
)))))
2355 "\n<draw:object %s xlink:href=\"%s\" xlink:type=\"simple\"/>"
2356 " xlink:show=\"embed\" xlink:actuate=\"onLoad\""
2357 (file-name-directory (org-e-odt--copy-formula-file src-expanded
))))
2358 (standalone-link-p (org-e-odt--standalone-link-p element info
))
2359 (embed-as (if standalone-link-p
'paragraph
'character
))
2360 (captions (org-e-odt-format-label element info
'definition
))
2361 (caption (car captions
)) (short-caption (cdr captions
))
2362 ;; Check if this link was created by LaTeX-to-MathML
2364 (replaces (org-element-property
2365 :replaces
(if (not standalone-link-p
) element
2366 (org-export-get-parent-element element
))))
2367 ;; If yes, note down the type of the element - LaTeX Fragment
2368 ;; or LaTeX environment. It will go in to frame title.
2369 (title (and replaces
(capitalize
2370 (symbol-name (org-element-type replaces
)))))
2372 ;; If yes, note down it's contents. It will go in to frame
2373 ;; description. This quite useful for debugging.
2374 (desc (and replaces
(org-element-property :value replaces
)))
2377 ((eq embed-as
'character
)
2378 (org-e-odt--render-image/formula
"InlineFormula" href width height
2379 nil nil title desc
))
2381 (let* ((equation (org-e-odt--render-image/formula
2382 "CaptionedDisplayFormula" href width height
2383 captions nil title desc
))
2385 (let* ((org-e-odt-category-map-alist
2386 '(("__MathFormula__" "Text" "math-label" "Equation"
2387 org-e-odt--enumerable-formula-p
))))
2388 (car (org-e-odt-format-label element info
'definition
)))))
2389 (concat equation
"<text:tab/>" label
))))))
2391 (defun org-e-odt--copy-formula-file (src-file)
2392 "Returns the internal name of the file"
2393 (let* ((target-dir (format "Formula-%04d/"
2394 (incf org-e-odt-embedded-formulas-count
)))
2395 (target-file (concat target-dir
"content.xml")))
2396 ;; Create a directory for holding formula file. Also enter it in
2398 (make-directory (concat org-e-odt-zip-dir target-dir
))
2399 (org-e-odt-create-manifest-file-entry
2400 "application/vnd.oasis.opendocument.formula" target-dir
"1.2")
2401 ;; Copy over the formula file from user directory to zip
2403 (message "Embedding %s as %s ..." src-file target-file
)
2404 (let ((case-fold-search nil
))
2407 ((string-match "\\.\\(mathml\\|mml\\)\\'" src-file
)
2408 (copy-file src-file
(concat org-e-odt-zip-dir target-file
) 'overwrite
))
2409 ;; Case 2: OpenDocument formula.
2410 ((string-match "\\.odf\\'" src-file
)
2411 (org-e-odt--zip-extract src-file
"content.xml"
2412 (concat org-e-odt-zip-dir target-dir
)))
2413 (t (error "%s is not a formula file" src-file
))))
2414 ;; Enter the formula file in to manifest.
2415 (org-e-odt-create-manifest-file-entry "text/xml" target-file
)
2420 (defun org-e-odt--render-image/formula
(cfg-key href width height
&optional
2421 captions user-frame-params
2422 &rest title-and-desc
)
2423 (let* ((frame-cfg-alist
2424 ;; Each element of this alist is of the form (CFG-HANDLE
2425 ;; INNER-FRAME-PARAMS OUTER-FRAME-PARAMS).
2427 ;; CFG-HANDLE is the key to the alist.
2429 ;; INNER-FRAME-PARAMS and OUTER-FRAME-PARAMS specify the
2430 ;; frame params for INNER-FRAME and OUTER-FRAME
2431 ;; respectively. See below.
2433 ;; Configurations that are meant to be applied to
2434 ;; non-captioned image/formula specifies no
2435 ;; OUTER-FRAME-PARAMS.
2439 ;; INNER-FRAME :: Frame that directly surrounds an
2442 ;; OUTER-FRAME :: Frame that encloses the INNER-FRAME. This
2443 ;; frame also contains the caption, if any.
2445 ;; FRAME-PARAMS :: List of the form (FRAME-STYLE-NAME
2446 ;; FRAME-ATTRIBUTES FRAME-ANCHOR). Note
2447 ;; that these are the last three arguments
2448 ;; to `org-e-odt--frame'.
2450 ;; Note that an un-captioned image/formula requires just an
2451 ;; INNER-FRAME, while a captioned image/formula requires
2452 ;; both an INNER and an OUTER-FRAME.
2453 '(("As-CharImage" ("OrgInlineImage" nil
"as-char"))
2454 ("ParagraphImage" ("OrgDisplayImage" nil
"paragraph"))
2455 ("PageImage" ("OrgPageImage" nil
"page"))
2456 ("CaptionedAs-CharImage"
2457 ("OrgCaptionedImage"
2458 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
2459 ("OrgInlineImage" nil
"as-char"))
2460 ("CaptionedParagraphImage"
2461 ("OrgCaptionedImage"
2462 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
2463 ("OrgImageCaptionFrame" nil
"paragraph"))
2464 ("CaptionedPageImage"
2465 ("OrgCaptionedImage"
2466 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
2467 ("OrgPageImageCaptionFrame" nil
"page"))
2468 ("InlineFormula" ("OrgInlineFormula" nil
"as-char"))
2469 ("DisplayFormula" ("OrgDisplayFormula" nil
"as-char"))
2470 ("CaptionedDisplayFormula"
2471 ("OrgCaptionedFormula" nil
"paragraph")
2472 ("OrgFormulaCaptionFrame" nil
"paragraph"))))
2473 (caption (car captions
)) (short-caption (cdr captions
))
2474 ;; Retrieve inner and outer frame params, from configuration.
2475 (frame-cfg (assoc-string cfg-key frame-cfg-alist t
))
2476 (inner (nth 1 frame-cfg
))
2477 (outer (nth 2 frame-cfg
))
2478 ;; User-specified frame params (from #+ATTR_ODT spec)
2479 (user user-frame-params
)
2480 (--merge-frame-params (function
2481 (lambda (default user
)
2482 "Merge default and user frame params."
2483 (if (not user
) default
2484 (assert (= (length default
) 3))
2485 (assert (= (length user
) 3))
2488 collect
(or u d
)))))))
2490 ;; Case 1: Image/Formula has no caption.
2491 ;; There is only one frame, one that surrounds the image
2494 ;; Merge user frame params with that from configuration.
2495 (setq inner
(funcall --merge-frame-params inner user
))
2496 (apply 'org-e-odt--frame href width height
2497 (append inner title-and-desc
)))
2498 ;; Case 2: Image/Formula is captioned or labeled.
2499 ;; There are two frames: The inner one surrounds the
2500 ;; image or formula. The outer one contains the
2501 ;; caption/sequence number.
2503 ;; Merge user frame params with outer frame params.
2504 (setq outer
(funcall --merge-frame-params outer user
))
2505 ;; Short caption, if specified, goes as part of inner frame.
2506 (setq inner
(let ((frame-params (copy-sequence inner
)))
2507 (setcar (cdr frame-params
)
2511 (format " draw:name=\"%s\" " short-caption
))))
2513 (apply 'org-e-odt--textbox
2514 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2517 (apply 'org-e-odt--frame href width height
2518 (append inner title-and-desc
))
2520 width height outer
)))))
2522 (defun org-e-odt--enumerable-p (element info
)
2523 ;; Element should have a caption or label.
2524 (or (org-element-property :caption element
)
2525 (org-element-property :name element
)))
2527 (defun org-e-odt--enumerable-image-p (element info
)
2528 (org-e-odt--standalone-link-p
2530 ;; Paragraph should have a caption or label. It SHOULD NOT be a
2531 ;; replacement element. (i.e., It SHOULD NOT be a result of LaTeX
2534 (and (not (org-element-property :replaces p
))
2535 (or (org-element-property :caption p
)
2536 (org-element-property :name p
))))
2537 ;; Link should point to an image file.
2539 (assert (eq (org-element-type l
) 'link
))
2540 (org-export-inline-image-p l org-e-odt-inline-image-rules
))))
2542 (defun org-e-odt--enumerable-latex-image-p (element info
)
2543 (org-e-odt--standalone-link-p
2545 ;; Paragraph should have a caption or label. It SHOULD also be a
2546 ;; replacement element. (i.e., It SHOULD be a result of LaTeX
2549 (and (org-element-property :replaces p
)
2550 (or (org-element-property :caption p
)
2551 (org-element-property :name p
))))
2552 ;; Link should point to an image file.
2554 (assert (eq (org-element-type l
) 'link
))
2555 (org-export-inline-image-p l org-e-odt-inline-image-rules
))))
2557 (defun org-e-odt--enumerable-formula-p (element info
)
2558 (org-e-odt--standalone-link-p
2560 ;; Paragraph should have a caption or label.
2562 (or (org-element-property :caption p
)
2563 (org-element-property :name p
)))
2564 ;; Link should point to a MathML or ODF file.
2566 (assert (eq (org-element-type l
) 'link
))
2567 (org-export-inline-image-p l org-e-odt-inline-formula-rules
))))
2569 (defun org-e-odt--standalone-link-p (element info
&optional
2572 "Test if ELEMENT is a standalone link for the purpose ODT export.
2573 INFO is a plist holding contextual information.
2575 Return non-nil, if ELEMENT is of type paragraph satisfying
2576 PARAGRAPH-PREDICATE and it's sole content, save for whitespaces,
2577 is a link that satisfies LINK-PREDICATE.
2579 Return non-nil, if ELEMENT is of type link satisfying
2580 LINK-PREDICATE and it's containing paragraph satisfies
2581 PARAGRAPH-PREDICATE inaddtion to having no other content save for
2582 leading and trailing whitespaces.
2584 Return nil, otherwise."
2585 (let ((p (case (org-element-type element
)
2587 (link (and (or (not link-predicate
)
2588 (funcall link-predicate element
))
2589 (org-export-get-parent element
)))
2591 (when (and p
(eq (org-element-type p
) 'paragraph
))
2592 (when (or (not paragraph-predicate
)
2593 (funcall paragraph-predicate p
))
2594 (let ((contents (org-element-contents p
)))
2595 (loop for x in contents
2596 with inline-image-count
= 0
2597 always
(case (org-element-type x
)
2599 (not (org-string-nw-p x
)))
2601 (and (or (not link-predicate
)
2602 (funcall link-predicate x
))
2603 (= (incf inline-image-count
) 1)))
2606 (defun org-e-odt-link--infer-description (destination info
)
2607 ;; DESTINATION is a HEADLINE, a "<<target>>" or an element (like
2608 ;; paragraph, verse-block etc) to which a "#+NAME: label" can be
2609 ;; attached. Note that labels that are attached to captioned
2610 ;; entities - inline images, math formulae and tables - get resolved
2611 ;; as part of `org-e-odt-format-label' and `org-e-odt--enumerate'.
2613 ;; Create a cross-reference to DESTINATION but make best-efforts to
2614 ;; create a *meaningful* description. Check item numbers, section
2615 ;; number and section title in that order.
2617 ;; NOTE: Counterpart of `org-export-get-ordinal'.
2618 ;; FIXME: Handle footnote-definition footnote-reference?
2619 (let* ((genealogy (org-export-get-genealogy destination
))
2620 (data (reverse genealogy
))
2621 (label (case (org-element-type destination
)
2623 (format "sec-%s" (mapconcat 'number-to-string
2624 (org-export-get-headline-number
2625 destination info
) "-")))
2627 (org-element-property :value destination
))
2628 (t (error "FIXME: Resolve %S" destination
)))))
2630 (let* ( ;; Locate top-level list.
2633 when
(eq (org-element-type (car x
)) 'plain-list
)
2635 ;; Get list item nos.
2637 (loop for
(plain-list item . rest
) on top-level-list by
#'cddr
2638 until
(not (eq (org-element-type plain-list
) 'plain-list
))
2639 collect
(when (eq (org-element-property :type
2642 (1+ (length (org-export-get-previous-element
2644 ;; Locate top-most listified headline.
2645 (listified-headlines
2647 when
(and (eq (org-element-type (car x
)) 'headline
)
2648 (org-export-low-level-p (car x
) info
))
2650 ;; Get listified headline numbers.
2651 (listified-headline-nos
2652 (loop for el in listified-headlines
2653 when
(eq (org-element-type el
) 'headline
)
2654 collect
(when (org-export-numbered-headline-p el info
)
2655 (1+ (length (org-export-get-previous-element
2657 ;; Combine item numbers from both the listified headlines and
2658 ;; regular list items.
2660 ;; Case 1: Check if all the parents of list item are numbered.
2661 ;; If yes, link to the item proper.
2662 (let ((item-numbers (append listified-headline-nos item-numbers
)))
2663 (when (and item-numbers
(not (memq nil item-numbers
)))
2664 (format "<text:bookmark-ref text:reference-format=\"number-all-superior\" text:ref-name=\"%s\">%s</text:bookmark-ref>"
2665 (org-export-solidify-link-text label
)
2666 (mapconcat (lambda (n) (if (not n
) " "
2667 (concat (number-to-string n
) ".")))
2668 item-numbers
"")))))
2669 ;; Case 2: Locate a regular and numbered headline in the
2670 ;; hierarchy. Display it's section number.
2671 (let ((headline (loop for el in
(cons destination genealogy
)
2672 when
(and (eq (org-element-type el
) 'headline
)
2673 (not (org-export-low-level-p el info
))
2674 (org-export-numbered-headline-p el info
))
2678 (format "<text:bookmark-ref text:reference-format=\"chapter\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
2679 (org-export-solidify-link-text label
)
2680 (mapconcat 'number-to-string
(org-export-get-headline-number
2681 headline info
) "."))))
2682 ;; Case 4: Locate a regular headline in the hierarchy. Display
2684 (let ((headline (loop for el in
(cons destination genealogy
)
2685 when
(and (eq (org-element-type el
) 'headline
)
2686 (not (org-export-low-level-p el info
)))
2690 (format "<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
2691 (org-export-solidify-link-text label
)
2692 (let ((title (org-element-property :title headline
)))
2693 (org-export-data title info
)))))
2696 (defun org-e-odt-link (link desc info
)
2697 "Transcode a LINK object from Org to ODT.
2699 DESC is the description part of the link, or the empty string.
2700 INFO is a plist holding contextual information. See
2702 (let* ((type (org-element-property :type link
))
2703 (raw-path (org-element-property :path link
))
2704 ;; Ensure DESC really exists, or set it to nil.
2705 (desc (and (not (string= desc
"")) desc
))
2706 (imagep (org-export-inline-image-p
2707 link org-e-odt-inline-image-rules
))
2709 ((member type
'("http" "https" "ftp" "mailto"))
2710 (concat type
":" raw-path
))
2711 ((string= type
"file")
2712 (if (file-name-absolute-p raw-path
)
2713 (concat "file://" (expand-file-name raw-path
))
2714 (concat "file://" raw-path
)))
2719 ((and (not desc
) (org-export-inline-image-p
2720 link org-e-odt-inline-image-rules
))
2721 (org-e-odt-link--inline-image link info
))
2723 ((and (not desc
) (org-export-inline-image-p
2724 link org-e-odt-inline-formula-rules
))
2725 (org-e-odt-link--inline-formula link info
))
2726 ;; Radio target: Transcode target's contents and use them as
2727 ;; link's description.
2728 ((string= type
"radio")
2729 (let ((destination (org-export-resolve-radio-link link info
)))
2731 (let ((desc (org-export-data (org-element-contents destination
) info
))
2732 (href (org-export-solidify-link-text path
)))
2734 "<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
2736 ;; Links pointing to an headline: Find destination and build
2737 ;; appropriate referencing command.
2738 ((member type
'("custom-id" "fuzzy" "id"))
2739 (let* ((destination (if (string= type
"fuzzy")
2740 (org-export-resolve-fuzzy-link link info
)
2741 (org-export-resolve-id-link link info
))))
2743 ;; Case 1: Fuzzy link points nowhere.
2744 (when (null (org-element-type destination
))
2745 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2746 "Emphasis" (or desc
(org-export-data
2747 (org-element-property
2748 :raw-link link
) info
))))
2749 ;; Case 2: Fuzzy link points to an invisible target. Strip it.
2750 (when (eq (org-element-type destination
) 'keyword
) "")
2751 ;; Case 3: LINK points to an headline.
2752 (when (eq (org-element-type destination
) 'headline
)
2753 ;; Case 3.1: LINK has a custom description that is
2754 ;; different from headline's title. Create a hyperlink.
2756 (let ((link-desc (org-element-contents link
)))
2757 (not (string= (org-element-interpret-data link-desc
)
2758 (org-element-property :raw-value
2760 (let* ((headline-no (org-export-get-headline-number
2762 (label (format "sec-%s" (mapconcat 'number-to-string
2764 (format "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
2766 ;; Case 4: LINK points to an Inline image, Math formula or a Table.
2767 (let ((label-reference (ignore-errors (org-e-odt-format-label
2768 destination info
'reference
))))
2769 (when label-reference
2771 ;; Case 4.1: LINK has no description. Create a
2772 ;; cross-reference showing entity's sequence number.
2773 ((not desc
) label-reference
)
2774 ;; Case 4.2: LINK has description. Insert a hyperlink
2775 ;; with user-provided description.
2776 (t (let* ((caption-from (case (org-element-type destination
)
2777 (link (org-export-get-parent-element
2780 ;; Get label and caption.
2781 (label (org-element-property :name caption-from
)))
2782 (format "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
2783 (org-export-solidify-link-text label
) desc
))))))
2784 ;; Case 5: Fuzzy link points to a TARGET.
2785 (when (eq (org-element-type destination
) 'target
)
2786 ;; Case 5.1: LINK has description. Create a hyperlink.
2788 (let ((label (org-element-property :value destination
)))
2789 (format "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
2790 (org-export-solidify-link-text label
) desc
))))
2791 ;; LINK has no description. It points to either a HEADLINE, a
2792 ;; TARGET or an ELEMENT with a #+NAME: LABEL attached to it.
2793 ;; LINK to DESTINATION, but make a best effort to provide a
2794 ;; *meaningful* description.
2795 (org-e-odt-link--infer-description destination info
))))
2796 ;; Coderef: replace link with the reference name or the
2797 ;; equivalent line number.
2798 ((string= type
"coderef")
2799 (let* ((line-no (format "%d" (org-export-resolve-coderef path info
)))
2800 (href (concat "coderef-" path
)))
2802 (org-export-get-coderef-format path desc
)
2804 "<text:bookmark-ref text:reference-format=\"number\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
2806 ;; Link type is handled by a special function.
2807 ((functionp (setq protocol
(nth 2 (assoc type org-link-protocols
))))
2808 (funcall protocol
(org-link-unescape path
) desc
'odt
))
2809 ;; External link with a description part.
2811 (let ((link-contents (org-element-contents link
)))
2812 ;; Check if description is a link to an inline image.
2813 (if (and (not (cdr link-contents
))
2814 (let ((desc-element (car link-contents
)))
2815 (and (eq (org-element-type desc-element
) 'link
)
2816 (org-export-inline-image-p
2817 desc-element org-e-odt-inline-image-rules
))))
2818 ;; Format link as a clickable image.
2819 (format "\n<draw:a xlink:type=\"simple\" xlink:href=\"%s\">\n%s\n</draw:a>"
2821 ;; Otherwise, format it as a regular link.
2822 (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
2824 ;; External link without a description part.
2826 (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
2828 ;; No path, only description. Try to do something useful.
2829 (t (format "<text:span text:style-name=\"%s\">%s</text:span>"
2830 "Emphasis" desc
)))))
2835 (defun org-e-odt-paragraph (paragraph contents info
)
2836 "Transcode a PARAGRAPH element from Org to ODT.
2837 CONTENTS is the contents of the paragraph, as a string. INFO is
2838 the plist used as a communication channel."
2839 (let* ((parent (org-export-get-parent paragraph
))
2840 (parent-type (org-element-type parent
))
2841 (style (case parent-type
2842 (quote-block "Quotations")
2843 (center-block "OrgCenter")
2844 (footnote-definition "Footnote")
2845 (t (or (org-element-property :style paragraph
)
2847 ;; If this paragraph is a leading paragraph in an item and the
2848 ;; item has a checkbox, splice the checkbox and paragraph contents
2850 (when (and (eq (org-element-type parent
) 'item
)
2851 (eq paragraph
(car (org-element-contents parent
))))
2852 (setq contents
(concat (org-e-odt--checkbox parent
) contents
)))
2854 (format "\n<text:p text:style-name=\"%s\">%s</text:p>" style contents
)))
2859 (defun org-e-odt-plain-list (plain-list contents info
)
2860 "Transcode a PLAIN-LIST element from Org to ODT.
2861 CONTENTS is the contents of the list. INFO is a plist holding
2862 contextual information."
2863 (format "\n<text:list text:style-name=\"%s\" %s>\n%s</text:list>"
2864 ;; Choose style based on list type.
2865 (case (org-element-property :type plain-list
)
2866 (ordered "OrgNumberedList")
2867 (unordered "OrgBulletedList")
2868 (descriptive-1 "OrgDescriptionList")
2869 (descriptive-2 "OrgDescriptionList"))
2870 ;; If top-level list, re-start numbering. Otherwise,
2871 ;; continue numbering.
2872 (format "text:continue-numbering=\"%s\""
2873 (let* ((parent (org-export-get-parent plain-list
)))
2874 (if (and parent
(eq (org-element-type parent
) 'item
))
2880 (defun org-e-odt--encode-tabs-and-spaces (line)
2881 (replace-regexp-in-string
2882 "\\([\t]\\|\\([ ]+\\)\\)"
2885 ((string= s
"\t") "<text:tab/>")
2886 (t (let ((n (length s
)))
2889 ((> n
1) (concat " " (format "<text:s text:c=\"%d\"/>" (1- n
))))
2893 (defun org-e-odt--encode-plain-text (text &optional no-whitespace-filling
)
2896 (setq text
(replace-regexp-in-string (car pair
) (cdr pair
) text t t
)))
2897 '(("&" .
"&") ("<" .
"<") (">" .
">")))
2898 (if no-whitespace-filling text
2899 (org-e-odt--encode-tabs-and-spaces text
)))
2901 (defun org-e-odt-plain-text (text info
)
2902 "Transcode a TEXT string from Org to ODT.
2903 TEXT is the string to transcode. INFO is a plist holding
2904 contextual information."
2905 (let ((output text
))
2906 ;; Protect &, < and >.
2907 (setq output
(org-e-odt--encode-plain-text output t
))
2908 ;; Handle smart quotes. Be sure to provide original string since
2909 ;; OUTPUT may have been modified.
2910 (setq output
(org-export-activate-smart-quotes output
:utf-8 info text
))
2911 ;; Convert special strings.
2912 (when (plist-get info
:with-special-strings
)
2916 (replace-regexp-in-string (car pair
) (cdr pair
) output t nil
)))
2917 org-e-odt-special-string-regexps
))
2918 ;; Handle break preservation if required.
2919 (when (plist-get info
:preserve-breaks
)
2920 (setq output
(replace-regexp-in-string
2921 "\\(\\\\\\\\\\)?[ \t]*\n" "<text:line-break/>\n" output t
)))
2928 (defun org-e-odt-planning (planning contents info
)
2929 "Transcode a PLANNING element from Org to ODT.
2930 CONTENTS is nil. INFO is a plist used as a communication
2932 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2935 (let ((closed (org-element-property :closed planning
)))
2938 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2939 "OrgClosedKeyword" org-closed-string
)
2940 (org-e-odt-timestamp closed contents info
))))
2941 (let ((deadline (org-element-property :deadline planning
)))
2944 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2945 "OrgDeadlineKeyword" org-deadline-string
)
2946 (org-e-odt-timestamp deadline contents info
))))
2947 (let ((scheduled (org-element-property :scheduled planning
)))
2950 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2951 "OrgScheduledKeyword" org-deadline-string
)
2952 (org-e-odt-timestamp scheduled contents info
)))))))
2955 ;;;; Property Drawer
2957 (defun org-e-odt-property-drawer (property-drawer contents info
)
2958 "Transcode a PROPERTY-DRAWER element from Org to ODT.
2959 CONTENTS is nil. INFO is a plist holding contextual
2961 ;; The property drawer isn't exported but we want separating blank
2962 ;; lines nonetheless.
2968 (defun org-e-odt-quote-block (quote-block contents info
)
2969 "Transcode a QUOTE-BLOCK element from Org to ODT.
2970 CONTENTS holds the contents of the block. INFO is a plist
2971 holding contextual information."
2977 (defun org-e-odt-quote-section (quote-section contents info
)
2978 "Transcode a QUOTE-SECTION element from Org to ODT.
2979 CONTENTS is nil. INFO is a plist holding contextual information."
2980 (let ((value (org-remove-indentation
2981 (org-element-property :value quote-section
))))
2982 (when value
(org-e-odt-do-format-code value
))))
2987 (defun org-e-odt-format-section (text style
&optional name
)
2988 (let ((default-name (car (org-e-odt-add-automatic-style "Section"))))
2989 (format "\n<text:section text:style-name=\"%s\" %s>\n%s\n</text:section>"
2991 (format "text:name=\"%s\"" (or name default-name
))
2995 (defun org-e-odt-section (section contents info
) ; FIXME
2996 "Transcode a SECTION element from Org to ODT.
2997 CONTENTS holds the contents of the section. INFO is a plist
2998 holding contextual information."
3003 (defun org-e-odt-radio-target (radio-target text info
)
3004 "Transcode a RADIO-TARGET object from Org to ODT.
3005 TEXT is the text of the target. INFO is a plist holding
3006 contextual information."
3008 text
(org-export-solidify-link-text
3009 (org-element-property :value radio-target
))))
3014 (defun org-e-odt-special-block (special-block contents info
)
3015 "Transcode a SPECIAL-BLOCK element from Org to ODT.
3016 CONTENTS holds the contents of the block. INFO is a plist
3017 holding contextual information."
3018 (let ((type (downcase (org-element-property :type special-block
)))
3019 (attributes (org-export-read-attribute :attr_odt special-block
)))
3022 ((string= type
"annotation")
3023 (let* ((author (or (plist-get attributes
:author
)
3024 (let ((author (plist-get info
:author
)))
3025 (and author
(org-export-data author info
)))))
3026 (date (or (plist-get attributes
:date
)
3027 ;; FIXME: Is `car' right thing to do below?
3028 (car (plist-get info
:date
)))))
3030 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3032 (format "<office:annotation>\n%s\n</office:annotation>"
3035 (format "<dc:creator>%s</dc:creator>" author
))
3037 (format "<dc:date>%s</dc:date>"
3038 (org-e-odt--format-timestamp date nil
'iso-date
)))
3041 ((string= type
"textbox")
3042 (let ((width (plist-get attributes
:width
))
3043 (height (plist-get attributes
:height
))
3044 (style (plist-get attributes
:style
))
3045 (extra (plist-get attributes
:extra
))
3046 (anchor (plist-get attributes
:anchor
)))
3047 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3048 "Text_20_body" (org-e-odt--textbox contents width height
3049 style extra anchor
))))
3055 (defun org-e-odt-hfy-face-to-css (fn)
3056 "Create custom style for face FN.
3057 When FN is the default face, use it's foreground and background
3058 properties to create \"OrgSrcBlock\" paragraph style. Otherwise
3059 use it's color attribute to create a character style whose name
3060 is obtained from FN. Currently all attributes of FN other than
3063 The style name for a face FN is derived using the following
3064 operations on the face name in that order - de-dash, CamelCase
3065 and prefix with \"OrgSrc\". For example,
3066 `font-lock-function-name-face' is associated with
3067 \"OrgSrcFontLockFunctionNameFace\"."
3068 (let* ((css-list (hfy-face-to-style fn
))
3069 (style-name ((lambda (fn)
3072 'capitalize
(split-string
3073 (hfy-face-or-def-to-name fn
) "-")
3075 (color-val (cdr (assoc "color" css-list
)))
3076 (background-color-val (cdr (assoc "background" css-list
)))
3077 (style (and org-e-odt-create-custom-styles-for-srcblocks
3080 (format org-e-odt-src-block-paragraph-format
3081 background-color-val color-val
))
3085 <style:style style:name=\"%s\" style:family=\"text\">
3086 <style:text-properties fo:color=\"%s\"/>
3087 </style:style>" style-name color-val
))))))
3088 (cons style-name style
)))
3090 (defun org-e-odt-htmlfontify-string (line)
3091 (let* ((hfy-html-quote-regex "\\([<\"&> ]\\)")
3092 (hfy-html-quote-map '(("\"" """)
3097 (" " "<text:tab/>")))
3098 (hfy-face-to-css 'org-e-odt-hfy-face-to-css
)
3099 (hfy-optimisations-1 (copy-sequence hfy-optimisations
))
3100 (hfy-optimisations (add-to-list 'hfy-optimisations-1
3102 (hfy-begin-span-handler
3103 (lambda (style text-block text-id text-begins-block-p
)
3104 (insert (format "<text:span text:style-name=\"%s\">" style
))))
3105 (hfy-end-span-handler (lambda nil
(insert "</text:span>"))))
3106 (with-no-warnings (htmlfontify-string line
))))
3108 (defun org-e-odt-do-format-code
3109 (code &optional lang refs retain-labels num-start
)
3110 (let* ((lang (or (assoc-default lang org-src-lang-modes
) lang
))
3111 (lang-mode (and lang
(intern (format "%s-mode" lang
))))
3112 (code-lines (org-split-string code
"\n"))
3113 (code-length (length code-lines
))
3114 (use-htmlfontify-p (and (functionp lang-mode
)
3115 org-e-odt-fontify-srcblocks
3116 (require 'htmlfontify nil t
)
3117 (fboundp 'htmlfontify-string
)))
3118 (code (if (not use-htmlfontify-p
) code
3122 (font-lock-fontify-buffer)
3124 (fontifier (if use-htmlfontify-p
'org-e-odt-htmlfontify-string
3125 'org-e-odt--encode-plain-text
))
3126 (par-style (if use-htmlfontify-p
"OrgSrcBlock"
3127 "OrgFixedWidthBlock"))
3129 (assert (= code-length
(length (org-split-string code
"\n"))))
3131 (org-export-format-code
3133 (lambda (loc line-num ref
)
3135 (concat par-style
(and (= (incf i
) code-length
) "LastLine")))
3137 (setq loc
(concat loc
(and ref retain-labels
(format " (%s)" ref
))))
3138 (setq loc
(funcall fontifier loc
))
3140 (setq loc
(org-e-odt--target loc
(concat "coderef-" ref
))))
3142 (setq loc
(format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3144 (if (not line-num
) loc
3145 (format "\n<text:list-item>%s\n</text:list-item>" loc
)))
3148 ((not num-start
) code
)
3151 "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
3152 " text:continue-numbering=\"false\"" code
))
3155 "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
3156 " text:continue-numbering=\"true\"" code
)))))
3158 (defun org-e-odt-format-code (element info
)
3159 (let* ((lang (org-element-property :language element
))
3160 ;; Extract code and references.
3161 (code-info (org-export-unravel-code element
))
3162 (code (car code-info
))
3163 (refs (cdr code-info
))
3164 ;; Does the src block contain labels?
3165 (retain-labels (org-element-property :retain-labels element
))
3166 ;; Does it have line numbers?
3167 (num-start (case (org-element-property :number-lines element
)
3168 (continued (org-export-get-loc element info
))
3170 (org-e-odt-do-format-code code lang refs retain-labels num-start
)))
3172 (defun org-e-odt-src-block (src-block contents info
)
3173 "Transcode a SRC-BLOCK element from Org to ODT.
3174 CONTENTS holds the contents of the item. INFO is a plist holding
3175 contextual information."
3176 (let* ((lang (org-element-property :language src-block
))
3177 (attributes (org-export-read-attribute :attr_odt src-block
))
3178 (captions (org-e-odt-format-label src-block info
'definition
))
3179 (caption (car captions
)) (short-caption (cdr captions
)))
3182 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3184 (let ((--src-block (org-e-odt-format-code src-block info
)))
3185 (if (not (plist-get attributes
:textbox
)) --src-block
3186 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3188 (org-e-odt--textbox --src-block nil nil nil
)))))))
3191 ;;;; Statistics Cookie
3193 (defun org-e-odt-statistics-cookie (statistics-cookie contents info
)
3194 "Transcode a STATISTICS-COOKIE object from Org to ODT.
3195 CONTENTS is nil. INFO is a plist holding contextual information."
3196 (let ((cookie-value (org-element-property :value statistics-cookie
)))
3197 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3198 "OrgCode" cookie-value
)))
3203 (defun org-e-odt-strike-through (strike-through contents info
)
3204 "Transcode STRIKE-THROUGH from Org to ODT.
3205 CONTENTS is the text with strike-through markup. INFO is a plist
3206 holding contextual information."
3207 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3208 "Strikethrough" contents
))
3213 (defun org-e-odt-subscript (subscript contents info
)
3214 "Transcode a SUBSCRIPT object from Org to ODT.
3215 CONTENTS is the contents of the object. INFO is a plist holding
3216 contextual information."
3217 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3218 "OrgSubscript" contents
))
3223 (defun org-e-odt-superscript (superscript contents info
)
3224 "Transcode a SUPERSCRIPT object from Org to ODT.
3225 CONTENTS is the contents of the object. INFO is a plist holding
3226 contextual information."
3227 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3228 "OrgSuperscript" contents
))
3233 (defun org-e-odt-table-style-spec (element info
)
3234 (let* ((table (org-export-get-parent-table element
))
3235 (table-attributes (org-export-read-attribute :attr_odt table
))
3236 (table-style (plist-get table-attributes
:style
)))
3237 (assoc table-style org-e-odt-table-styles
)))
3239 (defun org-e-odt-get-table-cell-styles (table-cell info
)
3240 "Retrieve styles applicable to a table cell.
3241 R and C are (zero-based) row and column numbers of the table
3242 cell. STYLE-SPEC is an entry in `org-e-odt-table-styles'
3243 applicable to the current table. It is `nil' if the table is not
3244 associated with any style attributes.
3246 Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
3248 When STYLE-SPEC is nil, style the table cell the conventional way
3249 - choose cell borders based on row and column groupings and
3250 choose paragraph alignment based on `org-col-cookies' text
3252 `org-e-odt-get-paragraph-style-cookie-for-table-cell'.
3254 When STYLE-SPEC is non-nil, ignore the above cookie and return
3255 styles congruent with the ODF-1.2 specification."
3256 (let* ((table-cell-address (org-export-table-cell-address table-cell info
))
3257 (r (car table-cell-address
)) (c (cdr table-cell-address
))
3258 (style-spec (org-e-odt-table-style-spec table-cell info
))
3259 (table-dimensions (org-export-table-dimensions
3260 (org-export-get-parent-table table-cell
)
3263 ;; LibreOffice - particularly the Writer - honors neither table
3264 ;; templates nor custom table-cell styles. Inorder to retain
3265 ;; inter-operability with LibreOffice, only automatic styles are
3266 ;; used for styling of table-cells. The current implementation is
3267 ;; congruent with ODF-1.2 specification and hence is
3268 ;; future-compatible.
3270 ;; Additional Note: LibreOffice's AutoFormat facility for tables -
3271 ;; which recognizes as many as 16 different cell types - is much
3272 ;; richer. Unfortunately it is NOT amenable to easy configuration
3274 (let* ((template-name (nth 1 style-spec
))
3275 (cell-style-selectors (nth 2 style-spec
))
3278 ((and (cdr (assoc 'use-first-column-styles cell-style-selectors
))
3279 (= c
0)) "FirstColumn")
3280 ((and (cdr (assoc 'use-last-column-styles cell-style-selectors
))
3281 (= (1+ c
) (cdr table-dimensions
)))
3283 ((and (cdr (assoc 'use-first-row-styles cell-style-selectors
))
3284 (= r
0)) "FirstRow")
3285 ((and (cdr (assoc 'use-last-row-styles cell-style-selectors
))
3286 (= (1+ r
) (car table-dimensions
)))
3288 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors
))
3289 (= (% r
2) 1)) "EvenRow")
3290 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors
))
3291 (= (% r
2) 0)) "OddRow")
3292 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors
))
3293 (= (% c
2) 1)) "EvenColumn")
3294 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors
))
3295 (= (% c
2) 0)) "OddColumn")
3297 (concat template-name cell-type
)))))
3299 (defun org-e-odt-table-cell (table-cell contents info
)
3300 "Transcode a TABLE-CELL element from Org to ODT.
3301 CONTENTS is nil. INFO is a plist used as a communication
3303 (let* ((table-cell-address (org-export-table-cell-address table-cell info
))
3304 (r (car table-cell-address
))
3305 (c (cdr table-cell-address
))
3306 (horiz-span (or (org-export-table-cell-width table-cell info
) 0))
3307 (table-row (org-export-get-parent table-cell
))
3308 (custom-style-prefix (org-e-odt-get-table-cell-styles
3312 (and custom-style-prefix
3313 (format "%sTableParagraph" custom-style-prefix
))
3316 ((and (= 1 (org-export-table-row-group table-row info
))
3317 (org-export-table-has-header-p
3318 (org-export-get-parent-table table-row
) info
))
3320 ((let* ((table (org-export-get-parent-table table-cell
))
3321 (table-attrs (org-export-read-attribute :attr_odt table
))
3322 (table-header-columns (plist-get table-attrs
3324 (<= c
(cond ((wholenump table-header-columns
)
3325 (- table-header-columns
1))
3326 (table-header-columns 0)
3329 (t "OrgTableContents"))
3330 (capitalize (symbol-name (org-export-table-cell-alignment
3331 table-cell info
))))))
3334 (and custom-style-prefix
(format "%sTableCell"
3335 custom-style-prefix
))
3338 (when (or (org-export-table-row-starts-rowgroup-p table-row info
)
3340 (when (org-export-table-row-ends-rowgroup-p table-row info
) "B")
3341 (when (and (org-export-table-cell-starts-colgroup-p table-cell info
)
3342 (not (zerop c
)) ) "L"))))
3345 (format " table:style-name=\"%s\"" cell-style-name
)
3346 (and (> horiz-span
0)
3347 (format " table:number-columns-spanned=\"%d\""
3348 (1+ horiz-span
))))))
3349 (unless contents
(setq contents
""))
3351 (assert paragraph-style
)
3352 (format "\n<table:table-cell%s>\n%s\n</table:table-cell>"
3354 (let ((table-cell-contents (org-element-contents table-cell
)))
3355 (if (memq (org-element-type (car table-cell-contents
))
3356 org-element-all-elements
)
3358 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3359 paragraph-style contents
))))
3361 (dotimes (i horiz-span s
)
3362 (setq s
(concat s
"\n<table:covered-table-cell/>"))))
3368 (defun org-e-odt-table-row (table-row contents info
)
3369 "Transcode a TABLE-ROW element from Org to ODT.
3370 CONTENTS is the contents of the row. INFO is a plist used as a
3371 communication channel."
3372 ;; Rules are ignored since table separators are deduced from
3373 ;; borders of the current row.
3374 (when (eq (org-element-property :type table-row
) 'standard
)
3375 (let* ((rowgroup-tags
3376 (if (and (= 1 (org-export-table-row-group table-row info
))
3377 (org-export-table-has-header-p
3378 (org-export-get-parent-table table-row
) info
))
3379 ;; If the row belongs to the first rowgroup and the
3380 ;; table has more than one row groups, then this row
3381 ;; belongs to the header row group.
3382 '("\n<table:table-header-rows>" .
"\n</table:table-header-rows>")
3383 ;; Otherwise, it belongs to non-header row group.
3384 '("\n<table:table-rows>" .
"\n</table:table-rows>"))))
3386 ;; Does this row begin a rowgroup?
3387 (when (org-export-table-row-starts-rowgroup-p table-row info
)
3388 (car rowgroup-tags
))
3390 (format "\n<table:table-row>\n%s\n</table:table-row>" contents
)
3391 ;; Does this row end a rowgroup?
3392 (when (org-export-table-row-ends-rowgroup-p table-row info
)
3393 (cdr rowgroup-tags
))))))
3398 (defun org-e-odt-table-first-row-data-cells (table info
)
3403 (unless (eq (org-element-property :type row
) 'rule
) row
))
3405 (special-column-p (org-export-table-has-special-column-p table
)))
3406 (if (not special-column-p
) (org-element-contents table-row
)
3407 (cdr (org-element-contents table-row
)))))
3409 (defun org-e-odt--table (table contents info
)
3410 "Transcode a TABLE element from Org to ODT.
3411 CONTENTS is the contents of the table. INFO is a plist holding
3412 contextual information."
3413 (case (org-element-property :type table
)
3414 ;; Case 1: table.el doesn't support export to OD format. Strip
3415 ;; such tables from export.
3420 "(org-e-odt): Found table.el-type table in the source Org file."
3421 " table.el doesn't support export to ODT format."
3422 " Stripping the table from export."))))
3423 ;; Case 2: Native Org tables.
3425 (let* ((captions (org-e-odt-format-label table info
'definition
))
3426 (caption (car captions
)) (short-caption (cdr captions
))
3427 (attributes (org-export-read-attribute :attr_odt table
))
3428 (custom-table-style (nth 1 (org-e-odt-table-style-spec table info
)))
3431 (lambda (table info
)
3432 (let* ((table-style (or custom-table-style
"OrgTable"))
3433 (column-style (format "%sColumn" table-style
)))
3435 (lambda (table-cell)
3436 (let ((width (1+ (or (org-export-table-cell-width
3437 table-cell info
) 0)))
3439 "\n<table:table-column table:style-name=\"%s\"/>"
3442 (dotimes (i width out
) (setq out
(concat s out
)))))
3443 (org-e-odt-table-first-row-data-cells table info
) "\n"))))))
3447 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3450 (let* ((automatic-name
3451 (org-e-odt-add-automatic-style "Table" attributes
)))
3453 "\n<table:table table:style-name=\"%s\"%s>"
3454 (or custom-table-style
(cdr automatic-name
) "OrgTable")
3455 (concat (when short-caption
3456 (format " table:name=\"%s\"" short-caption
)))))
3457 ;; column specification.
3458 (funcall table-column-specs table info
)
3462 "</table:table>")))))
3464 (defun org-e-odt-table (table contents info
)
3465 "Transcode a TABLE element from Org to ODT.
3466 CONTENTS is the contents of the table. INFO is a plist holding
3467 contextual information.
3469 Use `org-e-odt--table' to typeset the table. Handle details
3470 pertaining to indentation here."
3471 (let* ((--element-preceded-by-table-p
3473 (lambda (element info
)
3474 (loop for el in
(org-export-get-previous-element element info t
)
3475 thereis
(eq (org-element-type el
) 'table
)))))
3476 (--walk-list-genealogy-and-collect-tags
3478 (lambda (table info
)
3479 (let* ((genealogy (org-export-get-genealogy table
))
3481 (when (eq (org-element-type (car genealogy
)) 'item
)
3482 (loop for el in genealogy
3483 when
(memq (org-element-type el
)
3488 (loop for el in genealogy
3489 when
(and (eq (org-element-type el
) 'headline
)
3490 (org-export-low-level-p el info
))
3494 (org-element-contents
3495 (org-export-get-parent el
)))))))
3498 ;; Handle list genealogy.
3499 (loop for el in list-genealogy collect
3500 (case (org-element-type el
)
3502 (setq parent-list el
)
3503 (cons "</text:list>"
3504 (format "\n<text:list text:style-name=\"%s\" %s>"
3505 (case (org-element-property :type el
)
3506 (ordered "OrgNumberedList")
3507 (unordered "OrgBulletedList")
3508 (descriptive-1 "OrgDescriptionList")
3509 (descriptive-2 "OrgDescriptionList"))
3510 "text:continue-numbering=\"true\"")))
3514 (if (funcall --element-preceded-by-table-p table info
)
3515 '("</text:list-header>" .
"<text:list-header>")
3516 '("</text:list-item>" .
"<text:list-header>")))
3517 ((funcall --element-preceded-by-table-p
3519 '("</text:list-header>" .
"<text:list-header>"))
3520 (t '("</text:list-item>" .
"<text:list-item>"))))))
3521 ;; Handle low-level headlines.
3522 (loop for el in llh-genealogy
3523 with step
= 'item collect
3526 (setq step
'item
) ; Flip-flop
3527 (setq parent-list el
)
3528 (cons "</text:list>"
3529 (format "\n<text:list text:style-name=\"%s\" %s>"
3530 (if (org-export-numbered-headline-p
3534 "text:continue-numbering=\"true\"")))
3536 (setq step
'plain-list
) ; Flip-flop
3539 (if (funcall --element-preceded-by-table-p table info
)
3540 '("</text:list-header>" .
"<text:list-header>")
3541 '("</text:list-item>" .
"<text:list-header>")))
3542 ((let ((section?
(org-export-get-previous-element
3545 (eq (org-element-type section?
) 'section
)
3546 (assq 'table
(org-element-contents section?
))))
3547 '("</text:list-header>" .
"<text:list-header>"))
3549 '("</text:list-item>" .
"<text:list-item>")))))))))))
3550 (close-open-tags (funcall --walk-list-genealogy-and-collect-tags
3552 ;; OpenDocument schema does not permit table to occur within a
3555 ;; One solution - the easiest and lightweight, in terms of
3556 ;; implementation - is to put the table in an indented text box
3557 ;; and make the text box part of the list-item. Unfortunately if
3558 ;; the table is big and spans multiple pages, the text box could
3559 ;; overflow. In this case, the following attribute will come
3562 ;; ,---- From OpenDocument-v1.1.pdf
3563 ;; | 15.27.28 Overflow behavior
3565 ;; | For text boxes contained within text document, the
3566 ;; | style:overflow-behavior property specifies the behavior of text
3567 ;; | boxes where the containing text does not fit into the text
3570 ;; | If the attribute's value is clip, the text that does not fit
3571 ;; | into the text box is not displayed.
3573 ;; | If the attribute value is auto-create-new-frame, a new frame
3574 ;; | will be created on the next page, with the same position and
3575 ;; | dimensions of the original frame.
3577 ;; | If the style:overflow-behavior property's value is
3578 ;; | auto-create-new-frame and the text box has a minimum width or
3579 ;; | height specified, then the text box will grow until the page
3580 ;; | bounds are reached before a new frame is created.
3583 ;; Unfortunately, LibreOffice-3.4.6 doesn't honor
3584 ;; auto-create-new-frame property and always resorts to clipping
3585 ;; the text box. This results in table being truncated.
3587 ;; So we solve the problem the hard (and fun) way using list
3590 ;; The problem only becomes more interesting if you take in to
3591 ;; account the following facts:
3593 ;; - Description lists are simulated as plain lists.
3594 ;; - Low-level headlines can be listified.
3595 ;; - In Org-mode, a table can occur not only as a regular list
3596 ;; item, but also within description lists and low-level
3599 ;; See `org-e-odt-translate-description-lists' and
3600 ;; `org-e-odt-translate-low-level-headlines' for how this is
3604 ;; Discontinue the list.
3605 (mapconcat 'car close-open-tags
"\n")
3606 ;; Put the table in an indented section.
3607 (let* ((table (org-e-odt--table table contents info
))
3608 (level (/ (length (mapcar 'car close-open-tags
)) 2))
3609 (style (format "OrgIndentedSection-Level-%d" level
)))
3610 (when table
(org-e-odt-format-section table style
)))
3611 ;; Continue the list.
3612 (mapconcat 'cdr
(nreverse close-open-tags
) "\n"))))
3617 (defun org-e-odt-target (target contents info
)
3618 "Transcode a TARGET object from Org to ODT.
3619 CONTENTS is nil. INFO is a plist holding contextual
3621 (let ((value (org-element-property :value target
)))
3622 (org-e-odt--target "" (org-export-solidify-link-text value
))))
3627 (defun org-e-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* ((raw-value (org-element-property :raw-value timestamp
))
3632 (type (org-element-property :type timestamp
)))
3633 (if (not org-e-odt-use-date-fields
)
3634 (let ((value (org-e-odt-plain-text
3635 (org-export-translate-timestamp timestamp
) info
)))
3636 (case (org-element-property :type timestamp
)
3637 ((active active-range
)
3638 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3639 "OrgActiveTimestamp" value
))
3640 ((inactive inactive-range
)
3641 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3642 "OrgInactiveTimestamp" value
))
3646 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3647 "OrgActiveTimestamp"
3648 (format "<%s>" (org-e-odt--format-timestamp timestamp
))))
3650 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3651 "OrgInactiveTimestamp"
3652 (format "[%s]" (org-e-odt--format-timestamp timestamp
))))
3654 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3655 "OrgActiveTimestamp"
3656 (format "<%s>–<%s>"
3657 (org-e-odt--format-timestamp timestamp
)
3658 (org-e-odt--format-timestamp timestamp
'end
))))
3660 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3661 "OrgInactiveTimestamp"
3662 (format "[%s]–[%s]"
3663 (org-e-odt--format-timestamp timestamp
)
3664 (org-e-odt--format-timestamp timestamp
'end
))))
3666 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3668 (org-e-odt-plain-text (org-export-translate-timestamp
3669 timestamp
) info
)))))))
3674 (defun org-e-odt-underline (underline contents info
)
3675 "Transcode UNDERLINE from Org to ODT.
3676 CONTENTS is the text with underline markup. INFO is a plist
3677 holding contextual information."
3678 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3679 "Underline" contents
))
3684 (defun org-e-odt-verbatim (verbatim contents info
)
3685 "Transcode a VERBATIM object from Org to ODT.
3686 CONTENTS is nil. INFO is a plist used as a communication
3688 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3689 "OrgCode" (org-element-property :value verbatim
)))
3694 (defun org-e-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-e-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-e-odt--translate-latex-fragments (tree backend info
)
3715 (let ((processing-type (plist-get info
:LaTeX-fragments
))
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
)))
3728 (unless (and (org-check-external-command "latex" "" t
)
3729 (org-check-external-command "dvipng" "" t
))
3730 (message "LaTeX to PNG converter not available.")
3731 (setq processing-type
'verbatim
)))
3733 (message "Unknown LaTeX option. Forcing verbatim.")
3734 (setq processing-type
'verbatim
)))
3736 ;; Store normalized value for later use.
3737 (when (plist-get info
:LaTeX-fragments
)
3738 (plist-put info
:LaTeX-fragments processing-type
))
3739 (message "Formatting LaTeX using %s" processing-type
)
3741 ;; Convert `latex-fragment's and `latex-environment's.
3742 (when (memq processing-type
'(mathml dvipng
))
3744 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
3753 (mathml "ltxmathml/"))
3754 (file-name-sans-extension
3755 (file-name-nondirectory input-file
))))
3757 (case processing-type
3758 (dvipng (format "Creating LaTeX Image %d..." count
))
3759 (mathml (format "Creating MathML snippet %d..." count
))))
3760 ;; Get an Org-style link to PNG image or the MathML
3763 (let ((link (with-temp-buffer
3765 (org-format-latex cache-subdir cache-dir
3767 nil nil processing-type
)
3768 (buffer-substring-no-properties
3769 (point-min) (point-max)))))
3770 (if (not (string-match "file:\\([^]]*\\)" link
))
3771 (prog1 nil
(message "LaTeX Conversion failed."))
3774 ;; Conversion succeeded. Parse above Org-style link to a
3776 (let* ((link (car (org-element-map (with-temp-buffer
3779 (org-element-parse-buffer))
3782 (org-element-put-property link
:parent nil
)
3785 (case (org-element-type latex-
*)
3786 ;; Case 1: LaTeX environment.
3787 ;; Mimic a "standalone image or formula" by
3788 ;; enclosing the `link' in a `paragraph'.
3789 ;; Copy over original attributes, captions to
3790 ;; the enclosing paragraph.
3792 (org-element-adopt-elements
3794 (list :style
"OrgFormula"
3795 :name
(org-element-property :name
3797 :caption
(org-element-property :caption
3800 ;; Case 2: LaTeX fragment.
3801 ;; No special action.
3802 (latex-fragment link
))))
3803 ;; Note down the object that link replaces.
3804 (org-element-put-property replacement
:replaces
3805 (list (org-element-type latex-
*)
3806 (list :value latex-frag
)))
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-e-odt--table'.
3818 (defun org-e-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
3852 (when (equal (org-element-property :type el
) 'descriptive
)
3853 (org-element-set-element
3855 (apply 'org-element-adopt-elements
3856 (list 'plain-list
(list :type
'descriptive-1
))
3859 (org-element-adopt-elements
3860 (list 'item
(list :checkbox
(org-element-property
3862 (list 'paragraph
(list :style
"Text_20_body_20_bold")
3863 (or (org-element-property :tag item
) "(no term)"))
3864 (org-element-adopt-elements
3865 (list 'plain-list
(list :type
'descriptive-2
))
3866 (apply 'org-element-adopt-elements
3868 (org-element-contents item
)))))
3869 (org-element-contents el
)))))
3876 ;; Lists that are marked with attribute `:list-table' are called as
3877 ;; list tables. They will be rendered as a table within the exported
3880 ;; Consider an example. The following list table
3882 ;; #+attr_odt :list-table t
3892 ;; will be exported as though it were an Org table like the one show
3895 ;; | Row 1 | 1.1 | 1.2 | 1.3 |
3896 ;; | Row 2 | 2.1 | 2.2 | 2.3 |
3898 ;; Note that org-tables are NOT multi-line and each line is mapped to
3899 ;; a unique row in the exported document. So if an exported table
3900 ;; needs to contain a single paragraph (with copious text) it needs to
3901 ;; be typed up in a single line. Editing such long lines using the
3902 ;; table editor will be a cumbersome task. Furthermore inclusion of
3903 ;; multi-paragraph text in a table cell is well-nigh impossible.
3905 ;; A LIST-TABLE circumvents above problems.
3907 ;; Note that in the example above the list items could be paragraphs
3908 ;; themselves and the list can be arbitrarily deep.
3910 ;; Inspired by following thread:
3911 ;; https://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01101.html
3913 ;; Translate lists to tables
3915 (defun org-e-odt--translate-list-tables (tree backend info
)
3919 (when (org-export-read-attribute :attr_odt l1-list
:list-table
)
3920 ;; Replace list with table.
3921 (org-element-set-element
3923 ;; Build replacement table.
3924 (apply 'org-element-adopt-elements
3925 (list 'table
'(:type org
:attr_odt
(":style \"GriddedTable\"")))
3930 (let* ((l1-item-contents (org-element-contents l1-item
))
3931 l1-item-leading-text l2-list
)
3932 ;; Remove Level-2 list from the Level-item. It
3933 ;; will be subsequently attached as table-cells.
3934 (let ((cur l1-item-contents
) prev
)
3935 (while (and cur
(not (eq (org-element-type (car cur
))
3938 (setq cur
(cdr cur
)))
3941 (setq l2-list
(car cur
)))
3942 (setq l1-item-leading-text l1-item-contents
))
3943 ;; Level-1 items start a table row.
3944 (apply 'org-element-adopt-elements
3945 (list 'table-row
(list :type
'standard
))
3946 ;; Leading text of level-1 item define the
3947 ;; first table-cell.
3948 (apply 'org-element-adopt-elements
3949 (list 'table-cell nil
)
3950 l1-item-leading-text
)
3951 ;; Level-2 items define subsequent
3952 ;; table-cells of the row.
3957 (apply 'org-element-adopt-elements
3958 (list 'table-cell nil
)
3959 (org-element-contents l2-item
)))
3967 ;;; Interactive functions
3969 (defun org-e-odt-create-manifest-file-entry (&rest args
)
3970 (push args org-e-odt-manifest-file-entries
))
3972 (defun org-e-odt-write-manifest-file ()
3973 (make-directory (concat org-e-odt-zip-dir
"META-INF"))
3974 (let ((manifest-file (concat org-e-odt-zip-dir
"META-INF/manifest.xml")))
3975 (with-current-buffer
3976 (let ((nxml-auto-insert-xml-declaration-flag nil
))
3977 (find-file-noselect manifest-file t
))
3979 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
3980 <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
3982 (lambda (file-entry)
3983 (let* ((version (nth 2 file-entry
))
3984 (extra (if (not version
) ""
3985 (format " manifest:version=\"%s\"" version
))))
3987 (format org-e-odt-manifest-file-entry-tag
3988 (nth 0 file-entry
) (nth 1 file-entry
) extra
))))
3989 org-e-odt-manifest-file-entries
)
3990 (insert "\n</manifest:manifest>"))))
3992 (defmacro org-e-odt--export-wrap
(out-file &rest body
)
3993 `(let* ((--out-file ,out-file
)
3994 (out-file-type (file-name-extension --out-file
))
3995 (org-e-odt-xml-files '("META-INF/manifest.xml" "content.xml"
3996 "meta.xml" "styles.xml"))
3997 ;; Initialize temporary workarea. All files that end up in
3998 ;; the exported document get parked/created here.
3999 (org-e-odt-zip-dir (file-name-as-directory
4000 (make-temp-file (format "%s-" out-file-type
) t
)))
4001 (org-e-odt-manifest-file-entries nil
)
4002 (--cleanup-xml-buffers
4005 ;; Kill all XML buffers.
4006 (mapc (lambda (file)
4007 (let ((buf (find-buffer-visiting
4008 (concat org-e-odt-zip-dir file
))))
4010 (with-current-buffer buf
4011 (set-buffer-modified-p nil
)
4012 (kill-buffer buf
)))))
4013 org-e-odt-xml-files
)
4014 ;; Delete temporary directory and also other embedded
4015 ;; files that get copied there.
4016 (delete-directory org-e-odt-zip-dir t
)))))
4017 (org-condition-case-unless-debug err
4019 (unless (executable-find "zip")
4020 ;; Not at all OSes ship with zip by default
4021 (error "Executable \"zip\" needed for creating OpenDocument files"))
4022 ;; Do export. This creates a bunch of xml files ready to be
4023 ;; saved and zipped.
4025 ;; Create a manifest entry for content.xml.
4026 (org-e-odt-create-manifest-file-entry "text/xml" "content.xml")
4027 ;; Write mimetype file
4029 '(("odt" .
"application/vnd.oasis.opendocument.text")
4030 ("odf" .
"application/vnd.oasis.opendocument.formula")))
4031 (mimetype (cdr (assoc-string out-file-type mimetypes t
))))
4033 (error "Unknown OpenDocument backend %S" out-file-type
))
4034 (write-region mimetype nil
(concat org-e-odt-zip-dir
"mimetype"))
4035 (org-e-odt-create-manifest-file-entry mimetype
"/" "1.2"))
4036 ;; Write out the manifest entries before zipping
4037 (org-e-odt-write-manifest-file)
4038 ;; Save all XML files.
4039 (mapc (lambda (file)
4040 (let ((buf (find-buffer-visiting
4041 (concat org-e-odt-zip-dir file
))))
4043 (with-current-buffer buf
4044 ;; Prettify output if needed.
4045 (when org-e-odt-prettify-xml
4046 (indent-region (point-min) (point-max)))
4048 org-e-odt-xml-files
)
4050 (let* ((target --out-file
)
4051 (target-name (file-name-nondirectory target
))
4052 (cmds `(("zip" "-mX0" ,target-name
"mimetype")
4053 ("zip" "-rmTq" ,target-name
"."))))
4054 ;; If a file with same name as the desired output file
4055 ;; exists, remove it.
4056 (when (file-exists-p target
)
4057 (delete-file target
))
4058 ;; Zip up the xml files.
4059 (let ((coding-system-for-write 'no-conversion
) exitcode err-string
)
4060 (message "Creating ODT file...")
4061 ;; Switch temporarily to content.xml. This way Zip
4062 ;; process will inherit `org-e-odt-zip-dir' as the current
4064 (with-current-buffer
4065 (find-file-noselect (concat org-e-odt-zip-dir
"content.xml") t
)
4068 (message "Running %s" (mapconcat 'identity cmd
" "))
4070 (with-output-to-string
4072 (apply 'call-process
(car cmd
)
4073 nil standard-output nil
(cdr cmd
)))))
4074 (or (zerop exitcode
)
4075 (error (concat "Unable to create OpenDocument file."
4076 (format " Zip failed with error (%s)"
4079 ;; Move the zip file from temporary work directory to
4080 ;; user-mandated location.
4081 (rename-file (concat org-e-odt-zip-dir target-name
) target
)
4082 (message "Created %s" (expand-file-name target
))
4083 ;; Cleanup work directory and work files.
4084 (funcall --cleanup-xml-buffers
)
4085 ;; Open the OpenDocument file in archive-mode for
4087 (find-file-noselect target t
)
4088 ;; Return exported file.
4090 ;; Case 1: Conversion desired on exported file. Run the
4091 ;; converter on the OpenDocument file. Return the
4093 (org-e-odt-preferred-output-format
4094 (or (org-e-odt-convert target org-e-odt-preferred-output-format
)
4096 ;; Case 2: No further conversion. Return exported
4097 ;; OpenDocument file.
4100 ;; Cleanup work directory and work files.
4101 (funcall --cleanup-xml-buffers
)
4102 (message "OpenDocument export failed: %s"
4103 (error-message-string err
))))))
4106 ;;;; Export to OpenDocument formula
4109 (defun org-e-odt-export-as-odf (latex-frag &optional odf-file
)
4110 "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
4111 Use `org-create-math-formula' to convert LATEX-FRAG first to
4112 MathML. When invoked as an interactive command, use
4113 `org-latex-regexps' to infer LATEX-FRAG from currently active
4114 region. If no LaTeX fragments are found, prompt for it. Push
4115 MathML source to kill ring, if `org-export-copy-to-kill-ring' is
4119 (setq frag
(and (setq frag
(and (region-active-p)
4120 (buffer-substring (region-beginning)
4122 (loop for e in org-latex-regexps
4123 thereis
(when (string-match (nth 1 e
) frag
)
4124 (match-string (nth 2 e
) frag
)))))
4125 (read-string "LaTeX Fragment: " frag nil frag
))
4126 ,(let ((odf-filename (expand-file-name
4128 (file-name-sans-extension
4129 (or (file-name-nondirectory buffer-file-name
)))
4131 (file-name-directory buffer-file-name
))))
4132 (read-file-name "ODF filename: " nil odf-filename nil
4133 (file-name-nondirectory odf-filename
)))))
4134 (let ((filename (or odf-file
4137 (file-name-sans-extension
4138 (or (file-name-nondirectory buffer-file-name
)))
4140 (file-name-directory buffer-file-name
)))))
4141 (org-e-odt--export-wrap
4143 (let* ((buffer (progn
4144 (require 'nxml-mode
)
4145 (let ((nxml-auto-insert-xml-declaration-flag nil
))
4146 (find-file-noselect (concat org-e-odt-zip-dir
4147 "content.xml") t
))))
4148 (coding-system-for-write 'utf-8
)
4149 (save-buffer-coding-system 'utf-8
))
4151 (set-buffer-file-coding-system coding-system-for-write
)
4152 (let ((mathml (org-create-math-formula latex-frag
)))
4153 (unless mathml
(error "No Math formula created"))
4155 ;; Add MathML to kill ring, if needed.
4156 (when org-export-copy-to-kill-ring
4157 (org-kill-new (buffer-string))))))))
4160 (defun org-e-odt-export-as-odf-and-open ()
4161 "Export LaTeX fragment as OpenDocument formula and immediately open it.
4162 Use `org-e-odt-export-as-odf' to read LaTeX fragment and OpenDocument
4165 (org-open-file (call-interactively 'org-e-odt-export-as-odf
) 'system
))
4168 ;;;; Export to OpenDocument Text
4171 (defun org-e-odt-export-to-odt (&optional async subtreep visible-only ext-plist
)
4172 "Export current buffer to a ODT file.
4174 If narrowing is active in the current buffer, only export its
4177 If a region is active, export that region.
4179 A non-nil optional argument ASYNC means the process should happen
4180 asynchronously. The resulting file should be accessible through
4181 the `org-export-stack' interface.
4183 When optional argument SUBTREEP is non-nil, export the sub-tree
4184 at point, extracting information from the headline properties
4187 When optional argument VISIBLE-ONLY is non-nil, don't export
4188 contents of hidden elements.
4190 EXT-PLIST, when provided, is a property list with external
4191 parameters overriding Org default settings, but still inferior to
4192 file-local settings.
4194 Return output file's name."
4196 (let ((outfile (org-export-output-file-name ".odt" subtreep
)))
4198 (org-export-async-start (lambda (f) (org-export-add-to-stack f
'e-odt
))
4200 (org-e-odt--export-wrap
4202 (let* ((org-e-odt-embedded-images-count 0)
4203 (org-e-odt-embedded-formulas-count 0)
4204 (org-e-odt-automatic-styles nil
)
4205 (org-e-odt-object-counters nil
)
4206 ;; Let `htmlfontify' know that we are interested in
4207 ;; collecting styles.
4208 (hfy-user-sheet-assoc nil
))
4209 ;; Initialize content.xml and kick-off the export
4213 (require 'nxml-mode
)
4214 (let ((nxml-auto-insert-xml-declaration-flag nil
))
4216 (concat org-e-odt-zip-dir
"content.xml") t
)))))
4217 (org-export-to-buffer
4218 'e-odt out-buf
,subtreep
,visible-only nil
',ext-plist
))))))
4219 (org-e-odt--export-wrap
4221 (let* ((org-e-odt-embedded-images-count 0)
4222 (org-e-odt-embedded-formulas-count 0)
4223 (org-e-odt-automatic-styles nil
)
4224 (org-e-odt-object-counters nil
)
4225 ;; Let `htmlfontify' know that we are interested in collecting
4227 (hfy-user-sheet-assoc nil
))
4228 ;; Initialize content.xml and kick-off the export process.
4229 (let ((out-buf (progn
4230 (require 'nxml-mode
)
4231 (let ((nxml-auto-insert-xml-declaration-flag nil
))
4233 (concat org-e-odt-zip-dir
"content.xml") t
)))))
4234 (org-export-to-buffer
4235 'e-odt out-buf subtreep visible-only nil ext-plist
)))))))
4238 ;;;; Convert between OpenDocument and other formats
4240 (defun org-e-odt-reachable-p (in-fmt out-fmt
)
4241 "Return non-nil if IN-FMT can be converted to OUT-FMT."
4243 (let ((reachable-formats (org-e-odt-do-reachable-formats in-fmt
)))
4244 (dolist (e reachable-formats
)
4245 (let ((out-fmt-spec (assoc out-fmt
(cdr e
))))
4247 (throw 'done
(cons (car e
) out-fmt-spec
))))))))
4249 (defun org-e-odt-do-convert (in-file out-fmt
&optional prefix-arg
)
4250 "Workhorse routine for `org-e-odt-convert'."
4251 (require 'browse-url
)
4252 (let* ((in-file (expand-file-name (or in-file buffer-file-name
)))
4253 (dummy (or (file-readable-p in-file
)
4254 (error "Cannot read %s" in-file
)))
4255 (in-fmt (file-name-extension in-file
))
4256 (out-fmt (or out-fmt
(error "Output format unspecified")))
4257 (how (or (org-e-odt-reachable-p in-fmt out-fmt
)
4258 (error "Cannot convert from %s format to %s format?"
4260 (convert-process (car how
))
4261 (out-file (concat (file-name-sans-extension in-file
) "."
4262 (nth 1 (or (cdr how
) out-fmt
))))
4263 (extra-options (or (nth 2 (cdr how
)) ""))
4264 (out-dir (file-name-directory in-file
))
4265 (cmd (format-spec convert-process
4266 `((?i .
,(shell-quote-argument in-file
))
4267 (?I .
,(browse-url-file-url in-file
))
4270 (?O .
,(browse-url-file-url out-file
))
4271 (?d .
, (shell-quote-argument out-dir
))
4272 (?D .
,(browse-url-file-url out-dir
))
4273 (?x .
,extra-options
)))))
4274 (when (file-exists-p out-file
)
4275 (delete-file out-file
))
4277 (message "Executing %s" cmd
)
4278 (let ((cmd-output (shell-command-to-string cmd
)))
4279 (message "%s" cmd-output
))
4282 ((file-exists-p out-file
)
4283 (message "Exported to %s" out-file
)
4285 (message "Opening %s..." out-file
)
4286 (org-open-file out-file
'system
))
4289 (message "Export to %s failed" out-file
)
4292 (defun org-e-odt-do-reachable-formats (in-fmt)
4293 "Return verbose info about formats to which IN-FMT can be converted.
4294 Return a list where each element is of the
4295 form (CONVERTER-PROCESS . OUTPUT-FMT-ALIST). See
4296 `org-e-odt-convert-processes' for CONVERTER-PROCESS and see
4297 `org-e-odt-convert-capabilities' for OUTPUT-FMT-ALIST."
4299 (and org-e-odt-convert-process
4300 (cadr (assoc-string org-e-odt-convert-process
4301 org-e-odt-convert-processes t
))))
4303 (and org-e-odt-convert-process
4304 (cadr (assoc-string org-e-odt-convert-process
4305 org-e-odt-convert-processes t
))
4306 org-e-odt-convert-capabilities
))
4309 (dolist (c capabilities
)
4310 (when (member in-fmt
(nth 1 c
))
4311 (push (cons converter
(nth 2 c
)) reachable-formats
))))
4314 (defun org-e-odt-reachable-formats (in-fmt)
4315 "Return list of formats to which IN-FMT can be converted.
4316 The list of the form (OUTPUT-FMT-1 OUTPUT-FMT-2 ...)."
4318 (mapc (lambda (e) (add-to-list 'l e
))
4319 (apply 'append
(mapcar
4320 (lambda (e) (mapcar 'car
(cdr e
)))
4321 (org-e-odt-do-reachable-formats in-fmt
))))
4324 (defun org-e-odt-convert-read-params ()
4325 "Return IN-FILE and OUT-FMT params for `org-e-odt-do-convert'.
4326 This is a helper routine for interactive use."
4327 (let* ((input (if (featurep 'ido
) 'ido-completing-read
'completing-read
))
4328 (in-file (read-file-name "File to be converted: "
4329 nil buffer-file-name t
))
4330 (in-fmt (file-name-extension in-file
))
4331 (out-fmt-choices (org-e-odt-reachable-formats in-fmt
))
4333 (or (and out-fmt-choices
4334 (funcall input
"Output format: "
4335 out-fmt-choices nil nil nil
))
4337 "No known converter or no known output formats for %s files"
4339 (list in-file out-fmt
)))
4342 (defun org-e-odt-convert (&optional in-file out-fmt prefix-arg
)
4343 "Convert IN-FILE to format OUT-FMT using a command line converter.
4344 IN-FILE is the file to be converted. If unspecified, it defaults
4345 to variable `buffer-file-name'. OUT-FMT is the desired output
4346 format. Use `org-e-odt-convert-process' as the converter.
4347 If PREFIX-ARG is non-nil then the newly converted file is opened
4348 using `org-open-file'."
4350 (append (org-e-odt-convert-read-params) current-prefix-arg
))
4351 (org-e-odt-do-convert in-file out-fmt prefix-arg
))
4353 ;;; Library Initializations
4357 ;; Let Emacs open all OpenDocument files in archive mode
4358 (add-to-list 'auto-mode-alist
4359 (cons (concat "\\." (car desc
) "\\'") 'archive-mode
)))
4360 org-e-odt-file-extensions
)
4362 (provide 'org-e-odt
)
4364 ;;; org-e-odt.el ends here