org-odt.el: Put label generation and reference under user control
[org-mode/org-jambu.git] / contrib / lisp / org-odt.el
blob874ae8a6dc91d55371d4bf6ef66e38edf61ff24d
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 (defgroup org-export-odt nil
34 "Options specific for ODT export of Org-mode files."
35 :tag "Org Export ODT"
36 :group 'org-export)
38 (defun org-odt-end-export ()
39 (org-odt-fixup-label-references)
41 ;; remove empty paragraphs
42 (goto-char (point-min))
43 (while (re-search-forward
44 "<text:p\\( text:style-name=\"Text_20_body\"\\)?>[ \r\n\t]*</text:p>"
45 nil t)
46 (replace-match ""))
47 (goto-char (point-min))
49 ;; Convert whitespace place holders
50 (goto-char (point-min))
51 (let (beg end n)
52 (while (setq beg (next-single-property-change (point) 'org-whitespace))
53 (setq n (get-text-property beg 'org-whitespace)
54 end (next-single-property-change beg 'org-whitespace))
55 (goto-char beg)
56 (delete-region beg end)
57 (insert (format "<span style=\"visibility:hidden;\">%s</span>"
58 (make-string n ?x)))))
60 ;; Remove empty lines at the beginning of the file.
61 (goto-char (point-min))
62 (when (looking-at "\\s-+\n") (replace-match ""))
64 ;; Remove display properties
65 (remove-text-properties (point-min) (point-max) '(display t)))
67 (defvar org-odt-suppress-xref nil)
68 (defconst org-export-odt-special-string-regexps
69 '(("\\\\-" . "&#x00ad;\\1") ; shy
70 ("---\\([^-]\\)" . "&#x2014;\\1") ; mdash
71 ("--\\([^-]\\)" . "&#x2013;\\1") ; ndash
72 ("\\.\\.\\." . "&#x2026;")) ; hellip
73 "Regular expressions for special string conversion.")
75 (defconst org-odt-lib-dir (file-name-directory load-file-name))
76 (defconst org-odt-data-dir
77 (let ((dir1 (expand-file-name "../odt" org-odt-lib-dir)) ; git
78 (dir2 (expand-file-name "./contrib/odt" org-odt-lib-dir))) ; elpa
79 (cond
80 ((file-directory-p dir1) dir1)
81 ((file-directory-p dir2) dir2)
82 (t (error "Cannot find factory styles file. Check package dir layout"))))
83 "Directory that holds auxiliary files used by the ODT exporter.
85 The 'styles' subdir contains the following xml files -
86 'OrgOdtStyles.xml' and 'OrgOdtContentTemplate.xml' - which are
87 used as factory settings of `org-export-odt-styles-file' and
88 `org-export-odt-content-template-file'.
90 The 'etc/schema' subdir contains rnc files for validating of
91 OpenDocument xml files.")
93 (defvar org-odt-file-extensions
94 '(("odt" . "OpenDocument Text")
95 ("ott" . "OpenDocument Text Template")
96 ("odm" . "OpenDocument Master Document")
97 ("ods" . "OpenDocument Spreadsheet")
98 ("ots" . "OpenDocument Spreadsheet Template")
99 ("odg" . "OpenDocument Drawing (Graphics)")
100 ("otg" . "OpenDocument Drawing Template")
101 ("odp" . "OpenDocument Presentation")
102 ("otp" . "OpenDocument Presentation Template")
103 ("odi" . "OpenDocument Image")
104 ("odf" . "OpenDocument Formula")
105 ("odc" . "OpenDocument Chart")
106 ("doc" . "Microsoft Text")
107 ("docx" . "Microsoft Text")
108 ("xls" . "Microsoft Spreadsheet")
109 ("xlsx" . "Microsoft Spreadsheet")
110 ("ppt" . "Microsoft Presentation")
111 ("pptx" . "Microsoft Presentation")))
113 (defvar org-odt-ms-file-extensions
114 '(("doc" . "Microsoft Text")
115 ("docx" . "Microsoft Text")
116 ("xls" . "Microsoft Spreadsheet")
117 ("xlsx" . "Microsoft Spreadsheet")
118 ("ppt" . "Microsoft Presentation")
119 ("pptx" . "Microsoft Presentation")))
121 ;; RelaxNG validation of OpenDocument xml files
122 (eval-after-load 'rng-nxml
123 '(setq rng-nxml-auto-validate-flag t))
125 (eval-after-load 'rng-loc
126 '(add-to-list 'rng-schema-locating-files
127 (expand-file-name "etc/schema/schemas.xml" org-odt-data-dir)))
129 (mapc
130 (lambda (desc)
131 ;; Let Org open all OpenDocument files using system-registered app
132 (add-to-list 'org-file-apps
133 (cons (concat "\\." (car desc) "\\'") 'system))
134 ;; Let Emacs open all OpenDocument files in archive mode
135 (add-to-list 'auto-mode-alist
136 (cons (concat "\\." (car desc) "\\'") 'archive-mode)))
137 org-odt-file-extensions)
139 (mapc
140 (lambda (desc)
141 ;; Let Org open all Microsoft files using system-registered app
142 (add-to-list 'org-file-apps
143 (cons (concat "\\." (car desc) "\\'") 'system)))
144 org-odt-ms-file-extensions)
146 ;; register the odt exporter with the pre-processor
147 (add-to-list 'org-export-backends 'odt)
149 ;; register the odt exporter with org-lparse library
150 (org-lparse-register-backend 'odt)
152 (defun org-odt-unload-function ()
153 (org-lparse-unregister-backend 'odt)
154 (remove-hook 'org-export-preprocess-after-blockquote-hook
155 'org-export-odt-preprocess-latex-fragments)
156 nil)
158 (defcustom org-export-odt-content-template-file nil
159 "Template file for \"content.xml\".
160 The exporter embeds the exported content just before
161 \"</office:text>\" element.
163 If unspecified, the file named \"OrgOdtContentTemplate.xml\"
164 under `org-odt-data-dir' is used."
165 :type 'file
166 :group 'org-export-odt)
168 (defcustom org-export-odt-styles-file nil
169 "Default styles file for use with ODT export.
170 Valid values are one of:
171 1. nil
172 2. path to a styles.xml file
173 3. path to a *.odt or a *.ott file
174 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
175 ...))
177 In case of option 1, an in-built styles.xml is used. See
178 `org-odt-data-dir' for more information.
180 In case of option 3, the specified file is unzipped and the
181 styles.xml embedded therein is used.
183 In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
184 and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
185 generated odt file. Use relative path for specifying the
186 FILE-MEMBERS. styles.xml must be specified as one of the
187 FILE-MEMBERS.
189 Use options 1, 2 or 3 only if styles.xml alone suffices for
190 achieving the desired formatting. Use option 4, if the styles.xml
191 references additional files like header and footer images for
192 achieving the desired formattting.
194 Use \"#+ODT_STYLES_FILE: ...\" directive to set this variable on
195 a per-file basis. For example,
197 #+ODT_STYLES_FILE: \"/path/to/styles.xml\" or
198 #+ODT_STYLES_FILE: (\"/path/to/file.ott\" (\"styles.xml\" \"image/hdr.png\"))."
199 :group 'org-export-odt
200 :type
201 '(choice
202 (const :tag "Factory settings" nil)
203 (file :must-match t :tag "styles.xml")
204 (file :must-match t :tag "ODT or OTT file")
205 (list :tag "ODT or OTT file + Members"
206 (file :must-match t :tag "ODF Text or Text Template file")
207 (cons :tag "Members"
208 (file :tag " Member" "styles.xml")
209 (repeat (file :tag "Member"))))))
211 (eval-after-load 'org-exp
212 '(add-to-list 'org-export-inbuffer-options-extra
213 '("ODT_STYLES_FILE" :odt-styles-file)))
215 (defconst org-export-odt-tmpdir-prefix "odt-")
216 (defconst org-export-odt-bookmark-prefix "OrgXref.")
218 (defvar org-export-odt-embed-images t
219 "Should the images be copied in to the odt file or just linked?")
221 (defvar org-export-odt-inline-images 'maybe) ; counterpart of
222 ; `org-export-html-inline-images'
224 (defcustom org-export-odt-inline-image-extensions
225 '("png" "jpeg" "jpg" "gif")
226 "Extensions of image files that can be inlined into HTML."
227 :type '(repeat (string :tag "Extension"))
228 :group 'org-export-odt)
230 (defcustom org-export-odt-pixels-per-inch display-pixels-per-inch
231 ;; FIXME add docstring
233 :type 'float
234 :group 'org-export-odt)
236 (defvar org-export-odt-default-org-styles-alist
237 '((paragraph . ((default . "Text_20_body")
238 (fixedwidth . "OrgFixedWidthBlock")
239 (verse . "OrgVerse")
240 (quote . "Quotations")
241 (blockquote . "Quotations")
242 (center . "OrgCenter")
243 (left . "OrgLeft")
244 (right . "OrgRight")
245 (title . "Heading_20_1.title")
246 (footnote . "Footnote")
247 (src . "OrgSrcBlock")
248 (illustration . "Illustration")
249 (table . "Table")
250 (definition-term . "Text_20_body_20_bold")
251 (horizontal-line . "Horizontal_20_Line")))
252 (character . ((bold . "Bold")
253 (emphasis . "Emphasis")
254 (code . "OrgCode")
255 (verbatim . "OrgCode")
256 (strike . "Strikethrough")
257 (underline . "Underline")
258 (subscript . "OrgSubscript")
259 (superscript . "OrgSuperscript")))
260 (list . ((ordered . "OrgNumberedList")
261 (unordered . "OrgBulletedList")
262 (description . "OrgDescriptionList"))))
263 "Default styles for various entities.")
265 (defvar org-export-odt-org-styles-alist org-export-odt-default-org-styles-alist)
266 (defun org-odt-get-style-name-for-entity (category &optional entity)
267 (let ((entity (or entity 'default)))
269 (cdr (assoc entity (cdr (assoc category
270 org-export-odt-org-styles-alist))))
271 (cdr (assoc entity (cdr (assoc category
272 org-export-odt-default-org-styles-alist))))
273 (error "Cannot determine style name for entity %s of type %s"
274 entity category))))
276 (defcustom org-export-odt-preferred-output-format nil
277 "Automatically post-process to this format after exporting to \"odt\".
278 Interactive commands `org-export-as-odt' and
279 `org-export-as-odt-and-open' export first to \"odt\" format and
280 then use `org-export-odt-convert-process' to convert the
281 resulting document to this format. During customization of this
282 variable, the list of valid values are populated based on
283 `org-export-odt-convert-capabilities'."
284 :group 'org-export-odt
285 :type '(choice :convert-widget
286 (lambda (w)
287 (apply 'widget-convert (widget-type w)
288 (eval (car (widget-get w :args)))))
289 `((const :tag "None" nil)
290 ,@(mapcar (lambda (c)
291 `(const :tag ,c ,c))
292 (org-lparse-reachable-formats "odt")))))
294 ;;;###autoload
295 (defun org-export-as-odt-and-open (arg)
296 "Export the outline as ODT and immediately open it with a browser.
297 If there is an active region, export only the region.
298 The prefix ARG specifies how many levels of the outline should become
299 headlines. The default is 3. Lower levels will become bulleted lists."
300 (interactive "P")
301 (org-lparse-and-open
302 (or org-export-odt-preferred-output-format "odt") "odt" arg))
304 ;;;###autoload
305 (defun org-export-as-odt-batch ()
306 "Call the function `org-lparse-batch'.
307 This function can be used in batch processing as:
308 emacs --batch
309 --load=$HOME/lib/emacs/org.el
310 --eval \"(setq org-export-headline-levels 2)\"
311 --visit=MyFile --funcall org-export-as-odt-batch"
312 (org-lparse-batch "odt"))
314 ;;;###autoload
315 (defun org-export-as-odt-to-buffer (arg)
316 "Call `org-lparse-odt` with output to a temporary buffer.
317 No file is created. The prefix ARG is passed through to `org-lparse-to-buffer'."
318 (interactive "P")
319 (org-lparse-to-buffer "odt" arg))
321 ;;;###autoload
322 (defun org-replace-region-by-odt (beg end)
323 "Assume the current region has org-mode syntax, and convert it to ODT.
324 This can be used in any buffer. For example, you could write an
325 itemized list in org-mode syntax in an ODT buffer and then use this
326 command to convert it."
327 (interactive "r")
328 (org-replace-region-by "odt" beg end))
330 ;;;###autoload
331 (defun org-export-region-as-odt (beg end &optional body-only buffer)
332 "Convert region from BEG to END in org-mode buffer to ODT.
333 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
334 contents, and only produce the region of converted text, useful for
335 cut-and-paste operations.
336 If BUFFER is a buffer or a string, use/create that buffer as a target
337 of the converted ODT. If BUFFER is the symbol `string', return the
338 produced ODT as a string and leave not buffer behind. For example,
339 a Lisp program could call this function in the following way:
341 (setq odt (org-export-region-as-odt beg end t 'string))
343 When called interactively, the output buffer is selected, and shown
344 in a window. A non-interactive call will only return the buffer."
345 (interactive "r\nP")
346 (org-lparse-region "odt" beg end body-only buffer))
348 ;;; org-export-as-odt
349 ;;;###autoload
350 (defun org-export-as-odt (arg &optional hidden ext-plist
351 to-buffer body-only pub-dir)
352 "Export the outline as a OpenDocumentText file.
353 If there is an active region, export only the region. The prefix
354 ARG specifies how many levels of the outline should become
355 headlines. The default is 3. Lower levels will become bulleted
356 lists. HIDDEN is obsolete and does nothing.
357 EXT-PLIST is a property list with external parameters overriding
358 org-mode's default settings, but still inferior to file-local
359 settings. When TO-BUFFER is non-nil, create a buffer with that
360 name and export to that buffer. If TO-BUFFER is the symbol
361 `string', don't leave any buffer behind but just return the
362 resulting XML as a string. When BODY-ONLY is set, don't produce
363 the file header and footer, simply return the content of
364 <body>...</body>, without even the body tags themselves. When
365 PUB-DIR is set, use this as the publishing directory."
366 (interactive "P")
367 (org-lparse (or org-export-odt-preferred-output-format "odt")
368 "odt" arg hidden ext-plist to-buffer body-only pub-dir))
370 (defvar org-odt-entity-control-callbacks-alist
371 `((EXPORT
372 . (org-odt-begin-export org-odt-end-export))
373 (DOCUMENT-CONTENT
374 . (org-odt-begin-document-content org-odt-end-document-content))
375 (DOCUMENT-BODY
376 . (org-odt-begin-document-body org-odt-end-document-body))
377 (TOC
378 . (org-odt-begin-toc org-odt-end-toc))
379 (ENVIRONMENT
380 . (org-odt-begin-environment org-odt-end-environment))
381 (FOOTNOTE-DEFINITION
382 . (org-odt-begin-footnote-definition org-odt-end-footnote-definition))
383 (TABLE
384 . (org-odt-begin-table org-odt-end-table))
385 (TABLE-ROWGROUP
386 . (org-odt-begin-table-rowgroup org-odt-end-table-rowgroup))
387 (LIST
388 . (org-odt-begin-list org-odt-end-list))
389 (LIST-ITEM
390 . (org-odt-begin-list-item org-odt-end-list-item))
391 (OUTLINE
392 . (org-odt-begin-outline org-odt-end-outline))
393 (OUTLINE-TEXT
394 . (org-odt-begin-outline-text org-odt-end-outline-text))
395 (PARAGRAPH
396 . (org-odt-begin-paragraph org-odt-end-paragraph)))
399 (defvar org-odt-entity-format-callbacks-alist
400 `((EXTRA-TARGETS . org-lparse-format-extra-targets)
401 (ORG-TAGS . org-lparse-format-org-tags)
402 (SECTION-NUMBER . org-lparse-format-section-number)
403 (HEADLINE . org-odt-format-headline)
404 (TOC-ENTRY . org-odt-format-toc-entry)
405 (TOC-ITEM . org-odt-format-toc-item)
406 (TAGS . org-odt-format-tags)
407 (SPACES . org-odt-format-spaces)
408 (TABS . org-odt-format-tabs)
409 (LINE-BREAK . org-odt-format-line-break)
410 (FONTIFY . org-odt-format-fontify)
411 (TODO . org-lparse-format-todo)
412 (LINK . org-odt-format-link)
413 (INLINE-IMAGE . org-odt-format-inline-image)
414 (ORG-LINK . org-odt-format-org-link)
415 (HEADING . org-odt-format-heading)
416 (ANCHOR . org-odt-format-anchor)
417 (TABLE . org-lparse-format-table)
418 (TABLE-ROW . org-odt-format-table-row)
419 (TABLE-CELL . org-odt-format-table-cell)
420 (FOOTNOTES-SECTION . ignore)
421 (FOOTNOTE-REFERENCE . org-odt-format-footnote-reference)
422 (HORIZONTAL-LINE . org-odt-format-horizontal-line)
423 (COMMENT . org-odt-format-comment)
424 (LINE . org-odt-format-line)
425 (ORG-ENTITY . org-odt-format-org-entity))
428 ;;;_. callbacks
429 ;;;_. control callbacks
430 ;;;_ , document body
431 (defun org-odt-begin-office-body ()
432 ;; automatic styles
433 (insert-file-contents
434 (or org-export-odt-content-template-file
435 (expand-file-name "styles/OrgOdtContentTemplate.xml"
436 org-odt-data-dir)))
437 (goto-char (point-min))
438 (re-search-forward "</office:text>" nil nil)
439 (delete-region (match-beginning 0) (point-max)))
441 ;; Following variable is let bound when `org-do-lparse' is in
442 ;; progress. See org-html.el.
443 (defvar org-lparse-toc)
444 (defun org-odt-begin-document-body (opt-plist)
445 (org-odt-begin-office-body)
446 (let ((title (plist-get opt-plist :title)))
447 (when title
448 (insert
449 (org-odt-format-stylized-paragraph 'title title))))
451 ;; insert toc
452 (when org-lparse-toc
453 (insert "\n" org-lparse-toc "\n")))
455 (defvar org-lparse-body-only) ; let bound during org-do-lparse
456 (defvar org-lparse-to-buffer) ; let bound during org-do-lparse
457 (defun org-odt-end-document-body (opt-plist)
458 (unless org-lparse-body-only
459 (org-lparse-insert-tag "</office:text>")
460 (org-lparse-insert-tag "</office:body>")))
462 (defun org-odt-begin-document-content (opt-plist)
463 (ignore))
465 (defun org-odt-end-document-content ()
466 (org-lparse-insert-tag "</office:document-content>"))
468 (defun org-odt-begin-outline (level1 snumber title tags
469 target extra-targets class)
470 (org-lparse-insert
471 'HEADING (org-lparse-format
472 'HEADLINE title extra-targets tags snumber level1)
473 level1 target))
475 (defun org-odt-end-outline ()
476 (ignore))
478 (defun org-odt-begin-outline-text (level1 snumber class)
479 (ignore))
481 (defun org-odt-end-outline-text ()
482 (ignore))
484 (defun org-odt-begin-paragraph (&optional style)
485 (org-lparse-insert-tag
486 "<text:p%s>" (org-odt-get-extra-attrs-for-paragraph-style style)))
488 (defun org-odt-end-paragraph ()
489 (org-lparse-insert-tag "</text:p>"))
491 (defun org-odt-get-extra-attrs-for-paragraph-style (style)
492 (let (style-name)
493 (setq style-name
494 (cond
495 ((stringp style) style)
496 ((symbolp style) (org-odt-get-style-name-for-entity
497 'paragraph style))))
498 (unless style-name
499 (error "Don't know how to handle paragraph style %s" style))
500 (format " text:style-name=\"%s\"" style-name)))
502 (defun org-odt-format-stylized-paragraph (style text)
503 (org-odt-format-tags
504 '("<text:p%s>" . "</text:p>") text
505 (org-odt-get-extra-attrs-for-paragraph-style style)))
507 (defvar org-lparse-opt-plist) ; bound during org-do-lparse
508 (defun org-odt-format-author (&optional author)
509 (when (setq author (or author (plist-get org-lparse-opt-plist :author)))
510 (org-odt-format-tags '("<dc:creator>" . "</dc:creator>") author)))
512 (defun org-odt-iso-date-from-org-timestamp (&optional org-ts)
513 (save-match-data
514 (let* ((time
515 (and (stringp org-ts)
516 (string-match org-ts-regexp0 org-ts)
517 (apply 'encode-time
518 (org-fix-decoded-time
519 (org-parse-time-string (match-string 0 org-ts) t)))))
520 (date (format-time-string "%Y-%m-%dT%H:%M:%S%z" time)))
521 (format "%s:%s" (substring date 0 -2) (substring date -2)))))
523 (defun org-odt-begin-annotation (&optional author date)
524 (org-lparse-insert-tag "<office:annotation>")
525 (when (setq author (org-odt-format-author author))
526 (insert author))
527 (insert (org-odt-format-tags
528 '("<dc:date>" . "</dc:date>")
529 (org-odt-iso-date-from-org-timestamp
530 (or date (plist-get org-lparse-opt-plist :date)))))
531 (org-lparse-begin-paragraph))
533 (defun org-odt-end-annotation ()
534 (org-lparse-insert-tag "</office:annotation>"))
536 (defun org-odt-begin-environment (style env-options-plist)
537 (case style
538 (annotation
539 (org-lparse-stash-save-paragraph-state)
540 (org-odt-begin-annotation (plist-get env-options-plist 'author)
541 (plist-get env-options-plist 'date)))
542 ((blockquote verse center quote)
543 (org-lparse-begin-paragraph style)
544 (list))
545 ((fixedwidth native)
546 (org-lparse-end-paragraph)
547 (list))
548 (t (error "Unknown environment %s" style))))
550 (defun org-odt-end-environment (style env-options-plist)
551 (case style
552 (annotation
553 (org-lparse-end-paragraph)
554 (org-odt-end-annotation)
555 (org-lparse-stash-pop-paragraph-state))
556 ((blockquote verse center quote)
557 (org-lparse-end-paragraph)
558 (list))
559 ((fixedwidth native)
560 (org-lparse-begin-paragraph)
561 (list))
562 (t (error "Unknown environment %s" style))))
564 (defvar org-lparse-list-level) ; dynamically bound in org-do-lparse
565 (defun org-odt-begin-list (ltype)
566 (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
567 ltype))
568 (let* ((style-name (org-odt-get-style-name-for-entity 'list ltype))
569 (extra (concat (when (= org-lparse-list-level 1)
570 " text:continue-numbering=\"false\"")
571 (when style-name
572 (format " text:style-name=\"%s\"" style-name)))))
573 (case ltype
574 ((ordered unordered description)
575 (org-lparse-end-paragraph)
576 (org-lparse-insert-tag "<text:list%s>" extra))
577 (t (error "Unknown list type: %s" ltype)))))
579 (defun org-odt-end-list (ltype)
580 (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
581 ltype))
582 (if ltype
583 (org-lparse-insert-tag "</text:list>")
584 (error "Unknown list type: %s" ltype)))
586 (defun org-odt-begin-list-item (ltype &optional arg headline)
587 (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
588 ltype))
589 (case ltype
590 (ordered
591 (assert (not headline) t)
592 (let* ((counter arg) (extra ""))
593 (org-lparse-insert-tag "<text:list-item>")
594 (org-lparse-begin-paragraph)))
595 (unordered
596 (let* ((id arg) (extra ""))
597 (org-lparse-insert-tag "<text:list-item>")
598 (org-lparse-begin-paragraph)
599 (insert (if headline (org-odt-format-target headline id)
600 (org-odt-format-bookmark "" id)))))
601 (description
602 (assert (not headline) t)
603 (let ((term (or arg "(no term)")))
604 (insert
605 (org-odt-format-tags
606 '("<text:list-item>" . "</text:list-item>")
607 (org-odt-format-stylized-paragraph 'definition-term term)))
608 (org-lparse-begin-list-item 'unordered)
609 (org-lparse-begin-list 'description)
610 (org-lparse-begin-list-item 'unordered)))
611 (t (error "Unknown list type"))))
613 (defun org-odt-end-list-item (ltype)
614 (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
615 ltype))
616 (case ltype
617 ((ordered unordered)
618 (org-lparse-insert-tag "</text:list-item>"))
619 (description
620 (org-lparse-end-list-item-1)
621 (org-lparse-end-list 'description)
622 (org-lparse-end-list-item-1))
623 (t (error "Unknown list type"))))
625 ;; Following variables are let bound when table emission is in
626 ;; progress. See org-lparse.el.
627 (defvar org-lparse-table-begin-marker)
628 (defvar org-lparse-table-ncols)
629 (defvar org-lparse-table-rowgrp-open)
630 (defvar org-lparse-table-rownum)
631 (defvar org-lparse-table-cur-rowgrp-is-hdr)
632 (defvar org-lparse-table-is-styled)
633 (defvar org-lparse-table-rowgrp-info)
634 (defvar org-lparse-table-colalign-vector)
636 (defvar org-odt-table-style nil
637 "Table style specified by \"#+ATTR_ODT: <style-name>\" line.
638 This is set during `org-odt-begin-table'.")
640 (defvar org-odt-table-style-spec nil
641 "Entry for `org-odt-table-style' in `org-export-odt-table-styles'.")
643 (defcustom org-export-odt-table-styles
644 '(("OrgEquation" "OrgEquation"
645 ((use-first-column-styles . t)
646 (use-last-column-styles . t))))
647 "Specify how Table Styles should be derived from a Table Template.
648 This is a list where each element is of the
649 form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
651 TABLE-STYLE-NAME is the style associated with the table through
652 `org-odt-table-style'.
654 TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
655 TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
656 below) that is included in
657 `org-export-odt-content-template-file'.
659 TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
660 \"TableCell\"
661 PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
662 \"TableParagraph\"
663 TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
664 \"FirstRow\" | \"LastRow\" |
665 \"EvenRow\" | \"OddRow\" |
666 \"EvenColumn\" | \"OddColumn\" | \"\"
667 where \"+\" above denotes string concatenation.
669 TABLE-CELL-OPTIONS is an alist where each element is of the
670 form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
671 TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
672 `use-last-row-styles' |
673 `use-first-column-styles' |
674 `use-last-column-styles' |
675 `use-banding-rows-styles' |
676 `use-banding-columns-styles' |
677 `use-first-row-styles'
678 ON-OR-OFF := `t' | `nil'
680 For example, with the following configuration
682 \(setq org-export-odt-table-styles
683 '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
684 \(\(use-first-row-styles . t\)
685 \(use-first-column-styles . t\)\)\)
686 \(\"TableWithHeaderColumns\" \"Custom\"
687 \(\(use-first-column-styles . t\)\)\)\)\)
689 1. A table associated with \"TableWithHeaderRowsAndColumns\"
690 style will use the following table-cell styles -
691 \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
692 \"CustomTableCell\" and the following paragraph styles
693 \"CustomFirstRowTableParagraph\",
694 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
695 as appropriate.
697 2. A table associated with \"TableWithHeaderColumns\" style will
698 use the following table-cell styles -
699 \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
700 following paragraph styles
701 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
702 as appropriate..
704 Note that TABLE-TEMPLATE-NAME corresponds to the
705 \"<table:table-template>\" elements contained within
706 \"<office:styles>\". The entries (TABLE-STYLE-NAME
707 TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
708 \"table:template-name\" and \"table:use-first-row-styles\" etc
709 attributes of \"<table:table>\" element. Refer ODF-1.2
710 specification for more information. Also consult the
711 implementation filed under `org-odt-get-table-cell-styles'.
713 The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
714 formatting of numbered display equations. Do not delete this
715 style from the list."
716 :group 'org-export-odt
717 :type '(choice
718 (const :tag "None" nil)
719 (repeat :tag "Table Styles"
720 (list :tag "Table Style Specification"
721 (string :tag "Table Style Name")
722 (string :tag "Table Template Name")
723 (alist :options (use-first-row-styles
724 use-last-row-styles
725 use-first-column-styles
726 use-last-column-styles
727 use-banding-rows-styles
728 use-banding-columns-styles)
729 :key-type symbol
730 :value-type (const :tag "True" t))))))
732 (defun org-odt-begin-table (caption label attributes)
733 (setq org-odt-table-style attributes)
734 (setq org-odt-table-style-spec
735 (assoc org-odt-table-style org-export-odt-table-styles))
736 (when label
737 (insert
738 (org-odt-format-stylized-paragraph
739 'table (org-odt-format-entity-caption label caption "__Table__"))))
740 (org-lparse-insert-tag
741 "<table:table table:name=\"%s\" table:style-name=\"%s\">"
742 (or label "") (or (nth 1 org-odt-table-style-spec) "OrgTable"))
743 (setq org-lparse-table-begin-marker (point)))
745 (defvar org-lparse-table-colalign-info)
746 (defun org-odt-end-table ()
747 (goto-char org-lparse-table-begin-marker)
748 (loop for level from 0 below org-lparse-table-ncols
749 do (let* ((col-cookie (and org-lparse-table-is-styled
750 (cdr (assoc (1+ level)
751 org-lparse-table-colalign-info))))
752 (extra-columns (or (nth 1 col-cookie) 0)))
753 (dotimes (i (1+ extra-columns))
754 (insert
755 (org-odt-format-tags
756 "<table:table-column table:style-name=\"%sColumn\"/>"
757 "" (or (nth 1 org-odt-table-style-spec) "OrgTable"))))
758 (insert "\n")))
759 ;; fill style attributes for table cells
760 (when org-lparse-table-is-styled
761 (while (re-search-forward "@@\\(table-cell:p\\|table-cell:style-name\\)@@\\([0-9]+\\)@@\\([0-9]+\\)@@" nil t)
762 (let* ((spec (match-string 1))
763 (r (string-to-number (match-string 2)))
764 (c (string-to-number (match-string 3)))
765 (cell-styles (org-odt-get-table-cell-styles
766 r c org-odt-table-style-spec))
767 (table-cell-style (car cell-styles))
768 (table-cell-paragraph-style (cdr cell-styles)))
769 (cond
770 ((equal spec "table-cell:p")
771 (replace-match table-cell-paragraph-style t t))
772 ((equal spec "table-cell:style-name")
773 (replace-match table-cell-style t t))))))
774 (goto-char (point-max))
775 (org-lparse-insert-tag "</table:table>"))
777 (defun org-odt-begin-table-rowgroup (&optional is-header-row)
778 (when org-lparse-table-rowgrp-open
779 (org-lparse-end 'TABLE-ROWGROUP))
780 (org-lparse-insert-tag (if is-header-row
781 "<table:table-header-rows>"
782 "<table:table-rows>"))
783 (setq org-lparse-table-rowgrp-open t)
784 (setq org-lparse-table-cur-rowgrp-is-hdr is-header-row))
786 (defun org-odt-end-table-rowgroup ()
787 (when org-lparse-table-rowgrp-open
788 (setq org-lparse-table-rowgrp-open nil)
789 (org-lparse-insert-tag
790 (if org-lparse-table-cur-rowgrp-is-hdr
791 "</table:table-header-rows>" "</table:table-rows>"))))
793 (defun org-odt-format-table-row (row)
794 (org-odt-format-tags
795 '("<table:table-row>" . "</table:table-row>") row))
797 (defun org-odt-get-table-cell-styles (r c &optional style-spec)
798 "Retrieve styles applicable to a table cell.
799 R and C are (zero-based) row and column numbers of the table
800 cell. STYLE-SPEC is an entry in `org-export-odt-table-styles'
801 applicable to the current table. It is `nil' if the table is not
802 associated with any style attributes.
804 Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
806 When STYLE-SPEC is nil, style the table cell the conventional way
807 - choose cell borders based on row and column groupings and
808 choose paragraph alignment based on `org-col-cookies' text
809 property. See also
810 `org-odt-get-paragraph-style-cookie-for-table-cell'.
812 When STYLE-SPEC is non-nil, ignore the above cookie and return
813 styles congruent with the ODF-1.2 specification."
814 (cond
815 (style-spec
817 ;; LibreOffice - particularly the Writer - honors neither table
818 ;; templates nor custom table-cell styles. Inorder to retain
819 ;; inter-operability with LibreOffice, only automatic styles are
820 ;; used for styling of table-cells. The current implementation is
821 ;; congruent with ODF-1.2 specification and hence is
822 ;; future-compatible.
824 ;; Additional Note: LibreOffice's AutoFormat facility for tables -
825 ;; which recognizes as many as 16 different cell types - is much
826 ;; richer. Unfortunately it is NOT amenable to easy configuration
827 ;; by hand.
829 (let* ((template-name (nth 1 style-spec))
830 (cell-style-selectors (nth 2 style-spec))
831 (cell-type
832 (cond
833 ((and (cdr (assoc 'use-first-column-styles cell-style-selectors))
834 (= c 0)) "FirstColumn")
835 ((and (cdr (assoc 'use-last-column-styles cell-style-selectors))
836 (= c (1- org-lparse-table-ncols))) "LastColumn")
837 ((and (cdr (assoc 'use-first-row-styles cell-style-selectors))
838 (= r 0)) "FirstRow")
839 ((and (cdr (assoc 'use-last-row-styles cell-style-selectors))
840 (= r org-lparse-table-rownum))
841 "LastRow")
842 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
843 (= (% r 2) 1)) "EvenRow")
844 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
845 (= (% r 2) 0)) "OddRow")
846 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
847 (= (% c 2) 1)) "EvenColumn")
848 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
849 (= (% c 2) 0)) "OddColumn")
850 (t ""))))
851 (cons
852 (concat template-name cell-type "TableCell")
853 (concat template-name cell-type "TableParagraph"))))
855 (cons
856 (concat
857 "OrgTblCell"
858 (cond
859 ((= r 0) "T")
860 ((eq (cdr (assoc r org-lparse-table-rowgrp-info)) :start) "T")
861 (t ""))
862 (when (= r org-lparse-table-rownum) "B")
863 (cond
864 ((= c 0) "")
865 ((or (memq (nth c org-table-colgroup-info) '(:start :startend))
866 (memq (nth (1- c) org-table-colgroup-info) '(:end :startend))) "L")
867 (t "")))
868 (capitalize (aref org-lparse-table-colalign-vector c))))))
870 (defun org-odt-get-paragraph-style-cookie-for-table-cell (r c)
871 (concat
872 (and (not org-odt-table-style-spec)
873 (cond
874 (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
875 ((and (= c 0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS))
876 "OrgTableHeading")
877 (t "OrgTableContents")))
878 (and org-lparse-table-is-styled
879 (format "@@table-cell:p@@%03d@@%03d@@" r c))))
881 (defun org-odt-get-style-name-cookie-for-table-cell (r c)
882 (when org-lparse-table-is-styled
883 (format "@@table-cell:style-name@@%03d@@%03d@@" r c)))
885 (defun org-odt-format-table-cell (data r c horiz-span)
886 (concat
887 (let* ((paragraph-style-cookie
888 (org-odt-get-paragraph-style-cookie-for-table-cell r c))
889 (style-name-cookie
890 (org-odt-get-style-name-cookie-for-table-cell r c))
891 (extra (and style-name-cookie
892 (format " table:style-name=\"%s\"" style-name-cookie)))
893 (extra (concat extra
894 (and (> horiz-span 0)
895 (format " table:number-columns-spanned=\"%d\""
896 (1+ horiz-span))))))
897 (org-odt-format-tags
898 '("<table:table-cell%s>" . "</table:table-cell>")
899 (if org-lparse-list-table-p data
900 (org-odt-format-stylized-paragraph paragraph-style-cookie data)) extra))
901 (let (s)
902 (dotimes (i horiz-span)
903 (setq s (concat s "\n<table:covered-table-cell/>"))) s)
904 "\n"))
906 (defun org-odt-begin-footnote-definition (n)
907 (org-lparse-begin-paragraph 'footnote))
909 (defun org-odt-end-footnote-definition (n)
910 (org-lparse-end-paragraph))
912 (defun org-odt-begin-toc (lang-specific-heading)
913 (insert
914 (format "
915 <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
916 <text:table-of-content-source text:outline-level=\"10\">
917 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
918 " lang-specific-heading))
920 (loop for level from 1 upto 10
921 do (insert (format
923 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
924 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
925 <text:index-entry-chapter/>
926 <text:index-entry-text/>
927 <text:index-entry-link-end/>
928 </text:table-of-content-entry-template>
929 " level level)))
931 (insert
932 (format "
933 </text:table-of-content-source>
935 <text:index-body>
936 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
937 <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
938 </text:index-title>
939 " lang-specific-heading)))
941 (defun org-odt-end-toc ()
942 (insert "
943 </text:index-body>
944 </text:table-of-content>
947 (defun org-odt-format-toc-entry (snumber todo headline tags href)
948 (setq headline (concat
949 (and org-export-with-section-numbers
950 (concat snumber ". "))
951 headline
952 (and tags
953 (concat
954 (org-lparse-format 'SPACES 3)
955 (org-lparse-format 'FONTIFY tags "tag")))))
956 (when todo
957 (setq headline (org-lparse-format 'FONTIFY headline "todo")))
959 (let ((org-odt-suppress-xref t))
960 (org-odt-format-link headline (concat "#" href))))
962 (defun org-odt-format-toc-item (toc-entry level org-last-level)
963 (let ((style (format "Contents_20_%d"
964 (+ level (or (org-lparse-get 'TOPLEVEL-HLEVEL) 1) -1))))
965 (insert "\n" (org-odt-format-stylized-paragraph style toc-entry) "\n")))
967 ;; Following variable is let bound during 'ORG-LINK callback. See
968 ;; org-html.el
969 (defvar org-lparse-link-description-is-image nil)
970 (defun org-odt-format-link (desc href &optional attr)
971 (cond
972 ((and (= (string-to-char href) ?#) (not org-odt-suppress-xref))
973 (setq href (concat org-export-odt-bookmark-prefix (substring href 1)))
974 (let ((xref-format "text"))
975 (when (numberp desc)
976 (setq desc (format "%d" desc) xref-format "number"))
977 (org-odt-format-tags
978 '("<text:bookmark-ref text:reference-format=\"%s\" text:ref-name=\"%s\">" .
979 "</text:bookmark-ref>")
980 desc xref-format href)))
981 (org-lparse-link-description-is-image
982 (org-odt-format-tags
983 '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</draw:a>")
984 desc href (or attr "")))
986 (org-odt-format-tags
987 '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</text:a>")
988 desc href (or attr "")))))
990 (defun org-odt-format-spaces (n)
991 (cond
992 ((= n 1) " ")
993 ((> n 1) (concat
994 " " (org-odt-format-tags "<text:s text:c=\"%d\"/>" "" (1- n))))
995 (t "")))
997 (defun org-odt-format-tabs (&optional n)
998 (let ((tab "<text:tab/>")
999 (n (or n 1)))
1000 (insert tab)))
1002 (defun org-odt-format-line-break ()
1003 (org-odt-format-tags "<text:line-break/>" ""))
1005 (defun org-odt-format-horizontal-line ()
1006 (org-odt-format-stylized-paragraph 'horizontal-line ""))
1008 (defun org-odt-format-line (line)
1009 (case org-lparse-dyn-current-environment
1010 (fixedwidth (concat
1011 (org-odt-format-stylized-paragraph
1012 'fixedwidth (org-odt-fill-tabs-and-spaces
1013 (org-xml-encode-plain-text line))) "\n"))
1014 (t (concat line "\n"))))
1016 (defun org-odt-format-comment (fmt &rest args)
1017 (let ((comment (apply 'format fmt args)))
1018 (format "\n<!-- %s -->\n" comment)))
1020 (defun org-odt-format-org-entity (wd)
1021 (org-entity-get-representation wd 'utf8))
1023 (defun org-odt-fill-tabs-and-spaces (line)
1024 (replace-regexp-in-string
1025 "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
1026 (cond
1027 ((string= s "\t") (org-odt-format-tabs))
1028 (t (org-odt-format-spaces (length s))))) line))
1030 (defcustom org-export-odt-fontify-srcblocks t
1031 "Specify whether or not source blocks need to be fontified.
1032 Turn this option on if you want to colorize the source code
1033 blocks in the exported file. For colorization to work, you need
1034 to make available an enhanced version of `htmlfontify' library."
1035 :type 'boolean
1036 :group 'org-export-odt)
1038 (defun org-odt-format-source-line-with-line-number-and-label
1039 (line rpllbl num fontifier par-style)
1041 (let ((keep-label (not (numberp rpllbl)))
1042 (ref (org-find-text-property-in-string 'org-coderef line)))
1043 (setq line (concat line (and keep-label ref (format "(%s)" ref))))
1044 (setq line (funcall fontifier line))
1045 (when ref
1046 (setq line (org-odt-format-target line (concat "coderef-" ref))))
1047 (setq line (org-odt-format-stylized-paragraph par-style line))
1048 (when num
1049 (org-odt-format-tags '("<text:list-item>" . "</text:list-item>") line))))
1051 (defun org-odt-format-source-code-or-example-plain
1052 (lines lang caption textareap cols rows num cont rpllbl fmt)
1053 "Format source or example blocks much like fixedwidth blocks.
1054 Use this when `org-export-odt-fontify-srcblocks' option is turned
1055 off."
1056 (let* ((lines (org-split-string lines "[\r\n]"))
1057 (line-count (length lines))
1058 (i 0))
1059 (mapconcat
1060 (lambda (line)
1061 (incf i)
1062 (org-odt-format-source-line-with-line-number-and-label
1063 line rpllbl num (lambda (line)
1064 (org-odt-fill-tabs-and-spaces
1065 (org-xml-encode-plain-text line)))
1066 (if (= i line-count) "OrgFixedWidthBlockLastLine" "OrgFixedWidthBlock")))
1067 lines "\n")))
1069 (defvar org-src-block-paragraph-format
1070 "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
1071 <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
1072 <style:background-image/>
1073 </style:paragraph-properties>
1074 <style:text-properties fo:color=\"%s\"/>
1075 </style:style>"
1076 "Custom paragraph style for colorized source and example blocks.
1077 This style is much the same as that of \"OrgFixedWidthBlock\"
1078 except that the foreground and background colors are set
1079 according to the default face identified by the `htmlfontify'.")
1081 (defun org-odt-hfy-face-to-css (fn)
1082 "Create custom style for face FN.
1083 When FN is the default face, use it's foreground and background
1084 properties to create \"OrgSrcBlock\" paragraph style. Otherwise
1085 use it's color attribute to create a character style whose name
1086 is obtained from FN. Currently all attributes of FN other than
1087 color are ignored.
1089 The style name for a face FN is derived using the following
1090 operations on the face name in that order - de-dash, CamelCase
1091 and prefix with \"OrgSrc\". For example,
1092 `font-lock-function-name-face' is associated with
1093 \"OrgSrcFontLockFunctionNameFace\"."
1094 (let* ((css-list (hfy-face-to-style fn))
1095 (style-name ((lambda (fn)
1096 (concat "OrgSrc"
1097 (mapconcat
1098 'capitalize (split-string
1099 (hfy-face-or-def-to-name fn) "-")
1100 ""))) fn))
1101 (color-val (cdr (assoc "color" css-list)))
1102 (background-color-val (cdr (assoc "background" css-list)))
1103 (style (and org-export-odt-create-custom-styles-for-srcblocks
1104 (cond
1105 ((eq fn 'default)
1106 (format org-src-block-paragraph-format
1107 background-color-val color-val))
1109 (format
1111 <style:style style:name=\"%s\" style:family=\"text\">
1112 <style:text-properties fo:color=\"%s\"/>
1113 </style:style>" style-name color-val))))))
1114 (cons style-name style)))
1116 (defcustom org-export-odt-create-custom-styles-for-srcblocks t
1117 "Whether custom styles for colorized source blocks be automatically created.
1118 When this option is turned on, the exporter creates custom styles
1119 for source blocks based on the advice of `htmlfontify'. Creation
1120 of custom styles happen as part of `org-odt-hfy-face-to-css'.
1122 When this option is turned off exporter does not create such
1123 styles.
1125 Use the latter option if you do not want the custom styles to be
1126 based on your current display settings. It is necessary that the
1127 styles.xml already contains needed styles for colorizing to work.
1129 This variable is effective only if
1130 `org-export-odt-fontify-srcblocks' is turned on."
1131 :group 'org-export-odt
1132 :type 'boolean)
1134 (defun org-odt-insert-custom-styles-for-srcblocks (styles)
1135 "Save STYLES used for colorizing of source blocks.
1136 Update styles.xml with styles that were collected as part of
1137 `org-odt-hfy-face-to-css' callbacks."
1138 (when styles
1139 (with-current-buffer
1140 (find-file-noselect (expand-file-name "styles.xml") t)
1141 (goto-char (point-min))
1142 (when (re-search-forward "</office:styles>" nil t)
1143 (goto-char (match-beginning 0))
1144 (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n")))))
1146 (defun org-odt-format-source-code-or-example-colored
1147 (lines lang caption textareap cols rows num cont rpllbl fmt)
1148 "Format source or example blocks using `htmlfontify-string'.
1149 Use this routine when `org-export-odt-fontify-srcblocks' option
1150 is turned on."
1151 (let* ((lang-m (and lang (or (cdr (assoc lang org-src-lang-modes)) lang)))
1152 (mode (and lang-m (intern (concat (if (symbolp lang-m)
1153 (symbol-name lang-m)
1154 lang-m) "-mode"))))
1155 (org-inhibit-startup t)
1156 (org-startup-folded nil)
1157 (lines (with-temp-buffer
1158 (insert lines)
1159 (if (functionp mode) (funcall mode) (fundamental-mode))
1160 (font-lock-fontify-buffer)
1161 (buffer-string)))
1162 (hfy-html-quote-regex "\\([<\"&> ]\\)")
1163 (hfy-html-quote-map '(("\"" "&quot;")
1164 ("<" "&lt;")
1165 ("&" "&amp;")
1166 (">" "&gt;")
1167 (" " "<text:s/>")
1168 (" " "<text:tab/>")))
1169 (hfy-face-to-css 'org-odt-hfy-face-to-css)
1170 (hfy-optimisations-1 (copy-seq hfy-optimisations))
1171 (hfy-optimisations (add-to-list 'hfy-optimisations-1
1172 'body-text-only))
1173 (hfy-begin-span-handler
1174 (lambda (style text-block text-id text-begins-block-p)
1175 (insert (format "<text:span text:style-name=\"%s\">" style))))
1176 (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
1177 (when (fboundp 'htmlfontify-string)
1178 (let* ((lines (org-split-string lines "[\r\n]"))
1179 (line-count (length lines))
1180 (i 0))
1181 (mapconcat
1182 (lambda (line)
1183 (incf i)
1184 (org-odt-format-source-line-with-line-number-and-label
1185 line rpllbl num 'htmlfontify-string
1186 (if (= i line-count) "OrgSrcBlockLastLine" "OrgSrcBlock")))
1187 lines "\n")))))
1189 (defun org-odt-format-source-code-or-example (lines lang caption textareap
1190 cols rows num cont
1191 rpllbl fmt)
1192 "Format source or example blocks for export.
1193 Use `org-odt-format-source-code-or-example-plain' or
1194 `org-odt-format-source-code-or-example-colored' depending on the
1195 value of `org-export-odt-fontify-srcblocks."
1196 (setq lines (org-export-number-lines
1197 lines 0 0 num cont rpllbl fmt 'preprocess)
1198 lines (funcall
1199 (or (and org-export-odt-fontify-srcblocks
1200 (or (featurep 'htmlfontify)
1201 (require 'htmlfontify))
1202 (fboundp 'htmlfontify-string)
1203 'org-odt-format-source-code-or-example-colored)
1204 'org-odt-format-source-code-or-example-plain)
1205 lines lang caption textareap cols rows num cont rpllbl fmt))
1206 (if (not num) lines
1207 (let ((extra (format " text:continue-numbering=\"%s\""
1208 (if cont "true" "false"))))
1209 (org-odt-format-tags
1210 '("<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>"
1211 . "</text:list>") lines extra))))
1213 (defun org-odt-remap-stylenames (style-name)
1215 (cdr (assoc style-name '(("timestamp-wrapper" . "OrgTimestampWrapper")
1216 ("timestamp" . "OrgTimestamp")
1217 ("timestamp-kwd" . "OrgTimestampKeyword")
1218 ("tag" . "OrgTag")
1219 ("todo" . "OrgTodo")
1220 ("done" . "OrgDone")
1221 ("target" . "OrgTarget"))))
1222 style-name))
1224 (defun org-odt-format-fontify (text style &optional id)
1225 (let* ((style-name
1226 (cond
1227 ((stringp style)
1228 (org-odt-remap-stylenames style))
1229 ((symbolp style)
1230 (org-odt-get-style-name-for-entity 'character style))
1231 ((listp style)
1232 (assert (< 1 (length style)))
1233 (let ((parent-style (pop style)))
1234 (mapconcat (lambda (s)
1235 ;; (assert (stringp s) t)
1236 (org-odt-remap-stylenames s)) style "")
1237 (org-odt-remap-stylenames parent-style)))
1238 (t (error "Don't how to handle style %s" style)))))
1239 (org-odt-format-tags
1240 '("<text:span text:style-name=\"%s\">" . "</text:span>")
1241 text style-name)))
1243 (defun org-odt-relocate-relative-path (path dir)
1244 (if (file-name-absolute-p path) path
1245 (file-relative-name (expand-file-name path dir)
1246 (expand-file-name "eyecandy" dir))))
1248 (defun org-odt-format-inline-image (thefile)
1249 (let* ((thelink (if (file-name-absolute-p thefile) thefile
1250 (org-xml-format-href
1251 (org-odt-relocate-relative-path
1252 thefile org-current-export-file))))
1253 (href
1254 (org-odt-format-tags
1255 "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
1256 (if org-export-odt-embed-images
1257 (org-odt-copy-image-file thefile) thelink))))
1258 (org-export-odt-format-image thefile href)))
1260 (defun org-export-odt-format-formula (src href &optional embed-as)
1261 "Create image tag with source and attributes."
1262 (save-match-data
1263 (let* ((caption (org-find-text-property-in-string 'org-caption src))
1264 (caption (and caption (org-xml-format-desc caption)))
1265 (label (org-find-text-property-in-string 'org-label src))
1266 (embed-as (or embed-as
1267 (and (org-find-text-property-in-string
1268 'org-latex-src src)
1269 (org-find-text-property-in-string
1270 'org-latex-src-embed-type src))
1271 'paragraph))
1272 width height)
1273 (cond
1274 ((eq embed-as 'character)
1275 (org-odt-format-entity "InlineFormula" href width height))
1277 (org-lparse-end-paragraph)
1278 (org-lparse-insert-list-table
1279 `((,(org-odt-format-entity
1280 (if caption "CaptionedDisplayFormula" "DisplayFormula")
1281 href width height caption nil)
1282 ,(if (not label) ""
1283 (org-odt-format-entity-caption label nil "__MathFormula__"))))
1284 nil nil nil "OrgEquation" nil '((1 "c" 8) (2 "c" 1)))
1285 (throw 'nextline nil))))))
1287 (defvar org-odt-embedded-formulas-count 0)
1288 (defun org-odt-copy-formula-file (path)
1289 "Returns the internal name of the file"
1290 (let* ((src-file (expand-file-name
1291 path (file-name-directory org-current-export-file)))
1292 (target-dir (format "Formula-%04d/"
1293 (incf org-odt-embedded-formulas-count)))
1294 (target-file (concat target-dir "content.xml")))
1295 (when (not org-lparse-to-buffer)
1296 (message "Embedding %s as %s ..."
1297 (substring-no-properties path) target-file)
1299 (make-directory target-dir)
1300 (org-odt-create-manifest-file-entry
1301 "application/vnd.oasis.opendocument.formula" target-dir "1.2")
1303 (copy-file src-file target-file 'overwrite)
1304 (org-odt-create-manifest-file-entry "text/xml" target-file))
1305 target-file))
1307 (defun org-odt-format-inline-formula (thefile)
1308 (let* ((thelink (if (file-name-absolute-p thefile) thefile
1309 (org-xml-format-href
1310 (org-odt-relocate-relative-path
1311 thefile org-current-export-file))))
1312 (href
1313 (org-odt-format-tags
1314 "<draw:object xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
1315 (file-name-directory (org-odt-copy-formula-file thefile)))))
1316 (org-export-odt-format-formula thefile href)))
1318 (defun org-odt-is-formula-link-p (file)
1319 (let ((case-fold-search nil))
1320 (string-match "\\.mathml\\'" file)))
1322 (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
1323 descp)
1324 "Make an HTML link.
1325 OPT-PLIST is an options list.
1326 TYPE is the device-type of the link (THIS://foo.html)
1327 PATH is the path of the link (http://THIS#locationx)
1328 FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
1329 DESC is the link description, if any.
1330 ATTR is a string of other attributes of the a element.
1331 MAY-INLINE-P allows inlining it as an image."
1332 (declare (special org-lparse-par-open))
1333 (save-match-data
1334 (let* ((may-inline-p
1335 (and (member type-1 '("http" "https" "file"))
1336 (org-lparse-should-inline-p path descp)
1337 (not fragment)))
1338 (type (if (equal type-1 "id") "file" type-1))
1339 (filename path)
1340 (thefile path))
1341 (cond
1342 ;; check for inlined images
1343 ((and (member type '("file"))
1344 (not fragment)
1345 (org-file-image-p
1346 filename org-export-odt-inline-image-extensions)
1347 (or (eq t org-export-odt-inline-images)
1348 (and org-export-odt-inline-images (not descp))))
1349 (org-odt-format-inline-image thefile))
1350 ;; check for embedded formulas
1351 ((and (member type '("file"))
1352 (not fragment)
1353 (org-odt-is-formula-link-p filename)
1354 (or (not descp)))
1355 (org-odt-format-inline-formula thefile))
1356 ((string= type "coderef")
1357 (let* ((ref fragment)
1358 (lineno-or-ref (cdr (assoc ref org-export-code-refs)))
1359 (desc (and descp desc))
1360 (org-odt-suppress-xref nil)
1361 (href (org-xml-format-href (concat "#coderef-" ref))))
1362 (cond
1363 ((and (numberp lineno-or-ref) (not desc))
1364 (org-odt-format-link lineno-or-ref href))
1365 ((and (numberp lineno-or-ref) desc
1366 (string-match (regexp-quote (concat "(" ref ")")) desc))
1367 (format (replace-match "%s" t t desc)
1368 (org-odt-format-link lineno-or-ref href)))
1370 (setq desc (format
1371 (if (and desc (string-match
1372 (regexp-quote (concat "(" ref ")"))
1373 desc))
1374 (replace-match "%s" t t desc)
1375 (or desc "%s"))
1376 lineno-or-ref))
1377 (org-odt-format-link (org-xml-format-desc desc) href)))))
1379 (when (string= type "file")
1380 (setq thefile
1381 (cond
1382 ((file-name-absolute-p path)
1383 (concat "file://" (expand-file-name path)))
1384 (t (org-odt-relocate-relative-path
1385 thefile org-current-export-file)))))
1387 (when (and (member type '("" "http" "https" "file")) fragment)
1388 (setq thefile (concat thefile "#" fragment)))
1390 (setq thefile (org-xml-format-href thefile))
1392 (when (not (member type '("" "file")))
1393 (setq thefile (concat type ":" thefile)))
1395 (let ((org-odt-suppress-xref nil))
1396 (org-odt-format-link
1397 (org-xml-format-desc desc) thefile attr)))))))
1399 (defun org-odt-format-heading (text level &optional id)
1400 (let* ((text (if id (org-odt-format-target text id) text)))
1401 (org-odt-format-tags
1402 '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
1403 "</text:h>") text level level)))
1405 (defun org-odt-format-headline (title extra-targets tags
1406 &optional snumber level)
1407 (concat
1408 (org-lparse-format 'EXTRA-TARGETS extra-targets)
1410 ;; No need to generate section numbers. They are auto-generated by
1411 ;; the application
1413 ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
1414 title
1415 (and tags (concat (org-lparse-format 'SPACES 3)
1416 (org-lparse-format 'ORG-TAGS tags)))))
1418 (defun org-odt-format-anchor (text name &optional class)
1419 (org-odt-format-target text name))
1421 (defun org-odt-format-bookmark (text id)
1422 (if id
1423 (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id)
1424 text))
1426 (defun org-odt-format-target (text id)
1427 (let ((name (concat org-export-odt-bookmark-prefix id)))
1428 (concat
1429 (and id (org-odt-format-tags
1430 "<text:bookmark-start text:name=\"%s\"/>" "" name))
1431 (org-odt-format-bookmark text id)
1432 (and id (org-odt-format-tags
1433 "<text:bookmark-end text:name=\"%s\"/>" "" name)))))
1435 (defun org-odt-format-footnote (n def)
1436 (let ((id (concat "fn" n))
1437 (note-class "footnote")
1438 (par-style "Footnote"))
1439 (org-odt-format-tags
1440 '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
1441 "</text:note>")
1442 (concat
1443 (org-odt-format-tags
1444 '("<text:note-citation>" . "</text:note-citation>")
1446 (org-odt-format-tags
1447 '("<text:note-body>" . "</text:note-body>")
1448 def))
1449 id note-class)))
1451 (defun org-odt-format-footnote-reference (n def refcnt)
1452 (if (= refcnt 1)
1453 (org-odt-format-footnote n def)
1454 (org-odt-format-footnote-ref n)))
1456 (defun org-odt-format-footnote-ref (n)
1457 (let ((note-class "footnote")
1458 (ref-format "text")
1459 (ref-name (concat "fn" n)))
1460 (org-odt-format-tags
1461 '("<text:span text:style-name=\"%s\">" . "</text:span>")
1462 (org-odt-format-tags
1463 '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" . "</text:note-ref>")
1464 n note-class ref-format ref-name)
1465 "OrgSuperscript")))
1467 (defun org-odt-get-image-name (file-name)
1468 (require 'sha1)
1469 (file-relative-name
1470 (expand-file-name
1471 (concat (sha1 file-name) "." (file-name-extension file-name)) "Pictures")))
1473 (defun org-export-odt-format-image (src href &optional embed-as)
1474 "Create image tag with source and attributes."
1475 (save-match-data
1476 (let* ((caption (org-find-text-property-in-string 'org-caption src))
1477 (caption (and caption (org-xml-format-desc caption)))
1478 (attr (org-find-text-property-in-string 'org-attributes src))
1479 (label (org-find-text-property-in-string 'org-label src))
1480 (latex-fragment-p (org-find-text-property-in-string
1481 'org-latex-src src))
1482 (category (and latex-fragment-p "__DvipngImage__"))
1483 (embed-as (or embed-as
1484 (if latex-fragment-p
1485 (or (org-find-text-property-in-string
1486 'org-latex-src-embed-type src) 'character)
1487 'paragraph)))
1488 (attr-plist (org-lparse-get-block-params attr))
1489 (size (org-odt-image-size-from-file
1490 src (plist-get attr-plist :width)
1491 (plist-get attr-plist :height)
1492 (plist-get attr-plist :scale) nil embed-as))
1493 (width (car size)) (height (cdr size)))
1494 (cond
1495 ((not (or caption label))
1496 (case embed-as
1497 (paragraph (org-odt-format-entity "DisplayImage" href width height))
1498 (character (org-odt-format-entity "InlineImage" href width height))
1499 (t (error "Unknown value for embed-as %S" embed-as))))
1501 (org-odt-format-entity
1502 "CaptionedDisplayImage" href width height caption label category))))))
1504 (defun org-odt-format-frame (text width height style &optional
1505 extra anchor-type)
1506 (let ((frame-attrs
1507 (concat
1508 (if width (format " svg:width=\"%0.2fcm\"" width) "")
1509 (if height (format " svg:height=\"%0.2fcm\"" height) "")
1510 extra
1511 (format " text:anchor-type=\"%s\"" (or anchor-type "paragraph")))))
1512 (org-odt-format-tags
1513 '("<draw:frame draw:style-name=\"%s\"%s>" . "</draw:frame>")
1514 text style frame-attrs)))
1516 (defun org-odt-format-textbox (text width height style &optional
1517 extra anchor-type)
1518 (org-odt-format-frame
1519 (org-odt-format-tags
1520 '("<draw:text-box %s>" . "</draw:text-box>")
1521 text (concat (format " fo:min-height=\"%0.2fcm\"" (or height .2))
1522 (format " fo:min-width=\"%0.2fcm\"" (or width .2))))
1523 width nil style extra anchor-type))
1525 (defun org-odt-format-inlinetask (heading content
1526 &optional todo priority tags)
1527 (org-odt-format-stylized-paragraph
1528 nil (org-odt-format-textbox
1529 (concat (org-odt-format-stylized-paragraph
1530 "OrgInlineTaskHeading"
1531 (org-lparse-format
1532 'HEADLINE (concat (org-lparse-format-todo todo) " " heading)
1533 nil tags))
1534 content) nil nil "OrgInlineTaskFrame" " style:rel-width=\"100%\"")))
1536 (defvar org-odt-entity-frame-styles
1537 '(("InlineImage" "__Figure__" ("OrgInlineImage" nil "as-char"))
1538 ("DisplayImage" "__Figure__" ("OrgDisplayImage" nil "paragraph"))
1539 ("CaptionedDisplayImage" "__Figure__"
1540 ("OrgCaptionedImage"
1541 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
1542 ("OrgImageCaptionFrame"))
1543 ("InlineFormula" "__MathFormula__" ("OrgInlineFormula" nil "as-char"))
1544 ("DisplayFormula" "__MathFormula__" ("OrgDisplayFormula" nil "as-char"))
1545 ("CaptionedDisplayFormula" "__MathFormula__"
1546 ("OrgCaptionedFormula" nil "paragraph")
1547 ("OrgFormulaCaptionFrame" nil "as-char"))))
1549 (defun org-odt-format-entity (entity href width height
1550 &optional caption label category)
1551 (let* ((entity-style (assoc entity org-odt-entity-frame-styles))
1552 (entity-frame (apply 'org-odt-format-frame
1553 href width height (nth 2 entity-style))))
1554 (if (not (or caption label)) entity-frame
1555 (apply 'org-odt-format-textbox
1556 (org-odt-format-stylized-paragraph
1557 'illustration
1558 (concat entity-frame
1559 (org-odt-format-entity-caption
1560 label caption (or category (nth 1 entity-style)))))
1561 width height (nth 3 entity-style)))))
1563 (defvar org-odt-embedded-images-count 0)
1564 (defun org-odt-copy-image-file (path)
1565 "Returns the internal name of the file"
1566 (let* ((image-type (file-name-extension path))
1567 (media-type (format "image/%s" image-type))
1568 (src-file (expand-file-name
1569 path (file-name-directory org-current-export-file)))
1570 (target-dir "Images/")
1571 (target-file
1572 (format "%s%04d.%s" target-dir
1573 (incf org-odt-embedded-images-count) image-type)))
1574 (when (not org-lparse-to-buffer)
1575 (message "Embedding %s as %s ..."
1576 (substring-no-properties path) target-file)
1578 (when (= 1 org-odt-embedded-images-count)
1579 (make-directory target-dir)
1580 (org-odt-create-manifest-file-entry "" target-dir))
1582 (copy-file src-file target-file 'overwrite)
1583 (org-odt-create-manifest-file-entry media-type target-file))
1584 target-file))
1586 (defvar org-export-odt-image-size-probe-method
1587 '(emacs imagemagick force)
1588 "Ordered list of methods by for determining size of an embedded
1589 image.")
1591 (defvar org-export-odt-default-image-sizes-alist
1592 '(("character" . (5 . 0.4))
1593 ("paragraph" . (5 . 5)))
1594 "Hardcoded image dimensions one for each of the anchor
1595 methods.")
1597 (defun org-odt-do-image-size (probe-method file &optional dpi anchor-type)
1598 (setq dpi (or dpi org-export-odt-pixels-per-inch))
1599 (setq anchor-type (or anchor-type "paragraph"))
1600 (flet ((size-in-cms (size-in-pixels)
1601 (flet ((pixels-to-cms (pixels)
1602 (let* ((cms-per-inch 2.54)
1603 (inches (/ pixels dpi)))
1604 (* cms-per-inch inches))))
1605 (and size-in-pixels
1606 (cons (pixels-to-cms (car size-in-pixels))
1607 (pixels-to-cms (cdr size-in-pixels)))))))
1608 (case probe-method
1609 (emacs
1610 (size-in-cms (ignore-errors (image-size (create-image file) 'pixels))))
1611 (imagemagick
1612 (size-in-cms
1613 (let ((dim (shell-command-to-string
1614 (format "identify -format \"%%w:%%h\" \"%s\"" file))))
1615 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
1616 (cons (string-to-number (match-string 1 dim))
1617 (string-to-number (match-string 2 dim)))))))
1619 (cdr (assoc-string anchor-type
1620 org-export-odt-default-image-sizes-alist))))))
1622 (defun org-odt-image-size-from-file (file &optional user-width
1623 user-height scale dpi embed-as)
1624 (unless (file-name-absolute-p file)
1625 (setq file (expand-file-name
1626 file (file-name-directory org-current-export-file))))
1627 (let* (size width height)
1628 (unless (and user-height user-width)
1629 (loop for probe-method in org-export-odt-image-size-probe-method
1630 until size
1631 do (setq size (org-odt-do-image-size
1632 probe-method file dpi embed-as)))
1633 (or size (error "Cannot determine Image size. Aborting ..."))
1634 (setq width (car size) height (cdr size)))
1635 (cond
1636 (scale
1637 (setq width (* width scale) height (* height scale)))
1638 ((and user-height user-width)
1639 (setq width user-width height user-height))
1640 (user-height
1641 (setq width (* user-height (/ width height)) height user-height))
1642 (user-width
1643 (setq height (* user-width (/ height width)) width user-width))
1644 (t (ignore)))
1645 (cons width height)))
1647 (defvar org-odt-entity-labels-alist nil
1648 "Associate Labels with the Labelled entities.
1649 Each element of the alist is of the form (LABEL-NAME
1650 CATEGORY-NAME SEQNO LABEL-STYLE-NAME). LABEL-NAME is same as
1651 that specified by \"#+LABEL: ...\" line. CATEGORY-NAME is the
1652 type of the entity that LABEL-NAME is attached to. CATEGORY-NAME
1653 can be one of \"Table\", \"Figure\" or \"Equation\". SEQNO is
1654 the unique number assigned to the referenced entity on a
1655 per-CATEGORY basis. It is generated sequentially and is 1-based.
1656 LABEL-STYLE-NAME is a key `org-odt-label-styles'.
1658 See `org-odt-add-label-definition' and
1659 `org-odt-fixup-label-references'.")
1661 (defvar org-odt-entity-counts-plist nil
1662 "Plist of running counters of SEQNOs for each of the CATEGORY-NAMEs.
1663 See `org-odt-entity-labels-alist' for known CATEGORY-NAMEs.")
1665 (defvar org-odt-label-styles
1666 '(("text" "(%n)" "text" "(%n)")
1667 ("category-and-value" "%e %n%c" "category-and-value" "%e %n"))
1668 "Specify how labels are applied and referenced.
1669 This is an alist where each element is of the
1670 form (LABEL-STYLE-NAME LABEL-ATTACH-FMT LABEL-REF-MODE
1671 LABEL-REF-FMT).
1673 LABEL-ATTACH-FMT controls how labels and captions are attached to
1674 an entity. It may contain following specifiers - %e, %n and %c.
1675 %e is replaced with the CATEGORY-NAME. %n is replaced with
1676 \"<text:sequence ...> SEQNO </text:sequence>\". %c is replaced
1677 with CAPTION. See `org-odt-format-label-definition'.
1679 LABEL-REF-MODE and LABEL-REF-FMT controls how label references
1680 are generated. The following XML is generated for a label
1681 reference - \"<text:sequence-ref
1682 text:reference-format=\"LABEL-REF-MODE\" ...> LABEL-REF-FMT
1683 </text:sequence-ref>\". LABEL-REF-FMT may contain following
1684 specifiers - %e and %n. %e is replaced with the CATEGORY-NAME.
1685 %n is replaced with SEQNO. See
1686 `org-odt-format-label-reference'.")
1688 (defvar org-odt-category-map-alist
1689 '(("__Table__" "Table" "category-and-value")
1690 ("__Figure__" "Figure" "category-and-value")
1691 ("__MathFormula__" "Equation" "text")
1692 ("__DvipngImage__" "Equation" "category-and-value"))
1693 "Map a CATEGORY-HANDLE to CATEGORY-NAME and LABEL-STYLE.
1694 This is an alist where each element is of the form
1695 \\(CATEGORY-HANDLE CATEGORY-NAME LABEL-STYLE\\). CATEGORY_HANDLE
1696 could either be one of the internal handles (as seen above) or be
1697 derived from the \"#+LABEL:<label-name>\" specification. See
1698 `org-export-odt-get-category-from-label'. CATEGORY-NAME and
1699 LABEL-STYLE are used for generating ODT labels. See
1700 `org-odt-label-styles'.")
1702 (defvar org-export-odt-user-categories
1703 '("Illustration" "Table" "Text" "Drawing" "Equation" "Figure"))
1705 (defvar org-export-odt-get-category-from-label nil
1706 "Should category of label be inferred from label itself.
1707 When this option is non-nil, a label is parsed in to two
1708 component parts delimited by a \":\" (colon) as shown here -
1709 #+LABEL:[CATEGORY-HANDLE:]EXTRA. The CATEGORY-HANDLE is mapped
1710 to a CATEGORY-NAME and LABEL-STYLE using
1711 `org-odt-category-map-alist'. (If no such map is provided and
1712 CATEGORY-NAME is set to CATEGORY-HANDLE and LABEL-STYLE is set to
1713 \"category-and-value\"). If CATEGORY-NAME so obtained is listed
1714 under `org-export-odt-user-categories' then the user specified
1715 styles are used. Otherwise styles as determined by the internal
1716 CATEGORY-HANDLE is used. See
1717 `org-odt-get-label-category-and-style' for details.")
1719 (defun org-odt-get-label-category-and-style (label default-category)
1720 "See `org-export-odt-get-category-from-label'."
1721 (let ((default-category-map
1722 (assoc default-category org-odt-category-map-alist))
1723 user-category user-category-map category)
1724 (cond
1725 ((not org-export-odt-get-category-from-label)
1726 default-category-map)
1727 ((not (setq user-category
1728 (save-match-data
1729 (and (string-match "\\`\\(.*\\):.+" label)
1730 (match-string 1 label)))))
1731 default-category-map)
1733 (setq user-category-map
1734 (or (assoc user-category org-odt-category-map-alist)
1735 (list nil user-category "category-and-value"))
1736 category (nth 1 user-category-map))
1737 (if (member category org-export-odt-user-categories)
1738 user-category-map
1739 default-category-map)))))
1741 (defun org-odt-add-label-definition (label default-category)
1742 "Create an entry in `org-odt-entity-labels-alist' and return it."
1743 (setq label (substring-no-properties label))
1744 (let* ((label-props (org-odt-get-label-category-and-style
1745 label default-category))
1746 (category (nth 1 label-props))
1747 (counter category)
1748 (label-style (nth 2 label-props))
1749 (sequence-var (intern (mapconcat
1750 'downcase
1751 (org-split-string counter) "-")))
1752 (seqno (1+ (or (plist-get org-odt-entity-counts-plist sequence-var)
1753 0)))
1754 (label-props (list label category seqno label-style)))
1755 (setq org-odt-entity-counts-plist
1756 (plist-put org-odt-entity-counts-plist sequence-var seqno))
1757 (push label-props org-odt-entity-labels-alist)
1758 label-props))
1760 (defun org-odt-format-label-definition (caption label category seqno label-style)
1761 (assert label)
1762 (format-spec
1763 (cadr (assoc-string label-style org-odt-label-styles t))
1764 `((?e . ,category)
1765 (?n . ,(org-odt-format-tags
1766 '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" . "</text:sequence>")
1767 (format "%d" seqno) label category category))
1768 (?c . ,(or (and caption (concat ": " caption)) "")))))
1770 (defun org-odt-format-label-reference (label category seqno label-style)
1771 (assert label)
1772 (save-match-data
1773 (let* ((fmt (cddr (assoc-string label-style org-odt-label-styles t)))
1774 (fmt1 (car fmt))
1775 (fmt2 (cadr fmt)))
1776 (org-odt-format-tags
1777 '("<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">"
1778 . "</text:sequence-ref>")
1779 (format-spec fmt2 `((?e . ,category)
1780 (?n . ,(format "%d" seqno)))) fmt1 label))))
1782 (defun org-odt-fixup-label-references ()
1783 (goto-char (point-min))
1784 (while (re-search-forward
1785 "<text:sequence-ref text:ref-name=\"\\([^\"]+\\)\"/>" nil t)
1786 (let* ((label (match-string 1))
1787 (label-def (assoc label org-odt-entity-labels-alist))
1788 (rpl (and label-def
1789 (apply 'org-odt-format-label-reference label-def))))
1790 (if rpl (replace-match rpl t t)
1791 (org-lparse-warn
1792 (format "Unable to resolve reference to label \"%s\"" label))))))
1794 (defun org-odt-format-entity-caption (label caption category)
1795 (or (and label
1796 (apply 'org-odt-format-label-definition
1797 caption (org-odt-add-label-definition label category)))
1798 caption ""))
1800 (defun org-odt-format-tags (tag text &rest args)
1801 (let ((prefix (when org-lparse-encode-pending "@"))
1802 (suffix (when org-lparse-encode-pending "@")))
1803 (apply 'org-lparse-format-tags tag text prefix suffix args)))
1805 (defun org-odt-init-outfile (filename)
1806 (unless (executable-find "zip")
1807 ;; Not at all OSes ship with zip by default
1808 (error "Executable \"zip\" needed for creating OpenDocument files"))
1810 (let* ((outdir (make-temp-file org-export-odt-tmpdir-prefix t))
1811 (content-file (expand-file-name "content.xml" outdir)))
1813 ;; init conten.xml
1814 (with-current-buffer (find-file-noselect content-file t))
1816 ;; reset variables
1817 (setq org-odt-manifest-file-entries nil
1818 org-odt-embedded-images-count 0
1819 org-odt-embedded-formulas-count 0
1820 org-odt-entity-labels-alist nil
1821 org-odt-entity-counts-plist nil)
1822 content-file))
1824 (defcustom org-export-odt-prettify-xml nil
1825 "Specify whether or not the xml output should be prettified.
1826 When this option is turned on, `indent-region' is run on all
1827 component xml buffers before they are saved. Turn this off for
1828 regular use. Turn this on if you need to examine the xml
1829 visually."
1830 :group 'org-export-odt
1831 :type 'boolean)
1833 (defvar hfy-user-sheet-assoc) ; bound during org-do-lparse
1834 (defun org-odt-save-as-outfile (target opt-plist)
1835 ;; create mimetype file
1836 (write-region "application/vnd.oasis.opendocument.text" nil
1837 (expand-file-name "mimetype"))
1839 ;; write meta file
1840 (org-odt-update-meta-file opt-plist)
1842 ;; write styles file
1843 (let ((styles-file (plist-get opt-plist :odt-styles-file)))
1844 (org-odt-copy-styles-file (and styles-file
1845 (read (org-trim styles-file)))))
1847 ;; Update styles.xml - take care of outline numbering
1848 (with-current-buffer
1849 (find-file-noselect (expand-file-name "styles.xml") t)
1850 ;; Don't make automatic backup of styles.xml file. This setting
1851 ;; prevents the backedup styles.xml file from being zipped in to
1852 ;; odt file. This is more of a hackish fix. Better alternative
1853 ;; would be to fix the zip command so that the output odt file
1854 ;; includes only the needed files and excludes any auto-generated
1855 ;; extra files like backups and auto-saves etc etc. Note that
1856 ;; currently the zip command zips up the entire temp directory so
1857 ;; that any auto-generated files created under the hood ends up in
1858 ;; the resulting odt file.
1859 (set (make-local-variable 'backup-inhibited) t)
1861 ;; Import local setting of `org-export-with-section-numbers'
1862 (org-lparse-bind-local-variables opt-plist)
1863 (org-odt-configure-outline-numbering
1864 (if org-export-with-section-numbers org-export-headline-levels 0)))
1866 ;; Write custom stlyes for source blocks
1867 (org-odt-insert-custom-styles-for-srcblocks
1868 (mapconcat
1869 (lambda (style)
1870 (format " %s\n" (cddr style)))
1871 hfy-user-sheet-assoc ""))
1873 ;; create a manifest entry for content.xml
1874 (org-odt-create-manifest-file-entry
1875 "application/vnd.oasis.opendocument.text" "/" "1.2")
1877 (org-odt-create-manifest-file-entry "text/xml" "content.xml")
1879 ;; write out the manifest entries before zipping
1880 (org-odt-write-manifest-file)
1882 (let ((xml-files '("mimetype" "META-INF/manifest.xml" "content.xml"
1883 "meta.xml" "styles.xml"))
1884 (zipdir default-directory))
1885 (message "Switching to directory %s" (expand-file-name zipdir))
1887 ;; save all xml files
1888 (mapc (lambda (file)
1889 (with-current-buffer
1890 (find-file-noselect (expand-file-name file) t)
1891 ;; prettify output if needed
1892 (when org-export-odt-prettify-xml
1893 (indent-region (point-min) (point-max)))
1894 (save-buffer 0)))
1895 xml-files)
1897 (let* ((target-name (file-name-nondirectory target))
1898 (target-dir (file-name-directory target))
1899 (cmds `(("zip" "-mX0" ,target-name "mimetype")
1900 ("zip" "-rmTq" ,target-name "."))))
1901 (when (file-exists-p target)
1902 ;; FIXME: If the file is locked this throws a cryptic error
1903 (delete-file target))
1905 (let ((coding-system-for-write 'no-conversion) exitcode err-string)
1906 (message "Creating odt file...")
1907 (mapc
1908 (lambda (cmd)
1909 (message "Running %s" (mapconcat 'identity cmd " "))
1910 (setq err-string
1911 (with-output-to-string
1912 (setq exitcode
1913 (apply 'call-process (car cmd)
1914 nil standard-output nil (cdr cmd)))))
1915 (or (zerop exitcode)
1916 (ignore (message "%s" err-string))
1917 (error "Unable to create odt file (%S)" exitcode)))
1918 cmds))
1920 ;; move the file from outdir to target-dir
1921 (rename-file target-name target-dir)
1923 ;; kill all xml buffers
1924 (mapc (lambda (file)
1925 (kill-buffer
1926 (find-file-noselect (expand-file-name file zipdir) t)))
1927 xml-files)
1929 (delete-directory zipdir)))
1930 (message "Created %s" target)
1931 (set-buffer (find-file-noselect target t)))
1933 (defconst org-odt-manifest-file-entry-tag
1935 <manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
1937 (defvar org-odt-manifest-file-entries nil)
1939 (defun org-odt-create-manifest-file-entry (&rest args)
1940 (push args org-odt-manifest-file-entries))
1942 (defun org-odt-write-manifest-file ()
1943 (make-directory "META-INF")
1944 (let ((manifest-file (expand-file-name "META-INF/manifest.xml")))
1945 (write-region
1946 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
1947 <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n"
1948 nil manifest-file)
1949 (mapc
1950 (lambda (file-entry)
1951 (let* ((version (nth 2 file-entry))
1952 (extra (if version
1953 (format " manifest:version=\"%s\"" version)
1954 "")))
1955 (write-region
1956 (format org-odt-manifest-file-entry-tag
1957 (nth 0 file-entry) (nth 1 file-entry) extra)
1958 nil manifest-file t))) org-odt-manifest-file-entries)
1959 (write-region "\n</manifest:manifest>" nil manifest-file t)))
1961 (defun org-odt-update-meta-file (opt-plist)
1962 (let ((date (org-odt-iso-date-from-org-timestamp
1963 (plist-get opt-plist :date)))
1964 (author (or (plist-get opt-plist :author) ""))
1965 (email (plist-get opt-plist :email))
1966 (keywords (plist-get opt-plist :keywords))
1967 (description (plist-get opt-plist :description))
1968 (title (plist-get opt-plist :title)))
1969 (write-region
1970 (concat
1971 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
1972 <office:document-meta
1973 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
1974 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
1975 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
1976 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
1977 xmlns:ooo=\"http://openoffice.org/2004/office\"
1978 office:version=\"1.2\">
1979 <office:meta>" "\n"
1980 (org-odt-format-author)
1981 (org-odt-format-tags
1982 '("\n<meta:initial-creator>" . "</meta:initial-creator>") author)
1983 (org-odt-format-tags '("\n<dc:date>" . "</dc:date>") date)
1984 (org-odt-format-tags
1985 '("\n<meta:creation-date>" . "</meta:creation-date>") date)
1986 (org-odt-format-tags '("\n<meta:generator>" . "</meta:generator>")
1987 (when org-export-creator-info
1988 (format "Org-%s/Emacs-%s"
1989 org-version emacs-version)))
1990 (org-odt-format-tags '("\n<meta:keyword>" . "</meta:keyword>") keywords)
1991 (org-odt-format-tags '("\n<dc:subject>" . "</dc:subject>") description)
1992 (org-odt-format-tags '("\n<dc:title>" . "</dc:title>") title)
1993 "\n"
1994 " </office:meta>" "</office:document-meta>")
1995 nil (expand-file-name "meta.xml")))
1997 ;; create a manifest entry for meta.xml
1998 (org-odt-create-manifest-file-entry "text/xml" "meta.xml"))
2000 (defun org-odt-finalize-outfile ()
2001 (org-odt-delete-empty-paragraphs))
2003 (defun org-odt-delete-empty-paragraphs ()
2004 (goto-char (point-min))
2005 (let ((open "<text:p[^>]*>")
2006 (close "</text:p>"))
2007 (while (re-search-forward (format "%s[ \r\n\t]*%s" open close) nil t)
2008 (replace-match ""))))
2010 (defcustom org-export-odt-convert-processes
2011 '(("BasicODConverter"
2012 ("soffice" "-norestore" "-invisible" "-headless"
2013 "\"macro:///BasicODConverter.Main.Convert(%I,%f,%O)\""))
2014 ("unoconv"
2015 ("unoconv" "-f" "%f" "-o" "%d" "%i")))
2016 "Specify a list of document converters and their usage.
2017 The converters in this list are offered as choices while
2018 customizing `org-export-odt-convert-process'.
2020 This variable is an alist where each element is of the
2021 form (CONVERTER-NAME CONVERTER-PROCESS). CONVERTER-NAME is name
2022 of the converter. CONVERTER-PROCESS specifies the command-line
2023 syntax of the converter and is of the form (CONVERTER-PROGRAM
2024 ARG1 ARG2 ...). CONVERTER-PROGRAM is the name of the executable.
2025 ARG1, ARG2 etc are command line options that are passed to
2026 CONVERTER-PROGRAM. Format specifiers can be used in the ARGs and
2027 they are interpreted as below:
2029 %i input file name in full
2030 %I input file name as a URL
2031 %f format of the output file
2032 %o output file name in full
2033 %O output file name as a URL
2034 %d output dir in full
2035 %D output dir as a URL."
2036 :group 'org-export-odt
2037 :type
2038 '(choice
2039 (const :tag "None" nil)
2040 (alist :tag "Converters"
2041 :key-type (string :tag "Converter Name")
2042 :value-type (group (cons (string :tag "Executable")
2043 (repeat (string :tag "Command line args")))))))
2045 (defcustom org-export-odt-convert-process nil
2046 "Use this converter to convert from \"odt\" format to other formats.
2047 During customization, the list of converter names are populated
2048 from `org-export-odt-convert-processes'."
2049 :group 'org-export-odt
2050 :type '(choice :convert-widget
2051 (lambda (w)
2052 (apply 'widget-convert (widget-type w)
2053 (eval (car (widget-get w :args)))))
2054 `((const :tag "None" nil)
2055 ,@(mapcar (lambda (c)
2056 `(const :tag ,(car c) ,(car c)))
2057 org-export-odt-convert-processes))))
2059 (defcustom org-export-odt-convert-capabilities
2060 '(("Text"
2061 ("odt" "ott" "doc" "rtf")
2062 (("pdf" "pdf") ("odt" "odt") ("xhtml" "html") ("rtf" "rtf")
2063 ("ott" "ott") ("doc" "doc") ("ooxml" "xml") ("html" "html")))
2064 ("Web"
2065 ("html" "xhtml") (("pdf" "pdf") ("odt" "txt") ("html" "html")))
2066 ("Spreadsheet"
2067 ("ods" "ots" "xls" "csv")
2068 (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv")
2069 ("ods" "ods") ("xls" "xls") ("xhtml" "xhtml") ("ooxml" "xml")))
2070 ("Presentation"
2071 ("odp" "otp" "ppt")
2072 (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("xhtml" "xml")
2073 ("otp" "otp") ("ppt" "ppt") ("odg" "odg") ("html" "html"))))
2074 "Specify input and output formats of `org-export-odt-convert-process'.
2075 More correctly, specify the set of input and output formats that
2076 the user is actually interested in.
2078 This variable is an alist where each element is of the
2079 form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
2080 INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
2081 alist where each element is of the form (OUTPUT-FMT
2082 OUTPUT-FILE-EXTENSION).
2084 The variable is interpreted as follows:
2085 `org-export-odt-convert-process' can take any document that is in
2086 INPUT-FMT-LIST and produce any document that is in the
2087 OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
2088 OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
2089 serves dual purposes:
2090 - It is used for populating completion candidates during
2091 `org-export-odt-convert' commands.
2092 - It is used as the value of \"%f\" specifier in
2093 `org-export-odt-convert-process'.
2095 DOCUMENT-CLASS is used to group a set of file formats in
2096 INPUT-FMT-LIST in to a single class.
2098 Note that this variable inherently captures how LibreOffice based
2099 converters work. LibreOffice maps documents of various formats
2100 to classes like Text, Web, Spreadsheet, Presentation etc and
2101 allow document of a given class (irrespective of it's source
2102 format) to be converted to any of the export formats associated
2103 with that class.
2105 See default setting of this variable for an typical
2106 configuration."
2107 :group 'org-export-odt
2108 :type
2109 '(choice
2110 (const :tag "None" nil)
2111 (alist :key-type (string :tag "Document Class")
2112 :value-type
2113 (group (repeat :tag "Input formats" (string :tag "Input format"))
2114 (alist :tag "Output formats"
2115 :key-type (string :tag "Output format")
2116 :value-type
2117 (group (string :tag "Output file extension")))))))
2119 (defun org-export-odt-convert (&optional in-file out-fmt prefix-arg)
2120 "Convert IN-FILE to format OUT-FMT using a command line converter.
2121 IN-FILE is the file to be converted. If unspecified, it defaults
2122 to variable `buffer-file-name'. OUT-FMT is the desired output
2123 format. Use `org-export-odt-convert-process' as the converter.
2124 If PREFIX-ARG is non-nil then the newly converted file is opened
2125 using `org-open-file'."
2126 (interactive
2127 (append (org-lparse-convert-read-params) current-prefix-arg))
2128 (org-lparse-do-convert in-file out-fmt prefix-arg))
2130 (defun org-odt-get (what &optional opt-plist)
2131 (case what
2132 (BACKEND 'odt)
2133 (EXPORT-DIR (org-export-directory :html opt-plist))
2134 (FILE-NAME-EXTENSION "odt")
2135 (EXPORT-BUFFER-NAME "*Org ODT Export*")
2136 (ENTITY-CONTROL org-odt-entity-control-callbacks-alist)
2137 (ENTITY-FORMAT org-odt-entity-format-callbacks-alist)
2138 (INIT-METHOD 'org-odt-init-outfile)
2139 (FINAL-METHOD 'org-odt-finalize-outfile)
2140 (SAVE-METHOD 'org-odt-save-as-outfile)
2141 (CONVERT-METHOD
2142 (and org-export-odt-convert-process
2143 (cadr (assoc-string org-export-odt-convert-process
2144 org-export-odt-convert-processes t))))
2145 (CONVERT-CAPABILITIES
2146 (and org-export-odt-convert-process
2147 (cadr (assoc-string org-export-odt-convert-process
2148 org-export-odt-convert-processes t))
2149 org-export-odt-convert-capabilities))
2150 (TOPLEVEL-HLEVEL 1)
2151 (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps)
2152 (INLINE-IMAGES 'maybe)
2153 (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
2154 (PLAIN-TEXT-MAP '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
2155 (TABLE-FIRST-COLUMN-AS-LABELS nil)
2156 (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY "," 'superscript))
2157 (CODING-SYSTEM-FOR-WRITE 'utf-8)
2158 (CODING-SYSTEM-FOR-SAVE 'utf-8)
2159 (t (error "Unknown property: %s" what))))
2161 (defvar org-lparse-latex-fragment-fallback) ; set by org-do-lparse
2162 (defun org-export-odt-do-preprocess-latex-fragments ()
2163 "Convert LaTeX fragments to images."
2164 (let* ((latex-frag-opt (plist-get org-lparse-opt-plist :LaTeX-fragments))
2165 (latex-frag-opt ; massage the options
2166 (or (and (member latex-frag-opt '(mathjax t))
2167 (not (and (fboundp 'org-format-latex-mathml-available-p)
2168 (org-format-latex-mathml-available-p)))
2169 (prog1 org-lparse-latex-fragment-fallback
2170 (org-lparse-warn
2171 (concat
2172 "LaTeX to MathML converter not available. "
2173 (format "Using %S instead."
2174 org-lparse-latex-fragment-fallback)))))
2175 latex-frag-opt))
2176 cache-dir display-msg)
2177 (cond
2178 ((eq latex-frag-opt 'dvipng)
2179 (setq cache-dir "ltxpng/")
2180 (setq display-msg "Creating LaTeX image %s"))
2181 ((member latex-frag-opt '(mathjax t))
2182 (setq latex-frag-opt 'mathml)
2183 (setq cache-dir "ltxmathml/")
2184 (setq display-msg "Creating MathML formula %s")))
2185 (when (and org-current-export-file)
2186 (org-format-latex
2187 (concat cache-dir (file-name-sans-extension
2188 (file-name-nondirectory org-current-export-file)))
2189 org-current-export-dir nil display-msg
2190 nil nil latex-frag-opt))))
2192 (defun org-export-odt-preprocess-latex-fragments ()
2193 (when (equal org-export-current-backend 'odt)
2194 (org-export-odt-do-preprocess-latex-fragments)))
2196 (defun org-export-odt-preprocess-label-references ()
2197 (goto-char (point-min))
2198 (let (label label-components category value pretty-label)
2199 (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t)
2200 (org-if-unprotected-at (match-beginning 1)
2201 (replace-match
2202 (let ((org-lparse-encode-pending t)
2203 (label (match-string 1)))
2204 ;; markup generated below is mostly an eye-candy. At
2205 ;; pre-processing stage, there is no information on which
2206 ;; entity a label reference points to. The actual markup
2207 ;; is generated as part of `org-odt-fixup-label-references'
2208 ;; which gets called at the fag end of export. By this
2209 ;; time we would have seen and collected all the label
2210 ;; definitions in `org-odt-entity-labels-alist'.
2211 (org-odt-format-tags
2212 "<text:sequence-ref text:ref-name=\"%s\"/>" "" label)) t t)))))
2214 ;; process latex fragments as part of
2215 ;; `org-export-preprocess-after-blockquote-hook'. Note that this hook
2216 ;; is the one that is closest and well before the call to
2217 ;; `org-export-attach-captions-and-attributes' in
2218 ;; `org-export-preprocess-stirng'. The above arrangement permits
2219 ;; captions, labels and attributes to be attached to png images
2220 ;; generated out of latex equations.
2221 (add-hook 'org-export-preprocess-after-blockquote-hook
2222 'org-export-odt-preprocess-latex-fragments)
2224 (defun org-export-odt-preprocess (parameters)
2225 (org-export-odt-preprocess-label-references))
2227 (declare-function archive-zip-extract "arc-mode.el" (archive name))
2228 (defun org-odt-zip-extract-one (archive member &optional target)
2229 (require 'arc-mode)
2230 (let* ((target (or target default-directory))
2231 (archive (expand-file-name archive))
2232 (archive-zip-extract
2233 (list "unzip" "-qq" "-o" "-d" target))
2234 exit-code command-output)
2235 (setq command-output
2236 (with-temp-buffer
2237 (setq exit-code (archive-zip-extract archive member))
2238 (buffer-string)))
2239 (unless (zerop exit-code)
2240 (message command-output)
2241 (error "Extraction failed"))))
2243 (defun org-odt-zip-extract (archive members &optional target)
2244 (when (atom members) (setq members (list members)))
2245 (mapc (lambda (member)
2246 (org-odt-zip-extract-one archive member target))
2247 members))
2249 (defun org-odt-copy-styles-file (&optional styles-file)
2250 ;; Non-availability of styles.xml is not a critical error. For now
2251 ;; throw an error purely for aesthetic reasons.
2252 (setq styles-file (or styles-file
2253 org-export-odt-styles-file
2254 (expand-file-name "styles/OrgOdtStyles.xml"
2255 org-odt-data-dir)
2256 (error "org-odt: Missing styles file?")))
2257 (cond
2258 ((listp styles-file)
2259 (let ((archive (nth 0 styles-file))
2260 (members (nth 1 styles-file)))
2261 (org-odt-zip-extract archive members)
2262 (mapc
2263 (lambda (member)
2264 (when (org-file-image-p member)
2265 (let* ((image-type (file-name-extension member))
2266 (media-type (format "image/%s" image-type)))
2267 (org-odt-create-manifest-file-entry media-type member))))
2268 members)))
2269 ((and (stringp styles-file) (file-exists-p styles-file))
2270 (let ((styles-file-type (file-name-extension styles-file)))
2271 (cond
2272 ((string= styles-file-type "xml")
2273 (copy-file styles-file "styles.xml" t))
2274 ((member styles-file-type '("odt" "ott"))
2275 (org-odt-zip-extract styles-file "styles.xml")))))
2277 (error (format "Invalid specification of styles.xml file: %S"
2278 org-export-odt-styles-file))))
2280 ;; create a manifest entry for styles.xml
2281 (org-odt-create-manifest-file-entry "text/xml" "styles.xml"))
2283 (defvar org-export-odt-factory-settings
2284 "d4328fb9d1b6cb211d4320ff546829f26700dc5e"
2285 "SHA1 hash of OrgOdtStyles.xml.")
2287 (defun org-odt-configure-outline-numbering (level)
2288 "Outline numbering is retained only upto LEVEL.
2289 To disable outline numbering pass a LEVEL of 0."
2290 (goto-char (point-min))
2291 (let ((regex
2292 "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
2293 (replacement
2294 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
2295 (while (re-search-forward regex nil t)
2296 (when (> (string-to-number (match-string 2)) level)
2297 (replace-match replacement t nil))))
2298 (save-buffer 0))
2300 (provide 'org-odt)
2302 ;;; org-odt.el ends here