org-odt.el: Set mathml option
[org-mode/org-jambu.git] / contrib / lisp / org-odt.el
blobe7cf978ae2dab4cc58f901d95549114f4bb04857
1 ;;; org-odt.el --- OpenDocumentText export for Org-mode
3 ;; Copyright (C) 2010-2011 Jambunathan <kjambunathan at gmail dot com>
5 ;; Author: Jambunathan K <kjambunathan at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;; Version: 0.8
10 ;; This file is not (yet) part of GNU Emacs.
11 ;; However, it is distributed under the same license.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
29 ;;; Code:
30 (eval-when-compile (require 'cl))
31 (require 'org-lparse)
33 (defun org-odt-end-export ()
34 ;; remove empty paragraphs
35 (goto-char (point-min))
36 (while (re-search-forward
37 "<text:p\\( text:style-name=\"Text_20_body\"\\)?>[ \r\n\t]*</text:p>"
38 nil t)
39 (replace-match ""))
40 (goto-char (point-min))
42 ;; Convert whitespace place holders
43 (goto-char (point-min))
44 (let (beg end n)
45 (while (setq beg (next-single-property-change (point) 'org-whitespace))
46 (setq n (get-text-property beg 'org-whitespace)
47 end (next-single-property-change beg 'org-whitespace))
48 (goto-char beg)
49 (delete-region beg end)
50 (insert (format "<span style=\"visibility:hidden;\">%s</span>"
51 (make-string n ?x)))))
53 ;; Remove empty lines at the beginning of the file.
54 (goto-char (point-min))
55 (when (looking-at "\\s-+\n") (replace-match ""))
57 ;; Remove display properties
58 (remove-text-properties (point-min) (point-max) '(display t)))
60 (defvar org-odt-suppress-xref nil)
61 (defconst org-export-odt-special-string-regexps
62 '(("\\\\-" . "&#x00ad;\\1") ; shy
63 ("---\\([^-]\\)" . "&#x2014;\\1") ; mdash
64 ("--\\([^-]\\)" . "&#x2013;\\1") ; ndash
65 ("\\.\\.\\." . "&#x2026;")) ; hellip
66 "Regular expressions for special string conversion.")
68 (defconst org-odt-lib-dir (file-name-directory load-file-name))
69 (defconst org-odt-data-dir
70 (let ((dir1 (expand-file-name "../odt" org-odt-lib-dir)) ; git
71 (dir2 (expand-file-name "./contrib/odt" org-odt-lib-dir))) ; elpa
72 (cond
73 ((file-directory-p dir1) dir1)
74 ((file-directory-p dir2) dir2)
75 (t (error "Cannot find factory styles file. Check package dir layout"))))
76 "Directory that holds auxiliary files used by the ODT exporter.
78 The 'styles' subdir contains the following xml files -
79 'OrgOdtStyles.xml' and 'OrgOdtAutomaticStyles.xml' - which are
80 used as factory settings of `org-export-odt-styles-file' and
81 `org-export-odt-automatic-styles-file'.
83 The 'etc/schema' subdir contains rnc files for validating of
84 OpenDocument xml files.")
86 (defvar org-odt-file-extensions
87 '(("odt" . "OpenDocument Text")
88 ("ott" . "OpenDocument Text Template")
89 ("odm" . "OpenDocument Master Document")
90 ("ods" . "OpenDocument Spreadsheet")
91 ("ots" . "OpenDocument Spreadsheet Template")
92 ("odg" . "OpenDocument Drawing (Graphics)")
93 ("otg" . "OpenDocument Drawing Template")
94 ("odp" . "OpenDocument Presentation")
95 ("otp" . "OpenDocument Presentation Template")
96 ("odi" . "OpenDocument Image")
97 ("odf" . "OpenDocument Formula")
98 ("odc" . "OpenDocument Chart")
99 ("doc" . "Microsoft Text")
100 ("docx" . "Microsoft Text")
101 ("xls" . "Microsoft Spreadsheet")
102 ("xlsx" . "Microsoft Spreadsheet")
103 ("ppt" . "Microsoft Presentation")
104 ("pptx" . "Microsoft Presentation")))
106 (defvar org-odt-ms-file-extensions
107 '(("doc" . "Microsoft Text")
108 ("docx" . "Microsoft Text")
109 ("xls" . "Microsoft Spreadsheet")
110 ("xlsx" . "Microsoft Spreadsheet")
111 ("ppt" . "Microsoft Presentation")
112 ("pptx" . "Microsoft Presentation")))
114 ;; RelaxNG validation of OpenDocument xml files
115 (eval-after-load 'rng-nxml
116 '(setq rng-nxml-auto-validate-flag t))
118 (eval-after-load 'rng-loc
119 '(add-to-list 'rng-schema-locating-files
120 (expand-file-name "etc/schema/schemas.xml" org-odt-data-dir)))
122 (mapc
123 (lambda (desc)
124 ;; Let Org open all OpenDocument files using system-registered app
125 (add-to-list 'org-file-apps
126 (cons (concat "\\." (car desc) "\\'") 'system))
127 ;; Let Emacs open all OpenDocument files in archive mode
128 (add-to-list 'auto-mode-alist
129 (cons (concat "\\." (car desc) "\\'") 'archive-mode)))
130 org-odt-file-extensions)
132 (mapc
133 (lambda (desc)
134 ;; Let Org open all Microsoft files using system-registered app
135 (add-to-list 'org-file-apps
136 (cons (concat "\\." (car desc) "\\'") 'system)))
137 org-odt-ms-file-extensions)
139 ;; register the odt exporter with the pre-processor
140 (add-to-list 'org-export-backends 'odt)
142 ;; register the odt exporter with org-lparse library
143 (org-lparse-register-backend 'odt)
145 (defun org-odt-unload-function ()
146 (org-lparse-unregister-backend 'odt)
147 (remove-hook 'org-export-preprocess-after-blockquote-hook
148 'org-export-odt-preprocess-latex-fragments)
149 nil)
151 (defcustom org-export-odt-automatic-styles-file nil
152 "Automatic styles for use with ODT exporter.
153 If unspecified, the file under `org-odt-data-dir' is used."
154 :type 'file
155 :group 'org-export-odt)
157 (defcustom org-export-odt-styles-file nil
158 "Default styles file for use with ODT export.
159 Valid values are one of:
160 1. nil
161 2. path to a styles.xml file
162 3. path to a *.odt or a *.ott file
163 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
164 ...))
166 In case of option 1, an in-built styles.xml is used. See
167 `org-odt-data-dir' for more information.
169 In case of option 3, the specified file is unzipped and the
170 styles.xml embedded therein is used.
172 In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
173 and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
174 generated odt file. Use relative path for specifying the
175 FILE-MEMBERS. styles.xml must be specified as one of the
176 FILE-MEMBERS.
178 Use options 1, 2 or 3 only if styles.xml alone suffices for
179 achieving the desired formatting. Use option 4, if the styles.xml
180 references additional files like header and footer images for
181 achieving the desired formattting."
182 :group 'org-export-odt
183 :type
184 '(choice
185 (const :tag "Factory settings" nil)
186 (file :must-match t :tag "styles.xml")
187 (file :must-match t :tag "ODT or OTT file")
188 (list :tag "ODT or OTT file + Members"
189 (file :must-match t :tag "ODF Text or Text Template file")
190 (cons :tag "Members"
191 (file :tag " Member" "styles.xml")
192 (repeat (file :tag "Member"))))))
194 (defconst org-export-odt-tmpdir-prefix "odt-")
195 (defconst org-export-odt-bookmark-prefix "OrgXref.")
196 (defcustom org-export-odt-use-bookmarks-for-internal-links t
197 "Export Internal links as bookmarks?."
198 :type 'boolean
199 :group 'org-export-odt)
201 (defcustom org-export-odt-embed-images t
202 "Should the images be copied in to the odt file or just linked?"
203 :type 'boolean
204 :group 'org-export-odt)
206 (defcustom org-odt-export-inline-images 'maybe
207 "Non-nil means inline images into exported HTML pages.
208 This is done using an <img> tag. When nil, an anchor with href is used to
209 link to the image. If this option is `maybe', then images in links with
210 an empty description will be inlined, while images with a description will
211 be linked only."
212 :group 'org-odt-export
213 :type '(choice (const :tag "Never" nil)
214 (const :tag "Always" t)
215 (const :tag "When there is no description" maybe)))
217 (defcustom org-odt-export-inline-image-extensions
218 '("png" "jpeg" "jpg" "gif")
219 "Extensions of image files that can be inlined into HTML."
220 :type '(repeat (string :tag "Extension"))
221 :group 'org-odt-export)
223 (defcustom org-export-odt-pixels-per-inch display-pixels-per-inch
224 ;; FIXME add docstring
226 :type 'float
227 :group 'org-export-odt)
229 (defvar org-export-odt-default-org-styles-alist
230 '((paragraph . ((default . "Text_20_body")
231 (fixedwidth . "OrgFixedWidthBlock")
232 (verse . "OrgVerse")
233 (quote . "Quotations")
234 (blockquote . "Quotations")
235 (center . "OrgCenter")
236 (left . "OrgLeft")
237 (right . "OrgRight")
238 (title . "Heading_20_1.title")
239 (footnote . "Footnote")
240 (src . "OrgSrcBlock")
241 (illustration . "Illustration")
242 (table . "Table")
243 (definition-term . "Text_20_body_20_bold")
244 (horizontal-line . "Horizontal_20_Line")))
245 (character . ((bold . "Bold")
246 (emphasis . "Emphasis")
247 (code . "OrgCode")
248 (verbatim . "OrgCode")
249 (strike . "Strikethrough")
250 (underline . "Underline")
251 (subscript . "OrgSubscript")
252 (superscript . "OrgSuperscript")))
253 (list . ((ordered . "OrgNumberedList")
254 (unordered . "OrgBulletedList")
255 (description . "OrgDescriptionList"))))
256 "Default styles for various entities.")
258 (defvar org-export-odt-org-styles-alist org-export-odt-default-org-styles-alist)
259 (defun org-odt-get-style-name-for-entity (category &optional entity)
260 (let ((entity (or entity 'default)))
262 (cdr (assoc entity (cdr (assoc category
263 org-export-odt-org-styles-alist))))
264 (cdr (assoc entity (cdr (assoc category
265 org-export-odt-default-org-styles-alist))))
266 (error "Cannot determine style name for entity %s of type %s"
267 entity category))))
269 (defcustom org-export-odt-preferred-output-format nil
270 "Automatically post-process to this format after exporting to \"odt\".
271 Interactive commands `org-export-as-odt' and
272 `org-export-as-odt-and-open' export first to \"odt\" format and
273 then use an external converter to convert the resulting document
274 to this format.
276 The converter used is that specified with CONVERT-METHOD option
277 in `org-odt-get'. If the above option is unspecified then
278 `org-lparse-convert-process' is used.
280 The format specified here should be listed in OTHER-BACKENDS
281 option of `org-odt-get' or `org-lparse-convert-capabilities' as
282 appropriate."
283 :group 'org-odt
284 :type '(choice :convert-widget
285 (lambda (w)
286 (apply 'widget-convert (widget-type w)
287 (eval (car (widget-get w :args)))))
288 `((const :tag "None" nil)
289 ,@(mapcar (lambda (c)
290 `(const :tag ,(car c) ,(car c)))
291 (org-lparse-get-other-backends "odt")))))
293 ;;;###autoload
294 (defun org-export-as-odt-and-open (arg)
295 "Export the outline as ODT and immediately open it with a browser.
296 If there is an active region, export only the region.
297 The prefix ARG specifies how many levels of the outline should become
298 headlines. The default is 3. Lower levels will become bulleted lists."
299 (interactive "P")
300 (org-lparse-and-open
301 (or org-export-odt-preferred-output-format "odt") "odt" arg))
303 ;;;###autoload
304 (defun org-export-as-odt-batch ()
305 "Call the function `org-lparse-batch'.
306 This function can be used in batch processing as:
307 emacs --batch
308 --load=$HOME/lib/emacs/org.el
309 --eval \"(setq org-export-headline-levels 2)\"
310 --visit=MyFile --funcall org-export-as-odt-batch"
311 (org-lparse-batch "odt"))
313 ;;;###autoload
314 (defun org-export-as-odt-to-buffer (arg)
315 "Call `org-lparse-odt` with output to a temporary buffer.
316 No file is created. The prefix ARG is passed through to `org-lparse-to-buffer'."
317 (interactive "P")
318 (org-lparse-to-buffer "odt" arg))
320 ;;;###autoload
321 (defun org-replace-region-by-odt (beg end)
322 "Assume the current region has org-mode syntax, and convert it to ODT.
323 This can be used in any buffer. For example, you could write an
324 itemized list in org-mode syntax in an ODT buffer and then use this
325 command to convert it."
326 (interactive "r")
327 (org-replace-region-by "odt" beg end))
329 ;;;###autoload
330 (defun org-export-region-as-odt (beg end &optional body-only buffer)
331 "Convert region from BEG to END in org-mode buffer to ODT.
332 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
333 contents, and only produce the region of converted text, useful for
334 cut-and-paste operations.
335 If BUFFER is a buffer or a string, use/create that buffer as a target
336 of the converted ODT. If BUFFER is the symbol `string', return the
337 produced ODT as a string and leave not buffer behind. For example,
338 a Lisp program could call this function in the following way:
340 (setq odt (org-export-region-as-odt beg end t 'string))
342 When called interactively, the output buffer is selected, and shown
343 in a window. A non-interactive call will only return the buffer."
344 (interactive "r\nP")
345 (org-lparse-region "odt" beg end body-only buffer))
347 ;;; org-export-as-odt
348 ;;;###autoload
349 (defun org-export-as-odt (arg &optional hidden ext-plist
350 to-buffer body-only pub-dir)
351 "Export the outline as a OpenDocumentText file.
352 If there is an active region, export only the region. The prefix
353 ARG specifies how many levels of the outline should become
354 headlines. The default is 3. Lower levels will become bulleted
355 lists. HIDDEN is obsolete and does nothing.
356 EXT-PLIST is a property list with external parameters overriding
357 org-mode's default settings, but still inferior to file-local
358 settings. When TO-BUFFER is non-nil, create a buffer with that
359 name and export to that buffer. If TO-BUFFER is the symbol
360 `string', don't leave any buffer behind but just return the
361 resulting XML as a string. When BODY-ONLY is set, don't produce
362 the file header and footer, simply return the content of
363 <body>...</body>, without even the body tags themselves. When
364 PUB-DIR is set, use this as the publishing directory."
365 (interactive "P")
366 (org-lparse (or org-export-odt-preferred-output-format "odt")
367 "odt" arg hidden ext-plist to-buffer body-only pub-dir))
369 (defvar org-odt-entity-control-callbacks-alist
370 `((EXPORT
371 . (org-odt-begin-export org-odt-end-export))
372 (DOCUMENT-CONTENT
373 . (org-odt-begin-document-content org-odt-end-document-content))
374 (DOCUMENT-BODY
375 . (org-odt-begin-document-body org-odt-end-document-body))
376 (TOC
377 . (org-odt-begin-toc org-odt-end-toc))
378 (ENVIRONMENT
379 . (org-odt-begin-environment org-odt-end-environment))
380 (FOOTNOTE-DEFINITION
381 . (org-odt-begin-footnote-definition org-odt-end-footnote-definition))
382 (TABLE
383 . (org-odt-begin-table org-odt-end-table))
384 (TABLE-ROWGROUP
385 . (org-odt-begin-table-rowgroup org-odt-end-table-rowgroup))
386 (LIST
387 . (org-odt-begin-list org-odt-end-list))
388 (LIST-ITEM
389 . (org-odt-begin-list-item org-odt-end-list-item))
390 (OUTLINE
391 . (org-odt-begin-outline org-odt-end-outline))
392 (OUTLINE-TEXT
393 . (org-odt-begin-outline-text org-odt-end-outline-text))
394 (PARAGRAPH
395 . (org-odt-begin-paragraph org-odt-end-paragraph)))
398 (defvar org-odt-entity-format-callbacks-alist
399 `((EXTRA-TARGETS . org-lparse-format-extra-targets)
400 (ORG-TAGS . org-lparse-format-org-tags)
401 (SECTION-NUMBER . org-lparse-format-section-number)
402 (HEADLINE . org-odt-format-headline)
403 (TOC-ENTRY . org-odt-format-toc-entry)
404 (TOC-ITEM . org-odt-format-toc-item)
405 (TAGS . org-odt-format-tags)
406 (SPACES . org-odt-format-spaces)
407 (TABS . org-odt-format-tabs)
408 (LINE-BREAK . org-odt-format-line-break)
409 (FONTIFY . org-odt-format-fontify)
410 (TODO . org-lparse-format-todo)
411 (LINK . org-odt-format-link)
412 (INLINE-IMAGE . org-odt-format-inline-image)
413 (ORG-LINK . org-odt-format-org-link)
414 (HEADING . org-odt-format-heading)
415 (ANCHOR . org-odt-format-anchor)
416 (TABLE . org-lparse-format-table)
417 (TABLE-ROW . org-odt-format-table-row)
418 (TABLE-CELL . org-odt-format-table-cell)
419 (FOOTNOTES-SECTION . ignore)
420 (FOOTNOTE-REFERENCE . org-odt-format-footnote-reference)
421 (HORIZONTAL-LINE . org-odt-format-horizontal-line)
422 (COMMENT . org-odt-format-comment)
423 (LINE . org-odt-format-line)
424 (ORG-ENTITY . org-odt-format-org-entity))
427 ;;;_. callbacks
428 ;;;_. control callbacks
429 ;;;_ , document body
430 (defun org-odt-begin-office-body ()
431 (insert "
432 <office:body>
433 <office:text>
434 <text:sequence-decls>
435 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Illustration\"/>
436 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Table\"/>
437 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Text\"/>
438 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Drawing\"/>
439 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Equation\"/>
440 </text:sequence-decls>"))
442 ;; Following variable is let bound when `org-do-lparse' is in
443 ;; progress. See org-html.el.
444 (defvar org-lparse-toc)
445 (defun org-odt-begin-document-body (opt-plist)
446 (org-odt-begin-office-body)
447 (let ((title (plist-get opt-plist :title)))
448 (when title
449 (insert
450 (org-odt-format-stylized-paragraph 'title title))))
452 ;; insert toc
453 (when org-lparse-toc
454 (insert "\n" org-lparse-toc "\n")))
456 (defvar org-lparse-body-only) ; let bound during org-do-lparse
457 (defvar org-lparse-to-buffer) ; let bound during org-do-lparse
458 (defun org-odt-end-document-body (opt-plist)
459 (unless org-lparse-body-only
460 (org-lparse-insert-tag "</office:text>")
461 (org-lparse-insert-tag "</office:body>")))
463 (defconst org-odt-document-content-header
464 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
465 <office:document-content
466 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
467 xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\"
468 xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\"
469 xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\"
470 xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\"
471 xmlns:fo=\"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0\"
472 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
473 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
474 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
475 xmlns:number=\"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0\"
476 xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\"
477 xmlns:chart=\"urn:oasis:names:tc:opendocument:xmlns:chart:1.0\"
478 xmlns:dr3d=\"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0\"
479 xmlns:math=\"http://www.w3.org/1998/Math/MathML\"
480 xmlns:form=\"urn:oasis:names:tc:opendocument:xmlns:form:1.0\"
481 xmlns:script=\"urn:oasis:names:tc:opendocument:xmlns:script:1.0\"
482 xmlns:ooo=\"http://openoffice.org/2004/office\"
483 xmlns:ooow=\"http://openoffice.org/2004/writer\"
484 xmlns:oooc=\"http://openoffice.org/2004/calc\"
485 xmlns:dom=\"http://www.w3.org/2001/xml-events\"
486 xmlns:xforms=\"http://www.w3.org/2002/xforms\"
487 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
488 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
489 xmlns:rpt=\"http://openoffice.org/2005/report\"
490 xmlns:of=\"urn:oasis:names:tc:opendocument:xmlns:of:1.2\"
491 xmlns:xodt=\"http://www.w3.org/1999/xodt\"
492 xmlns:field=\"urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0\" office:version=\"1.2\">
495 (defun org-odt-begin-document-content (opt-plist)
496 ;; document header
497 (insert org-odt-document-content-header)
499 ;; automatic styles
500 (insert-file-contents
501 (or org-export-odt-automatic-styles-file
502 (expand-file-name "styles/OrgOdtAutomaticStyles.xml"
503 org-odt-data-dir)))
504 (goto-char (point-max)))
506 (defun org-odt-end-document-content ()
507 (org-lparse-insert-tag "</office:document-content>"))
509 (defun org-odt-begin-outline (level1 snumber title tags
510 target extra-targets class)
511 (org-lparse-insert
512 'HEADING (org-lparse-format
513 'HEADLINE title extra-targets tags snumber level1)
514 level1 target))
516 (defun org-odt-end-outline ()
517 (ignore))
519 (defun org-odt-begin-outline-text (level1 snumber class)
520 (ignore))
522 (defun org-odt-end-outline-text ()
523 (ignore))
525 (defun org-odt-begin-paragraph (&optional style)
526 (org-lparse-insert-tag
527 "<text:p%s>" (org-odt-get-extra-attrs-for-paragraph-style style)))
529 (defun org-odt-end-paragraph ()
530 (org-lparse-insert-tag "</text:p>"))
532 (defun org-odt-get-extra-attrs-for-paragraph-style (style)
533 (let (style-name)
534 (setq style-name
535 (cond
536 ((stringp style) style)
537 ((symbolp style) (org-odt-get-style-name-for-entity
538 'paragraph style))))
539 (unless style-name
540 (error "Don't know how to handle paragraph style %s" style))
541 (format " text:style-name=\"%s\"" style-name)))
543 (defun org-odt-format-stylized-paragraph (style text)
544 (org-odt-format-tags
545 '("<text:p%s>" . "</text:p>") text
546 (org-odt-get-extra-attrs-for-paragraph-style style)))
548 (defun org-odt-begin-environment (style)
549 (case style
550 ((blockquote verse center quote)
551 (org-lparse-begin-paragraph style)
552 (list))
553 ((fixedwidth native)
554 (org-lparse-end-paragraph)
555 (list))
556 (t (error "Unknown environment %s" style))))
558 (defun org-odt-end-environment (style)
559 (case style
560 ((blockquote verse center quote)
561 (org-lparse-end-paragraph)
562 (list))
563 ((fixedwidth native)
564 (org-lparse-begin-paragraph)
565 (list))
566 (t (error "Unknown environment %s" style))))
568 (defvar org-lparse-list-level) ; dynamically bound in org-do-lparse
569 (defun org-odt-begin-list (ltype)
570 (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
571 ltype))
572 (let* ((style-name (org-odt-get-style-name-for-entity 'list ltype))
573 (extra (concat (when (= org-lparse-list-level 1)
574 " text:continue-numbering=\"false\"")
575 (when style-name
576 (format " text:style-name=\"%s\"" style-name)))))
577 (case ltype
578 ((ordered unordered description)
579 (org-lparse-end-paragraph)
580 (org-lparse-insert-tag "<text:list%s>" extra))
581 (t (error "Unknown list type: %s" ltype)))))
583 (defun org-odt-end-list (ltype)
584 (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
585 ltype))
586 (if ltype
587 (org-lparse-insert-tag "</text:list>")
588 (error "Unknown list type: %s" ltype)))
590 (defun org-odt-begin-list-item (ltype &optional arg headline)
591 (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
592 ltype))
593 (case ltype
594 (ordered
595 (assert (not headline) t)
596 (let* ((counter arg) (extra ""))
597 (org-lparse-insert-tag "<text:list-item>")
598 (org-lparse-begin-paragraph)))
599 (unordered
600 (let* ((id arg) (extra ""))
601 (org-lparse-insert-tag "<text:list-item>")
602 (org-lparse-begin-paragraph)
603 (insert (if headline (org-odt-format-target headline id)
604 (org-odt-format-bookmark "" id)))))
605 (description
606 (assert (not headline) t)
607 (let ((term (or arg "(no term)")))
608 (insert
609 (org-odt-format-tags
610 '("<text:list-item>" . "</text:list-item>")
611 (org-odt-format-stylized-paragraph 'definition-term term)))
612 (org-lparse-begin-list-item 'unordered)
613 (org-lparse-begin-list 'description)
614 (org-lparse-begin-list-item 'unordered)))
615 (t (error "Unknown list type"))))
617 (defun org-odt-end-list-item (ltype)
618 (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
619 ltype))
620 (case ltype
621 ((ordered unordered)
622 (org-lparse-insert-tag "</text:list-item>"))
623 (description
624 (org-lparse-end-list-item-1)
625 (org-lparse-end-list 'description)
626 (org-lparse-end-list-item-1))
627 (t (error "Unknown list type"))))
629 ;; Following variables are let bound when table emission is in
630 ;; progress. See org-lparse.el.
631 (defvar org-lparse-table-begin-marker)
632 (defvar org-lparse-table-ncols)
633 (defvar org-lparse-table-rowgrp-open)
634 (defvar org-lparse-table-rownum)
635 (defvar org-lparse-table-cur-rowgrp-is-hdr)
636 (defvar org-lparse-table-is-styled)
637 (defvar org-lparse-table-rowgrp-info)
638 (defvar org-lparse-table-colalign-vector)
639 (defun org-odt-begin-table (caption label attributes)
640 (when label
641 (insert
642 (org-odt-format-stylized-paragraph
643 'table (org-odt-format-entity-caption label caption "Table"))))
645 (org-lparse-insert-tag
646 "<table:table table:name=\"%s\" table:style-name=\"%s\">"
647 (or label "") "OrgTable")
648 (setq org-lparse-table-begin-marker (point)))
650 (defun org-odt-end-table ()
651 (goto-char org-lparse-table-begin-marker)
652 (loop for level from 0 below org-lparse-table-ncols
653 do (insert
654 (org-odt-format-tags
655 "<table:table-column table:style-name=\"OrgTableColumn\"/>" "")))
657 ;; fill style attributes for table cells
658 (when org-lparse-table-is-styled
659 (while (re-search-forward "@@\\(table-cell:p\\|table-cell:style-name\\)@@\\([0-9]+\\)@@\\([0-9]+\\)@@" nil t)
660 (let ((spec (match-string 1))
661 (r (string-to-number (match-string 2)))
662 (c (string-to-number (match-string 3))))
663 (cond
664 ((equal spec "table-cell:p")
665 (let ((style-name (org-odt-get-paragraph-style-for-table-cell r c)))
666 (replace-match style-name t t)))
667 ((equal spec "table-cell:style-name")
668 (let ((style-name (org-odt-get-style-name-for-table-cell r c)))
669 (replace-match style-name t t)))))))
671 (goto-char (point-max))
672 (org-lparse-insert-tag "</table:table>"))
674 (defun org-odt-begin-table-rowgroup (&optional is-header-row)
675 (when org-lparse-table-rowgrp-open
676 (org-lparse-end 'TABLE-ROWGROUP))
677 (org-lparse-insert-tag (if is-header-row
678 "<table:table-header-rows>"
679 "<table:table-rows>"))
680 (setq org-lparse-table-rowgrp-open t)
681 (setq org-lparse-table-cur-rowgrp-is-hdr is-header-row))
683 (defun org-odt-end-table-rowgroup ()
684 (when org-lparse-table-rowgrp-open
685 (setq org-lparse-table-rowgrp-open nil)
686 (org-lparse-insert-tag
687 (if org-lparse-table-cur-rowgrp-is-hdr
688 "</table:table-header-rows>" "</table:table-rows>"))))
690 (defun org-odt-format-table-row (row)
691 (org-odt-format-tags
692 '("<table:table-row>" . "</table:table-row>") row))
694 (defun org-odt-get-style-name-for-table-cell (r c)
695 (concat
696 "OrgTblCell"
697 (cond
698 ((= r 0) "T")
699 ((eq (cdr (assoc r org-lparse-table-rowgrp-info)) :start) "T")
700 (t ""))
701 (when (= r org-lparse-table-rownum) "B")
702 (cond
703 ((= c 0) "")
704 ((or (memq (nth c org-table-colgroup-info) '(:start :startend))
705 (memq (nth (1- c) org-table-colgroup-info) '(:end :startend))) "L")
706 (t ""))))
708 (defun org-odt-get-paragraph-style-for-table-cell (r c)
709 (capitalize (aref org-lparse-table-colalign-vector c)))
711 (defun org-odt-get-paragraph-style-cookie-for-table-cell (r c)
712 (concat
713 (cond
714 (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
715 ((and (= c 0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS))
716 "OrgTableHeading")
717 (t "OrgTableContents"))
718 (and org-lparse-table-is-styled
719 (format "@@table-cell:p@@%03d@@%03d@@" r c))))
721 (defun org-odt-get-style-name-cookie-for-table-cell (r c)
722 (when org-lparse-table-is-styled
723 (format "@@table-cell:style-name@@%03d@@%03d@@" r c)))
725 (defun org-odt-format-table-cell (data r c)
726 (let* ((paragraph-style-cookie
727 (org-odt-get-paragraph-style-cookie-for-table-cell r c))
728 (style-name-cookie
729 (org-odt-get-style-name-cookie-for-table-cell r c))
730 (extra (if style-name-cookie
731 (format " table:style-name=\"%s\"" style-name-cookie) "")))
732 (org-odt-format-tags
733 '("<table:table-cell%s>" . "</table:table-cell>")
734 (if org-lparse-list-table-p data
735 (org-odt-format-stylized-paragraph paragraph-style-cookie data)) extra)))
737 (defun org-odt-begin-footnote-definition (n)
738 (org-lparse-begin-paragraph 'footnote))
740 (defun org-odt-end-footnote-definition (n)
741 (org-lparse-end-paragraph))
743 (defun org-odt-begin-toc (lang-specific-heading)
744 (insert
745 (format "
746 <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
747 <text:table-of-content-source text:outline-level=\"10\">
748 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
749 " lang-specific-heading))
751 (loop for level from 1 upto 10
752 do (insert (format
754 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
755 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
756 <text:index-entry-chapter/>
757 <text:index-entry-text/>
758 <text:index-entry-link-end/>
759 </text:table-of-content-entry-template>
760 " level level)))
762 (insert
763 (format "
764 </text:table-of-content-source>
766 <text:index-body>
767 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
768 <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
769 </text:index-title>
770 " lang-specific-heading)))
772 (defun org-odt-end-toc ()
773 (insert "
774 </text:index-body>
775 </text:table-of-content>
778 (defun org-odt-format-toc-entry (snumber todo headline tags href)
779 (setq headline (concat
780 (and org-export-with-section-numbers
781 (concat snumber ". "))
782 headline
783 (and tags
784 (concat
785 (org-lparse-format 'SPACES 3)
786 (org-lparse-format 'FONTIFY tags "tag")))))
787 (when todo
788 (setq headline (org-lparse-format 'FONTIFY headline "todo")))
790 (let ((org-odt-suppress-xref t))
791 (org-odt-format-link headline (concat "#" href))))
793 (defun org-odt-format-toc-item (toc-entry level org-last-level)
794 (let ((style (format "Contents_20_%d"
795 (+ level (or (org-lparse-get 'TOPLEVEL-HLEVEL) 1) -1))))
796 (insert "\n" (org-odt-format-stylized-paragraph style toc-entry) "\n")))
798 ;; Following variable is let bound during 'ORG-LINK callback. See
799 ;; org-html.el
800 (defvar org-lparse-link-description-is-image nil)
801 (defun org-odt-format-link (desc href &optional attr)
802 (cond
803 ((and (= (string-to-char href) ?#) (not org-odt-suppress-xref))
804 (setq href (concat org-export-odt-bookmark-prefix (substring href 1)))
805 (org-odt-format-tags
806 '("<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"%s\">" .
807 "</text:bookmark-ref>")
808 desc href))
809 (org-lparse-link-description-is-image
810 (org-odt-format-tags
811 '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</draw:a>")
812 desc href (or attr "")))
814 (org-odt-format-tags
815 '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</text:a>")
816 desc href (or attr "")))))
818 (defun org-odt-format-spaces (n)
819 (cond
820 ((= n 1) " ")
821 ((> n 1) (concat
822 " " (org-odt-format-tags "<text:s text:c=\"%d\"/>" "" (1- n))))
823 (t "")))
825 (defun org-odt-format-tabs (&optional n)
826 (let ((tab "<text:tab/>")
827 (n (or n 1)))
828 (insert tab)))
830 (defun org-odt-format-line-break ()
831 (org-odt-format-tags "<text:line-break/>" ""))
833 (defun org-odt-format-horizontal-line ()
834 (org-odt-format-stylized-paragraph 'horizontal-line ""))
836 (defun org-odt-format-line (line)
837 (case org-lparse-dyn-current-environment
838 (fixedwidth (concat
839 (org-odt-format-stylized-paragraph
840 'fixedwidth (org-odt-fill-tabs-and-spaces
841 (org-xml-encode-plain-text line))) "\n"))
842 (t (concat line "\n"))))
844 (defun org-odt-format-comment (fmt &rest args)
845 (let ((comment (apply 'format fmt args)))
846 (format "\n<!-- %s -->\n" comment)))
848 (defun org-odt-format-org-entity (wd)
849 (org-entity-get-representation wd 'utf8))
851 (defun org-odt-fill-tabs-and-spaces (line)
852 (replace-regexp-in-string
853 "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
854 (cond
855 ((string= s "\t") (org-odt-format-tabs))
856 (t (org-odt-format-spaces (length s))))) line))
858 (defcustom org-export-odt-use-htmlfontify t
859 "Specify whether or not source blocks need to be fontified.
860 Turn this option on if you want to colorize the source code
861 blocks in the exported file. For colorization to work, you need
862 to make available an enhanced version of `htmlfontify' library."
863 :type 'boolean
864 :group 'org-export-odt)
866 (defun org-odt-format-source-code-or-example-plain
867 (lines lang caption textareap cols rows num cont rpllbl fmt)
868 "Format source or example blocks much like fixedwidth blocks.
869 Use this when `org-export-odt-use-htmlfontify' option is turned
870 off."
871 (setq lines (org-export-number-lines (org-xml-encode-plain-text-lines lines)
872 0 0 num cont rpllbl fmt))
873 (mapconcat
874 (lambda (line)
875 (org-odt-format-stylized-paragraph
876 'fixedwidth (org-odt-fill-tabs-and-spaces line)))
877 (org-split-string lines "[\r\n]") "\n"))
879 (defvar org-src-block-paragraph-format
880 "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
881 <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
882 <style:background-image/>
883 </style:paragraph-properties>
884 <style:text-properties fo:color=\"%s\"/>
885 </style:style>"
886 "Custom paragraph style for colorized source and example blocks.
887 This style is much the same as that of \"OrgFixedWidthBlock\"
888 except that the foreground and background colors are set
889 according to the default face identified by the `htmlfontify'.")
891 (defun org-odt-hfy-face-to-css (fn)
892 "Create custom style for face FN.
893 When FN is the default face, use it's foreground and background
894 properties to create \"OrgSrcBlock\" paragraph style. Otherwise
895 use it's color attribute to create a character style whose name
896 is obtained from FN. Currently all attributes of FN other than
897 color are ignored.
899 The style name for a face FN is derived using the following
900 operations on the face name in that order - de-dash, CamelCase
901 and prefix with \"OrgSrc\". For example,
902 `font-lock-function-name-face' is associated with
903 \"OrgSrcFontLockFunctionNameFace\"."
904 (let* ((css-list (hfy-face-to-style fn))
905 (style-name ((lambda (fn)
906 (concat "OrgSrc"
907 (mapconcat
908 'capitalize (split-string
909 (hfy-face-or-def-to-name fn) "-")
910 ""))) fn))
911 (color-val (cdr (assoc "color" css-list)))
912 (background-color-val (cdr (assoc "background" css-list)))
913 (style (and org-export-odt-create-custom-styles-for-srcblocks
914 (cond
915 ((eq fn 'default)
916 (format org-src-block-paragraph-format
917 background-color-val color-val))
919 (format
921 <style:style style:name=\"%s\" style:family=\"text\">
922 <style:text-properties fo:color=\"%s\"/>
923 </style:style>" style-name color-val))))))
924 (cons style-name style)))
926 (defcustom org-export-odt-create-custom-styles-for-srcblocks t
927 "Whether custom styles for colorized source blocks be automatically created.
928 When this option is turned on, the exporter creates custom styles
929 for source blocks based on the advice of `htmlfontify'. Creation
930 of custom styles happen as part of `org-odt-hfy-face-to-css'.
932 When this option is turned off exporter does not create such
933 styles.
935 Use the latter option if you do not want the custom styles to be
936 based on your current display settings. It is necessary that the
937 styles.xml already contains needed styles for colorizing to work.
939 This variable is effective only if
940 `org-export-odt-use-htmlfontify' is turned on."
941 :group 'org-export-odt
942 :type 'boolean)
944 (defun org-odt-insert-custom-styles-for-srcblocks (styles)
945 "Save STYLES used for colorizing of source blocks.
946 Update styles.xml with styles that were collected as part of
947 `org-odt-hfy-face-to-css' callbacks."
948 (when styles
949 (with-current-buffer
950 (find-file-noselect (expand-file-name "styles.xml") t)
951 (goto-char (point-min))
952 (when (re-search-forward "</office:styles>" nil t)
953 (goto-char (match-beginning 0))
954 (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n")))))
956 (defun org-odt-format-source-code-or-example-colored
957 (lines lang caption textareap cols rows num cont rpllbl fmt)
958 "Format source or example blocks using `htmlfontify-string'.
959 Use this routine when `org-export-odt-use-htmlfontify' option is
960 turned on."
961 (let* ((lang-m (and lang (or (cdr (assoc lang org-src-lang-modes)) lang)))
962 (mode (and lang-m (intern (concat (if (symbolp lang-m)
963 (symbol-name lang-m)
964 lang-m) "-mode"))))
965 (org-inhibit-startup t)
966 (org-startup-folded nil)
967 (lines (with-temp-buffer
968 (insert lines)
969 (if (functionp mode) (funcall mode) (fundamental-mode))
970 (font-lock-fontify-buffer)
971 (buffer-string)))
972 (hfy-html-quote-regex "\\([<\"&> ]\\)")
973 (hfy-html-quote-map '(("\"" "&quot;")
974 ("<" "&lt;")
975 ("&" "&amp;")
976 (">" "&gt;")
977 (" " "<text:s/>")
978 (" " "<text:tab/>")))
979 (hfy-face-to-css 'org-odt-hfy-face-to-css)
980 (hfy-optimisations-1 (copy-seq hfy-optimisations))
981 (hfy-optimisations (add-to-list 'hfy-optimisations-1
982 'body-text-only))
983 (hfy-begin-span-handler
984 (lambda (style text-block text-id text-begins-block-p)
985 (insert (format "<text:span text:style-name=\"%s\">" style))))
986 (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
987 (when (fboundp 'htmlfontify-string)
988 (mapconcat
989 (lambda (line)
990 (org-odt-format-stylized-paragraph 'src (htmlfontify-string line)))
991 (org-split-string lines "[\r\n]") "\n"))))
993 (defun org-odt-format-source-code-or-example (lines lang caption textareap
994 cols rows num cont
995 rpllbl fmt)
996 "Format source or example blocks for export.
997 Use `org-odt-format-source-code-or-example-plain' or
998 `org-odt-format-source-code-or-example-colored' depending on the
999 value of `org-export-odt-use-htmlfontify."
1000 (funcall
1001 (if (and org-export-odt-use-htmlfontify
1002 (or (featurep 'htmlfontify) (require 'htmlfontify))
1003 (fboundp 'htmlfontify-string))
1004 'org-odt-format-source-code-or-example-colored
1005 'org-odt-format-source-code-or-example-plain)
1006 lines lang caption textareap cols rows num cont rpllbl fmt))
1008 (defun org-xml-encode-plain-text-lines (rtn)
1009 (mapconcat 'org-xml-encode-plain-text (org-split-string rtn "[\r\n]") "\n"))
1011 (defun org-odt-remap-stylenames (style-name)
1013 (cdr (assoc style-name '(("timestamp-wrapper" . "OrgTimestampWrapper")
1014 ("timestamp" . "OrgTimestamp")
1015 ("timestamp-kwd" . "OrgTimestampKeyword")
1016 ("tag" . "OrgTag")
1017 ("todo" . "OrgTodo")
1018 ("done" . "OrgDone")
1019 ("target" . "OrgTarget"))))
1020 style-name))
1022 (defun org-odt-format-fontify (text style &optional id)
1023 (let* ((style-name
1024 (cond
1025 ((stringp style)
1026 (org-odt-remap-stylenames style))
1027 ((symbolp style)
1028 (org-odt-get-style-name-for-entity 'character style))
1029 ((listp style)
1030 (assert (< 1 (length style)))
1031 (let ((parent-style (pop style)))
1032 (mapconcat (lambda (s)
1033 ;; (assert (stringp s) t)
1034 (org-odt-remap-stylenames s)) style "")
1035 (org-odt-remap-stylenames parent-style)))
1036 (t (error "Don't how to handle style %s" style)))))
1037 (org-odt-format-tags
1038 '("<text:span text:style-name=\"%s\">" . "</text:span>")
1039 text style-name)))
1041 (defun org-odt-relocate-relative-path (path dir)
1042 (if (file-name-absolute-p path) path
1043 (file-relative-name (expand-file-name path dir)
1044 (expand-file-name "eyecandy" dir))))
1046 (defun org-odt-format-inline-image (thefile)
1047 (let* ((thelink (if (file-name-absolute-p thefile) thefile
1048 (org-xml-format-href
1049 (org-odt-relocate-relative-path
1050 thefile org-current-export-file))))
1051 (href
1052 (org-odt-format-tags
1053 "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
1054 (if org-export-odt-embed-images
1055 (org-odt-copy-image-file thefile) thelink))))
1056 (org-export-odt-format-image thefile href)))
1058 (defun org-export-odt-do-format-numbered-formula (embed-as caption attr label
1059 width height href)
1060 (with-temp-buffer
1061 (let ((org-lparse-table-colalign-info '((0 "c" "8") (0 "c" "1"))))
1062 (org-lparse-do-format-list-table
1063 `((,(org-export-odt-do-format-formula ; caption and label
1064 ; should be nil
1065 embed-as nil attr nil width height href)
1066 ,(org-odt-format-entity-caption label caption "Equation")))
1067 nil nil nil nil nil org-lparse-table-colalign-info))
1068 (buffer-substring-no-properties (point-min) (point-max))))
1070 (defun org-export-odt-do-format-formula (embed-as caption attr label
1071 width height href)
1072 "Create image tag with source and attributes."
1073 (save-match-data
1074 (cond
1075 ((and (not caption) (not label))
1076 (let (style-name anchor-type)
1077 (case embed-as
1078 (paragraph
1079 (setq style-name "OrgSimpleGraphics" anchor-type "paragraph"))
1080 (character
1081 (setq style-name "OrgInlineGraphics" anchor-type "as-char"))
1083 (error "Unknown value for embed-as %S" embed-as)))
1084 (org-odt-format-frame href style-name width height nil anchor-type)))
1086 (concat
1087 (org-odt-format-textbox
1088 (org-odt-format-stylized-paragraph
1089 'illustration
1090 (concat
1091 (let ((extra ""))
1092 (org-odt-format-frame
1093 href "" width height extra "paragraph"))
1094 (org-odt-format-entity-caption label caption)))
1095 "OrgCaptionFrame" width height))))))
1097 (defun org-export-odt-format-formula (src href &optional embed-as)
1098 "Create image tag with source and attributes."
1099 (save-match-data
1100 (let* ((caption (org-find-text-property-in-string 'org-caption src))
1101 (caption (and caption (org-xml-format-desc caption)))
1102 (attr (org-find-text-property-in-string 'org-attributes src))
1103 (label (org-find-text-property-in-string 'org-label src))
1104 (embed-as (or embed-as
1105 (and (org-find-text-property-in-string
1106 'org-latex-src src)
1107 (org-find-text-property-in-string
1108 'org-latex-src-embed-type src))
1109 'paragraph))
1110 (attr-plist (when attr (read attr)))
1111 (width (plist-get attr-plist :width))
1112 (height (plist-get attr-plist :height)))
1113 (org-export-odt-do-format-formula
1114 embed-as caption attr label width height href))))
1116 (defvar org-odt-embedded-formulas-count 0)
1117 (defun org-odt-copy-formula-file (path)
1118 "Returns the internal name of the file"
1119 (let* ((src-file (expand-file-name
1120 path (file-name-directory org-current-export-file)))
1121 (target-dir (format "Formula-%04d/"
1122 (incf org-odt-embedded-formulas-count)))
1123 (target-file (concat target-dir "content.xml")))
1124 (when (not org-lparse-to-buffer)
1125 (message "Embedding %s as %s ..."
1126 (substring-no-properties path) target-file)
1128 (make-directory target-dir)
1129 (org-odt-create-manifest-file-entry
1130 "application/vnd.oasis.opendocument.formula" target-dir "1.2")
1132 (copy-file src-file target-file 'overwrite)
1133 (org-odt-create-manifest-file-entry "text/xml" target-file))
1134 target-file))
1136 (defun org-odt-format-inline-formula (thefile)
1137 (let* ((thelink (if (file-name-absolute-p thefile) thefile
1138 (org-xml-format-href
1139 (org-odt-relocate-relative-path
1140 thefile org-current-export-file))))
1141 (href
1142 (org-odt-format-tags
1143 "<draw:object xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
1144 (file-name-directory (org-odt-copy-formula-file thefile)))))
1145 (org-export-odt-format-formula thefile href)))
1147 (defun org-odt-is-formula-link-p (file)
1148 (member (downcase (file-name-extension file)) '("mathml")))
1150 (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
1151 descp)
1152 "Make an HTML link.
1153 OPT-PLIST is an options list.
1154 TYPE is the device-type of the link (THIS://foo.html)
1155 PATH is the path of the link (http://THIS#locationx)
1156 FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
1157 DESC is the link description, if any.
1158 ATTR is a string of other attributes of the a element.
1159 MAY-INLINE-P allows inlining it as an image."
1160 (declare (special org-lparse-par-open))
1161 (save-match-data
1162 (let* ((may-inline-p
1163 (and (member type-1 '("http" "https" "file"))
1164 (org-lparse-should-inline-p path descp)
1165 (not fragment)))
1166 (type (if (equal type-1 "id") "file" type-1))
1167 (filename path)
1168 (thefile path))
1169 (cond
1170 ;; check for inlined images
1171 ((and (member type '("file"))
1172 (not fragment)
1173 (org-file-image-p
1174 filename org-odt-export-inline-image-extensions)
1175 (or (eq t org-odt-export-inline-images)
1176 (and org-odt-export-inline-images (not descp))))
1177 (org-odt-format-inline-image thefile))
1178 ;; check for embedded formulas
1179 ((and (member type '("file"))
1180 (not fragment)
1181 (org-odt-is-formula-link-p filename)
1182 (or (not descp)))
1183 (org-odt-format-inline-formula thefile))
1185 (when (string= type "file")
1186 (setq thefile
1187 (cond
1188 ((file-name-absolute-p path)
1189 (concat "file://" (expand-file-name path)))
1190 (t (org-odt-relocate-relative-path
1191 thefile org-current-export-file)))))
1193 (when (and (member type '("" "http" "https" "file" "coderef"))
1194 fragment)
1195 (setq thefile (concat thefile "#" fragment)))
1197 (setq thefile (org-xml-format-href thefile))
1199 (when (not (member type '("" "file" "coderef")))
1200 (setq thefile (concat type ":" thefile)))
1202 (let ((org-odt-suppress-xref (string= type "coderef")))
1203 (org-odt-format-link
1204 (org-xml-format-desc desc) thefile attr)))))))
1206 (defun org-odt-format-heading (text level &optional id)
1207 (let* ((text (if id (org-odt-format-target text id) text)))
1208 (org-odt-format-tags
1209 '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
1210 "</text:h>") text level level)))
1212 (defun org-odt-format-headline (title extra-targets tags
1213 &optional snumber level)
1214 (concat
1215 (org-lparse-format 'EXTRA-TARGETS extra-targets)
1217 ;; No need to generate section numbers. They are auto-generated by
1218 ;; the application
1220 ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
1221 title
1222 (and tags (concat (org-lparse-format 'SPACES 3)
1223 (org-lparse-format 'ORG-TAGS tags)))))
1225 (defun org-odt-format-anchor (text name &optional class)
1226 (org-odt-format-target text name))
1228 (defun org-odt-format-bookmark (text id)
1229 (if id
1230 (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id)
1231 text))
1233 (defun org-odt-format-target (text id)
1234 (let ((name (concat org-export-odt-bookmark-prefix id)))
1235 (concat
1236 (and id (org-odt-format-tags
1237 "<text:bookmark-start text:name=\"%s\"/>" "" name))
1238 (org-odt-format-bookmark text id)
1239 (and id (org-odt-format-tags
1240 "<text:bookmark-end text:name=\"%s\"/>" "" name)))))
1242 (defun org-odt-format-footnote (n def)
1243 (let ((id (concat "fn" n))
1244 (note-class "footnote")
1245 (par-style "Footnote"))
1246 (org-odt-format-tags
1247 '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
1248 "</text:note>")
1249 (concat
1250 (org-odt-format-tags
1251 '("<text:note-citation>" . "</text:note-citation>")
1253 (org-odt-format-tags
1254 '("<text:note-body>" . "</text:note-body>")
1255 def))
1256 id note-class)))
1258 (defun org-odt-format-footnote-reference (n def refcnt)
1259 (if (= refcnt 1)
1260 (org-odt-format-footnote n def)
1261 (org-odt-format-footnote-ref n)))
1263 (defun org-odt-format-footnote-ref (n)
1264 (let ((note-class "footnote")
1265 (ref-format "text")
1266 (ref-name (concat "fn" n)))
1267 (org-odt-format-tags
1268 '("<text:span text:style-name=\"%s\">" . "</text:span>")
1269 (org-odt-format-tags
1270 '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" . "</text:note-ref>")
1271 n note-class ref-format ref-name)
1272 "OrgSuperscript")))
1274 (defun org-odt-get-image-name (file-name)
1275 (require 'sha1)
1276 (file-relative-name
1277 (expand-file-name
1278 (concat (sha1 file-name) "." (file-name-extension file-name)) "Pictures")))
1280 (defun org-export-odt-format-image (src href &optional embed-as)
1281 "Create image tag with source and attributes."
1282 (save-match-data
1283 (let* ((caption (org-find-text-property-in-string 'org-caption src))
1284 (caption (and caption (org-xml-format-desc caption)))
1285 (attr (org-find-text-property-in-string 'org-attributes src))
1286 (label (org-find-text-property-in-string 'org-label src))
1287 (embed-as (or embed-as
1288 (if (org-find-text-property-in-string
1289 'org-latex-src src)
1290 (or (org-find-text-property-in-string
1291 'org-latex-src-embed-type src) 'character)
1292 'paragraph)))
1293 (attr-plist (when attr (read attr)))
1294 (size (org-odt-image-size-from-file
1295 src (plist-get attr-plist :width)
1296 (plist-get attr-plist :height)
1297 (plist-get attr-plist :scale) nil embed-as)))
1298 (org-export-odt-do-format-image
1299 embed-as caption attr label (car size) (cdr size) href))))
1301 (defun org-odt-format-frame (text style &optional
1302 width height extra anchor-type)
1303 (let ((frame-attrs
1304 (concat
1305 (if width (format " svg:width=\"%0.2fcm\"" width) "")
1306 (if height (format " svg:height=\"%0.2fcm\"" height) "")
1307 extra
1308 (format " text:anchor-type=\"%s\"" (or anchor-type "paragraph")))))
1309 (org-odt-format-tags
1310 '("<draw:frame draw:style-name=\"%s\"%s>" . "</draw:frame>")
1311 text style frame-attrs)))
1313 (defun org-odt-format-textbox (text style &optional width height extra)
1314 (org-odt-format-frame
1315 (org-odt-format-tags
1316 '("<draw:text-box %s>" . "</draw:text-box>")
1317 text (concat (format " fo:min-height=\"%0.2fcm\"" (or height .2))
1318 (format " fo:min-width=\"%0.2fcm\"" (or width .2))))
1319 style width nil extra))
1321 (defun org-odt-format-inlinetask (heading content
1322 &optional todo priority tags)
1323 (org-odt-format-stylized-paragraph
1324 nil (org-odt-format-textbox
1325 (concat (org-odt-format-stylized-paragraph
1326 "OrgInlineTaskHeading"
1327 (org-lparse-format
1328 'HEADLINE (concat (org-lparse-format-todo todo) " " heading)
1329 nil tags))
1330 content) "OrgInlineTaskFrame" nil nil " style:rel-width=\"100%\"")))
1331 (defun org-export-odt-do-format-image (embed-as caption attr label
1332 width height href)
1333 "Create image tag with source and attributes."
1334 (save-match-data
1335 (cond
1336 ((and (not caption) (not label))
1337 (let (style-name anchor-type)
1338 (case embed-as
1339 (paragraph
1340 (setq style-name "OrgSimpleGraphics" anchor-type "paragraph"))
1341 (character
1342 (setq style-name "OrgInlineGraphics" anchor-type "as-char"))
1344 (error "Unknown value for embed-as %S" embed-as)))
1345 (org-odt-format-frame href style-name width height nil anchor-type)))
1347 (concat
1348 (org-odt-format-textbox
1349 (org-odt-format-stylized-paragraph
1350 'illustration
1351 (concat
1352 (let ((extra " style:rel-width=\"100%\" style:rel-height=\"scale\""))
1353 (org-odt-format-frame
1354 href "OrgCaptionedGraphics" width height extra "paragraph"))
1355 (org-odt-format-entity-caption label caption)))
1356 "OrgCaptionFrame" width height))))))
1358 (defvar org-odt-embedded-images-count 0)
1359 (defun org-odt-copy-image-file (path)
1360 "Returns the internal name of the file"
1361 (let* ((image-type (file-name-extension path))
1362 (media-type (format "image/%s" image-type))
1363 (src-file (expand-file-name
1364 path (file-name-directory org-current-export-file)))
1365 (target-dir "Images/")
1366 (target-file
1367 (format "%s%04d.%s" target-dir
1368 (incf org-odt-embedded-images-count) image-type)))
1369 (when (not org-lparse-to-buffer)
1370 (message "Embedding %s as %s ..."
1371 (substring-no-properties path) target-file)
1373 (when (= 1 org-odt-embedded-images-count)
1374 (make-directory target-dir)
1375 (org-odt-create-manifest-file-entry "" target-dir))
1377 (copy-file src-file target-file 'overwrite)
1378 (org-odt-create-manifest-file-entry media-type target-file))
1379 target-file))
1381 (defvar org-export-odt-image-size-probe-method
1382 '(emacs imagemagick force)
1383 "Ordered list of methods by for determining size of an embedded
1384 image.")
1386 (defvar org-export-odt-default-image-sizes-alist
1387 '(("character" . (5 . 0.4))
1388 ("paragraph" . (5 . 5)))
1389 "Hardcoded image dimensions one for each of the anchor
1390 methods.")
1392 (defun org-odt-do-image-size (probe-method file &optional dpi anchor-type)
1393 (setq dpi (or dpi org-export-odt-pixels-per-inch))
1394 (setq anchor-type (or anchor-type "paragraph"))
1395 (flet ((size-in-cms (size-in-pixels)
1396 (flet ((pixels-to-cms (pixels)
1397 (let* ((cms-per-inch 2.54)
1398 (inches (/ pixels dpi)))
1399 (* cms-per-inch inches))))
1400 (and size-in-pixels
1401 (cons (pixels-to-cms (car size-in-pixels))
1402 (pixels-to-cms (cdr size-in-pixels)))))))
1403 (case probe-method
1404 (emacs
1405 (size-in-cms (ignore-errors (image-size (create-image file) 'pixels))))
1406 (imagemagick
1407 (size-in-cms
1408 (let ((dim (shell-command-to-string
1409 (format "identify -format \"%%w:%%h\" \"%s\"" file))))
1410 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
1411 (cons (string-to-number (match-string 1 dim))
1412 (string-to-number (match-string 2 dim)))))))
1414 (cdr (assoc-string anchor-type
1415 org-export-odt-default-image-sizes-alist))))))
1417 (defun org-odt-image-size-from-file (file &optional user-width
1418 user-height scale dpi embed-as)
1419 (unless (file-name-absolute-p file)
1420 (setq file (expand-file-name
1421 file (file-name-directory org-current-export-file))))
1422 (let* (size width height)
1423 (unless (and user-height user-width)
1424 (loop for probe-method in org-export-odt-image-size-probe-method
1425 until size
1426 do (setq size (org-odt-do-image-size
1427 probe-method file dpi embed-as)))
1428 (or size (error "Cannot determine Image size. Aborting ..."))
1429 (setq width (car size) height (cdr size)))
1430 (cond
1431 (scale
1432 (setq width (* width scale) height (* height scale)))
1433 ((and user-height user-width)
1434 (setq width user-width height user-height))
1435 (user-height
1436 (setq width (* user-height (/ width height)) height user-height))
1437 (user-width
1438 (setq height (* user-width (/ height width)) width user-width))
1439 (t (ignore)))
1440 (cons width height)))
1442 (defvar org-odt-default-entity "Illustration")
1443 (defun org-odt-format-entity-caption (label caption &optional default-entity)
1444 (if (not label) (or caption "")
1445 (let* ((label-components (org-odt-parse-label label))
1446 (entity (car label-components))
1447 (seqno (cdr label-components))
1448 (caption (and caption (concat ": " caption))))
1449 (unless seqno
1450 (setq seqno label
1451 entity (or default-entity org-odt-default-entity)))
1452 (concat
1453 entity " "
1454 (org-odt-format-tags
1455 '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" . "</text:sequence>")
1456 seqno label entity entity)
1457 caption))))
1459 (defun org-odt-format-tags (tag text &rest args)
1460 (let ((prefix (when org-lparse-encode-pending "@"))
1461 (suffix (when org-lparse-encode-pending "@")))
1462 (apply 'org-lparse-format-tags tag text prefix suffix args)))
1464 (defun org-odt-init-outfile (filename)
1465 (unless (executable-find "zip")
1466 ;; Not at all OSes ship with zip by default
1467 (error "Executable \"zip\" needed for creating OpenDocument files"))
1469 (let* ((outdir (make-temp-file org-export-odt-tmpdir-prefix t))
1470 (content-file (expand-file-name "content.xml" outdir)))
1472 ;; init conten.xml
1473 (with-current-buffer (find-file-noselect content-file t))
1475 ;; reset variables
1476 (setq org-odt-manifest-file-entries nil
1477 org-odt-embedded-images-count 0
1478 org-odt-embedded-formulas-count 0)
1480 content-file))
1482 (defcustom org-export-odt-prettify-xml nil
1483 "Specify whether or not the xml output should be prettified.
1484 When this option is turned on, `indent-region' is run on all
1485 component xml buffers before they are saved. Turn this off for
1486 regular use. Turn this on if you need to examine the xml
1487 visually."
1488 :group 'org-export-odt
1489 :type 'boolean)
1491 (defvar hfy-user-sheet-assoc) ; bound during org-do-lparse
1492 (defun org-odt-save-as-outfile (target opt-plist)
1493 ;; create mimetype file
1494 (write-region "application/vnd.oasis.opendocument.text" nil
1495 (expand-file-name "mimetype"))
1497 ;; write meta file
1498 (org-odt-update-meta-file opt-plist)
1500 ;; write styles file
1501 (org-odt-copy-styles-file)
1503 ;; Update styles.xml - take care of outline numbering
1504 (with-current-buffer
1505 (find-file-noselect (expand-file-name "styles.xml") t)
1506 ;; Don't make automatic backup of styles.xml file. This setting
1507 ;; prevents the backedup styles.xml file from being zipped in to
1508 ;; odt file. This is more of a hackish fix. Better alternative
1509 ;; would be to fix the zip command so that the output odt file
1510 ;; includes only the needed files and excludes any auto-generated
1511 ;; extra files like backups and auto-saves etc etc. Note that
1512 ;; currently the zip command zips up the entire temp directory so
1513 ;; that any auto-generated files created under the hood ends up in
1514 ;; the resulting odt file.
1515 (set (make-local-variable 'backup-inhibited) t)
1517 ;; Import local setting of `org-export-with-section-numbers'
1518 (org-lparse-bind-local-variables opt-plist)
1519 (org-odt-configure-outline-numbering
1520 (if org-export-with-section-numbers org-export-headline-levels 0)))
1522 ;; Write custom stlyes for source blocks
1523 (org-odt-insert-custom-styles-for-srcblocks
1524 (mapconcat
1525 (lambda (style)
1526 (format " %s\n" (cddr style)))
1527 hfy-user-sheet-assoc ""))
1529 ;; create a manifest entry for content.xml
1530 (org-odt-create-manifest-file-entry
1531 "application/vnd.oasis.opendocument.text" "/" "1.2")
1533 (org-odt-create-manifest-file-entry "text/xml" "content.xml")
1535 ;; write out the manifest entries before zipping
1536 (org-odt-write-manifest-file)
1538 (let ((xml-files '("mimetype" "META-INF/manifest.xml" "content.xml"
1539 "meta.xml" "styles.xml"))
1540 (zipdir default-directory))
1541 (message "Switching to directory %s" (expand-file-name zipdir))
1543 ;; save all xml files
1544 (mapc (lambda (file)
1545 (with-current-buffer
1546 (find-file-noselect (expand-file-name file) t)
1547 ;; prettify output if needed
1548 (when org-export-odt-prettify-xml
1549 (indent-region (point-min) (point-max)))
1550 (save-buffer 0)))
1551 xml-files)
1553 (let* ((target-name (file-name-nondirectory target))
1554 (target-dir (file-name-directory target))
1555 (cmds `(("zip" "-mX0" ,target-name "mimetype")
1556 ("zip" "-rmTq" ,target-name "."))))
1557 (when (file-exists-p target)
1558 ;; FIXME: If the file is locked this throws a cryptic error
1559 (delete-file target))
1561 (let ((coding-system-for-write 'no-conversion) exitcode)
1562 (message "Creating odt file...")
1563 (mapc
1564 (lambda (cmd)
1565 (message "Running %s" (mapconcat 'identity cmd " "))
1566 (setq exitcode
1567 (apply 'call-process (car cmd) nil nil nil (cdr cmd)))
1568 (or (zerop exitcode)
1569 (error "Unable to create odt file (%S)" exitcode)))
1570 cmds))
1572 ;; move the file from outdir to target-dir
1573 (rename-file target-name target-dir)
1575 ;; kill all xml buffers
1576 (mapc (lambda (file)
1577 (kill-buffer
1578 (find-file-noselect (expand-file-name file zipdir) t)))
1579 xml-files)
1581 (delete-directory zipdir)))
1583 (message "Created %s" target)
1584 (set-buffer (find-file-noselect target t)))
1586 (defun org-odt-format-date (date)
1587 (let ((warning-msg
1588 "OpenDocument files require that dates be in ISO-8601 format. Please review your DATE options for compatibility."))
1589 ;; If the user is not careful with the date specification, an
1590 ;; invalid meta.xml will be emitted.
1592 ;; For now honor user's diktat and let him off with a warning
1593 ;; message. This is OK as LibreOffice (and possibly other
1594 ;; apps) doesn't deem this deviation as critical and continue
1595 ;; to load the file.
1597 ;; FIXME: Surely there a better way to handle this. Revisit this
1598 ;; later.
1599 (cond
1600 ((and date (string-match "%" date))
1601 ;; Honor user's diktat. See comments above
1602 (org-lparse-warn warning-msg)
1603 (format-time-string date))
1604 (date
1605 ;; Honor user's diktat. See comments above
1606 (org-lparse-warn warning-msg)
1607 date)
1609 ;; ISO 8601 format
1610 (let ((stamp (format-time-string "%Y-%m-%dT%H:%M:%S%z")))
1611 (format "%s:%s" (substring stamp 0 -2) (substring stamp -2)))))))
1613 (defconst org-odt-manifest-file-entry-tag
1615 <manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
1617 (defvar org-odt-manifest-file-entries nil)
1619 (defun org-odt-create-manifest-file-entry (&rest args)
1620 (push args org-odt-manifest-file-entries))
1622 (defun org-odt-write-manifest-file ()
1623 (make-directory "META-INF")
1624 (let ((manifest-file (expand-file-name "META-INF/manifest.xml")))
1625 (write-region
1626 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
1627 <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n"
1628 nil manifest-file)
1629 (mapc
1630 (lambda (file-entry)
1631 (let* ((version (nth 2 file-entry))
1632 (extra (if version
1633 (format " manifest:version=\"%s\"" version)
1634 "")))
1635 (write-region
1636 (format org-odt-manifest-file-entry-tag
1637 (nth 0 file-entry) (nth 1 file-entry) extra)
1638 nil manifest-file t))) org-odt-manifest-file-entries)
1639 (write-region "\n</manifest:manifest>" nil manifest-file t)))
1641 (defun org-odt-update-meta-file (opt-plist)
1642 (let ((date (org-odt-format-date (plist-get opt-plist :date)))
1643 (author (or (plist-get opt-plist :author) ""))
1644 (email (plist-get opt-plist :email))
1645 (keywords (plist-get opt-plist :keywords))
1646 (description (plist-get opt-plist :description))
1647 (title (plist-get opt-plist :title)))
1649 (write-region
1650 (concat
1651 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
1652 <office:document-meta
1653 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
1654 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
1655 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
1656 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
1657 xmlns:ooo=\"http://openoffice.org/2004/office\"
1658 office:version=\"1.2\">
1659 <office:meta>" "\n"
1660 (org-odt-format-tags '("<dc:creator>" . "</dc:creator>") author)
1661 (org-odt-format-tags
1662 '("\n<meta:initial-creator>" . "</meta:initial-creator>") author)
1663 (org-odt-format-tags '("\n<dc:date>" . "</dc:date>") date)
1664 (org-odt-format-tags
1665 '("\n<meta:creation-date>" . "</meta:creation-date>") date)
1666 (org-odt-format-tags '("\n<meta:generator>" . "</meta:generator>")
1667 (when org-export-creator-info
1668 (format "Org-%s/Emacs-%s"
1669 org-version emacs-version)))
1670 (org-odt-format-tags '("\n<meta:keyword>" . "</meta:keyword>") keywords)
1671 (org-odt-format-tags '("\n<dc:subject>" . "</dc:subject>") description)
1672 (org-odt-format-tags '("\n<dc:title>" . "</dc:title>") title)
1673 "\n"
1674 " </office:meta>" "</office:document-meta>")
1675 nil (expand-file-name "meta.xml")))
1677 ;; create a manifest entry for meta.xml
1678 (org-odt-create-manifest-file-entry "text/xml" "meta.xml"))
1680 (defun org-odt-finalize-outfile ()
1681 (org-odt-delete-empty-paragraphs))
1683 (defun org-odt-delete-empty-paragraphs ()
1684 (goto-char (point-min))
1685 (let ((open "<text:p[^>]*>")
1686 (close "</text:p>"))
1687 (while (re-search-forward (format "%s[ \r\n\t]*%s" open close) nil t)
1688 (replace-match ""))))
1690 (defun org-odt-get (what &optional opt-plist)
1691 (case what
1692 (BACKEND 'odt)
1693 (EXPORT-DIR (org-export-directory :html opt-plist))
1694 (FILE-NAME-EXTENSION "odt")
1695 (EXPORT-BUFFER-NAME "*Org ODT Export*")
1696 (ENTITY-CONTROL org-odt-entity-control-callbacks-alist)
1697 (ENTITY-FORMAT org-odt-entity-format-callbacks-alist)
1698 (INIT-METHOD 'org-odt-init-outfile)
1699 (FINAL-METHOD 'org-odt-finalize-outfile)
1700 (SAVE-METHOD 'org-odt-save-as-outfile)
1701 ;; (OTHER-BACKENDS) ; see note in `org-xhtml-get'
1702 ;; (CONVERT-METHOD) ; see note in `org-xhtml-get'
1703 (TOPLEVEL-HLEVEL 1)
1704 (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps)
1705 (INLINE-IMAGES 'maybe)
1706 (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
1707 (PLAIN-TEXT-MAP '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
1708 (TABLE-FIRST-COLUMN-AS-LABELS nil)
1709 (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY "," 'superscript))
1710 (CODING-SYSTEM-FOR-WRITE 'utf-8)
1711 (CODING-SYSTEM-FOR-SAVE 'utf-8)
1712 (t (error "Unknown property: %s" what))))
1714 (defun org-odt-parse-label (label)
1715 (save-match-data
1716 (if (not (string-match "\\`[a-zA-Z]+:\\(.+\\)" label))
1717 (cons label nil)
1718 (cons
1719 (capitalize (substring label 0 (1- (match-beginning 1))))
1720 (substring label (match-beginning 1))))))
1722 (defvar org-lparse-latex-fragment-fallback) ; set by org-do-lparse
1723 (defvar org-lparse-opt-plist) ; bound during org-do-lparse
1724 (defun org-export-odt-do-preprocess-latex-fragments ()
1725 "Convert LaTeX fragments to images."
1726 (let* ((latex-frag-opt (plist-get org-lparse-opt-plist :LaTeX-fragments))
1727 (latex-frag-opt ; massage the options
1728 (or (and (member latex-frag-opt '(mathjax t))
1729 (not (and (fboundp 'org-format-latex-mathml-available-p)
1730 (org-format-latex-mathml-available-p)))
1731 (prog1 org-lparse-latex-fragment-fallback
1732 (org-lparse-warn
1733 (concat
1734 "LaTeX to MathML converter not available. "
1735 (format "Using %S instead."
1736 org-lparse-latex-fragment-fallback)))))
1737 latex-frag-opt))
1738 cache-dir display-msg)
1739 (cond
1740 ((eq latex-frag-opt 'dvipng)
1741 (setq cache-dir "ltxpng/")
1742 (setq display-msg "Creating LaTeX image %s"))
1743 ((member latex-frag-opt '(mathjax t))
1744 (setq latex-frag-opt 'mathml)
1745 (setq cache-dir "ltxmathml/")
1746 (setq display-msg "Creating MathML formula %s")))
1747 (when (and org-current-export-file)
1748 (org-format-latex
1749 (concat cache-dir (file-name-sans-extension
1750 (file-name-nondirectory org-current-export-file)))
1751 org-current-export-dir nil display-msg
1752 nil nil latex-frag-opt))))
1754 (defun org-export-odt-preprocess-latex-fragments ()
1755 (when (equal org-export-current-backend 'odt)
1756 (org-export-odt-do-preprocess-latex-fragments)))
1758 (defun org-export-odt-preprocess-label-references ()
1759 (goto-char (point-min))
1760 (let (label label-components category value pretty-label)
1761 (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t)
1762 (org-if-unprotected-at (match-beginning 1)
1763 (setq label (match-string 1)
1764 label-components (org-odt-parse-label label)
1765 category (car label-components)
1766 value (cdr label-components)
1767 pretty-label (if value (concat category " " value) label))
1768 (replace-match
1769 (let ((org-lparse-encode-pending t))
1770 (org-odt-format-tags
1771 '("<text:sequence-ref text:reference-format=\"category-and-value\" text:ref-name=\"%s\">"
1772 . "</text:sequence-ref>") pretty-label label)) t t)))))
1774 ;; process latex fragments as part of
1775 ;; `org-export-preprocess-after-blockquote-hook'. Note that this hook
1776 ;; is the one that is closest and well before the call to
1777 ;; `org-export-attach-captions-and-attributes' in
1778 ;; `org-export-preprocess-stirng'. The above arrangement permits
1779 ;; captions, labels and attributes to be attached to png images
1780 ;; generated out of latex equations.
1781 (add-hook 'org-export-preprocess-after-blockquote-hook
1782 'org-export-odt-preprocess-latex-fragments)
1784 (defun org-export-odt-preprocess (parameters)
1785 (org-export-odt-preprocess-label-references))
1787 (declare-function archive-zip-extract "arc-mode.el" (archive name))
1788 (defun org-odt-zip-extract-one (archive member &optional target)
1789 (require 'arc-mode)
1790 (let* ((target (or target default-directory))
1791 (archive (expand-file-name archive))
1792 (archive-zip-extract
1793 (list "unzip" "-qq" "-o" "-d" target))
1794 exit-code command-output)
1795 (setq command-output
1796 (with-temp-buffer
1797 (setq exit-code (archive-zip-extract archive member))
1798 (buffer-string)))
1799 (unless (zerop exit-code)
1800 (message command-output)
1801 (error "Extraction failed"))))
1803 (defun org-odt-zip-extract (archive members &optional target)
1804 (when (atom members) (setq members (list members)))
1805 (mapc (lambda (member)
1806 (org-odt-zip-extract-one archive member target))
1807 members))
1809 (defun org-odt-copy-styles-file (&optional styles-file)
1810 ;; Non-availability of styles.xml is not a critical error. For now
1811 ;; throw an error purely for aesthetic reasons.
1812 (setq styles-file (or styles-file
1813 org-export-odt-styles-file
1814 (expand-file-name "styles/OrgOdtStyles.xml"
1815 org-odt-data-dir)
1816 (error "org-odt: Missing styles file?")))
1817 (cond
1818 ((listp styles-file)
1819 (let ((archive (nth 0 styles-file))
1820 (members (nth 1 styles-file)))
1821 (org-odt-zip-extract archive members)
1822 (mapc
1823 (lambda (member)
1824 (when (org-file-image-p member)
1825 (let* ((image-type (file-name-extension member))
1826 (media-type (format "image/%s" image-type)))
1827 (org-odt-create-manifest-file-entry media-type member))))
1828 members)))
1829 ((and (stringp styles-file) (file-exists-p styles-file))
1830 (let ((styles-file-type (file-name-extension styles-file)))
1831 (cond
1832 ((string= styles-file-type "xml")
1833 (copy-file styles-file "styles.xml" t))
1834 ((member styles-file-type '("odt" "ott"))
1835 (org-odt-zip-extract styles-file "styles.xml")))))
1837 (error (format "Invalid specification of styles.xml file: %S"
1838 org-export-odt-styles-file))))
1840 ;; create a manifest entry for styles.xml
1841 (org-odt-create-manifest-file-entry "text/xml" "styles.xml"))
1843 (defvar org-export-odt-factory-settings
1844 "d4328fb9d1b6cb211d4320ff546829f26700dc5e"
1845 "SHA1 hash of OrgOdtStyles.xml.")
1847 (defun org-odt-configure-outline-numbering (level)
1848 "Outline numbering is retained only upto LEVEL.
1849 To disable outline numbering pass a LEVEL of 0."
1850 (goto-char (point-min))
1851 (let ((regex
1852 "<text:outline-level-style\\(.*\\)text:level=\"\\([^\"]*\\)\"\\(.*\\)>")
1853 (replacement
1854 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1855 (while (re-search-forward regex nil t)
1856 (when (> (string-to-number (match-string 2)) level)
1857 (replace-match replacement t nil))))
1858 (save-buffer 0))
1860 (provide 'org-odt)
1862 ;;; org-odt.el ends here