org-odt: Force columns of tables to have the same width
[org-mode/org-jambu.git] / contrib / lisp / org-odt.el
blob46e678e699a596b078b44501d2d84b5ccdbb7000
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 ;; notify org-lparse library on unload
147 (org-lparse-unregister-backend 'odt)
148 nil)
150 (defcustom org-export-odt-automatic-styles-file nil
151 "Automatic styles for use with ODT exporter.
152 If unspecified, the file under `org-odt-data-dir' is used."
153 :type 'file
154 :group 'org-export-odt)
156 (defcustom org-export-odt-styles-file nil
157 "Default styles file for use with ODT export.
158 Valid values are one of:
159 1. nil
160 2. path to a styles.xml file
161 3. path to a *.odt or a *.ott file
162 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
163 ...))
165 In case of option 1, an in-built styles.xml is used. See
166 `org-odt-data-dir' for more information.
168 In case of option 3, the specified file is unzipped and the
169 styles.xml embedded therein is used.
171 In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
172 and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
173 generated odt file. Use relative path for specifying the
174 FILE-MEMBERS. styles.xml must be specified as one of the
175 FILE-MEMBERS.
177 Use options 1, 2 or 3 only if styles.xml alone suffices for
178 achieving the desired formatting. Use option 4, if the styles.xml
179 references additional files like header and footer images for
180 achieving the desired formattting."
181 :group 'org-export-odt
182 :type
183 '(choice
184 (const :tag "Factory settings" nil)
185 (file :must-match t :tag "styles.xml")
186 (file :must-match t :tag "ODT or OTT file")
187 (list :tag "ODT or OTT file + Members"
188 (file :must-match t :tag "ODF Text or Text Template file")
189 (cons :tag "Members"
190 (file :tag " Member" "styles.xml")
191 (repeat (file :tag "Member"))))))
193 (defconst org-export-odt-tmpdir-prefix "odt-")
194 (defconst org-export-odt-bookmark-prefix "OrgXref.")
195 (defcustom org-export-odt-use-bookmarks-for-internal-links t
196 "Export Internal links as bookmarks?."
197 :type 'boolean
198 :group 'org-export-odt)
200 (defcustom org-export-odt-embed-images t
201 "Should the images be copied in to the odt file or just linked?"
202 :type 'boolean
203 :group 'org-export-odt)
205 (defcustom org-odt-export-inline-images 'maybe
206 "Non-nil means inline images into exported HTML pages.
207 This is done using an <img> tag. When nil, an anchor with href is used to
208 link to the image. If this option is `maybe', then images in links with
209 an empty description will be inlined, while images with a description will
210 be linked only."
211 :group 'org-odt-export
212 :type '(choice (const :tag "Never" nil)
213 (const :tag "Always" t)
214 (const :tag "When there is no description" maybe)))
216 (defcustom org-odt-export-inline-image-extensions
217 '("png" "jpeg" "jpg" "gif")
218 "Extensions of image files that can be inlined into HTML."
219 :type '(repeat (string :tag "Extension"))
220 :group 'org-odt-export)
222 (defcustom org-export-odt-pixels-per-inch display-pixels-per-inch
223 ;; FIXME add docstring
225 :type 'float
226 :group 'org-export-odt)
228 (defvar org-export-odt-default-org-styles-alist
229 '((paragraph . ((default . "Text_20_body")
230 (fixedwidth . "OrgFixedWidthBlock")
231 (verse . "OrgVerse")
232 (quote . "Quotations")
233 (blockquote . "Quotations")
234 (center . "OrgCenter")
235 (left . "OrgLeft")
236 (right . "OrgRight")
237 (title . "Heading_20_1.title")
238 (footnote . "Footnote")
239 (src . "OrgSrcBlock")
240 (illustration . "Illustration")
241 (table . "Table")
242 (definition-term . "Text_20_body_20_bold")
243 (horizontal-line . "Horizontal_20_Line")))
244 (character . ((bold . "Bold")
245 (emphasis . "Emphasis")
246 (code . "OrgCode")
247 (verbatim . "OrgCode")
248 (strike . "Strikethrough")
249 (underline . "Underline")
250 (subscript . "OrgSubscript")
251 (superscript . "OrgSuperscript")))
252 (list . ((ordered . "OrgNumberedList")
253 (unordered . "OrgBulletedList")
254 (description . "OrgDescriptionList"))))
255 "Default styles for various entities.")
257 (defvar org-export-odt-org-styles-alist org-export-odt-default-org-styles-alist)
258 (defun org-odt-get-style-name-for-entity (category &optional entity)
259 (let ((entity (or entity 'default)))
261 (cdr (assoc entity (cdr (assoc category
262 org-export-odt-org-styles-alist))))
263 (cdr (assoc entity (cdr (assoc category
264 org-export-odt-default-org-styles-alist))))
265 (error "Cannot determine style name for entity %s of type %s"
266 entity category))))
268 (defcustom org-export-odt-preferred-output-format nil
269 "Automatically post-process to this format after exporting to \"odt\".
270 Interactive commands `org-export-as-odt' and
271 `org-export-as-odt-and-open' export first to \"odt\" format and
272 then use an external converter to convert the resulting document
273 to this format.
275 The converter used is that specified with CONVERT-METHOD option
276 in `org-odt-get'. If the above option is unspecified then
277 `org-lparse-convert-process' is used.
279 The format specified here should be listed in OTHER-BACKENDS
280 option of `org-odt-get' or `org-lparse-convert-capabilities' as
281 appropriate."
282 :group 'org-odt
283 :type '(choice :convert-widget
284 (lambda (w)
285 (apply 'widget-convert (widget-type w)
286 (eval (car (widget-get w :args)))))
287 `((const :tag "None" nil)
288 ,@(mapcar (lambda (c)
289 `(const :tag ,(car c) ,(car c)))
290 (org-lparse-get-other-backends "odt")))))
292 ;;;###autoload
293 (defun org-export-as-odt-and-open (arg)
294 "Export the outline as ODT and immediately open it with a browser.
295 If there is an active region, export only the region.
296 The prefix ARG specifies how many levels of the outline should become
297 headlines. The default is 3. Lower levels will become bulleted lists."
298 (interactive "P")
299 (org-lparse-and-open
300 (or org-export-odt-preferred-output-format "odt") "odt" arg))
302 ;;;###autoload
303 (defun org-export-as-odt-batch ()
304 "Call the function `org-lparse-batch'.
305 This function can be used in batch processing as:
306 emacs --batch
307 --load=$HOME/lib/emacs/org.el
308 --eval \"(setq org-export-headline-levels 2)\"
309 --visit=MyFile --funcall org-export-as-odt-batch"
310 (org-lparse-batch "odt"))
312 ;;;###autoload
313 (defun org-export-as-odt-to-buffer (arg)
314 "Call `org-lparse-odt` with output to a temporary buffer.
315 No file is created. The prefix ARG is passed through to `org-lparse-to-buffer'."
316 (interactive "P")
317 (org-lparse-to-buffer "odt" arg))
319 ;;;###autoload
320 (defun org-replace-region-by-odt (beg end)
321 "Assume the current region has org-mode syntax, and convert it to ODT.
322 This can be used in any buffer. For example, you could write an
323 itemized list in org-mode syntax in an ODT buffer and then use this
324 command to convert it."
325 (interactive "r")
326 (org-replace-region-by "odt" beg end))
328 ;;;###autoload
329 (defun org-export-region-as-odt (beg end &optional body-only buffer)
330 "Convert region from BEG to END in org-mode buffer to ODT.
331 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
332 contents, and only produce the region of converted text, useful for
333 cut-and-paste operations.
334 If BUFFER is a buffer or a string, use/create that buffer as a target
335 of the converted ODT. If BUFFER is the symbol `string', return the
336 produced ODT as a string and leave not buffer behind. For example,
337 a Lisp program could call this function in the following way:
339 (setq odt (org-export-region-as-odt beg end t 'string))
341 When called interactively, the output buffer is selected, and shown
342 in a window. A non-interactive call will only return the buffer."
343 (interactive "r\nP")
344 (org-lparse-region "odt" beg end body-only buffer))
346 ;;; org-export-as-odt
347 ;;;###autoload
348 (defun org-export-as-odt (arg &optional hidden ext-plist
349 to-buffer body-only pub-dir)
350 "Export the outline as a OpenDocumentText file.
351 If there is an active region, export only the region. The prefix
352 ARG specifies how many levels of the outline should become
353 headlines. The default is 3. Lower levels will become bulleted
354 lists. HIDDEN is obsolete and does nothing.
355 EXT-PLIST is a property list with external parameters overriding
356 org-mode's default settings, but still inferior to file-local
357 settings. When TO-BUFFER is non-nil, create a buffer with that
358 name and export to that buffer. If TO-BUFFER is the symbol
359 `string', don't leave any buffer behind but just return the
360 resulting XML as a string. When BODY-ONLY is set, don't produce
361 the file header and footer, simply return the content of
362 <body>...</body>, without even the body tags themselves. When
363 PUB-DIR is set, use this as the publishing directory."
364 (interactive "P")
365 (org-lparse (or org-export-odt-preferred-output-format "odt")
366 "odt" arg hidden ext-plist to-buffer body-only pub-dir))
368 (defvar org-odt-entity-control-callbacks-alist
369 `((EXPORT
370 . (org-odt-begin-export org-odt-end-export))
371 (DOCUMENT-CONTENT
372 . (org-odt-begin-document-content org-odt-end-document-content))
373 (DOCUMENT-BODY
374 . (org-odt-begin-document-body org-odt-end-document-body))
375 (TOC
376 . (org-odt-begin-toc org-odt-end-toc))
377 (ENVIRONMENT
378 . (org-odt-begin-environment org-odt-end-environment))
379 (FOOTNOTE-DEFINITION
380 . (org-odt-begin-footnote-definition org-odt-end-footnote-definition))
381 (TABLE
382 . (org-odt-begin-table org-odt-end-table))
383 (TABLE-ROWGROUP
384 . (org-odt-begin-table-rowgroup org-odt-end-table-rowgroup))
385 (LIST
386 . (org-odt-begin-list org-odt-end-list))
387 (LIST-ITEM
388 . (org-odt-begin-list-item org-odt-end-list-item))
389 (OUTLINE
390 . (org-odt-begin-outline org-odt-end-outline))
391 (OUTLINE-TEXT
392 . (org-odt-begin-outline-text org-odt-end-outline-text))
393 (PARAGRAPH
394 . (org-odt-begin-paragraph org-odt-end-paragraph)))
397 (defvar org-odt-entity-format-callbacks-alist
398 `((EXTRA-TARGETS . org-lparse-format-extra-targets)
399 (ORG-TAGS . org-lparse-format-org-tags)
400 (SECTION-NUMBER . org-lparse-format-section-number)
401 (HEADLINE . org-odt-format-headline)
402 (TOC-ENTRY . org-odt-format-toc-entry)
403 (TOC-ITEM . org-odt-format-toc-item)
404 (TAGS . org-odt-format-tags)
405 (SPACES . org-odt-format-spaces)
406 (TABS . org-odt-format-tabs)
407 (LINE-BREAK . org-odt-format-line-break)
408 (FONTIFY . org-odt-format-fontify)
409 (TODO . org-lparse-format-todo)
410 (LINK . org-odt-format-link)
411 (INLINE-IMAGE . org-odt-format-inline-image)
412 (ORG-LINK . org-odt-format-org-link)
413 (HEADING . org-odt-format-heading)
414 (ANCHOR . org-odt-format-anchor)
415 (TABLE . org-lparse-format-table)
416 (TABLE-ROW . org-odt-format-table-row)
417 (TABLE-CELL . org-odt-format-table-cell)
418 (FOOTNOTES-SECTION . ignore)
419 (FOOTNOTE-REFERENCE . org-odt-format-footnote-reference)
420 (HORIZONTAL-LINE . org-odt-format-horizontal-line)
421 (COMMENT . org-odt-format-comment)
422 (LINE . org-odt-format-line)
423 (ORG-ENTITY . org-odt-format-org-entity))
426 ;;;_. callbacks
427 ;;;_. control callbacks
428 ;;;_ , document body
429 (defun org-odt-begin-office-body ()
430 (insert "
431 <office:body>
432 <office:text>
433 <text:sequence-decls>
434 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Illustration\"/>
435 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Table\"/>
436 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Text\"/>
437 <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Drawing\"/>
438 </text:sequence-decls>"))
440 ;; Following variable is let bound when `org-do-lparse' is in
441 ;; progress. See org-html.el.
442 (defvar org-lparse-toc)
443 (defun org-odt-begin-document-body (opt-plist)
444 (org-odt-begin-office-body)
445 (let ((title (plist-get opt-plist :title)))
446 (when title
447 (insert
448 (org-odt-format-stylized-paragraph 'title title))))
450 ;; insert toc
451 (when org-lparse-toc
452 (insert "\n" org-lparse-toc "\n")))
454 (defvar org-lparse-body-only) ; let bound during org-do-lparse
455 (defvar org-lparse-to-buffer) ; let bound during org-do-lparse
456 (defun org-odt-end-document-body (opt-plist)
457 (unless org-lparse-body-only
458 (org-lparse-insert-tag "</office:text>")
459 (org-lparse-insert-tag "</office:body>")))
461 (defconst org-odt-document-content-header
462 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
463 <office:document-content
464 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
465 xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\"
466 xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\"
467 xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\"
468 xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\"
469 xmlns:fo=\"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0\"
470 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
471 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
472 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
473 xmlns:number=\"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0\"
474 xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\"
475 xmlns:chart=\"urn:oasis:names:tc:opendocument:xmlns:chart:1.0\"
476 xmlns:dr3d=\"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0\"
477 xmlns:math=\"http://www.w3.org/1998/Math/MathML\"
478 xmlns:form=\"urn:oasis:names:tc:opendocument:xmlns:form:1.0\"
479 xmlns:script=\"urn:oasis:names:tc:opendocument:xmlns:script:1.0\"
480 xmlns:ooo=\"http://openoffice.org/2004/office\"
481 xmlns:ooow=\"http://openoffice.org/2004/writer\"
482 xmlns:oooc=\"http://openoffice.org/2004/calc\"
483 xmlns:dom=\"http://www.w3.org/2001/xml-events\"
484 xmlns:xforms=\"http://www.w3.org/2002/xforms\"
485 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
486 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
487 xmlns:rpt=\"http://openoffice.org/2005/report\"
488 xmlns:of=\"urn:oasis:names:tc:opendocument:xmlns:of:1.2\"
489 xmlns:xodt=\"http://www.w3.org/1999/xodt\"
490 xmlns:field=\"urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0\" office:version=\"1.2\">
493 (defun org-odt-begin-document-content (opt-plist)
494 ;; document header
495 (insert org-odt-document-content-header)
497 ;; automatic styles
498 (insert-file-contents
499 (or org-export-odt-automatic-styles-file
500 (expand-file-name "styles/OrgOdtAutomaticStyles.xml"
501 org-odt-data-dir)))
502 (goto-char (point-max)))
504 (defun org-odt-end-document-content ()
505 (org-lparse-insert-tag "</office:document-content>"))
507 (defun org-odt-begin-outline (level1 snumber title tags
508 target extra-targets class)
509 (org-lparse-insert
510 'HEADING (org-lparse-format
511 'HEADLINE title extra-targets tags snumber level1)
512 level1 target))
514 (defun org-odt-end-outline ()
515 (ignore))
517 (defun org-odt-begin-outline-text (level1 snumber class)
518 (ignore))
520 (defun org-odt-end-outline-text ()
521 (ignore))
523 (defun org-odt-begin-paragraph (&optional style)
524 (org-lparse-insert-tag
525 "<text:p%s>" (org-odt-get-extra-attrs-for-paragraph-style style)))
527 (defun org-odt-end-paragraph ()
528 (org-lparse-insert-tag "</text:p>"))
530 (defun org-odt-get-extra-attrs-for-paragraph-style (style)
531 (let (style-name)
532 (setq style-name
533 (cond
534 ((stringp style) style)
535 ((symbolp style) (org-odt-get-style-name-for-entity
536 'paragraph style))))
537 (unless style-name
538 (error "Don't know how to handle paragraph style %s" style))
539 (format " text:style-name=\"%s\"" style-name)))
541 (defun org-odt-format-stylized-paragraph (style text)
542 (org-odt-format-tags
543 '("<text:p%s>" . "</text:p>") text
544 (org-odt-get-extra-attrs-for-paragraph-style style)))
546 (defun org-odt-begin-environment (style)
547 (case style
548 ((blockquote verse center quote)
549 (org-lparse-begin-paragraph style)
550 (list))
551 ((fixedwidth native)
552 (org-lparse-end-paragraph)
553 (list))
554 (t (error "Unknown environment %s" style))))
556 (defun org-odt-end-environment (style)
557 (case style
558 ((blockquote verse center quote)
559 (org-lparse-end-paragraph)
560 (list))
561 ((fixedwidth native)
562 (org-lparse-begin-paragraph)
563 (list))
564 (t (error "Unknown environment %s" style))))
566 (defvar org-lparse-list-level) ; dynamically bound in org-do-lparse
567 (defun org-odt-begin-list (ltype)
568 (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
569 ltype))
570 (let* ((style-name (org-odt-get-style-name-for-entity 'list ltype))
571 (extra (concat (when (= org-lparse-list-level 1)
572 " text:continue-numbering=\"false\"")
573 (when style-name
574 (format " text:style-name=\"%s\"" style-name)))))
575 (case ltype
576 ((ordered unordered description)
577 (org-lparse-end-paragraph)
578 (org-lparse-insert-tag "<text:list%s>" extra))
579 (t (error "Unknown list type: %s" ltype)))))
581 (defun org-odt-end-list (ltype)
582 (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
583 ltype))
584 (if ltype
585 (org-lparse-insert-tag "</text:list>")
586 (error "Unknown list type: %s" ltype)))
588 (defun org-odt-begin-list-item (ltype &optional arg headline)
589 (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
590 ltype))
591 (case ltype
592 (ordered
593 (assert (not headline) t)
594 (let* ((counter arg) (extra ""))
595 (org-lparse-insert-tag "<text:list-item>")
596 (org-lparse-begin-paragraph)))
597 (unordered
598 (let* ((id arg) (extra ""))
599 (org-lparse-insert-tag "<text:list-item>")
600 (org-lparse-begin-paragraph)
601 (insert (if headline (org-odt-format-target headline id)
602 (org-odt-format-bookmark "" id)))))
603 (description
604 (assert (not headline) t)
605 (let ((term (or arg "(no term)")))
606 (insert
607 (org-odt-format-tags
608 '("<text:list-item>" . "</text:list-item>")
609 (org-odt-format-stylized-paragraph 'definition-term term)))
610 (org-lparse-begin 'LIST-ITEM 'unordered)
611 (org-lparse-begin-list 'description)
612 (org-lparse-begin 'LIST-ITEM 'unordered)))
613 (t (error "Unknown list type"))))
615 (defun org-odt-end-list-item (ltype)
616 (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
617 ltype))
618 (case ltype
619 ((ordered unordered)
620 (org-lparse-insert-tag "</text:list-item>"))
621 (description
622 (org-lparse-end-list-item)
623 (org-lparse-end-list 'description)
624 (org-lparse-end-list-item))
625 (t (error "Unknown list type"))))
627 ;; Following variables are let bound when table emission is in
628 ;; progress. See org-lparse.el.
629 (defvar org-lparse-table-begin-marker)
630 (defvar org-lparse-table-ncols)
631 (defvar org-lparse-table-rowgrp-open)
632 (defvar org-lparse-table-rownum)
633 (defvar org-lparse-table-cur-rowgrp-is-hdr)
634 (defvar org-lparse-table-is-styled)
635 (defvar org-lparse-table-rowgrp-info)
636 (defvar org-lparse-table-colalign-vector)
637 (defun org-odt-begin-table (caption label attributes)
638 (when label
639 (insert
640 (org-odt-format-stylized-paragraph
641 'table (org-odt-format-entity-caption label caption "Table"))))
643 (org-lparse-insert-tag
644 "<table:table table:name=\"%s\" table:style-name=\"%s\">"
645 (or label "") "OrgTable")
646 (setq org-lparse-table-begin-marker (point)))
648 (defun org-odt-end-table ()
649 (goto-char org-lparse-table-begin-marker)
650 (loop for level from 0 below org-lparse-table-ncols
651 do (insert
652 (org-odt-format-tags
653 "<table:table-column table:style-name=\"OrgTableColumn\"/>" "")))
655 ;; fill style attributes for table cells
656 (when org-lparse-table-is-styled
657 (while (re-search-forward "@@\\(table-cell:p\\|table-cell:style-name\\)@@\\([0-9]+\\)@@\\([0-9]+\\)@@" nil t)
658 (let ((spec (match-string 1))
659 (r (string-to-number (match-string 2)))
660 (c (string-to-number (match-string 3))))
661 (cond
662 ((equal spec "table-cell:p")
663 (let ((style-name (org-odt-get-paragraph-style-for-table-cell r c)))
664 (replace-match style-name t t)))
665 ((equal spec "table-cell:style-name")
666 (let ((style-name (org-odt-get-style-name-for-table-cell r c)))
667 (replace-match style-name t t)))))))
669 (goto-char (point-max))
670 (org-lparse-insert-tag "</table:table>"))
672 (defun org-odt-begin-table-rowgroup (&optional is-header-row)
673 (when org-lparse-table-rowgrp-open
674 (org-lparse-end 'TABLE-ROWGROUP))
675 (org-lparse-insert-tag (if is-header-row
676 "<table:table-header-rows>"
677 "<table:table-rows>"))
678 (setq org-lparse-table-rowgrp-open t)
679 (setq org-lparse-table-cur-rowgrp-is-hdr is-header-row))
681 (defun org-odt-end-table-rowgroup ()
682 (when org-lparse-table-rowgrp-open
683 (setq org-lparse-table-rowgrp-open nil)
684 (org-lparse-insert-tag
685 (if org-lparse-table-cur-rowgrp-is-hdr
686 "</table:table-header-rows>" "</table:table-rows>"))))
688 (defun org-odt-format-table-row (row)
689 (org-odt-format-tags
690 '("<table:table-row>" . "</table:table-row>") row))
692 (defun org-odt-get-style-name-for-table-cell (r c)
693 (concat
694 "OrgTblCell"
695 (cond
696 ((= r 0) "T")
697 ((eq (cdr (assoc r org-lparse-table-rowgrp-info)) :start) "T")
698 (t ""))
699 (when (= r org-lparse-table-rownum) "B")
700 (cond
701 ((= c 0) "")
702 ((or (memq (nth c org-table-colgroup-info) '(:start :startend))
703 (memq (nth (1- c) org-table-colgroup-info) '(:end :startend))) "L")
704 (t ""))))
706 (defun org-odt-get-paragraph-style-for-table-cell (r c)
707 (capitalize (aref org-lparse-table-colalign-vector c)))
709 (defun org-odt-format-table-cell (data r c)
710 (if (not org-lparse-table-is-styled)
711 (org-odt-format-tags
712 '("<table:table-cell>" . "</table:table-cell>")
713 (org-odt-format-stylized-paragraph
714 (cond
715 (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
716 ((and (= c 0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS))
717 "OrgTableHeading")
718 (t "OrgTableContents"))
719 data))
720 (let* ((style-name-cookie
721 (format "@@table-cell:style-name@@%03d@@%03d@@" r c))
722 (paragraph-style-cookie
723 (concat
724 (cond
725 (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
726 ((and (= c 0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS))
727 "OrgTableHeading")
728 (t "OrgTableContents"))
729 (format "@@table-cell:p@@%03d@@%03d@@" r c))))
730 (org-odt-format-tags
731 '("<table:table-cell table:style-name=\"%s\">" .
732 "</table:table-cell>")
733 (org-odt-format-stylized-paragraph paragraph-style-cookie data)
734 style-name-cookie))))
736 (defun org-odt-begin-footnote-definition (n)
737 (org-lparse-begin-paragraph 'footnote))
739 (defun org-odt-end-footnote-definition (n)
740 (org-lparse-end-paragraph))
742 (defun org-odt-begin-toc (lang-specific-heading)
743 (insert
744 (format "
745 <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
746 <text:table-of-content-source text:outline-level=\"10\">
747 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
748 " lang-specific-heading))
750 (loop for level from 1 upto 10
751 do (insert (format
753 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
754 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
755 <text:index-entry-chapter/>
756 <text:index-entry-text/>
757 <text:index-entry-link-end/>
758 </text:table-of-content-entry-template>
759 " level level)))
761 (insert
762 (format "
763 </text:table-of-content-source>
765 <text:index-body>
766 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
767 <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
768 </text:index-title>
769 " lang-specific-heading)))
771 (defun org-odt-end-toc ()
772 (insert "
773 </text:index-body>
774 </text:table-of-content>
777 (defun org-odt-format-toc-entry (snumber todo headline tags href)
778 (setq headline (concat
779 (and org-export-with-section-numbers
780 (concat snumber ". "))
781 headline
782 (and tags
783 (concat
784 (org-lparse-format 'SPACES 3)
785 (org-lparse-format 'FONTIFY tags "tag")))))
786 (when todo
787 (setq headline (org-lparse-format 'FONTIFY headline "todo")))
789 (let ((org-odt-suppress-xref t))
790 (org-odt-format-link headline (concat "#" href))))
792 (defun org-odt-format-toc-item (toc-entry level org-last-level)
793 (let ((style (format "Contents_20_%d"
794 (+ level (or (org-lparse-get 'TOPLEVEL-HLEVEL) 1) -1))))
795 (insert "\n" (org-odt-format-stylized-paragraph style toc-entry) "\n")))
797 ;; Following variable is let bound during 'ORG-LINK callback. See
798 ;; org-html.el
799 (defvar org-lparse-link-description-is-image nil)
800 (defun org-odt-format-link (desc href &optional attr)
801 (cond
802 ((and (= (string-to-char href) ?#) (not org-odt-suppress-xref))
803 (setq href (concat org-export-odt-bookmark-prefix (substring href 1)))
804 (org-odt-format-tags
805 '("<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"%s\">" .
806 "</text:bookmark-ref>")
807 desc href))
808 (org-lparse-link-description-is-image
809 (org-odt-format-tags
810 '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</draw:a>")
811 desc href (or attr "")))
813 (org-odt-format-tags
814 '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</text:a>")
815 desc href (or attr "")))))
817 (defun org-odt-format-spaces (n)
818 (cond
819 ((= n 1) " ")
820 ((> n 1) (concat
821 " " (org-odt-format-tags "<text:s text:c=\"%d\"/>" "" (1- n))))
822 (t "")))
824 (defun org-odt-format-tabs (&optional n)
825 (let ((tab "<text:tab/>")
826 (n (or n 1)))
827 (insert tab)))
829 (defun org-odt-format-line-break ()
830 (org-odt-format-tags "<text:line-break/>" ""))
832 (defun org-odt-format-horizontal-line ()
833 (org-odt-format-stylized-paragraph 'horizontal-line ""))
835 (defun org-odt-format-line (line)
836 (case org-lparse-dyn-current-environment
837 (fixedwidth (concat
838 (org-odt-format-stylized-paragraph
839 'fixedwidth (org-odt-fill-tabs-and-spaces
840 (org-xml-encode-plain-text line))) "\n"))
841 (t (concat line "\n"))))
843 (defun org-odt-format-comment (fmt &rest args)
844 (let ((comment (apply 'format fmt args)))
845 (format "\n<!-- %s -->\n" comment)))
847 (defun org-odt-format-org-entity (wd)
848 (org-entity-get-representation wd 'utf8))
850 (defun org-odt-fill-tabs-and-spaces (line)
851 (replace-regexp-in-string
852 "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
853 (cond
854 ((string= s "\t") (org-odt-format-tabs))
855 (t (org-odt-format-spaces (length s))))) line))
857 (defcustom org-export-odt-use-htmlfontify t
858 "Specify whether or not source blocks need to be fontified.
859 Turn this option on if you want to colorize the source code
860 blocks in the exported file. For colorization to work, you need
861 to make available an enhanced version of `htmlfontify' library."
862 :type 'boolean
863 :group 'org-export-odt)
865 (defun org-odt-format-source-code-or-example-plain
866 (lines lang caption textareap cols rows num cont rpllbl fmt)
867 "Format source or example blocks much like fixedwidth blocks.
868 Use this when `org-export-odt-use-htmlfontify' option is turned
869 off."
870 (setq lines (org-export-number-lines (org-xml-encode-plain-text-lines lines)
871 0 0 num cont rpllbl fmt))
872 (mapconcat
873 (lambda (line)
874 (org-odt-format-stylized-paragraph
875 'fixedwidth (org-odt-fill-tabs-and-spaces line)))
876 (org-split-string lines "[\r\n]") "\n"))
878 (defvar org-src-block-paragraph-format
879 "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
880 <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
881 <style:background-image/>
882 </style:paragraph-properties>
883 <style:text-properties fo:color=\"%s\"/>
884 </style:style>"
885 "Custom paragraph style for colorized source and example blocks.
886 This style is much the same as that of \"OrgFixedWidthBlock\"
887 except that the foreground and background colors are set
888 according to the default face identified by the `htmlfontify'.")
890 (defun org-odt-hfy-face-to-css (fn)
891 "Create custom style for face FN.
892 When FN is the default face, use it's foreground and background
893 properties to create \"OrgSrcBlock\" paragraph style. Otherwise
894 use it's color attribute to create a character style whose name
895 is obtained from FN. Currently all attributes of FN other than
896 color are ignored.
898 The style name for a face FN is derived using the following
899 operations on the face name in that order - de-dash, CamelCase
900 and prefix with \"OrgSrc\". For example,
901 `font-lock-function-name-face' is associated with
902 \"OrgSrcFontLockFunctionNameFace\"."
903 (let* ((css-list (hfy-face-to-style fn))
904 (style-name ((lambda (fn)
905 (concat "OrgSrc"
906 (mapconcat
907 'capitalize (split-string
908 (hfy-face-or-def-to-name fn) "-")
909 ""))) fn))
910 (color-val (cdr (assoc "color" css-list)))
911 (background-color-val (cdr (assoc "background" css-list)))
912 (style (and org-export-odt-create-custom-styles-for-srcblocks
913 (cond
914 ((eq fn 'default)
915 (format org-src-block-paragraph-format
916 background-color-val color-val))
918 (format
920 <style:style style:name=\"%s\" style:family=\"text\">
921 <style:text-properties fo:color=\"%s\"/>
922 </style:style>" style-name color-val))))))
923 (cons style-name style)))
925 (defcustom org-export-odt-create-custom-styles-for-srcblocks t
926 "Whether custom styles for colorized source blocks be automatically created.
927 When this option is turned on, the exporter creates custom styles
928 for source blocks based on the advice of `htmlfontify'. Creation
929 of custom styles happen as part of `org-odt-hfy-face-to-css'.
931 When this option is turned off exporter does not create such
932 styles.
934 Use the latter option if you do not want the custom styles to be
935 based on your current display settings. It is necessary that the
936 styles.xml already contains needed styles for colorizing to work.
938 This variable is effective only if
939 `org-export-odt-use-htmlfontify' is turned on."
940 :group 'org-export-odt
941 :type 'boolean)
943 (defun org-odt-insert-custom-styles-for-srcblocks (styles)
944 "Save STYLES used for colorizing of source blocks.
945 Update styles.xml with styles that were collected as part of
946 `org-odt-hfy-face-to-css' callbacks."
947 (when styles
948 (with-current-buffer
949 (find-file-noselect (expand-file-name "styles.xml") t)
950 (goto-char (point-min))
951 (when (re-search-forward "</office:styles>" nil t)
952 (goto-char (match-beginning 0))
953 (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n")))))
955 (defun org-odt-format-source-code-or-example-colored
956 (lines lang caption textareap cols rows num cont rpllbl fmt)
957 "Format source or example blocks using `htmlfontify-string'.
958 Use this routine when `org-export-odt-use-htmlfontify' option is
959 turned on."
960 (let* ((lang-m (and lang (or (cdr (assoc lang org-src-lang-modes)) lang)))
961 (mode (and lang-m (intern (concat (if (symbolp lang-m)
962 (symbol-name lang-m)
963 lang-m) "-mode"))))
964 (org-inhibit-startup t)
965 (org-startup-folded nil)
966 (lines (with-temp-buffer
967 (insert lines)
968 (if (functionp mode) (funcall mode) (fundamental-mode))
969 (font-lock-fontify-buffer)
970 (buffer-string)))
971 (hfy-html-quote-regex "\\([<\"&> ]\\)")
972 (hfy-html-quote-map '(("\"" "&quot;")
973 ("<" "&lt;")
974 ("&" "&amp;")
975 (">" "&gt;")
976 (" " "<text:s/>")
977 (" " "<text:tab/>")))
978 (hfy-face-to-css 'org-odt-hfy-face-to-css)
979 (hfy-optimisations-1 (copy-seq hfy-optimisations))
980 (hfy-optimisations (add-to-list 'hfy-optimisations-1
981 'body-text-only))
982 (hfy-begin-span-handler
983 (lambda (style text-block text-id text-begins-block-p)
984 (insert (format "<text:span text:style-name=\"%s\">" style))))
985 (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
986 (mapconcat
987 (lambda (line)
988 (org-odt-format-stylized-paragraph 'src (htmlfontify-string line)))
989 (org-split-string lines "[\r\n]") "\n")))
991 (defun org-odt-format-source-code-or-example (lines lang caption textareap
992 cols rows num cont
993 rpllbl fmt)
994 "Format source or example blocks for export.
995 Use `org-odt-format-source-code-or-example-plain' or
996 `org-odt-format-source-code-or-example-colored' depending on the
997 value of `org-export-odt-use-htmlfontify."
998 (funcall
999 (if (and org-export-odt-use-htmlfontify
1000 (or (featurep 'htmlfontify) (require 'htmlfontify))
1001 (fboundp 'htmlfontify-string))
1002 'org-odt-format-source-code-or-example-colored
1003 'org-odt-format-source-code-or-example-plain)
1004 lines lang caption textareap cols rows num cont rpllbl fmt))
1006 (defun org-xml-encode-plain-text-lines (rtn)
1007 (mapconcat 'org-xml-encode-plain-text (org-split-string rtn "[\r\n]") "\n"))
1009 (defun org-odt-remap-stylenames (style-name)
1011 (cdr (assoc style-name '(("timestamp-wrapper" . "OrgTimestampWrapper")
1012 ("timestamp" . "OrgTimestamp")
1013 ("timestamp-kwd" . "OrgTimestampKeyword")
1014 ("tag" . "OrgTag")
1015 ("todo" . "OrgTodo")
1016 ("done" . "OrgDone")
1017 ("target" . "OrgTarget"))))
1018 style-name))
1020 (defun org-odt-format-fontify (text style &optional id)
1021 (let* ((style-name
1022 (cond
1023 ((stringp style)
1024 (org-odt-remap-stylenames style))
1025 ((symbolp style)
1026 (org-odt-get-style-name-for-entity 'character style))
1027 ((listp style)
1028 (assert (< 1 (length style)))
1029 (let ((parent-style (pop style)))
1030 (mapconcat (lambda (s)
1031 ;; (assert (stringp s) t)
1032 (org-odt-remap-stylenames s)) style "")
1033 (org-odt-remap-stylenames parent-style)))
1034 (t (error "Don't how to handle style %s" style)))))
1035 (org-odt-format-tags
1036 '("<text:span text:style-name=\"%s\">" . "</text:span>")
1037 text style-name)))
1039 (defun org-odt-relocate-relative-path (path dir)
1040 (if (file-name-absolute-p path) path
1041 (file-relative-name (expand-file-name path dir)
1042 (expand-file-name "eyecandy" dir))))
1044 (defun org-odt-format-inline-image (thefile)
1045 (let* ((thelink (if (file-name-absolute-p thefile) thefile
1046 (org-xml-format-href
1047 (org-odt-relocate-relative-path
1048 thefile org-current-export-file))))
1049 (href
1050 (org-odt-format-tags
1051 "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
1052 (if org-export-odt-embed-images
1053 (org-odt-copy-image-file thefile) thelink))))
1054 (org-export-odt-format-image thefile href)))
1056 (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
1057 descp)
1058 "Make an HTML link.
1059 OPT-PLIST is an options list.
1060 TYPE is the device-type of the link (THIS://foo.html)
1061 PATH is the path of the link (http://THIS#locationx)
1062 FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
1063 DESC is the link description, if any.
1064 ATTR is a string of other attributes of the a element.
1065 MAY-INLINE-P allows inlining it as an image."
1067 (declare (special org-lparse-par-open))
1068 (save-match-data
1069 (let* ((may-inline-p
1070 (and (member type-1 '("http" "https" "file"))
1071 (org-lparse-should-inline-p path descp)
1072 (not fragment)))
1073 (type (if (equal type-1 "id") "file" type-1))
1074 (filename path)
1075 (thefile path))
1077 (cond
1078 ;; check for inlined images
1079 ((and (member type '("file"))
1080 (not fragment)
1081 (org-file-image-p
1082 filename org-odt-export-inline-image-extensions)
1083 (or (eq t org-odt-export-inline-images)
1084 (and org-odt-export-inline-images (not descp))))
1086 ;; (when (and (string= type "file") (file-name-absolute-p path))
1087 ;; (setq thefile (concat "file://" (expand-file-name path))))
1088 ;; (setq thefile (org-xml-format-href thefile))
1089 ;; (org-export-html-format-image thefile)
1090 (org-odt-format-inline-image thefile))
1092 (when (string= type "file")
1093 (setq thefile
1094 (cond
1095 ((file-name-absolute-p path)
1096 (concat "file://" (expand-file-name path)))
1097 (t (org-odt-relocate-relative-path
1098 thefile org-current-export-file)))))
1100 (when (and (member type '("" "http" "https" "file" "coderef"))
1101 fragment)
1102 (setq thefile (concat thefile "#" fragment)))
1104 (setq thefile (org-xml-format-href thefile))
1106 (when (not (member type '("" "file" "coderef")))
1107 (setq thefile (concat type ":" thefile)))
1109 (let ((org-odt-suppress-xref (string= type "coderef")))
1110 (org-odt-format-link
1111 (org-xml-format-desc desc) thefile attr)))))))
1113 (defun org-odt-format-heading (text level &optional id)
1114 (let* ((text (if id (org-odt-format-target text id) text)))
1115 (org-odt-format-tags
1116 '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
1117 "</text:h>") text level level)))
1119 (defun org-odt-format-headline (title extra-targets tags
1120 &optional snumber level)
1121 (concat
1122 (org-lparse-format 'EXTRA-TARGETS extra-targets)
1124 ;; No need to generate section numbers. They are auto-generated by
1125 ;; the application
1127 ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
1128 title
1129 (and tags (concat (org-lparse-format 'SPACES 3)
1130 (org-lparse-format 'ORG-TAGS tags)))))
1132 (defun org-odt-format-anchor (text name &optional class)
1133 (org-odt-format-target text name))
1135 (defun org-odt-format-bookmark (text id)
1136 (if id
1137 (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id)
1138 text))
1140 (defun org-odt-format-target (text id)
1141 (let ((name (concat org-export-odt-bookmark-prefix id)))
1142 (concat
1143 (and id (org-odt-format-tags
1144 "<text:bookmark-start text:name=\"%s\"/>" "" name))
1145 (org-odt-format-bookmark text id)
1146 (and id (org-odt-format-tags
1147 "<text:bookmark-end text:name=\"%s\"/>" "" name)))))
1149 (defun org-odt-format-footnote (n def)
1150 (let ((id (concat "fn" n))
1151 (note-class "footnote")
1152 (par-style "Footnote"))
1153 (org-odt-format-tags
1154 '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
1155 "</text:note>")
1156 (concat
1157 (org-odt-format-tags
1158 '("<text:note-citation>" . "</text:note-citation>")
1160 (org-odt-format-tags
1161 '("<text:note-body>" . "</text:note-body>")
1162 def))
1163 id note-class)))
1165 (defun org-odt-format-footnote-reference (n def refcnt)
1166 (if (= refcnt 1)
1167 (org-odt-format-footnote n def)
1168 (org-odt-format-footnote-ref n)))
1170 (defun org-odt-format-footnote-ref (n)
1171 (let ((note-class "footnote")
1172 (ref-format "text")
1173 (ref-name (concat "fn" n)))
1174 (org-odt-format-tags
1175 '("<text:span text:style-name=\"%s\">" . "</text:span>")
1176 (org-odt-format-tags
1177 '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" . "</text:note-ref>")
1178 n note-class ref-format ref-name)
1179 "OrgSuperscript")))
1181 (defun org-odt-get-image-name (file-name)
1182 (require 'sha1)
1183 (file-relative-name
1184 (expand-file-name
1185 (concat (sha1 file-name) "." (file-name-extension file-name)) "Pictures")))
1187 (defun org-export-odt-format-image (src href
1188 ;; par-open
1190 "Create image tag with source and attributes."
1191 (save-match-data
1193 (let (embed-as caption attr label attr-plist size width height)
1195 (cond
1196 ((string-match "^ltxpng/" src)
1197 ;; FIXME: Anyway the latex src can be embedded as an
1198 ;; annotation
1200 ;; (org-find-text-property-in-string 'org-latex-src src)
1201 (setq caption nil attr nil label nil embed-as 'character))
1204 (setq caption (org-find-text-property-in-string 'org-caption src)
1205 caption (and caption (org-xml-format-desc caption))
1206 attr (org-find-text-property-in-string 'org-attributes src)
1207 label (org-find-text-property-in-string 'org-label src)
1208 embed-as 'paragraph)))
1210 (setq attr-plist (when attr (read attr)))
1211 (setq size (org-odt-image-size-from-file
1212 src (plist-get attr-plist :width)
1213 (plist-get attr-plist :height)
1214 (plist-get attr-plist :scale) nil embed-as))
1216 (org-export-odt-do-format-image embed-as caption attr label
1217 size href))))
1218 (defun org-odt-format-textbox (text style)
1219 (let ((draw-frame-pair
1220 '("<draw:frame draw:style-name=\"%s\"
1221 text:anchor-type=\"paragraph\"
1222 style:rel-width=\"100%%\"
1223 draw:z-index=\"0\">" . "</draw:frame>")))
1224 (org-odt-format-tags
1225 draw-frame-pair
1226 (org-odt-format-tags
1227 '("<draw:text-box fo:min-height=\"%dcm\">" . "</draw:text-box>")
1228 text 0) style)))
1230 (defun org-odt-format-inlinetask (heading content
1231 &optional todo priority tags)
1232 (org-odt-format-stylized-paragraph
1233 nil (org-odt-format-textbox
1234 (concat (org-odt-format-stylized-paragraph
1235 "OrgInlineTaskHeading"
1236 (org-lparse-format
1237 'HEADLINE (concat (org-lparse-format-todo todo) " " heading)
1238 nil tags))
1239 content) "OrgInlineTaskFrame")))
1241 (defun org-export-odt-do-format-image (embed-as caption attr label
1242 size href)
1243 "Create image tag with source and attributes."
1244 (save-match-data
1245 (let ((width (car size)) (height (cdr size))
1246 (draw-frame-pair
1247 '("<draw:frame draw:style-name=\"%s\"
1248 text:anchor-type=\"%s\"
1249 draw:z-index=\"%d\" %s>" . "</draw:frame>")))
1250 (cond
1251 ((and (not caption) (not label))
1252 (let (style-name anchor-type)
1253 (cond
1254 ((eq embed-as 'paragraph)
1255 (setq style-name "OrgGraphicsParagraph" anchor-type "paragraph"))
1256 ((eq embed-as 'character)
1257 (setq style-name "OrgGraphicsBaseline" anchor-type "as-char")))
1258 (org-odt-format-tags
1259 draw-frame-pair href style-name anchor-type 0
1260 (org-odt-image-attrs-from-size width height))))
1263 (concat
1264 ;; (when par-open (org-odt-close-par))
1265 (org-odt-format-tags
1266 draw-frame-pair
1267 (org-odt-format-tags
1268 '("<draw:text-box fo:min-height=\"%dcm\">" . "</draw:text-box>")
1269 (org-odt-format-stylized-paragraph
1270 'illustration
1271 (concat
1272 (let ((extra " style:rel-width=\"100%\" style:rel-height=\"scale\""))
1273 (org-odt-format-tags
1274 draw-frame-pair href "OrgGraphicsParagraphContent" "paragraph" 2
1275 (concat (org-odt-image-attrs-from-size width height) extra)))
1276 (org-odt-format-entity-caption label caption)))
1277 height)
1278 "OrgFrame" "paragraph" 1
1279 (org-odt-image-attrs-from-size width))
1281 ;; (when par-open (org-odt-open-par))
1282 ))))))
1284 ;; xml files generated on-the-fly
1285 (defconst org-export-odt-save-list
1286 '("mimetype" "META-INF/manifest.xml" "content.xml" "meta.xml" "styles.xml"))
1288 ;; xml files that are copied
1289 (defconst org-export-odt-nosave-list '())
1291 ;; xml files that contribute to the final odt file
1292 (defvar org-export-odt-file-list nil)
1294 (defconst org-export-odt-manifest-lines
1295 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1296 "<manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">"
1297 "<manifest:file-entry manifest:media-type=\"application/vnd.oasis.opendocument.text\" manifest:version=\"1.2\" manifest:full-path=\"/\"/>"
1298 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"content.xml\"/>"
1299 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"styles.xml\"/>"
1300 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"meta.xml\"/>"
1301 "<manifest:file-entry manifest:media-type=\"\" manifest:full-path=\"Pictures/\"/>") . ("</manifest:manifest>")))
1303 (defconst org-export-odt-meta-lines
1304 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1305 "<office:document-meta"
1306 " xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\""
1307 " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
1308 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\""
1309 " xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\""
1310 " xmlns:ooo=\"http://openoffice.org/2004/office\" "
1311 " office:version=\"1.2\">"
1312 " <office:meta>") . (" </office:meta>" "</office:document-meta>")))
1314 (defun org-odt-copy-image-file (path &optional target-file)
1315 "Returns the internal name of the file"
1316 (let* ((image-type (file-name-extension path))
1317 (media-type (format "image/%s" image-type))
1318 (src-file (expand-file-name
1319 path (file-name-directory org-current-export-file)))
1320 (target-file (or target-file (org-odt-get-image-name src-file)))
1321 ;; FIXME
1322 (body-only nil))
1324 (when (not org-lparse-to-buffer)
1325 (message "Embedding %s as %s ..."
1326 (substring-no-properties path) target-file)
1327 (copy-file src-file target-file 'overwrite)
1328 (org-odt-update-manifest-file media-type target-file)
1329 (push target-file org-export-odt-file-list)) target-file))
1331 (defun org-odt-image-attrs-from-size (&optional width height)
1332 (concat
1333 (when width (format "svg:width=\"%0.2fcm\"" width))
1335 (when height (format "svg:height=\"%0.2fcm\"" height))))
1337 (defvar org-export-odt-image-size-probe-method
1338 '(emacs imagemagick force)
1339 "Ordered list of methods by for determining size of an embedded
1340 image.")
1342 (defvar org-export-odt-default-image-sizes-alist
1343 '(("character" . (5 . 0.4))
1344 ("paragraph" . (5 . 5)))
1345 "Hardcoded image dimensions one for each of the anchor
1346 methods.")
1348 (defun org-odt-do-image-size (probe-method file &optional dpi anchor-type)
1349 (setq dpi (or dpi org-export-odt-pixels-per-inch))
1350 (setq anchor-type (or anchor-type "paragraph"))
1351 (flet ((size-in-cms (size-in-pixels)
1352 (flet ((pixels-to-cms (pixels)
1353 (let* ((cms-per-inch 2.54)
1354 (inches (/ pixels dpi)))
1355 (* cms-per-inch inches))))
1356 (and size-in-pixels
1357 (cons (pixels-to-cms (car size-in-pixels))
1358 (pixels-to-cms (cdr size-in-pixels)))))))
1359 (case probe-method
1360 (emacs
1361 (size-in-cms (ignore-errors (image-size (create-image file) 'pixels))))
1362 (imagemagick
1363 (size-in-cms
1364 (let ((dim (shell-command-to-string
1365 (format "identify -format \"%%w:%%h\" \"%s\"" file))))
1366 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
1367 (cons (string-to-number (match-string 1 dim))
1368 (string-to-number (match-string 2 dim)))))))
1370 (cdr (assoc-string anchor-type
1371 org-export-odt-default-image-sizes-alist))))))
1373 (defun org-odt-image-size-from-file (file &optional user-width
1374 user-height scale dpi embed-as)
1375 (unless (file-name-absolute-p file)
1376 (setq file (expand-file-name
1377 file (file-name-directory org-current-export-file))))
1378 (let* (size width height)
1379 (unless (and user-height user-width)
1380 (loop for probe-method in org-export-odt-image-size-probe-method
1381 until size
1382 do (setq size (org-odt-do-image-size
1383 probe-method file dpi embed-as)))
1384 (or size (error "Cannot determine Image size. Aborting ..."))
1385 (setq width (car size) height (cdr size)))
1386 (cond
1387 (scale
1388 (setq width (* width scale) height (* height scale)))
1389 ((and user-height user-width)
1390 (setq width user-width height user-height))
1391 (user-height
1392 (setq width (* user-height (/ width height)) height user-height))
1393 (user-width
1394 (setq height (* user-width (/ height width)) width user-width))
1395 (t (ignore)))
1396 (cons width height)))
1398 (defvar org-odt-default-entity "Illustration")
1399 (defun org-odt-format-entity-caption (label caption &optional default-entity)
1400 (if (not label) (or caption "")
1401 (let* ((label-components (org-odt-parse-label label))
1402 (entity (car label-components))
1403 (seqno (cdr label-components))
1404 (caption (and caption (concat ": " caption))))
1405 (unless seqno
1406 (setq seqno label
1407 entity (or default-entity org-odt-default-entity)))
1408 (concat
1409 entity " "
1410 (org-odt-format-tags
1411 '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" . "</text:sequence>")
1412 seqno label entity entity)
1413 caption))))
1415 (defun org-odt-format-tags (tag text &rest args)
1416 (let ((prefix (when org-lparse-encode-pending "@"))
1417 (suffix (when org-lparse-encode-pending "@")))
1418 (apply 'org-lparse-format-tags tag text prefix suffix args)))
1420 (defun org-odt-init-outfile (filename)
1421 (unless (executable-find "zip")
1422 ;; Not at all OSes ship with zip by default
1423 (error "Executable \"zip\" needed for creating OpenDocument files. Aborting."))
1425 (let* ((outdir (make-temp-file org-export-odt-tmpdir-prefix t))
1426 (mimetype-file (expand-file-name "mimetype" outdir))
1427 (content-file (expand-file-name "content.xml" outdir))
1428 (manifest-file (expand-file-name "META-INF/manifest.xml" outdir))
1429 (meta-file (expand-file-name "meta.xml" outdir))
1430 (styles-file (expand-file-name "styles.xml" outdir))
1431 (pictures-dir (expand-file-name "Pictures" outdir))
1432 (body-only nil))
1434 ;; content file
1435 (with-current-buffer (find-file-noselect content-file t)
1436 (erase-buffer))
1438 ;; FIXME: How to factor in body-only here
1439 (unless body-only
1440 ;; manifest file
1441 (make-directory (file-name-directory manifest-file))
1442 (with-current-buffer (find-file-noselect manifest-file t)
1443 (erase-buffer)
1444 (insert (mapconcat 'identity (car org-export-odt-manifest-lines) "\n"))
1445 (insert "\n")
1446 (save-excursion
1447 (insert (mapconcat 'identity (cdr org-export-odt-manifest-lines) "\n"))))
1449 ;; meta file
1450 (with-current-buffer (find-file-noselect meta-file t)
1451 (erase-buffer)
1452 (insert (mapconcat 'identity (car org-export-odt-meta-lines) "\n"))
1453 (insert "\n")
1454 (save-excursion
1455 (insert (mapconcat 'identity (cdr org-export-odt-meta-lines) "\n"))))
1457 ;; mimetype
1458 (with-current-buffer (find-file-noselect mimetype-file t)
1459 (insert "application/vnd.oasis.opendocument.text"))
1461 ;; styles file
1462 ;; (copy-file org-export-odt-styles-file styles-file t)
1464 ;; Pictures dir
1465 (make-directory pictures-dir)
1467 ;; initialize list of files that contribute to the odt file
1468 (setq org-export-odt-file-list
1469 (append org-export-odt-save-list org-export-odt-nosave-list)))
1470 content-file))
1472 (defconst org-odt-manifest-file-entry-tag
1473 "<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"/>")
1475 (defcustom org-export-odt-prettify-xml nil
1476 "Specify whether or not the xml output should be prettified.
1477 When this option is turned on, `indent-region' is run on all
1478 component xml buffers before they are saved. Turn this off for
1479 regular use. Turn this on if you need to examine the xml
1480 visually."
1481 :group 'org-export-odt
1482 :type 'boolean)
1484 (defun org-odt-save-as-outfile (target opt-plist)
1485 ;; write meta file
1486 (org-odt-update-meta-file opt-plist)
1488 ;; write styles file
1489 (org-odt-copy-styles-file)
1491 ;; Update styles.xml - take care of outline numbering
1492 (with-current-buffer
1493 (find-file-noselect (expand-file-name "styles.xml") t)
1494 ;; Don't make automatic backup of styles.xml file. This setting
1495 ;; prevents the backedup styles.xml file from being zipped in to
1496 ;; odt file. This is more of a hackish fix. Better alternative
1497 ;; would be to fix the zip command so that the output odt file
1498 ;; includes only the needed files and excludes any auto-generated
1499 ;; extra files like backups and auto-saves etc etc. Note that
1500 ;; currently the zip command zips up the entire temp directory so
1501 ;; that any auto-generated files created under the hood ends up in
1502 ;; the resulting odt file.
1503 (set (make-local-variable 'backup-inhibited) t)
1505 ;; Import local setting of `org-export-with-section-numbers'
1506 (org-lparse-bind-local-variables opt-plist)
1507 (org-odt-configure-outline-numbering
1508 (if org-export-with-section-numbers org-export-headline-levels 0)))
1510 ;; Write custom stlyes for source blocks
1511 (org-odt-insert-custom-styles-for-srcblocks
1512 (mapconcat
1513 (lambda (style)
1514 (format " %s\n" (cddr style)))
1515 hfy-user-sheet-assoc ""))
1517 (let ((zipdir default-directory))
1518 (message "Switching to directory %s" (expand-file-name zipdir))
1520 ;; save all xml files
1521 (mapc (lambda (file)
1522 (with-current-buffer
1523 (find-file-noselect (expand-file-name file) t)
1524 ;; prettify output if needed
1525 (when org-export-odt-prettify-xml
1526 (indent-region (point-min) (point-max)))
1527 (save-buffer)))
1528 org-export-odt-save-list)
1530 (let* ((target-name (file-name-nondirectory target))
1531 (target-dir (file-name-directory target))
1532 (cmds `(("zip" "-mX0" ,target-name "mimetype")
1533 ("zip" "-rmTq" ,target-name "."))))
1534 (when (file-exists-p target)
1535 ;; FIXME: If the file is locked this throws a cryptic error
1536 (delete-file target))
1538 (let ((coding-system-for-write 'no-conversion) exitcode)
1539 (message "Creating odt file...")
1540 (mapc
1541 (lambda (cmd)
1542 (message "Running %s" (mapconcat 'identity cmd " "))
1543 (setq exitcode
1544 (apply 'call-process (car cmd) nil nil nil (cdr cmd)))
1545 (or (zerop exitcode)
1546 (error "Unable to create odt file (%S)" exitcode)))
1547 cmds))
1549 ;; move the file from outdir to target-dir
1550 (rename-file target-name target-dir)
1552 ;; kill all xml buffers
1553 (mapc (lambda (file)
1554 (kill-buffer
1555 (find-file-noselect (expand-file-name file zipdir) t)))
1556 org-export-odt-save-list)
1558 (delete-directory zipdir)))
1560 (message "Created %s" target)
1561 (set-buffer (find-file-noselect target t)))
1563 (defun org-odt-format-date (date)
1564 (let ((warning-msg
1565 "OpenDocument files require that dates be in ISO-8601 format. Please review your DATE options for compatibility."))
1566 ;; If the user is not careful with the date specification, an
1567 ;; invalid meta.xml will be emitted.
1569 ;; For now honor user's diktat and let him off with a warning
1570 ;; message. This is OK as LibreOffice (and possibly other
1571 ;; apps) doesn't deem this deviation as critical and continue
1572 ;; to load the file.
1574 ;; FIXME: Surely there a better way to handle this. Revisit this
1575 ;; later.
1576 (cond
1577 ((and date (string-match "%" date))
1578 ;; Honor user's diktat. See comments above
1579 (org-lparse-warn warning-msg)
1580 (format-time-string date))
1581 (date
1582 ;; Honor user's diktat. See comments above
1583 (org-lparse-warn warning-msg)
1584 date)
1586 ;; ISO 8601 format
1587 (format-time-string "%Y-%m-%dT%T%:z")))))
1589 (defun org-odt-update-meta-file (opt-plist)
1590 (with-current-buffer
1591 (find-file-noselect (expand-file-name "meta.xml") t)
1592 (let ((date (org-odt-format-date (plist-get opt-plist :date)))
1593 (author (or (plist-get opt-plist :author) ""))
1594 (email (plist-get opt-plist :email))
1595 (keywords (plist-get opt-plist :keywords))
1596 (description (plist-get opt-plist :description))
1597 (title (plist-get opt-plist :title)))
1599 (insert
1600 "\n"
1601 (org-odt-format-tags '("<dc:creator>" . "</dc:creator>") author)
1602 (org-odt-format-tags
1603 '("\n<meta:initial-creator>" . "</meta:initial-creator>") author)
1604 (org-odt-format-tags '("\n<dc:date>" . "</dc:date>") date)
1605 (org-odt-format-tags
1606 '("\n<meta:creation-date>" . "</meta:creation-date>") date)
1607 (org-odt-format-tags '("\n<meta:generator>" . "</meta:generator>")
1608 (when org-export-creator-info
1609 (format "Org-%s/Emacs-%s"
1610 org-version emacs-version)))
1611 (org-odt-format-tags '("\n<meta:keyword>" . "</meta:keyword>") keywords)
1612 (org-odt-format-tags '("\n<dc:subject>" . "</dc:subject>") description)
1613 (org-odt-format-tags '("\n<dc:title>" . "</dc:title>") title)
1614 "\n"))))
1616 (defun org-odt-update-manifest-file (media-type full-path)
1617 (with-current-buffer
1618 (find-file-noselect (expand-file-name "META-INF/manifest.xml") t)
1619 (insert (format org-odt-manifest-file-entry-tag media-type full-path))))
1621 (defun org-odt-finalize-outfile ()
1622 (org-odt-delete-empty-paragraphs))
1624 (defun org-odt-delete-empty-paragraphs ()
1625 (goto-char (point-min))
1626 (let ((open "<text:p[^>]*>")
1627 (close "</text:p>"))
1628 (while (re-search-forward (format "%s[ \r\n\t]*%s" open close) nil t)
1629 (replace-match ""))))
1631 (defun org-odt-get (what &optional opt-plist)
1632 (case what
1633 (BACKEND 'odt)
1634 (EXPORT-DIR (org-export-directory :html opt-plist))
1635 (FILE-NAME-EXTENSION "odt")
1636 (EXPORT-BUFFER-NAME "*Org ODT Export*")
1637 (ENTITY-CONTROL org-odt-entity-control-callbacks-alist)
1638 (ENTITY-FORMAT org-odt-entity-format-callbacks-alist)
1639 (INIT-METHOD 'org-odt-init-outfile)
1640 (FINAL-METHOD 'org-odt-finalize-outfile)
1641 (SAVE-METHOD 'org-odt-save-as-outfile)
1642 ;; (OTHER-BACKENDS) ; see note in `org-xhtml-get'
1643 ;; (CONVERT-METHOD) ; see note in `org-xhtml-get'
1644 (TOPLEVEL-HLEVEL 1)
1645 (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps)
1646 (INLINE-IMAGES 'maybe)
1647 (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
1648 (PLAIN-TEXT-MAP '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
1649 (TABLE-FIRST-COLUMN-AS-LABELS nil)
1650 (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY "," 'superscript))
1651 (CODING-SYSTEM-FOR-WRITE 'utf-8)
1652 (CODING-SYSTEM-FOR-SAVE 'utf-8)
1653 (t (error "Unknown property: %s" what))))
1655 (defun org-odt-parse-label (label)
1656 (save-match-data
1657 (if (not (string-match "\\`[a-zA-Z]+:\\(.+\\)" label))
1658 (cons label nil)
1659 (cons
1660 (capitalize (substring label 0 (1- (match-beginning 1))))
1661 (substring label (match-beginning 1))))))
1663 (defvar org-lparse-latex-fragment-fallback) ; set by org-do-lparse
1664 (defun org-export-odt-preprocess (parameters)
1665 "Convert LaTeX fragments to images."
1666 (when (and org-current-export-file
1667 (plist-get parameters :LaTeX-fragments))
1668 (org-format-latex
1669 (concat "ltxpng/" (file-name-sans-extension
1670 (file-name-nondirectory
1671 org-current-export-file)))
1672 org-current-export-dir nil "Creating LaTeX image %s"
1673 nil nil
1674 (cond
1675 ((eq (plist-get parameters :LaTeX-fragments) 'verbatim) 'verbatim)
1676 ;; Investigate MathToWeb for converting TeX equations to MathML
1677 ;; See http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01755.html
1678 ((or (eq (plist-get parameters :LaTeX-fragments) 'mathjax )
1679 (eq (plist-get parameters :LaTeX-fragments) t ))
1680 (org-lparse-warn
1681 (concat
1682 "Use of MathJax is incompatible with ODT exporter. "
1683 (format "Using %S instead." org-lparse-latex-fragment-fallback)))
1684 org-lparse-latex-fragment-fallback)
1685 ((eq (plist-get parameters :LaTeX-fragments) 'dvipng ) 'dvipng)
1686 (t nil))))
1687 (goto-char (point-min))
1688 (let (label label-components category value pretty-label)
1689 (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t)
1690 (org-if-unprotected-at (match-beginning 1)
1691 (setq label (match-string 1)
1692 label-components (org-odt-parse-label label)
1693 category (car label-components)
1694 value (cdr label-components)
1695 pretty-label (if value (concat category " " value) label))
1696 (replace-match
1697 (let ((org-lparse-encode-pending t))
1698 (org-odt-format-tags
1699 '("<text:sequence-ref text:reference-format=\"category-and-value\" text:ref-name=\"%s\">"
1700 . "</text:sequence-ref>") pretty-label label)) t t)))))
1702 (declare-function archive-zip-extract "arc-mode.el" (archive name))
1703 (defun org-odt-zip-extract-one (archive member &optional target)
1704 (require 'arc-mode)
1705 (let* ((target (or target default-directory))
1706 (archive (expand-file-name archive))
1707 (archive-zip-extract
1708 (list "unzip" "-qq" "-o" "-d" target))
1709 exit-code command-output)
1710 (setq command-output
1711 (with-temp-buffer
1712 (setq exit-code (archive-zip-extract archive member))
1713 (buffer-string)))
1714 (unless (zerop exit-code)
1715 (message command-output)
1716 (error "Extraction failed"))))
1718 (defun org-odt-zip-extract (archive members &optional target)
1719 (when (atom members) (setq members (list members)))
1720 (mapc (lambda (member)
1721 (org-odt-zip-extract-one archive member target))
1722 members))
1724 (defun org-odt-copy-styles-file (&optional styles-file)
1725 ;; Non-availability of styles.xml is not a critical error. For now
1726 ;; throw an error purely for aesthetic reasons.
1727 (setq styles-file (or styles-file
1728 org-export-odt-styles-file
1729 (expand-file-name "styles/OrgOdtStyles.xml"
1730 org-odt-data-dir)
1731 (error "org-odt: Missing styles file?")))
1732 (cond
1733 ((listp styles-file)
1734 (let ((archive (nth 0 styles-file))
1735 (members (nth 1 styles-file)))
1736 (org-odt-zip-extract archive members)
1737 (mapc
1738 (lambda (member)
1739 (when (org-file-image-p member)
1740 (let* ((image-type (file-name-extension member))
1741 (media-type (format "image/%s" image-type)))
1742 (org-odt-update-manifest-file media-type member))))
1743 members)))
1744 ((and (stringp styles-file) (file-exists-p styles-file))
1745 (let ((styles-file-type (file-name-extension styles-file)))
1746 (cond
1747 ((string= styles-file-type "xml")
1748 (copy-file styles-file "styles.xml" t))
1749 ((member styles-file-type '("odt" "ott"))
1750 (org-odt-zip-extract styles-file "styles.xml")))))
1752 (error (format "Invalid specification of styles.xml file: %S"
1753 org-export-odt-styles-file)))))
1755 (defvar org-export-odt-factory-settings
1756 "d4328fb9d1b6cb211d4320ff546829f26700dc5e"
1757 "SHA1 hash of OrgOdtStyles.xml.")
1759 (defun org-odt-configure-outline-numbering (level)
1760 "Outline numbering is retained only upto LEVEL.
1761 To disable outline numbering pass a LEVEL of 0."
1762 (goto-char (point-min))
1763 (let ((regex
1764 "<text:outline-level-style\\(.*\\)text:level=\"\\([^\"]*\\)\"\\(.*\\)>")
1765 (replacement
1766 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1767 (while (re-search-forward regex nil t)
1768 (when (> (string-to-number (match-string 2)) level)
1769 (replace-match replacement t nil))))
1770 (save-buffer 0))
1772 (provide 'org-odt)
1774 ;;; org-odt.el ends here