1 ;;; org-e-odt.el --- OpenDocument Text exporter for Org-mode
3 ;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
5 ;; Author: Jambunathan K <kjambunathan at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
9 ;; This file is not part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
31 (require 'format-spec
)
36 (org-export-define-backend e-odt
37 ((bold . org-e-odt-bold
)
38 (center-block . org-e-odt-center-block
)
39 (clock . org-e-odt-clock
)
40 (code . org-e-odt-code
)
41 (drawer . org-e-odt-drawer
)
42 (dynamic-block . org-e-odt-dynamic-block
)
43 (entity . org-e-odt-entity
)
44 (example-block . org-e-odt-example-block
)
45 (export-block . org-e-odt-export-block
)
46 (export-snippet . org-e-odt-export-snippet
)
47 (fixed-width . org-e-odt-fixed-width
)
48 (footnote-definition . org-e-odt-footnote-definition
)
49 (footnote-reference . org-e-odt-footnote-reference
)
50 (headline . org-e-odt-headline
)
51 (horizontal-rule . org-e-odt-horizontal-rule
)
52 (inline-src-block . org-e-odt-inline-src-block
)
53 (inlinetask . org-e-odt-inlinetask
)
54 (italic . org-e-odt-italic
)
55 (item . org-e-odt-item
)
56 (keyword . org-e-odt-keyword
)
57 (latex-environment . org-e-odt-latex-environment
)
58 (latex-fragment . org-e-odt-latex-fragment
)
59 (line-break . org-e-odt-line-break
)
60 (link . org-e-odt-link
)
61 (paragraph . org-e-odt-paragraph
)
62 (plain-list . org-e-odt-plain-list
)
63 (plain-text . org-e-odt-plain-text
)
64 (planning . org-e-odt-planning
)
65 (property-drawer . org-e-odt-property-drawer
)
66 (quote-block . org-e-odt-quote-block
)
67 (quote-section . org-e-odt-quote-section
)
68 (radio-target . org-e-odt-radio-target
)
69 (section . org-e-odt-section
)
70 (special-block . org-e-odt-special-block
)
71 (src-block . org-e-odt-src-block
)
72 (statistics-cookie . org-e-odt-statistics-cookie
)
73 (strike-through . org-e-odt-strike-through
)
74 (subscript . org-e-odt-subscript
)
75 (superscript . org-e-odt-superscript
)
76 (table . org-e-odt-table
)
77 (table-cell . org-e-odt-table-cell
)
78 (table-row . org-e-odt-table-row
)
79 (target . org-e-odt-target
)
80 (template . org-e-odt-template
)
81 (timestamp . org-e-odt-timestamp
)
82 (underline . org-e-odt-underline
)
83 (verbatim . org-e-odt-verbatim
)
84 (verse-block . org-e-odt-verse-block
))
86 :filters-alist
((:filter-parse-tree
87 .
(org-e-odt--translate-latex-fragments
88 org-e-odt--translate-description-lists
89 org-e-odt--translate-list-tables
)))
92 ((?o
"As ODT file" org-e-odt-export-to-odt
)
93 (?O
"As ODT file and open"
95 (if a
(org-e-odt-export-to-odt t s v
)
96 (org-open-file (org-e-odt-export-to-odt nil s v
) 'system
))))))
98 ((:odt-styles-file
"ODT_STYLES_FILE" nil nil t
)
99 (:LaTeX-fragments nil
"LaTeX" org-export-with-LaTeX-fragments
)))
106 ;;; Function Declarations
108 (declare-function org-id-find-id-file
"org-id" (id))
109 (declare-function hfy-face-to-style
"htmlfontify" (fn))
110 (declare-function hfy-face-or-def-to-name
"htmlfontify" (fn))
111 (declare-function archive-zip-extract
"arc-mode" (archive name
))
112 (declare-function org-create-math-formula
"org" (latex-frag &optional mathml-file
))
113 (declare-function browse-url-file-url
"browse-url" (file))
117 ;;; Internal Variables
119 (defconst org-e-odt-lib-dir
120 (file-name-directory load-file-name
)
121 "Location of ODT exporter.
122 Use this to infer values of `org-e-odt-styles-dir' and
123 `org-e-odt-schema-dir'.")
125 (defvar org-e-odt-data-dir
126 (expand-file-name "../../etc/" org-e-odt-lib-dir
)
127 "Data directory for ODT exporter.
128 Use this to infer values of `org-e-odt-styles-dir' and
129 `org-e-odt-schema-dir'.")
131 (defconst org-e-odt-special-string-regexps
132 '(("\\\\-" .
"­\\1") ; shy
133 ("---\\([^-]\\)" .
"—\\1") ; mdash
134 ("--\\([^-]\\)" .
"–\\1") ; ndash
135 ("\\.\\.\\." .
"…")) ; hellip
136 "Regular expressions for special string conversion.")
138 (defconst org-e-odt-schema-dir-list
140 (and org-e-odt-data-dir
141 (expand-file-name "./schema/" org-e-odt-data-dir
)) ; bail out
143 (and (boundp 'org-e-odt-data-dir
) org-e-odt-data-dir
; see make install
144 (expand-file-name "./schema/" org-e-odt-data-dir
))))
145 "List of directories to search for OpenDocument schema files.
146 Use this list to set the default value of
147 `org-e-odt-schema-dir'. The entries in this list are
148 populated heuristically based on the values of `org-e-odt-lib-dir'
149 and `org-e-odt-data-dir'.")
151 (defconst org-e-odt-styles-dir-list
153 (and org-e-odt-data-dir
154 (expand-file-name "./styles/" org-e-odt-data-dir
)) ; bail out
156 (and (boundp 'org-e-odt-data-dir
) org-e-odt-data-dir
; see make install
157 (expand-file-name "./styles/" org-e-odt-data-dir
)))
158 (expand-file-name "../../etc/styles/" org-e-odt-lib-dir
) ; git
159 (expand-file-name "./etc/styles/" org-e-odt-lib-dir
) ; elpa
160 (expand-file-name "./org/" data-directory
) ; system
162 "List of directories to search for OpenDocument styles files.
163 See `org-e-odt-styles-dir'. The entries in this list are populated
164 heuristically based on the values of `org-e-odt-lib-dir' and
165 `org-e-odt-data-dir'.")
167 (defconst org-e-odt-styles-dir
170 (message "Debug (org-e-odt): Searching for OpenDocument styles files...")
171 (mapc (lambda (styles-dir)
173 (message "Debug (org-e-odt): Trying %s..." styles-dir
)
174 (when (and (file-readable-p
176 "OrgOdtContentTemplate.xml" styles-dir
))
179 "OrgOdtStyles.xml" styles-dir
)))
180 (message "Debug (org-e-odt): Using styles under %s"
182 (throw 'styles-dir styles-dir
))))
183 org-e-odt-styles-dir-list
)
186 (error "Error (org-e-odt): Cannot find factory styles files, aborting"))
188 "Directory that holds auxiliary XML files used by the ODT exporter.
190 This directory contains the following XML files -
191 \"OrgOdtStyles.xml\" and \"OrgOdtContentTemplate.xml\". These
192 XML files are used as the default values of
193 `org-e-odt-styles-file' and
194 `org-e-odt-content-template-file'.
196 The default value of this variable varies depending on the
197 version of org in use and is initialized from
198 `org-e-odt-styles-dir-list'. Note that the user could be using org
199 from one of: org's own private git repository, GNU ELPA tar or
202 (defconst org-e-odt-bookmark-prefix
"OrgXref.")
204 (defconst org-e-odt-manifest-file-entry-tag
205 "\n<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
207 (defconst org-e-odt-file-extensions
208 '(("odt" .
"OpenDocument Text")
209 ("ott" .
"OpenDocument Text Template")
210 ("odm" .
"OpenDocument Master Document")
211 ("ods" .
"OpenDocument Spreadsheet")
212 ("ots" .
"OpenDocument Spreadsheet Template")
213 ("odg" .
"OpenDocument Drawing (Graphics)")
214 ("otg" .
"OpenDocument Drawing Template")
215 ("odp" .
"OpenDocument Presentation")
216 ("otp" .
"OpenDocument Presentation Template")
217 ("odi" .
"OpenDocument Image")
218 ("odf" .
"OpenDocument Formula")
219 ("odc" .
"OpenDocument Chart")))
221 (defconst org-e-odt-table-style-format
223 <style:style style:name=\"%s\" style:family=\"table\">
224 <style:table-properties style:rel-width=\"%d%%\" fo:margin-top=\"0cm\" fo:margin-bottom=\"0.20cm\" table:align=\"center\"/>
227 "Template for auto-generated Table styles.")
229 (defvar org-e-odt-automatic-styles
'()
230 "Registry of automatic styles for various OBJECT-TYPEs.
231 The variable has the following form:
233 \(\(OBJECT-NAME-A.1 OBJECT-PROPS-A.1\)
234 \(OBJECT-NAME-A.2 OBJECT-PROPS-A.2\) ...\)\)
236 \(\(OBJECT-NAME-B.1 OBJECT-PROPS-B.1\)
237 \(OBJECT-NAME-B.2 OBJECT-PROPS-B.2\) ...\)\)
240 OBJECT-TYPEs could be \"Section\", \"Table\", \"Figure\" etc.
241 OBJECT-PROPS is (typically) a plist created by passing
242 \"#+ATTR_ODT: \" option to `org-e-odt-parse-block-attributes'.
244 Use `org-e-odt-add-automatic-style' to add update this variable.'")
246 (defvar org-e-odt-object-counters nil
247 "Running counters for various OBJECT-TYPEs.
248 Use this to generate automatic names and style-names. See
249 `org-e-odt-add-automatic-style'.")
251 (defvar org-e-odt-src-block-paragraph-format
252 "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
253 <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
254 <style:background-image/>
255 </style:paragraph-properties>
256 <style:text-properties fo:color=\"%s\"/>
258 "Custom paragraph style for colorized source and example blocks.
259 This style is much the same as that of \"OrgFixedWidthBlock\"
260 except that the foreground and background colors are set
261 according to the default face identified by the `htmlfontify'.")
263 (defvar hfy-optimisations
)
264 (defvar org-e-odt-embedded-formulas-count
0)
265 (defvar org-e-odt-embedded-images-count
0)
266 (defvar org-e-odt-image-size-probe-method
267 (append (and (executable-find "identify") '(imagemagick)) ; See Bug#10675
269 "Ordered list of methods for determining image sizes.")
271 (defvar org-e-odt-default-image-sizes-alist
272 '(("as-char" .
(5 .
0.4))
273 ("paragraph" .
(5 .
5)))
274 "Hardcoded image dimensions one for each of the anchor
277 ;; A4 page size is 21.0 by 29.7 cms
278 ;; The default page settings has 2cm margin on each of the sides. So
279 ;; the effective text area is 17.0 by 25.7 cm
280 (defvar org-e-odt-max-image-size
'(17.0 .
20.0)
281 "Limiting dimensions for an embedded image.")
283 (defconst org-e-odt-label-styles
284 '(("math-formula" "%c" "text" "(%n)")
285 ("math-label" "(%n)" "text" "(%n)")
286 ("category-and-value" "%e %n: %c" "category-and-value" "%e %n")
287 ("value" "%e %n: %c" "value" "%n"))
288 "Specify how labels are applied and referenced.
289 This is an alist where each element is of the
290 form (LABEL-STYLE-NAME LABEL-ATTACH-FMT LABEL-REF-MODE
293 LABEL-ATTACH-FMT controls how labels and captions are attached to
294 an entity. It may contain following specifiers - %e, %n and %c.
295 %e is replaced with the CATEGORY-NAME. %n is replaced with
296 \"<text:sequence ...> SEQNO </text:sequence>\". %c is replaced
297 with CAPTION. See `org-e-odt-format-label-definition'.
299 LABEL-REF-MODE and LABEL-REF-FMT controls how label references
300 are generated. The following XML is generated for a label
301 reference - \"<text:sequence-ref
302 text:reference-format=\"LABEL-REF-MODE\" ...> LABEL-REF-FMT
303 </text:sequence-ref>\". LABEL-REF-FMT may contain following
304 specifiers - %e and %n. %e is replaced with the CATEGORY-NAME.
305 %n is replaced with SEQNO. See
306 `org-e-odt-format-label-reference'.")
308 (defvar org-e-odt-category-map-alist
309 '(("__Table__" "Table" "value" "Table" org-e-odt--enumerable-p
)
310 ("__Figure__" "Illustration" "value" "Figure" org-e-odt--enumerable-image-p
)
311 ("__MathFormula__" "Text" "math-formula" "Equation" org-e-odt--enumerable-formula-p
)
312 ("__DvipngImage__" "Equation" "value" "Equation" org-e-odt--enumerable-latex-image-p
)
313 ("__Listing__" "Listing" "value" "Listing" org-e-odt--enumerable-p
)
314 ;; ("__Table__" "Table" "category-and-value")
315 ;; ("__Figure__" "Figure" "category-and-value")
316 ;; ("__DvipngImage__" "Equation" "category-and-value")
318 "Map a CATEGORY-HANDLE to OD-VARIABLE and LABEL-STYLE.
319 This is a list where each entry is of the form \\(CATEGORY-HANDLE
320 OD-VARIABLE LABEL-STYLE CATEGORY-NAME ENUMERATOR-PREDICATE\\).
322 CATEGORY_HANDLE identifies the captionable entity in question.
324 OD-VARIABLE is the OpenDocument sequence counter associated with
325 the entity. These counters are declared within
326 \"<text:sequence-decls>...</text:sequence-decls>\" block of
327 `org-e-odt-content-template-file'.
329 LABEL-STYLE is a key into `org-e-odt-label-styles' and specifies
330 how a given entity should be captioned and referenced.
332 CATEGORY-NAME is used for qualifying captions on export. You can
333 modify the CATEGORY-NAME used in the exported document by
334 modifying `org-export-dictionary'. For example, an embedded
335 image in an English document is captioned as \"Figure 1: Orgmode
336 Logo\", by default. If you want the image to be captioned as
337 \"Illustration 1: Orgmode Logo\" instead, install an entry in
338 `org-export-dictionary' which translates \"Figure\" to
339 \"Illustration\" when the language is \"en\" and encoding is
342 ENUMERATOR-PREDICATE is used for assigning a sequence number to
343 the entity. See `org-e-odt--enumerate'.")
345 (defvar org-e-odt-manifest-file-entries nil
)
346 (defvar hfy-user-sheet-assoc
)
348 (defvar org-e-odt-zip-dir nil
349 "Temporary work directory for OpenDocument exporter.")
353 ;;; User Configuration Variables
355 (defgroup org-export-e-odt nil
356 "Options for exporting Org mode files to ODT."
357 :tag
"Org Export ODT"
363 (defcustom org-e-odt-prettify-xml nil
364 "Specify whether or not the xml output should be prettified.
365 When this option is turned on, `indent-region' is run on all
366 component xml buffers before they are saved. Turn this off for
367 regular use. Turn this on if you need to examine the xml
369 :group
'org-export-e-odt
376 (defcustom org-e-odt-schema-dir
379 (message "Debug (org-e-odt): Searching for OpenDocument schema files...")
383 (message "Debug (org-e-odt): Trying %s..." schema-dir
)
384 (when (and (file-expand-wildcards
385 (expand-file-name "od-manifest-schema*.rnc"
387 (file-expand-wildcards
388 (expand-file-name "od-schema*.rnc"
391 (expand-file-name "schemas.xml" schema-dir
)))
392 (message "Debug (org-e-odt): Using schema files under %s"
394 (throw 'schema-dir schema-dir
))))
395 org-e-odt-schema-dir-list
)
396 (message "Debug (org-e-odt): No OpenDocument schema files installed")
399 "Directory that contains OpenDocument schema files.
401 This directory contains:
402 1. rnc files for OpenDocument schema
403 2. a \"schemas.xml\" file that specifies locating rules needed
404 for auto validation of OpenDocument XML files.
406 Use the customize interface to set this variable. This ensures
407 that `rng-schema-locating-files' is updated and auto-validation
408 of OpenDocument XML takes place based on the value
409 `rng-nxml-auto-validate-flag'.
411 The default value of this variable varies depending on the
412 version of org in use and is initialized from
413 `org-e-odt-schema-dir-list'. The OASIS schema files are available
414 only in the org's private git repository. It is *not* bundled
415 with GNU ELPA tar or standard Emacs distribution."
417 (const :tag
"Not set" nil
)
418 (directory :tag
"Schema directory"))
419 :group
'org-export-e-odt
423 "Set `org-e-odt-schema-dir'.
424 Also add it to `rng-schema-locating-files'."
425 (let ((schema-dir value
))
428 (file-expand-wildcards
429 (expand-file-name "od-manifest-schema*.rnc" schema-dir
))
430 (file-expand-wildcards
431 (expand-file-name "od-schema*.rnc" schema-dir
))
433 (expand-file-name "schemas.xml" schema-dir
)))
436 (message "Error (org-e-odt): %s has no OpenDocument schema files"
439 (when org-e-odt-schema-dir
440 (eval-after-load 'rng-loc
441 '(add-to-list 'rng-schema-locating-files
442 (expand-file-name "schemas.xml"
443 org-e-odt-schema-dir
))))))
448 (defcustom org-e-odt-content-template-file nil
449 "Template file for \"content.xml\".
450 The exporter embeds the exported content just before
451 \"</office:text>\" element.
453 If unspecified, the file named \"OrgOdtContentTemplate.xml\"
454 under `org-e-odt-styles-dir' is used."
456 :group
'org-export-e-odt
459 (defcustom org-e-odt-styles-file nil
460 "Default styles file for use with ODT export.
461 Valid values are one of:
463 2. path to a styles.xml file
464 3. path to a *.odt or a *.ott file
465 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
468 In case of option 1, an in-built styles.xml is used. See
469 `org-e-odt-styles-dir' for more information.
471 In case of option 3, the specified file is unzipped and the
472 styles.xml embedded therein is used.
474 In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
475 and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
476 generated odt file. Use relative path for specifying the
477 FILE-MEMBERS. styles.xml must be specified as one of the
480 Use options 1, 2 or 3 only if styles.xml alone suffices for
481 achieving the desired formatting. Use option 4, if the styles.xml
482 references additional files like header and footer images for
483 achieving the desired formatting.
485 Use \"#+ODT_STYLES_FILE: ...\" directive to set this variable on
486 a per-file basis. For example,
488 #+ODT_STYLES_FILE: \"/path/to/styles.xml\" or
489 #+ODT_STYLES_FILE: (\"/path/to/file.ott\" (\"styles.xml\" \"image/hdr.png\"))."
490 :group
'org-export-e-odt
494 (const :tag
"Factory settings" nil
)
495 (file :must-match t
:tag
"styles.xml")
496 (file :must-match t
:tag
"ODT or OTT file")
497 (list :tag
"ODT or OTT file + Members"
498 (file :must-match t
:tag
"ODF Text or Text Template file")
500 (file :tag
" Member" "styles.xml")
501 (repeat (file :tag
"Member"))))))
503 (defcustom org-e-odt-display-outline-level
2
504 "Outline levels considered for enumerating captioned entities."
505 :group
'org-export-e-odt
509 ;;;; Document conversion
511 (defcustom org-e-odt-convert-processes
513 "soffice --headless --convert-to %f%x --outdir %d %i")
515 "unoconv -f %f -o %d %i"))
516 "Specify a list of document converters and their usage.
517 The converters in this list are offered as choices while
518 customizing `org-e-odt-convert-process'.
520 This variable is a list where each element is of the
521 form (CONVERTER-NAME CONVERTER-CMD). CONVERTER-NAME is the name
522 of the converter. CONVERTER-CMD is the shell command for the
523 converter and can contain format specifiers. These format
524 specifiers are interpreted as below:
526 %i input file name in full
527 %I input file name as a URL
528 %f format of the output file
529 %o output file name in full
530 %O output file name as a URL
531 %d output dir in full
532 %D output dir as a URL.
533 %x extra options as set in `org-e-odt-convert-capabilities'."
534 :group
'org-export-e-odt
538 (const :tag
"None" nil
)
539 (alist :tag
"Converters"
540 :key-type
(string :tag
"Converter Name")
541 :value-type
(group (string :tag
"Command line")))))
543 (defcustom org-e-odt-convert-process
"LibreOffice"
544 "Use this converter to convert from \"odt\" format to other formats.
545 During customization, the list of converter names are populated
546 from `org-e-odt-convert-processes'."
547 :group
'org-export-e-odt
549 :type
'(choice :convert-widget
551 (apply 'widget-convert
(widget-type w
)
552 (eval (car (widget-get w
:args
)))))
553 `((const :tag
"None" nil
)
554 ,@(mapcar (lambda (c)
555 `(const :tag
,(car c
) ,(car c
)))
556 org-e-odt-convert-processes
))))
558 (defcustom org-e-odt-convert-capabilities
560 ("odt" "ott" "doc" "rtf" "docx")
561 (("pdf" "pdf") ("odt" "odt") ("rtf" "rtf") ("ott" "ott")
562 ("doc" "doc" ":\"MS Word 97\"") ("docx" "docx") ("html" "html")))
565 (("pdf" "pdf") ("odt" "odt") ("html" "html")))
567 ("ods" "ots" "xls" "csv" "xlsx")
568 (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv") ("ods" "ods")
569 ("xls" "xls") ("xlsx" "xlsx")))
571 ("odp" "otp" "ppt" "pptx")
572 (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("otp" "otp") ("ppt" "ppt")
573 ("pptx" "pptx") ("odg" "odg"))))
574 "Specify input and output formats of `org-e-odt-convert-process'.
575 More correctly, specify the set of input and output formats that
576 the user is actually interested in.
578 This variable is an alist where each element is of the
579 form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
580 INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
581 alist where each element is of the form (OUTPUT-FMT
582 OUTPUT-FILE-EXTENSION EXTRA-OPTIONS).
584 The variable is interpreted as follows:
585 `org-e-odt-convert-process' can take any document that is in
586 INPUT-FMT-LIST and produce any document that is in the
587 OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
588 OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
589 serves dual purposes:
590 - It is used for populating completion candidates during
591 `org-e-odt-convert' commands.
592 - It is used as the value of \"%f\" specifier in
593 `org-e-odt-convert-process'.
595 EXTRA-OPTIONS is used as the value of \"%x\" specifier in
596 `org-e-odt-convert-process'.
598 DOCUMENT-CLASS is used to group a set of file formats in
599 INPUT-FMT-LIST in to a single class.
601 Note that this variable inherently captures how LibreOffice based
602 converters work. LibreOffice maps documents of various formats
603 to classes like Text, Web, Spreadsheet, Presentation etc and
604 allow document of a given class (irrespective of it's source
605 format) to be converted to any of the export formats associated
608 See default setting of this variable for an typical
610 :group
'org-export-e-odt
614 (const :tag
"None" nil
)
615 (alist :tag
"Capabilities"
616 :key-type
(string :tag
"Document Class")
618 (group (repeat :tag
"Input formats" (string :tag
"Input format"))
619 (alist :tag
"Output formats"
620 :key-type
(string :tag
"Output format")
622 (group (string :tag
"Output file extension")
624 (const :tag
"None" nil
)
625 (string :tag
"Extra options"))))))))
627 (defcustom org-e-odt-preferred-output-format nil
628 "Automatically post-process to this format after exporting to \"odt\".
629 Command `org-e-odt-export-to-odt' exports first to \"odt\" format
630 and then uses `org-e-odt-convert-process' to convert the
631 resulting document to this format. During customization of this
632 variable, the list of valid values are populated based on
633 `org-e-odt-convert-capabilities'.
635 You can set this option on per-file basis using file local
636 values. See Info node `(emacs) File Variables'."
637 :group
'org-export-e-odt
639 :type
'(choice :convert-widget
641 (apply 'widget-convert
(widget-type w
)
642 (eval (car (widget-get w
:args
)))))
643 `((const :tag
"None" nil
)
644 ,@(mapcar (lambda (c)
646 (org-e-odt-reachable-formats "odt")))))
648 (put 'org-e-odt-preferred-output-format
'safe-local-variable
'stringp
)
653 (defcustom org-e-odt-format-drawer-function nil
654 "Function called to format a drawer in HTML code.
656 The function must accept two parameters:
657 NAME the drawer name, like \"LOGBOOK\"
658 CONTENTS the contents of the drawer.
660 The function should return the string to be exported.
662 For example, the variable could be set to the following function
663 in order to mimic default behaviour:
665 \(defun org-e-odt-format-drawer-default \(name contents\)
666 \"Format a drawer element for HTML export.\"
668 :group
'org-export-e-odt
674 (defcustom org-e-odt-format-headline-function nil
675 "Function to format headline text.
677 This function will be called with 5 arguments:
678 TODO the todo keyword \(string or nil\).
679 TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
680 PRIORITY the priority of the headline \(integer or nil\)
681 TEXT the main headline text \(string\).
682 TAGS the tags string, separated with colons \(string or nil\).
684 The function result will be used in the section format string.
686 As an example, one could set the variable to the following, in
687 order to reproduce the default set-up:
689 \(defun org-e-odt-format-headline \(todo todo-type priority text tags\)
690 \"Default format function for an headline.\"
692 \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo\)\)
694 \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
696 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
697 :group
'org-export-e-odt
703 (defcustom org-e-odt-format-inlinetask-function nil
704 "Function called to format an inlinetask in HTML code.
706 The function must accept six parameters:
707 TODO the todo keyword, as a string
708 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
709 PRIORITY the inlinetask priority, as a string
710 NAME the inlinetask name, as a string.
711 TAGS the inlinetask tags, as a string.
712 CONTENTS the contents of the inlinetask, as a string.
714 The function should return the string to be exported.
716 For example, the variable could be set to the following function
717 in order to mimic default behaviour:
719 \(defun org-e-odt-format-inlinetask \(todo type priority name tags contents\)
720 \"Format an inline task element for HTML export.\"
724 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
725 \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
727 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
728 \(format \(concat \"\\\\begin{center}\\n\"
730 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
732 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
734 \"\\\\end{minipage}}\"
735 \"\\\\end{center}\"\)
736 full-title contents\)\)"
737 :group
'org-export-e-odt
743 (defcustom org-e-odt-inline-formula-rules
744 '(("file" .
"\\.\\(mathml\\|mml\\|odf\\)\\'"))
745 "Rules characterizing formula files that can be inlined into HTML.
747 A rule consists in an association whose key is the type of link
748 to consider, and value is a regexp that will be matched against
750 :group
'org-export-e-odt
751 :type
'(alist :key-type
(string :tag
"Type")
752 :value-type
(regexp :tag
"Path")))
754 (defcustom org-e-odt-inline-image-rules
755 '(("file" .
"\\.\\(jpeg\\|jpg\\|png\\|gif\\)\\'"))
756 "Rules characterizing image files that can be inlined into HTML.
758 A rule consists in an association whose key is the type of link
759 to consider, and value is a regexp that will be matched against
761 :group
'org-export-e-odt
762 :type
'(alist :key-type
(string :tag
"Type")
763 :value-type
(regexp :tag
"Path")))
765 (defcustom org-e-odt-pixels-per-inch display-pixels-per-inch
766 "Scaling factor for converting images pixels to inches.
767 Use this for sizing of embedded images. See Info node `(org)
768 Images in ODT export' for more information."
770 :group
'org-export-e-odt
776 (defcustom org-e-odt-create-custom-styles-for-srcblocks t
777 "Whether custom styles for colorized source blocks be automatically created.
778 When this option is turned on, the exporter creates custom styles
779 for source blocks based on the advice of `htmlfontify'. Creation
780 of custom styles happen as part of `org-e-odt-hfy-face-to-css'.
782 When this option is turned off exporter does not create such
785 Use the latter option if you do not want the custom styles to be
786 based on your current display settings. It is necessary that the
787 styles.xml already contains needed styles for colorizing to work.
789 This variable is effective only if
790 `org-e-odt-fontify-srcblocks' is turned on."
791 :group
'org-export-e-odt
795 (defcustom org-e-odt-fontify-srcblocks t
796 "Specify whether or not source blocks need to be fontified.
797 Turn this option on if you want to colorize the source code
798 blocks in the exported file. For colorization to work, you need
799 to make available an enhanced version of `htmlfontify' library."
801 :group
'org-export-e-odt
807 (defcustom org-e-odt-table-caption-above t
808 "When non-nil, place caption string at the beginning of the table.
809 Otherwise, place it near the end."
810 :group
'org-export-e-odt
813 (defcustom org-e-odt-table-styles
814 '(("OrgEquation" "OrgEquation"
815 ((use-first-column-styles . t
)
816 (use-last-column-styles . t
)))
817 ("TableWithHeaderRowAndColumn" "Custom"
818 ((use-first-row-styles . t
)
819 (use-first-column-styles . t
)))
820 ("TableWithFirstRowandLastRow" "Custom"
821 ((use-first-row-styles . t
)
822 (use-last-row-styles . t
)))
823 ("GriddedTable" "Custom" nil
))
824 "Specify how Table Styles should be derived from a Table Template.
825 This is a list where each element is of the
826 form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
828 TABLE-STYLE-NAME is the style associated with the table through
829 \"#+ATTR_ODT: :style TABLE-STYLE-NAME\" line.
831 TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
832 TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
833 below) that is included in
834 `org-e-odt-content-template-file'.
836 TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
838 PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
840 TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
841 \"FirstRow\" | \"LastRow\" |
842 \"EvenRow\" | \"OddRow\" |
843 \"EvenColumn\" | \"OddColumn\" | \"\"
844 where \"+\" above denotes string concatenation.
846 TABLE-CELL-OPTIONS is an alist where each element is of the
847 form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
848 TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
849 `use-last-row-styles' |
850 `use-first-column-styles' |
851 `use-last-column-styles' |
852 `use-banding-rows-styles' |
853 `use-banding-columns-styles' |
854 `use-first-row-styles'
855 ON-OR-OFF := `t' | `nil'
857 For example, with the following configuration
859 \(setq org-e-odt-table-styles
860 '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
861 \(\(use-first-row-styles . t\)
862 \(use-first-column-styles . t\)\)\)
863 \(\"TableWithHeaderColumns\" \"Custom\"
864 \(\(use-first-column-styles . t\)\)\)\)\)
866 1. A table associated with \"TableWithHeaderRowsAndColumns\"
867 style will use the following table-cell styles -
868 \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
869 \"CustomTableCell\" and the following paragraph styles
870 \"CustomFirstRowTableParagraph\",
871 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
874 2. A table associated with \"TableWithHeaderColumns\" style will
875 use the following table-cell styles -
876 \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
877 following paragraph styles
878 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
881 Note that TABLE-TEMPLATE-NAME corresponds to the
882 \"<table:table-template>\" elements contained within
883 \"<office:styles>\". The entries (TABLE-STYLE-NAME
884 TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
885 \"table:template-name\" and \"table:use-first-row-styles\" etc
886 attributes of \"<table:table>\" element. Refer ODF-1.2
887 specification for more information. Also consult the
888 implementation filed under `org-e-odt-get-table-cell-styles'.
890 The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
891 formatting of numbered display equations. Do not delete this
892 style from the list."
893 :group
'org-export-e-odt
896 (const :tag
"None" nil
)
897 (repeat :tag
"Table Styles"
898 (list :tag
"Table Style Specification"
899 (string :tag
"Table Style Name")
900 (string :tag
"Table Template Name")
901 (alist :options
(use-first-row-styles
903 use-first-column-styles
904 use-last-column-styles
905 use-banding-rows-styles
906 use-banding-columns-styles
)
908 :value-type
(const :tag
"True" t
))))))
912 (defcustom org-e-odt-use-date-fields nil
913 "Non-nil, if timestamps should be exported as date fields.
915 When nil, export timestamps as plain text.
917 When non-nil, map `org-time-stamp-custom-formats' to a pair of
918 OpenDocument date-styles with names \"OrgDate1\" and \"OrgDate2\"
919 respectively. A timestamp with no time component is formatted
920 with style \"OrgDate1\" while one with explicit hour and minutes
921 is formatted with style \"OrgDate2\".
923 This feature is experimental. Most (but not all) of the common
924 %-specifiers in `format-time-string' are supported.
925 Specifically, locale-dependent specifiers like \"%c\", \"%x\" are
926 formatted as canonical Org timestamps. For finer control, avoid
929 Textutal specifiers like \"%b\", \"%h\", \"%B\", \"%a\", \"%A\"
930 etc., are displayed by the application in the default language
931 and country specified in `org-e-odt-styles-file'. Note that the
932 default styles file uses language \"en\" and country \"GB\". You
933 can localize the week day and month strings in the exported
934 document by setting the default language and country either using
935 the application UI or through a custom styles file.
937 See `org-e-odt--build-date-styles' for implementation details."
938 :group
'org-export-e-odt
943 ;;; Internal functions
947 (defun org-e-odt--format-timestamp (timestamp &optional end iso-date-p
)
948 (let* ((format-timestamp
949 (lambda (timestamp format
&optional end utc
)
951 (org-export-format-timestamp timestamp format end utc
)
952 (format-time-string format nil utc
))))
953 (has-time-p (or (not timestamp
)
954 (org-export-timestamp-has-time-p timestamp
)))
955 (iso-date (let ((format (if has-time-p
"%Y-%m-%dT%H:%M:%S"
956 "%Y-%m-%dT%H:%M:%S")))
957 (funcall format-timestamp timestamp format end
))))
958 (if iso-date-p iso-date
959 (let* ((style (if has-time-p
"OrgDate2" "OrgDate1"))
960 ;; LibreOffice does not care about end goes as content
961 ;; within the "<text:date>...</text:date>" field. The
962 ;; displayed date is automagically corrected to match the
963 ;; format requested by "style:data-style-name" attribute. So
964 ;; don't bother about formatting the date contents to be
965 ;; compatible with "OrgDate1" and "OrgDateTime" styles. A
966 ;; simple Org-style date should suffice.
967 (date (let* ((formats
968 (if org-display-custom-times
970 (car org-time-stamp-custom-formats
) 1 -
1)
972 (cdr org-time-stamp-custom-formats
) 1 -
1))
973 '("%Y-%m-%d %a" .
"%Y-%m-%d %a %H:%M")))
974 (format (if has-time-p
(cdr formats
) (car formats
))))
975 (funcall format-timestamp timestamp format end
)))
976 (repeater (let ((repeater-type (org-element-property
977 :repeater-type timestamp
))
978 (repeater-value (org-element-property
979 :repeater-value timestamp
))
980 (repeater-unit (org-element-property
981 :repeater-unit timestamp
)))
984 (catchup "++") (restart ".+") (cumulate "+"))
986 (number-to-string repeater-value
))
988 (hour "h") (day "d") (week "w") (month "m")
991 (format "<text:date text:date-value=\"%s\" style:data-style-name=\"%s\" text:fixed=\"true\">%s</text:date>"
993 (and (not (string= repeater
"")) " ")
998 (defun org-e-odt--frame (text width height style
&optional extra
999 anchor-type
&rest title-and-desc
)
1002 (if width
(format " svg:width=\"%0.2fcm\"" width
) "")
1003 (if height
(format " svg:height=\"%0.2fcm\"" height
) "")
1005 (format " text:anchor-type=\"%s\"" (or anchor-type
"paragraph")))))
1007 "\n<draw:frame draw:style-name=\"%s\"%s>\n%s\n</draw:frame>"
1010 (let ((title (car title-and-desc
))
1011 (desc (cadr title-and-desc
)))
1013 (format "<svg:title>%s</svg:title>"
1014 (org-e-odt--encode-plain-text title t
)))
1016 (format "<svg:desc>%s</svg:desc>"
1017 (org-e-odt--encode-plain-text desc t
)))))))))
1020 ;;;; Library wrappers
1022 (defun org-e-odt--zip-extract (archive members target
)
1023 (when (atom members
) (setq members
(list members
)))
1024 (mapc (lambda (member)
1026 (let* ((--quote-file-name
1027 ;; This is shamelessly stolen from `archive-zip-extract'.
1029 (if (or (not (memq system-type
'(windows-nt ms-dos
)))
1030 (and (boundp 'w32-quote-process-args
)
1031 (null w32-quote-process-args
)))
1032 (shell-quote-argument name
)
1034 (target (funcall --quote-file-name target
))
1035 (archive (expand-file-name archive
))
1036 (archive-zip-extract
1037 (list "unzip" "-qq" "-o" "-d" target
))
1038 exit-code command-output
)
1039 (setq command-output
1041 (setq exit-code
(archive-zip-extract archive member
))
1043 (unless (zerop exit-code
)
1044 (message command-output
)
1045 (error "Extraction failed"))))
1048 (defun org-e-odt--suppress-some-translators (info types
)
1049 ;; See comments in `org-e-odt-format-label' and `org-e-odt-toc'.
1052 ;; Override translators.
1054 (nconc (mapcar (lambda (type) (cons type
(lambda (data contents info
)
1056 (plist-get info
:translate-alist
))
1057 ;; Reset data translation cache. FIXME.
1058 ;; :exported-data nil
1064 (defun org-e-odt--target (text id
)
1067 (format "\n<text:bookmark-start text:name=\"OrgXref.%s\"/>" id
)
1068 (format "\n<text:bookmark text:name=\"%s\"/>" id
) text
1069 (format "\n<text:bookmark-end text:name=\"OrgXref.%s\"/>" id
))))
1073 (defun org-e-odt--textbox (text width height style
&optional
1076 (format "\n<draw:text-box %s>%s\n</draw:text-box>"
1077 (concat (format " fo:min-height=\"%0.2fcm\"" (or height
.2))
1079 (format " fo:min-width=\"%0.2fcm\"" (or width
.2))))
1081 width nil style extra anchor-type
))
1085 ;;;; Table of Contents
1087 (defun org-e-odt-begin-toc (index-title depth
)
1090 <text:table-of-content text:style-name=\"OrgIndexSection\" text:protected=\"true\" text:name=\"Table of Contents\">
1091 <text:table-of-content-source text:outline-level=\"%d\">
1092 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
1093 " depth index-title
)
1095 (let ((levels (number-sequence 1 10)))
1100 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
1101 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
1102 <text:index-entry-chapter/>
1103 <text:index-entry-text/>
1104 <text:index-entry-link-end/>
1105 </text:table-of-content-entry-template>
1106 " level level
)) levels
""))
1109 </text:table-of-content-source>
1112 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
1113 <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
1117 (defun org-e-odt-end-toc ()
1120 </text:table-of-content>
1123 (defun* org-e-odt-format-toc-headline
1124 (todo todo-type priority text tags
1125 &key level section-number headline-label
&allow-other-keys
)
1129 (when section-number
(concat section-number
". "))
1132 (let ((style (if (member todo org-done-keywords
)
1133 "OrgDone" "OrgTodo")))
1134 (format "<text:span text:style-name=\"%s\">%s</text:span> "
1137 (let* ((style (format "OrgPriority-%s" priority
))
1138 (priority (format "[#%c]" priority
)))
1139 (format "<text:span text:style-name=\"%s\">%s</text:span> "
1146 (format " <text:span text:style-name=\"%s\">[%s]</text:span>"
1151 "<text:span text:style-name=\"%s\">%s</text:span>"
1152 "OrgTag" tag
)) tags
" : "))))))
1153 (format "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
1154 headline-label text
))
1156 (defun org-e-odt-toc (depth info
)
1157 (assert (wholenump depth
))
1158 ;; When a headline is marked as a radio target, as in the example below:
1160 ;; ** <<<Some Heading>>>
1163 ;; suppress generation of radio targets. i.e., Radio targets are to
1164 ;; be marked as targets within /document body/ and *not* within
1165 ;; /TOC/, as otherwise there will be duplicated anchors one in TOC
1166 ;; and one in the document body.
1168 ;; FIXME-1: Currently exported headings are memoized. `org-export.el'
1169 ;; doesn't provide a way to disable memoization. So this doesn't
1172 ;; FIXME-2: Are there any other objects that need to be suppressed
1174 (setq info
(org-e-odt--suppress-some-translators info
(list 'radio-target
)))
1175 (let* ((title (org-export-translate "Table of Contents" :utf-8 info
))
1176 (headlines (org-export-collect-headlines
1177 info
(and (wholenump depth
) depth
))))
1180 (org-e-odt-begin-toc title depth
)
1183 (let* ((entry (org-e-odt-format-headline--wrap
1184 headline info
'org-e-odt-format-toc-headline
))
1185 (level (org-export-get-relative-level headline info
))
1186 (style (format "Contents_20_%d" level
)))
1187 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1190 (org-e-odt-end-toc)))))
1193 ;;;; Document styles
1195 (defun org-e-odt-add-automatic-style (object-type &optional object-props
)
1196 "Create an automatic style of type OBJECT-TYPE with param OBJECT-PROPS.
1197 OBJECT-PROPS is (typically) a plist created by passing
1198 \"#+ATTR_ODT: \" option of the object in question to
1199 `org-e-odt-parse-block-attributes'.
1201 Use `org-e-odt-object-counters' to generate an automatic
1202 OBJECT-NAME and STYLE-NAME. If OBJECT-PROPS is non-nil, add a
1203 new entry in `org-e-odt-automatic-styles'. Return (OBJECT-NAME
1205 (assert (stringp object-type
))
1206 (let* ((object (intern object-type
))
1208 (seqno (1+ (or (plist-get org-e-odt-object-counters seqvar
) 0)))
1209 (object-name (format "%s%d" object-type seqno
)) style-name
)
1210 (setq org-e-odt-object-counters
1211 (plist-put org-e-odt-object-counters seqvar seqno
))
1213 (setq style-name
(format "Org%s" object-name
))
1214 (setq org-e-odt-automatic-styles
1215 (plist-put org-e-odt-automatic-styles object
1216 (append (list (list style-name object-props
))
1217 (plist-get org-e-odt-automatic-styles object
)))))
1218 (cons object-name style-name
)))
1222 (defun org-e-odt--checkbox (item)
1223 "Return check-box string associated to ITEM."
1224 (let ((checkbox (org-element-property :checkbox item
)))
1225 (if (not checkbox
) ""
1226 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1227 "OrgCode" (case checkbox
1228 (on "[✓] ") ; CHECK MARK
1234 (defun org-e-odt--build-date-styles (fmt style
)
1235 ;; In LibreOffice 3.4.6, there doesn't seem to be a convenient way
1236 ;; to modify the date fields. A date could be modified by
1237 ;; offsetting in days. That's about it. Also, date and time may
1238 ;; have to be emitted as two fields - a date field and a time field
1241 ;; One can add Form Controls to date and time fields so that they
1242 ;; can be easily modified. But then, the exported document will
1243 ;; become tightly coupled with LibreOffice and may not function
1244 ;; properly with other OpenDocument applications.
1246 ;; I have a strange feeling that Date styles are a bit flaky at the
1249 ;; The feature is experimental.
1250 (when (and fmt style
)
1252 '(("%A" .
"<number:day-of-week number:style=\"long\"/>")
1253 ("%B" .
"<number:month number:textual=\"true\" number:style=\"long\"/>")
1254 ("%H" .
"<number:hours number:style=\"long\"/>")
1255 ("%M" .
"<number:minutes number:style=\"long\"/>")
1256 ("%S" .
"<number:seconds number:style=\"long\"/>")
1257 ("%V" .
"<number:week-of-year/>")
1258 ("%Y" .
"<number:year number:style=\"long\"/>")
1259 ("%a" .
"<number:day-of-week number:style=\"short\"/>")
1260 ("%b" .
"<number:month number:textual=\"true\" number:style=\"short\"/>")
1261 ("%d" .
"<number:day number:style=\"long\"/>")
1262 ("%e" .
"<number:day number:style=\"short\"/>")
1263 ("%h" .
"<number:month number:textual=\"true\" number:style=\"short\"/>")
1264 ("%k" .
"<number:hours number:style=\"short\"/>")
1265 ("%m" .
"<number:month number:style=\"long\"/>")
1266 ("%p" .
"<number:am-pm/>")
1267 ("%y" .
"<number:year number:style=\"short\"/>")))
1268 (case-fold-search nil
)
1269 (re (mapconcat 'identity
(mapcar 'car fmt-alist
) "\\|"))
1270 match rpl
(start 0) (filler-beg 0) filler-end filler output
)
1273 (setq fmt
(replace-regexp-in-string (car pair
) (cdr pair
) fmt t t
)))
1274 '(("\\(?:%[[:digit:]]*N\\)" .
"") ; strip ns, us and ns
1275 ("%C" .
"Y") ; replace century with year
1277 ("%G" .
"Y") ; year corresponding to iso week
1278 ("%I" .
"%H") ; hour on a 12-hour clock
1281 ("%U\\|%W" .
"%V") ; week no. starting on Sun./Mon.
1282 ("%Z" .
"") ; time zone name
1283 ("%c" .
"%Y-%M-%d %a %H:%M" ) ; locale's date and time format
1285 ("%X" .
"%x" ) ; locale's pref. time format
1286 ("%j" .
"") ; day of the year
1287 ("%l" .
"%k") ; like %I blank-padded
1288 ("%s" .
"") ; no. of secs since 1970-01-01 00:00:00 +0000
1289 ("%n" .
"<text:line-break/>")
1290 ("%r" .
"%I:%M:%S %p")
1291 ("%t" .
"<text:tab/>")
1292 ("%u\\|%w" .
"") ; numeric day of week - Mon (1-7), Sun(0-6)
1293 ("%x" .
"%Y-%M-%d %a") ; locale's pref. time format
1294 ("%z" .
"") ; time zone in numeric form
1296 (while (string-match re fmt start
)
1297 (setq match
(match-string 0 fmt
))
1298 (setq rpl
(assoc-default match fmt-alist
))
1299 (setq start
(match-end 0))
1300 (setq filler-end
(match-beginning 0))
1301 (setq filler
(substring fmt
(prog1 filler-beg
1302 (setq filler-beg
(match-end 0)))
1304 (setq filler
(and (not (string= filler
""))
1305 (format "<number:text>%s</number:text>"
1306 (org-e-odt--encode-plain-text filler
))))
1307 (setq output
(concat output
"\n" filler
"\n" rpl
)))
1308 (setq filler
(substring fmt filler-beg
))
1309 (unless (string= filler
"")
1310 (setq output
(concat output
1311 (format "\n<number:text>%s</number:text>"
1312 (org-e-odt--encode-plain-text filler
)))))
1313 (format "\n<number:date-style style:name=\"%s\" %s>%s\n</number:date-style>"
1315 (concat " number:automatic-order=\"true\""
1316 " number:format-source=\"fixed\"")
1319 (defun org-e-odt-template (contents info
)
1320 "Return complete document string after HTML conversion.
1321 CONTENTS is the transcoded contents string. RAW-DATA is the
1322 original parsed data. INFO is a plist holding export options."
1324 (let ((title (org-export-data (plist-get info
:title
) info
))
1325 (author (let ((author (plist-get info
:author
)))
1326 (if (not author
) "" (org-export-data author info
))))
1327 (email (plist-get info
:email
))
1328 (keywords (plist-get info
:keywords
))
1329 (description (plist-get info
:description
)))
1332 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
1333 <office:document-meta
1334 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
1335 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
1336 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
1337 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
1338 xmlns:ooo=\"http://openoffice.org/2004/office\"
1339 office:version=\"1.2\">
1341 (format "<dc:creator>%s</dc:creator>\n" author
)
1342 (format "<meta:initial-creator>%s</meta:initial-creator>\n" author
)
1343 ;; Date, if required.
1344 (when (plist-get info
:with-date
)
1345 ;; Check if DATE is specified as an Org-timestamp. If yes,
1346 ;; include it as meta information. Otherwise, just use
1348 (let* ((date (let ((date (plist-get info
:date
)))
1349 (and (not (cdr date
))
1350 (eq (org-element-type (car date
)) 'timestamp
)
1352 (let ((iso-date (org-e-odt--format-timestamp date nil
'iso-date
)))
1354 (format "<dc:date>%s</dc:date>\n" iso-date
)
1355 (format "<meta:creation-date>%s</meta:creation-date>\n"
1357 (format "<meta:generator>%s</meta:generator>\n"
1358 (let ((creator-info (plist-get info
:with-creator
)))
1359 (if (or (not creator-info
) (eq creator-info
'comment
)) ""
1360 (plist-get info
:creator
))))
1361 (format "<meta:keyword>%s</meta:keyword>\n" keywords
)
1362 (format "<dc:subject>%s</dc:subject>\n" description
)
1363 (format "<dc:title>%s</dc:title>\n" title
)
1365 " </office:meta>\n" "</office:document-meta>")
1366 nil
(concat org-e-odt-zip-dir
"meta.xml"))
1367 ;; Add meta.xml in to manifest.
1368 (org-e-odt-create-manifest-file-entry "text/xml" "meta.xml"))
1370 ;; Update styles file.
1371 ;; Copy styles.xml. Also dump htmlfontify styles, if there is any.
1372 ;; Write styles file.
1373 (let* ((styles-file (plist-get info
:odt-styles-file
))
1374 (styles-file (and styles-file
(read (org-trim styles-file
))))
1375 ;; Non-availability of styles.xml is not a critical
1376 ;; error. For now, throw an error.
1377 (styles-file (or styles-file
1378 org-e-odt-styles-file
1379 (expand-file-name "OrgOdtStyles.xml"
1380 org-e-odt-styles-dir
)
1381 (error "org-e-odt: Missing styles file?"))))
1383 ((listp styles-file
)
1384 (let ((archive (nth 0 styles-file
))
1385 (members (nth 1 styles-file
)))
1386 (org-e-odt--zip-extract archive members org-e-odt-zip-dir
)
1389 (when (org-file-image-p member
)
1390 (let* ((image-type (file-name-extension member
))
1391 (media-type (format "image/%s" image-type
)))
1392 (org-e-odt-create-manifest-file-entry media-type member
))))
1394 ((and (stringp styles-file
) (file-exists-p styles-file
))
1395 (let ((styles-file-type (file-name-extension styles-file
)))
1397 ((string= styles-file-type
"xml")
1398 (copy-file styles-file
(concat org-e-odt-zip-dir
"styles.xml") t
))
1399 ((member styles-file-type
'("odt" "ott"))
1400 (org-e-odt--zip-extract styles-file
"styles.xml" org-e-odt-zip-dir
)))))
1402 (error (format "Invalid specification of styles.xml file: %S"
1403 org-e-odt-styles-file
))))
1405 ;; create a manifest entry for styles.xml
1406 (org-e-odt-create-manifest-file-entry "text/xml" "styles.xml")
1408 ;; FIXME: Who is opening an empty styles.xml before this point?
1409 (with-current-buffer
1410 (find-file-noselect (concat org-e-odt-zip-dir
"styles.xml") t
)
1413 ;; Write custom styles for source blocks
1414 ;; Save STYLES used for colorizing of source blocks.
1415 ;; Update styles.xml with styles that were collected as part of
1416 ;; `org-e-odt-hfy-face-to-css' callbacks.
1417 (let ((styles (mapconcat (lambda (style) (format " %s\n" (cddr style
)))
1418 hfy-user-sheet-assoc
"")))
1420 (goto-char (point-min))
1421 (when (re-search-forward "</office:styles>" nil t
)
1422 (goto-char (match-beginning 0))
1423 (insert "\n<!-- Org Htmlfontify Styles -->\n" styles
"\n"))))
1425 ;; Update styles.xml - take care of outline numbering
1427 ;; Don't make automatic backup of styles.xml file. This setting
1428 ;; prevents the backed-up styles.xml file from being zipped in to
1429 ;; odt file. This is more of a hackish fix. Better alternative
1430 ;; would be to fix the zip command so that the output odt file
1431 ;; includes only the needed files and excludes any auto-generated
1432 ;; extra files like backups and auto-saves etc etc. Note that
1433 ;; currently the zip command zips up the entire temp directory so
1434 ;; that any auto-generated files created under the hood ends up in
1435 ;; the resulting odt file.
1436 (set (make-local-variable 'backup-inhibited
) t
)
1438 ;; Outline numbering is retained only upto LEVEL.
1439 ;; To disable outline numbering pass a LEVEL of 0.
1441 (goto-char (point-min))
1443 "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
1445 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1446 (while (re-search-forward regex nil t
)
1447 (unless (let ((sec-num (plist-get info
:section-numbers
))
1448 (level (string-to-number (match-string 2))))
1449 (if (wholenump sec-num
) (<= level sec-num
) sec-num
))
1450 (replace-match replacement t nil
))))
1452 ;; Update content.xml.
1454 (let* ( ;; `org-display-custom-times' should be accessed right
1455 ;; within the context of the Org buffer. So obtain it's
1456 ;; value before moving on to temp-buffer context down below.
1458 (if org-display-custom-times
1459 (cons (substring (car org-time-stamp-custom-formats
) 1 -
1)
1460 (substring (cdr org-time-stamp-custom-formats
) 1 -
1))
1461 '("%Y-%M-%d %a" .
"%Y-%M-%d %a %H:%M"))))
1463 (insert-file-contents
1464 (or org-e-odt-content-template-file
1465 (expand-file-name "OrgOdtContentTemplate.xml"
1466 org-e-odt-styles-dir
)))
1467 ;; Write automatic styles.
1468 ;; - Position the cursor.
1469 (goto-char (point-min))
1470 (re-search-forward " </office:automatic-styles>" nil t
)
1471 (goto-char (match-beginning 0))
1472 ;; - Dump automatic table styles.
1473 (loop for
(style-name props
) in
1474 (plist-get org-e-odt-automatic-styles
'Table
) do
1475 (when (setq props
(or (plist-get props
:rel-width
) 96))
1476 (insert (format org-e-odt-table-style-format style-name props
))))
1477 ;; - Dump date-styles.
1478 (when org-e-odt-use-date-fields
1479 (insert (org-e-odt--build-date-styles (car custom-time-fmts
)
1481 (org-e-odt--build-date-styles (cdr custom-time-fmts
)
1483 ;; Update display level.
1484 ;; - Remove existing sequence decls. Also position the cursor.
1485 (goto-char (point-min))
1486 (when (re-search-forward "<text:sequence-decls" nil t
)
1487 (delete-region (match-beginning 0)
1488 (re-search-forward "</text:sequence-decls>" nil nil
)))
1489 ;; Update sequence decls according to user preference.
1492 "\n<text:sequence-decls>\n%s\n</text:sequence-decls>"
1496 "<text:sequence-decl text:display-outline-level=\"%d\" text:name=\"%s\"/>"
1497 org-e-odt-display-outline-level
(nth 1 x
)))
1498 org-e-odt-category-map-alist
"\n")))
1499 ;; Position the cursor to document body.
1500 (goto-char (point-min))
1501 (re-search-forward "</office:text>" nil nil
)
1502 (goto-char (match-beginning 0))
1504 ;; Preamble - Title, Author, Date etc.
1506 (let* ((title (org-export-data (plist-get info
:title
) info
))
1507 (author (and (plist-get info
:with-author
)
1508 (let ((auth (plist-get info
:author
)))
1509 (and auth
(org-export-data auth info
)))))
1510 (email (plist-get info
:email
))
1511 ;; Switch on or off above vars based on user settings
1512 (author (and (plist-get info
:with-author
) (or author email
)))
1513 (email (and (plist-get info
:with-email
) email
)))
1518 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1519 "OrgTitle" (format "\n<text:title>%s</text:title>" title
))
1521 "\n<text:p text:style-name=\"OrgTitle\"/>"))
1523 ((and author
(not email
))
1526 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1528 (format "<text:initial-creator>%s</text:initial-creator>" author
))
1530 "\n<text:p text:style-name=\"OrgSubtitle\"/>"))
1532 ;; Author and E-mail.
1535 "\n<text:p text:style-name=\"%s\">%s</text:p>"
1538 "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
1539 (concat "mailto:" email
)
1540 (format "<text:initial-creator>%s</text:initial-creator>" author
)))
1542 "\n<text:p text:style-name=\"OrgSubtitle\"/>")))
1543 ;; Date, if required.
1544 (when (plist-get info
:with-date
)
1545 (let* ((date (plist-get info
:date
))
1546 ;; Check if DATE is specified as a timestamp.
1547 (timestamp (and (not (cdr date
))
1548 (eq (org-element-type (car date
)) 'timestamp
)
1551 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1553 (if (and org-e-odt-use-date-fields timestamp
)
1554 (org-e-odt--format-timestamp (car date
))
1555 (org-export-data (plist-get info
:date
) info
)))
1557 "<text:p text:style-name=\"OrgSubtitle\"/>"))))))
1558 ;; Table of Contents
1559 (let* ((with-toc (plist-get info
:with-toc
))
1560 (depth (and with-toc
(if (wholenump with-toc
)
1562 (plist-get info
:headline-levels
)))))
1563 (when depth
(insert (or (org-e-odt-toc depth info
) ""))))
1567 (buffer-substring-no-properties (point-min) (point-max)))))
1571 ;;; Transcode Functions
1575 (defun org-e-odt-bold (bold contents info
)
1576 "Transcode BOLD from Org to ODT.
1577 CONTENTS is the text with bold markup. INFO is a plist holding
1578 contextual information."
1579 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1585 (defun org-e-odt-center-block (center-block contents info
)
1586 "Transcode a CENTER-BLOCK element from Org to ODT.
1587 CONTENTS holds the contents of the center block. INFO is a plist
1588 holding contextual information."
1594 (defun org-e-odt-clock (clock contents info
)
1595 "Transcode a CLOCK element from Org to ODT.
1596 CONTENTS is nil. INFO is a plist used as a communication
1598 (let ((timestamp (org-element-property :value clock
))
1599 (duration (org-element-property :duration clock
)))
1600 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1601 (if (eq (org-element-type (org-export-get-next-element clock info
))
1602 'clock
) "OrgClock" "OrgClockLastLine")
1604 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1605 "OrgClockKeyword" org-clock-string
)
1606 (org-e-odt-timestamp timestamp contents info
)
1607 (and duration
(format " (%s)" duration
))))))
1612 (defun org-e-odt-code (code contents info
)
1613 "Transcode a CODE object from Org to ODT.
1614 CONTENTS is nil. INFO is a plist used as a communication
1616 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1617 "OrgCode" (org-element-property :value code
)))
1622 ;; Comments are ignored.
1627 ;; Comment Blocks are ignored.
1632 (defun org-e-odt-drawer (drawer contents info
)
1633 "Transcode a DRAWER element from Org to ODT.
1634 CONTENTS holds the contents of the block. INFO is a plist
1635 holding contextual information."
1636 (let* ((name (org-element-property :drawer-name drawer
))
1637 (output (if (functionp org-e-odt-format-drawer-function
)
1638 (funcall org-e-odt-format-drawer-function
1640 ;; If there's no user defined function: simply
1641 ;; display contents of the drawer.
1648 (defun org-e-odt-dynamic-block (dynamic-block contents info
)
1649 "Transcode a DYNAMIC-BLOCK element from Org to ODT.
1650 CONTENTS holds the contents of the block. INFO is a plist
1651 holding contextual information. See `org-export-data'."
1657 (defun org-e-odt-entity (entity contents info
)
1658 "Transcode an ENTITY object from Org to ODT.
1659 CONTENTS are the definition itself. INFO is a plist holding
1660 contextual information."
1661 (org-element-property :utf-8 entity
))
1666 (defun org-e-odt-example-block (example-block contents info
)
1667 "Transcode a EXAMPLE-BLOCK element from Org to ODT.
1668 CONTENTS is nil. INFO is a plist holding contextual information."
1669 (org-e-odt-format-code example-block info
))
1674 (defun org-e-odt-export-snippet (export-snippet contents info
)
1675 "Transcode a EXPORT-SNIPPET object from Org to ODT.
1676 CONTENTS is nil. INFO is a plist holding contextual information."
1677 (when (eq (org-export-snippet-backend export-snippet
) 'e-odt
)
1678 (org-element-property :value export-snippet
)))
1683 (defun org-e-odt-export-block (export-block contents info
)
1684 "Transcode a EXPORT-BLOCK element from Org to ODT.
1685 CONTENTS is nil. INFO is a plist holding contextual information."
1686 (when (string= (org-element-property :type export-block
) "ODT")
1687 (org-remove-indentation (org-element-property :value export-block
))))
1692 (defun org-e-odt-fixed-width (fixed-width contents info
)
1693 "Transcode a FIXED-WIDTH element from Org to ODT.
1694 CONTENTS is nil. INFO is a plist holding contextual information."
1695 (org-e-odt-do-format-code (org-element-property :value fixed-width
)))
1698 ;;;; Footnote Definition
1700 ;; Footnote Definitions are ignored.
1703 ;;;; Footnote Reference
1705 (defun org-e-odt-footnote-reference (footnote-reference contents info
)
1706 "Transcode a FOOTNOTE-REFERENCE element from Org to ODT.
1707 CONTENTS is nil. INFO is a plist holding contextual information."
1708 (let ((--format-footnote-definition
1711 (setq n
(format "%d" n
))
1712 (let ((id (concat "fn" n
))
1713 (note-class "footnote")
1714 (par-style "Footnote"))
1716 "<text:note text:id=\"%s\" text:note-class=\"%s\">%s</text:note>"
1719 (format "<text:note-citation>%s</text:note-citation>" n
)
1720 (format "<text:note-body>%s</text:note-body>" def
)))))))
1721 (--format-footnote-reference
1724 (setq n
(format "%d" n
))
1725 (let ((note-class "footnote")
1727 (ref-name (concat "fn" n
)))
1729 "<text:span text:style-name=\"%s\">%s</text:span>"
1731 (format "<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:note-ref>"
1732 note-class ref-format ref-name n
)))))))
1734 ;; Insert separator between two footnotes in a row.
1735 (let ((prev (org-export-get-previous-element footnote-reference info
)))
1736 (and (eq (org-element-type prev
) 'footnote-reference
)
1737 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1738 "OrgSuperscript" ",")))
1739 ;; Trancode footnote reference.
1740 (let ((n (org-export-get-footnote-number footnote-reference info
)))
1742 ((not (org-export-footnote-first-reference-p footnote-reference info
))
1743 (funcall --format-footnote-reference n
))
1744 ;; Inline definitions are secondary strings.
1745 ;; Non-inline footnotes definitions are full Org data.
1747 (let* ((raw (org-export-get-footnote-definition footnote-reference
1749 (def (let ((def (org-trim (org-export-data raw info
))))
1750 (if (eq (org-element-type raw
) 'org-data
) def
1751 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1753 (funcall --format-footnote-definition n def
))))))))
1758 (defun* org-e-odt-format-headline
1759 (todo todo-type priority text tags
1760 &key level section-number headline-label
&allow-other-keys
)
1764 (let ((style (if (member todo org-done-keywords
) "OrgDone" "OrgTodo")))
1765 (format "<text:span text:style-name=\"%s\">%s</text:span> "
1768 (let* ((style (format "OrgPriority-%s" priority
))
1769 (priority (format "[#%c]" priority
)))
1770 (format "<text:span text:style-name=\"%s\">%s</text:span> "
1778 (format "<text:span text:style-name=\"%s\">[%s]</text:span>"
1779 "OrgTags" (mapconcat
1782 "<text:span text:style-name=\"%s\">%s</text:span>"
1783 "OrgTag" tag
)) tags
" : "))))))
1785 (defun org-e-odt-format-headline--wrap (headline info
1786 &optional format-function
1788 "Transcode an HEADLINE element from Org to ODT.
1789 CONTENTS holds the contents of the headline. INFO is a plist
1790 holding contextual information."
1791 (let* ((level (+ (org-export-get-relative-level headline info
)))
1792 (headline-number (org-export-get-headline-number headline info
))
1793 (section-number (and (org-export-numbered-headline-p headline info
)
1794 (mapconcat 'number-to-string
1795 headline-number
".")))
1796 (todo (and (plist-get info
:with-todo-keywords
)
1797 (let ((todo (org-element-property :todo-keyword headline
)))
1798 (and todo
(org-export-data todo info
)))))
1799 (todo-type (and todo
(org-element-property :todo-type headline
)))
1800 (priority (and (plist-get info
:with-priority
)
1801 (org-element-property :priority headline
)))
1802 (text (org-export-data (org-element-property :title headline
) info
))
1803 (tags (and (plist-get info
:with-tags
)
1804 (org-export-get-tags headline info
)))
1805 (headline-label (concat "sec-" (mapconcat 'number-to-string
1806 headline-number
"-")))
1807 (format-function (cond
1808 ((functionp format-function
) format-function
)
1809 ((functionp org-e-odt-format-headline-function
)
1811 (lambda (todo todo-type priority text tags
1813 (funcall org-e-odt-format-headline-function
1814 todo todo-type priority text tags
))))
1815 (t 'org-e-odt-format-headline
))))
1816 (apply format-function
1817 todo todo-type priority text tags
1818 :headline-label headline-label
:level level
1819 :section-number section-number extra-keys
)))
1821 (defun org-e-odt-headline (headline contents info
)
1822 "Transcode an HEADLINE element from Org to ODT.
1823 CONTENTS holds the contents of the headline. INFO is a plist
1824 holding contextual information."
1825 ;; Case 1: This is a footnote section: ignore it.
1826 (unless (org-element-property :footnote-section-p headline
)
1827 (let* ((text (org-export-data (org-element-property :title headline
) info
))
1828 ;; Create the headline text.
1829 (full-text (org-e-odt-format-headline--wrap headline info
))
1830 ;; Get level relative to current parsed data.
1831 (level (org-export-get-relative-level headline info
))
1832 ;; Get canonical label for the headline.
1833 (id (concat "sec-" (mapconcat 'number-to-string
1834 (org-export-get-headline-number
1835 headline info
) "-")))
1836 ;; Get user-specified labels for the headline.
1837 (extra-ids (list (org-element-property :custom-id headline
)
1838 (org-element-property :id headline
)))
1841 (mapconcat (lambda (x)
1843 (let ((x (if (org-uuidgen-p x
) (concat "ID-" x
) x
)))
1845 "" (org-export-solidify-link-text x
)))))
1848 (anchored-title (org-e-odt--target full-text id
)))
1850 ;; Case 2. This is a deep sub-tree: export it as a list item.
1851 ;; Also export as items headlines for which no section
1852 ;; format has been found.
1853 ((org-export-low-level-p headline info
)
1854 ;; Build the real contents of the sub-tree.
1856 (and (org-export-first-sibling-p headline info
)
1857 (format "\n<text:list text:style-name=\"%s\" %s>"
1858 ;; Choose style based on list type.
1859 (if (org-export-numbered-headline-p headline info
)
1860 "OrgNumberedList" "OrgBulletedList")
1861 ;; If top-level list, re-start numbering. Otherwise,
1862 ;; continue numbering.
1863 (format "text:continue-numbering=\"%s\""
1864 (let* ((parent (org-export-get-parent-headline
1867 (org-export-low-level-p parent info
))
1869 (let ((headline-has-table-p
1870 (let ((section (assq 'section
(org-element-contents headline
))))
1871 (assq 'table
(and section
(org-element-contents section
))))))
1872 (format "\n<text:list-item>\n%s\n%s"
1874 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1876 (concat extra-targets anchored-title
))
1878 (if headline-has-table-p
1879 "</text:list-header>"
1880 "</text:list-item>")))
1881 (and (org-export-last-sibling-p headline info
)
1883 ;; Case 3. Standard headline. Export it as a section.
1887 "\n<text:h text:style-name=\"%s\" text:outline-level=\"%s\">%s</text:h>"
1888 (format "Heading_20_%s" level
)
1890 (concat extra-targets anchored-title
))
1894 ;;;; Horizontal Rule
1896 (defun org-e-odt-horizontal-rule (horizontal-rule contents info
)
1897 "Transcode an HORIZONTAL-RULE object from Org to ODT.
1898 CONTENTS is nil. INFO is a plist holding contextual information."
1899 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1900 "Horizontal_20_Line" ""))
1903 ;;;; Inline Babel Call
1905 ;; Inline Babel Calls are ignored.
1908 ;;;; Inline Src Block
1910 (defun org-e-odt--find-verb-separator (s)
1911 "Return a character not used in string S.
1912 This is used to choose a separator for constructs like \\verb."
1913 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1914 (loop for c across ll
1915 when
(not (string-match (regexp-quote (char-to-string c
)) s
))
1916 return
(char-to-string c
))))
1918 (defun org-e-odt-inline-src-block (inline-src-block contents info
)
1919 "Transcode an INLINE-SRC-BLOCK element from Org to ODT.
1920 CONTENTS holds the contents of the item. INFO is a plist holding
1921 contextual information."
1922 (let* ((org-lang (org-element-property :language inline-src-block
))
1923 (code (org-element-property :value inline-src-block
))
1924 (separator (org-e-odt--find-verb-separator code
)))
1930 (defun org-e-odt-inlinetask (inlinetask contents info
)
1931 "Transcode an INLINETASK element from Org to ODT.
1932 CONTENTS holds the contents of the block. INFO is a plist
1933 holding contextual information."
1935 ;; If `org-e-odt-format-inlinetask-function' is provided, call it
1936 ;; with appropriate arguments.
1937 ((functionp org-e-odt-format-inlinetask-function
)
1938 (let ((format-function
1940 (lambda (todo todo-type priority text tags
1941 &key contents
&allow-other-keys
)
1942 (funcall org-e-odt-format-inlinetask-function
1943 todo todo-type priority text tags contents
)))))
1944 (org-e-odt-format-headline--wrap
1945 inlinetask info format-function
:contents contents
)))
1946 ;; Otherwise, use a default template.
1948 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1952 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1953 "OrgInlineTaskHeading"
1954 (org-e-odt-format-headline--wrap
1957 nil nil
"OrgInlineTaskFrame" " style:rel-width=\"100%\"")))))
1961 (defun org-e-odt-italic (italic contents info
)
1962 "Transcode ITALIC from Org to ODT.
1963 CONTENTS is the text with italic markup. INFO is a plist holding
1964 contextual information."
1965 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1966 "Emphasis" contents
))
1971 (defun org-e-odt-item (item contents info
)
1972 "Transcode an ITEM element from Org to ODT.
1973 CONTENTS holds the contents of the item. INFO is a plist holding
1974 contextual information."
1975 (let* ((plain-list (org-export-get-parent item
))
1976 (type (org-element-property :type plain-list
))
1977 (counter (org-element-property :counter item
))
1978 (tag (let ((tag (org-element-property :tag item
)))
1980 (concat (org-e-odt--checkbox item
)
1981 (org-export-data tag info
))))))
1983 ((ordered unordered descriptive-1 descriptive-2
)
1984 (format "\n<text:list-item>\n%s\n%s"
1986 (let* ((--element-has-a-table-p
1988 (lambda (element info
)
1989 (loop for el in
(org-element-contents element
)
1990 thereis
(eq (org-element-type el
) 'table
))))))
1992 ((funcall --element-has-a-table-p item info
)
1993 "</text:list-header>")
1994 (t "</text:list-item>")))))
1995 (t (error "Unknown list type: %S" type
)))))
1999 (defun org-e-odt-keyword (keyword contents info
)
2000 "Transcode a KEYWORD element from Org to ODT.
2001 CONTENTS is nil. INFO is a plist holding contextual information."
2002 (let ((key (org-element-property :key keyword
))
2003 (value (org-element-property :value keyword
)))
2005 ((string= key
"ODT") value
)
2006 ((string= key
"INDEX")
2009 ((string= key
"TARGET") nil
)
2010 ((string= key
"toc")
2011 (let ((value (downcase value
)))
2013 ((string-match "\\<headlines\\>" value
)
2014 (let ((depth (or (and (string-match "[0-9]+" value
)
2015 (string-to-number (match-string 0 value
)))
2016 (plist-get info
:with-toc
))))
2017 (when (wholenump depth
) (org-e-odt-toc depth info
))))
2018 ((member value
'("tables" "figures" "listings"))
2023 ;;;; Latex Environment
2026 (eval-after-load 'org-odt
2027 '(ad-deactivate 'org-format-latex-as-mathml
))
2029 ;; (defadvice org-format-latex-as-mathml ; FIXME
2030 ;; (after org-e-odt-protect-latex-fragment activate)
2031 ;; "Encode LaTeX fragment as XML.
2032 ;; Do this when translation to MathML fails."
2033 ;; (when (or (not (> (length ad-return-value) 0))
2034 ;; (get-text-property 0 'org-protected ad-return-value))
2035 ;; (setq ad-return-value
2036 ;; (org-propertize (org-e-odt--encode-plain-text (ad-get-arg 0))
2037 ;; 'org-protected t))))
2039 (defun org-e-odt-latex-environment (latex-environment contents info
)
2040 "Transcode a LATEX-ENVIRONMENT element from Org to ODT.
2041 CONTENTS is nil. INFO is a plist holding contextual information."
2042 (let* ((latex-frag (org-remove-indentation
2043 (org-element-property :value latex-environment
))))
2044 (org-e-odt-do-format-code latex-frag
)))
2049 ;; (when latex-frag ; FIXME
2050 ;; (setq href (org-propertize href :title "LaTeX Fragment"
2051 ;; :description latex-frag)))
2053 ;; provide descriptions
2055 (defun org-e-odt-latex-fragment (latex-fragment contents info
)
2056 "Transcode a LATEX-FRAGMENT object from Org to ODT.
2057 CONTENTS is nil. INFO is a plist holding contextual information."
2058 (let* ((latex-frag (org-element-property :value latex-fragment
))
2059 (processing-type (plist-get info
:LaTeX-fragments
)))
2060 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2061 "OrgCode" (org-e-odt--encode-plain-text latex-frag t
))))
2066 (defun org-e-odt-line-break (line-break contents info
)
2067 "Transcode a LINE-BREAK object from Org to ODT.
2068 CONTENTS is nil. INFO is a plist holding contextual information."
2069 "<text:line-break/>\n")
2074 ;;;; Links :: Label references
2076 (defun org-e-odt--enumerate (element info
&optional predicate n
)
2077 (when predicate
(assert (funcall predicate element info
)))
2078 (let* ((--numbered-parent-headline-at-<=-n
2080 (lambda (element n info
)
2081 (loop for x in
(org-export-get-genealogy element
)
2082 thereis
(and (eq (org-element-type x
) 'headline
)
2083 (<= (org-export-get-relative-level x info
) n
)
2084 (org-export-numbered-headline-p x info
)
2088 (lambda (element scope info
&optional predicate
)
2091 (or scope
(plist-get info
:parse-tree
))
2092 (org-element-type element
)
2094 (and (or (not predicate
) (funcall predicate el info
))
2098 info
'first-match
)))))
2099 (scope (funcall --numbered-parent-headline-at-
<=-n
2100 element
(or n org-e-odt-display-outline-level
) info
))
2101 (ordinal (funcall --enumerate element scope info predicate
))
2106 (mapconcat 'number-to-string
2107 (org-export-get-headline-number scope info
) "."))
2111 (number-to-string ordinal
))))
2114 (defun org-e-odt-format-label (element info op
)
2115 (assert (memq (org-element-type element
) '(link table src-block paragraph
)))
2116 (let* ((caption-from
2117 (case (org-element-type element
)
2118 (link (org-export-get-parent-element element
))
2120 ;; Get label and caption.
2121 (label (org-element-property :name caption-from
))
2122 (caption (org-export-get-caption caption-from
))
2123 (short-caption (org-export-get-caption caption-from t
))
2124 ;; Transcode captions.
2125 (caption (and caption
(org-export-data caption info
)))
2126 ;; Currently short caption are sneaked in as object names.
2128 ;; The advantages are:
2130 ;; - Table Of Contents: Currently, there is no support for
2131 ;; building TOC for figures, listings and tables. See
2132 ;; `org-e-odt-keyword'. User instead has to rely on
2133 ;; external application for building such indices. Within
2134 ;; LibreOffice, building an "Illustration Index" or "Index
2135 ;; of Tables" will create a table with long captions (only)
2136 ;; and building a table with "Object names" will create a
2137 ;; table with short captions.
2139 ;; - Easy navigation: In LibreOffice, object names are
2140 ;; offered via the navigation bar. This way one can
2141 ;; quickly locate and jump to object of his choice in the
2142 ;; exported document.
2144 ;; The main disadvantage is that there cannot be any markups
2145 ;; within object names i.e., one cannot embolden, italicize
2146 ;; or underline text within short caption. So suppress
2147 ;; generation of <text:span >...</text:span> and other
2148 ;; markups by overriding the default translators. We
2149 ;; probably shouldn't be suppressing translators for all
2150 ;; elements in `org-element-all-objects', but for now this
2153 (let ((short-caption (or short-caption caption
)))
2155 (org-export-data short-caption
2156 (org-e-odt--suppress-some-translators
2157 info org-element-all-objects
))))))
2158 (when (or label caption
)
2159 (let* ((default-category
2160 (case (org-element-type element
)
2162 (src-block "__Listing__")
2165 ((org-e-odt--enumerable-latex-image-p element info
)
2167 ((org-e-odt--enumerable-image-p element info
)
2169 ((org-e-odt--enumerable-formula-p element info
)
2171 (t (error "Don't know how to format label for link: %S"
2173 (t (error "Don't know how to format label for element type: %s"
2174 (org-element-type element
)))))
2176 (assert default-category
)
2177 (destructuring-bind (counter label-style category predicate
)
2178 (assoc-default default-category org-e-odt-category-map-alist
)
2179 ;; Compute sequence number of the element.
2180 (setq seqno
(org-e-odt--enumerate element info predicate
))
2181 ;; Localize category string.
2182 (setq category
(org-export-translate category
:utf-8 info
))
2184 ;; Case 1: Handle Label definition.
2186 ;; Assign an internal label, if user has not provided one
2187 (setq label
(or label
(format "%s-%s" default-category seqno
)))
2188 (setq label
(org-export-solidify-link-text label
))
2191 (cadr (assoc-string label-style org-e-odt-label-styles t
))
2194 "<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">%s</text:sequence>"
2195 label counter counter seqno
))
2196 (?c .
,(or caption
""))))
2198 ;; Case 2: Handle Label reference.
2201 (setq label
(org-export-solidify-link-text label
))
2202 (let* ((fmt (cddr (assoc-string label-style org-e-odt-label-styles t
)))
2205 (format "<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:sequence-ref>"
2206 fmt1 label
(format-spec fmt2
`((?e .
,category
)
2208 (t (error "Unknow %S on label" op
))))))))
2211 ;;;; Links :: Inline Images
2213 (defun org-e-odt--copy-image-file (path)
2214 "Returns the internal name of the file"
2215 (let* ((image-type (file-name-extension path
))
2216 (media-type (format "image/%s" image-type
))
2217 (target-dir "Images/")
2219 (format "%s%04d.%s" target-dir
2220 (incf org-e-odt-embedded-images-count
) image-type
)))
2221 (message "Embedding %s as %s ..."
2222 (substring-no-properties path
) target-file
)
2224 (when (= 1 org-e-odt-embedded-images-count
)
2225 (make-directory (concat org-e-odt-zip-dir target-dir
))
2226 (org-e-odt-create-manifest-file-entry "" target-dir
))
2228 (copy-file path
(concat org-e-odt-zip-dir target-file
) 'overwrite
)
2229 (org-e-odt-create-manifest-file-entry media-type target-file
)
2232 (defun org-e-odt--image-size (file &optional user-width
2233 user-height scale dpi embed-as
)
2234 (let* ((--pixels-to-cms
2235 (function (lambda (pixels dpi
)
2236 (let ((cms-per-inch 2.54)
2237 (inches (/ pixels dpi
)))
2238 (* cms-per-inch inches
)))))
2241 (lambda (size-in-pixels dpi
)
2243 (cons (funcall --pixels-to-cms
(car size-in-pixels
) dpi
)
2244 (funcall --pixels-to-cms
(cdr size-in-pixels
) dpi
))))))
2245 (dpi (or dpi org-e-odt-pixels-per-inch
))
2246 (anchor-type (or embed-as
"paragraph"))
2247 (user-width (and (not scale
) user-width
))
2248 (user-height (and (not scale
) user-height
))
2251 (not (and user-height user-width
))
2254 (and (executable-find "identify")
2255 (let ((size-in-pixels
2256 (let ((dim (shell-command-to-string
2257 (format "identify -format \"%%w:%%h\" \"%s\""
2259 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim
)
2260 (cons (string-to-number (match-string 1 dim
))
2261 (string-to-number (match-string 2 dim
)))))))
2262 (funcall --size-in-cms size-in-pixels dpi
)))
2264 (let ((size-in-pixels
2265 (ignore-errors ; Emacs could be in batch mode
2267 (image-size (create-image file
) 'pixels
))))
2268 (funcall --size-in-cms size-in-pixels dpi
))
2269 ;; Use hard-coded values.
2270 (cdr (assoc-string anchor-type
2271 org-e-odt-default-image-sizes-alist
))
2273 (error "Cannot determine image size, aborting"))))
2274 (width (car size
)) (height (cdr size
)))
2277 (setq width
(* width scale
) height
(* height scale
)))
2278 ((and user-height user-width
)
2279 (setq width user-width height user-height
))
2281 (setq width
(* user-height
(/ width height
)) height user-height
))
2283 (setq height
(* user-width
(/ height width
)) width user-width
))
2285 ;; ensure that an embedded image fits comfortably within a page
2286 (let ((max-width (car org-e-odt-max-image-size
))
2287 (max-height (cdr org-e-odt-max-image-size
)))
2288 (when (or (> width max-width
) (> height max-height
))
2289 (let* ((scale1 (/ max-width width
))
2290 (scale2 (/ max-height height
))
2291 (scale (min scale1 scale2
)))
2292 (setq width
(* scale width
) height
(* scale height
)))))
2293 (cons width height
)))
2295 (defun org-e-odt-link--inline-image (element info
)
2296 "Return HTML code for an inline image.
2297 LINK is the link pointing to the inline image. INFO is a plist
2298 used as a communication channel."
2299 (assert (eq (org-element-type element
) 'link
))
2300 (let* ((src (let* ((type (org-element-property :type element
))
2301 (raw-path (org-element-property :path element
)))
2302 (cond ((member type
'("http" "https"))
2303 (concat type
":" raw-path
))
2304 ((file-name-absolute-p raw-path
)
2305 (expand-file-name raw-path
))
2307 (src-expanded (if (file-name-absolute-p src
) src
2308 (expand-file-name src
(file-name-directory
2309 (plist-get info
:input-file
)))))
2311 "\n<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>"
2312 (org-e-odt--copy-image-file src-expanded
)))
2313 ;; Extract attributes from #+ATTR_ODT line.
2314 (attr-from (case (org-element-type element
)
2315 (link (org-export-get-parent-element element
))
2317 ;; Convert attributes to a plist.
2318 (attr-plist (org-export-read-attribute :attr_odt attr-from
))
2319 ;; Handle `:anchor', `:style' and `:attributes' properties.
2321 (car (assoc-string (plist-get attr-plist
:anchor
)
2322 '(("as-char") ("paragraph") ("page")) t
)))
2324 (and user-frame-anchor
(plist-get attr-plist
:style
)))
2326 (and user-frame-anchor
(plist-get attr-plist
:attributes
)))
2328 (list user-frame-style user-frame-attrs user-frame-anchor
))
2329 ;; (embed-as (or embed-as user-frame-anchor "paragraph"))
2331 ;; handle `:width', `:height' and `:scale' properties.
2332 (size (org-e-odt--image-size
2333 src-expanded
(plist-get attr-plist
:width
)
2334 (plist-get attr-plist
:height
)
2335 (plist-get attr-plist
:scale
) nil
;; embed-as
2338 (width (car size
)) (height (cdr size
))
2339 (standalone-link-p (org-e-odt--standalone-link-p element info
))
2340 (embed-as (if standalone-link-p
"paragraph" "as-char"))
2341 (captions (org-e-odt-format-label element info
'definition
))
2342 (caption (car captions
)) (short-caption (cdr captions
))
2343 (entity (concat (and caption
"Captioned") embed-as
"Image"))
2344 ;; Check if this link was created by LaTeX-to-PNG converter.
2345 (replaces (org-element-property
2346 :replaces
(if (not standalone-link-p
) element
2347 (org-export-get-parent-element element
))))
2348 ;; If yes, note down the type of the element - LaTeX Fragment
2349 ;; or LaTeX environment. It will go in to frame title.
2350 (title (and replaces
(capitalize
2351 (symbol-name (org-element-type replaces
)))))
2353 ;; If yes, note down it's contents. It will go in to frame
2354 ;; description. This quite useful for debugging.
2355 (desc (and replaces
(org-element-property :value replaces
))))
2356 (org-e-odt--render-image/formula entity href width height
2357 captions user-frame-params title desc
)))
2360 ;;;; Links :: Math formula
2362 (defun org-e-odt-link--inline-formula (element info
)
2363 (let* ((src (let* ((type (org-element-property :type element
))
2364 (raw-path (org-element-property :path element
)))
2366 ((file-name-absolute-p raw-path
)
2367 (expand-file-name raw-path
))
2369 (src-expanded (if (file-name-absolute-p src
) src
2370 (expand-file-name src
(file-name-directory
2371 (plist-get info
:input-file
)))))
2374 "\n<draw:object %s xlink:href=\"%s\" xlink:type=\"simple\"/>"
2375 " xlink:show=\"embed\" xlink:actuate=\"onLoad\""
2376 (file-name-directory (org-e-odt--copy-formula-file src-expanded
))))
2377 (standalone-link-p (org-e-odt--standalone-link-p element info
))
2378 (embed-as (if standalone-link-p
'paragraph
'character
))
2379 (captions (org-e-odt-format-label element info
'definition
))
2380 (caption (car captions
)) (short-caption (cdr captions
))
2381 ;; Check if this link was created by LaTeX-to-MathML
2383 (replaces (org-element-property
2384 :replaces
(if (not standalone-link-p
) element
2385 (org-export-get-parent-element element
))))
2386 ;; If yes, note down the type of the element - LaTeX Fragment
2387 ;; or LaTeX environment. It will go in to frame title.
2388 (title (and replaces
(capitalize
2389 (symbol-name (org-element-type replaces
)))))
2391 ;; If yes, note down it's contents. It will go in to frame
2392 ;; description. This quite useful for debugging.
2393 (desc (and replaces
(org-element-property :value replaces
)))
2396 ((eq embed-as
'character
)
2397 (org-e-odt--render-image/formula
"InlineFormula" href width height
2398 nil nil title desc
))
2400 (let* ((equation (org-e-odt--render-image/formula
2401 "CaptionedDisplayFormula" href width height
2402 captions nil title desc
))
2404 (let* ((org-e-odt-category-map-alist
2405 '(("__MathFormula__" "Text" "math-label" "Equation"
2406 org-e-odt--enumerable-formula-p
))))
2407 (car (org-e-odt-format-label element info
'definition
)))))
2408 (concat equation
"<text:tab/>" label
))))))
2410 (defun org-e-odt--copy-formula-file (src-file)
2411 "Returns the internal name of the file"
2412 (let* ((target-dir (format "Formula-%04d/"
2413 (incf org-e-odt-embedded-formulas-count
)))
2414 (target-file (concat target-dir
"content.xml")))
2415 ;; Create a directory for holding formula file. Also enter it in
2417 (make-directory (concat org-e-odt-zip-dir target-dir
))
2418 (org-e-odt-create-manifest-file-entry
2419 "application/vnd.oasis.opendocument.formula" target-dir
"1.2")
2420 ;; Copy over the formula file from user directory to zip
2422 (message "Embedding %s as %s ..." src-file target-file
)
2423 (let ((case-fold-search nil
))
2426 ((string-match "\\.\\(mathml\\|mml\\)\\'" src-file
)
2427 (copy-file src-file
(concat org-e-odt-zip-dir target-file
) 'overwrite
))
2428 ;; Case 2: OpenDocument formula.
2429 ((string-match "\\.odf\\'" src-file
)
2430 (org-e-odt--zip-extract src-file
"content.xml"
2431 (concat org-e-odt-zip-dir target-dir
)))
2432 (t (error "%s is not a formula file" src-file
))))
2433 ;; Enter the formula file in to manifest.
2434 (org-e-odt-create-manifest-file-entry "text/xml" target-file
)
2439 (defun org-e-odt--render-image/formula
(cfg-key href width height
&optional
2440 captions user-frame-params
2441 &rest title-and-desc
)
2442 (let* ((frame-cfg-alist
2443 ;; Each element of this alist is of the form (CFG-HANDLE
2444 ;; INNER-FRAME-PARAMS OUTER-FRAME-PARAMS).
2446 ;; CFG-HANDLE is the key to the alist.
2448 ;; INNER-FRAME-PARAMS and OUTER-FRAME-PARAMS specify the
2449 ;; frame params for INNER-FRAME and OUTER-FRAME
2450 ;; respectively. See below.
2452 ;; Configurations that are meant to be applied to
2453 ;; non-captioned image/formula specifies no
2454 ;; OUTER-FRAME-PARAMS.
2458 ;; INNER-FRAME :: Frame that directly surrounds an
2461 ;; OUTER-FRAME :: Frame that encloses the INNER-FRAME. This
2462 ;; frame also contains the caption, if any.
2464 ;; FRAME-PARAMS :: List of the form (FRAME-STYLE-NAME
2465 ;; FRAME-ATTRIBUTES FRAME-ANCHOR). Note
2466 ;; that these are the last three arguments
2467 ;; to `org-e-odt--frame'.
2469 ;; Note that an un-captioned image/formula requires just an
2470 ;; INNER-FRAME, while a captioned image/formula requires
2471 ;; both an INNER and an OUTER-FRAME.
2472 '(("As-CharImage" ("OrgInlineImage" nil
"as-char"))
2473 ("ParagraphImage" ("OrgDisplayImage" nil
"paragraph"))
2474 ("PageImage" ("OrgPageImage" nil
"page"))
2475 ("CaptionedAs-CharImage"
2476 ("OrgCaptionedImage"
2477 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
2478 ("OrgInlineImage" nil
"as-char"))
2479 ("CaptionedParagraphImage"
2480 ("OrgCaptionedImage"
2481 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
2482 ("OrgImageCaptionFrame" nil
"paragraph"))
2483 ("CaptionedPageImage"
2484 ("OrgCaptionedImage"
2485 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
2486 ("OrgPageImageCaptionFrame" nil
"page"))
2487 ("InlineFormula" ("OrgInlineFormula" nil
"as-char"))
2488 ("DisplayFormula" ("OrgDisplayFormula" nil
"as-char"))
2489 ("CaptionedDisplayFormula"
2490 ("OrgCaptionedFormula" nil
"paragraph")
2491 ("OrgFormulaCaptionFrame" nil
"paragraph"))))
2492 (caption (car captions
)) (short-caption (cdr captions
))
2493 ;; Retrieve inner and outer frame params, from configuration.
2494 (frame-cfg (assoc-string cfg-key frame-cfg-alist t
))
2495 (inner (nth 1 frame-cfg
))
2496 (outer (nth 2 frame-cfg
))
2497 ;; User-specified frame params (from #+ATTR_ODT spec)
2498 (user user-frame-params
)
2499 (--merge-frame-params (function
2500 (lambda (default user
)
2501 "Merge default and user frame params."
2502 (if (not user
) default
2503 (assert (= (length default
) 3))
2504 (assert (= (length user
) 3))
2507 collect
(or u d
)))))))
2509 ;; Case 1: Image/Formula has no caption.
2510 ;; There is only one frame, one that surrounds the image
2513 ;; Merge user frame params with that from configuration.
2514 (setq inner
(funcall --merge-frame-params inner user
))
2515 (apply 'org-e-odt--frame href width height
2516 (append inner title-and-desc
)))
2517 ;; Case 2: Image/Formula is captioned or labeled.
2518 ;; There are two frames: The inner one surrounds the
2519 ;; image or formula. The outer one contains the
2520 ;; caption/sequence number.
2522 ;; Merge user frame params with outer frame params.
2523 (setq outer
(funcall --merge-frame-params outer user
))
2524 ;; Short caption, if specified, goes as part of inner frame.
2525 (setq inner
(let ((frame-params (copy-sequence inner
)))
2526 (setcar (cdr frame-params
)
2530 (format " draw:name=\"%s\" " short-caption
))))
2532 (apply 'org-e-odt--textbox
2533 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2536 (apply 'org-e-odt--frame href width height
2537 (append inner title-and-desc
))
2539 width height outer
)))))
2541 (defun org-e-odt--enumerable-p (element info
)
2542 ;; Element should have a caption or label.
2543 (or (org-element-property :caption element
)
2544 (org-element-property :name element
)))
2546 (defun org-e-odt--enumerable-image-p (element info
)
2547 (org-e-odt--standalone-link-p
2549 ;; Paragraph should have a caption or label. It SHOULD NOT be a
2550 ;; replacement element. (i.e., It SHOULD NOT be a result of LaTeX
2553 (and (not (org-element-property :replaces p
))
2554 (or (org-element-property :caption p
)
2555 (org-element-property :name p
))))
2556 ;; Link should point to an image file.
2558 (assert (eq (org-element-type l
) 'link
))
2559 (org-export-inline-image-p l org-e-odt-inline-image-rules
))))
2561 (defun org-e-odt--enumerable-latex-image-p (element info
)
2562 (org-e-odt--standalone-link-p
2564 ;; Paragraph should have a caption or label. It SHOULD also be a
2565 ;; replacement element. (i.e., It SHOULD be a result of LaTeX
2568 (and (org-element-property :replaces p
)
2569 (or (org-element-property :caption p
)
2570 (org-element-property :name p
))))
2571 ;; Link should point to an image file.
2573 (assert (eq (org-element-type l
) 'link
))
2574 (org-export-inline-image-p l org-e-odt-inline-image-rules
))))
2576 (defun org-e-odt--enumerable-formula-p (element info
)
2577 (org-e-odt--standalone-link-p
2579 ;; Paragraph should have a caption or label.
2581 (or (org-element-property :caption p
)
2582 (org-element-property :name p
)))
2583 ;; Link should point to a MathML or ODF file.
2585 (assert (eq (org-element-type l
) 'link
))
2586 (org-export-inline-image-p l org-e-odt-inline-formula-rules
))))
2588 (defun org-e-odt--standalone-link-p (element info
&optional
2591 "Test if ELEMENT is a standalone link for the purpose ODT export.
2592 INFO is a plist holding contextual information.
2594 Return non-nil, if ELEMENT is of type paragraph satisfying
2595 PARAGRAPH-PREDICATE and it's sole content, save for whitespaces,
2596 is a link that satisfies LINK-PREDICATE.
2598 Return non-nil, if ELEMENT is of type link satisfying
2599 LINK-PREDICATE and it's containing paragraph satisfies
2600 PARAGRAPH-PREDICATE inaddtion to having no other content save for
2601 leading and trailing whitespaces.
2603 Return nil, otherwise."
2604 (let ((p (case (org-element-type element
)
2606 (link (and (or (not link-predicate
)
2607 (funcall link-predicate element
))
2608 (org-export-get-parent element
)))
2611 (assert (eq (org-element-type p
) 'paragraph
))
2612 (when (or (not paragraph-predicate
)
2613 (funcall paragraph-predicate p
))
2614 (let ((contents (org-element-contents p
)))
2615 (loop for x in contents
2616 with inline-image-count
= 0
2617 always
(case (org-element-type x
)
2619 (not (org-string-nw-p x
)))
2621 (and (or (not link-predicate
)
2622 (funcall link-predicate x
))
2623 (= (incf inline-image-count
) 1)))
2626 (defun org-e-odt-resolve-numbered-paragraph (element info
)
2627 (when (eq (org-element-type element
) 'item
)
2628 (let ((el element
) ordinal
)
2629 (while (eq (org-element-type el
) 'item
)
2630 (push (1+ (length (org-export-get-previous-element el info t
))) ordinal
)
2631 (setq el
(org-export-get-parent (org-export-get-parent el
))))
2634 (defun org-e-odt-link (link desc info
)
2635 "Transcode a LINK object from Org to ODT.
2637 DESC is the description part of the link, or the empty string.
2638 INFO is a plist holding contextual information. See
2640 (let* ((type (org-element-property :type link
))
2641 (raw-path (org-element-property :path link
))
2642 ;; Ensure DESC really exists, or set it to nil.
2643 (desc (and (not (string= desc
"")) desc
))
2644 (imagep (org-export-inline-image-p
2645 link org-e-odt-inline-image-rules
))
2647 ((member type
'("http" "https" "ftp" "mailto"))
2648 (concat type
":" raw-path
))
2649 ((string= type
"file")
2650 (if (file-name-absolute-p raw-path
)
2651 (concat "file://" (expand-file-name raw-path
))
2652 (concat "file://" raw-path
)))
2657 ((and (not desc
) (org-export-inline-image-p
2658 link org-e-odt-inline-image-rules
))
2659 (org-e-odt-link--inline-image link info
))
2661 ((and (not desc
) (org-export-inline-image-p
2662 link org-e-odt-inline-formula-rules
))
2663 (org-e-odt-link--inline-formula link info
))
2664 ;; Radio target: Transcode target's contents and use them as
2665 ;; link's description.
2666 ((string= type
"radio")
2667 (let ((destination (org-export-resolve-radio-link link info
)))
2669 (let ((desc (org-export-data (org-element-contents destination
) info
))
2670 (href (org-export-solidify-link-text path
)))
2672 "<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
2674 ;; Links pointing to an headline: Find destination and build
2675 ;; appropriate referencing command.
2676 ((member type
'("custom-id" "fuzzy" "id"))
2677 (let ((destination (if (string= type
"fuzzy")
2678 (org-export-resolve-fuzzy-link link info
)
2679 (org-export-resolve-id-link link info
))))
2680 (case (org-element-type destination
)
2681 ;; Fuzzy link points nowhere.
2683 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2684 "Emphasis" (or desc
(org-export-data
2685 (org-element-property
2686 :raw-link link
) info
))))
2687 ;; Fuzzy link points to an invisible target.
2689 ;; LINK points to an headline. Check if LINK should display
2692 (let* ((headline-no (org-export-get-headline-number destination info
))
2693 (label (format "sec-%s" (mapconcat 'number-to-string
2696 ;; Case 1: Headline is numbered and LINK has no
2697 ;; description or LINK's description matches headline's
2698 ;; title. Display section number.
2699 ((and (org-export-numbered-headline-p destination info
)
2700 (or (not desc
) (string= desc
(org-element-property
2701 :raw-value destination
))))
2703 "<text:bookmark-ref text:reference-format=\"chapter\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
2704 label
(mapconcat 'number-to-string headline-no
".")))
2705 ;; Case 2: Either the headline is un-numbered or
2706 ;; LINK has a custom description. Display LINK's
2707 ;; description or headline's title.
2709 (let ((desc (or desc
(org-export-data
2710 (org-element-property :title destination
)
2713 "<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
2715 ;; Fuzzy link points to a target. Do as above.
2717 ;; Identify nearest meaningful container
2719 (loop for parent in
(org-export-get-genealogy destination
)
2722 (org-element-type parent
)
2723 '(footnote-definition footnote-reference headline item
2726 ;; There is a meaningful container
2728 (case (org-element-type container
)
2729 ;; Container is item
2732 "<text:bookmark-ref text:reference-format=\"number-all-superior\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
2733 (org-export-solidify-link-text path
)
2735 (mapconcat 'number-to-string
2736 (org-e-odt-resolve-numbered-paragraph
2737 container info
) ".")))))))
2740 ;; (setq number (cond
2741 ;; ((org-e-odt--standalone-link-p destination info)
2742 ;; (org-export-get-ordinal
2743 ;; (assoc 'link (org-element-contents destination))
2744 ;; info 'link 'org-e-odt--standalone-link-p))
2745 ;; (t (org-export-get-ordinal destination info))))
2746 ;; (setq desc (when number
2747 ;; (if (atom number) (number-to-string number)
2748 ;; (mapconcat 'number-to-string number ".")))))
2750 (let ((label-reference
2751 (org-e-odt-format-label destination info
'reference
)))
2752 (assert label-reference
)
2753 label-reference
)))))
2754 ;; Coderef: replace link with the reference name or the
2755 ;; equivalent line number.
2756 ((string= type
"coderef")
2757 (let* ((line-no (format "%d" (org-export-resolve-coderef path info
)))
2758 (href (concat "coderef-" path
)))
2760 (org-export-get-coderef-format path desc
)
2762 "<text:bookmark-ref text:reference-format=\"number\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
2764 ;; Link type is handled by a special function.
2765 ((functionp (setq protocol
(nth 2 (assoc type org-link-protocols
))))
2766 (funcall protocol
(org-link-unescape path
) desc
'odt
))
2767 ;; External link with a description part.
2769 (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
2771 ;; External link without a description part.
2773 (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
2775 ;; No path, only description. Try to do something useful.
2776 (t (format "<text:span text:style-name=\"%s\">%s</text:span>"
2777 "Emphasis" desc
)))))
2782 (defun org-e-odt-paragraph (paragraph contents info
)
2783 "Transcode a PARAGRAPH element from Org to ODT.
2784 CONTENTS is the contents of the paragraph, as a string. INFO is
2785 the plist used as a communication channel."
2786 (let* ((parent (org-export-get-parent paragraph
))
2787 (parent-type (org-element-type parent
))
2788 (style (case parent-type
2789 (quote-block "Quotations")
2790 (center-block "OrgCenter")
2791 (footnote-definition "Footnote")
2792 (t (or (org-element-property :style paragraph
)
2794 ;; If this paragraph is a leading paragraph in an item and the
2795 ;; item has a checkbox, splice the checkbox and paragraph contents
2797 (when (and (eq (org-element-type parent
) 'item
)
2798 (eq paragraph
(car (org-element-contents parent
))))
2799 (setq contents
(concat (org-e-odt--checkbox parent
) contents
)))
2801 (format "\n<text:p text:style-name=\"%s\">%s</text:p>" style contents
)))
2806 (defun org-e-odt-plain-list (plain-list contents info
)
2807 "Transcode a PLAIN-LIST element from Org to ODT.
2808 CONTENTS is the contents of the list. INFO is a plist holding
2809 contextual information."
2810 (format "\n<text:list text:style-name=\"%s\" %s>\n%s</text:list>"
2811 ;; Choose style based on list type.
2812 (case (org-element-property :type plain-list
)
2813 (ordered "OrgNumberedList")
2814 (unordered "OrgBulletedList")
2815 (descriptive-1 "OrgDescriptionList")
2816 (descriptive-2 "OrgDescriptionList"))
2817 ;; If top-level list, re-start numbering. Otherwise,
2818 ;; continue numbering.
2819 (format "text:continue-numbering=\"%s\""
2820 (let* ((parent (org-export-get-parent plain-list
)))
2821 (if (and parent
(eq (org-element-type parent
) 'item
))
2827 (defun org-e-odt--encode-tabs-and-spaces (line)
2828 (replace-regexp-in-string
2829 "\\([\t]\\|\\([ ]+\\)\\)"
2832 ((string= s
"\t") "<text:tab/>")
2833 (t (let ((n (length s
)))
2836 ((> n
1) (concat " " (format "<text:s text:c=\"%d\"/>" (1- n
))))
2840 (defun org-e-odt--encode-plain-text (text &optional no-whitespace-filling
)
2843 (setq text
(replace-regexp-in-string (car pair
) (cdr pair
) text t t
)))
2844 '(("&" .
"&") ("<" .
"<") (">" .
">")))
2845 (if no-whitespace-filling text
2846 (org-e-odt--encode-tabs-and-spaces text
)))
2848 (defun org-e-odt-plain-text (text info
)
2849 "Transcode a TEXT string from Org to ODT.
2850 TEXT is the string to transcode. INFO is a plist holding
2851 contextual information."
2852 (let ((output text
))
2853 ;; Protect &, < and >.
2854 (setq output
(org-e-odt--encode-plain-text output t
))
2855 ;; Handle smart quotes. Be sure to provide original string since
2856 ;; OUTPUT may have been modified.
2857 (setq output
(org-export-activate-smart-quotes output
:utf-8 info text
))
2858 ;; Convert special strings.
2859 (when (plist-get info
:with-special-strings
)
2863 (replace-regexp-in-string (car pair
) (cdr pair
) output t nil
)))
2864 org-e-odt-special-string-regexps
))
2865 ;; Handle break preservation if required.
2866 (when (plist-get info
:preserve-breaks
)
2867 (setq output
(replace-regexp-in-string
2868 "\\(\\\\\\\\\\)?[ \t]*\n" "<text:line-break/>\n" output t
)))
2875 (defun org-e-odt-planning (planning contents info
)
2876 "Transcode a PLANNING element from Org to ODT.
2877 CONTENTS is nil. INFO is a plist used as a communication
2879 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2882 (let ((closed (org-element-property :closed planning
)))
2885 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2886 "OrgClosedKeyword" org-closed-string
)
2887 (org-e-odt-timestamp closed contents info
))))
2888 (let ((deadline (org-element-property :deadline planning
)))
2891 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2892 "OrgDeadlineKeyword" org-deadline-string
)
2893 (org-e-odt-timestamp deadline contents info
))))
2894 (let ((scheduled (org-element-property :scheduled planning
)))
2897 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2898 "OrgScheduledKeyword" org-deadline-string
)
2899 (org-e-odt-timestamp scheduled contents info
)))))))
2902 ;;;; Property Drawer
2904 (defun org-e-odt-property-drawer (property-drawer contents info
)
2905 "Transcode a PROPERTY-DRAWER element from Org to ODT.
2906 CONTENTS is nil. INFO is a plist holding contextual
2908 ;; The property drawer isn't exported but we want separating blank
2909 ;; lines nonetheless.
2915 (defun org-e-odt-quote-block (quote-block contents info
)
2916 "Transcode a QUOTE-BLOCK element from Org to ODT.
2917 CONTENTS holds the contents of the block. INFO is a plist
2918 holding contextual information."
2924 (defun org-e-odt-quote-section (quote-section contents info
)
2925 "Transcode a QUOTE-SECTION element from Org to ODT.
2926 CONTENTS is nil. INFO is a plist holding contextual information."
2927 (let ((value (org-remove-indentation
2928 (org-element-property :value quote-section
))))
2929 (when value
(org-e-odt-do-format-code value
))))
2934 (defun org-e-odt-format-section (text style
&optional name
)
2935 (let ((default-name (car (org-e-odt-add-automatic-style "Section"))))
2936 (format "\n<text:section text:style-name=\"%s\" %s>\n%s\n</text:section>"
2938 (format "text:name=\"%s\"" (or name default-name
))
2942 (defun org-e-odt-section (section contents info
) ; FIXME
2943 "Transcode a SECTION element from Org to ODT.
2944 CONTENTS holds the contents of the section. INFO is a plist
2945 holding contextual information."
2950 (defun org-e-odt-radio-target (radio-target text info
)
2951 "Transcode a RADIO-TARGET object from Org to ODT.
2952 TEXT is the text of the target. INFO is a plist holding
2953 contextual information."
2955 text
(org-export-solidify-link-text
2956 (org-element-property :value radio-target
))))
2961 (defun org-e-odt-special-block (special-block contents info
)
2962 "Transcode a SPECIAL-BLOCK element from Org to ODT.
2963 CONTENTS holds the contents of the block. INFO is a plist
2964 holding contextual information."
2965 (let ((type (downcase (org-element-property :type special-block
)))
2966 (attributes (org-export-read-attribute :attr_odt special-block
)))
2969 ((string= type
"annotation")
2970 (let* ((author (or (plist-get attributes
:author
)
2971 (let ((author (plist-get info
:author
)))
2972 (and author
(org-export-data author info
)))))
2973 (date (or (plist-get attributes
:date
)
2974 ;; FIXME: Is `car' right thing to do below?
2975 (car (plist-get info
:date
)))))
2977 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2979 (format "<office:annotation>\n%s\n</office:annotation>"
2982 (format "<dc:creator>%s</dc:creator>" author
))
2984 (format "<dc:date>%s</dc:date>"
2985 (org-e-odt--format-timestamp date nil
'iso-date
)))
2988 ((string= type
"textbox")
2989 (let ((width (plist-get attributes
:width
))
2990 (height (plist-get attributes
:height
))
2991 (style (plist-get attributes
:style
))
2992 (extra (plist-get attributes
:extra
))
2993 (anchor (plist-get attributes
:anchor
)))
2994 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2995 "Text_20_body" (org-e-odt--textbox contents width height
2996 style extra anchor
))))
3002 (defun org-e-odt-hfy-face-to-css (fn)
3003 "Create custom style for face FN.
3004 When FN is the default face, use it's foreground and background
3005 properties to create \"OrgSrcBlock\" paragraph style. Otherwise
3006 use it's color attribute to create a character style whose name
3007 is obtained from FN. Currently all attributes of FN other than
3010 The style name for a face FN is derived using the following
3011 operations on the face name in that order - de-dash, CamelCase
3012 and prefix with \"OrgSrc\". For example,
3013 `font-lock-function-name-face' is associated with
3014 \"OrgSrcFontLockFunctionNameFace\"."
3015 (let* ((css-list (hfy-face-to-style fn
))
3016 (style-name ((lambda (fn)
3019 'capitalize
(split-string
3020 (hfy-face-or-def-to-name fn
) "-")
3022 (color-val (cdr (assoc "color" css-list
)))
3023 (background-color-val (cdr (assoc "background" css-list
)))
3024 (style (and org-e-odt-create-custom-styles-for-srcblocks
3027 (format org-e-odt-src-block-paragraph-format
3028 background-color-val color-val
))
3032 <style:style style:name=\"%s\" style:family=\"text\">
3033 <style:text-properties fo:color=\"%s\"/>
3034 </style:style>" style-name color-val
))))))
3035 (cons style-name style
)))
3037 (defun org-e-odt-htmlfontify-string (line)
3038 (let* ((hfy-html-quote-regex "\\([<\"&> ]\\)")
3039 (hfy-html-quote-map '(("\"" """)
3044 (" " "<text:tab/>")))
3045 (hfy-face-to-css 'org-e-odt-hfy-face-to-css
)
3046 (hfy-optimisations-1 (copy-sequence hfy-optimisations
))
3047 (hfy-optimisations (add-to-list 'hfy-optimisations-1
3049 (hfy-begin-span-handler
3050 (lambda (style text-block text-id text-begins-block-p
)
3051 (insert (format "<text:span text:style-name=\"%s\">" style
))))
3052 (hfy-end-span-handler (lambda nil
(insert "</text:span>"))))
3053 (with-no-warnings (htmlfontify-string line
))))
3055 (defun org-e-odt-do-format-code
3056 (code &optional lang refs retain-labels num-start
)
3057 (let* ((lang (or (assoc-default lang org-src-lang-modes
) lang
))
3058 (lang-mode (and lang
(intern (format "%s-mode" lang
))))
3059 (code-lines (org-split-string code
"\n"))
3060 (code-length (length code-lines
))
3061 (use-htmlfontify-p (and (functionp lang-mode
)
3062 org-e-odt-fontify-srcblocks
3063 (require 'htmlfontify nil t
)
3064 (fboundp 'htmlfontify-string
)))
3065 (code (if (not use-htmlfontify-p
) code
3069 (font-lock-fontify-buffer)
3071 (fontifier (if use-htmlfontify-p
'org-e-odt-htmlfontify-string
3072 'org-e-odt--encode-plain-text
))
3073 (par-style (if use-htmlfontify-p
"OrgSrcBlock"
3074 "OrgFixedWidthBlock"))
3076 (assert (= code-length
(length (org-split-string code
"\n"))))
3078 (org-export-format-code
3080 (lambda (loc line-num ref
)
3082 (concat par-style
(and (= (incf i
) code-length
) "LastLine")))
3084 (setq loc
(concat loc
(and ref retain-labels
(format " (%s)" ref
))))
3085 (setq loc
(funcall fontifier loc
))
3087 (setq loc
(org-e-odt--target loc
(concat "coderef-" ref
))))
3089 (setq loc
(format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3091 (if (not line-num
) loc
3092 (format "\n<text:list-item>%s\n</text:list-item>" loc
)))
3095 ((not num-start
) code
)
3098 "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
3099 " text:continue-numbering=\"false\"" code
))
3102 "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
3103 " text:continue-numbering=\"true\"" code
)))))
3105 (defun org-e-odt-format-code (element info
)
3106 (let* ((lang (org-element-property :language element
))
3107 ;; Extract code and references.
3108 (code-info (org-export-unravel-code element
))
3109 (code (car code-info
))
3110 (refs (cdr code-info
))
3111 ;; Does the src block contain labels?
3112 (retain-labels (org-element-property :retain-labels element
))
3113 ;; Does it have line numbers?
3114 (num-start (case (org-element-property :number-lines element
)
3115 (continued (org-export-get-loc element info
))
3117 (org-e-odt-do-format-code code lang refs retain-labels num-start
)))
3119 (defun org-e-odt-src-block (src-block contents info
)
3120 "Transcode a SRC-BLOCK element from Org to ODT.
3121 CONTENTS holds the contents of the item. INFO is a plist holding
3122 contextual information."
3123 (let* ((lang (org-element-property :language src-block
))
3124 (attributes (org-export-read-attribute :attr_odt src-block
))
3125 (captions (org-e-odt-format-label src-block info
'definition
))
3126 (caption (car captions
)) (short-caption (cdr captions
)))
3129 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3131 (let ((--src-block (org-e-odt-format-code src-block info
)))
3132 (if (not (plist-get attributes
:textbox
)) --src-block
3133 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3135 (org-e-odt--textbox --src-block nil nil nil
)))))))
3138 ;;;; Statistics Cookie
3140 (defun org-e-odt-statistics-cookie (statistics-cookie contents info
)
3141 "Transcode a STATISTICS-COOKIE object from Org to ODT.
3142 CONTENTS is nil. INFO is a plist holding contextual information."
3143 (let ((cookie-value (org-element-property :value statistics-cookie
)))
3144 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3145 "OrgCode" cookie-value
)))
3150 (defun org-e-odt-strike-through (strike-through contents info
)
3151 "Transcode STRIKE-THROUGH from Org to ODT.
3152 CONTENTS is the text with strike-through markup. INFO is a plist
3153 holding contextual information."
3154 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3155 "Strikethrough" contents
))
3160 (defun org-e-odt-subscript (subscript contents info
)
3161 "Transcode a SUBSCRIPT object from Org to ODT.
3162 CONTENTS is the contents of the object. INFO is a plist holding
3163 contextual information."
3164 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3165 "OrgSubscript" contents
))
3170 (defun org-e-odt-superscript (superscript contents info
)
3171 "Transcode a SUPERSCRIPT object from Org to ODT.
3172 CONTENTS is the contents of the object. INFO is a plist holding
3173 contextual information."
3174 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3175 "OrgSuperscript" contents
))
3180 (defun org-e-odt-table-style-spec (element info
)
3181 (let* ((table (org-export-get-parent-table element
))
3182 (table-attributes (org-export-read-attribute :attr_odt table
))
3183 (table-style (plist-get table-attributes
:style
)))
3184 (assoc table-style org-e-odt-table-styles
)))
3186 (defun org-e-odt-get-table-cell-styles (table-cell info
)
3187 "Retrieve styles applicable to a table cell.
3188 R and C are (zero-based) row and column numbers of the table
3189 cell. STYLE-SPEC is an entry in `org-e-odt-table-styles'
3190 applicable to the current table. It is `nil' if the table is not
3191 associated with any style attributes.
3193 Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
3195 When STYLE-SPEC is nil, style the table cell the conventional way
3196 - choose cell borders based on row and column groupings and
3197 choose paragraph alignment based on `org-col-cookies' text
3199 `org-e-odt-get-paragraph-style-cookie-for-table-cell'.
3201 When STYLE-SPEC is non-nil, ignore the above cookie and return
3202 styles congruent with the ODF-1.2 specification."
3203 (let* ((table-cell-address (org-export-table-cell-address table-cell info
))
3204 (r (car table-cell-address
)) (c (cdr table-cell-address
))
3205 (style-spec (org-e-odt-table-style-spec table-cell info
))
3206 (table-dimensions (org-export-table-dimensions
3207 (org-export-get-parent-table table-cell
)
3210 ;; LibreOffice - particularly the Writer - honors neither table
3211 ;; templates nor custom table-cell styles. Inorder to retain
3212 ;; inter-operability with LibreOffice, only automatic styles are
3213 ;; used for styling of table-cells. The current implementation is
3214 ;; congruent with ODF-1.2 specification and hence is
3215 ;; future-compatible.
3217 ;; Additional Note: LibreOffice's AutoFormat facility for tables -
3218 ;; which recognizes as many as 16 different cell types - is much
3219 ;; richer. Unfortunately it is NOT amenable to easy configuration
3221 (let* ((template-name (nth 1 style-spec
))
3222 (cell-style-selectors (nth 2 style-spec
))
3225 ((and (cdr (assoc 'use-first-column-styles cell-style-selectors
))
3226 (= c
0)) "FirstColumn")
3227 ((and (cdr (assoc 'use-last-column-styles cell-style-selectors
))
3228 (= (1+ c
) (cdr table-dimensions
)))
3230 ((and (cdr (assoc 'use-first-row-styles cell-style-selectors
))
3231 (= r
0)) "FirstRow")
3232 ((and (cdr (assoc 'use-last-row-styles cell-style-selectors
))
3233 (= (1+ r
) (car table-dimensions
)))
3235 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors
))
3236 (= (% r
2) 1)) "EvenRow")
3237 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors
))
3238 (= (% r
2) 0)) "OddRow")
3239 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors
))
3240 (= (% c
2) 1)) "EvenColumn")
3241 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors
))
3242 (= (% c
2) 0)) "OddColumn")
3244 (concat template-name cell-type
)))))
3246 (defun org-e-odt-table-cell (table-cell contents info
)
3247 "Transcode a TABLE-CELL element from Org to ODT.
3248 CONTENTS is nil. INFO is a plist used as a communication
3250 (let* ((table-cell-address (org-export-table-cell-address table-cell info
))
3251 (r (car table-cell-address
))
3252 (c (cdr table-cell-address
))
3253 (horiz-span (or (org-export-table-cell-width table-cell info
) 0))
3254 (table-row (org-export-get-parent table-cell
))
3255 (custom-style-prefix (org-e-odt-get-table-cell-styles
3259 (and custom-style-prefix
3260 (format "%sTableParagraph" custom-style-prefix
))
3263 ((and (= 1 (org-export-table-row-group table-row info
))
3264 (org-export-table-has-header-p
3265 (org-export-get-parent-table table-row
) info
))
3267 ((let* ((table (org-export-get-parent-table table-cell
))
3268 (table-attrs (org-export-read-attribute :attr_odt table
))
3269 (table-header-columns (plist-get table-attrs
3271 (<= c
(cond ((wholenump table-header-columns
)
3272 (- table-header-columns
1))
3273 (table-header-columns 0)
3276 (t "OrgTableContents"))
3277 (capitalize (symbol-name (org-export-table-cell-alignment
3278 table-cell info
))))))
3281 (and custom-style-prefix
(format "%sTableCell"
3282 custom-style-prefix
))
3285 (when (or (org-export-table-row-starts-rowgroup-p table-row info
)
3287 (when (org-export-table-row-ends-rowgroup-p table-row info
) "B")
3288 (when (and (org-export-table-cell-starts-colgroup-p table-cell info
)
3289 (not (zerop c
)) ) "L"))))
3292 (format " table:style-name=\"%s\"" cell-style-name
)
3293 (and (> horiz-span
0)
3294 (format " table:number-columns-spanned=\"%d\""
3295 (1+ horiz-span
))))))
3296 (unless contents
(setq contents
""))
3298 (assert paragraph-style
)
3299 (format "\n<table:table-cell%s>\n%s\n</table:table-cell>"
3301 (let ((table-cell-contents (org-element-contents table-cell
)))
3302 (if (memq (org-element-type (car table-cell-contents
))
3303 org-element-all-elements
)
3305 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3306 paragraph-style contents
))))
3308 (dotimes (i horiz-span s
)
3309 (setq s
(concat s
"\n<table:covered-table-cell/>"))))
3315 (defun org-e-odt-table-row (table-row contents info
)
3316 "Transcode a TABLE-ROW element from Org to ODT.
3317 CONTENTS is the contents of the row. INFO is a plist used as a
3318 communication channel."
3319 ;; Rules are ignored since table separators are deduced from
3320 ;; borders of the current row.
3321 (when (eq (org-element-property :type table-row
) 'standard
)
3322 (let* ((rowgroup-tags
3323 (if (and (= 1 (org-export-table-row-group table-row info
))
3324 (org-export-table-has-header-p
3325 (org-export-get-parent-table table-row
) info
))
3326 ;; If the row belongs to the first rowgroup and the
3327 ;; table has more than one row groups, then this row
3328 ;; belongs to the header row group.
3329 '("\n<table:table-header-rows>" .
"\n</table:table-header-rows>")
3330 ;; Otherwise, it belongs to non-header row group.
3331 '("\n<table:table-rows>" .
"\n</table:table-rows>"))))
3333 ;; Does this row begin a rowgroup?
3334 (when (org-export-table-row-starts-rowgroup-p table-row info
)
3335 (car rowgroup-tags
))
3337 (format "\n<table:table-row>\n%s\n</table:table-row>" contents
)
3338 ;; Does this row end a rowgroup?
3339 (when (org-export-table-row-ends-rowgroup-p table-row info
)
3340 (cdr rowgroup-tags
))))))
3345 (defun org-e-odt-table-first-row-data-cells (table info
)
3350 (unless (eq (org-element-property :type row
) 'rule
) row
))
3352 (special-column-p (org-export-table-has-special-column-p table
)))
3353 (if (not special-column-p
) (org-element-contents table-row
)
3354 (cdr (org-element-contents table-row
)))))
3356 (defun org-e-odt--table (table contents info
)
3357 "Transcode a TABLE element from Org to ODT.
3358 CONTENTS is the contents of the table. INFO is a plist holding
3359 contextual information."
3360 (case (org-element-property :type table
)
3361 ;; Case 1: table.el doesn't support export to OD format. Strip
3362 ;; such tables from export.
3367 "(org-e-odt): Found table.el-type table in the source Org file."
3368 " table.el doesn't support export to ODT format."
3369 " Stripping the table from export."))))
3370 ;; Case 2: Native Org tables.
3372 (let* ((captions (org-e-odt-format-label table info
'definition
))
3373 (caption (car captions
)) (short-caption (cdr captions
))
3374 (attributes (org-export-read-attribute :attr_odt table
))
3375 (custom-table-style (nth 1 (org-e-odt-table-style-spec table info
)))
3378 (lambda (table info
)
3379 (let* ((table-style (or custom-table-style
"OrgTable"))
3380 (column-style (format "%sColumn" table-style
)))
3382 (lambda (table-cell)
3383 (let ((width (1+ (or (org-export-table-cell-width
3384 table-cell info
) 0)))
3386 "\n<table:table-column table:style-name=\"%s\"/>"
3389 (dotimes (i width out
) (setq out
(concat s out
)))))
3390 (org-e-odt-table-first-row-data-cells table info
) "\n"))))))
3394 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3397 (let* ((automatic-name
3398 (org-e-odt-add-automatic-style "Table" attributes
)))
3400 "\n<table:table table:style-name=\"%s\"%s>"
3401 (or custom-table-style
(cdr automatic-name
) "OrgTable")
3402 (concat (when short-caption
3403 (format " table:name=\"%s\"" short-caption
)))))
3404 ;; column specification.
3405 (funcall table-column-specs table info
)
3409 "</table:table>")))))
3411 (defun org-e-odt-table (table contents info
)
3412 "Transcode a TABLE element from Org to ODT.
3413 CONTENTS is the contents of the table. INFO is a plist holding
3414 contextual information.
3416 Use `org-e-odt--table' to typeset the table. Handle details
3417 pertaining to indentation here."
3418 (let* ((--element-preceded-by-table-p
3420 (lambda (element info
)
3421 (loop for el in
(org-export-get-previous-element element info t
)
3422 thereis
(eq (org-element-type el
) 'table
)))))
3423 (--walk-list-genealogy-and-collect-tags
3425 (lambda (table info
)
3426 (let* ((genealogy (org-export-get-genealogy table
))
3428 (when (eq (org-element-type (car genealogy
)) 'item
)
3429 (loop for el in genealogy
3430 when
(memq (org-element-type el
)
3435 (loop for el in genealogy
3436 when
(and (eq (org-element-type el
) 'headline
)
3437 (org-export-low-level-p el info
))
3441 (org-element-contents
3442 (org-export-get-parent el
)))))))
3445 ;; Handle list genealogy.
3446 (loop for el in list-genealogy collect
3447 (case (org-element-type el
)
3449 (setq parent-list el
)
3450 (cons "</text:list>"
3451 (format "\n<text:list text:style-name=\"%s\" %s>"
3452 (case (org-element-property :type el
)
3453 (ordered "OrgNumberedList")
3454 (unordered "OrgBulletedList")
3455 (descriptive-1 "OrgDescriptionList")
3456 (descriptive-2 "OrgDescriptionList"))
3457 "text:continue-numbering=\"true\"")))
3461 (if (funcall --element-preceded-by-table-p table info
)
3462 '("</text:list-header>" .
"<text:list-header>")
3463 '("</text:list-item>" .
"<text:list-header>")))
3464 ((funcall --element-preceded-by-table-p
3466 '("</text:list-header>" .
"<text:list-header>"))
3467 (t '("</text:list-item>" .
"<text:list-item>"))))))
3468 ;; Handle low-level headlines.
3469 (loop for el in llh-genealogy
3470 with step
= 'item collect
3473 (setq step
'item
) ; Flip-flop
3474 (setq parent-list el
)
3475 (cons "</text:list>"
3476 (format "\n<text:list text:style-name=\"%s\" %s>"
3477 (if (org-export-numbered-headline-p
3481 "text:continue-numbering=\"true\"")))
3483 (setq step
'plain-list
) ; Flip-flop
3486 (if (funcall --element-preceded-by-table-p table info
)
3487 '("</text:list-header>" .
"<text:list-header>")
3488 '("</text:list-item>" .
"<text:list-header>")))
3489 ((let ((section?
(org-export-get-previous-element
3492 (eq (org-element-type section?
) 'section
)
3493 (assq 'table
(org-element-contents section?
))))
3494 '("</text:list-header>" .
"<text:list-header>"))
3496 '("</text:list-item>" .
"<text:list-item>")))))))))))
3497 (close-open-tags (funcall --walk-list-genealogy-and-collect-tags
3499 ;; OpenDocument schema does not permit table to occur within a
3502 ;; One solution - the easiest and lightweight, in terms of
3503 ;; implementation - is to put the table in an indented text box
3504 ;; and make the text box part of the list-item. Unfortunately if
3505 ;; the table is big and spans multiple pages, the text box could
3506 ;; overflow. In this case, the following attribute will come
3509 ;; ,---- From OpenDocument-v1.1.pdf
3510 ;; | 15.27.28 Overflow behavior
3512 ;; | For text boxes contained within text document, the
3513 ;; | style:overflow-behavior property specifies the behavior of text
3514 ;; | boxes where the containing text does not fit into the text
3517 ;; | If the attribute's value is clip, the text that does not fit
3518 ;; | into the text box is not displayed.
3520 ;; | If the attribute value is auto-create-new-frame, a new frame
3521 ;; | will be created on the next page, with the same position and
3522 ;; | dimensions of the original frame.
3524 ;; | If the style:overflow-behavior property's value is
3525 ;; | auto-create-new-frame and the text box has a minimum width or
3526 ;; | height specified, then the text box will grow until the page
3527 ;; | bounds are reached before a new frame is created.
3530 ;; Unfortunately, LibreOffice-3.4.6 doesn't honor
3531 ;; auto-create-new-frame property and always resorts to clipping
3532 ;; the text box. This results in table being truncated.
3534 ;; So we solve the problem the hard (and fun) way using list
3537 ;; The problem only becomes more interesting if you take in to
3538 ;; account the following facts:
3540 ;; - Description lists are simulated as plain lists.
3541 ;; - Low-level headlines can be listified.
3542 ;; - In Org-mode, a table can occur not only as a regular list
3543 ;; item, but also within description lists and low-level
3546 ;; See `org-e-odt-translate-description-lists' and
3547 ;; `org-e-odt-translate-low-level-headlines' for how this is
3551 ;; Discontinue the list.
3552 (mapconcat 'car close-open-tags
"\n")
3553 ;; Put the table in an indented section.
3554 (let* ((table (org-e-odt--table table contents info
))
3555 (level (/ (length (mapcar 'car close-open-tags
)) 2))
3556 (style (format "OrgIndentedSection-Level-%d" level
)))
3557 (when table
(org-e-odt-format-section table style
)))
3558 ;; Continue the list.
3559 (mapconcat 'cdr
(nreverse close-open-tags
) "\n"))))
3564 (defun org-e-odt-target (target contents info
)
3565 "Transcode a TARGET object from Org to ODT.
3566 CONTENTS is nil. INFO is a plist holding contextual
3568 (let ((value (org-element-property :value target
)))
3569 (org-e-odt--target "" (org-export-solidify-link-text value
))))
3574 (defun org-e-odt-timestamp (timestamp contents info
)
3575 "Transcode a TIMESTAMP object from Org to ODT.
3576 CONTENTS is nil. INFO is a plist used as a communication
3578 (let* ((raw-value (org-element-property :raw-value timestamp
))
3579 (type (org-element-property :type timestamp
)))
3580 (if (not org-e-odt-use-date-fields
)
3581 (let ((value (org-e-odt-plain-text
3582 (org-export-translate-timestamp timestamp
) info
)))
3583 (case (org-element-property :type timestamp
)
3584 ((active active-range
)
3585 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3586 "OrgActiveTimestamp" value
))
3587 ((inactive inactive-range
)
3588 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3589 "OrgInactiveTimestamp" value
))
3593 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3594 "OrgActiveTimestamp"
3595 (format "<%s>" (org-e-odt--format-timestamp timestamp
))))
3597 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3598 "OrgInactiveTimestamp"
3599 (format "[%s]" (org-e-odt--format-timestamp timestamp
))))
3601 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3602 "OrgActiveTimestamp"
3603 (format "<%s>–<%s>"
3604 (org-e-odt--format-timestamp timestamp
)
3605 (org-e-odt--format-timestamp timestamp
'end
))))
3607 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3608 "OrgInactiveTimestamp"
3609 (format "[%s]–[%s]"
3610 (org-e-odt--format-timestamp timestamp
)
3611 (org-e-odt--format-timestamp timestamp
'end
))))
3613 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3615 (org-e-odt-plain-text (org-export-translate-timestamp
3616 timestamp
) info
)))))))
3621 (defun org-e-odt-underline (underline contents info
)
3622 "Transcode UNDERLINE from Org to ODT.
3623 CONTENTS is the text with underline markup. INFO is a plist
3624 holding contextual information."
3625 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3626 "Underline" contents
))
3631 (defun org-e-odt-verbatim (verbatim contents info
)
3632 "Transcode a VERBATIM object from Org to ODT.
3633 CONTENTS is nil. INFO is a plist used as a communication
3635 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3636 "OrgCode" (org-element-property :value verbatim
)))
3641 (defun org-e-odt-verse-block (verse-block contents info
)
3642 "Transcode a VERSE-BLOCK element from Org to ODT.
3643 CONTENTS is verse block contents. INFO is a plist holding
3644 contextual information."
3645 ;; Add line breaks to each line of verse.
3646 (setq contents
(replace-regexp-in-string
3647 "\\(<text:line-break/>\\)?[ \t]*\n"
3648 "<text:line-break/>" contents
))
3649 ;; Replace tabs and spaces.
3650 (setq contents
(org-e-odt--encode-tabs-and-spaces contents
))
3651 ;; Surround it in a verse environment.
3652 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3653 "OrgVerse" contents
))
3659 ;;;; LaTeX fragments
3661 (defun org-e-odt--translate-latex-fragments (tree backend info
)
3662 (let ((processing-type (plist-get info
:LaTeX-fragments
))
3664 ;; Normalize processing-type to one of dvipng, mathml or verbatim.
3665 ;; If the desired converter is not available, force verbatim
3667 (case processing-type
3669 (if (and (fboundp 'org-format-latex-mathml-available-p
)
3670 (org-format-latex-mathml-available-p))
3671 (setq processing-type
'mathml
)
3672 (message "LaTeX to MathML converter not available.")
3673 (setq processing-type
'verbatim
)))
3675 (unless (and (org-check-external-command "latex" "" t
)
3676 (org-check-external-command "dvipng" "" t
))
3677 (message "LaTeX to PNG converter not available.")
3678 (setq processing-type
'verbatim
)))
3680 (message "Unknown LaTeX option. Forcing verbatim.")
3681 (setq processing-type
'verbatim
)))
3683 ;; Store normalized value for later use.
3684 (when (plist-get info
:LaTeX-fragments
)
3685 (plist-put info
:LaTeX-fragments processing-type
))
3686 (message "Formatting LaTeX using %s" processing-type
)
3688 ;; Convert `latex-fragment's and `latex-environment's.
3689 (when (memq processing-type
'(mathml dvipng
))
3691 tree
'(latex-fragment latex-environment
)
3694 (let* ((latex-frag (org-element-property :value latex-
*))
3695 (input-file (plist-get info
:input-file
))
3696 (cache-dir (file-name-directory input-file
))
3697 (cache-subdir (concat
3698 (case processing-type
3700 (mathml "ltxmathml/"))
3701 (file-name-sans-extension
3702 (file-name-nondirectory input-file
))))
3704 (case processing-type
3705 (dvipng (format "Creating LaTeX Image %d..." count
))
3706 (mathml (format "Creating MathML snippet %d..." count
))))
3707 ;; Get an Org-style link to PNG image or the MathML
3710 (let ((link (with-temp-buffer
3712 (org-format-latex cache-subdir cache-dir
3714 nil nil processing-type
)
3715 (buffer-substring-no-properties
3716 (point-min) (point-max)))))
3717 (if (not (string-match "file:\\([^]]*\\)" link
))
3718 (prog1 nil
(message "LaTeX Conversion failed."))
3721 ;; Conversion succeeded. Parse above Org-style link to a
3723 (let* ((link (car (org-element-map (with-temp-buffer
3726 (org-element-parse-buffer))
3729 (org-element-put-property link
:parent nil
)
3732 (case (org-element-type latex-
*)
3733 ;; Case 1: LaTeX environment.
3734 ;; Mimic a "standalone image or formula" by
3735 ;; enclosing the `link' in a `paragraph'.
3736 ;; Copy over original attributes, captions to
3737 ;; the enclosing paragraph.
3739 (org-element-adopt-elements
3741 (list :style
"OrgFormula"
3742 :name
(org-element-property :name
3744 :caption
(org-element-property :caption
3747 ;; Case 2: LaTeX fragment.
3748 ;; No special action.
3749 (latex-fragment link
))))
3750 ;; Note down the object that link replaces.
3751 (org-element-put-property replacement
:replaces
3752 (list (org-element-type latex-
*)
3753 (list :value latex-frag
)))
3755 (org-element-set-element latex-
* replacement
))))))
3760 ;;;; Description lists
3762 ;; This translator is necessary to handle indented tables in a uniform
3763 ;; manner. See comment in `org-e-odt--table'.
3765 (defun org-e-odt--translate-description-lists (tree backend info
)
3766 ;; OpenDocument has no notion of a description list. So simulate it
3767 ;; using plain lists. Description lists in the exported document
3768 ;; are typeset in the same manner as they are in a typical HTML
3771 ;; Specifically, a description list like this:
3774 ;; | - term-1 :: definition-1
3775 ;; | - term-2 :: definition-2
3778 ;; gets translated in to the following form:
3787 ;; Further effect is achieved by fixing the OD styles as below:
3789 ;; 1. Set the :type property of the simulated lists to
3790 ;; `descriptive-1' and `descriptive-2'. Map these to list-styles
3791 ;; that has *no* bullets whatsoever.
3793 ;; 2. The paragraph containing the definition term is styled to be
3799 (when (equal (org-element-property :type el
) 'descriptive
)
3800 (org-element-set-element
3802 (apply 'org-element-adopt-elements
3803 (list 'plain-list
(list :type
'descriptive-1
))
3806 (org-element-adopt-elements
3807 (list 'item
(list :checkbox
(org-element-property
3809 (list 'paragraph
(list :style
"Text_20_body_20_bold")
3810 (or (org-element-property :tag item
) "(no term)"))
3811 (org-element-adopt-elements
3812 (list 'plain-list
(list :type
'descriptive-2
))
3813 (apply 'org-element-adopt-elements
3815 (org-element-contents item
)))))
3816 (org-element-contents el
)))))
3823 ;; Lists that are marked with attribute `:list-table' are called as
3824 ;; list tables. They will be rendered as a table within the exported
3827 ;; Consider an example. The following list table
3829 ;; #+attr_odt :list-table t
3839 ;; will be exported as though it were an Org table like the one show
3842 ;; | Row 1 | 1.1 | 1.2 | 1.3 |
3843 ;; | Row 2 | 2.1 | 2.2 | 2.3 |
3845 ;; Note that org-tables are NOT multi-line and each line is mapped to
3846 ;; a unique row in the exported document. So if an exported table
3847 ;; needs to contain a single paragraph (with copious text) it needs to
3848 ;; be typed up in a single line. Editing such long lines using the
3849 ;; table editor will be a cumbersome task. Furthermore inclusion of
3850 ;; multi-paragraph text in a table cell is well-nigh impossible.
3852 ;; A LIST-TABLE circumvents above problems.
3854 ;; Note that in the example above the list items could be paragraphs
3855 ;; themselves and the list can be arbitrarily deep.
3857 ;; Inspired by following thread:
3858 ;; https://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01101.html
3860 ;; Translate lists to tables
3862 (defun org-e-odt--translate-list-tables (tree backend info
)
3866 (when (org-export-read-attribute :attr_odt l1-list
:list-table
)
3867 ;; Replace list with table.
3868 (org-element-set-element
3870 ;; Build replacement table.
3871 (apply 'org-element-adopt-elements
3872 (list 'table
'(:type org
:attr_odt
(":style \"GriddedTable\"")))
3877 (let* ((l1-item-contents (org-element-contents l1-item
))
3878 l1-item-leading-text l2-list
)
3879 ;; Remove Level-2 list from the Level-item. It
3880 ;; will be subsequently attached as table-cells.
3881 (let ((cur l1-item-contents
) prev
)
3882 (while (and cur
(not (eq (org-element-type (car cur
))
3885 (setq cur
(cdr cur
)))
3888 (setq l2-list
(car cur
)))
3889 (setq l1-item-leading-text l1-item-contents
))
3890 ;; Level-1 items start a table row.
3891 (apply 'org-element-adopt-elements
3892 (list 'table-row
(list :type
'standard
))
3893 ;; Leading text of level-1 item define the
3894 ;; first table-cell.
3895 (apply 'org-element-adopt-elements
3896 (list 'table-cell nil
)
3897 l1-item-leading-text
)
3898 ;; Level-2 items define subsequent
3899 ;; table-cells of the row.
3904 (apply 'org-element-adopt-elements
3905 (list 'table-cell nil
)
3906 (org-element-contents l2-item
)))
3914 ;;; Interactive functions
3916 (defun org-e-odt-create-manifest-file-entry (&rest args
)
3917 (push args org-e-odt-manifest-file-entries
))
3919 (defun org-e-odt-write-manifest-file ()
3920 (make-directory (concat org-e-odt-zip-dir
"META-INF"))
3921 (let ((manifest-file (concat org-e-odt-zip-dir
"META-INF/manifest.xml")))
3922 (with-current-buffer
3923 (let ((nxml-auto-insert-xml-declaration-flag nil
))
3924 (find-file-noselect manifest-file t
))
3926 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
3927 <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
3929 (lambda (file-entry)
3930 (let* ((version (nth 2 file-entry
))
3931 (extra (if (not version
) ""
3932 (format " manifest:version=\"%s\"" version
))))
3934 (format org-e-odt-manifest-file-entry-tag
3935 (nth 0 file-entry
) (nth 1 file-entry
) extra
))))
3936 org-e-odt-manifest-file-entries
)
3937 (insert "\n</manifest:manifest>"))))
3939 (defmacro org-e-odt--export-wrap
(out-file &rest body
)
3940 `(let* ((--out-file ,out-file
)
3941 (out-file-type (file-name-extension --out-file
))
3942 (org-e-odt-xml-files '("META-INF/manifest.xml" "content.xml"
3943 "meta.xml" "styles.xml"))
3944 ;; Initialize temporary workarea. All files that end up in
3945 ;; the exported document get parked/created here.
3946 (org-e-odt-zip-dir (file-name-as-directory
3947 (make-temp-file (format "%s-" out-file-type
) t
)))
3948 (org-e-odt-manifest-file-entries nil
)
3949 (--cleanup-xml-buffers
3952 ;; Kill all XML buffers.
3953 (mapc (lambda (file)
3954 (let ((buf (find-buffer-visiting
3955 (concat org-e-odt-zip-dir file
))))
3957 (with-current-buffer buf
3958 (set-buffer-modified-p nil
)
3959 (kill-buffer buf
)))))
3960 org-e-odt-xml-files
)
3961 ;; Delete temporary directory and also other embedded
3962 ;; files that get copied there.
3963 (delete-directory org-e-odt-zip-dir t
)))))
3964 (org-condition-case-unless-debug err
3966 (unless (executable-find "zip")
3967 ;; Not at all OSes ship with zip by default
3968 (error "Executable \"zip\" needed for creating OpenDocument files"))
3969 ;; Do export. This creates a bunch of xml files ready to be
3970 ;; saved and zipped.
3972 ;; Create a manifest entry for content.xml.
3973 (org-e-odt-create-manifest-file-entry "text/xml" "content.xml")
3974 ;; Write mimetype file
3976 '(("odt" .
"application/vnd.oasis.opendocument.text")
3977 ("odf" .
"application/vnd.oasis.opendocument.formula")))
3978 (mimetype (cdr (assoc-string out-file-type mimetypes t
))))
3980 (error "Unknown OpenDocument backend %S" out-file-type
))
3981 (write-region mimetype nil
(concat org-e-odt-zip-dir
"mimetype"))
3982 (org-e-odt-create-manifest-file-entry mimetype
"/" "1.2"))
3983 ;; Write out the manifest entries before zipping
3984 (org-e-odt-write-manifest-file)
3985 ;; Save all XML files.
3986 (mapc (lambda (file)
3987 (let ((buf (find-buffer-visiting
3988 (concat org-e-odt-zip-dir file
))))
3990 (with-current-buffer buf
3991 ;; Prettify output if needed.
3992 (when org-e-odt-prettify-xml
3993 (indent-region (point-min) (point-max)))
3995 org-e-odt-xml-files
)
3997 (let* ((target --out-file
)
3998 (target-name (file-name-nondirectory target
))
3999 (cmds `(("zip" "-mX0" ,target-name
"mimetype")
4000 ("zip" "-rmTq" ,target-name
"."))))
4001 ;; If a file with same name as the desired output file
4002 ;; exists, remove it.
4003 (when (file-exists-p target
)
4004 (delete-file target
))
4005 ;; Zip up the xml files.
4006 (let ((coding-system-for-write 'no-conversion
) exitcode err-string
)
4007 (message "Creating ODT file...")
4008 ;; Switch temporarily to content.xml. This way Zip
4009 ;; process will inherit `org-e-odt-zip-dir' as the current
4011 (with-current-buffer
4012 (find-file-noselect (concat org-e-odt-zip-dir
"content.xml") t
)
4015 (message "Running %s" (mapconcat 'identity cmd
" "))
4017 (with-output-to-string
4019 (apply 'call-process
(car cmd
)
4020 nil standard-output nil
(cdr cmd
)))))
4021 (or (zerop exitcode
)
4022 (error (concat "Unable to create OpenDocument file."
4023 (format " Zip failed with error (%s)"
4026 ;; Move the zip file from temporary work directory to
4027 ;; user-mandated location.
4028 (rename-file (concat org-e-odt-zip-dir target-name
) target
)
4029 (message "Created %s" (expand-file-name target
))
4030 ;; Cleanup work directory and work files.
4031 (funcall --cleanup-xml-buffers
)
4032 ;; Open the OpenDocument file in archive-mode for
4034 (find-file-noselect target t
)
4035 ;; Return exported file.
4037 ;; Case 1: Conversion desired on exported file. Run the
4038 ;; converter on the OpenDocument file. Return the
4040 (org-e-odt-preferred-output-format
4041 (or (org-e-odt-convert target org-e-odt-preferred-output-format
)
4043 ;; Case 2: No further conversion. Return exported
4044 ;; OpenDocument file.
4047 ;; Cleanup work directory and work files.
4048 (funcall --cleanup-xml-buffers
)
4049 (message "OpenDocument export failed: %s"
4050 (error-message-string err
))))))
4053 ;;;; Export to OpenDocument formula
4056 (defun org-e-odt-export-as-odf (latex-frag &optional odf-file
)
4057 "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
4058 Use `org-create-math-formula' to convert LATEX-FRAG first to
4059 MathML. When invoked as an interactive command, use
4060 `org-latex-regexps' to infer LATEX-FRAG from currently active
4061 region. If no LaTeX fragments are found, prompt for it. Push
4062 MathML source to kill ring, if `org-export-copy-to-kill-ring' is
4066 (setq frag
(and (setq frag
(and (region-active-p)
4067 (buffer-substring (region-beginning)
4069 (loop for e in org-latex-regexps
4070 thereis
(when (string-match (nth 1 e
) frag
)
4071 (match-string (nth 2 e
) frag
)))))
4072 (read-string "LaTeX Fragment: " frag nil frag
))
4073 ,(let ((odf-filename (expand-file-name
4075 (file-name-sans-extension
4076 (or (file-name-nondirectory buffer-file-name
)))
4078 (file-name-directory buffer-file-name
))))
4079 (read-file-name "ODF filename: " nil odf-filename nil
4080 (file-name-nondirectory odf-filename
)))))
4081 (let ((filename (or odf-file
4084 (file-name-sans-extension
4085 (or (file-name-nondirectory buffer-file-name
)))
4087 (file-name-directory buffer-file-name
)))))
4088 (org-e-odt--export-wrap
4090 (let* ((buffer (progn
4091 (require 'nxml-mode
)
4092 (let ((nxml-auto-insert-xml-declaration-flag nil
))
4093 (find-file-noselect (concat org-e-odt-zip-dir
4094 "content.xml") t
))))
4095 (coding-system-for-write 'utf-8
)
4096 (save-buffer-coding-system 'utf-8
))
4098 (set-buffer-file-coding-system coding-system-for-write
)
4099 (let ((mathml (org-create-math-formula latex-frag
)))
4100 (unless mathml
(error "No Math formula created"))
4102 ;; Add MathML to kill ring, if needed.
4103 (when org-export-copy-to-kill-ring
4104 (org-kill-new (buffer-string))))))))
4107 (defun org-e-odt-export-as-odf-and-open ()
4108 "Export LaTeX fragment as OpenDocument formula and immediately open it.
4109 Use `org-e-odt-export-as-odf' to read LaTeX fragment and OpenDocument
4112 (org-open-file (call-interactively 'org-e-odt-export-as-odf
) 'system
))
4115 ;;;; Export to OpenDocument Text
4118 (defun org-e-odt-export-to-odt (&optional async subtreep visible-only ext-plist
)
4119 "Export current buffer to a HTML file.
4121 If narrowing is active in the current buffer, only export its
4124 If a region is active, export that region.
4126 A non-nil optional argument ASYNC means the process should happen
4127 asynchronously. The resulting file should be accessible through
4128 the `org-export-stack' interface.
4130 When optional argument SUBTREEP is non-nil, export the sub-tree
4131 at point, extracting information from the headline properties
4134 When optional argument VISIBLE-ONLY is non-nil, don't export
4135 contents of hidden elements.
4137 EXT-PLIST, when provided, is a property list with external
4138 parameters overriding Org default settings, but still inferior to
4139 file-local settings.
4141 Return output file's name."
4143 (let ((outfile (org-export-output-file-name ".odt" subtreep
)))
4145 (org-export-async-start (lambda (f) (org-export-add-to-stack f
'e-odt
))
4147 (org-e-odt--export-wrap
4149 (let* ((org-e-odt-embedded-images-count 0)
4150 (org-e-odt-embedded-formulas-count 0)
4151 (org-e-odt-automatic-styles nil
)
4152 (org-e-odt-object-counters nil
)
4153 ;; Let `htmlfontify' know that we are interested in
4154 ;; collecting styles.
4155 (hfy-user-sheet-assoc nil
))
4156 ;; Initialize content.xml and kick-off the export
4160 (require 'nxml-mode
)
4161 (let ((nxml-auto-insert-xml-declaration-flag nil
))
4163 (concat org-e-odt-zip-dir
"content.xml") t
)))))
4164 (org-export-to-buffer
4165 'e-odt out-buf
,subtreep
,visible-only nil
',ext-plist
))))))
4166 (org-e-odt--export-wrap
4168 (let* ((org-e-odt-embedded-images-count 0)
4169 (org-e-odt-embedded-formulas-count 0)
4170 (org-e-odt-automatic-styles nil
)
4171 (org-e-odt-object-counters nil
)
4172 ;; Let `htmlfontify' know that we are interested in collecting
4174 (hfy-user-sheet-assoc nil
))
4175 ;; Initialize content.xml and kick-off the export process.
4176 (let ((out-buf (progn
4177 (require 'nxml-mode
)
4178 (let ((nxml-auto-insert-xml-declaration-flag nil
))
4180 (concat org-e-odt-zip-dir
"content.xml") t
)))))
4181 (org-export-to-buffer
4182 'e-odt out-buf subtreep visible-only nil ext-plist
)))))))
4185 ;;;; Convert between OpenDocument and other formats
4187 (defun org-e-odt-reachable-p (in-fmt out-fmt
)
4188 "Return non-nil if IN-FMT can be converted to OUT-FMT."
4190 (let ((reachable-formats (org-e-odt-do-reachable-formats in-fmt
)))
4191 (dolist (e reachable-formats
)
4192 (let ((out-fmt-spec (assoc out-fmt
(cdr e
))))
4194 (throw 'done
(cons (car e
) out-fmt-spec
))))))))
4196 (defun org-e-odt-do-convert (in-file out-fmt
&optional prefix-arg
)
4197 "Workhorse routine for `org-e-odt-convert'."
4198 (require 'browse-url
)
4199 (let* ((in-file (expand-file-name (or in-file buffer-file-name
)))
4200 (dummy (or (file-readable-p in-file
)
4201 (error "Cannot read %s" in-file
)))
4202 (in-fmt (file-name-extension in-file
))
4203 (out-fmt (or out-fmt
(error "Output format unspecified")))
4204 (how (or (org-e-odt-reachable-p in-fmt out-fmt
)
4205 (error "Cannot convert from %s format to %s format?"
4207 (convert-process (car how
))
4208 (out-file (concat (file-name-sans-extension in-file
) "."
4209 (nth 1 (or (cdr how
) out-fmt
))))
4210 (extra-options (or (nth 2 (cdr how
)) ""))
4211 (out-dir (file-name-directory in-file
))
4212 (cmd (format-spec convert-process
4213 `((?i .
,(shell-quote-argument in-file
))
4214 (?I .
,(browse-url-file-url in-file
))
4217 (?O .
,(browse-url-file-url out-file
))
4218 (?d .
, (shell-quote-argument out-dir
))
4219 (?D .
,(browse-url-file-url out-dir
))
4220 (?x .
,extra-options
)))))
4221 (when (file-exists-p out-file
)
4222 (delete-file out-file
))
4224 (message "Executing %s" cmd
)
4225 (let ((cmd-output (shell-command-to-string cmd
)))
4226 (message "%s" cmd-output
))
4229 ((file-exists-p out-file
)
4230 (message "Exported to %s" out-file
)
4232 (message "Opening %s..." out-file
)
4233 (org-open-file out-file
'system
))
4236 (message "Export to %s failed" out-file
)
4239 (defun org-e-odt-do-reachable-formats (in-fmt)
4240 "Return verbose info about formats to which IN-FMT can be converted.
4241 Return a list where each element is of the
4242 form (CONVERTER-PROCESS . OUTPUT-FMT-ALIST). See
4243 `org-e-odt-convert-processes' for CONVERTER-PROCESS and see
4244 `org-e-odt-convert-capabilities' for OUTPUT-FMT-ALIST."
4246 (and org-e-odt-convert-process
4247 (cadr (assoc-string org-e-odt-convert-process
4248 org-e-odt-convert-processes t
))))
4250 (and org-e-odt-convert-process
4251 (cadr (assoc-string org-e-odt-convert-process
4252 org-e-odt-convert-processes t
))
4253 org-e-odt-convert-capabilities
))
4256 (dolist (c capabilities
)
4257 (when (member in-fmt
(nth 1 c
))
4258 (push (cons converter
(nth 2 c
)) reachable-formats
))))
4261 (defun org-e-odt-reachable-formats (in-fmt)
4262 "Return list of formats to which IN-FMT can be converted.
4263 The list of the form (OUTPUT-FMT-1 OUTPUT-FMT-2 ...)."
4265 (mapc (lambda (e) (add-to-list 'l e
))
4266 (apply 'append
(mapcar
4267 (lambda (e) (mapcar 'car
(cdr e
)))
4268 (org-e-odt-do-reachable-formats in-fmt
))))
4271 (defun org-e-odt-convert-read-params ()
4272 "Return IN-FILE and OUT-FMT params for `org-e-odt-do-convert'.
4273 This is a helper routine for interactive use."
4274 (let* ((input (if (featurep 'ido
) 'ido-completing-read
'completing-read
))
4275 (in-file (read-file-name "File to be converted: "
4276 nil buffer-file-name t
))
4277 (in-fmt (file-name-extension in-file
))
4278 (out-fmt-choices (org-e-odt-reachable-formats in-fmt
))
4280 (or (and out-fmt-choices
4281 (funcall input
"Output format: "
4282 out-fmt-choices nil nil nil
))
4284 "No known converter or no known output formats for %s files"
4286 (list in-file out-fmt
)))
4289 (defun org-e-odt-convert (&optional in-file out-fmt prefix-arg
)
4290 "Convert IN-FILE to format OUT-FMT using a command line converter.
4291 IN-FILE is the file to be converted. If unspecified, it defaults
4292 to variable `buffer-file-name'. OUT-FMT is the desired output
4293 format. Use `org-e-odt-convert-process' as the converter.
4294 If PREFIX-ARG is non-nil then the newly converted file is opened
4295 using `org-open-file'."
4297 (append (org-e-odt-convert-read-params) current-prefix-arg
))
4298 (org-e-odt-do-convert in-file out-fmt prefix-arg
))
4300 ;;; Library Initializations
4304 ;; Let Emacs open all OpenDocument files in archive mode
4305 (add-to-list 'auto-mode-alist
4306 (cons (concat "\\." (car desc
) "\\'") 'archive-mode
)))
4307 org-e-odt-file-extensions
)
4312 ;;;; Links whose description is image
4314 ;; (org-lparse-link-description-is-image
4315 ;; (format "\n<draw:a xlink:type=\"simple\" xlink:href=\"%s\">\n%s\n</draw:a>"
4319 (provide 'org-e-odt
)
4321 ;;; org-e-odt.el ends here