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