Merge branch 'fix-orgstruct'
[org-mode.git] / lisp / org-odt.el
blobcec0e8c733a8730191027745f369f7729bd93e61
1 ;;; org-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 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/>.
24 ;;; Commentary:
26 ;;; Code:
27 (eval-when-compile
28 (require 'cl))
29 (require 'org-lparse)
31 (defgroup org-export-odt nil
32 "Options specific for ODT export of Org-mode files."
33 :tag "Org Export ODT"
34 :group 'org-export
35 :version "24.1")
37 (defvar org-lparse-dyn-first-heading-pos) ; let bound during org-do-lparse
38 (defun org-odt-insert-toc ()
39 (goto-char (point-min))
40 (cond
41 ((re-search-forward
42 "\\(<text:p [^>]*>\\)?\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*\\(</text:p>\\)?"
43 nil t)
44 (replace-match ""))
46 (goto-char org-lparse-dyn-first-heading-pos)))
47 (insert (org-odt-format-toc)))
49 (defun org-odt-end-export ()
50 (org-odt-insert-toc)
51 (org-odt-fixup-label-references)
53 ;; remove empty paragraphs
54 (goto-char (point-min))
55 (while (re-search-forward
56 "<text:p\\( text:style-name=\"Text_20_body\"\\)?>[ \r\n\t]*</text:p>"
57 nil t)
58 (replace-match ""))
59 (goto-char (point-min))
61 ;; Convert whitespace place holders
62 (goto-char (point-min))
63 (let (beg end n)
64 (while (setq beg (next-single-property-change (point) 'org-whitespace))
65 (setq n (get-text-property beg 'org-whitespace)
66 end (next-single-property-change beg 'org-whitespace))
67 (goto-char beg)
68 (delete-region beg end)
69 (insert (format "<span style=\"visibility:hidden;\">%s</span>"
70 (make-string n ?x)))))
72 ;; Remove empty lines at the beginning of the file.
73 (goto-char (point-min))
74 (when (looking-at "\\s-+\n") (replace-match ""))
76 ;; Remove display properties
77 (remove-text-properties (point-min) (point-max) '(display t)))
79 (defvar org-odt-suppress-xref nil)
80 (defconst org-export-odt-special-string-regexps
81 '(("\\\\-" . "&#x00ad;\\1") ; shy
82 ("---\\([^-]\\)" . "&#x2014;\\1") ; mdash
83 ("--\\([^-]\\)" . "&#x2013;\\1") ; ndash
84 ("\\.\\.\\." . "&#x2026;")) ; hellip
85 "Regular expressions for special string conversion.")
87 (defconst org-odt-lib-dir (file-name-directory load-file-name)
88 "Location of ODT exporter.
89 Use this to infer values of `org-odt-styles-dir' and
90 `org-export-odt-schema-dir'.")
92 (defvar org-odt-data-dir nil
93 "Data directory for ODT exporter.
94 Use this to infer values of `org-odt-styles-dir' and
95 `org-export-odt-schema-dir'.")
97 (defconst org-odt-schema-dir-list
98 (list
99 (and org-odt-data-dir
100 (expand-file-name "./schema/" org-odt-data-dir)) ; bail out
101 (eval-when-compile
102 (and (boundp 'org-odt-data-dir) org-odt-data-dir ; see make install
103 (expand-file-name "./schema/" org-odt-data-dir))))
104 "List of directories to search for OpenDocument schema files.
105 Use this list to set the default value of
106 `org-export-odt-schema-dir'. The entries in this list are
107 populated heuristically based on the values of `org-odt-lib-dir'
108 and `org-odt-data-dir'.")
110 (defcustom org-export-odt-schema-dir
111 (let* ((schema-dir
112 (catch 'schema-dir
113 (message "Debug (org-odt): Searching for OpenDocument schema files...")
114 (mapc
115 (lambda (schema-dir)
116 (when schema-dir
117 (message "Debug (org-odt): Trying %s..." schema-dir)
118 (when (and (file-readable-p
119 (expand-file-name "od-manifest-schema-v1.2-cs01.rnc"
120 schema-dir))
121 (file-readable-p
122 (expand-file-name "od-schema-v1.2-cs01.rnc"
123 schema-dir))
124 (file-readable-p
125 (expand-file-name "schemas.xml" schema-dir)))
126 (message "Debug (org-odt): Using schema files under %s"
127 schema-dir)
128 (throw 'schema-dir schema-dir))))
129 org-odt-schema-dir-list)
130 (message "Debug (org-odt): No OpenDocument schema files installed")
131 nil)))
132 schema-dir)
133 "Directory that contains OpenDocument schema files.
135 This directory contains:
136 1. rnc files for OpenDocument schema
137 2. a \"schemas.xml\" file that specifies locating rules needed
138 for auto validation of OpenDocument XML files.
140 Use the customize interface to set this variable. This ensures
141 that `rng-schema-locating-files' is updated and auto-validation
142 of OpenDocument XML takes place based on the value
143 `rng-nxml-auto-validate-flag'.
145 The default value of this variable varies depending on the
146 version of org in use and is initialized from
147 `org-odt-schema-dir-list'. The OASIS schema files are available
148 only in the org's private git repository. It is *not* bundled
149 with GNU ELPA tar or standard Emacs distribution."
150 :type '(choice
151 (const :tag "Not set" nil)
152 (directory :tag "Schema directory"))
153 :group 'org-export-odt
154 :version "24.1"
155 :set
156 (lambda (var value)
157 "Set `org-export-odt-schema-dir'.
158 Also add it to `rng-schema-locating-files'."
159 (let ((schema-dir value))
160 (set var
161 (if (and
162 (file-readable-p
163 (expand-file-name "od-manifest-schema-v1.2-cs01.rnc" schema-dir))
164 (file-readable-p
165 (expand-file-name "od-schema-v1.2-cs01.rnc" schema-dir))
166 (file-readable-p
167 (expand-file-name "schemas.xml" schema-dir)))
168 schema-dir
169 (when value
170 (message "Error (org-odt): %s has no OpenDocument schema files"
171 value))
172 nil)))
173 (when org-export-odt-schema-dir
174 (eval-after-load 'rng-loc
175 '(add-to-list 'rng-schema-locating-files
176 (expand-file-name "schemas.xml"
177 org-export-odt-schema-dir))))))
179 (defconst org-odt-styles-dir-list
180 (list
181 (and org-odt-data-dir
182 (expand-file-name "./styles/" org-odt-data-dir)) ; bail out
183 (eval-when-compile
184 (and (boundp 'org-odt-data-dir) org-odt-data-dir ; see make install
185 (expand-file-name "./styles/" org-odt-data-dir)))
186 (expand-file-name "../etc/styles/" org-odt-lib-dir) ; git
187 (expand-file-name "./etc/styles/" org-odt-lib-dir) ; elpa
188 (expand-file-name "./org/" data-directory) ; system
190 "List of directories to search for OpenDocument styles files.
191 See `org-odt-styles-dir'. The entries in this list are populated
192 heuristically based on the values of `org-odt-lib-dir' and
193 `org-odt-data-dir'.")
195 (defconst org-odt-styles-dir
196 (let* ((styles-dir
197 (catch 'styles-dir
198 (message "Debug (org-odt): Searching for OpenDocument styles files...")
199 (mapc (lambda (styles-dir)
200 (when styles-dir
201 (message "Debug (org-odt): Trying %s..." styles-dir)
202 (when (and (file-readable-p
203 (expand-file-name
204 "OrgOdtContentTemplate.xml" styles-dir))
205 (file-readable-p
206 (expand-file-name
207 "OrgOdtStyles.xml" styles-dir)))
208 (message "Debug (org-odt): Using styles under %s"
209 styles-dir)
210 (throw 'styles-dir styles-dir))))
211 org-odt-styles-dir-list)
212 nil)))
213 (unless styles-dir
214 (error "Error (org-odt): Cannot find factory styles files. Aborting."))
215 styles-dir)
216 "Directory that holds auxiliary XML files used by the ODT exporter.
218 This directory contains the following XML files -
219 \"OrgOdtStyles.xml\" and \"OrgOdtContentTemplate.xml\". These
220 XML files are used as the default values of
221 `org-export-odt-styles-file' and
222 `org-export-odt-content-template-file'.
224 The default value of this variable varies depending on the
225 version of org in use and is initialized from
226 `org-odt-styles-dir-list'. Note that the user could be using org
227 from one of: org's own private git repository, GNU ELPA tar or
228 standard Emacs.")
230 (defvar org-odt-file-extensions
231 '(("odt" . "OpenDocument Text")
232 ("ott" . "OpenDocument Text Template")
233 ("odm" . "OpenDocument Master Document")
234 ("ods" . "OpenDocument Spreadsheet")
235 ("ots" . "OpenDocument Spreadsheet Template")
236 ("odg" . "OpenDocument Drawing (Graphics)")
237 ("otg" . "OpenDocument Drawing Template")
238 ("odp" . "OpenDocument Presentation")
239 ("otp" . "OpenDocument Presentation Template")
240 ("odi" . "OpenDocument Image")
241 ("odf" . "OpenDocument Formula")
242 ("odc" . "OpenDocument Chart")))
244 (mapc
245 (lambda (desc)
246 ;; Let Org open all OpenDocument files using system-registered app
247 (add-to-list 'org-file-apps
248 (cons (concat "\\." (car desc) "\\'") 'system))
249 ;; Let Emacs open all OpenDocument files in archive mode
250 (add-to-list 'auto-mode-alist
251 (cons (concat "\\." (car desc) "\\'") 'archive-mode)))
252 org-odt-file-extensions)
254 ;; register the odt exporter with the pre-processor
255 (add-to-list 'org-export-backends 'odt)
257 ;; register the odt exporter with org-lparse library
258 (org-lparse-register-backend 'odt)
260 (defun org-odt-unload-function ()
261 (org-lparse-unregister-backend 'odt)
262 (remove-hook 'org-export-preprocess-after-blockquote-hook
263 'org-export-odt-preprocess-latex-fragments)
264 nil)
266 (defcustom org-export-odt-content-template-file nil
267 "Template file for \"content.xml\".
268 The exporter embeds the exported content just before
269 \"</office:text>\" element.
271 If unspecified, the file named \"OrgOdtContentTemplate.xml\"
272 under `org-odt-styles-dir' is used."
273 :type 'file
274 :group 'org-export-odt
275 :version "24.1")
277 (defcustom org-export-odt-styles-file nil
278 "Default styles file for use with ODT export.
279 Valid values are one of:
280 1. nil
281 2. path to a styles.xml file
282 3. path to a *.odt or a *.ott file
283 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
284 ...))
286 In case of option 1, an in-built styles.xml is used. See
287 `org-odt-styles-dir' for more information.
289 In case of option 3, the specified file is unzipped and the
290 styles.xml embedded therein is used.
292 In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
293 and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
294 generated odt file. Use relative path for specifying the
295 FILE-MEMBERS. styles.xml must be specified as one of the
296 FILE-MEMBERS.
298 Use options 1, 2 or 3 only if styles.xml alone suffices for
299 achieving the desired formatting. Use option 4, if the styles.xml
300 references additional files like header and footer images for
301 achieving the desired formatting.
303 Use \"#+ODT_STYLES_FILE: ...\" directive to set this variable on
304 a per-file basis. For example,
306 #+ODT_STYLES_FILE: \"/path/to/styles.xml\" or
307 #+ODT_STYLES_FILE: (\"/path/to/file.ott\" (\"styles.xml\" \"image/hdr.png\"))."
308 :group 'org-export-odt
309 :version "24.1"
310 :type
311 '(choice
312 (const :tag "Factory settings" nil)
313 (file :must-match t :tag "styles.xml")
314 (file :must-match t :tag "ODT or OTT file")
315 (list :tag "ODT or OTT file + Members"
316 (file :must-match t :tag "ODF Text or Text Template file")
317 (cons :tag "Members"
318 (file :tag " Member" "styles.xml")
319 (repeat (file :tag "Member"))))))
321 (eval-after-load 'org-exp
322 '(add-to-list 'org-export-inbuffer-options-extra
323 '("ODT_STYLES_FILE" :odt-styles-file)))
325 (defconst org-export-odt-tmpdir-prefix "%s-")
326 (defconst org-export-odt-bookmark-prefix "OrgXref.")
328 (defvar org-export-odt-embed-images t
329 "Should the images be copied in to the odt file or just linked?")
331 (defvar org-export-odt-inline-images 'maybe)
332 (defcustom org-export-odt-inline-image-extensions
333 '("png" "jpeg" "jpg" "gif")
334 "Extensions of image files that can be inlined into HTML."
335 :type '(repeat (string :tag "Extension"))
336 :group 'org-export-odt
337 :version "24.1")
339 (defcustom org-export-odt-pixels-per-inch display-pixels-per-inch
340 "Scaling factor for converting images pixels to inches.
341 Use this for sizing of embedded images. See Info node `(org)
342 Images in ODT export' for more information."
343 :type 'float
344 :group 'org-export-odt
345 :version "24.1")
347 (defcustom org-export-odt-create-custom-styles-for-srcblocks t
348 "Whether custom styles for colorized source blocks be automatically created.
349 When this option is turned on, the exporter creates custom styles
350 for source blocks based on the advice of `htmlfontify'. Creation
351 of custom styles happen as part of `org-odt-hfy-face-to-css'.
353 When this option is turned off exporter does not create such
354 styles.
356 Use the latter option if you do not want the custom styles to be
357 based on your current display settings. It is necessary that the
358 styles.xml already contains needed styles for colorizing to work.
360 This variable is effective only if
361 `org-export-odt-fontify-srcblocks' is turned on."
362 :group 'org-export-odt
363 :version "24.1"
364 :type 'boolean)
366 (defvar org-export-odt-default-org-styles-alist
367 '((paragraph . ((default . "Text_20_body")
368 (fixedwidth . "OrgFixedWidthBlock")
369 (verse . "OrgVerse")
370 (quote . "Quotations")
371 (blockquote . "Quotations")
372 (center . "OrgCenter")
373 (left . "OrgLeft")
374 (right . "OrgRight")
375 (title . "OrgTitle")
376 (subtitle . "OrgSubtitle")
377 (footnote . "Footnote")
378 (src . "OrgSrcBlock")
379 (illustration . "Illustration")
380 (table . "Table")
381 (definition-term . "Text_20_body_20_bold")
382 (horizontal-line . "Horizontal_20_Line")))
383 (character . ((bold . "Bold")
384 (emphasis . "Emphasis")
385 (code . "OrgCode")
386 (verbatim . "OrgCode")
387 (strike . "Strikethrough")
388 (underline . "Underline")
389 (subscript . "OrgSubscript")
390 (superscript . "OrgSuperscript")))
391 (list . ((ordered . "OrgNumberedList")
392 (unordered . "OrgBulletedList")
393 (description . "OrgDescriptionList"))))
394 "Default styles for various entities.")
396 (defvar org-export-odt-org-styles-alist org-export-odt-default-org-styles-alist)
397 (defun org-odt-get-style-name-for-entity (category &optional entity)
398 (let ((entity (or entity 'default)))
400 (cdr (assoc entity (cdr (assoc category
401 org-export-odt-org-styles-alist))))
402 (cdr (assoc entity (cdr (assoc category
403 org-export-odt-default-org-styles-alist))))
404 (error "Cannot determine style name for entity %s of type %s"
405 entity category))))
407 (defcustom org-export-odt-preferred-output-format nil
408 "Automatically post-process to this format after exporting to \"odt\".
409 Interactive commands `org-export-as-odt' and
410 `org-export-as-odt-and-open' export first to \"odt\" format and
411 then use `org-export-odt-convert-process' to convert the
412 resulting document to this format. During customization of this
413 variable, the list of valid values are populated based on
414 `org-export-odt-convert-capabilities'."
415 :group 'org-export-odt
416 :version "24.1"
417 :type '(choice :convert-widget
418 (lambda (w)
419 (apply 'widget-convert (widget-type w)
420 (eval (car (widget-get w :args)))))
421 `((const :tag "None" nil)
422 ,@(mapcar (lambda (c)
423 `(const :tag ,c ,c))
424 (org-lparse-reachable-formats "odt")))))
426 ;;;###autoload
427 (defun org-export-as-odt-and-open (arg)
428 "Export the outline as ODT and immediately open it with a browser.
429 If there is an active region, export only the region.
430 The prefix ARG specifies how many levels of the outline should become
431 headlines. The default is 3. Lower levels will become bulleted lists."
432 (interactive "P")
433 (org-lparse-and-open
434 (or org-export-odt-preferred-output-format "odt") "odt" arg))
436 ;;;###autoload
437 (defun org-export-as-odt-batch ()
438 "Call the function `org-lparse-batch'.
439 This function can be used in batch processing as:
440 emacs --batch
441 --load=$HOME/lib/emacs/org.el
442 --eval \"(setq org-export-headline-levels 2)\"
443 --visit=MyFile --funcall org-export-as-odt-batch"
444 (org-lparse-batch "odt"))
446 ;;; org-export-as-odt
447 ;;;###autoload
448 (defun org-export-as-odt (arg &optional hidden ext-plist
449 to-buffer body-only pub-dir)
450 "Export the outline as a OpenDocumentText file.
451 If there is an active region, export only the region. The prefix
452 ARG specifies how many levels of the outline should become
453 headlines. The default is 3. Lower levels will become bulleted
454 lists. HIDDEN is obsolete and does nothing.
455 EXT-PLIST is a property list with external parameters overriding
456 org-mode's default settings, but still inferior to file-local
457 settings. When TO-BUFFER is non-nil, create a buffer with that
458 name and export to that buffer. If TO-BUFFER is the symbol
459 `string', don't leave any buffer behind but just return the
460 resulting XML as a string. When BODY-ONLY is set, don't produce
461 the file header and footer, simply return the content of
462 <body>...</body>, without even the body tags themselves. When
463 PUB-DIR is set, use this as the publishing directory."
464 (interactive "P")
465 (org-lparse (or org-export-odt-preferred-output-format "odt")
466 "odt" arg hidden ext-plist to-buffer body-only pub-dir))
468 (defvar org-odt-entity-control-callbacks-alist
469 `((EXPORT
470 . (org-odt-begin-export org-odt-end-export))
471 (DOCUMENT-CONTENT
472 . (org-odt-begin-document-content org-odt-end-document-content))
473 (DOCUMENT-BODY
474 . (org-odt-begin-document-body org-odt-end-document-body))
475 (TOC
476 . (org-odt-begin-toc org-odt-end-toc))
477 (ENVIRONMENT
478 . (org-odt-begin-environment org-odt-end-environment))
479 (FOOTNOTE-DEFINITION
480 . (org-odt-begin-footnote-definition org-odt-end-footnote-definition))
481 (TABLE
482 . (org-odt-begin-table org-odt-end-table))
483 (TABLE-ROWGROUP
484 . (org-odt-begin-table-rowgroup org-odt-end-table-rowgroup))
485 (LIST
486 . (org-odt-begin-list org-odt-end-list))
487 (LIST-ITEM
488 . (org-odt-begin-list-item org-odt-end-list-item))
489 (OUTLINE
490 . (org-odt-begin-outline org-odt-end-outline))
491 (OUTLINE-TEXT
492 . (org-odt-begin-outline-text org-odt-end-outline-text))
493 (PARAGRAPH
494 . (org-odt-begin-paragraph org-odt-end-paragraph)))
497 (defvar org-odt-entity-format-callbacks-alist
498 `((EXTRA-TARGETS . org-lparse-format-extra-targets)
499 (ORG-TAGS . org-lparse-format-org-tags)
500 (SECTION-NUMBER . org-lparse-format-section-number)
501 (HEADLINE . org-odt-format-headline)
502 (TOC-ENTRY . org-odt-format-toc-entry)
503 (TOC-ITEM . org-odt-format-toc-item)
504 (TAGS . org-odt-format-tags)
505 (SPACES . org-odt-format-spaces)
506 (TABS . org-odt-format-tabs)
507 (LINE-BREAK . org-odt-format-line-break)
508 (FONTIFY . org-odt-format-fontify)
509 (TODO . org-lparse-format-todo)
510 (LINK . org-odt-format-link)
511 (INLINE-IMAGE . org-odt-format-inline-image)
512 (ORG-LINK . org-odt-format-org-link)
513 (HEADING . org-odt-format-heading)
514 (ANCHOR . org-odt-format-anchor)
515 (TABLE . org-lparse-format-table)
516 (TABLE-ROW . org-odt-format-table-row)
517 (TABLE-CELL . org-odt-format-table-cell)
518 (FOOTNOTES-SECTION . ignore)
519 (FOOTNOTE-REFERENCE . org-odt-format-footnote-reference)
520 (HORIZONTAL-LINE . org-odt-format-horizontal-line)
521 (COMMENT . org-odt-format-comment)
522 (LINE . org-odt-format-line)
523 (ORG-ENTITY . org-odt-format-org-entity))
526 ;;;_. callbacks
527 ;;;_. control callbacks
528 ;;;_ , document body
529 (defun org-odt-begin-office-body ()
530 ;; automatic styles
531 (insert-file-contents
532 (or org-export-odt-content-template-file
533 (expand-file-name "OrgOdtContentTemplate.xml"
534 org-odt-styles-dir)))
535 (goto-char (point-min))
536 (re-search-forward "</office:text>" nil nil)
537 (delete-region (match-beginning 0) (point-max)))
539 ;; Following variable is let bound when `org-do-lparse' is in
540 ;; progress. See org-html.el.
541 (defvar org-lparse-toc)
542 (defun org-odt-format-toc ()
543 (if (not org-lparse-toc) "" (concat "\n" org-lparse-toc "\n")))
545 (defun org-odt-format-preamble (opt-plist)
546 (let* ((title (plist-get opt-plist :title))
547 (author (plist-get opt-plist :author))
548 (date (plist-get opt-plist :date))
549 (iso-date (org-odt-format-date date))
550 (date (org-odt-format-date date "%d %b %Y"))
551 (email (plist-get opt-plist :email))
552 ;; switch on or off above vars based on user settings
553 (author (and (plist-get opt-plist :author-info) (or author email)))
554 (email (and (plist-get opt-plist :email-info) email))
555 (date (and (plist-get opt-plist :time-stamp-file) date)))
556 (concat
557 ;; title
558 (when title
559 (concat
560 (org-odt-format-stylized-paragraph
561 'title (org-odt-format-tags
562 '("<text:title>" . "</text:title>") title))
563 ;; separator
564 "<text:p text:style-name=\"OrgTitle\"/>"))
565 (cond
566 ((and author (not email))
567 ;; author only
568 (concat
569 (org-odt-format-stylized-paragraph
570 'subtitle
571 (org-odt-format-tags
572 '("<text:initial-creator>" . "</text:initial-creator>")
573 author))
574 ;; separator
575 "<text:p text:style-name=\"OrgSubtitle\"/>"))
576 ((and author email)
577 ;; author and email
578 (concat
579 (org-odt-format-stylized-paragraph
580 'subtitle
581 (org-odt-format-link
582 (org-odt-format-tags
583 '("<text:initial-creator>" . "</text:initial-creator>")
584 author) (concat "mailto:" email)))
585 ;; separator
586 "<text:p text:style-name=\"OrgSubtitle\"/>")))
587 ;; date
588 (when date
589 (concat
590 (org-odt-format-stylized-paragraph
591 'subtitle
592 (org-odt-format-tags
593 '("<text:date style:data-style-name=\"%s\" text:date-value=\"%s\">"
594 . "</text:date>") date "N75" iso-date))
595 ;; separator
596 "<text:p text:style-name=\"OrgSubtitle\"/>")))))
598 (defun org-odt-begin-document-body (opt-plist)
599 (org-odt-begin-office-body)
600 (insert (org-odt-format-preamble opt-plist))
601 (setq org-lparse-dyn-first-heading-pos (point)))
603 (defvar org-lparse-body-only) ; let bound during org-do-lparse
604 (defvar org-lparse-to-buffer) ; let bound during org-do-lparse
605 (defun org-odt-end-document-body (opt-plist)
606 (unless org-lparse-body-only
607 (org-lparse-insert-tag "</office:text>")
608 (org-lparse-insert-tag "</office:body>")))
610 (defun org-odt-begin-document-content (opt-plist)
611 (ignore))
613 (defun org-odt-end-document-content ()
614 (org-lparse-insert-tag "</office:document-content>"))
616 (defun org-odt-begin-outline (level1 snumber title tags
617 target extra-targets class)
618 (org-lparse-insert
619 'HEADING (org-lparse-format
620 'HEADLINE title extra-targets tags snumber level1)
621 level1 target))
623 (defun org-odt-end-outline ()
624 (ignore))
626 (defun org-odt-begin-outline-text (level1 snumber class)
627 (ignore))
629 (defun org-odt-end-outline-text ()
630 (ignore))
632 (defun org-odt-begin-section (style &optional name)
633 (let ((default-name (car (org-odt-add-automatic-style "Section"))))
634 (org-lparse-insert-tag
635 "<text:section text:style-name=\"%s\" text:name=\"%s\">"
636 style (or name default-name))))
638 (defun org-odt-end-section ()
639 (org-lparse-insert-tag "</text:section>"))
641 (defun org-odt-begin-paragraph (&optional style)
642 (org-lparse-insert-tag
643 "<text:p%s>" (org-odt-get-extra-attrs-for-paragraph-style style)))
645 (defun org-odt-end-paragraph ()
646 (org-lparse-insert-tag "</text:p>"))
648 (defun org-odt-get-extra-attrs-for-paragraph-style (style)
649 (let (style-name)
650 (setq style-name
651 (cond
652 ((stringp style) style)
653 ((symbolp style) (org-odt-get-style-name-for-entity
654 'paragraph style))))
655 (unless style-name
656 (error "Don't know how to handle paragraph style %s" style))
657 (format " text:style-name=\"%s\"" style-name)))
659 (defun org-odt-format-stylized-paragraph (style text)
660 (org-odt-format-tags
661 '("<text:p%s>" . "</text:p>") text
662 (org-odt-get-extra-attrs-for-paragraph-style style)))
664 (defvar org-lparse-opt-plist) ; bound during org-do-lparse
665 (defun org-odt-format-author (&optional author)
666 (when (setq author (or author (plist-get org-lparse-opt-plist :author)))
667 (org-odt-format-tags '("<dc:creator>" . "</dc:creator>") author)))
669 (defun org-odt-format-date (&optional org-ts fmt)
670 (save-match-data
671 (let* ((time
672 (and (stringp org-ts)
673 (string-match org-ts-regexp0 org-ts)
674 (apply 'encode-time
675 (org-fix-decoded-time
676 (org-parse-time-string (match-string 0 org-ts) t)))))
677 date)
678 (cond
679 (fmt (format-time-string fmt time))
680 (t (setq date (format-time-string "%Y-%m-%dT%H:%M:%S%z" time))
681 (format "%s:%s" (substring date 0 -2) (substring date -2)))))))
683 (defun org-odt-begin-annotation (&optional author date)
684 (org-lparse-insert-tag "<office:annotation>")
685 (when (setq author (org-odt-format-author author))
686 (insert author))
687 (insert (org-odt-format-tags
688 '("<dc:date>" . "</dc:date>")
689 (org-odt-format-date
690 (or date (plist-get org-lparse-opt-plist :date)))))
691 (org-lparse-begin-paragraph))
693 (defun org-odt-end-annotation ()
694 (org-lparse-insert-tag "</office:annotation>"))
696 (defun org-odt-begin-environment (style env-options-plist)
697 (case style
698 (annotation
699 (org-lparse-stash-save-paragraph-state)
700 (org-odt-begin-annotation (plist-get env-options-plist 'author)
701 (plist-get env-options-plist 'date)))
702 ((blockquote verse center quote)
703 (org-lparse-begin-paragraph style)
704 (list))
705 ((fixedwidth native)
706 (org-lparse-end-paragraph)
707 (list))
708 (t (error "Unknown environment %s" style))))
710 (defun org-odt-end-environment (style env-options-plist)
711 (case style
712 (annotation
713 (org-lparse-end-paragraph)
714 (org-odt-end-annotation)
715 (org-lparse-stash-pop-paragraph-state))
716 ((blockquote verse center quote)
717 (org-lparse-end-paragraph)
718 (list))
719 ((fixedwidth native)
720 (org-lparse-begin-paragraph)
721 (list))
722 (t (error "Unknown environment %s" style))))
724 (defvar org-lparse-list-stack) ; dynamically bound in org-do-lparse
725 (defvar org-odt-list-stack-stashed)
726 (defun org-odt-begin-list (ltype)
727 (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
728 ltype))
729 (let* ((style-name (org-odt-get-style-name-for-entity 'list ltype))
730 (extra (concat (if (or org-lparse-list-table-p
731 (and (= 1 (length org-lparse-list-stack))
732 (null org-odt-list-stack-stashed)))
733 " text:continue-numbering=\"false\""
734 " text:continue-numbering=\"true\"")
735 (when style-name
736 (format " text:style-name=\"%s\"" style-name)))))
737 (case ltype
738 ((ordered unordered description)
739 (org-lparse-end-paragraph)
740 (org-lparse-insert-tag "<text:list%s>" extra))
741 (t (error "Unknown list type: %s" ltype)))))
743 (defun org-odt-end-list (ltype)
744 (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
745 ltype))
746 (if ltype
747 (org-lparse-insert-tag "</text:list>")
748 (error "Unknown list type: %s" ltype)))
750 (defun org-odt-begin-list-item (ltype &optional arg headline)
751 (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
752 ltype))
753 (case ltype
754 (ordered
755 (assert (not headline) t)
756 (let* ((counter arg) (extra ""))
757 (org-lparse-insert-tag (if (= (length org-lparse-list-stack)
758 (length org-odt-list-stack-stashed))
759 "<text:list-header>" "<text:list-item>"))
760 (org-lparse-begin-paragraph)))
761 (unordered
762 (let* ((id arg) (extra ""))
763 (org-lparse-insert-tag (if (= (length org-lparse-list-stack)
764 (length org-odt-list-stack-stashed))
765 "<text:list-header>" "<text:list-item>"))
766 (org-lparse-begin-paragraph)
767 (insert (if headline (org-odt-format-target headline id)
768 (org-odt-format-bookmark "" id)))))
769 (description
770 (assert (not headline) t)
771 (let ((term (or arg "(no term)")))
772 (insert
773 (org-odt-format-tags
774 '("<text:list-item>" . "</text:list-item>")
775 (org-odt-format-stylized-paragraph 'definition-term term)))
776 (org-lparse-begin-list-item 'unordered)
777 (org-lparse-begin-list 'description)
778 (org-lparse-begin-list-item 'unordered)))
779 (t (error "Unknown list type"))))
781 (defun org-odt-end-list-item (ltype)
782 (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
783 ltype))
784 (case ltype
785 ((ordered unordered)
786 (org-lparse-insert-tag (if (= (length org-lparse-list-stack)
787 (length org-odt-list-stack-stashed))
788 (prog1 "</text:list-header>"
789 (setq org-odt-list-stack-stashed nil))
790 "</text:list-item>")))
791 (description
792 (org-lparse-end-list-item-1)
793 (org-lparse-end-list 'description)
794 (org-lparse-end-list-item-1))
795 (t (error "Unknown list type"))))
797 (defun org-odt-discontinue-list ()
798 (let ((stashed-stack org-lparse-list-stack))
799 (loop for list-type in stashed-stack
800 do (org-lparse-end-list-item-1 list-type)
801 (org-lparse-end-list list-type))
802 (setq org-odt-list-stack-stashed stashed-stack)))
804 (defun org-odt-continue-list ()
805 (setq org-odt-list-stack-stashed (nreverse org-odt-list-stack-stashed))
806 (loop for list-type in org-odt-list-stack-stashed
807 do (org-lparse-begin-list list-type)
808 (org-lparse-begin-list-item list-type)))
810 ;; Following variables are let bound when table emission is in
811 ;; progress. See org-lparse.el.
812 (defvar org-lparse-table-begin-marker)
813 (defvar org-lparse-table-ncols)
814 (defvar org-lparse-table-rowgrp-open)
815 (defvar org-lparse-table-rownum)
816 (defvar org-lparse-table-cur-rowgrp-is-hdr)
817 (defvar org-lparse-table-is-styled)
818 (defvar org-lparse-table-rowgrp-info)
819 (defvar org-lparse-table-colalign-vector)
821 (defvar org-odt-table-style nil
822 "Table style specified by \"#+ATTR_ODT: <style-name>\" line.
823 This is set during `org-odt-begin-table'.")
825 (defvar org-odt-table-style-spec nil
826 "Entry for `org-odt-table-style' in `org-export-odt-table-styles'.")
828 (defcustom org-export-odt-table-styles
829 '(("OrgEquation" "OrgEquation"
830 ((use-first-column-styles . t)
831 (use-last-column-styles . t))))
832 "Specify how Table Styles should be derived from a Table Template.
833 This is a list where each element is of the
834 form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
836 TABLE-STYLE-NAME is the style associated with the table through
837 `org-odt-table-style'.
839 TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
840 TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
841 below) that is included in
842 `org-export-odt-content-template-file'.
844 TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
845 \"TableCell\"
846 PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
847 \"TableParagraph\"
848 TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
849 \"FirstRow\" | \"LastRow\" |
850 \"EvenRow\" | \"OddRow\" |
851 \"EvenColumn\" | \"OddColumn\" | \"\"
852 where \"+\" above denotes string concatenation.
854 TABLE-CELL-OPTIONS is an alist where each element is of the
855 form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
856 TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
857 `use-last-row-styles' |
858 `use-first-column-styles' |
859 `use-last-column-styles' |
860 `use-banding-rows-styles' |
861 `use-banding-columns-styles' |
862 `use-first-row-styles'
863 ON-OR-OFF := `t' | `nil'
865 For example, with the following configuration
867 \(setq org-export-odt-table-styles
868 '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
869 \(\(use-first-row-styles . t\)
870 \(use-first-column-styles . t\)\)\)
871 \(\"TableWithHeaderColumns\" \"Custom\"
872 \(\(use-first-column-styles . t\)\)\)\)\)
874 1. A table associated with \"TableWithHeaderRowsAndColumns\"
875 style will use the following table-cell styles -
876 \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
877 \"CustomTableCell\" and the following paragraph styles
878 \"CustomFirstRowTableParagraph\",
879 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
880 as appropriate.
882 2. A table associated with \"TableWithHeaderColumns\" style will
883 use the following table-cell styles -
884 \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
885 following paragraph styles
886 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
887 as appropriate..
889 Note that TABLE-TEMPLATE-NAME corresponds to the
890 \"<table:table-template>\" elements contained within
891 \"<office:styles>\". The entries (TABLE-STYLE-NAME
892 TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
893 \"table:template-name\" and \"table:use-first-row-styles\" etc
894 attributes of \"<table:table>\" element. Refer ODF-1.2
895 specification for more information. Also consult the
896 implementation filed under `org-odt-get-table-cell-styles'.
898 The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
899 formatting of numbered display equations. Do not delete this
900 style from the list."
901 :group 'org-export-odt
902 :version "24.1"
903 :type '(choice
904 (const :tag "None" nil)
905 (repeat :tag "Table Styles"
906 (list :tag "Table Style Specification"
907 (string :tag "Table Style Name")
908 (string :tag "Table Template Name")
909 (alist :options (use-first-row-styles
910 use-last-row-styles
911 use-first-column-styles
912 use-last-column-styles
913 use-banding-rows-styles
914 use-banding-columns-styles)
915 :key-type symbol
916 :value-type (const :tag "True" t))))))
918 (defvar org-odt-table-style-format
920 <style:style style:name=\"%s\" style:family=\"table\">
921 <style:table-properties style:rel-width=\"%d%%\" fo:margin-top=\"0cm\" fo:margin-bottom=\"0.20cm\" table:align=\"center\"/>
922 </style:style>
924 "Template for auto-generated Table styles.")
926 (defvar org-odt-automatic-styles '()
927 "Registry of automatic styles for various OBJECT-TYPEs.
928 The variable has the following form:
929 \(\(OBJECT-TYPE-A
930 \(\(OBJECT-NAME-A.1 OBJECT-PROPS-A.1\)
931 \(OBJECT-NAME-A.2 OBJECT-PROPS-A.2\) ...\)\)
932 \(OBJECT-TYPE-B
933 \(\(OBJECT-NAME-B.1 OBJECT-PROPS-B.1\)
934 \(OBJECT-NAME-B.2 OBJECT-PROPS-B.2\) ...\)\)
935 ...\).
937 OBJECT-TYPEs could be \"Section\", \"Table\", \"Figure\" etc.
938 OBJECT-PROPS is (typically) a plist created by passing
939 \"#+ATTR_ODT: \" option to `org-lparse-get-block-params'.
941 Use `org-odt-add-automatic-style' to add update this variable.'")
943 (defvar org-odt-object-counters nil
944 "Running counters for various OBJECT-TYPEs.
945 Use this to generate automatic names and style-names. See
946 `org-odt-add-automatic-style'.")
948 (defun org-odt-write-automatic-styles ()
949 "Write automatic styles to \"content.xml\"."
950 (with-current-buffer
951 (find-file-noselect (expand-file-name "content.xml") t)
952 ;; position the cursor
953 (goto-char (point-min))
954 (re-search-forward " </office:automatic-styles>" nil t)
955 (goto-char (match-beginning 0))
956 ;; write automatic table styles
957 (loop for (style-name props) in
958 (plist-get org-odt-automatic-styles 'Table) do
959 (when (setq props (or (plist-get props :rel-width) 96))
960 (insert (format org-odt-table-style-format style-name props))))))
962 (defun org-odt-add-automatic-style (object-type &optional object-props)
963 "Create an automatic style of type OBJECT-TYPE with param OBJECT-PROPS.
964 OBJECT-PROPS is (typically) a plist created by passing
965 \"#+ATTR_ODT: \" option of the object in question to
966 `org-lparse-get-block-params'.
968 Use `org-odt-object-counters' to generate an automatic
969 OBJECT-NAME and STYLE-NAME. If OBJECT-PROPS is non-nil, add a
970 new entry in `org-odt-automatic-styles'. Return (OBJECT-NAME
971 . STYLE-NAME)."
972 (assert (stringp object-type))
973 (let* ((object (intern object-type))
974 (seqvar object)
975 (seqno (1+ (or (plist-get org-odt-object-counters seqvar) 0)))
976 (object-name (format "%s%d" object-type seqno)) style-name)
977 (setq org-odt-object-counters
978 (plist-put org-odt-object-counters seqvar seqno))
979 (when object-props
980 (setq style-name (format "Org%s" object-name))
981 (setq org-odt-automatic-styles
982 (plist-put org-odt-automatic-styles object
983 (append (list (list style-name object-props))
984 (plist-get org-odt-automatic-styles object)))))
985 (cons object-name style-name)))
987 (defvar org-odt-table-indentedp nil)
988 (defun org-odt-begin-table (caption label attributes short-caption)
989 (setq org-odt-table-indentedp (not (null org-lparse-list-stack)))
990 (when org-odt-table-indentedp
991 ;; Within the Org file, the table is appearing within a list item.
992 ;; OpenDocument doesn't allow table to appear within list items.
993 ;; Temporarily terminate the list, emit the table and then
994 ;; re-continue the list.
995 (org-odt-discontinue-list)
996 ;; Put the Table in an indented section.
997 (let ((level (length org-odt-list-stack-stashed)))
998 (org-odt-begin-section (format "OrgIndentedSection-Level-%d" level))))
999 (setq attributes (org-lparse-get-block-params attributes))
1000 (setq org-odt-table-style (plist-get attributes :style))
1001 (setq org-odt-table-style-spec
1002 (assoc org-odt-table-style org-export-odt-table-styles))
1003 (when (or label caption)
1004 (insert
1005 (org-odt-format-stylized-paragraph
1006 'table (org-odt-format-entity-caption label caption "__Table__"))))
1007 (let ((automatic-name (org-odt-add-automatic-style "Table" attributes)))
1008 (org-lparse-insert-tag
1009 "<table:table table:name=\"%s\" table:style-name=\"%s\">"
1010 (or short-caption (car automatic-name))
1011 (or (nth 1 org-odt-table-style-spec)
1012 (cdr automatic-name) "OrgTable")))
1013 (setq org-lparse-table-begin-marker (point)))
1015 (defvar org-lparse-table-colalign-info)
1016 (defun org-odt-end-table ()
1017 (goto-char org-lparse-table-begin-marker)
1018 (loop for level from 0 below org-lparse-table-ncols
1019 do (let* ((col-cookie (and org-lparse-table-is-styled
1020 (cdr (assoc (1+ level)
1021 org-lparse-table-colalign-info))))
1022 (extra-columns (or (nth 1 col-cookie) 0)))
1023 (dotimes (i (1+ extra-columns))
1024 (insert
1025 (org-odt-format-tags
1026 "<table:table-column table:style-name=\"%sColumn\"/>"
1027 "" (or (nth 1 org-odt-table-style-spec) "OrgTable"))))
1028 (insert "\n")))
1029 ;; fill style attributes for table cells
1030 (when org-lparse-table-is-styled
1031 (while (re-search-forward "@@\\(table-cell:p\\|table-cell:style-name\\)@@\\([0-9]+\\)@@\\([0-9]+\\)@@" nil t)
1032 (let* ((spec (match-string 1))
1033 (r (string-to-number (match-string 2)))
1034 (c (string-to-number (match-string 3)))
1035 (cell-styles (org-odt-get-table-cell-styles
1036 r c org-odt-table-style-spec))
1037 (table-cell-style (car cell-styles))
1038 (table-cell-paragraph-style (cdr cell-styles)))
1039 (cond
1040 ((equal spec "table-cell:p")
1041 (replace-match table-cell-paragraph-style t t))
1042 ((equal spec "table-cell:style-name")
1043 (replace-match table-cell-style t t))))))
1044 (goto-char (point-max))
1045 (org-lparse-insert-tag "</table:table>")
1046 (when org-odt-table-indentedp
1047 (org-odt-end-section)
1048 (org-odt-continue-list)))
1050 (defun org-odt-begin-table-rowgroup (&optional is-header-row)
1051 (when org-lparse-table-rowgrp-open
1052 (org-lparse-end 'TABLE-ROWGROUP))
1053 (org-lparse-insert-tag (if is-header-row
1054 "<table:table-header-rows>"
1055 "<table:table-rows>"))
1056 (setq org-lparse-table-rowgrp-open t)
1057 (setq org-lparse-table-cur-rowgrp-is-hdr is-header-row))
1059 (defun org-odt-end-table-rowgroup ()
1060 (when org-lparse-table-rowgrp-open
1061 (setq org-lparse-table-rowgrp-open nil)
1062 (org-lparse-insert-tag
1063 (if org-lparse-table-cur-rowgrp-is-hdr
1064 "</table:table-header-rows>" "</table:table-rows>"))))
1066 (defun org-odt-format-table-row (row)
1067 (org-odt-format-tags
1068 '("<table:table-row>" . "</table:table-row>") row))
1070 (defun org-odt-get-table-cell-styles (r c &optional style-spec)
1071 "Retrieve styles applicable to a table cell.
1072 R and C are (zero-based) row and column numbers of the table
1073 cell. STYLE-SPEC is an entry in `org-export-odt-table-styles'
1074 applicable to the current table. It is `nil' if the table is not
1075 associated with any style attributes.
1077 Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
1079 When STYLE-SPEC is nil, style the table cell the conventional way
1080 - choose cell borders based on row and column groupings and
1081 choose paragraph alignment based on `org-col-cookies' text
1082 property. See also
1083 `org-odt-get-paragraph-style-cookie-for-table-cell'.
1085 When STYLE-SPEC is non-nil, ignore the above cookie and return
1086 styles congruent with the ODF-1.2 specification."
1087 (cond
1088 (style-spec
1090 ;; LibreOffice - particularly the Writer - honors neither table
1091 ;; templates nor custom table-cell styles. Inorder to retain
1092 ;; inter-operability with LibreOffice, only automatic styles are
1093 ;; used for styling of table-cells. The current implementation is
1094 ;; congruent with ODF-1.2 specification and hence is
1095 ;; future-compatible.
1097 ;; Additional Note: LibreOffice's AutoFormat facility for tables -
1098 ;; which recognizes as many as 16 different cell types - is much
1099 ;; richer. Unfortunately it is NOT amenable to easy configuration
1100 ;; by hand.
1102 (let* ((template-name (nth 1 style-spec))
1103 (cell-style-selectors (nth 2 style-spec))
1104 (cell-type
1105 (cond
1106 ((and (cdr (assoc 'use-first-column-styles cell-style-selectors))
1107 (= c 0)) "FirstColumn")
1108 ((and (cdr (assoc 'use-last-column-styles cell-style-selectors))
1109 (= c (1- org-lparse-table-ncols))) "LastColumn")
1110 ((and (cdr (assoc 'use-first-row-styles cell-style-selectors))
1111 (= r 0)) "FirstRow")
1112 ((and (cdr (assoc 'use-last-row-styles cell-style-selectors))
1113 (= r org-lparse-table-rownum))
1114 "LastRow")
1115 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
1116 (= (% r 2) 1)) "EvenRow")
1117 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
1118 (= (% r 2) 0)) "OddRow")
1119 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
1120 (= (% c 2) 1)) "EvenColumn")
1121 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
1122 (= (% c 2) 0)) "OddColumn")
1123 (t ""))))
1124 (cons
1125 (concat template-name cell-type "TableCell")
1126 (concat template-name cell-type "TableParagraph"))))
1128 (cons
1129 (concat
1130 "OrgTblCell"
1131 (cond
1132 ((= r 0) "T")
1133 ((eq (cdr (assoc r org-lparse-table-rowgrp-info)) :start) "T")
1134 (t ""))
1135 (when (= r org-lparse-table-rownum) "B")
1136 (cond
1137 ((= c 0) "")
1138 ((or (memq (nth c org-table-colgroup-info) '(:start :startend))
1139 (memq (nth (1- c) org-table-colgroup-info) '(:end :startend))) "L")
1140 (t "")))
1141 (capitalize (aref org-lparse-table-colalign-vector c))))))
1143 (defun org-odt-get-paragraph-style-cookie-for-table-cell (r c)
1144 (concat
1145 (and (not org-odt-table-style-spec)
1146 (cond
1147 (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
1148 ((and (= c 0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS))
1149 "OrgTableHeading")
1150 (t "OrgTableContents")))
1151 (and org-lparse-table-is-styled
1152 (format "@@table-cell:p@@%03d@@%03d@@" r c))))
1154 (defun org-odt-get-style-name-cookie-for-table-cell (r c)
1155 (when org-lparse-table-is-styled
1156 (format "@@table-cell:style-name@@%03d@@%03d@@" r c)))
1158 (defun org-odt-format-table-cell (data r c horiz-span)
1159 (concat
1160 (let* ((paragraph-style-cookie
1161 (org-odt-get-paragraph-style-cookie-for-table-cell r c))
1162 (style-name-cookie
1163 (org-odt-get-style-name-cookie-for-table-cell r c))
1164 (extra (and style-name-cookie
1165 (format " table:style-name=\"%s\"" style-name-cookie)))
1166 (extra (concat extra
1167 (and (> horiz-span 0)
1168 (format " table:number-columns-spanned=\"%d\""
1169 (1+ horiz-span))))))
1170 (org-odt-format-tags
1171 '("<table:table-cell%s>" . "</table:table-cell>")
1172 (if org-lparse-list-table-p data
1173 (org-odt-format-stylized-paragraph paragraph-style-cookie data)) extra))
1174 (let (s)
1175 (dotimes (i horiz-span)
1176 (setq s (concat s "\n<table:covered-table-cell/>"))) s)
1177 "\n"))
1179 (defun org-odt-begin-footnote-definition (n)
1180 (org-lparse-begin-paragraph 'footnote))
1182 (defun org-odt-end-footnote-definition (n)
1183 (org-lparse-end-paragraph))
1185 (defun org-odt-begin-toc (lang-specific-heading max-level)
1186 ;; Strings in `org-export-language-setup' can contain named html
1187 ;; entities. Replace those with utf-8 equivalents.
1188 (let ((i 0) entity rpl)
1189 (while (string-match "&\\([^#].*?\\);" lang-specific-heading i)
1190 (setq entity (match-string 1 lang-specific-heading))
1191 (if (not (setq rpl (org-entity-get-representation entity 'utf8)))
1192 (setq i (match-end 0))
1193 (setq i (+ (match-beginning 0) (length rpl)))
1194 (setq lang-specific-heading
1195 (replace-match rpl t t lang-specific-heading)))))
1196 (insert
1197 (format "
1198 <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
1199 <text:table-of-content-source text:outline-level=\"%d\">
1200 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
1201 " max-level lang-specific-heading))
1202 (loop for level from 1 upto 10
1203 do (insert (format
1205 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
1206 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
1207 <text:index-entry-chapter/>
1208 <text:index-entry-text/>
1209 <text:index-entry-link-end/>
1210 </text:table-of-content-entry-template>
1211 " level level)))
1213 (insert
1214 (format "
1215 </text:table-of-content-source>
1217 <text:index-body>
1218 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
1219 <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
1220 </text:index-title>
1221 " lang-specific-heading)))
1223 (defun org-odt-end-toc ()
1224 (insert "
1225 </text:index-body>
1226 </text:table-of-content>
1229 (defun org-odt-format-toc-entry (snumber todo headline tags href)
1230 (setq headline (concat
1231 (and org-export-with-section-numbers
1232 (concat snumber ". "))
1233 headline
1234 (and tags
1235 (concat
1236 (org-lparse-format 'SPACES 3)
1237 (org-lparse-format 'FONTIFY tags "tag")))))
1238 (when todo
1239 (setq headline (org-lparse-format 'FONTIFY headline "todo")))
1241 (let ((org-odt-suppress-xref t))
1242 (org-odt-format-link headline (concat "#" href))))
1244 (defun org-odt-format-toc-item (toc-entry level org-last-level)
1245 (let ((style (format "Contents_20_%d"
1246 (+ level (or (org-lparse-get 'TOPLEVEL-HLEVEL) 1) -1))))
1247 (insert "\n" (org-odt-format-stylized-paragraph style toc-entry) "\n")))
1249 ;; Following variable is let bound during 'ORG-LINK callback. See
1250 ;; org-html.el
1251 (defvar org-lparse-link-description-is-image nil)
1252 (defun org-odt-format-link (desc href &optional attr)
1253 (cond
1254 ((and (= (string-to-char href) ?#) (not org-odt-suppress-xref))
1255 (setq href (substring href 1))
1256 (let ((xref-format "text"))
1257 (when (numberp desc)
1258 (setq desc (format "%d" desc) xref-format "number"))
1259 (when (listp desc)
1260 (setq desc (mapconcat 'identity desc ".") xref-format "chapter"))
1261 (setq href (concat org-export-odt-bookmark-prefix href))
1262 (org-odt-format-tags
1263 '("<text:bookmark-ref text:reference-format=\"%s\" text:ref-name=\"%s\">" .
1264 "</text:bookmark-ref>")
1265 desc xref-format href)))
1266 (org-lparse-link-description-is-image
1267 (org-odt-format-tags
1268 '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</draw:a>")
1269 desc href (or attr "")))
1271 (org-odt-format-tags
1272 '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</text:a>")
1273 desc href (or attr "")))))
1275 (defun org-odt-format-spaces (n)
1276 (cond
1277 ((= n 1) " ")
1278 ((> n 1) (concat
1279 " " (org-odt-format-tags "<text:s text:c=\"%d\"/>" "" (1- n))))
1280 (t "")))
1282 (defun org-odt-format-tabs (&optional n)
1283 (let ((tab "<text:tab/>")
1284 (n (or n 1)))
1285 (insert tab)))
1287 (defun org-odt-format-line-break ()
1288 (org-odt-format-tags "<text:line-break/>" ""))
1290 (defun org-odt-format-horizontal-line ()
1291 (org-odt-format-stylized-paragraph 'horizontal-line ""))
1293 (defun org-odt-encode-plain-text (line &optional no-whitespace-filling)
1294 (setq line (org-xml-encode-plain-text line))
1295 (if no-whitespace-filling line
1296 (org-odt-fill-tabs-and-spaces line)))
1298 (defun org-odt-format-line (line)
1299 (case org-lparse-dyn-current-environment
1300 (fixedwidth (concat
1301 (org-odt-format-stylized-paragraph
1302 'fixedwidth (org-odt-encode-plain-text line)) "\n"))
1303 (t (concat line "\n"))))
1305 (defun org-odt-format-comment (fmt &rest args)
1306 (let ((comment (apply 'format fmt args)))
1307 (format "\n<!-- %s -->\n" comment)))
1309 (defun org-odt-format-org-entity (wd)
1310 (org-entity-get-representation wd 'utf8))
1312 (defun org-odt-fill-tabs-and-spaces (line)
1313 (replace-regexp-in-string
1314 "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
1315 (cond
1316 ((string= s "\t") (org-odt-format-tabs))
1317 (t (org-odt-format-spaces (length s))))) line))
1319 (defcustom org-export-odt-fontify-srcblocks t
1320 "Specify whether or not source blocks need to be fontified.
1321 Turn this option on if you want to colorize the source code
1322 blocks in the exported file. For colorization to work, you need
1323 to make available an enhanced version of `htmlfontify' library."
1324 :type 'boolean
1325 :group 'org-export-odt
1326 :version "24.1")
1328 (defun org-odt-format-source-line-with-line-number-and-label
1329 (line rpllbl num fontifier par-style)
1331 (let ((keep-label (not (numberp rpllbl)))
1332 (ref (org-find-text-property-in-string 'org-coderef line)))
1333 (setq line (concat line (and keep-label ref (format "(%s)" ref))))
1334 (setq line (funcall fontifier line))
1335 (when ref
1336 (setq line (org-odt-format-target line (concat "coderef-" ref))))
1337 (setq line (org-odt-format-stylized-paragraph par-style line))
1338 (if (not num) line
1339 (org-odt-format-tags '("<text:list-item>" . "</text:list-item>") line))))
1341 (defun org-odt-format-source-code-or-example-plain
1342 (lines lang caption textareap cols rows num cont rpllbl fmt)
1343 "Format source or example blocks much like fixedwidth blocks.
1344 Use this when `org-export-odt-fontify-srcblocks' option is turned
1345 off."
1346 (let* ((lines (org-split-string lines "[\r\n]"))
1347 (line-count (length lines))
1348 (i 0))
1349 (mapconcat
1350 (lambda (line)
1351 (incf i)
1352 (org-odt-format-source-line-with-line-number-and-label
1353 line rpllbl num 'org-odt-encode-plain-text
1354 (if (= i line-count) "OrgFixedWidthBlockLastLine"
1355 "OrgFixedWidthBlock")))
1356 lines "\n")))
1358 (defvar org-src-block-paragraph-format
1359 "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
1360 <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
1361 <style:background-image/>
1362 </style:paragraph-properties>
1363 <style:text-properties fo:color=\"%s\"/>
1364 </style:style>"
1365 "Custom paragraph style for colorized source and example blocks.
1366 This style is much the same as that of \"OrgFixedWidthBlock\"
1367 except that the foreground and background colors are set
1368 according to the default face identified by the `htmlfontify'.")
1370 (defvar hfy-optimisations)
1371 (declare-function hfy-face-to-style "htmlfontify" (fn))
1372 (declare-function hfy-face-or-def-to-name "htmlfontify" (fn))
1374 (defun org-odt-hfy-face-to-css (fn)
1375 "Create custom style for face FN.
1376 When FN is the default face, use it's foreground and background
1377 properties to create \"OrgSrcBlock\" paragraph style. Otherwise
1378 use it's color attribute to create a character style whose name
1379 is obtained from FN. Currently all attributes of FN other than
1380 color are ignored.
1382 The style name for a face FN is derived using the following
1383 operations on the face name in that order - de-dash, CamelCase
1384 and prefix with \"OrgSrc\". For example,
1385 `font-lock-function-name-face' is associated with
1386 \"OrgSrcFontLockFunctionNameFace\"."
1387 (let* ((css-list (hfy-face-to-style fn))
1388 (style-name ((lambda (fn)
1389 (concat "OrgSrc"
1390 (mapconcat
1391 'capitalize (split-string
1392 (hfy-face-or-def-to-name fn) "-")
1393 ""))) fn))
1394 (color-val (cdr (assoc "color" css-list)))
1395 (background-color-val (cdr (assoc "background" css-list)))
1396 (style (and org-export-odt-create-custom-styles-for-srcblocks
1397 (cond
1398 ((eq fn 'default)
1399 (format org-src-block-paragraph-format
1400 background-color-val color-val))
1402 (format
1404 <style:style style:name=\"%s\" style:family=\"text\">
1405 <style:text-properties fo:color=\"%s\"/>
1406 </style:style>" style-name color-val))))))
1407 (cons style-name style)))
1409 (defun org-odt-insert-custom-styles-for-srcblocks (styles)
1410 "Save STYLES used for colorizing of source blocks.
1411 Update styles.xml with styles that were collected as part of
1412 `org-odt-hfy-face-to-css' callbacks."
1413 (when styles
1414 (with-current-buffer
1415 (find-file-noselect (expand-file-name "styles.xml") t)
1416 (goto-char (point-min))
1417 (when (re-search-forward "</office:styles>" nil t)
1418 (goto-char (match-beginning 0))
1419 (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n")))))
1421 (defun org-odt-format-source-code-or-example-colored
1422 (lines lang caption textareap cols rows num cont rpllbl fmt)
1423 "Format source or example blocks using `htmlfontify-string'.
1424 Use this routine when `org-export-odt-fontify-srcblocks' option
1425 is turned on."
1426 (let* ((lang-m (and lang (or (cdr (assoc lang org-src-lang-modes)) lang)))
1427 (mode (and lang-m (intern (concat (if (symbolp lang-m)
1428 (symbol-name lang-m)
1429 lang-m) "-mode"))))
1430 (org-inhibit-startup t)
1431 (org-startup-folded nil)
1432 (lines (with-temp-buffer
1433 (insert lines)
1434 (if (functionp mode) (funcall mode) (fundamental-mode))
1435 (font-lock-fontify-buffer)
1436 (buffer-string)))
1437 (hfy-html-quote-regex "\\([<\"&> ]\\)")
1438 (hfy-html-quote-map '(("\"" "&quot;")
1439 ("<" "&lt;")
1440 ("&" "&amp;")
1441 (">" "&gt;")
1442 (" " "<text:s/>")
1443 (" " "<text:tab/>")))
1444 (hfy-face-to-css 'org-odt-hfy-face-to-css)
1445 (hfy-optimisations-1 (copy-seq hfy-optimisations))
1446 (hfy-optimisations (add-to-list 'hfy-optimisations-1
1447 'body-text-only))
1448 (hfy-begin-span-handler
1449 (lambda (style text-block text-id text-begins-block-p)
1450 (insert (format "<text:span text:style-name=\"%s\">" style))))
1451 (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
1452 (when (fboundp 'htmlfontify-string)
1453 (let* ((lines (org-split-string lines "[\r\n]"))
1454 (line-count (length lines))
1455 (i 0))
1456 (mapconcat
1457 (lambda (line)
1458 (incf i)
1459 (org-odt-format-source-line-with-line-number-and-label
1460 line rpllbl num 'htmlfontify-string
1461 (if (= i line-count) "OrgSrcBlockLastLine" "OrgSrcBlock")))
1462 lines "\n")))))
1464 (defun org-odt-format-source-code-or-example (lines lang caption textareap
1465 cols rows num cont
1466 rpllbl fmt)
1467 "Format source or example blocks for export.
1468 Use `org-odt-format-source-code-or-example-plain' or
1469 `org-odt-format-source-code-or-example-colored' depending on the
1470 value of `org-export-odt-fontify-srcblocks."
1471 (setq lines (org-export-number-lines
1472 lines 0 0 num cont rpllbl fmt 'preprocess)
1473 lines (funcall
1474 (or (and org-export-odt-fontify-srcblocks
1475 (or (featurep 'htmlfontify)
1476 ;; htmlfontify.el was introduced in Emacs 23.2
1477 ;; So load it with some caution
1478 (require 'htmlfontify nil t))
1479 (fboundp 'htmlfontify-string)
1480 'org-odt-format-source-code-or-example-colored)
1481 'org-odt-format-source-code-or-example-plain)
1482 lines lang caption textareap cols rows num cont rpllbl fmt))
1483 (if (not num) lines
1484 (let ((extra (format " text:continue-numbering=\"%s\""
1485 (if cont "true" "false"))))
1486 (org-odt-format-tags
1487 '("<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>"
1488 . "</text:list>") lines extra))))
1490 (defun org-odt-remap-stylenames (style-name)
1492 (cdr (assoc style-name '(("timestamp-wrapper" . "OrgTimestampWrapper")
1493 ("timestamp" . "OrgTimestamp")
1494 ("timestamp-kwd" . "OrgTimestampKeyword")
1495 ("tag" . "OrgTag")
1496 ("todo" . "OrgTodo")
1497 ("done" . "OrgDone")
1498 ("target" . "OrgTarget"))))
1499 style-name))
1501 (defun org-odt-format-fontify (text style &optional id)
1502 (let* ((style-name
1503 (cond
1504 ((stringp style)
1505 (org-odt-remap-stylenames style))
1506 ((symbolp style)
1507 (org-odt-get-style-name-for-entity 'character style))
1508 ((listp style)
1509 (assert (< 1 (length style)))
1510 (let ((parent-style (pop style)))
1511 (mapconcat (lambda (s)
1512 ;; (assert (stringp s) t)
1513 (org-odt-remap-stylenames s)) style "")
1514 (org-odt-remap-stylenames parent-style)))
1515 (t (error "Don't how to handle style %s" style)))))
1516 (org-odt-format-tags
1517 '("<text:span text:style-name=\"%s\">" . "</text:span>")
1518 text style-name)))
1520 (defun org-odt-relocate-relative-path (path dir)
1521 (if (file-name-absolute-p path) path
1522 (file-relative-name (expand-file-name path dir)
1523 (expand-file-name "eyecandy" dir))))
1525 (defun org-odt-format-inline-image (thefile)
1526 (let* ((thelink (if (file-name-absolute-p thefile) thefile
1527 (org-xml-format-href
1528 (org-odt-relocate-relative-path
1529 thefile org-current-export-file))))
1530 (href
1531 (org-odt-format-tags
1532 "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
1533 (if org-export-odt-embed-images
1534 (org-odt-copy-image-file thefile) thelink))))
1535 (org-export-odt-format-image thefile href)))
1537 (defvar org-odt-entity-labels-alist nil
1538 "Associate Labels with the Labeled entities.
1539 Each element of the alist is of the form (LABEL-NAME
1540 CATEGORY-NAME SEQNO LABEL-STYLE-NAME). LABEL-NAME is same as
1541 that specified by \"#+LABEL: ...\" line. CATEGORY-NAME is the
1542 type of the entity that LABEL-NAME is attached to. CATEGORY-NAME
1543 can be one of \"Table\", \"Figure\" or \"Equation\". SEQNO is
1544 the unique number assigned to the referenced entity on a
1545 per-CATEGORY basis. It is generated sequentially and is 1-based.
1546 LABEL-STYLE-NAME is a key `org-odt-label-styles'.
1548 See `org-odt-add-label-definition' and
1549 `org-odt-fixup-label-references'.")
1551 (defun org-export-odt-format-formula (src href)
1552 (save-match-data
1553 (let* ((caption (org-find-text-property-in-string 'org-caption src))
1554 (short-caption
1555 (or (org-find-text-property-in-string 'org-caption-shortn src)
1556 caption))
1557 (caption (and caption (org-xml-format-desc caption)))
1558 (short-caption (and short-caption
1559 (org-xml-encode-plain-text short-caption)))
1560 (label (org-find-text-property-in-string 'org-label src))
1561 (latex-frag (org-find-text-property-in-string 'org-latex-src src))
1562 (embed-as (or (and latex-frag
1563 (org-find-text-property-in-string
1564 'org-latex-src-embed-type src))
1565 (if (or caption label) 'paragraph 'character)))
1566 width height)
1567 (when latex-frag
1568 (setq href (org-propertize href :title "LaTeX Fragment"
1569 :description latex-frag)))
1570 (cond
1571 ((eq embed-as 'character)
1572 (org-odt-format-entity "InlineFormula" href width height))
1574 (org-lparse-end-paragraph)
1575 (org-lparse-insert-list-table
1576 `((,(org-odt-format-entity
1577 (if (not (or caption label)) "DisplayFormula"
1578 "CaptionedDisplayFormula")
1579 href width height :caption caption :label label
1580 :short-caption short-caption)
1581 ,(if (not (or caption label)) ""
1582 (let* ((label-props (car org-odt-entity-labels-alist)))
1583 (setcar (last label-props) "math-label")
1584 (apply 'org-odt-format-label-definition
1585 caption label-props)))))
1586 nil nil nil ":style \"OrgEquation\"" nil '((1 "c" 8) (2 "c" 1)))
1587 (throw 'nextline nil))))))
1589 (defvar org-odt-embedded-formulas-count 0)
1590 (defun org-odt-copy-formula-file (path)
1591 "Returns the internal name of the file"
1592 (let* ((src-file (expand-file-name
1593 path (file-name-directory org-current-export-file)))
1594 (target-dir (format "Formula-%04d/"
1595 (incf org-odt-embedded-formulas-count)))
1596 (target-file (concat target-dir "content.xml")))
1597 (when (not org-lparse-to-buffer)
1598 (message "Embedding %s as %s ..."
1599 (substring-no-properties path) target-file)
1601 (make-directory target-dir)
1602 (org-odt-create-manifest-file-entry
1603 "application/vnd.oasis.opendocument.formula" target-dir "1.2")
1605 (case (org-odt-is-formula-link-p src-file)
1606 (mathml
1607 (copy-file src-file target-file 'overwrite))
1608 (odf
1609 (org-odt-zip-extract-one src-file "content.xml" target-dir))
1611 (error "%s is not a formula file" src-file)))
1613 (org-odt-create-manifest-file-entry "text/xml" target-file))
1614 target-file))
1616 (defun org-odt-format-inline-formula (thefile)
1617 (let* ((thelink (if (file-name-absolute-p thefile) thefile
1618 (org-xml-format-href
1619 (org-odt-relocate-relative-path
1620 thefile org-current-export-file))))
1621 (href
1622 (org-odt-format-tags
1623 "<draw:object xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
1624 (file-name-directory (org-odt-copy-formula-file thefile)))))
1625 (org-export-odt-format-formula thefile href)))
1627 (defun org-odt-is-formula-link-p (file)
1628 (let ((case-fold-search nil))
1629 (cond
1630 ((string-match "\\.\\(mathml\\|mml\\)\\'" file)
1631 'mathml)
1632 ((string-match "\\.odf\\'" file)
1633 'odf))))
1635 (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
1636 descp)
1637 "Make a OpenDocument link.
1638 OPT-PLIST is an options list.
1639 TYPE-1 is the device-type of the link (THIS://foo.html).
1640 PATH is the path of the link (http://THIS#location).
1641 FRAGMENT is the fragment part of the link, if any (foo.html#THIS).
1642 DESC is the link description, if any.
1643 ATTR is a string of other attributes of the a element."
1644 (declare (special org-lparse-par-open))
1645 (save-match-data
1646 (let* ((may-inline-p
1647 (and (member type-1 '("http" "https" "file"))
1648 (org-lparse-should-inline-p path descp)
1649 (not fragment)))
1650 (type (if (equal type-1 "id") "file" type-1))
1651 (filename path)
1652 (thefile path)
1653 sec-frag sec-nos)
1654 (cond
1655 ;; check for inlined images
1656 ((and (member type '("file"))
1657 (not fragment)
1658 (org-file-image-p
1659 filename org-export-odt-inline-image-extensions)
1660 (or (eq t org-export-odt-inline-images)
1661 (and org-export-odt-inline-images (not descp))))
1662 (org-odt-format-inline-image thefile))
1663 ;; check for embedded formulas
1664 ((and (member type '("file"))
1665 (not fragment)
1666 (org-odt-is-formula-link-p filename)
1667 (or (not descp)))
1668 (org-odt-format-inline-formula thefile))
1669 ;; code references
1670 ((string= type "coderef")
1671 (let* ((ref fragment)
1672 (lineno-or-ref (cdr (assoc ref org-export-code-refs)))
1673 (desc (and descp desc))
1674 (org-odt-suppress-xref nil)
1675 (href (org-xml-format-href (concat "#coderef-" ref))))
1676 (cond
1677 ((and (numberp lineno-or-ref) (not desc))
1678 (org-odt-format-link lineno-or-ref href))
1679 ((and (numberp lineno-or-ref) desc
1680 (string-match (regexp-quote (concat "(" ref ")")) desc))
1681 (format (replace-match "%s" t t desc)
1682 (org-odt-format-link lineno-or-ref href)))
1684 (setq desc (format
1685 (if (and desc (string-match
1686 (regexp-quote (concat "(" ref ")"))
1687 desc))
1688 (replace-match "%s" t t desc)
1689 (or desc "%s"))
1690 lineno-or-ref))
1691 (org-odt-format-link (org-xml-format-desc desc) href)))))
1692 ;; links to headlines
1693 ((and (string= type "")
1694 (or (not thefile) (string= thefile ""))
1695 (plist-get org-lparse-opt-plist :section-numbers)
1696 (setq sec-frag fragment)
1697 (or (string-match "\\`sec\\(\\(-[0-9]+\\)+\\)" sec-frag)
1698 (and (setq sec-frag
1699 (loop for alias in org-export-target-aliases do
1700 (when (member fragment (cdr alias))
1701 (return (car alias)))))
1702 (string-match "\\`sec\\(\\(-[0-9]+\\)+\\)" sec-frag)))
1703 (setq sec-nos (org-split-string (match-string 1 sec-frag) "-"))
1704 (<= (length sec-nos) (plist-get org-lparse-opt-plist
1705 :headline-levels)))
1706 (let ((org-odt-suppress-xref nil))
1707 (org-odt-format-link sec-nos (concat "#" sec-frag) attr)))
1709 (when (string= type "file")
1710 (setq thefile
1711 (cond
1712 ((file-name-absolute-p path)
1713 (concat "file://" (expand-file-name path)))
1714 (t (org-odt-relocate-relative-path
1715 thefile org-current-export-file)))))
1717 (when (and (member type '("" "http" "https" "file")) fragment)
1718 (setq thefile (concat thefile "#" fragment)))
1720 (setq thefile (org-xml-format-href thefile))
1722 (when (not (member type '("" "file")))
1723 (setq thefile (concat type ":" thefile)))
1725 (let ((org-odt-suppress-xref nil))
1726 (org-odt-format-link
1727 (org-xml-format-desc desc) thefile attr)))))))
1729 (defun org-odt-format-heading (text level &optional id)
1730 (let* ((text (if id (org-odt-format-target text id) text)))
1731 (org-odt-format-tags
1732 '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
1733 "</text:h>") text level level)))
1735 (defun org-odt-format-headline (title extra-targets tags
1736 &optional snumber level)
1737 (concat
1738 (org-lparse-format 'EXTRA-TARGETS extra-targets)
1740 ;; No need to generate section numbers. They are auto-generated by
1741 ;; the application
1743 ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
1744 title
1745 (and tags (concat (org-lparse-format 'SPACES 3)
1746 (org-lparse-format 'ORG-TAGS tags)))))
1748 (defun org-odt-format-anchor (text name &optional class)
1749 (org-odt-format-target text name))
1751 (defun org-odt-format-bookmark (text id)
1752 (if id
1753 (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id)
1754 text))
1756 (defun org-odt-format-target (text id)
1757 (let ((name (concat org-export-odt-bookmark-prefix id)))
1758 (concat
1759 (and id (org-odt-format-tags
1760 "<text:bookmark-start text:name=\"%s\"/>" "" name))
1761 (org-odt-format-bookmark text id)
1762 (and id (org-odt-format-tags
1763 "<text:bookmark-end text:name=\"%s\"/>" "" name)))))
1765 (defun org-odt-format-footnote (n def)
1766 (let ((id (concat "fn" n))
1767 (note-class "footnote")
1768 (par-style "Footnote"))
1769 (org-odt-format-tags
1770 '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
1771 "</text:note>")
1772 (concat
1773 (org-odt-format-tags
1774 '("<text:note-citation>" . "</text:note-citation>")
1776 (org-odt-format-tags
1777 '("<text:note-body>" . "</text:note-body>")
1778 def))
1779 id note-class)))
1781 (defun org-odt-format-footnote-reference (n def refcnt)
1782 (if (= refcnt 1)
1783 (org-odt-format-footnote n def)
1784 (org-odt-format-footnote-ref n)))
1786 (defun org-odt-format-footnote-ref (n)
1787 (let ((note-class "footnote")
1788 (ref-format "text")
1789 (ref-name (concat "fn" n)))
1790 (org-odt-format-tags
1791 '("<text:span text:style-name=\"%s\">" . "</text:span>")
1792 (org-odt-format-tags
1793 '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" . "</text:note-ref>")
1794 n note-class ref-format ref-name)
1795 "OrgSuperscript")))
1797 (defun org-odt-get-image-name (file-name)
1798 (require 'sha1)
1799 (file-relative-name
1800 (expand-file-name
1801 (concat (sha1 file-name) "." (file-name-extension file-name)) "Pictures")))
1803 (defun org-export-odt-format-image (src href)
1804 "Create image tag with source and attributes."
1805 (save-match-data
1806 (let* ((caption (org-find-text-property-in-string 'org-caption src))
1807 (short-caption
1808 (or (org-find-text-property-in-string 'org-caption-shortn src)
1809 caption))
1810 (caption (and caption (org-xml-format-desc caption)))
1811 (short-caption (and short-caption
1812 (org-xml-encode-plain-text short-caption)))
1813 (attr (org-find-text-property-in-string 'org-attributes src))
1814 (label (org-find-text-property-in-string 'org-label src))
1815 (latex-frag (org-find-text-property-in-string
1816 'org-latex-src src))
1817 (category (and latex-frag "__DvipngImage__"))
1818 (attr-plist (org-lparse-get-block-params attr))
1819 (user-frame-anchor
1820 (car (assoc-string (plist-get attr-plist :anchor)
1821 '(("as-char") ("paragraph") ("page")) t)))
1822 (user-frame-style
1823 (and user-frame-anchor (plist-get attr-plist :style)))
1824 (user-frame-attrs
1825 (and user-frame-anchor (plist-get attr-plist :attributes)))
1826 (user-frame-params
1827 (list user-frame-style user-frame-attrs user-frame-anchor))
1828 (embed-as (cond
1829 (latex-frag
1830 (symbol-name
1831 (case (org-find-text-property-in-string
1832 'org-latex-src-embed-type src)
1833 (paragraph 'paragraph)
1834 (t 'as-char))))
1835 (user-frame-anchor)
1836 (t "paragraph")))
1837 (size (org-odt-image-size-from-file
1838 src (plist-get attr-plist :width)
1839 (plist-get attr-plist :height)
1840 (plist-get attr-plist :scale) nil embed-as))
1841 (width (car size)) (height (cdr size)))
1842 (when latex-frag
1843 (setq href (org-propertize href :title "LaTeX Fragment"
1844 :description latex-frag)))
1845 (let ((frame-style-handle (concat (and (or caption label) "Captioned")
1846 embed-as "Image")))
1847 (org-odt-format-entity
1848 frame-style-handle href width height
1849 :caption caption :label label :category category
1850 :short-caption short-caption
1851 :user-frame-params user-frame-params)))))
1853 (defun org-odt-format-object-description (title description)
1854 (concat (and title (org-odt-format-tags
1855 '("<svg:title>" . "</svg:title>")
1856 (org-odt-encode-plain-text title t)))
1857 (and description (org-odt-format-tags
1858 '("<svg:desc>" . "</svg:desc>")
1859 (org-odt-encode-plain-text description t)))))
1861 (defun org-odt-format-frame (text width height style &optional
1862 extra anchor-type)
1863 (let ((frame-attrs
1864 (concat
1865 (if width (format " svg:width=\"%0.2fcm\"" width) "")
1866 (if height (format " svg:height=\"%0.2fcm\"" height) "")
1867 extra
1868 (format " text:anchor-type=\"%s\"" (or anchor-type "paragraph")))))
1869 (org-odt-format-tags
1870 '("<draw:frame draw:style-name=\"%s\"%s>" . "</draw:frame>")
1871 (concat text (org-odt-format-object-description
1872 (get-text-property 0 :title text)
1873 (get-text-property 0 :description text)))
1874 style frame-attrs)))
1876 (defun org-odt-format-textbox (text width height style &optional
1877 extra anchor-type)
1878 (org-odt-format-frame
1879 (org-odt-format-tags
1880 '("<draw:text-box %s>" . "</draw:text-box>")
1881 text (concat (format " fo:min-height=\"%0.2fcm\"" (or height .2))
1882 (unless width
1883 (format " fo:min-width=\"%0.2fcm\"" (or width .2)))))
1884 width nil style extra anchor-type))
1886 (defun org-odt-format-inlinetask (heading content
1887 &optional todo priority tags)
1888 (org-odt-format-stylized-paragraph
1889 nil (org-odt-format-textbox
1890 (concat (org-odt-format-stylized-paragraph
1891 "OrgInlineTaskHeading"
1892 (org-lparse-format
1893 'HEADLINE (concat (org-lparse-format-todo todo) " " heading)
1894 nil tags))
1895 content) nil nil "OrgInlineTaskFrame" " style:rel-width=\"100%\"")))
1897 (defvar org-odt-entity-frame-styles
1898 '(("As-CharImage" "__Figure__" ("OrgInlineImage" nil "as-char"))
1899 ("ParagraphImage" "__Figure__" ("OrgDisplayImage" nil "paragraph"))
1900 ("PageImage" "__Figure__" ("OrgPageImage" nil "page"))
1901 ("CaptionedAs-CharImage" "__Figure__"
1902 ("OrgCaptionedImage"
1903 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
1904 ("OrgInlineImage" nil "as-char"))
1905 ("CaptionedParagraphImage" "__Figure__"
1906 ("OrgCaptionedImage"
1907 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
1908 ("OrgImageCaptionFrame" nil "paragraph"))
1909 ("CaptionedPageImage" "__Figure__"
1910 ("OrgCaptionedImage"
1911 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
1912 ("OrgPageImageCaptionFrame" nil "page"))
1913 ("InlineFormula" "__MathFormula__" ("OrgInlineFormula" nil "as-char"))
1914 ("DisplayFormula" "__MathFormula__" ("OrgDisplayFormula" nil "as-char"))
1915 ("CaptionedDisplayFormula" "__MathFormula__"
1916 ("OrgCaptionedFormula" nil "paragraph")
1917 ("OrgFormulaCaptionFrame" nil "as-char"))))
1919 (defun org-odt-merge-frame-params(default-frame-params user-frame-params)
1920 (if (not user-frame-params) default-frame-params
1921 (assert (= (length default-frame-params) 3))
1922 (assert (= (length user-frame-params) 3))
1923 (loop for user-frame-param in user-frame-params
1924 for default-frame-param in default-frame-params
1925 collect (or user-frame-param default-frame-param))))
1927 (defun* org-odt-format-entity (entity href width height
1928 &key caption label category
1929 user-frame-params short-caption)
1930 (let* ((entity-style (assoc-string entity org-odt-entity-frame-styles t))
1931 default-frame-params frame-params)
1932 (cond
1933 ((not (or caption label))
1934 (setq default-frame-params (nth 2 entity-style))
1935 (setq frame-params (org-odt-merge-frame-params
1936 default-frame-params user-frame-params))
1937 (apply 'org-odt-format-frame href width height frame-params))
1939 (setq default-frame-params (nth 3 entity-style))
1940 (setq frame-params (org-odt-merge-frame-params
1941 default-frame-params user-frame-params))
1942 (apply 'org-odt-format-textbox
1943 (org-odt-format-stylized-paragraph
1944 'illustration
1945 (concat
1946 (apply 'org-odt-format-frame href width height
1947 (let ((entity-style-1 (copy-sequence
1948 (nth 2 entity-style))))
1949 (setcar (cdr entity-style-1)
1950 (concat
1951 (cadr entity-style-1)
1952 (and short-caption
1953 (format " draw:name=\"%s\" "
1954 short-caption))))
1956 entity-style-1))
1957 (org-odt-format-entity-caption
1958 label caption (or category (nth 1 entity-style)))))
1959 width height frame-params)))))
1961 (defvar org-odt-embedded-images-count 0)
1962 (defun org-odt-copy-image-file (path)
1963 "Returns the internal name of the file"
1964 (let* ((image-type (file-name-extension path))
1965 (media-type (format "image/%s" image-type))
1966 (src-file (expand-file-name
1967 path (file-name-directory org-current-export-file)))
1968 (target-dir "Images/")
1969 (target-file
1970 (format "%s%04d.%s" target-dir
1971 (incf org-odt-embedded-images-count) image-type)))
1972 (when (not org-lparse-to-buffer)
1973 (message "Embedding %s as %s ..."
1974 (substring-no-properties path) target-file)
1976 (when (= 1 org-odt-embedded-images-count)
1977 (make-directory target-dir)
1978 (org-odt-create-manifest-file-entry "" target-dir))
1980 (copy-file src-file target-file 'overwrite)
1981 (org-odt-create-manifest-file-entry media-type target-file))
1982 target-file))
1984 (defvar org-export-odt-image-size-probe-method
1985 (append (and (executable-find "identify") '(imagemagick)) ; See Bug#10675
1986 '(emacs fixed))
1987 "Ordered list of methods for determining image sizes.")
1989 (defvar org-export-odt-default-image-sizes-alist
1990 '(("as-char" . (5 . 0.4))
1991 ("paragraph" . (5 . 5)))
1992 "Hardcoded image dimensions one for each of the anchor
1993 methods.")
1995 ;; A4 page size is 21.0 by 29.7 cms
1996 ;; The default page settings has 2cm margin on each of the sides. So
1997 ;; the effective text area is 17.0 by 25.7 cm
1998 (defvar org-export-odt-max-image-size '(17.0 . 20.0)
1999 "Limiting dimensions for an embedded image.")
2001 (defun org-odt-do-image-size (probe-method file &optional dpi anchor-type)
2002 (setq dpi (or dpi org-export-odt-pixels-per-inch))
2003 (setq anchor-type (or anchor-type "paragraph"))
2004 (flet ((size-in-cms (size-in-pixels)
2005 (flet ((pixels-to-cms (pixels)
2006 (let* ((cms-per-inch 2.54)
2007 (inches (/ pixels dpi)))
2008 (* cms-per-inch inches))))
2009 (and size-in-pixels
2010 (cons (pixels-to-cms (car size-in-pixels))
2011 (pixels-to-cms (cdr size-in-pixels)))))))
2012 (case probe-method
2013 (emacs
2014 (size-in-cms (ignore-errors ; Emacs could be in batch mode
2015 (clear-image-cache)
2016 (image-size (create-image file) 'pixels))))
2017 (imagemagick
2018 (size-in-cms
2019 (let ((dim (shell-command-to-string
2020 (format "identify -format \"%%w:%%h\" \"%s\"" file))))
2021 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
2022 (cons (string-to-number (match-string 1 dim))
2023 (string-to-number (match-string 2 dim)))))))
2025 (cdr (assoc-string anchor-type
2026 org-export-odt-default-image-sizes-alist))))))
2028 (defun org-odt-image-size-from-file (file &optional user-width
2029 user-height scale dpi embed-as)
2030 (unless (file-name-absolute-p file)
2031 (setq file (expand-file-name
2032 file (file-name-directory org-current-export-file))))
2033 (let* (size width height)
2034 (unless (and user-height user-width)
2035 (loop for probe-method in org-export-odt-image-size-probe-method
2036 until size
2037 do (setq size (org-odt-do-image-size
2038 probe-method file dpi embed-as)))
2039 (or size (error "Cannot determine Image size. Aborting ..."))
2040 (setq width (car size) height (cdr size)))
2041 (cond
2042 (scale
2043 (setq width (* width scale) height (* height scale)))
2044 ((and user-height user-width)
2045 (setq width user-width height user-height))
2046 (user-height
2047 (setq width (* user-height (/ width height)) height user-height))
2048 (user-width
2049 (setq height (* user-width (/ height width)) width user-width))
2050 (t (ignore)))
2051 ;; ensure that an embedded image fits comfortably within a page
2052 (let ((max-width (car org-export-odt-max-image-size))
2053 (max-height (cdr org-export-odt-max-image-size)))
2054 (when (or (> width max-width) (> height max-height))
2055 (let* ((scale1 (/ max-width width))
2056 (scale2 (/ max-height height))
2057 (scale (min scale1 scale2)))
2058 (setq width (* scale width) height (* scale height)))))
2059 (cons width height)))
2061 (defvar org-odt-entity-counts-plist nil
2062 "Plist of running counters of SEQNOs for each of the CATEGORY-NAMEs.
2063 See `org-odt-entity-labels-alist' for known CATEGORY-NAMEs.")
2065 (defvar org-odt-label-styles
2066 '(("math-formula" "%c" "text" "(%n)")
2067 ("math-label" "(%n)" "text" "(%n)")
2068 ("category-and-value" "%e %n: %c" "category-and-value" "%e %n")
2069 ("value" "%e %n: %c" "value" "%n"))
2070 "Specify how labels are applied and referenced.
2071 This is an alist where each element is of the
2072 form (LABEL-STYLE-NAME LABEL-ATTACH-FMT LABEL-REF-MODE
2073 LABEL-REF-FMT).
2075 LABEL-ATTACH-FMT controls how labels and captions are attached to
2076 an entity. It may contain following specifiers - %e, %n and %c.
2077 %e is replaced with the CATEGORY-NAME. %n is replaced with
2078 \"<text:sequence ...> SEQNO </text:sequence>\". %c is replaced
2079 with CAPTION. See `org-odt-format-label-definition'.
2081 LABEL-REF-MODE and LABEL-REF-FMT controls how label references
2082 are generated. The following XML is generated for a label
2083 reference - \"<text:sequence-ref
2084 text:reference-format=\"LABEL-REF-MODE\" ...> LABEL-REF-FMT
2085 </text:sequence-ref>\". LABEL-REF-FMT may contain following
2086 specifiers - %e and %n. %e is replaced with the CATEGORY-NAME.
2087 %n is replaced with SEQNO. See
2088 `org-odt-format-label-reference'.")
2090 (defcustom org-export-odt-category-strings
2091 '(("en" "Table" "Figure" "Equation" "Equation"))
2092 "Specify category strings for various captionable entities.
2093 Captionable entity can be one of a Table, an Embedded Image, a
2094 LaTeX fragment (generated with dvipng) or a Math Formula.
2096 For example, when `org-export-default-language' is \"en\", an
2097 embedded image will be captioned as \"Figure 1: Orgmode Logo\".
2098 If you want the images to be captioned instead as \"Illustration
2099 1: Orgmode Logo\", then modify the entry for \"en\" as shown
2100 below.
2102 \(setq org-export-odt-category-strings
2103 '\(\(\"en\" \"Table\" \"Illustration\"
2104 \"Equation\" \"Equation\"\)\)\)"
2105 :group 'org-export-odt
2106 :version "24.1"
2107 :type '(repeat (list (string :tag "Language tag")
2108 (choice :tag "Table"
2109 (const :tag "Use Default" nil)
2110 (string :tag "Category string"))
2111 (choice :tag "Figure"
2112 (const :tag "Use Default" nil)
2113 (string :tag "Category string"))
2114 (choice :tag "Math Formula"
2115 (const :tag "Use Default" nil)
2116 (string :tag "Category string"))
2117 (choice :tag "Dvipng Image"
2118 (const :tag "Use Default" nil)
2119 (string :tag "Category string")))))
2121 (defvar org-odt-category-map-alist
2122 '(("__Table__" "Table" "value")
2123 ("__Figure__" "Illustration" "value")
2124 ("__MathFormula__" "Text" "math-formula")
2125 ("__DvipngImage__" "Equation" "value")
2126 ;; ("__Table__" "Table" "category-and-value")
2127 ;; ("__Figure__" "Figure" "category-and-value")
2128 ;; ("__DvipngImage__" "Equation" "category-and-value")
2130 "Map a CATEGORY-HANDLE to OD-VARIABLE and LABEL-STYLE.
2131 This is a list where each entry is of the form \\(CATEGORY-HANDLE
2132 OD-VARIABLE LABEL-STYLE\\). CATEGORY_HANDLE identifies the
2133 captionable entity in question. OD-VARIABLE is the OpenDocument
2134 sequence counter associated with the entity. These counters are
2135 declared within
2136 \"<text:sequence-decls>...</text:sequence-decls>\" block of
2137 `org-export-odt-content-template-file'. LABEL-STYLE is a key
2138 into `org-odt-label-styles' and specifies how a given entity
2139 should be captioned and referenced.
2141 The position of a CATEGORY-HANDLE in this list is used as an
2142 index in to per-language entry for
2143 `org-export-odt-category-strings' to retrieve a CATEGORY-NAME.
2144 This CATEGORY-NAME is then used for qualifying the user-specified
2145 captions on export.")
2147 (defun org-odt-add-label-definition (label default-category)
2148 "Create an entry in `org-odt-entity-labels-alist' and return it."
2149 (let* ((label-props (assoc default-category org-odt-category-map-alist))
2150 ;; identify the sequence number
2151 (counter (nth 1 label-props))
2152 (sequence-var (intern counter))
2153 (seqno (1+ (or (plist-get org-odt-entity-counts-plist sequence-var)
2154 0)))
2155 ;; assign an internal label, if user has not provided one
2156 (label (if label (substring-no-properties label)
2157 (format "%s-%s" default-category seqno)))
2158 ;; identify label style
2159 (label-style (nth 2 label-props))
2160 ;; grok language setting
2161 (en-strings (assoc-default "en" org-export-odt-category-strings))
2162 (lang (plist-get org-lparse-opt-plist :language))
2163 (lang-strings (assoc-default lang org-export-odt-category-strings))
2164 ;; retrieve localized category sting
2165 (pos (- (length org-odt-category-map-alist)
2166 (length (memq label-props org-odt-category-map-alist))))
2167 (category (or (nth pos lang-strings) (nth pos en-strings)))
2168 (label-props (list label category counter seqno label-style)))
2169 ;; synchronize internal counters
2170 (setq org-odt-entity-counts-plist
2171 (plist-put org-odt-entity-counts-plist sequence-var seqno))
2172 ;; stash label properties for later retrieval
2173 (push label-props org-odt-entity-labels-alist)
2174 label-props))
2176 (defun org-odt-format-label-definition (caption label category counter
2177 seqno label-style)
2178 (assert label)
2179 (format-spec
2180 (cadr (assoc-string label-style org-odt-label-styles t))
2181 `((?e . ,category)
2182 (?n . ,(org-odt-format-tags
2183 '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" . "</text:sequence>")
2184 (format "%d" seqno) label counter counter))
2185 (?c . ,(or caption "")))))
2187 (defun org-odt-format-label-reference (label category counter
2188 seqno label-style)
2189 (assert label)
2190 (save-match-data
2191 (let* ((fmt (cddr (assoc-string label-style org-odt-label-styles t)))
2192 (fmt1 (car fmt))
2193 (fmt2 (cadr fmt)))
2194 (org-odt-format-tags
2195 '("<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">"
2196 . "</text:sequence-ref>")
2197 (format-spec fmt2 `((?e . ,category)
2198 (?n . ,(format "%d" seqno)))) fmt1 label))))
2200 (defun org-odt-fixup-label-references ()
2201 (goto-char (point-min))
2202 (while (re-search-forward
2203 "<text:sequence-ref text:ref-name=\"\\([^\"]+\\)\">[ \t\n]*</text:sequence-ref>"
2204 nil t)
2205 (let* ((label (match-string 1))
2206 (label-def (assoc label org-odt-entity-labels-alist))
2207 (rpl (and label-def
2208 (apply 'org-odt-format-label-reference label-def))))
2209 (if rpl (replace-match rpl t t)
2210 (org-lparse-warn
2211 (format "Unable to resolve reference to label \"%s\"" label))))))
2213 (defun org-odt-format-entity-caption (label caption category)
2214 (if (not (or label caption)) ""
2215 (apply 'org-odt-format-label-definition caption
2216 (org-odt-add-label-definition label category))))
2218 (defun org-odt-format-tags (tag text &rest args)
2219 (let ((prefix (when org-lparse-encode-pending "@"))
2220 (suffix (when org-lparse-encode-pending "@")))
2221 (apply 'org-lparse-format-tags tag text prefix suffix args)))
2223 (defvar org-odt-manifest-file-entries nil)
2224 (defun org-odt-init-outfile (filename)
2225 (unless (executable-find "zip")
2226 ;; Not at all OSes ship with zip by default
2227 (error "Executable \"zip\" needed for creating OpenDocument files"))
2229 (let* ((outdir (make-temp-file
2230 (format org-export-odt-tmpdir-prefix org-lparse-backend) t))
2231 (content-file (expand-file-name "content.xml" outdir)))
2233 ;; init conten.xml
2234 (with-current-buffer (find-file-noselect content-file t))
2236 ;; reset variables
2237 (setq org-odt-manifest-file-entries nil
2238 org-odt-embedded-images-count 0
2239 org-odt-embedded-formulas-count 0
2240 org-odt-entity-labels-alist nil
2241 org-odt-list-stack-stashed nil
2242 org-odt-automatic-styles nil
2243 org-odt-object-counters nil
2244 org-odt-entity-counts-plist nil)
2245 content-file))
2247 (defcustom org-export-odt-prettify-xml nil
2248 "Specify whether or not the xml output should be prettified.
2249 When this option is turned on, `indent-region' is run on all
2250 component xml buffers before they are saved. Turn this off for
2251 regular use. Turn this on if you need to examine the xml
2252 visually."
2253 :group 'org-export-odt
2254 :version "24.1"
2255 :type 'boolean)
2257 (defvar hfy-user-sheet-assoc) ; bound during org-do-lparse
2258 (defun org-odt-save-as-outfile (target opt-plist)
2259 ;; write automatic styles
2260 (org-odt-write-automatic-styles)
2262 ;; write meta file
2263 (org-odt-update-meta-file opt-plist)
2265 ;; write styles file
2266 (when (equal org-lparse-backend 'odt)
2267 (org-odt-update-styles-file opt-plist))
2269 ;; create mimetype file
2270 (let ((mimetype (org-odt-write-mimetype-file org-lparse-backend)))
2271 (org-odt-create-manifest-file-entry mimetype "/" "1.2"))
2273 ;; create a manifest entry for content.xml
2274 (org-odt-create-manifest-file-entry "text/xml" "content.xml")
2276 ;; write out the manifest entries before zipping
2277 (org-odt-write-manifest-file)
2279 (let ((xml-files '("mimetype" "META-INF/manifest.xml" "content.xml"
2280 "meta.xml"))
2281 (zipdir default-directory))
2282 (when (equal org-lparse-backend 'odt)
2283 (push "styles.xml" xml-files))
2284 (message "Switching to directory %s" (expand-file-name zipdir))
2286 ;; save all xml files
2287 (mapc (lambda (file)
2288 (with-current-buffer
2289 (find-file-noselect (expand-file-name file) t)
2290 ;; prettify output if needed
2291 (when org-export-odt-prettify-xml
2292 (indent-region (point-min) (point-max)))
2293 (save-buffer 0)))
2294 xml-files)
2296 (let* ((target-name (file-name-nondirectory target))
2297 (target-dir (file-name-directory target))
2298 (cmds `(("zip" "-mX0" ,target-name "mimetype")
2299 ("zip" "-rmTq" ,target-name "."))))
2300 (when (file-exists-p target)
2301 ;; FIXME: If the file is locked this throws a cryptic error
2302 (delete-file target))
2304 (let ((coding-system-for-write 'no-conversion) exitcode err-string)
2305 (message "Creating odt file...")
2306 (mapc
2307 (lambda (cmd)
2308 (message "Running %s" (mapconcat 'identity cmd " "))
2309 (setq err-string
2310 (with-output-to-string
2311 (setq exitcode
2312 (apply 'call-process (car cmd)
2313 nil standard-output nil (cdr cmd)))))
2314 (or (zerop exitcode)
2315 (ignore (message "%s" err-string))
2316 (error "Unable to create odt file (%S)" exitcode)))
2317 cmds))
2319 ;; move the file from outdir to target-dir
2320 (rename-file target-name target-dir)
2322 ;; kill all xml buffers
2323 (mapc (lambda (file)
2324 (kill-buffer
2325 (find-file-noselect (expand-file-name file zipdir) t)))
2326 xml-files)
2328 (delete-directory zipdir)))
2329 (message "Created %s" target)
2330 (set-buffer (find-file-noselect target t)))
2332 (defconst org-odt-manifest-file-entry-tag
2334 <manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
2336 (defun org-odt-create-manifest-file-entry (&rest args)
2337 (push args org-odt-manifest-file-entries))
2339 (defun org-odt-write-manifest-file ()
2340 (make-directory "META-INF")
2341 (let ((manifest-file (expand-file-name "META-INF/manifest.xml")))
2342 (with-current-buffer
2343 (find-file-noselect manifest-file t)
2344 (insert
2345 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
2346 <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
2347 (mapc
2348 (lambda (file-entry)
2349 (let* ((version (nth 2 file-entry))
2350 (extra (if version
2351 (format " manifest:version=\"%s\"" version)
2352 "")))
2353 (insert
2354 (format org-odt-manifest-file-entry-tag
2355 (nth 0 file-entry) (nth 1 file-entry) extra))))
2356 org-odt-manifest-file-entries)
2357 (insert "\n</manifest:manifest>"))))
2359 (defun org-odt-update-meta-file (opt-plist)
2360 (let ((date (org-odt-format-date (plist-get opt-plist :date)))
2361 (author (or (plist-get opt-plist :author) ""))
2362 (email (plist-get opt-plist :email))
2363 (keywords (plist-get opt-plist :keywords))
2364 (description (plist-get opt-plist :description))
2365 (title (plist-get opt-plist :title)))
2366 (write-region
2367 (concat
2368 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
2369 <office:document-meta
2370 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
2371 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
2372 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
2373 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
2374 xmlns:ooo=\"http://openoffice.org/2004/office\"
2375 office:version=\"1.2\">
2376 <office:meta>" "\n"
2377 (org-odt-format-author)
2378 (org-odt-format-tags
2379 '("\n<meta:initial-creator>" . "</meta:initial-creator>") author)
2380 (org-odt-format-tags '("\n<dc:date>" . "</dc:date>") date)
2381 (org-odt-format-tags
2382 '("\n<meta:creation-date>" . "</meta:creation-date>") date)
2383 (org-odt-format-tags '("\n<meta:generator>" . "</meta:generator>")
2384 (when org-export-creator-info
2385 (format "Org-%s/Emacs-%s"
2386 (org-version)
2387 emacs-version)))
2388 (org-odt-format-tags '("\n<meta:keyword>" . "</meta:keyword>") keywords)
2389 (org-odt-format-tags '("\n<dc:subject>" . "</dc:subject>") description)
2390 (org-odt-format-tags '("\n<dc:title>" . "</dc:title>") title)
2391 "\n"
2392 " </office:meta>" "</office:document-meta>")
2393 nil (expand-file-name "meta.xml")))
2395 ;; create a manifest entry for meta.xml
2396 (org-odt-create-manifest-file-entry "text/xml" "meta.xml"))
2398 (defun org-odt-update-styles-file (opt-plist)
2399 ;; write styles file
2400 (let ((styles-file (plist-get opt-plist :odt-styles-file)))
2401 (org-odt-copy-styles-file (and styles-file
2402 (read (org-trim styles-file)))))
2404 ;; Update styles.xml - take care of outline numbering
2405 (with-current-buffer
2406 (find-file-noselect (expand-file-name "styles.xml") t)
2407 ;; Don't make automatic backup of styles.xml file. This setting
2408 ;; prevents the backed-up styles.xml file from being zipped in to
2409 ;; odt file. This is more of a hackish fix. Better alternative
2410 ;; would be to fix the zip command so that the output odt file
2411 ;; includes only the needed files and excludes any auto-generated
2412 ;; extra files like backups and auto-saves etc etc. Note that
2413 ;; currently the zip command zips up the entire temp directory so
2414 ;; that any auto-generated files created under the hood ends up in
2415 ;; the resulting odt file.
2416 (set (make-local-variable 'backup-inhibited) t)
2418 ;; Import local setting of `org-export-with-section-numbers'
2419 (org-lparse-bind-local-variables opt-plist)
2420 (org-odt-configure-outline-numbering
2421 (if org-export-with-section-numbers org-export-headline-levels 0)))
2423 ;; Write custom styles for source blocks
2424 (org-odt-insert-custom-styles-for-srcblocks
2425 (mapconcat
2426 (lambda (style)
2427 (format " %s\n" (cddr style)))
2428 hfy-user-sheet-assoc "")))
2430 (defun org-odt-write-mimetype-file (format)
2431 ;; create mimetype file
2432 (let ((mimetype
2433 (case format
2434 (odt "application/vnd.oasis.opendocument.text")
2435 (odf "application/vnd.oasis.opendocument.formula")
2436 (t (error "Unknown OpenDocument backend %S" org-lparse-backend)))))
2437 (write-region mimetype nil (expand-file-name "mimetype"))
2438 mimetype))
2440 (defun org-odt-finalize-outfile ()
2441 (org-odt-delete-empty-paragraphs))
2443 (defun org-odt-delete-empty-paragraphs ()
2444 (goto-char (point-min))
2445 (let ((open "<text:p[^>]*>")
2446 (close "</text:p>"))
2447 (while (re-search-forward (format "%s[ \r\n\t]*%s" open close) nil t)
2448 (replace-match ""))))
2450 (defcustom org-export-odt-convert-processes
2451 '(("LibreOffice"
2452 "soffice --headless --convert-to %f%x --outdir %d %i")
2453 ("unoconv"
2454 "unoconv -f %f -o %d %i"))
2455 "Specify a list of document converters and their usage.
2456 The converters in this list are offered as choices while
2457 customizing `org-export-odt-convert-process'.
2459 This variable is a list where each element is of the
2460 form (CONVERTER-NAME CONVERTER-CMD). CONVERTER-NAME is the name
2461 of the converter. CONVERTER-CMD is the shell command for the
2462 converter and can contain format specifiers. These format
2463 specifiers are interpreted as below:
2465 %i input file name in full
2466 %I input file name as a URL
2467 %f format of the output file
2468 %o output file name in full
2469 %O output file name as a URL
2470 %d output dir in full
2471 %D output dir as a URL.
2472 %x extra options as set in `org-export-odt-convert-capabilities'."
2473 :group 'org-export-odt
2474 :version "24.1"
2475 :type
2476 '(choice
2477 (const :tag "None" nil)
2478 (alist :tag "Converters"
2479 :key-type (string :tag "Converter Name")
2480 :value-type (group (string :tag "Command line")))))
2482 (defcustom org-export-odt-convert-process "LibreOffice"
2483 "Use this converter to convert from \"odt\" format to other formats.
2484 During customization, the list of converter names are populated
2485 from `org-export-odt-convert-processes'."
2486 :group 'org-export-odt
2487 :version "24.1"
2488 :type '(choice :convert-widget
2489 (lambda (w)
2490 (apply 'widget-convert (widget-type w)
2491 (eval (car (widget-get w :args)))))
2492 `((const :tag "None" nil)
2493 ,@(mapcar (lambda (c)
2494 `(const :tag ,(car c) ,(car c)))
2495 org-export-odt-convert-processes))))
2497 (defcustom org-export-odt-convert-capabilities
2498 '(("Text"
2499 ("odt" "ott" "doc" "rtf" "docx")
2500 (("pdf" "pdf") ("odt" "odt") ("rtf" "rtf") ("ott" "ott")
2501 ("doc" "doc" ":\"MS Word 97\"") ("docx" "docx") ("html" "html")))
2502 ("Web"
2503 ("html")
2504 (("pdf" "pdf") ("odt" "odt") ("html" "html")))
2505 ("Spreadsheet"
2506 ("ods" "ots" "xls" "csv" "xlsx")
2507 (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv") ("ods" "ods")
2508 ("xls" "xls") ("xlsx" "xlsx")))
2509 ("Presentation"
2510 ("odp" "otp" "ppt" "pptx")
2511 (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("otp" "otp") ("ppt" "ppt")
2512 ("pptx" "pptx") ("odg" "odg"))))
2513 "Specify input and output formats of `org-export-odt-convert-process'.
2514 More correctly, specify the set of input and output formats that
2515 the user is actually interested in.
2517 This variable is an alist where each element is of the
2518 form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
2519 INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
2520 alist where each element is of the form (OUTPUT-FMT
2521 OUTPUT-FILE-EXTENSION EXTRA-OPTIONS).
2523 The variable is interpreted as follows:
2524 `org-export-odt-convert-process' can take any document that is in
2525 INPUT-FMT-LIST and produce any document that is in the
2526 OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
2527 OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
2528 serves dual purposes:
2529 - It is used for populating completion candidates during
2530 `org-export-odt-convert' commands.
2531 - It is used as the value of \"%f\" specifier in
2532 `org-export-odt-convert-process'.
2534 EXTRA-OPTIONS is used as the value of \"%x\" specifier in
2535 `org-export-odt-convert-process'.
2537 DOCUMENT-CLASS is used to group a set of file formats in
2538 INPUT-FMT-LIST in to a single class.
2540 Note that this variable inherently captures how LibreOffice based
2541 converters work. LibreOffice maps documents of various formats
2542 to classes like Text, Web, Spreadsheet, Presentation etc and
2543 allow document of a given class (irrespective of it's source
2544 format) to be converted to any of the export formats associated
2545 with that class.
2547 See default setting of this variable for an typical
2548 configuration."
2549 :group 'org-export-odt
2550 :version "24.1"
2551 :type
2552 '(choice
2553 (const :tag "None" nil)
2554 (alist :tag "Capabilities"
2555 :key-type (string :tag "Document Class")
2556 :value-type
2557 (group (repeat :tag "Input formats" (string :tag "Input format"))
2558 (alist :tag "Output formats"
2559 :key-type (string :tag "Output format")
2560 :value-type
2561 (group (string :tag "Output file extension")
2562 (choice
2563 (const :tag "None" nil)
2564 (string :tag "Extra options"))))))))
2566 (declare-function org-create-math-formula "org"
2567 (latex-frag &optional mathml-file))
2569 ;;;###autoload
2570 (defun org-export-odt-convert (&optional in-file out-fmt prefix-arg)
2571 "Convert IN-FILE to format OUT-FMT using a command line converter.
2572 IN-FILE is the file to be converted. If unspecified, it defaults
2573 to variable `buffer-file-name'. OUT-FMT is the desired output
2574 format. Use `org-export-odt-convert-process' as the converter.
2575 If PREFIX-ARG is non-nil then the newly converted file is opened
2576 using `org-open-file'."
2577 (interactive
2578 (append (org-lparse-convert-read-params) current-prefix-arg))
2579 (org-lparse-do-convert in-file out-fmt prefix-arg))
2581 (defun org-odt-get (what &optional opt-plist)
2582 (case what
2583 (BACKEND 'odt)
2584 (EXPORT-DIR (org-export-directory :html opt-plist))
2585 (FILE-NAME-EXTENSION "odt")
2586 (EXPORT-BUFFER-NAME "*Org ODT Export*")
2587 (ENTITY-CONTROL org-odt-entity-control-callbacks-alist)
2588 (ENTITY-FORMAT org-odt-entity-format-callbacks-alist)
2589 (INIT-METHOD 'org-odt-init-outfile)
2590 (FINAL-METHOD 'org-odt-finalize-outfile)
2591 (SAVE-METHOD 'org-odt-save-as-outfile)
2592 (CONVERT-METHOD
2593 (and org-export-odt-convert-process
2594 (cadr (assoc-string org-export-odt-convert-process
2595 org-export-odt-convert-processes t))))
2596 (CONVERT-CAPABILITIES
2597 (and org-export-odt-convert-process
2598 (cadr (assoc-string org-export-odt-convert-process
2599 org-export-odt-convert-processes t))
2600 org-export-odt-convert-capabilities))
2601 (TOPLEVEL-HLEVEL 1)
2602 (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps)
2603 (INLINE-IMAGES 'maybe)
2604 (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
2605 (PLAIN-TEXT-MAP '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
2606 (TABLE-FIRST-COLUMN-AS-LABELS nil)
2607 (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY "," 'superscript))
2608 (CODING-SYSTEM-FOR-WRITE 'utf-8)
2609 (CODING-SYSTEM-FOR-SAVE 'utf-8)
2610 (t (error "Unknown property: %s" what))))
2612 (defvar org-lparse-latex-fragment-fallback) ; set by org-do-lparse
2613 (defun org-export-odt-do-preprocess-latex-fragments ()
2614 "Convert LaTeX fragments to images."
2615 (let* ((latex-frag-opt (plist-get org-lparse-opt-plist :LaTeX-fragments))
2616 (latex-frag-opt ; massage the options
2617 (or (and (member latex-frag-opt '(mathjax t))
2618 (not (and (fboundp 'org-format-latex-mathml-available-p)
2619 (org-format-latex-mathml-available-p)))
2620 (prog1 org-lparse-latex-fragment-fallback
2621 (org-lparse-warn
2622 (concat
2623 "LaTeX to MathML converter not available. "
2624 (format "Using %S instead."
2625 org-lparse-latex-fragment-fallback)))))
2626 latex-frag-opt))
2627 cache-dir display-msg)
2628 (cond
2629 ((eq latex-frag-opt 'dvipng)
2630 (setq cache-dir "ltxpng/")
2631 (setq display-msg "Creating LaTeX image %s"))
2632 ((member latex-frag-opt '(mathjax t))
2633 (setq latex-frag-opt 'mathml)
2634 (setq cache-dir "ltxmathml/")
2635 (setq display-msg "Creating MathML formula %s")))
2636 (when (and org-current-export-file)
2637 (org-format-latex
2638 (concat cache-dir (file-name-sans-extension
2639 (file-name-nondirectory org-current-export-file)))
2640 org-current-export-dir nil display-msg
2641 nil nil latex-frag-opt))))
2643 (defadvice org-format-latex-as-mathml
2644 (after org-odt-protect-latex-fragment activate)
2645 "Encode LaTeX fragment as XML.
2646 Do this when translation to MathML fails."
2647 (when (or (not (> (length ad-return-value) 0))
2648 (get-text-property 0 'org-protected ad-return-value))
2649 (setq ad-return-value
2650 (org-propertize (org-odt-encode-plain-text (ad-get-arg 0))
2651 'org-protected t))))
2653 (defun org-export-odt-preprocess-latex-fragments ()
2654 (when (equal org-export-current-backend 'odt)
2655 (org-export-odt-do-preprocess-latex-fragments)))
2657 (defun org-export-odt-preprocess-label-references ()
2658 (goto-char (point-min))
2659 (let (label label-components category value pretty-label)
2660 (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t)
2661 (org-if-unprotected-at (match-beginning 1)
2662 (replace-match
2663 (let ((org-lparse-encode-pending t)
2664 (label (match-string 1)))
2665 ;; markup generated below is mostly an eye-candy. At
2666 ;; pre-processing stage, there is no information on which
2667 ;; entity a label reference points to. The actual markup
2668 ;; is generated as part of `org-odt-fixup-label-references'
2669 ;; which gets called at the fag end of export. By this
2670 ;; time we would have seen and collected all the label
2671 ;; definitions in `org-odt-entity-labels-alist'.
2672 (org-odt-format-tags
2673 '("<text:sequence-ref text:ref-name=\"%s\">" .
2674 "</text:sequence-ref>")
2675 "" (org-add-props label '(org-protected t)))) t t)))))
2677 ;; process latex fragments as part of
2678 ;; `org-export-preprocess-after-blockquote-hook'. Note that this hook
2679 ;; is the one that is closest and well before the call to
2680 ;; `org-export-attach-captions-and-attributes' in
2681 ;; `org-export-preprocess-string'. The above arrangement permits
2682 ;; captions, labels and attributes to be attached to png images
2683 ;; generated out of latex equations.
2684 (add-hook 'org-export-preprocess-after-blockquote-hook
2685 'org-export-odt-preprocess-latex-fragments)
2687 (defun org-export-odt-preprocess (parameters)
2688 (org-export-odt-preprocess-label-references))
2690 (declare-function archive-zip-extract "arc-mode.el" (archive name))
2691 (defun org-odt-zip-extract-one (archive member &optional target)
2692 (require 'arc-mode)
2693 (let* ((target (or target default-directory))
2694 (archive (expand-file-name archive))
2695 (archive-zip-extract
2696 (list "unzip" "-qq" "-o" "-d" target))
2697 exit-code command-output)
2698 (setq command-output
2699 (with-temp-buffer
2700 (setq exit-code (archive-zip-extract archive member))
2701 (buffer-string)))
2702 (unless (zerop exit-code)
2703 (message command-output)
2704 (error "Extraction failed"))))
2706 (defun org-odt-zip-extract (archive members &optional target)
2707 (when (atom members) (setq members (list members)))
2708 (mapc (lambda (member)
2709 (org-odt-zip-extract-one archive member target))
2710 members))
2712 (defun org-odt-copy-styles-file (&optional styles-file)
2713 ;; Non-availability of styles.xml is not a critical error. For now
2714 ;; throw an error purely for aesthetic reasons.
2715 (setq styles-file (or styles-file
2716 org-export-odt-styles-file
2717 (expand-file-name "OrgOdtStyles.xml"
2718 org-odt-styles-dir)
2719 (error "org-odt: Missing styles file?")))
2720 (cond
2721 ((listp styles-file)
2722 (let ((archive (nth 0 styles-file))
2723 (members (nth 1 styles-file)))
2724 (org-odt-zip-extract archive members)
2725 (mapc
2726 (lambda (member)
2727 (when (org-file-image-p member)
2728 (let* ((image-type (file-name-extension member))
2729 (media-type (format "image/%s" image-type)))
2730 (org-odt-create-manifest-file-entry media-type member))))
2731 members)))
2732 ((and (stringp styles-file) (file-exists-p styles-file))
2733 (let ((styles-file-type (file-name-extension styles-file)))
2734 (cond
2735 ((string= styles-file-type "xml")
2736 (copy-file styles-file "styles.xml" t))
2737 ((member styles-file-type '("odt" "ott"))
2738 (org-odt-zip-extract styles-file "styles.xml")))))
2740 (error (format "Invalid specification of styles.xml file: %S"
2741 org-export-odt-styles-file))))
2743 ;; create a manifest entry for styles.xml
2744 (org-odt-create-manifest-file-entry "text/xml" "styles.xml"))
2746 (defun org-odt-configure-outline-numbering (level)
2747 "Outline numbering is retained only upto LEVEL.
2748 To disable outline numbering pass a LEVEL of 0."
2749 (goto-char (point-min))
2750 (let ((regex
2751 "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
2752 (replacement
2753 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
2754 (while (re-search-forward regex nil t)
2755 (when (> (string-to-number (match-string 2)) level)
2756 (replace-match replacement t nil))))
2757 (save-buffer 0))
2759 ;;;###autoload
2760 (defun org-export-as-odf (latex-frag &optional odf-file)
2761 "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
2762 Use `org-create-math-formula' to convert LATEX-FRAG first to
2763 MathML. When invoked as an interactive command, use
2764 `org-latex-regexps' to infer LATEX-FRAG from currently active
2765 region. If no LaTeX fragments are found, prompt for it. Push
2766 MathML source to kill ring, if `org-export-copy-to-kill-ring' is
2767 non-nil."
2768 (interactive
2769 `(,(let (frag)
2770 (setq frag (and (setq frag (and (region-active-p)
2771 (buffer-substring (region-beginning)
2772 (region-end))))
2773 (loop for e in org-latex-regexps
2774 thereis (when (string-match (nth 1 e) frag)
2775 (match-string (nth 2 e) frag)))))
2776 (read-string "LaTeX Fragment: " frag nil frag))
2777 ,(let ((odf-filename (expand-file-name
2778 (concat
2779 (file-name-sans-extension
2780 (or (file-name-nondirectory buffer-file-name)))
2781 "." "odf")
2782 (file-name-directory buffer-file-name))))
2783 (read-file-name "ODF filename: " nil odf-filename nil
2784 (file-name-nondirectory odf-filename)))))
2785 (let* ((org-lparse-backend 'odf)
2786 org-lparse-opt-plist
2787 (filename (or odf-file
2788 (expand-file-name
2789 (concat
2790 (file-name-sans-extension
2791 (or (file-name-nondirectory buffer-file-name)))
2792 "." "odf")
2793 (file-name-directory buffer-file-name))))
2794 (buffer (find-file-noselect (org-odt-init-outfile filename)))
2795 (coding-system-for-write 'utf-8)
2796 (save-buffer-coding-system 'utf-8))
2797 (set-buffer buffer)
2798 (set-buffer-file-coding-system coding-system-for-write)
2799 (let ((mathml (org-create-math-formula latex-frag)))
2800 (unless mathml (error "No Math formula created"))
2801 (insert mathml)
2802 (or (org-export-push-to-kill-ring
2803 (upcase (symbol-name org-lparse-backend)))
2804 (message "Exporting... done")))
2805 (org-odt-save-as-outfile filename nil)))
2807 ;;;###autoload
2808 (defun org-export-as-odf-and-open ()
2809 "Export LaTeX fragment as OpenDocument formula and immediately open it.
2810 Use `org-export-as-odf' to read LaTeX fragment and OpenDocument
2811 formula file."
2812 (interactive)
2813 (org-lparse-and-open
2814 nil nil nil (call-interactively 'org-export-as-odf)))
2816 (provide 'org-odt)
2818 ;;; org-odt.el ends here