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 (org-open-file (org-e-odt-export-to-odt s v b
) 'system
)))))
97 ((:odt-styles-file
"ODT_STYLES_FILE" nil nil t
)
98 (:LaTeX-fragments nil
"LaTeX" org-export-with-LaTeX-fragments
)))
105 ;;; Function Declarations
107 (declare-function org-id-find-id-file
"org-id" (id))
108 (declare-function hfy-face-to-style
"htmlfontify" (fn))
109 (declare-function hfy-face-or-def-to-name
"htmlfontify" (fn))
110 (declare-function archive-zip-extract
"arc-mode" (archive name
))
111 (declare-function org-create-math-formula
"org" (latex-frag &optional mathml-file
))
112 (declare-function browse-url-file-url
"browse-url" (file))
116 ;;; Internal Variables
118 (defconst org-e-odt-lib-dir
119 (file-name-directory load-file-name
)
120 "Location of ODT exporter.
121 Use this to infer values of `org-e-odt-styles-dir' and
122 `org-e-odt-schema-dir'.")
124 (defvar org-e-odt-data-dir
125 (expand-file-name "../../etc/" org-e-odt-lib-dir
)
126 "Data directory for ODT exporter.
127 Use this to infer values of `org-e-odt-styles-dir' and
128 `org-e-odt-schema-dir'.")
130 (defconst org-e-odt-special-string-regexps
131 '(("\\\\-" .
"­\\1") ; shy
132 ("---\\([^-]\\)" .
"—\\1") ; mdash
133 ("--\\([^-]\\)" .
"–\\1") ; ndash
134 ("\\.\\.\\." .
"…")) ; hellip
135 "Regular expressions for special string conversion.")
137 (defconst org-e-odt-schema-dir-list
139 (and org-e-odt-data-dir
140 (expand-file-name "./schema/" org-e-odt-data-dir
)) ; bail out
142 (and (boundp 'org-e-odt-data-dir
) org-e-odt-data-dir
; see make install
143 (expand-file-name "./schema/" org-e-odt-data-dir
))))
144 "List of directories to search for OpenDocument schema files.
145 Use this list to set the default value of
146 `org-e-odt-schema-dir'. The entries in this list are
147 populated heuristically based on the values of `org-e-odt-lib-dir'
148 and `org-e-odt-data-dir'.")
150 (defconst org-e-odt-styles-dir-list
152 (and org-e-odt-data-dir
153 (expand-file-name "./styles/" org-e-odt-data-dir
)) ; bail out
155 (and (boundp 'org-e-odt-data-dir
) org-e-odt-data-dir
; see make install
156 (expand-file-name "./styles/" org-e-odt-data-dir
)))
157 (expand-file-name "../../etc/styles/" org-e-odt-lib-dir
) ; git
158 (expand-file-name "./etc/styles/" org-e-odt-lib-dir
) ; elpa
159 (expand-file-name "./org/" data-directory
) ; system
161 "List of directories to search for OpenDocument styles files.
162 See `org-e-odt-styles-dir'. The entries in this list are populated
163 heuristically based on the values of `org-e-odt-lib-dir' and
164 `org-e-odt-data-dir'.")
166 (defconst org-e-odt-styles-dir
169 (message "Debug (org-e-odt): Searching for OpenDocument styles files...")
170 (mapc (lambda (styles-dir)
172 (message "Debug (org-e-odt): Trying %s..." styles-dir
)
173 (when (and (file-readable-p
175 "OrgOdtContentTemplate.xml" styles-dir
))
178 "OrgOdtStyles.xml" styles-dir
)))
179 (message "Debug (org-e-odt): Using styles under %s"
181 (throw 'styles-dir styles-dir
))))
182 org-e-odt-styles-dir-list
)
185 (error "Error (org-e-odt): Cannot find factory styles files, aborting"))
187 "Directory that holds auxiliary XML files used by the ODT exporter.
189 This directory contains the following XML files -
190 \"OrgOdtStyles.xml\" and \"OrgOdtContentTemplate.xml\". These
191 XML files are used as the default values of
192 `org-e-odt-styles-file' and
193 `org-e-odt-content-template-file'.
195 The default value of this variable varies depending on the
196 version of org in use and is initialized from
197 `org-e-odt-styles-dir-list'. Note that the user could be using org
198 from one of: org's own private git repository, GNU ELPA tar or
201 (defconst org-e-odt-bookmark-prefix
"OrgXref.")
203 (defconst org-e-odt-manifest-file-entry-tag
204 "\n<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
206 (defconst org-e-odt-file-extensions
207 '(("odt" .
"OpenDocument Text")
208 ("ott" .
"OpenDocument Text Template")
209 ("odm" .
"OpenDocument Master Document")
210 ("ods" .
"OpenDocument Spreadsheet")
211 ("ots" .
"OpenDocument Spreadsheet Template")
212 ("odg" .
"OpenDocument Drawing (Graphics)")
213 ("otg" .
"OpenDocument Drawing Template")
214 ("odp" .
"OpenDocument Presentation")
215 ("otp" .
"OpenDocument Presentation Template")
216 ("odi" .
"OpenDocument Image")
217 ("odf" .
"OpenDocument Formula")
218 ("odc" .
"OpenDocument Chart")))
220 (defconst org-e-odt-table-style-format
222 <style:style style:name=\"%s\" style:family=\"table\">
223 <style:table-properties style:rel-width=\"%d%%\" fo:margin-top=\"0cm\" fo:margin-bottom=\"0.20cm\" table:align=\"center\"/>
226 "Template for auto-generated Table styles.")
228 (defvar org-e-odt-automatic-styles
'()
229 "Registry of automatic styles for various OBJECT-TYPEs.
230 The variable has the following form:
232 \(\(OBJECT-NAME-A.1 OBJECT-PROPS-A.1\)
233 \(OBJECT-NAME-A.2 OBJECT-PROPS-A.2\) ...\)\)
235 \(\(OBJECT-NAME-B.1 OBJECT-PROPS-B.1\)
236 \(OBJECT-NAME-B.2 OBJECT-PROPS-B.2\) ...\)\)
239 OBJECT-TYPEs could be \"Section\", \"Table\", \"Figure\" etc.
240 OBJECT-PROPS is (typically) a plist created by passing
241 \"#+ATTR_ODT: \" option to `org-e-odt-parse-block-attributes'.
243 Use `org-e-odt-add-automatic-style' to add update this variable.'")
245 (defvar org-e-odt-object-counters nil
246 "Running counters for various OBJECT-TYPEs.
247 Use this to generate automatic names and style-names. See
248 `org-e-odt-add-automatic-style'.")
250 (defvar org-e-odt-src-block-paragraph-format
251 "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
252 <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
253 <style:background-image/>
254 </style:paragraph-properties>
255 <style:text-properties fo:color=\"%s\"/>
257 "Custom paragraph style for colorized source and example blocks.
258 This style is much the same as that of \"OrgFixedWidthBlock\"
259 except that the foreground and background colors are set
260 according to the default face identified by the `htmlfontify'.")
262 (defvar hfy-optimisations
)
263 (defvar org-e-odt-embedded-formulas-count
0)
264 (defvar org-e-odt-embedded-images-count
0)
265 (defvar org-e-odt-image-size-probe-method
266 (append (and (executable-find "identify") '(imagemagick)) ; See Bug#10675
268 "Ordered list of methods for determining image sizes.")
270 (defvar org-e-odt-default-image-sizes-alist
271 '(("as-char" .
(5 .
0.4))
272 ("paragraph" .
(5 .
5)))
273 "Hardcoded image dimensions one for each of the anchor
276 ;; A4 page size is 21.0 by 29.7 cms
277 ;; The default page settings has 2cm margin on each of the sides. So
278 ;; the effective text area is 17.0 by 25.7 cm
279 (defvar org-e-odt-max-image-size
'(17.0 .
20.0)
280 "Limiting dimensions for an embedded image.")
282 (defconst org-e-odt-label-styles
283 '(("math-formula" "%c" "text" "(%n)")
284 ("math-label" "(%n)" "text" "(%n)")
285 ("category-and-value" "%e %n: %c" "category-and-value" "%e %n")
286 ("value" "%e %n: %c" "value" "%n"))
287 "Specify how labels are applied and referenced.
288 This is an alist where each element is of the
289 form (LABEL-STYLE-NAME LABEL-ATTACH-FMT LABEL-REF-MODE
292 LABEL-ATTACH-FMT controls how labels and captions are attached to
293 an entity. It may contain following specifiers - %e, %n and %c.
294 %e is replaced with the CATEGORY-NAME. %n is replaced with
295 \"<text:sequence ...> SEQNO </text:sequence>\". %c is replaced
296 with CAPTION. See `org-e-odt-format-label-definition'.
298 LABEL-REF-MODE and LABEL-REF-FMT controls how label references
299 are generated. The following XML is generated for a label
300 reference - \"<text:sequence-ref
301 text:reference-format=\"LABEL-REF-MODE\" ...> LABEL-REF-FMT
302 </text:sequence-ref>\". LABEL-REF-FMT may contain following
303 specifiers - %e and %n. %e is replaced with the CATEGORY-NAME.
304 %n is replaced with SEQNO. See
305 `org-e-odt-format-label-reference'.")
307 (defvar org-e-odt-category-map-alist
308 '(("__Table__" "Table" "value" "Table" org-e-odt--enumerable-p
)
309 ("__Figure__" "Illustration" "value" "Figure" org-e-odt--enumerable-image-p
)
310 ("__MathFormula__" "Text" "math-formula" "Equation" org-e-odt--enumerable-formula-p
)
311 ("__DvipngImage__" "Equation" "value" "Equation" org-e-odt--enumerable-latex-image-p
)
312 ("__Listing__" "Listing" "value" "Listing" org-e-odt--enumerable-p
)
313 ;; ("__Table__" "Table" "category-and-value")
314 ;; ("__Figure__" "Figure" "category-and-value")
315 ;; ("__DvipngImage__" "Equation" "category-and-value")
317 "Map a CATEGORY-HANDLE to OD-VARIABLE and LABEL-STYLE.
318 This is a list where each entry is of the form \\(CATEGORY-HANDLE
319 OD-VARIABLE LABEL-STYLE CATEGORY-NAME ENUMERATOR-PREDICATE\\).
321 CATEGORY_HANDLE identifies the captionable entity in question.
323 OD-VARIABLE is the OpenDocument sequence counter associated with
324 the entity. These counters are declared within
325 \"<text:sequence-decls>...</text:sequence-decls>\" block of
326 `org-e-odt-content-template-file'.
328 LABEL-STYLE is a key into `org-e-odt-label-styles' and specifies
329 how a given entity should be captioned and referenced.
331 CATEGORY-NAME is used for qualifying captions on export. You can
332 modify the CATEGORY-NAME used in the exported document by
333 modifying `org-export-dictionary'. For example, an embedded
334 image in an English document is captioned as \"Figure 1: Orgmode
335 Logo\", by default. If you want the image to be captioned as
336 \"Illustration 1: Orgmode Logo\" instead, install an entry in
337 `org-export-dictionary' which translates \"Figure\" to
338 \"Illustration\" when the language is \"en\" and encoding is
341 ENUMERATOR-PREDICATE is used for assigning a sequence number to
342 the entity. See `org-e-odt--enumerate'.")
344 (defvar org-e-odt-manifest-file-entries nil
)
345 (defvar hfy-user-sheet-assoc
)
347 (defvar org-e-odt-zip-dir nil
348 "Temporary work directory for OpenDocument exporter.")
352 ;;; User Configuration Variables
354 (defgroup org-export-e-odt nil
355 "Options for exporting Org mode files to ODT."
356 :tag
"Org Export ODT"
362 (defcustom org-e-odt-prettify-xml nil
363 "Specify whether or not the xml output should be prettified.
364 When this option is turned on, `indent-region' is run on all
365 component xml buffers before they are saved. Turn this off for
366 regular use. Turn this on if you need to examine the xml
368 :group
'org-export-e-odt
375 (defcustom org-e-odt-schema-dir
378 (message "Debug (org-e-odt): Searching for OpenDocument schema files...")
382 (message "Debug (org-e-odt): Trying %s..." schema-dir
)
383 (when (and (file-readable-p
384 (expand-file-name "od-manifest-schema-v1.2-cs01.rnc"
387 (expand-file-name "od-schema-v1.2-cs01.rnc"
390 (expand-file-name "schemas.xml" schema-dir
)))
391 (message "Debug (org-e-odt): Using schema files under %s"
393 (throw 'schema-dir schema-dir
))))
394 org-e-odt-schema-dir-list
)
395 (message "Debug (org-e-odt): No OpenDocument schema files installed")
398 "Directory that contains OpenDocument schema files.
400 This directory contains:
401 1. rnc files for OpenDocument schema
402 2. a \"schemas.xml\" file that specifies locating rules needed
403 for auto validation of OpenDocument XML files.
405 Use the customize interface to set this variable. This ensures
406 that `rng-schema-locating-files' is updated and auto-validation
407 of OpenDocument XML takes place based on the value
408 `rng-nxml-auto-validate-flag'.
410 The default value of this variable varies depending on the
411 version of org in use and is initialized from
412 `org-e-odt-schema-dir-list'. The OASIS schema files are available
413 only in the org's private git repository. It is *not* bundled
414 with GNU ELPA tar or standard Emacs distribution."
416 (const :tag
"Not set" nil
)
417 (directory :tag
"Schema directory"))
418 :group
'org-export-e-odt
422 "Set `org-e-odt-schema-dir'.
423 Also add it to `rng-schema-locating-files'."
424 (let ((schema-dir value
))
428 (expand-file-name "od-manifest-schema-v1.2-cs01.rnc" schema-dir
))
430 (expand-file-name "od-schema-v1.2-cs01.rnc" schema-dir
))
432 (expand-file-name "schemas.xml" schema-dir
)))
435 (message "Error (org-e-odt): %s has no OpenDocument schema files"
438 (when org-e-odt-schema-dir
439 (eval-after-load 'rng-loc
440 '(add-to-list 'rng-schema-locating-files
441 (expand-file-name "schemas.xml"
442 org-e-odt-schema-dir
))))))
447 (defcustom org-e-odt-content-template-file nil
448 "Template file for \"content.xml\".
449 The exporter embeds the exported content just before
450 \"</office:text>\" element.
452 If unspecified, the file named \"OrgOdtContentTemplate.xml\"
453 under `org-e-odt-styles-dir' is used."
455 :group
'org-export-e-odt
458 (defcustom org-e-odt-styles-file nil
459 "Default styles file for use with ODT export.
460 Valid values are one of:
462 2. path to a styles.xml file
463 3. path to a *.odt or a *.ott file
464 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
467 In case of option 1, an in-built styles.xml is used. See
468 `org-e-odt-styles-dir' for more information.
470 In case of option 3, the specified file is unzipped and the
471 styles.xml embedded therein is used.
473 In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
474 and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
475 generated odt file. Use relative path for specifying the
476 FILE-MEMBERS. styles.xml must be specified as one of the
479 Use options 1, 2 or 3 only if styles.xml alone suffices for
480 achieving the desired formatting. Use option 4, if the styles.xml
481 references additional files like header and footer images for
482 achieving the desired formatting.
484 Use \"#+ODT_STYLES_FILE: ...\" directive to set this variable on
485 a per-file basis. For example,
487 #+ODT_STYLES_FILE: \"/path/to/styles.xml\" or
488 #+ODT_STYLES_FILE: (\"/path/to/file.ott\" (\"styles.xml\" \"image/hdr.png\"))."
489 :group
'org-export-e-odt
493 (const :tag
"Factory settings" nil
)
494 (file :must-match t
:tag
"styles.xml")
495 (file :must-match t
:tag
"ODT or OTT file")
496 (list :tag
"ODT or OTT file + Members"
497 (file :must-match t
:tag
"ODF Text or Text Template file")
499 (file :tag
" Member" "styles.xml")
500 (repeat (file :tag
"Member"))))))
502 (defcustom org-e-odt-display-outline-level
2
503 "Outline levels considered for enumerating captioned entities."
504 :group
'org-export-e-odt
508 ;;;; Document conversion
510 (defcustom org-e-odt-convert-processes
512 "soffice --headless --convert-to %f%x --outdir %d %i")
514 "unoconv -f %f -o %d %i"))
515 "Specify a list of document converters and their usage.
516 The converters in this list are offered as choices while
517 customizing `org-e-odt-convert-process'.
519 This variable is a list where each element is of the
520 form (CONVERTER-NAME CONVERTER-CMD). CONVERTER-NAME is the name
521 of the converter. CONVERTER-CMD is the shell command for the
522 converter and can contain format specifiers. These format
523 specifiers are interpreted as below:
525 %i input file name in full
526 %I input file name as a URL
527 %f format of the output file
528 %o output file name in full
529 %O output file name as a URL
530 %d output dir in full
531 %D output dir as a URL.
532 %x extra options as set in `org-e-odt-convert-capabilities'."
533 :group
'org-export-e-odt
537 (const :tag
"None" nil
)
538 (alist :tag
"Converters"
539 :key-type
(string :tag
"Converter Name")
540 :value-type
(group (string :tag
"Command line")))))
542 (defcustom org-e-odt-convert-process
"LibreOffice"
543 "Use this converter to convert from \"odt\" format to other formats.
544 During customization, the list of converter names are populated
545 from `org-e-odt-convert-processes'."
546 :group
'org-export-e-odt
548 :type
'(choice :convert-widget
550 (apply 'widget-convert
(widget-type w
)
551 (eval (car (widget-get w
:args
)))))
552 `((const :tag
"None" nil
)
553 ,@(mapcar (lambda (c)
554 `(const :tag
,(car c
) ,(car c
)))
555 org-e-odt-convert-processes
))))
557 (defcustom org-e-odt-convert-capabilities
559 ("odt" "ott" "doc" "rtf" "docx")
560 (("pdf" "pdf") ("odt" "odt") ("rtf" "rtf") ("ott" "ott")
561 ("doc" "doc" ":\"MS Word 97\"") ("docx" "docx") ("html" "html")))
564 (("pdf" "pdf") ("odt" "odt") ("html" "html")))
566 ("ods" "ots" "xls" "csv" "xlsx")
567 (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv") ("ods" "ods")
568 ("xls" "xls") ("xlsx" "xlsx")))
570 ("odp" "otp" "ppt" "pptx")
571 (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("otp" "otp") ("ppt" "ppt")
572 ("pptx" "pptx") ("odg" "odg"))))
573 "Specify input and output formats of `org-e-odt-convert-process'.
574 More correctly, specify the set of input and output formats that
575 the user is actually interested in.
577 This variable is an alist where each element is of the
578 form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
579 INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
580 alist where each element is of the form (OUTPUT-FMT
581 OUTPUT-FILE-EXTENSION EXTRA-OPTIONS).
583 The variable is interpreted as follows:
584 `org-e-odt-convert-process' can take any document that is in
585 INPUT-FMT-LIST and produce any document that is in the
586 OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
587 OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
588 serves dual purposes:
589 - It is used for populating completion candidates during
590 `org-e-odt-convert' commands.
591 - It is used as the value of \"%f\" specifier in
592 `org-e-odt-convert-process'.
594 EXTRA-OPTIONS is used as the value of \"%x\" specifier in
595 `org-e-odt-convert-process'.
597 DOCUMENT-CLASS is used to group a set of file formats in
598 INPUT-FMT-LIST in to a single class.
600 Note that this variable inherently captures how LibreOffice based
601 converters work. LibreOffice maps documents of various formats
602 to classes like Text, Web, Spreadsheet, Presentation etc and
603 allow document of a given class (irrespective of it's source
604 format) to be converted to any of the export formats associated
607 See default setting of this variable for an typical
609 :group
'org-export-e-odt
613 (const :tag
"None" nil
)
614 (alist :tag
"Capabilities"
615 :key-type
(string :tag
"Document Class")
617 (group (repeat :tag
"Input formats" (string :tag
"Input format"))
618 (alist :tag
"Output formats"
619 :key-type
(string :tag
"Output format")
621 (group (string :tag
"Output file extension")
623 (const :tag
"None" nil
)
624 (string :tag
"Extra options"))))))))
626 (defcustom org-e-odt-preferred-output-format nil
627 "Automatically post-process to this format after exporting to \"odt\".
628 Command `org-e-odt-export-to-odt' exports first to \"odt\" format
629 and then uses `org-e-odt-convert-process' to convert the
630 resulting document to this format. During customization of this
631 variable, the list of valid values are populated based on
632 `org-e-odt-convert-capabilities'.
634 You can set this option on per-file basis using file local
635 values. See Info node `(emacs) File Variables'."
636 :group
'org-export-e-odt
638 :type
'(choice :convert-widget
640 (apply 'widget-convert
(widget-type w
)
641 (eval (car (widget-get w
:args
)))))
642 `((const :tag
"None" nil
)
643 ,@(mapcar (lambda (c)
645 (org-e-odt-reachable-formats "odt")))))
647 (put 'org-e-odt-preferred-output-format
'safe-local-variable
'stringp
)
652 (defcustom org-e-odt-format-drawer-function nil
653 "Function called to format a drawer in HTML code.
655 The function must accept two parameters:
656 NAME the drawer name, like \"LOGBOOK\"
657 CONTENTS the contents of the drawer.
659 The function should return the string to be exported.
661 For example, the variable could be set to the following function
662 in order to mimic default behaviour:
664 \(defun org-e-odt-format-drawer-default \(name contents\)
665 \"Format a drawer element for HTML export.\"
667 :group
'org-export-e-odt
673 (defcustom org-e-odt-format-headline-function nil
674 "Function to format headline text.
676 This function will be called with 5 arguments:
677 TODO the todo keyword \(string or nil\).
678 TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
679 PRIORITY the priority of the headline \(integer or nil\)
680 TEXT the main headline text \(string\).
681 TAGS the tags string, separated with colons \(string or nil\).
683 The function result will be used in the section format string.
685 As an example, one could set the variable to the following, in
686 order to reproduce the default set-up:
688 \(defun org-e-odt-format-headline \(todo todo-type priority text tags\)
689 \"Default format function for an headline.\"
691 \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo\)\)
693 \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
695 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
696 :group
'org-export-e-odt
702 (defcustom org-e-odt-format-inlinetask-function nil
703 "Function called to format an inlinetask in HTML code.
705 The function must accept six parameters:
706 TODO the todo keyword, as a string
707 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
708 PRIORITY the inlinetask priority, as a string
709 NAME the inlinetask name, as a string.
710 TAGS the inlinetask tags, as a string.
711 CONTENTS the contents of the inlinetask, as a string.
713 The function should return the string to be exported.
715 For example, the variable could be set to the following function
716 in order to mimic default behaviour:
718 \(defun org-e-odt-format-inlinetask \(todo type priority name tags contents\)
719 \"Format an inline task element for HTML export.\"
723 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
724 \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
726 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
727 \(format \(concat \"\\\\begin{center}\\n\"
729 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
731 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
733 \"\\\\end{minipage}}\"
734 \"\\\\end{center}\"\)
735 full-title contents\)\)"
736 :group
'org-export-e-odt
742 (defcustom org-e-odt-inline-formula-rules
743 '(("file" .
"\\.\\(mathml\\|mml\\)\\'"))
744 "Rules characterizing formula files that can be inlined into HTML.
746 A rule consists in an association whose key is the type of link
747 to consider, and value is a regexp that will be matched against
749 :group
'org-export-e-odt
750 :type
'(alist :key-type
(string :tag
"Type")
751 :value-type
(regexp :tag
"Path")))
753 (defcustom org-e-odt-inline-image-rules
754 '(("file" .
"\\.\\(jpeg\\|jpg\\|png\\|gif\\)\\'"))
755 "Rules characterizing image files that can be inlined into HTML.
757 A rule consists in an association whose key is the type of link
758 to consider, and value is a regexp that will be matched against
760 :group
'org-export-e-odt
761 :type
'(alist :key-type
(string :tag
"Type")
762 :value-type
(regexp :tag
"Path")))
764 (defcustom org-e-odt-pixels-per-inch display-pixels-per-inch
765 "Scaling factor for converting images pixels to inches.
766 Use this for sizing of embedded images. See Info node `(org)
767 Images in ODT export' for more information."
769 :group
'org-export-e-odt
775 (defcustom org-e-odt-quotes
777 ("\\(\\s-\\|[[(]\\|^\\)\"" .
"« ")
778 ("\\(\\S-\\)\"" .
"» ")
779 ("\\(\\s-\\|(\\|^\\)'" .
"'"))
781 ("\\(\\s-\\|[[(]\\|^\\)\"" .
"“")
782 ("\\(\\S-\\)\"" .
"”")
783 ("\\(\\s-\\|(\\|^\\)'" .
"‘")
784 ("\\(\\S-\\)'" .
"’")))
785 "Alist for quotes to use when converting english double-quotes.
787 The CAR of each item in this alist is the language code.
788 The CDR of each item in this alist is a list of three CONS:
789 - the first CONS defines the opening quote;
790 - the second CONS defines the closing quote;
791 - the last CONS defines single quotes.
793 For each item in a CONS, the first string is a regexp
794 for allowed characters before/after the quote, the second
795 string defines the replacement string for this quote."
796 :group
'org-export-e-odt
798 (cons :tag
"Opening quote"
799 (string :tag
"Regexp for char before")
800 (string :tag
"Replacement quote "))
801 (cons :tag
"Closing quote"
802 (string :tag
"Regexp for char after ")
803 (string :tag
"Replacement quote "))
804 (cons :tag
"Single quote"
805 (string :tag
"Regexp for char before")
806 (string :tag
"Replacement quote "))))
811 (defcustom org-e-odt-create-custom-styles-for-srcblocks t
812 "Whether custom styles for colorized source blocks be automatically created.
813 When this option is turned on, the exporter creates custom styles
814 for source blocks based on the advice of `htmlfontify'. Creation
815 of custom styles happen as part of `org-e-odt-hfy-face-to-css'.
817 When this option is turned off exporter does not create such
820 Use the latter option if you do not want the custom styles to be
821 based on your current display settings. It is necessary that the
822 styles.xml already contains needed styles for colorizing to work.
824 This variable is effective only if
825 `org-e-odt-fontify-srcblocks' is turned on."
826 :group
'org-export-e-odt
830 (defcustom org-e-odt-fontify-srcblocks t
831 "Specify whether or not source blocks need to be fontified.
832 Turn this option on if you want to colorize the source code
833 blocks in the exported file. For colorization to work, you need
834 to make available an enhanced version of `htmlfontify' library."
836 :group
'org-export-e-odt
842 (defcustom org-e-odt-table-caption-above t
843 "When non-nil, place caption string at the beginning of the table.
844 Otherwise, place it near the end."
845 :group
'org-export-e-odt
848 (defcustom org-e-odt-table-styles
849 '(("OrgEquation" "OrgEquation"
850 ((use-first-column-styles . t
)
851 (use-last-column-styles . t
)))
852 ("TableWithHeaderRowAndColumn" "Custom"
853 ((use-first-row-styles . t
)
854 (use-first-column-styles . t
)))
855 ("TableWithFirstRowandLastRow" "Custom"
856 ((use-first-row-styles . t
)
857 (use-last-row-styles . t
)))
858 ("GriddedTable" "Custom" nil
))
859 "Specify how Table Styles should be derived from a Table Template.
860 This is a list where each element is of the
861 form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
863 TABLE-STYLE-NAME is the style associated with the table through
864 \"#+ATTR_ODT: :style TABLE-STYLE-NAME\" line.
866 TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
867 TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
868 below) that is included in
869 `org-e-odt-content-template-file'.
871 TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
873 PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
875 TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
876 \"FirstRow\" | \"LastRow\" |
877 \"EvenRow\" | \"OddRow\" |
878 \"EvenColumn\" | \"OddColumn\" | \"\"
879 where \"+\" above denotes string concatenation.
881 TABLE-CELL-OPTIONS is an alist where each element is of the
882 form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
883 TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
884 `use-last-row-styles' |
885 `use-first-column-styles' |
886 `use-last-column-styles' |
887 `use-banding-rows-styles' |
888 `use-banding-columns-styles' |
889 `use-first-row-styles'
890 ON-OR-OFF := `t' | `nil'
892 For example, with the following configuration
894 \(setq org-e-odt-table-styles
895 '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
896 \(\(use-first-row-styles . t\)
897 \(use-first-column-styles . t\)\)\)
898 \(\"TableWithHeaderColumns\" \"Custom\"
899 \(\(use-first-column-styles . t\)\)\)\)\)
901 1. A table associated with \"TableWithHeaderRowsAndColumns\"
902 style will use the following table-cell styles -
903 \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
904 \"CustomTableCell\" and the following paragraph styles
905 \"CustomFirstRowTableParagraph\",
906 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
909 2. A table associated with \"TableWithHeaderColumns\" style will
910 use the following table-cell styles -
911 \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
912 following paragraph styles
913 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
916 Note that TABLE-TEMPLATE-NAME corresponds to the
917 \"<table:table-template>\" elements contained within
918 \"<office:styles>\". The entries (TABLE-STYLE-NAME
919 TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
920 \"table:template-name\" and \"table:use-first-row-styles\" etc
921 attributes of \"<table:table>\" element. Refer ODF-1.2
922 specification for more information. Also consult the
923 implementation filed under `org-e-odt-get-table-cell-styles'.
925 The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
926 formatting of numbered display equations. Do not delete this
927 style from the list."
928 :group
'org-export-e-odt
931 (const :tag
"None" nil
)
932 (repeat :tag
"Table Styles"
933 (list :tag
"Table Style Specification"
934 (string :tag
"Table Style Name")
935 (string :tag
"Table Template Name")
936 (alist :options
(use-first-row-styles
938 use-first-column-styles
939 use-last-column-styles
940 use-banding-rows-styles
941 use-banding-columns-styles
)
943 :value-type
(const :tag
"True" t
))))))
947 ;;; Internal functions
951 (defun org-e-odt--date (&optional org-ts fmt
)
954 (and (stringp org-ts
)
955 (string-match org-ts-regexp0 org-ts
)
957 (org-fix-decoded-time
958 (org-parse-time-string (match-string 0 org-ts
) t
)))))
961 (fmt (format-time-string fmt time
))
962 (t (setq date
(format-time-string "%Y-%m-%dT%H:%M:%S%z" time
))
963 (format "%s:%s" (substring date
0 -
2) (substring date -
2)))))))
967 (defun org-e-odt--frame (text width height style
&optional extra
971 (if width
(format " svg:width=\"%0.2fcm\"" width
) "")
972 (if height
(format " svg:height=\"%0.2fcm\"" height
) "")
974 (format " text:anchor-type=\"%s\"" (or anchor-type
"paragraph")))))
976 "\n<draw:frame draw:style-name=\"%s\"%s>\n%s\n</draw:frame>"
979 (let ((title (get-text-property 0 :title text
))
980 (desc (get-text-property 0 :description text
)))
982 (format "<svg:title>%s</svg:title>"
983 (org-e-odt--encode-plain-text title t
)))
985 (format "<svg:desc>%s</svg:desc>"
986 (org-e-odt--encode-plain-text desc t
)))))))))
989 ;;;; Library wrappers
991 (defun org-e-odt--zip-extract (archive members target
)
992 (when (atom members
) (setq members
(list members
)))
993 (mapc (lambda (archive member target
)
995 (let* ((--quote-file-name
996 ;; This is shamelessly stolen from `archive-zip-extract'.
998 (if (or (not (memq system-type
'(windows-nt ms-dos
)))
999 (and (boundp 'w32-quote-process-args
)
1000 (null w32-quote-process-args
)))
1001 (shell-quote-argument name
)
1003 (target (funcall --quote-file-name target
))
1004 (archive (expand-file-name archive
))
1005 (archive-zip-extract
1006 (list "unzip" "-qq" "-o" "-d" target
))
1007 exit-code command-output
)
1008 (setq command-output
1010 (setq exit-code
(archive-zip-extract archive member
))
1012 (unless (zerop exit-code
)
1013 (message command-output
)
1014 (error "Extraction failed"))))
1020 (defun org-e-odt--target (text id
)
1023 (format "\n<text:bookmark-start text:name=\"OrgXref.%s\"/>" id
)
1024 (format "\n<text:bookmark text:name=\"%s\"/>" id
) text
1025 (format "\n<text:bookmark-end text:name=\"OrgXref.%s\"/>" id
))))
1029 (defun org-e-odt--textbox (text width height style
&optional
1032 (format "\n<draw:text-box %s>%s\n</draw:text-box>"
1033 (concat (format " fo:min-height=\"%0.2fcm\"" (or height
.2))
1035 (format " fo:min-width=\"%0.2fcm\"" (or width
.2))))
1037 width nil style extra anchor-type
))
1041 ;;;; Table of Contents
1043 (defun org-e-odt-begin-toc (index-title depth
)
1046 <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
1047 <text:table-of-content-source text:outline-level=\"%d\">
1048 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
1049 " depth index-title
)
1051 (let ((levels (number-sequence 1 10)))
1056 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
1057 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
1058 <text:index-entry-chapter/>
1059 <text:index-entry-text/>
1060 <text:index-entry-link-end/>
1061 </text:table-of-content-entry-template>
1062 " level level
)) levels
""))
1065 </text:table-of-content-source>
1068 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
1069 <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
1073 (defun org-e-odt-end-toc ()
1076 </text:table-of-content>
1081 (defun* org-e-odt-format-toc-headline
1082 (todo todo-type priority text tags
1083 &key level section-number headline-label
&allow-other-keys
)
1085 (and org-export-with-section-numbers
1086 (concat section-number
". "))
1091 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1094 (setq text
(format "<text:span text:style-name=\"%s\">%s</text:span>"
1096 (format "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
1097 headline-label text
))
1099 (defun org-e-odt-toc (depth info
)
1100 (assert (wholenump depth
))
1101 (let* ((title (org-export-translate "Table of Contents" :utf-8 info
))
1102 (headlines (org-export-collect-headlines
1103 info
(and (wholenump depth
) depth
))))
1106 (org-e-odt-begin-toc title depth
)
1109 (let* ((entry (org-e-odt-format-headline--wrap
1110 headline info
'org-e-odt-format-toc-headline
))
1111 (level (org-export-get-relative-level headline info
))
1112 (style (format "Contents_20_%d" level
)))
1113 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1116 (org-e-odt-end-toc)))))
1119 ;;;; Document styles
1121 (defun org-e-odt-add-automatic-style (object-type &optional object-props
)
1122 "Create an automatic style of type OBJECT-TYPE with param OBJECT-PROPS.
1123 OBJECT-PROPS is (typically) a plist created by passing
1124 \"#+ATTR_ODT: \" option of the object in question to
1125 `org-e-odt-parse-block-attributes'.
1127 Use `org-e-odt-object-counters' to generate an automatic
1128 OBJECT-NAME and STYLE-NAME. If OBJECT-PROPS is non-nil, add a
1129 new entry in `org-e-odt-automatic-styles'. Return (OBJECT-NAME
1131 (assert (stringp object-type
))
1132 (let* ((object (intern object-type
))
1134 (seqno (1+ (or (plist-get org-e-odt-object-counters seqvar
) 0)))
1135 (object-name (format "%s%d" object-type seqno
)) style-name
)
1136 (setq org-e-odt-object-counters
1137 (plist-put org-e-odt-object-counters seqvar seqno
))
1139 (setq style-name
(format "Org%s" object-name
))
1140 (setq org-e-odt-automatic-styles
1141 (plist-put org-e-odt-automatic-styles object
1142 (append (list (list style-name object-props
))
1143 (plist-get org-e-odt-automatic-styles object
)))))
1144 (cons object-name style-name
)))
1148 (defun org-e-odt--checkbox (item)
1149 "Return check-box string associated to ITEM."
1150 (let ((checkbox (org-element-property :checkbox item
)))
1151 (if (not checkbox
) ""
1152 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1153 "OrgCode" (case checkbox
1154 (on "[✓] ") ; CHECK MARK
1160 (defun org-e-odt-template (contents info
)
1161 "Return complete document string after HTML conversion.
1162 CONTENTS is the transcoded contents string. RAW-DATA is the
1163 original parsed data. INFO is a plist holding export options."
1165 (let ((title (org-export-data (plist-get info
:title
) info
))
1166 (author (let ((author (plist-get info
:author
)))
1167 (if (not author
) "" (org-export-data author info
))))
1168 (date (org-e-odt--date
1169 (org-export-data (plist-get info
:date
) info
)))
1170 (email (plist-get info
:email
))
1171 (keywords (plist-get info
:keywords
))
1172 (description (plist-get info
:description
)))
1175 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
1176 <office:document-meta
1177 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
1178 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
1179 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
1180 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
1181 xmlns:ooo=\"http://openoffice.org/2004/office\"
1182 office:version=\"1.2\">
1184 (format "<dc:creator>%s</dc:creator>\n" author
)
1185 (format "<meta:initial-creator>%s</meta:initial-creator>\n" author
)
1186 (format "<dc:date>%s</dc:date>\n" date
)
1187 (format "<meta:creation-date>%s</meta:creation-date>\n" date
)
1188 (format "<meta:generator>%s</meta:generator>\n"
1189 (let ((creator-info (plist-get info
:with-creator
)))
1190 (if (or (not creator-info
) (eq creator-info
'comment
)) ""
1191 (plist-get info
:creator
))))
1192 (format "<meta:keyword>%s</meta:keyword>\n" keywords
)
1193 (format "<dc:subject>%s</dc:subject>\n" description
)
1194 (format "<dc:title>%s</dc:title>\n" title
)
1196 " </office:meta>\n" "</office:document-meta>")
1197 nil
(concat org-e-odt-zip-dir
"meta.xml"))
1198 ;; Add meta.xml in to manifest.
1199 (org-e-odt-create-manifest-file-entry "text/xml" "meta.xml"))
1201 ;; Update styles file.
1202 ;; Copy styles.xml. Also dump htmlfontify styles, if there is any.
1203 ;; Write styles file.
1204 (let* ((styles-file (plist-get info
:odt-styles-file
))
1205 (styles-file (and styles-file
(read (org-trim styles-file
))))
1206 ;; Non-availability of styles.xml is not a critical
1207 ;; error. For now throw an error purely for aesthetic
1209 (styles-file (or styles-file
1210 org-e-odt-styles-file
1211 (expand-file-name "OrgOdtStyles.xml"
1212 org-e-odt-styles-dir
)
1213 (error "org-e-odt: Missing styles file?"))))
1215 ((listp styles-file
)
1216 (let ((archive (nth 0 styles-file
))
1217 (members (nth 1 styles-file
)))
1218 (org-e-odt--zip-extract archive members org-e-odt-zip-dir
)
1221 (when (org-file-image-p member
)
1222 (let* ((image-type (file-name-extension member
))
1223 (media-type (format "image/%s" image-type
)))
1224 (org-e-odt-create-manifest-file-entry media-type member
))))
1226 ((and (stringp styles-file
) (file-exists-p styles-file
))
1227 (let ((styles-file-type (file-name-extension styles-file
)))
1229 ((string= styles-file-type
"xml")
1230 (copy-file styles-file
(concat org-e-odt-zip-dir
"styles.xml") t
))
1231 ((member styles-file-type
'("odt" "ott"))
1232 (org-e-odt--zip-extract styles-file
"styles.xml" org-e-odt-zip-dir
)))))
1234 (error (format "Invalid specification of styles.xml file: %S"
1235 org-e-odt-styles-file
))))
1237 ;; create a manifest entry for styles.xml
1238 (org-e-odt-create-manifest-file-entry "text/xml" "styles.xml")
1240 ;; FIXME: Who is opening an empty styles.xml before this point?
1241 (with-current-buffer
1242 (find-file-noselect (concat org-e-odt-zip-dir
"styles.xml") t
)
1245 ;; Write custom styles for source blocks
1246 ;; Save STYLES used for colorizing of source blocks.
1247 ;; Update styles.xml with styles that were collected as part of
1248 ;; `org-e-odt-hfy-face-to-css' callbacks.
1249 (let ((styles (mapconcat (lambda (style) (format " %s\n" (cddr style
)))
1250 hfy-user-sheet-assoc
"")))
1252 (goto-char (point-min))
1253 (when (re-search-forward "</office:styles>" nil t
)
1254 (goto-char (match-beginning 0))
1255 (insert "\n<!-- Org Htmlfontify Styles -->\n" styles
"\n"))))
1257 ;; Update styles.xml - take care of outline numbering
1259 ;; Don't make automatic backup of styles.xml file. This setting
1260 ;; prevents the backed-up styles.xml file from being zipped in to
1261 ;; odt file. This is more of a hackish fix. Better alternative
1262 ;; would be to fix the zip command so that the output odt file
1263 ;; includes only the needed files and excludes any auto-generated
1264 ;; extra files like backups and auto-saves etc etc. Note that
1265 ;; currently the zip command zips up the entire temp directory so
1266 ;; that any auto-generated files created under the hood ends up in
1267 ;; the resulting odt file.
1268 (set (make-local-variable 'backup-inhibited
) t
)
1270 ;; Outline numbering is retained only upto LEVEL.
1271 ;; To disable outline numbering pass a LEVEL of 0.
1273 (goto-char (point-min))
1275 "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
1277 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1278 (while (re-search-forward regex nil t
)
1279 (unless (let ((sec-num (plist-get info
:section-numbers
))
1280 (level (string-to-number (match-string 2))))
1281 (if (wholenump sec-num
) (<= level sec-num
) sec-num
))
1282 (replace-match replacement t nil
))))
1284 ;; Update content.xml.
1286 (insert-file-contents
1287 (or org-e-odt-content-template-file
1288 (expand-file-name "OrgOdtContentTemplate.xml"
1289 org-e-odt-styles-dir
)))
1290 ;; Write automatic styles.
1291 ;; - Position the cursor.
1292 (goto-char (point-min))
1293 (re-search-forward " </office:automatic-styles>" nil t
)
1294 (goto-char (match-beginning 0))
1295 ;; - Dump automatic table styles
1296 (loop for
(style-name props
) in
1297 (plist-get org-e-odt-automatic-styles
'Table
) do
1298 (when (setq props
(or (plist-get props
:rel-width
) 96))
1299 (insert (format org-e-odt-table-style-format style-name props
))))
1300 ;; Update display level.
1301 ;; - Remove existing sequence decls. Also position the cursor.
1302 (goto-char (point-min))
1303 (when (re-search-forward "<text:sequence-decls" nil t
)
1304 (delete-region (match-beginning 0)
1305 (re-search-forward "</text:sequence-decls>" nil nil
)))
1306 ;; Update sequence decls according to user preference.
1309 "\n<text:sequence-decls>\n%s\n</text:sequence-decls>"
1313 "<text:sequence-decl text:display-outline-level=\"%d\" text:name=\"%s\"/>"
1314 org-e-odt-display-outline-level
(nth 1 x
)))
1315 org-e-odt-category-map-alist
"\n")))
1316 ;; Position the cursor to document body.
1317 (goto-char (point-min))
1318 (re-search-forward "</office:text>" nil nil
)
1319 (goto-char (match-beginning 0))
1321 ;; Preamble - Title, Author, Date etc.
1323 (let* ((title (org-export-data (plist-get info
:title
) info
))
1324 (author (and (plist-get info
:with-author
)
1325 (let ((auth (plist-get info
:author
)))
1326 (and auth
(org-export-data auth info
)))))
1327 (date (org-export-data (plist-get info
:date
) info
))
1328 (iso-date (org-e-odt--date date
))
1329 (date (org-e-odt--date date
"%d %b %Y"))
1330 (email (plist-get info
:email
))
1331 ;; switch on or off above vars based on user settings
1332 (author (and (plist-get info
:with-author
) (or author email
)))
1333 ;; (date (and (plist-get info :time-stamp-file) date))
1334 (email (and (plist-get info
:with-email
) email
)))
1339 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1340 "OrgTitle" (format "\n<text:title>%s</text:title>" title
))
1342 "\n<text:p text:style-name=\"OrgTitle\"/>"))
1344 ((and author
(not email
))
1347 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1349 (format "<text:initial-creator>%s</text:initial-creator>" author
))
1351 "\n<text:p text:style-name=\"OrgSubtitle\"/>"))
1356 "\n<text:p text:style-name=\"%s\">%s</text:p>"
1359 "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
1360 (concat "mailto:" email
)
1361 (format "<text:initial-creator>%s</text:initial-creator>" author
)))
1363 "\n<text:p text:style-name=\"OrgSubtitle\"/>")))
1368 "\n<text:p text:style-name=\"%s\">%s</text:p>"
1371 "\n<text:date style:data-style-name=\"%s\" text:date-value=\"%s\">%s</text:date>"
1373 "OrgDate" iso-date date
))
1375 "<text:p text:style-name=\"OrgSubtitle\"/>")))))
1376 ;; Table of Contents
1377 (let* ((with-toc (plist-get info
:with-toc
))
1378 (depth (and with-toc
(if (wholenump with-toc
)
1380 (plist-get info
:headline-levels
)))))
1381 (when depth
(insert (or (org-e-odt-toc depth info
) ""))))
1385 (buffer-substring-no-properties (point-min) (point-max))))
1389 ;;; Transcode Functions
1393 (defun org-e-odt-bold (bold contents info
)
1394 "Transcode BOLD from Org to ODT.
1395 CONTENTS is the text with bold markup. INFO is a plist holding
1396 contextual information."
1397 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1403 (defun org-e-odt-center-block (center-block contents info
)
1404 "Transcode a CENTER-BLOCK element from Org to ODT.
1405 CONTENTS holds the contents of the center block. INFO is a plist
1406 holding contextual information."
1412 (defun org-e-odt-clock (clock contents info
)
1413 "Transcode a CLOCK element from Org to ODT.
1414 CONTENTS is nil. INFO is a plist used as a communication
1416 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1417 "OrgTimestampWrapper"
1419 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1420 "OrgTimestampKeyword" org-clock-string
)
1421 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1423 (concat (org-translate-time
1424 (org-element-property :value clock
))
1425 (let ((time (org-element-property :time clock
)))
1426 (and time
(format " (%s)" time
))))))))
1431 (defun org-e-odt-code (code contents info
)
1432 "Transcode a CODE object from Org to ODT.
1433 CONTENTS is nil. INFO is a plist used as a communication
1435 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1436 "OrgCode" (org-element-property :value code
)))
1441 ;; Comments are ignored.
1446 ;; Comment Blocks are ignored.
1451 (defun org-e-odt-drawer (drawer contents info
)
1452 "Transcode a DRAWER element from Org to ODT.
1453 CONTENTS holds the contents of the block. INFO is a plist
1454 holding contextual information."
1455 (let* ((name (org-element-property :drawer-name drawer
))
1456 (output (if (functionp org-e-odt-format-drawer-function
)
1457 (funcall org-e-odt-format-drawer-function
1459 ;; If there's no user defined function: simply
1460 ;; display contents of the drawer.
1467 (defun org-e-odt-dynamic-block (dynamic-block contents info
)
1468 "Transcode a DYNAMIC-BLOCK element from Org to ODT.
1469 CONTENTS holds the contents of the block. INFO is a plist
1470 holding contextual information. See `org-export-data'."
1476 (defun org-e-odt-entity (entity contents info
)
1477 "Transcode an ENTITY object from Org to ODT.
1478 CONTENTS are the definition itself. INFO is a plist holding
1479 contextual information."
1480 ;; (let ((ent (org-element-property :latex entity)))
1481 ;; (if (org-element-property :latex-math-p entity)
1482 ;; (format "$%s$" ent)
1484 (org-element-property :utf-8 entity
))
1489 (defun org-e-odt-example-block (example-block contents info
)
1490 "Transcode a EXAMPLE-BLOCK element from Org to ODT.
1491 CONTENTS is nil. INFO is a plist holding contextual information."
1492 (org-e-odt-format-code example-block info
))
1497 (defun org-e-odt-export-snippet (export-snippet contents info
)
1498 "Transcode a EXPORT-SNIPPET object from Org to ODT.
1499 CONTENTS is nil. INFO is a plist holding contextual information."
1500 (when (eq (org-export-snippet-backend export-snippet
) 'e-odt
)
1501 (org-element-property :value export-snippet
)))
1506 (defun org-e-odt-export-block (export-block contents info
)
1507 "Transcode a EXPORT-BLOCK element from Org to ODT.
1508 CONTENTS is nil. INFO is a plist holding contextual information."
1509 (when (string= (org-element-property :type export-block
) "ODT")
1510 (org-remove-indentation (org-element-property :value export-block
))))
1515 (defun org-e-odt-fixed-width (fixed-width contents info
)
1516 "Transcode a FIXED-WIDTH element from Org to ODT.
1517 CONTENTS is nil. INFO is a plist holding contextual information."
1518 (org-e-odt-do-format-code (org-element-property :value fixed-width
)))
1521 ;;;; Footnote Definition
1523 ;; Footnote Definitions are ignored.
1526 ;;;; Footnote Reference
1528 (defun org-e-odt-footnote-reference (footnote-reference contents info
)
1529 "Transcode a FOOTNOTE-REFERENCE element from Org to ODT.
1530 CONTENTS is nil. INFO is a plist holding contextual information."
1531 (let ((--format-footnote-definition
1534 (setq n
(format "%d" n
))
1535 (let ((id (concat "fn" n
))
1536 (note-class "footnote")
1537 (par-style "Footnote"))
1539 "<text:note text:id=\"%s\" text:note-class=\"%s\">%s</text:note>"
1542 (format "<text:note-citation>%s</text:note-citation>" n
)
1543 (format "<text:note-body>%s</text:note-body>" def
)))))))
1544 (--format-footnote-reference
1547 (setq n
(format "%d" n
))
1548 (let ((note-class "footnote")
1550 (ref-name (concat "fn" n
)))
1552 "<text:span text:style-name=\"%s\">%s</text:span>"
1554 (format "<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:note-ref>"
1555 note-class ref-format ref-name n
)))))))
1557 ;; Insert separator between two footnotes in a row.
1558 (let ((prev (org-export-get-previous-element footnote-reference info
)))
1559 (and (eq (org-element-type prev
) 'footnote-reference
)
1560 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1561 "OrgSuperscript" ",")))
1562 ;; Trancode footnote reference.
1563 (let ((n (org-export-get-footnote-number footnote-reference info
)))
1565 ((not (org-export-footnote-first-reference-p footnote-reference info
))
1566 (funcall --format-footnote-reference n
))
1567 ;; Inline definitions are secondary strings.
1568 ;; Non-inline footnotes definitions are full Org data.
1570 (let* ((raw (org-export-get-footnote-definition footnote-reference
1572 (def (let ((def (org-trim (org-export-data raw info
))))
1573 (if (eq (org-element-type raw
) 'org-data
) def
1574 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1576 (funcall --format-footnote-definition n def
))))))))
1581 (defun* org-e-odt-format-headline
1582 (todo todo-type priority text tags
1583 &key level section-number headline-label
&allow-other-keys
)
1588 (let ((style (if (member todo org-done-keywords
) "OrgDone" "OrgTodo")))
1589 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1595 (concat "<text:tab/>"
1596 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1597 "OrgTag" (mapconcat 'org-trim tags
" : "))))))
1599 (defun org-e-odt-format-headline--wrap (headline info
1600 &optional format-function
1602 "Transcode an HEADLINE element from Org to ODT.
1603 CONTENTS holds the contents of the headline. INFO is a plist
1604 holding contextual information."
1605 (let* ((level (+ (org-export-get-relative-level headline info
)))
1606 (headline-number (org-export-get-headline-number headline info
))
1607 (section-number (and (org-export-numbered-headline-p headline info
)
1608 (mapconcat 'number-to-string
1609 headline-number
".")))
1610 (todo (and (plist-get info
:with-todo-keywords
)
1611 (let ((todo (org-element-property :todo-keyword headline
)))
1612 (and todo
(org-export-data todo info
)))))
1613 (todo-type (and todo
(org-element-property :todo-type headline
)))
1614 (priority (and (plist-get info
:with-priority
)
1615 (org-element-property :priority headline
)))
1616 (text (org-export-data (org-element-property :title headline
) info
))
1617 (tags (and (plist-get info
:with-tags
)
1618 (org-export-get-tags headline info
)))
1619 (headline-label (concat "sec-" (mapconcat 'number-to-string
1620 headline-number
"-")))
1621 (format-function (cond
1622 ((functionp format-function
) format-function
)
1623 ((functionp org-e-odt-format-headline-function
)
1625 (lambda (todo todo-type priority text tags
1627 (funcall org-e-odt-format-headline-function
1628 todo todo-type priority text tags
))))
1629 (t 'org-e-odt-format-headline
))))
1630 (apply format-function
1631 todo todo-type priority text tags
1632 :headline-label headline-label
:level level
1633 :section-number section-number extra-keys
)))
1635 (defun org-e-odt-headline (headline contents info
)
1636 "Transcode an HEADLINE element from Org to ODT.
1637 CONTENTS holds the contents of the headline. INFO is a plist
1638 holding contextual information."
1639 ;; Case 1: This is a footnote section: ignore it.
1640 (unless (org-element-property :footnote-section-p headline
)
1641 (let* ((text (org-export-data (org-element-property :title headline
) info
))
1642 ;; Create the headline text.
1643 (full-text (org-e-odt-format-headline--wrap headline info
))
1644 ;; Get level relative to current parsed data.
1645 (level (org-export-get-relative-level headline info
))
1646 ;; Get canonical label for the headline.
1647 (id (concat "sec-" (mapconcat 'number-to-string
1648 (org-export-get-headline-number
1649 headline info
) "-")))
1650 ;; Get user-specified labels for the headline.
1651 (extra-ids (list (org-element-property :custom-id headline
)
1652 (org-element-property :id headline
)))
1655 (mapconcat (lambda (x)
1657 (let ((x (if (org-uuidgen-p x
) (concat "ID-" x
) x
)))
1659 "" (org-export-solidify-link-text x
)))))
1662 (anchored-title (org-e-odt--target full-text id
)))
1664 ;; Case 2. This is a deep sub-tree: export it as a list item.
1665 ;; Also export as items headlines for which no section
1666 ;; format has been found.
1667 ((org-export-low-level-p headline info
)
1668 ;; Build the real contents of the sub-tree.
1670 (and (org-export-first-sibling-p headline info
)
1671 (format "\n<text:list text:style-name=\"%s\" %s>"
1672 ;; Choose style based on list type.
1673 (if (org-export-numbered-headline-p headline info
)
1674 "OrgNumberedList" "OrgBulletedList")
1675 ;; If top-level list, re-start numbering. Otherwise,
1676 ;; continue numbering.
1677 (format "text:continue-numbering=\"%s\""
1678 (let* ((parent (org-export-get-parent-headline
1681 (org-export-low-level-p parent info
))
1683 (let ((headline-has-table-p
1684 (let ((section (assq 'section
(org-element-contents headline
))))
1685 (assq 'table
(and section
(org-element-contents section
))))))
1686 (format "\n<text:list-item>\n%s\n%s"
1688 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1690 (concat extra-targets anchored-title
))
1692 (if headline-has-table-p
1693 "</text:list-header>"
1694 "</text:list-item>")))
1695 (and (org-export-last-sibling-p headline info
)
1697 ;; Case 3. Standard headline. Export it as a section.
1701 "\n<text:h text:style-name=\"%s\" text:outline-level=\"%s\">%s</text:h>"
1702 (format "Heading_20_%s" level
)
1704 (concat extra-targets anchored-title
))
1708 ;;;; Horizontal Rule
1710 (defun org-e-odt-horizontal-rule (horizontal-rule contents info
)
1711 "Transcode an HORIZONTAL-RULE object from Org to ODT.
1712 CONTENTS is nil. INFO is a plist holding contextual information."
1713 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1714 "Horizontal_20_Line" ""))
1717 ;;;; Inline Babel Call
1719 ;; Inline Babel Calls are ignored.
1722 ;;;; Inline Src Block
1724 (defun org-e-odt--find-verb-separator (s)
1725 "Return a character not used in string S.
1726 This is used to choose a separator for constructs like \\verb."
1727 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1728 (loop for c across ll
1729 when
(not (string-match (regexp-quote (char-to-string c
)) s
))
1730 return
(char-to-string c
))))
1732 (defun org-e-odt-inline-src-block (inline-src-block contents info
)
1733 "Transcode an INLINE-SRC-BLOCK element from Org to ODT.
1734 CONTENTS holds the contents of the item. INFO is a plist holding
1735 contextual information."
1736 (let* ((org-lang (org-element-property :language inline-src-block
))
1737 (code (org-element-property :value inline-src-block
))
1738 (separator (org-e-odt--find-verb-separator code
)))
1744 (defun org-e-odt-inlinetask (inlinetask contents info
)
1745 "Transcode an INLINETASK element from Org to ODT.
1746 CONTENTS holds the contents of the block. INFO is a plist
1747 holding contextual information."
1749 ;; If `org-e-odt-format-inlinetask-function' is provided, call it
1750 ;; with appropriate arguments.
1751 ((functionp org-e-odt-format-inlinetask-function
)
1752 (let ((format-function
1754 (lambda (todo todo-type priority text tags
1755 &key contents
&allow-other-keys
)
1756 (funcall org-e-odt-format-inlinetask-function
1757 todo todo-type priority text tags contents
)))))
1758 (org-e-odt-format-headline--wrap
1759 inlinetask info format-function
:contents contents
)))
1760 ;; Otherwise, use a default template.
1762 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1766 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1767 "OrgInlineTaskHeading"
1768 (org-e-odt-format-headline--wrap
1771 nil nil
"OrgInlineTaskFrame" " style:rel-width=\"100%\"")))))
1775 (defun org-e-odt-italic (italic contents info
)
1776 "Transcode ITALIC from Org to ODT.
1777 CONTENTS is the text with italic markup. INFO is a plist holding
1778 contextual information."
1779 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1780 "Emphasis" contents
))
1785 (defun org-e-odt-item (item contents info
)
1786 "Transcode an ITEM element from Org to ODT.
1787 CONTENTS holds the contents of the item. INFO is a plist holding
1788 contextual information."
1789 (let* ((plain-list (org-export-get-parent item
))
1790 (type (org-element-property :type plain-list
))
1791 (counter (org-element-property :counter item
))
1792 (tag (let ((tag (org-element-property :tag item
)))
1794 (concat (org-e-odt--checkbox item
)
1795 (org-export-data tag info
))))))
1797 ((ordered unordered descriptive-1 descriptive-2
)
1798 (format "\n<text:list-item>\n%s\n%s"
1800 (let* ((--element-has-a-table-p
1802 (lambda (element info
)
1803 (loop for el in
(org-element-contents element
)
1804 thereis
(eq (org-element-type el
) 'table
))))))
1806 ((funcall --element-has-a-table-p item info
)
1807 "</text:list-header>")
1808 (t "</text:list-item>")))))
1809 (t (error "Unknown list type: %S" type
)))))
1813 (defun org-e-odt-keyword (keyword contents info
)
1814 "Transcode a KEYWORD element from Org to ODT.
1815 CONTENTS is nil. INFO is a plist holding contextual information."
1816 (let ((key (org-element-property :key keyword
))
1817 (value (org-element-property :value keyword
)))
1819 ((string= key
"ODT") value
)
1820 ((string= key
"INDEX") (format "\\index{%s}" value
))
1821 ((string= key
"TARGET") nil
; FIXME
1822 ;; (format "\\label{%s}" (org-export-solidify-link-text value))
1824 ((string= key
"toc")
1825 (let ((value (downcase value
)))
1827 ((string-match "\\<headlines\\>" value
)
1828 (let ((depth (or (and (string-match "[0-9]+" value
)
1829 (string-to-number (match-string 0 value
)))
1830 (plist-get info
:with-toc
))))
1831 (when (wholenump depth
) (org-e-odt-toc depth info
))))
1832 ((string= "tables" value
) "FIXME")
1833 ((string= "figures" value
) "FIXME")
1834 ((string= "listings" value
)
1836 ;; At the moment, src blocks with a caption are wrapped
1837 ;; into a figure environment.
1841 ;;;; Latex Environment
1844 (eval-after-load 'org-odt
1845 '(ad-deactivate 'org-format-latex-as-mathml
))
1847 ;; (defadvice org-format-latex-as-mathml ; FIXME
1848 ;; (after org-e-odt-protect-latex-fragment activate)
1849 ;; "Encode LaTeX fragment as XML.
1850 ;; Do this when translation to MathML fails."
1851 ;; (when (or (not (> (length ad-return-value) 0))
1852 ;; (get-text-property 0 'org-protected ad-return-value))
1853 ;; (setq ad-return-value
1854 ;; (org-propertize (org-e-odt--encode-plain-text (ad-get-arg 0))
1855 ;; 'org-protected t))))
1857 (defun org-e-odt-latex-environment (latex-environment contents info
)
1858 "Transcode a LATEX-ENVIRONMENT element from Org to ODT.
1859 CONTENTS is nil. INFO is a plist holding contextual information."
1860 (let* ((latex-frag (org-remove-indentation
1861 (org-element-property :value latex-environment
))))
1862 (org-e-odt-do-format-code latex-frag
)))
1867 ;; (when latex-frag ; FIXME
1868 ;; (setq href (org-propertize href :title "LaTeX Fragment"
1869 ;; :description latex-frag)))
1871 ;; provide descriptions
1873 (defun org-e-odt-latex-fragment (latex-fragment contents info
)
1874 "Transcode a LATEX-FRAGMENT object from Org to ODT.
1875 CONTENTS is nil. INFO is a plist holding contextual information."
1876 (let* ((latex-frag (org-element-property :value latex-fragment
))
1877 (processing-type (plist-get info
:LaTeX-fragments
)))
1878 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1879 "OrgCode" (org-e-odt--encode-plain-text latex-frag t
))))
1884 (defun org-e-odt-line-break (line-break contents info
)
1885 "Transcode a LINE-BREAK object from Org to ODT.
1886 CONTENTS is nil. INFO is a plist holding contextual information."
1887 "<text:line-break/>\n")
1892 ;;;; Links :: Label references
1894 (defun org-e-odt--enumerate (element info
&optional predicate n
)
1895 (when predicate
(assert (funcall predicate element info
)))
1896 (let* ((--numbered-parent-headline-at-<=-n
1898 (lambda (element n info
)
1899 (loop for x in
(org-export-get-genealogy element
)
1900 thereis
(and (eq (org-element-type x
) 'headline
)
1901 (<= (org-export-get-relative-level x info
) n
)
1902 (org-export-numbered-headline-p x info
)
1906 (lambda (element scope info
&optional predicate
)
1909 (or scope
(plist-get info
:parse-tree
))
1910 (org-element-type element
)
1912 (and (or (not predicate
) (funcall predicate el info
))
1916 info
'first-match
)))))
1917 (scope (funcall --numbered-parent-headline-at-
<=-n
1918 element
(or n org-e-odt-display-outline-level
) info
))
1919 (ordinal (funcall --enumerate element scope info predicate
))
1924 (mapconcat 'number-to-string
1925 (org-export-get-headline-number scope info
) "."))
1929 (number-to-string ordinal
))))
1932 (defun org-e-odt-format-label (element info op
)
1933 (assert (memq (org-element-type element
) '(link table src-block paragraph
)))
1934 (let* ((caption-from
1935 (case (org-element-type element
)
1936 (link (org-export-get-parent-element element
))
1938 ;; Get label and caption.
1939 (label (org-element-property :name caption-from
))
1940 (caption (org-export-get-caption caption-from
))
1941 (short-caption (org-export-get-caption caption-from t
))
1942 ;; Transcode captions.
1943 (caption (and caption
(org-export-data caption info
)))
1944 (short-caption (and short-caption
1945 (org-export-data short-caption info
))))
1946 (when (or label caption
)
1947 (let* ((default-category
1948 (case (org-element-type element
)
1950 (src-block "__Listing__")
1953 ((org-e-odt--enumerable-latex-image-p element info
)
1955 ((org-e-odt--enumerable-image-p element info
)
1957 ((org-e-odt--enumerable-formula-p element info
)
1959 (t (error "Don't know how to format label for link: %S"
1961 (t (error "Don't know how to format label for element type: %s"
1962 (org-element-type element
)))))
1964 (assert default-category
)
1965 (destructuring-bind (counter label-style category predicate
)
1966 (assoc-default default-category org-e-odt-category-map-alist
)
1967 ;; Compute sequence number of the element.
1968 (setq seqno
(org-e-odt--enumerate element info predicate
))
1969 ;; Localize category string.
1970 (setq category
(org-export-translate category
:utf-8 info
))
1972 ;; Case 1: Handle Label definition.
1974 ;; Assign an internal label, if user has not provided one
1975 (setq label
(or label
(format "%s-%s" default-category seqno
)))
1976 (setq label
(org-export-solidify-link-text label
))
1979 (cadr (assoc-string label-style org-e-odt-label-styles t
))
1982 "<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">%s</text:sequence>"
1983 label counter counter seqno
))
1984 (?c .
,(or caption
""))))
1986 ;; Case 2: Handle Label reference.
1989 (setq label
(org-export-solidify-link-text label
))
1990 (let* ((fmt (cddr (assoc-string label-style org-e-odt-label-styles t
)))
1993 (format "<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:sequence-ref>"
1994 fmt1 label
(format-spec fmt2
`((?e .
,category
)
1996 (t (error "Unknow %S on label" op
))))))))
1999 ;;;; Links :: Inline Images
2001 (defun org-e-odt--copy-image-file (path)
2002 "Returns the internal name of the file"
2003 (let* ((image-type (file-name-extension path
))
2004 (media-type (format "image/%s" image-type
))
2005 (target-dir "Images/")
2007 (format "%s%04d.%s" target-dir
2008 (incf org-e-odt-embedded-images-count
) image-type
)))
2009 (message "Embedding %s as %s ..."
2010 (substring-no-properties path
) target-file
)
2012 (when (= 1 org-e-odt-embedded-images-count
)
2013 (make-directory (concat org-e-odt-zip-dir target-dir
))
2014 (org-e-odt-create-manifest-file-entry "" target-dir
))
2016 (copy-file path
(concat org-e-odt-zip-dir target-file
) 'overwrite
)
2017 (org-e-odt-create-manifest-file-entry media-type target-file
)
2020 (defun org-e-odt--image-size (file &optional user-width
2021 user-height scale dpi embed-as
)
2022 (let* ((--pixels-to-cms
2023 (function (lambda (pixels dpi
)
2024 (let ((cms-per-inch 2.54)
2025 (inches (/ pixels dpi
)))
2026 (* cms-per-inch inches
)))))
2029 (lambda (size-in-pixels dpi
)
2031 (cons (funcall --pixels-to-cms
(car size-in-pixels
) dpi
)
2032 (funcall --pixels-to-cms
(cdr size-in-pixels
) dpi
))))))
2033 (dpi (or dpi org-e-odt-pixels-per-inch
))
2034 (anchor-type (or embed-as
"paragraph"))
2035 (user-width (and (not scale
) user-width
))
2036 (user-height (and (not scale
) user-height
))
2039 (not (and user-height user-width
))
2042 (and (executable-find "identify")
2043 (let ((size-in-pixels
2044 (let ((dim (shell-command-to-string
2045 (format "identify -format \"%%w:%%h\" \"%s\""
2047 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim
)
2048 (cons (string-to-number (match-string 1 dim
))
2049 (string-to-number (match-string 2 dim
)))))))
2050 (funcall --size-in-cms size-in-pixels dpi
)))
2052 (let ((size-in-pixels
2053 (ignore-errors ; Emacs could be in batch mode
2055 (image-size (create-image file
) 'pixels
))))
2056 (funcall --size-in-cms size-in-pixels dpi
))
2057 ;; Use hard-coded values.
2058 (cdr (assoc-string anchor-type
2059 org-e-odt-default-image-sizes-alist
))
2061 (error "Cannot determine image size, aborting"))))
2062 (width (car size
)) (height (cdr size
)))
2065 (setq width
(* width scale
) height
(* height scale
)))
2066 ((and user-height user-width
)
2067 (setq width user-width height user-height
))
2069 (setq width
(* user-height
(/ width height
)) height user-height
))
2071 (setq height
(* user-width
(/ height width
)) width user-width
))
2073 ;; ensure that an embedded image fits comfortably within a page
2074 (let ((max-width (car org-e-odt-max-image-size
))
2075 (max-height (cdr org-e-odt-max-image-size
)))
2076 (when (or (> width max-width
) (> height max-height
))
2077 (let* ((scale1 (/ max-width width
))
2078 (scale2 (/ max-height height
))
2079 (scale (min scale1 scale2
)))
2080 (setq width
(* scale width
) height
(* scale height
)))))
2081 (cons width height
)))
2083 (defun org-e-odt-link--inline-image (element info
)
2084 "Return HTML code for an inline image.
2085 LINK is the link pointing to the inline image. INFO is a plist
2086 used as a communication channel."
2087 (assert (eq (org-element-type element
) 'link
))
2088 (let* ((src (let* ((type (org-element-property :type element
))
2089 (raw-path (org-element-property :path element
)))
2090 (cond ((member type
'("http" "https"))
2091 (concat type
":" raw-path
))
2092 ((file-name-absolute-p raw-path
)
2093 (expand-file-name raw-path
))
2095 (src-expanded (if (file-name-absolute-p src
) src
2096 (expand-file-name src
(file-name-directory
2097 (plist-get info
:input-file
)))))
2099 "\n<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>"
2100 (org-e-odt--copy-image-file src-expanded
)))
2101 ;; Extract attributes from #+ATTR_ODT line.
2102 (attr-from (case (org-element-type element
)
2103 (link (org-export-get-parent-element element
))
2105 ;; Convert attributes to a plist.
2106 (attr-plist (org-export-read-attribute :attr_odt attr-from
))
2107 ;; Handle `:anchor', `:style' and `:attributes' properties.
2109 (car (assoc-string (plist-get attr-plist
:anchor
)
2110 '(("as-char") ("paragraph") ("page")) t
)))
2112 (and user-frame-anchor
(plist-get attr-plist
:style
)))
2114 (and user-frame-anchor
(plist-get attr-plist
:attributes
)))
2116 (list user-frame-style user-frame-attrs user-frame-anchor
))
2117 ;; (embed-as (or embed-as user-frame-anchor "paragraph"))
2119 ;; handle `:width', `:height' and `:scale' properties.
2120 (size (org-e-odt--image-size
2121 src-expanded
(plist-get attr-plist
:width
)
2122 (plist-get attr-plist
:height
)
2123 (plist-get attr-plist
:scale
) nil
;; embed-as
2126 (width (car size
)) (height (cdr size
))
2127 (embed-as (if (org-e-odt--standalone-link-p element info
) "paragraph"
2129 (captions (org-e-odt-format-label element info
'definition
))
2130 (caption (car captions
)) (short-caption (cdr captions
))
2131 (entity (concat (and caption
"Captioned") embed-as
"Image")))
2132 (org-e-odt--render-image/formula entity href width height
2133 captions user-frame-params
)))
2136 ;;;; Links :: Math formula
2138 (defun org-e-odt-link--inline-formula (element info
)
2139 (let* ((src (let* ((type (org-element-property :type element
))
2140 (raw-path (org-element-property :path element
)))
2142 ((file-name-absolute-p raw-path
)
2143 (expand-file-name raw-path
))
2145 (full-src (if (file-name-absolute-p src
) src
2146 (expand-file-name src
(file-name-directory
2147 (plist-get info
:input-file
)))))
2149 (case (org-element-type element
)
2150 (link (org-export-get-parent-element element
))
2152 (captions (org-e-odt-format-label caption-from info
'definition
))
2153 (caption (car captions
))
2155 (format "\n<draw:object %s xlink:href=\"%s\" xlink:type=\"simple\"/>"
2156 " xlink:show=\"embed\" xlink:actuate=\"onLoad\""
2157 (file-name-directory (org-e-odt--copy-formula-file full-src
))))
2158 (embed-as (if caption
'paragraph
'character
))
2161 ((eq embed-as
'character
)
2162 (org-e-odt--render-image/formula
"InlineFormula" href width height
))
2164 (let* ((equation (org-e-odt--render-image/formula
2165 "CaptionedDisplayFormula" href width height captions
))
2167 (let* ((org-e-odt-category-map-alist
2168 '(("__MathFormula__" "Text" "math-label" "Equation"
2169 org-e-odt--enumerable-formula-p
))))
2170 (car (org-e-odt-format-label caption-from info
'definition
)))))
2171 (concat equation
"<text:tab/>" label
))))))
2173 (defun org-e-odt--copy-formula-file (src-file)
2174 "Returns the internal name of the file"
2175 (let* ((target-dir (format "Formula-%04d/"
2176 (incf org-e-odt-embedded-formulas-count
)))
2177 (target-file (concat target-dir
"content.xml")))
2178 ;; Create a directory for holding formula file. Also enter it in
2180 (make-directory (concat org-e-odt-zip-dir target-dir
))
2181 (org-e-odt-create-manifest-file-entry
2182 "application/vnd.oasis.opendocument.formula" target-dir
"1.2")
2183 ;; Copy over the formula file from user directory to zip
2185 (message "Embedding %s as %s ..." src-file target-file
)
2186 (let ((case-fold-search nil
))
2189 ((string-match "\\.\\(mathml\\|mml\\)\\'" src-file
)
2190 (copy-file src-file
(concat org-e-odt-zip-dir target-file
) 'overwrite
))
2191 ;; Case 2: OpenDocument formula.
2192 ((string-match "\\.odf\\'" src-file
)
2193 (org-e-odt--zip-extract src-file
"content.xml"
2194 (concat org-e-odt-zip-dir target-dir
)))
2195 (t (error "%s is not a formula file" src-file
))))
2196 ;; Enter the formula file in to manifest.
2197 (org-e-odt-create-manifest-file-entry "text/xml" target-file
)
2202 (defun org-e-odt--render-image/formula
(cfg-key href width height
&optional
2203 captions user-frame-params
)
2204 (let* ((frame-cfg-alist
2205 ;; Each element of this alist is of the form (CFG-HANDLE
2206 ;; INNER-FRAME-PARAMS OUTER-FRAME-PARAMS).
2208 ;; CFG-HANDLE is the key to the alist.
2210 ;; INNER-FRAME-PARAMS and OUTER-FRAME-PARAMS specify the
2211 ;; frame params for INNER-FRAME and OUTER-FRAME
2212 ;; respectively. See below.
2214 ;; Configurations that are meant to be applied to
2215 ;; non-captioned image/formula specifies no
2216 ;; OUTER-FRAME-PARAMS.
2220 ;; INNER-FRAME :: Frame that directly surrounds an
2223 ;; OUTER-FRAME :: Frame that encloses the INNER-FRAME. This
2224 ;; frame also contains the caption, if any.
2226 ;; FRAME-PARAMS :: List of the form (FRAME-STYLE-NAME
2227 ;; FRAME-ATTRIBUTES FRAME-ANCHOR). Note
2228 ;; that these are the last three arguments
2229 ;; to `org-e-odt--frame'.
2231 ;; Note that an un-captioned image/formula requires just an
2232 ;; INNER-FRAME, while a captioned image/formula requires
2233 ;; both an INNER and an OUTER-FRAME.
2234 '(("As-CharImage" ("OrgInlineImage" nil
"as-char"))
2235 ("ParagraphImage" ("OrgDisplayImage" nil
"paragraph"))
2236 ("PageImage" ("OrgPageImage" nil
"page"))
2237 ("CaptionedAs-CharImage"
2238 ("OrgCaptionedImage"
2239 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
2240 ("OrgInlineImage" nil
"as-char"))
2241 ("CaptionedParagraphImage"
2242 ("OrgCaptionedImage"
2243 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
2244 ("OrgImageCaptionFrame" nil
"paragraph"))
2245 ("CaptionedPageImage"
2246 ("OrgCaptionedImage"
2247 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
2248 ("OrgPageImageCaptionFrame" nil
"page"))
2249 ("InlineFormula" ("OrgInlineFormula" nil
"as-char"))
2250 ("DisplayFormula" ("OrgDisplayFormula" nil
"as-char"))
2251 ("CaptionedDisplayFormula"
2252 ("OrgCaptionedFormula" nil
"paragraph")
2253 ("OrgFormulaCaptionFrame" nil
"paragraph"))))
2254 (caption (car captions
)) (short-caption (cdr captions
))
2255 ;; Retrieve inner and outer frame params, from configuration.
2256 (frame-cfg (assoc-string cfg-key frame-cfg-alist t
))
2257 (inner (nth 1 frame-cfg
))
2258 (outer (nth 2 frame-cfg
))
2259 ;; User-specified frame params (from #+ATTR_ODT spec)
2260 (user user-frame-params
)
2261 (--merge-frame-params (function
2262 (lambda (default user
)
2263 "Merge default and user frame params."
2264 (if (not user
) default
2265 (assert (= (length default
) 3))
2266 (assert (= (length user
) 3))
2269 collect
(or u d
)))))))
2271 ;; Case 1: Image/Formula has no caption.
2272 ;; There is only one frame, one that surrounds the image
2275 ;; Merge user frame params with that from configuration.
2276 (setq inner
(funcall --merge-frame-params inner user
))
2277 (apply 'org-e-odt--frame href width height inner
))
2278 ;; Case 2: Image/Formula is captioned or labeled.
2279 ;; There are two frames: The inner one surrounds the
2280 ;; image or formula. The outer one contains the
2281 ;; caption/sequence number.
2283 ;; Merge user frame params with outer frame params.
2284 (setq outer
(funcall --merge-frame-params outer user
))
2285 ;; Short caption, if specified, goes as part of inner frame.
2286 (setq inner
(let ((frame-params (copy-sequence inner
)))
2287 (setcar (cdr frame-params
)
2291 (format " draw:name=\"%s\" " short-caption
))))
2293 (apply 'org-e-odt--textbox
2294 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2297 (apply 'org-e-odt--frame href width height inner
)
2299 width height outer
)))))
2301 (defun org-e-odt--enumerable-p (element info
)
2302 ;; Element should have a caption or label.
2303 (or (org-element-property :caption element
)
2304 (org-element-property :name element
)))
2306 (defun org-e-odt--enumerable-image-p (element info
)
2307 (org-e-odt--standalone-link-p
2309 ;; Paragraph should have a caption or label. It SHOULD NOT be a
2310 ;; replacement element. (i.e., It SHOULD NOT be a result of LaTeX
2313 (and (not (org-element-property :replaces p
))
2314 (or (org-element-property :caption p
)
2315 (org-element-property :name p
))))
2316 ;; Link should point to an image file.
2318 (assert (eq (org-element-type l
) 'link
))
2319 (org-export-inline-image-p l org-e-odt-inline-image-rules
))))
2321 (defun org-e-odt--enumerable-latex-image-p (element info
)
2322 (org-e-odt--standalone-link-p
2324 ;; Paragraph should have a caption or label. It SHOULD also be a
2325 ;; replacement element. (i.e., It SHOULD be a result of LaTeX
2328 (and (org-element-property :replaces p
)
2329 (or (org-element-property :caption p
)
2330 (org-element-property :name p
))))
2331 ;; Link should point to an image file.
2333 (assert (eq (org-element-type l
) 'link
))
2334 (org-export-inline-image-p l org-e-odt-inline-image-rules
))))
2336 (defun org-e-odt--enumerable-formula-p (element info
)
2337 (org-e-odt--standalone-link-p
2339 ;; Paragraph should have a caption or label.
2341 (or (org-element-property :caption p
)
2342 (org-element-property :name p
)))
2343 ;; Link should point to a MathML or ODF file.
2345 (assert (eq (org-element-type l
) 'link
))
2346 (org-export-inline-image-p l org-e-odt-inline-formula-rules
))))
2348 (defun org-e-odt--standalone-link-p (element info
&optional
2351 "Test if ELEMENT is a standalone link for the purpose ODT export.
2352 INFO is a plist holding contextual information.
2354 Return non-nil, if ELEMENT is of type paragraph satisfying
2355 PARAGRAPH-PREDICATE and it's sole content, save for whitespaces,
2356 is a link that satisfies LINK-PREDICATE.
2358 Return non-nil, if ELEMENT is of type link satisfying
2359 LINK-PREDICATE and it's containing paragraph satisfies
2360 PARAGRAPH-PREDICATE inaddtion to having no other content save for
2361 leading and trailing whitespaces.
2363 Return nil, otherwise."
2364 (let ((p (case (org-element-type element
)
2366 (link (and (or (not link-predicate
)
2367 (funcall link-predicate element
))
2368 (org-export-get-parent element
)))
2371 (assert (eq (org-element-type p
) 'paragraph
))
2372 (when (or (not paragraph-predicate
)
2373 (funcall paragraph-predicate p
))
2374 (let ((contents (org-element-contents p
)))
2375 (loop for x in contents
2376 with inline-image-count
= 0
2377 always
(case (org-element-type x
)
2379 (not (org-string-nw-p x
)))
2381 (and (or (not link-predicate
)
2382 (funcall link-predicate x
))
2383 (= (incf inline-image-count
) 1)))
2386 (defun org-e-odt-get-previous-elements (blob info
)
2387 (let ((parent (org-export-get-parent blob
)))
2388 (cdr (memq blob
(reverse (org-element-contents parent
))))))
2390 (defun org-e-odt-resolve-numbered-paragraph (element info
)
2391 (when (eq (org-element-type element
) 'item
)
2392 (let ((el element
) ordinal
)
2393 (while (eq (org-element-type el
) 'item
)
2394 (push (1+ (length (org-e-odt-get-previous-elements el info
))) ordinal
)
2395 (setq el
(org-export-get-parent (org-export-get-parent el
))))
2398 (defun org-e-odt-link (link desc info
)
2399 "Transcode a LINK object from Org to ODT.
2401 DESC is the description part of the link, or the empty string.
2402 INFO is a plist holding contextual information. See
2404 (let* ((type (org-element-property :type link
))
2405 (raw-path (org-element-property :path link
))
2406 ;; Ensure DESC really exists, or set it to nil.
2407 (desc (and (not (string= desc
"")) desc
))
2408 (imagep (org-export-inline-image-p
2409 link org-e-odt-inline-image-rules
))
2411 ((member type
'("http" "https" "ftp" "mailto"))
2412 (concat type
":" raw-path
))
2413 ((string= type
"file")
2414 (if (file-name-absolute-p raw-path
)
2415 (concat "file://" (expand-file-name raw-path
))
2416 (concat "file://" raw-path
)))
2421 ((and (not desc
) (org-export-inline-image-p
2422 link org-e-odt-inline-image-rules
))
2423 (org-e-odt-link--inline-image link info
))
2425 ((and (not desc
) (org-export-inline-image-p
2426 link org-e-odt-inline-formula-rules
))
2427 (org-e-odt-link--inline-formula link info
))
2428 ;; Radio target: Transcode target's contents and use them as
2429 ;; link's description.
2430 ((string= type
"radio")
2431 (let ((destination (org-export-resolve-radio-link link info
)))
2433 (let ((desc (org-export-data (org-element-contents destination
) info
))
2434 (href (org-export-solidify-link-text path
)))
2436 "<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
2438 ;; Links pointing to an headline: Find destination and build
2439 ;; appropriate referencing command.
2440 ((member type
'("custom-id" "fuzzy" "id"))
2441 (let ((destination (if (string= type
"fuzzy")
2442 (org-export-resolve-fuzzy-link link info
)
2443 (org-export-resolve-id-link link info
))))
2444 (case (org-element-type destination
)
2445 ;; Fuzzy link points nowhere.
2447 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2448 "Emphasis" (or desc
(org-export-data
2449 (org-element-property
2450 :raw-link link
) info
))))
2451 ;; Fuzzy link points to an invisible target.
2453 ;; LINK points to an headline. Check if LINK should display
2456 (let* ((headline-no (org-export-get-headline-number destination info
))
2457 (label (format "sec-%s" (mapconcat 'number-to-string
2460 ;; Case 1: Headline is numbered and LINK has no
2461 ;; description or LINK's description matches headline's
2462 ;; title. Display section number.
2463 ((and (org-export-numbered-headline-p destination info
)
2464 (or (not desc
) (string= desc
(org-element-property
2465 :raw-value destination
))))
2467 "<text:bookmark-ref text:reference-format=\"chapter\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
2468 label
(mapconcat 'number-to-string headline-no
".")))
2469 ;; Case 2: Either the headline is un-numbered or
2470 ;; LINK has a custom description. Display LINK's
2471 ;; description or headline's title.
2473 (let ((desc (or desc
(org-export-data
2474 (org-element-property :title destination
)
2477 "<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
2479 ;; Fuzzy link points to a target. Do as above.
2481 ;; Identify nearest meaningful container
2483 (loop for parent in
(org-export-get-genealogy destination
)
2486 (org-element-type parent
)
2487 '(footnote-definition footnote-reference headline item
2490 ;; There is a meaningful container
2492 (case (org-element-type container
)
2493 ;; Container is item
2496 "<text:bookmark-ref text:reference-format=\"number-all-superior\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
2497 (org-export-solidify-link-text path
)
2499 (mapconcat 'number-to-string
2500 (org-e-odt-resolve-numbered-paragraph
2501 container info
) ".")))))))
2504 ;; (setq number (cond
2505 ;; ((org-e-odt--standalone-link-p destination info)
2506 ;; (org-export-get-ordinal
2507 ;; (assoc 'link (org-element-contents destination))
2508 ;; info 'link 'org-e-odt--standalone-link-p))
2509 ;; (t (org-export-get-ordinal destination info))))
2510 ;; (setq desc (when number
2511 ;; (if (atom number) (number-to-string number)
2512 ;; (mapconcat 'number-to-string number ".")))))
2514 (let ((label-reference
2515 (org-e-odt-format-label destination info
'reference
)))
2516 (assert label-reference
)
2517 label-reference
)))))
2518 ;; Coderef: replace link with the reference name or the
2519 ;; equivalent line number.
2520 ((string= type
"coderef")
2521 (let* ((line-no (format "%d" (org-export-resolve-coderef path info
)))
2522 (href (concat "coderef-" path
)))
2524 (org-export-get-coderef-format path desc
)
2526 "<text:bookmark-ref text:reference-format=\"number\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
2528 ;; Link type is handled by a special function.
2529 ((functionp (setq protocol
(nth 2 (assoc type org-link-protocols
))))
2530 (funcall protocol
(org-link-unescape path
) desc
'odt
))
2531 ;; External link with a description part.
2533 (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
2535 ;; External link without a description part.
2537 (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
2539 ;; No path, only description. Try to do something useful.
2540 (t (format "<text:span text:style-name=\"%s\">%s</text:span>"
2541 "Emphasis" desc
)))))
2546 (defun org-e-odt-paragraph (paragraph contents info
)
2547 "Transcode a PARAGRAPH element from Org to ODT.
2548 CONTENTS is the contents of the paragraph, as a string. INFO is
2549 the plist used as a communication channel."
2550 (let* ((parent (org-export-get-parent paragraph
))
2551 (parent-type (org-element-type parent
))
2552 (style (case parent-type
2553 (quote-block "Quotations")
2554 (center-block "OrgCenter")
2555 (footnote-definition "Footnote")
2556 (t (or (org-element-property :style paragraph
)
2558 ;; If this paragraph is a leading paragraph in an item and the
2559 ;; item has a checkbox, splice the checkbox and paragraph contents
2561 (when (and (eq (org-element-type parent
) 'item
)
2562 (eq paragraph
(car (org-element-contents parent
))))
2563 (setq contents
(concat (org-e-odt--checkbox parent
) contents
)))
2565 (format "\n<text:p text:style-name=\"%s\">%s</text:p>" style contents
)))
2570 (defun org-e-odt-plain-list (plain-list contents info
)
2571 "Transcode a PLAIN-LIST element from Org to ODT.
2572 CONTENTS is the contents of the list. INFO is a plist holding
2573 contextual information."
2574 (format "\n<text:list text:style-name=\"%s\" %s>\n%s</text:list>"
2575 ;; Choose style based on list type.
2576 (case (org-element-property :type plain-list
)
2577 (ordered "OrgNumberedList")
2578 (unordered "OrgBulletedList")
2579 (descriptive-1 "OrgDescriptionList")
2580 (descriptive-2 "OrgDescriptionList"))
2581 ;; If top-level list, re-start numbering. Otherwise,
2582 ;; continue numbering.
2583 (format "text:continue-numbering=\"%s\""
2584 (let* ((parent (org-export-get-parent plain-list
)))
2585 (if (and parent
(eq (org-element-type parent
) 'item
))
2591 (defun org-e-odt--encode-tabs-and-spaces (line)
2592 (replace-regexp-in-string
2593 "\\([\t]\\|\\([ ]+\\)\\)"
2596 ((string= s
"\t") "<text:tab/>")
2597 (t (let ((n (length s
)))
2600 ((> n
1) (concat " " (format "<text:s text:c=\"%d\"/>" (1- n
))))
2604 (defun org-e-odt--encode-plain-text (text &optional no-whitespace-filling
)
2607 (setq text
(replace-regexp-in-string (car pair
) (cdr pair
) text t t
)))
2608 '(("&" .
"&") ("<" .
"<") (">" .
">")))
2609 (if no-whitespace-filling text
2610 (org-e-odt--encode-tabs-and-spaces text
)))
2612 (defun org-e-odt--quotation-marks (text info
)
2613 "Export quotation marks depending on language conventions.
2614 TEXT is a string containing quotation marks to be replaced. INFO
2615 is a plist used as a communication channel."
2618 (while (setq start
(string-match (car l
) text start
))
2619 (let ((new-quote (concat (match-string 1 text
) (cdr l
))))
2620 (setq text
(replace-match new-quote t t text
))))))
2621 (cdr (or (assoc (plist-get info
:language
) org-e-odt-quotes
)
2622 ;; Falls back on English.
2623 (assoc "en" org-e-odt-quotes
))))
2626 (defun org-e-odt-plain-text (text info
)
2627 "Transcode a TEXT string from Org to ODT.
2628 TEXT is the string to transcode. INFO is a plist holding
2629 contextual information."
2630 ;; Protect &, < and >.
2631 (setq text
(org-e-odt--encode-plain-text text t
))
2632 ;; Handle quotation marks
2633 (setq text
(org-e-odt--quotation-marks text info
))
2634 ;; Convert special strings.
2635 (when (plist-get info
:with-special-strings
)
2638 (setq text
(replace-regexp-in-string (car pair
) (cdr pair
) text t nil
)))
2639 org-e-odt-special-string-regexps
))
2640 ;; Handle break preservation if required.
2641 (when (plist-get info
:preserve-breaks
)
2642 (setq text
(replace-regexp-in-string
2643 "\\(\\\\\\\\\\)?[ \t]*\n" "<text:line-break/>\n" text t
)))
2650 (defun org-e-odt-planning (planning contents info
)
2651 "Transcode a PLANNING element from Org to ODT.
2652 CONTENTS is nil. INFO is a plist used as a communication
2654 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2655 "OrgTimestampWrapper"
2657 (let ((closed (org-element-property :closed planning
)))
2660 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2661 "OrgTimestampKeyword" org-closed-string
)
2662 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2663 "OrgTimestamp" (org-translate-time closed
)))))
2664 (let ((deadline (org-element-property :deadline planning
)))
2667 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2668 "OrgTimestampKeyword" org-deadline-string
)
2669 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2670 "OrgTimestamp" (org-translate-time deadline
)))))
2671 (let ((scheduled (org-element-property :scheduled planning
)))
2674 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2675 "OrgTimestampKeyword" org-scheduled-string
)
2676 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2677 "OrgTimestamp" (org-translate-time scheduled
))))))))
2680 ;;;; Property Drawer
2682 (defun org-e-odt-property-drawer (property-drawer contents info
)
2683 "Transcode a PROPERTY-DRAWER element from Org to ODT.
2684 CONTENTS is nil. INFO is a plist holding contextual
2686 ;; The property drawer isn't exported but we want separating blank
2687 ;; lines nonetheless.
2693 (defun org-e-odt-quote-block (quote-block contents info
)
2694 "Transcode a QUOTE-BLOCK element from Org to ODT.
2695 CONTENTS holds the contents of the block. INFO is a plist
2696 holding contextual information."
2702 (defun org-e-odt-quote-section (quote-section contents info
)
2703 "Transcode a QUOTE-SECTION element from Org to ODT.
2704 CONTENTS is nil. INFO is a plist holding contextual information."
2705 (let ((value (org-remove-indentation
2706 (org-element-property :value quote-section
))))
2707 (when value
(org-e-odt-do-format-code value
))))
2712 (defun org-e-odt-format-section (text style
&optional name
)
2713 (let ((default-name (car (org-e-odt-add-automatic-style "Section"))))
2714 (format "\n<text:section text:style-name=\"%s\" %s>\n%s\n</text:section>"
2716 (format "text:name=\"%s\"" (or name default-name
))
2720 (defun org-e-odt-section (section contents info
) ; FIXME
2721 "Transcode a SECTION element from Org to ODT.
2722 CONTENTS holds the contents of the section. INFO is a plist
2723 holding contextual information."
2728 (defun org-e-odt-radio-target (radio-target text info
)
2729 "Transcode a RADIO-TARGET object from Org to ODT.
2730 TEXT is the text of the target. INFO is a plist holding
2731 contextual information."
2733 text
(org-export-solidify-link-text
2734 (org-element-property :value radio-target
))))
2739 (defun org-e-odt-special-block (special-block contents info
)
2740 "Transcode a SPECIAL-BLOCK element from Org to ODT.
2741 CONTENTS holds the contents of the block. INFO is a plist
2742 holding contextual information."
2743 (let ((type (downcase (org-element-property :type special-block
)))
2744 (attributes (org-export-read-attribute :attr_odt special-block
)))
2747 ((string= type
"annotation")
2748 (let ((author (or (plist-get attributes
:author
)
2749 (let ((author (plist-get info
:author
)))
2750 (and author
(org-export-data author info
)))))
2751 (date (or (plist-get attributes
:date
)
2752 (plist-get info
:date
))))
2754 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2756 (format "<office:annotation>\n%s\n</office:annotation>"
2759 (format "<dc:creator>%s</dc:creator>" author
))
2761 (format "<dc:date>%s</dc:date>"
2762 (org-e-odt--date date
)))
2765 ((string= type
"textbox")
2766 (let ((width (plist-get attributes
:width
))
2767 (height (plist-get attributes
:height
))
2768 (style (plist-get attributes
:style
))
2769 (extra (plist-get attributes
:extra
))
2770 (anchor (plist-get attributes
:anchor
)))
2771 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2772 "Text_20_body" (org-e-odt--textbox contents width height
2773 style extra anchor
))))
2779 (defun org-e-odt-hfy-face-to-css (fn)
2780 "Create custom style for face FN.
2781 When FN is the default face, use it's foreground and background
2782 properties to create \"OrgSrcBlock\" paragraph style. Otherwise
2783 use it's color attribute to create a character style whose name
2784 is obtained from FN. Currently all attributes of FN other than
2787 The style name for a face FN is derived using the following
2788 operations on the face name in that order - de-dash, CamelCase
2789 and prefix with \"OrgSrc\". For example,
2790 `font-lock-function-name-face' is associated with
2791 \"OrgSrcFontLockFunctionNameFace\"."
2792 (let* ((css-list (hfy-face-to-style fn
))
2793 (style-name ((lambda (fn)
2796 'capitalize
(split-string
2797 (hfy-face-or-def-to-name fn
) "-")
2799 (color-val (cdr (assoc "color" css-list
)))
2800 (background-color-val (cdr (assoc "background" css-list
)))
2801 (style (and org-e-odt-create-custom-styles-for-srcblocks
2804 (format org-e-odt-src-block-paragraph-format
2805 background-color-val color-val
))
2809 <style:style style:name=\"%s\" style:family=\"text\">
2810 <style:text-properties fo:color=\"%s\"/>
2811 </style:style>" style-name color-val
))))))
2812 (cons style-name style
)))
2814 (defun org-e-odt-htmlfontify-string (line)
2815 (let* ((hfy-html-quote-regex "\\([<\"&> ]\\)")
2816 (hfy-html-quote-map '(("\"" """)
2821 (" " "<text:tab/>")))
2822 (hfy-face-to-css 'org-e-odt-hfy-face-to-css
)
2823 (hfy-optimisations-1 (copy-sequence hfy-optimisations
))
2824 (hfy-optimisations (add-to-list 'hfy-optimisations-1
2826 (hfy-begin-span-handler
2827 (lambda (style text-block text-id text-begins-block-p
)
2828 (insert (format "<text:span text:style-name=\"%s\">" style
))))
2829 (hfy-end-span-handler (lambda nil
(insert "</text:span>"))))
2830 (with-no-warnings (htmlfontify-string line
))))
2832 (defun org-e-odt-do-format-code
2833 (code &optional lang refs retain-labels num-start
)
2834 (let* ((lang (or (assoc-default lang org-src-lang-modes
) lang
))
2835 (lang-mode (and lang
(intern (format "%s-mode" lang
))))
2836 (code-lines (org-split-string code
"\n"))
2837 (code-length (length code-lines
))
2838 (use-htmlfontify-p (and (functionp lang-mode
)
2839 org-e-odt-fontify-srcblocks
2840 (require 'htmlfontify nil t
)
2841 (fboundp 'htmlfontify-string
)))
2842 (code (if (not use-htmlfontify-p
) code
2846 (font-lock-fontify-buffer)
2848 (fontifier (if use-htmlfontify-p
'org-e-odt-htmlfontify-string
2849 'org-e-odt--encode-plain-text
))
2850 (par-style (if use-htmlfontify-p
"OrgSrcBlock"
2851 "OrgFixedWidthBlock"))
2853 (assert (= code-length
(length (org-split-string code
"\n"))))
2855 (org-export-format-code
2857 (lambda (loc line-num ref
)
2859 (concat par-style
(and (= (incf i
) code-length
) "LastLine")))
2861 (setq loc
(concat loc
(and ref retain-labels
(format " (%s)" ref
))))
2862 (setq loc
(funcall fontifier loc
))
2864 (setq loc
(org-e-odt--target loc
(concat "coderef-" ref
))))
2866 (setq loc
(format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2868 (if (not line-num
) loc
2869 (format "\n<text:list-item>%s\n</text:list-item>" loc
)))
2872 ((not num-start
) code
)
2875 "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
2876 " text:continue-numbering=\"false\"" code
))
2879 "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
2880 " text:continue-numbering=\"true\"" code
)))))
2882 (defun org-e-odt-format-code (element info
)
2883 (let* ((lang (org-element-property :language element
))
2884 ;; Extract code and references.
2885 (code-info (org-export-unravel-code element
))
2886 (code (car code-info
))
2887 (refs (cdr code-info
))
2888 ;; Does the src block contain labels?
2889 (retain-labels (org-element-property :retain-labels element
))
2890 ;; Does it have line numbers?
2891 (num-start (case (org-element-property :number-lines element
)
2892 (continued (org-export-get-loc element info
))
2894 (org-e-odt-do-format-code code lang refs retain-labels num-start
)))
2896 (defun org-e-odt-src-block (src-block contents info
)
2897 "Transcode a SRC-BLOCK element from Org to ODT.
2898 CONTENTS holds the contents of the item. INFO is a plist holding
2899 contextual information."
2900 (let* ((lang (org-element-property :language src-block
))
2901 (attributes (org-export-read-attribute :attr_odt src-block
))
2902 (captions (org-e-odt-format-label src-block info
'definition
))
2903 (caption (car captions
)) (short-caption (cdr captions
)))
2906 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2908 (let ((--src-block (org-e-odt-format-code src-block info
)))
2909 (if (not (plist-get attributes
:textbox
)) --src-block
2910 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2912 (org-e-odt--textbox --src-block nil nil nil
)))))))
2915 ;;;; Statistics Cookie
2917 (defun org-e-odt-statistics-cookie (statistics-cookie contents info
)
2918 "Transcode a STATISTICS-COOKIE object from Org to ODT.
2919 CONTENTS is nil. INFO is a plist holding contextual information."
2920 (let ((cookie-value (org-element-property :value statistics-cookie
)))
2921 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2922 "OrgCode" cookie-value
)))
2927 (defun org-e-odt-strike-through (strike-through contents info
)
2928 "Transcode STRIKE-THROUGH from Org to ODT.
2929 CONTENTS is the text with strike-through markup. INFO is a plist
2930 holding contextual information."
2931 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2932 "Strikethrough" contents
))
2937 (defun org-e-odt-subscript (subscript contents info
)
2938 "Transcode a SUBSCRIPT object from Org to ODT.
2939 CONTENTS is the contents of the object. INFO is a plist holding
2940 contextual information."
2941 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2942 "OrgSubscript" contents
))
2947 (defun org-e-odt-superscript (superscript contents info
)
2948 "Transcode a SUPERSCRIPT object from Org to ODT.
2949 CONTENTS is the contents of the object. INFO is a plist holding
2950 contextual information."
2951 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2952 "OrgSuperscript" contents
))
2957 (defun org-e-odt-table-style-spec (element info
)
2958 (let* ((table (org-export-get-parent-table element
))
2959 (table-attributes (org-export-read-attribute :attr_odt table
))
2960 (table-style (plist-get table-attributes
:style
)))
2961 (assoc table-style org-e-odt-table-styles
)))
2963 (defun org-e-odt-get-table-cell-styles (table-cell info
)
2964 "Retrieve styles applicable to a table cell.
2965 R and C are (zero-based) row and column numbers of the table
2966 cell. STYLE-SPEC is an entry in `org-e-odt-table-styles'
2967 applicable to the current table. It is `nil' if the table is not
2968 associated with any style attributes.
2970 Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
2972 When STYLE-SPEC is nil, style the table cell the conventional way
2973 - choose cell borders based on row and column groupings and
2974 choose paragraph alignment based on `org-col-cookies' text
2976 `org-e-odt-get-paragraph-style-cookie-for-table-cell'.
2978 When STYLE-SPEC is non-nil, ignore the above cookie and return
2979 styles congruent with the ODF-1.2 specification."
2980 (let* ((table-cell-address (org-export-table-cell-address table-cell info
))
2981 (r (car table-cell-address
)) (c (cdr table-cell-address
))
2982 (style-spec (org-e-odt-table-style-spec table-cell info
))
2983 (table-dimensions (org-export-table-dimensions
2984 (org-export-get-parent-table table-cell
)
2987 ;; LibreOffice - particularly the Writer - honors neither table
2988 ;; templates nor custom table-cell styles. Inorder to retain
2989 ;; inter-operability with LibreOffice, only automatic styles are
2990 ;; used for styling of table-cells. The current implementation is
2991 ;; congruent with ODF-1.2 specification and hence is
2992 ;; future-compatible.
2994 ;; Additional Note: LibreOffice's AutoFormat facility for tables -
2995 ;; which recognizes as many as 16 different cell types - is much
2996 ;; richer. Unfortunately it is NOT amenable to easy configuration
2998 (let* ((template-name (nth 1 style-spec
))
2999 (cell-style-selectors (nth 2 style-spec
))
3002 ((and (cdr (assoc 'use-first-column-styles cell-style-selectors
))
3003 (= c
0)) "FirstColumn")
3004 ((and (cdr (assoc 'use-last-column-styles cell-style-selectors
))
3005 (= (1+ c
) (cdr table-dimensions
)))
3007 ((and (cdr (assoc 'use-first-row-styles cell-style-selectors
))
3008 (= r
0)) "FirstRow")
3009 ((and (cdr (assoc 'use-last-row-styles cell-style-selectors
))
3010 (= (1+ r
) (car table-dimensions
)))
3012 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors
))
3013 (= (% r
2) 1)) "EvenRow")
3014 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors
))
3015 (= (% r
2) 0)) "OddRow")
3016 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors
))
3017 (= (% c
2) 1)) "EvenColumn")
3018 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors
))
3019 (= (% c
2) 0)) "OddColumn")
3021 (concat template-name cell-type
)))))
3023 (defun org-e-odt-table-cell (table-cell contents info
)
3024 "Transcode a TABLE-CELL element from Org to ODT.
3025 CONTENTS is nil. INFO is a plist used as a communication
3027 (let* ((table-cell-address (org-export-table-cell-address table-cell info
))
3028 (r (car table-cell-address
))
3029 (c (cdr table-cell-address
))
3030 (horiz-span (or (org-export-table-cell-width table-cell info
) 0))
3031 (table-row (org-export-get-parent table-cell
))
3032 (custom-style-prefix (org-e-odt-get-table-cell-styles
3036 (and custom-style-prefix
3037 (format "%sTableParagraph" custom-style-prefix
))
3040 ((and (= 1 (org-export-table-row-group table-row info
))
3041 (org-export-table-has-header-p
3042 (org-export-get-parent-table table-row
) info
))
3044 ((let* ((table (org-export-get-parent-table table-cell
))
3045 (table-attrs (org-export-read-attribute :attr_odt table
))
3046 (table-header-columns (plist-get table-attrs
3048 (<= c
(cond ((wholenump table-header-columns
)
3049 (- table-header-columns
1))
3050 (table-header-columns 0)
3053 (t "OrgTableContents"))
3054 (capitalize (symbol-name (org-export-table-cell-alignment
3055 table-cell info
))))))
3058 (and custom-style-prefix
(format "%sTableCell"
3059 custom-style-prefix
))
3062 (when (or (org-export-table-row-starts-rowgroup-p table-row info
)
3064 (when (org-export-table-row-ends-rowgroup-p table-row info
) "B")
3065 (when (and (org-export-table-cell-starts-colgroup-p table-cell info
)
3066 (not (zerop c
)) ) "L"))))
3069 (format " table:style-name=\"%s\"" cell-style-name
)
3070 (and (> horiz-span
0)
3071 (format " table:number-columns-spanned=\"%d\""
3072 (1+ horiz-span
))))))
3073 (unless contents
(setq contents
""))
3075 (assert paragraph-style
)
3076 (format "\n<table:table-cell%s>\n%s\n</table:table-cell>"
3078 (let ((table-cell-contents (org-element-contents table-cell
)))
3079 (if (memq (org-element-type (car table-cell-contents
))
3080 org-element-all-elements
)
3082 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3083 paragraph-style contents
))))
3085 (dotimes (i horiz-span s
)
3086 (setq s
(concat s
"\n<table:covered-table-cell/>"))))
3092 (defun org-e-odt-table-row (table-row contents info
)
3093 "Transcode a TABLE-ROW element from Org to ODT.
3094 CONTENTS is the contents of the row. INFO is a plist used as a
3095 communication channel."
3096 ;; Rules are ignored since table separators are deduced from
3097 ;; borders of the current row.
3098 (when (eq (org-element-property :type table-row
) 'standard
)
3099 (let* ((rowgroup-tags
3100 (if (and (= 1 (org-export-table-row-group table-row info
))
3101 (org-export-table-has-header-p
3102 (org-export-get-parent-table table-row
) info
))
3103 ;; If the row belongs to the first rowgroup and the
3104 ;; table has more than one row groups, then this row
3105 ;; belongs to the header row group.
3106 '("\n<table:table-header-rows>" .
"\n</table:table-header-rows>")
3107 ;; Otherwise, it belongs to non-header row group.
3108 '("\n<table:table-rows>" .
"\n</table:table-rows>"))))
3110 ;; Does this row begin a rowgroup?
3111 (when (org-export-table-row-starts-rowgroup-p table-row info
)
3112 (car rowgroup-tags
))
3114 (format "\n<table:table-row>\n%s\n</table:table-row>" contents
)
3115 ;; Does this row end a rowgroup?
3116 (when (org-export-table-row-ends-rowgroup-p table-row info
)
3117 (cdr rowgroup-tags
))))))
3122 (defun org-e-odt-table-first-row-data-cells (table info
)
3127 (unless (eq (org-element-property :type row
) 'rule
) row
))
3129 (special-column-p (org-export-table-has-special-column-p table
)))
3130 (if (not special-column-p
) (org-element-contents table-row
)
3131 (cdr (org-element-contents table-row
)))))
3133 (defun org-e-odt--table (table contents info
)
3134 "Transcode a TABLE element from Org to ODT.
3135 CONTENTS is the contents of the table. INFO is a plist holding
3136 contextual information."
3137 (case (org-element-property :type table
)
3138 ;; Case 1: table.el doesn't support export to OD format. Strip
3139 ;; such tables from export.
3144 "(org-e-odt): Found table.el-type table in the source Org file."
3145 " table.el doesn't support export to ODT format."
3146 " Stripping the table from export."))))
3147 ;; Case 2: Native Org tables.
3149 (let* ((captions (org-e-odt-format-label table info
'definition
))
3150 (caption (car captions
)) (short-caption (cdr captions
))
3151 (attributes (org-export-read-attribute :attr_odt table
))
3152 (custom-table-style (nth 1 (org-e-odt-table-style-spec table info
)))
3155 (lambda (table info
)
3156 (let* ((table-style (or custom-table-style
"OrgTable"))
3157 (column-style (format "%sColumn" table-style
)))
3159 (lambda (table-cell)
3160 (let ((width (1+ (or (org-export-table-cell-width
3161 table-cell info
) 0)))
3163 "\n<table:table-column table:style-name=\"%s\"/>"
3166 (dotimes (i width out
) (setq out
(concat s out
)))))
3167 (org-e-odt-table-first-row-data-cells table info
) "\n"))))))
3171 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3174 (let* ((automatic-name
3175 (org-e-odt-add-automatic-style "Table" attributes
)))
3177 "\n<table:table table:name=\"%s\" table:style-name=\"%s\">"
3178 (or short-caption
(car automatic-name
))
3179 (or custom-table-style
(cdr automatic-name
) "OrgTable")))
3180 ;; column specification.
3181 (funcall table-column-specs table info
)
3185 "</table:table>")))))
3187 (defun org-e-odt-table (table contents info
)
3188 "Transcode a TABLE element from Org to ODT.
3189 CONTENTS is the contents of the table. INFO is a plist holding
3190 contextual information.
3192 Use `org-e-odt--table' to typeset the table. Handle details
3193 pertaining to indentation here."
3194 (let* ((--get-previous-elements
3197 (let ((parent (org-export-get-parent blob
)))
3198 (cdr (memq blob
(reverse (org-element-contents parent
))))))))
3199 (--element-preceded-by-table-p
3201 (lambda (element info
)
3202 (loop for el in
(funcall --get-previous-elements element info
)
3203 thereis
(eq (org-element-type el
) 'table
)))))
3204 (--walk-list-genealogy-and-collect-tags
3206 (lambda (table info
)
3207 (let* ((genealogy (org-export-get-genealogy table
))
3209 (when (eq (org-element-type (car genealogy
)) 'item
)
3210 (loop for el in genealogy
3211 when
(memq (org-element-type el
)
3216 (loop for el in genealogy
3217 when
(and (eq (org-element-type el
) 'headline
)
3218 (org-export-low-level-p el info
))
3222 (org-element-contents
3223 (org-export-get-parent el
)))))))
3226 ;; Handle list genealogy.
3227 (loop for el in list-genealogy collect
3228 (case (org-element-type el
)
3230 (setq parent-list el
)
3231 (cons "</text:list>"
3232 (format "\n<text:list text:style-name=\"%s\" %s>"
3233 (case (org-element-property :type el
)
3234 (ordered "OrgNumberedList")
3235 (unordered "OrgBulletedList")
3236 (descriptive-1 "OrgDescriptionList")
3237 (descriptive-2 "OrgDescriptionList"))
3238 "text:continue-numbering=\"true\"")))
3242 (if (funcall --element-preceded-by-table-p table info
)
3243 '("</text:list-header>" .
"<text:list-header>")
3244 '("</text:list-item>" .
"<text:list-header>")))
3245 ((funcall --element-preceded-by-table-p
3247 '("</text:list-header>" .
"<text:list-header>"))
3248 (t '("</text:list-item>" .
"<text:list-item>"))))))
3249 ;; Handle low-level headlines.
3250 (loop for el in llh-genealogy
3251 with step
= 'item collect
3254 (setq step
'item
) ; Flip-flop
3255 (setq parent-list el
)
3256 (cons "</text:list>"
3257 (format "\n<text:list text:style-name=\"%s\" %s>"
3258 (if (org-export-numbered-headline-p
3262 "text:continue-numbering=\"true\"")))
3264 (setq step
'plain-list
) ; Flip-flop
3267 (if (funcall --element-preceded-by-table-p table info
)
3268 '("</text:list-header>" .
"<text:list-header>")
3269 '("</text:list-item>" .
"<text:list-header>")))
3270 ((let ((section?
(org-export-get-previous-element
3273 (eq (org-element-type section?
) 'section
)
3274 (assq 'table
(org-element-contents section?
))))
3275 '("</text:list-header>" .
"<text:list-header>"))
3277 '("</text:list-item>" .
"<text:list-item>")))))))))))
3278 (close-open-tags (funcall --walk-list-genealogy-and-collect-tags
3280 ;; OpenDocument schema does not permit table to occur within a
3283 ;; One solution - the easiest and lightweight, in terms of
3284 ;; implementation - is to put the table in an indented text box
3285 ;; and make the text box part of the list-item. Unfortunately if
3286 ;; the table is big and spans multiple pages, the text box could
3287 ;; overflow. In this case, the following attribute will come
3290 ;; ,---- From OpenDocument-v1.1.pdf
3291 ;; | 15.27.28 Overflow behavior
3293 ;; | For text boxes contained within text document, the
3294 ;; | style:overflow-behavior property specifies the behavior of text
3295 ;; | boxes where the containing text does not fit into the text
3298 ;; | If the attribute's value is clip, the text that does not fit
3299 ;; | into the text box is not displayed.
3301 ;; | If the attribute value is auto-create-new-frame, a new frame
3302 ;; | will be created on the next page, with the same position and
3303 ;; | dimensions of the original frame.
3305 ;; | If the style:overflow-behavior property's value is
3306 ;; | auto-create-new-frame and the text box has a minimum width or
3307 ;; | height specified, then the text box will grow until the page
3308 ;; | bounds are reached before a new frame is created.
3311 ;; Unfortunately, LibreOffice-3.4.6 doesn't honor
3312 ;; auto-create-new-frame property and always resorts to clipping
3313 ;; the text box. This results in table being truncated.
3315 ;; So we solve the problem the hard (and fun) way using list
3318 ;; The problem only becomes more interesting if you take in to
3319 ;; account the following facts:
3321 ;; - Description lists are simulated as plain lists.
3322 ;; - Low-level headlines can be listified.
3323 ;; - In Org-mode, a table can occur not only as a regular list
3324 ;; item, but also within description lists and low-level
3327 ;; See `org-e-odt-translate-description-lists' and
3328 ;; `org-e-odt-translate-low-level-headlines' for how this is
3332 ;; Discontinue the list.
3333 (mapconcat 'car close-open-tags
"\n")
3334 ;; Put the table in an indented section.
3335 (let* ((table (org-e-odt--table table contents info
))
3336 (level (/ (length (mapcar 'car close-open-tags
)) 2))
3337 (style (format "OrgIndentedSection-Level-%d" level
)))
3338 (when table
(org-e-odt-format-section table style
)))
3339 ;; Continue the list.
3340 (mapconcat 'cdr
(nreverse close-open-tags
) "\n"))))
3345 (defun org-e-odt-target (target contents info
)
3346 "Transcode a TARGET object from Org to ODT.
3347 CONTENTS is nil. INFO is a plist holding contextual
3349 (let ((value (org-element-property :value target
)))
3350 (org-e-odt--target "" (org-export-solidify-link-text value
))))
3355 (defun org-e-odt-timestamp (timestamp contents info
)
3356 "Transcode a TIMESTAMP object from Org to ODT.
3357 CONTENTS is nil. INFO is a plist used as a communication
3359 (let ((timestamp-1 (org-element-property :value timestamp
))
3360 (timestamp-2 (org-element-property :range-end timestamp
)))
3361 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3362 "OrgTimestampWrapper"
3364 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3365 "OrgTimestamp" (org-translate-time timestamp-1
))
3368 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3369 "OrgTimestamp" (org-translate-time timestamp-2
)))))))
3374 (defun org-e-odt-underline (underline contents info
)
3375 "Transcode UNDERLINE from Org to ODT.
3376 CONTENTS is the text with underline markup. INFO is a plist
3377 holding contextual information."
3378 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3379 "Underline" contents
))
3384 (defun org-e-odt-verbatim (verbatim contents info
)
3385 "Transcode a VERBATIM object from Org to ODT.
3386 CONTENTS is nil. INFO is a plist used as a communication
3388 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3389 "OrgCode" (org-element-property :value verbatim
)))
3394 (defun org-e-odt-verse-block (verse-block contents info
)
3395 "Transcode a VERSE-BLOCK element from Org to ODT.
3396 CONTENTS is verse block contents. INFO is a plist holding
3397 contextual information."
3398 ;; Add line breaks to each line of verse.
3399 (setq contents
(replace-regexp-in-string
3400 "\\(<text:line-break/>\\)?[ \t]*\n"
3401 "<text:line-break/>" contents
))
3402 ;; Replace tabs and spaces.
3403 (setq contents
(org-e-odt--encode-tabs-and-spaces contents
))
3404 ;; Surround it in a verse environment.
3405 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3406 "OrgVerse" contents
))
3412 ;;;; LaTeX fragments
3414 (defun org-e-odt--translate-latex-fragments (tree backend info
)
3415 (let ((processing-type (plist-get info
:LaTeX-fragments
))
3417 ;; Normalize processing-type to one of dvipng, mathml or verbatim.
3418 ;; If the desired converter is not available, force verbatim
3420 (case processing-type
3422 (if (and (fboundp 'org-format-latex-mathml-available-p
)
3423 (org-format-latex-mathml-available-p))
3424 (setq processing-type
'mathml
)
3425 (message "LaTeX to MathML converter not available.")
3426 (setq processing-type
'verbatim
)))
3428 (unless (and (org-check-external-command "latex" "" t
)
3429 (org-check-external-command "dvipng" "" t
))
3430 (message "LaTeX to PNG converter not available.")
3431 (setq processing-type
'verbatim
)))
3433 (message "Unknown LaTeX option. Forcing verbatim.")
3434 (setq processing-type
'verbatim
)))
3436 ;; Store normalized value for later use.
3437 (when (plist-get info
:LaTeX-fragments
)
3438 (plist-put info
:LaTeX-fragments processing-type
))
3439 (message "Formatting LaTeX using %s" processing-type
)
3441 ;; Convert `latex-fragment's and `latex-environment's.
3442 (when (memq processing-type
'(mathml dvipng
))
3444 tree
'(latex-fragment latex-environment
)
3447 (let* ((latex-frag (org-element-property :value latex-
*))
3448 (input-file (plist-get info
:input-file
))
3449 (cache-dir (file-name-directory input-file
))
3450 (cache-subdir (concat
3451 (case processing-type
3453 (mathml "ltxmathml/"))
3454 (file-name-sans-extension
3455 (file-name-nondirectory input-file
))))
3457 (case processing-type
3458 (dvipng (format "Creating LaTeX Image %d..." count
))
3459 (mathml (format "Creating MathML snippet %d..." count
))))
3460 ;; Get an Org-style link to PNG image or the MathML
3463 (let ((link (with-temp-buffer
3465 (org-format-latex cache-subdir cache-dir
3467 nil nil processing-type
)
3468 (buffer-substring-no-properties
3469 (point-min) (point-max)))))
3470 (if (not (string-match "file:\\([^]]*\\)" link
))
3471 (prog1 nil
(message "LaTeX Conversion failed."))
3474 ;; Conversion succeeded. Parse above Org-style link to a
3476 (let* ((link (car (org-element-map (with-temp-buffer
3479 (org-element-parse-buffer))
3482 (org-element-put-property link
:parent nil
)
3485 (case (org-element-type latex-
*)
3486 ;; Case 1: LaTeX environment.
3487 ;; Mimic a "standalone image or formula" by
3488 ;; enclosing the `link' in a `paragraph'.
3489 ;; Copy over original attributes, captions to
3490 ;; the enclosing paragraph.
3492 (org-element-adopt-elements
3494 (list :style
"OrgFormula"
3495 :name
(org-element-property :name
3497 :caption
(org-element-property :caption
3500 ;; Case 2: LaTeX fragment.
3501 ;; No special action.
3502 (latex-fragment link
))))
3503 ;; Note down the object that link replaces.
3504 (org-element-put-property replacement
:replaces
3505 (list (org-element-type latex-
*)
3506 (list :value latex-frag
)))
3508 (org-element-set-element latex-
* replacement
))))))
3513 ;;;; Description lists
3515 ;; This translator is necessary to handle indented tables in a uniform
3516 ;; manner. See comment in `org-e-odt--table'.
3518 (defun org-e-odt--translate-description-lists (tree backend info
)
3519 ;; OpenDocument has no notion of a description list. So simulate it
3520 ;; using plain lists. Description lists in the exported document
3521 ;; are typeset in the same manner as they are in a typical HTML
3524 ;; Specifically, a description list like this:
3527 ;; | - term-1 :: definition-1
3528 ;; | - term-2 :: definition-2
3531 ;; gets translated in to the following form:
3540 ;; Further effect is achieved by fixing the OD styles as below:
3542 ;; 1. Set the :type property of the simulated lists to
3543 ;; `descriptive-1' and `descriptive-2'. Map these to list-styles
3544 ;; that has *no* bullets whatsoever.
3546 ;; 2. The paragraph containing the definition term is styled to be
3552 (when (equal (org-element-property :type el
) 'descriptive
)
3553 (org-element-set-element
3555 (apply 'org-element-adopt-elements
3556 (list 'plain-list
(list :type
'descriptive-1
))
3559 (org-element-adopt-elements
3560 (list 'item
(list :checkbox
(org-element-property
3562 (list 'paragraph
(list :style
"Text_20_body_20_bold")
3563 (or (org-element-property :tag item
) "(no term)"))
3564 (org-element-adopt-elements
3565 (list 'plain-list
(list :type
'descriptive-2
))
3566 (apply 'org-element-adopt-elements
3568 (org-element-contents item
)))))
3569 (org-element-contents el
)))))
3576 ;; Lists that are marked with attribute `:list-table' are called as
3577 ;; list tables. They will be rendered as a table within the exported
3580 ;; Consider an example. The following list table
3582 ;; #+attr_odt :list-table t
3592 ;; will be exported as though it were an Org table like the one show
3595 ;; | Row 1 | 1.1 | 1.2 | 1.3 |
3596 ;; | Row 2 | 2.1 | 2.2 | 2.3 |
3598 ;; Note that org-tables are NOT multi-line and each line is mapped to
3599 ;; a unique row in the exported document. So if an exported table
3600 ;; needs to contain a single paragraph (with copious text) it needs to
3601 ;; be typed up in a single line. Editing such long lines using the
3602 ;; table editor will be a cumbersome task. Furthermore inclusion of
3603 ;; multi-paragraph text in a table cell is well-nigh impossible.
3605 ;; A LIST-TABLE circumvents above problems.
3607 ;; Note that in the example above the list items could be paragraphs
3608 ;; themselves and the list can be arbitrarily deep.
3610 ;; Inspired by following thread:
3611 ;; https://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01101.html
3613 ;; Translate lists to tables
3615 (defun org-e-odt--translate-list-tables (tree backend info
)
3619 (when (org-export-read-attribute :attr_odt l1-list
:list-table
)
3620 ;; Replace list with table.
3621 (org-element-set-element
3623 ;; Build replacement table.
3624 (apply 'org-element-adopt-elements
3625 (list 'table
'(:type org
:attr_odt
(":style \"GriddedTable\"")))
3630 (let* ((l1-item-contents (org-element-contents l1-item
))
3631 l1-item-leading-text l2-list
)
3632 ;; Remove Level-2 list from the Level-item. It
3633 ;; will be subsequently attached as table-cells.
3634 (let ((cur l1-item-contents
) prev
)
3635 (while (and cur
(not (eq (org-element-type (car cur
))
3638 (setq cur
(cdr cur
)))
3641 (setq l2-list
(car cur
)))
3642 (setq l1-item-leading-text l1-item-contents
))
3643 ;; Level-1 items start a table row.
3644 (apply 'org-element-adopt-elements
3645 (list 'table-row
(list :type
'standard
))
3646 ;; Leading text of level-1 item define the
3647 ;; first table-cell.
3648 (apply 'org-element-adopt-elements
3649 (list 'table-cell nil
)
3650 l1-item-leading-text
)
3651 ;; Level-2 items define subsequent
3652 ;; table-cells of the row.
3657 (apply 'org-element-adopt-elements
3658 (list 'table-cell nil
)
3659 (org-element-contents l2-item
)))
3667 ;;; Interactive functions
3669 (defun org-e-odt-create-manifest-file-entry (&rest args
)
3670 (push args org-e-odt-manifest-file-entries
))
3672 (defun org-e-odt-write-manifest-file ()
3673 (make-directory (concat org-e-odt-zip-dir
"META-INF"))
3674 (let ((manifest-file (concat org-e-odt-zip-dir
"META-INF/manifest.xml")))
3675 (with-current-buffer
3676 (let ((nxml-auto-insert-xml-declaration-flag nil
))
3677 (find-file-noselect manifest-file t
))
3679 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
3680 <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
3682 (lambda (file-entry)
3683 (let* ((version (nth 2 file-entry
))
3684 (extra (if (not version
) ""
3685 (format " manifest:version=\"%s\"" version
))))
3687 (format org-e-odt-manifest-file-entry-tag
3688 (nth 0 file-entry
) (nth 1 file-entry
) extra
))))
3689 org-e-odt-manifest-file-entries
)
3690 (insert "\n</manifest:manifest>"))))
3692 (defmacro org-e-odt--export-wrap
(out-file &rest body
)
3693 `(let* ((--out-file ,out-file
)
3694 (out-file-type (file-name-extension --out-file
))
3695 (org-e-odt-xml-files '("META-INF/manifest.xml" "content.xml"
3696 "meta.xml" "styles.xml"))
3697 ;; Initialize workarea. All files that end up in the
3698 ;; exported get created here.
3699 (org-e-odt-zip-dir (file-name-as-directory
3700 (make-temp-file (format "%s-" out-file-type
) t
)))
3701 (org-e-odt-manifest-file-entries nil
)
3702 (--cleanup-xml-buffers
3705 ;; Kill all XML buffers.
3706 (mapc (lambda (file)
3707 (let ((buf (get-file-buffer
3708 (concat org-e-odt-zip-dir file
))))
3710 (set-buffer-modified-p nil
)
3711 (kill-buffer buf
))))
3712 org-e-odt-xml-files
)
3713 ;; Delete temporary directory and also other embedded
3714 ;; files that get copied there.
3715 (delete-directory org-e-odt-zip-dir t
)))))
3716 (org-condition-case-unless-debug err
3718 (unless (executable-find "zip")
3719 ;; Not at all OSes ship with zip by default
3720 (error "Executable \"zip\" needed for creating OpenDocument files"))
3721 ;; Do export. This creates a bunch of xml files ready to be
3722 ;; saved and zipped.
3724 ;; Create a manifest entry for content.xml.
3725 (org-e-odt-create-manifest-file-entry "text/xml" "content.xml")
3726 ;; Write mimetype file
3728 '(("odt" .
"application/vnd.oasis.opendocument.text")
3729 ("odf" .
"application/vnd.oasis.opendocument.formula")))
3730 (mimetype (cdr (assoc-string out-file-type mimetypes t
))))
3732 (error "Unknown OpenDocument backend %S" out-file-type
))
3733 (write-region mimetype nil
(concat org-e-odt-zip-dir
"mimetype"))
3734 (org-e-odt-create-manifest-file-entry mimetype
"/" "1.2"))
3735 ;; Write out the manifest entries before zipping
3736 (org-e-odt-write-manifest-file)
3737 ;; Save all XML files.
3738 (mapc (lambda (file)
3739 (let ((buf (get-file-buffer (concat org-e-odt-zip-dir file
))))
3741 (with-current-buffer buf
3742 ;; Prettify output if needed.
3743 (when org-e-odt-prettify-xml
3744 (indent-region (point-min) (point-max)))
3746 org-e-odt-xml-files
)
3748 (let* ((target --out-file
)
3749 (target-name (file-name-nondirectory target
))
3750 (target-dir (file-name-directory target
))
3751 (cmds `(("zip" "-mX0" ,target-name
"mimetype")
3752 ("zip" "-rmTq" ,target-name
"."))))
3753 ;; If a file with same name as the desired output file
3754 ;; exists, remove it.
3755 (when (file-exists-p target
)
3756 (delete-file target
))
3757 ;; Zip up the xml files.
3758 (let ((coding-system-for-write 'no-conversion
) exitcode err-string
)
3759 (message "Creating ODT file...")
3760 ;; Switch temporarily to content.xml. This way Zip
3761 ;; process will inherit `org-e-odt-zip-dir' as the current
3763 (with-current-buffer
3764 (find-file-noselect (concat org-e-odt-zip-dir
"content.xml") t
)
3767 (message "Running %s" (mapconcat 'identity cmd
" "))
3769 (with-output-to-string
3771 (apply 'call-process
(car cmd
)
3772 nil standard-output nil
(cdr cmd
)))))
3773 (or (zerop exitcode
)
3774 (error (concat "Unable to create OpenDocument file."
3775 (format " Zip failed with error (%s)"
3778 ;; Zip file is now in the rightful place.
3779 (rename-file target-name target
)))
3780 (message "Created %s" target
)
3781 ;; Cleanup work directory and work files.
3782 (funcall --cleanup-xml-buffers
)
3783 ;; Open the OpenDocument file in archive-mode for
3785 (find-file-noselect target t
)
3786 ;; Return exported file.
3788 ;; Case 1: Conversion desired on exported file. Run the
3789 ;; converter on the OpenDocument file. Return the
3791 (org-e-odt-preferred-output-format
3792 (or (org-e-odt-convert target org-e-odt-preferred-output-format
)
3794 ;; Case 2: No further conversion. Return exported
3795 ;; OpenDocument file.
3798 ;; Cleanup work directory and work files.
3799 (funcall --cleanup-xml-buffers
)
3800 (message "OpenDocument export failed: %s"
3801 (error-message-string err
))))))
3804 ;;;; Export to OpenDocument formula
3807 (defun org-e-odt-export-as-odf (latex-frag &optional odf-file
)
3808 "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
3809 Use `org-create-math-formula' to convert LATEX-FRAG first to
3810 MathML. When invoked as an interactive command, use
3811 `org-latex-regexps' to infer LATEX-FRAG from currently active
3812 region. If no LaTeX fragments are found, prompt for it. Push
3813 MathML source to kill ring, if `org-export-copy-to-kill-ring' is
3817 (setq frag
(and (setq frag
(and (region-active-p)
3818 (buffer-substring (region-beginning)
3820 (loop for e in org-latex-regexps
3821 thereis
(when (string-match (nth 1 e
) frag
)
3822 (match-string (nth 2 e
) frag
)))))
3823 (read-string "LaTeX Fragment: " frag nil frag
))
3824 ,(let ((odf-filename (expand-file-name
3826 (file-name-sans-extension
3827 (or (file-name-nondirectory buffer-file-name
)))
3829 (file-name-directory buffer-file-name
))))
3830 (read-file-name "ODF filename: " nil odf-filename nil
3831 (file-name-nondirectory odf-filename
)))))
3832 (let ((filename (or odf-file
3835 (file-name-sans-extension
3836 (or (file-name-nondirectory buffer-file-name
)))
3838 (file-name-directory buffer-file-name
)))))
3839 (org-e-odt--export-wrap
3841 (let* ((buffer (progn
3842 (require 'nxml-mode
)
3843 (let ((nxml-auto-insert-xml-declaration-flag nil
))
3844 (find-file-noselect (concat org-e-odt-zip-dir
3845 "content.xml") t
))))
3846 (coding-system-for-write 'utf-8
)
3847 (save-buffer-coding-system 'utf-8
))
3849 (set-buffer-file-coding-system coding-system-for-write
)
3850 (let ((mathml (org-create-math-formula latex-frag
)))
3851 (unless mathml
(error "No Math formula created"))
3853 ;; Add MathML to kill ring, if needed.
3854 (when org-export-copy-to-kill-ring
3855 (org-kill-new (buffer-string))))))))
3858 (defun org-e-odt-export-as-odf-and-open ()
3859 "Export LaTeX fragment as OpenDocument formula and immediately open it.
3860 Use `org-e-odt-export-as-odf' to read LaTeX fragment and OpenDocument
3863 (org-open-file (call-interactively 'org-e-odt-export-as-odf
) 'system
))
3866 ;;;; Export to OpenDocument Text
3869 (defun org-e-odt-export-to-odt
3870 (&optional subtreep visible-only body-only ext-plist pub-dir
)
3871 "Export current buffer to a HTML file.
3873 If narrowing is active in the current buffer, only export its
3876 If a region is active, export that region.
3878 When optional argument SUBTREEP is non-nil, export the sub-tree
3879 at point, extracting information from the headline properties
3882 When optional argument VISIBLE-ONLY is non-nil, don't export
3883 contents of hidden elements.
3885 When optional argument BODY-ONLY is non-nil, only write code
3886 between \"\\begin{document}\" and \"\\end{document}\".
3888 EXT-PLIST, when provided, is a property list with external
3889 parameters overriding Org default settings, but still inferior to
3890 file-local settings.
3892 When optional argument PUB-DIR is set, use it as the publishing
3895 Return output file's name."
3897 (org-e-odt--export-wrap
3898 (org-export-output-file-name ".odt" subtreep pub-dir
)
3899 (let* ((org-e-odt-embedded-images-count 0)
3900 (org-e-odt-embedded-formulas-count 0)
3901 (org-e-odt-automatic-styles nil
)
3902 (org-e-odt-object-counters nil
)
3903 ;; Let `htmlfontify' know that we are interested in collecting
3905 (hfy-user-sheet-assoc nil
))
3906 ;; Initialize content.xml and kick-off the export process.
3907 (let ((out-buf (progn
3908 (require 'nxml-mode
)
3909 (let ((nxml-auto-insert-xml-declaration-flag nil
))
3911 (concat org-e-odt-zip-dir
"content.xml") t
)))))
3912 (org-export-to-buffer 'e-odt out-buf subtreep visible-only body-only
)))))
3915 ;;;; Convert between OpenDocument and other formats
3917 (defun org-e-odt-reachable-p (in-fmt out-fmt
)
3918 "Return non-nil if IN-FMT can be converted to OUT-FMT."
3920 (let ((reachable-formats (org-e-odt-do-reachable-formats in-fmt
)))
3921 (dolist (e reachable-formats
)
3922 (let ((out-fmt-spec (assoc out-fmt
(cdr e
))))
3924 (throw 'done
(cons (car e
) out-fmt-spec
))))))))
3926 (defun org-e-odt-do-convert (in-file out-fmt
&optional prefix-arg
)
3927 "Workhorse routine for `org-e-odt-convert'."
3928 (require 'browse-url
)
3929 (let* ((in-file (expand-file-name (or in-file buffer-file-name
)))
3930 (dummy (or (file-readable-p in-file
)
3931 (error "Cannot read %s" in-file
)))
3932 (in-fmt (file-name-extension in-file
))
3933 (out-fmt (or out-fmt
(error "Output format unspecified")))
3934 (how (or (org-e-odt-reachable-p in-fmt out-fmt
)
3935 (error "Cannot convert from %s format to %s format?"
3937 (convert-process (car how
))
3938 (out-file (concat (file-name-sans-extension in-file
) "."
3939 (nth 1 (or (cdr how
) out-fmt
))))
3940 (extra-options (or (nth 2 (cdr how
)) ""))
3941 (out-dir (file-name-directory in-file
))
3942 (cmd (format-spec convert-process
3943 `((?i .
,(shell-quote-argument in-file
))
3944 (?I .
,(browse-url-file-url in-file
))
3947 (?O .
,(browse-url-file-url out-file
))
3948 (?d .
, (shell-quote-argument out-dir
))
3949 (?D .
,(browse-url-file-url out-dir
))
3950 (?x .
,extra-options
)))))
3951 (when (file-exists-p out-file
)
3952 (delete-file out-file
))
3954 (message "Executing %s" cmd
)
3955 (let ((cmd-output (shell-command-to-string cmd
)))
3956 (message "%s" cmd-output
))
3959 ((file-exists-p out-file
)
3960 (message "Exported to %s" out-file
)
3962 (message "Opening %s..." out-file
)
3963 (org-open-file out-file
'system
))
3966 (message "Export to %s failed" out-file
)
3969 (defun org-e-odt-do-reachable-formats (in-fmt)
3970 "Return verbose info about formats to which IN-FMT can be converted.
3971 Return a list where each element is of the
3972 form (CONVERTER-PROCESS . OUTPUT-FMT-ALIST). See
3973 `org-e-odt-convert-processes' for CONVERTER-PROCESS and see
3974 `org-e-odt-convert-capabilities' for OUTPUT-FMT-ALIST."
3976 (and org-e-odt-convert-process
3977 (cadr (assoc-string org-e-odt-convert-process
3978 org-e-odt-convert-processes t
))))
3980 (and org-e-odt-convert-process
3981 (cadr (assoc-string org-e-odt-convert-process
3982 org-e-odt-convert-processes t
))
3983 org-e-odt-convert-capabilities
))
3986 (dolist (c capabilities
)
3987 (when (member in-fmt
(nth 1 c
))
3988 (push (cons converter
(nth 2 c
)) reachable-formats
))))
3991 (defun org-e-odt-reachable-formats (in-fmt)
3992 "Return list of formats to which IN-FMT can be converted.
3993 The list of the form (OUTPUT-FMT-1 OUTPUT-FMT-2 ...)."
3995 (mapc (lambda (e) (add-to-list 'l e
))
3996 (apply 'append
(mapcar
3997 (lambda (e) (mapcar 'car
(cdr e
)))
3998 (org-e-odt-do-reachable-formats in-fmt
))))
4001 (defun org-e-odt-convert-read-params ()
4002 "Return IN-FILE and OUT-FMT params for `org-e-odt-do-convert'.
4003 This is a helper routine for interactive use."
4004 (let* ((input (if (featurep 'ido
) 'ido-completing-read
'completing-read
))
4005 (in-file (read-file-name "File to be converted: "
4006 nil buffer-file-name t
))
4007 (in-fmt (file-name-extension in-file
))
4008 (out-fmt-choices (org-e-odt-reachable-formats in-fmt
))
4010 (or (and out-fmt-choices
4011 (funcall input
"Output format: "
4012 out-fmt-choices nil nil nil
))
4014 "No known converter or no known output formats for %s files"
4016 (list in-file out-fmt
)))
4019 (defun org-e-odt-convert (&optional in-file out-fmt prefix-arg
)
4020 "Convert IN-FILE to format OUT-FMT using a command line converter.
4021 IN-FILE is the file to be converted. If unspecified, it defaults
4022 to variable `buffer-file-name'. OUT-FMT is the desired output
4023 format. Use `org-e-odt-convert-process' as the converter.
4024 If PREFIX-ARG is non-nil then the newly converted file is opened
4025 using `org-open-file'."
4027 (append (org-e-odt-convert-read-params) current-prefix-arg
))
4028 (org-e-odt-do-convert in-file out-fmt prefix-arg
))
4030 ;;; Library Initializations
4034 ;; Let Emacs open all OpenDocument files in archive mode
4035 (add-to-list 'auto-mode-alist
4036 (cons (concat "\\." (car desc
) "\\'") 'archive-mode
)))
4037 org-e-odt-file-extensions
)
4042 ;;;; Links whose description is image
4044 ;; (org-lparse-link-description-is-image
4045 ;; (format "\n<draw:a xlink:type=\"simple\" xlink:href=\"%s\">\n%s\n</draw:a>"
4049 (provide 'org-e-odt
)
4051 ;;; org-e-odt.el ends here