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