org-odt.el: Support for captioning and labelling of equation images
[org-mode.git] / contrib / lisp / org-odt.el
blob9c81e842318f39166e4f8d3224ca657728c344f3
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)
625 (org-lparse-end-list 'description)
626 (org-lparse-end-list-item))
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-format-table-cell (data r c)
712 (if (not org-lparse-table-is-styled)
713 (org-odt-format-tags
714 '("<table:table-cell>" . "</table:table-cell>")
715 (org-odt-format-stylized-paragraph
716 (cond
717 (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
718 ((and (= c 0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS))
719 "OrgTableHeading")
720 (t "OrgTableContents"))
721 data))
722 (let* ((style-name-cookie
723 (format "@@table-cell:style-name@@%03d@@%03d@@" r c))
724 (paragraph-style-cookie
725 (concat
726 (cond
727 (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
728 ((and (= c 0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS))
729 "OrgTableHeading")
730 (t "OrgTableContents"))
731 (format "@@table-cell:p@@%03d@@%03d@@" r c))))
732 (org-odt-format-tags
733 '("<table:table-cell table:style-name=\"%s\">" .
734 "</table:table-cell>")
735 (org-odt-format-stylized-paragraph paragraph-style-cookie data)
736 style-name-cookie))))
738 (defun org-odt-begin-footnote-definition (n)
739 (org-lparse-begin-paragraph 'footnote))
741 (defun org-odt-end-footnote-definition (n)
742 (org-lparse-end-paragraph))
744 (defun org-odt-begin-toc (lang-specific-heading)
745 (insert
746 (format "
747 <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
748 <text:table-of-content-source text:outline-level=\"10\">
749 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
750 " lang-specific-heading))
752 (loop for level from 1 upto 10
753 do (insert (format
755 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
756 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
757 <text:index-entry-chapter/>
758 <text:index-entry-text/>
759 <text:index-entry-link-end/>
760 </text:table-of-content-entry-template>
761 " level level)))
763 (insert
764 (format "
765 </text:table-of-content-source>
767 <text:index-body>
768 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
769 <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
770 </text:index-title>
771 " lang-specific-heading)))
773 (defun org-odt-end-toc ()
774 (insert "
775 </text:index-body>
776 </text:table-of-content>
779 (defun org-odt-format-toc-entry (snumber todo headline tags href)
780 (setq headline (concat
781 (and org-export-with-section-numbers
782 (concat snumber ". "))
783 headline
784 (and tags
785 (concat
786 (org-lparse-format 'SPACES 3)
787 (org-lparse-format 'FONTIFY tags "tag")))))
788 (when todo
789 (setq headline (org-lparse-format 'FONTIFY headline "todo")))
791 (let ((org-odt-suppress-xref t))
792 (org-odt-format-link headline (concat "#" href))))
794 (defun org-odt-format-toc-item (toc-entry level org-last-level)
795 (let ((style (format "Contents_20_%d"
796 (+ level (or (org-lparse-get 'TOPLEVEL-HLEVEL) 1) -1))))
797 (insert "\n" (org-odt-format-stylized-paragraph style toc-entry) "\n")))
799 ;; Following variable is let bound during 'ORG-LINK callback. See
800 ;; org-html.el
801 (defvar org-lparse-link-description-is-image nil)
802 (defun org-odt-format-link (desc href &optional attr)
803 (cond
804 ((and (= (string-to-char href) ?#) (not org-odt-suppress-xref))
805 (setq href (concat org-export-odt-bookmark-prefix (substring href 1)))
806 (org-odt-format-tags
807 '("<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"%s\">" .
808 "</text:bookmark-ref>")
809 desc href))
810 (org-lparse-link-description-is-image
811 (org-odt-format-tags
812 '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</draw:a>")
813 desc href (or attr "")))
815 (org-odt-format-tags
816 '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</text:a>")
817 desc href (or attr "")))))
819 (defun org-odt-format-spaces (n)
820 (cond
821 ((= n 1) " ")
822 ((> n 1) (concat
823 " " (org-odt-format-tags "<text:s text:c=\"%d\"/>" "" (1- n))))
824 (t "")))
826 (defun org-odt-format-tabs (&optional n)
827 (let ((tab "<text:tab/>")
828 (n (or n 1)))
829 (insert tab)))
831 (defun org-odt-format-line-break ()
832 (org-odt-format-tags "<text:line-break/>" ""))
834 (defun org-odt-format-horizontal-line ()
835 (org-odt-format-stylized-paragraph 'horizontal-line ""))
837 (defun org-odt-format-line (line)
838 (case org-lparse-dyn-current-environment
839 (fixedwidth (concat
840 (org-odt-format-stylized-paragraph
841 'fixedwidth (org-odt-fill-tabs-and-spaces
842 (org-xml-encode-plain-text line))) "\n"))
843 (t (concat line "\n"))))
845 (defun org-odt-format-comment (fmt &rest args)
846 (let ((comment (apply 'format fmt args)))
847 (format "\n<!-- %s -->\n" comment)))
849 (defun org-odt-format-org-entity (wd)
850 (org-entity-get-representation wd 'utf8))
852 (defun org-odt-fill-tabs-and-spaces (line)
853 (replace-regexp-in-string
854 "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
855 (cond
856 ((string= s "\t") (org-odt-format-tabs))
857 (t (org-odt-format-spaces (length s))))) line))
859 (defcustom org-export-odt-use-htmlfontify t
860 "Specify whether or not source blocks need to be fontified.
861 Turn this option on if you want to colorize the source code
862 blocks in the exported file. For colorization to work, you need
863 to make available an enhanced version of `htmlfontify' library."
864 :type 'boolean
865 :group 'org-export-odt)
867 (defun org-odt-format-source-code-or-example-plain
868 (lines lang caption textareap cols rows num cont rpllbl fmt)
869 "Format source or example blocks much like fixedwidth blocks.
870 Use this when `org-export-odt-use-htmlfontify' option is turned
871 off."
872 (setq lines (org-export-number-lines (org-xml-encode-plain-text-lines lines)
873 0 0 num cont rpllbl fmt))
874 (mapconcat
875 (lambda (line)
876 (org-odt-format-stylized-paragraph
877 'fixedwidth (org-odt-fill-tabs-and-spaces line)))
878 (org-split-string lines "[\r\n]") "\n"))
880 (defvar org-src-block-paragraph-format
881 "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
882 <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
883 <style:background-image/>
884 </style:paragraph-properties>
885 <style:text-properties fo:color=\"%s\"/>
886 </style:style>"
887 "Custom paragraph style for colorized source and example blocks.
888 This style is much the same as that of \"OrgFixedWidthBlock\"
889 except that the foreground and background colors are set
890 according to the default face identified by the `htmlfontify'.")
892 (defun org-odt-hfy-face-to-css (fn)
893 "Create custom style for face FN.
894 When FN is the default face, use it's foreground and background
895 properties to create \"OrgSrcBlock\" paragraph style. Otherwise
896 use it's color attribute to create a character style whose name
897 is obtained from FN. Currently all attributes of FN other than
898 color are ignored.
900 The style name for a face FN is derived using the following
901 operations on the face name in that order - de-dash, CamelCase
902 and prefix with \"OrgSrc\". For example,
903 `font-lock-function-name-face' is associated with
904 \"OrgSrcFontLockFunctionNameFace\"."
905 (let* ((css-list (hfy-face-to-style fn))
906 (style-name ((lambda (fn)
907 (concat "OrgSrc"
908 (mapconcat
909 'capitalize (split-string
910 (hfy-face-or-def-to-name fn) "-")
911 ""))) fn))
912 (color-val (cdr (assoc "color" css-list)))
913 (background-color-val (cdr (assoc "background" css-list)))
914 (style (and org-export-odt-create-custom-styles-for-srcblocks
915 (cond
916 ((eq fn 'default)
917 (format org-src-block-paragraph-format
918 background-color-val color-val))
920 (format
922 <style:style style:name=\"%s\" style:family=\"text\">
923 <style:text-properties fo:color=\"%s\"/>
924 </style:style>" style-name color-val))))))
925 (cons style-name style)))
927 (defcustom org-export-odt-create-custom-styles-for-srcblocks t
928 "Whether custom styles for colorized source blocks be automatically created.
929 When this option is turned on, the exporter creates custom styles
930 for source blocks based on the advice of `htmlfontify'. Creation
931 of custom styles happen as part of `org-odt-hfy-face-to-css'.
933 When this option is turned off exporter does not create such
934 styles.
936 Use the latter option if you do not want the custom styles to be
937 based on your current display settings. It is necessary that the
938 styles.xml already contains needed styles for colorizing to work.
940 This variable is effective only if
941 `org-export-odt-use-htmlfontify' is turned on."
942 :group 'org-export-odt
943 :type 'boolean)
945 (defun org-odt-insert-custom-styles-for-srcblocks (styles)
946 "Save STYLES used for colorizing of source blocks.
947 Update styles.xml with styles that were collected as part of
948 `org-odt-hfy-face-to-css' callbacks."
949 (when styles
950 (with-current-buffer
951 (find-file-noselect (expand-file-name "styles.xml") t)
952 (goto-char (point-min))
953 (when (re-search-forward "</office:styles>" nil t)
954 (goto-char (match-beginning 0))
955 (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n")))))
957 (defun org-odt-format-source-code-or-example-colored
958 (lines lang caption textareap cols rows num cont rpllbl fmt)
959 "Format source or example blocks using `htmlfontify-string'.
960 Use this routine when `org-export-odt-use-htmlfontify' option is
961 turned on."
962 (let* ((lang-m (and lang (or (cdr (assoc lang org-src-lang-modes)) lang)))
963 (mode (and lang-m (intern (concat (if (symbolp lang-m)
964 (symbol-name lang-m)
965 lang-m) "-mode"))))
966 (org-inhibit-startup t)
967 (org-startup-folded nil)
968 (lines (with-temp-buffer
969 (insert lines)
970 (if (functionp mode) (funcall mode) (fundamental-mode))
971 (font-lock-fontify-buffer)
972 (buffer-string)))
973 (hfy-html-quote-regex "\\([<\"&> ]\\)")
974 (hfy-html-quote-map '(("\"" "&quot;")
975 ("<" "&lt;")
976 ("&" "&amp;")
977 (">" "&gt;")
978 (" " "<text:s/>")
979 (" " "<text:tab/>")))
980 (hfy-face-to-css 'org-odt-hfy-face-to-css)
981 (hfy-optimisations-1 (copy-seq hfy-optimisations))
982 (hfy-optimisations (add-to-list 'hfy-optimisations-1
983 'body-text-only))
984 (hfy-begin-span-handler
985 (lambda (style text-block text-id text-begins-block-p)
986 (insert (format "<text:span text:style-name=\"%s\">" style))))
987 (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
988 (when (fboundp 'htmlfontify-string)
989 (mapconcat
990 (lambda (line)
991 (org-odt-format-stylized-paragraph 'src (htmlfontify-string line)))
992 (org-split-string lines "[\r\n]") "\n"))))
994 (defun org-odt-format-source-code-or-example (lines lang caption textareap
995 cols rows num cont
996 rpllbl fmt)
997 "Format source or example blocks for export.
998 Use `org-odt-format-source-code-or-example-plain' or
999 `org-odt-format-source-code-or-example-colored' depending on the
1000 value of `org-export-odt-use-htmlfontify."
1001 (funcall
1002 (if (and org-export-odt-use-htmlfontify
1003 (or (featurep 'htmlfontify) (require 'htmlfontify))
1004 (fboundp 'htmlfontify-string))
1005 'org-odt-format-source-code-or-example-colored
1006 'org-odt-format-source-code-or-example-plain)
1007 lines lang caption textareap cols rows num cont rpllbl fmt))
1009 (defun org-xml-encode-plain-text-lines (rtn)
1010 (mapconcat 'org-xml-encode-plain-text (org-split-string rtn "[\r\n]") "\n"))
1012 (defun org-odt-remap-stylenames (style-name)
1014 (cdr (assoc style-name '(("timestamp-wrapper" . "OrgTimestampWrapper")
1015 ("timestamp" . "OrgTimestamp")
1016 ("timestamp-kwd" . "OrgTimestampKeyword")
1017 ("tag" . "OrgTag")
1018 ("todo" . "OrgTodo")
1019 ("done" . "OrgDone")
1020 ("target" . "OrgTarget"))))
1021 style-name))
1023 (defun org-odt-format-fontify (text style &optional id)
1024 (let* ((style-name
1025 (cond
1026 ((stringp style)
1027 (org-odt-remap-stylenames style))
1028 ((symbolp style)
1029 (org-odt-get-style-name-for-entity 'character style))
1030 ((listp style)
1031 (assert (< 1 (length style)))
1032 (let ((parent-style (pop style)))
1033 (mapconcat (lambda (s)
1034 ;; (assert (stringp s) t)
1035 (org-odt-remap-stylenames s)) style "")
1036 (org-odt-remap-stylenames parent-style)))
1037 (t (error "Don't how to handle style %s" style)))))
1038 (org-odt-format-tags
1039 '("<text:span text:style-name=\"%s\">" . "</text:span>")
1040 text style-name)))
1042 (defun org-odt-relocate-relative-path (path dir)
1043 (if (file-name-absolute-p path) path
1044 (file-relative-name (expand-file-name path dir)
1045 (expand-file-name "eyecandy" dir))))
1047 (defun org-odt-format-inline-image (thefile)
1048 (let* ((thelink (if (file-name-absolute-p thefile) thefile
1049 (org-xml-format-href
1050 (org-odt-relocate-relative-path
1051 thefile org-current-export-file))))
1052 (href
1053 (org-odt-format-tags
1054 "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
1055 (if org-export-odt-embed-images
1056 (org-odt-copy-image-file thefile) thelink))))
1057 (org-export-odt-format-image thefile href)))
1059 (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
1060 descp)
1061 "Make an HTML link.
1062 OPT-PLIST is an options list.
1063 TYPE is the device-type of the link (THIS://foo.html)
1064 PATH is the path of the link (http://THIS#locationx)
1065 FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
1066 DESC is the link description, if any.
1067 ATTR is a string of other attributes of the a element.
1068 MAY-INLINE-P allows inlining it as an image."
1070 (declare (special org-lparse-par-open))
1071 (save-match-data
1072 (let* ((may-inline-p
1073 (and (member type-1 '("http" "https" "file"))
1074 (org-lparse-should-inline-p path descp)
1075 (not fragment)))
1076 (type (if (equal type-1 "id") "file" type-1))
1077 (filename path)
1078 (thefile path))
1080 (cond
1081 ;; check for inlined images
1082 ((and (member type '("file"))
1083 (not fragment)
1084 (org-file-image-p
1085 filename org-odt-export-inline-image-extensions)
1086 (or (eq t org-odt-export-inline-images)
1087 (and org-odt-export-inline-images (not descp))))
1089 ;; (when (and (string= type "file") (file-name-absolute-p path))
1090 ;; (setq thefile (concat "file://" (expand-file-name path))))
1091 ;; (setq thefile (org-xml-format-href thefile))
1092 ;; (org-export-html-format-image thefile)
1093 (org-odt-format-inline-image thefile))
1095 (when (string= type "file")
1096 (setq thefile
1097 (cond
1098 ((file-name-absolute-p path)
1099 (concat "file://" (expand-file-name path)))
1100 (t (org-odt-relocate-relative-path
1101 thefile org-current-export-file)))))
1103 (when (and (member type '("" "http" "https" "file" "coderef"))
1104 fragment)
1105 (setq thefile (concat thefile "#" fragment)))
1107 (setq thefile (org-xml-format-href thefile))
1109 (when (not (member type '("" "file" "coderef")))
1110 (setq thefile (concat type ":" thefile)))
1112 (let ((org-odt-suppress-xref (string= type "coderef")))
1113 (org-odt-format-link
1114 (org-xml-format-desc desc) thefile attr)))))))
1116 (defun org-odt-format-heading (text level &optional id)
1117 (let* ((text (if id (org-odt-format-target text id) text)))
1118 (org-odt-format-tags
1119 '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
1120 "</text:h>") text level level)))
1122 (defun org-odt-format-headline (title extra-targets tags
1123 &optional snumber level)
1124 (concat
1125 (org-lparse-format 'EXTRA-TARGETS extra-targets)
1127 ;; No need to generate section numbers. They are auto-generated by
1128 ;; the application
1130 ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
1131 title
1132 (and tags (concat (org-lparse-format 'SPACES 3)
1133 (org-lparse-format 'ORG-TAGS tags)))))
1135 (defun org-odt-format-anchor (text name &optional class)
1136 (org-odt-format-target text name))
1138 (defun org-odt-format-bookmark (text id)
1139 (if id
1140 (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id)
1141 text))
1143 (defun org-odt-format-target (text id)
1144 (let ((name (concat org-export-odt-bookmark-prefix id)))
1145 (concat
1146 (and id (org-odt-format-tags
1147 "<text:bookmark-start text:name=\"%s\"/>" "" name))
1148 (org-odt-format-bookmark text id)
1149 (and id (org-odt-format-tags
1150 "<text:bookmark-end text:name=\"%s\"/>" "" name)))))
1152 (defun org-odt-format-footnote (n def)
1153 (let ((id (concat "fn" n))
1154 (note-class "footnote")
1155 (par-style "Footnote"))
1156 (org-odt-format-tags
1157 '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
1158 "</text:note>")
1159 (concat
1160 (org-odt-format-tags
1161 '("<text:note-citation>" . "</text:note-citation>")
1163 (org-odt-format-tags
1164 '("<text:note-body>" . "</text:note-body>")
1165 def))
1166 id note-class)))
1168 (defun org-odt-format-footnote-reference (n def refcnt)
1169 (if (= refcnt 1)
1170 (org-odt-format-footnote n def)
1171 (org-odt-format-footnote-ref n)))
1173 (defun org-odt-format-footnote-ref (n)
1174 (let ((note-class "footnote")
1175 (ref-format "text")
1176 (ref-name (concat "fn" n)))
1177 (org-odt-format-tags
1178 '("<text:span text:style-name=\"%s\">" . "</text:span>")
1179 (org-odt-format-tags
1180 '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" . "</text:note-ref>")
1181 n note-class ref-format ref-name)
1182 "OrgSuperscript")))
1184 (defun org-odt-get-image-name (file-name)
1185 (require 'sha1)
1186 (file-relative-name
1187 (expand-file-name
1188 (concat (sha1 file-name) "." (file-name-extension file-name)) "Pictures")))
1190 (defun org-export-odt-format-image (src href)
1191 "Create image tag with source and attributes."
1192 (save-match-data
1193 (let* ((caption (org-find-text-property-in-string 'org-caption src))
1194 (caption (and caption (org-xml-format-desc caption)))
1195 (attr (org-find-text-property-in-string 'org-attributes src))
1196 (label (org-find-text-property-in-string 'org-label src))
1197 (embed-as (if (string-match "^ltxpng/" src) 'character 'paragraph))
1198 (attr-plist (when attr (read attr)))
1199 (size (org-odt-image-size-from-file
1200 src (plist-get attr-plist :width)
1201 (plist-get attr-plist :height)
1202 (plist-get attr-plist :scale) nil embed-as)))
1203 (org-export-odt-do-format-image
1204 embed-as caption attr label size href))))
1206 (defun org-odt-format-textbox (text style)
1207 (let ((draw-frame-pair
1208 '("<draw:frame draw:style-name=\"%s\"
1209 text:anchor-type=\"paragraph\"
1210 style:rel-width=\"100%%\"
1211 draw:z-index=\"0\">" . "</draw:frame>")))
1212 (org-odt-format-tags
1213 draw-frame-pair
1214 (org-odt-format-tags
1215 '("<draw:text-box fo:min-height=\"%dcm\">" . "</draw:text-box>")
1216 text 0) style)))
1218 (defun org-odt-format-inlinetask (heading content
1219 &optional todo priority tags)
1220 (org-odt-format-stylized-paragraph
1221 nil (org-odt-format-textbox
1222 (concat (org-odt-format-stylized-paragraph
1223 "OrgInlineTaskHeading"
1224 (org-lparse-format
1225 'HEADLINE (concat (org-lparse-format-todo todo) " " heading)
1226 nil tags))
1227 content) "OrgInlineTaskFrame")))
1229 (defun org-export-odt-do-format-image (embed-as caption attr label
1230 size href)
1231 "Create image tag with source and attributes."
1232 (save-match-data
1233 (let ((width (car size)) (height (cdr size))
1234 (draw-frame-pair
1235 '("<draw:frame draw:style-name=\"%s\"
1236 text:anchor-type=\"%s\"
1237 draw:z-index=\"%d\" %s>" . "</draw:frame>")))
1238 (cond
1239 ((and (not caption) (not label))
1240 (let (style-name anchor-type)
1241 (cond
1242 ((eq embed-as 'paragraph)
1243 (setq style-name "OrgGraphicsParagraph" anchor-type "paragraph"))
1244 ((eq embed-as 'character)
1245 (setq style-name "OrgGraphicsBaseline" anchor-type "as-char")))
1246 (org-odt-format-tags
1247 draw-frame-pair href style-name anchor-type 0
1248 (org-odt-image-attrs-from-size width height))))
1251 (concat
1252 ;; (when par-open (org-odt-close-par))
1253 (org-odt-format-tags
1254 draw-frame-pair
1255 (org-odt-format-tags
1256 '("<draw:text-box fo:min-height=\"%dcm\">" . "</draw:text-box>")
1257 (org-odt-format-stylized-paragraph
1258 'illustration
1259 (concat
1260 (let ((extra " style:rel-width=\"100%\" style:rel-height=\"scale\""))
1261 (org-odt-format-tags
1262 draw-frame-pair href "OrgGraphicsParagraphContent" "paragraph" 2
1263 (concat (org-odt-image-attrs-from-size width height) extra)))
1264 (org-odt-format-entity-caption label caption)))
1265 height)
1266 "OrgFrame" "paragraph" 1
1267 (org-odt-image-attrs-from-size width))
1269 ;; (when par-open (org-odt-open-par))
1270 ))))))
1272 ;; xml files generated on-the-fly
1273 (defconst org-export-odt-save-list
1274 '("mimetype" "META-INF/manifest.xml" "content.xml" "meta.xml" "styles.xml"))
1276 ;; xml files that are copied
1277 (defconst org-export-odt-nosave-list '())
1279 ;; xml files that contribute to the final odt file
1280 (defvar org-export-odt-file-list nil)
1282 (defconst org-export-odt-manifest-lines
1283 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1284 "<manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">"
1285 "<manifest:file-entry manifest:media-type=\"application/vnd.oasis.opendocument.text\" manifest:version=\"1.2\" manifest:full-path=\"/\"/>"
1286 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"content.xml\"/>"
1287 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"styles.xml\"/>"
1288 "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"meta.xml\"/>"
1289 "<manifest:file-entry manifest:media-type=\"\" manifest:full-path=\"Pictures/\"/>") . ("</manifest:manifest>")))
1291 (defconst org-export-odt-meta-lines
1292 '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1293 "<office:document-meta"
1294 " xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\""
1295 " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
1296 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\""
1297 " xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\""
1298 " xmlns:ooo=\"http://openoffice.org/2004/office\" "
1299 " office:version=\"1.2\">"
1300 " <office:meta>") . (" </office:meta>" "</office:document-meta>")))
1302 (defun org-odt-copy-image-file (path &optional target-file)
1303 "Returns the internal name of the file"
1304 (let* ((image-type (file-name-extension path))
1305 (media-type (format "image/%s" image-type))
1306 (src-file (expand-file-name
1307 path (file-name-directory org-current-export-file)))
1308 (target-file (or target-file (org-odt-get-image-name src-file)))
1309 ;; FIXME
1310 (body-only nil))
1312 (when (not org-lparse-to-buffer)
1313 (message "Embedding %s as %s ..."
1314 (substring-no-properties path) target-file)
1315 (copy-file src-file target-file 'overwrite)
1316 (org-odt-update-manifest-file media-type target-file)
1317 (push target-file org-export-odt-file-list)) target-file))
1319 (defun org-odt-image-attrs-from-size (&optional width height)
1320 (concat
1321 (when width (format "svg:width=\"%0.2fcm\"" width))
1323 (when height (format "svg:height=\"%0.2fcm\"" height))))
1325 (defvar org-export-odt-image-size-probe-method
1326 '(emacs imagemagick force)
1327 "Ordered list of methods by for determining size of an embedded
1328 image.")
1330 (defvar org-export-odt-default-image-sizes-alist
1331 '(("character" . (5 . 0.4))
1332 ("paragraph" . (5 . 5)))
1333 "Hardcoded image dimensions one for each of the anchor
1334 methods.")
1336 (defun org-odt-do-image-size (probe-method file &optional dpi anchor-type)
1337 (setq dpi (or dpi org-export-odt-pixels-per-inch))
1338 (setq anchor-type (or anchor-type "paragraph"))
1339 (flet ((size-in-cms (size-in-pixels)
1340 (flet ((pixels-to-cms (pixels)
1341 (let* ((cms-per-inch 2.54)
1342 (inches (/ pixels dpi)))
1343 (* cms-per-inch inches))))
1344 (and size-in-pixels
1345 (cons (pixels-to-cms (car size-in-pixels))
1346 (pixels-to-cms (cdr size-in-pixels)))))))
1347 (case probe-method
1348 (emacs
1349 (size-in-cms (ignore-errors (image-size (create-image file) 'pixels))))
1350 (imagemagick
1351 (size-in-cms
1352 (let ((dim (shell-command-to-string
1353 (format "identify -format \"%%w:%%h\" \"%s\"" file))))
1354 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
1355 (cons (string-to-number (match-string 1 dim))
1356 (string-to-number (match-string 2 dim)))))))
1358 (cdr (assoc-string anchor-type
1359 org-export-odt-default-image-sizes-alist))))))
1361 (defun org-odt-image-size-from-file (file &optional user-width
1362 user-height scale dpi embed-as)
1363 (unless (file-name-absolute-p file)
1364 (setq file (expand-file-name
1365 file (file-name-directory org-current-export-file))))
1366 (let* (size width height)
1367 (unless (and user-height user-width)
1368 (loop for probe-method in org-export-odt-image-size-probe-method
1369 until size
1370 do (setq size (org-odt-do-image-size
1371 probe-method file dpi embed-as)))
1372 (or size (error "Cannot determine Image size. Aborting ..."))
1373 (setq width (car size) height (cdr size)))
1374 (cond
1375 (scale
1376 (setq width (* width scale) height (* height scale)))
1377 ((and user-height user-width)
1378 (setq width user-width height user-height))
1379 (user-height
1380 (setq width (* user-height (/ width height)) height user-height))
1381 (user-width
1382 (setq height (* user-width (/ height width)) width user-width))
1383 (t (ignore)))
1384 (cons width height)))
1386 (defvar org-odt-default-entity "Illustration")
1387 (defun org-odt-format-entity-caption (label caption &optional default-entity)
1388 (if (not label) (or caption "")
1389 (let* ((label-components (org-odt-parse-label label))
1390 (entity (car label-components))
1391 (seqno (cdr label-components))
1392 (caption (and caption (concat ": " caption))))
1393 (unless seqno
1394 (setq seqno label
1395 entity (or default-entity org-odt-default-entity)))
1396 (concat
1397 entity " "
1398 (org-odt-format-tags
1399 '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" . "</text:sequence>")
1400 seqno label entity entity)
1401 caption))))
1403 (defun org-odt-format-tags (tag text &rest args)
1404 (let ((prefix (when org-lparse-encode-pending "@"))
1405 (suffix (when org-lparse-encode-pending "@")))
1406 (apply 'org-lparse-format-tags tag text prefix suffix args)))
1408 (defun org-odt-init-outfile (filename)
1409 (unless (executable-find "zip")
1410 ;; Not at all OSes ship with zip by default
1411 (error "Executable \"zip\" needed for creating OpenDocument files. Aborting."))
1413 (let* ((outdir (make-temp-file org-export-odt-tmpdir-prefix t))
1414 (mimetype-file (expand-file-name "mimetype" outdir))
1415 (content-file (expand-file-name "content.xml" outdir))
1416 (manifest-file (expand-file-name "META-INF/manifest.xml" outdir))
1417 (meta-file (expand-file-name "meta.xml" outdir))
1418 (styles-file (expand-file-name "styles.xml" outdir))
1419 (pictures-dir (expand-file-name "Pictures" outdir))
1420 (body-only nil))
1422 ;; content file
1423 (with-current-buffer (find-file-noselect content-file t)
1424 (erase-buffer))
1426 ;; FIXME: How to factor in body-only here
1427 (unless body-only
1428 ;; manifest file
1429 (make-directory (file-name-directory manifest-file))
1430 (with-current-buffer (find-file-noselect manifest-file t)
1431 (erase-buffer)
1432 (insert (mapconcat 'identity (car org-export-odt-manifest-lines) "\n"))
1433 (insert "\n")
1434 (save-excursion
1435 (insert (mapconcat 'identity (cdr org-export-odt-manifest-lines) "\n"))))
1437 ;; meta file
1438 (with-current-buffer (find-file-noselect meta-file t)
1439 (erase-buffer)
1440 (insert (mapconcat 'identity (car org-export-odt-meta-lines) "\n"))
1441 (insert "\n")
1442 (save-excursion
1443 (insert (mapconcat 'identity (cdr org-export-odt-meta-lines) "\n"))))
1445 ;; mimetype
1446 (with-current-buffer (find-file-noselect mimetype-file t)
1447 (insert "application/vnd.oasis.opendocument.text"))
1449 ;; styles file
1450 ;; (copy-file org-export-odt-styles-file styles-file t)
1452 ;; Pictures dir
1453 (make-directory pictures-dir)
1455 ;; initialize list of files that contribute to the odt file
1456 (setq org-export-odt-file-list
1457 (append org-export-odt-save-list org-export-odt-nosave-list)))
1458 content-file))
1460 (defconst org-odt-manifest-file-entry-tag
1461 "<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"/>")
1463 (defcustom org-export-odt-prettify-xml nil
1464 "Specify whether or not the xml output should be prettified.
1465 When this option is turned on, `indent-region' is run on all
1466 component xml buffers before they are saved. Turn this off for
1467 regular use. Turn this on if you need to examine the xml
1468 visually."
1469 :group 'org-export-odt
1470 :type 'boolean)
1472 (defvar hfy-user-sheet-assoc) ; bound during org-do-lparse
1473 (defun org-odt-save-as-outfile (target opt-plist)
1474 ;; write meta file
1475 (org-odt-update-meta-file opt-plist)
1477 ;; write styles file
1478 (org-odt-copy-styles-file)
1480 ;; Update styles.xml - take care of outline numbering
1481 (with-current-buffer
1482 (find-file-noselect (expand-file-name "styles.xml") t)
1483 ;; Don't make automatic backup of styles.xml file. This setting
1484 ;; prevents the backedup styles.xml file from being zipped in to
1485 ;; odt file. This is more of a hackish fix. Better alternative
1486 ;; would be to fix the zip command so that the output odt file
1487 ;; includes only the needed files and excludes any auto-generated
1488 ;; extra files like backups and auto-saves etc etc. Note that
1489 ;; currently the zip command zips up the entire temp directory so
1490 ;; that any auto-generated files created under the hood ends up in
1491 ;; the resulting odt file.
1492 (set (make-local-variable 'backup-inhibited) t)
1494 ;; Import local setting of `org-export-with-section-numbers'
1495 (org-lparse-bind-local-variables opt-plist)
1496 (org-odt-configure-outline-numbering
1497 (if org-export-with-section-numbers org-export-headline-levels 0)))
1499 ;; Write custom stlyes for source blocks
1500 (org-odt-insert-custom-styles-for-srcblocks
1501 (mapconcat
1502 (lambda (style)
1503 (format " %s\n" (cddr style)))
1504 hfy-user-sheet-assoc ""))
1506 (let ((zipdir default-directory))
1507 (message "Switching to directory %s" (expand-file-name zipdir))
1509 ;; save all xml files
1510 (mapc (lambda (file)
1511 (with-current-buffer
1512 (find-file-noselect (expand-file-name file) t)
1513 ;; prettify output if needed
1514 (when org-export-odt-prettify-xml
1515 (indent-region (point-min) (point-max)))
1516 (save-buffer)))
1517 org-export-odt-save-list)
1519 (let* ((target-name (file-name-nondirectory target))
1520 (target-dir (file-name-directory target))
1521 (cmds `(("zip" "-mX0" ,target-name "mimetype")
1522 ("zip" "-rmTq" ,target-name "."))))
1523 (when (file-exists-p target)
1524 ;; FIXME: If the file is locked this throws a cryptic error
1525 (delete-file target))
1527 (let ((coding-system-for-write 'no-conversion) exitcode)
1528 (message "Creating odt file...")
1529 (mapc
1530 (lambda (cmd)
1531 (message "Running %s" (mapconcat 'identity cmd " "))
1532 (setq exitcode
1533 (apply 'call-process (car cmd) nil nil nil (cdr cmd)))
1534 (or (zerop exitcode)
1535 (error "Unable to create odt file (%S)" exitcode)))
1536 cmds))
1538 ;; move the file from outdir to target-dir
1539 (rename-file target-name target-dir)
1541 ;; kill all xml buffers
1542 (mapc (lambda (file)
1543 (kill-buffer
1544 (find-file-noselect (expand-file-name file zipdir) t)))
1545 org-export-odt-save-list)
1547 (delete-directory zipdir)))
1549 (message "Created %s" target)
1550 (set-buffer (find-file-noselect target t)))
1552 (defun org-odt-format-date (date)
1553 (let ((warning-msg
1554 "OpenDocument files require that dates be in ISO-8601 format. Please review your DATE options for compatibility."))
1555 ;; If the user is not careful with the date specification, an
1556 ;; invalid meta.xml will be emitted.
1558 ;; For now honor user's diktat and let him off with a warning
1559 ;; message. This is OK as LibreOffice (and possibly other
1560 ;; apps) doesn't deem this deviation as critical and continue
1561 ;; to load the file.
1563 ;; FIXME: Surely there a better way to handle this. Revisit this
1564 ;; later.
1565 (cond
1566 ((and date (string-match "%" date))
1567 ;; Honor user's diktat. See comments above
1568 (org-lparse-warn warning-msg)
1569 (format-time-string date))
1570 (date
1571 ;; Honor user's diktat. See comments above
1572 (org-lparse-warn warning-msg)
1573 date)
1575 ;; ISO 8601 format
1576 (format-time-string "%Y-%m-%dT%T%:z")))))
1578 (defun org-odt-update-meta-file (opt-plist)
1579 (with-current-buffer
1580 (find-file-noselect (expand-file-name "meta.xml") t)
1581 (let ((date (org-odt-format-date (plist-get opt-plist :date)))
1582 (author (or (plist-get opt-plist :author) ""))
1583 (email (plist-get opt-plist :email))
1584 (keywords (plist-get opt-plist :keywords))
1585 (description (plist-get opt-plist :description))
1586 (title (plist-get opt-plist :title)))
1588 (insert
1589 "\n"
1590 (org-odt-format-tags '("<dc:creator>" . "</dc:creator>") author)
1591 (org-odt-format-tags
1592 '("\n<meta:initial-creator>" . "</meta:initial-creator>") author)
1593 (org-odt-format-tags '("\n<dc:date>" . "</dc:date>") date)
1594 (org-odt-format-tags
1595 '("\n<meta:creation-date>" . "</meta:creation-date>") date)
1596 (org-odt-format-tags '("\n<meta:generator>" . "</meta:generator>")
1597 (when org-export-creator-info
1598 (format "Org-%s/Emacs-%s"
1599 org-version emacs-version)))
1600 (org-odt-format-tags '("\n<meta:keyword>" . "</meta:keyword>") keywords)
1601 (org-odt-format-tags '("\n<dc:subject>" . "</dc:subject>") description)
1602 (org-odt-format-tags '("\n<dc:title>" . "</dc:title>") title)
1603 "\n"))))
1605 (defun org-odt-update-manifest-file (media-type full-path)
1606 (with-current-buffer
1607 (find-file-noselect (expand-file-name "META-INF/manifest.xml") t)
1608 (insert (format org-odt-manifest-file-entry-tag media-type full-path))))
1610 (defun org-odt-finalize-outfile ()
1611 (org-odt-delete-empty-paragraphs))
1613 (defun org-odt-delete-empty-paragraphs ()
1614 (goto-char (point-min))
1615 (let ((open "<text:p[^>]*>")
1616 (close "</text:p>"))
1617 (while (re-search-forward (format "%s[ \r\n\t]*%s" open close) nil t)
1618 (replace-match ""))))
1620 (defun org-odt-get (what &optional opt-plist)
1621 (case what
1622 (BACKEND 'odt)
1623 (EXPORT-DIR (org-export-directory :html opt-plist))
1624 (FILE-NAME-EXTENSION "odt")
1625 (EXPORT-BUFFER-NAME "*Org ODT Export*")
1626 (ENTITY-CONTROL org-odt-entity-control-callbacks-alist)
1627 (ENTITY-FORMAT org-odt-entity-format-callbacks-alist)
1628 (INIT-METHOD 'org-odt-init-outfile)
1629 (FINAL-METHOD 'org-odt-finalize-outfile)
1630 (SAVE-METHOD 'org-odt-save-as-outfile)
1631 ;; (OTHER-BACKENDS) ; see note in `org-xhtml-get'
1632 ;; (CONVERT-METHOD) ; see note in `org-xhtml-get'
1633 (TOPLEVEL-HLEVEL 1)
1634 (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps)
1635 (INLINE-IMAGES 'maybe)
1636 (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
1637 (PLAIN-TEXT-MAP '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
1638 (TABLE-FIRST-COLUMN-AS-LABELS nil)
1639 (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY "," 'superscript))
1640 (CODING-SYSTEM-FOR-WRITE 'utf-8)
1641 (CODING-SYSTEM-FOR-SAVE 'utf-8)
1642 (t (error "Unknown property: %s" what))))
1644 (defun org-odt-parse-label (label)
1645 (save-match-data
1646 (if (not (string-match "\\`[a-zA-Z]+:\\(.+\\)" label))
1647 (cons label nil)
1648 (cons
1649 (capitalize (substring label 0 (1- (match-beginning 1))))
1650 (substring label (match-beginning 1))))))
1652 (defvar org-lparse-latex-fragment-fallback) ; set by org-do-lparse
1653 (defvar org-lparse-opt-plist) ; bound during org-do-lparse
1654 (defun org-export-odt-preprocess-latex-fragments ()
1655 "Convert LaTeX fragments to images."
1656 (let* ((latex-frag-opt (plist-get org-lparse-opt-plist :LaTeX-fragments))
1657 (latex-frag-opt-1 ; massage the options
1658 (or (and (member latex-frag-opt '(mathjax t))
1659 (prog1 org-lparse-latex-fragment-fallback
1660 (org-lparse-warn
1661 (concat
1662 "Use of MathJax is incompatible with ODT exporter. "
1663 (format "Using %S instead."
1664 org-lparse-latex-fragment-fallback)))))
1665 latex-frag-opt)))
1666 (when (and org-current-export-file latex-frag-opt-1)
1667 ;; Investigate MathToWeb for converting TeX equations to MathML
1668 ;; http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01755.html
1669 (org-format-latex
1670 (concat "ltxpng/" (file-name-sans-extension
1671 (file-name-nondirectory
1672 org-current-export-file)))
1673 org-current-export-dir nil "Creating LaTeX image %s"
1674 nil nil latex-frag-opt-1))))
1676 (defun org-export-odt-preprocess-label-references ()
1677 (goto-char (point-min))
1678 (let (label label-components category value pretty-label)
1679 (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t)
1680 (org-if-unprotected-at (match-beginning 1)
1681 (setq label (match-string 1)
1682 label-components (org-odt-parse-label label)
1683 category (car label-components)
1684 value (cdr label-components)
1685 pretty-label (if value (concat category " " value) label))
1686 (replace-match
1687 (let ((org-lparse-encode-pending t))
1688 (org-odt-format-tags
1689 '("<text:sequence-ref text:reference-format=\"category-and-value\" text:ref-name=\"%s\">"
1690 . "</text:sequence-ref>") pretty-label label)) t t)))))
1692 ;; process latex fragments as part of
1693 ;; `org-export-preprocess-after-blockquote-hook'. Note that this hook
1694 ;; is the one that is closest and well before the call to
1695 ;; `org-export-attach-captions-and-attributes' in
1696 ;; `org-export-preprocess-stirng'. The above arrangement permits
1697 ;; captions, labels and attributes to be attached to png images
1698 ;; generated out of latex equations.
1699 (add-hook 'org-export-preprocess-after-blockquote-hook
1700 'org-export-odt-preprocess-latex-fragments)
1702 (defun org-export-odt-preprocess (parameters)
1703 (org-export-odt-preprocess-label-references))
1705 (declare-function archive-zip-extract "arc-mode.el" (archive name))
1706 (defun org-odt-zip-extract-one (archive member &optional target)
1707 (require 'arc-mode)
1708 (let* ((target (or target default-directory))
1709 (archive (expand-file-name archive))
1710 (archive-zip-extract
1711 (list "unzip" "-qq" "-o" "-d" target))
1712 exit-code command-output)
1713 (setq command-output
1714 (with-temp-buffer
1715 (setq exit-code (archive-zip-extract archive member))
1716 (buffer-string)))
1717 (unless (zerop exit-code)
1718 (message command-output)
1719 (error "Extraction failed"))))
1721 (defun org-odt-zip-extract (archive members &optional target)
1722 (when (atom members) (setq members (list members)))
1723 (mapc (lambda (member)
1724 (org-odt-zip-extract-one archive member target))
1725 members))
1727 (defun org-odt-copy-styles-file (&optional styles-file)
1728 ;; Non-availability of styles.xml is not a critical error. For now
1729 ;; throw an error purely for aesthetic reasons.
1730 (setq styles-file (or styles-file
1731 org-export-odt-styles-file
1732 (expand-file-name "styles/OrgOdtStyles.xml"
1733 org-odt-data-dir)
1734 (error "org-odt: Missing styles file?")))
1735 (cond
1736 ((listp styles-file)
1737 (let ((archive (nth 0 styles-file))
1738 (members (nth 1 styles-file)))
1739 (org-odt-zip-extract archive members)
1740 (mapc
1741 (lambda (member)
1742 (when (org-file-image-p member)
1743 (let* ((image-type (file-name-extension member))
1744 (media-type (format "image/%s" image-type)))
1745 (org-odt-update-manifest-file media-type member))))
1746 members)))
1747 ((and (stringp styles-file) (file-exists-p styles-file))
1748 (let ((styles-file-type (file-name-extension styles-file)))
1749 (cond
1750 ((string= styles-file-type "xml")
1751 (copy-file styles-file "styles.xml" t))
1752 ((member styles-file-type '("odt" "ott"))
1753 (org-odt-zip-extract styles-file "styles.xml")))))
1755 (error (format "Invalid specification of styles.xml file: %S"
1756 org-export-odt-styles-file)))))
1758 (defvar org-export-odt-factory-settings
1759 "d4328fb9d1b6cb211d4320ff546829f26700dc5e"
1760 "SHA1 hash of OrgOdtStyles.xml.")
1762 (defun org-odt-configure-outline-numbering (level)
1763 "Outline numbering is retained only upto LEVEL.
1764 To disable outline numbering pass a LEVEL of 0."
1765 (goto-char (point-min))
1766 (let ((regex
1767 "<text:outline-level-style\\(.*\\)text:level=\"\\([^\"]*\\)\"\\(.*\\)>")
1768 (replacement
1769 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1770 (while (re-search-forward regex nil t)
1771 (when (> (string-to-number (match-string 2)) level)
1772 (replace-match replacement t nil))))
1773 (save-buffer 0))
1775 (provide 'org-odt)
1777 ;;; org-odt.el ends here