1 ;;; org-odt.el --- OpenDocumentText export for Org-mode
3 ;; Copyright (C) 2010-2011 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 (yet) part of GNU Emacs.
10 ;; However, it is distributed under the same license.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
31 (require 'htmlfontify
))
34 (defgroup org-export-odt nil
35 "Options specific for ODT export of Org-mode files."
39 (defun org-odt-end-export ()
40 (org-odt-fixup-label-references)
42 ;; remove empty paragraphs
43 (goto-char (point-min))
44 (while (re-search-forward
45 "<text:p\\( text:style-name=\"Text_20_body\"\\)?>[ \r\n\t]*</text:p>"
48 (goto-char (point-min))
50 ;; Convert whitespace place holders
51 (goto-char (point-min))
53 (while (setq beg
(next-single-property-change (point) 'org-whitespace
))
54 (setq n
(get-text-property beg
'org-whitespace
)
55 end
(next-single-property-change beg
'org-whitespace
))
57 (delete-region beg end
)
58 (insert (format "<span style=\"visibility:hidden;\">%s</span>"
59 (make-string n ?x
)))))
61 ;; Remove empty lines at the beginning of the file.
62 (goto-char (point-min))
63 (when (looking-at "\\s-+\n") (replace-match ""))
65 ;; Remove display properties
66 (remove-text-properties (point-min) (point-max) '(display t
)))
68 (defvar org-odt-suppress-xref nil
)
69 (defconst org-export-odt-special-string-regexps
70 '(("\\\\-" .
"­\\1") ; shy
71 ("---\\([^-]\\)" .
"—\\1") ; mdash
72 ("--\\([^-]\\)" .
"–\\1") ; ndash
73 ("\\.\\.\\." .
"…")) ; hellip
74 "Regular expressions for special string conversion.")
76 (defconst org-odt-lib-dir
(file-name-directory load-file-name
))
77 (defconst org-odt-styles-dir
78 (let* ((styles-dir1 (expand-file-name "../etc/styles/" org-odt-lib-dir
))
79 (styles-dir2 (expand-file-name "./etc/styles/" org-odt-lib-dir
))
82 (mapc (lambda (styles-dir)
83 (when (and (file-readable-p
85 "OrgOdtContentTemplate.xml" styles-dir
))
88 "OrgOdtStyles.xml" styles-dir
)))
89 (throw 'styles-dir styles-dir
)))
90 (list styles-dir1 styles-dir2
))
93 (error "Cannot find factory styles file. Check package dir layout"))
95 "Directory that holds auxiliary XML files used by the ODT exporter.
97 This directory contains the following XML files -
98 \"OrgOdtStyles.xml\" and \"OrgOdtContentTemplate.xml\". These
99 XML files are used as the default values of
100 `org-export-odt-styles-file' and
101 `org-export-odt-content-template-file'.")
103 (defcustom org-export-odt-schema-dir
104 (let ((schema-dir (expand-file-name
105 "../contrib/odt/etc/schema/" org-odt-lib-dir
)))
106 (if (and (file-readable-p
107 (expand-file-name "od-manifest-schema-v1.2-cs01.rnc" schema-dir
))
109 (expand-file-name "od-schema-v1.2-cs01.rnc" schema-dir
))
111 (expand-file-name "schemas.xml" schema-dir
)))
113 (prog1 nil
(message "Unable to locate OpenDocument schema files."))))
114 "Directory that contains OpenDocument schema files.
116 This directory contains rnc files for OpenDocument schema. It
117 also contains a \"schemas.xml\" that can be added to
118 `rng-schema-locating-files' for auto validation of OpenDocument
119 XML files. See also `rng-nxml-auto-validate-flag'."
121 (const :tag
"Not set" nil
)
122 (directory :tag
"Schema directory"))
123 :group
'org-export-odt
126 "Set `org-export-odt-schema-dir'.
127 Also add it to `rng-schema-locating-files'."
128 (let ((schema-dir value
))
132 (expand-file-name "od-manifest-schema-v1.2-cs01.rnc" schema-dir
))
134 (expand-file-name "od-schema-v1.2-cs01.rnc" schema-dir
))
136 (expand-file-name "schemas.xml" schema-dir
)))
139 (message "Warning (org-odt): Unable to locate OpenDocument schema files.")))))
140 (when org-export-odt-schema-dir
141 (eval-after-load 'rng-loc
142 '(add-to-list 'rng-schema-locating-files
143 (expand-file-name "schemas.xml"
144 org-export-odt-schema-dir
))))))
146 (defvar org-odt-file-extensions
147 '(("odt" .
"OpenDocument Text")
148 ("ott" .
"OpenDocument Text Template")
149 ("odm" .
"OpenDocument Master Document")
150 ("ods" .
"OpenDocument Spreadsheet")
151 ("ots" .
"OpenDocument Spreadsheet Template")
152 ("odg" .
"OpenDocument Drawing (Graphics)")
153 ("otg" .
"OpenDocument Drawing Template")
154 ("odp" .
"OpenDocument Presentation")
155 ("otp" .
"OpenDocument Presentation Template")
156 ("odi" .
"OpenDocument Image")
157 ("odf" .
"OpenDocument Formula")
158 ("odc" .
"OpenDocument Chart")))
162 ;; Let Org open all OpenDocument files using system-registered app
163 (add-to-list 'org-file-apps
164 (cons (concat "\\." (car desc
) "\\'") 'system
))
165 ;; Let Emacs open all OpenDocument files in archive mode
166 (add-to-list 'auto-mode-alist
167 (cons (concat "\\." (car desc
) "\\'") 'archive-mode
)))
168 org-odt-file-extensions
)
170 ;; register the odt exporter with the pre-processor
171 (add-to-list 'org-export-backends
'odt
)
173 ;; register the odt exporter with org-lparse library
174 (org-lparse-register-backend 'odt
)
176 (defun org-odt-unload-function ()
177 (org-lparse-unregister-backend 'odt
)
178 (remove-hook 'org-export-preprocess-after-blockquote-hook
179 'org-export-odt-preprocess-latex-fragments
)
182 (defcustom org-export-odt-content-template-file nil
183 "Template file for \"content.xml\".
184 The exporter embeds the exported content just before
185 \"</office:text>\" element.
187 If unspecified, the file named \"OrgOdtContentTemplate.xml\"
188 under `org-odt-styles-dir' is used."
190 :group
'org-export-odt
)
192 (defcustom org-export-odt-styles-file nil
193 "Default styles file for use with ODT export.
194 Valid values are one of:
196 2. path to a styles.xml file
197 3. path to a *.odt or a *.ott file
198 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
201 In case of option 1, an in-built styles.xml is used. See
202 `org-odt-styles-dir' for more information.
204 In case of option 3, the specified file is unzipped and the
205 styles.xml embedded therein is used.
207 In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
208 and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
209 generated odt file. Use relative path for specifying the
210 FILE-MEMBERS. styles.xml must be specified as one of the
213 Use options 1, 2 or 3 only if styles.xml alone suffices for
214 achieving the desired formatting. Use option 4, if the styles.xml
215 references additional files like header and footer images for
216 achieving the desired formattting.
218 Use \"#+ODT_STYLES_FILE: ...\" directive to set this variable on
219 a per-file basis. For example,
221 #+ODT_STYLES_FILE: \"/path/to/styles.xml\" or
222 #+ODT_STYLES_FILE: (\"/path/to/file.ott\" (\"styles.xml\" \"image/hdr.png\"))."
223 :group
'org-export-odt
226 (const :tag
"Factory settings" nil
)
227 (file :must-match t
:tag
"styles.xml")
228 (file :must-match t
:tag
"ODT or OTT file")
229 (list :tag
"ODT or OTT file + Members"
230 (file :must-match t
:tag
"ODF Text or Text Template file")
232 (file :tag
" Member" "styles.xml")
233 (repeat (file :tag
"Member"))))))
235 (eval-after-load 'org-exp
236 '(add-to-list 'org-export-inbuffer-options-extra
237 '("ODT_STYLES_FILE" :odt-styles-file
)))
239 (defconst org-export-odt-tmpdir-prefix
"%s-")
240 (defconst org-export-odt-bookmark-prefix
"OrgXref.")
242 (defvar org-export-odt-embed-images t
243 "Should the images be copied in to the odt file or just linked?")
245 (defvar org-export-odt-inline-images
'maybe
) ; counterpart of
246 ; `org-export-html-inline-images'
248 (defcustom org-export-odt-inline-image-extensions
249 '("png" "jpeg" "jpg" "gif")
250 "Extensions of image files that can be inlined into HTML."
251 :type
'(repeat (string :tag
"Extension"))
252 :group
'org-export-odt
)
254 (defcustom org-export-odt-pixels-per-inch display-pixels-per-inch
255 ;; FIXME add docstring
258 :group
'org-export-odt
)
260 (defcustom org-export-odt-create-custom-styles-for-srcblocks t
261 "Whether custom styles for colorized source blocks be automatically created.
262 When this option is turned on, the exporter creates custom styles
263 for source blocks based on the advice of `htmlfontify'. Creation
264 of custom styles happen as part of `org-odt-hfy-face-to-css'.
266 When this option is turned off exporter does not create such
269 Use the latter option if you do not want the custom styles to be
270 based on your current display settings. It is necessary that the
271 styles.xml already contains needed styles for colorizing to work.
273 This variable is effective only if
274 `org-export-odt-fontify-srcblocks' is turned on."
275 :group
'org-export-odt
278 (defvar org-export-odt-default-org-styles-alist
279 '((paragraph .
((default .
"Text_20_body")
280 (fixedwidth .
"OrgFixedWidthBlock")
282 (quote .
"Quotations")
283 (blockquote .
"Quotations")
284 (center .
"OrgCenter")
287 (title .
"Heading_20_1.title")
288 (footnote .
"Footnote")
289 (src .
"OrgSrcBlock")
290 (illustration .
"Illustration")
292 (definition-term .
"Text_20_body_20_bold")
293 (horizontal-line .
"Horizontal_20_Line")))
294 (character .
((bold .
"Bold")
295 (emphasis .
"Emphasis")
297 (verbatim .
"OrgCode")
298 (strike .
"Strikethrough")
299 (underline .
"Underline")
300 (subscript .
"OrgSubscript")
301 (superscript .
"OrgSuperscript")))
302 (list .
((ordered .
"OrgNumberedList")
303 (unordered .
"OrgBulletedList")
304 (description .
"OrgDescriptionList"))))
305 "Default styles for various entities.")
307 (defvar org-export-odt-org-styles-alist org-export-odt-default-org-styles-alist
)
308 (defun org-odt-get-style-name-for-entity (category &optional entity
)
309 (let ((entity (or entity
'default
)))
311 (cdr (assoc entity
(cdr (assoc category
312 org-export-odt-org-styles-alist
))))
313 (cdr (assoc entity
(cdr (assoc category
314 org-export-odt-default-org-styles-alist
))))
315 (error "Cannot determine style name for entity %s of type %s"
318 (defcustom org-export-odt-preferred-output-format nil
319 "Automatically post-process to this format after exporting to \"odt\".
320 Interactive commands `org-export-as-odt' and
321 `org-export-as-odt-and-open' export first to \"odt\" format and
322 then use `org-export-odt-convert-process' to convert the
323 resulting document to this format. During customization of this
324 variable, the list of valid values are populated based on
325 `org-export-odt-convert-capabilities'."
326 :group
'org-export-odt
327 :type
'(choice :convert-widget
329 (apply 'widget-convert
(widget-type w
)
330 (eval (car (widget-get w
:args
)))))
331 `((const :tag
"None" nil
)
332 ,@(mapcar (lambda (c)
334 (org-lparse-reachable-formats "odt")))))
337 (defun org-export-as-odt-and-open (arg)
338 "Export the outline as ODT and immediately open it with a browser.
339 If there is an active region, export only the region.
340 The prefix ARG specifies how many levels of the outline should become
341 headlines. The default is 3. Lower levels will become bulleted lists."
344 (or org-export-odt-preferred-output-format
"odt") "odt" arg
))
347 (defun org-export-as-odt-batch ()
348 "Call the function `org-lparse-batch'.
349 This function can be used in batch processing as:
351 --load=$HOME/lib/emacs/org.el
352 --eval \"(setq org-export-headline-levels 2)\"
353 --visit=MyFile --funcall org-export-as-odt-batch"
354 (org-lparse-batch "odt"))
357 (defun org-export-as-odt-to-buffer (arg)
358 "Call `org-lparse-odt` with output to a temporary buffer.
359 No file is created. The prefix ARG is passed through to `org-lparse-to-buffer'."
361 (org-lparse-to-buffer "odt" arg
))
364 (defun org-replace-region-by-odt (beg end
)
365 "Assume the current region has org-mode syntax, and convert it to ODT.
366 This can be used in any buffer. For example, you could write an
367 itemized list in org-mode syntax in an ODT buffer and then use this
368 command to convert it."
370 (org-replace-region-by "odt" beg end
))
373 (defun org-export-region-as-odt (beg end
&optional body-only buffer
)
374 "Convert region from BEG to END in org-mode buffer to ODT.
375 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
376 contents, and only produce the region of converted text, useful for
377 cut-and-paste operations.
378 If BUFFER is a buffer or a string, use/create that buffer as a target
379 of the converted ODT. If BUFFER is the symbol `string', return the
380 produced ODT as a string and leave not buffer behind. For example,
381 a Lisp program could call this function in the following way:
383 (setq odt (org-export-region-as-odt beg end t 'string))
385 When called interactively, the output buffer is selected, and shown
386 in a window. A non-interactive call will only return the buffer."
388 (org-lparse-region "odt" beg end body-only buffer
))
390 ;;; org-export-as-odt
392 (defun org-export-as-odt (arg &optional hidden ext-plist
393 to-buffer body-only pub-dir
)
394 "Export the outline as a OpenDocumentText file.
395 If there is an active region, export only the region. The prefix
396 ARG specifies how many levels of the outline should become
397 headlines. The default is 3. Lower levels will become bulleted
398 lists. HIDDEN is obsolete and does nothing.
399 EXT-PLIST is a property list with external parameters overriding
400 org-mode's default settings, but still inferior to file-local
401 settings. When TO-BUFFER is non-nil, create a buffer with that
402 name and export to that buffer. If TO-BUFFER is the symbol
403 `string', don't leave any buffer behind but just return the
404 resulting XML as a string. When BODY-ONLY is set, don't produce
405 the file header and footer, simply return the content of
406 <body>...</body>, without even the body tags themselves. When
407 PUB-DIR is set, use this as the publishing directory."
409 (org-lparse (or org-export-odt-preferred-output-format
"odt")
410 "odt" arg hidden ext-plist to-buffer body-only pub-dir
))
412 (defvar org-odt-entity-control-callbacks-alist
414 .
(org-odt-begin-export org-odt-end-export
))
416 .
(org-odt-begin-document-content org-odt-end-document-content
))
418 .
(org-odt-begin-document-body org-odt-end-document-body
))
420 .
(org-odt-begin-toc org-odt-end-toc
))
422 .
(org-odt-begin-environment org-odt-end-environment
))
424 .
(org-odt-begin-footnote-definition org-odt-end-footnote-definition
))
426 .
(org-odt-begin-table org-odt-end-table
))
428 .
(org-odt-begin-table-rowgroup org-odt-end-table-rowgroup
))
430 .
(org-odt-begin-list org-odt-end-list
))
432 .
(org-odt-begin-list-item org-odt-end-list-item
))
434 .
(org-odt-begin-outline org-odt-end-outline
))
436 .
(org-odt-begin-outline-text org-odt-end-outline-text
))
438 .
(org-odt-begin-paragraph org-odt-end-paragraph
)))
441 (defvar org-odt-entity-format-callbacks-alist
442 `((EXTRA-TARGETS . org-lparse-format-extra-targets
)
443 (ORG-TAGS . org-lparse-format-org-tags
)
444 (SECTION-NUMBER . org-lparse-format-section-number
)
445 (HEADLINE . org-odt-format-headline
)
446 (TOC-ENTRY . org-odt-format-toc-entry
)
447 (TOC-ITEM . org-odt-format-toc-item
)
448 (TAGS . org-odt-format-tags
)
449 (SPACES . org-odt-format-spaces
)
450 (TABS . org-odt-format-tabs
)
451 (LINE-BREAK . org-odt-format-line-break
)
452 (FONTIFY . org-odt-format-fontify
)
453 (TODO . org-lparse-format-todo
)
454 (LINK . org-odt-format-link
)
455 (INLINE-IMAGE . org-odt-format-inline-image
)
456 (ORG-LINK . org-odt-format-org-link
)
457 (HEADING . org-odt-format-heading
)
458 (ANCHOR . org-odt-format-anchor
)
459 (TABLE . org-lparse-format-table
)
460 (TABLE-ROW . org-odt-format-table-row
)
461 (TABLE-CELL . org-odt-format-table-cell
)
462 (FOOTNOTES-SECTION . ignore
)
463 (FOOTNOTE-REFERENCE . org-odt-format-footnote-reference
)
464 (HORIZONTAL-LINE . org-odt-format-horizontal-line
)
465 (COMMENT . org-odt-format-comment
)
466 (LINE . org-odt-format-line
)
467 (ORG-ENTITY . org-odt-format-org-entity
))
471 ;;;_. control callbacks
473 (defun org-odt-begin-office-body ()
475 (insert-file-contents
476 (or org-export-odt-content-template-file
477 (expand-file-name "OrgOdtContentTemplate.xml"
478 org-odt-styles-dir
)))
479 (goto-char (point-min))
480 (re-search-forward "</office:text>" nil nil
)
481 (delete-region (match-beginning 0) (point-max)))
483 ;; Following variable is let bound when `org-do-lparse' is in
484 ;; progress. See org-html.el.
485 (defvar org-lparse-toc
)
486 (defun org-odt-begin-document-body (opt-plist)
487 (org-odt-begin-office-body)
488 (let ((title (plist-get opt-plist
:title
)))
491 (org-odt-format-stylized-paragraph 'title title
))))
495 (insert "\n" org-lparse-toc
"\n")))
497 (defvar org-lparse-body-only
) ; let bound during org-do-lparse
498 (defvar org-lparse-to-buffer
) ; let bound during org-do-lparse
499 (defun org-odt-end-document-body (opt-plist)
500 (unless org-lparse-body-only
501 (org-lparse-insert-tag "</office:text>")
502 (org-lparse-insert-tag "</office:body>")))
504 (defun org-odt-begin-document-content (opt-plist)
507 (defun org-odt-end-document-content ()
508 (org-lparse-insert-tag "</office:document-content>"))
510 (defun org-odt-begin-outline (level1 snumber title tags
511 target extra-targets class
)
513 'HEADING
(org-lparse-format
514 'HEADLINE title extra-targets tags snumber level1
)
517 (defun org-odt-end-outline ()
520 (defun org-odt-begin-outline-text (level1 snumber class
)
523 (defun org-odt-end-outline-text ()
526 (defun org-odt-begin-paragraph (&optional style
)
527 (org-lparse-insert-tag
528 "<text:p%s>" (org-odt-get-extra-attrs-for-paragraph-style style
)))
530 (defun org-odt-end-paragraph ()
531 (org-lparse-insert-tag "</text:p>"))
533 (defun org-odt-get-extra-attrs-for-paragraph-style (style)
537 ((stringp style
) style
)
538 ((symbolp style
) (org-odt-get-style-name-for-entity
541 (error "Don't know how to handle paragraph style %s" style
))
542 (format " text:style-name=\"%s\"" style-name
)))
544 (defun org-odt-format-stylized-paragraph (style text
)
546 '("<text:p%s>" .
"</text:p>") text
547 (org-odt-get-extra-attrs-for-paragraph-style style
)))
549 (defvar org-lparse-opt-plist
) ; bound during org-do-lparse
550 (defun org-odt-format-author (&optional author
)
551 (when (setq author
(or author
(plist-get org-lparse-opt-plist
:author
)))
552 (org-odt-format-tags '("<dc:creator>" .
"</dc:creator>") author
)))
554 (defun org-odt-iso-date-from-org-timestamp (&optional org-ts
)
557 (and (stringp org-ts
)
558 (string-match org-ts-regexp0 org-ts
)
560 (org-fix-decoded-time
561 (org-parse-time-string (match-string 0 org-ts
) t
)))))
562 (date (format-time-string "%Y-%m-%dT%H:%M:%S%z" time
)))
563 (format "%s:%s" (substring date
0 -
2) (substring date -
2)))))
565 (defun org-odt-begin-annotation (&optional author date
)
566 (org-lparse-insert-tag "<office:annotation>")
567 (when (setq author
(org-odt-format-author author
))
569 (insert (org-odt-format-tags
570 '("<dc:date>" .
"</dc:date>")
571 (org-odt-iso-date-from-org-timestamp
572 (or date
(plist-get org-lparse-opt-plist
:date
)))))
573 (org-lparse-begin-paragraph))
575 (defun org-odt-end-annotation ()
576 (org-lparse-insert-tag "</office:annotation>"))
578 (defun org-odt-begin-environment (style env-options-plist
)
581 (org-lparse-stash-save-paragraph-state)
582 (org-odt-begin-annotation (plist-get env-options-plist
'author
)
583 (plist-get env-options-plist
'date
)))
584 ((blockquote verse center quote
)
585 (org-lparse-begin-paragraph style
)
588 (org-lparse-end-paragraph)
590 (t (error "Unknown environment %s" style
))))
592 (defun org-odt-end-environment (style env-options-plist
)
595 (org-lparse-end-paragraph)
596 (org-odt-end-annotation)
597 (org-lparse-stash-pop-paragraph-state))
598 ((blockquote verse center quote
)
599 (org-lparse-end-paragraph)
602 (org-lparse-begin-paragraph)
604 (t (error "Unknown environment %s" style
))))
606 (defvar org-lparse-list-level
) ; dynamically bound in org-do-lparse
607 (defun org-odt-begin-list (ltype)
608 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
610 (let* ((style-name (org-odt-get-style-name-for-entity 'list ltype
))
611 (extra (concat (when (= org-lparse-list-level
1)
612 " text:continue-numbering=\"false\"")
614 (format " text:style-name=\"%s\"" style-name
)))))
616 ((ordered unordered description
)
617 (org-lparse-end-paragraph)
618 (org-lparse-insert-tag "<text:list%s>" extra
))
619 (t (error "Unknown list type: %s" ltype
)))))
621 (defun org-odt-end-list (ltype)
622 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
625 (org-lparse-insert-tag "</text:list>")
626 (error "Unknown list type: %s" ltype
)))
628 (defun org-odt-begin-list-item (ltype &optional arg headline
)
629 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
633 (assert (not headline
) t
)
634 (let* ((counter arg
) (extra ""))
635 (org-lparse-insert-tag "<text:list-item>")
636 (org-lparse-begin-paragraph)))
638 (let* ((id arg
) (extra ""))
639 (org-lparse-insert-tag "<text:list-item>")
640 (org-lparse-begin-paragraph)
641 (insert (if headline
(org-odt-format-target headline id
)
642 (org-odt-format-bookmark "" id
)))))
644 (assert (not headline
) t
)
645 (let ((term (or arg
"(no term)")))
648 '("<text:list-item>" .
"</text:list-item>")
649 (org-odt-format-stylized-paragraph 'definition-term term
)))
650 (org-lparse-begin-list-item 'unordered
)
651 (org-lparse-begin-list 'description
)
652 (org-lparse-begin-list-item 'unordered
)))
653 (t (error "Unknown list type"))))
655 (defun org-odt-end-list-item (ltype)
656 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
660 (org-lparse-insert-tag "</text:list-item>"))
662 (org-lparse-end-list-item-1)
663 (org-lparse-end-list 'description
)
664 (org-lparse-end-list-item-1))
665 (t (error "Unknown list type"))))
667 ;; Following variables are let bound when table emission is in
668 ;; progress. See org-lparse.el.
669 (defvar org-lparse-table-begin-marker
)
670 (defvar org-lparse-table-ncols
)
671 (defvar org-lparse-table-rowgrp-open
)
672 (defvar org-lparse-table-rownum
)
673 (defvar org-lparse-table-cur-rowgrp-is-hdr
)
674 (defvar org-lparse-table-is-styled
)
675 (defvar org-lparse-table-rowgrp-info
)
676 (defvar org-lparse-table-colalign-vector
)
678 (defvar org-odt-table-style nil
679 "Table style specified by \"#+ATTR_ODT: <style-name>\" line.
680 This is set during `org-odt-begin-table'.")
682 (defvar org-odt-table-style-spec nil
683 "Entry for `org-odt-table-style' in `org-export-odt-table-styles'.")
685 (defcustom org-export-odt-table-styles
686 '(("OrgEquation" "OrgEquation"
687 ((use-first-column-styles . t
)
688 (use-last-column-styles . t
))))
689 "Specify how Table Styles should be derived from a Table Template.
690 This is a list where each element is of the
691 form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
693 TABLE-STYLE-NAME is the style associated with the table through
694 `org-odt-table-style'.
696 TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
697 TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
698 below) that is included in
699 `org-export-odt-content-template-file'.
701 TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
703 PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
705 TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
706 \"FirstRow\" | \"LastRow\" |
707 \"EvenRow\" | \"OddRow\" |
708 \"EvenColumn\" | \"OddColumn\" | \"\"
709 where \"+\" above denotes string concatenation.
711 TABLE-CELL-OPTIONS is an alist where each element is of the
712 form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
713 TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
714 `use-last-row-styles' |
715 `use-first-column-styles' |
716 `use-last-column-styles' |
717 `use-banding-rows-styles' |
718 `use-banding-columns-styles' |
719 `use-first-row-styles'
720 ON-OR-OFF := `t' | `nil'
722 For example, with the following configuration
724 \(setq org-export-odt-table-styles
725 '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
726 \(\(use-first-row-styles . t\)
727 \(use-first-column-styles . t\)\)\)
728 \(\"TableWithHeaderColumns\" \"Custom\"
729 \(\(use-first-column-styles . t\)\)\)\)\)
731 1. A table associated with \"TableWithHeaderRowsAndColumns\"
732 style will use the following table-cell styles -
733 \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
734 \"CustomTableCell\" and the following paragraph styles
735 \"CustomFirstRowTableParagraph\",
736 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
739 2. A table associated with \"TableWithHeaderColumns\" style will
740 use the following table-cell styles -
741 \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
742 following paragraph styles
743 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
746 Note that TABLE-TEMPLATE-NAME corresponds to the
747 \"<table:table-template>\" elements contained within
748 \"<office:styles>\". The entries (TABLE-STYLE-NAME
749 TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
750 \"table:template-name\" and \"table:use-first-row-styles\" etc
751 attributes of \"<table:table>\" element. Refer ODF-1.2
752 specification for more information. Also consult the
753 implementation filed under `org-odt-get-table-cell-styles'.
755 The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
756 formatting of numbered display equations. Do not delete this
757 style from the list."
758 :group
'org-export-odt
760 (const :tag
"None" nil
)
761 (repeat :tag
"Table Styles"
762 (list :tag
"Table Style Specification"
763 (string :tag
"Table Style Name")
764 (string :tag
"Table Template Name")
765 (alist :options
(use-first-row-styles
767 use-first-column-styles
768 use-last-column-styles
769 use-banding-rows-styles
770 use-banding-columns-styles
)
772 :value-type
(const :tag
"True" t
))))))
774 (defun org-odt-begin-table (caption label attributes
)
775 (setq org-odt-table-style attributes
)
776 (setq org-odt-table-style-spec
777 (assoc org-odt-table-style org-export-odt-table-styles
))
780 (org-odt-format-stylized-paragraph
781 'table
(org-odt-format-entity-caption label caption
"__Table__"))))
782 (org-lparse-insert-tag
783 "<table:table table:name=\"%s\" table:style-name=\"%s\">"
784 (or label
"") (or (nth 1 org-odt-table-style-spec
) "OrgTable"))
785 (setq org-lparse-table-begin-marker
(point)))
787 (defvar org-lparse-table-colalign-info
)
788 (defun org-odt-end-table ()
789 (goto-char org-lparse-table-begin-marker
)
790 (loop for level from
0 below org-lparse-table-ncols
791 do
(let* ((col-cookie (and org-lparse-table-is-styled
792 (cdr (assoc (1+ level
)
793 org-lparse-table-colalign-info
))))
794 (extra-columns (or (nth 1 col-cookie
) 0)))
795 (dotimes (i (1+ extra-columns
))
798 "<table:table-column table:style-name=\"%sColumn\"/>"
799 "" (or (nth 1 org-odt-table-style-spec
) "OrgTable"))))
801 ;; fill style attributes for table cells
802 (when org-lparse-table-is-styled
803 (while (re-search-forward "@@\\(table-cell:p\\|table-cell:style-name\\)@@\\([0-9]+\\)@@\\([0-9]+\\)@@" nil t
)
804 (let* ((spec (match-string 1))
805 (r (string-to-number (match-string 2)))
806 (c (string-to-number (match-string 3)))
807 (cell-styles (org-odt-get-table-cell-styles
808 r c org-odt-table-style-spec
))
809 (table-cell-style (car cell-styles
))
810 (table-cell-paragraph-style (cdr cell-styles
)))
812 ((equal spec
"table-cell:p")
813 (replace-match table-cell-paragraph-style t t
))
814 ((equal spec
"table-cell:style-name")
815 (replace-match table-cell-style t t
))))))
816 (goto-char (point-max))
817 (org-lparse-insert-tag "</table:table>"))
819 (defun org-odt-begin-table-rowgroup (&optional is-header-row
)
820 (when org-lparse-table-rowgrp-open
821 (org-lparse-end 'TABLE-ROWGROUP
))
822 (org-lparse-insert-tag (if is-header-row
823 "<table:table-header-rows>"
824 "<table:table-rows>"))
825 (setq org-lparse-table-rowgrp-open t
)
826 (setq org-lparse-table-cur-rowgrp-is-hdr is-header-row
))
828 (defun org-odt-end-table-rowgroup ()
829 (when org-lparse-table-rowgrp-open
830 (setq org-lparse-table-rowgrp-open nil
)
831 (org-lparse-insert-tag
832 (if org-lparse-table-cur-rowgrp-is-hdr
833 "</table:table-header-rows>" "</table:table-rows>"))))
835 (defun org-odt-format-table-row (row)
837 '("<table:table-row>" .
"</table:table-row>") row
))
839 (defun org-odt-get-table-cell-styles (r c
&optional style-spec
)
840 "Retrieve styles applicable to a table cell.
841 R and C are (zero-based) row and column numbers of the table
842 cell. STYLE-SPEC is an entry in `org-export-odt-table-styles'
843 applicable to the current table. It is `nil' if the table is not
844 associated with any style attributes.
846 Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
848 When STYLE-SPEC is nil, style the table cell the conventional way
849 - choose cell borders based on row and column groupings and
850 choose paragraph alignment based on `org-col-cookies' text
852 `org-odt-get-paragraph-style-cookie-for-table-cell'.
854 When STYLE-SPEC is non-nil, ignore the above cookie and return
855 styles congruent with the ODF-1.2 specification."
859 ;; LibreOffice - particularly the Writer - honors neither table
860 ;; templates nor custom table-cell styles. Inorder to retain
861 ;; inter-operability with LibreOffice, only automatic styles are
862 ;; used for styling of table-cells. The current implementation is
863 ;; congruent with ODF-1.2 specification and hence is
864 ;; future-compatible.
866 ;; Additional Note: LibreOffice's AutoFormat facility for tables -
867 ;; which recognizes as many as 16 different cell types - is much
868 ;; richer. Unfortunately it is NOT amenable to easy configuration
871 (let* ((template-name (nth 1 style-spec
))
872 (cell-style-selectors (nth 2 style-spec
))
875 ((and (cdr (assoc 'use-first-column-styles cell-style-selectors
))
876 (= c
0)) "FirstColumn")
877 ((and (cdr (assoc 'use-last-column-styles cell-style-selectors
))
878 (= c
(1- org-lparse-table-ncols
))) "LastColumn")
879 ((and (cdr (assoc 'use-first-row-styles cell-style-selectors
))
881 ((and (cdr (assoc 'use-last-row-styles cell-style-selectors
))
882 (= r org-lparse-table-rownum
))
884 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors
))
885 (= (% r
2) 1)) "EvenRow")
886 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors
))
887 (= (% r
2) 0)) "OddRow")
888 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors
))
889 (= (% c
2) 1)) "EvenColumn")
890 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors
))
891 (= (% c
2) 0)) "OddColumn")
894 (concat template-name cell-type
"TableCell")
895 (concat template-name cell-type
"TableParagraph"))))
902 ((eq (cdr (assoc r org-lparse-table-rowgrp-info
)) :start
) "T")
904 (when (= r org-lparse-table-rownum
) "B")
907 ((or (memq (nth c org-table-colgroup-info
) '(:start
:startend
))
908 (memq (nth (1- c
) org-table-colgroup-info
) '(:end
:startend
))) "L")
910 (capitalize (aref org-lparse-table-colalign-vector c
))))))
912 (defun org-odt-get-paragraph-style-cookie-for-table-cell (r c
)
914 (and (not org-odt-table-style-spec
)
916 (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
917 ((and (= c
0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS
))
919 (t "OrgTableContents")))
920 (and org-lparse-table-is-styled
921 (format "@@table-cell:p@@%03d@@%03d@@" r c
))))
923 (defun org-odt-get-style-name-cookie-for-table-cell (r c
)
924 (when org-lparse-table-is-styled
925 (format "@@table-cell:style-name@@%03d@@%03d@@" r c
)))
927 (defun org-odt-format-table-cell (data r c horiz-span
)
929 (let* ((paragraph-style-cookie
930 (org-odt-get-paragraph-style-cookie-for-table-cell r c
))
932 (org-odt-get-style-name-cookie-for-table-cell r c
))
933 (extra (and style-name-cookie
934 (format " table:style-name=\"%s\"" style-name-cookie
)))
936 (and (> horiz-span
0)
937 (format " table:number-columns-spanned=\"%d\""
940 '("<table:table-cell%s>" .
"</table:table-cell>")
941 (if org-lparse-list-table-p data
942 (org-odt-format-stylized-paragraph paragraph-style-cookie data
)) extra
))
944 (dotimes (i horiz-span
)
945 (setq s
(concat s
"\n<table:covered-table-cell/>"))) s
)
948 (defun org-odt-begin-footnote-definition (n)
949 (org-lparse-begin-paragraph 'footnote
))
951 (defun org-odt-end-footnote-definition (n)
952 (org-lparse-end-paragraph))
954 (defun org-odt-begin-toc (lang-specific-heading max-level
)
957 <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
958 <text:table-of-content-source text:outline-level=\"%d\">
959 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
960 " max-level lang-specific-heading
))
961 (loop for level from
1 upto
10
964 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
965 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
966 <text:index-entry-chapter/>
967 <text:index-entry-text/>
968 <text:index-entry-link-end/>
969 </text:table-of-content-entry-template>
974 </text:table-of-content-source>
977 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
978 <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
980 " lang-specific-heading
)))
982 (defun org-odt-end-toc ()
985 </text:table-of-content>
988 (defun org-odt-format-toc-entry (snumber todo headline tags href
)
989 (setq headline
(concat
990 (and org-export-with-section-numbers
991 (concat snumber
". "))
995 (org-lparse-format 'SPACES
3)
996 (org-lparse-format 'FONTIFY tags
"tag")))))
998 (setq headline
(org-lparse-format 'FONTIFY headline
"todo")))
1000 (let ((org-odt-suppress-xref t
))
1001 (org-odt-format-link headline
(concat "#" href
))))
1003 (defun org-odt-format-toc-item (toc-entry level org-last-level
)
1004 (let ((style (format "Contents_20_%d"
1005 (+ level
(or (org-lparse-get 'TOPLEVEL-HLEVEL
) 1) -
1))))
1006 (insert "\n" (org-odt-format-stylized-paragraph style toc-entry
) "\n")))
1008 ;; Following variable is let bound during 'ORG-LINK callback. See
1010 (defvar org-lparse-link-description-is-image nil
)
1011 (defun org-odt-format-link (desc href
&optional attr
)
1013 ((and (= (string-to-char href
) ?
#) (not org-odt-suppress-xref
))
1014 (setq href
(concat org-export-odt-bookmark-prefix
(substring href
1)))
1015 (let ((xref-format "text"))
1016 (when (numberp desc
)
1017 (setq desc
(format "%d" desc
) xref-format
"number"))
1018 (org-odt-format-tags
1019 '("<text:bookmark-ref text:reference-format=\"%s\" text:ref-name=\"%s\">" .
1020 "</text:bookmark-ref>")
1021 desc xref-format href
)))
1022 (org-lparse-link-description-is-image
1023 (org-odt-format-tags
1024 '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" .
"</draw:a>")
1025 desc href
(or attr
"")))
1027 (org-odt-format-tags
1028 '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" .
"</text:a>")
1029 desc href
(or attr
"")))))
1031 (defun org-odt-format-spaces (n)
1035 " " (org-odt-format-tags "<text:s text:c=\"%d\"/>" "" (1- n
))))
1038 (defun org-odt-format-tabs (&optional n
)
1039 (let ((tab "<text:tab/>")
1043 (defun org-odt-format-line-break ()
1044 (org-odt-format-tags "<text:line-break/>" ""))
1046 (defun org-odt-format-horizontal-line ()
1047 (org-odt-format-stylized-paragraph 'horizontal-line
""))
1049 (defun org-odt-encode-plain-text (line &optional no-whitespace-filling
)
1050 (setq line
(org-xml-encode-plain-text line
))
1051 (if no-whitespace-filling line
1052 (org-odt-fill-tabs-and-spaces line
)))
1054 (defun org-odt-format-line (line)
1055 (case org-lparse-dyn-current-environment
1057 (org-odt-format-stylized-paragraph
1058 'fixedwidth
(org-odt-encode-plain-text line
)) "\n"))
1059 (t (concat line
"\n"))))
1061 (defun org-odt-format-comment (fmt &rest args
)
1062 (let ((comment (apply 'format fmt args
)))
1063 (format "\n<!-- %s -->\n" comment
)))
1065 (defun org-odt-format-org-entity (wd)
1066 (org-entity-get-representation wd
'utf8
))
1068 (defun org-odt-fill-tabs-and-spaces (line)
1069 (replace-regexp-in-string
1070 "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
1072 ((string= s
"\t") (org-odt-format-tabs))
1073 (t (org-odt-format-spaces (length s
))))) line
))
1075 (defcustom org-export-odt-fontify-srcblocks t
1076 "Specify whether or not source blocks need to be fontified.
1077 Turn this option on if you want to colorize the source code
1078 blocks in the exported file. For colorization to work, you need
1079 to make available an enhanced version of `htmlfontify' library."
1081 :group
'org-export-odt
)
1083 (defun org-odt-format-source-line-with-line-number-and-label
1084 (line rpllbl num fontifier par-style
)
1086 (let ((keep-label (not (numberp rpllbl
)))
1087 (ref (org-find-text-property-in-string 'org-coderef line
)))
1088 (setq line
(concat line
(and keep-label ref
(format "(%s)" ref
))))
1089 (setq line
(funcall fontifier line
))
1091 (setq line
(org-odt-format-target line
(concat "coderef-" ref
))))
1092 (setq line
(org-odt-format-stylized-paragraph par-style line
))
1094 (org-odt-format-tags '("<text:list-item>" .
"</text:list-item>") line
))))
1096 (defun org-odt-format-source-code-or-example-plain
1097 (lines lang caption textareap cols rows num cont rpllbl fmt
)
1098 "Format source or example blocks much like fixedwidth blocks.
1099 Use this when `org-export-odt-fontify-srcblocks' option is turned
1101 (let* ((lines (org-split-string lines
"[\r\n]"))
1102 (line-count (length lines
))
1107 (org-odt-format-source-line-with-line-number-and-label
1108 line rpllbl num
'org-odt-encode-plain-text
1109 (if (= i line-count
) "OrgFixedWidthBlockLastLine"
1110 "OrgFixedWidthBlock")))
1113 (defvar org-src-block-paragraph-format
1114 "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
1115 <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
1116 <style:background-image/>
1117 </style:paragraph-properties>
1118 <style:text-properties fo:color=\"%s\"/>
1120 "Custom paragraph style for colorized source and example blocks.
1121 This style is much the same as that of \"OrgFixedWidthBlock\"
1122 except that the foreground and background colors are set
1123 according to the default face identified by the `htmlfontify'.")
1125 (defun org-odt-hfy-face-to-css (fn)
1126 "Create custom style for face FN.
1127 When FN is the default face, use it's foreground and background
1128 properties to create \"OrgSrcBlock\" paragraph style. Otherwise
1129 use it's color attribute to create a character style whose name
1130 is obtained from FN. Currently all attributes of FN other than
1133 The style name for a face FN is derived using the following
1134 operations on the face name in that order - de-dash, CamelCase
1135 and prefix with \"OrgSrc\". For example,
1136 `font-lock-function-name-face' is associated with
1137 \"OrgSrcFontLockFunctionNameFace\"."
1138 (let* ((css-list (hfy-face-to-style fn
))
1139 (style-name ((lambda (fn)
1142 'capitalize
(split-string
1143 (hfy-face-or-def-to-name fn
) "-")
1145 (color-val (cdr (assoc "color" css-list
)))
1146 (background-color-val (cdr (assoc "background" css-list
)))
1147 (style (and org-export-odt-create-custom-styles-for-srcblocks
1150 (format org-src-block-paragraph-format
1151 background-color-val color-val
))
1155 <style:style style:name=\"%s\" style:family=\"text\">
1156 <style:text-properties fo:color=\"%s\"/>
1157 </style:style>" style-name color-val
))))))
1158 (cons style-name style
)))
1160 (defun org-odt-insert-custom-styles-for-srcblocks (styles)
1161 "Save STYLES used for colorizing of source blocks.
1162 Update styles.xml with styles that were collected as part of
1163 `org-odt-hfy-face-to-css' callbacks."
1165 (with-current-buffer
1166 (find-file-noselect (expand-file-name "styles.xml") t
)
1167 (goto-char (point-min))
1168 (when (re-search-forward "</office:styles>" nil t
)
1169 (goto-char (match-beginning 0))
1170 (insert "\n<!-- Org Htmlfontify Styles -->\n" styles
"\n")))))
1172 (defun org-odt-format-source-code-or-example-colored
1173 (lines lang caption textareap cols rows num cont rpllbl fmt
)
1174 "Format source or example blocks using `htmlfontify-string'.
1175 Use this routine when `org-export-odt-fontify-srcblocks' option
1177 (let* ((lang-m (and lang
(or (cdr (assoc lang org-src-lang-modes
)) lang
)))
1178 (mode (and lang-m
(intern (concat (if (symbolp lang-m
)
1179 (symbol-name lang-m
)
1181 (org-inhibit-startup t
)
1182 (org-startup-folded nil
)
1183 (lines (with-temp-buffer
1185 (if (functionp mode
) (funcall mode
) (fundamental-mode))
1186 (font-lock-fontify-buffer)
1188 (hfy-html-quote-regex "\\([<\"&> ]\\)")
1189 (hfy-html-quote-map '(("\"" """)
1194 (" " "<text:tab/>")))
1195 (hfy-face-to-css 'org-odt-hfy-face-to-css
)
1196 (hfy-optimisations-1 (copy-seq hfy-optimisations
))
1197 (hfy-optimisations (add-to-list 'hfy-optimisations-1
1199 (hfy-begin-span-handler
1200 (lambda (style text-block text-id text-begins-block-p
)
1201 (insert (format "<text:span text:style-name=\"%s\">" style
))))
1202 (hfy-end-span-handler (lambda nil
(insert "</text:span>"))))
1203 (when (fboundp 'htmlfontify-string
)
1204 (let* ((lines (org-split-string lines
"[\r\n]"))
1205 (line-count (length lines
))
1210 (org-odt-format-source-line-with-line-number-and-label
1211 line rpllbl num
'htmlfontify-string
1212 (if (= i line-count
) "OrgSrcBlockLastLine" "OrgSrcBlock")))
1215 (defun org-odt-format-source-code-or-example (lines lang caption textareap
1218 "Format source or example blocks for export.
1219 Use `org-odt-format-source-code-or-example-plain' or
1220 `org-odt-format-source-code-or-example-colored' depending on the
1221 value of `org-export-odt-fontify-srcblocks."
1222 (setq lines
(org-export-number-lines
1223 lines
0 0 num cont rpllbl fmt
'preprocess
)
1225 (or (and org-export-odt-fontify-srcblocks
1226 (or (featurep 'htmlfontify
)
1227 (require 'htmlfontify
))
1228 (fboundp 'htmlfontify-string
)
1229 'org-odt-format-source-code-or-example-colored
)
1230 'org-odt-format-source-code-or-example-plain
)
1231 lines lang caption textareap cols rows num cont rpllbl fmt
))
1233 (let ((extra (format " text:continue-numbering=\"%s\""
1234 (if cont
"true" "false"))))
1235 (org-odt-format-tags
1236 '("<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>"
1237 .
"</text:list>") lines extra
))))
1239 (defun org-odt-remap-stylenames (style-name)
1241 (cdr (assoc style-name
'(("timestamp-wrapper" .
"OrgTimestampWrapper")
1242 ("timestamp" .
"OrgTimestamp")
1243 ("timestamp-kwd" .
"OrgTimestampKeyword")
1245 ("todo" .
"OrgTodo")
1246 ("done" .
"OrgDone")
1247 ("target" .
"OrgTarget"))))
1250 (defun org-odt-format-fontify (text style
&optional id
)
1254 (org-odt-remap-stylenames style
))
1256 (org-odt-get-style-name-for-entity 'character style
))
1258 (assert (< 1 (length style
)))
1259 (let ((parent-style (pop style
)))
1260 (mapconcat (lambda (s)
1261 ;; (assert (stringp s) t)
1262 (org-odt-remap-stylenames s
)) style
"")
1263 (org-odt-remap-stylenames parent-style
)))
1264 (t (error "Don't how to handle style %s" style
)))))
1265 (org-odt-format-tags
1266 '("<text:span text:style-name=\"%s\">" .
"</text:span>")
1269 (defun org-odt-relocate-relative-path (path dir
)
1270 (if (file-name-absolute-p path
) path
1271 (file-relative-name (expand-file-name path dir
)
1272 (expand-file-name "eyecandy" dir
))))
1274 (defun org-odt-format-inline-image (thefile)
1275 (let* ((thelink (if (file-name-absolute-p thefile
) thefile
1276 (org-xml-format-href
1277 (org-odt-relocate-relative-path
1278 thefile org-current-export-file
))))
1280 (org-odt-format-tags
1281 "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
1282 (if org-export-odt-embed-images
1283 (org-odt-copy-image-file thefile
) thelink
))))
1284 (org-export-odt-format-image thefile href
)))
1286 (defun org-export-odt-format-formula (src href
&optional embed-as
)
1287 "Create image tag with source and attributes."
1289 (let* ((caption (org-find-text-property-in-string 'org-caption src
))
1290 (caption (and caption
(org-xml-format-desc caption
)))
1291 (label (org-find-text-property-in-string 'org-label src
))
1292 (latex-frag (org-find-text-property-in-string 'org-latex-src src
))
1293 (embed-as (or embed-as
1295 (org-find-text-property-in-string
1296 'org-latex-src-embed-type src
))
1297 (if (or caption label
) 'paragraph
'character
)))
1300 (setq href
(org-propertize href
:title
"LaTeX Fragment"
1301 :description latex-frag
)))
1303 ((eq embed-as
'character
)
1304 (org-odt-format-entity "InlineFormula" href width height
))
1306 (org-lparse-end-paragraph)
1307 (org-lparse-insert-list-table
1308 `((,(org-odt-format-entity
1309 (if caption
"CaptionedDisplayFormula" "DisplayFormula")
1310 href width height caption nil
)
1312 (org-odt-format-entity-caption label nil
"__MathFormula__"))))
1313 nil nil nil
"OrgEquation" nil
'((1 "c" 8) (2 "c" 1)))
1314 (throw 'nextline nil
))))))
1316 (defvar org-odt-embedded-formulas-count
0)
1317 (defun org-odt-copy-formula-file (path)
1318 "Returns the internal name of the file"
1319 (let* ((src-file (expand-file-name
1320 path
(file-name-directory org-current-export-file
)))
1321 (target-dir (format "Formula-%04d/"
1322 (incf org-odt-embedded-formulas-count
)))
1323 (target-file (concat target-dir
"content.xml")))
1324 (when (not org-lparse-to-buffer
)
1325 (message "Embedding %s as %s ..."
1326 (substring-no-properties path
) target-file
)
1328 (make-directory target-dir
)
1329 (org-odt-create-manifest-file-entry
1330 "application/vnd.oasis.opendocument.formula" target-dir
"1.2")
1332 (case (org-odt-is-formula-link-p src-file
)
1334 (copy-file src-file target-file
'overwrite
))
1336 (org-odt-zip-extract-one src-file
"content.xml" target-dir
))
1338 (error "%s is not a formula file" src-file
)))
1340 (org-odt-create-manifest-file-entry "text/xml" target-file
))
1343 (defun org-odt-format-inline-formula (thefile)
1344 (let* ((thelink (if (file-name-absolute-p thefile
) thefile
1345 (org-xml-format-href
1346 (org-odt-relocate-relative-path
1347 thefile org-current-export-file
))))
1349 (org-odt-format-tags
1350 "<draw:object xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
1351 (file-name-directory (org-odt-copy-formula-file thefile
)))))
1352 (org-export-odt-format-formula thefile href
)))
1354 (defun org-odt-is-formula-link-p (file)
1355 (let ((case-fold-search nil
))
1357 ((string-match "\\.\\(mathml\\|mml\\)\\'" file
)
1359 ((string-match "\\.odf\\'" file
)
1362 (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
1364 "Make a OpenDocument link.
1365 OPT-PLIST is an options list.
1366 TYPE-1 is the device-type of the link (THIS://foo.html).
1367 PATH is the path of the link (http://THIS#location).
1368 FRAGMENT is the fragment part of the link, if any (foo.html#THIS).
1369 DESC is the link description, if any.
1370 ATTR is a string of other attributes of the a element."
1371 (declare (special org-lparse-par-open
))
1373 (let* ((may-inline-p
1374 (and (member type-1
'("http" "https" "file"))
1375 (org-lparse-should-inline-p path descp
)
1377 (type (if (equal type-1
"id") "file" type-1
))
1381 ;; check for inlined images
1382 ((and (member type
'("file"))
1385 filename org-export-odt-inline-image-extensions
)
1386 (or (eq t org-export-odt-inline-images
)
1387 (and org-export-odt-inline-images
(not descp
))))
1388 (org-odt-format-inline-image thefile
))
1389 ;; check for embedded formulas
1390 ((and (member type
'("file"))
1392 (org-odt-is-formula-link-p filename
)
1394 (org-odt-format-inline-formula thefile
))
1395 ((string= type
"coderef")
1396 (let* ((ref fragment
)
1397 (lineno-or-ref (cdr (assoc ref org-export-code-refs
)))
1398 (desc (and descp desc
))
1399 (org-odt-suppress-xref nil
)
1400 (href (org-xml-format-href (concat "#coderef-" ref
))))
1402 ((and (numberp lineno-or-ref
) (not desc
))
1403 (org-odt-format-link lineno-or-ref href
))
1404 ((and (numberp lineno-or-ref
) desc
1405 (string-match (regexp-quote (concat "(" ref
")")) desc
))
1406 (format (replace-match "%s" t t desc
)
1407 (org-odt-format-link lineno-or-ref href
)))
1410 (if (and desc
(string-match
1411 (regexp-quote (concat "(" ref
")"))
1413 (replace-match "%s" t t desc
)
1416 (org-odt-format-link (org-xml-format-desc desc
) href
)))))
1418 (when (string= type
"file")
1421 ((file-name-absolute-p path
)
1422 (concat "file://" (expand-file-name path
)))
1423 (t (org-odt-relocate-relative-path
1424 thefile org-current-export-file
)))))
1426 (when (and (member type
'("" "http" "https" "file")) fragment
)
1427 (setq thefile
(concat thefile
"#" fragment
)))
1429 (setq thefile
(org-xml-format-href thefile
))
1431 (when (not (member type
'("" "file")))
1432 (setq thefile
(concat type
":" thefile
)))
1434 (let ((org-odt-suppress-xref nil
))
1435 (org-odt-format-link
1436 (org-xml-format-desc desc
) thefile attr
)))))))
1438 (defun org-odt-format-heading (text level
&optional id
)
1439 (let* ((text (if id
(org-odt-format-target text id
) text
)))
1440 (org-odt-format-tags
1441 '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
1442 "</text:h>") text level level
)))
1444 (defun org-odt-format-headline (title extra-targets tags
1445 &optional snumber level
)
1447 (org-lparse-format 'EXTRA-TARGETS extra-targets
)
1449 ;; No need to generate section numbers. They are auto-generated by
1452 ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
1454 (and tags
(concat (org-lparse-format 'SPACES
3)
1455 (org-lparse-format 'ORG-TAGS tags
)))))
1457 (defun org-odt-format-anchor (text name
&optional class
)
1458 (org-odt-format-target text name
))
1460 (defun org-odt-format-bookmark (text id
)
1462 (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id
)
1465 (defun org-odt-format-target (text id
)
1466 (let ((name (concat org-export-odt-bookmark-prefix id
)))
1468 (and id
(org-odt-format-tags
1469 "<text:bookmark-start text:name=\"%s\"/>" "" name
))
1470 (org-odt-format-bookmark text id
)
1471 (and id
(org-odt-format-tags
1472 "<text:bookmark-end text:name=\"%s\"/>" "" name
)))))
1474 (defun org-odt-format-footnote (n def
)
1475 (let ((id (concat "fn" n
))
1476 (note-class "footnote")
1477 (par-style "Footnote"))
1478 (org-odt-format-tags
1479 '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
1482 (org-odt-format-tags
1483 '("<text:note-citation>" .
"</text:note-citation>")
1485 (org-odt-format-tags
1486 '("<text:note-body>" .
"</text:note-body>")
1490 (defun org-odt-format-footnote-reference (n def refcnt
)
1492 (org-odt-format-footnote n def
)
1493 (org-odt-format-footnote-ref n
)))
1495 (defun org-odt-format-footnote-ref (n)
1496 (let ((note-class "footnote")
1498 (ref-name (concat "fn" n
)))
1499 (org-odt-format-tags
1500 '("<text:span text:style-name=\"%s\">" .
"</text:span>")
1501 (org-odt-format-tags
1502 '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" .
"</text:note-ref>")
1503 n note-class ref-format ref-name
)
1506 (defun org-odt-get-image-name (file-name)
1510 (concat (sha1 file-name
) "." (file-name-extension file-name
)) "Pictures")))
1512 (defun org-export-odt-format-image (src href
&optional embed-as
)
1513 "Create image tag with source and attributes."
1515 (let* ((caption (org-find-text-property-in-string 'org-caption src
))
1516 (caption (and caption
(org-xml-format-desc caption
)))
1517 (attr (org-find-text-property-in-string 'org-attributes src
))
1518 (label (org-find-text-property-in-string 'org-label src
))
1519 (latex-frag (org-find-text-property-in-string
1520 'org-latex-src src
))
1521 (category (and latex-frag
"__DvipngImage__"))
1522 (embed-as (or embed-as
1524 (or (org-find-text-property-in-string
1525 'org-latex-src-embed-type src
) 'character
)
1527 (attr-plist (org-lparse-get-block-params attr
))
1528 (size (org-odt-image-size-from-file
1529 src
(plist-get attr-plist
:width
)
1530 (plist-get attr-plist
:height
)
1531 (plist-get attr-plist
:scale
) nil embed-as
))
1532 (width (car size
)) (height (cdr size
)))
1534 (setq href
(org-propertize href
:title
"LaTeX Fragment"
1535 :description latex-frag
)))
1537 ((not (or caption label
))
1539 (paragraph (org-odt-format-entity "DisplayImage" href width height
))
1540 (character (org-odt-format-entity "InlineImage" href width height
))
1541 (t (error "Unknown value for embed-as %S" embed-as
))))
1543 (org-odt-format-entity
1544 "CaptionedDisplayImage" href width height caption label category
))))))
1546 (defun org-odt-format-object-description (title description
)
1547 (concat (and title
(org-odt-format-tags
1548 '("<svg:title>" .
"</svg:title>")
1549 (org-odt-encode-plain-text title t
)))
1550 (and description
(org-odt-format-tags
1551 '("<svg:desc>" .
"</svg:desc>")
1552 (org-odt-encode-plain-text description t
)))))
1554 (defun org-odt-format-frame (text width height style
&optional
1558 (if width
(format " svg:width=\"%0.2fcm\"" width
) "")
1559 (if height
(format " svg:height=\"%0.2fcm\"" height
) "")
1561 (format " text:anchor-type=\"%s\"" (or anchor-type
"paragraph")))))
1562 (org-odt-format-tags
1563 '("<draw:frame draw:style-name=\"%s\"%s>" .
"</draw:frame>")
1564 (concat text
(org-odt-format-object-description
1565 (get-text-property 0 :title text
)
1566 (get-text-property 0 :description text
)))
1567 style frame-attrs
)))
1569 (defun org-odt-format-textbox (text width height style
&optional
1571 (org-odt-format-frame
1572 (org-odt-format-tags
1573 '("<draw:text-box %s>" .
"</draw:text-box>")
1574 text
(concat (format " fo:min-height=\"%0.2fcm\"" (or height
.2))
1575 (format " fo:min-width=\"%0.2fcm\"" (or width
.2))))
1576 width nil style extra anchor-type
))
1578 (defun org-odt-format-inlinetask (heading content
1579 &optional todo priority tags
)
1580 (org-odt-format-stylized-paragraph
1581 nil
(org-odt-format-textbox
1582 (concat (org-odt-format-stylized-paragraph
1583 "OrgInlineTaskHeading"
1585 'HEADLINE
(concat (org-lparse-format-todo todo
) " " heading
)
1587 content
) nil nil
"OrgInlineTaskFrame" " style:rel-width=\"100%\"")))
1589 (defvar org-odt-entity-frame-styles
1590 '(("InlineImage" "__Figure__" ("OrgInlineImage" nil
"as-char"))
1591 ("DisplayImage" "__Figure__" ("OrgDisplayImage" nil
"paragraph"))
1592 ("CaptionedDisplayImage" "__Figure__"
1593 ("OrgCaptionedImage"
1594 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
1595 ("OrgImageCaptionFrame"))
1596 ("InlineFormula" "__MathFormula__" ("OrgInlineFormula" nil
"as-char"))
1597 ("DisplayFormula" "__MathFormula__" ("OrgDisplayFormula" nil
"as-char"))
1598 ("CaptionedDisplayFormula" "__MathFormula__"
1599 ("OrgCaptionedFormula" nil
"paragraph")
1600 ("OrgFormulaCaptionFrame" nil
"as-char"))))
1602 (defun org-odt-format-entity (entity href width height
1603 &optional caption label category
)
1604 (let* ((entity-style (assoc entity org-odt-entity-frame-styles
))
1605 (entity-frame (apply 'org-odt-format-frame
1606 href width height
(nth 2 entity-style
))))
1607 (if (not (or caption label
)) entity-frame
1608 (apply 'org-odt-format-textbox
1609 (org-odt-format-stylized-paragraph
1611 (concat entity-frame
1612 (org-odt-format-entity-caption
1613 label caption
(or category
(nth 1 entity-style
)))))
1614 width height
(nth 3 entity-style
)))))
1616 (defvar org-odt-embedded-images-count
0)
1617 (defun org-odt-copy-image-file (path)
1618 "Returns the internal name of the file"
1619 (let* ((image-type (file-name-extension path
))
1620 (media-type (format "image/%s" image-type
))
1621 (src-file (expand-file-name
1622 path
(file-name-directory org-current-export-file
)))
1623 (target-dir "Images/")
1625 (format "%s%04d.%s" target-dir
1626 (incf org-odt-embedded-images-count
) image-type
)))
1627 (when (not org-lparse-to-buffer
)
1628 (message "Embedding %s as %s ..."
1629 (substring-no-properties path
) target-file
)
1631 (when (= 1 org-odt-embedded-images-count
)
1632 (make-directory target-dir
)
1633 (org-odt-create-manifest-file-entry "" target-dir
))
1635 (copy-file src-file target-file
'overwrite
)
1636 (org-odt-create-manifest-file-entry media-type target-file
))
1639 (defvar org-export-odt-image-size-probe-method
1640 '(emacs imagemagick force
)
1641 "Ordered list of methods by for determining size of an embedded
1644 (defvar org-export-odt-default-image-sizes-alist
1645 '(("character" .
(5 .
0.4))
1646 ("paragraph" .
(5 .
5)))
1647 "Hardcoded image dimensions one for each of the anchor
1650 ;; A4 page size is 21.0 by 29.7 cms
1651 ;; The default page settings has 2cm margin on each of the sides. So
1652 ;; the effective text area is 17.0 by 25.7 cm
1653 (defvar org-export-odt-max-image-size
'(17.0 .
20.0)
1654 "Limiting dimensions for an embedded image.")
1656 (defun org-odt-do-image-size (probe-method file
&optional dpi anchor-type
)
1657 (setq dpi
(or dpi org-export-odt-pixels-per-inch
))
1658 (setq anchor-type
(or anchor-type
"paragraph"))
1659 (flet ((size-in-cms (size-in-pixels)
1660 (flet ((pixels-to-cms (pixels)
1661 (let* ((cms-per-inch 2.54)
1662 (inches (/ pixels dpi
)))
1663 (* cms-per-inch inches
))))
1665 (cons (pixels-to-cms (car size-in-pixels
))
1666 (pixels-to-cms (cdr size-in-pixels
)))))))
1669 (size-in-cms (ignore-errors (image-size (create-image file
) 'pixels
))))
1672 (let ((dim (shell-command-to-string
1673 (format "identify -format \"%%w:%%h\" \"%s\"" file
))))
1674 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim
)
1675 (cons (string-to-number (match-string 1 dim
))
1676 (string-to-number (match-string 2 dim
)))))))
1678 (cdr (assoc-string anchor-type
1679 org-export-odt-default-image-sizes-alist
))))))
1681 (defun org-odt-image-size-from-file (file &optional user-width
1682 user-height scale dpi embed-as
)
1683 (unless (file-name-absolute-p file
)
1684 (setq file
(expand-file-name
1685 file
(file-name-directory org-current-export-file
))))
1686 (let* (size width height
)
1687 (unless (and user-height user-width
)
1688 (loop for probe-method in org-export-odt-image-size-probe-method
1690 do
(setq size
(org-odt-do-image-size
1691 probe-method file dpi embed-as
)))
1692 (or size
(error "Cannot determine Image size. Aborting ..."))
1693 (setq width
(car size
) height
(cdr size
)))
1696 (setq width
(* width scale
) height
(* height scale
)))
1697 ((and user-height user-width
)
1698 (setq width user-width height user-height
))
1700 (setq width
(* user-height
(/ width height
)) height user-height
))
1702 (setq height
(* user-width
(/ height width
)) width user-width
))
1704 ;; ensure that an embedded image fits comfortably within a page
1705 (let ((max-width (car org-export-odt-max-image-size
))
1706 (max-height (cdr org-export-odt-max-image-size
)))
1707 (when (or (> width max-width
) (> height max-height
))
1708 (let* ((scale1 (/ max-width width
))
1709 (scale2 (/ max-height height
))
1710 (scale (min scale1 scale2
)))
1711 (setq width
(* scale width
) height
(* scale height
)))))
1712 (cons width height
)))
1714 (defvar org-odt-entity-labels-alist nil
1715 "Associate Labels with the Labelled entities.
1716 Each element of the alist is of the form (LABEL-NAME
1717 CATEGORY-NAME SEQNO LABEL-STYLE-NAME). LABEL-NAME is same as
1718 that specified by \"#+LABEL: ...\" line. CATEGORY-NAME is the
1719 type of the entity that LABEL-NAME is attached to. CATEGORY-NAME
1720 can be one of \"Table\", \"Figure\" or \"Equation\". SEQNO is
1721 the unique number assigned to the referenced entity on a
1722 per-CATEGORY basis. It is generated sequentially and is 1-based.
1723 LABEL-STYLE-NAME is a key `org-odt-label-styles'.
1725 See `org-odt-add-label-definition' and
1726 `org-odt-fixup-label-references'.")
1728 (defvar org-odt-entity-counts-plist nil
1729 "Plist of running counters of SEQNOs for each of the CATEGORY-NAMEs.
1730 See `org-odt-entity-labels-alist' for known CATEGORY-NAMEs.")
1732 (defvar org-odt-label-styles
1733 '(("text" "(%n)" "text" "(%n)")
1734 ("category-and-value" "%e %n%c" "category-and-value" "%e %n"))
1735 "Specify how labels are applied and referenced.
1736 This is an alist where each element is of the
1737 form (LABEL-STYLE-NAME LABEL-ATTACH-FMT LABEL-REF-MODE
1740 LABEL-ATTACH-FMT controls how labels and captions are attached to
1741 an entity. It may contain following specifiers - %e, %n and %c.
1742 %e is replaced with the CATEGORY-NAME. %n is replaced with
1743 \"<text:sequence ...> SEQNO </text:sequence>\". %c is replaced
1744 with CAPTION. See `org-odt-format-label-definition'.
1746 LABEL-REF-MODE and LABEL-REF-FMT controls how label references
1747 are generated. The following XML is generated for a label
1748 reference - \"<text:sequence-ref
1749 text:reference-format=\"LABEL-REF-MODE\" ...> LABEL-REF-FMT
1750 </text:sequence-ref>\". LABEL-REF-FMT may contain following
1751 specifiers - %e and %n. %e is replaced with the CATEGORY-NAME.
1752 %n is replaced with SEQNO. See
1753 `org-odt-format-label-reference'.")
1755 (defvar org-odt-category-map-alist
1756 '(("__Table__" "Table" "category-and-value")
1757 ("__Figure__" "Figure" "category-and-value")
1758 ("__MathFormula__" "Equation" "text")
1759 ("__DvipngImage__" "Equation" "category-and-value"))
1760 "Map a CATEGORY-HANDLE to CATEGORY-NAME and LABEL-STYLE.
1761 This is an alist where each element is of the form
1762 \\(CATEGORY-HANDLE CATEGORY-NAME LABEL-STYLE\\). CATEGORY_HANDLE
1763 could either be one of the internal handles (as seen above) or be
1764 derived from the \"#+LABEL:<label-name>\" specification. See
1765 `org-export-odt-get-category-from-label'. CATEGORY-NAME and
1766 LABEL-STYLE are used for generating ODT labels. See
1767 `org-odt-label-styles'.")
1769 (defvar org-export-odt-user-categories
1770 '("Illustration" "Table" "Text" "Drawing" "Equation" "Figure"))
1772 (defvar org-export-odt-get-category-from-label nil
1773 "Should category of label be inferred from label itself.
1774 When this option is non-nil, a label is parsed in to two
1775 component parts delimited by a \":\" (colon) as shown here -
1776 #+LABEL:[CATEGORY-HANDLE:]EXTRA. The CATEGORY-HANDLE is mapped
1777 to a CATEGORY-NAME and LABEL-STYLE using
1778 `org-odt-category-map-alist'. (If no such map is provided and
1779 CATEGORY-NAME is set to CATEGORY-HANDLE and LABEL-STYLE is set to
1780 \"category-and-value\"). If CATEGORY-NAME so obtained is listed
1781 under `org-export-odt-user-categories' then the user specified
1782 styles are used. Otherwise styles as determined by the internal
1783 CATEGORY-HANDLE is used. See
1784 `org-odt-get-label-category-and-style' for details.")
1786 (defun org-odt-get-label-category-and-style (label default-category
)
1787 "See `org-export-odt-get-category-from-label'."
1788 (let ((default-category-map
1789 (assoc default-category org-odt-category-map-alist
))
1790 user-category user-category-map category
)
1792 ((not org-export-odt-get-category-from-label
)
1793 default-category-map
)
1794 ((not (setq user-category
1796 (and (string-match "\\`\\(.*\\):.+" label
)
1797 (match-string 1 label
)))))
1798 default-category-map
)
1800 (setq user-category-map
1801 (or (assoc user-category org-odt-category-map-alist
)
1802 (list nil user-category
"category-and-value"))
1803 category
(nth 1 user-category-map
))
1804 (if (member category org-export-odt-user-categories
)
1806 default-category-map
)))))
1808 (defun org-odt-add-label-definition (label default-category
)
1809 "Create an entry in `org-odt-entity-labels-alist' and return it."
1810 (setq label
(substring-no-properties label
))
1811 (let* ((label-props (org-odt-get-label-category-and-style
1812 label default-category
))
1813 (category (nth 1 label-props
))
1815 (label-style (nth 2 label-props
))
1816 (sequence-var (intern (mapconcat
1818 (org-split-string counter
) "-")))
1819 (seqno (1+ (or (plist-get org-odt-entity-counts-plist sequence-var
)
1821 (label-props (list label category seqno label-style
)))
1822 (setq org-odt-entity-counts-plist
1823 (plist-put org-odt-entity-counts-plist sequence-var seqno
))
1824 (push label-props org-odt-entity-labels-alist
)
1827 (defun org-odt-format-label-definition (caption label category seqno label-style
)
1830 (cadr (assoc-string label-style org-odt-label-styles t
))
1832 (?n .
,(org-odt-format-tags
1833 '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" .
"</text:sequence>")
1834 (format "%d" seqno
) label category category
))
1835 (?c .
,(or (and caption
(concat ": " caption
)) "")))))
1837 (defun org-odt-format-label-reference (label category seqno label-style
)
1840 (let* ((fmt (cddr (assoc-string label-style org-odt-label-styles t
)))
1843 (org-odt-format-tags
1844 '("<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">"
1845 .
"</text:sequence-ref>")
1846 (format-spec fmt2
`((?e .
,category
)
1847 (?n .
,(format "%d" seqno
)))) fmt1 label
))))
1849 (defun org-odt-fixup-label-references ()
1850 (goto-char (point-min))
1851 (while (re-search-forward
1852 "<text:sequence-ref text:ref-name=\"\\([^\"]+\\)\">[ \t\n]*</text:sequence-ref>"
1854 (let* ((label (match-string 1))
1855 (label-def (assoc label org-odt-entity-labels-alist
))
1857 (apply 'org-odt-format-label-reference label-def
))))
1858 (if rpl
(replace-match rpl t t
)
1860 (format "Unable to resolve reference to label \"%s\"" label
))))))
1862 (defun org-odt-format-entity-caption (label caption category
)
1864 (apply 'org-odt-format-label-definition
1865 caption
(org-odt-add-label-definition label category
)))
1868 (defun org-odt-format-tags (tag text
&rest args
)
1869 (let ((prefix (when org-lparse-encode-pending
"@"))
1870 (suffix (when org-lparse-encode-pending
"@")))
1871 (apply 'org-lparse-format-tags tag text prefix suffix args
)))
1873 (defvar org-odt-manifest-file-entries nil
)
1874 (defun org-odt-init-outfile (filename)
1875 (unless (executable-find "zip")
1876 ;; Not at all OSes ship with zip by default
1877 (error "Executable \"zip\" needed for creating OpenDocument files"))
1879 (let* ((outdir (make-temp-file
1880 (format org-export-odt-tmpdir-prefix org-lparse-backend
) t
))
1881 (content-file (expand-file-name "content.xml" outdir
)))
1884 (with-current-buffer (find-file-noselect content-file t
))
1887 (setq org-odt-manifest-file-entries nil
1888 org-odt-embedded-images-count
0
1889 org-odt-embedded-formulas-count
0
1890 org-odt-entity-labels-alist nil
1891 org-odt-entity-counts-plist nil
)
1894 (defcustom org-export-odt-prettify-xml nil
1895 "Specify whether or not the xml output should be prettified.
1896 When this option is turned on, `indent-region' is run on all
1897 component xml buffers before they are saved. Turn this off for
1898 regular use. Turn this on if you need to examine the xml
1900 :group
'org-export-odt
1903 (defvar hfy-user-sheet-assoc
) ; bound during org-do-lparse
1904 (defun org-odt-save-as-outfile (target opt-plist
)
1906 (org-odt-update-meta-file opt-plist
)
1908 ;; write styles file
1909 (when (equal org-lparse-backend
'odt
)
1910 (org-odt-update-styles-file opt-plist
))
1912 ;; create mimetype file
1913 (let ((mimetype (org-odt-write-mimetype-file org-lparse-backend
)))
1914 (org-odt-create-manifest-file-entry mimetype
"/" "1.2"))
1916 ;; create a manifest entry for content.xml
1917 (org-odt-create-manifest-file-entry "text/xml" "content.xml")
1919 ;; write out the manifest entries before zipping
1920 (org-odt-write-manifest-file)
1922 (let ((xml-files '("mimetype" "META-INF/manifest.xml" "content.xml"
1924 (zipdir default-directory
))
1925 (when (equal org-lparse-backend
'odt
)
1926 (push "styles.xml" xml-files
))
1927 (message "Switching to directory %s" (expand-file-name zipdir
))
1929 ;; save all xml files
1930 (mapc (lambda (file)
1931 (with-current-buffer
1932 (find-file-noselect (expand-file-name file
) t
)
1933 ;; prettify output if needed
1934 (when org-export-odt-prettify-xml
1935 (indent-region (point-min) (point-max)))
1939 (let* ((target-name (file-name-nondirectory target
))
1940 (target-dir (file-name-directory target
))
1941 (cmds `(("zip" "-mX0" ,target-name
"mimetype")
1942 ("zip" "-rmTq" ,target-name
"."))))
1943 (when (file-exists-p target
)
1944 ;; FIXME: If the file is locked this throws a cryptic error
1945 (delete-file target
))
1947 (let ((coding-system-for-write 'no-conversion
) exitcode err-string
)
1948 (message "Creating odt file...")
1951 (message "Running %s" (mapconcat 'identity cmd
" "))
1953 (with-output-to-string
1955 (apply 'call-process
(car cmd
)
1956 nil standard-output nil
(cdr cmd
)))))
1957 (or (zerop exitcode
)
1958 (ignore (message "%s" err-string
))
1959 (error "Unable to create odt file (%S)" exitcode
)))
1962 ;; move the file from outdir to target-dir
1963 (rename-file target-name target-dir
)
1965 ;; kill all xml buffers
1966 (mapc (lambda (file)
1968 (find-file-noselect (expand-file-name file zipdir
) t
)))
1971 (delete-directory zipdir
)))
1972 (message "Created %s" target
)
1973 (set-buffer (find-file-noselect target t
)))
1975 (defconst org-odt-manifest-file-entry-tag
1977 <manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
1979 (defun org-odt-create-manifest-file-entry (&rest args
)
1980 (push args org-odt-manifest-file-entries
))
1982 (defun org-odt-write-manifest-file ()
1983 (make-directory "META-INF")
1984 (let ((manifest-file (expand-file-name "META-INF/manifest.xml")))
1985 (with-current-buffer
1986 (find-file-noselect manifest-file t
)
1988 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
1989 <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
1991 (lambda (file-entry)
1992 (let* ((version (nth 2 file-entry
))
1994 (format " manifest:version=\"%s\"" version
)
1997 (format org-odt-manifest-file-entry-tag
1998 (nth 0 file-entry
) (nth 1 file-entry
) extra
))))
1999 org-odt-manifest-file-entries
)
2000 (insert "\n</manifest:manifest>"))))
2002 (defun org-odt-update-meta-file (opt-plist)
2003 (let ((date (org-odt-iso-date-from-org-timestamp
2004 (plist-get opt-plist
:date
)))
2005 (author (or (plist-get opt-plist
:author
) ""))
2006 (email (plist-get opt-plist
:email
))
2007 (keywords (plist-get opt-plist
:keywords
))
2008 (description (plist-get opt-plist
:description
))
2009 (title (plist-get opt-plist
:title
)))
2012 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
2013 <office:document-meta
2014 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
2015 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
2016 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
2017 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
2018 xmlns:ooo=\"http://openoffice.org/2004/office\"
2019 office:version=\"1.2\">
2021 (org-odt-format-author)
2022 (org-odt-format-tags
2023 '("\n<meta:initial-creator>" .
"</meta:initial-creator>") author
)
2024 (org-odt-format-tags '("\n<dc:date>" .
"</dc:date>") date
)
2025 (org-odt-format-tags
2026 '("\n<meta:creation-date>" .
"</meta:creation-date>") date
)
2027 (org-odt-format-tags '("\n<meta:generator>" .
"</meta:generator>")
2028 (when org-export-creator-info
2029 (format "Org-%s/Emacs-%s"
2030 org-version emacs-version
)))
2031 (org-odt-format-tags '("\n<meta:keyword>" .
"</meta:keyword>") keywords
)
2032 (org-odt-format-tags '("\n<dc:subject>" .
"</dc:subject>") description
)
2033 (org-odt-format-tags '("\n<dc:title>" .
"</dc:title>") title
)
2035 " </office:meta>" "</office:document-meta>")
2036 nil
(expand-file-name "meta.xml")))
2038 ;; create a manifest entry for meta.xml
2039 (org-odt-create-manifest-file-entry "text/xml" "meta.xml"))
2041 (defun org-odt-update-styles-file (opt-plist)
2042 ;; write styles file
2043 (let ((styles-file (plist-get opt-plist
:odt-styles-file
)))
2044 (org-odt-copy-styles-file (and styles-file
2045 (read (org-trim styles-file
)))))
2047 ;; Update styles.xml - take care of outline numbering
2048 (with-current-buffer
2049 (find-file-noselect (expand-file-name "styles.xml") t
)
2050 ;; Don't make automatic backup of styles.xml file. This setting
2051 ;; prevents the backedup styles.xml file from being zipped in to
2052 ;; odt file. This is more of a hackish fix. Better alternative
2053 ;; would be to fix the zip command so that the output odt file
2054 ;; includes only the needed files and excludes any auto-generated
2055 ;; extra files like backups and auto-saves etc etc. Note that
2056 ;; currently the zip command zips up the entire temp directory so
2057 ;; that any auto-generated files created under the hood ends up in
2058 ;; the resulting odt file.
2059 (set (make-local-variable 'backup-inhibited
) t
)
2061 ;; Import local setting of `org-export-with-section-numbers'
2062 (org-lparse-bind-local-variables opt-plist
)
2063 (org-odt-configure-outline-numbering
2064 (if org-export-with-section-numbers org-export-headline-levels
0)))
2066 ;; Write custom stlyes for source blocks
2067 (org-odt-insert-custom-styles-for-srcblocks
2070 (format " %s\n" (cddr style
)))
2071 hfy-user-sheet-assoc
"")))
2073 (defun org-odt-write-mimetype-file (format)
2074 ;; create mimetype file
2077 (odt "application/vnd.oasis.opendocument.text")
2078 (odf "application/vnd.oasis.opendocument.formula")
2079 (t (error "Unknown OpenDocument backend %S" org-lparse-backend
)))))
2080 (write-region mimetype nil
(expand-file-name "mimetype"))
2083 (defun org-odt-finalize-outfile ()
2084 (org-odt-delete-empty-paragraphs))
2086 (defun org-odt-delete-empty-paragraphs ()
2087 (goto-char (point-min))
2088 (let ((open "<text:p[^>]*>")
2089 (close "</text:p>"))
2090 (while (re-search-forward (format "%s[ \r\n\t]*%s" open close
) nil t
)
2091 (replace-match ""))))
2093 (defcustom org-export-odt-convert-processes
2094 '(("BasicODConverter"
2095 ("soffice" "-norestore" "-invisible" "-headless"
2096 "\"macro:///BasicODConverter.Main.Convert(%I,%f,%O)\""))
2098 ("unoconv" "-f" "%f" "-o" "%d" "%i")))
2099 "Specify a list of document converters and their usage.
2100 The converters in this list are offered as choices while
2101 customizing `org-export-odt-convert-process'.
2103 This variable is an alist where each element is of the
2104 form (CONVERTER-NAME CONVERTER-PROCESS). CONVERTER-NAME is name
2105 of the converter. CONVERTER-PROCESS specifies the command-line
2106 syntax of the converter and is of the form (CONVERTER-PROGRAM
2107 ARG1 ARG2 ...). CONVERTER-PROGRAM is the name of the executable.
2108 ARG1, ARG2 etc are command line options that are passed to
2109 CONVERTER-PROGRAM. Format specifiers can be used in the ARGs and
2110 they are interpreted as below:
2112 %i input file name in full
2113 %I input file name as a URL
2114 %f format of the output file
2115 %o output file name in full
2116 %O output file name as a URL
2117 %d output dir in full
2118 %D output dir as a URL."
2119 :group
'org-export-odt
2122 (const :tag
"None" nil
)
2123 (alist :tag
"Converters"
2124 :key-type
(string :tag
"Converter Name")
2125 :value-type
(group (cons (string :tag
"Executable")
2126 (repeat (string :tag
"Command line args")))))))
2128 (defcustom org-export-odt-convert-process nil
2129 "Use this converter to convert from \"odt\" format to other formats.
2130 During customization, the list of converter names are populated
2131 from `org-export-odt-convert-processes'."
2132 :group
'org-export-odt
2133 :type
'(choice :convert-widget
2135 (apply 'widget-convert
(widget-type w
)
2136 (eval (car (widget-get w
:args
)))))
2137 `((const :tag
"None" nil
)
2138 ,@(mapcar (lambda (c)
2139 `(const :tag
,(car c
) ,(car c
)))
2140 org-export-odt-convert-processes
))))
2142 (defcustom org-export-odt-convert-capabilities
2144 ("odt" "ott" "doc" "rtf")
2145 (("pdf" "pdf") ("odt" "odt") ("xhtml" "html") ("rtf" "rtf")
2146 ("ott" "ott") ("doc" "doc") ("ooxml" "xml") ("html" "html")))
2148 ("html" "xhtml") (("pdf" "pdf") ("odt" "txt") ("html" "html")))
2150 ("ods" "ots" "xls" "csv")
2151 (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv")
2152 ("ods" "ods") ("xls" "xls") ("xhtml" "xhtml") ("ooxml" "xml")))
2155 (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("xhtml" "xml")
2156 ("otp" "otp") ("ppt" "ppt") ("odg" "odg") ("html" "html"))))
2157 "Specify input and output formats of `org-export-odt-convert-process'.
2158 More correctly, specify the set of input and output formats that
2159 the user is actually interested in.
2161 This variable is an alist where each element is of the
2162 form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
2163 INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
2164 alist where each element is of the form (OUTPUT-FMT
2165 OUTPUT-FILE-EXTENSION).
2167 The variable is interpreted as follows:
2168 `org-export-odt-convert-process' can take any document that is in
2169 INPUT-FMT-LIST and produce any document that is in the
2170 OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
2171 OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
2172 serves dual purposes:
2173 - It is used for populating completion candidates during
2174 `org-export-odt-convert' commands.
2175 - It is used as the value of \"%f\" specifier in
2176 `org-export-odt-convert-process'.
2178 DOCUMENT-CLASS is used to group a set of file formats in
2179 INPUT-FMT-LIST in to a single class.
2181 Note that this variable inherently captures how LibreOffice based
2182 converters work. LibreOffice maps documents of various formats
2183 to classes like Text, Web, Spreadsheet, Presentation etc and
2184 allow document of a given class (irrespective of it's source
2185 format) to be converted to any of the export formats associated
2188 See default setting of this variable for an typical
2190 :group
'org-export-odt
2193 (const :tag
"None" nil
)
2194 (alist :key-type
(string :tag
"Document Class")
2196 (group (repeat :tag
"Input formats" (string :tag
"Input format"))
2197 (alist :tag
"Output formats"
2198 :key-type
(string :tag
"Output format")
2200 (group (string :tag
"Output file extension")))))))
2202 (declare-function org-create-math-formula
"org"
2203 (latex-frag &optional mathml-file
))
2206 (defun org-export-odt-convert (&optional in-file out-fmt prefix-arg
)
2207 "Convert IN-FILE to format OUT-FMT using a command line converter.
2208 IN-FILE is the file to be converted. If unspecified, it defaults
2209 to variable `buffer-file-name'. OUT-FMT is the desired output
2210 format. Use `org-export-odt-convert-process' as the converter.
2211 If PREFIX-ARG is non-nil then the newly converted file is opened
2212 using `org-open-file'."
2214 (append (org-lparse-convert-read-params) current-prefix-arg
))
2215 (org-lparse-do-convert in-file out-fmt prefix-arg
))
2217 (defun org-odt-get (what &optional opt-plist
)
2220 (EXPORT-DIR (org-export-directory :html opt-plist
))
2221 (FILE-NAME-EXTENSION "odt")
2222 (EXPORT-BUFFER-NAME "*Org ODT Export*")
2223 (ENTITY-CONTROL org-odt-entity-control-callbacks-alist
)
2224 (ENTITY-FORMAT org-odt-entity-format-callbacks-alist
)
2225 (INIT-METHOD 'org-odt-init-outfile
)
2226 (FINAL-METHOD 'org-odt-finalize-outfile
)
2227 (SAVE-METHOD 'org-odt-save-as-outfile
)
2229 (and org-export-odt-convert-process
2230 (cadr (assoc-string org-export-odt-convert-process
2231 org-export-odt-convert-processes t
))))
2232 (CONVERT-CAPABILITIES
2233 (and org-export-odt-convert-process
2234 (cadr (assoc-string org-export-odt-convert-process
2235 org-export-odt-convert-processes t
))
2236 org-export-odt-convert-capabilities
))
2238 (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps
)
2239 (INLINE-IMAGES 'maybe
)
2240 (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
2241 (PLAIN-TEXT-MAP '(("&" .
"&") ("<" .
"<") (">" .
">")))
2242 (TABLE-FIRST-COLUMN-AS-LABELS nil
)
2243 (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY
"," 'superscript
))
2244 (CODING-SYSTEM-FOR-WRITE 'utf-8
)
2245 (CODING-SYSTEM-FOR-SAVE 'utf-8
)
2246 (t (error "Unknown property: %s" what
))))
2248 (defvar org-lparse-latex-fragment-fallback
) ; set by org-do-lparse
2249 (defun org-export-odt-do-preprocess-latex-fragments ()
2250 "Convert LaTeX fragments to images."
2251 (let* ((latex-frag-opt (plist-get org-lparse-opt-plist
:LaTeX-fragments
))
2252 (latex-frag-opt ; massage the options
2253 (or (and (member latex-frag-opt
'(mathjax t
))
2254 (not (and (fboundp 'org-format-latex-mathml-available-p
)
2255 (org-format-latex-mathml-available-p)))
2256 (prog1 org-lparse-latex-fragment-fallback
2259 "LaTeX to MathML converter not available. "
2260 (format "Using %S instead."
2261 org-lparse-latex-fragment-fallback
)))))
2263 cache-dir display-msg
)
2265 ((eq latex-frag-opt
'dvipng
)
2266 (setq cache-dir
"ltxpng/")
2267 (setq display-msg
"Creating LaTeX image %s"))
2268 ((member latex-frag-opt
'(mathjax t
))
2269 (setq latex-frag-opt
'mathml
)
2270 (setq cache-dir
"ltxmathml/")
2271 (setq display-msg
"Creating MathML formula %s")))
2272 (when (and org-current-export-file
)
2274 (concat cache-dir
(file-name-sans-extension
2275 (file-name-nondirectory org-current-export-file
)))
2276 org-current-export-dir nil display-msg
2277 nil nil latex-frag-opt
))))
2279 (defadvice org-format-latex-as-mathml
2280 (after org-odt-protect-latex-fragment activate
)
2281 "Encode LaTeX fragment as XML.
2282 Do this when translation to MathML fails."
2283 (when (or (not (> (length ad-return-value
) 0))
2284 (get-text-property 0 'org-protected ad-return-value
))
2285 (setq ad-return-value
2286 (org-propertize (org-odt-encode-plain-text (ad-get-arg 0))
2287 'org-protected t
))))
2289 (defun org-export-odt-preprocess-latex-fragments ()
2290 (when (equal org-export-current-backend
'odt
)
2291 (org-export-odt-do-preprocess-latex-fragments)))
2293 (defun org-export-odt-preprocess-label-references ()
2294 (goto-char (point-min))
2295 (let (label label-components category value pretty-label
)
2296 (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t
)
2297 (org-if-unprotected-at (match-beginning 1)
2299 (let ((org-lparse-encode-pending t
)
2300 (label (match-string 1)))
2301 ;; markup generated below is mostly an eye-candy. At
2302 ;; pre-processing stage, there is no information on which
2303 ;; entity a label reference points to. The actual markup
2304 ;; is generated as part of `org-odt-fixup-label-references'
2305 ;; which gets called at the fag end of export. By this
2306 ;; time we would have seen and collected all the label
2307 ;; definitions in `org-odt-entity-labels-alist'.
2308 (org-odt-format-tags
2309 '("<text:sequence-ref text:ref-name=\"%s\">" .
2310 "</text:sequence-ref>")
2311 "" (org-add-props label
'(org-protected t
)))) t t
)))))
2313 ;; process latex fragments as part of
2314 ;; `org-export-preprocess-after-blockquote-hook'. Note that this hook
2315 ;; is the one that is closest and well before the call to
2316 ;; `org-export-attach-captions-and-attributes' in
2317 ;; `org-export-preprocess-stirng'. The above arrangement permits
2318 ;; captions, labels and attributes to be attached to png images
2319 ;; generated out of latex equations.
2320 (add-hook 'org-export-preprocess-after-blockquote-hook
2321 'org-export-odt-preprocess-latex-fragments
)
2323 (defun org-export-odt-preprocess (parameters)
2324 (org-export-odt-preprocess-label-references))
2326 (declare-function archive-zip-extract
"arc-mode.el" (archive name
))
2327 (defun org-odt-zip-extract-one (archive member
&optional target
)
2329 (let* ((target (or target default-directory
))
2330 (archive (expand-file-name archive
))
2331 (archive-zip-extract
2332 (list "unzip" "-qq" "-o" "-d" target
))
2333 exit-code command-output
)
2334 (setq command-output
2336 (setq exit-code
(archive-zip-extract archive member
))
2338 (unless (zerop exit-code
)
2339 (message command-output
)
2340 (error "Extraction failed"))))
2342 (defun org-odt-zip-extract (archive members
&optional target
)
2343 (when (atom members
) (setq members
(list members
)))
2344 (mapc (lambda (member)
2345 (org-odt-zip-extract-one archive member target
))
2348 (defun org-odt-copy-styles-file (&optional styles-file
)
2349 ;; Non-availability of styles.xml is not a critical error. For now
2350 ;; throw an error purely for aesthetic reasons.
2351 (setq styles-file
(or styles-file
2352 org-export-odt-styles-file
2353 (expand-file-name "OrgOdtStyles.xml"
2355 (error "org-odt: Missing styles file?")))
2357 ((listp styles-file
)
2358 (let ((archive (nth 0 styles-file
))
2359 (members (nth 1 styles-file
)))
2360 (org-odt-zip-extract archive members
)
2363 (when (org-file-image-p member
)
2364 (let* ((image-type (file-name-extension member
))
2365 (media-type (format "image/%s" image-type
)))
2366 (org-odt-create-manifest-file-entry media-type member
))))
2368 ((and (stringp styles-file
) (file-exists-p styles-file
))
2369 (let ((styles-file-type (file-name-extension styles-file
)))
2371 ((string= styles-file-type
"xml")
2372 (copy-file styles-file
"styles.xml" t
))
2373 ((member styles-file-type
'("odt" "ott"))
2374 (org-odt-zip-extract styles-file
"styles.xml")))))
2376 (error (format "Invalid specification of styles.xml file: %S"
2377 org-export-odt-styles-file
))))
2379 ;; create a manifest entry for styles.xml
2380 (org-odt-create-manifest-file-entry "text/xml" "styles.xml"))
2382 (defvar org-export-odt-factory-settings
2383 "d4328fb9d1b6cb211d4320ff546829f26700dc5e"
2384 "SHA1 hash of OrgOdtStyles.xml.")
2386 (defun org-odt-configure-outline-numbering (level)
2387 "Outline numbering is retained only upto LEVEL.
2388 To disable outline numbering pass a LEVEL of 0."
2389 (goto-char (point-min))
2391 "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
2393 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
2394 (while (re-search-forward regex nil t
)
2395 (when (> (string-to-number (match-string 2)) level
)
2396 (replace-match replacement t nil
))))
2400 (defun org-export-as-odf (latex-frag &optional odf-file
)
2401 "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
2402 Use `org-create-math-formula' to convert LATEX-FRAG first to
2403 MathML. When invoked as an interactive command, use
2404 `org-latex-regexps' to infer LATEX-FRAG from currently active
2405 region. If no LaTeX fragments are found, prompt for it. Push
2406 MathML source to kill ring, if `org-export-copy-to-kill-ring' is
2410 (setq frag
(and (setq frag
(and (region-active-p)
2411 (buffer-substring (region-beginning)
2413 (loop for e in org-latex-regexps
2414 thereis
(when (string-match (nth 1 e
) frag
)
2415 (match-string (nth 2 e
) frag
)))))
2416 (read-string "LaTeX Fragment: " frag nil frag
))
2417 ,(let ((odf-filename (expand-file-name
2419 (file-name-sans-extension
2420 (or (file-name-nondirectory buffer-file-name
)))
2422 (file-name-directory buffer-file-name
))))
2423 (message "default val is %s" odf-filename
)
2424 (read-file-name "ODF filename: " nil odf-filename nil
2425 (file-name-nondirectory odf-filename
)))))
2426 (let* ((org-lparse-backend 'odf
)
2427 org-lparse-opt-plist
2428 (filename (or odf-file
2431 (file-name-sans-extension
2432 (or (file-name-nondirectory buffer-file-name
)))
2434 (file-name-directory buffer-file-name
))))
2435 (buffer (find-file-noselect (org-odt-init-outfile filename
)))
2436 (coding-system-for-write 'utf-8
)
2437 (save-buffer-coding-system 'utf-8
))
2439 (set-buffer-file-coding-system coding-system-for-write
)
2440 (let ((mathml (org-create-math-formula latex-frag
)))
2441 (unless mathml
(error "No Math formula created"))
2443 (or (org-export-push-to-kill-ring
2444 (upcase (symbol-name org-lparse-backend
)))
2445 (message "Exporting... done")))
2446 (org-odt-save-as-outfile filename nil
)))
2449 (defun org-export-as-odf-and-open ()
2450 "Export LaTeX fragment as OpenDocument formula and immediately open it.
2451 Use `org-export-as-odf' to read LaTeX fragment and OpenDocument
2454 (org-lparse-and-open
2455 nil nil nil
(call-interactively 'org-export-as-odf
)))
2459 ;;; org-odt.el ends here