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 (defconst org-odt-lib-dir
(file-name-directory load-file-name
)
88 "Location of ODT exporter.
89 Use this to infer values of `org-odt-styles-dir' and
90 `org-export-odt-schema-dir'.")
92 (defvar org-odt-data-dir nil
93 "Data directory for ODT exporter.
94 Use this to infer values of `org-odt-styles-dir' and
95 `org-export-odt-schema-dir'.")
97 (defconst org-odt-schema-dir-list
100 (expand-file-name "./schema/" org-odt-data-dir
)) ; bail out
102 (and (boundp 'org-odt-data-dir
) org-odt-data-dir
; see make install
103 (expand-file-name "./schema/" org-odt-data-dir
))))
104 "List of directories to search for OpenDocument schema files.
105 Use this list to set the default value of
106 `org-export-odt-schema-dir'. The entries in this list are
107 populated heuristically based on the values of `org-odt-lib-dir'
108 and `org-odt-data-dir'.")
110 (defcustom org-export-odt-schema-dir
113 (message "Debug (org-odt): Searching for OpenDocument schema files...")
117 (message "Debug (org-odt): Trying %s..." schema-dir
)
118 (when (and (file-readable-p
119 (expand-file-name "od-manifest-schema-v1.2-cs01.rnc"
122 (expand-file-name "od-schema-v1.2-cs01.rnc"
125 (expand-file-name "schemas.xml" schema-dir
)))
126 (message "Debug (org-odt): Using schema files under %s"
128 (throw 'schema-dir schema-dir
))))
129 org-odt-schema-dir-list
)
130 (message "Debug (org-odt): No OpenDocument schema files installed")
133 "Directory that contains OpenDocument schema files.
135 This directory contains:
136 1. rnc files for OpenDocument schema
137 2. a \"schemas.xml\" file that specifies locating rules needed
138 for auto validation of OpenDocument XML files.
140 Use the customize interface to set this variable. This ensures
141 that `rng-schema-locating-files' is updated and auto-validation
142 of OpenDocument XML takes place based on the value
143 `rng-nxml-auto-validate-flag'.
145 The default value of this variable varies depending on the
146 version of org in use and is initialized from
147 `org-odt-schema-dir-list'. The OASIS schema files are available
148 only in the org's private git repository. It is *not* bundled
149 with GNU ELPA tar or standard Emacs distribution."
151 (const :tag
"Not set" nil
)
152 (directory :tag
"Schema directory"))
153 :group
'org-export-odt
157 "Set `org-export-odt-schema-dir'.
158 Also add it to `rng-schema-locating-files'."
159 (let ((schema-dir value
))
163 (expand-file-name "od-manifest-schema-v1.2-cs01.rnc" schema-dir
))
165 (expand-file-name "od-schema-v1.2-cs01.rnc" schema-dir
))
167 (expand-file-name "schemas.xml" schema-dir
)))
170 (message "Error (org-odt): %s has no OpenDocument schema files"
173 (when org-export-odt-schema-dir
174 (eval-after-load 'rng-loc
175 '(add-to-list 'rng-schema-locating-files
176 (expand-file-name "schemas.xml"
177 org-export-odt-schema-dir
))))))
179 (defconst org-odt-styles-dir-list
181 (and org-odt-data-dir
182 (expand-file-name "./styles/" org-odt-data-dir
)) ; bail out
184 (and (boundp 'org-odt-data-dir
) org-odt-data-dir
; see make install
185 (expand-file-name "./styles/" org-odt-data-dir
)))
186 (expand-file-name "../etc/styles/" org-odt-lib-dir
) ; git
187 (expand-file-name "./etc/styles/" org-odt-lib-dir
) ; elpa
188 (expand-file-name "./org/" data-directory
) ; system
190 "List of directories to search for OpenDocument styles files.
191 See `org-odt-styles-dir'. The entries in this list are populated
192 heuristically based on the values of `org-odt-lib-dir' and
193 `org-odt-data-dir'.")
195 (defconst org-odt-styles-dir
198 (message "Debug (org-odt): Searching for OpenDocument styles files...")
199 (mapc (lambda (styles-dir)
201 (message "Debug (org-odt): Trying %s..." styles-dir
)
202 (when (and (file-readable-p
204 "OrgOdtContentTemplate.xml" styles-dir
))
207 "OrgOdtStyles.xml" styles-dir
)))
208 (message "Debug (org-odt): Using styles under %s"
210 (throw 'styles-dir styles-dir
))))
211 org-odt-styles-dir-list
)
214 (error "Error (org-odt): Cannot find factory styles files. Aborting."))
216 "Directory that holds auxiliary XML files used by the ODT exporter.
218 This directory contains the following XML files -
219 \"OrgOdtStyles.xml\" and \"OrgOdtContentTemplate.xml\". These
220 XML files are used as the default values of
221 `org-export-odt-styles-file' and
222 `org-export-odt-content-template-file'.
224 The default value of this variable varies depending on the
225 version of org in use and is initialized from
226 `org-odt-styles-dir-list'. Note that the user could be using org
227 from one of: org's own private git repository, GNU ELPA tar or
230 (defvar org-odt-file-extensions
231 '(("odt" .
"OpenDocument Text")
232 ("ott" .
"OpenDocument Text Template")
233 ("odm" .
"OpenDocument Master Document")
234 ("ods" .
"OpenDocument Spreadsheet")
235 ("ots" .
"OpenDocument Spreadsheet Template")
236 ("odg" .
"OpenDocument Drawing (Graphics)")
237 ("otg" .
"OpenDocument Drawing Template")
238 ("odp" .
"OpenDocument Presentation")
239 ("otp" .
"OpenDocument Presentation Template")
240 ("odi" .
"OpenDocument Image")
241 ("odf" .
"OpenDocument Formula")
242 ("odc" .
"OpenDocument Chart")))
246 ;; Let Emacs open all OpenDocument files in archive mode
247 (add-to-list 'auto-mode-alist
248 (cons (concat "\\." (car desc
) "\\'") 'archive-mode
)))
249 org-odt-file-extensions
)
251 ;; register the odt exporter with the pre-processor
252 (add-to-list 'org-export-backends
'odt
)
254 ;; register the odt exporter with org-lparse library
255 (org-lparse-register-backend 'odt
)
257 (defun org-odt-unload-function ()
258 (org-lparse-unregister-backend 'odt
)
259 (remove-hook 'org-export-preprocess-after-blockquote-hook
260 'org-export-odt-preprocess-latex-fragments
)
263 (defcustom org-export-odt-content-template-file nil
264 "Template file for \"content.xml\".
265 The exporter embeds the exported content just before
266 \"</office:text>\" element.
268 If unspecified, the file named \"OrgOdtContentTemplate.xml\"
269 under `org-odt-styles-dir' is used."
271 :group
'org-export-odt
274 (defcustom org-export-odt-styles-file nil
275 "Default styles file for use with ODT export.
276 Valid values are one of:
278 2. path to a styles.xml file
279 3. path to a *.odt or a *.ott file
280 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
283 In case of option 1, an in-built styles.xml is used. See
284 `org-odt-styles-dir' for more information.
286 In case of option 3, the specified file is unzipped and the
287 styles.xml embedded therein is used.
289 In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
290 and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
291 generated odt file. Use relative path for specifying the
292 FILE-MEMBERS. styles.xml must be specified as one of the
295 Use options 1, 2 or 3 only if styles.xml alone suffices for
296 achieving the desired formatting. Use option 4, if the styles.xml
297 references additional files like header and footer images for
298 achieving the desired formatting.
300 Use \"#+ODT_STYLES_FILE: ...\" directive to set this variable on
301 a per-file basis. For example,
303 #+ODT_STYLES_FILE: \"/path/to/styles.xml\" or
304 #+ODT_STYLES_FILE: (\"/path/to/file.ott\" (\"styles.xml\" \"image/hdr.png\"))."
305 :group
'org-export-odt
309 (const :tag
"Factory settings" nil
)
310 (file :must-match t
:tag
"styles.xml")
311 (file :must-match t
:tag
"ODT or OTT file")
312 (list :tag
"ODT or OTT file + Members"
313 (file :must-match t
:tag
"ODF Text or Text Template file")
315 (file :tag
" Member" "styles.xml")
316 (repeat (file :tag
"Member"))))))
318 (eval-after-load 'org-exp
319 '(add-to-list 'org-export-inbuffer-options-extra
320 '("ODT_STYLES_FILE" :odt-styles-file
)))
322 (defconst org-export-odt-tmpdir-prefix
"%s-")
323 (defconst org-export-odt-bookmark-prefix
"OrgXref.")
324 (defvar org-odt-zip-dir nil
325 "Temporary directory that holds XML files during export.")
327 (defvar org-export-odt-embed-images t
328 "Should the images be copied in to the odt file or just linked?")
330 (defvar org-export-odt-inline-images
'maybe
)
331 (defcustom org-export-odt-inline-image-extensions
332 '("png" "jpeg" "jpg" "gif")
333 "Extensions of image files that can be inlined into HTML."
334 :type
'(repeat (string :tag
"Extension"))
335 :group
'org-export-odt
338 (defcustom org-export-odt-pixels-per-inch display-pixels-per-inch
339 "Scaling factor for converting images pixels to inches.
340 Use this for sizing of embedded images. See Info node `(org)
341 Images in ODT export' for more information."
343 :group
'org-export-odt
346 (defcustom org-export-odt-create-custom-styles-for-srcblocks t
347 "Whether custom styles for colorized source blocks be automatically created.
348 When this option is turned on, the exporter creates custom styles
349 for source blocks based on the advice of `htmlfontify'. Creation
350 of custom styles happen as part of `org-odt-hfy-face-to-css'.
352 When this option is turned off exporter does not create such
355 Use the latter option if you do not want the custom styles to be
356 based on your current display settings. It is necessary that the
357 styles.xml already contains needed styles for colorizing to work.
359 This variable is effective only if
360 `org-export-odt-fontify-srcblocks' is turned on."
361 :group
'org-export-odt
365 (defvar org-export-odt-default-org-styles-alist
366 '((paragraph .
((default .
"Text_20_body")
367 (fixedwidth .
"OrgFixedWidthBlock")
369 (quote .
"Quotations")
370 (blockquote .
"Quotations")
371 (center .
"OrgCenter")
375 (subtitle .
"OrgSubtitle")
376 (footnote .
"Footnote")
377 (src .
"OrgSrcBlock")
378 (illustration .
"Illustration")
380 (definition-term .
"Text_20_body_20_bold")
381 (horizontal-line .
"Horizontal_20_Line")))
382 (character .
((default .
"Default")
384 (emphasis .
"Emphasis")
386 (verbatim .
"OrgCode")
387 (strike .
"Strikethrough")
388 (underline .
"Underline")
389 (subscript .
"OrgSubscript")
390 (superscript .
"OrgSuperscript")))
391 (list .
((ordered .
"OrgNumberedList")
392 (unordered .
"OrgBulletedList")
393 (description .
"OrgDescriptionList"))))
394 "Default styles for various entities.")
396 (defvar org-export-odt-org-styles-alist org-export-odt-default-org-styles-alist
)
397 (defun org-odt-get-style-name-for-entity (category &optional entity
)
398 (let ((entity (or entity
'default
)))
400 (cdr (assoc entity
(cdr (assoc category
401 org-export-odt-org-styles-alist
))))
402 (cdr (assoc entity
(cdr (assoc category
403 org-export-odt-default-org-styles-alist
))))
404 (error "Cannot determine style name for entity %s of type %s"
407 (defcustom org-export-odt-preferred-output-format nil
408 "Automatically post-process to this format after exporting to \"odt\".
409 Interactive commands `org-export-as-odt' and
410 `org-export-as-odt-and-open' export first to \"odt\" format and
411 then use `org-export-odt-convert-process' to convert the
412 resulting document to this format. During customization of this
413 variable, the list of valid values are populated based on
414 `org-export-odt-convert-capabilities'.
416 You can set this option on per-file basis using file local
417 values. See Info node `(emacs) File Variables'."
418 :group
'org-export-odt
420 :type
'(choice :convert-widget
422 (apply 'widget-convert
(widget-type w
)
423 (eval (car (widget-get w
:args
)))))
424 `((const :tag
"None" nil
)
425 ,@(mapcar (lambda (c)
427 (org-lparse-reachable-formats "odt")))))
429 (put 'org-export-odt-preferred-output-format
'safe-local-variable
'stringp
)
431 (defmacro org-odt-cleanup-xml-buffers
(&rest body
)
432 `(let ((org-odt-zip-dir
434 (format org-export-odt-tmpdir-prefix
"odf") t
))
435 (--cleanup-xml-buffers
438 (let ((xml-files '("mimetype" "META-INF/manifest.xml" "content.xml"
439 "meta.xml" "styles.xml")))
440 ;; kill all xml buffers
442 (let ((buf (find-file-noselect
443 (expand-file-name file org-odt-zip-dir
) t
)))
444 (when (buffer-name buf
)
445 (set-buffer-modified-p nil
)
448 ;; delete temporary directory.
449 (delete-directory org-odt-zip-dir t
)))))
450 (org-condition-case-unless-debug err
451 (prog1 (progn ,@body
)
452 (funcall --cleanup-xml-buffers
))
454 (funcall --cleanup-xml-buffers
)
455 (message "OpenDocument export failed: %s"
456 (error-message-string err
))))))
459 (defun org-export-as-odt-and-open (arg)
460 "Export the outline as ODT and immediately open it with a browser.
461 If there is an active region, export only the region.
462 The prefix ARG specifies how many levels of the outline should become
463 headlines. The default is 3. Lower levels will become bulleted lists."
465 (org-odt-cleanup-xml-buffers
467 (or org-export-odt-preferred-output-format
"odt") "odt" arg
)))
470 (defun org-export-as-odt-batch ()
471 "Call the function `org-lparse-batch'.
472 This function can be used in batch processing as:
474 --load=$HOME/lib/emacs/org.el
475 --eval \"(setq org-export-headline-levels 2)\"
476 --visit=MyFile --funcall org-export-as-odt-batch"
477 (org-lparse-batch "odt"))
479 ;;; org-export-as-odt
481 (defun org-export-as-odt (arg &optional hidden ext-plist
482 to-buffer body-only pub-dir
)
483 "Export the outline as a OpenDocumentText file.
484 If there is an active region, export only the region. The prefix
485 ARG specifies how many levels of the outline should become
486 headlines. The default is 3. Lower levels will become bulleted
487 lists. HIDDEN is obsolete and does nothing.
488 EXT-PLIST is a property list with external parameters overriding
489 org-mode's default settings, but still inferior to file-local
490 settings. When TO-BUFFER is non-nil, create a buffer with that
491 name and export to that buffer. If TO-BUFFER is the symbol
492 `string', don't leave any buffer behind but just return the
493 resulting XML as a string. When BODY-ONLY is set, don't produce
494 the file header and footer, simply return the content of
495 <body>...</body>, without even the body tags themselves. When
496 PUB-DIR is set, use this as the publishing directory."
498 (org-odt-cleanup-xml-buffers
499 (org-lparse (or org-export-odt-preferred-output-format
"odt")
500 "odt" arg hidden ext-plist to-buffer body-only pub-dir
)))
502 (defvar org-odt-entity-control-callbacks-alist
504 .
(org-odt-begin-export org-odt-end-export
))
506 .
(org-odt-begin-document-content org-odt-end-document-content
))
508 .
(org-odt-begin-document-body org-odt-end-document-body
))
510 .
(org-odt-begin-toc org-odt-end-toc
))
512 .
(org-odt-begin-environment org-odt-end-environment
))
514 .
(org-odt-begin-footnote-definition org-odt-end-footnote-definition
))
516 .
(org-odt-begin-table org-odt-end-table
))
518 .
(org-odt-begin-table-rowgroup org-odt-end-table-rowgroup
))
520 .
(org-odt-begin-list org-odt-end-list
))
522 .
(org-odt-begin-list-item org-odt-end-list-item
))
524 .
(org-odt-begin-outline org-odt-end-outline
))
526 .
(org-odt-begin-outline-text org-odt-end-outline-text
))
528 .
(org-odt-begin-paragraph org-odt-end-paragraph
)))
531 (defvar org-odt-entity-format-callbacks-alist
532 `((EXTRA-TARGETS . org-lparse-format-extra-targets
)
533 (ORG-TAGS . org-lparse-format-org-tags
)
534 (SECTION-NUMBER . org-lparse-format-section-number
)
535 (HEADLINE . org-odt-format-headline
)
536 (TOC-ENTRY . org-odt-format-toc-entry
)
537 (TOC-ITEM . org-odt-format-toc-item
)
538 (TAGS . org-odt-format-tags
)
539 (SPACES . org-odt-format-spaces
)
540 (TABS . org-odt-format-tabs
)
541 (LINE-BREAK . org-odt-format-line-break
)
542 (FONTIFY . org-odt-format-fontify
)
543 (TODO . org-lparse-format-todo
)
544 (LINK . org-odt-format-link
)
545 (INLINE-IMAGE . org-odt-format-inline-image
)
546 (ORG-LINK . org-odt-format-org-link
)
547 (HEADING . org-odt-format-heading
)
548 (ANCHOR . org-odt-format-anchor
)
549 (TABLE . org-lparse-format-table
)
550 (TABLE-ROW . org-odt-format-table-row
)
551 (TABLE-CELL . org-odt-format-table-cell
)
552 (FOOTNOTES-SECTION . ignore
)
553 (FOOTNOTE-REFERENCE . org-odt-format-footnote-reference
)
554 (HORIZONTAL-LINE . org-odt-format-horizontal-line
)
555 (COMMENT . org-odt-format-comment
)
556 (LINE . org-odt-format-line
)
557 (ORG-ENTITY . org-odt-format-org-entity
))
561 ;;;_. control callbacks
563 (defun org-odt-begin-office-body ()
565 (insert-file-contents
566 (or org-export-odt-content-template-file
567 (expand-file-name "OrgOdtContentTemplate.xml"
568 org-odt-styles-dir
)))
569 (goto-char (point-min))
570 (re-search-forward "</office:text>" nil nil
)
571 (delete-region (match-beginning 0) (point-max)))
573 ;; Following variable is let bound when `org-do-lparse' is in
574 ;; progress. See org-html.el.
575 (defvar org-lparse-toc
)
576 (defun org-odt-format-toc ()
577 (if (not org-lparse-toc
) "" (concat "\n" org-lparse-toc
"\n")))
579 (defun org-odt-format-preamble (opt-plist)
580 (let* ((title (plist-get opt-plist
:title
))
581 (author (plist-get opt-plist
:author
))
582 (date (plist-get opt-plist
:date
))
583 (iso-date (org-odt-format-date date
))
584 (date (org-odt-format-date date
"%d %b %Y"))
585 (email (plist-get opt-plist
:email
))
586 ;; switch on or off above vars based on user settings
587 (author (and (plist-get opt-plist
:author-info
) (or author email
)))
588 (email (and (plist-get opt-plist
:email-info
) email
))
589 (date (and (plist-get opt-plist
:time-stamp-file
) date
)))
594 (org-odt-format-stylized-paragraph
595 'title
(org-odt-format-tags
596 '("<text:title>" .
"</text:title>") title
))
598 "<text:p text:style-name=\"OrgTitle\"/>"))
600 ((and author
(not email
))
603 (org-odt-format-stylized-paragraph
606 '("<text:initial-creator>" .
"</text:initial-creator>")
609 "<text:p text:style-name=\"OrgSubtitle\"/>"))
613 (org-odt-format-stylized-paragraph
617 '("<text:initial-creator>" .
"</text:initial-creator>")
618 author
) (concat "mailto:" email
)))
620 "<text:p text:style-name=\"OrgSubtitle\"/>")))
624 (org-odt-format-stylized-paragraph
627 '("<text:date style:data-style-name=\"%s\" text:date-value=\"%s\">"
628 .
"</text:date>") date
"N75" iso-date
))
630 "<text:p text:style-name=\"OrgSubtitle\"/>")))))
632 (defun org-odt-begin-document-body (opt-plist)
633 (org-odt-begin-office-body)
634 (insert (org-odt-format-preamble opt-plist
))
635 (setq org-lparse-dyn-first-heading-pos
(point)))
637 (defvar org-lparse-body-only
) ; let bound during org-do-lparse
638 (defvar org-lparse-to-buffer
) ; let bound during org-do-lparse
639 (defun org-odt-end-document-body (opt-plist)
640 (unless org-lparse-body-only
641 (org-lparse-insert-tag "</office:text>")
642 (org-lparse-insert-tag "</office:body>")))
644 (defun org-odt-begin-document-content (opt-plist)
647 (defun org-odt-end-document-content ()
648 (org-lparse-insert-tag "</office:document-content>"))
650 (defun org-odt-begin-outline (level1 snumber title tags
651 target extra-targets class
)
653 'HEADING
(org-lparse-format
654 'HEADLINE title extra-targets tags snumber level1
)
657 (defun org-odt-end-outline ()
660 (defun org-odt-begin-outline-text (level1 snumber class
)
663 (defun org-odt-end-outline-text ()
666 (defun org-odt-begin-section (style &optional name
)
667 (let ((default-name (car (org-odt-add-automatic-style "Section"))))
668 (org-lparse-insert-tag
669 "<text:section text:style-name=\"%s\" text:name=\"%s\">"
670 style
(or name default-name
))))
672 (defun org-odt-end-section ()
673 (org-lparse-insert-tag "</text:section>"))
675 (defun org-odt-begin-paragraph (&optional style
)
676 (org-lparse-insert-tag
677 "<text:p%s>" (org-odt-get-extra-attrs-for-paragraph-style style
)))
679 (defun org-odt-end-paragraph ()
680 (org-lparse-insert-tag "</text:p>"))
682 (defun org-odt-get-extra-attrs-for-paragraph-style (style)
686 ((stringp style
) style
)
687 ((symbolp style
) (org-odt-get-style-name-for-entity
690 (error "Don't know how to handle paragraph style %s" style
))
691 (format " text:style-name=\"%s\"" style-name
)))
693 (defun org-odt-format-stylized-paragraph (style text
)
695 '("<text:p%s>" .
"</text:p>") text
696 (org-odt-get-extra-attrs-for-paragraph-style style
)))
698 (defvar org-lparse-opt-plist
) ; bound during org-do-lparse
699 (defun org-odt-format-author (&optional author
)
700 (when (setq author
(or author
(plist-get org-lparse-opt-plist
:author
)))
701 (org-odt-format-tags '("<dc:creator>" .
"</dc:creator>") author
)))
703 (defun org-odt-format-date (&optional org-ts fmt
)
706 (and (stringp org-ts
)
707 (string-match org-ts-regexp0 org-ts
)
709 (org-fix-decoded-time
710 (org-parse-time-string (match-string 0 org-ts
) t
)))))
713 (fmt (format-time-string fmt time
))
714 (t (setq date
(format-time-string "%Y-%m-%dT%H:%M:%S%z" time
))
715 (format "%s:%s" (substring date
0 -
2) (substring date -
2)))))))
717 (defun org-odt-begin-annotation (&optional author date
)
718 (org-lparse-insert-tag "<office:annotation>")
719 (when (setq author
(org-odt-format-author author
))
721 (insert (org-odt-format-tags
722 '("<dc:date>" .
"</dc:date>")
724 (or date
(plist-get org-lparse-opt-plist
:date
)))))
725 (org-lparse-begin-paragraph))
727 (defun org-odt-end-annotation ()
728 (org-lparse-insert-tag "</office:annotation>"))
730 (defun org-odt-begin-environment (style env-options-plist
)
733 (org-lparse-stash-save-paragraph-state)
734 (org-odt-begin-annotation (plist-get env-options-plist
'author
)
735 (plist-get env-options-plist
'date
)))
736 ((blockquote verse center quote
)
737 (org-lparse-begin-paragraph style
)
740 (org-lparse-end-paragraph)
742 (t (error "Unknown environment %s" style
))))
744 (defun org-odt-end-environment (style env-options-plist
)
747 (org-lparse-end-paragraph)
748 (org-odt-end-annotation)
749 (org-lparse-stash-pop-paragraph-state))
750 ((blockquote verse center quote
)
751 (org-lparse-end-paragraph)
754 (org-lparse-begin-paragraph)
756 (t (error "Unknown environment %s" style
))))
758 (defvar org-lparse-list-stack
) ; dynamically bound in org-do-lparse
759 (defvar org-odt-list-stack-stashed
)
760 (defun org-odt-begin-list (ltype)
761 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
763 (let* ((style-name (org-odt-get-style-name-for-entity 'list ltype
))
764 (extra (concat (if (or org-lparse-list-table-p
765 (and (= 1 (length org-lparse-list-stack
))
766 (null org-odt-list-stack-stashed
)))
767 " text:continue-numbering=\"false\""
768 " text:continue-numbering=\"true\"")
770 (format " text:style-name=\"%s\"" style-name
)))))
772 ((ordered unordered description
)
773 (org-lparse-end-paragraph)
774 (org-lparse-insert-tag "<text:list%s>" extra
))
775 (t (error "Unknown list type: %s" ltype
)))))
777 (defun org-odt-end-list (ltype)
778 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
781 (org-lparse-insert-tag "</text:list>")
782 (error "Unknown list type: %s" ltype
)))
784 (defun org-odt-begin-list-item (ltype &optional arg headline
)
785 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
789 (assert (not headline
) t
)
790 (let* ((counter arg
) (extra ""))
791 (org-lparse-insert-tag (if (= (length org-lparse-list-stack
)
792 (length org-odt-list-stack-stashed
))
793 "<text:list-header>" "<text:list-item>"))
794 (org-lparse-begin-paragraph)))
796 (let* ((id arg
) (extra ""))
797 (org-lparse-insert-tag (if (= (length org-lparse-list-stack
)
798 (length org-odt-list-stack-stashed
))
799 "<text:list-header>" "<text:list-item>"))
800 (org-lparse-begin-paragraph)
801 (insert (if headline
(org-odt-format-target headline id
)
802 (org-odt-format-bookmark "" id
)))))
804 (assert (not headline
) t
)
805 (let ((term (or arg
"(no term)")))
808 '("<text:list-item>" .
"</text:list-item>")
809 (org-odt-format-stylized-paragraph 'definition-term term
)))
810 (org-lparse-begin-list-item 'unordered
)
811 (org-lparse-begin-list 'description
)
812 (org-lparse-begin-list-item 'unordered
)))
813 (t (error "Unknown list type"))))
815 (defun org-odt-end-list-item (ltype)
816 (setq ltype
(or (org-lparse-html-list-type-to-canonical-list-type ltype
)
820 (org-lparse-insert-tag (if (= (length org-lparse-list-stack
)
821 (length org-odt-list-stack-stashed
))
822 (prog1 "</text:list-header>"
823 (setq org-odt-list-stack-stashed nil
))
824 "</text:list-item>")))
826 (org-lparse-end-list-item-1)
827 (org-lparse-end-list 'description
)
828 (org-lparse-end-list-item-1))
829 (t (error "Unknown list type"))))
831 (defun org-odt-discontinue-list ()
832 (let ((stashed-stack org-lparse-list-stack
))
833 (loop for list-type in stashed-stack
834 do
(org-lparse-end-list-item-1 list-type
)
835 (org-lparse-end-list list-type
))
836 (setq org-odt-list-stack-stashed stashed-stack
)))
838 (defun org-odt-continue-list ()
839 (setq org-odt-list-stack-stashed
(nreverse org-odt-list-stack-stashed
))
840 (loop for list-type in org-odt-list-stack-stashed
841 do
(org-lparse-begin-list list-type
)
842 (org-lparse-begin-list-item list-type
)))
844 ;; Following variables are let bound when table emission is in
845 ;; progress. See org-lparse.el.
846 (defvar org-lparse-table-begin-marker
)
847 (defvar org-lparse-table-ncols
)
848 (defvar org-lparse-table-rowgrp-open
)
849 (defvar org-lparse-table-rownum
)
850 (defvar org-lparse-table-cur-rowgrp-is-hdr
)
851 (defvar org-lparse-table-is-styled
)
852 (defvar org-lparse-table-rowgrp-info
)
853 (defvar org-lparse-table-colalign-vector
)
855 (defvar org-odt-table-style nil
856 "Table style specified by \"#+ATTR_ODT: <style-name>\" line.
857 This is set during `org-odt-begin-table'.")
859 (defvar org-odt-table-style-spec nil
860 "Entry for `org-odt-table-style' in `org-export-odt-table-styles'.")
862 (defcustom org-export-odt-table-styles
863 '(("OrgEquation" "OrgEquation"
864 ((use-first-column-styles . t
)
865 (use-last-column-styles . t
))))
866 "Specify how Table Styles should be derived from a Table Template.
867 This is a list where each element is of the
868 form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
870 TABLE-STYLE-NAME is the style associated with the table through
871 `org-odt-table-style'.
873 TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
874 TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
875 below) that is included in
876 `org-export-odt-content-template-file'.
878 TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
880 PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
882 TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
883 \"FirstRow\" | \"LastRow\" |
884 \"EvenRow\" | \"OddRow\" |
885 \"EvenColumn\" | \"OddColumn\" | \"\"
886 where \"+\" above denotes string concatenation.
888 TABLE-CELL-OPTIONS is an alist where each element is of the
889 form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
890 TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
891 `use-last-row-styles' |
892 `use-first-column-styles' |
893 `use-last-column-styles' |
894 `use-banding-rows-styles' |
895 `use-banding-columns-styles' |
896 `use-first-row-styles'
897 ON-OR-OFF := `t' | `nil'
899 For example, with the following configuration
901 \(setq org-export-odt-table-styles
902 '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
903 \(\(use-first-row-styles . t\)
904 \(use-first-column-styles . t\)\)\)
905 \(\"TableWithHeaderColumns\" \"Custom\"
906 \(\(use-first-column-styles . t\)\)\)\)\)
908 1. A table associated with \"TableWithHeaderRowsAndColumns\"
909 style will use the following table-cell styles -
910 \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
911 \"CustomTableCell\" and the following paragraph styles
912 \"CustomFirstRowTableParagraph\",
913 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
916 2. A table associated with \"TableWithHeaderColumns\" style will
917 use the following table-cell styles -
918 \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
919 following paragraph styles
920 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
923 Note that TABLE-TEMPLATE-NAME corresponds to the
924 \"<table:table-template>\" elements contained within
925 \"<office:styles>\". The entries (TABLE-STYLE-NAME
926 TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
927 \"table:template-name\" and \"table:use-first-row-styles\" etc
928 attributes of \"<table:table>\" element. Refer ODF-1.2
929 specification for more information. Also consult the
930 implementation filed under `org-odt-get-table-cell-styles'.
932 The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
933 formatting of numbered display equations. Do not delete this
934 style from the list."
935 :group
'org-export-odt
938 (const :tag
"None" nil
)
939 (repeat :tag
"Table Styles"
940 (list :tag
"Table Style Specification"
941 (string :tag
"Table Style Name")
942 (string :tag
"Table Template Name")
943 (alist :options
(use-first-row-styles
945 use-first-column-styles
946 use-last-column-styles
947 use-banding-rows-styles
948 use-banding-columns-styles
)
950 :value-type
(const :tag
"True" t
))))))
952 (defvar org-odt-table-style-format
954 <style:style style:name=\"%s\" style:family=\"table\">
955 <style:table-properties style:rel-width=\"%d%%\" fo:margin-top=\"0cm\" fo:margin-bottom=\"0.20cm\" table:align=\"center\"/>
958 "Template for auto-generated Table styles.")
960 (defvar org-odt-automatic-styles
'()
961 "Registry of automatic styles for various OBJECT-TYPEs.
962 The variable has the following form:
964 \(\(OBJECT-NAME-A.1 OBJECT-PROPS-A.1\)
965 \(OBJECT-NAME-A.2 OBJECT-PROPS-A.2\) ...\)\)
967 \(\(OBJECT-NAME-B.1 OBJECT-PROPS-B.1\)
968 \(OBJECT-NAME-B.2 OBJECT-PROPS-B.2\) ...\)\)
971 OBJECT-TYPEs could be \"Section\", \"Table\", \"Figure\" etc.
972 OBJECT-PROPS is (typically) a plist created by passing
973 \"#+ATTR_ODT: \" option to `org-lparse-get-block-params'.
975 Use `org-odt-add-automatic-style' to add update this variable.'")
977 (defvar org-odt-object-counters nil
978 "Running counters for various OBJECT-TYPEs.
979 Use this to generate automatic names and style-names. See
980 `org-odt-add-automatic-style'.")
982 (defun org-odt-write-automatic-styles ()
983 "Write automatic styles to \"content.xml\"."
985 (find-file-noselect (expand-file-name "content.xml") t
)
986 ;; position the cursor
987 (goto-char (point-min))
988 (re-search-forward " </office:automatic-styles>" nil t
)
989 (goto-char (match-beginning 0))
990 ;; write automatic table styles
991 (loop for
(style-name props
) in
992 (plist-get org-odt-automatic-styles
'Table
) do
993 (when (setq props
(or (plist-get props
:rel-width
) 96))
994 (insert (format org-odt-table-style-format style-name props
))))))
996 (defun org-odt-add-automatic-style (object-type &optional object-props
)
997 "Create an automatic style of type OBJECT-TYPE with param OBJECT-PROPS.
998 OBJECT-PROPS is (typically) a plist created by passing
999 \"#+ATTR_ODT: \" option of the object in question to
1000 `org-lparse-get-block-params'.
1002 Use `org-odt-object-counters' to generate an automatic
1003 OBJECT-NAME and STYLE-NAME. If OBJECT-PROPS is non-nil, add a
1004 new entry in `org-odt-automatic-styles'. Return (OBJECT-NAME
1006 (assert (stringp object-type
))
1007 (let* ((object (intern object-type
))
1009 (seqno (1+ (or (plist-get org-odt-object-counters seqvar
) 0)))
1010 (object-name (format "%s%d" object-type seqno
)) style-name
)
1011 (setq org-odt-object-counters
1012 (plist-put org-odt-object-counters seqvar seqno
))
1014 (setq style-name
(format "Org%s" object-name
))
1015 (setq org-odt-automatic-styles
1016 (plist-put org-odt-automatic-styles object
1017 (append (list (list style-name object-props
))
1018 (plist-get org-odt-automatic-styles object
)))))
1019 (cons object-name style-name
)))
1021 (defvar org-odt-table-indentedp nil
)
1022 (defun org-odt-begin-table (caption label attributes short-caption
)
1023 (setq org-odt-table-indentedp
(not (null org-lparse-list-stack
)))
1024 (when org-odt-table-indentedp
1025 ;; Within the Org file, the table is appearing within a list item.
1026 ;; OpenDocument doesn't allow table to appear within list items.
1027 ;; Temporarily terminate the list, emit the table and then
1028 ;; re-continue the list.
1029 (org-odt-discontinue-list)
1030 ;; Put the Table in an indented section.
1031 (let ((level (length org-odt-list-stack-stashed
)))
1032 (org-odt-begin-section (format "OrgIndentedSection-Level-%d" level
))))
1033 (setq attributes
(org-lparse-get-block-params attributes
))
1034 (setq org-odt-table-style
(plist-get attributes
:style
))
1035 (setq org-odt-table-style-spec
1036 (assoc org-odt-table-style org-export-odt-table-styles
))
1037 (when (or label caption
)
1039 (org-odt-format-stylized-paragraph
1040 'table
(org-odt-format-entity-caption label caption
"__Table__"))))
1041 (let ((automatic-name (org-odt-add-automatic-style "Table" attributes
)))
1042 (org-lparse-insert-tag
1043 "<table:table table:name=\"%s\" table:style-name=\"%s\">"
1044 (or short-caption
(car automatic-name
))
1045 (or (nth 1 org-odt-table-style-spec
)
1046 (cdr automatic-name
) "OrgTable")))
1047 (setq org-lparse-table-begin-marker
(point)))
1049 (defvar org-lparse-table-colalign-info
)
1050 (defun org-odt-end-table ()
1051 (goto-char org-lparse-table-begin-marker
)
1052 (loop for level from
0 below org-lparse-table-ncols
1053 do
(let* ((col-cookie (and org-lparse-table-is-styled
1054 (cdr (assoc (1+ level
)
1055 org-lparse-table-colalign-info
))))
1056 (extra-columns (or (nth 1 col-cookie
) 0)))
1057 (dotimes (i (1+ extra-columns
))
1059 (org-odt-format-tags
1060 "<table:table-column table:style-name=\"%sColumn\"/>"
1061 "" (or (nth 1 org-odt-table-style-spec
) "OrgTable"))))
1063 ;; fill style attributes for table cells
1064 (when org-lparse-table-is-styled
1065 (while (re-search-forward "@@\\(table-cell:p\\|table-cell:style-name\\)@@\\([0-9]+\\)@@\\([0-9]+\\)@@" nil t
)
1066 (let* ((spec (match-string 1))
1067 (r (string-to-number (match-string 2)))
1068 (c (string-to-number (match-string 3)))
1069 (cell-styles (org-odt-get-table-cell-styles
1070 r c org-odt-table-style-spec
))
1071 (table-cell-style (car cell-styles
))
1072 (table-cell-paragraph-style (cdr cell-styles
)))
1074 ((equal spec
"table-cell:p")
1075 (replace-match table-cell-paragraph-style t t
))
1076 ((equal spec
"table-cell:style-name")
1077 (replace-match table-cell-style t t
))))))
1078 (goto-char (point-max))
1079 (org-lparse-insert-tag "</table:table>")
1080 (when org-odt-table-indentedp
1081 (org-odt-end-section)
1082 (org-odt-continue-list)))
1084 (defun org-odt-begin-table-rowgroup (&optional is-header-row
)
1085 (when org-lparse-table-rowgrp-open
1086 (org-lparse-end 'TABLE-ROWGROUP
))
1087 (org-lparse-insert-tag (if is-header-row
1088 "<table:table-header-rows>"
1089 "<table:table-rows>"))
1090 (setq org-lparse-table-rowgrp-open t
)
1091 (setq org-lparse-table-cur-rowgrp-is-hdr is-header-row
))
1093 (defun org-odt-end-table-rowgroup ()
1094 (when org-lparse-table-rowgrp-open
1095 (setq org-lparse-table-rowgrp-open nil
)
1096 (org-lparse-insert-tag
1097 (if org-lparse-table-cur-rowgrp-is-hdr
1098 "</table:table-header-rows>" "</table:table-rows>"))))
1100 (defun org-odt-format-table-row (row)
1101 (org-odt-format-tags
1102 '("<table:table-row>" .
"</table:table-row>") row
))
1104 (defun org-odt-get-table-cell-styles (r c
&optional style-spec
)
1105 "Retrieve styles applicable to a table cell.
1106 R and C are (zero-based) row and column numbers of the table
1107 cell. STYLE-SPEC is an entry in `org-export-odt-table-styles'
1108 applicable to the current table. It is `nil' if the table is not
1109 associated with any style attributes.
1111 Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
1113 When STYLE-SPEC is nil, style the table cell the conventional way
1114 - choose cell borders based on row and column groupings and
1115 choose paragraph alignment based on `org-col-cookies' text
1117 `org-odt-get-paragraph-style-cookie-for-table-cell'.
1119 When STYLE-SPEC is non-nil, ignore the above cookie and return
1120 styles congruent with the ODF-1.2 specification."
1124 ;; LibreOffice - particularly the Writer - honors neither table
1125 ;; templates nor custom table-cell styles. Inorder to retain
1126 ;; inter-operability with LibreOffice, only automatic styles are
1127 ;; used for styling of table-cells. The current implementation is
1128 ;; congruent with ODF-1.2 specification and hence is
1129 ;; future-compatible.
1131 ;; Additional Note: LibreOffice's AutoFormat facility for tables -
1132 ;; which recognizes as many as 16 different cell types - is much
1133 ;; richer. Unfortunately it is NOT amenable to easy configuration
1136 (let* ((template-name (nth 1 style-spec
))
1137 (cell-style-selectors (nth 2 style-spec
))
1140 ((and (cdr (assoc 'use-first-column-styles cell-style-selectors
))
1141 (= c
0)) "FirstColumn")
1142 ((and (cdr (assoc 'use-last-column-styles cell-style-selectors
))
1143 (= c
(1- org-lparse-table-ncols
))) "LastColumn")
1144 ((and (cdr (assoc 'use-first-row-styles cell-style-selectors
))
1145 (= r
0)) "FirstRow")
1146 ((and (cdr (assoc 'use-last-row-styles cell-style-selectors
))
1147 (= r org-lparse-table-rownum
))
1149 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors
))
1150 (= (% r
2) 1)) "EvenRow")
1151 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors
))
1152 (= (% r
2) 0)) "OddRow")
1153 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors
))
1154 (= (% c
2) 1)) "EvenColumn")
1155 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors
))
1156 (= (% c
2) 0)) "OddColumn")
1159 (concat template-name cell-type
"TableCell")
1160 (concat template-name cell-type
"TableParagraph"))))
1167 ((eq (cdr (assoc r org-lparse-table-rowgrp-info
)) :start
) "T")
1169 (when (= r org-lparse-table-rownum
) "B")
1172 ((or (memq (nth c org-table-colgroup-info
) '(:start
:startend
))
1173 (memq (nth (1- c
) org-table-colgroup-info
) '(:end
:startend
))) "L")
1175 (capitalize (aref org-lparse-table-colalign-vector c
))))))
1177 (defun org-odt-get-paragraph-style-cookie-for-table-cell (r c
)
1179 (and (not org-odt-table-style-spec
)
1181 (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
1182 ((and (= c
0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS
))
1184 (t "OrgTableContents")))
1185 (and org-lparse-table-is-styled
1186 (format "@@table-cell:p@@%03d@@%03d@@" r c
))))
1188 (defun org-odt-get-style-name-cookie-for-table-cell (r c
)
1189 (when org-lparse-table-is-styled
1190 (format "@@table-cell:style-name@@%03d@@%03d@@" r c
)))
1192 (defun org-odt-format-table-cell (data r c horiz-span
)
1194 (let* ((paragraph-style-cookie
1195 (org-odt-get-paragraph-style-cookie-for-table-cell r c
))
1197 (org-odt-get-style-name-cookie-for-table-cell r c
))
1198 (extra (and style-name-cookie
1199 (format " table:style-name=\"%s\"" style-name-cookie
)))
1200 (extra (concat extra
1201 (and (> horiz-span
0)
1202 (format " table:number-columns-spanned=\"%d\""
1203 (1+ horiz-span
))))))
1204 (org-odt-format-tags
1205 '("<table:table-cell%s>" .
"</table:table-cell>")
1206 (if org-lparse-list-table-p data
1207 (org-odt-format-stylized-paragraph paragraph-style-cookie data
)) extra
))
1209 (dotimes (i horiz-span
)
1210 (setq s
(concat s
"\n<table:covered-table-cell/>"))) s
)
1213 (defun org-odt-begin-footnote-definition (n)
1214 (org-lparse-begin-paragraph 'footnote
))
1216 (defun org-odt-end-footnote-definition (n)
1217 (org-lparse-end-paragraph))
1219 (defun org-odt-begin-toc (lang-specific-heading max-level
)
1220 ;; Strings in `org-export-language-setup' can contain named html
1221 ;; entities. Replace those with utf-8 equivalents.
1222 (let ((i 0) entity rpl
)
1223 (while (string-match "&\\([^#].*?\\);" lang-specific-heading i
)
1224 (setq entity
(match-string 1 lang-specific-heading
))
1225 (if (not (setq rpl
(org-entity-get-representation entity
'utf8
)))
1226 (setq i
(match-end 0))
1227 (setq i
(+ (match-beginning 0) (length rpl
)))
1228 (setq lang-specific-heading
1229 (replace-match rpl t t lang-specific-heading
)))))
1232 <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
1233 <text:table-of-content-source text:outline-level=\"%d\">
1234 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
1235 " max-level lang-specific-heading
))
1236 (loop for level from
1 upto
10
1239 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
1240 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
1241 <text:index-entry-chapter/>
1242 <text:index-entry-text/>
1243 <text:index-entry-link-end/>
1244 </text:table-of-content-entry-template>
1249 </text:table-of-content-source>
1252 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
1253 <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
1255 " lang-specific-heading
)))
1257 (defun org-odt-end-toc ()
1260 </text:table-of-content>
1263 (defun org-odt-format-toc-entry (snumber todo headline tags href
)
1264 (setq headline
(concat
1265 (and org-export-with-section-numbers
1266 (concat snumber
". "))
1270 (org-lparse-format 'SPACES
3)
1271 (org-lparse-format 'FONTIFY tags
"tag")))))
1273 (setq headline
(org-lparse-format 'FONTIFY headline
"todo")))
1275 (let ((org-odt-suppress-xref t
))
1276 (org-odt-format-link headline
(concat "#" href
))))
1278 (defun org-odt-format-toc-item (toc-entry level org-last-level
)
1279 (let ((style (format "Contents_20_%d"
1280 (+ level
(or (org-lparse-get 'TOPLEVEL-HLEVEL
) 1) -
1))))
1281 (insert "\n" (org-odt-format-stylized-paragraph style toc-entry
) "\n")))
1283 ;; Following variable is let bound during 'ORG-LINK callback. See
1285 (defvar org-lparse-link-description-is-image nil
)
1286 (defun org-odt-format-link (desc href
&optional attr
)
1288 ((and (= (string-to-char href
) ?
#) (not org-odt-suppress-xref
))
1289 (setq href
(substring href
1))
1290 (let ((xref-format "text"))
1291 (when (numberp desc
)
1292 (setq desc
(format "%d" desc
) xref-format
"number"))
1294 (setq desc
(mapconcat 'identity desc
".") xref-format
"chapter"))
1295 (setq href
(concat org-export-odt-bookmark-prefix href
))
1296 (org-odt-format-tags
1297 '("<text:bookmark-ref text:reference-format=\"%s\" text:ref-name=\"%s\">" .
1298 "</text:bookmark-ref>")
1299 desc xref-format href
)))
1300 (org-lparse-link-description-is-image
1301 (org-odt-format-tags
1302 '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" .
"</draw:a>")
1303 desc href
(or attr
"")))
1305 (org-odt-format-tags
1306 '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" .
"</text:a>")
1307 desc href
(or attr
"")))))
1309 (defun org-odt-format-spaces (n)
1313 " " (org-odt-format-tags "<text:s text:c=\"%d\"/>" "" (1- n
))))
1316 (defun org-odt-format-tabs (&optional n
)
1317 (let ((tab "<text:tab/>")
1321 (defun org-odt-format-line-break ()
1322 (org-odt-format-tags "<text:line-break/>" ""))
1324 (defun org-odt-format-horizontal-line ()
1325 (org-odt-format-stylized-paragraph 'horizontal-line
""))
1327 (defun org-odt-encode-plain-text (line &optional no-whitespace-filling
)
1328 (setq line
(org-xml-encode-plain-text line
))
1329 (if no-whitespace-filling line
1330 (org-odt-fill-tabs-and-spaces line
)))
1332 (defun org-odt-format-line (line)
1333 (case org-lparse-dyn-current-environment
1335 (org-odt-format-stylized-paragraph
1336 'fixedwidth
(org-odt-encode-plain-text line
)) "\n"))
1337 (t (concat line
"\n"))))
1339 (defun org-odt-format-comment (fmt &rest args
)
1340 (let ((comment (apply 'format fmt args
)))
1341 (format "\n<!-- %s -->\n" comment
)))
1343 (defun org-odt-format-org-entity (wd)
1344 (org-entity-get-representation wd
'utf8
))
1346 (defun org-odt-fill-tabs-and-spaces (line)
1347 (replace-regexp-in-string
1348 "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
1350 ((string= s
"\t") (org-odt-format-tabs))
1351 (t (org-odt-format-spaces (length s
))))) line
))
1353 (defcustom org-export-odt-fontify-srcblocks t
1354 "Specify whether or not source blocks need to be fontified.
1355 Turn this option on if you want to colorize the source code
1356 blocks in the exported file. For colorization to work, you need
1357 to make available an enhanced version of `htmlfontify' library."
1359 :group
'org-export-odt
1362 (defun org-odt-format-source-line-with-line-number-and-label
1363 (line rpllbl num fontifier par-style
)
1365 (let ((keep-label (not (numberp rpllbl
)))
1366 (ref (org-find-text-property-in-string 'org-coderef line
)))
1367 (setq line
(concat line
(and keep-label ref
(format "(%s)" ref
))))
1368 (setq line
(funcall fontifier line
))
1370 (setq line
(org-odt-format-target line
(concat "coderef-" ref
))))
1371 (setq line
(org-odt-format-stylized-paragraph par-style line
))
1373 (org-odt-format-tags '("<text:list-item>" .
"</text:list-item>") line
))))
1375 (defun org-odt-format-source-code-or-example-plain
1376 (lines lang caption textareap cols rows num cont rpllbl fmt
)
1377 "Format source or example blocks much like fixedwidth blocks.
1378 Use this when `org-export-odt-fontify-srcblocks' option is turned
1380 (let* ((lines (org-split-string lines
"[\r\n]"))
1381 (line-count (length lines
))
1386 (org-odt-format-source-line-with-line-number-and-label
1387 line rpllbl num
'org-odt-encode-plain-text
1388 (if (= i line-count
) "OrgFixedWidthBlockLastLine"
1389 "OrgFixedWidthBlock")))
1392 (defvar org-src-block-paragraph-format
1393 "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
1394 <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
1395 <style:background-image/>
1396 </style:paragraph-properties>
1397 <style:text-properties fo:color=\"%s\"/>
1399 "Custom paragraph style for colorized source and example blocks.
1400 This style is much the same as that of \"OrgFixedWidthBlock\"
1401 except that the foreground and background colors are set
1402 according to the default face identified by the `htmlfontify'.")
1404 (defvar hfy-optimisations
)
1405 (declare-function hfy-face-to-style
"htmlfontify" (fn))
1406 (declare-function hfy-face-or-def-to-name
"htmlfontify" (fn))
1408 (defun org-odt-hfy-face-to-css (fn)
1409 "Create custom style for face FN.
1410 When FN is the default face, use it's foreground and background
1411 properties to create \"OrgSrcBlock\" paragraph style. Otherwise
1412 use it's color attribute to create a character style whose name
1413 is obtained from FN. Currently all attributes of FN other than
1416 The style name for a face FN is derived using the following
1417 operations on the face name in that order - de-dash, CamelCase
1418 and prefix with \"OrgSrc\". For example,
1419 `font-lock-function-name-face' is associated with
1420 \"OrgSrcFontLockFunctionNameFace\"."
1421 (let* ((css-list (hfy-face-to-style fn
))
1422 (style-name ((lambda (fn)
1425 'capitalize
(split-string
1426 (hfy-face-or-def-to-name fn
) "-")
1428 (color-val (cdr (assoc "color" css-list
)))
1429 (background-color-val (cdr (assoc "background" css-list
)))
1430 (style (and org-export-odt-create-custom-styles-for-srcblocks
1433 (format org-src-block-paragraph-format
1434 background-color-val color-val
))
1438 <style:style style:name=\"%s\" style:family=\"text\">
1439 <style:text-properties fo:color=\"%s\"/>
1440 </style:style>" style-name color-val
))))))
1441 (cons style-name style
)))
1443 (defun org-odt-insert-custom-styles-for-srcblocks (styles)
1444 "Save STYLES used for colorizing of source blocks.
1445 Update styles.xml with styles that were collected as part of
1446 `org-odt-hfy-face-to-css' callbacks."
1448 (with-current-buffer
1449 (find-file-noselect (expand-file-name "styles.xml") t
)
1450 (goto-char (point-min))
1451 (when (re-search-forward "</office:styles>" nil t
)
1452 (goto-char (match-beginning 0))
1453 (insert "\n<!-- Org Htmlfontify Styles -->\n" styles
"\n")))))
1455 (defun org-odt-format-source-code-or-example-colored
1456 (lines lang caption textareap cols rows num cont rpllbl fmt
)
1457 "Format source or example blocks using `htmlfontify-string'.
1458 Use this routine when `org-export-odt-fontify-srcblocks' option
1460 (let* ((lang-m (and lang
(or (cdr (assoc lang org-src-lang-modes
)) lang
)))
1461 (mode (and lang-m
(intern (concat (if (symbolp lang-m
)
1462 (symbol-name lang-m
)
1464 (org-inhibit-startup t
)
1465 (org-startup-folded nil
)
1466 (lines (with-temp-buffer
1468 (if (functionp mode
) (funcall mode
) (fundamental-mode))
1469 (font-lock-fontify-buffer)
1471 (hfy-html-quote-regex "\\([<\"&> ]\\)")
1472 (hfy-html-quote-map '(("\"" """)
1477 (" " "<text:tab/>")))
1478 (hfy-face-to-css 'org-odt-hfy-face-to-css
)
1479 (hfy-optimisations-1 (copy-sequence hfy-optimisations
))
1480 (hfy-optimisations (add-to-list 'hfy-optimisations-1
1482 (hfy-begin-span-handler
1483 (lambda (style text-block text-id text-begins-block-p
)
1484 (insert (format "<text:span text:style-name=\"%s\">" style
))))
1485 (hfy-end-span-handler (lambda nil
(insert "</text:span>"))))
1486 (when (fboundp 'htmlfontify-string
)
1487 (let* ((lines (org-split-string lines
"[\r\n]"))
1488 (line-count (length lines
))
1493 (org-odt-format-source-line-with-line-number-and-label
1494 line rpllbl num
'htmlfontify-string
1495 (if (= i line-count
) "OrgSrcBlockLastLine" "OrgSrcBlock")))
1498 (defun org-odt-format-source-code-or-example (lines lang caption textareap
1501 "Format source or example blocks for export.
1502 Use `org-odt-format-source-code-or-example-plain' or
1503 `org-odt-format-source-code-or-example-colored' depending on the
1504 value of `org-export-odt-fontify-srcblocks."
1505 (setq lines
(org-export-number-lines
1506 lines
0 0 num cont rpllbl fmt
'preprocess
)
1508 (or (and org-export-odt-fontify-srcblocks
1509 (or (featurep 'htmlfontify
)
1510 ;; htmlfontify.el was introduced in Emacs 23.2
1511 ;; So load it with some caution
1512 (require 'htmlfontify nil t
))
1513 (fboundp 'htmlfontify-string
)
1514 'org-odt-format-source-code-or-example-colored
)
1515 'org-odt-format-source-code-or-example-plain
)
1516 lines lang caption textareap cols rows num cont rpllbl fmt
))
1518 (let ((extra (format " text:continue-numbering=\"%s\""
1519 (if cont
"true" "false"))))
1520 (org-odt-format-tags
1521 '("<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>"
1522 .
"</text:list>") lines extra
))))
1524 (defun org-odt-remap-stylenames (style-name)
1526 (cdr (assoc style-name
'(("timestamp-wrapper" .
"OrgTimestampWrapper")
1527 ("timestamp" .
"OrgTimestamp")
1528 ("timestamp-kwd" .
"OrgTimestampKeyword")
1530 ("todo" .
"OrgTodo")
1531 ("done" .
"OrgDone")
1532 ("target" .
"OrgTarget"))))
1535 (defun org-odt-format-fontify (text style
&optional id
)
1539 (org-odt-remap-stylenames style
))
1541 (org-odt-get-style-name-for-entity 'character style
))
1543 (assert (< 1 (length style
)))
1544 (let ((parent-style (pop style
)))
1545 (mapconcat (lambda (s)
1546 ;; (assert (stringp s) t)
1547 (org-odt-remap-stylenames s
)) style
"")
1548 (org-odt-remap-stylenames parent-style
)))
1549 (t (error "Don't how to handle style %s" style
)))))
1550 (org-odt-format-tags
1551 '("<text:span text:style-name=\"%s\">" .
"</text:span>")
1554 (defun org-odt-relocate-relative-path (path dir
)
1555 (if (file-name-absolute-p path
) path
1556 (file-relative-name (expand-file-name path dir
)
1557 (expand-file-name "eyecandy" dir
))))
1559 (defun org-odt-format-inline-image (thefile)
1560 (let* ((thelink (if (file-name-absolute-p thefile
) thefile
1561 (org-xml-format-href
1562 (org-odt-relocate-relative-path
1563 thefile org-current-export-file
))))
1565 (org-odt-format-tags
1566 "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
1567 (if org-export-odt-embed-images
1568 (org-odt-copy-image-file thefile
) thelink
))))
1569 (org-export-odt-format-image thefile href
)))
1571 (defvar org-odt-entity-labels-alist nil
1572 "Associate Labels with the Labeled entities.
1573 Each element of the alist is of the form (LABEL-NAME
1574 CATEGORY-NAME SEQNO LABEL-STYLE-NAME). LABEL-NAME is same as
1575 that specified by \"#+LABEL: ...\" line. CATEGORY-NAME is the
1576 type of the entity that LABEL-NAME is attached to. CATEGORY-NAME
1577 can be one of \"Table\", \"Figure\" or \"Equation\". SEQNO is
1578 the unique number assigned to the referenced entity on a
1579 per-CATEGORY basis. It is generated sequentially and is 1-based.
1580 LABEL-STYLE-NAME is a key `org-odt-label-styles'.
1582 See `org-odt-add-label-definition' and
1583 `org-odt-fixup-label-references'.")
1585 (defun org-export-odt-format-formula (src href
)
1587 (let* ((caption (org-find-text-property-in-string 'org-caption src
))
1589 (or (org-find-text-property-in-string 'org-caption-shortn src
)
1591 (caption (and caption
(org-xml-format-desc caption
)))
1592 (short-caption (and short-caption
1593 (org-xml-encode-plain-text short-caption
)))
1594 (label (org-find-text-property-in-string 'org-label src
))
1595 (latex-frag (org-find-text-property-in-string 'org-latex-src src
))
1596 (embed-as (or (and latex-frag
1597 (org-find-text-property-in-string
1598 'org-latex-src-embed-type src
))
1599 (if (or caption label
) 'paragraph
'character
)))
1602 (setq href
(org-propertize href
:title
"LaTeX Fragment"
1603 :description latex-frag
)))
1605 ((eq embed-as
'character
)
1606 (org-odt-format-entity "InlineFormula" href width height
))
1608 (org-lparse-end-paragraph)
1609 (org-lparse-insert-list-table
1610 `((,(org-odt-format-entity
1611 (if (not (or caption label
)) "DisplayFormula"
1612 "CaptionedDisplayFormula")
1613 href width height
:caption caption
:label label
1614 :short-caption short-caption
)
1615 ,(if (not (or caption label
)) ""
1616 (let* ((label-props (car org-odt-entity-labels-alist
)))
1617 (setcar (last label-props
) "math-label")
1618 (apply 'org-odt-format-label-definition
1619 caption label-props
)))))
1620 nil nil nil
":style \"OrgEquation\"" nil
'((1 "c" 8) (2 "c" 1)))
1621 (throw 'nextline nil
))))))
1623 (defvar org-odt-embedded-formulas-count
0)
1624 (defun org-odt-copy-formula-file (path)
1625 "Returns the internal name of the file"
1626 (let* ((src-file (expand-file-name
1627 path
(file-name-directory org-current-export-file
)))
1628 (target-dir (format "Formula-%04d/"
1629 (incf org-odt-embedded-formulas-count
)))
1630 (target-file (concat target-dir
"content.xml")))
1631 (when (not org-lparse-to-buffer
)
1632 (message "Embedding %s as %s ..."
1633 (substring-no-properties path
) target-file
)
1635 (make-directory target-dir
)
1636 (org-odt-create-manifest-file-entry
1637 "application/vnd.oasis.opendocument.formula" target-dir
"1.2")
1639 (case (org-odt-is-formula-link-p src-file
)
1641 (copy-file src-file target-file
'overwrite
))
1643 (org-odt-zip-extract-one src-file
"content.xml" target-dir
))
1645 (error "%s is not a formula file" src-file
)))
1647 (org-odt-create-manifest-file-entry "text/xml" target-file
))
1650 (defun org-odt-format-inline-formula (thefile)
1651 (let* ((thelink (if (file-name-absolute-p thefile
) thefile
1652 (org-xml-format-href
1653 (org-odt-relocate-relative-path
1654 thefile org-current-export-file
))))
1656 (org-odt-format-tags
1657 "<draw:object xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
1658 (file-name-directory (org-odt-copy-formula-file thefile
)))))
1659 (org-export-odt-format-formula thefile href
)))
1661 (defun org-odt-is-formula-link-p (file)
1662 (let ((case-fold-search nil
))
1664 ((string-match "\\.\\(mathml\\|mml\\)\\'" file
)
1666 ((string-match "\\.odf\\'" file
)
1669 (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
1671 "Make a OpenDocument link.
1672 OPT-PLIST is an options list.
1673 TYPE-1 is the device-type of the link (THIS://foo.html).
1674 PATH is the path of the link (http://THIS#location).
1675 FRAGMENT is the fragment part of the link, if any (foo.html#THIS).
1676 DESC is the link description, if any.
1677 ATTR is a string of other attributes of the a element."
1678 (declare (special org-lparse-par-open
))
1680 (let* ((may-inline-p
1681 (and (member type-1
'("http" "https" "file"))
1682 (org-lparse-should-inline-p path descp
)
1684 (type (if (equal type-1
"id") "file" type-1
))
1689 ;; check for inlined images
1690 ((and (member type
'("file"))
1693 filename org-export-odt-inline-image-extensions
)
1694 (or (eq t org-export-odt-inline-images
)
1695 (and org-export-odt-inline-images
(not descp
))))
1696 (org-odt-format-inline-image thefile
))
1697 ;; check for embedded formulas
1698 ((and (member type
'("file"))
1700 (org-odt-is-formula-link-p filename
)
1702 (org-odt-format-inline-formula thefile
))
1704 ((string= type
"coderef")
1705 (let* ((ref fragment
)
1706 (lineno-or-ref (cdr (assoc ref org-export-code-refs
)))
1707 (desc (and descp desc
))
1708 (org-odt-suppress-xref nil
)
1709 (href (org-xml-format-href (concat "#coderef-" ref
))))
1711 ((and (numberp lineno-or-ref
) (not desc
))
1712 (org-odt-format-link lineno-or-ref href
))
1713 ((and (numberp lineno-or-ref
) desc
1714 (string-match (regexp-quote (concat "(" ref
")")) desc
))
1715 (format (replace-match "%s" t t desc
)
1716 (org-odt-format-link lineno-or-ref href
)))
1719 (if (and desc
(string-match
1720 (regexp-quote (concat "(" ref
")"))
1722 (replace-match "%s" t t desc
)
1725 (org-odt-format-link (org-xml-format-desc desc
) href
)))))
1726 ;; links to headlines
1727 ((and (string= type
"")
1728 (or (not thefile
) (string= thefile
""))
1729 (plist-get org-lparse-opt-plist
:section-numbers
)
1730 (setq sec-frag fragment
)
1731 (or (string-match "\\`sec\\(\\(-[0-9]+\\)+\\)" sec-frag
)
1733 (loop for alias in org-export-target-aliases do
1734 (when (member fragment
(cdr alias
))
1735 (return (car alias
)))))
1736 (string-match "\\`sec\\(\\(-[0-9]+\\)+\\)" sec-frag
)))
1737 (setq sec-nos
(org-split-string (match-string 1 sec-frag
) "-"))
1738 (<= (length sec-nos
) (plist-get org-lparse-opt-plist
1740 (let ((org-odt-suppress-xref nil
))
1741 (org-odt-format-link sec-nos
(concat "#" sec-frag
) attr
)))
1743 (when (string= type
"file")
1746 ((file-name-absolute-p path
)
1747 (concat "file://" (expand-file-name path
)))
1748 (t (org-odt-relocate-relative-path
1749 thefile org-current-export-file
)))))
1751 (when (and (member type
'("" "http" "https" "file")) fragment
)
1752 (setq thefile
(concat thefile
"#" fragment
)))
1754 (setq thefile
(org-xml-format-href thefile
))
1756 (when (not (member type
'("" "file")))
1757 (setq thefile
(concat type
":" thefile
)))
1759 (let ((org-odt-suppress-xref nil
))
1760 (org-odt-format-link
1761 (org-xml-format-desc desc
) thefile attr
)))))))
1763 (defun org-odt-format-heading (text level
&optional id
)
1764 (let* ((text (if id
(org-odt-format-target text id
) text
)))
1765 (org-odt-format-tags
1766 '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
1767 "</text:h>") text level level
)))
1769 (defun org-odt-format-headline (title extra-targets tags
1770 &optional snumber level
)
1772 (org-lparse-format 'EXTRA-TARGETS extra-targets
)
1774 ;; No need to generate section numbers. They are auto-generated by
1777 ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
1779 (and tags
(concat (org-lparse-format 'SPACES
3)
1780 (org-lparse-format 'ORG-TAGS tags
)))))
1782 (defun org-odt-format-anchor (text name
&optional class
)
1783 (org-odt-format-target text name
))
1785 (defun org-odt-format-bookmark (text id
)
1787 (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id
)
1790 (defun org-odt-format-target (text id
)
1791 (let ((name (concat org-export-odt-bookmark-prefix id
)))
1793 (and id
(org-odt-format-tags
1794 "<text:bookmark-start text:name=\"%s\"/>" "" name
))
1795 (org-odt-format-bookmark text id
)
1796 (and id
(org-odt-format-tags
1797 "<text:bookmark-end text:name=\"%s\"/>" "" name
)))))
1799 (defun org-odt-format-footnote (n def
)
1800 (let ((id (concat "fn" n
))
1801 (note-class "footnote")
1802 (par-style "Footnote"))
1803 (org-odt-format-tags
1804 '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
1807 (org-odt-format-tags
1808 '("<text:note-citation>" .
"</text:note-citation>")
1810 (org-odt-format-tags
1811 '("<text:note-body>" .
"</text:note-body>")
1815 (defun org-odt-format-footnote-reference (n def refcnt
)
1817 (org-odt-format-footnote n def
)
1818 (org-odt-format-footnote-ref n
)))
1820 (defun org-odt-format-footnote-ref (n)
1821 (let ((note-class "footnote")
1823 (ref-name (concat "fn" n
)))
1824 (org-odt-format-tags
1825 '("<text:span text:style-name=\"%s\">" .
"</text:span>")
1826 (org-odt-format-tags
1827 '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" .
"</text:note-ref>")
1828 n note-class ref-format ref-name
)
1831 (defun org-odt-get-image-name (file-name)
1835 (concat (sha1 file-name
) "." (file-name-extension file-name
)) "Pictures")))
1837 (defun org-export-odt-format-image (src href
)
1838 "Create image tag with source and attributes."
1840 (let* ((caption (org-find-text-property-in-string 'org-caption src
))
1842 (or (org-find-text-property-in-string 'org-caption-shortn src
)
1844 (caption (and caption
(org-xml-format-desc caption
)))
1845 (short-caption (and short-caption
1846 (org-xml-encode-plain-text short-caption
)))
1847 (attr (org-find-text-property-in-string 'org-attributes src
))
1848 (label (org-find-text-property-in-string 'org-label src
))
1849 (latex-frag (org-find-text-property-in-string
1850 'org-latex-src src
))
1851 (category (and latex-frag
"__DvipngImage__"))
1852 (attr-plist (org-lparse-get-block-params attr
))
1854 (car (assoc-string (plist-get attr-plist
:anchor
)
1855 '(("as-char") ("paragraph") ("page")) t
)))
1857 (and user-frame-anchor
(plist-get attr-plist
:style
)))
1859 (and user-frame-anchor
(plist-get attr-plist
:attributes
)))
1861 (list user-frame-style user-frame-attrs user-frame-anchor
))
1865 (case (org-find-text-property-in-string
1866 'org-latex-src-embed-type src
)
1867 (paragraph 'paragraph
)
1871 (size (org-odt-image-size-from-file
1872 src
(plist-get attr-plist
:width
)
1873 (plist-get attr-plist
:height
)
1874 (plist-get attr-plist
:scale
) nil embed-as
))
1875 (width (car size
)) (height (cdr size
)))
1877 (setq href
(org-propertize href
:title
"LaTeX Fragment"
1878 :description latex-frag
)))
1879 (let ((frame-style-handle (concat (and (or caption label
) "Captioned")
1881 (org-odt-format-entity
1882 frame-style-handle href width height
1883 :caption caption
:label label
:category category
1884 :short-caption short-caption
1885 :user-frame-params user-frame-params
)))))
1887 (defun org-odt-format-object-description (title description
)
1888 (concat (and title
(org-odt-format-tags
1889 '("<svg:title>" .
"</svg:title>")
1890 (org-odt-encode-plain-text title t
)))
1891 (and description
(org-odt-format-tags
1892 '("<svg:desc>" .
"</svg:desc>")
1893 (org-odt-encode-plain-text description t
)))))
1895 (defun org-odt-format-frame (text width height style
&optional
1899 (if width
(format " svg:width=\"%0.2fcm\"" width
) "")
1900 (if height
(format " svg:height=\"%0.2fcm\"" height
) "")
1902 (format " text:anchor-type=\"%s\"" (or anchor-type
"paragraph")))))
1903 (org-odt-format-tags
1904 '("<draw:frame draw:style-name=\"%s\"%s>" .
"</draw:frame>")
1905 (concat text
(org-odt-format-object-description
1906 (get-text-property 0 :title text
)
1907 (get-text-property 0 :description text
)))
1908 style frame-attrs
)))
1910 (defun org-odt-format-textbox (text width height style
&optional
1912 (org-odt-format-frame
1913 (org-odt-format-tags
1914 '("<draw:text-box %s>" .
"</draw:text-box>")
1915 text
(concat (format " fo:min-height=\"%0.2fcm\"" (or height
.2))
1917 (format " fo:min-width=\"%0.2fcm\"" (or width
.2)))))
1918 width nil style extra anchor-type
))
1920 (defun org-odt-format-inlinetask (heading content
1921 &optional todo priority tags
)
1922 (org-odt-format-stylized-paragraph
1923 nil
(org-odt-format-textbox
1924 (concat (org-odt-format-stylized-paragraph
1925 "OrgInlineTaskHeading"
1927 'HEADLINE
(concat (org-lparse-format-todo todo
) " " heading
)
1929 content
) nil nil
"OrgInlineTaskFrame" " style:rel-width=\"100%\"")))
1931 (defvar org-odt-entity-frame-styles
1932 '(("As-CharImage" "__Figure__" ("OrgInlineImage" nil
"as-char"))
1933 ("ParagraphImage" "__Figure__" ("OrgDisplayImage" nil
"paragraph"))
1934 ("PageImage" "__Figure__" ("OrgPageImage" nil
"page"))
1935 ("CaptionedAs-CharImage" "__Figure__"
1936 ("OrgCaptionedImage"
1937 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
1938 ("OrgInlineImage" nil
"as-char"))
1939 ("CaptionedParagraphImage" "__Figure__"
1940 ("OrgCaptionedImage"
1941 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
1942 ("OrgImageCaptionFrame" nil
"paragraph"))
1943 ("CaptionedPageImage" "__Figure__"
1944 ("OrgCaptionedImage"
1945 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
1946 ("OrgPageImageCaptionFrame" nil
"page"))
1947 ("InlineFormula" "__MathFormula__" ("OrgInlineFormula" nil
"as-char"))
1948 ("DisplayFormula" "__MathFormula__" ("OrgDisplayFormula" nil
"as-char"))
1949 ("CaptionedDisplayFormula" "__MathFormula__"
1950 ("OrgCaptionedFormula" nil
"paragraph")
1951 ("OrgFormulaCaptionFrame" nil
"as-char"))))
1953 (defun org-odt-merge-frame-params(default-frame-params user-frame-params
)
1954 (if (not user-frame-params
) default-frame-params
1955 (assert (= (length default-frame-params
) 3))
1956 (assert (= (length user-frame-params
) 3))
1957 (loop for user-frame-param in user-frame-params
1958 for default-frame-param in default-frame-params
1959 collect
(or user-frame-param default-frame-param
))))
1961 (defun* org-odt-format-entity
(entity href width height
1962 &key caption label category
1963 user-frame-params short-caption
)
1964 (let* ((entity-style (assoc-string entity org-odt-entity-frame-styles t
))
1965 default-frame-params frame-params
)
1967 ((not (or caption label
))
1968 (setq default-frame-params
(nth 2 entity-style
))
1969 (setq frame-params
(org-odt-merge-frame-params
1970 default-frame-params user-frame-params
))
1971 (apply 'org-odt-format-frame href width height frame-params
))
1973 (setq default-frame-params
(nth 3 entity-style
))
1974 (setq frame-params
(org-odt-merge-frame-params
1975 default-frame-params user-frame-params
))
1976 (apply 'org-odt-format-textbox
1977 (org-odt-format-stylized-paragraph
1980 (apply 'org-odt-format-frame href width height
1981 (let ((entity-style-1 (copy-sequence
1982 (nth 2 entity-style
))))
1983 (setcar (cdr entity-style-1
)
1985 (cadr entity-style-1
)
1987 (format " draw:name=\"%s\" "
1991 (org-odt-format-entity-caption
1992 label caption
(or category
(nth 1 entity-style
)))))
1993 width height frame-params
)))))
1995 (defvar org-odt-embedded-images-count
0)
1996 (defun org-odt-copy-image-file (path)
1997 "Returns the internal name of the file"
1998 (let* ((image-type (file-name-extension path
))
1999 (media-type (format "image/%s" image-type
))
2000 (src-file (expand-file-name
2001 path
(file-name-directory org-current-export-file
)))
2002 (target-dir "Images/")
2004 (format "%s%04d.%s" target-dir
2005 (incf org-odt-embedded-images-count
) image-type
)))
2006 (when (not org-lparse-to-buffer
)
2007 (message "Embedding %s as %s ..."
2008 (substring-no-properties path
) target-file
)
2010 (when (= 1 org-odt-embedded-images-count
)
2011 (make-directory target-dir
)
2012 (org-odt-create-manifest-file-entry "" target-dir
))
2014 (copy-file src-file target-file
'overwrite
)
2015 (org-odt-create-manifest-file-entry media-type target-file
))
2018 (defvar org-export-odt-image-size-probe-method
2019 (append (and (executable-find "identify") '(imagemagick)) ; See Bug#10675
2021 "Ordered list of methods for determining image sizes.")
2023 (defvar org-export-odt-default-image-sizes-alist
2024 '(("as-char" .
(5 .
0.4))
2025 ("paragraph" .
(5 .
5)))
2026 "Hardcoded image dimensions one for each of the anchor
2029 ;; A4 page size is 21.0 by 29.7 cms
2030 ;; The default page settings has 2cm margin on each of the sides. So
2031 ;; the effective text area is 17.0 by 25.7 cm
2032 (defvar org-export-odt-max-image-size
'(17.0 .
20.0)
2033 "Limiting dimensions for an embedded image.")
2035 (defun org-odt-do-image-size (probe-method file
&optional dpi anchor-type
)
2036 (let* ((dpi (or dpi org-export-odt-pixels-per-inch
))
2037 (anchor-type (or anchor-type
"paragraph"))
2040 (lambda (pixels dpi
)
2041 (let* ((cms-per-inch 2.54)
2042 (inches (/ pixels dpi
)))
2043 (* cms-per-inch inches
)))))
2046 (lambda (size-in-pixels dpi
)
2048 (cons (funcall --pixels-to-cms
(car size-in-pixels
) dpi
)
2049 (funcall --pixels-to-cms
(cdr size-in-pixels
) dpi
)))))))
2052 (let ((size-in-pixels
2053 (ignore-errors ; Emacs could be in batch mode
2055 (image-size (create-image file
) 'pixels
))))
2056 (funcall --size-in-cms size-in-pixels dpi
)))
2058 (let ((size-in-pixels
2059 (let ((dim (shell-command-to-string
2060 (format "identify -format \"%%w:%%h\" \"%s\"" file
))))
2061 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim
)
2062 (cons (string-to-number (match-string 1 dim
))
2063 (string-to-number (match-string 2 dim
)))))))
2064 (funcall --size-in-cms size-in-pixels dpi
)))
2065 (t (cdr (assoc-string anchor-type
2066 org-export-odt-default-image-sizes-alist
))))))
2068 (defun org-odt-image-size-from-file (file &optional user-width
2069 user-height scale dpi embed-as
)
2070 (unless (file-name-absolute-p file
)
2071 (setq file
(expand-file-name
2072 file
(file-name-directory org-current-export-file
))))
2073 (let* (size width height
)
2074 (unless (and user-height user-width
)
2075 (loop for probe-method in org-export-odt-image-size-probe-method
2077 do
(setq size
(org-odt-do-image-size
2078 probe-method file dpi embed-as
)))
2079 (or size
(error "Cannot determine Image size. Aborting ..."))
2080 (setq width
(car size
) height
(cdr size
)))
2083 (setq width
(* width scale
) height
(* height scale
)))
2084 ((and user-height user-width
)
2085 (setq width user-width height user-height
))
2087 (setq width
(* user-height
(/ width height
)) height user-height
))
2089 (setq height
(* user-width
(/ height width
)) width user-width
))
2091 ;; ensure that an embedded image fits comfortably within a page
2092 (let ((max-width (car org-export-odt-max-image-size
))
2093 (max-height (cdr org-export-odt-max-image-size
)))
2094 (when (or (> width max-width
) (> height max-height
))
2095 (let* ((scale1 (/ max-width width
))
2096 (scale2 (/ max-height height
))
2097 (scale (min scale1 scale2
)))
2098 (setq width
(* scale width
) height
(* scale height
)))))
2099 (cons width height
)))
2101 (defvar org-odt-entity-counts-plist nil
2102 "Plist of running counters of SEQNOs for each of the CATEGORY-NAMEs.
2103 See `org-odt-entity-labels-alist' for known CATEGORY-NAMEs.")
2105 (defvar org-odt-label-styles
2106 '(("math-formula" "%c" "text" "(%n)")
2107 ("math-label" "(%n)" "text" "(%n)")
2108 ("category-and-value" "%e %n: %c" "category-and-value" "%e %n")
2109 ("value" "%e %n: %c" "value" "%n"))
2110 "Specify how labels are applied and referenced.
2111 This is an alist where each element is of the
2112 form (LABEL-STYLE-NAME LABEL-ATTACH-FMT LABEL-REF-MODE
2115 LABEL-ATTACH-FMT controls how labels and captions are attached to
2116 an entity. It may contain following specifiers - %e, %n and %c.
2117 %e is replaced with the CATEGORY-NAME. %n is replaced with
2118 \"<text:sequence ...> SEQNO </text:sequence>\". %c is replaced
2119 with CAPTION. See `org-odt-format-label-definition'.
2121 LABEL-REF-MODE and LABEL-REF-FMT controls how label references
2122 are generated. The following XML is generated for a label
2123 reference - \"<text:sequence-ref
2124 text:reference-format=\"LABEL-REF-MODE\" ...> LABEL-REF-FMT
2125 </text:sequence-ref>\". LABEL-REF-FMT may contain following
2126 specifiers - %e and %n. %e is replaced with the CATEGORY-NAME.
2127 %n is replaced with SEQNO. See
2128 `org-odt-format-label-reference'.")
2130 (defcustom org-export-odt-category-strings
2131 '(("en" "Table" "Figure" "Equation" "Equation"))
2132 "Specify category strings for various captionable entities.
2133 Captionable entity can be one of a Table, an Embedded Image, a
2134 LaTeX fragment (generated with dvipng) or a Math Formula.
2136 For example, when `org-export-default-language' is \"en\", an
2137 embedded image will be captioned as \"Figure 1: Orgmode Logo\".
2138 If you want the images to be captioned instead as \"Illustration
2139 1: Orgmode Logo\", then modify the entry for \"en\" as shown
2142 \(setq org-export-odt-category-strings
2143 '\(\(\"en\" \"Table\" \"Illustration\"
2144 \"Equation\" \"Equation\"\)\)\)"
2145 :group
'org-export-odt
2147 :type
'(repeat (list (string :tag
"Language tag")
2148 (choice :tag
"Table"
2149 (const :tag
"Use Default" nil
)
2150 (string :tag
"Category string"))
2151 (choice :tag
"Figure"
2152 (const :tag
"Use Default" nil
)
2153 (string :tag
"Category string"))
2154 (choice :tag
"Math Formula"
2155 (const :tag
"Use Default" nil
)
2156 (string :tag
"Category string"))
2157 (choice :tag
"Dvipng Image"
2158 (const :tag
"Use Default" nil
)
2159 (string :tag
"Category string")))))
2161 (defvar org-odt-category-map-alist
2162 '(("__Table__" "Table" "value")
2163 ("__Figure__" "Illustration" "value")
2164 ("__MathFormula__" "Text" "math-formula")
2165 ("__DvipngImage__" "Equation" "value")
2166 ;; ("__Table__" "Table" "category-and-value")
2167 ;; ("__Figure__" "Figure" "category-and-value")
2168 ;; ("__DvipngImage__" "Equation" "category-and-value")
2170 "Map a CATEGORY-HANDLE to OD-VARIABLE and LABEL-STYLE.
2171 This is a list where each entry is of the form \\(CATEGORY-HANDLE
2172 OD-VARIABLE LABEL-STYLE\\). CATEGORY_HANDLE identifies the
2173 captionable entity in question. OD-VARIABLE is the OpenDocument
2174 sequence counter associated with the entity. These counters are
2176 \"<text:sequence-decls>...</text:sequence-decls>\" block of
2177 `org-export-odt-content-template-file'. LABEL-STYLE is a key
2178 into `org-odt-label-styles' and specifies how a given entity
2179 should be captioned and referenced.
2181 The position of a CATEGORY-HANDLE in this list is used as an
2182 index in to per-language entry for
2183 `org-export-odt-category-strings' to retrieve a CATEGORY-NAME.
2184 This CATEGORY-NAME is then used for qualifying the user-specified
2185 captions on export.")
2187 (defun org-odt-add-label-definition (label default-category
)
2188 "Create an entry in `org-odt-entity-labels-alist' and return it."
2189 (let* ((label-props (assoc default-category org-odt-category-map-alist
))
2190 ;; identify the sequence number
2191 (counter (nth 1 label-props
))
2192 (sequence-var (intern counter
))
2193 (seqno (1+ (or (plist-get org-odt-entity-counts-plist sequence-var
)
2195 ;; assign an internal label, if user has not provided one
2196 (label (if label
(substring-no-properties label
)
2197 (format "%s-%s" default-category seqno
)))
2198 ;; identify label style
2199 (label-style (nth 2 label-props
))
2200 ;; grok language setting
2201 (en-strings (assoc-default "en" org-export-odt-category-strings
))
2202 (lang (plist-get org-lparse-opt-plist
:language
))
2203 (lang-strings (assoc-default lang org-export-odt-category-strings
))
2204 ;; retrieve localized category sting
2205 (pos (- (length org-odt-category-map-alist
)
2206 (length (memq label-props org-odt-category-map-alist
))))
2207 (category (or (nth pos lang-strings
) (nth pos en-strings
)))
2208 (label-props (list label category counter seqno label-style
)))
2209 ;; synchronize internal counters
2210 (setq org-odt-entity-counts-plist
2211 (plist-put org-odt-entity-counts-plist sequence-var seqno
))
2212 ;; stash label properties for later retrieval
2213 (push label-props org-odt-entity-labels-alist
)
2216 (defun org-odt-format-label-definition (caption label category counter
2220 (cadr (assoc-string label-style org-odt-label-styles t
))
2222 (?n .
,(org-odt-format-tags
2223 '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" .
"</text:sequence>")
2224 (format "%d" seqno
) label counter counter
))
2225 (?c .
,(or caption
"")))))
2227 (defun org-odt-format-label-reference (label category counter
2231 (let* ((fmt (cddr (assoc-string label-style org-odt-label-styles t
)))
2234 (org-odt-format-tags
2235 '("<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">"
2236 .
"</text:sequence-ref>")
2237 (format-spec fmt2
`((?e .
,category
)
2238 (?n .
,(format "%d" seqno
)))) fmt1 label
))))
2240 (defun org-odt-fixup-label-references ()
2241 (goto-char (point-min))
2242 (while (re-search-forward
2243 "<text:sequence-ref text:ref-name=\"\\([^\"]+\\)\">[ \t\n]*</text:sequence-ref>"
2245 (let* ((label (match-string 1))
2246 (label-def (assoc label org-odt-entity-labels-alist
))
2248 (apply 'org-odt-format-label-reference label-def
))))
2249 (if rpl
(replace-match rpl t t
)
2251 (format "Unable to resolve reference to label \"%s\"" label
))))))
2253 (defun org-odt-format-entity-caption (label caption category
)
2254 (if (not (or label caption
)) ""
2255 (apply 'org-odt-format-label-definition caption
2256 (org-odt-add-label-definition label category
))))
2258 (defun org-odt-format-tags (tag text
&rest args
)
2259 (let ((prefix (when org-lparse-encode-pending
"@"))
2260 (suffix (when org-lparse-encode-pending
"@")))
2261 (apply 'org-lparse-format-tags tag text prefix suffix args
)))
2263 (defvar org-odt-manifest-file-entries nil
)
2264 (defun org-odt-init-outfile (filename)
2265 (unless (executable-find "zip")
2266 ;; Not at all OSes ship with zip by default
2267 (error "Executable \"zip\" needed for creating OpenDocument files"))
2269 (let* ((content-file (expand-file-name "content.xml" org-odt-zip-dir
)))
2271 (require 'nxml-mode
)
2272 (let ((nxml-auto-insert-xml-declaration-flag nil
))
2273 (find-file-noselect content-file t
))
2276 (setq org-odt-manifest-file-entries nil
2277 org-odt-embedded-images-count
0
2278 org-odt-embedded-formulas-count
0
2279 org-odt-entity-labels-alist nil
2280 org-odt-list-stack-stashed nil
2281 org-odt-automatic-styles nil
2282 org-odt-object-counters nil
2283 org-odt-entity-counts-plist nil
)
2286 (defcustom org-export-odt-prettify-xml nil
2287 "Specify whether or not the xml output should be prettified.
2288 When this option is turned on, `indent-region' is run on all
2289 component xml buffers before they are saved. Turn this off for
2290 regular use. Turn this on if you need to examine the xml
2292 :group
'org-export-odt
2296 (defvar hfy-user-sheet-assoc
) ; bound during org-do-lparse
2297 (defun org-odt-save-as-outfile (target opt-plist
)
2298 ;; write automatic styles
2299 (org-odt-write-automatic-styles)
2302 (org-odt-update-meta-file opt-plist
)
2304 ;; write styles file
2305 (when (equal org-lparse-backend
'odt
)
2306 (org-odt-update-styles-file opt-plist
))
2308 ;; create mimetype file
2309 (let ((mimetype (org-odt-write-mimetype-file org-lparse-backend
)))
2310 (org-odt-create-manifest-file-entry mimetype
"/" "1.2"))
2312 ;; create a manifest entry for content.xml
2313 (org-odt-create-manifest-file-entry "text/xml" "content.xml")
2315 ;; write out the manifest entries before zipping
2316 (org-odt-write-manifest-file)
2318 (let ((xml-files '("mimetype" "META-INF/manifest.xml" "content.xml"
2320 (when (equal org-lparse-backend
'odt
)
2321 (push "styles.xml" xml-files
))
2323 ;; save all xml files
2324 (mapc (lambda (file)
2325 (with-current-buffer
2326 (find-file-noselect (expand-file-name file
) t
)
2327 ;; prettify output if needed
2328 (when org-export-odt-prettify-xml
2329 (indent-region (point-min) (point-max)))
2333 (let* ((target-name (file-name-nondirectory target
))
2334 (target-dir (file-name-directory target
))
2335 (cmds `(("zip" "-mX0" ,target-name
"mimetype")
2336 ("zip" "-rmTq" ,target-name
"."))))
2337 (when (file-exists-p target
)
2338 ;; FIXME: If the file is locked this throws a cryptic error
2339 (delete-file target
))
2341 (let ((coding-system-for-write 'no-conversion
) exitcode err-string
)
2342 (message "Creating odt file...")
2345 (message "Running %s" (mapconcat 'identity cmd
" "))
2347 (with-output-to-string
2349 (apply 'call-process
(car cmd
)
2350 nil standard-output nil
(cdr cmd
)))))
2351 (or (zerop exitcode
)
2352 (ignore (message "%s" err-string
))
2353 (error "Unable to create odt file (%S)" exitcode
)))
2356 ;; move the file from outdir to target-dir
2357 (rename-file target-name target-dir
)))
2359 (message "Created %s" target
)
2360 (set-buffer (find-file-noselect target t
)))
2362 (defconst org-odt-manifest-file-entry-tag
2364 <manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
2366 (defun org-odt-create-manifest-file-entry (&rest args
)
2367 (push args org-odt-manifest-file-entries
))
2369 (defun org-odt-write-manifest-file ()
2370 (make-directory "META-INF")
2371 (let ((manifest-file (expand-file-name "META-INF/manifest.xml")))
2372 (with-current-buffer
2373 (let ((nxml-auto-insert-xml-declaration-flag nil
))
2374 (find-file-noselect manifest-file t
))
2376 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
2377 <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
2379 (lambda (file-entry)
2380 (let* ((version (nth 2 file-entry
))
2382 (format " manifest:version=\"%s\"" version
)
2385 (format org-odt-manifest-file-entry-tag
2386 (nth 0 file-entry
) (nth 1 file-entry
) extra
))))
2387 org-odt-manifest-file-entries
)
2388 (insert "\n</manifest:manifest>"))))
2390 (defun org-odt-update-meta-file (opt-plist)
2391 (let ((date (org-odt-format-date (plist-get opt-plist
:date
)))
2392 (author (or (plist-get opt-plist
:author
) ""))
2393 (email (plist-get opt-plist
:email
))
2394 (keywords (plist-get opt-plist
:keywords
))
2395 (description (plist-get opt-plist
:description
))
2396 (title (plist-get opt-plist
:title
)))
2399 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
2400 <office:document-meta
2401 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
2402 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
2403 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
2404 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
2405 xmlns:ooo=\"http://openoffice.org/2004/office\"
2406 office:version=\"1.2\">
2408 (org-odt-format-author)
2409 (org-odt-format-tags
2410 '("\n<meta:initial-creator>" .
"</meta:initial-creator>") author
)
2411 (org-odt-format-tags '("\n<dc:date>" .
"</dc:date>") date
)
2412 (org-odt-format-tags
2413 '("\n<meta:creation-date>" .
"</meta:creation-date>") date
)
2414 (org-odt-format-tags '("\n<meta:generator>" .
"</meta:generator>")
2415 (when org-export-creator-info
2416 (format "Org-%s/Emacs-%s"
2419 (org-odt-format-tags '("\n<meta:keyword>" .
"</meta:keyword>") keywords
)
2420 (org-odt-format-tags '("\n<dc:subject>" .
"</dc:subject>") description
)
2421 (org-odt-format-tags '("\n<dc:title>" .
"</dc:title>") title
)
2423 " </office:meta>" "</office:document-meta>")
2424 nil
(expand-file-name "meta.xml")))
2426 ;; create a manifest entry for meta.xml
2427 (org-odt-create-manifest-file-entry "text/xml" "meta.xml"))
2429 (defun org-odt-update-styles-file (opt-plist)
2430 ;; write styles file
2431 (let ((styles-file (plist-get opt-plist
:odt-styles-file
)))
2432 (org-odt-copy-styles-file (and styles-file
2433 (read (org-trim styles-file
)))))
2435 ;; Update styles.xml - take care of outline numbering
2436 (with-current-buffer
2437 (find-file-noselect (expand-file-name "styles.xml") t
)
2438 ;; Don't make automatic backup of styles.xml file. This setting
2439 ;; prevents the backed-up styles.xml file from being zipped in to
2440 ;; odt file. This is more of a hackish fix. Better alternative
2441 ;; would be to fix the zip command so that the output odt file
2442 ;; includes only the needed files and excludes any auto-generated
2443 ;; extra files like backups and auto-saves etc etc. Note that
2444 ;; currently the zip command zips up the entire temp directory so
2445 ;; that any auto-generated files created under the hood ends up in
2446 ;; the resulting odt file.
2447 (set (make-local-variable 'backup-inhibited
) t
)
2449 ;; Import local setting of `org-export-with-section-numbers'
2450 (org-lparse-bind-local-variables opt-plist
)
2451 (org-odt-configure-outline-numbering
2452 (if org-export-with-section-numbers org-export-headline-levels
0)))
2454 ;; Write custom styles for source blocks
2455 (org-odt-insert-custom-styles-for-srcblocks
2458 (format " %s\n" (cddr style
)))
2459 hfy-user-sheet-assoc
"")))
2461 (defun org-odt-write-mimetype-file (format)
2462 ;; create mimetype file
2465 (odt "application/vnd.oasis.opendocument.text")
2466 (odf "application/vnd.oasis.opendocument.formula")
2467 (t (error "Unknown OpenDocument backend %S" org-lparse-backend
)))))
2468 (write-region mimetype nil
(expand-file-name "mimetype"))
2471 (defun org-odt-finalize-outfile ()
2472 (org-odt-delete-empty-paragraphs))
2474 (defun org-odt-delete-empty-paragraphs ()
2475 (goto-char (point-min))
2476 (let ((open "<text:p[^>]*>")
2477 (close "</text:p>"))
2478 (while (re-search-forward (format "%s[ \r\n\t]*%s" open close
) nil t
)
2479 (replace-match ""))))
2481 (defcustom org-export-odt-convert-processes
2483 "soffice --headless --convert-to %f%x --outdir %d %i")
2485 "unoconv -f %f -o %d %i"))
2486 "Specify a list of document converters and their usage.
2487 The converters in this list are offered as choices while
2488 customizing `org-export-odt-convert-process'.
2490 This variable is a list where each element is of the
2491 form (CONVERTER-NAME CONVERTER-CMD). CONVERTER-NAME is the name
2492 of the converter. CONVERTER-CMD is the shell command for the
2493 converter and can contain format specifiers. These format
2494 specifiers are interpreted as below:
2496 %i input file name in full
2497 %I input file name as a URL
2498 %f format of the output file
2499 %o output file name in full
2500 %O output file name as a URL
2501 %d output dir in full
2502 %D output dir as a URL.
2503 %x extra options as set in `org-export-odt-convert-capabilities'."
2504 :group
'org-export-odt
2508 (const :tag
"None" nil
)
2509 (alist :tag
"Converters"
2510 :key-type
(string :tag
"Converter Name")
2511 :value-type
(group (string :tag
"Command line")))))
2513 (defcustom org-export-odt-convert-process
"LibreOffice"
2514 "Use this converter to convert from \"odt\" format to other formats.
2515 During customization, the list of converter names are populated
2516 from `org-export-odt-convert-processes'."
2517 :group
'org-export-odt
2519 :type
'(choice :convert-widget
2521 (apply 'widget-convert
(widget-type w
)
2522 (eval (car (widget-get w
:args
)))))
2523 `((const :tag
"None" nil
)
2524 ,@(mapcar (lambda (c)
2525 `(const :tag
,(car c
) ,(car c
)))
2526 org-export-odt-convert-processes
))))
2528 (defcustom org-export-odt-convert-capabilities
2530 ("odt" "ott" "doc" "rtf" "docx")
2531 (("pdf" "pdf") ("odt" "odt") ("rtf" "rtf") ("ott" "ott")
2532 ("doc" "doc" ":\"MS Word 97\"") ("docx" "docx") ("html" "html")))
2535 (("pdf" "pdf") ("odt" "odt") ("html" "html")))
2537 ("ods" "ots" "xls" "csv" "xlsx")
2538 (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv") ("ods" "ods")
2539 ("xls" "xls") ("xlsx" "xlsx")))
2541 ("odp" "otp" "ppt" "pptx")
2542 (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("otp" "otp") ("ppt" "ppt")
2543 ("pptx" "pptx") ("odg" "odg"))))
2544 "Specify input and output formats of `org-export-odt-convert-process'.
2545 More correctly, specify the set of input and output formats that
2546 the user is actually interested in.
2548 This variable is an alist where each element is of the
2549 form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
2550 INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
2551 alist where each element is of the form (OUTPUT-FMT
2552 OUTPUT-FILE-EXTENSION EXTRA-OPTIONS).
2554 The variable is interpreted as follows:
2555 `org-export-odt-convert-process' can take any document that is in
2556 INPUT-FMT-LIST and produce any document that is in the
2557 OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
2558 OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
2559 serves dual purposes:
2560 - It is used for populating completion candidates during
2561 `org-export-odt-convert' commands.
2562 - It is used as the value of \"%f\" specifier in
2563 `org-export-odt-convert-process'.
2565 EXTRA-OPTIONS is used as the value of \"%x\" specifier in
2566 `org-export-odt-convert-process'.
2568 DOCUMENT-CLASS is used to group a set of file formats in
2569 INPUT-FMT-LIST in to a single class.
2571 Note that this variable inherently captures how LibreOffice based
2572 converters work. LibreOffice maps documents of various formats
2573 to classes like Text, Web, Spreadsheet, Presentation etc and
2574 allow document of a given class (irrespective of it's source
2575 format) to be converted to any of the export formats associated
2578 See default setting of this variable for an typical
2580 :group
'org-export-odt
2584 (const :tag
"None" nil
)
2585 (alist :tag
"Capabilities"
2586 :key-type
(string :tag
"Document Class")
2588 (group (repeat :tag
"Input formats" (string :tag
"Input format"))
2589 (alist :tag
"Output formats"
2590 :key-type
(string :tag
"Output format")
2592 (group (string :tag
"Output file extension")
2594 (const :tag
"None" nil
)
2595 (string :tag
"Extra options"))))))))
2597 (declare-function org-create-math-formula
"org"
2598 (latex-frag &optional mathml-file
))
2601 (defun org-export-odt-convert (&optional in-file out-fmt prefix-arg
)
2602 "Convert IN-FILE to format OUT-FMT using a command line converter.
2603 IN-FILE is the file to be converted. If unspecified, it defaults
2604 to variable `buffer-file-name'. OUT-FMT is the desired output
2605 format. Use `org-export-odt-convert-process' as the converter.
2606 If PREFIX-ARG is non-nil then the newly converted file is opened
2607 using `org-open-file'."
2609 (append (org-lparse-convert-read-params) current-prefix-arg
))
2610 (org-lparse-do-convert in-file out-fmt prefix-arg
))
2612 (defun org-odt-get (what &optional opt-plist
)
2615 (EXPORT-DIR (org-export-directory :html opt-plist
))
2616 (FILE-NAME-EXTENSION "odt")
2617 (EXPORT-BUFFER-NAME "*Org ODT Export*")
2618 (ENTITY-CONTROL org-odt-entity-control-callbacks-alist
)
2619 (ENTITY-FORMAT org-odt-entity-format-callbacks-alist
)
2620 (INIT-METHOD 'org-odt-init-outfile
)
2621 (FINAL-METHOD 'org-odt-finalize-outfile
)
2622 (SAVE-METHOD 'org-odt-save-as-outfile
)
2624 (and org-export-odt-convert-process
2625 (cadr (assoc-string org-export-odt-convert-process
2626 org-export-odt-convert-processes t
))))
2627 (CONVERT-CAPABILITIES
2628 (and org-export-odt-convert-process
2629 (cadr (assoc-string org-export-odt-convert-process
2630 org-export-odt-convert-processes t
))
2631 org-export-odt-convert-capabilities
))
2633 (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps
)
2634 (INLINE-IMAGES 'maybe
)
2635 (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
2636 (PLAIN-TEXT-MAP '(("&" .
"&") ("<" .
"<") (">" .
">")))
2637 (TABLE-FIRST-COLUMN-AS-LABELS nil
)
2638 (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY
"," 'superscript
))
2639 (CODING-SYSTEM-FOR-WRITE 'utf-8
)
2640 (CODING-SYSTEM-FOR-SAVE 'utf-8
)
2641 (t (error "Unknown property: %s" what
))))
2643 (defvar org-lparse-latex-fragment-fallback
) ; set by org-do-lparse
2644 (defun org-export-odt-do-preprocess-latex-fragments ()
2645 "Convert LaTeX fragments to images."
2646 (let* ((latex-frag-opt (plist-get org-lparse-opt-plist
:LaTeX-fragments
))
2647 (latex-frag-opt ; massage the options
2648 (or (and (member latex-frag-opt
'(mathjax t
))
2649 (not (and (fboundp 'org-format-latex-mathml-available-p
)
2650 (org-format-latex-mathml-available-p)))
2651 (prog1 org-lparse-latex-fragment-fallback
2654 "LaTeX to MathML converter not available. "
2655 (format "Using %S instead."
2656 org-lparse-latex-fragment-fallback
)))))
2658 cache-dir display-msg
)
2660 ((eq latex-frag-opt
'dvipng
)
2661 (setq cache-dir
"ltxpng/")
2662 (setq display-msg
"Creating LaTeX image %s"))
2663 ((member latex-frag-opt
'(mathjax t
))
2664 (setq latex-frag-opt
'mathml
)
2665 (setq cache-dir
"ltxmathml/")
2666 (setq display-msg
"Creating MathML formula %s")))
2667 (when (and org-current-export-file
)
2669 (concat cache-dir
(file-name-sans-extension
2670 (file-name-nondirectory org-current-export-file
)))
2671 org-current-export-dir nil display-msg
2672 nil nil latex-frag-opt
))))
2674 (defadvice org-format-latex-as-mathml
2675 (after org-odt-protect-latex-fragment activate
)
2676 "Encode LaTeX fragment as XML.
2677 Do this when translation to MathML fails."
2678 (when (or (not (> (length ad-return-value
) 0))
2679 (get-text-property 0 'org-protected ad-return-value
))
2680 (setq ad-return-value
2681 (org-propertize (org-odt-encode-plain-text (ad-get-arg 0))
2682 'org-protected t
))))
2684 (defun org-export-odt-preprocess-latex-fragments ()
2685 (when (equal org-export-current-backend
'odt
)
2686 (org-export-odt-do-preprocess-latex-fragments)))
2688 (defun org-export-odt-preprocess-label-references ()
2689 (goto-char (point-min))
2690 (let (label label-components category value pretty-label
)
2691 (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t
)
2692 (org-if-unprotected-at (match-beginning 1)
2694 (let ((org-lparse-encode-pending t
)
2695 (label (match-string 1)))
2696 ;; markup generated below is mostly an eye-candy. At
2697 ;; pre-processing stage, there is no information on which
2698 ;; entity a label reference points to. The actual markup
2699 ;; is generated as part of `org-odt-fixup-label-references'
2700 ;; which gets called at the fag end of export. By this
2701 ;; time we would have seen and collected all the label
2702 ;; definitions in `org-odt-entity-labels-alist'.
2703 (org-odt-format-tags
2704 '("<text:sequence-ref text:ref-name=\"%s\">" .
2705 "</text:sequence-ref>")
2706 "" (org-add-props label
'(org-protected t
)))) t t
)))))
2708 ;; process latex fragments as part of
2709 ;; `org-export-preprocess-after-blockquote-hook'. Note that this hook
2710 ;; is the one that is closest and well before the call to
2711 ;; `org-export-attach-captions-and-attributes' in
2712 ;; `org-export-preprocess-string'. The above arrangement permits
2713 ;; captions, labels and attributes to be attached to png images
2714 ;; generated out of latex equations.
2715 (add-hook 'org-export-preprocess-after-blockquote-hook
2716 'org-export-odt-preprocess-latex-fragments
)
2718 (defun org-export-odt-preprocess (parameters)
2719 (org-export-odt-preprocess-label-references))
2721 (declare-function archive-zip-extract
"arc-mode.el" (archive name
))
2722 (defun org-odt-zip-extract-one (archive member
&optional target
)
2724 (let* ((target (or target default-directory
))
2725 (archive (expand-file-name archive
))
2726 (archive-zip-extract
2727 (list "unzip" "-qq" "-o" "-d" target
))
2728 exit-code command-output
)
2729 (setq command-output
2731 (setq exit-code
(archive-zip-extract archive member
))
2733 (unless (zerop exit-code
)
2734 (message command-output
)
2735 (error "Extraction failed"))))
2737 (defun org-odt-zip-extract (archive members
&optional target
)
2738 (when (atom members
) (setq members
(list members
)))
2739 (mapc (lambda (member)
2740 (org-odt-zip-extract-one archive member target
))
2743 (defun org-odt-copy-styles-file (&optional styles-file
)
2744 ;; Non-availability of styles.xml is not a critical error. For now
2745 ;; throw an error purely for aesthetic reasons.
2746 (setq styles-file
(or styles-file
2747 org-export-odt-styles-file
2748 (expand-file-name "OrgOdtStyles.xml"
2750 (error "org-odt: Missing styles file?")))
2752 ((listp styles-file
)
2753 (let ((archive (nth 0 styles-file
))
2754 (members (nth 1 styles-file
)))
2755 (org-odt-zip-extract archive members
)
2758 (when (org-file-image-p member
)
2759 (let* ((image-type (file-name-extension member
))
2760 (media-type (format "image/%s" image-type
)))
2761 (org-odt-create-manifest-file-entry media-type member
))))
2763 ((and (stringp styles-file
) (file-exists-p styles-file
))
2764 (let ((styles-file-type (file-name-extension styles-file
)))
2766 ((string= styles-file-type
"xml")
2767 (copy-file styles-file
"styles.xml" t
))
2768 ((member styles-file-type
'("odt" "ott"))
2769 (org-odt-zip-extract styles-file
"styles.xml")))))
2771 (error (format "Invalid specification of styles.xml file: %S"
2772 org-export-odt-styles-file
))))
2774 ;; create a manifest entry for styles.xml
2775 (org-odt-create-manifest-file-entry "text/xml" "styles.xml"))
2777 (defun org-odt-configure-outline-numbering (level)
2778 "Outline numbering is retained only upto LEVEL.
2779 To disable outline numbering pass a LEVEL of 0."
2780 (goto-char (point-min))
2782 "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
2784 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
2785 (while (re-search-forward regex nil t
)
2786 (when (> (string-to-number (match-string 2)) level
)
2787 (replace-match replacement t nil
))))
2791 (defun org-export-as-odf (latex-frag &optional odf-file
)
2792 "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
2793 Use `org-create-math-formula' to convert LATEX-FRAG first to
2794 MathML. When invoked as an interactive command, use
2795 `org-latex-regexps' to infer LATEX-FRAG from currently active
2796 region. If no LaTeX fragments are found, prompt for it. Push
2797 MathML source to kill ring, if `org-export-copy-to-kill-ring' is
2801 (setq frag
(and (setq frag
(and (org-region-active-p)
2802 (buffer-substring (region-beginning)
2804 (loop for e in org-latex-regexps
2805 thereis
(when (string-match (nth 1 e
) frag
)
2806 (match-string (nth 2 e
) frag
)))))
2807 (read-string "LaTeX Fragment: " frag nil frag
))
2808 ,(let ((odf-filename (expand-file-name
2810 (file-name-sans-extension
2811 (or (file-name-nondirectory buffer-file-name
)))
2813 (file-name-directory buffer-file-name
))))
2814 (read-file-name "ODF filename: " nil odf-filename nil
2815 (file-name-nondirectory odf-filename
)))))
2816 (org-odt-cleanup-xml-buffers
2817 (let* ((org-lparse-backend 'odf
)
2818 org-lparse-opt-plist
2819 (filename (or odf-file
2822 (file-name-sans-extension
2823 (or (file-name-nondirectory buffer-file-name
)))
2825 (file-name-directory buffer-file-name
))))
2826 (buffer (find-file-noselect (org-odt-init-outfile filename
)))
2827 (coding-system-for-write 'utf-8
)
2828 (save-buffer-coding-system 'utf-8
))
2830 (set-buffer-file-coding-system coding-system-for-write
)
2831 (let ((mathml (org-create-math-formula latex-frag
)))
2832 (unless mathml
(error "No Math formula created"))
2834 (or (org-export-push-to-kill-ring
2835 (upcase (symbol-name org-lparse-backend
)))
2836 (message "Exporting... done")))
2837 (org-odt-save-as-outfile filename nil
))))
2840 (defun org-export-as-odf-and-open ()
2841 "Export LaTeX fragment as OpenDocument formula and immediately open it.
2842 Use `org-export-as-odf' to read LaTeX fragment and OpenDocument
2845 (org-lparse-and-open
2846 nil nil nil
(call-interactively 'org-export-as-odf
)))
2850 ;;; org-odt.el ends here