org-odt.el: Rework embedding of images
[org-mode.git] / contrib / lisp / org-odt.el
blob2ac7b66790171920dfc1ab0cfccfca4062630a6b
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-odt-format-org-link (opt-plist type-1 path fragment desc attr
1059 descp)
1060 "Make an HTML link.
1061 OPT-PLIST is an options list.
1062 TYPE is the device-type of the link (THIS://foo.html)
1063 PATH is the path of the link (http://THIS#locationx)
1064 FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
1065 DESC is the link description, if any.
1066 ATTR is a string of other attributes of the a element.
1067 MAY-INLINE-P allows inlining it as an image."
1069 (declare (special org-lparse-par-open))
1070 (save-match-data
1071 (let* ((may-inline-p
1072 (and (member type-1 '("http" "https" "file"))
1073 (org-lparse-should-inline-p path descp)
1074 (not fragment)))
1075 (type (if (equal type-1 "id") "file" type-1))
1076 (filename path)
1077 (thefile path))
1079 (cond
1080 ;; check for inlined images
1081 ((and (member type '("file"))
1082 (not fragment)
1083 (org-file-image-p
1084 filename org-odt-export-inline-image-extensions)
1085 (or (eq t org-odt-export-inline-images)
1086 (and org-odt-export-inline-images (not descp))))
1088 ;; (when (and (string= type "file") (file-name-absolute-p path))
1089 ;; (setq thefile (concat "file://" (expand-file-name path))))
1090 ;; (setq thefile (org-xml-format-href thefile))
1091 ;; (org-export-html-format-image thefile)
1092 (org-odt-format-inline-image thefile))
1094 (when (string= type "file")
1095 (setq thefile
1096 (cond
1097 ((file-name-absolute-p path)
1098 (concat "file://" (expand-file-name path)))
1099 (t (org-odt-relocate-relative-path
1100 thefile org-current-export-file)))))
1102 (when (and (member type '("" "http" "https" "file" "coderef"))
1103 fragment)
1104 (setq thefile (concat thefile "#" fragment)))
1106 (setq thefile (org-xml-format-href thefile))
1108 (when (not (member type '("" "file" "coderef")))
1109 (setq thefile (concat type ":" thefile)))
1111 (let ((org-odt-suppress-xref (string= type "coderef")))
1112 (org-odt-format-link
1113 (org-xml-format-desc desc) thefile attr)))))))
1115 (defun org-odt-format-heading (text level &optional id)
1116 (let* ((text (if id (org-odt-format-target text id) text)))
1117 (org-odt-format-tags
1118 '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
1119 "</text:h>") text level level)))
1121 (defun org-odt-format-headline (title extra-targets tags
1122 &optional snumber level)
1123 (concat
1124 (org-lparse-format 'EXTRA-TARGETS extra-targets)
1126 ;; No need to generate section numbers. They are auto-generated by
1127 ;; the application
1129 ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
1130 title
1131 (and tags (concat (org-lparse-format 'SPACES 3)
1132 (org-lparse-format 'ORG-TAGS tags)))))
1134 (defun org-odt-format-anchor (text name &optional class)
1135 (org-odt-format-target text name))
1137 (defun org-odt-format-bookmark (text id)
1138 (if id
1139 (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id)
1140 text))
1142 (defun org-odt-format-target (text id)
1143 (let ((name (concat org-export-odt-bookmark-prefix id)))
1144 (concat
1145 (and id (org-odt-format-tags
1146 "<text:bookmark-start text:name=\"%s\"/>" "" name))
1147 (org-odt-format-bookmark text id)
1148 (and id (org-odt-format-tags
1149 "<text:bookmark-end text:name=\"%s\"/>" "" name)))))
1151 (defun org-odt-format-footnote (n def)
1152 (let ((id (concat "fn" n))
1153 (note-class "footnote")
1154 (par-style "Footnote"))
1155 (org-odt-format-tags
1156 '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
1157 "</text:note>")
1158 (concat
1159 (org-odt-format-tags
1160 '("<text:note-citation>" . "</text:note-citation>")
1162 (org-odt-format-tags
1163 '("<text:note-body>" . "</text:note-body>")
1164 def))
1165 id note-class)))
1167 (defun org-odt-format-footnote-reference (n def refcnt)
1168 (if (= refcnt 1)
1169 (org-odt-format-footnote n def)
1170 (org-odt-format-footnote-ref n)))
1172 (defun org-odt-format-footnote-ref (n)
1173 (let ((note-class "footnote")
1174 (ref-format "text")
1175 (ref-name (concat "fn" n)))
1176 (org-odt-format-tags
1177 '("<text:span text:style-name=\"%s\">" . "</text:span>")
1178 (org-odt-format-tags
1179 '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" . "</text:note-ref>")
1180 n note-class ref-format ref-name)
1181 "OrgSuperscript")))
1183 (defun org-odt-get-image-name (file-name)
1184 (require 'sha1)
1185 (file-relative-name
1186 (expand-file-name
1187 (concat (sha1 file-name) "." (file-name-extension file-name)) "Pictures")))
1189 (defun org-export-odt-format-image (src href)
1190 "Create image tag with source and attributes."
1191 (save-match-data
1192 (let* ((caption (org-find-text-property-in-string 'org-caption src))
1193 (caption (and caption (org-xml-format-desc caption)))
1194 (attr (org-find-text-property-in-string 'org-attributes src))
1195 (label (org-find-text-property-in-string 'org-label src))
1196 (embed-as (if (string-match "^ltxpng/" src) 'character 'paragraph))
1197 (attr-plist (when attr (read attr)))
1198 (size (org-odt-image-size-from-file
1199 src (plist-get attr-plist :width)
1200 (plist-get attr-plist :height)
1201 (plist-get attr-plist :scale) nil embed-as)))
1202 (org-export-odt-do-format-image
1203 embed-as caption attr label size href))))
1205 (defun org-odt-format-textbox (text style)
1206 (let ((draw-frame-pair
1207 '("<draw:frame draw:style-name=\"%s\"
1208 text:anchor-type=\"paragraph\"
1209 style:rel-width=\"100%%\"
1210 draw:z-index=\"0\">" . "</draw:frame>")))
1211 (org-odt-format-tags
1212 draw-frame-pair
1213 (org-odt-format-tags
1214 '("<draw:text-box fo:min-height=\"%dcm\">" . "</draw:text-box>")
1215 text 0) style)))
1217 (defun org-odt-format-inlinetask (heading content
1218 &optional todo priority tags)
1219 (org-odt-format-stylized-paragraph
1220 nil (org-odt-format-textbox
1221 (concat (org-odt-format-stylized-paragraph
1222 "OrgInlineTaskHeading"
1223 (org-lparse-format
1224 'HEADLINE (concat (org-lparse-format-todo todo) " " heading)
1225 nil tags))
1226 content) "OrgInlineTaskFrame")))
1228 (defun org-export-odt-do-format-image (embed-as caption attr label
1229 size href)
1230 "Create image tag with source and attributes."
1231 (save-match-data
1232 (let ((width (car size)) (height (cdr size))
1233 (draw-frame-pair
1234 '("<draw:frame draw:style-name=\"%s\"
1235 text:anchor-type=\"%s\"
1236 draw:z-index=\"%d\" %s>" . "</draw:frame>")))
1237 (cond
1238 ((and (not caption) (not label))
1239 (let (style-name anchor-type)
1240 (cond
1241 ((eq embed-as 'paragraph)
1242 (setq style-name "OrgGraphicsParagraph" anchor-type "paragraph"))
1243 ((eq embed-as 'character)
1244 (setq style-name "OrgGraphicsBaseline" anchor-type "as-char")))
1245 (org-odt-format-tags
1246 draw-frame-pair href style-name anchor-type 0
1247 (org-odt-image-attrs-from-size width height))))
1250 (concat
1251 ;; (when par-open (org-odt-close-par))
1252 (org-odt-format-tags
1253 draw-frame-pair
1254 (org-odt-format-tags
1255 '("<draw:text-box fo:min-height=\"%dcm\">" . "</draw:text-box>")
1256 (org-odt-format-stylized-paragraph
1257 'illustration
1258 (concat
1259 (let ((extra " style:rel-width=\"100%\" style:rel-height=\"scale\""))
1260 (org-odt-format-tags
1261 draw-frame-pair href "OrgGraphicsParagraphContent" "paragraph" 2
1262 (concat (org-odt-image-attrs-from-size width height) extra)))
1263 (org-odt-format-entity-caption label caption)))
1264 height)
1265 "OrgFrame" "paragraph" 1
1266 (org-odt-image-attrs-from-size width))
1268 ;; (when par-open (org-odt-open-par))
1269 ))))))
1271 (defvar org-odt-embedded-images-count 0)
1272 (defun org-odt-copy-image-file (path)
1273 "Returns the internal name of the file"
1274 (let* ((image-type (file-name-extension path))
1275 (media-type (format "image/%s" image-type))
1276 (src-file (expand-file-name
1277 path (file-name-directory org-current-export-file)))
1278 (target-dir "Images/")
1279 (target-file
1280 (format "%s%04d.%s" target-dir
1281 (incf org-odt-embedded-images-count) image-type)))
1282 (when (not org-lparse-to-buffer)
1283 (message "Embedding %s as %s ..."
1284 (substring-no-properties path) target-file)
1286 (when (= 1 org-odt-embedded-images-count)
1287 (make-directory target-dir)
1288 (org-odt-create-manifest-file-entry "" target-dir))
1290 (copy-file src-file target-file 'overwrite)
1291 (org-odt-create-manifest-file-entry media-type target-file))
1292 target-file))
1294 (defun org-odt-image-attrs-from-size (&optional width height)
1295 (concat
1296 (when width (format "svg:width=\"%0.2fcm\"" width))
1298 (when height (format "svg:height=\"%0.2fcm\"" height))))
1300 (defvar org-export-odt-image-size-probe-method
1301 '(emacs imagemagick force)
1302 "Ordered list of methods by for determining size of an embedded
1303 image.")
1305 (defvar org-export-odt-default-image-sizes-alist
1306 '(("character" . (5 . 0.4))
1307 ("paragraph" . (5 . 5)))
1308 "Hardcoded image dimensions one for each of the anchor
1309 methods.")
1311 (defun org-odt-do-image-size (probe-method file &optional dpi anchor-type)
1312 (setq dpi (or dpi org-export-odt-pixels-per-inch))
1313 (setq anchor-type (or anchor-type "paragraph"))
1314 (flet ((size-in-cms (size-in-pixels)
1315 (flet ((pixels-to-cms (pixels)
1316 (let* ((cms-per-inch 2.54)
1317 (inches (/ pixels dpi)))
1318 (* cms-per-inch inches))))
1319 (and size-in-pixels
1320 (cons (pixels-to-cms (car size-in-pixels))
1321 (pixels-to-cms (cdr size-in-pixels)))))))
1322 (case probe-method
1323 (emacs
1324 (size-in-cms (ignore-errors (image-size (create-image file) 'pixels))))
1325 (imagemagick
1326 (size-in-cms
1327 (let ((dim (shell-command-to-string
1328 (format "identify -format \"%%w:%%h\" \"%s\"" file))))
1329 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
1330 (cons (string-to-number (match-string 1 dim))
1331 (string-to-number (match-string 2 dim)))))))
1333 (cdr (assoc-string anchor-type
1334 org-export-odt-default-image-sizes-alist))))))
1336 (defun org-odt-image-size-from-file (file &optional user-width
1337 user-height scale dpi embed-as)
1338 (unless (file-name-absolute-p file)
1339 (setq file (expand-file-name
1340 file (file-name-directory org-current-export-file))))
1341 (let* (size width height)
1342 (unless (and user-height user-width)
1343 (loop for probe-method in org-export-odt-image-size-probe-method
1344 until size
1345 do (setq size (org-odt-do-image-size
1346 probe-method file dpi embed-as)))
1347 (or size (error "Cannot determine Image size. Aborting ..."))
1348 (setq width (car size) height (cdr size)))
1349 (cond
1350 (scale
1351 (setq width (* width scale) height (* height scale)))
1352 ((and user-height user-width)
1353 (setq width user-width height user-height))
1354 (user-height
1355 (setq width (* user-height (/ width height)) height user-height))
1356 (user-width
1357 (setq height (* user-width (/ height width)) width user-width))
1358 (t (ignore)))
1359 (cons width height)))
1361 (defvar org-odt-default-entity "Illustration")
1362 (defun org-odt-format-entity-caption (label caption &optional default-entity)
1363 (if (not label) (or caption "")
1364 (let* ((label-components (org-odt-parse-label label))
1365 (entity (car label-components))
1366 (seqno (cdr label-components))
1367 (caption (and caption (concat ": " caption))))
1368 (unless seqno
1369 (setq seqno label
1370 entity (or default-entity org-odt-default-entity)))
1371 (concat
1372 entity " "
1373 (org-odt-format-tags
1374 '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" . "</text:sequence>")
1375 seqno label entity entity)
1376 caption))))
1378 (defun org-odt-format-tags (tag text &rest args)
1379 (let ((prefix (when org-lparse-encode-pending "@"))
1380 (suffix (when org-lparse-encode-pending "@")))
1381 (apply 'org-lparse-format-tags tag text prefix suffix args)))
1383 (defun org-odt-init-outfile (filename)
1384 (unless (executable-find "zip")
1385 ;; Not at all OSes ship with zip by default
1386 (error "Executable \"zip\" needed for creating OpenDocument files"))
1388 (let* ((outdir (make-temp-file org-export-odt-tmpdir-prefix t))
1389 (content-file (expand-file-name "content.xml" outdir)))
1391 ;; init conten.xml
1392 (with-current-buffer (find-file-noselect content-file t))
1394 ;; reset variables
1395 (setq org-odt-manifest-file-entries nil
1396 org-odt-embedded-images-count 0)
1398 content-file))
1400 (defcustom org-export-odt-prettify-xml nil
1401 "Specify whether or not the xml output should be prettified.
1402 When this option is turned on, `indent-region' is run on all
1403 component xml buffers before they are saved. Turn this off for
1404 regular use. Turn this on if you need to examine the xml
1405 visually."
1406 :group 'org-export-odt
1407 :type 'boolean)
1409 (defvar hfy-user-sheet-assoc) ; bound during org-do-lparse
1410 (defun org-odt-save-as-outfile (target opt-plist)
1411 ;; create mimetype file
1412 (write-region "application/vnd.oasis.opendocument.text" nil
1413 (expand-file-name "mimetype"))
1415 ;; write meta file
1416 (org-odt-update-meta-file opt-plist)
1418 ;; write styles file
1419 (org-odt-copy-styles-file)
1421 ;; Update styles.xml - take care of outline numbering
1422 (with-current-buffer
1423 (find-file-noselect (expand-file-name "styles.xml") t)
1424 ;; Don't make automatic backup of styles.xml file. This setting
1425 ;; prevents the backedup styles.xml file from being zipped in to
1426 ;; odt file. This is more of a hackish fix. Better alternative
1427 ;; would be to fix the zip command so that the output odt file
1428 ;; includes only the needed files and excludes any auto-generated
1429 ;; extra files like backups and auto-saves etc etc. Note that
1430 ;; currently the zip command zips up the entire temp directory so
1431 ;; that any auto-generated files created under the hood ends up in
1432 ;; the resulting odt file.
1433 (set (make-local-variable 'backup-inhibited) t)
1435 ;; Import local setting of `org-export-with-section-numbers'
1436 (org-lparse-bind-local-variables opt-plist)
1437 (org-odt-configure-outline-numbering
1438 (if org-export-with-section-numbers org-export-headline-levels 0)))
1440 ;; Write custom stlyes for source blocks
1441 (org-odt-insert-custom-styles-for-srcblocks
1442 (mapconcat
1443 (lambda (style)
1444 (format " %s\n" (cddr style)))
1445 hfy-user-sheet-assoc ""))
1447 ;; create a manifest entry for content.xml
1448 (org-odt-create-manifest-file-entry
1449 "application/vnd.oasis.opendocument.text" "/" "1.2")
1451 (org-odt-create-manifest-file-entry "text/xml" "content.xml")
1453 ;; write out the manifest entries before zipping
1454 (org-odt-write-manifest-file)
1456 (let ((xml-files '("mimetype" "META-INF/manifest.xml" "content.xml"
1457 "meta.xml" "styles.xml"))
1458 (zipdir default-directory))
1459 (message "Switching to directory %s" (expand-file-name zipdir))
1461 ;; save all xml files
1462 (mapc (lambda (file)
1463 (with-current-buffer
1464 (find-file-noselect (expand-file-name file) t)
1465 ;; prettify output if needed
1466 (when org-export-odt-prettify-xml
1467 (indent-region (point-min) (point-max)))
1468 (save-buffer 0)))
1469 xml-files)
1471 (let* ((target-name (file-name-nondirectory target))
1472 (target-dir (file-name-directory target))
1473 (cmds `(("zip" "-mX0" ,target-name "mimetype")
1474 ("zip" "-rmTq" ,target-name "."))))
1475 (when (file-exists-p target)
1476 ;; FIXME: If the file is locked this throws a cryptic error
1477 (delete-file target))
1479 (let ((coding-system-for-write 'no-conversion) exitcode)
1480 (message "Creating odt file...")
1481 (mapc
1482 (lambda (cmd)
1483 (message "Running %s" (mapconcat 'identity cmd " "))
1484 (setq exitcode
1485 (apply 'call-process (car cmd) nil nil nil (cdr cmd)))
1486 (or (zerop exitcode)
1487 (error "Unable to create odt file (%S)" exitcode)))
1488 cmds))
1490 ;; move the file from outdir to target-dir
1491 (rename-file target-name target-dir)
1493 ;; kill all xml buffers
1494 (mapc (lambda (file)
1495 (kill-buffer
1496 (find-file-noselect (expand-file-name file zipdir) t)))
1497 xml-files)
1499 (delete-directory zipdir)))
1501 (message "Created %s" target)
1502 (set-buffer (find-file-noselect target t)))
1504 (defun org-odt-format-date (date)
1505 (let ((warning-msg
1506 "OpenDocument files require that dates be in ISO-8601 format. Please review your DATE options for compatibility."))
1507 ;; If the user is not careful with the date specification, an
1508 ;; invalid meta.xml will be emitted.
1510 ;; For now honor user's diktat and let him off with a warning
1511 ;; message. This is OK as LibreOffice (and possibly other
1512 ;; apps) doesn't deem this deviation as critical and continue
1513 ;; to load the file.
1515 ;; FIXME: Surely there a better way to handle this. Revisit this
1516 ;; later.
1517 (cond
1518 ((and date (string-match "%" date))
1519 ;; Honor user's diktat. See comments above
1520 (org-lparse-warn warning-msg)
1521 (format-time-string date))
1522 (date
1523 ;; Honor user's diktat. See comments above
1524 (org-lparse-warn warning-msg)
1525 date)
1527 ;; ISO 8601 format
1528 (format-time-string "%Y-%m-%dT%T%:z")))))
1530 (defconst org-odt-manifest-file-entry-tag
1532 <manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
1534 (defvar org-odt-manifest-file-entries nil)
1536 (defun org-odt-create-manifest-file-entry (&rest args)
1537 (push args org-odt-manifest-file-entries))
1539 (defun org-odt-write-manifest-file ()
1540 (make-directory "META-INF")
1541 (let ((manifest-file (expand-file-name "META-INF/manifest.xml")))
1542 (write-region
1543 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
1544 <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n"
1545 nil manifest-file)
1546 (mapc
1547 (lambda (file-entry)
1548 (let* ((version (nth 2 file-entry))
1549 (extra (if version
1550 (format " manifest:version=\"%s\"" version)
1551 "")))
1552 (write-region
1553 (format org-odt-manifest-file-entry-tag
1554 (nth 0 file-entry) (nth 1 file-entry) extra)
1555 nil manifest-file t))) org-odt-manifest-file-entries)
1556 (write-region "\n</manifest:manifest>" nil manifest-file t)))
1558 (defun org-odt-update-meta-file (opt-plist)
1559 (let ((date (org-odt-format-date (plist-get opt-plist :date)))
1560 (author (or (plist-get opt-plist :author) ""))
1561 (email (plist-get opt-plist :email))
1562 (keywords (plist-get opt-plist :keywords))
1563 (description (plist-get opt-plist :description))
1564 (title (plist-get opt-plist :title)))
1566 (write-region
1567 (concat
1568 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
1569 <office:document-meta
1570 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
1571 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
1572 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
1573 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
1574 xmlns:ooo=\"http://openoffice.org/2004/office\"
1575 office:version=\"1.2\">
1576 <office:meta>" "\n"
1577 (org-odt-format-tags '("<dc:creator>" . "</dc:creator>") author)
1578 (org-odt-format-tags
1579 '("\n<meta:initial-creator>" . "</meta:initial-creator>") author)
1580 (org-odt-format-tags '("\n<dc:date>" . "</dc:date>") date)
1581 (org-odt-format-tags
1582 '("\n<meta:creation-date>" . "</meta:creation-date>") date)
1583 (org-odt-format-tags '("\n<meta:generator>" . "</meta:generator>")
1584 (when org-export-creator-info
1585 (format "Org-%s/Emacs-%s"
1586 org-version emacs-version)))
1587 (org-odt-format-tags '("\n<meta:keyword>" . "</meta:keyword>") keywords)
1588 (org-odt-format-tags '("\n<dc:subject>" . "</dc:subject>") description)
1589 (org-odt-format-tags '("\n<dc:title>" . "</dc:title>") title)
1590 "\n"
1591 " </office:meta>" "</office:document-meta>")
1592 nil (expand-file-name "meta.xml")))
1594 ;; create a manifest entry for meta.xml
1595 (org-odt-create-manifest-file-entry "text/xml" "meta.xml"))
1597 (defun org-odt-finalize-outfile ()
1598 (org-odt-delete-empty-paragraphs))
1600 (defun org-odt-delete-empty-paragraphs ()
1601 (goto-char (point-min))
1602 (let ((open "<text:p[^>]*>")
1603 (close "</text:p>"))
1604 (while (re-search-forward (format "%s[ \r\n\t]*%s" open close) nil t)
1605 (replace-match ""))))
1607 (defun org-odt-get (what &optional opt-plist)
1608 (case what
1609 (BACKEND 'odt)
1610 (EXPORT-DIR (org-export-directory :html opt-plist))
1611 (FILE-NAME-EXTENSION "odt")
1612 (EXPORT-BUFFER-NAME "*Org ODT Export*")
1613 (ENTITY-CONTROL org-odt-entity-control-callbacks-alist)
1614 (ENTITY-FORMAT org-odt-entity-format-callbacks-alist)
1615 (INIT-METHOD 'org-odt-init-outfile)
1616 (FINAL-METHOD 'org-odt-finalize-outfile)
1617 (SAVE-METHOD 'org-odt-save-as-outfile)
1618 ;; (OTHER-BACKENDS) ; see note in `org-xhtml-get'
1619 ;; (CONVERT-METHOD) ; see note in `org-xhtml-get'
1620 (TOPLEVEL-HLEVEL 1)
1621 (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps)
1622 (INLINE-IMAGES 'maybe)
1623 (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
1624 (PLAIN-TEXT-MAP '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
1625 (TABLE-FIRST-COLUMN-AS-LABELS nil)
1626 (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY "," 'superscript))
1627 (CODING-SYSTEM-FOR-WRITE 'utf-8)
1628 (CODING-SYSTEM-FOR-SAVE 'utf-8)
1629 (t (error "Unknown property: %s" what))))
1631 (defun org-odt-parse-label (label)
1632 (save-match-data
1633 (if (not (string-match "\\`[a-zA-Z]+:\\(.+\\)" label))
1634 (cons label nil)
1635 (cons
1636 (capitalize (substring label 0 (1- (match-beginning 1))))
1637 (substring label (match-beginning 1))))))
1639 (defvar org-lparse-latex-fragment-fallback) ; set by org-do-lparse
1640 (defvar org-lparse-opt-plist) ; bound during org-do-lparse
1641 (defun org-export-odt-do-preprocess-latex-fragments ()
1642 "Convert LaTeX fragments to images."
1643 (let* ((latex-frag-opt (plist-get org-lparse-opt-plist :LaTeX-fragments))
1644 (latex-frag-opt-1 ; massage the options
1645 (or (and (member latex-frag-opt '(mathjax t))
1646 (prog1 org-lparse-latex-fragment-fallback
1647 (org-lparse-warn
1648 (concat
1649 "Use of MathJax is incompatible with ODT exporter. "
1650 (format "Using %S instead."
1651 org-lparse-latex-fragment-fallback)))))
1652 latex-frag-opt)))
1653 (when (and org-current-export-file latex-frag-opt-1)
1654 ;; Investigate MathToWeb for converting TeX equations to MathML
1655 ;; http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01755.html
1656 (org-format-latex
1657 (concat "ltxpng/" (file-name-sans-extension
1658 (file-name-nondirectory
1659 org-current-export-file)))
1660 org-current-export-dir nil "Creating LaTeX image %s"
1661 nil nil latex-frag-opt-1))))
1663 (defun org-export-odt-preprocess-latex-fragments ()
1664 (when (equal org-export-current-backend 'odt)
1665 (org-export-odt-do-preprocess-latex-fragments)))
1667 (defun org-export-odt-preprocess-label-references ()
1668 (goto-char (point-min))
1669 (let (label label-components category value pretty-label)
1670 (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t)
1671 (org-if-unprotected-at (match-beginning 1)
1672 (setq label (match-string 1)
1673 label-components (org-odt-parse-label label)
1674 category (car label-components)
1675 value (cdr label-components)
1676 pretty-label (if value (concat category " " value) label))
1677 (replace-match
1678 (let ((org-lparse-encode-pending t))
1679 (org-odt-format-tags
1680 '("<text:sequence-ref text:reference-format=\"category-and-value\" text:ref-name=\"%s\">"
1681 . "</text:sequence-ref>") pretty-label label)) t t)))))
1683 ;; process latex fragments as part of
1684 ;; `org-export-preprocess-after-blockquote-hook'. Note that this hook
1685 ;; is the one that is closest and well before the call to
1686 ;; `org-export-attach-captions-and-attributes' in
1687 ;; `org-export-preprocess-stirng'. The above arrangement permits
1688 ;; captions, labels and attributes to be attached to png images
1689 ;; generated out of latex equations.
1690 (add-hook 'org-export-preprocess-after-blockquote-hook
1691 'org-export-odt-preprocess-latex-fragments)
1693 (defun org-export-odt-preprocess (parameters)
1694 (org-export-odt-preprocess-label-references))
1696 (declare-function archive-zip-extract "arc-mode.el" (archive name))
1697 (defun org-odt-zip-extract-one (archive member &optional target)
1698 (require 'arc-mode)
1699 (let* ((target (or target default-directory))
1700 (archive (expand-file-name archive))
1701 (archive-zip-extract
1702 (list "unzip" "-qq" "-o" "-d" target))
1703 exit-code command-output)
1704 (setq command-output
1705 (with-temp-buffer
1706 (setq exit-code (archive-zip-extract archive member))
1707 (buffer-string)))
1708 (unless (zerop exit-code)
1709 (message command-output)
1710 (error "Extraction failed"))))
1712 (defun org-odt-zip-extract (archive members &optional target)
1713 (when (atom members) (setq members (list members)))
1714 (mapc (lambda (member)
1715 (org-odt-zip-extract-one archive member target))
1716 members))
1718 (defun org-odt-copy-styles-file (&optional styles-file)
1719 ;; Non-availability of styles.xml is not a critical error. For now
1720 ;; throw an error purely for aesthetic reasons.
1721 (setq styles-file (or styles-file
1722 org-export-odt-styles-file
1723 (expand-file-name "styles/OrgOdtStyles.xml"
1724 org-odt-data-dir)
1725 (error "org-odt: Missing styles file?")))
1726 (cond
1727 ((listp styles-file)
1728 (let ((archive (nth 0 styles-file))
1729 (members (nth 1 styles-file)))
1730 (org-odt-zip-extract archive members)
1731 (mapc
1732 (lambda (member)
1733 (when (org-file-image-p member)
1734 (let* ((image-type (file-name-extension member))
1735 (media-type (format "image/%s" image-type)))
1736 (org-odt-create-manifest-file-entry media-type member))))
1737 members)))
1738 ((and (stringp styles-file) (file-exists-p styles-file))
1739 (let ((styles-file-type (file-name-extension styles-file)))
1740 (cond
1741 ((string= styles-file-type "xml")
1742 (copy-file styles-file "styles.xml" t))
1743 ((member styles-file-type '("odt" "ott"))
1744 (org-odt-zip-extract styles-file "styles.xml")))))
1746 (error (format "Invalid specification of styles.xml file: %S"
1747 org-export-odt-styles-file))))
1749 ;; create a manifest entry for styles.xml
1750 (org-odt-create-manifest-file-entry "text/xml" "styles.xml"))
1752 (defvar org-export-odt-factory-settings
1753 "d4328fb9d1b6cb211d4320ff546829f26700dc5e"
1754 "SHA1 hash of OrgOdtStyles.xml.")
1756 (defun org-odt-configure-outline-numbering (level)
1757 "Outline numbering is retained only upto LEVEL.
1758 To disable outline numbering pass a LEVEL of 0."
1759 (goto-char (point-min))
1760 (let ((regex
1761 "<text:outline-level-style\\(.*\\)text:level=\"\\([^\"]*\\)\"\\(.*\\)>")
1762 (replacement
1763 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1764 (while (re-search-forward regex nil t)
1765 (when (> (string-to-number (match-string 2)) level)
1766 (replace-match replacement t nil))))
1767 (save-buffer 0))
1769 (provide 'org-odt)
1771 ;;; org-odt.el ends here