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