org.el: Fix bug in `org-fill-template'
[org-mode.git] / lisp / org-odt.el
blob17c2331b0331de08313f710a158d38e75ac149c8
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.")
327 (defvar org-odt-zip-dir nil
328 "Temporary directory that holds XML files during export.")
330 (defvar org-export-odt-embed-images t
331 "Should the images be copied in to the odt file or just linked?")
333 (defvar org-export-odt-inline-images 'maybe)
334 (defcustom org-export-odt-inline-image-extensions
335 '("png" "jpeg" "jpg" "gif")
336 "Extensions of image files that can be inlined into HTML."
337 :type '(repeat (string :tag "Extension"))
338 :group 'org-export-odt
339 :version "24.1")
341 (defcustom org-export-odt-pixels-per-inch display-pixels-per-inch
342 "Scaling factor for converting images pixels to inches.
343 Use this for sizing of embedded images. See Info node `(org)
344 Images in ODT export' for more information."
345 :type 'float
346 :group 'org-export-odt
347 :version "24.1")
349 (defcustom org-export-odt-create-custom-styles-for-srcblocks t
350 "Whether custom styles for colorized source blocks be automatically created.
351 When this option is turned on, the exporter creates custom styles
352 for source blocks based on the advice of `htmlfontify'. Creation
353 of custom styles happen as part of `org-odt-hfy-face-to-css'.
355 When this option is turned off exporter does not create such
356 styles.
358 Use the latter option if you do not want the custom styles to be
359 based on your current display settings. It is necessary that the
360 styles.xml already contains needed styles for colorizing to work.
362 This variable is effective only if
363 `org-export-odt-fontify-srcblocks' is turned on."
364 :group 'org-export-odt
365 :version "24.1"
366 :type 'boolean)
368 (defvar org-export-odt-default-org-styles-alist
369 '((paragraph . ((default . "Text_20_body")
370 (fixedwidth . "OrgFixedWidthBlock")
371 (verse . "OrgVerse")
372 (quote . "Quotations")
373 (blockquote . "Quotations")
374 (center . "OrgCenter")
375 (left . "OrgLeft")
376 (right . "OrgRight")
377 (title . "OrgTitle")
378 (subtitle . "OrgSubtitle")
379 (footnote . "Footnote")
380 (src . "OrgSrcBlock")
381 (illustration . "Illustration")
382 (table . "Table")
383 (definition-term . "Text_20_body_20_bold")
384 (horizontal-line . "Horizontal_20_Line")))
385 (character . ((default . "Default")
386 (bold . "Bold")
387 (emphasis . "Emphasis")
388 (code . "OrgCode")
389 (verbatim . "OrgCode")
390 (strike . "Strikethrough")
391 (underline . "Underline")
392 (subscript . "OrgSubscript")
393 (superscript . "OrgSuperscript")))
394 (list . ((ordered . "OrgNumberedList")
395 (unordered . "OrgBulletedList")
396 (description . "OrgDescriptionList"))))
397 "Default styles for various entities.")
399 (defvar org-export-odt-org-styles-alist org-export-odt-default-org-styles-alist)
400 (defun org-odt-get-style-name-for-entity (category &optional entity)
401 (let ((entity (or entity 'default)))
403 (cdr (assoc entity (cdr (assoc category
404 org-export-odt-org-styles-alist))))
405 (cdr (assoc entity (cdr (assoc category
406 org-export-odt-default-org-styles-alist))))
407 (error "Cannot determine style name for entity %s of type %s"
408 entity category))))
410 (defcustom org-export-odt-preferred-output-format nil
411 "Automatically post-process to this format after exporting to \"odt\".
412 Interactive commands `org-export-as-odt' and
413 `org-export-as-odt-and-open' export first to \"odt\" format and
414 then use `org-export-odt-convert-process' to convert the
415 resulting document to this format. During customization of this
416 variable, the list of valid values are populated based on
417 `org-export-odt-convert-capabilities'."
418 :group 'org-export-odt
419 :version "24.1"
420 :type '(choice :convert-widget
421 (lambda (w)
422 (apply 'widget-convert (widget-type w)
423 (eval (car (widget-get w :args)))))
424 `((const :tag "None" nil)
425 ,@(mapcar (lambda (c)
426 `(const :tag ,c ,c))
427 (org-lparse-reachable-formats "odt")))))
429 (defmacro org-odt-cleanup-xml-buffers (&rest body)
430 `(let ((org-odt-zip-dir
431 (make-temp-file
432 (format org-export-odt-tmpdir-prefix "odf") t))
433 (--cleanup-xml-buffers
434 (function
435 (lambda nil
436 (let ((xml-files '("mimetype" "META-INF/manifest.xml" "content.xml"
437 "meta.xml" "styles.xml")))
438 ;; kill all xml buffers
439 (mapc (lambda (file)
440 (let ((buf (find-file-noselect
441 (expand-file-name file org-odt-zip-dir) t)))
442 (when (buffer-name buf)
443 (set-buffer-modified-p nil)
444 (kill-buffer buf))))
445 xml-files))
446 ;; delete temporary directory.
447 (delete-directory org-odt-zip-dir t)))))
448 (org-condition-case-unless-debug err
449 (prog1 (progn ,@body)
450 (funcall --cleanup-xml-buffers))
451 ((quit error)
452 (funcall --cleanup-xml-buffers)
453 (message "OpenDocument export failed: %s"
454 (error-message-string err))))))
456 ;;;###autoload
457 (defun org-export-as-odt-and-open (arg)
458 "Export the outline as ODT and immediately open it with a browser.
459 If there is an active region, export only the region.
460 The prefix ARG specifies how many levels of the outline should become
461 headlines. The default is 3. Lower levels will become bulleted lists."
462 (interactive "P")
463 (org-odt-cleanup-xml-buffers
464 (org-lparse-and-open
465 (or org-export-odt-preferred-output-format "odt") "odt" arg)))
467 ;;;###autoload
468 (defun org-export-as-odt-batch ()
469 "Call the function `org-lparse-batch'.
470 This function can be used in batch processing as:
471 emacs --batch
472 --load=$HOME/lib/emacs/org.el
473 --eval \"(setq org-export-headline-levels 2)\"
474 --visit=MyFile --funcall org-export-as-odt-batch"
475 (org-lparse-batch "odt"))
477 ;;; org-export-as-odt
478 ;;;###autoload
479 (defun org-export-as-odt (arg &optional hidden ext-plist
480 to-buffer body-only pub-dir)
481 "Export the outline as a OpenDocumentText file.
482 If there is an active region, export only the region. The prefix
483 ARG specifies how many levels of the outline should become
484 headlines. The default is 3. Lower levels will become bulleted
485 lists. HIDDEN is obsolete and does nothing.
486 EXT-PLIST is a property list with external parameters overriding
487 org-mode's default settings, but still inferior to file-local
488 settings. When TO-BUFFER is non-nil, create a buffer with that
489 name and export to that buffer. If TO-BUFFER is the symbol
490 `string', don't leave any buffer behind but just return the
491 resulting XML as a string. When BODY-ONLY is set, don't produce
492 the file header and footer, simply return the content of
493 <body>...</body>, without even the body tags themselves. When
494 PUB-DIR is set, use this as the publishing directory."
495 (interactive "P")
496 (org-odt-cleanup-xml-buffers
497 (org-lparse (or org-export-odt-preferred-output-format "odt")
498 "odt" arg hidden ext-plist to-buffer body-only pub-dir)))
500 (defvar org-odt-entity-control-callbacks-alist
501 `((EXPORT
502 . (org-odt-begin-export org-odt-end-export))
503 (DOCUMENT-CONTENT
504 . (org-odt-begin-document-content org-odt-end-document-content))
505 (DOCUMENT-BODY
506 . (org-odt-begin-document-body org-odt-end-document-body))
507 (TOC
508 . (org-odt-begin-toc org-odt-end-toc))
509 (ENVIRONMENT
510 . (org-odt-begin-environment org-odt-end-environment))
511 (FOOTNOTE-DEFINITION
512 . (org-odt-begin-footnote-definition org-odt-end-footnote-definition))
513 (TABLE
514 . (org-odt-begin-table org-odt-end-table))
515 (TABLE-ROWGROUP
516 . (org-odt-begin-table-rowgroup org-odt-end-table-rowgroup))
517 (LIST
518 . (org-odt-begin-list org-odt-end-list))
519 (LIST-ITEM
520 . (org-odt-begin-list-item org-odt-end-list-item))
521 (OUTLINE
522 . (org-odt-begin-outline org-odt-end-outline))
523 (OUTLINE-TEXT
524 . (org-odt-begin-outline-text org-odt-end-outline-text))
525 (PARAGRAPH
526 . (org-odt-begin-paragraph org-odt-end-paragraph)))
529 (defvar org-odt-entity-format-callbacks-alist
530 `((EXTRA-TARGETS . org-lparse-format-extra-targets)
531 (ORG-TAGS . org-lparse-format-org-tags)
532 (SECTION-NUMBER . org-lparse-format-section-number)
533 (HEADLINE . org-odt-format-headline)
534 (TOC-ENTRY . org-odt-format-toc-entry)
535 (TOC-ITEM . org-odt-format-toc-item)
536 (TAGS . org-odt-format-tags)
537 (SPACES . org-odt-format-spaces)
538 (TABS . org-odt-format-tabs)
539 (LINE-BREAK . org-odt-format-line-break)
540 (FONTIFY . org-odt-format-fontify)
541 (TODO . org-lparse-format-todo)
542 (LINK . org-odt-format-link)
543 (INLINE-IMAGE . org-odt-format-inline-image)
544 (ORG-LINK . org-odt-format-org-link)
545 (HEADING . org-odt-format-heading)
546 (ANCHOR . org-odt-format-anchor)
547 (TABLE . org-lparse-format-table)
548 (TABLE-ROW . org-odt-format-table-row)
549 (TABLE-CELL . org-odt-format-table-cell)
550 (FOOTNOTES-SECTION . ignore)
551 (FOOTNOTE-REFERENCE . org-odt-format-footnote-reference)
552 (HORIZONTAL-LINE . org-odt-format-horizontal-line)
553 (COMMENT . org-odt-format-comment)
554 (LINE . org-odt-format-line)
555 (ORG-ENTITY . org-odt-format-org-entity))
558 ;;;_. callbacks
559 ;;;_. control callbacks
560 ;;;_ , document body
561 (defun org-odt-begin-office-body ()
562 ;; automatic styles
563 (insert-file-contents
564 (or org-export-odt-content-template-file
565 (expand-file-name "OrgOdtContentTemplate.xml"
566 org-odt-styles-dir)))
567 (goto-char (point-min))
568 (re-search-forward "</office:text>" nil nil)
569 (delete-region (match-beginning 0) (point-max)))
571 ;; Following variable is let bound when `org-do-lparse' is in
572 ;; progress. See org-html.el.
573 (defvar org-lparse-toc)
574 (defun org-odt-format-toc ()
575 (if (not org-lparse-toc) "" (concat "\n" org-lparse-toc "\n")))
577 (defun org-odt-format-preamble (opt-plist)
578 (let* ((title (plist-get opt-plist :title))
579 (author (plist-get opt-plist :author))
580 (date (plist-get opt-plist :date))
581 (iso-date (org-odt-format-date date))
582 (date (org-odt-format-date date "%d %b %Y"))
583 (email (plist-get opt-plist :email))
584 ;; switch on or off above vars based on user settings
585 (author (and (plist-get opt-plist :author-info) (or author email)))
586 (email (and (plist-get opt-plist :email-info) email))
587 (date (and (plist-get opt-plist :time-stamp-file) date)))
588 (concat
589 ;; title
590 (when title
591 (concat
592 (org-odt-format-stylized-paragraph
593 'title (org-odt-format-tags
594 '("<text:title>" . "</text:title>") title))
595 ;; separator
596 "<text:p text:style-name=\"OrgTitle\"/>"))
597 (cond
598 ((and author (not email))
599 ;; author only
600 (concat
601 (org-odt-format-stylized-paragraph
602 'subtitle
603 (org-odt-format-tags
604 '("<text:initial-creator>" . "</text:initial-creator>")
605 author))
606 ;; separator
607 "<text:p text:style-name=\"OrgSubtitle\"/>"))
608 ((and author email)
609 ;; author and email
610 (concat
611 (org-odt-format-stylized-paragraph
612 'subtitle
613 (org-odt-format-link
614 (org-odt-format-tags
615 '("<text:initial-creator>" . "</text:initial-creator>")
616 author) (concat "mailto:" email)))
617 ;; separator
618 "<text:p text:style-name=\"OrgSubtitle\"/>")))
619 ;; date
620 (when date
621 (concat
622 (org-odt-format-stylized-paragraph
623 'subtitle
624 (org-odt-format-tags
625 '("<text:date style:data-style-name=\"%s\" text:date-value=\"%s\">"
626 . "</text:date>") date "N75" iso-date))
627 ;; separator
628 "<text:p text:style-name=\"OrgSubtitle\"/>")))))
630 (defun org-odt-begin-document-body (opt-plist)
631 (org-odt-begin-office-body)
632 (insert (org-odt-format-preamble opt-plist))
633 (setq org-lparse-dyn-first-heading-pos (point)))
635 (defvar org-lparse-body-only) ; let bound during org-do-lparse
636 (defvar org-lparse-to-buffer) ; let bound during org-do-lparse
637 (defun org-odt-end-document-body (opt-plist)
638 (unless org-lparse-body-only
639 (org-lparse-insert-tag "</office:text>")
640 (org-lparse-insert-tag "</office:body>")))
642 (defun org-odt-begin-document-content (opt-plist)
643 (ignore))
645 (defun org-odt-end-document-content ()
646 (org-lparse-insert-tag "</office:document-content>"))
648 (defun org-odt-begin-outline (level1 snumber title tags
649 target extra-targets class)
650 (org-lparse-insert
651 'HEADING (org-lparse-format
652 'HEADLINE title extra-targets tags snumber level1)
653 level1 target))
655 (defun org-odt-end-outline ()
656 (ignore))
658 (defun org-odt-begin-outline-text (level1 snumber class)
659 (ignore))
661 (defun org-odt-end-outline-text ()
662 (ignore))
664 (defun org-odt-begin-section (style &optional name)
665 (let ((default-name (car (org-odt-add-automatic-style "Section"))))
666 (org-lparse-insert-tag
667 "<text:section text:style-name=\"%s\" text:name=\"%s\">"
668 style (or name default-name))))
670 (defun org-odt-end-section ()
671 (org-lparse-insert-tag "</text:section>"))
673 (defun org-odt-begin-paragraph (&optional style)
674 (org-lparse-insert-tag
675 "<text:p%s>" (org-odt-get-extra-attrs-for-paragraph-style style)))
677 (defun org-odt-end-paragraph ()
678 (org-lparse-insert-tag "</text:p>"))
680 (defun org-odt-get-extra-attrs-for-paragraph-style (style)
681 (let (style-name)
682 (setq style-name
683 (cond
684 ((stringp style) style)
685 ((symbolp style) (org-odt-get-style-name-for-entity
686 'paragraph style))))
687 (unless style-name
688 (error "Don't know how to handle paragraph style %s" style))
689 (format " text:style-name=\"%s\"" style-name)))
691 (defun org-odt-format-stylized-paragraph (style text)
692 (org-odt-format-tags
693 '("<text:p%s>" . "</text:p>") text
694 (org-odt-get-extra-attrs-for-paragraph-style style)))
696 (defvar org-lparse-opt-plist) ; bound during org-do-lparse
697 (defun org-odt-format-author (&optional author)
698 (when (setq author (or author (plist-get org-lparse-opt-plist :author)))
699 (org-odt-format-tags '("<dc:creator>" . "</dc:creator>") author)))
701 (defun org-odt-format-date (&optional org-ts fmt)
702 (save-match-data
703 (let* ((time
704 (and (stringp org-ts)
705 (string-match org-ts-regexp0 org-ts)
706 (apply 'encode-time
707 (org-fix-decoded-time
708 (org-parse-time-string (match-string 0 org-ts) t)))))
709 date)
710 (cond
711 (fmt (format-time-string fmt time))
712 (t (setq date (format-time-string "%Y-%m-%dT%H:%M:%S%z" time))
713 (format "%s:%s" (substring date 0 -2) (substring date -2)))))))
715 (defun org-odt-begin-annotation (&optional author date)
716 (org-lparse-insert-tag "<office:annotation>")
717 (when (setq author (org-odt-format-author author))
718 (insert author))
719 (insert (org-odt-format-tags
720 '("<dc:date>" . "</dc:date>")
721 (org-odt-format-date
722 (or date (plist-get org-lparse-opt-plist :date)))))
723 (org-lparse-begin-paragraph))
725 (defun org-odt-end-annotation ()
726 (org-lparse-insert-tag "</office:annotation>"))
728 (defun org-odt-begin-environment (style env-options-plist)
729 (case style
730 (annotation
731 (org-lparse-stash-save-paragraph-state)
732 (org-odt-begin-annotation (plist-get env-options-plist 'author)
733 (plist-get env-options-plist 'date)))
734 ((blockquote verse center quote)
735 (org-lparse-begin-paragraph style)
736 (list))
737 ((fixedwidth native)
738 (org-lparse-end-paragraph)
739 (list))
740 (t (error "Unknown environment %s" style))))
742 (defun org-odt-end-environment (style env-options-plist)
743 (case style
744 (annotation
745 (org-lparse-end-paragraph)
746 (org-odt-end-annotation)
747 (org-lparse-stash-pop-paragraph-state))
748 ((blockquote verse center quote)
749 (org-lparse-end-paragraph)
750 (list))
751 ((fixedwidth native)
752 (org-lparse-begin-paragraph)
753 (list))
754 (t (error "Unknown environment %s" style))))
756 (defvar org-lparse-list-stack) ; dynamically bound in org-do-lparse
757 (defvar org-odt-list-stack-stashed)
758 (defun org-odt-begin-list (ltype)
759 (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
760 ltype))
761 (let* ((style-name (org-odt-get-style-name-for-entity 'list ltype))
762 (extra (concat (if (or org-lparse-list-table-p
763 (and (= 1 (length org-lparse-list-stack))
764 (null org-odt-list-stack-stashed)))
765 " text:continue-numbering=\"false\""
766 " text:continue-numbering=\"true\"")
767 (when style-name
768 (format " text:style-name=\"%s\"" style-name)))))
769 (case ltype
770 ((ordered unordered description)
771 (org-lparse-end-paragraph)
772 (org-lparse-insert-tag "<text:list%s>" extra))
773 (t (error "Unknown list type: %s" ltype)))))
775 (defun org-odt-end-list (ltype)
776 (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
777 ltype))
778 (if ltype
779 (org-lparse-insert-tag "</text:list>")
780 (error "Unknown list type: %s" ltype)))
782 (defun org-odt-begin-list-item (ltype &optional arg headline)
783 (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
784 ltype))
785 (case ltype
786 (ordered
787 (assert (not headline) t)
788 (let* ((counter arg) (extra ""))
789 (org-lparse-insert-tag (if (= (length org-lparse-list-stack)
790 (length org-odt-list-stack-stashed))
791 "<text:list-header>" "<text:list-item>"))
792 (org-lparse-begin-paragraph)))
793 (unordered
794 (let* ((id arg) (extra ""))
795 (org-lparse-insert-tag (if (= (length org-lparse-list-stack)
796 (length org-odt-list-stack-stashed))
797 "<text:list-header>" "<text:list-item>"))
798 (org-lparse-begin-paragraph)
799 (insert (if headline (org-odt-format-target headline id)
800 (org-odt-format-bookmark "" id)))))
801 (description
802 (assert (not headline) t)
803 (let ((term (or arg "(no term)")))
804 (insert
805 (org-odt-format-tags
806 '("<text:list-item>" . "</text:list-item>")
807 (org-odt-format-stylized-paragraph 'definition-term term)))
808 (org-lparse-begin-list-item 'unordered)
809 (org-lparse-begin-list 'description)
810 (org-lparse-begin-list-item 'unordered)))
811 (t (error "Unknown list type"))))
813 (defun org-odt-end-list-item (ltype)
814 (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
815 ltype))
816 (case ltype
817 ((ordered unordered)
818 (org-lparse-insert-tag (if (= (length org-lparse-list-stack)
819 (length org-odt-list-stack-stashed))
820 (prog1 "</text:list-header>"
821 (setq org-odt-list-stack-stashed nil))
822 "</text:list-item>")))
823 (description
824 (org-lparse-end-list-item-1)
825 (org-lparse-end-list 'description)
826 (org-lparse-end-list-item-1))
827 (t (error "Unknown list type"))))
829 (defun org-odt-discontinue-list ()
830 (let ((stashed-stack org-lparse-list-stack))
831 (loop for list-type in stashed-stack
832 do (org-lparse-end-list-item-1 list-type)
833 (org-lparse-end-list list-type))
834 (setq org-odt-list-stack-stashed stashed-stack)))
836 (defun org-odt-continue-list ()
837 (setq org-odt-list-stack-stashed (nreverse org-odt-list-stack-stashed))
838 (loop for list-type in org-odt-list-stack-stashed
839 do (org-lparse-begin-list list-type)
840 (org-lparse-begin-list-item list-type)))
842 ;; Following variables are let bound when table emission is in
843 ;; progress. See org-lparse.el.
844 (defvar org-lparse-table-begin-marker)
845 (defvar org-lparse-table-ncols)
846 (defvar org-lparse-table-rowgrp-open)
847 (defvar org-lparse-table-rownum)
848 (defvar org-lparse-table-cur-rowgrp-is-hdr)
849 (defvar org-lparse-table-is-styled)
850 (defvar org-lparse-table-rowgrp-info)
851 (defvar org-lparse-table-colalign-vector)
853 (defvar org-odt-table-style nil
854 "Table style specified by \"#+ATTR_ODT: <style-name>\" line.
855 This is set during `org-odt-begin-table'.")
857 (defvar org-odt-table-style-spec nil
858 "Entry for `org-odt-table-style' in `org-export-odt-table-styles'.")
860 (defcustom org-export-odt-table-styles
861 '(("OrgEquation" "OrgEquation"
862 ((use-first-column-styles . t)
863 (use-last-column-styles . t))))
864 "Specify how Table Styles should be derived from a Table Template.
865 This is a list where each element is of the
866 form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
868 TABLE-STYLE-NAME is the style associated with the table through
869 `org-odt-table-style'.
871 TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
872 TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
873 below) that is included in
874 `org-export-odt-content-template-file'.
876 TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
877 \"TableCell\"
878 PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
879 \"TableParagraph\"
880 TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
881 \"FirstRow\" | \"LastRow\" |
882 \"EvenRow\" | \"OddRow\" |
883 \"EvenColumn\" | \"OddColumn\" | \"\"
884 where \"+\" above denotes string concatenation.
886 TABLE-CELL-OPTIONS is an alist where each element is of the
887 form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
888 TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
889 `use-last-row-styles' |
890 `use-first-column-styles' |
891 `use-last-column-styles' |
892 `use-banding-rows-styles' |
893 `use-banding-columns-styles' |
894 `use-first-row-styles'
895 ON-OR-OFF := `t' | `nil'
897 For example, with the following configuration
899 \(setq org-export-odt-table-styles
900 '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
901 \(\(use-first-row-styles . t\)
902 \(use-first-column-styles . t\)\)\)
903 \(\"TableWithHeaderColumns\" \"Custom\"
904 \(\(use-first-column-styles . t\)\)\)\)\)
906 1. A table associated with \"TableWithHeaderRowsAndColumns\"
907 style will use the following table-cell styles -
908 \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
909 \"CustomTableCell\" and the following paragraph styles
910 \"CustomFirstRowTableParagraph\",
911 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
912 as appropriate.
914 2. A table associated with \"TableWithHeaderColumns\" style will
915 use the following table-cell styles -
916 \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
917 following paragraph styles
918 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
919 as appropriate..
921 Note that TABLE-TEMPLATE-NAME corresponds to the
922 \"<table:table-template>\" elements contained within
923 \"<office:styles>\". The entries (TABLE-STYLE-NAME
924 TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
925 \"table:template-name\" and \"table:use-first-row-styles\" etc
926 attributes of \"<table:table>\" element. Refer ODF-1.2
927 specification for more information. Also consult the
928 implementation filed under `org-odt-get-table-cell-styles'.
930 The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
931 formatting of numbered display equations. Do not delete this
932 style from the list."
933 :group 'org-export-odt
934 :version "24.1"
935 :type '(choice
936 (const :tag "None" nil)
937 (repeat :tag "Table Styles"
938 (list :tag "Table Style Specification"
939 (string :tag "Table Style Name")
940 (string :tag "Table Template Name")
941 (alist :options (use-first-row-styles
942 use-last-row-styles
943 use-first-column-styles
944 use-last-column-styles
945 use-banding-rows-styles
946 use-banding-columns-styles)
947 :key-type symbol
948 :value-type (const :tag "True" t))))))
950 (defvar org-odt-table-style-format
952 <style:style style:name=\"%s\" style:family=\"table\">
953 <style:table-properties style:rel-width=\"%d%%\" fo:margin-top=\"0cm\" fo:margin-bottom=\"0.20cm\" table:align=\"center\"/>
954 </style:style>
956 "Template for auto-generated Table styles.")
958 (defvar org-odt-automatic-styles '()
959 "Registry of automatic styles for various OBJECT-TYPEs.
960 The variable has the following form:
961 \(\(OBJECT-TYPE-A
962 \(\(OBJECT-NAME-A.1 OBJECT-PROPS-A.1\)
963 \(OBJECT-NAME-A.2 OBJECT-PROPS-A.2\) ...\)\)
964 \(OBJECT-TYPE-B
965 \(\(OBJECT-NAME-B.1 OBJECT-PROPS-B.1\)
966 \(OBJECT-NAME-B.2 OBJECT-PROPS-B.2\) ...\)\)
967 ...\).
969 OBJECT-TYPEs could be \"Section\", \"Table\", \"Figure\" etc.
970 OBJECT-PROPS is (typically) a plist created by passing
971 \"#+ATTR_ODT: \" option to `org-lparse-get-block-params'.
973 Use `org-odt-add-automatic-style' to add update this variable.'")
975 (defvar org-odt-object-counters nil
976 "Running counters for various OBJECT-TYPEs.
977 Use this to generate automatic names and style-names. See
978 `org-odt-add-automatic-style'.")
980 (defun org-odt-write-automatic-styles ()
981 "Write automatic styles to \"content.xml\"."
982 (with-current-buffer
983 (find-file-noselect (expand-file-name "content.xml") t)
984 ;; position the cursor
985 (goto-char (point-min))
986 (re-search-forward " </office:automatic-styles>" nil t)
987 (goto-char (match-beginning 0))
988 ;; write automatic table styles
989 (loop for (style-name props) in
990 (plist-get org-odt-automatic-styles 'Table) do
991 (when (setq props (or (plist-get props :rel-width) 96))
992 (insert (format org-odt-table-style-format style-name props))))))
994 (defun org-odt-add-automatic-style (object-type &optional object-props)
995 "Create an automatic style of type OBJECT-TYPE with param OBJECT-PROPS.
996 OBJECT-PROPS is (typically) a plist created by passing
997 \"#+ATTR_ODT: \" option of the object in question to
998 `org-lparse-get-block-params'.
1000 Use `org-odt-object-counters' to generate an automatic
1001 OBJECT-NAME and STYLE-NAME. If OBJECT-PROPS is non-nil, add a
1002 new entry in `org-odt-automatic-styles'. Return (OBJECT-NAME
1003 . STYLE-NAME)."
1004 (assert (stringp object-type))
1005 (let* ((object (intern object-type))
1006 (seqvar object)
1007 (seqno (1+ (or (plist-get org-odt-object-counters seqvar) 0)))
1008 (object-name (format "%s%d" object-type seqno)) style-name)
1009 (setq org-odt-object-counters
1010 (plist-put org-odt-object-counters seqvar seqno))
1011 (when object-props
1012 (setq style-name (format "Org%s" object-name))
1013 (setq org-odt-automatic-styles
1014 (plist-put org-odt-automatic-styles object
1015 (append (list (list style-name object-props))
1016 (plist-get org-odt-automatic-styles object)))))
1017 (cons object-name style-name)))
1019 (defvar org-odt-table-indentedp nil)
1020 (defun org-odt-begin-table (caption label attributes short-caption)
1021 (setq org-odt-table-indentedp (not (null org-lparse-list-stack)))
1022 (when org-odt-table-indentedp
1023 ;; Within the Org file, the table is appearing within a list item.
1024 ;; OpenDocument doesn't allow table to appear within list items.
1025 ;; Temporarily terminate the list, emit the table and then
1026 ;; re-continue the list.
1027 (org-odt-discontinue-list)
1028 ;; Put the Table in an indented section.
1029 (let ((level (length org-odt-list-stack-stashed)))
1030 (org-odt-begin-section (format "OrgIndentedSection-Level-%d" level))))
1031 (setq attributes (org-lparse-get-block-params attributes))
1032 (setq org-odt-table-style (plist-get attributes :style))
1033 (setq org-odt-table-style-spec
1034 (assoc org-odt-table-style org-export-odt-table-styles))
1035 (when (or label caption)
1036 (insert
1037 (org-odt-format-stylized-paragraph
1038 'table (org-odt-format-entity-caption label caption "__Table__"))))
1039 (let ((automatic-name (org-odt-add-automatic-style "Table" attributes)))
1040 (org-lparse-insert-tag
1041 "<table:table table:name=\"%s\" table:style-name=\"%s\">"
1042 (or short-caption (car automatic-name))
1043 (or (nth 1 org-odt-table-style-spec)
1044 (cdr automatic-name) "OrgTable")))
1045 (setq org-lparse-table-begin-marker (point)))
1047 (defvar org-lparse-table-colalign-info)
1048 (defun org-odt-end-table ()
1049 (goto-char org-lparse-table-begin-marker)
1050 (loop for level from 0 below org-lparse-table-ncols
1051 do (let* ((col-cookie (and org-lparse-table-is-styled
1052 (cdr (assoc (1+ level)
1053 org-lparse-table-colalign-info))))
1054 (extra-columns (or (nth 1 col-cookie) 0)))
1055 (dotimes (i (1+ extra-columns))
1056 (insert
1057 (org-odt-format-tags
1058 "<table:table-column table:style-name=\"%sColumn\"/>"
1059 "" (or (nth 1 org-odt-table-style-spec) "OrgTable"))))
1060 (insert "\n")))
1061 ;; fill style attributes for table cells
1062 (when org-lparse-table-is-styled
1063 (while (re-search-forward "@@\\(table-cell:p\\|table-cell:style-name\\)@@\\([0-9]+\\)@@\\([0-9]+\\)@@" nil t)
1064 (let* ((spec (match-string 1))
1065 (r (string-to-number (match-string 2)))
1066 (c (string-to-number (match-string 3)))
1067 (cell-styles (org-odt-get-table-cell-styles
1068 r c org-odt-table-style-spec))
1069 (table-cell-style (car cell-styles))
1070 (table-cell-paragraph-style (cdr cell-styles)))
1071 (cond
1072 ((equal spec "table-cell:p")
1073 (replace-match table-cell-paragraph-style t t))
1074 ((equal spec "table-cell:style-name")
1075 (replace-match table-cell-style t t))))))
1076 (goto-char (point-max))
1077 (org-lparse-insert-tag "</table:table>")
1078 (when org-odt-table-indentedp
1079 (org-odt-end-section)
1080 (org-odt-continue-list)))
1082 (defun org-odt-begin-table-rowgroup (&optional is-header-row)
1083 (when org-lparse-table-rowgrp-open
1084 (org-lparse-end 'TABLE-ROWGROUP))
1085 (org-lparse-insert-tag (if is-header-row
1086 "<table:table-header-rows>"
1087 "<table:table-rows>"))
1088 (setq org-lparse-table-rowgrp-open t)
1089 (setq org-lparse-table-cur-rowgrp-is-hdr is-header-row))
1091 (defun org-odt-end-table-rowgroup ()
1092 (when org-lparse-table-rowgrp-open
1093 (setq org-lparse-table-rowgrp-open nil)
1094 (org-lparse-insert-tag
1095 (if org-lparse-table-cur-rowgrp-is-hdr
1096 "</table:table-header-rows>" "</table:table-rows>"))))
1098 (defun org-odt-format-table-row (row)
1099 (org-odt-format-tags
1100 '("<table:table-row>" . "</table:table-row>") row))
1102 (defun org-odt-get-table-cell-styles (r c &optional style-spec)
1103 "Retrieve styles applicable to a table cell.
1104 R and C are (zero-based) row and column numbers of the table
1105 cell. STYLE-SPEC is an entry in `org-export-odt-table-styles'
1106 applicable to the current table. It is `nil' if the table is not
1107 associated with any style attributes.
1109 Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
1111 When STYLE-SPEC is nil, style the table cell the conventional way
1112 - choose cell borders based on row and column groupings and
1113 choose paragraph alignment based on `org-col-cookies' text
1114 property. See also
1115 `org-odt-get-paragraph-style-cookie-for-table-cell'.
1117 When STYLE-SPEC is non-nil, ignore the above cookie and return
1118 styles congruent with the ODF-1.2 specification."
1119 (cond
1120 (style-spec
1122 ;; LibreOffice - particularly the Writer - honors neither table
1123 ;; templates nor custom table-cell styles. Inorder to retain
1124 ;; inter-operability with LibreOffice, only automatic styles are
1125 ;; used for styling of table-cells. The current implementation is
1126 ;; congruent with ODF-1.2 specification and hence is
1127 ;; future-compatible.
1129 ;; Additional Note: LibreOffice's AutoFormat facility for tables -
1130 ;; which recognizes as many as 16 different cell types - is much
1131 ;; richer. Unfortunately it is NOT amenable to easy configuration
1132 ;; by hand.
1134 (let* ((template-name (nth 1 style-spec))
1135 (cell-style-selectors (nth 2 style-spec))
1136 (cell-type
1137 (cond
1138 ((and (cdr (assoc 'use-first-column-styles cell-style-selectors))
1139 (= c 0)) "FirstColumn")
1140 ((and (cdr (assoc 'use-last-column-styles cell-style-selectors))
1141 (= c (1- org-lparse-table-ncols))) "LastColumn")
1142 ((and (cdr (assoc 'use-first-row-styles cell-style-selectors))
1143 (= r 0)) "FirstRow")
1144 ((and (cdr (assoc 'use-last-row-styles cell-style-selectors))
1145 (= r org-lparse-table-rownum))
1146 "LastRow")
1147 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
1148 (= (% r 2) 1)) "EvenRow")
1149 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
1150 (= (% r 2) 0)) "OddRow")
1151 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
1152 (= (% c 2) 1)) "EvenColumn")
1153 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
1154 (= (% c 2) 0)) "OddColumn")
1155 (t ""))))
1156 (cons
1157 (concat template-name cell-type "TableCell")
1158 (concat template-name cell-type "TableParagraph"))))
1160 (cons
1161 (concat
1162 "OrgTblCell"
1163 (cond
1164 ((= r 0) "T")
1165 ((eq (cdr (assoc r org-lparse-table-rowgrp-info)) :start) "T")
1166 (t ""))
1167 (when (= r org-lparse-table-rownum) "B")
1168 (cond
1169 ((= c 0) "")
1170 ((or (memq (nth c org-table-colgroup-info) '(:start :startend))
1171 (memq (nth (1- c) org-table-colgroup-info) '(:end :startend))) "L")
1172 (t "")))
1173 (capitalize (aref org-lparse-table-colalign-vector c))))))
1175 (defun org-odt-get-paragraph-style-cookie-for-table-cell (r c)
1176 (concat
1177 (and (not org-odt-table-style-spec)
1178 (cond
1179 (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
1180 ((and (= c 0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS))
1181 "OrgTableHeading")
1182 (t "OrgTableContents")))
1183 (and org-lparse-table-is-styled
1184 (format "@@table-cell:p@@%03d@@%03d@@" r c))))
1186 (defun org-odt-get-style-name-cookie-for-table-cell (r c)
1187 (when org-lparse-table-is-styled
1188 (format "@@table-cell:style-name@@%03d@@%03d@@" r c)))
1190 (defun org-odt-format-table-cell (data r c horiz-span)
1191 (concat
1192 (let* ((paragraph-style-cookie
1193 (org-odt-get-paragraph-style-cookie-for-table-cell r c))
1194 (style-name-cookie
1195 (org-odt-get-style-name-cookie-for-table-cell r c))
1196 (extra (and style-name-cookie
1197 (format " table:style-name=\"%s\"" style-name-cookie)))
1198 (extra (concat extra
1199 (and (> horiz-span 0)
1200 (format " table:number-columns-spanned=\"%d\""
1201 (1+ horiz-span))))))
1202 (org-odt-format-tags
1203 '("<table:table-cell%s>" . "</table:table-cell>")
1204 (if org-lparse-list-table-p data
1205 (org-odt-format-stylized-paragraph paragraph-style-cookie data)) extra))
1206 (let (s)
1207 (dotimes (i horiz-span)
1208 (setq s (concat s "\n<table:covered-table-cell/>"))) s)
1209 "\n"))
1211 (defun org-odt-begin-footnote-definition (n)
1212 (org-lparse-begin-paragraph 'footnote))
1214 (defun org-odt-end-footnote-definition (n)
1215 (org-lparse-end-paragraph))
1217 (defun org-odt-begin-toc (lang-specific-heading max-level)
1218 ;; Strings in `org-export-language-setup' can contain named html
1219 ;; entities. Replace those with utf-8 equivalents.
1220 (let ((i 0) entity rpl)
1221 (while (string-match "&\\([^#].*?\\);" lang-specific-heading i)
1222 (setq entity (match-string 1 lang-specific-heading))
1223 (if (not (setq rpl (org-entity-get-representation entity 'utf8)))
1224 (setq i (match-end 0))
1225 (setq i (+ (match-beginning 0) (length rpl)))
1226 (setq lang-specific-heading
1227 (replace-match rpl t t lang-specific-heading)))))
1228 (insert
1229 (format "
1230 <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
1231 <text:table-of-content-source text:outline-level=\"%d\">
1232 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
1233 " max-level lang-specific-heading))
1234 (loop for level from 1 upto 10
1235 do (insert (format
1237 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
1238 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
1239 <text:index-entry-chapter/>
1240 <text:index-entry-text/>
1241 <text:index-entry-link-end/>
1242 </text:table-of-content-entry-template>
1243 " level level)))
1245 (insert
1246 (format "
1247 </text:table-of-content-source>
1249 <text:index-body>
1250 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
1251 <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
1252 </text:index-title>
1253 " lang-specific-heading)))
1255 (defun org-odt-end-toc ()
1256 (insert "
1257 </text:index-body>
1258 </text:table-of-content>
1261 (defun org-odt-format-toc-entry (snumber todo headline tags href)
1262 (setq headline (concat
1263 (and org-export-with-section-numbers
1264 (concat snumber ". "))
1265 headline
1266 (and tags
1267 (concat
1268 (org-lparse-format 'SPACES 3)
1269 (org-lparse-format 'FONTIFY tags "tag")))))
1270 (when todo
1271 (setq headline (org-lparse-format 'FONTIFY headline "todo")))
1273 (let ((org-odt-suppress-xref t))
1274 (org-odt-format-link headline (concat "#" href))))
1276 (defun org-odt-format-toc-item (toc-entry level org-last-level)
1277 (let ((style (format "Contents_20_%d"
1278 (+ level (or (org-lparse-get 'TOPLEVEL-HLEVEL) 1) -1))))
1279 (insert "\n" (org-odt-format-stylized-paragraph style toc-entry) "\n")))
1281 ;; Following variable is let bound during 'ORG-LINK callback. See
1282 ;; org-html.el
1283 (defvar org-lparse-link-description-is-image nil)
1284 (defun org-odt-format-link (desc href &optional attr)
1285 (cond
1286 ((and (= (string-to-char href) ?#) (not org-odt-suppress-xref))
1287 (setq href (substring href 1))
1288 (let ((xref-format "text"))
1289 (when (numberp desc)
1290 (setq desc (format "%d" desc) xref-format "number"))
1291 (when (listp desc)
1292 (setq desc (mapconcat 'identity desc ".") xref-format "chapter"))
1293 (setq href (concat org-export-odt-bookmark-prefix href))
1294 (org-odt-format-tags
1295 '("<text:bookmark-ref text:reference-format=\"%s\" text:ref-name=\"%s\">" .
1296 "</text:bookmark-ref>")
1297 desc xref-format href)))
1298 (org-lparse-link-description-is-image
1299 (org-odt-format-tags
1300 '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</draw:a>")
1301 desc href (or attr "")))
1303 (org-odt-format-tags
1304 '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</text:a>")
1305 desc href (or attr "")))))
1307 (defun org-odt-format-spaces (n)
1308 (cond
1309 ((= n 1) " ")
1310 ((> n 1) (concat
1311 " " (org-odt-format-tags "<text:s text:c=\"%d\"/>" "" (1- n))))
1312 (t "")))
1314 (defun org-odt-format-tabs (&optional n)
1315 (let ((tab "<text:tab/>")
1316 (n (or n 1)))
1317 (insert tab)))
1319 (defun org-odt-format-line-break ()
1320 (org-odt-format-tags "<text:line-break/>" ""))
1322 (defun org-odt-format-horizontal-line ()
1323 (org-odt-format-stylized-paragraph 'horizontal-line ""))
1325 (defun org-odt-encode-plain-text (line &optional no-whitespace-filling)
1326 (setq line (org-xml-encode-plain-text line))
1327 (if no-whitespace-filling line
1328 (org-odt-fill-tabs-and-spaces line)))
1330 (defun org-odt-format-line (line)
1331 (case org-lparse-dyn-current-environment
1332 (fixedwidth (concat
1333 (org-odt-format-stylized-paragraph
1334 'fixedwidth (org-odt-encode-plain-text line)) "\n"))
1335 (t (concat line "\n"))))
1337 (defun org-odt-format-comment (fmt &rest args)
1338 (let ((comment (apply 'format fmt args)))
1339 (format "\n<!-- %s -->\n" comment)))
1341 (defun org-odt-format-org-entity (wd)
1342 (org-entity-get-representation wd 'utf8))
1344 (defun org-odt-fill-tabs-and-spaces (line)
1345 (replace-regexp-in-string
1346 "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
1347 (cond
1348 ((string= s "\t") (org-odt-format-tabs))
1349 (t (org-odt-format-spaces (length s))))) line))
1351 (defcustom org-export-odt-fontify-srcblocks t
1352 "Specify whether or not source blocks need to be fontified.
1353 Turn this option on if you want to colorize the source code
1354 blocks in the exported file. For colorization to work, you need
1355 to make available an enhanced version of `htmlfontify' library."
1356 :type 'boolean
1357 :group 'org-export-odt
1358 :version "24.1")
1360 (defun org-odt-format-source-line-with-line-number-and-label
1361 (line rpllbl num fontifier par-style)
1363 (let ((keep-label (not (numberp rpllbl)))
1364 (ref (org-find-text-property-in-string 'org-coderef line)))
1365 (setq line (concat line (and keep-label ref (format "(%s)" ref))))
1366 (setq line (funcall fontifier line))
1367 (when ref
1368 (setq line (org-odt-format-target line (concat "coderef-" ref))))
1369 (setq line (org-odt-format-stylized-paragraph par-style line))
1370 (if (not num) line
1371 (org-odt-format-tags '("<text:list-item>" . "</text:list-item>") line))))
1373 (defun org-odt-format-source-code-or-example-plain
1374 (lines lang caption textareap cols rows num cont rpllbl fmt)
1375 "Format source or example blocks much like fixedwidth blocks.
1376 Use this when `org-export-odt-fontify-srcblocks' option is turned
1377 off."
1378 (let* ((lines (org-split-string lines "[\r\n]"))
1379 (line-count (length lines))
1380 (i 0))
1381 (mapconcat
1382 (lambda (line)
1383 (incf i)
1384 (org-odt-format-source-line-with-line-number-and-label
1385 line rpllbl num 'org-odt-encode-plain-text
1386 (if (= i line-count) "OrgFixedWidthBlockLastLine"
1387 "OrgFixedWidthBlock")))
1388 lines "\n")))
1390 (defvar org-src-block-paragraph-format
1391 "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
1392 <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
1393 <style:background-image/>
1394 </style:paragraph-properties>
1395 <style:text-properties fo:color=\"%s\"/>
1396 </style:style>"
1397 "Custom paragraph style for colorized source and example blocks.
1398 This style is much the same as that of \"OrgFixedWidthBlock\"
1399 except that the foreground and background colors are set
1400 according to the default face identified by the `htmlfontify'.")
1402 (defvar hfy-optimisations)
1403 (declare-function hfy-face-to-style "htmlfontify" (fn))
1404 (declare-function hfy-face-or-def-to-name "htmlfontify" (fn))
1406 (defun org-odt-hfy-face-to-css (fn)
1407 "Create custom style for face FN.
1408 When FN is the default face, use it's foreground and background
1409 properties to create \"OrgSrcBlock\" paragraph style. Otherwise
1410 use it's color attribute to create a character style whose name
1411 is obtained from FN. Currently all attributes of FN other than
1412 color are ignored.
1414 The style name for a face FN is derived using the following
1415 operations on the face name in that order - de-dash, CamelCase
1416 and prefix with \"OrgSrc\". For example,
1417 `font-lock-function-name-face' is associated with
1418 \"OrgSrcFontLockFunctionNameFace\"."
1419 (let* ((css-list (hfy-face-to-style fn))
1420 (style-name ((lambda (fn)
1421 (concat "OrgSrc"
1422 (mapconcat
1423 'capitalize (split-string
1424 (hfy-face-or-def-to-name fn) "-")
1425 ""))) fn))
1426 (color-val (cdr (assoc "color" css-list)))
1427 (background-color-val (cdr (assoc "background" css-list)))
1428 (style (and org-export-odt-create-custom-styles-for-srcblocks
1429 (cond
1430 ((eq fn 'default)
1431 (format org-src-block-paragraph-format
1432 background-color-val color-val))
1434 (format
1436 <style:style style:name=\"%s\" style:family=\"text\">
1437 <style:text-properties fo:color=\"%s\"/>
1438 </style:style>" style-name color-val))))))
1439 (cons style-name style)))
1441 (defun org-odt-insert-custom-styles-for-srcblocks (styles)
1442 "Save STYLES used for colorizing of source blocks.
1443 Update styles.xml with styles that were collected as part of
1444 `org-odt-hfy-face-to-css' callbacks."
1445 (when styles
1446 (with-current-buffer
1447 (find-file-noselect (expand-file-name "styles.xml") t)
1448 (goto-char (point-min))
1449 (when (re-search-forward "</office:styles>" nil t)
1450 (goto-char (match-beginning 0))
1451 (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n")))))
1453 (defun org-odt-format-source-code-or-example-colored
1454 (lines lang caption textareap cols rows num cont rpllbl fmt)
1455 "Format source or example blocks using `htmlfontify-string'.
1456 Use this routine when `org-export-odt-fontify-srcblocks' option
1457 is turned on."
1458 (let* ((lang-m (and lang (or (cdr (assoc lang org-src-lang-modes)) lang)))
1459 (mode (and lang-m (intern (concat (if (symbolp lang-m)
1460 (symbol-name lang-m)
1461 lang-m) "-mode"))))
1462 (org-inhibit-startup t)
1463 (org-startup-folded nil)
1464 (lines (with-temp-buffer
1465 (insert lines)
1466 (if (functionp mode) (funcall mode) (fundamental-mode))
1467 (font-lock-fontify-buffer)
1468 (buffer-string)))
1469 (hfy-html-quote-regex "\\([<\"&> ]\\)")
1470 (hfy-html-quote-map '(("\"" "&quot;")
1471 ("<" "&lt;")
1472 ("&" "&amp;")
1473 (">" "&gt;")
1474 (" " "<text:s/>")
1475 (" " "<text:tab/>")))
1476 (hfy-face-to-css 'org-odt-hfy-face-to-css)
1477 (hfy-optimisations-1 (copy-seq hfy-optimisations))
1478 (hfy-optimisations (add-to-list 'hfy-optimisations-1
1479 'body-text-only))
1480 (hfy-begin-span-handler
1481 (lambda (style text-block text-id text-begins-block-p)
1482 (insert (format "<text:span text:style-name=\"%s\">" style))))
1483 (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
1484 (when (fboundp 'htmlfontify-string)
1485 (let* ((lines (org-split-string lines "[\r\n]"))
1486 (line-count (length lines))
1487 (i 0))
1488 (mapconcat
1489 (lambda (line)
1490 (incf i)
1491 (org-odt-format-source-line-with-line-number-and-label
1492 line rpllbl num 'htmlfontify-string
1493 (if (= i line-count) "OrgSrcBlockLastLine" "OrgSrcBlock")))
1494 lines "\n")))))
1496 (defun org-odt-format-source-code-or-example (lines lang caption textareap
1497 cols rows num cont
1498 rpllbl fmt)
1499 "Format source or example blocks for export.
1500 Use `org-odt-format-source-code-or-example-plain' or
1501 `org-odt-format-source-code-or-example-colored' depending on the
1502 value of `org-export-odt-fontify-srcblocks."
1503 (setq lines (org-export-number-lines
1504 lines 0 0 num cont rpllbl fmt 'preprocess)
1505 lines (funcall
1506 (or (and org-export-odt-fontify-srcblocks
1507 (or (featurep 'htmlfontify)
1508 ;; htmlfontify.el was introduced in Emacs 23.2
1509 ;; So load it with some caution
1510 (require 'htmlfontify nil t))
1511 (fboundp 'htmlfontify-string)
1512 'org-odt-format-source-code-or-example-colored)
1513 'org-odt-format-source-code-or-example-plain)
1514 lines lang caption textareap cols rows num cont rpllbl fmt))
1515 (if (not num) lines
1516 (let ((extra (format " text:continue-numbering=\"%s\""
1517 (if cont "true" "false"))))
1518 (org-odt-format-tags
1519 '("<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>"
1520 . "</text:list>") lines extra))))
1522 (defun org-odt-remap-stylenames (style-name)
1524 (cdr (assoc style-name '(("timestamp-wrapper" . "OrgTimestampWrapper")
1525 ("timestamp" . "OrgTimestamp")
1526 ("timestamp-kwd" . "OrgTimestampKeyword")
1527 ("tag" . "OrgTag")
1528 ("todo" . "OrgTodo")
1529 ("done" . "OrgDone")
1530 ("target" . "OrgTarget"))))
1531 style-name))
1533 (defun org-odt-format-fontify (text style &optional id)
1534 (let* ((style-name
1535 (cond
1536 ((stringp style)
1537 (org-odt-remap-stylenames style))
1538 ((symbolp style)
1539 (org-odt-get-style-name-for-entity 'character style))
1540 ((listp style)
1541 (assert (< 1 (length style)))
1542 (let ((parent-style (pop style)))
1543 (mapconcat (lambda (s)
1544 ;; (assert (stringp s) t)
1545 (org-odt-remap-stylenames s)) style "")
1546 (org-odt-remap-stylenames parent-style)))
1547 (t (error "Don't how to handle style %s" style)))))
1548 (org-odt-format-tags
1549 '("<text:span text:style-name=\"%s\">" . "</text:span>")
1550 text style-name)))
1552 (defun org-odt-relocate-relative-path (path dir)
1553 (if (file-name-absolute-p path) path
1554 (file-relative-name (expand-file-name path dir)
1555 (expand-file-name "eyecandy" dir))))
1557 (defun org-odt-format-inline-image (thefile)
1558 (let* ((thelink (if (file-name-absolute-p thefile) thefile
1559 (org-xml-format-href
1560 (org-odt-relocate-relative-path
1561 thefile org-current-export-file))))
1562 (href
1563 (org-odt-format-tags
1564 "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
1565 (if org-export-odt-embed-images
1566 (org-odt-copy-image-file thefile) thelink))))
1567 (org-export-odt-format-image thefile href)))
1569 (defvar org-odt-entity-labels-alist nil
1570 "Associate Labels with the Labeled entities.
1571 Each element of the alist is of the form (LABEL-NAME
1572 CATEGORY-NAME SEQNO LABEL-STYLE-NAME). LABEL-NAME is same as
1573 that specified by \"#+LABEL: ...\" line. CATEGORY-NAME is the
1574 type of the entity that LABEL-NAME is attached to. CATEGORY-NAME
1575 can be one of \"Table\", \"Figure\" or \"Equation\". SEQNO is
1576 the unique number assigned to the referenced entity on a
1577 per-CATEGORY basis. It is generated sequentially and is 1-based.
1578 LABEL-STYLE-NAME is a key `org-odt-label-styles'.
1580 See `org-odt-add-label-definition' and
1581 `org-odt-fixup-label-references'.")
1583 (defun org-export-odt-format-formula (src href)
1584 (save-match-data
1585 (let* ((caption (org-find-text-property-in-string 'org-caption src))
1586 (short-caption
1587 (or (org-find-text-property-in-string 'org-caption-shortn src)
1588 caption))
1589 (caption (and caption (org-xml-format-desc caption)))
1590 (short-caption (and short-caption
1591 (org-xml-encode-plain-text short-caption)))
1592 (label (org-find-text-property-in-string 'org-label src))
1593 (latex-frag (org-find-text-property-in-string 'org-latex-src src))
1594 (embed-as (or (and latex-frag
1595 (org-find-text-property-in-string
1596 'org-latex-src-embed-type src))
1597 (if (or caption label) 'paragraph 'character)))
1598 width height)
1599 (when latex-frag
1600 (setq href (org-propertize href :title "LaTeX Fragment"
1601 :description latex-frag)))
1602 (cond
1603 ((eq embed-as 'character)
1604 (org-odt-format-entity "InlineFormula" href width height))
1606 (org-lparse-end-paragraph)
1607 (org-lparse-insert-list-table
1608 `((,(org-odt-format-entity
1609 (if (not (or caption label)) "DisplayFormula"
1610 "CaptionedDisplayFormula")
1611 href width height :caption caption :label label
1612 :short-caption short-caption)
1613 ,(if (not (or caption label)) ""
1614 (let* ((label-props (car org-odt-entity-labels-alist)))
1615 (setcar (last label-props) "math-label")
1616 (apply 'org-odt-format-label-definition
1617 caption label-props)))))
1618 nil nil nil ":style \"OrgEquation\"" nil '((1 "c" 8) (2 "c" 1)))
1619 (throw 'nextline nil))))))
1621 (defvar org-odt-embedded-formulas-count 0)
1622 (defun org-odt-copy-formula-file (path)
1623 "Returns the internal name of the file"
1624 (let* ((src-file (expand-file-name
1625 path (file-name-directory org-current-export-file)))
1626 (target-dir (format "Formula-%04d/"
1627 (incf org-odt-embedded-formulas-count)))
1628 (target-file (concat target-dir "content.xml")))
1629 (when (not org-lparse-to-buffer)
1630 (message "Embedding %s as %s ..."
1631 (substring-no-properties path) target-file)
1633 (make-directory target-dir)
1634 (org-odt-create-manifest-file-entry
1635 "application/vnd.oasis.opendocument.formula" target-dir "1.2")
1637 (case (org-odt-is-formula-link-p src-file)
1638 (mathml
1639 (copy-file src-file target-file 'overwrite))
1640 (odf
1641 (org-odt-zip-extract-one src-file "content.xml" target-dir))
1643 (error "%s is not a formula file" src-file)))
1645 (org-odt-create-manifest-file-entry "text/xml" target-file))
1646 target-file))
1648 (defun org-odt-format-inline-formula (thefile)
1649 (let* ((thelink (if (file-name-absolute-p thefile) thefile
1650 (org-xml-format-href
1651 (org-odt-relocate-relative-path
1652 thefile org-current-export-file))))
1653 (href
1654 (org-odt-format-tags
1655 "<draw:object xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
1656 (file-name-directory (org-odt-copy-formula-file thefile)))))
1657 (org-export-odt-format-formula thefile href)))
1659 (defun org-odt-is-formula-link-p (file)
1660 (let ((case-fold-search nil))
1661 (cond
1662 ((string-match "\\.\\(mathml\\|mml\\)\\'" file)
1663 'mathml)
1664 ((string-match "\\.odf\\'" file)
1665 'odf))))
1667 (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
1668 descp)
1669 "Make a OpenDocument link.
1670 OPT-PLIST is an options list.
1671 TYPE-1 is the device-type of the link (THIS://foo.html).
1672 PATH is the path of the link (http://THIS#location).
1673 FRAGMENT is the fragment part of the link, if any (foo.html#THIS).
1674 DESC is the link description, if any.
1675 ATTR is a string of other attributes of the a element."
1676 (declare (special org-lparse-par-open))
1677 (save-match-data
1678 (let* ((may-inline-p
1679 (and (member type-1 '("http" "https" "file"))
1680 (org-lparse-should-inline-p path descp)
1681 (not fragment)))
1682 (type (if (equal type-1 "id") "file" type-1))
1683 (filename path)
1684 (thefile path)
1685 sec-frag sec-nos)
1686 (cond
1687 ;; check for inlined images
1688 ((and (member type '("file"))
1689 (not fragment)
1690 (org-file-image-p
1691 filename org-export-odt-inline-image-extensions)
1692 (or (eq t org-export-odt-inline-images)
1693 (and org-export-odt-inline-images (not descp))))
1694 (org-odt-format-inline-image thefile))
1695 ;; check for embedded formulas
1696 ((and (member type '("file"))
1697 (not fragment)
1698 (org-odt-is-formula-link-p filename)
1699 (or (not descp)))
1700 (org-odt-format-inline-formula thefile))
1701 ;; code references
1702 ((string= type "coderef")
1703 (let* ((ref fragment)
1704 (lineno-or-ref (cdr (assoc ref org-export-code-refs)))
1705 (desc (and descp desc))
1706 (org-odt-suppress-xref nil)
1707 (href (org-xml-format-href (concat "#coderef-" ref))))
1708 (cond
1709 ((and (numberp lineno-or-ref) (not desc))
1710 (org-odt-format-link lineno-or-ref href))
1711 ((and (numberp lineno-or-ref) desc
1712 (string-match (regexp-quote (concat "(" ref ")")) desc))
1713 (format (replace-match "%s" t t desc)
1714 (org-odt-format-link lineno-or-ref href)))
1716 (setq desc (format
1717 (if (and desc (string-match
1718 (regexp-quote (concat "(" ref ")"))
1719 desc))
1720 (replace-match "%s" t t desc)
1721 (or desc "%s"))
1722 lineno-or-ref))
1723 (org-odt-format-link (org-xml-format-desc desc) href)))))
1724 ;; links to headlines
1725 ((and (string= type "")
1726 (or (not thefile) (string= thefile ""))
1727 (plist-get org-lparse-opt-plist :section-numbers)
1728 (setq sec-frag fragment)
1729 (or (string-match "\\`sec\\(\\(-[0-9]+\\)+\\)" sec-frag)
1730 (and (setq sec-frag
1731 (loop for alias in org-export-target-aliases do
1732 (when (member fragment (cdr alias))
1733 (return (car alias)))))
1734 (string-match "\\`sec\\(\\(-[0-9]+\\)+\\)" sec-frag)))
1735 (setq sec-nos (org-split-string (match-string 1 sec-frag) "-"))
1736 (<= (length sec-nos) (plist-get org-lparse-opt-plist
1737 :headline-levels)))
1738 (let ((org-odt-suppress-xref nil))
1739 (org-odt-format-link sec-nos (concat "#" sec-frag) attr)))
1741 (when (string= type "file")
1742 (setq thefile
1743 (cond
1744 ((file-name-absolute-p path)
1745 (concat "file://" (expand-file-name path)))
1746 (t (org-odt-relocate-relative-path
1747 thefile org-current-export-file)))))
1749 (when (and (member type '("" "http" "https" "file")) fragment)
1750 (setq thefile (concat thefile "#" fragment)))
1752 (setq thefile (org-xml-format-href thefile))
1754 (when (not (member type '("" "file")))
1755 (setq thefile (concat type ":" thefile)))
1757 (let ((org-odt-suppress-xref nil))
1758 (org-odt-format-link
1759 (org-xml-format-desc desc) thefile attr)))))))
1761 (defun org-odt-format-heading (text level &optional id)
1762 (let* ((text (if id (org-odt-format-target text id) text)))
1763 (org-odt-format-tags
1764 '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
1765 "</text:h>") text level level)))
1767 (defun org-odt-format-headline (title extra-targets tags
1768 &optional snumber level)
1769 (concat
1770 (org-lparse-format 'EXTRA-TARGETS extra-targets)
1772 ;; No need to generate section numbers. They are auto-generated by
1773 ;; the application
1775 ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
1776 title
1777 (and tags (concat (org-lparse-format 'SPACES 3)
1778 (org-lparse-format 'ORG-TAGS tags)))))
1780 (defun org-odt-format-anchor (text name &optional class)
1781 (org-odt-format-target text name))
1783 (defun org-odt-format-bookmark (text id)
1784 (if id
1785 (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id)
1786 text))
1788 (defun org-odt-format-target (text id)
1789 (let ((name (concat org-export-odt-bookmark-prefix id)))
1790 (concat
1791 (and id (org-odt-format-tags
1792 "<text:bookmark-start text:name=\"%s\"/>" "" name))
1793 (org-odt-format-bookmark text id)
1794 (and id (org-odt-format-tags
1795 "<text:bookmark-end text:name=\"%s\"/>" "" name)))))
1797 (defun org-odt-format-footnote (n def)
1798 (let ((id (concat "fn" n))
1799 (note-class "footnote")
1800 (par-style "Footnote"))
1801 (org-odt-format-tags
1802 '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
1803 "</text:note>")
1804 (concat
1805 (org-odt-format-tags
1806 '("<text:note-citation>" . "</text:note-citation>")
1808 (org-odt-format-tags
1809 '("<text:note-body>" . "</text:note-body>")
1810 def))
1811 id note-class)))
1813 (defun org-odt-format-footnote-reference (n def refcnt)
1814 (if (= refcnt 1)
1815 (org-odt-format-footnote n def)
1816 (org-odt-format-footnote-ref n)))
1818 (defun org-odt-format-footnote-ref (n)
1819 (let ((note-class "footnote")
1820 (ref-format "text")
1821 (ref-name (concat "fn" n)))
1822 (org-odt-format-tags
1823 '("<text:span text:style-name=\"%s\">" . "</text:span>")
1824 (org-odt-format-tags
1825 '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" . "</text:note-ref>")
1826 n note-class ref-format ref-name)
1827 "OrgSuperscript")))
1829 (defun org-odt-get-image-name (file-name)
1830 (require 'sha1)
1831 (file-relative-name
1832 (expand-file-name
1833 (concat (sha1 file-name) "." (file-name-extension file-name)) "Pictures")))
1835 (defun org-export-odt-format-image (src href)
1836 "Create image tag with source and attributes."
1837 (save-match-data
1838 (let* ((caption (org-find-text-property-in-string 'org-caption src))
1839 (short-caption
1840 (or (org-find-text-property-in-string 'org-caption-shortn src)
1841 caption))
1842 (caption (and caption (org-xml-format-desc caption)))
1843 (short-caption (and short-caption
1844 (org-xml-encode-plain-text short-caption)))
1845 (attr (org-find-text-property-in-string 'org-attributes src))
1846 (label (org-find-text-property-in-string 'org-label src))
1847 (latex-frag (org-find-text-property-in-string
1848 'org-latex-src src))
1849 (category (and latex-frag "__DvipngImage__"))
1850 (attr-plist (org-lparse-get-block-params attr))
1851 (user-frame-anchor
1852 (car (assoc-string (plist-get attr-plist :anchor)
1853 '(("as-char") ("paragraph") ("page")) t)))
1854 (user-frame-style
1855 (and user-frame-anchor (plist-get attr-plist :style)))
1856 (user-frame-attrs
1857 (and user-frame-anchor (plist-get attr-plist :attributes)))
1858 (user-frame-params
1859 (list user-frame-style user-frame-attrs user-frame-anchor))
1860 (embed-as (cond
1861 (latex-frag
1862 (symbol-name
1863 (case (org-find-text-property-in-string
1864 'org-latex-src-embed-type src)
1865 (paragraph 'paragraph)
1866 (t 'as-char))))
1867 (user-frame-anchor)
1868 (t "paragraph")))
1869 (size (org-odt-image-size-from-file
1870 src (plist-get attr-plist :width)
1871 (plist-get attr-plist :height)
1872 (plist-get attr-plist :scale) nil embed-as))
1873 (width (car size)) (height (cdr size)))
1874 (when latex-frag
1875 (setq href (org-propertize href :title "LaTeX Fragment"
1876 :description latex-frag)))
1877 (let ((frame-style-handle (concat (and (or caption label) "Captioned")
1878 embed-as "Image")))
1879 (org-odt-format-entity
1880 frame-style-handle href width height
1881 :caption caption :label label :category category
1882 :short-caption short-caption
1883 :user-frame-params user-frame-params)))))
1885 (defun org-odt-format-object-description (title description)
1886 (concat (and title (org-odt-format-tags
1887 '("<svg:title>" . "</svg:title>")
1888 (org-odt-encode-plain-text title t)))
1889 (and description (org-odt-format-tags
1890 '("<svg:desc>" . "</svg:desc>")
1891 (org-odt-encode-plain-text description t)))))
1893 (defun org-odt-format-frame (text width height style &optional
1894 extra anchor-type)
1895 (let ((frame-attrs
1896 (concat
1897 (if width (format " svg:width=\"%0.2fcm\"" width) "")
1898 (if height (format " svg:height=\"%0.2fcm\"" height) "")
1899 extra
1900 (format " text:anchor-type=\"%s\"" (or anchor-type "paragraph")))))
1901 (org-odt-format-tags
1902 '("<draw:frame draw:style-name=\"%s\"%s>" . "</draw:frame>")
1903 (concat text (org-odt-format-object-description
1904 (get-text-property 0 :title text)
1905 (get-text-property 0 :description text)))
1906 style frame-attrs)))
1908 (defun org-odt-format-textbox (text width height style &optional
1909 extra anchor-type)
1910 (org-odt-format-frame
1911 (org-odt-format-tags
1912 '("<draw:text-box %s>" . "</draw:text-box>")
1913 text (concat (format " fo:min-height=\"%0.2fcm\"" (or height .2))
1914 (unless width
1915 (format " fo:min-width=\"%0.2fcm\"" (or width .2)))))
1916 width nil style extra anchor-type))
1918 (defun org-odt-format-inlinetask (heading content
1919 &optional todo priority tags)
1920 (org-odt-format-stylized-paragraph
1921 nil (org-odt-format-textbox
1922 (concat (org-odt-format-stylized-paragraph
1923 "OrgInlineTaskHeading"
1924 (org-lparse-format
1925 'HEADLINE (concat (org-lparse-format-todo todo) " " heading)
1926 nil tags))
1927 content) nil nil "OrgInlineTaskFrame" " style:rel-width=\"100%\"")))
1929 (defvar org-odt-entity-frame-styles
1930 '(("As-CharImage" "__Figure__" ("OrgInlineImage" nil "as-char"))
1931 ("ParagraphImage" "__Figure__" ("OrgDisplayImage" nil "paragraph"))
1932 ("PageImage" "__Figure__" ("OrgPageImage" nil "page"))
1933 ("CaptionedAs-CharImage" "__Figure__"
1934 ("OrgCaptionedImage"
1935 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
1936 ("OrgInlineImage" nil "as-char"))
1937 ("CaptionedParagraphImage" "__Figure__"
1938 ("OrgCaptionedImage"
1939 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
1940 ("OrgImageCaptionFrame" nil "paragraph"))
1941 ("CaptionedPageImage" "__Figure__"
1942 ("OrgCaptionedImage"
1943 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
1944 ("OrgPageImageCaptionFrame" nil "page"))
1945 ("InlineFormula" "__MathFormula__" ("OrgInlineFormula" nil "as-char"))
1946 ("DisplayFormula" "__MathFormula__" ("OrgDisplayFormula" nil "as-char"))
1947 ("CaptionedDisplayFormula" "__MathFormula__"
1948 ("OrgCaptionedFormula" nil "paragraph")
1949 ("OrgFormulaCaptionFrame" nil "as-char"))))
1951 (defun org-odt-merge-frame-params(default-frame-params user-frame-params)
1952 (if (not user-frame-params) default-frame-params
1953 (assert (= (length default-frame-params) 3))
1954 (assert (= (length user-frame-params) 3))
1955 (loop for user-frame-param in user-frame-params
1956 for default-frame-param in default-frame-params
1957 collect (or user-frame-param default-frame-param))))
1959 (defun* org-odt-format-entity (entity href width height
1960 &key caption label category
1961 user-frame-params short-caption)
1962 (let* ((entity-style (assoc-string entity org-odt-entity-frame-styles t))
1963 default-frame-params frame-params)
1964 (cond
1965 ((not (or caption label))
1966 (setq default-frame-params (nth 2 entity-style))
1967 (setq frame-params (org-odt-merge-frame-params
1968 default-frame-params user-frame-params))
1969 (apply 'org-odt-format-frame href width height frame-params))
1971 (setq default-frame-params (nth 3 entity-style))
1972 (setq frame-params (org-odt-merge-frame-params
1973 default-frame-params user-frame-params))
1974 (apply 'org-odt-format-textbox
1975 (org-odt-format-stylized-paragraph
1976 'illustration
1977 (concat
1978 (apply 'org-odt-format-frame href width height
1979 (let ((entity-style-1 (copy-sequence
1980 (nth 2 entity-style))))
1981 (setcar (cdr entity-style-1)
1982 (concat
1983 (cadr entity-style-1)
1984 (and short-caption
1985 (format " draw:name=\"%s\" "
1986 short-caption))))
1988 entity-style-1))
1989 (org-odt-format-entity-caption
1990 label caption (or category (nth 1 entity-style)))))
1991 width height frame-params)))))
1993 (defvar org-odt-embedded-images-count 0)
1994 (defun org-odt-copy-image-file (path)
1995 "Returns the internal name of the file"
1996 (let* ((image-type (file-name-extension path))
1997 (media-type (format "image/%s" image-type))
1998 (src-file (expand-file-name
1999 path (file-name-directory org-current-export-file)))
2000 (target-dir "Images/")
2001 (target-file
2002 (format "%s%04d.%s" target-dir
2003 (incf org-odt-embedded-images-count) image-type)))
2004 (when (not org-lparse-to-buffer)
2005 (message "Embedding %s as %s ..."
2006 (substring-no-properties path) target-file)
2008 (when (= 1 org-odt-embedded-images-count)
2009 (make-directory target-dir)
2010 (org-odt-create-manifest-file-entry "" target-dir))
2012 (copy-file src-file target-file 'overwrite)
2013 (org-odt-create-manifest-file-entry media-type target-file))
2014 target-file))
2016 (defvar org-export-odt-image-size-probe-method
2017 (append (and (executable-find "identify") '(imagemagick)) ; See Bug#10675
2018 '(emacs fixed))
2019 "Ordered list of methods for determining image sizes.")
2021 (defvar org-export-odt-default-image-sizes-alist
2022 '(("as-char" . (5 . 0.4))
2023 ("paragraph" . (5 . 5)))
2024 "Hardcoded image dimensions one for each of the anchor
2025 methods.")
2027 ;; A4 page size is 21.0 by 29.7 cms
2028 ;; The default page settings has 2cm margin on each of the sides. So
2029 ;; the effective text area is 17.0 by 25.7 cm
2030 (defvar org-export-odt-max-image-size '(17.0 . 20.0)
2031 "Limiting dimensions for an embedded image.")
2033 (defun org-odt-do-image-size (probe-method file &optional dpi anchor-type)
2034 (let* ((dpi (or dpi org-export-odt-pixels-per-inch))
2035 (anchor-type (or anchor-type "paragraph"))
2036 (--pixels-to-cms
2037 (function
2038 (lambda (pixels dpi)
2039 (let* ((cms-per-inch 2.54)
2040 (inches (/ pixels dpi)))
2041 (* cms-per-inch inches)))))
2042 (--size-in-cms
2043 (function
2044 (lambda (size-in-pixels dpi)
2045 (and size-in-pixels
2046 (cons (funcall --pixels-to-cms (car size-in-pixels) dpi)
2047 (funcall --pixels-to-cms (cdr size-in-pixels) dpi)))))))
2048 (case probe-method
2049 (emacs
2050 (let ((size-in-pixels
2051 (ignore-errors ; Emacs could be in batch mode
2052 (clear-image-cache)
2053 (image-size (create-image file) 'pixels))))
2054 (funcall --size-in-cms size-in-pixels dpi)))
2055 (imagemagick
2056 (let ((size-in-pixels
2057 (let ((dim (shell-command-to-string
2058 (format "identify -format \"%%w:%%h\" \"%s\"" file))))
2059 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
2060 (cons (string-to-number (match-string 1 dim))
2061 (string-to-number (match-string 2 dim)))))))
2062 (funcall --size-in-cms size-in-pixels dpi)))
2063 (t (cdr (assoc-string anchor-type
2064 org-export-odt-default-image-sizes-alist))))))
2066 (defun org-odt-image-size-from-file (file &optional user-width
2067 user-height scale dpi embed-as)
2068 (unless (file-name-absolute-p file)
2069 (setq file (expand-file-name
2070 file (file-name-directory org-current-export-file))))
2071 (let* (size width height)
2072 (unless (and user-height user-width)
2073 (loop for probe-method in org-export-odt-image-size-probe-method
2074 until size
2075 do (setq size (org-odt-do-image-size
2076 probe-method file dpi embed-as)))
2077 (or size (error "Cannot determine Image size. Aborting ..."))
2078 (setq width (car size) height (cdr size)))
2079 (cond
2080 (scale
2081 (setq width (* width scale) height (* height scale)))
2082 ((and user-height user-width)
2083 (setq width user-width height user-height))
2084 (user-height
2085 (setq width (* user-height (/ width height)) height user-height))
2086 (user-width
2087 (setq height (* user-width (/ height width)) width user-width))
2088 (t (ignore)))
2089 ;; ensure that an embedded image fits comfortably within a page
2090 (let ((max-width (car org-export-odt-max-image-size))
2091 (max-height (cdr org-export-odt-max-image-size)))
2092 (when (or (> width max-width) (> height max-height))
2093 (let* ((scale1 (/ max-width width))
2094 (scale2 (/ max-height height))
2095 (scale (min scale1 scale2)))
2096 (setq width (* scale width) height (* scale height)))))
2097 (cons width height)))
2099 (defvar org-odt-entity-counts-plist nil
2100 "Plist of running counters of SEQNOs for each of the CATEGORY-NAMEs.
2101 See `org-odt-entity-labels-alist' for known CATEGORY-NAMEs.")
2103 (defvar org-odt-label-styles
2104 '(("math-formula" "%c" "text" "(%n)")
2105 ("math-label" "(%n)" "text" "(%n)")
2106 ("category-and-value" "%e %n: %c" "category-and-value" "%e %n")
2107 ("value" "%e %n: %c" "value" "%n"))
2108 "Specify how labels are applied and referenced.
2109 This is an alist where each element is of the
2110 form (LABEL-STYLE-NAME LABEL-ATTACH-FMT LABEL-REF-MODE
2111 LABEL-REF-FMT).
2113 LABEL-ATTACH-FMT controls how labels and captions are attached to
2114 an entity. It may contain following specifiers - %e, %n and %c.
2115 %e is replaced with the CATEGORY-NAME. %n is replaced with
2116 \"<text:sequence ...> SEQNO </text:sequence>\". %c is replaced
2117 with CAPTION. See `org-odt-format-label-definition'.
2119 LABEL-REF-MODE and LABEL-REF-FMT controls how label references
2120 are generated. The following XML is generated for a label
2121 reference - \"<text:sequence-ref
2122 text:reference-format=\"LABEL-REF-MODE\" ...> LABEL-REF-FMT
2123 </text:sequence-ref>\". LABEL-REF-FMT may contain following
2124 specifiers - %e and %n. %e is replaced with the CATEGORY-NAME.
2125 %n is replaced with SEQNO. See
2126 `org-odt-format-label-reference'.")
2128 (defcustom org-export-odt-category-strings
2129 '(("en" "Table" "Figure" "Equation" "Equation"))
2130 "Specify category strings for various captionable entities.
2131 Captionable entity can be one of a Table, an Embedded Image, a
2132 LaTeX fragment (generated with dvipng) or a Math Formula.
2134 For example, when `org-export-default-language' is \"en\", an
2135 embedded image will be captioned as \"Figure 1: Orgmode Logo\".
2136 If you want the images to be captioned instead as \"Illustration
2137 1: Orgmode Logo\", then modify the entry for \"en\" as shown
2138 below.
2140 \(setq org-export-odt-category-strings
2141 '\(\(\"en\" \"Table\" \"Illustration\"
2142 \"Equation\" \"Equation\"\)\)\)"
2143 :group 'org-export-odt
2144 :version "24.1"
2145 :type '(repeat (list (string :tag "Language tag")
2146 (choice :tag "Table"
2147 (const :tag "Use Default" nil)
2148 (string :tag "Category string"))
2149 (choice :tag "Figure"
2150 (const :tag "Use Default" nil)
2151 (string :tag "Category string"))
2152 (choice :tag "Math Formula"
2153 (const :tag "Use Default" nil)
2154 (string :tag "Category string"))
2155 (choice :tag "Dvipng Image"
2156 (const :tag "Use Default" nil)
2157 (string :tag "Category string")))))
2159 (defvar org-odt-category-map-alist
2160 '(("__Table__" "Table" "value")
2161 ("__Figure__" "Illustration" "value")
2162 ("__MathFormula__" "Text" "math-formula")
2163 ("__DvipngImage__" "Equation" "value")
2164 ;; ("__Table__" "Table" "category-and-value")
2165 ;; ("__Figure__" "Figure" "category-and-value")
2166 ;; ("__DvipngImage__" "Equation" "category-and-value")
2168 "Map a CATEGORY-HANDLE to OD-VARIABLE and LABEL-STYLE.
2169 This is a list where each entry is of the form \\(CATEGORY-HANDLE
2170 OD-VARIABLE LABEL-STYLE\\). CATEGORY_HANDLE identifies the
2171 captionable entity in question. OD-VARIABLE is the OpenDocument
2172 sequence counter associated with the entity. These counters are
2173 declared within
2174 \"<text:sequence-decls>...</text:sequence-decls>\" block of
2175 `org-export-odt-content-template-file'. LABEL-STYLE is a key
2176 into `org-odt-label-styles' and specifies how a given entity
2177 should be captioned and referenced.
2179 The position of a CATEGORY-HANDLE in this list is used as an
2180 index in to per-language entry for
2181 `org-export-odt-category-strings' to retrieve a CATEGORY-NAME.
2182 This CATEGORY-NAME is then used for qualifying the user-specified
2183 captions on export.")
2185 (defun org-odt-add-label-definition (label default-category)
2186 "Create an entry in `org-odt-entity-labels-alist' and return it."
2187 (let* ((label-props (assoc default-category org-odt-category-map-alist))
2188 ;; identify the sequence number
2189 (counter (nth 1 label-props))
2190 (sequence-var (intern counter))
2191 (seqno (1+ (or (plist-get org-odt-entity-counts-plist sequence-var)
2192 0)))
2193 ;; assign an internal label, if user has not provided one
2194 (label (if label (substring-no-properties label)
2195 (format "%s-%s" default-category seqno)))
2196 ;; identify label style
2197 (label-style (nth 2 label-props))
2198 ;; grok language setting
2199 (en-strings (assoc-default "en" org-export-odt-category-strings))
2200 (lang (plist-get org-lparse-opt-plist :language))
2201 (lang-strings (assoc-default lang org-export-odt-category-strings))
2202 ;; retrieve localized category sting
2203 (pos (- (length org-odt-category-map-alist)
2204 (length (memq label-props org-odt-category-map-alist))))
2205 (category (or (nth pos lang-strings) (nth pos en-strings)))
2206 (label-props (list label category counter seqno label-style)))
2207 ;; synchronize internal counters
2208 (setq org-odt-entity-counts-plist
2209 (plist-put org-odt-entity-counts-plist sequence-var seqno))
2210 ;; stash label properties for later retrieval
2211 (push label-props org-odt-entity-labels-alist)
2212 label-props))
2214 (defun org-odt-format-label-definition (caption label category counter
2215 seqno label-style)
2216 (assert label)
2217 (format-spec
2218 (cadr (assoc-string label-style org-odt-label-styles t))
2219 `((?e . ,category)
2220 (?n . ,(org-odt-format-tags
2221 '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" . "</text:sequence>")
2222 (format "%d" seqno) label counter counter))
2223 (?c . ,(or caption "")))))
2225 (defun org-odt-format-label-reference (label category counter
2226 seqno label-style)
2227 (assert label)
2228 (save-match-data
2229 (let* ((fmt (cddr (assoc-string label-style org-odt-label-styles t)))
2230 (fmt1 (car fmt))
2231 (fmt2 (cadr fmt)))
2232 (org-odt-format-tags
2233 '("<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">"
2234 . "</text:sequence-ref>")
2235 (format-spec fmt2 `((?e . ,category)
2236 (?n . ,(format "%d" seqno)))) fmt1 label))))
2238 (defun org-odt-fixup-label-references ()
2239 (goto-char (point-min))
2240 (while (re-search-forward
2241 "<text:sequence-ref text:ref-name=\"\\([^\"]+\\)\">[ \t\n]*</text:sequence-ref>"
2242 nil t)
2243 (let* ((label (match-string 1))
2244 (label-def (assoc label org-odt-entity-labels-alist))
2245 (rpl (and label-def
2246 (apply 'org-odt-format-label-reference label-def))))
2247 (if rpl (replace-match rpl t t)
2248 (org-lparse-warn
2249 (format "Unable to resolve reference to label \"%s\"" label))))))
2251 (defun org-odt-format-entity-caption (label caption category)
2252 (if (not (or label caption)) ""
2253 (apply 'org-odt-format-label-definition caption
2254 (org-odt-add-label-definition label category))))
2256 (defun org-odt-format-tags (tag text &rest args)
2257 (let ((prefix (when org-lparse-encode-pending "@"))
2258 (suffix (when org-lparse-encode-pending "@")))
2259 (apply 'org-lparse-format-tags tag text prefix suffix args)))
2261 (defvar org-odt-manifest-file-entries nil)
2262 (defun org-odt-init-outfile (filename)
2263 (unless (executable-find "zip")
2264 ;; Not at all OSes ship with zip by default
2265 (error "Executable \"zip\" needed for creating OpenDocument files"))
2267 (let* ((content-file (expand-file-name "content.xml" org-odt-zip-dir)))
2268 ;; init conten.xml
2269 (require 'nxml-mode)
2270 (let ((nxml-auto-insert-xml-declaration-flag nil))
2271 (find-file-noselect content-file t))
2273 ;; reset variables
2274 (setq org-odt-manifest-file-entries nil
2275 org-odt-embedded-images-count 0
2276 org-odt-embedded-formulas-count 0
2277 org-odt-entity-labels-alist nil
2278 org-odt-list-stack-stashed nil
2279 org-odt-automatic-styles nil
2280 org-odt-object-counters nil
2281 org-odt-entity-counts-plist nil)
2282 content-file))
2284 (defcustom org-export-odt-prettify-xml nil
2285 "Specify whether or not the xml output should be prettified.
2286 When this option is turned on, `indent-region' is run on all
2287 component xml buffers before they are saved. Turn this off for
2288 regular use. Turn this on if you need to examine the xml
2289 visually."
2290 :group 'org-export-odt
2291 :version "24.1"
2292 :type 'boolean)
2294 (defvar hfy-user-sheet-assoc) ; bound during org-do-lparse
2295 (defun org-odt-save-as-outfile (target opt-plist)
2296 ;; write automatic styles
2297 (org-odt-write-automatic-styles)
2299 ;; write meta file
2300 (org-odt-update-meta-file opt-plist)
2302 ;; write styles file
2303 (when (equal org-lparse-backend 'odt)
2304 (org-odt-update-styles-file opt-plist))
2306 ;; create mimetype file
2307 (let ((mimetype (org-odt-write-mimetype-file org-lparse-backend)))
2308 (org-odt-create-manifest-file-entry mimetype "/" "1.2"))
2310 ;; create a manifest entry for content.xml
2311 (org-odt-create-manifest-file-entry "text/xml" "content.xml")
2313 ;; write out the manifest entries before zipping
2314 (org-odt-write-manifest-file)
2316 (let ((xml-files '("mimetype" "META-INF/manifest.xml" "content.xml"
2317 "meta.xml")))
2318 (when (equal org-lparse-backend 'odt)
2319 (push "styles.xml" xml-files))
2321 ;; save all xml files
2322 (mapc (lambda (file)
2323 (with-current-buffer
2324 (find-file-noselect (expand-file-name file) t)
2325 ;; prettify output if needed
2326 (when org-export-odt-prettify-xml
2327 (indent-region (point-min) (point-max)))
2328 (save-buffer 0)))
2329 xml-files)
2331 (let* ((target-name (file-name-nondirectory target))
2332 (target-dir (file-name-directory target))
2333 (cmds `(("zip" "-mX0" ,target-name "mimetype")
2334 ("zip" "-rmTq" ,target-name "."))))
2335 (when (file-exists-p target)
2336 ;; FIXME: If the file is locked this throws a cryptic error
2337 (delete-file target))
2339 (let ((coding-system-for-write 'no-conversion) exitcode err-string)
2340 (message "Creating odt file...")
2341 (mapc
2342 (lambda (cmd)
2343 (message "Running %s" (mapconcat 'identity cmd " "))
2344 (setq err-string
2345 (with-output-to-string
2346 (setq exitcode
2347 (apply 'call-process (car cmd)
2348 nil standard-output nil (cdr cmd)))))
2349 (or (zerop exitcode)
2350 (ignore (message "%s" err-string))
2351 (error "Unable to create odt file (%S)" exitcode)))
2352 cmds))
2354 ;; move the file from outdir to target-dir
2355 (rename-file target-name target-dir)))
2357 (message "Created %s" target)
2358 (set-buffer (find-file-noselect target t)))
2360 (defconst org-odt-manifest-file-entry-tag
2362 <manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
2364 (defun org-odt-create-manifest-file-entry (&rest args)
2365 (push args org-odt-manifest-file-entries))
2367 (defun org-odt-write-manifest-file ()
2368 (make-directory "META-INF")
2369 (let ((manifest-file (expand-file-name "META-INF/manifest.xml")))
2370 (with-current-buffer
2371 (let ((nxml-auto-insert-xml-declaration-flag nil))
2372 (find-file-noselect manifest-file t))
2373 (insert
2374 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
2375 <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
2376 (mapc
2377 (lambda (file-entry)
2378 (let* ((version (nth 2 file-entry))
2379 (extra (if version
2380 (format " manifest:version=\"%s\"" version)
2381 "")))
2382 (insert
2383 (format org-odt-manifest-file-entry-tag
2384 (nth 0 file-entry) (nth 1 file-entry) extra))))
2385 org-odt-manifest-file-entries)
2386 (insert "\n</manifest:manifest>"))))
2388 (defun org-odt-update-meta-file (opt-plist)
2389 (let ((date (org-odt-format-date (plist-get opt-plist :date)))
2390 (author (or (plist-get opt-plist :author) ""))
2391 (email (plist-get opt-plist :email))
2392 (keywords (plist-get opt-plist :keywords))
2393 (description (plist-get opt-plist :description))
2394 (title (plist-get opt-plist :title)))
2395 (write-region
2396 (concat
2397 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
2398 <office:document-meta
2399 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
2400 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
2401 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
2402 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
2403 xmlns:ooo=\"http://openoffice.org/2004/office\"
2404 office:version=\"1.2\">
2405 <office:meta>" "\n"
2406 (org-odt-format-author)
2407 (org-odt-format-tags
2408 '("\n<meta:initial-creator>" . "</meta:initial-creator>") author)
2409 (org-odt-format-tags '("\n<dc:date>" . "</dc:date>") date)
2410 (org-odt-format-tags
2411 '("\n<meta:creation-date>" . "</meta:creation-date>") date)
2412 (org-odt-format-tags '("\n<meta:generator>" . "</meta:generator>")
2413 (when org-export-creator-info
2414 (format "Org-%s/Emacs-%s"
2415 (org-version)
2416 emacs-version)))
2417 (org-odt-format-tags '("\n<meta:keyword>" . "</meta:keyword>") keywords)
2418 (org-odt-format-tags '("\n<dc:subject>" . "</dc:subject>") description)
2419 (org-odt-format-tags '("\n<dc:title>" . "</dc:title>") title)
2420 "\n"
2421 " </office:meta>" "</office:document-meta>")
2422 nil (expand-file-name "meta.xml")))
2424 ;; create a manifest entry for meta.xml
2425 (org-odt-create-manifest-file-entry "text/xml" "meta.xml"))
2427 (defun org-odt-update-styles-file (opt-plist)
2428 ;; write styles file
2429 (let ((styles-file (plist-get opt-plist :odt-styles-file)))
2430 (org-odt-copy-styles-file (and styles-file
2431 (read (org-trim styles-file)))))
2433 ;; Update styles.xml - take care of outline numbering
2434 (with-current-buffer
2435 (find-file-noselect (expand-file-name "styles.xml") t)
2436 ;; Don't make automatic backup of styles.xml file. This setting
2437 ;; prevents the backed-up styles.xml file from being zipped in to
2438 ;; odt file. This is more of a hackish fix. Better alternative
2439 ;; would be to fix the zip command so that the output odt file
2440 ;; includes only the needed files and excludes any auto-generated
2441 ;; extra files like backups and auto-saves etc etc. Note that
2442 ;; currently the zip command zips up the entire temp directory so
2443 ;; that any auto-generated files created under the hood ends up in
2444 ;; the resulting odt file.
2445 (set (make-local-variable 'backup-inhibited) t)
2447 ;; Import local setting of `org-export-with-section-numbers'
2448 (org-lparse-bind-local-variables opt-plist)
2449 (org-odt-configure-outline-numbering
2450 (if org-export-with-section-numbers org-export-headline-levels 0)))
2452 ;; Write custom styles for source blocks
2453 (org-odt-insert-custom-styles-for-srcblocks
2454 (mapconcat
2455 (lambda (style)
2456 (format " %s\n" (cddr style)))
2457 hfy-user-sheet-assoc "")))
2459 (defun org-odt-write-mimetype-file (format)
2460 ;; create mimetype file
2461 (let ((mimetype
2462 (case format
2463 (odt "application/vnd.oasis.opendocument.text")
2464 (odf "application/vnd.oasis.opendocument.formula")
2465 (t (error "Unknown OpenDocument backend %S" org-lparse-backend)))))
2466 (write-region mimetype nil (expand-file-name "mimetype"))
2467 mimetype))
2469 (defun org-odt-finalize-outfile ()
2470 (org-odt-delete-empty-paragraphs))
2472 (defun org-odt-delete-empty-paragraphs ()
2473 (goto-char (point-min))
2474 (let ((open "<text:p[^>]*>")
2475 (close "</text:p>"))
2476 (while (re-search-forward (format "%s[ \r\n\t]*%s" open close) nil t)
2477 (replace-match ""))))
2479 (defcustom org-export-odt-convert-processes
2480 '(("LibreOffice"
2481 "soffice --headless --convert-to %f%x --outdir %d %i")
2482 ("unoconv"
2483 "unoconv -f %f -o %d %i"))
2484 "Specify a list of document converters and their usage.
2485 The converters in this list are offered as choices while
2486 customizing `org-export-odt-convert-process'.
2488 This variable is a list where each element is of the
2489 form (CONVERTER-NAME CONVERTER-CMD). CONVERTER-NAME is the name
2490 of the converter. CONVERTER-CMD is the shell command for the
2491 converter and can contain format specifiers. These format
2492 specifiers are interpreted as below:
2494 %i input file name in full
2495 %I input file name as a URL
2496 %f format of the output file
2497 %o output file name in full
2498 %O output file name as a URL
2499 %d output dir in full
2500 %D output dir as a URL.
2501 %x extra options as set in `org-export-odt-convert-capabilities'."
2502 :group 'org-export-odt
2503 :version "24.1"
2504 :type
2505 '(choice
2506 (const :tag "None" nil)
2507 (alist :tag "Converters"
2508 :key-type (string :tag "Converter Name")
2509 :value-type (group (string :tag "Command line")))))
2511 (defcustom org-export-odt-convert-process "LibreOffice"
2512 "Use this converter to convert from \"odt\" format to other formats.
2513 During customization, the list of converter names are populated
2514 from `org-export-odt-convert-processes'."
2515 :group 'org-export-odt
2516 :version "24.1"
2517 :type '(choice :convert-widget
2518 (lambda (w)
2519 (apply 'widget-convert (widget-type w)
2520 (eval (car (widget-get w :args)))))
2521 `((const :tag "None" nil)
2522 ,@(mapcar (lambda (c)
2523 `(const :tag ,(car c) ,(car c)))
2524 org-export-odt-convert-processes))))
2526 (defcustom org-export-odt-convert-capabilities
2527 '(("Text"
2528 ("odt" "ott" "doc" "rtf" "docx")
2529 (("pdf" "pdf") ("odt" "odt") ("rtf" "rtf") ("ott" "ott")
2530 ("doc" "doc" ":\"MS Word 97\"") ("docx" "docx") ("html" "html")))
2531 ("Web"
2532 ("html")
2533 (("pdf" "pdf") ("odt" "odt") ("html" "html")))
2534 ("Spreadsheet"
2535 ("ods" "ots" "xls" "csv" "xlsx")
2536 (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv") ("ods" "ods")
2537 ("xls" "xls") ("xlsx" "xlsx")))
2538 ("Presentation"
2539 ("odp" "otp" "ppt" "pptx")
2540 (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("otp" "otp") ("ppt" "ppt")
2541 ("pptx" "pptx") ("odg" "odg"))))
2542 "Specify input and output formats of `org-export-odt-convert-process'.
2543 More correctly, specify the set of input and output formats that
2544 the user is actually interested in.
2546 This variable is an alist where each element is of the
2547 form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
2548 INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
2549 alist where each element is of the form (OUTPUT-FMT
2550 OUTPUT-FILE-EXTENSION EXTRA-OPTIONS).
2552 The variable is interpreted as follows:
2553 `org-export-odt-convert-process' can take any document that is in
2554 INPUT-FMT-LIST and produce any document that is in the
2555 OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
2556 OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
2557 serves dual purposes:
2558 - It is used for populating completion candidates during
2559 `org-export-odt-convert' commands.
2560 - It is used as the value of \"%f\" specifier in
2561 `org-export-odt-convert-process'.
2563 EXTRA-OPTIONS is used as the value of \"%x\" specifier in
2564 `org-export-odt-convert-process'.
2566 DOCUMENT-CLASS is used to group a set of file formats in
2567 INPUT-FMT-LIST in to a single class.
2569 Note that this variable inherently captures how LibreOffice based
2570 converters work. LibreOffice maps documents of various formats
2571 to classes like Text, Web, Spreadsheet, Presentation etc and
2572 allow document of a given class (irrespective of it's source
2573 format) to be converted to any of the export formats associated
2574 with that class.
2576 See default setting of this variable for an typical
2577 configuration."
2578 :group 'org-export-odt
2579 :version "24.1"
2580 :type
2581 '(choice
2582 (const :tag "None" nil)
2583 (alist :tag "Capabilities"
2584 :key-type (string :tag "Document Class")
2585 :value-type
2586 (group (repeat :tag "Input formats" (string :tag "Input format"))
2587 (alist :tag "Output formats"
2588 :key-type (string :tag "Output format")
2589 :value-type
2590 (group (string :tag "Output file extension")
2591 (choice
2592 (const :tag "None" nil)
2593 (string :tag "Extra options"))))))))
2595 (declare-function org-create-math-formula "org"
2596 (latex-frag &optional mathml-file))
2598 ;;;###autoload
2599 (defun org-export-odt-convert (&optional in-file out-fmt prefix-arg)
2600 "Convert IN-FILE to format OUT-FMT using a command line converter.
2601 IN-FILE is the file to be converted. If unspecified, it defaults
2602 to variable `buffer-file-name'. OUT-FMT is the desired output
2603 format. Use `org-export-odt-convert-process' as the converter.
2604 If PREFIX-ARG is non-nil then the newly converted file is opened
2605 using `org-open-file'."
2606 (interactive
2607 (append (org-lparse-convert-read-params) current-prefix-arg))
2608 (org-lparse-do-convert in-file out-fmt prefix-arg))
2610 (defun org-odt-get (what &optional opt-plist)
2611 (case what
2612 (BACKEND 'odt)
2613 (EXPORT-DIR (org-export-directory :html opt-plist))
2614 (FILE-NAME-EXTENSION "odt")
2615 (EXPORT-BUFFER-NAME "*Org ODT Export*")
2616 (ENTITY-CONTROL org-odt-entity-control-callbacks-alist)
2617 (ENTITY-FORMAT org-odt-entity-format-callbacks-alist)
2618 (INIT-METHOD 'org-odt-init-outfile)
2619 (FINAL-METHOD 'org-odt-finalize-outfile)
2620 (SAVE-METHOD 'org-odt-save-as-outfile)
2621 (CONVERT-METHOD
2622 (and org-export-odt-convert-process
2623 (cadr (assoc-string org-export-odt-convert-process
2624 org-export-odt-convert-processes t))))
2625 (CONVERT-CAPABILITIES
2626 (and org-export-odt-convert-process
2627 (cadr (assoc-string org-export-odt-convert-process
2628 org-export-odt-convert-processes t))
2629 org-export-odt-convert-capabilities))
2630 (TOPLEVEL-HLEVEL 1)
2631 (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps)
2632 (INLINE-IMAGES 'maybe)
2633 (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
2634 (PLAIN-TEXT-MAP '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
2635 (TABLE-FIRST-COLUMN-AS-LABELS nil)
2636 (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY "," 'superscript))
2637 (CODING-SYSTEM-FOR-WRITE 'utf-8)
2638 (CODING-SYSTEM-FOR-SAVE 'utf-8)
2639 (t (error "Unknown property: %s" what))))
2641 (defvar org-lparse-latex-fragment-fallback) ; set by org-do-lparse
2642 (defun org-export-odt-do-preprocess-latex-fragments ()
2643 "Convert LaTeX fragments to images."
2644 (let* ((latex-frag-opt (plist-get org-lparse-opt-plist :LaTeX-fragments))
2645 (latex-frag-opt ; massage the options
2646 (or (and (member latex-frag-opt '(mathjax t))
2647 (not (and (fboundp 'org-format-latex-mathml-available-p)
2648 (org-format-latex-mathml-available-p)))
2649 (prog1 org-lparse-latex-fragment-fallback
2650 (org-lparse-warn
2651 (concat
2652 "LaTeX to MathML converter not available. "
2653 (format "Using %S instead."
2654 org-lparse-latex-fragment-fallback)))))
2655 latex-frag-opt))
2656 cache-dir display-msg)
2657 (cond
2658 ((eq latex-frag-opt 'dvipng)
2659 (setq cache-dir "ltxpng/")
2660 (setq display-msg "Creating LaTeX image %s"))
2661 ((member latex-frag-opt '(mathjax t))
2662 (setq latex-frag-opt 'mathml)
2663 (setq cache-dir "ltxmathml/")
2664 (setq display-msg "Creating MathML formula %s")))
2665 (when (and org-current-export-file)
2666 (org-format-latex
2667 (concat cache-dir (file-name-sans-extension
2668 (file-name-nondirectory org-current-export-file)))
2669 org-current-export-dir nil display-msg
2670 nil nil latex-frag-opt))))
2672 (defadvice org-format-latex-as-mathml
2673 (after org-odt-protect-latex-fragment activate)
2674 "Encode LaTeX fragment as XML.
2675 Do this when translation to MathML fails."
2676 (when (or (not (> (length ad-return-value) 0))
2677 (get-text-property 0 'org-protected ad-return-value))
2678 (setq ad-return-value
2679 (org-propertize (org-odt-encode-plain-text (ad-get-arg 0))
2680 'org-protected t))))
2682 (defun org-export-odt-preprocess-latex-fragments ()
2683 (when (equal org-export-current-backend 'odt)
2684 (org-export-odt-do-preprocess-latex-fragments)))
2686 (defun org-export-odt-preprocess-label-references ()
2687 (goto-char (point-min))
2688 (let (label label-components category value pretty-label)
2689 (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t)
2690 (org-if-unprotected-at (match-beginning 1)
2691 (replace-match
2692 (let ((org-lparse-encode-pending t)
2693 (label (match-string 1)))
2694 ;; markup generated below is mostly an eye-candy. At
2695 ;; pre-processing stage, there is no information on which
2696 ;; entity a label reference points to. The actual markup
2697 ;; is generated as part of `org-odt-fixup-label-references'
2698 ;; which gets called at the fag end of export. By this
2699 ;; time we would have seen and collected all the label
2700 ;; definitions in `org-odt-entity-labels-alist'.
2701 (org-odt-format-tags
2702 '("<text:sequence-ref text:ref-name=\"%s\">" .
2703 "</text:sequence-ref>")
2704 "" (org-add-props label '(org-protected t)))) t t)))))
2706 ;; process latex fragments as part of
2707 ;; `org-export-preprocess-after-blockquote-hook'. Note that this hook
2708 ;; is the one that is closest and well before the call to
2709 ;; `org-export-attach-captions-and-attributes' in
2710 ;; `org-export-preprocess-string'. The above arrangement permits
2711 ;; captions, labels and attributes to be attached to png images
2712 ;; generated out of latex equations.
2713 (add-hook 'org-export-preprocess-after-blockquote-hook
2714 'org-export-odt-preprocess-latex-fragments)
2716 (defun org-export-odt-preprocess (parameters)
2717 (org-export-odt-preprocess-label-references))
2719 (declare-function archive-zip-extract "arc-mode.el" (archive name))
2720 (defun org-odt-zip-extract-one (archive member &optional target)
2721 (require 'arc-mode)
2722 (let* ((target (or target default-directory))
2723 (archive (expand-file-name archive))
2724 (archive-zip-extract
2725 (list "unzip" "-qq" "-o" "-d" target))
2726 exit-code command-output)
2727 (setq command-output
2728 (with-temp-buffer
2729 (setq exit-code (archive-zip-extract archive member))
2730 (buffer-string)))
2731 (unless (zerop exit-code)
2732 (message command-output)
2733 (error "Extraction failed"))))
2735 (defun org-odt-zip-extract (archive members &optional target)
2736 (when (atom members) (setq members (list members)))
2737 (mapc (lambda (member)
2738 (org-odt-zip-extract-one archive member target))
2739 members))
2741 (defun org-odt-copy-styles-file (&optional styles-file)
2742 ;; Non-availability of styles.xml is not a critical error. For now
2743 ;; throw an error purely for aesthetic reasons.
2744 (setq styles-file (or styles-file
2745 org-export-odt-styles-file
2746 (expand-file-name "OrgOdtStyles.xml"
2747 org-odt-styles-dir)
2748 (error "org-odt: Missing styles file?")))
2749 (cond
2750 ((listp styles-file)
2751 (let ((archive (nth 0 styles-file))
2752 (members (nth 1 styles-file)))
2753 (org-odt-zip-extract archive members)
2754 (mapc
2755 (lambda (member)
2756 (when (org-file-image-p member)
2757 (let* ((image-type (file-name-extension member))
2758 (media-type (format "image/%s" image-type)))
2759 (org-odt-create-manifest-file-entry media-type member))))
2760 members)))
2761 ((and (stringp styles-file) (file-exists-p styles-file))
2762 (let ((styles-file-type (file-name-extension styles-file)))
2763 (cond
2764 ((string= styles-file-type "xml")
2765 (copy-file styles-file "styles.xml" t))
2766 ((member styles-file-type '("odt" "ott"))
2767 (org-odt-zip-extract styles-file "styles.xml")))))
2769 (error (format "Invalid specification of styles.xml file: %S"
2770 org-export-odt-styles-file))))
2772 ;; create a manifest entry for styles.xml
2773 (org-odt-create-manifest-file-entry "text/xml" "styles.xml"))
2775 (defun org-odt-configure-outline-numbering (level)
2776 "Outline numbering is retained only upto LEVEL.
2777 To disable outline numbering pass a LEVEL of 0."
2778 (goto-char (point-min))
2779 (let ((regex
2780 "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
2781 (replacement
2782 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
2783 (while (re-search-forward regex nil t)
2784 (when (> (string-to-number (match-string 2)) level)
2785 (replace-match replacement t nil))))
2786 (save-buffer 0))
2788 ;;;###autoload
2789 (defun org-export-as-odf (latex-frag &optional odf-file)
2790 "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
2791 Use `org-create-math-formula' to convert LATEX-FRAG first to
2792 MathML. When invoked as an interactive command, use
2793 `org-latex-regexps' to infer LATEX-FRAG from currently active
2794 region. If no LaTeX fragments are found, prompt for it. Push
2795 MathML source to kill ring, if `org-export-copy-to-kill-ring' is
2796 non-nil."
2797 (interactive
2798 `(,(let (frag)
2799 (setq frag (and (setq frag (and (region-active-p)
2800 (buffer-substring (region-beginning)
2801 (region-end))))
2802 (loop for e in org-latex-regexps
2803 thereis (when (string-match (nth 1 e) frag)
2804 (match-string (nth 2 e) frag)))))
2805 (read-string "LaTeX Fragment: " frag nil frag))
2806 ,(let ((odf-filename (expand-file-name
2807 (concat
2808 (file-name-sans-extension
2809 (or (file-name-nondirectory buffer-file-name)))
2810 "." "odf")
2811 (file-name-directory buffer-file-name))))
2812 (read-file-name "ODF filename: " nil odf-filename nil
2813 (file-name-nondirectory odf-filename)))))
2814 (org-odt-cleanup-xml-buffers
2815 (let* ((org-lparse-backend 'odf)
2816 org-lparse-opt-plist
2817 (filename (or odf-file
2818 (expand-file-name
2819 (concat
2820 (file-name-sans-extension
2821 (or (file-name-nondirectory buffer-file-name)))
2822 "." "odf")
2823 (file-name-directory buffer-file-name))))
2824 (buffer (find-file-noselect (org-odt-init-outfile filename)))
2825 (coding-system-for-write 'utf-8)
2826 (save-buffer-coding-system 'utf-8))
2827 (set-buffer buffer)
2828 (set-buffer-file-coding-system coding-system-for-write)
2829 (let ((mathml (org-create-math-formula latex-frag)))
2830 (unless mathml (error "No Math formula created"))
2831 (insert mathml)
2832 (or (org-export-push-to-kill-ring
2833 (upcase (symbol-name org-lparse-backend)))
2834 (message "Exporting... done")))
2835 (org-odt-save-as-outfile filename nil))))
2837 ;;;###autoload
2838 (defun org-export-as-odf-and-open ()
2839 "Export LaTeX fragment as OpenDocument formula and immediately open it.
2840 Use `org-export-as-odf' to read LaTeX fragment and OpenDocument
2841 formula file."
2842 (interactive)
2843 (org-lparse-and-open
2844 nil nil nil (call-interactively 'org-export-as-odf)))
2846 (provide 'org-odt)
2848 ;;; org-odt.el ends here