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 (macro . org-e-odt-macro
)
62 (paragraph . org-e-odt-paragraph
)
63 (plain-list . org-e-odt-plain-list
)
64 (plain-text . org-e-odt-plain-text
)
65 (planning . org-e-odt-planning
)
66 (property-drawer . org-e-odt-property-drawer
)
67 (quote-block . org-e-odt-quote-block
)
68 (quote-section . org-e-odt-quote-section
)
69 (radio-target . org-e-odt-radio-target
)
70 (section . org-e-odt-section
)
71 (special-block . org-e-odt-special-block
)
72 (src-block . org-e-odt-src-block
)
73 (statistics-cookie . org-e-odt-statistics-cookie
)
74 (strike-through . org-e-odt-strike-through
)
75 (subscript . org-e-odt-subscript
)
76 (superscript . org-e-odt-superscript
)
77 (table . org-e-odt-table
)
78 (table-cell . org-e-odt-table-cell
)
79 (table-row . org-e-odt-table-row
)
80 (target . org-e-odt-target
)
81 (template . org-e-odt-template
)
82 (timestamp . org-e-odt-timestamp
)
83 (underline . org-e-odt-underline
)
84 (verbatim . org-e-odt-verbatim
)
85 (verse-block . org-e-odt-verse-block
))
88 ((:odt-styles-file
"ODT_STYLES_FILE" nil nil t
)
89 (:LaTeX-fragments nil
"LaTeX" org-export-with-LaTeX-fragments
)))
96 ;;; Function Declarations
98 (declare-function org-id-find-id-file
"org-id" (id))
99 (declare-function hfy-face-to-style
"htmlfontify" (fn))
100 (declare-function hfy-face-or-def-to-name
"htmlfontify" (fn))
101 (declare-function archive-zip-extract
"arc-mode.el" (archive name
))
102 (declare-function org-create-math-formula
"org" (latex-frag &optional mathml-file
))
103 (declare-function browse-url-file-url
"browse-url" (file))
108 ;;; Internal Variables
110 (defconst org-e-odt-lib-dir
111 (file-name-directory load-file-name
)
112 "Location of ODT exporter.
113 Use this to infer values of `org-e-odt-styles-dir' and
114 `org-e-odt-schema-dir'.")
116 (defvar org-e-odt-data-dir
117 (expand-file-name "../../etc/" org-e-odt-lib-dir
)
118 "Data directory for ODT exporter.
119 Use this to infer values of `org-e-odt-styles-dir' and
120 `org-e-odt-schema-dir'.")
122 (defconst org-e-odt-special-string-regexps
123 '(("\\\\-" .
"­\\1") ; shy
124 ("---\\([^-]\\)" .
"—\\1") ; mdash
125 ("--\\([^-]\\)" .
"–\\1") ; ndash
126 ("\\.\\.\\." .
"…")) ; hellip
127 "Regular expressions for special string conversion.")
129 (defconst org-e-odt-schema-dir-list
131 (and org-e-odt-data-dir
132 (expand-file-name "./schema/" org-e-odt-data-dir
)) ; bail out
134 (and (boundp 'org-e-odt-data-dir
) org-e-odt-data-dir
; see make install
135 (expand-file-name "./schema/" org-e-odt-data-dir
))))
136 "List of directories to search for OpenDocument schema files.
137 Use this list to set the default value of
138 `org-e-odt-schema-dir'. The entries in this list are
139 populated heuristically based on the values of `org-e-odt-lib-dir'
140 and `org-e-odt-data-dir'.")
142 (defconst org-e-odt-styles-dir-list
144 (and org-e-odt-data-dir
145 (expand-file-name "./styles/" org-e-odt-data-dir
)) ; bail out
147 (and (boundp 'org-e-odt-data-dir
) org-e-odt-data-dir
; see make install
148 (expand-file-name "./styles/" org-e-odt-data-dir
)))
149 (expand-file-name "../../etc/styles/" org-e-odt-lib-dir
) ; git
150 (expand-file-name "./etc/styles/" org-e-odt-lib-dir
) ; elpa
151 (expand-file-name "./org/" data-directory
) ; system
153 "List of directories to search for OpenDocument styles files.
154 See `org-e-odt-styles-dir'. The entries in this list are populated
155 heuristically based on the values of `org-e-odt-lib-dir' and
156 `org-e-odt-data-dir'.")
158 (defconst org-e-odt-styles-dir
161 (message "Debug (org-e-odt): Searching for OpenDocument styles files...")
162 (mapc (lambda (styles-dir)
164 (message "Debug (org-e-odt): Trying %s..." styles-dir
)
165 (when (and (file-readable-p
167 "OrgOdtContentTemplate.xml" styles-dir
))
170 "OrgOdtStyles.xml" styles-dir
)))
171 (message "Debug (org-e-odt): Using styles under %s"
173 (throw 'styles-dir styles-dir
))))
174 org-e-odt-styles-dir-list
)
177 (error "Error (org-e-odt): Cannot find factory styles files. Aborting."))
179 "Directory that holds auxiliary XML files used by the ODT exporter.
181 This directory contains the following XML files -
182 \"OrgOdtStyles.xml\" and \"OrgOdtContentTemplate.xml\". These
183 XML files are used as the default values of
184 `org-e-odt-styles-file' and
185 `org-e-odt-content-template-file'.
187 The default value of this variable varies depending on the
188 version of org in use and is initialized from
189 `org-e-odt-styles-dir-list'. Note that the user could be using org
190 from one of: org's own private git repository, GNU ELPA tar or
193 (defconst org-e-odt-bookmark-prefix
"OrgXref.")
195 (defconst org-e-odt-manifest-file-entry-tag
196 "\n<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
198 (defconst org-e-odt-file-extensions
199 '(("odt" .
"OpenDocument Text")
200 ("ott" .
"OpenDocument Text Template")
201 ("odm" .
"OpenDocument Master Document")
202 ("ods" .
"OpenDocument Spreadsheet")
203 ("ots" .
"OpenDocument Spreadsheet Template")
204 ("odg" .
"OpenDocument Drawing (Graphics)")
205 ("otg" .
"OpenDocument Drawing Template")
206 ("odp" .
"OpenDocument Presentation")
207 ("otp" .
"OpenDocument Presentation Template")
208 ("odi" .
"OpenDocument Image")
209 ("odf" .
"OpenDocument Formula")
210 ("odc" .
"OpenDocument Chart")))
212 (defvar org-e-odt-table-style-format
214 <style:style style:name=\"%s\" style:family=\"table\">
215 <style:table-properties style:rel-width=\"%d%%\" fo:margin-top=\"0cm\" fo:margin-bottom=\"0.20cm\" table:align=\"center\"/>
218 "Template for auto-generated Table styles.")
220 (defvar org-e-odt-automatic-styles
'()
221 "Registry of automatic styles for various OBJECT-TYPEs.
222 The variable has the following form:
224 \(\(OBJECT-NAME-A.1 OBJECT-PROPS-A.1\)
225 \(OBJECT-NAME-A.2 OBJECT-PROPS-A.2\) ...\)\)
227 \(\(OBJECT-NAME-B.1 OBJECT-PROPS-B.1\)
228 \(OBJECT-NAME-B.2 OBJECT-PROPS-B.2\) ...\)\)
231 OBJECT-TYPEs could be \"Section\", \"Table\", \"Figure\" etc.
232 OBJECT-PROPS is (typically) a plist created by passing
233 \"#+ATTR_ODT: \" option to `org-e-odt-parse-block-attributes'.
235 Use `org-e-odt-add-automatic-style' to add update this variable.'")
237 (defvar org-e-odt-object-counters nil
238 "Running counters for various OBJECT-TYPEs.
239 Use this to generate automatic names and style-names. See
240 `org-e-odt-add-automatic-style'.")
242 (defvar org-e-odt-src-block-paragraph-format
243 "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
244 <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
245 <style:background-image/>
246 </style:paragraph-properties>
247 <style:text-properties fo:color=\"%s\"/>
249 "Custom paragraph style for colorized source and example blocks.
250 This style is much the same as that of \"OrgFixedWidthBlock\"
251 except that the foreground and background colors are set
252 according to the default face identified by the `htmlfontify'.")
254 (defvar hfy-optimisations
)
255 (defvar org-e-odt-embedded-formulas-count
0)
256 (defvar org-e-odt-entity-frame-styles
257 '(("As-CharImage" "__Figure__" ("OrgInlineImage" nil
"as-char"))
258 ("ParagraphImage" "__Figure__" ("OrgDisplayImage" nil
"paragraph"))
259 ("PageImage" "__Figure__" ("OrgPageImage" nil
"page"))
260 ("CaptionedAs-CharImage" "__Figure__"
262 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
263 ("OrgInlineImage" nil
"as-char"))
264 ("CaptionedParagraphImage" "__Figure__"
266 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
267 ("OrgImageCaptionFrame" nil
"paragraph"))
268 ("CaptionedPageImage" "__Figure__"
270 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
271 ("OrgPageImageCaptionFrame" nil
"page"))
272 ("InlineFormula" "__MathFormula__" ("OrgInlineFormula" nil
"as-char"))
273 ("DisplayFormula" "__MathFormula__" ("OrgDisplayFormula" nil
"as-char"))
274 ("CaptionedDisplayFormula" "__MathFormula__"
275 ("OrgCaptionedFormula" nil
"paragraph")
276 ("OrgFormulaCaptionFrame" nil
"as-char"))))
278 (defvar org-e-odt-embedded-images-count
0)
279 (defvar org-e-odt-image-size-probe-method
280 (append (and (executable-find "identify") '(imagemagick)) ; See Bug#10675
282 "Ordered list of methods for determining image sizes.")
284 (defvar org-e-odt-default-image-sizes-alist
285 '(("as-char" .
(5 .
0.4))
286 ("paragraph" .
(5 .
5)))
287 "Hardcoded image dimensions one for each of the anchor
290 ;; A4 page size is 21.0 by 29.7 cms
291 ;; The default page settings has 2cm margin on each of the sides. So
292 ;; the effective text area is 17.0 by 25.7 cm
293 (defvar org-e-odt-max-image-size
'(17.0 .
20.0)
294 "Limiting dimensions for an embedded image.")
296 (defvar org-e-odt-label-styles
297 '(("math-formula" "%c" "text" "(%n)")
298 ("math-label" "(%n)" "text" "(%n)")
299 ("category-and-value" "%e %n: %c" "category-and-value" "%e %n")
300 ("value" "%e %n: %c" "value" "%n"))
301 "Specify how labels are applied and referenced.
302 This is an alist where each element is of the
303 form (LABEL-STYLE-NAME LABEL-ATTACH-FMT LABEL-REF-MODE
306 LABEL-ATTACH-FMT controls how labels and captions are attached to
307 an entity. It may contain following specifiers - %e, %n and %c.
308 %e is replaced with the CATEGORY-NAME. %n is replaced with
309 \"<text:sequence ...> SEQNO </text:sequence>\". %c is replaced
310 with CAPTION. See `org-e-odt-format-label-definition'.
312 LABEL-REF-MODE and LABEL-REF-FMT controls how label references
313 are generated. The following XML is generated for a label
314 reference - \"<text:sequence-ref
315 text:reference-format=\"LABEL-REF-MODE\" ...> LABEL-REF-FMT
316 </text:sequence-ref>\". LABEL-REF-FMT may contain following
317 specifiers - %e and %n. %e is replaced with the CATEGORY-NAME.
318 %n is replaced with SEQNO. See
319 `org-e-odt-format-label-reference'.")
321 (defvar org-e-odt-category-map-alist
322 '(("__Table__" "Table" "value" "Table")
323 ("__Figure__" "Illustration" "value" "Figure")
324 ("__MathFormula__" "Text" "math-formula" "Equation")
325 ("__DvipngImage__" "Equation" "value" "Equation")
326 ("__Listing__" "Listing" "value" "Listing")
327 ;; ("__Table__" "Table" "category-and-value")
328 ;; ("__Figure__" "Figure" "category-and-value")
329 ;; ("__DvipngImage__" "Equation" "category-and-value")
331 "Map a CATEGORY-HANDLE to OD-VARIABLE and LABEL-STYLE.
332 This is a list where each entry is of the form \\(CATEGORY-HANDLE
333 OD-VARIABLE LABEL-STYLE CATEGORY-NAME\\). CATEGORY_HANDLE
334 identifies the captionable entity in question. OD-VARIABLE is
335 the OpenDocument sequence counter associated with the entity.
336 These counters are declared within
337 \"<text:sequence-decls>...</text:sequence-decls>\" block of
338 `org-e-odt-content-template-file'. LABEL-STYLE is a key into
339 `org-e-odt-label-styles' and specifies how a given entity should
340 be captioned and referenced. CATEGORY-NAME is used for
341 qualifying captions on export. You can modify the CATEGORY-NAME
342 used in the exported document by modifying
343 `org-export-dictionary'. For example, an embedded image in an
344 English document is captioned as \"Figure 1: Orgmode Logo\", by
345 default. If you want the image to be captioned as \"Illustration
346 1: Orgmode Logo\" instead, install an entry in
347 `org-export-dictionary' which translates \"Figure\" to
348 \"Illustration\" when the language is \"en\" and encoding is
351 (defvar org-e-odt-manifest-file-entries nil
)
352 (defvar hfy-user-sheet-assoc
)
354 (defvar org-e-odt-zip-dir nil
355 "Temporary work directory for OpenDocument exporter.")
359 ;;; User Configuration Variables
361 (defgroup org-export-e-odt nil
362 "Options for exporting Org mode files to ODT."
363 :tag
"Org Export ODT"
369 (defcustom org-e-odt-prettify-xml nil
370 "Specify whether or not the xml output should be prettified.
371 When this option is turned on, `indent-region' is run on all
372 component xml buffers before they are saved. Turn this off for
373 regular use. Turn this on if you need to examine the xml
375 :group
'org-export-e-odt
382 (defcustom org-e-odt-schema-dir
385 (message "Debug (org-e-odt): Searching for OpenDocument schema files...")
389 (message "Debug (org-e-odt): Trying %s..." schema-dir
)
390 (when (and (file-readable-p
391 (expand-file-name "od-manifest-schema-v1.2-cs01.rnc"
394 (expand-file-name "od-schema-v1.2-cs01.rnc"
397 (expand-file-name "schemas.xml" schema-dir
)))
398 (message "Debug (org-e-odt): Using schema files under %s"
400 (throw 'schema-dir schema-dir
))))
401 org-e-odt-schema-dir-list
)
402 (message "Debug (org-e-odt): No OpenDocument schema files installed")
405 "Directory that contains OpenDocument schema files.
407 This directory contains:
408 1. rnc files for OpenDocument schema
409 2. a \"schemas.xml\" file that specifies locating rules needed
410 for auto validation of OpenDocument XML files.
412 Use the customize interface to set this variable. This ensures
413 that `rng-schema-locating-files' is updated and auto-validation
414 of OpenDocument XML takes place based on the value
415 `rng-nxml-auto-validate-flag'.
417 The default value of this variable varies depending on the
418 version of org in use and is initialized from
419 `org-e-odt-schema-dir-list'. The OASIS schema files are available
420 only in the org's private git repository. It is *not* bundled
421 with GNU ELPA tar or standard Emacs distribution."
423 (const :tag
"Not set" nil
)
424 (directory :tag
"Schema directory"))
425 :group
'org-export-e-odt
429 "Set `org-e-odt-schema-dir'.
430 Also add it to `rng-schema-locating-files'."
431 (let ((schema-dir value
))
435 (expand-file-name "od-manifest-schema-v1.2-cs01.rnc" schema-dir
))
437 (expand-file-name "od-schema-v1.2-cs01.rnc" schema-dir
))
439 (expand-file-name "schemas.xml" schema-dir
)))
442 (message "Error (org-e-odt): %s has no OpenDocument schema files"
445 (when org-e-odt-schema-dir
446 (eval-after-load 'rng-loc
447 '(add-to-list 'rng-schema-locating-files
448 (expand-file-name "schemas.xml"
449 org-e-odt-schema-dir
))))))
454 (defcustom org-e-odt-content-template-file nil
455 "Template file for \"content.xml\".
456 The exporter embeds the exported content just before
457 \"</office:text>\" element.
459 If unspecified, the file named \"OrgOdtContentTemplate.xml\"
460 under `org-e-odt-styles-dir' is used."
462 :group
'org-export-e-odt
465 (defcustom org-e-odt-styles-file nil
466 "Default styles file for use with ODT export.
467 Valid values are one of:
469 2. path to a styles.xml file
470 3. path to a *.odt or a *.ott file
471 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
474 In case of option 1, an in-built styles.xml is used. See
475 `org-e-odt-styles-dir' for more information.
477 In case of option 3, the specified file is unzipped and the
478 styles.xml embedded therein is used.
480 In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
481 and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
482 generated odt file. Use relative path for specifying the
483 FILE-MEMBERS. styles.xml must be specified as one of the
486 Use options 1, 2 or 3 only if styles.xml alone suffices for
487 achieving the desired formatting. Use option 4, if the styles.xml
488 references additional files like header and footer images for
489 achieving the desired formatting.
491 Use \"#+ODT_STYLES_FILE: ...\" directive to set this variable on
492 a per-file basis. For example,
494 #+ODT_STYLES_FILE: \"/path/to/styles.xml\" or
495 #+ODT_STYLES_FILE: (\"/path/to/file.ott\" (\"styles.xml\" \"image/hdr.png\"))."
496 :group
'org-export-e-odt
500 (const :tag
"Factory settings" nil
)
501 (file :must-match t
:tag
"styles.xml")
502 (file :must-match t
:tag
"ODT or OTT file")
503 (list :tag
"ODT or OTT file + Members"
504 (file :must-match t
:tag
"ODF Text or Text Template file")
506 (file :tag
" Member" "styles.xml")
507 (repeat (file :tag
"Member"))))))
509 (defcustom org-e-odt-display-outline-level
2
510 "Outline levels considered for enumerating captioned entities."
511 :group
'org-export-e-odt
515 ;;;; Document conversion
517 (defcustom org-e-odt-convert-processes
519 "soffice --headless --convert-to %f%x --outdir %d %i")
521 "unoconv -f %f -o %d %i"))
522 "Specify a list of document converters and their usage.
523 The converters in this list are offered as choices while
524 customizing `org-e-odt-convert-process'.
526 This variable is a list where each element is of the
527 form (CONVERTER-NAME CONVERTER-CMD). CONVERTER-NAME is the name
528 of the converter. CONVERTER-CMD is the shell command for the
529 converter and can contain format specifiers. These format
530 specifiers are interpreted as below:
532 %i input file name in full
533 %I input file name as a URL
534 %f format of the output file
535 %o output file name in full
536 %O output file name as a URL
537 %d output dir in full
538 %D output dir as a URL.
539 %x extra options as set in `org-e-odt-convert-capabilities'."
540 :group
'org-export-e-odt
544 (const :tag
"None" nil
)
545 (alist :tag
"Converters"
546 :key-type
(string :tag
"Converter Name")
547 :value-type
(group (string :tag
"Command line")))))
549 (defcustom org-e-odt-convert-process
"LibreOffice"
550 "Use this converter to convert from \"odt\" format to other formats.
551 During customization, the list of converter names are populated
552 from `org-e-odt-convert-processes'."
553 :group
'org-export-e-odt
555 :type
'(choice :convert-widget
557 (apply 'widget-convert
(widget-type w
)
558 (eval (car (widget-get w
:args
)))))
559 `((const :tag
"None" nil
)
560 ,@(mapcar (lambda (c)
561 `(const :tag
,(car c
) ,(car c
)))
562 org-e-odt-convert-processes
))))
564 (defcustom org-e-odt-convert-capabilities
566 ("odt" "ott" "doc" "rtf" "docx")
567 (("pdf" "pdf") ("odt" "odt") ("rtf" "rtf") ("ott" "ott")
568 ("doc" "doc" ":\"MS Word 97\"") ("docx" "docx") ("html" "html")))
571 (("pdf" "pdf") ("odt" "odt") ("html" "html")))
573 ("ods" "ots" "xls" "csv" "xlsx")
574 (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv") ("ods" "ods")
575 ("xls" "xls") ("xlsx" "xlsx")))
577 ("odp" "otp" "ppt" "pptx")
578 (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("otp" "otp") ("ppt" "ppt")
579 ("pptx" "pptx") ("odg" "odg"))))
580 "Specify input and output formats of `org-e-odt-convert-process'.
581 More correctly, specify the set of input and output formats that
582 the user is actually interested in.
584 This variable is an alist where each element is of the
585 form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
586 INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
587 alist where each element is of the form (OUTPUT-FMT
588 OUTPUT-FILE-EXTENSION EXTRA-OPTIONS).
590 The variable is interpreted as follows:
591 `org-e-odt-convert-process' can take any document that is in
592 INPUT-FMT-LIST and produce any document that is in the
593 OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
594 OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
595 serves dual purposes:
596 - It is used for populating completion candidates during
597 `org-e-odt-convert' commands.
598 - It is used as the value of \"%f\" specifier in
599 `org-e-odt-convert-process'.
601 EXTRA-OPTIONS is used as the value of \"%x\" specifier in
602 `org-e-odt-convert-process'.
604 DOCUMENT-CLASS is used to group a set of file formats in
605 INPUT-FMT-LIST in to a single class.
607 Note that this variable inherently captures how LibreOffice based
608 converters work. LibreOffice maps documents of various formats
609 to classes like Text, Web, Spreadsheet, Presentation etc and
610 allow document of a given class (irrespective of it's source
611 format) to be converted to any of the export formats associated
614 See default setting of this variable for an typical
616 :group
'org-export-e-odt
620 (const :tag
"None" nil
)
621 (alist :tag
"Capabilities"
622 :key-type
(string :tag
"Document Class")
624 (group (repeat :tag
"Input formats" (string :tag
"Input format"))
625 (alist :tag
"Output formats"
626 :key-type
(string :tag
"Output format")
628 (group (string :tag
"Output file extension")
630 (const :tag
"None" nil
)
631 (string :tag
"Extra options"))))))))
633 (defcustom org-e-odt-preferred-output-format nil
634 "Automatically post-process to this format after exporting to \"odt\".
635 Interactive commands `org-export-as-e-odt' and
636 `org-export-as-e-odt-and-open' export first to \"odt\" format and
637 then use `org-e-odt-convert-process' to convert the
638 resulting document to this format. During customization of this
639 variable, the list of valid values are populated based on
640 `org-e-odt-convert-capabilities'."
641 :group
'org-export-e-odt
643 :type
'(choice :convert-widget
645 (apply 'widget-convert
(widget-type w
)
646 (eval (car (widget-get w
:args
)))))
647 `((const :tag
"None" nil
)
648 ,@(mapcar (lambda (c)
650 (org-e-odt-reachable-formats "odt")))))
655 (defcustom org-e-odt-format-drawer-function nil
656 "Function called to format a drawer in HTML code.
658 The function must accept two parameters:
659 NAME the drawer name, like \"LOGBOOK\"
660 CONTENTS the contents of the drawer.
662 The function should return the string to be exported.
664 For example, the variable could be set to the following function
665 in order to mimic default behaviour:
667 \(defun org-e-odt-format-drawer-default \(name contents\)
668 \"Format a drawer element for HTML export.\"
670 :group
'org-export-e-odt
676 (defcustom org-e-odt-format-headline-function nil
677 "Function to format headline text.
679 This function will be called with 5 arguments:
680 TODO the todo keyword \(string or nil\).
681 TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
682 PRIORITY the priority of the headline \(integer or nil\)
683 TEXT the main headline text \(string\).
684 TAGS the tags string, separated with colons \(string or nil\).
686 The function result will be used in the section format string.
688 As an example, one could set the variable to the following, in
689 order to reproduce the default set-up:
691 \(defun org-e-odt-format-headline \(todo todo-type priority text tags\)
692 \"Default format function for an headline.\"
694 \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo\)\)
696 \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
698 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
699 :group
'org-export-e-odt
705 (defcustom org-e-odt-format-inlinetask-function nil
706 "Function called to format an inlinetask in HTML code.
708 The function must accept six parameters:
709 TODO the todo keyword, as a string
710 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
711 PRIORITY the inlinetask priority, as a string
712 NAME the inlinetask name, as a string.
713 TAGS the inlinetask tags, as a string.
714 CONTENTS the contents of the inlinetask, as a string.
716 The function should return the string to be exported.
718 For example, the variable could be set to the following function
719 in order to mimic default behaviour:
721 \(defun org-e-odt-format-inlinetask \(todo type priority name tags contents\)
722 \"Format an inline task element for HTML export.\"
726 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
727 \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
729 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
730 \(format \(concat \"\\\\begin{center}\\n\"
732 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
734 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
736 \"\\\\end{minipage}}\"
737 \"\\\\end{center}\"\)
738 full-title contents\)\)"
739 :group
'org-export-e-odt
745 (defcustom org-e-odt-inline-image-rules
746 '(("file" .
"\\.\\(jpeg\\|jpg\\|png\\|gif\\)\\'"))
747 "Rules characterizing image files that can be inlined into HTML.
749 A rule consists in an association whose key is the type of link
750 to consider, and value is a regexp that will be matched against
753 Note that, by default, the image extension *actually* allowed
754 depend on the way the HTML file is processed. When used with
755 pdflatex, pdf, jpg and png images are OK. When processing
756 through dvi to Postscript, only ps and eps are allowed. The
757 default we use here encompasses both."
758 :group
'org-export-e-odt
759 :type
'(alist :key-type
(string :tag
"Type")
760 :value-type
(regexp :tag
"Path")))
762 (defcustom org-e-odt-pixels-per-inch display-pixels-per-inch
763 "Scaling factor for converting images pixels to inches.
764 Use this for sizing of embedded images. See Info node `(org)
765 Images in ODT export' for more information."
767 :group
'org-export-e-odt
773 (defcustom org-e-odt-quotes
775 ("\\(\\s-\\|[[(]\\|^\\)\"" .
"« ")
776 ("\\(\\S-\\)\"" .
"» ")
777 ("\\(\\s-\\|(\\|^\\)'" .
"'"))
779 ("\\(\\s-\\|[[(]\\|^\\)\"" .
"“")
780 ("\\(\\S-\\)\"" .
"”")
781 ("\\(\\s-\\|(\\|^\\)'" .
"‘")
782 ("\\(\\S-\\)'" .
"’")))
783 "Alist for quotes to use when converting english double-quotes.
785 The CAR of each item in this alist is the language code.
786 The CDR of each item in this alist is a list of three CONS:
787 - the first CONS defines the opening quote;
788 - the second CONS defines the closing quote;
789 - the last CONS defines single quotes.
791 For each item in a CONS, the first string is a regexp
792 for allowed characters before/after the quote, the second
793 string defines the replacement string for this quote."
794 :group
'org-export-e-odt
796 (cons :tag
"Opening quote"
797 (string :tag
"Regexp for char before")
798 (string :tag
"Replacement quote "))
799 (cons :tag
"Closing quote"
800 (string :tag
"Regexp for char after ")
801 (string :tag
"Replacement quote "))
802 (cons :tag
"Single quote"
803 (string :tag
"Regexp for char before")
804 (string :tag
"Replacement quote "))))
809 (defcustom org-e-odt-create-custom-styles-for-srcblocks t
810 "Whether custom styles for colorized source blocks be automatically created.
811 When this option is turned on, the exporter creates custom styles
812 for source blocks based on the advice of `htmlfontify'. Creation
813 of custom styles happen as part of `org-e-odt-hfy-face-to-css'.
815 When this option is turned off exporter does not create such
818 Use the latter option if you do not want the custom styles to be
819 based on your current display settings. It is necessary that the
820 styles.xml already contains needed styles for colorizing to work.
822 This variable is effective only if
823 `org-e-odt-fontify-srcblocks' is turned on."
824 :group
'org-export-e-odt
828 (defcustom org-e-odt-fontify-srcblocks t
829 "Specify whether or not source blocks need to be fontified.
830 Turn this option on if you want to colorize the source code
831 blocks in the exported file. For colorization to work, you need
832 to make available an enhanced version of `htmlfontify' library."
834 :group
'org-export-e-odt
840 (defcustom org-e-odt-table-caption-above t
841 "When non-nil, place caption string at the beginning of the table.
842 Otherwise, place it near the end."
843 :group
'org-export-e-odt
846 (defcustom org-e-odt-table-styles
847 '(("OrgEquation" "OrgEquation"
848 ((use-first-column-styles . t
)
849 (use-last-column-styles . t
))))
850 "Specify how Table Styles should be derived from a Table Template.
851 This is a list where each element is of the
852 form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
854 TABLE-STYLE-NAME is the style associated with the table through
855 \"#+ATTR_ODT: :style TABLE-STYLE-NAME\" line.
857 TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
858 TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
859 below) that is included in
860 `org-e-odt-content-template-file'.
862 TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
864 PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
866 TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
867 \"FirstRow\" | \"LastRow\" |
868 \"EvenRow\" | \"OddRow\" |
869 \"EvenColumn\" | \"OddColumn\" | \"\"
870 where \"+\" above denotes string concatenation.
872 TABLE-CELL-OPTIONS is an alist where each element is of the
873 form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
874 TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
875 `use-last-row-styles' |
876 `use-first-column-styles' |
877 `use-last-column-styles' |
878 `use-banding-rows-styles' |
879 `use-banding-columns-styles' |
880 `use-first-row-styles'
881 ON-OR-OFF := `t' | `nil'
883 For example, with the following configuration
885 \(setq org-e-odt-table-styles
886 '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
887 \(\(use-first-row-styles . t\)
888 \(use-first-column-styles . t\)\)\)
889 \(\"TableWithHeaderColumns\" \"Custom\"
890 \(\(use-first-column-styles . t\)\)\)\)\)
892 1. A table associated with \"TableWithHeaderRowsAndColumns\"
893 style will use the following table-cell styles -
894 \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
895 \"CustomTableCell\" and the following paragraph styles
896 \"CustomFirstRowTableParagraph\",
897 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
900 2. A table associated with \"TableWithHeaderColumns\" style will
901 use the following table-cell styles -
902 \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
903 following paragraph styles
904 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
907 Note that TABLE-TEMPLATE-NAME corresponds to the
908 \"<table:table-template>\" elements contained within
909 \"<office:styles>\". The entries (TABLE-STYLE-NAME
910 TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
911 \"table:template-name\" and \"table:use-first-row-styles\" etc
912 attributes of \"<table:table>\" element. Refer ODF-1.2
913 specification for more information. Also consult the
914 implementation filed under `org-e-odt-get-table-cell-styles'.
916 The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
917 formatting of numbered display equations. Do not delete this
918 style from the list."
919 :group
'org-export-e-odt
922 (const :tag
"None" nil
)
923 (repeat :tag
"Table Styles"
924 (list :tag
"Table Style Specification"
925 (string :tag
"Table Style Name")
926 (string :tag
"Table Template Name")
927 (alist :options
(use-first-row-styles
929 use-first-column-styles
930 use-last-column-styles
931 use-banding-rows-styles
932 use-banding-columns-styles
)
934 :value-type
(const :tag
"True" t
))))))
938 ;;; Internal functions
942 (defun org-e-odt--date (&optional org-ts fmt
)
945 (and (stringp org-ts
)
946 (string-match org-ts-regexp0 org-ts
)
948 (org-fix-decoded-time
949 (org-parse-time-string (match-string 0 org-ts
) t
)))))
952 (fmt (format-time-string fmt time
))
953 (t (setq date
(format-time-string "%Y-%m-%dT%H:%M:%S%z" time
))
954 (format "%s:%s" (substring date
0 -
2) (substring date -
2)))))))
958 (defun org-e-odt--frame (text width height style
&optional extra
962 (if width
(format " svg:width=\"%0.2fcm\"" width
) "")
963 (if height
(format " svg:height=\"%0.2fcm\"" height
) "")
965 (format " text:anchor-type=\"%s\"" (or anchor-type
"paragraph")))))
967 "\n<draw:frame draw:style-name=\"%s\"%s>\n%s\n</draw:frame>"
970 (let ((title (get-text-property 0 :title text
))
971 (desc (get-text-property 0 :description text
)))
973 (format "<svg:title>%s</svg:title>"
974 (org-e-odt-encode-plain-text title t
)))
976 (format "<svg:desc>%s</svg:desc>"
977 (org-e-odt-encode-plain-text desc t
)))))))))
979 ;;;; Library wrappers
981 (defun org-e-odt--adopt-elements (parent &rest children
)
983 (mapc (lambda (child)
984 (let ((parent-1 (org-element-adopt-element parent child nil
)))
985 (assert (eq parent-1 parent
))))
988 (defun org-e-odt--zip-extract (archive members target
)
989 (when (atom members
) (setq members
(list members
)))
990 (mapc (lambda (archive member target
)
992 (let* ((--quote-file-name
993 ;; This is shamelessly stolen from `archive-zip-extract'.
995 (if (or (not (memq system-type
'(windows-nt ms-dos
)))
996 (and (boundp 'w32-quote-process-args
)
997 (null w32-quote-process-args
)))
998 (shell-quote-argument name
)
1000 (target (funcall --quote-file-name target
))
1001 (archive (expand-file-name archive
))
1002 (archive-zip-extract
1003 (list "unzip" "-qq" "-o" "-d" target
))
1004 exit-code command-output
)
1005 (setq command-output
1007 (setq exit-code
(archive-zip-extract archive member
))
1009 (unless (zerop exit-code
)
1010 (message command-output
)
1011 (error "Extraction failed"))))
1016 (defun org-e-odt--textbox (text width height style
&optional
1019 (format "\n<draw:text-box %s>%s\n</draw:text-box>"
1020 (concat (format " fo:min-height=\"%0.2fcm\"" (or height
.2))
1022 (format " fo:min-width=\"%0.2fcm\"" (or width
.2))))
1024 width nil style extra anchor-type
))
1028 ;;;; Table of Contents
1030 (defun org-e-odt-begin-toc (index-title depth
)
1033 <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
1034 <text:table-of-content-source text:outline-level=\"%d\">
1035 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
1036 " depth index-title
)
1038 (let ((levels (number-sequence 1 10)))
1043 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
1044 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
1045 <text:index-entry-chapter/>
1046 <text:index-entry-text/>
1047 <text:index-entry-link-end/>
1048 </text:table-of-content-entry-template>
1049 " level level
)) levels
""))
1052 </text:table-of-content-source>
1055 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
1056 <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
1060 (defun org-e-odt-end-toc ()
1063 </text:table-of-content>
1068 (defun* org-e-odt-format-toc-headline
1069 (todo todo-type priority text tags
1070 &key level section-number headline-label
&allow-other-keys
)
1072 (and org-export-with-section-numbers
1073 (concat section-number
". "))
1078 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1081 (setq text
(format "<text:span text:style-name=\"%s\">%s</text:span>"
1083 (org-e-odt-format-link text
(concat "#" headline-label
) t
))
1085 (defun org-e-odt-toc (depth info
)
1086 (assert (wholenump depth
))
1087 (let* ((title (org-export-translate "Table of Contents" :utf-8 info
))
1088 (headlines (org-export-collect-headlines info depth
)))
1092 (org-e-odt-begin-toc title depth
)
1096 (let* ((entry (org-e-odt-format-headline--wrap
1097 headline info
'org-e-odt-format-toc-headline
))
1098 (level (org-export-get-relative-level headline info
))
1099 (style (format "Contents_20_%d" level
)))
1100 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1104 (org-e-odt-end-toc)))))
1107 ;;;; Document styles
1109 (defun org-e-odt-add-automatic-style (object-type &optional object-props
)
1110 "Create an automatic style of type OBJECT-TYPE with param OBJECT-PROPS.
1111 OBJECT-PROPS is (typically) a plist created by passing
1112 \"#+ATTR_ODT: \" option of the object in question to
1113 `org-e-odt-parse-block-attributes'.
1115 Use `org-e-odt-object-counters' to generate an automatic
1116 OBJECT-NAME and STYLE-NAME. If OBJECT-PROPS is non-nil, add a
1117 new entry in `org-e-odt-automatic-styles'. Return (OBJECT-NAME
1119 (assert (stringp object-type
))
1120 (let* ((object (intern object-type
))
1122 (seqno (1+ (or (plist-get org-e-odt-object-counters seqvar
) 0)))
1123 (object-name (format "%s%d" object-type seqno
)) style-name
)
1124 (setq org-e-odt-object-counters
1125 (plist-put org-e-odt-object-counters seqvar seqno
))
1127 (setq style-name
(format "Org%s" object-name
))
1128 (setq org-e-odt-automatic-styles
1129 (plist-put org-e-odt-automatic-styles object
1130 (append (list (list style-name object-props
))
1131 (plist-get org-e-odt-automatic-styles object
)))))
1132 (cons object-name style-name
)))
1135 ;;;; Caption and Labels
1138 (defun org-e-odt--wrap-label (element output
)
1139 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
1140 This function shouldn't be used for floats. See
1141 `org-e-odt--caption/label-string'."
1142 ;; (let ((label (org-element-property :name element)))
1143 ;; (if (or (not output) (not label) (string= output "") (string= label ""))
1145 ;; (concat (format "\\label{%s}\n" label) output)))
1149 (defun org-e-odt--caption/label-string
(caption label info
)
1150 "Return caption and label HTML string for floats.
1152 CAPTION is a cons cell of secondary strings, the car being the
1153 standard caption and the cdr its short form. LABEL is a string
1154 representing the label. INFO is a plist holding contextual
1157 If there's no caption nor label, return the empty string.
1159 For non-floats, see `org-e-odt--wrap-label'."
1160 (setq label nil
) ;; FIXME
1162 (let ((label-str (if label
(format "\\label{%s}" label
) "")))
1164 ((and (not caption
) (not label
)) "")
1165 ((not caption
) (format "\\label{%s}\n" label
))
1166 ;; Option caption format with short name.
1168 (format "\\caption[%s]{%s%s}\n"
1169 (org-export-data (cdr caption
) info
)
1171 (org-export-data (car caption
) info
)))
1172 ;; Standard caption format.
1173 ;; (t (format "\\caption{%s%s}\n"
1175 ;; (org-export-data (car caption) info)))
1176 (t (org-export-data (car caption
) info
)))))
1180 (defun org-e-odt--checkbox (item)
1181 "Return check-box string associated to ITEM."
1182 (let ((checkbox (org-element-property :checkbox item
)))
1183 (if (not checkbox
) ""
1184 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1185 "OrgCode" (case checkbox
1186 (on "[✓] ") ; CHECK MARK
1194 (defun org-e-odt-template (contents info
)
1195 "Return complete document string after HTML conversion.
1196 CONTENTS is the transcoded contents string. RAW-DATA is the
1197 original parsed data. INFO is a plist holding export options."
1199 (let ((title (org-export-data (plist-get info
:title
) info
))
1200 (author (let ((author (plist-get info
:author
)))
1201 (if (not author
) "" (org-export-data author info
))))
1202 (date (org-e-odt--date
1203 (org-export-data (plist-get info
:date
) info
)))
1204 (email (plist-get info
:email
))
1205 (keywords (plist-get info
:keywords
))
1206 (description (plist-get info
:description
)))
1209 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
1210 <office:document-meta
1211 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
1212 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
1213 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
1214 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
1215 xmlns:ooo=\"http://openoffice.org/2004/office\"
1216 office:version=\"1.2\">
1218 (format "<dc:creator>%s</dc:creator>\n" author
)
1219 (format "<meta:initial-creator>%s</meta:initial-creator>\n" author
)
1220 (format "<dc:date>%s</dc:date>\n" date
)
1221 (format "<meta:creation-date>%s</meta:creation-date>\n" date
)
1222 (format "<meta:generator>%s</meta:generator>\n"
1223 (let ((creator-info (plist-get info
:with-creator
)))
1224 (if (or (not creator-info
) (eq creator-info
'comment
)) ""
1225 (plist-get info
:creator
))))
1226 (format "<meta:keyword>%s</meta:keyword>\n" keywords
)
1227 (format "<dc:subject>%s</dc:subject>\n" description
)
1228 (format "<dc:title>%s</dc:title>\n" title
)
1230 " </office:meta>\n" "</office:document-meta>")
1231 nil
(concat org-e-odt-zip-dir
"meta.xml"))
1232 ;; Add meta.xml in to manifest.
1233 (org-e-odt-create-manifest-file-entry "text/xml" "meta.xml"))
1235 ;; Update styles file.
1236 ;; Copy styles.xml. Also dump htmlfontify styles, if there is any.
1237 ;; Write styles file.
1238 (let* ((styles-file (plist-get info
:odt-styles-file
))
1239 (styles-file (and styles-file
(read (org-trim styles-file
))))
1240 ;; Non-availability of styles.xml is not a critical
1241 ;; error. For now throw an error purely for aesthetic
1243 (styles-file (or styles-file
1244 org-e-odt-styles-file
1245 (expand-file-name "OrgOdtStyles.xml"
1246 org-e-odt-styles-dir
)
1247 (error "org-e-odt: Missing styles file?"))))
1249 ((listp styles-file
)
1250 (let ((archive (nth 0 styles-file
))
1251 (members (nth 1 styles-file
)))
1252 (org-e-odt--zip-extract archive members org-e-odt-zip-dir
)
1255 (when (org-file-image-p member
)
1256 (let* ((image-type (file-name-extension member
))
1257 (media-type (format "image/%s" image-type
)))
1258 (org-e-odt-create-manifest-file-entry media-type member
))))
1260 ((and (stringp styles-file
) (file-exists-p styles-file
))
1261 (let ((styles-file-type (file-name-extension styles-file
)))
1263 ((string= styles-file-type
"xml")
1264 (copy-file styles-file
(concat org-e-odt-zip-dir
"styles.xml") t
))
1265 ((member styles-file-type
'("odt" "ott"))
1266 (org-e-odt--zip-extract styles-file
"styles.xml" org-e-odt-zip-dir
)))))
1268 (error (format "Invalid specification of styles.xml file: %S"
1269 org-e-odt-styles-file
))))
1271 ;; create a manifest entry for styles.xml
1272 (org-e-odt-create-manifest-file-entry "text/xml" "styles.xml")
1274 ;; FIXME: Who is opening an empty styles.xml before this point?
1275 (with-current-buffer
1276 (find-file-noselect (concat org-e-odt-zip-dir
"styles.xml") t
)
1279 ;; Write custom styles for source blocks
1280 ;; Save STYLES used for colorizing of source blocks.
1281 ;; Update styles.xml with styles that were collected as part of
1282 ;; `org-e-odt-hfy-face-to-css' callbacks.
1283 (let ((styles (mapconcat (lambda (style) (format " %s\n" (cddr style
)))
1284 hfy-user-sheet-assoc
"")))
1286 (goto-char (point-min))
1287 (when (re-search-forward "</office:styles>" nil t
)
1288 (goto-char (match-beginning 0))
1289 (insert "\n<!-- Org Htmlfontify Styles -->\n" styles
"\n"))))
1291 ;; Update styles.xml - take care of outline numbering
1293 ;; Don't make automatic backup of styles.xml file. This setting
1294 ;; prevents the backed-up styles.xml file from being zipped in to
1295 ;; odt file. This is more of a hackish fix. Better alternative
1296 ;; would be to fix the zip command so that the output odt file
1297 ;; includes only the needed files and excludes any auto-generated
1298 ;; extra files like backups and auto-saves etc etc. Note that
1299 ;; currently the zip command zips up the entire temp directory so
1300 ;; that any auto-generated files created under the hood ends up in
1301 ;; the resulting odt file.
1302 (set (make-local-variable 'backup-inhibited
) t
)
1304 ;; Outline numbering is retained only upto LEVEL.
1305 ;; To disable outline numbering pass a LEVEL of 0.
1307 (goto-char (point-min))
1309 "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
1311 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1312 (while (re-search-forward regex nil t
)
1313 (unless (let ((sec-num (plist-get info
:section-numbers
))
1314 (level (string-to-number (match-string 2))))
1315 (if (wholenump sec-num
) (<= level sec-num
) sec-num
))
1316 (replace-match replacement t nil
))))
1318 ;; Update content.xml.
1320 (insert-file-contents
1321 (or org-e-odt-content-template-file
1322 (expand-file-name "OrgOdtContentTemplate.xml"
1323 org-e-odt-styles-dir
)))
1324 ;; Write automatic styles.
1325 ;; - Position the cursor.
1326 (goto-char (point-min))
1327 (re-search-forward " </office:automatic-styles>" nil t
)
1328 (goto-char (match-beginning 0))
1329 ;; - Dump automatic table styles
1330 (loop for
(style-name props
) in
1331 (plist-get org-e-odt-automatic-styles
'Table
) do
1332 (when (setq props
(or (plist-get props
:rel-width
) 96))
1333 (insert (format org-e-odt-table-style-format style-name props
))))
1334 ;; Update display level.
1335 ;; - Remove existing sequence decls. Also position the cursor.
1336 (goto-char (point-min))
1337 (when (re-search-forward "<text:sequence-decls" nil t
)
1338 (delete-region (match-beginning 0)
1339 (re-search-forward "</text:sequence-decls>" nil nil
)))
1340 ;; Update sequence decls according to user preference.
1343 "\n<text:sequence-decls>\n%s\n</text:sequence-decls>"
1347 "<text:sequence-decl text:display-outline-level=\"%d\" text:name=\"%s\"/>"
1348 org-e-odt-display-outline-level
(nth 1 x
)))
1349 org-e-odt-category-map-alist
"\n")))
1350 ;; Position the cursor to document body.
1351 (goto-char (point-min))
1352 (re-search-forward "</office:text>" nil nil
)
1353 (goto-char (match-beginning 0))
1355 ;; Preamble - Title, Author, Date etc.
1357 (let* ((title (org-export-data (plist-get info
:title
) info
))
1358 (author (and (plist-get info
:with-author
)
1359 (let ((auth (plist-get info
:author
)))
1360 (and auth
(org-export-data auth info
)))))
1361 (date (org-export-data (plist-get info
:date
) info
))
1362 (iso-date (org-e-odt--date date
))
1363 (date (org-e-odt--date date
"%d %b %Y"))
1364 (email (plist-get info
:email
))
1365 ;; switch on or off above vars based on user settings
1366 (author (and (plist-get info
:with-author
) (or author email
)))
1367 ;; (date (and (plist-get info :time-stamp-file) date))
1368 (email (and (plist-get info
:with-email
) email
)))
1373 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1374 "OrgTitle" (format "\n<text:title>%s</text:title>" title
))
1376 "\n<text:p text:style-name=\"OrgTitle\"/>"))
1378 ((and author
(not email
))
1381 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1383 (format "<text:initial-creator>%s</text:initial-creator>" author
))
1385 "\n<text:p text:style-name=\"OrgSubtitle\"/>"))
1389 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1391 (org-e-odt-format-link
1392 (format "<text:initial-creator>%s</text:initial-creator>" author
)
1393 (concat "mailto:" email
)))
1395 "\n<text:p text:style-name=\"OrgSubtitle\"/>")))
1400 "\n<text:p text:style-name=\"%s\">%s</text:p>"
1403 "\n<text:date style:data-style-name=\"%s\" text:date-value=\"%s\">%s</text:date>"
1405 "N75" iso-date date
))
1407 "<text:p text:style-name=\"OrgSubtitle\"/>")))))
1409 ;; Table of Contents
1410 (let ((depth (plist-get info
:with-toc
)))
1411 (when (wholenump depth
) (insert (org-e-odt-toc depth info
))))
1415 (buffer-substring-no-properties (point-min) (point-max))))
1419 ;;; Transcode Functions
1423 (defun org-e-odt-bold (bold contents info
)
1424 "Transcode BOLD from Org to ODT.
1425 CONTENTS is the text with bold markup. INFO is a plist holding
1426 contextual information."
1427 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1433 (defun org-e-odt-center-block (center-block contents info
)
1434 "Transcode a CENTER-BLOCK element from Org to ODT.
1435 CONTENTS holds the contents of the center block. INFO is a plist
1436 holding contextual information."
1437 (org-e-odt--wrap-label center-block contents
))
1442 (defun org-e-odt-clock (clock contents info
)
1443 "Transcode a CLOCK element from Org to ODT.
1444 CONTENTS is nil. INFO is a plist used as a communication
1446 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1447 "OrgTimestampWrapper"
1449 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1450 "OrgTimestampKeyword" org-clock-string
)
1451 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1453 (concat (org-translate-time
1454 (org-element-property :value clock
))
1455 (let ((time (org-element-property :time clock
)))
1456 (and time
(format " (%s)" time
))))))))
1461 (defun org-e-odt-code (code contents info
)
1462 "Transcode a CODE object from Org to ODT.
1463 CONTENTS is nil. INFO is a plist used as a communication
1465 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1466 "OrgCode" (org-element-property :value code
)))
1471 ;; Comments are ignored.
1476 ;; Comment Blocks are ignored.
1481 (defun org-e-odt-drawer (drawer contents info
)
1482 "Transcode a DRAWER element from Org to ODT.
1483 CONTENTS holds the contents of the block. INFO is a plist
1484 holding contextual information."
1485 (let* ((name (org-element-property :drawer-name drawer
))
1486 (output (if (functionp org-e-odt-format-drawer-function
)
1487 (funcall org-e-odt-format-drawer-function
1489 ;; If there's no user defined function: simply
1490 ;; display contents of the drawer.
1492 (org-e-odt--wrap-label drawer output
)))
1497 (defun org-e-odt-dynamic-block (dynamic-block contents info
)
1498 "Transcode a DYNAMIC-BLOCK element from Org to ODT.
1499 CONTENTS holds the contents of the block. INFO is a plist
1500 holding contextual information. See `org-export-data'."
1501 (org-e-odt--wrap-label dynamic-block contents
))
1506 (defun org-e-odt-entity (entity contents info
)
1507 "Transcode an ENTITY object from Org to ODT.
1508 CONTENTS are the definition itself. INFO is a plist holding
1509 contextual information."
1510 ;; (let ((ent (org-element-property :latex entity)))
1511 ;; (if (org-element-property :latex-math-p entity)
1512 ;; (format "$%s$" ent)
1514 (org-element-property :utf-8 entity
))
1519 (defun org-e-odt-example-block (example-block contents info
)
1520 "Transcode a EXAMPLE-BLOCK element from Org to ODT.
1521 CONTENTS is nil. INFO is a plist holding contextual information."
1522 (org-e-odt--wrap-label
1523 example-block
(org-e-odt-format-code example-block info
)))
1528 (defun org-e-odt-export-snippet (export-snippet contents info
)
1529 "Transcode a EXPORT-SNIPPET object from Org to ODT.
1530 CONTENTS is nil. INFO is a plist holding contextual information."
1531 (when (eq (org-export-snippet-backend export-snippet
) 'e-odt
)
1532 (org-element-property :value export-snippet
)))
1537 (defun org-e-odt-export-block (export-block contents info
)
1538 "Transcode a EXPORT-BLOCK element from Org to ODT.
1539 CONTENTS is nil. INFO is a plist holding contextual information."
1540 (when (string= (org-element-property :type export-block
) "ODT")
1541 (org-remove-indentation (org-element-property :value export-block
))))
1546 (defun org-e-odt-fixed-width (fixed-width contents info
)
1547 "Transcode a FIXED-WIDTH element from Org to ODT.
1548 CONTENTS is nil. INFO is a plist holding contextual information."
1549 (org-e-odt--wrap-label
1550 fixed-width
(org-e-odt-do-format-code
1551 (org-element-property :value fixed-width
))))
1554 ;;;; Footnote Definition
1556 ;; Footnote Definitions are ignored.
1559 ;;;; Footnote Reference
1561 (defun org-e-odt-footnote-reference (footnote-reference contents info
)
1562 "Transcode a FOOTNOTE-REFERENCE element from Org to ODT.
1563 CONTENTS is nil. INFO is a plist holding contextual information."
1564 (let ((--format-footnote-definition
1567 (setq n
(format "%d" n
))
1568 (let ((id (concat "fn" n
))
1569 (note-class "footnote")
1570 (par-style "Footnote"))
1572 "<text:note text:id=\"%s\" text:note-class=\"%s\">%s</text:note>"
1575 (format "<text:note-citation>%s</text:note-citation>" n
)
1576 (format "<text:note-body>%s</text:note-body>" def
)))))))
1577 (--format-footnote-reference
1580 (setq n
(format "%d" n
))
1581 (let ((note-class "footnote")
1583 (ref-name (concat "fn" n
)))
1585 "<text:span text:style-name=\"%s\">%s</text:span>"
1587 (format "<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:note-ref>"
1588 note-class ref-format ref-name n
)))))))
1590 ;; Insert separator between two footnotes in a row.
1591 (let ((prev (org-export-get-previous-element footnote-reference info
)))
1592 (and (eq (org-element-type prev
) 'footnote-reference
)
1593 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1594 "OrgSuperscript" ",")))
1595 ;; Trancode footnote reference.
1596 (let ((n (org-export-get-footnote-number footnote-reference info
)))
1598 ((not (org-export-footnote-first-reference-p footnote-reference info
))
1599 (funcall --format-footnote-reference n
))
1600 ;; Inline definitions are secondary strings.
1601 ;; Non-inline footnotes definitions are full Org data.
1603 (let* ((raw (org-export-get-footnote-definition footnote-reference
1605 (def (let ((def (org-trim (org-export-data raw info
))))
1606 (if (eq (org-element-type raw
) 'org-data
) def
1607 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1609 (funcall --format-footnote-definition n def
))))))))
1614 (defun* org-e-odt-format-headline
1615 (todo todo-type priority text tags
1616 &key level section-number headline-label
&allow-other-keys
)
1621 (let ((style (if (member todo org-done-keywords
) "OrgDone" "OrgTodo")))
1622 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1628 (concat "<text:tab/>"
1629 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1630 "OrgTag" (mapconcat 'org-trim tags
" : "))))))
1632 (defun org-e-odt-format-headline--wrap (headline info
1633 &optional format-function
1635 "Transcode an HEADLINE element from Org to ODT.
1636 CONTENTS holds the contents of the headline. INFO is a plist
1637 holding contextual information."
1638 (let* ((level (+ (org-export-get-relative-level headline info
)))
1639 (headline-number (org-export-get-headline-number headline info
))
1640 (section-number (and (org-export-numbered-headline-p headline info
)
1641 (mapconcat 'number-to-string
1642 headline-number
".")))
1643 (todo (and (plist-get info
:with-todo-keywords
)
1644 (let ((todo (org-element-property :todo-keyword headline
)))
1645 (and todo
(org-export-data todo info
)))))
1646 (todo-type (and todo
(org-element-property :todo-type headline
)))
1647 (priority (and (plist-get info
:with-priority
)
1648 (org-element-property :priority headline
)))
1649 (text (org-export-data (org-element-property :title headline
) info
))
1650 (tags (and (plist-get info
:with-tags
)
1651 (org-export-get-tags headline info
)))
1652 (headline-label (concat "sec-" (mapconcat 'number-to-string
1653 headline-number
"-")))
1654 (format-function (cond
1655 ((functionp format-function
) format-function
)
1656 ((functionp org-e-odt-format-headline-function
)
1658 (lambda (todo todo-type priority text tags
1660 (funcall org-e-odt-format-headline-function
1661 todo todo-type priority text tags
))))
1662 (t 'org-e-odt-format-headline
))))
1663 (apply format-function
1664 todo todo-type priority text tags
1665 :headline-label headline-label
:level level
1666 :section-number section-number extra-keys
)))
1669 (defun org-e-odt-begin-plain-list (ltype &optional continue-numbering
)
1670 (unless (member ltype
'(ordered unordered descriptive
))
1671 (error "Unknown list type: %s" ltype
))
1672 (let ((style-name (assoc-default ltype
1673 '((ordered .
"OrgNumberedList")
1674 (unordered .
"OrgBulletedList")
1675 (descriptive .
"OrgDescriptionList")))))
1676 (format "<text:list text:style-name=\"%s\" text:continue-numbering=\"%s\">"
1677 style-name
(if continue-numbering
"true" "false"))))
1679 (defun org-e-odt-headline (headline contents info
)
1680 "Transcode an HEADLINE element from Org to ODT.
1681 CONTENTS holds the contents of the headline. INFO is a plist
1682 holding contextual information."
1683 (let* ((numberedp (org-export-numbered-headline-p headline info
))
1684 ;; Get level relative to current parsed data.
1685 (level (org-export-get-relative-level headline info
))
1686 (text (org-export-data (org-element-property :title headline
) info
))
1687 ;; Create the headline text.
1688 (full-text (org-e-odt-format-headline--wrap headline info
)))
1690 ;; Case 1: This is a footnote section: ignore it.
1691 ((org-element-property :footnote-section-p headline
) nil
)
1692 ;; Case 2. This is a deep sub-tree: export it as a list item.
1693 ;; Also export as items headlines for which no section
1694 ;; format has been found.
1696 ;; ((org-export-low-level-p headline info)
1697 ;; ;; Build the real contents of the sub-tree.
1698 ;; (let* ((type (if numberedp 'unordered 'unordered)) ; FIXME
1699 ;; (itemized-body (org-e-odt-format-list-item
1700 ;; contents type nil nil full-text)))
1702 ;; (and (org-export-first-sibling-p headline info)
1703 ;; (org-e-odt-begin-plain-list type))
1705 ;; (and (org-export-last-sibling-p headline info)
1706 ;; "</text:list>"))))
1707 ;; Case 3. Standard headline. Export it as a section.
1709 (let* ((extra-ids (list (org-element-property :custom-id headline
)
1710 (org-element-property :id headline
)))
1711 (extra-ids nil
) ; FIXME
1712 (id (concat "sec-" (mapconcat 'number-to-string
1713 (org-export-get-headline-number
1714 headline info
) "-"))))
1717 "\n<text:h text:style-name=\"%s\" text:outline-level=\"%s\">%s%s</text:h>"
1718 (format "Heading_20_%s" level
)
1721 (mapconcat (lambda (x)
1723 (let ((x (if (org-uuidgen-p x
) (concat "ID-" x
) x
)))
1724 (org-e-odt-format-target
1725 "" (org-export-solidify-link-text x
)))))
1728 (org-e-odt-format-target full-text id
))
1732 ;;;; Horizontal Rule
1734 (defun org-e-odt-horizontal-rule (horizontal-rule contents info
)
1735 "Transcode an HORIZONTAL-RULE object from Org to ODT.
1736 CONTENTS is nil. INFO is a plist holding contextual information."
1737 (org-e-odt--wrap-label
1739 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1740 "Horizontal_20_Line" "")))
1743 ;;;; Inline Babel Call
1745 ;; Inline Babel Calls are ignored.
1748 ;;;; Inline Src Block
1750 (defun org-e-odt--find-verb-separator (s)
1751 "Return a character not used in string S.
1752 This is used to choose a separator for constructs like \\verb."
1753 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
1754 (loop for c across ll
1755 when
(not (string-match (regexp-quote (char-to-string c
)) s
))
1756 return
(char-to-string c
))))
1758 (defun org-e-odt-inline-src-block (inline-src-block contents info
)
1759 "Transcode an INLINE-SRC-BLOCK element from Org to ODT.
1760 CONTENTS holds the contents of the item. INFO is a plist holding
1761 contextual information."
1762 (let* ((org-lang (org-element-property :language inline-src-block
))
1763 (code (org-element-property :value inline-src-block
))
1764 (separator (org-e-odt--find-verb-separator code
)))
1770 (defun org-e-odt-inlinetask (inlinetask contents info
)
1771 "Transcode an INLINETASK element from Org to ODT.
1772 CONTENTS holds the contents of the block. INFO is a plist
1773 holding contextual information."
1775 ;; If `org-e-odt-format-inlinetask-function' is provided, call it
1776 ;; with appropriate arguments.
1777 ((functionp org-e-odt-format-inlinetask-function
)
1778 (let ((format-function
1780 (lambda (todo todo-type priority text tags
1781 &key contents
&allow-other-keys
)
1782 (funcall org-e-odt-format-inlinetask-function
1783 todo todo-type priority text tags contents
)))))
1784 (org-e-odt-format-headline--wrap
1785 inlinetask info format-function
:contents contents
)))
1786 ;; Otherwise, use a default template.
1787 (t (org-e-odt--wrap-label
1789 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1793 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1794 "OrgInlineTaskHeading"
1795 (org-e-odt-format-headline--wrap
1798 nil nil
"OrgInlineTaskFrame" " style:rel-width=\"100%\""))))))
1802 (defun org-e-odt-italic (italic contents info
)
1803 "Transcode ITALIC from Org to ODT.
1804 CONTENTS is the text with italic markup. INFO is a plist holding
1805 contextual information."
1806 (format "<text:span text:style-name=\"%s\">%s</text:span>"
1807 "Emphasis" contents
))
1812 (defun org-e-odt-item (item contents info
)
1813 "Transcode an ITEM element from Org to ODT.
1814 CONTENTS holds the contents of the item. INFO is a plist holding
1815 contextual information."
1816 (let* ((plain-list (org-export-get-parent item
))
1817 (type (org-element-property :type plain-list
))
1818 (counter (org-element-property :counter item
))
1819 (tag (let ((tag (org-element-property :tag item
)))
1821 (concat (org-e-odt--checkbox item
)
1822 (org-export-data tag info
))))))
1824 ((ordered unordered
)
1825 (format "\n<text:list-item>\n%s\n%s"
1827 (let* ((--element-has-a-table-p
1829 (lambda (element info
)
1830 (loop for el in
(org-element-contents element
)
1831 thereis
(eq (org-element-type el
) 'table
))))))
1833 ((funcall --element-has-a-table-p item info
)
1834 "</text:list-header>")
1835 (t "</text:list-item>")))))
1838 (let ((term (or tag
"(no term)")))
1840 (format "\n<text:list-item>\n%s\n</text:list-item>"
1841 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1842 "Text_20_body_20_bold" term
))
1844 "\n<text:list-item>\n%s\n</text:list-item>"
1845 (format "\n<text:list text:style-name=\"%s\" %s>\n%s\n</text:list>"
1846 "OrgDescriptionList"
1847 "text:continue-numbering=\"false\""
1848 (format "\n<text:list-item>\n%s\n</text:list-item>"
1850 (t (error "Unknown list type: %S" type
)))))
1855 (defun org-e-odt-keyword (keyword contents info
)
1856 "Transcode a KEYWORD element from Org to ODT.
1857 CONTENTS is nil. INFO is a plist holding contextual information."
1858 (let ((key (org-element-property :key keyword
))
1859 (value (org-element-property :value keyword
)))
1861 ((string= key
"LATEX") value
)
1862 ((string= key
"INDEX") (format "\\index{%s}" value
))
1863 ((string= key
"TARGET") nil
; FIXME
1864 ;; (format "\\label{%s}" (org-export-solidify-link-text value))
1866 ((string= key
"toc")
1867 (let ((value (downcase value
)))
1869 ((string-match "\\<headlines\\>" value
)
1870 (let ((depth (or (and (string-match "[0-9]+" value
)
1871 (string-to-number (match-string 0 value
)))
1872 (plist-get info
:with-toc
))))
1873 (when (wholenump depth
) (org-e-odt-toc depth info
))))
1874 ((string= "tables" value
) "FIXME")
1875 ((string= "figures" value
) "FIXME")
1876 ((string= "listings" value
)
1878 ;; At the moment, src blocks with a caption are wrapped
1879 ;; into a figure environment.
1883 ;;;; Latex Environment
1886 (eval-after-load 'org-odt
1887 '(ad-deactivate 'org-format-latex-as-mathml
))
1889 ;; (defadvice org-format-latex-as-mathml ; FIXME
1890 ;; (after org-e-odt-protect-latex-fragment activate)
1891 ;; "Encode LaTeX fragment as XML.
1892 ;; Do this when translation to MathML fails."
1893 ;; (when (or (not (> (length ad-return-value) 0))
1894 ;; (get-text-property 0 'org-protected ad-return-value))
1895 ;; (setq ad-return-value
1896 ;; (org-propertize (org-e-odt-encode-plain-text (ad-get-arg 0))
1897 ;; 'org-protected t))))
1899 (defun org-e-odt-format-latex (latex-frag processing-type info
)
1900 (let* ((prefix (case processing-type
1902 (mathml "ltxmathml/")))
1903 (input-file (plist-get info
:input-file
))
1905 (concat prefix
(file-name-sans-extension
1906 (file-name-nondirectory input-file
))))
1907 (cache-dir (file-name-directory input-file
))
1908 (display-msg (case processing-type
1909 (dvipng "Creating LaTeX Image...")
1910 (mathml "Creating MathML snippet..."))))
1913 (org-format-latex cache-subdir cache-dir nil display-msg
1914 nil nil processing-type
)
1917 (defun org-e-odt-latex-environment (latex-environment contents info
)
1918 "Transcode a LATEX-ENVIRONMENT element from Org to ODT.
1919 CONTENTS is nil. INFO is a plist holding contextual information."
1920 (org-e-odt--wrap-label
1923 (org-remove-indentation
1924 (org-element-property :value latex-environment
)))
1925 (processing-type (plist-get info
:LaTeX-fragments
))
1926 (caption (org-element-property :caption latex-environment
))
1927 (short-caption (and (cdr caption
)
1928 (org-export-data (cdr caption
) info
)))
1929 (caption (and (car caption
) (org-export-data (car caption
) info
)))
1930 (label (org-element-property :name latex-environment
))
1932 (label (org-element-property :name latex-environment
)))
1934 (when (memq processing-type
'(t mathjax
))
1935 (unless (and (fboundp 'org-format-latex-mathml-available-p
)
1936 (org-format-latex-mathml-available-p))
1937 (message "LaTeX to MathML converter not available. Trying dvinpng...")
1938 (setq processing-type
'dvipng
)))
1940 (when (eq processing-type
'dvipng
)
1941 (unless (and (org-check-external-command "latex" "" t
)
1942 (org-check-external-command "dvipng" "" t
))
1943 (message "LaTeX to PNG converter not available. Using verbatim.")
1944 (setq processing-type
'verbatim
)))
1946 (case processing-type
1948 (org-e-odt-format-formula latex-environment info
))
1950 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
1952 (org-e-odt-link--inline-image latex-environment info
)))
1953 (t (org-e-odt-do-format-code latex-frag
))))))
1959 ;; (when latex-frag ; FIXME
1960 ;; (setq href (org-propertize href :title "LaTeX Fragment"
1961 ;; :description latex-frag)))
1963 ;; provide descriptions
1965 (defun org-e-odt-latex-fragment (latex-fragment contents info
)
1966 "Transcode a LATEX-FRAGMENT object from Org to ODT.
1967 CONTENTS is nil. INFO is a plist holding contextual information."
1968 (let* ((latex-frag (org-element-property :value latex-fragment
))
1969 (processing-type (plist-get info
:LaTeX-fragments
)))
1971 ((member processing-type
'(t mathjax
))
1972 (org-e-odt-format-formula latex-fragment info
))
1973 ((eq processing-type
'dvipng
)
1974 (org-e-odt-link--inline-image latex-fragment info
))
1975 (t (org-e-odt-encode-plain-text latex-frag t
)))))
1980 (defun org-e-odt-line-break (line-break contents info
)
1981 "Transcode a LINE-BREAK object from Org to ODT.
1982 CONTENTS is nil. INFO is a plist holding contextual information."
1983 "<text:line-break/>\n")
1990 ;;;; Links :: Generic
1992 (defun org-e-odt-format-link (desc href
&optional suppress-xref
)
1994 ((and (= (string-to-char href
) ?
#) (not suppress-xref
))
1995 (setq href
(substring href
1))
1996 (let ((xref-format "text"))
1997 (when (numberp desc
)
1998 (setq desc
(format "%d" desc
) xref-format
"number"))
2000 (setq desc
(mapconcat 'number-to-string desc
".") xref-format
"chapter"))
2001 (setq href
(concat org-e-odt-bookmark-prefix href
))
2003 "<text:bookmark-ref text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:bookmark-ref>"
2004 xref-format href desc
)))
2005 ;; (org-lparse-link-description-is-image
2006 ;; (format "\n<draw:a xlink:type=\"simple\" xlink:href=\"%s\">\n%s\n</draw:a>"
2008 (t (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
2011 (defun org-e-odt-format-internal-link (text href
)
2012 (org-e-odt-format-link text
(concat "#" href
)))
2014 ;;;; Links :: Label references
2016 (defun org-e-odt-enumerate-element (element info
&optional predicate n
)
2017 (let* ((--numbered-parent-headline-at-<=-n
2019 (lambda (element n info
)
2020 (loop for x in
(org-export-get-genealogy element
)
2021 thereis
(and (eq (org-element-type x
) 'headline
)
2022 (<= (org-export-get-relative-level x info
) n
)
2023 (org-export-numbered-headline-p x info
)
2027 (lambda (element scope info
&optional predicate
)
2030 (or scope
(plist-get info
:parse-tree
))
2031 (org-element-type element
)
2033 (and (or (not predicate
) (funcall predicate el info
))
2037 info
'first-match
)))))
2038 (scope (funcall --numbered-parent-headline-at-
<=-n
2039 element
(or n org-e-odt-display-outline-level
) info
))
2040 (ordinal (funcall --enumerate element scope info predicate
))
2045 (mapconcat 'number-to-string
2046 (org-export-get-headline-number scope info
) "."))
2050 (number-to-string ordinal
))))
2053 (defun org-e-odt-format-label (element info op
)
2054 (let* ((caption-from
2055 (case (org-element-type element
)
2056 (link (org-export-get-parent-element element
))
2058 ;; get label and caption.
2059 (label (org-element-property :name caption-from
))
2060 (caption (org-element-property :caption caption-from
))
2061 (short-caption (cdr caption
))
2062 ;; transcode captions.
2063 (caption (and (car caption
) (org-export-data (car caption
) info
)))
2064 (short-caption (and short-caption
2065 (org-export-data short-caption info
))))
2066 (when (or label caption
)
2067 (let* ((default-category
2069 ((eq (org-element-type element
) 'table
)
2071 ((org-e-odt-standalone-image-p element info
)
2073 ((member (org-element-type element
)
2074 '(latex-environment latex-fragment
))
2075 (let ((processing-type (plist-get info
:LaTeX-fragments
)))
2077 ((eq processing-type
'dvipng
) "__DvipngImage__")
2078 ((eq processing-type
'mathjax
) "__MathFormula__")
2079 ((eq processing-type
't
) "__MathFormula__")
2080 (t (error "Handle LaTeX:verbatim")))))
2081 ((eq (org-element-type element
) 'src-block
)
2083 (t (error "Handle enumeration of %S" element
))))
2086 ((member (org-element-type element
)
2087 '(table latex-environment src-block
))
2089 ((org-e-odt-standalone-image-p element info
)
2090 'org-e-odt-standalone-image-p
)
2091 (t (error "Handle enumeration of %S" element
))))
2092 (seqno (org-e-odt-enumerate-element
2093 element info predicate
)) ; FIXME
2094 ;; handle label props.
2095 (label-props (assoc default-category org-e-odt-category-map-alist
))
2096 ;; identify opendocument counter
2097 (counter (nth 1 label-props
))
2098 ;; identify label style
2099 (label-style (nth 2 label-props
))
2100 ;; retrieve localized category sting
2101 (category (org-export-translate (nth 3 label-props
) :utf-8 info
)))
2104 ;; assign an internal label, if user has not provided one
2105 (setq label
(or label
(format "%s-%s" default-category seqno
)))
2106 (setq label
(org-export-solidify-link-text label
))
2110 (cadr (assoc-string label-style org-e-odt-label-styles t
))
2113 "<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">%s</text:sequence>"
2114 label counter counter seqno
))
2115 (?c .
,(or caption
""))))
2119 (setq label
(org-export-solidify-link-text label
))
2120 (let* ((fmt (cddr (assoc-string label-style org-e-odt-label-styles t
)))
2123 (format "<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:sequence-ref>"
2124 fmt1 label
(format-spec fmt2
`((?e .
,category
)
2126 (t (error "Unknow %S on label" op
)))))))
2128 ;;;; Links :: Embedded images
2130 (defun org-e-odt-copy-image-file (path)
2131 "Returns the internal name of the file"
2132 (let* ((image-type (file-name-extension path
))
2133 (media-type (format "image/%s" image-type
))
2134 (target-dir "Images/")
2136 (format "%s%04d.%s" target-dir
2137 (incf org-e-odt-embedded-images-count
) image-type
)))
2138 (message "Embedding %s as %s ..."
2139 (substring-no-properties path
) target-file
)
2141 (when (= 1 org-e-odt-embedded-images-count
)
2142 (make-directory (concat org-e-odt-zip-dir target-dir
))
2143 (org-e-odt-create-manifest-file-entry "" target-dir
))
2145 (copy-file path
(concat org-e-odt-zip-dir target-file
) 'overwrite
)
2146 (org-e-odt-create-manifest-file-entry media-type target-file
)
2149 (defun org-e-odt-image-size-from-file (file &optional user-width
2150 user-height scale dpi embed-as
)
2151 (let* ((--pixels-to-cms
2152 (function (lambda (pixels dpi
)
2153 (let ((cms-per-inch 2.54)
2154 (inches (/ pixels dpi
)))
2155 (* cms-per-inch inches
)))))
2158 (lambda (size-in-pixels dpi
)
2160 (cons (funcall --pixels-to-cms
(car size-in-pixels
) dpi
)
2161 (funcall --pixels-to-cms
(cdr size-in-pixels
) dpi
))))))
2162 (dpi (or dpi org-e-odt-pixels-per-inch
))
2163 (anchor-type (or embed-as
"paragraph"))
2164 (user-width (and (not scale
) user-width
))
2165 (user-height (and (not scale
) user-height
))
2168 (not (and user-height user-width
))
2171 (and (executable-find "identify")
2172 (let ((size-in-pixels
2173 (let ((dim (shell-command-to-string
2174 (format "identify -format \"%%w:%%h\" \"%s\""
2176 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim
)
2177 (cons (string-to-number (match-string 1 dim
))
2178 (string-to-number (match-string 2 dim
)))))))
2179 (funcall --size-in-cms size-in-pixels dpi
)))
2181 (let ((size-in-pixels
2182 (ignore-errors ; Emacs could be in batch mode
2184 (image-size (create-image file
) 'pixels
))))
2185 (funcall --size-in-cms size-in-pixels dpi
))
2186 ;; Use hard-coded values.
2187 (cdr (assoc-string anchor-type
2188 org-e-odt-default-image-sizes-alist
))
2190 (error "Cannot determine Image size. Aborting ..."))))
2191 (width (car size
)) (height (cdr size
)))
2194 (setq width
(* width scale
) height
(* height scale
)))
2195 ((and user-height user-width
)
2196 (setq width user-width height user-height
))
2198 (setq width
(* user-height
(/ width height
)) height user-height
))
2200 (setq height
(* user-width
(/ height width
)) width user-width
))
2202 ;; ensure that an embedded image fits comfortably within a page
2203 (let ((max-width (car org-e-odt-max-image-size
))
2204 (max-height (cdr org-e-odt-max-image-size
)))
2205 (when (or (> width max-width
) (> height max-height
))
2206 (let* ((scale1 (/ max-width width
))
2207 (scale2 (/ max-height height
))
2208 (scale (min scale1 scale2
)))
2209 (setq width
(* scale width
) height
(* scale height
)))))
2210 (cons width height
)))
2212 ;;;; Links :: Math formula
2214 (defun org-e-odt-format-formula (element info
)
2216 ((eq (org-element-type element
) 'link
) ; FIXME
2217 (let* ((type (org-element-property :type element
))
2218 (raw-path (org-element-property :path element
)))
2220 ((file-name-absolute-p raw-path
)
2221 (expand-file-name raw-path
))
2223 ((member (org-element-type element
)
2224 '(latex-fragment latex-environment
))
2225 (let* ((latex-frag (org-remove-indentation
2226 (org-element-property :value element
)))
2227 (formula-link (org-e-odt-format-latex
2228 latex-frag
'mathml info
)))
2230 (string-match "file:\\([^]]*\\)" formula-link
)
2231 (match-string 1 formula-link
))))
2232 (t (error "what is this?"))))
2233 (full-src (if (file-name-absolute-p src
) src
2234 (expand-file-name src
(file-name-directory
2235 (plist-get info
:input-file
)))))
2237 (case (org-element-type element
)
2238 (link (org-export-get-parent-element element
))
2240 (captions (org-e-odt-format-label caption-from info
'definition
))
2241 (caption (car captions
))
2243 (format "\n<draw:object %s xlink:href=\"%s\" xlink:type=\"simple\"/>"
2244 " xlink:show=\"embed\" xlink:actuate=\"onLoad\""
2245 (file-name-directory (org-e-odt-copy-formula-file full-src
))))
2246 (embed-as (if caption
'paragraph
'character
))
2249 ((eq embed-as
'character
)
2250 (org-e-odt-format-entity "InlineFormula" href width height
))
2252 (let* ((equation (org-e-odt-format-entity
2253 "CaptionedDisplayFormula" href width height captions
))
2255 (let* ((org-e-odt-category-map-alist
2256 '(("__Table__" "Table" "value")
2257 ("__Figure__" "Illustration" "value")
2258 ("__MathFormula__" "Text" "math-label")
2259 ("__DvipngImage__" "Equation" "value")
2260 ("__Listing__" "Listing" "value"))))
2261 (car (org-e-odt-format-label caption-from info
'definition
))))
2263 (org-e-odt--adopt-elements
2264 `(table (:type org
:attr_odt
(":style \"OrgEquation\"")))
2265 (org-e-odt--adopt-elements
2266 `(table-row (:type standard
))
2267 `(table-cell nil
"<c8>") `(table-cell nil
"<c1>"))
2268 (org-e-odt--adopt-elements
2269 `(table-row (:type standard
))
2270 (org-e-odt--adopt-elements
2271 `(table-cell nil
) `(export-block
2272 (:type
"ODT" :value
,equation
)))
2273 (org-e-odt--adopt-elements
2274 `(table-cell nil
) `(export-block
2275 (:type
"ODT" :value
,label
))))))
2277 (org-export-collect-tree-properties
2278 formula-tree
(org-export-get-environment 'e-odt
))))
2279 (org-export-data formula-tree formula-info
))))))
2281 (defun org-e-odt-copy-formula-file (src-file)
2282 "Returns the internal name of the file"
2283 (let* ((target-dir (format "Formula-%04d/"
2284 (incf org-e-odt-embedded-formulas-count
)))
2285 (target-file (concat target-dir
"content.xml")))
2286 ;; Create a directory for holding formula file. Also enter it in
2288 (make-directory (concat org-e-odt-zip-dir target-dir
))
2289 (org-e-odt-create-manifest-file-entry
2290 "application/vnd.oasis.opendocument.formula" target-dir
"1.2")
2291 ;; Copy over the formula file from user directory to zip
2293 (message "Embedding %s as %s ..." src-file target-file
)
2294 (let ((case-fold-search nil
))
2297 ((string-match "\\.\\(mathml\\|mml\\)\\'" src-file
)
2298 (copy-file src-file
(concat org-e-odt-zip-dir target-file
) 'overwrite
))
2299 ;; Case 2: OpenDocument formula.
2300 ((string-match "\\.odf\\'" src-file
)
2301 (org-e-odt--zip-extract src-file
"content.xml"
2302 (concat org-e-odt-zip-dir target-dir
)))
2303 (t (error "%s is not a formula file" src-file
))))
2304 ;; Enter the formula file in to manifest.
2305 (org-e-odt-create-manifest-file-entry "text/xml" target-file
)
2310 (defun org-e-odt-format-target (text id
)
2311 (let ((name (concat org-e-odt-bookmark-prefix id
)))
2313 (and id
(format "\n<text:bookmark-start text:name=\"%s\"/>" name
))
2314 (concat (and id
(format "\n<text:bookmark text:name=\"%s\"/>" id
)) text
)
2315 (and id
(format "\n<text:bookmark-end text:name=\"%s\"/>" name
)))))
2317 (defun org-e-odt-link--inline-image (element info
)
2318 "Return HTML code for an inline image.
2319 LINK is the link pointing to the inline image. INFO is a plist
2320 used as a communication channel."
2322 ((eq (org-element-type element
) 'link
)
2323 (let* ((type (org-element-property :type element
))
2324 (raw-path (org-element-property :path element
)))
2325 (cond ((member type
'("http" "https"))
2326 (concat type
":" raw-path
))
2327 ((file-name-absolute-p raw-path
)
2328 (expand-file-name raw-path
))
2330 ((member (org-element-type element
)
2331 '(latex-fragment latex-environment
))
2332 (let* ((latex-frag (org-remove-indentation
2333 (org-element-property :value element
)))
2334 (formula-link (org-e-odt-format-latex
2335 latex-frag
'dvipng info
)))
2337 (string-match "file:\\([^]]*\\)" formula-link
)
2338 (match-string 1 formula-link
))))
2339 (t (error "what is this?"))))
2340 (src-expanded (if (file-name-absolute-p src
) src
2341 (expand-file-name src
(file-name-directory
2342 (plist-get info
:input-file
)))))
2344 "\n<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>"
2345 (org-e-odt-copy-image-file src-expanded
)))
2346 ;; extract attributes from #+ATTR_ODT line.
2347 (attr-from (case (org-element-type element
)
2348 (link (org-export-get-parent-element element
))
2350 ;; convert attributes to a plist.
2351 (attr-plist (org-export-read-attribute :attr_odt attr-from
))
2352 ;; handle `:anchor', `:style' and `:attributes' properties.
2354 (car (assoc-string (plist-get attr-plist
:anchor
)
2355 '(("as-char") ("paragraph") ("page")) t
)))
2357 (and user-frame-anchor
(plist-get attr-plist
:style
)))
2359 (and user-frame-anchor
(plist-get attr-plist
:attributes
)))
2361 (list user-frame-style user-frame-attrs user-frame-anchor
))
2362 ;; (embed-as (or embed-as user-frame-anchor "paragraph"))
2364 ;; handle `:width', `:height' and `:scale' properties.
2365 (size (org-e-odt-image-size-from-file
2366 src-expanded
(plist-get attr-plist
:width
)
2367 (plist-get attr-plist
:height
)
2368 (plist-get attr-plist
:scale
) nil
;; embed-as
2371 (width (car size
)) (height (cdr size
))
2373 (case (org-element-type element
)
2374 ((org-e-odt-standalone-image-p element info
) "paragraph")
2375 (latex-fragment "as-char")
2376 (latex-environment "paragraph")
2378 (captions (org-e-odt-format-label element info
'definition
))
2379 (caption (car captions
)) (short-caption (cdr captions
))
2380 (entity (concat (and caption
"Captioned") embed-as
"Image")))
2381 (org-e-odt-format-entity entity href width height
2382 captions user-frame-params
)))
2384 (defun org-e-odt-format-entity (entity href width height
&optional
2385 captions user-frame-params
)
2386 (let* ((caption (car captions
)) (short-caption (cdr captions
))
2387 (entity-style (assoc-string entity org-e-odt-entity-frame-styles t
))
2388 default-frame-params frame-params
2389 (--merge-frame-params
2391 (lambda (default-frame-params user-frame-params
)
2392 (if (not user-frame-params
) default-frame-params
2393 (assert (= (length default-frame-params
) 3))
2394 (assert (= (length user-frame-params
) 3))
2395 (loop for user-frame-param in user-frame-params
2396 for default-frame-param in default-frame-params
2397 collect
(or user-frame-param default-frame-param
)))))))
2400 (setq default-frame-params
(nth 2 entity-style
))
2401 (setq frame-params
(funcall --merge-frame-params
2402 default-frame-params user-frame-params
))
2403 (apply 'org-e-odt--frame href width height frame-params
))
2405 (setq default-frame-params
(nth 3 entity-style
))
2406 (setq frame-params
(funcall --merge-frame-params
2407 default-frame-params user-frame-params
))
2408 (apply 'org-e-odt--textbox
2409 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2412 (apply 'org-e-odt--frame href width height
2413 (let ((entity-style-1 (copy-sequence
2414 (nth 2 entity-style
))))
2415 (setcar (cdr entity-style-1
)
2417 (cadr entity-style-1
)
2419 (format " draw:name=\"%s\" "
2423 width height frame-params
)))))
2425 (defun org-e-odt-standalone-image-p (element info
)
2426 "Test if ELEMENT is a standalone image for the purpose ODT export.
2427 INFO is a plist holding contextual information.
2429 Return non-nil, if ELEMENT is of type paragraph and it's sole
2430 content, save for whitespaces, is a link that qualifies as an
2433 Return non-nil, if ELEMENT is of type link and it's containing
2434 paragraph has no other content save for leading and trailing
2437 Return nil, otherwise.
2439 Bind `org-e-odt-standalone-image-predicate' to constrain
2440 paragraph further. For example, to check for only captioned
2441 standalone images, do the following.
2443 \(setq org-e-odt-standalone-image-predicate
2444 \(lambda \(paragraph\)
2445 \(org-element-property :caption paragraph\)\)\)
2447 (let ((--standalone-image-predicate
2448 (function (lambda (paragraph)
2449 (or (org-element-property :caption paragraph
)
2450 (org-element-property :name paragraph
)))))
2451 (paragraph (case (org-element-type element
)
2453 (link (and (org-export-inline-image-p
2454 element org-e-odt-inline-image-rules
)
2455 (org-export-get-parent element
)))
2458 (assert (eq (org-element-type paragraph
) 'paragraph
))
2459 (when (funcall --standalone-image-predicate paragraph
)
2460 (let ((contents (org-element-contents paragraph
)))
2461 (loop for x in contents
2462 with inline-image-count
= 0
2464 ((eq (org-element-type x
) 'plain-text
)
2465 (not (org-string-nw-p x
)))
2466 ((eq (org-element-type x
) 'link
)
2467 (when (org-export-inline-image-p
2468 x org-e-odt-inline-image-rules
)
2469 (= (incf inline-image-count
) 1)))
2472 (defun org-e-odt-link (link desc info
)
2473 "Transcode a LINK object from Org to ODT.
2475 DESC is the description part of the link, or the empty string.
2476 INFO is a plist holding contextual information. See
2478 (let* ((type (org-element-property :type link
))
2479 (raw-path (org-element-property :path link
))
2480 ;; Ensure DESC really exists, or set it to nil.
2481 (desc (and (not (string= desc
"")) desc
))
2482 (imagep (org-export-inline-image-p
2483 link org-e-odt-inline-image-rules
))
2485 ((member type
'("http" "https" "ftp" "mailto"))
2486 (concat type
":" raw-path
))
2487 ((string= type
"file")
2488 (when (string-match "\\(.+\\)::.+" raw-path
)
2489 (setq raw-path
(match-string 1 raw-path
)))
2490 (if (file-name-absolute-p raw-path
)
2491 (concat "file://" (expand-file-name raw-path
))
2492 (concat "file://" raw-path
)))
2497 ((and (not desc
) (org-export-inline-image-p
2498 link org-e-odt-inline-image-rules
))
2499 (org-e-odt-link--inline-image link info
))
2500 ;; Radio target: Transcode target's contents and use them as
2501 ;; link's description.
2502 ((string= type
"radio")
2503 (let ((destination (org-export-resolve-radio-link link info
)))
2505 (org-e-odt-format-internal-link
2506 (org-export-data (org-element-contents destination
) info
)
2507 (org-export-solidify-link-text path
)))))
2508 ;; Links pointing to an headline: Find destination and build
2509 ;; appropriate referencing command.
2510 ((member type
'("custom-id" "fuzzy" "id"))
2511 (let ((destination (if (string= type
"fuzzy")
2512 (org-export-resolve-fuzzy-link link info
)
2513 (org-export-resolve-id-link link info
))))
2514 (case (org-element-type destination
)
2515 ;; Fuzzy link points nowhere.
2517 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2518 "Emphasis" (or desc
(org-export-data
2519 (org-element-property
2520 :raw-link link
) info
))))
2521 ;; Fuzzy link points to an invisible target.
2523 ;; LINK points to an headline. Check if LINK should display
2526 (let* ((headline-no (org-export-get-headline-number destination info
))
2527 (label (format "sec-%s" (mapconcat 'number-to-string
2530 ;; Case 1: Headline is numbered and LINK has no
2531 ;; description or LINK's description matches
2532 ;; headline's title. Display section number.
2533 (if (and (org-export-numbered-headline-p destination info
)
2535 (string= desc
(org-element-property
2536 :raw-value destination
))))
2538 ;; Case 2: Either the headline is un-numbered or
2539 ;; LINK has a custom description. Display LINK's
2540 ;; description or headline's title.
2541 (or desc
(org-export-data (org-element-property
2542 :title destination
) info
)))))
2543 (org-e-odt-format-internal-link desc label
)))
2544 ;; Fuzzy link points to a target. Do as above.
2547 ;; (setq number (cond
2548 ;; ((org-e-odt-standalone-image-p destination info)
2549 ;; (org-export-get-ordinal
2550 ;; (assoc 'link (org-element-contents destination))
2551 ;; info 'link 'org-e-odt-standalone-image-p))
2552 ;; (t (org-export-get-ordinal destination info))))
2553 ;; (setq desc (when number
2554 ;; (if (atom number) (number-to-string number)
2555 ;; (mapconcat 'number-to-string number ".")))))
2557 (let ((label-reference
2558 (org-e-odt-format-label destination info
'reference
)))
2559 (assert label-reference
)
2560 label-reference
)))))
2561 ;; Coderef: replace link with the reference name or the
2562 ;; equivalent line number.
2563 ((string= type
"coderef")
2564 (let* ((fmt (org-export-get-coderef-format path desc
))
2565 (res (org-export-resolve-coderef path info
))
2566 (href (concat "#coderef-" path
)))
2567 (format fmt
(org-e-odt-format-link res href
))))
2568 ;; Link type is handled by a special function.
2569 ((functionp (setq protocol
(nth 2 (assoc type org-link-protocols
))))
2570 (funcall protocol
(org-link-unescape path
) desc
'odt
))
2571 ;; External link with a description part.
2572 ((and path desc
) (org-e-odt-format-link desc path
))
2573 ;; External link without a description part.
2574 (path (org-e-odt-format-link path path
))
2575 ;; No path, only description. Try to do something useful.
2576 (t (format "<text:span text:style-name=\"%s\">%s</text:span>"
2577 "Emphasis" desc
)))))
2582 ;; Babel Calls are ignored.
2587 (defun org-e-odt-macro (macro contents info
)
2588 "Transcode a MACRO element from Org to ODT.
2589 CONTENTS is nil. INFO is a plist holding contextual information."
2590 ;; Use available tools.
2591 (org-export-expand-macro macro info
))
2596 (defun org-e-odt-paragraph (paragraph contents info
)
2597 "Transcode a PARAGRAPH element from Org to ODT.
2598 CONTENTS is the contents of the paragraph, as a string. INFO is
2599 the plist used as a communication channel."
2600 (let* ((parent (org-export-get-parent paragraph
))
2601 (parent-type (org-element-type parent
))
2602 (style (case parent-type
2603 (quote-block "Quotations")
2604 (center-block "OrgCenter")
2605 (footnote-definition "Footnote")
2606 (t "Text_20_body"))))
2607 ;; If this paragraph is a leading paragraph in a non-descriptive
2608 ;; item and the item has a checkbox, splice the checkbox and
2609 ;; paragraph contents together.
2610 (when (and (eq (org-element-type parent
) 'item
)
2611 (not (eq (org-element-property :type
2612 (org-export-get-parent parent
))
2614 (eq paragraph
(car (org-element-contents parent
))))
2615 (setq contents
(concat (org-e-odt--checkbox parent
) contents
)))
2617 (format "\n<text:p text:style-name=\"%s\">%s</text:p>" style contents
)))
2622 (defun org-e-odt-plain-list (plain-list contents info
)
2623 "Transcode a PLAIN-LIST element from Org to ODT.
2624 CONTENTS is the contents of the list. INFO is a plist holding
2625 contextual information."
2626 (let* ((type (org-element-property :type plain-list
))
2627 (continue-numbering nil
))
2628 (assert (member type
'(ordered unordered descriptive
)))
2629 (org-e-odt--wrap-label
2631 (format "\n<text:list text:style-name=\"%s\" %s>\n%s</text:list>"
2632 (assoc-default type
'((ordered .
"OrgNumberedList")
2633 (unordered .
"OrgBulletedList")
2634 (descriptive .
"OrgDescriptionList")))
2635 ;; If top-level list, re-start numbering. Otherwise,
2636 ;; continue numbering.
2637 (format "text:continue-numbering=\"%s\""
2638 (let* ((parent (org-export-get-parent plain-list
)))
2639 (if (and parent
(eq (org-element-type parent
) 'item
))
2645 (defun org-e-odt-fill-tabs-and-spaces (line)
2646 (replace-regexp-in-string
2647 "\\([\t]\\|\\([ ]+\\)\\)"
2650 ((string= s
"\t") "<text:tab/>")
2651 (t (let ((n (length s
)))
2654 ((> n
1) (concat " " (format "<text:s text:c=\"%d\"/>" (1- n
))))
2658 (defun org-e-odt-encode-plain-text (text &optional no-whitespace-filling
)
2661 (setq text
(replace-regexp-in-string (car pair
) (cdr pair
) text t t
)))
2662 '(("&" .
"&") ("<" .
"<") (">" .
">")))
2663 (if no-whitespace-filling text
2664 (org-e-odt-fill-tabs-and-spaces text
)))
2666 (defun org-e-odt--quotation-marks (text info
)
2667 "Export quotation marks depending on language conventions.
2668 TEXT is a string containing quotation marks to be replaced. INFO
2669 is a plist used as a communication channel."
2672 (while (setq start
(string-match (car l
) text start
))
2673 (let ((new-quote (concat (match-string 1 text
) (cdr l
))))
2674 (setq text
(replace-match new-quote t t text
))))))
2675 (cdr (or (assoc (plist-get info
:language
) org-e-odt-quotes
)
2676 ;; Falls back on English.
2677 (assoc "en" org-e-odt-quotes
))))
2680 (defun org-e-odt-plain-text (text info
)
2681 "Transcode a TEXT string from Org to ODT.
2682 TEXT is the string to transcode. INFO is a plist holding
2683 contextual information."
2684 ;; Protect &, < and >.
2685 (setq text
(org-e-odt-encode-plain-text text t
))
2686 ;; Handle quotation marks
2687 (setq text
(org-e-odt--quotation-marks text info
))
2688 ;; Convert special strings.
2689 (when (plist-get info
:with-special-strings
)
2692 (setq text
(replace-regexp-in-string (car pair
) (cdr pair
) text t nil
)))
2693 org-e-odt-special-string-regexps
))
2694 ;; Handle break preservation if required.
2695 (when (plist-get info
:preserve-breaks
)
2696 (setq text
(replace-regexp-in-string
2697 "\\(\\\\\\\\\\)?[ \t]*\n" "<text:line-break/>\n" text t
)))
2704 (defun org-e-odt-planning (planning contents info
)
2705 "Transcode a PLANNING element from Org to ODT.
2706 CONTENTS is nil. INFO is a plist used as a communication
2708 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2709 "OrgTimestampWrapper"
2711 (let ((closed (org-element-property :closed planning
)))
2714 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2715 "OrgTimestampKeyword" org-closed-string
)
2716 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2717 "OrgTimestamp" (org-translate-time closed
)))))
2718 (let ((deadline (org-element-property :deadline planning
)))
2721 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2722 "OrgTimestampKeyword" org-deadline-string
)
2723 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2724 "OrgTimestamp" (org-translate-time deadline
)))))
2725 (let ((scheduled (org-element-property :scheduled planning
)))
2728 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2729 "OrgTimestampKeyword" org-scheduled-string
)
2730 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2731 "OrgTimestamp" (org-translate-time scheduled
))))))))
2734 ;;;; Property Drawer
2736 (defun org-e-odt-property-drawer (property-drawer contents info
)
2737 "Transcode a PROPERTY-DRAWER element from Org to ODT.
2738 CONTENTS is nil. INFO is a plist holding contextual
2740 ;; The property drawer isn't exported but we want separating blank
2741 ;; lines nonetheless.
2747 (defun org-e-odt-quote-block (quote-block contents info
)
2748 "Transcode a QUOTE-BLOCK element from Org to ODT.
2749 CONTENTS holds the contents of the block. INFO is a plist
2750 holding contextual information."
2751 (org-e-odt--wrap-label quote-block contents
))
2756 (defun org-e-odt-quote-section (quote-section contents info
)
2757 "Transcode a QUOTE-SECTION element from Org to ODT.
2758 CONTENTS is nil. INFO is a plist holding contextual information."
2759 (let ((value (org-remove-indentation
2760 (org-element-property :value quote-section
))))
2761 (when value
(org-e-odt-do-format-code value
))))
2767 (defun org-e-odt-format-section (text style
&optional name
)
2768 (let ((default-name (car (org-e-odt-add-automatic-style "Section"))))
2769 (format "\n<text:section text:style-name=\"%s\" %s>\n%s</text:section>"
2771 (format "text:name=\"%s\"" (or name default-name
))
2775 (defun org-e-odt-section (section contents info
) ; FIXME
2776 "Transcode a SECTION element from Org to ODT.
2777 CONTENTS holds the contents of the section. INFO is a plist
2778 holding contextual information."
2783 (defun org-e-odt-radio-target (radio-target text info
)
2784 "Transcode a RADIO-TARGET object from Org to ODT.
2785 TEXT is the text of the target. INFO is a plist holding
2786 contextual information."
2787 (org-e-odt-format-target
2788 text
(org-export-solidify-link-text
2789 (org-element-property :value radio-target
))))
2794 (defun org-e-odt-special-block (special-block contents info
)
2795 "Transcode a SPECIAL-BLOCK element from Org to ODT.
2796 CONTENTS holds the contents of the block. INFO is a plist
2797 holding contextual information."
2798 (let ((type (downcase (org-element-property :type special-block
)))
2799 (attributes (org-export-read-attribute :attr_odt special-block
)))
2800 (org-e-odt--wrap-label
2804 ((string= type
"annotation")
2805 (let ((author (or (plist-get attributes
:author
)
2806 (let ((author (plist-get info
:author
)))
2807 (and author
(org-export-data author info
)))))
2808 (date (or (plist-get attributes
:date
)
2809 (plist-get info
:date
))))
2811 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2813 (format "<office:annotation>\n%s\n</office:annotation>"
2816 (format "<dc:creator>%s</dc:creator>" author
))
2818 (format "<dc:date>%s</dc:date>"
2819 (org-e-odt--date date
)))
2822 ((string= type
"textbox")
2823 (let ((width (plist-get attributes
:width
))
2824 (height (plist-get attributes
:height
))
2825 (style (plist-get attributes
:style
))
2826 (extra (plist-get attributes
:extra
))
2827 (anchor (plist-get attributes
:anchor
)))
2828 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2829 "Text_20_body" (org-e-odt--textbox contents width height
2830 style extra anchor
))))
2837 (defun org-e-odt-hfy-face-to-css (fn)
2838 "Create custom style for face FN.
2839 When FN is the default face, use it's foreground and background
2840 properties to create \"OrgSrcBlock\" paragraph style. Otherwise
2841 use it's color attribute to create a character style whose name
2842 is obtained from FN. Currently all attributes of FN other than
2845 The style name for a face FN is derived using the following
2846 operations on the face name in that order - de-dash, CamelCase
2847 and prefix with \"OrgSrc\". For example,
2848 `font-lock-function-name-face' is associated with
2849 \"OrgSrcFontLockFunctionNameFace\"."
2850 (let* ((css-list (hfy-face-to-style fn
))
2851 (style-name ((lambda (fn)
2854 'capitalize
(split-string
2855 (hfy-face-or-def-to-name fn
) "-")
2857 (color-val (cdr (assoc "color" css-list
)))
2858 (background-color-val (cdr (assoc "background" css-list
)))
2859 (style (and org-e-odt-create-custom-styles-for-srcblocks
2862 (format org-e-odt-src-block-paragraph-format
2863 background-color-val color-val
))
2867 <style:style style:name=\"%s\" style:family=\"text\">
2868 <style:text-properties fo:color=\"%s\"/>
2869 </style:style>" style-name color-val
))))))
2870 (cons style-name style
)))
2872 (defun org-e-odt-htmlfontify-string (line)
2873 (let* ((hfy-html-quote-regex "\\([<\"&> ]\\)")
2874 (hfy-html-quote-map '(("\"" """)
2879 (" " "<text:tab/>")))
2880 (hfy-face-to-css 'org-e-odt-hfy-face-to-css
)
2881 (hfy-optimisations-1 (copy-seq hfy-optimisations
))
2882 (hfy-optimisations (add-to-list 'hfy-optimisations-1
2884 (hfy-begin-span-handler
2885 (lambda (style text-block text-id text-begins-block-p
)
2886 (insert (format "<text:span text:style-name=\"%s\">" style
))))
2887 (hfy-end-span-handler (lambda nil
(insert "</text:span>"))))
2888 (with-no-warnings (htmlfontify-string line
))))
2890 (defun org-e-odt-do-format-code
2891 (code &optional lang refs retain-labels num-start
)
2892 (let* ((lang (or (assoc-default lang org-src-lang-modes
) lang
))
2893 (lang-mode (and lang
(intern (format "%s-mode" lang
))))
2894 (code-lines (org-split-string code
"\n"))
2895 (code-length (length code-lines
))
2896 (use-htmlfontify-p (and (functionp lang-mode
)
2897 org-e-odt-fontify-srcblocks
2898 (require 'htmlfontify nil t
)
2899 (fboundp 'htmlfontify-string
)))
2900 (code (if (not use-htmlfontify-p
) code
2904 (font-lock-fontify-buffer)
2906 (fontifier (if use-htmlfontify-p
'org-e-odt-htmlfontify-string
2907 'org-e-odt-encode-plain-text
))
2908 (par-style (if use-htmlfontify-p
"OrgSrcBlock"
2909 "OrgFixedWidthBlock"))
2911 (assert (= code-length
(length (org-split-string code
"\n"))))
2913 (org-export-format-code
2915 (lambda (loc line-num ref
)
2917 (concat par-style
(and (= (incf i
) code-length
) "LastLine")))
2919 (setq loc
(concat loc
(and ref retain-labels
(format " (%s)" ref
))))
2920 (setq loc
(funcall fontifier loc
))
2922 (setq loc
(org-e-odt-format-target loc
(concat "coderef-" ref
))))
2924 (setq loc
(format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2926 (if (not line-num
) loc
2927 (format "\n<text:list-item>%s\n</text:list-item>" loc
)))
2930 ((not num-start
) code
)
2933 "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
2934 " text:continue-numbering=\"false\"" code
))
2937 "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
2938 " text:continue-numbering=\"true\"" code
)))))
2940 (defun org-e-odt-format-code (element info
)
2941 (let* ((lang (org-element-property :language element
))
2942 ;; Extract code and references.
2943 (code-info (org-export-unravel-code element
))
2944 (code (car code-info
))
2945 (refs (cdr code-info
))
2946 ;; Does the src block contain labels?
2947 (retain-labels (org-element-property :retain-labels element
))
2948 ;; Does it have line numbers?
2949 (num-start (case (org-element-property :number-lines element
)
2950 (continued (org-export-get-loc element info
))
2952 (org-e-odt-do-format-code code lang refs retain-labels num-start
)))
2954 (defun org-e-odt-src-block (src-block contents info
)
2955 "Transcode a SRC-BLOCK element from Org to ODT.
2956 CONTENTS holds the contents of the item. INFO is a plist holding
2957 contextual information."
2958 (let* ((lang (org-element-property :language src-block
))
2959 (caption (org-element-property :caption src-block
))
2960 (short-caption (and (cdr caption
)
2961 (org-export-data (cdr caption
) info
)))
2962 (caption (and (car caption
) (org-export-data (car caption
) info
)))
2963 (label (org-element-property :name src-block
))
2964 (attributes (org-export-read-attribute :attr_odt src-block
)))
2965 ;; FIXME: Handle caption
2966 ;; caption-str (when caption)
2967 ;; (main (org-export-data (car caption) info))
2968 ;; (secondary (org-export-data (cdr caption) info))
2969 ;; (caption-str (org-e-odt--caption/label-string caption label info))
2970 (let* ((captions (org-e-odt-format-label src-block info
'definition
))
2971 (caption (car captions
)) (short-caption (cdr captions
)))
2974 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2976 (let ((--src-block (org-e-odt-format-code src-block info
)))
2977 (if (not (plist-get attributes
:textbox
)) --src-block
2978 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
2980 (org-e-odt--textbox --src-block nil nil nil
))))))))
2983 ;;;; Statistics Cookie
2985 (defun org-e-odt-statistics-cookie (statistics-cookie contents info
)
2986 "Transcode a STATISTICS-COOKIE object from Org to ODT.
2987 CONTENTS is nil. INFO is a plist holding contextual information."
2988 (let ((cookie-value (org-element-property :value statistics-cookie
)))
2989 (format "<text:span text:style-name=\"%s\">%s</text:span>"
2990 "OrgCode" cookie-value
)))
2995 (defun org-e-odt-strike-through (strike-through contents info
)
2996 "Transcode STRIKE-THROUGH from Org to ODT.
2997 CONTENTS is the text with strike-through markup. INFO is a plist
2998 holding contextual information."
2999 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3000 "Strikethrough" contents
))
3005 (defun org-e-odt-subscript (subscript contents info
)
3006 "Transcode a SUBSCRIPT object from Org to ODT.
3007 CONTENTS is the contents of the object. INFO is a plist holding
3008 contextual information."
3009 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3010 "OrgSubscript" contents
))
3015 (defun org-e-odt-superscript (superscript contents info
)
3016 "Transcode a SUPERSCRIPT object from Org to ODT.
3017 CONTENTS is the contents of the object. INFO is a plist holding
3018 contextual information."
3019 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3020 "OrgSuperscript" contents
))
3025 (defun org-e-odt-table-style-spec (element info
)
3026 (let* ((table (org-export-get-parent-table element
))
3027 (table-attributes (org-export-read-attribute :attr_odt table
))
3028 (table-style (plist-get table-attributes
:style
)))
3029 (assoc table-style org-e-odt-table-styles
)))
3031 (defun org-e-odt-get-table-cell-styles (table-cell info
)
3032 "Retrieve styles applicable to a table cell.
3033 R and C are (zero-based) row and column numbers of the table
3034 cell. STYLE-SPEC is an entry in `org-e-odt-table-styles'
3035 applicable to the current table. It is `nil' if the table is not
3036 associated with any style attributes.
3038 Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
3040 When STYLE-SPEC is nil, style the table cell the conventional way
3041 - choose cell borders based on row and column groupings and
3042 choose paragraph alignment based on `org-col-cookies' text
3044 `org-e-odt-get-paragraph-style-cookie-for-table-cell'.
3046 When STYLE-SPEC is non-nil, ignore the above cookie and return
3047 styles congruent with the ODF-1.2 specification."
3048 (let* ((table-cell-address (org-export-table-cell-address table-cell info
))
3049 (r (car table-cell-address
)) (c (cdr table-cell-address
))
3050 (style-spec (org-e-odt-table-style-spec table-cell info
))
3051 (table-dimensions (org-export-table-dimensions
3052 (org-export-get-parent-table table-cell
)
3055 ;; LibreOffice - particularly the Writer - honors neither table
3056 ;; templates nor custom table-cell styles. Inorder to retain
3057 ;; inter-operability with LibreOffice, only automatic styles are
3058 ;; used for styling of table-cells. The current implementation is
3059 ;; congruent with ODF-1.2 specification and hence is
3060 ;; future-compatible.
3062 ;; Additional Note: LibreOffice's AutoFormat facility for tables -
3063 ;; which recognizes as many as 16 different cell types - is much
3064 ;; richer. Unfortunately it is NOT amenable to easy configuration
3066 (let* ((template-name (nth 1 style-spec
))
3067 (cell-style-selectors (nth 2 style-spec
))
3070 ((and (cdr (assoc 'use-first-column-styles cell-style-selectors
))
3071 (= c
0)) "FirstColumn")
3072 ((and (cdr (assoc 'use-last-column-styles cell-style-selectors
))
3073 (= (1+ c
) (cdr table-dimensions
)))
3075 ((and (cdr (assoc 'use-first-row-styles cell-style-selectors
))
3076 (= r
0)) "FirstRow")
3077 ((and (cdr (assoc 'use-last-row-styles cell-style-selectors
))
3078 (= (1+ r
) (car table-dimensions
)))
3080 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors
))
3081 (= (% r
2) 1)) "EvenRow")
3082 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors
))
3083 (= (% r
2) 0)) "OddRow")
3084 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors
))
3085 (= (% c
2) 1)) "EvenColumn")
3086 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors
))
3087 (= (% c
2) 0)) "OddColumn")
3089 (concat template-name cell-type
)))))
3091 (defun org-e-odt-table-cell (table-cell contents info
)
3092 "Transcode a TABLE-CELL element from Org to ODT.
3093 CONTENTS is nil. INFO is a plist used as a communication
3095 (let* ((table-cell-address (org-export-table-cell-address table-cell info
))
3096 (r (car table-cell-address
))
3097 (c (cdr table-cell-address
))
3098 (horiz-span (or (org-export-table-cell-width table-cell info
) 0))
3099 (table-row (org-export-get-parent table-cell
))
3100 (custom-style-prefix (org-e-odt-get-table-cell-styles
3104 (and custom-style-prefix
3105 (format "%sTableParagraph" custom-style-prefix
))
3108 ((and (= 1 (org-export-table-row-group table-row info
))
3109 (org-export-table-has-header-p
3110 (org-export-get-parent-table table-row
) info
))
3112 ((let* ((table (org-export-get-parent-table table-cell
))
3113 (table-attrs (org-export-read-attribute :attr_odt table
))
3114 (table-header-columns (plist-get table-attrs
3116 (<= c
(cond ((wholenump table-header-columns
)
3117 (- table-header-columns
1))
3118 (table-header-columns 0)
3121 (t "OrgTableContents"))
3122 (capitalize (symbol-name (org-export-table-cell-alignment
3123 table-cell info
))))))
3126 (and custom-style-prefix
(format "%sTableCell"
3127 custom-style-prefix
))
3130 (when (or (org-export-table-row-starts-rowgroup-p table-row info
)
3132 (when (org-export-table-row-ends-rowgroup-p table-row info
) "B")
3133 (when (and (org-export-table-cell-starts-colgroup-p table-cell info
)
3134 (not (zerop c
)) ) "L"))))
3137 (format " table:style-name=\"%s\"" cell-style-name
)
3138 (and (> horiz-span
0)
3139 (format " table:number-columns-spanned=\"%d\""
3140 (1+ horiz-span
))))))
3141 (unless contents
(setq contents
""))
3143 (assert paragraph-style
)
3144 (format "\n<table:table-cell%s>\n%s\n</table:table-cell>"
3146 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3147 paragraph-style contents
))
3149 (dotimes (i horiz-span s
)
3150 (setq s
(concat s
"\n<table:covered-table-cell/>"))))
3156 (defun org-e-odt-table-row (table-row contents info
)
3157 "Transcode a TABLE-ROW element from Org to ODT.
3158 CONTENTS is the contents of the row. INFO is a plist used as a
3159 communication channel."
3160 ;; Rules are ignored since table separators are deduced from
3161 ;; borders of the current row.
3162 (when (eq (org-element-property :type table-row
) 'standard
)
3163 (let* ((rowgroup-tags
3164 (if (and (= 1 (org-export-table-row-group table-row info
))
3165 (org-export-table-has-header-p
3166 (org-export-get-parent-table table-row
) info
))
3167 ;; If the row belongs to the first rowgroup and the
3168 ;; table has more than one row groups, then this row
3169 ;; belongs to the header row group.
3170 '("\n<table:table-header-rows>" .
"\n</table:table-header-rows>")
3171 ;; Otherwise, it belongs to non-header row group.
3172 '("\n<table:table-rows>" .
"\n</table:table-rows>"))))
3174 ;; Does this row begin a rowgroup?
3175 (when (org-export-table-row-starts-rowgroup-p table-row info
)
3176 (car rowgroup-tags
))
3178 (format "\n<table:table-row>\n%s\n</table:table-row>" contents
)
3179 ;; Does this row end a rowgroup?
3180 (when (org-export-table-row-ends-rowgroup-p table-row info
)
3181 (cdr rowgroup-tags
))))))
3186 (defun org-e-odt-table-first-row-data-cells (table info
)
3191 (unless (eq (org-element-property :type row
) 'rule
) row
))
3193 (special-column-p (org-export-table-has-special-column-p table
)))
3194 (if (not special-column-p
) (org-element-contents table-row
)
3195 (cdr (org-element-contents table-row
)))))
3197 (defun org-e-odt--table (table contents info
)
3198 "Transcode a TABLE element from Org to ODT.
3199 CONTENTS is the contents of the table. INFO is a plist holding
3200 contextual information."
3201 (case (org-element-property :type table
)
3202 ;; Case 1: table.el doesn't support export to OD format. Strip
3203 ;; such tables from export.
3208 "(org-e-odt): Found table.el-type table in the source Org file."
3209 " table.el doesn't support export to ODT format."
3210 " Stripping the table from export."))))
3211 ;; Case 2: Native Org tables.
3213 (let* ((captions (org-e-odt-format-label table info
'definition
))
3214 (caption (car captions
)) (short-caption (cdr captions
))
3215 (attributes (org-export-read-attribute :attr_odt table
))
3216 (custom-table-style (nth 1 (org-e-odt-table-style-spec table info
)))
3219 (lambda (table info
)
3220 (let* ((table-style (or custom-table-style
"OrgTable"))
3221 (column-style (format "%sColumn" table-style
)))
3223 (lambda (table-cell)
3224 (let ((width (1+ (or (org-export-table-cell-width
3225 table-cell info
) 0)))
3227 "\n<table:table-column table:style-name=\"%s\"/>"
3230 (dotimes (i width out
) (setq out
(concat s out
)))))
3231 (org-e-odt-table-first-row-data-cells table info
) "\n"))))))
3235 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3238 (let* ((automatic-name
3239 (org-e-odt-add-automatic-style "Table" attributes
)))
3241 "\n<table:table table:name=\"%s\" table:style-name=\"%s\">"
3242 (or short-caption
(car automatic-name
))
3243 (or custom-table-style
(cdr automatic-name
) "OrgTable")))
3244 ;; column specification.
3245 (funcall table-column-specs table info
)
3249 "</table:table>")))))
3251 (defun org-e-odt-table (table contents info
)
3252 "Transcode a TABLE element from Org to ODT.
3253 CONTENTS is the contents of the table. INFO is a plist holding
3254 contextual information."
3255 (let* ((--get-previous-elements
3258 (let ((parent (org-export-get-parent blob
)))
3259 (cdr (member blob
(reverse (org-element-contents parent
))))))))
3260 (--element-preceded-by-table-p
3262 (lambda (element info
)
3263 (loop for el in
(funcall --get-previous-elements element info
)
3264 thereis
(eq (org-element-type el
) 'table
)))))
3265 (--walk-list-genealogy-and-collect-tags
3267 (lambda (table info
)
3268 (let* ((genealogy (org-export-get-genealogy table
))
3270 (when (eq (org-element-type (car genealogy
)) 'item
)
3271 (loop for el in genealogy
3272 when
(member (org-element-type el
)
3275 (loop for el in list-genealogy
3276 with parent-list collect
3277 (case (org-element-type el
)
3279 (setq parent-list el
)
3281 .
,(let ((type (org-element-property :type el
)))
3283 "<text:list text:style-name=\"%s\" %s>"
3285 type
'((ordered .
"OrgNumberedList")
3286 (unordered .
"OrgBulletedList")
3287 (descriptive .
"OrgDescriptionList")))
3288 "text:continue-numbering=\"true\""))))
3292 (if (funcall --element-preceded-by-table-p table info
)
3293 '("</text:list-header>" .
"<text:list-header>")
3294 '("</text:list-item>" .
"<text:list-header>")))
3295 ((funcall --element-preceded-by-table-p
3297 '("</text:list-header>" .
"<text:list-header>"))
3298 (t '("</text:list-item>" .
"<text:list-item>"))))))))))
3299 (close-open-tags (funcall --walk-list-genealogy-and-collect-tags
3301 ;; OpenDocument schema does not permit table to occur within a
3302 ;; list item. So, to typeset an indented table, we make use of
3303 ;; list continuations.
3305 ;; Discontinue the list.
3306 (mapconcat 'car close-open-tags
"\n")
3307 ;; Put the table in an indented section.
3308 (let* ((table (org-e-odt--table table contents info
))
3309 (level (/ (length (mapcar 'car close-open-tags
)) 2))
3310 (style (format "OrgIndentedSection-Level-%d" level
)))
3311 (when table
(org-e-odt-format-section table style
)))
3312 ;; Continue the list.
3313 (mapconcat 'cdr
(nreverse close-open-tags
) "\n"))))
3318 (defun org-e-odt-target (target contents info
)
3319 "Transcode a TARGET object from Org to ODT.
3320 CONTENTS is nil. INFO is a plist holding contextual
3322 (org-e-odt-format-target
3323 "" (org-export-solidify-link-text (org-element-property :value target
))))
3328 (defun org-e-odt-timestamp (timestamp contents info
)
3329 "Transcode a TIMESTAMP object from Org to ODT.
3330 CONTENTS is nil. INFO is a plist used as a communication
3332 (let ((timestamp-1 (org-element-property :value timestamp
))
3333 (timestamp-2 (org-element-property :range-end timestamp
)))
3334 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3335 "OrgTimestampWrapper"
3337 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3338 "OrgTimestamp" (org-translate-time timestamp-1
))
3341 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3342 "OrgTimestamp" (org-translate-time timestamp-2
)))))))
3347 (defun org-e-odt-underline (underline contents info
)
3348 "Transcode UNDERLINE from Org to ODT.
3349 CONTENTS is the text with underline markup. INFO is a plist
3350 holding contextual information."
3351 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3352 "Underline" contents
))
3357 (defun org-e-odt-verbatim (verbatim contents info
)
3358 "Transcode a VERBATIM object from Org to ODT.
3359 CONTENTS is nil. INFO is a plist used as a communication
3361 (format "<text:span text:style-name=\"%s\">%s</text:span>"
3362 "OrgCode" (org-element-property :value verbatim
)))
3367 (defun org-e-odt-verse-block (verse-block contents info
)
3368 "Transcode a VERSE-BLOCK element from Org to ODT.
3369 CONTENTS is verse block contents. INFO is a plist holding
3370 contextual information."
3371 ;; Add line breaks to each line of verse.
3372 (setq contents
(replace-regexp-in-string
3373 "\\(<text:line-break/>\\)?[ \t]*\n"
3374 "<text:line-break/>" contents
))
3375 ;; Replace tabs and spaces.
3376 (setq contents
(org-e-odt-fill-tabs-and-spaces contents
))
3377 ;; Surround it in a verse environment.
3378 (org-e-odt--wrap-label
3380 (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
3381 "OrgVerse" contents
)))
3387 ;;; Interactive functions
3389 (defun org-e-odt-create-manifest-file-entry (&rest args
)
3390 (push args org-e-odt-manifest-file-entries
))
3392 (defun org-e-odt-write-manifest-file ()
3393 (make-directory (concat org-e-odt-zip-dir
"META-INF"))
3394 (let ((manifest-file (concat org-e-odt-zip-dir
"META-INF/manifest.xml")))
3395 (with-current-buffer
3396 (let ((nxml-auto-insert-xml-declaration-flag nil
))
3397 (find-file-noselect manifest-file t
))
3399 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
3400 <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
3402 (lambda (file-entry)
3403 (let* ((version (nth 2 file-entry
))
3404 (extra (if (not version
) ""
3405 (format " manifest:version=\"%s\"" version
))))
3407 (format org-e-odt-manifest-file-entry-tag
3408 (nth 0 file-entry
) (nth 1 file-entry
) extra
))))
3409 org-e-odt-manifest-file-entries
)
3410 (insert "\n</manifest:manifest>"))))
3412 (defmacro org-e-odt--export-wrap
(out-file &rest body
)
3413 `(let* ((--out-file ,out-file
)
3414 (out-file-type (file-name-extension --out-file
))
3415 (org-e-odt-xml-files '("META-INF/manifest.xml" "content.xml"
3416 "meta.xml" "styles.xml"))
3417 ;; Initialize workarea. All files that end up in the
3418 ;; exported get created here.
3419 (org-e-odt-zip-dir (file-name-as-directory
3420 (make-temp-file (format "%s-" out-file-type
) t
)))
3421 (org-e-odt-manifest-file-entries nil
)
3422 (--cleanup-xml-buffers
3425 ;; Kill all XML buffers.
3426 (mapc (lambda (file)
3427 (let ((buf (get-file-buffer
3428 (concat org-e-odt-zip-dir file
))))
3430 (set-buffer-modified-p nil
)
3431 (kill-buffer buf
))))
3432 org-e-odt-xml-files
)
3433 ;; Delete temporary directory and also other embedded
3434 ;; files that get copied there.
3435 (delete-directory org-e-odt-zip-dir t
)))))
3436 (org-condition-case-unless-debug
3439 (unless (executable-find "zip")
3440 ;; Not at all OSes ship with zip by default
3441 (error "Executable \"zip\" needed for creating OpenDocument files"))
3442 ;; Do export. This creates a bunch of xml files ready to be
3443 ;; saved and zipped.
3445 ;; Create a manifest entry for content.xml.
3446 (org-e-odt-create-manifest-file-entry "text/xml" "content.xml")
3448 ;; Write mimetype file
3450 '(("odt" .
"application/vnd.oasis.opendocument.text")
3451 ("odf" .
"application/vnd.oasis.opendocument.formula")))
3452 (mimetype (cdr (assoc-string out-file-type mimetypes t
))))
3454 (error "Unknown OpenDocument backend %S" out-file-type
))
3455 (write-region mimetype nil
(concat org-e-odt-zip-dir
"mimetype"))
3456 (org-e-odt-create-manifest-file-entry mimetype
"/" "1.2"))
3457 ;; Write out the manifest entries before zipping
3458 (org-e-odt-write-manifest-file)
3459 ;; Save all XML files.
3460 (mapc (lambda (file)
3461 (let ((buf (get-file-buffer (concat org-e-odt-zip-dir file
))))
3463 (with-current-buffer buf
3464 ;; Prettify output if needed.
3465 (when org-e-odt-prettify-xml
3466 (indent-region (point-min) (point-max)))
3468 org-e-odt-xml-files
)
3470 (let* ((target --out-file
)
3471 (target-name (file-name-nondirectory target
))
3472 (target-dir (file-name-directory target
))
3473 (cmds `(("zip" "-mX0" ,target-name
"mimetype")
3474 ("zip" "-rmTq" ,target-name
"."))))
3475 ;; If a file with same name as the desired output file
3476 ;; exists, remove it.
3477 (when (file-exists-p target
)
3478 (delete-file target
))
3479 ;; Zip up the xml files.
3480 (let ((coding-system-for-write 'no-conversion
) exitcode err-string
)
3481 (message "Creating ODT file...")
3482 ;; Switch temporarily to content.xml. This way Zip
3483 ;; process will inherit `org-e-odt-zip-dir' as the current
3485 (with-current-buffer
3486 (find-file-noselect (concat org-e-odt-zip-dir
"content.xml") t
)
3489 (message "Running %s" (mapconcat 'identity cmd
" "))
3491 (with-output-to-string
3493 (apply 'call-process
(car cmd
)
3494 nil standard-output nil
(cdr cmd
)))))
3495 (or (zerop exitcode
)
3496 (error (concat "Unable to create OpenDocument file."
3497 (format " Zip failed with error (%s)"
3500 ;; Zip file is now in the rightful place.
3501 (rename-file target-name target
)))
3502 (message "Created %s" target
)
3503 ;; Cleanup work directory and work files.
3504 (funcall --cleanup-xml-buffers
)
3505 ;; Open the OpenDocument file in archive-mode for
3507 (find-file-noselect target t
)
3508 ;; Return exported file.
3510 ;; Case 1: Conversion desired on exported file. Run the
3511 ;; converter on the OpenDocument file. Return the
3513 (org-e-odt-preferred-output-format
3514 (or (org-e-odt-convert target org-e-odt-preferred-output-format
)
3516 ;; Case 2: No further conversion. Return exported
3517 ;; OpenDocument file.
3520 ;; Cleanup work directory and work files.
3521 (funcall --cleanup-xml-buffers
)
3522 (message "OpenDocument export failed: %s"
3523 (error-message-string err
))))))
3528 (defun org-e-odt-export-as-odf (latex-frag &optional odf-file
)
3529 "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
3530 Use `org-create-math-formula' to convert LATEX-FRAG first to
3531 MathML. When invoked as an interactive command, use
3532 `org-latex-regexps' to infer LATEX-FRAG from currently active
3533 region. If no LaTeX fragments are found, prompt for it. Push
3534 MathML source to kill ring, if `org-export-copy-to-kill-ring' is
3538 (setq frag
(and (setq frag
(and (region-active-p)
3539 (buffer-substring (region-beginning)
3541 (loop for e in org-latex-regexps
3542 thereis
(when (string-match (nth 1 e
) frag
)
3543 (match-string (nth 2 e
) frag
)))))
3544 (read-string "LaTeX Fragment: " frag nil frag
))
3545 ,(let ((odf-filename (expand-file-name
3547 (file-name-sans-extension
3548 (or (file-name-nondirectory buffer-file-name
)))
3550 (file-name-directory buffer-file-name
))))
3551 (read-file-name "ODF filename: " nil odf-filename nil
3552 (file-name-nondirectory odf-filename
)))))
3553 (let ((filename (or odf-file
3556 (file-name-sans-extension
3557 (or (file-name-nondirectory buffer-file-name
)))
3559 (file-name-directory buffer-file-name
)))))
3560 (org-e-odt--export-wrap
3562 (let* ((buffer (progn
3563 (require 'nxml-mode
)
3564 (let ((nxml-auto-insert-xml-declaration-flag nil
))
3565 (find-file-noselect (concat org-e-odt-zip-dir
3566 "content.xml") t
))))
3567 (coding-system-for-write 'utf-8
)
3568 (save-buffer-coding-system 'utf-8
))
3570 (set-buffer-file-coding-system coding-system-for-write
)
3571 (let ((mathml (org-create-math-formula latex-frag
)))
3572 (unless mathml
(error "No Math formula created"))
3574 ;; Add MathML to kill ring, if needed.
3575 (when org-export-copy-to-kill-ring
3576 (org-kill-new (buffer-string))))))))
3579 (defun org-e-odt-export-as-odf-and-open ()
3580 "Export LaTeX fragment as OpenDocument formula and immediately open it.
3581 Use `org-e-odt-export-as-odf' to read LaTeX fragment and OpenDocument
3584 (org-open-file (call-interactively 'org-e-odt-export-as-odf
)))
3587 (defun org-e-odt-export-to-odt
3588 (&optional subtreep visible-only body-only ext-plist pub-dir
)
3589 "Export current buffer to a HTML file.
3591 If narrowing is active in the current buffer, only export its
3594 If a region is active, export that region.
3596 When optional argument SUBTREEP is non-nil, export the sub-tree
3597 at point, extracting information from the headline properties
3600 When optional argument VISIBLE-ONLY is non-nil, don't export
3601 contents of hidden elements.
3603 When optional argument BODY-ONLY is non-nil, only write code
3604 between \"\\begin{document}\" and \"\\end{document}\".
3606 EXT-PLIST, when provided, is a property list with external
3607 parameters overriding Org default settings, but still inferior to
3608 file-local settings.
3610 When optional argument PUB-DIR is set, use it as the publishing
3613 Return output file's name."
3615 (org-e-odt--export-wrap
3616 (org-export-output-file-name ".odt" subtreep pub-dir
)
3617 (let* ((org-e-odt-embedded-images-count 0)
3618 (org-e-odt-embedded-formulas-count 0)
3619 (org-e-odt-automatic-styles nil
)
3620 (org-e-odt-object-counters nil
)
3621 ;; Let `htmlfontify' know that we are interested in collecting
3623 (hfy-user-sheet-assoc nil
))
3624 ;; Initialize content.xml and kick-off the export process.
3625 (let ((out-buf (progn
3626 (require 'nxml-mode
)
3627 (let ((nxml-auto-insert-xml-declaration-flag nil
))
3629 (concat org-e-odt-zip-dir
"content.xml") t
)))))
3630 (org-export-to-buffer 'e-odt out-buf subtreep visible-only body-only
)))))
3635 (defun org-e-odt-reachable-p (in-fmt out-fmt
)
3636 "Return non-nil if IN-FMT can be converted to OUT-FMT."
3638 (let ((reachable-formats (org-e-odt-do-reachable-formats in-fmt
)))
3639 (dolist (e reachable-formats
)
3640 (let ((out-fmt-spec (assoc out-fmt
(cdr e
))))
3642 (throw 'done
(cons (car e
) out-fmt-spec
))))))))
3644 (defun org-e-odt-do-convert (in-file out-fmt
&optional prefix-arg
)
3645 "Workhorse routine for `org-e-odt-convert'."
3646 (require 'browse-url
)
3647 (let* ((in-file (expand-file-name (or in-file buffer-file-name
)))
3648 (dummy (or (file-readable-p in-file
)
3649 (error "Cannot read %s" in-file
)))
3650 (in-fmt (file-name-extension in-file
))
3651 (out-fmt (or out-fmt
(error "Output format unspecified")))
3652 (how (or (org-e-odt-reachable-p in-fmt out-fmt
)
3653 (error "Cannot convert from %s format to %s format?"
3655 (convert-process (car how
))
3656 (out-file (concat (file-name-sans-extension in-file
) "."
3657 (nth 1 (or (cdr how
) out-fmt
))))
3658 (extra-options (or (nth 2 (cdr how
)) ""))
3659 (out-dir (file-name-directory in-file
))
3660 (cmd (format-spec convert-process
3661 `((?i .
,(shell-quote-argument in-file
))
3662 (?I .
,(browse-url-file-url in-file
))
3665 (?O .
,(browse-url-file-url out-file
))
3666 (?d .
, (shell-quote-argument out-dir
))
3667 (?D .
,(browse-url-file-url out-dir
))
3668 (?x .
,extra-options
)))))
3669 (when (file-exists-p out-file
)
3670 (delete-file out-file
))
3672 (message "Executing %s" cmd
)
3673 (let ((cmd-output (shell-command-to-string cmd
)))
3674 (message "%s" cmd-output
))
3677 ((file-exists-p out-file
)
3678 (message "Exported to %s" out-file
)
3680 (message "Opening %s..." out-file
)
3681 (org-open-file out-file
))
3684 (message "Export to %s failed" out-file
)
3687 (defun org-e-odt-do-reachable-formats (in-fmt)
3688 "Return verbose info about formats to which IN-FMT can be converted.
3689 Return a list where each element is of the
3690 form (CONVERTER-PROCESS . OUTPUT-FMT-ALIST). See
3691 `org-e-odt-convert-processes' for CONVERTER-PROCESS and see
3692 `org-e-odt-convert-capabilities' for OUTPUT-FMT-ALIST."
3694 (and org-e-odt-convert-process
3695 (cadr (assoc-string org-e-odt-convert-process
3696 org-e-odt-convert-processes t
))))
3698 (and org-e-odt-convert-process
3699 (cadr (assoc-string org-e-odt-convert-process
3700 org-e-odt-convert-processes t
))
3701 org-e-odt-convert-capabilities
))
3704 (dolist (c capabilities
)
3705 (when (member in-fmt
(nth 1 c
))
3706 (push (cons converter
(nth 2 c
)) reachable-formats
))))
3709 (defun org-e-odt-reachable-formats (in-fmt)
3710 "Return list of formats to which IN-FMT can be converted.
3711 The list of the form (OUTPUT-FMT-1 OUTPUT-FMT-2 ...)."
3713 (mapc (lambda (e) (add-to-list 'l e
))
3714 (apply 'append
(mapcar
3715 (lambda (e) (mapcar 'car
(cdr e
)))
3716 (org-e-odt-do-reachable-formats in-fmt
))))
3719 (defun org-e-odt-convert-read-params ()
3720 "Return IN-FILE and OUT-FMT params for `org-e-odt-do-convert'.
3721 This is a helper routine for interactive use."
3722 (let* ((input (if (featurep 'ido
) 'ido-completing-read
'completing-read
))
3723 (in-file (read-file-name "File to be converted: "
3724 nil buffer-file-name t
))
3725 (in-fmt (file-name-extension in-file
))
3726 (out-fmt-choices (org-e-odt-reachable-formats in-fmt
))
3728 (or (and out-fmt-choices
3729 (funcall input
"Output format: "
3730 out-fmt-choices nil nil nil
))
3732 "No known converter or no known output formats for %s files"
3734 (list in-file out-fmt
)))
3737 (defun org-e-odt-convert (&optional in-file out-fmt prefix-arg
)
3738 "Convert IN-FILE to format OUT-FMT using a command line converter.
3739 IN-FILE is the file to be converted. If unspecified, it defaults
3740 to variable `buffer-file-name'. OUT-FMT is the desired output
3741 format. Use `org-e-odt-convert-process' as the converter.
3742 If PREFIX-ARG is non-nil then the newly converted file is opened
3743 using `org-open-file'."
3745 (append (org-e-odt-convert-read-params) current-prefix-arg
))
3746 (org-e-odt-do-convert in-file out-fmt prefix-arg
))
3748 ;;; Library Initializations
3752 ;; Let Org open all OpenDocument files using system-registered app
3753 (add-to-list 'org-file-apps
3754 (cons (concat "\\." (car desc
) "\\'") 'system
))
3755 ;; Let Emacs open all OpenDocument files in archive mode
3756 (add-to-list 'auto-mode-alist
3757 (cons (concat "\\." (car desc
) "\\'") 'archive-mode
)))
3758 org-e-odt-file-extensions
)
3760 (provide 'org-e-odt
)
3762 ;;; org-e-odt.el ends here