1 ;;; org-odt.el --- OpenDocument Text exporter for Org-mode
3 ;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
5 ;; Author: Jambunathan K <kjambunathan at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
31 (defgroup org-export-odt nil
32 "Options specific for ODT export of Org-mode files."
37 (defvar org-lparse-dyn-first-heading-pos
) ; let bound during org-do-lparse
38 (defun org-odt-insert-toc ()
39 (goto-char (point-min))
42 "\\(<text:p [^>]*>\\)?\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*\\(</text:p>\\)?"
46 (goto-char org-lparse-dyn-first-heading-pos
)))
47 (insert (org-odt-format-toc)))
49 (defun org-odt-end-export ()
51 (org-odt-fixup-label-references)
53 ;; remove empty paragraphs
54 (goto-char (point-min))
55 (while (re-search-forward
56 "<text:p\\( text:style-name=\"Text_20_body\"\\)?>[ \r\n\t]*</text:p>"
59 (goto-char (point-min))
61 ;; Convert whitespace place holders
62 (goto-char (point-min))
64 (while (setq beg
(next-single-property-change (point) 'org-whitespace
))
65 (setq n
(get-text-property beg
'org-whitespace
)
66 end
(next-single-property-change beg
'org-whitespace
))
68 (delete-region beg end
)
69 (insert (format "<span style=\"visibility:hidden;\">%s</span>"
70 (make-string n ?x
)))))
72 ;; Remove empty lines at the beginning of the file.
73 (goto-char (point-min))
74 (when (looking-at "\\s-+\n") (replace-match ""))
76 ;; Remove display properties
77 (remove-text-properties (point-min) (point-max) '(display t
)))
79 (defvar org-odt-suppress-xref nil
)
80 (defconst org-export-odt-special-string-regexps
81 '(("\\\\-" .
"­\\1") ; shy
82 ("---\\([^-]\\)" .
"—\\1") ; mdash
83 ("--\\([^-]\\)" .
"–\\1") ; ndash
84 ("\\.\\.\\." .
"…")) ; hellip
85 "Regular expressions for special string conversion.")
87 (defcustom org-export-odt-schema-dir
88 (let* ((d (expand-file-name "../" (org-find-library-dir "org-odt.el")))
89 (schema-dir-list (list (concat d
"etc/schema/"))) ;; ELPA and Git
95 (when (and (file-readable-p
97 "od-manifest-schema-v1.2-cs01.rnc" sd
))
100 "od-schema-v1.2-cs01.rnc" sd
))
107 (message "No OpenDocument schema files installed")))
108 "Directory that contains OpenDocument schema files.
110 This directory contains:
111 1. rnc files for OpenDocument schema
112 2. a \"schemas.xml\" file that specifies locating rules needed
113 for auto validation of OpenDocument XML files.
115 Use the customize interface to set this variable. This ensures
116 that `rng-schema-locating-files' is updated and auto-validation
117 of OpenDocument XML takes place based on the value
118 `rng-nxml-auto-validate-flag'.
120 The OASIS schema files are available only in the Org's private
121 git repository. It is *not* bundled with GNU ELPA tar or
122 standard Emacs distribution."
124 (const :tag
"Not set" nil
)
125 (directory :tag
"Schema directory"))
126 :group
'org-export-odt
130 "Set `org-export-odt-schema-dir'.
131 Also add it to `rng-schema-locating-files'."
132 (let ((schema-dir value
))
136 (expand-file-name "od-manifest-schema-v1.2-cs01.rnc" schema-dir
))
138 (expand-file-name "od-schema-v1.2-cs01.rnc" schema-dir
))
140 (expand-file-name "schemas.xml" schema-dir
)))
143 (message "No OpenDocument schema files" value
))
145 (when org-export-odt-schema-dir
146 (eval-after-load 'rng-loc
147 '(add-to-list 'rng-schema-locating-files
148 (expand-file-name "schemas.xml"
149 org-export-odt-schema-dir
))))))
151 (defconst org-odt-styles-dir
152 (let* ((d (expand-file-name "../" (org-find-library-dir "org-odt.el")))
153 (styles-dir-list (list
154 (concat d
"etc/styles/") ;; ELPA and Git
155 (concat d
"etc/org/")))
161 (when (and (file-readable-p
163 "OrgOdtContentTemplate.xml" sd
))
166 "OrgOdtStyles.xml" sd
)))
170 (error "Cannot find factory styles files for Org ODT, aborting")))
171 "Directory that holds auxiliary XML files used by the ODT exporter.
173 This directory contains the following XML files -
174 \"OrgOdtStyles.xml\" and \"OrgOdtContentTemplate.xml\".
175 These XML files are used as the default values of
176 `org-export-odt-styles-file' and
177 `org-export-odt-content-template-file'.")
179 (defvar org-odt-file-extensions
180 '(("odt" .
"OpenDocument Text")
181 ("ott" .
"OpenDocument Text Template")
182 ("odm" .
"OpenDocument Master Document")
183 ("ods" .
"OpenDocument Spreadsheet")
184 ("ots" .
"OpenDocument Spreadsheet Template")
185 ("odg" .
"OpenDocument Drawing (Graphics)")
186 ("otg" .
"OpenDocument Drawing Template")
187 ("odp" .
"OpenDocument Presentation")
188 ("otp" .
"OpenDocument Presentation Template")
189 ("odi" .
"OpenDocument Image")
190 ("odf" .
"OpenDocument Formula")
191 ("odc" .
"OpenDocument Chart")))
195 ;; Let Emacs open all OpenDocument files in archive mode
196 (add-to-list 'auto-mode-alist
197 (cons (concat "\\." (car desc
) "\\'") 'archive-mode
)))
198 org-odt-file-extensions
)
200 ;; register the odt exporter with the pre-processor
201 (add-to-list 'org-export-backends
'odt
)
203 ;; register the odt exporter with org-lparse library
204 (org-lparse-register-backend 'odt
)
206 (defun org-odt-unload-function ()
207 (org-lparse-unregister-backend 'odt
)
208 (remove-hook 'org-export-preprocess-after-blockquote-hook
209 'org-export-odt-preprocess-latex-fragments
)
212 (defcustom org-export-odt-content-template-file nil
213 "Template file for \"content.xml\".
214 The exporter embeds the exported content just before
215 \"</office:text>\" element.
217 If unspecified, the file named \"OrgOdtContentTemplate.xml\"
218 under `org-odt-styles-dir' is used."
220 :group
'org-export-odt
223 (defcustom org-export-odt-styles-file nil
224 "Default styles file for use with ODT export.
225 Valid values are one of:
227 2. path to a styles.xml file
228 3. path to a *.odt or a *.ott file
229 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
232 In case of option 1, an in-built styles.xml is used. See
233 `org-odt-styles-dir' for more information.
235 In case of option 3, the specified file is unzipped and the
236 styles.xml embedded therein is used.
238 In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
239 and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
240 generated odt file. Use relative path for specifying the
241 FILE-MEMBERS. styles.xml must be specified as one of the
244 Use options 1, 2 or 3 only if styles.xml alone suffices for
245 achieving the desired formatting. Use option 4, if the styles.xml
246 references additional files like header and footer images for
247 achieving the desired formatting.
249 Use \"#+ODT_STYLES_FILE: ...\" directive to set this variable on
250 a per-file basis. For example,
252 #+ODT_STYLES_FILE: \"/path/to/styles.xml\" or
253 #+ODT_STYLES_FILE: (\"/path/to/file.ott\" (\"styles.xml\" \"image/hdr.png\"))."
254 :group
'org-export-odt
258 (const :tag
"Factory settings" nil
)
259 (file :must-match t
:tag
"styles.xml")
260 (file :must-match t
:tag
"ODT or OTT file")
261 (list :tag
"ODT or OTT file + Members"
262 (file :must-match t
:tag
"ODF Text or Text Template file")
264 (file :tag
" Member" "styles.xml")
265 (repeat (file :tag
"Member"))))))
267 (eval-after-load 'org-exp
268 '(add-to-list 'org-export-inbuffer-options-extra
269 '("ODT_STYLES_FILE" :odt-styles-file
)))
271 (defconst org-export-odt-tmpdir-prefix
"%s-")
272 (defconst org-export-odt-bookmark-prefix
"OrgXref.")
273 (defvar org-odt-zip-dir nil
274 "Temporary directory that holds XML files during export.")
276 (defvar org-export-odt-embed-images t
277 "Should the images be copied in to the odt file or just linked?")
279 (defvar org-export-odt-inline-images
'maybe
)
280 (defcustom org-export-odt-inline-image-extensions
281 '("png" "jpeg" "jpg" "gif")
282 "Extensions of image files that can be inlined into HTML."
283 :type
'(repeat (string :tag
"Extension"))
284 :group
'org-export-odt
287 (defcustom org-export-odt-pixels-per-inch display-pixels-per-inch
288 "Scaling factor for converting images pixels to inches.
289 Use this for sizing of embedded images. See Info node `(org)
290 Images in ODT export' for more information."
292 :group
'org-export-odt
295 (defcustom org-export-odt-create-custom-styles-for-srcblocks t
296 "Whether custom styles for colorized source blocks be automatically created.
297 When this option is turned on, the exporter creates custom styles
298 for source blocks based on the advice of `htmlfontify'. Creation
299 of custom styles happen as part of `org-odt-hfy-face-to-css'.
301 When this option is turned off exporter does not create such
304 Use the latter option if you do not want the custom styles to be
305 based on your current display settings. It is necessary that the
306 styles.xml already contains needed styles for colorizing to work.
308 This variable is effective only if
309 `org-export-odt-fontify-srcblocks' is turned on."
310 :group
'org-export-odt
314 (defvar org-export-odt-default-org-styles-alist
315 '((paragraph .
((default .
"Text_20_body")
316 (fixedwidth .
"OrgFixedWidthBlock")
318 (quote .
"Quotations")
319 (blockquote .
"Quotations")
320 (center .
"OrgCenter")
324 (subtitle .
"OrgSubtitle")
325 (footnote .
"Footnote")
326 (src .
"OrgSrcBlock")
327 (illustration .
"Illustration")
329 (definition-term .
"Text_20_body_20_bold")
330 (horizontal-line .
"Horizontal_20_Line")))
331 (character .
((default .
"Default")
333 (emphasis .
"Emphasis")
335 (verbatim .
"OrgCode")
336 (strike .
"Strikethrough")
337 (underline .
"Underline")
338 (subscript .
"OrgSubscript")
339 (superscript .
"OrgSuperscript")))
340 (list .
((ordered .
"OrgNumberedList")
341 (unordered .
"OrgBulletedList")
342 (description .
"OrgDescriptionList"))))
343 "Default styles for various entities.")
345 (defvar org-export-odt-org-styles-alist org-export-odt-default-org-styles-alist
)
346 (defun org-odt-get-style-name-for-entity (category &optional entity
)
347 (let ((entity (or entity
'default
)))
349 (cdr (assoc entity
(cdr (assoc category
350 org-export-odt-org-styles-alist
))))
351 (cdr (assoc entity
(cdr (assoc category
352 org-export-odt-default-org-styles-alist
))))
353 (error "Cannot determine style name for entity %s of type %s"
356 (defcustom org-export-odt-preferred-output-format nil
357 "Automatically post-process to this format after exporting to \"odt\".
358 Interactive commands `org-export-as-odt' and
359 `org-export-as-odt-and-open' export first to \"odt\" format and
360 then use `org-export-odt-convert-process' to convert the
361 resulting document to this format. During customization of this
362 variable, the list of valid values are populated based on
363 `org-export-odt-convert-capabilities'.
365 You can set this option on per-file basis using file local
366 values. See Info node `(emacs) File Variables'."
367 :group
'org-export-odt
369 :type
'(choice :convert-widget
371 (apply 'widget-convert
(widget-type w
)
372 (eval (car (widget-get w
:args
)))))
373 `((const :tag
"None" nil
)
374 ,@(mapcar (lambda (c)
376 (org-lparse-reachable-formats "odt")))))
378 (put 'org-export-odt-preferred-output-format
'safe-local-variable
'stringp
)
380 (defmacro org-odt-cleanup-xml-buffers
(&rest body
)
381 `(let ((org-odt-zip-dir
383 (format org-export-odt-tmpdir-prefix
"odf") t
))
384 (--cleanup-xml-buffers
387 (let ((xml-files '("mimetype" "META-INF/manifest.xml" "content.xml"
388 "meta.xml" "styles.xml")))
389 ;; kill all xml buffers
391 (let ((buf (find-file-noselect
392 (expand-file-name file org-odt-zip-dir
) t
)))
393 (when (buffer-name buf
)
394 (set-buffer-modified-p nil
)
397 ;; delete temporary directory.
398 (delete-directory org-odt-zip-dir t
)))))
399 (org-condition-case-unless-debug err
400 (prog1 (progn ,@body
)
401 (funcall --cleanup-xml-buffers
))
403 (funcall --cleanup-xml-buffers
)
404 (message "OpenDocument export failed: %s"
405 (error-message-string err
))))))
408 (defun org-export-as-odt-and-open (arg)
409 "Export the outline as ODT and immediately open it with a browser.
410 If there is an active region, export only the region.
411 The prefix ARG specifies how many levels of the outline should become
412 headlines. The default is 3. Lower levels will become bulleted lists."
414 (org-odt-cleanup-xml-buffers
416 (or org-export-odt-preferred-output-format
"odt") "odt" arg
)))
419 (defun org-export-as-odt-batch ()
420 "Call the function `org-lparse-batch'.
421 This function can be used in batch processing as:
423 --load=$HOME/lib/emacs/org.el
424 --eval \"(setq org-export-headline-levels 2)\"
425 --visit=MyFile --funcall org-export-as-odt-batch"
426 (org-lparse-batch "odt"))
428 ;;; org-export-as-odt
430 (defun org-export-as-odt (arg &optional hidden ext-plist
431 to-buffer body-only pub-dir
)
432 "Export the outline as a OpenDocumentText file.
433 If there is an active region, export only the region. The prefix
434 ARG specifies how many levels of the outline should become
435 headlines. The default is 3. Lower levels will become bulleted
436 lists. HIDDEN is obsolete and does nothing.
437 EXT-PLIST is a property list with external parameters overriding
438 org-mode's default settings, but still inferior to file-local
439 settings. When TO-BUFFER is non-nil, create a buffer with that
440 name and export to that buffer. If TO-BUFFER is the symbol
441 `string', don't leave any buffer behind but just return the
442 resulting XML as a string. When BODY-ONLY is set, don't produce
443 the file header and footer, simply return the content of
444 <body>...</body>, without even the body tags themselves. When
445 PUB-DIR is set, use this as the publishing directory."
447 (org-odt-cleanup-xml-buffers
448 (org-lparse (or org-export-odt-preferred-output-format
"odt")
449 "odt" arg hidden ext-plist to-buffer body-only pub-dir
)))
451 (defvar org-odt-entity-control-callbacks-alist
453 .
(org-odt-begin-export org-odt-end-export
))
455 .
(org-odt-begin-document-content org-odt-end-document-content
))
457 .
(org-odt-begin-document-body org-odt-end-document-body
))
459 .
(org-odt-begin-toc org-odt-end-toc
))
461 .
(org-odt-begin-environment org-odt-end-environment
))
463 .
(org-odt-begin-footnote-definition org-odt-end-footnote-definition
))
465 .
(org-odt-begin-table org-odt-end-table
))
467 .
(org-odt-begin-table-rowgroup org-odt-end-table-rowgroup
))
469 .
(org-odt-begin-list org-odt-end-list
))
471 .
(org-odt-begin-list-item org-odt-end-list-item
))
473 .
(org-odt-begin-outline org-odt-end-outline
))
475 .
(org-odt-begin-outline-text org-odt-end-outline-text
))
477 .
(org-odt-begin-paragraph org-odt-end-paragraph
)))
480 (defvar org-odt-entity-format-callbacks-alist
481 `((EXTRA-TARGETS . org-lparse-format-extra-targets
)
482 (ORG-TAGS . org-lparse-format-org-tags
)
483 (SECTION-NUMBER . org-lparse-format-section-number
)
484 (HEADLINE . org-odt-format-headline
)
485 (TOC-ENTRY . org-odt-format-toc-entry
)
486 (TOC-ITEM . org-odt-format-toc-item
)
487 (TAGS . org-odt-format-tags
)
488 (SPACES . org-odt-format-spaces
)
489 (TABS . org-odt-format-tabs
)
490 (LINE-BREAK . org-odt-format-line-break
)
491 (FONTIFY . org-odt-format-fontify
)
492 (TODO . org-lparse-format-todo
)
493 (LINK . org-odt-format-link
)
494 (INLINE-IMAGE . org-odt-format-inline-image
)
495 (ORG-LINK . org-odt-format-org-link
)
496 (HEADING . org-odt-format-heading
)
497 (ANCHOR . org-odt-format-anchor
)
498 (TABLE . org-lparse-format-table
)
499 (TABLE-ROW . org-odt-format-table-row
)
500 (TABLE-CELL . org-odt-format-table-cell
)
501 (FOOTNOTES-SECTION . ignore
)
502 (FOOTNOTE-REFERENCE . org-odt-format-footnote-reference
)
503 (HORIZONTAL-LINE . org-odt-format-horizontal-line
)
504 (COMMENT . org-odt-format-comment
)
505 (LINE . org-odt-format-line
)
506 (ORG-ENTITY . org-odt-format-org-entity
))
510 ;;;_. control callbacks
512 (defun org-odt-begin-office-body ()
514 (insert-file-contents
515 (or org-export-odt-content-template-file
516 (expand-file-name "OrgOdtContentTemplate.xml"
517 org-odt-styles-dir
)))
518 (goto-char (point-min))
519 (re-search-forward "</office:text>" nil nil
)
520 (delete-region (match-beginning 0) (point-max)))
522 ;; Following variable is let bound when `org-do-lparse' is in
523 ;; progress. See org-html.el.
524 (defvar org-lparse-toc
)
525 (defun org-odt-format-toc ()
526 (if (not org-lparse-toc
) "" (concat "\n" org-lparse-toc
"\n")))
528 (defun org-odt-format-preamble (opt-plist)
529 (let* ((title (plist-get opt-plist
:title
))
530 (author (plist-get opt-plist
:author
))
531 (date (plist-get opt-plist
:date
))
532 (iso-date (org-odt-format-date date
))
533 (date (org-odt-format-date date
"%d %b %Y"))
534 (email (plist-get opt-plist
:email
))
535 ;; switch on or off above vars based on user settings
536 (author (and (plist-get opt-plist
:author-info
) (or author email
)))
537 (email (and (plist-get opt-plist
:email-info
) email
))
538 (date (and (plist-get opt-plist
:time-stamp-file
) date
)))
543 (org-odt-format-stylized-paragraph
544 'title
(org-odt-format-tags
545 '("<text:title>" .
"</text:title>") title
))
547 "<text:p text:style-name=\"OrgTitle\"/>"))
549 ((and author
(not email
))
552 (org-odt-format-stylized-paragraph
555 '("<text:initial-creator>" .
"</text:initial-creator>")
558 "<text:p text:style-name=\"OrgSubtitle\"/>"))
562 (org-odt-format-stylized-paragraph
566 '("<text:initial-creator>" .
"</text:initial-creator>")
567 author
) (concat "mailto:" email
)))
569 "<text:p text:style-name=\"OrgSubtitle\"/>")))
573 (org-odt-format-stylized-paragraph
576 '("<text:date style:data-style-name=\"%s\" text:date-value=\"%s\">"
577 .
"</text:date>") date
"N75" iso-date
))
579 "<text:p text:style-name=\"OrgSubtitle\"/>")))))
581 (defun org-odt-begin-document-body (opt-plist)
582 (org-odt-begin-office-body)
583 (insert (org-odt-format-preamble opt-plist
))
584 (setq org-lparse-dyn-first-heading-pos
(point)))
586 (defvar org-lparse-body-only
) ; let bound during org-do-lparse
587 (defvar org-lparse-to-buffer
) ; let bound during org-do-lparse
588 (defun org-odt-end-document-body (opt-plist)
589 (unless org-lparse-body-only
590 (org-lparse-insert-tag "</office:text>")
591 (org-lparse-insert-tag "</office:body>")))
593 (defun org-odt-begin-document-content (opt-plist)
596 (defun org-odt-end-document-content ()
597 (org-lparse-insert-tag "</office:document-content>"))
599 (defun org-odt-begin-outline (level1 snumber title tags
600 target extra-targets class
)
602 'HEADING
(org-lparse-format
603 'HEADLINE title extra-targets tags snumber level1
)
606 (defun org-odt-end-outline ()
609 (defun org-odt-begin-outline-text (level1 snumber class
)
612 (defun org-odt-end-outline-text ()
615 (defun org-odt-begin-section (style &optional name
)
616 (let ((default-name (car (org-odt-add-automatic-style "Section"))))
617 (org-lparse-insert-tag
618 "<text:section text:style-name=\"%s\" text:name=\"%s\">"
619 style
(or name default-name
))))
621 (defun org-odt-end-section ()
622 (org-lparse-insert-tag "</text:section>"))
624 (defun org-odt-begin-paragraph (&optional style
)
625 (org-lparse-insert-tag
626 "<text:p%s>" (org-odt-get-extra-attrs-for-paragraph-style style
)))
628 (defun org-odt-end-paragraph ()
629 (org-lparse-insert-tag "</text:p>"))
631 (defun org-odt-get-extra-attrs-for-paragraph-style (style)
635 ((stringp style
) style
)
636 ((symbolp style
) (org-odt-get-style-name-for-entity
639 (error "Don't know how to handle paragraph style %s" style
))
640 (format " text:style-name=\"%s\"" style-name
)))
642 (defun org-odt-format-stylized-paragraph (style text
)
644 '("<text:p%s>" .
"</text:p>") text
645 (org-odt-get-extra-attrs-for-paragraph-style style
)))
647 (defvar org-lparse-opt-plist
) ; bound during org-do-lparse
648 (defun org-odt-format-author (&optional author
)
649 (when (setq author
(or author
(plist-get org-lparse-opt-plist
:author
)))
650 (org-odt-format-tags '("<dc:creator>" .
"</dc:creator>") author
)))
652 (defun org-odt-format-date (&optional org-ts fmt
)
655 (and (stringp org-ts
)
656 (string-match org-ts-regexp0 org-ts
)
658 (org-fix-decoded-time
659 (org-parse-time-string (match-string 0 org-ts
) t
)))))
662 (fmt (format-time-string fmt time
))
663 (t (setq date
(format-time-string "%Y-%m-%dT%H:%M:%S%z" time
))
664 (format "%s:%s" (substring date
0 -
2) (substring date -
2)))))))
666 (defun org-odt-begin-annotation (&optional author date
)
667 (org-lparse-insert-tag "<office:annotation>")
668 (when (setq author
(org-odt-format-author author
))
670 (insert (org-odt-format-tags
671 '("<dc:date>" .
"</dc:date>")
673 (or date
(plist-get org-lparse-opt-plist
:date
)))))
674 (org-lparse-begin-paragraph))
676 (defun org-odt-end-annotation ()
677 (org-lparse-insert-tag "</office:annotation>"))
679 (defun org-odt-begin-environment (style env-options-plist
)
682 (org-lparse-stash-save-paragraph-state)
683 (org-odt-begin-annotation (plist-get env-options-plist
'author
)
684 (plist-get env-options-plist
'date
)))
685 ((blockquote verse center quote
)
686 (org-lparse-begin-paragraph style
)
689 (org-lparse-end-paragraph)
691 (t (error "Unknown environment %s" style
))))
693 (defun org-odt-end-environment (style env-options-plist
)
696 (org-lparse-end-paragraph)
697 (org-odt-end-annotation)
698 (org-lparse-stash-pop-paragraph-state))
699 ((blockquote verse center quote
)
700 (org-lparse-end-paragraph)
703 (org-lparse-begin-paragraph)
705 (t (error "Unknown environment %s" style
))))
707 (defvar org-lparse-list-stack
) ; dynamically bound in org-do-lparse
708 (defvar org-odt-list-stack-stashed
)
709 (defun org-odt-begin-list (ltype)
710 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
712 (let* ((style-name (org-odt-get-style-name-for-entity 'list ltype
))
713 (extra (concat (if (or org-lparse-list-table-p
714 (and (= 1 (length org-lparse-list-stack
))
715 (null org-odt-list-stack-stashed
)))
716 " text:continue-numbering=\"false\""
717 " text:continue-numbering=\"true\"")
719 (format " text:style-name=\"%s\"" style-name
)))))
721 ((ordered unordered description
)
722 (org-lparse-end-paragraph)
723 (org-lparse-insert-tag "<text:list%s>" extra
))
724 (t (error "Unknown list type: %s" ltype
)))))
726 (defun org-odt-end-list (ltype)
727 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
730 (org-lparse-insert-tag "</text:list>")
731 (error "Unknown list type: %s" ltype
)))
733 (defun org-odt-begin-list-item (ltype &optional arg headline
)
734 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
738 (assert (not headline
) t
)
739 (let* ((counter arg
) (extra ""))
740 (org-lparse-insert-tag (if (= (length org-lparse-list-stack
)
741 (length org-odt-list-stack-stashed
))
742 "<text:list-header>" "<text:list-item>"))
743 (org-lparse-begin-paragraph)))
745 (let* ((id arg
) (extra ""))
746 (org-lparse-insert-tag (if (= (length org-lparse-list-stack
)
747 (length org-odt-list-stack-stashed
))
748 "<text:list-header>" "<text:list-item>"))
749 (org-lparse-begin-paragraph)
750 (insert (if headline
(org-odt-format-target headline id
)
751 (org-odt-format-bookmark "" id
)))))
753 (assert (not headline
) t
)
754 (let ((term (or arg
"(no term)")))
757 '("<text:list-item>" .
"</text:list-item>")
758 (org-odt-format-stylized-paragraph 'definition-term term
)))
759 (org-lparse-begin-list-item 'unordered
)
760 (org-lparse-begin-list 'description
)
761 (org-lparse-begin-list-item 'unordered
)))
762 (t (error "Unknown list type"))))
764 (defun org-odt-end-list-item (ltype)
765 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
769 (org-lparse-insert-tag (if (= (length org-lparse-list-stack
)
770 (length org-odt-list-stack-stashed
))
771 (prog1 "</text:list-header>"
772 (setq org-odt-list-stack-stashed nil
))
773 "</text:list-item>")))
775 (org-lparse-end-list-item-1)
776 (org-lparse-end-list 'description
)
777 (org-lparse-end-list-item-1))
778 (t (error "Unknown list type"))))
780 (defun org-odt-discontinue-list ()
781 (let ((stashed-stack org-lparse-list-stack
))
782 (loop for list-type in stashed-stack
783 do
(org-lparse-end-list-item-1 list-type
)
784 (org-lparse-end-list list-type
))
785 (setq org-odt-list-stack-stashed stashed-stack
)))
787 (defun org-odt-continue-list ()
788 (setq org-odt-list-stack-stashed
(nreverse org-odt-list-stack-stashed
))
789 (loop for list-type in org-odt-list-stack-stashed
790 do
(org-lparse-begin-list list-type
)
791 (org-lparse-begin-list-item list-type
)))
793 ;; Following variables are let bound when table emission is in
794 ;; progress. See org-lparse.el.
795 (defvar org-lparse-table-begin-marker
)
796 (defvar org-lparse-table-ncols
)
797 (defvar org-lparse-table-rowgrp-open
)
798 (defvar org-lparse-table-rownum
)
799 (defvar org-lparse-table-cur-rowgrp-is-hdr
)
800 (defvar org-lparse-table-is-styled
)
801 (defvar org-lparse-table-rowgrp-info
)
802 (defvar org-lparse-table-colalign-vector
)
804 (defvar org-odt-table-style nil
805 "Table style specified by \"#+ATTR_ODT: <style-name>\" line.
806 This is set during `org-odt-begin-table'.")
808 (defvar org-odt-table-style-spec nil
809 "Entry for `org-odt-table-style' in `org-export-odt-table-styles'.")
811 (defcustom org-export-odt-table-styles
812 '(("OrgEquation" "OrgEquation"
813 ((use-first-column-styles . t
)
814 (use-last-column-styles . t
))))
815 "Specify how Table Styles should be derived from a Table Template.
816 This is a list where each element is of the
817 form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
819 TABLE-STYLE-NAME is the style associated with the table through
820 `org-odt-table-style'.
822 TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
823 TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
824 below) that is included in
825 `org-export-odt-content-template-file'.
827 TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
829 PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
831 TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
832 \"FirstRow\" | \"LastRow\" |
833 \"EvenRow\" | \"OddRow\" |
834 \"EvenColumn\" | \"OddColumn\" | \"\"
835 where \"+\" above denotes string concatenation.
837 TABLE-CELL-OPTIONS is an alist where each element is of the
838 form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
839 TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
840 `use-last-row-styles' |
841 `use-first-column-styles' |
842 `use-last-column-styles' |
843 `use-banding-rows-styles' |
844 `use-banding-columns-styles' |
845 `use-first-row-styles'
846 ON-OR-OFF := `t' | `nil'
848 For example, with the following configuration
850 \(setq org-export-odt-table-styles
851 '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
852 \(\(use-first-row-styles . t\)
853 \(use-first-column-styles . t\)\)\)
854 \(\"TableWithHeaderColumns\" \"Custom\"
855 \(\(use-first-column-styles . t\)\)\)\)\)
857 1. A table associated with \"TableWithHeaderRowsAndColumns\"
858 style will use the following table-cell styles -
859 \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
860 \"CustomTableCell\" and the following paragraph styles
861 \"CustomFirstRowTableParagraph\",
862 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
865 2. A table associated with \"TableWithHeaderColumns\" style will
866 use the following table-cell styles -
867 \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
868 following paragraph styles
869 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
872 Note that TABLE-TEMPLATE-NAME corresponds to the
873 \"<table:table-template>\" elements contained within
874 \"<office:styles>\". The entries (TABLE-STYLE-NAME
875 TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
876 \"table:template-name\" and \"table:use-first-row-styles\" etc
877 attributes of \"<table:table>\" element. Refer ODF-1.2
878 specification for more information. Also consult the
879 implementation filed under `org-odt-get-table-cell-styles'.
881 The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
882 formatting of numbered display equations. Do not delete this
883 style from the list."
884 :group
'org-export-odt
887 (const :tag
"None" nil
)
888 (repeat :tag
"Table Styles"
889 (list :tag
"Table Style Specification"
890 (string :tag
"Table Style Name")
891 (string :tag
"Table Template Name")
892 (alist :options
(use-first-row-styles
894 use-first-column-styles
895 use-last-column-styles
896 use-banding-rows-styles
897 use-banding-columns-styles
)
899 :value-type
(const :tag
"True" t
))))))
901 (defvar org-odt-table-style-format
903 <style:style style:name=\"%s\" style:family=\"table\">
904 <style:table-properties style:rel-width=\"%d%%\" fo:margin-top=\"0cm\" fo:margin-bottom=\"0.20cm\" table:align=\"center\"/>
907 "Template for auto-generated Table styles.")
909 (defvar org-odt-automatic-styles
'()
910 "Registry of automatic styles for various OBJECT-TYPEs.
911 The variable has the following form:
913 \(\(OBJECT-NAME-A.1 OBJECT-PROPS-A.1\)
914 \(OBJECT-NAME-A.2 OBJECT-PROPS-A.2\) ...\)\)
916 \(\(OBJECT-NAME-B.1 OBJECT-PROPS-B.1\)
917 \(OBJECT-NAME-B.2 OBJECT-PROPS-B.2\) ...\)\)
920 OBJECT-TYPEs could be \"Section\", \"Table\", \"Figure\" etc.
921 OBJECT-PROPS is (typically) a plist created by passing
922 \"#+ATTR_ODT: \" option to `org-lparse-get-block-params'.
924 Use `org-odt-add-automatic-style' to add update this variable.'")
926 (defvar org-odt-object-counters nil
927 "Running counters for various OBJECT-TYPEs.
928 Use this to generate automatic names and style-names. See
929 `org-odt-add-automatic-style'.")
931 (defun org-odt-write-automatic-styles ()
932 "Write automatic styles to \"content.xml\"."
934 (find-file-noselect (expand-file-name "content.xml") t
)
935 ;; position the cursor
936 (goto-char (point-min))
937 (re-search-forward " </office:automatic-styles>" nil t
)
938 (goto-char (match-beginning 0))
939 ;; write automatic table styles
940 (loop for
(style-name props
) in
941 (plist-get org-odt-automatic-styles
'Table
) do
942 (when (setq props
(or (plist-get props
:rel-width
) 96))
943 (insert (format org-odt-table-style-format style-name props
))))))
945 (defun org-odt-add-automatic-style (object-type &optional object-props
)
946 "Create an automatic style of type OBJECT-TYPE with param OBJECT-PROPS.
947 OBJECT-PROPS is (typically) a plist created by passing
948 \"#+ATTR_ODT: \" option of the object in question to
949 `org-lparse-get-block-params'.
951 Use `org-odt-object-counters' to generate an automatic
952 OBJECT-NAME and STYLE-NAME. If OBJECT-PROPS is non-nil, add a
953 new entry in `org-odt-automatic-styles'. Return (OBJECT-NAME
955 (assert (stringp object-type
))
956 (let* ((object (intern object-type
))
958 (seqno (1+ (or (plist-get org-odt-object-counters seqvar
) 0)))
959 (object-name (format "%s%d" object-type seqno
)) style-name
)
960 (setq org-odt-object-counters
961 (plist-put org-odt-object-counters seqvar seqno
))
963 (setq style-name
(format "Org%s" object-name
))
964 (setq org-odt-automatic-styles
965 (plist-put org-odt-automatic-styles object
966 (append (list (list style-name object-props
))
967 (plist-get org-odt-automatic-styles object
)))))
968 (cons object-name style-name
)))
970 (defvar org-odt-table-indentedp nil
)
971 (defun org-odt-begin-table (caption label attributes short-caption
)
972 (setq org-odt-table-indentedp
(not (null org-lparse-list-stack
)))
973 (when org-odt-table-indentedp
974 ;; Within the Org file, the table is appearing within a list item.
975 ;; OpenDocument doesn't allow table to appear within list items.
976 ;; Temporarily terminate the list, emit the table and then
977 ;; re-continue the list.
978 (org-odt-discontinue-list)
979 ;; Put the Table in an indented section.
980 (let ((level (length org-odt-list-stack-stashed
)))
981 (org-odt-begin-section (format "OrgIndentedSection-Level-%d" level
))))
982 (setq attributes
(org-lparse-get-block-params attributes
))
983 (setq org-odt-table-style
(plist-get attributes
:style
))
984 (setq org-odt-table-style-spec
985 (assoc org-odt-table-style org-export-odt-table-styles
))
986 (when (or label caption
)
988 (org-odt-format-stylized-paragraph
989 'table
(org-odt-format-entity-caption label caption
"__Table__"))))
990 (let ((automatic-name (org-odt-add-automatic-style "Table" attributes
)))
991 (org-lparse-insert-tag
992 "<table:table table:name=\"%s\" table:style-name=\"%s\">"
993 (or short-caption
(car automatic-name
))
994 (or (nth 1 org-odt-table-style-spec
)
995 (cdr automatic-name
) "OrgTable")))
996 (setq org-lparse-table-begin-marker
(point)))
998 (defvar org-lparse-table-colalign-info
)
999 (defun org-odt-end-table ()
1000 (goto-char org-lparse-table-begin-marker
)
1001 (loop for level from
0 below org-lparse-table-ncols
1002 do
(let* ((col-cookie (and org-lparse-table-is-styled
1003 (cdr (assoc (1+ level
)
1004 org-lparse-table-colalign-info
))))
1005 (extra-columns (or (nth 1 col-cookie
) 0)))
1006 (dotimes (i (1+ extra-columns
))
1008 (org-odt-format-tags
1009 "<table:table-column table:style-name=\"%sColumn\"/>"
1010 "" (or (nth 1 org-odt-table-style-spec
) "OrgTable"))))
1012 ;; fill style attributes for table cells
1013 (when org-lparse-table-is-styled
1014 (while (re-search-forward "@@\\(table-cell:p\\|table-cell:style-name\\)@@\\([0-9]+\\)@@\\([0-9]+\\)@@" nil t
)
1015 (let* ((spec (match-string 1))
1016 (r (string-to-number (match-string 2)))
1017 (c (string-to-number (match-string 3)))
1018 (cell-styles (org-odt-get-table-cell-styles
1019 r c org-odt-table-style-spec
))
1020 (table-cell-style (car cell-styles
))
1021 (table-cell-paragraph-style (cdr cell-styles
)))
1023 ((equal spec
"table-cell:p")
1024 (replace-match table-cell-paragraph-style t t
))
1025 ((equal spec
"table-cell:style-name")
1026 (replace-match table-cell-style t t
))))))
1027 (goto-char (point-max))
1028 (org-lparse-insert-tag "</table:table>")
1029 (when org-odt-table-indentedp
1030 (org-odt-end-section)
1031 (org-odt-continue-list)))
1033 (defun org-odt-begin-table-rowgroup (&optional is-header-row
)
1034 (when org-lparse-table-rowgrp-open
1035 (org-lparse-end 'TABLE-ROWGROUP
))
1036 (org-lparse-insert-tag (if is-header-row
1037 "<table:table-header-rows>"
1038 "<table:table-rows>"))
1039 (setq org-lparse-table-rowgrp-open t
)
1040 (setq org-lparse-table-cur-rowgrp-is-hdr is-header-row
))
1042 (defun org-odt-end-table-rowgroup ()
1043 (when org-lparse-table-rowgrp-open
1044 (setq org-lparse-table-rowgrp-open nil
)
1045 (org-lparse-insert-tag
1046 (if org-lparse-table-cur-rowgrp-is-hdr
1047 "</table:table-header-rows>" "</table:table-rows>"))))
1049 (defun org-odt-format-table-row (row)
1050 (org-odt-format-tags
1051 '("<table:table-row>" .
"</table:table-row>") row
))
1053 (defun org-odt-get-table-cell-styles (r c
&optional style-spec
)
1054 "Retrieve styles applicable to a table cell.
1055 R and C are (zero-based) row and column numbers of the table
1056 cell. STYLE-SPEC is an entry in `org-export-odt-table-styles'
1057 applicable to the current table. It is `nil' if the table is not
1058 associated with any style attributes.
1060 Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
1062 When STYLE-SPEC is nil, style the table cell the conventional way
1063 - choose cell borders based on row and column groupings and
1064 choose paragraph alignment based on `org-col-cookies' text
1066 `org-odt-get-paragraph-style-cookie-for-table-cell'.
1068 When STYLE-SPEC is non-nil, ignore the above cookie and return
1069 styles congruent with the ODF-1.2 specification."
1073 ;; LibreOffice - particularly the Writer - honors neither table
1074 ;; templates nor custom table-cell styles. Inorder to retain
1075 ;; inter-operability with LibreOffice, only automatic styles are
1076 ;; used for styling of table-cells. The current implementation is
1077 ;; congruent with ODF-1.2 specification and hence is
1078 ;; future-compatible.
1080 ;; Additional Note: LibreOffice's AutoFormat facility for tables -
1081 ;; which recognizes as many as 16 different cell types - is much
1082 ;; richer. Unfortunately it is NOT amenable to easy configuration
1085 (let* ((template-name (nth 1 style-spec
))
1086 (cell-style-selectors (nth 2 style-spec
))
1089 ((and (cdr (assoc 'use-first-column-styles cell-style-selectors
))
1090 (= c
0)) "FirstColumn")
1091 ((and (cdr (assoc 'use-last-column-styles cell-style-selectors
))
1092 (= c
(1- org-lparse-table-ncols
))) "LastColumn")
1093 ((and (cdr (assoc 'use-first-row-styles cell-style-selectors
))
1094 (= r
0)) "FirstRow")
1095 ((and (cdr (assoc 'use-last-row-styles cell-style-selectors
))
1096 (= r org-lparse-table-rownum
))
1098 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors
))
1099 (= (% r
2) 1)) "EvenRow")
1100 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors
))
1101 (= (% r
2) 0)) "OddRow")
1102 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors
))
1103 (= (% c
2) 1)) "EvenColumn")
1104 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors
))
1105 (= (% c
2) 0)) "OddColumn")
1108 (concat template-name cell-type
"TableCell")
1109 (concat template-name cell-type
"TableParagraph"))))
1116 ((eq (cdr (assoc r org-lparse-table-rowgrp-info
)) :start
) "T")
1118 (when (= r org-lparse-table-rownum
) "B")
1121 ((or (memq (nth c org-table-colgroup-info
) '(:start
:startend
))
1122 (memq (nth (1- c
) org-table-colgroup-info
) '(:end
:startend
))) "L")
1124 (capitalize (aref org-lparse-table-colalign-vector c
))))))
1126 (defun org-odt-get-paragraph-style-cookie-for-table-cell (r c
)
1128 (and (not org-odt-table-style-spec
)
1130 (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
1131 ((and (= c
0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS
))
1133 (t "OrgTableContents")))
1134 (and org-lparse-table-is-styled
1135 (format "@@table-cell:p@@%03d@@%03d@@" r c
))))
1137 (defun org-odt-get-style-name-cookie-for-table-cell (r c
)
1138 (when org-lparse-table-is-styled
1139 (format "@@table-cell:style-name@@%03d@@%03d@@" r c
)))
1141 (defun org-odt-format-table-cell (data r c horiz-span
)
1143 (let* ((paragraph-style-cookie
1144 (org-odt-get-paragraph-style-cookie-for-table-cell r c
))
1146 (org-odt-get-style-name-cookie-for-table-cell r c
))
1147 (extra (and style-name-cookie
1148 (format " table:style-name=\"%s\"" style-name-cookie
)))
1149 (extra (concat extra
1150 (and (> horiz-span
0)
1151 (format " table:number-columns-spanned=\"%d\""
1152 (1+ horiz-span
))))))
1153 (org-odt-format-tags
1154 '("<table:table-cell%s>" .
"</table:table-cell>")
1155 (if org-lparse-list-table-p data
1156 (org-odt-format-stylized-paragraph paragraph-style-cookie data
)) extra
))
1158 (dotimes (i horiz-span
)
1159 (setq s
(concat s
"\n<table:covered-table-cell/>"))) s
)
1162 (defun org-odt-begin-footnote-definition (n)
1163 (org-lparse-begin-paragraph 'footnote
))
1165 (defun org-odt-end-footnote-definition (n)
1166 (org-lparse-end-paragraph))
1168 (defun org-odt-begin-toc (lang-specific-heading max-level
)
1169 ;; Strings in `org-export-language-setup' can contain named html
1170 ;; entities. Replace those with utf-8 equivalents.
1171 (let ((i 0) entity rpl
)
1172 (while (string-match "&\\([^#].*?\\);" lang-specific-heading i
)
1173 (setq entity
(match-string 1 lang-specific-heading
))
1174 (if (not (setq rpl
(org-entity-get-representation entity
'utf8
)))
1175 (setq i
(match-end 0))
1176 (setq i
(+ (match-beginning 0) (length rpl
)))
1177 (setq lang-specific-heading
1178 (replace-match rpl t t lang-specific-heading
)))))
1181 <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
1182 <text:table-of-content-source text:outline-level=\"%d\">
1183 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
1184 " max-level lang-specific-heading
))
1185 (loop for level from
1 upto
10
1188 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
1189 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
1190 <text:index-entry-chapter/>
1191 <text:index-entry-text/>
1192 <text:index-entry-link-end/>
1193 </text:table-of-content-entry-template>
1198 </text:table-of-content-source>
1201 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
1202 <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
1204 " lang-specific-heading
)))
1206 (defun org-odt-end-toc ()
1209 </text:table-of-content>
1212 (defun org-odt-format-toc-entry (snumber todo headline tags href
)
1213 (setq headline
(concat
1214 (and org-export-with-section-numbers
1215 (concat snumber
". "))
1219 (org-lparse-format 'SPACES
3)
1220 (org-lparse-format 'FONTIFY tags
"tag")))))
1222 (setq headline
(org-lparse-format 'FONTIFY headline
"todo")))
1224 (let ((org-odt-suppress-xref t
))
1225 (org-odt-format-link headline
(concat "#" href
))))
1227 (defun org-odt-format-toc-item (toc-entry level org-last-level
)
1228 (let ((style (format "Contents_20_%d"
1229 (+ level
(or (org-lparse-get 'TOPLEVEL-HLEVEL
) 1) -
1))))
1230 (insert "\n" (org-odt-format-stylized-paragraph style toc-entry
) "\n")))
1232 ;; Following variable is let bound during 'ORG-LINK callback. See
1234 (defvar org-lparse-link-description-is-image nil
)
1235 (defun org-odt-format-link (desc href
&optional attr
)
1237 ((and (= (string-to-char href
) ?
#) (not org-odt-suppress-xref
))
1238 (setq href
(substring href
1))
1239 (let ((xref-format "text"))
1240 (when (numberp desc
)
1241 (setq desc
(format "%d" desc
) xref-format
"number"))
1243 (setq desc
(mapconcat 'identity desc
".") xref-format
"chapter"))
1244 (setq href
(concat org-export-odt-bookmark-prefix href
))
1245 (org-odt-format-tags
1246 '("<text:bookmark-ref text:reference-format=\"%s\" text:ref-name=\"%s\">" .
1247 "</text:bookmark-ref>")
1248 desc xref-format href
)))
1249 (org-lparse-link-description-is-image
1250 (org-odt-format-tags
1251 '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" .
"</draw:a>")
1252 desc href
(or attr
"")))
1254 (org-odt-format-tags
1255 '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" .
"</text:a>")
1256 desc href
(or attr
"")))))
1258 (defun org-odt-format-spaces (n)
1262 " " (org-odt-format-tags "<text:s text:c=\"%d\"/>" "" (1- n
))))
1265 (defun org-odt-format-tabs (&optional n
)
1266 (let ((tab "<text:tab/>")
1270 (defun org-odt-format-line-break ()
1271 (org-odt-format-tags "<text:line-break/>" ""))
1273 (defun org-odt-format-horizontal-line ()
1274 (org-odt-format-stylized-paragraph 'horizontal-line
""))
1276 (defun org-odt-encode-plain-text (line &optional no-whitespace-filling
)
1277 (setq line
(org-xml-encode-plain-text line
))
1278 (if no-whitespace-filling line
1279 (org-odt-fill-tabs-and-spaces line
)))
1281 (defun org-odt-format-line (line)
1282 (case org-lparse-dyn-current-environment
1284 (org-odt-format-stylized-paragraph
1285 'fixedwidth
(org-odt-encode-plain-text line
)) "\n"))
1286 (t (concat line
"\n"))))
1288 (defun org-odt-format-comment (fmt &rest args
)
1289 (let ((comment (apply 'format fmt args
)))
1290 (format "\n<!-- %s -->\n" comment
)))
1292 (defun org-odt-format-org-entity (wd)
1293 (org-entity-get-representation wd
'utf8
))
1295 (defun org-odt-fill-tabs-and-spaces (line)
1296 (replace-regexp-in-string
1297 "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
1299 ((string= s
"\t") (org-odt-format-tabs))
1300 (t (org-odt-format-spaces (length s
))))) line
))
1302 (defcustom org-export-odt-fontify-srcblocks t
1303 "Specify whether or not source blocks need to be fontified.
1304 Turn this option on if you want to colorize the source code
1305 blocks in the exported file. For colorization to work, you need
1306 to make available an enhanced version of `htmlfontify' library."
1308 :group
'org-export-odt
1311 (defun org-odt-format-source-line-with-line-number-and-label
1312 (line rpllbl num fontifier par-style
)
1314 (let ((keep-label (not (numberp rpllbl
)))
1315 (ref (org-find-text-property-in-string 'org-coderef line
)))
1316 (setq line
(concat line
(and keep-label ref
(format "(%s)" ref
))))
1317 (setq line
(funcall fontifier line
))
1319 (setq line
(org-odt-format-target line
(concat "coderef-" ref
))))
1320 (setq line
(org-odt-format-stylized-paragraph par-style line
))
1322 (org-odt-format-tags '("<text:list-item>" .
"</text:list-item>") line
))))
1324 (defun org-odt-format-source-code-or-example-plain
1325 (lines lang caption textareap cols rows num cont rpllbl fmt
)
1326 "Format source or example blocks much like fixedwidth blocks.
1327 Use this when `org-export-odt-fontify-srcblocks' option is turned
1329 (let* ((lines (org-split-string lines
"[\r\n]"))
1330 (line-count (length lines
))
1335 (org-odt-format-source-line-with-line-number-and-label
1336 line rpllbl num
'org-odt-encode-plain-text
1337 (if (= i line-count
) "OrgFixedWidthBlockLastLine"
1338 "OrgFixedWidthBlock")))
1341 (defvar org-src-block-paragraph-format
1342 "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
1343 <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
1344 <style:background-image/>
1345 </style:paragraph-properties>
1346 <style:text-properties fo:color=\"%s\"/>
1348 "Custom paragraph style for colorized source and example blocks.
1349 This style is much the same as that of \"OrgFixedWidthBlock\"
1350 except that the foreground and background colors are set
1351 according to the default face identified by the `htmlfontify'.")
1353 (defvar hfy-optimisations
)
1354 (declare-function hfy-face-to-style
"htmlfontify" (fn))
1355 (declare-function hfy-face-or-def-to-name
"htmlfontify" (fn))
1357 (defun org-odt-hfy-face-to-css (fn)
1358 "Create custom style for face FN.
1359 When FN is the default face, use it's foreground and background
1360 properties to create \"OrgSrcBlock\" paragraph style. Otherwise
1361 use it's color attribute to create a character style whose name
1362 is obtained from FN. Currently all attributes of FN other than
1365 The style name for a face FN is derived using the following
1366 operations on the face name in that order - de-dash, CamelCase
1367 and prefix with \"OrgSrc\". For example,
1368 `font-lock-function-name-face' is associated with
1369 \"OrgSrcFontLockFunctionNameFace\"."
1370 (let* ((css-list (hfy-face-to-style fn
))
1371 (style-name ((lambda (fn)
1374 'capitalize
(split-string
1375 (hfy-face-or-def-to-name fn
) "-")
1377 (color-val (cdr (assoc "color" css-list
)))
1378 (background-color-val (cdr (assoc "background" css-list
)))
1379 (style (and org-export-odt-create-custom-styles-for-srcblocks
1382 (format org-src-block-paragraph-format
1383 background-color-val color-val
))
1387 <style:style style:name=\"%s\" style:family=\"text\">
1388 <style:text-properties fo:color=\"%s\"/>
1389 </style:style>" style-name color-val
))))))
1390 (cons style-name style
)))
1392 (defun org-odt-insert-custom-styles-for-srcblocks (styles)
1393 "Save STYLES used for colorizing of source blocks.
1394 Update styles.xml with styles that were collected as part of
1395 `org-odt-hfy-face-to-css' callbacks."
1397 (with-current-buffer
1398 (find-file-noselect (expand-file-name "styles.xml") t
)
1399 (goto-char (point-min))
1400 (when (re-search-forward "</office:styles>" nil t
)
1401 (goto-char (match-beginning 0))
1402 (insert "\n<!-- Org Htmlfontify Styles -->\n" styles
"\n")))))
1404 (defun org-odt-format-source-code-or-example-colored
1405 (lines lang caption textareap cols rows num cont rpllbl fmt
)
1406 "Format source or example blocks using `htmlfontify-string'.
1407 Use this routine when `org-export-odt-fontify-srcblocks' option
1409 (let* ((lang-m (and lang
(or (cdr (assoc lang org-src-lang-modes
)) lang
)))
1410 (mode (and lang-m
(intern (concat (if (symbolp lang-m
)
1411 (symbol-name lang-m
)
1413 (org-inhibit-startup t
)
1414 (org-startup-folded nil
)
1415 (lines (with-temp-buffer
1417 (if (functionp mode
) (funcall mode
) (fundamental-mode))
1418 (font-lock-fontify-buffer)
1420 (hfy-html-quote-regex "\\([<\"&> ]\\)")
1421 (hfy-html-quote-map '(("\"" """)
1426 (" " "<text:tab/>")))
1427 (hfy-face-to-css 'org-odt-hfy-face-to-css
)
1428 (hfy-optimisations-1 (copy-sequence hfy-optimisations
))
1429 (hfy-optimisations (add-to-list 'hfy-optimisations-1
1431 (hfy-begin-span-handler
1432 (lambda (style text-block text-id text-begins-block-p
)
1433 (insert (format "<text:span text:style-name=\"%s\">" style
))))
1434 (hfy-end-span-handler (lambda nil
(insert "</text:span>"))))
1435 (when (fboundp 'htmlfontify-string
)
1436 (let* ((lines (org-split-string lines
"[\r\n]"))
1437 (line-count (length lines
))
1442 (org-odt-format-source-line-with-line-number-and-label
1443 line rpllbl num
'htmlfontify-string
1444 (if (= i line-count
) "OrgSrcBlockLastLine" "OrgSrcBlock")))
1447 (defun org-odt-format-source-code-or-example (lines lang caption textareap
1450 "Format source or example blocks for export.
1451 Use `org-odt-format-source-code-or-example-plain' or
1452 `org-odt-format-source-code-or-example-colored' depending on the
1453 value of `org-export-odt-fontify-srcblocks."
1454 (setq lines
(org-export-number-lines
1455 lines
0 0 num cont rpllbl fmt
'preprocess
)
1457 (or (and org-export-odt-fontify-srcblocks
1458 (or (featurep 'htmlfontify
)
1459 ;; htmlfontify.el was introduced in Emacs 23.2
1460 ;; So load it with some caution
1461 (require 'htmlfontify nil t
))
1462 (fboundp 'htmlfontify-string
)
1463 'org-odt-format-source-code-or-example-colored
)
1464 'org-odt-format-source-code-or-example-plain
)
1465 lines lang caption textareap cols rows num cont rpllbl fmt
))
1467 (let ((extra (format " text:continue-numbering=\"%s\""
1468 (if cont
"true" "false"))))
1469 (org-odt-format-tags
1470 '("<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>"
1471 .
"</text:list>") lines extra
))))
1473 (defun org-odt-remap-stylenames (style-name)
1475 (cdr (assoc style-name
'(("timestamp-wrapper" .
"OrgTimestampWrapper")
1476 ("timestamp" .
"OrgTimestamp")
1477 ("timestamp-kwd" .
"OrgTimestampKeyword")
1479 ("todo" .
"OrgTodo")
1480 ("done" .
"OrgDone")
1481 ("target" .
"OrgTarget"))))
1484 (defun org-odt-format-fontify (text style
&optional id
)
1488 (org-odt-remap-stylenames style
))
1490 (org-odt-get-style-name-for-entity 'character style
))
1492 (assert (< 1 (length style
)))
1493 (let ((parent-style (pop style
)))
1494 (mapconcat (lambda (s)
1495 ;; (assert (stringp s) t)
1496 (org-odt-remap-stylenames s
)) style
"")
1497 (org-odt-remap-stylenames parent-style
)))
1498 (t (error "Don't how to handle style %s" style
)))))
1499 (org-odt-format-tags
1500 '("<text:span text:style-name=\"%s\">" .
"</text:span>")
1503 (defun org-odt-relocate-relative-path (path dir
)
1504 (if (file-name-absolute-p path
) path
1505 (file-relative-name (expand-file-name path dir
)
1506 (expand-file-name "eyecandy" dir
))))
1508 (defun org-odt-format-inline-image (thefile)
1509 (let* ((thelink (if (file-name-absolute-p thefile
) thefile
1510 (org-xml-format-href
1511 (org-odt-relocate-relative-path
1512 thefile org-current-export-file
))))
1514 (org-odt-format-tags
1515 "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
1516 (if org-export-odt-embed-images
1517 (org-odt-copy-image-file thefile
) thelink
))))
1518 (org-export-odt-format-image thefile href
)))
1520 (defvar org-odt-entity-labels-alist nil
1521 "Associate Labels with the Labeled entities.
1522 Each element of the alist is of the form (LABEL-NAME
1523 CATEGORY-NAME SEQNO LABEL-STYLE-NAME). LABEL-NAME is same as
1524 that specified by \"#+LABEL: ...\" line. CATEGORY-NAME is the
1525 type of the entity that LABEL-NAME is attached to. CATEGORY-NAME
1526 can be one of \"Table\", \"Figure\" or \"Equation\". SEQNO is
1527 the unique number assigned to the referenced entity on a
1528 per-CATEGORY basis. It is generated sequentially and is 1-based.
1529 LABEL-STYLE-NAME is a key `org-odt-label-styles'.
1531 See `org-odt-add-label-definition' and
1532 `org-odt-fixup-label-references'.")
1534 (defun org-export-odt-format-formula (src href
)
1536 (let* ((caption (org-find-text-property-in-string 'org-caption src
))
1538 (or (org-find-text-property-in-string 'org-caption-shortn src
)
1540 (caption (and caption
(org-xml-format-desc caption
)))
1541 (short-caption (and short-caption
1542 (org-xml-encode-plain-text short-caption
)))
1543 (label (org-find-text-property-in-string 'org-label src
))
1544 (latex-frag (org-find-text-property-in-string 'org-latex-src src
))
1545 (embed-as (or (and latex-frag
1546 (org-find-text-property-in-string
1547 'org-latex-src-embed-type src
))
1548 (if (or caption label
) 'paragraph
'character
)))
1551 (setq href
(org-propertize href
:title
"LaTeX Fragment"
1552 :description latex-frag
)))
1554 ((eq embed-as
'character
)
1555 (org-odt-format-entity "InlineFormula" href width height
))
1557 (org-lparse-end-paragraph)
1558 (org-lparse-insert-list-table
1559 `((,(org-odt-format-entity
1560 (if (not (or caption label
)) "DisplayFormula"
1561 "CaptionedDisplayFormula")
1562 href width height
:caption caption
:label label
1563 :short-caption short-caption
)
1564 ,(if (not (or caption label
)) ""
1565 (let* ((label-props (car org-odt-entity-labels-alist
)))
1566 (setcar (last label-props
) "math-label")
1567 (apply 'org-odt-format-label-definition
1568 caption label-props
)))))
1569 nil nil nil
":style \"OrgEquation\"" nil
'((1 "c" 8) (2 "c" 1)))
1570 (throw 'nextline nil
))))))
1572 (defvar org-odt-embedded-formulas-count
0)
1573 (defun org-odt-copy-formula-file (path)
1574 "Returns the internal name of the file"
1575 (let* ((src-file (expand-file-name
1576 path
(file-name-directory org-current-export-file
)))
1577 (target-dir (format "Formula-%04d/"
1578 (incf org-odt-embedded-formulas-count
)))
1579 (target-file (concat target-dir
"content.xml")))
1580 (when (not org-lparse-to-buffer
)
1581 (message "Embedding %s as %s ..."
1582 (substring-no-properties path
) target-file
)
1584 (make-directory target-dir
)
1585 (org-odt-create-manifest-file-entry
1586 "application/vnd.oasis.opendocument.formula" target-dir
"1.2")
1588 (case (org-odt-is-formula-link-p src-file
)
1590 (copy-file src-file target-file
'overwrite
))
1592 (org-odt-zip-extract-one src-file
"content.xml" target-dir
))
1594 (error "%s is not a formula file" src-file
)))
1596 (org-odt-create-manifest-file-entry "text/xml" target-file
))
1599 (defun org-odt-format-inline-formula (thefile)
1600 (let* ((thelink (if (file-name-absolute-p thefile
) thefile
1601 (org-xml-format-href
1602 (org-odt-relocate-relative-path
1603 thefile org-current-export-file
))))
1605 (org-odt-format-tags
1606 "<draw:object xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
1607 (file-name-directory (org-odt-copy-formula-file thefile
)))))
1608 (org-export-odt-format-formula thefile href
)))
1610 (defun org-odt-is-formula-link-p (file)
1611 (let ((case-fold-search nil
))
1613 ((string-match "\\.\\(mathml\\|mml\\)\\'" file
)
1615 ((string-match "\\.odf\\'" file
)
1618 (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
1620 "Make a OpenDocument link.
1621 OPT-PLIST is an options list.
1622 TYPE-1 is the device-type of the link (THIS://foo.html).
1623 PATH is the path of the link (http://THIS#location).
1624 FRAGMENT is the fragment part of the link, if any (foo.html#THIS).
1625 DESC is the link description, if any.
1626 ATTR is a string of other attributes of the a element."
1627 (declare (special org-lparse-par-open
))
1629 (let* ((may-inline-p
1630 (and (member type-1
'("http" "https" "file"))
1631 (org-lparse-should-inline-p path descp
)
1633 (type (if (equal type-1
"id") "file" type-1
))
1638 ;; check for inlined images
1639 ((and (member type
'("file"))
1642 filename org-export-odt-inline-image-extensions
)
1643 (or (eq t org-export-odt-inline-images
)
1644 (and org-export-odt-inline-images
(not descp
))))
1645 (org-odt-format-inline-image thefile
))
1646 ;; check for embedded formulas
1647 ((and (member type
'("file"))
1649 (org-odt-is-formula-link-p filename
)
1651 (org-odt-format-inline-formula thefile
))
1653 ((string= type
"coderef")
1654 (let* ((ref fragment
)
1655 (lineno-or-ref (cdr (assoc ref org-export-code-refs
)))
1656 (desc (and descp desc
))
1657 (org-odt-suppress-xref nil
)
1658 (href (org-xml-format-href (concat "#coderef-" ref
))))
1660 ((and (numberp lineno-or-ref
) (not desc
))
1661 (org-odt-format-link lineno-or-ref href
))
1662 ((and (numberp lineno-or-ref
) desc
1663 (string-match (regexp-quote (concat "(" ref
")")) desc
))
1664 (format (replace-match "%s" t t desc
)
1665 (org-odt-format-link lineno-or-ref href
)))
1668 (if (and desc
(string-match
1669 (regexp-quote (concat "(" ref
")"))
1671 (replace-match "%s" t t desc
)
1674 (org-odt-format-link (org-xml-format-desc desc
) href
)))))
1675 ;; links to headlines
1676 ((and (string= type
"")
1677 (or (not thefile
) (string= thefile
""))
1678 (plist-get org-lparse-opt-plist
:section-numbers
)
1679 (setq sec-frag fragment
)
1680 (or (string-match "\\`sec\\(\\(-[0-9]+\\)+\\)" sec-frag
)
1682 (loop for alias in org-export-target-aliases do
1683 (when (member fragment
(cdr alias
))
1684 (return (car alias
)))))
1685 (string-match "\\`sec\\(\\(-[0-9]+\\)+\\)" sec-frag
)))
1686 (setq sec-nos
(org-split-string (match-string 1 sec-frag
) "-"))
1687 (<= (length sec-nos
) (plist-get org-lparse-opt-plist
1689 (let ((org-odt-suppress-xref nil
))
1690 (org-odt-format-link sec-nos
(concat "#" sec-frag
) attr
)))
1692 (when (string= type
"file")
1695 ((file-name-absolute-p path
)
1696 (concat "file://" (expand-file-name path
)))
1697 (t (org-odt-relocate-relative-path
1698 thefile org-current-export-file
)))))
1700 (when (and (member type
'("" "http" "https" "file")) fragment
)
1701 (setq thefile
(concat thefile
"#" fragment
)))
1703 (setq thefile
(org-xml-format-href thefile
))
1705 (when (not (member type
'("" "file")))
1706 (setq thefile
(concat type
":" thefile
)))
1708 (let ((org-odt-suppress-xref nil
))
1709 (org-odt-format-link
1710 (org-xml-format-desc desc
) thefile attr
)))))))
1712 (defun org-odt-format-heading (text level
&optional id
)
1713 (let* ((text (if id
(org-odt-format-target text id
) text
)))
1714 (org-odt-format-tags
1715 '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
1716 "</text:h>") text level level
)))
1718 (defun org-odt-format-headline (title extra-targets tags
1719 &optional snumber level
)
1721 (org-lparse-format 'EXTRA-TARGETS extra-targets
)
1723 ;; No need to generate section numbers. They are auto-generated by
1726 ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
1728 (and tags
(concat (org-lparse-format 'SPACES
3)
1729 (org-lparse-format 'ORG-TAGS tags
)))))
1731 (defun org-odt-format-anchor (text name
&optional class
)
1732 (org-odt-format-target text name
))
1734 (defun org-odt-format-bookmark (text id
)
1736 (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id
)
1739 (defun org-odt-format-target (text id
)
1740 (let ((name (concat org-export-odt-bookmark-prefix id
)))
1742 (and id
(org-odt-format-tags
1743 "<text:bookmark-start text:name=\"%s\"/>" "" name
))
1744 (org-odt-format-bookmark text id
)
1745 (and id
(org-odt-format-tags
1746 "<text:bookmark-end text:name=\"%s\"/>" "" name
)))))
1748 (defun org-odt-format-footnote (n def
)
1749 (let ((id (concat "fn" n
))
1750 (note-class "footnote")
1751 (par-style "Footnote"))
1752 (org-odt-format-tags
1753 '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
1756 (org-odt-format-tags
1757 '("<text:note-citation>" .
"</text:note-citation>")
1759 (org-odt-format-tags
1760 '("<text:note-body>" .
"</text:note-body>")
1764 (defun org-odt-format-footnote-reference (n def refcnt
)
1766 (org-odt-format-footnote n def
)
1767 (org-odt-format-footnote-ref n
)))
1769 (defun org-odt-format-footnote-ref (n)
1770 (let ((note-class "footnote")
1772 (ref-name (concat "fn" n
)))
1773 (org-odt-format-tags
1774 '("<text:span text:style-name=\"%s\">" .
"</text:span>")
1775 (org-odt-format-tags
1776 '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" .
"</text:note-ref>")
1777 n note-class ref-format ref-name
)
1780 (defun org-odt-get-image-name (file-name)
1784 (concat (sha1 file-name
) "." (file-name-extension file-name
)) "Pictures")))
1786 (defun org-export-odt-format-image (src href
)
1787 "Create image tag with source and attributes."
1789 (let* ((caption (org-find-text-property-in-string 'org-caption src
))
1791 (or (org-find-text-property-in-string 'org-caption-shortn src
)
1793 (caption (and caption
(org-xml-format-desc caption
)))
1794 (short-caption (and short-caption
1795 (org-xml-encode-plain-text short-caption
)))
1796 (attr (org-find-text-property-in-string 'org-attributes src
))
1797 (label (org-find-text-property-in-string 'org-label src
))
1798 (latex-frag (org-find-text-property-in-string
1799 'org-latex-src src
))
1800 (category (and latex-frag
"__DvipngImage__"))
1801 (attr-plist (org-lparse-get-block-params attr
))
1803 (car (assoc-string (plist-get attr-plist
:anchor
)
1804 '(("as-char") ("paragraph") ("page")) t
)))
1806 (and user-frame-anchor
(plist-get attr-plist
:style
)))
1808 (and user-frame-anchor
(plist-get attr-plist
:attributes
)))
1810 (list user-frame-style user-frame-attrs user-frame-anchor
))
1814 (case (org-find-text-property-in-string
1815 'org-latex-src-embed-type src
)
1816 (paragraph 'paragraph
)
1820 (size (org-odt-image-size-from-file
1821 src
(plist-get attr-plist
:width
)
1822 (plist-get attr-plist
:height
)
1823 (plist-get attr-plist
:scale
) nil embed-as
))
1824 (width (car size
)) (height (cdr size
)))
1826 (setq href
(org-propertize href
:title
"LaTeX Fragment"
1827 :description latex-frag
)))
1828 (let ((frame-style-handle (concat (and (or caption label
) "Captioned")
1830 (org-odt-format-entity
1831 frame-style-handle href width height
1832 :caption caption
:label label
:category category
1833 :short-caption short-caption
1834 :user-frame-params user-frame-params
)))))
1836 (defun org-odt-format-object-description (title description
)
1837 (concat (and title
(org-odt-format-tags
1838 '("<svg:title>" .
"</svg:title>")
1839 (org-odt-encode-plain-text title t
)))
1840 (and description
(org-odt-format-tags
1841 '("<svg:desc>" .
"</svg:desc>")
1842 (org-odt-encode-plain-text description t
)))))
1844 (defun org-odt-format-frame (text width height style
&optional
1848 (if width
(format " svg:width=\"%0.2fcm\"" width
) "")
1849 (if height
(format " svg:height=\"%0.2fcm\"" height
) "")
1851 (format " text:anchor-type=\"%s\"" (or anchor-type
"paragraph")))))
1852 (org-odt-format-tags
1853 '("<draw:frame draw:style-name=\"%s\"%s>" .
"</draw:frame>")
1854 (concat text
(org-odt-format-object-description
1855 (get-text-property 0 :title text
)
1856 (get-text-property 0 :description text
)))
1857 style frame-attrs
)))
1859 (defun org-odt-format-textbox (text width height style
&optional
1861 (org-odt-format-frame
1862 (org-odt-format-tags
1863 '("<draw:text-box %s>" .
"</draw:text-box>")
1864 text
(concat (format " fo:min-height=\"%0.2fcm\"" (or height
.2))
1866 (format " fo:min-width=\"%0.2fcm\"" (or width
.2)))))
1867 width nil style extra anchor-type
))
1869 (defun org-odt-format-inlinetask (heading content
1870 &optional todo priority tags
)
1871 (org-odt-format-stylized-paragraph
1872 nil
(org-odt-format-textbox
1873 (concat (org-odt-format-stylized-paragraph
1874 "OrgInlineTaskHeading"
1876 'HEADLINE
(concat (org-lparse-format-todo todo
) " " heading
)
1878 content
) nil nil
"OrgInlineTaskFrame" " style:rel-width=\"100%\"")))
1880 (defvar org-odt-entity-frame-styles
1881 '(("As-CharImage" "__Figure__" ("OrgInlineImage" nil
"as-char"))
1882 ("ParagraphImage" "__Figure__" ("OrgDisplayImage" nil
"paragraph"))
1883 ("PageImage" "__Figure__" ("OrgPageImage" nil
"page"))
1884 ("CaptionedAs-CharImage" "__Figure__"
1885 ("OrgCaptionedImage"
1886 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
1887 ("OrgInlineImage" nil
"as-char"))
1888 ("CaptionedParagraphImage" "__Figure__"
1889 ("OrgCaptionedImage"
1890 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
1891 ("OrgImageCaptionFrame" nil
"paragraph"))
1892 ("CaptionedPageImage" "__Figure__"
1893 ("OrgCaptionedImage"
1894 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
1895 ("OrgPageImageCaptionFrame" nil
"page"))
1896 ("InlineFormula" "__MathFormula__" ("OrgInlineFormula" nil
"as-char"))
1897 ("DisplayFormula" "__MathFormula__" ("OrgDisplayFormula" nil
"as-char"))
1898 ("CaptionedDisplayFormula" "__MathFormula__"
1899 ("OrgCaptionedFormula" nil
"paragraph")
1900 ("OrgFormulaCaptionFrame" nil
"as-char"))))
1902 (defun org-odt-merge-frame-params(default-frame-params user-frame-params
)
1903 (if (not user-frame-params
) default-frame-params
1904 (assert (= (length default-frame-params
) 3))
1905 (assert (= (length user-frame-params
) 3))
1906 (loop for user-frame-param in user-frame-params
1907 for default-frame-param in default-frame-params
1908 collect
(or user-frame-param default-frame-param
))))
1910 (defun* org-odt-format-entity
(entity href width height
1911 &key caption label category
1912 user-frame-params short-caption
)
1913 (let* ((entity-style (assoc-string entity org-odt-entity-frame-styles t
))
1914 default-frame-params frame-params
)
1916 ((not (or caption label
))
1917 (setq default-frame-params
(nth 2 entity-style
))
1918 (setq frame-params
(org-odt-merge-frame-params
1919 default-frame-params user-frame-params
))
1920 (apply 'org-odt-format-frame href width height frame-params
))
1922 (setq default-frame-params
(nth 3 entity-style
))
1923 (setq frame-params
(org-odt-merge-frame-params
1924 default-frame-params user-frame-params
))
1925 (apply 'org-odt-format-textbox
1926 (org-odt-format-stylized-paragraph
1929 (apply 'org-odt-format-frame href width height
1930 (let ((entity-style-1 (copy-sequence
1931 (nth 2 entity-style
))))
1932 (setcar (cdr entity-style-1
)
1934 (cadr entity-style-1
)
1936 (format " draw:name=\"%s\" "
1940 (org-odt-format-entity-caption
1941 label caption
(or category
(nth 1 entity-style
)))))
1942 width height frame-params
)))))
1944 (defvar org-odt-embedded-images-count
0)
1945 (defun org-odt-copy-image-file (path)
1946 "Returns the internal name of the file"
1947 (let* ((image-type (file-name-extension path
))
1948 (media-type (format "image/%s" image-type
))
1949 (src-file (expand-file-name
1950 path
(file-name-directory org-current-export-file
)))
1951 (target-dir "Images/")
1953 (format "%s%04d.%s" target-dir
1954 (incf org-odt-embedded-images-count
) image-type
)))
1955 (when (not org-lparse-to-buffer
)
1956 (message "Embedding %s as %s ..."
1957 (substring-no-properties path
) target-file
)
1959 (when (= 1 org-odt-embedded-images-count
)
1960 (make-directory target-dir
)
1961 (org-odt-create-manifest-file-entry "" target-dir
))
1963 (copy-file src-file target-file
'overwrite
)
1964 (org-odt-create-manifest-file-entry media-type target-file
))
1967 (defvar org-export-odt-image-size-probe-method
1968 (append (and (executable-find "identify") '(imagemagick)) ; See Bug#10675
1970 "Ordered list of methods for determining image sizes.")
1972 (defvar org-export-odt-default-image-sizes-alist
1973 '(("as-char" .
(5 .
0.4))
1974 ("paragraph" .
(5 .
5)))
1975 "Hardcoded image dimensions one for each of the anchor
1978 ;; A4 page size is 21.0 by 29.7 cms
1979 ;; The default page settings has 2cm margin on each of the sides. So
1980 ;; the effective text area is 17.0 by 25.7 cm
1981 (defvar org-export-odt-max-image-size
'(17.0 .
20.0)
1982 "Limiting dimensions for an embedded image.")
1984 (defun org-odt-do-image-size (probe-method file
&optional dpi anchor-type
)
1985 (let* ((dpi (or dpi org-export-odt-pixels-per-inch
))
1986 (anchor-type (or anchor-type
"paragraph"))
1989 (lambda (pixels dpi
)
1990 (let* ((cms-per-inch 2.54)
1991 (inches (/ pixels dpi
)))
1992 (* cms-per-inch inches
)))))
1995 (lambda (size-in-pixels dpi
)
1997 (cons (funcall --pixels-to-cms
(car size-in-pixels
) dpi
)
1998 (funcall --pixels-to-cms
(cdr size-in-pixels
) dpi
)))))))
2001 (let ((size-in-pixels
2002 (ignore-errors ; Emacs could be in batch mode
2004 (image-size (create-image file
) 'pixels
))))
2005 (funcall --size-in-cms size-in-pixels dpi
)))
2007 (let ((size-in-pixels
2008 (let ((dim (shell-command-to-string
2009 (format "identify -format \"%%w:%%h\" \"%s\"" file
))))
2010 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim
)
2011 (cons (string-to-number (match-string 1 dim
))
2012 (string-to-number (match-string 2 dim
)))))))
2013 (funcall --size-in-cms size-in-pixels dpi
)))
2014 (t (cdr (assoc-string anchor-type
2015 org-export-odt-default-image-sizes-alist
))))))
2017 (defun org-odt-image-size-from-file (file &optional user-width
2018 user-height scale dpi embed-as
)
2019 (unless (file-name-absolute-p file
)
2020 (setq file
(expand-file-name
2021 file
(file-name-directory org-current-export-file
))))
2022 (let* (size width height
)
2023 (unless (and user-height user-width
)
2024 (loop for probe-method in org-export-odt-image-size-probe-method
2026 do
(setq size
(org-odt-do-image-size
2027 probe-method file dpi embed-as
)))
2028 (or size
(error "Cannot determine image size, aborting"))
2029 (setq width
(car size
) height
(cdr size
)))
2032 (setq width
(* width scale
) height
(* height scale
)))
2033 ((and user-height user-width
)
2034 (setq width user-width height user-height
))
2036 (setq width
(* user-height
(/ width height
)) height user-height
))
2038 (setq height
(* user-width
(/ height width
)) width user-width
))
2040 ;; ensure that an embedded image fits comfortably within a page
2041 (let ((max-width (car org-export-odt-max-image-size
))
2042 (max-height (cdr org-export-odt-max-image-size
)))
2043 (when (or (> width max-width
) (> height max-height
))
2044 (let* ((scale1 (/ max-width width
))
2045 (scale2 (/ max-height height
))
2046 (scale (min scale1 scale2
)))
2047 (setq width
(* scale width
) height
(* scale height
)))))
2048 (cons width height
)))
2050 (defvar org-odt-entity-counts-plist nil
2051 "Plist of running counters of SEQNOs for each of the CATEGORY-NAMEs.
2052 See `org-odt-entity-labels-alist' for known CATEGORY-NAMEs.")
2054 (defvar org-odt-label-styles
2055 '(("math-formula" "%c" "text" "(%n)")
2056 ("math-label" "(%n)" "text" "(%n)")
2057 ("category-and-value" "%e %n: %c" "category-and-value" "%e %n")
2058 ("value" "%e %n: %c" "value" "%n"))
2059 "Specify how labels are applied and referenced.
2060 This is an alist where each element is of the
2061 form (LABEL-STYLE-NAME LABEL-ATTACH-FMT LABEL-REF-MODE
2064 LABEL-ATTACH-FMT controls how labels and captions are attached to
2065 an entity. It may contain following specifiers - %e, %n and %c.
2066 %e is replaced with the CATEGORY-NAME. %n is replaced with
2067 \"<text:sequence ...> SEQNO </text:sequence>\". %c is replaced
2068 with CAPTION. See `org-odt-format-label-definition'.
2070 LABEL-REF-MODE and LABEL-REF-FMT controls how label references
2071 are generated. The following XML is generated for a label
2072 reference - \"<text:sequence-ref
2073 text:reference-format=\"LABEL-REF-MODE\" ...> LABEL-REF-FMT
2074 </text:sequence-ref>\". LABEL-REF-FMT may contain following
2075 specifiers - %e and %n. %e is replaced with the CATEGORY-NAME.
2076 %n is replaced with SEQNO. See
2077 `org-odt-format-label-reference'.")
2079 (defcustom org-export-odt-category-strings
2080 '(("en" "Table" "Figure" "Equation" "Equation"))
2081 "Specify category strings for various captionable entities.
2082 Captionable entity can be one of a Table, an Embedded Image, a
2083 LaTeX fragment (generated with dvipng) or a Math Formula.
2085 For example, when `org-export-default-language' is \"en\", an
2086 embedded image will be captioned as \"Figure 1: Orgmode Logo\".
2087 If you want the images to be captioned instead as \"Illustration
2088 1: Orgmode Logo\", then modify the entry for \"en\" as shown
2091 \(setq org-export-odt-category-strings
2092 '\(\(\"en\" \"Table\" \"Illustration\"
2093 \"Equation\" \"Equation\"\)\)\)"
2094 :group
'org-export-odt
2096 :type
'(repeat (list (string :tag
"Language tag")
2097 (choice :tag
"Table"
2098 (const :tag
"Use Default" nil
)
2099 (string :tag
"Category string"))
2100 (choice :tag
"Figure"
2101 (const :tag
"Use Default" nil
)
2102 (string :tag
"Category string"))
2103 (choice :tag
"Math Formula"
2104 (const :tag
"Use Default" nil
)
2105 (string :tag
"Category string"))
2106 (choice :tag
"Dvipng Image"
2107 (const :tag
"Use Default" nil
)
2108 (string :tag
"Category string")))))
2110 (defvar org-odt-category-map-alist
2111 '(("__Table__" "Table" "value")
2112 ("__Figure__" "Illustration" "value")
2113 ("__MathFormula__" "Text" "math-formula")
2114 ("__DvipngImage__" "Equation" "value")
2115 ;; ("__Table__" "Table" "category-and-value")
2116 ;; ("__Figure__" "Figure" "category-and-value")
2117 ;; ("__DvipngImage__" "Equation" "category-and-value")
2119 "Map a CATEGORY-HANDLE to OD-VARIABLE and LABEL-STYLE.
2120 This is a list where each entry is of the form \\(CATEGORY-HANDLE
2121 OD-VARIABLE LABEL-STYLE\\). CATEGORY_HANDLE identifies the
2122 captionable entity in question. OD-VARIABLE is the OpenDocument
2123 sequence counter associated with the entity. These counters are
2125 \"<text:sequence-decls>...</text:sequence-decls>\" block of
2126 `org-export-odt-content-template-file'. LABEL-STYLE is a key
2127 into `org-odt-label-styles' and specifies how a given entity
2128 should be captioned and referenced.
2130 The position of a CATEGORY-HANDLE in this list is used as an
2131 index in to per-language entry for
2132 `org-export-odt-category-strings' to retrieve a CATEGORY-NAME.
2133 This CATEGORY-NAME is then used for qualifying the user-specified
2134 captions on export.")
2136 (defun org-odt-add-label-definition (label default-category
)
2137 "Create an entry in `org-odt-entity-labels-alist' and return it."
2138 (let* ((label-props (assoc default-category org-odt-category-map-alist
))
2139 ;; identify the sequence number
2140 (counter (nth 1 label-props
))
2141 (sequence-var (intern counter
))
2142 (seqno (1+ (or (plist-get org-odt-entity-counts-plist sequence-var
)
2144 ;; assign an internal label, if user has not provided one
2145 (label (if label
(substring-no-properties label
)
2146 (format "%s-%s" default-category seqno
)))
2147 ;; identify label style
2148 (label-style (nth 2 label-props
))
2149 ;; grok language setting
2150 (en-strings (assoc-default "en" org-export-odt-category-strings
))
2151 (lang (plist-get org-lparse-opt-plist
:language
))
2152 (lang-strings (assoc-default lang org-export-odt-category-strings
))
2153 ;; retrieve localized category sting
2154 (pos (- (length org-odt-category-map-alist
)
2155 (length (memq label-props org-odt-category-map-alist
))))
2156 (category (or (nth pos lang-strings
) (nth pos en-strings
)))
2157 (label-props (list label category counter seqno label-style
)))
2158 ;; synchronize internal counters
2159 (setq org-odt-entity-counts-plist
2160 (plist-put org-odt-entity-counts-plist sequence-var seqno
))
2161 ;; stash label properties for later retrieval
2162 (push label-props org-odt-entity-labels-alist
)
2165 (defun org-odt-format-label-definition (caption label category counter
2169 (cadr (assoc-string label-style org-odt-label-styles t
))
2171 (?n .
,(org-odt-format-tags
2172 '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" .
"</text:sequence>")
2173 (format "%d" seqno
) label counter counter
))
2174 (?c .
,(or caption
"")))))
2176 (defun org-odt-format-label-reference (label category counter
2180 (let* ((fmt (cddr (assoc-string label-style org-odt-label-styles t
)))
2183 (org-odt-format-tags
2184 '("<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">"
2185 .
"</text:sequence-ref>")
2186 (format-spec fmt2
`((?e .
,category
)
2187 (?n .
,(format "%d" seqno
)))) fmt1 label
))))
2189 (defun org-odt-fixup-label-references ()
2190 (goto-char (point-min))
2191 (while (re-search-forward
2192 "<text:sequence-ref text:ref-name=\"\\([^\"]+\\)\">[ \t\n]*</text:sequence-ref>"
2194 (let* ((label (match-string 1))
2195 (label-def (assoc label org-odt-entity-labels-alist
))
2197 (apply 'org-odt-format-label-reference label-def
))))
2198 (if rpl
(replace-match rpl t t
)
2200 (format "Unable to resolve reference to label \"%s\"" label
))))))
2202 (defun org-odt-format-entity-caption (label caption category
)
2203 (if (not (or label caption
)) ""
2204 (apply 'org-odt-format-label-definition caption
2205 (org-odt-add-label-definition label category
))))
2207 (defun org-odt-format-tags (tag text
&rest args
)
2208 (let ((prefix (when org-lparse-encode-pending
"@"))
2209 (suffix (when org-lparse-encode-pending
"@")))
2210 (apply 'org-lparse-format-tags tag text prefix suffix args
)))
2212 (defvar org-odt-manifest-file-entries nil
)
2213 (defun org-odt-init-outfile (filename)
2214 (unless (executable-find "zip")
2215 ;; Not at all OSes ship with zip by default
2216 (error "Executable \"zip\" needed for creating OpenDocument files"))
2218 (let* ((content-file (expand-file-name "content.xml" org-odt-zip-dir
)))
2220 (require 'nxml-mode
)
2221 (let ((nxml-auto-insert-xml-declaration-flag nil
))
2222 (find-file-noselect content-file t
))
2225 (setq org-odt-manifest-file-entries nil
2226 org-odt-embedded-images-count
0
2227 org-odt-embedded-formulas-count
0
2228 org-odt-entity-labels-alist nil
2229 org-odt-list-stack-stashed nil
2230 org-odt-automatic-styles nil
2231 org-odt-object-counters nil
2232 org-odt-entity-counts-plist nil
)
2235 (defcustom org-export-odt-prettify-xml nil
2236 "Specify whether or not the xml output should be prettified.
2237 When this option is turned on, `indent-region' is run on all
2238 component xml buffers before they are saved. Turn this off for
2239 regular use. Turn this on if you need to examine the xml
2241 :group
'org-export-odt
2245 (defvar hfy-user-sheet-assoc
) ; bound during org-do-lparse
2246 (defun org-odt-save-as-outfile (target opt-plist
)
2247 ;; write automatic styles
2248 (org-odt-write-automatic-styles)
2251 (org-odt-update-meta-file opt-plist
)
2253 ;; write styles file
2254 (when (equal org-lparse-backend
'odt
)
2255 (org-odt-update-styles-file opt-plist
))
2257 ;; create mimetype file
2258 (let ((mimetype (org-odt-write-mimetype-file org-lparse-backend
)))
2259 (org-odt-create-manifest-file-entry mimetype
"/" "1.2"))
2261 ;; create a manifest entry for content.xml
2262 (org-odt-create-manifest-file-entry "text/xml" "content.xml")
2264 ;; write out the manifest entries before zipping
2265 (org-odt-write-manifest-file)
2267 (let ((xml-files '("mimetype" "META-INF/manifest.xml" "content.xml"
2269 (when (equal org-lparse-backend
'odt
)
2270 (push "styles.xml" xml-files
))
2272 ;; save all xml files
2273 (mapc (lambda (file)
2274 (with-current-buffer
2275 (find-file-noselect (expand-file-name file
) t
)
2276 ;; prettify output if needed
2277 (when org-export-odt-prettify-xml
2278 (indent-region (point-min) (point-max)))
2282 (let* ((target-name (file-name-nondirectory target
))
2283 (target-dir (file-name-directory target
))
2284 (cmds `(("zip" "-mX0" ,target-name
"mimetype")
2285 ("zip" "-rmTq" ,target-name
"."))))
2286 (when (file-exists-p target
)
2287 ;; FIXME: If the file is locked this throws a cryptic error
2288 (delete-file target
))
2290 (let ((coding-system-for-write 'no-conversion
) exitcode err-string
)
2291 (message "Creating odt file...")
2294 (message "Running %s" (mapconcat 'identity cmd
" "))
2296 (with-output-to-string
2298 (apply 'call-process
(car cmd
)
2299 nil standard-output nil
(cdr cmd
)))))
2300 (or (zerop exitcode
)
2301 (ignore (message "%s" err-string
))
2302 (error "Unable to create odt file (%S)" exitcode
)))
2305 ;; move the file from outdir to target-dir
2306 (rename-file target-name target-dir
)))
2308 (message "Created %s" target
)
2309 (set-buffer (find-file-noselect target t
)))
2311 (defconst org-odt-manifest-file-entry-tag
2313 <manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
2315 (defun org-odt-create-manifest-file-entry (&rest args
)
2316 (push args org-odt-manifest-file-entries
))
2318 (defun org-odt-write-manifest-file ()
2319 (make-directory "META-INF")
2320 (let ((manifest-file (expand-file-name "META-INF/manifest.xml")))
2321 (with-current-buffer
2322 (let ((nxml-auto-insert-xml-declaration-flag nil
))
2323 (find-file-noselect manifest-file t
))
2325 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
2326 <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
2328 (lambda (file-entry)
2329 (let* ((version (nth 2 file-entry
))
2331 (format " manifest:version=\"%s\"" version
)
2334 (format org-odt-manifest-file-entry-tag
2335 (nth 0 file-entry
) (nth 1 file-entry
) extra
))))
2336 org-odt-manifest-file-entries
)
2337 (insert "\n</manifest:manifest>"))))
2339 (defun org-odt-update-meta-file (opt-plist)
2340 (let ((date (org-odt-format-date (plist-get opt-plist
:date
)))
2341 (author (or (plist-get opt-plist
:author
) ""))
2342 (email (plist-get opt-plist
:email
))
2343 (keywords (plist-get opt-plist
:keywords
))
2344 (description (plist-get opt-plist
:description
))
2345 (title (plist-get opt-plist
:title
)))
2348 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
2349 <office:document-meta
2350 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
2351 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
2352 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
2353 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
2354 xmlns:ooo=\"http://openoffice.org/2004/office\"
2355 office:version=\"1.2\">
2357 (org-odt-format-author)
2358 (org-odt-format-tags
2359 '("\n<meta:initial-creator>" .
"</meta:initial-creator>") author
)
2360 (org-odt-format-tags '("\n<dc:date>" .
"</dc:date>") date
)
2361 (org-odt-format-tags
2362 '("\n<meta:creation-date>" .
"</meta:creation-date>") date
)
2363 (org-odt-format-tags '("\n<meta:generator>" .
"</meta:generator>")
2364 (when org-export-creator-info
2365 (format "Org-%s/Emacs-%s"
2368 (org-odt-format-tags '("\n<meta:keyword>" .
"</meta:keyword>") keywords
)
2369 (org-odt-format-tags '("\n<dc:subject>" .
"</dc:subject>") description
)
2370 (org-odt-format-tags '("\n<dc:title>" .
"</dc:title>") title
)
2372 " </office:meta>" "</office:document-meta>")
2373 nil
(expand-file-name "meta.xml")))
2375 ;; create a manifest entry for meta.xml
2376 (org-odt-create-manifest-file-entry "text/xml" "meta.xml"))
2378 (defun org-odt-update-styles-file (opt-plist)
2379 ;; write styles file
2380 (let ((styles-file (plist-get opt-plist
:odt-styles-file
)))
2381 (org-odt-copy-styles-file (and styles-file
2382 (read (org-trim styles-file
)))))
2384 ;; Update styles.xml - take care of outline numbering
2385 (with-current-buffer
2386 (find-file-noselect (expand-file-name "styles.xml") t
)
2387 ;; Don't make automatic backup of styles.xml file. This setting
2388 ;; prevents the backed-up styles.xml file from being zipped in to
2389 ;; odt file. This is more of a hackish fix. Better alternative
2390 ;; would be to fix the zip command so that the output odt file
2391 ;; includes only the needed files and excludes any auto-generated
2392 ;; extra files like backups and auto-saves etc etc. Note that
2393 ;; currently the zip command zips up the entire temp directory so
2394 ;; that any auto-generated files created under the hood ends up in
2395 ;; the resulting odt file.
2396 (set (make-local-variable 'backup-inhibited
) t
)
2398 ;; Import local setting of `org-export-with-section-numbers'
2399 (org-lparse-bind-local-variables opt-plist
)
2400 (org-odt-configure-outline-numbering
2401 (if org-export-with-section-numbers org-export-headline-levels
0)))
2403 ;; Write custom styles for source blocks
2404 (org-odt-insert-custom-styles-for-srcblocks
2407 (format " %s\n" (cddr style
)))
2408 hfy-user-sheet-assoc
"")))
2410 (defun org-odt-write-mimetype-file (format)
2411 ;; create mimetype file
2414 (odt "application/vnd.oasis.opendocument.text")
2415 (odf "application/vnd.oasis.opendocument.formula")
2416 (t (error "Unknown OpenDocument backend %S" org-lparse-backend
)))))
2417 (write-region mimetype nil
(expand-file-name "mimetype"))
2420 (defun org-odt-finalize-outfile ()
2421 (org-odt-delete-empty-paragraphs))
2423 (defun org-odt-delete-empty-paragraphs ()
2424 (goto-char (point-min))
2425 (let ((open "<text:p[^>]*>")
2426 (close "</text:p>"))
2427 (while (re-search-forward (format "%s[ \r\n\t]*%s" open close
) nil t
)
2428 (replace-match ""))))
2430 (defcustom org-export-odt-convert-processes
2432 "soffice --headless --convert-to %f%x --outdir %d %i")
2434 "unoconv -f %f -o %d %i"))
2435 "Specify a list of document converters and their usage.
2436 The converters in this list are offered as choices while
2437 customizing `org-export-odt-convert-process'.
2439 This variable is a list where each element is of the
2440 form (CONVERTER-NAME CONVERTER-CMD). CONVERTER-NAME is the name
2441 of the converter. CONVERTER-CMD is the shell command for the
2442 converter and can contain format specifiers. These format
2443 specifiers are interpreted as below:
2445 %i input file name in full
2446 %I input file name as a URL
2447 %f format of the output file
2448 %o output file name in full
2449 %O output file name as a URL
2450 %d output dir in full
2451 %D output dir as a URL.
2452 %x extra options as set in `org-export-odt-convert-capabilities'."
2453 :group
'org-export-odt
2457 (const :tag
"None" nil
)
2458 (alist :tag
"Converters"
2459 :key-type
(string :tag
"Converter Name")
2460 :value-type
(group (string :tag
"Command line")))))
2462 (defcustom org-export-odt-convert-process
"LibreOffice"
2463 "Use this converter to convert from \"odt\" format to other formats.
2464 During customization, the list of converter names are populated
2465 from `org-export-odt-convert-processes'."
2466 :group
'org-export-odt
2468 :type
'(choice :convert-widget
2470 (apply 'widget-convert
(widget-type w
)
2471 (eval (car (widget-get w
:args
)))))
2472 `((const :tag
"None" nil
)
2473 ,@(mapcar (lambda (c)
2474 `(const :tag
,(car c
) ,(car c
)))
2475 org-export-odt-convert-processes
))))
2477 (defcustom org-export-odt-convert-capabilities
2479 ("odt" "ott" "doc" "rtf" "docx")
2480 (("pdf" "pdf") ("odt" "odt") ("rtf" "rtf") ("ott" "ott")
2481 ("doc" "doc" ":\"MS Word 97\"") ("docx" "docx") ("html" "html")))
2484 (("pdf" "pdf") ("odt" "odt") ("html" "html")))
2486 ("ods" "ots" "xls" "csv" "xlsx")
2487 (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv") ("ods" "ods")
2488 ("xls" "xls") ("xlsx" "xlsx")))
2490 ("odp" "otp" "ppt" "pptx")
2491 (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("otp" "otp") ("ppt" "ppt")
2492 ("pptx" "pptx") ("odg" "odg"))))
2493 "Specify input and output formats of `org-export-odt-convert-process'.
2494 More correctly, specify the set of input and output formats that
2495 the user is actually interested in.
2497 This variable is an alist where each element is of the
2498 form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
2499 INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
2500 alist where each element is of the form (OUTPUT-FMT
2501 OUTPUT-FILE-EXTENSION EXTRA-OPTIONS).
2503 The variable is interpreted as follows:
2504 `org-export-odt-convert-process' can take any document that is in
2505 INPUT-FMT-LIST and produce any document that is in the
2506 OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
2507 OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
2508 serves dual purposes:
2509 - It is used for populating completion candidates during
2510 `org-export-odt-convert' commands.
2511 - It is used as the value of \"%f\" specifier in
2512 `org-export-odt-convert-process'.
2514 EXTRA-OPTIONS is used as the value of \"%x\" specifier in
2515 `org-export-odt-convert-process'.
2517 DOCUMENT-CLASS is used to group a set of file formats in
2518 INPUT-FMT-LIST in to a single class.
2520 Note that this variable inherently captures how LibreOffice based
2521 converters work. LibreOffice maps documents of various formats
2522 to classes like Text, Web, Spreadsheet, Presentation etc and
2523 allow document of a given class (irrespective of it's source
2524 format) to be converted to any of the export formats associated
2527 See default setting of this variable for an typical
2529 :group
'org-export-odt
2533 (const :tag
"None" nil
)
2534 (alist :tag
"Capabilities"
2535 :key-type
(string :tag
"Document Class")
2537 (group (repeat :tag
"Input formats" (string :tag
"Input format"))
2538 (alist :tag
"Output formats"
2539 :key-type
(string :tag
"Output format")
2541 (group (string :tag
"Output file extension")
2543 (const :tag
"None" nil
)
2544 (string :tag
"Extra options"))))))))
2546 (declare-function org-create-math-formula
"org"
2547 (latex-frag &optional mathml-file
))
2550 (defun org-export-odt-convert (&optional in-file out-fmt prefix-arg
)
2551 "Convert IN-FILE to format OUT-FMT using a command line converter.
2552 IN-FILE is the file to be converted. If unspecified, it defaults
2553 to variable `buffer-file-name'. OUT-FMT is the desired output
2554 format. Use `org-export-odt-convert-process' as the converter.
2555 If PREFIX-ARG is non-nil then the newly converted file is opened
2556 using `org-open-file'."
2558 (append (org-lparse-convert-read-params) current-prefix-arg
))
2559 (org-lparse-do-convert in-file out-fmt prefix-arg
))
2561 (defun org-odt-get (what &optional opt-plist
)
2564 (EXPORT-DIR (org-export-directory :html opt-plist
))
2565 (FILE-NAME-EXTENSION "odt")
2566 (EXPORT-BUFFER-NAME "*Org ODT Export*")
2567 (ENTITY-CONTROL org-odt-entity-control-callbacks-alist
)
2568 (ENTITY-FORMAT org-odt-entity-format-callbacks-alist
)
2569 (INIT-METHOD 'org-odt-init-outfile
)
2570 (FINAL-METHOD 'org-odt-finalize-outfile
)
2571 (SAVE-METHOD 'org-odt-save-as-outfile
)
2573 (and org-export-odt-convert-process
2574 (cadr (assoc-string org-export-odt-convert-process
2575 org-export-odt-convert-processes t
))))
2576 (CONVERT-CAPABILITIES
2577 (and org-export-odt-convert-process
2578 (cadr (assoc-string org-export-odt-convert-process
2579 org-export-odt-convert-processes t
))
2580 org-export-odt-convert-capabilities
))
2582 (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps
)
2583 (INLINE-IMAGES 'maybe
)
2584 (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
2585 (PLAIN-TEXT-MAP '(("&" .
"&") ("<" .
"<") (">" .
">")))
2586 (TABLE-FIRST-COLUMN-AS-LABELS nil
)
2587 (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY
"," 'superscript
))
2588 (CODING-SYSTEM-FOR-WRITE 'utf-8
)
2589 (CODING-SYSTEM-FOR-SAVE 'utf-8
)
2590 (t (error "Unknown property: %s" what
))))
2592 (defvar org-lparse-latex-fragment-fallback
) ; set by org-do-lparse
2593 (defun org-export-odt-do-preprocess-latex-fragments ()
2594 "Convert LaTeX fragments to images."
2595 (let* ((latex-frag-opt (plist-get org-lparse-opt-plist
:LaTeX-fragments
))
2596 (latex-frag-opt ; massage the options
2597 (or (and (member latex-frag-opt
'(mathjax t
))
2598 (not (and (fboundp 'org-format-latex-mathml-available-p
)
2599 (org-format-latex-mathml-available-p)))
2600 (prog1 org-lparse-latex-fragment-fallback
2603 "LaTeX to MathML converter not available. "
2604 (format "Using %S instead."
2605 org-lparse-latex-fragment-fallback
)))))
2607 cache-dir display-msg
)
2609 ((eq latex-frag-opt
'dvipng
)
2610 (setq cache-dir org-latex-preview-ltxpng-directory
)
2611 (setq display-msg
"Creating LaTeX image %s"))
2612 ((member latex-frag-opt
'(mathjax t
))
2613 (setq latex-frag-opt
'mathml
)
2614 (setq cache-dir
"ltxmathml/")
2615 (setq display-msg
"Creating MathML formula %s")))
2616 (when (and org-current-export-file
)
2618 (concat cache-dir
(file-name-sans-extension
2619 (file-name-nondirectory org-current-export-file
)))
2620 org-current-export-dir nil display-msg
2621 nil nil latex-frag-opt
))))
2623 (defadvice org-format-latex-as-mathml
2624 (after org-odt-protect-latex-fragment activate
)
2625 "Encode LaTeX fragment as XML.
2626 Do this when translation to MathML fails."
2627 (when (or (not (> (length ad-return-value
) 0))
2628 (get-text-property 0 'org-protected ad-return-value
))
2629 (setq ad-return-value
2630 (org-propertize (org-odt-encode-plain-text (ad-get-arg 0))
2631 'org-protected t
))))
2633 (defun org-export-odt-preprocess-latex-fragments ()
2634 (when (equal org-export-current-backend
'odt
)
2635 (org-export-odt-do-preprocess-latex-fragments)))
2637 (defun org-export-odt-preprocess-label-references ()
2638 (goto-char (point-min))
2639 (let (label label-components category value pretty-label
)
2640 (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t
)
2641 (org-if-unprotected-at (match-beginning 1)
2643 (let ((org-lparse-encode-pending t
)
2644 (label (match-string 1)))
2645 ;; markup generated below is mostly an eye-candy. At
2646 ;; pre-processing stage, there is no information on which
2647 ;; entity a label reference points to. The actual markup
2648 ;; is generated as part of `org-odt-fixup-label-references'
2649 ;; which gets called at the fag end of export. By this
2650 ;; time we would have seen and collected all the label
2651 ;; definitions in `org-odt-entity-labels-alist'.
2652 (org-odt-format-tags
2653 '("<text:sequence-ref text:ref-name=\"%s\">" .
2654 "</text:sequence-ref>")
2655 "" (org-add-props label
'(org-protected t
)))) t t
)))))
2657 ;; process latex fragments as part of
2658 ;; `org-export-preprocess-after-blockquote-hook'. Note that this hook
2659 ;; is the one that is closest and well before the call to
2660 ;; `org-export-attach-captions-and-attributes' in
2661 ;; `org-export-preprocess-string'. The above arrangement permits
2662 ;; captions, labels and attributes to be attached to png images
2663 ;; generated out of latex equations.
2664 (add-hook 'org-export-preprocess-after-blockquote-hook
2665 'org-export-odt-preprocess-latex-fragments
)
2667 (defun org-export-odt-preprocess (parameters)
2668 (org-export-odt-preprocess-label-references))
2670 (declare-function archive-zip-extract
"arc-mode.el" (archive name
))
2671 (defun org-odt-zip-extract-one (archive member
&optional target
)
2673 (let* ((target (or target default-directory
))
2674 (archive (expand-file-name archive
))
2675 (archive-zip-extract
2676 (list "unzip" "-qq" "-o" "-d" target
))
2677 exit-code command-output
)
2678 (setq command-output
2680 (setq exit-code
(archive-zip-extract archive member
))
2682 (unless (zerop exit-code
)
2683 (message command-output
)
2684 (error "Extraction failed"))))
2686 (defun org-odt-zip-extract (archive members
&optional target
)
2687 (when (atom members
) (setq members
(list members
)))
2688 (mapc (lambda (member)
2689 (org-odt-zip-extract-one archive member target
))
2692 (defun org-odt-copy-styles-file (&optional styles-file
)
2693 ;; Non-availability of styles.xml is not a critical error. For now
2694 ;; throw an error purely for aesthetic reasons.
2695 (setq styles-file
(or styles-file
2696 org-export-odt-styles-file
2697 (expand-file-name "OrgOdtStyles.xml"
2699 (error "org-odt: Missing styles file?")))
2701 ((listp styles-file
)
2702 (let ((archive (nth 0 styles-file
))
2703 (members (nth 1 styles-file
)))
2704 (org-odt-zip-extract archive members
)
2707 (when (org-file-image-p member
)
2708 (let* ((image-type (file-name-extension member
))
2709 (media-type (format "image/%s" image-type
)))
2710 (org-odt-create-manifest-file-entry media-type member
))))
2712 ((and (stringp styles-file
) (file-exists-p styles-file
))
2713 (let ((styles-file-type (file-name-extension styles-file
)))
2715 ((string= styles-file-type
"xml")
2716 (copy-file styles-file
"styles.xml" t
))
2717 ((member styles-file-type
'("odt" "ott"))
2718 (org-odt-zip-extract styles-file
"styles.xml")))))
2720 (error (format "Invalid specification of styles.xml file: %S"
2721 org-export-odt-styles-file
))))
2723 ;; create a manifest entry for styles.xml
2724 (org-odt-create-manifest-file-entry "text/xml" "styles.xml"))
2726 (defun org-odt-configure-outline-numbering (level)
2727 "Outline numbering is retained only upto LEVEL.
2728 To disable outline numbering pass a LEVEL of 0."
2729 (goto-char (point-min))
2731 "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
2733 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
2734 (while (re-search-forward regex nil t
)
2735 (when (> (string-to-number (match-string 2)) level
)
2736 (replace-match replacement t nil
))))
2740 (defun org-export-as-odf (latex-frag &optional odf-file
)
2741 "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
2742 Use `org-create-math-formula' to convert LATEX-FRAG first to
2743 MathML. When invoked as an interactive command, use
2744 `org-latex-regexps' to infer LATEX-FRAG from currently active
2745 region. If no LaTeX fragments are found, prompt for it. Push
2746 MathML source to kill ring, if `org-export-copy-to-kill-ring' is
2750 (setq frag
(and (setq frag
(and (org-region-active-p)
2751 (buffer-substring (region-beginning)
2753 (loop for e in org-latex-regexps
2754 thereis
(when (string-match (nth 1 e
) frag
)
2755 (match-string (nth 2 e
) frag
)))))
2756 (read-string "LaTeX Fragment: " frag nil frag
))
2757 ,(let ((odf-filename (expand-file-name
2759 (file-name-sans-extension
2760 (or (file-name-nondirectory buffer-file-name
)))
2762 (file-name-directory buffer-file-name
))))
2763 (read-file-name "ODF filename: " nil odf-filename nil
2764 (file-name-nondirectory odf-filename
)))))
2765 (org-odt-cleanup-xml-buffers
2766 (let* ((org-lparse-backend 'odf
)
2767 org-lparse-opt-plist
2768 (filename (or odf-file
2771 (file-name-sans-extension
2772 (or (file-name-nondirectory buffer-file-name
)))
2774 (file-name-directory buffer-file-name
))))
2775 (buffer (find-file-noselect (org-odt-init-outfile filename
)))
2776 (coding-system-for-write 'utf-8
)
2777 (save-buffer-coding-system 'utf-8
))
2779 (set-buffer-file-coding-system coding-system-for-write
)
2780 (let ((mathml (org-create-math-formula latex-frag
)))
2781 (unless mathml
(error "No Math formula created"))
2783 (or (org-export-push-to-kill-ring
2784 (upcase (symbol-name org-lparse-backend
)))
2785 (message "Exporting... done")))
2786 (org-odt-save-as-outfile filename nil
))))
2789 (defun org-export-as-odf-and-open ()
2790 "Export LaTeX fragment as OpenDocument formula and immediately open it.
2791 Use `org-export-as-odf' to read LaTeX fragment and OpenDocument
2794 (org-lparse-and-open
2795 nil nil nil
(call-interactively 'org-export-as-odf
)))
2800 ;; generated-autoload-file: "org-loaddefs.el"
2803 ;;; org-odt.el ends here