org-element: Verse blocks now contain objects
[org-mode/org-mode-NeilSmithlineMods.git] / contrib / lisp / org-e-odt.el
blob263c4eb9064ec0d738135cfe4071a7bb4c47ae50
1 ;;; org-e-odt.el --- OpenDocument Text exporter for Org-mode
3 ;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
5 ;; Author: Jambunathan K <kjambunathan at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;;; Code:
27 (eval-when-compile
28 (require 'cl))
30 ;; FIXMES
31 ;; org-e-odt-preprocess-latex-fragments
32 ;; org-export-as-e-odt-and-open
33 ;; org-export-as-e-odt-batch
34 ;; org-export-as-e-odt
36 (defun org-e-odt-get-style-name-for-entity (category &optional entity)
37 (let ((entity (or entity 'default)))
38 (or
39 (cdr (assoc entity (cdr (assoc category
40 org-e-odt-org-styles-alist))))
41 (cdr (assoc entity (cdr (assoc category
42 org-e-odt-default-org-styles-alist))))
43 (error "Cannot determine style name for entity %s of type %s"
44 entity category))))
46 ;; Following variable is let bound when `org-do-lparse' is in
47 ;; progress. See org-html.el.
49 (defun org-e-odt-format-preamble (info)
50 (let* ((title (org-export-secondary-string
51 (plist-get info :title) 'e-odt info))
52 (author (and (plist-get info :with-author)
53 (let ((auth (plist-get info :author)))
54 (and auth (org-export-secondary-string
55 auth 'e-odt info)))))
56 (date (plist-get info :date))
57 (iso-date (org-e-odt-format-date date))
58 (date (org-e-odt-format-date date "%d %b %Y"))
59 (email (plist-get info :email))
60 ;; switch on or off above vars based on user settings
61 (author (and (plist-get info :with-author) (or author email)))
62 ;; (date (and (plist-get info :time-stamp-file) date))
63 (email (and (plist-get info :with-email) email)))
64 (concat
65 ;; title
66 (when title
67 (concat
68 (org-e-odt-format-stylized-paragraph
69 'title (format "\n<text:title>%s</text:title>" title))
70 ;; separator
71 "\n<text:p text:style-name=\"OrgTitle\"/>"))
72 (cond
73 ((and author (not email))
74 ;; author only
75 (concat
76 (org-e-odt-format-stylized-paragraph
77 'subtitle
78 (format "<text:initial-creator>%s</text:initial-creator>" author))
79 ;; separator
80 "\n<text:p text:style-name=\"OrgSubtitle\"/>"))
81 ((and author email)
82 ;; author and email
83 (concat
84 (org-e-odt-format-stylized-paragraph
85 'subtitle
86 (org-e-odt-format-link
87 (format "<text:initial-creator>%s</text:initial-creator>" author)
88 (concat "mailto:" email)))
89 ;; separator
90 "\n<text:p text:style-name=\"OrgSubtitle\"/>")))
91 ;; date
92 (when date
93 (concat
94 (org-e-odt-format-stylized-paragraph
95 'subtitle
96 (org-e-odt-format-tags
97 '("<text:date style:data-style-name=\"%s\" text:date-value=\"%s\">"
98 . "</text:date>")
99 date "N75" iso-date))
100 ;; separator
101 "<text:p text:style-name=\"OrgSubtitle\"/>")))))
103 (defun org-e-odt-begin-section (style &optional name)
104 (let ((default-name (car (org-e-odt-add-automatic-style "Section"))))
105 (format "<text:section text:style-name=\"%s\" text:name=\"%s\">"
106 style (or name default-name))))
108 (defun org-e-odt-end-section ()
109 "</text:section>")
111 (defun org-e-odt-begin-paragraph (&optional style)
112 (format "<text:p%s>" (org-e-odt-get-extra-attrs-for-paragraph-style style)))
114 (defun org-e-odt-end-paragraph ()
115 "</text:p>")
117 (defun org-e-odt-get-extra-attrs-for-paragraph-style (style)
118 (let (style-name)
119 (setq style-name
120 (cond
121 ((stringp style) style)
122 ((symbolp style) (org-e-odt-get-style-name-for-entity
123 'paragraph style))))
124 (unless style-name
125 (error "Don't know how to handle paragraph style %s" style))
126 (format " text:style-name=\"%s\"" style-name)))
128 (defun org-e-odt-format-stylized-paragraph (style text)
129 (format "\n<text:p%s>%s</text:p>"
130 (org-e-odt-get-extra-attrs-for-paragraph-style style)
131 text))
133 (defun org-e-odt-format-author (&optional author )
134 (when (setq author (or author (plist-get org-lparse-opt-plist :author)))
135 (format "<dc:creator>%s</dc:creator>" author)))
137 (defun org-e-odt-format-date (&optional org-ts fmt)
138 (save-match-data
139 (let* ((time
140 (and (stringp org-ts)
141 (string-match org-ts-regexp0 org-ts)
142 (apply 'encode-time
143 (org-fix-decoded-time
144 (org-parse-time-string (match-string 0 org-ts) t)))))
145 date)
146 (cond
147 (fmt (format-time-string fmt time))
148 (t (setq date (format-time-string "%Y-%m-%dT%H:%M:%S%z" time))
149 (format "%s:%s" (substring date 0 -2) (substring date -2)))))))
151 (defun org-e-odt-begin-annotation (&optional author date)
152 (concat
153 "<office:annotation>\n"
154 (and author (org-e-odt-format-author author))
155 (org-e-odt-format-tags
156 '("<dc:date>" . "</dc:date>")
157 (org-e-odt-format-date
158 (or date (plist-get org-lparse-opt-plist :date))))
159 (org-e-odt-begin-paragraph)))
161 (defun org-e-odt-end-annotation ()
162 "</office:annotation>")
164 (defun org-e-odt-begin-plain-list (ltype)
165 (let* ((style-name (org-e-odt-get-style-name-for-entity 'list ltype))
166 (extra (concat
167 ;; (if (or org-lparse-list-table-p
168 ;; (and (= 1 (length org-lparse-list-stack))
169 ;; (null org-e-odt-list-stack-stashed)))
170 ;; " text:continue-numbering=\"false\""
171 ;; " text:continue-numbering=\"true\"")
173 " text:continue-numbering=\"true\""
175 (when style-name
176 (format " text:style-name=\"%s\"" style-name)))))
177 (case ltype
178 ((ordered unordered descriptive)
179 (concat
180 ;; (org-e-odt-end-paragraph)
181 (format "<text:list%s>" extra)))
182 (t (error "Unknown list type: %s" ltype)))))
184 (defun org-e-odt-end-plain-list (ltype)
185 (if ltype "</text:list>"
186 (error "Unknown list type: %s" ltype)))
188 (defun org-e-odt-begin-list-item (ltype &optional arg headline)
189 (case ltype
190 (ordered
191 (assert (not headline) t)
192 (let* ((counter arg) (extra ""))
193 (concat "<text:list-item>" ;; (org-e-odt-begin-paragraph)
195 ;; (if (= (length org-lparse-list-stack)
196 ;; (length org-e-odt-list-stack-stashed))
197 ;; "<text:list-header>" "<text:list-item>")
199 (unordered
200 (let* ((id arg) (extra ""))
201 (concat
202 "<text:list-item>"
203 ;; (org-e-odt-begin-paragraph)
204 (if headline (org-e-odt-format-target headline id)
205 (org-e-odt-format-bookmark "" id)))
206 ;; (if (= (length org-lparse-list-stack)
207 ;; (length org-e-odt-list-stack-stashed))
208 ;; "<text:list-header>" "<text:list-item>")
210 (descriptive
211 (assert (not headline) t)
212 (let ((term (or arg "(no term)")))
213 (concat
214 (org-e-odt-format-tags
215 '("<text:list-item>" . "</text:list-item>")
216 (org-e-odt-format-stylized-paragraph 'definition-term term))
217 (org-e-odt-begin-list-item 'unordered)
218 (org-e-odt-begin-plain-list 'descriptive)
219 (org-e-odt-begin-list-item 'unordered))))
220 (t (error "Unknown list type"))))
222 (defun org-e-odt-end-list-item (ltype)
223 (case ltype
224 ((ordered unordered)
225 ;; (org-lparse-insert-tag
226 ;; (if (= (length org-lparse-list-stack)
227 ;; (length org-e-odt-list-stack-stashed))
228 ;; (prog1 "</text:list-header>"
229 ;; (setq org-e-odt-list-stack-stashed nil))
230 ;; "</text:list-item>")
231 "</text:list-item>"
232 ;; )
234 (descriptive
235 (concat
236 (org-e-odt-end-list-item 'unordered)
237 (org-e-odt-end-plain-list 'descriptive)
238 (org-e-odt-end-list-item 'unordered)
240 (t (error "Unknown list type"))))
242 (defun org-e-odt-write-automatic-styles ()
243 "Write automatic styles to \"content.xml\"."
244 (with-current-buffer
245 (find-file-noselect (expand-file-name "content.xml") t)
246 ;; position the cursor
247 (goto-char (point-min))
248 (re-search-forward " </office:automatic-styles>" nil t)
249 (goto-char (match-beginning 0))
250 ;; write automatic table styles
251 (loop for (style-name props) in
252 (plist-get org-e-odt-automatic-styles 'Table) do
253 (when (setq props (or (plist-get props :rel-width) 96))
254 (insert (format org-e-odt-table-style-format style-name props))))))
256 (defun org-e-odt-update-display-level (&optional level)
257 (with-current-buffer
258 (find-file-noselect (expand-file-name "content.xml") t)
259 ;; position the cursor.
260 (goto-char (point-min))
261 ;; remove existing sequence decls.
262 (when (re-search-forward "<text:sequence-decls" nil t)
263 (delete-region (match-beginning 0)
264 (re-search-forward "</text:sequence-decls>" nil nil)))
265 ;; insert new ones.
266 (insert "
267 <text:sequence-decls>")
268 (loop for x in org-e-odt-category-map-alist
269 do (insert (format "
270 <text:sequence-decl text:display-outline-level=\"%d\" text:name=\"%s\"/>"
271 level (nth 1 x))))
272 (insert "
273 </text:sequence-decls>")))
275 (defun org-e-odt-add-automatic-style (object-type &optional object-props)
276 "Create an automatic style of type OBJECT-TYPE with param OBJECT-PROPS.
277 OBJECT-PROPS is (typically) a plist created by passing
278 \"#+ATTR_ODT: \" option of the object in question to
279 `org-e-odt-parse-block-attributes'.
281 Use `org-e-odt-object-counters' to generate an automatic
282 OBJECT-NAME and STYLE-NAME. If OBJECT-PROPS is non-nil, add a
283 new entry in `org-e-odt-automatic-styles'. Return (OBJECT-NAME
284 . STYLE-NAME)."
285 (assert (stringp object-type))
286 (let* ((object (intern object-type))
287 (seqvar object)
288 (seqno (1+ (or (plist-get org-e-odt-object-counters seqvar) 0)))
289 (object-name (format "%s%d" object-type seqno)) style-name)
290 (setq org-e-odt-object-counters
291 (plist-put org-e-odt-object-counters seqvar seqno))
292 (when object-props
293 (setq style-name (format "Org%s" object-name))
294 (setq org-e-odt-automatic-styles
295 (plist-put org-e-odt-automatic-styles object
296 (append (list (list style-name object-props))
297 (plist-get org-e-odt-automatic-styles object)))))
298 (cons object-name style-name)))
300 (defun org-e-odt-begin-toc (lang-specific-heading max-level)
301 (concat
302 (format "
303 <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
304 <text:table-of-content-source text:outline-level=\"%d\">
305 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
306 " max-level lang-specific-heading)
308 (let ((entry-templates ""))
309 (loop for level from 1 upto 10
310 do (setq entry-templates
311 (concat entry-templates
312 (format
314 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
315 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
316 <text:index-entry-chapter/>
317 <text:index-entry-text/>
318 <text:index-entry-link-end/>
319 </text:table-of-content-entry-template>
320 " level level))))
321 entry-templates)
323 (format "
324 </text:table-of-content-source>
326 <text:index-body>
327 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
328 <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
329 </text:index-title>
330 " lang-specific-heading)))
332 (defun org-e-odt-end-toc ()
333 (format "
334 </text:index-body>
335 </text:table-of-content>
338 (defun org-e-odt-format-toc-entry (snumber todo headline tags href)
340 ;; FIXME
341 (setq headline (concat
342 (and org-export-with-section-numbers
343 (concat snumber ". "))
344 headline
345 (and tags
346 (concat
347 (org-e-odt-format-spaces 3)
348 (org-e-odt-format-fontify tags "tag")))))
349 (when todo
350 (setq headline (org-e-odt-format-fontify headline "todo")))
352 (let ((org-e-odt-suppress-xref t))
353 (org-e-odt-format-link headline (concat "#" href))))
355 (defun org-e-odt-format-toc-item (toc-entry level org-last-level)
356 (let ((style (format "Contents_20_%d" level)))
357 (concat "\n" (org-e-odt-format-stylized-paragraph style toc-entry) "\n")))
359 ;; Following variable is let bound during 'ORG-LINK callback. See
360 ;; org-html.el
362 (defun org-e-odt-format-link (desc href &optional attr)
363 (cond
364 ((and (= (string-to-char href) ?#) (not org-e-odt-suppress-xref))
365 (setq href (substring href 1))
366 (let ((xref-format "text"))
367 (when (numberp desc)
368 (setq desc (format "%d" desc) xref-format "number"))
369 (when (listp desc)
370 (setq desc (mapconcat 'identity desc ".") xref-format "chapter"))
371 (setq href (concat org-e-odt-bookmark-prefix href))
372 (org-e-odt-format-tags-simple
373 '("<text:bookmark-ref text:reference-format=\"%s\" text:ref-name=\"%s\">" .
374 "</text:bookmark-ref>")
375 desc xref-format href)))
376 (org-lparse-link-description-is-image
377 (org-e-odt-format-tags
378 '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</draw:a>")
379 desc href (or attr "")))
381 (org-e-odt-format-tags-simple
382 '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</text:a>")
383 desc href (or attr "")))))
385 (defun org-e-odt-format-spaces (n)
386 (cond
387 ((= n 1) " ")
388 ((> n 1) (concat
389 " " (org-e-odt-format-tags "<text:s text:c=\"%d\"/>" "" (1- n))))
390 (t "")))
392 (defun org-e-odt-format-tabs (&optional n)
393 (let ((tab "<text:tab/>")
394 (n (or n 1)))
395 (insert tab)))
397 (defun org-e-odt-format-line-break ()
398 (org-e-odt-format-tags "<text:line-break/>" ""))
400 (defun org-e-odt-format-horizontal-line ()
401 (org-e-odt-format-stylized-paragraph 'horizontal-line ""))
403 (defun org-e-odt-encode-plain-text (line &optional no-whitespace-filling)
404 (setq line (org-e-html-encode-plain-text line))
405 (if no-whitespace-filling line
406 (org-e-odt-fill-tabs-and-spaces line)))
408 (defun org-e-odt-format-line (line)
409 (case org-lparse-dyn-current-environment
410 (fixedwidth (concat
411 (org-e-odt-format-stylized-paragraph
412 'fixedwidth (org-e-odt-encode-plain-text line)) "\n"))
413 (t (concat line "\n"))))
415 (defun org-e-odt-format-comment (fmt &rest args)
416 (let ((comment (apply 'format fmt args)))
417 (format "\n<!-- %s -->\n" comment)))
419 (defun org-e-odt-format-org-entity (wd)
420 (org-entity-get-representation wd 'utf8))
422 (defun org-e-odt-fill-tabs-and-spaces (line)
423 (replace-regexp-in-string
424 "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
425 (cond
426 ((string= s "\t") (org-e-odt-format-tabs))
427 (t (org-e-odt-format-spaces (length s))))) line))
429 (defun org-e-odt-hfy-face-to-css (fn)
430 "Create custom style for face FN.
431 When FN is the default face, use it's foreground and background
432 properties to create \"OrgSrcBlock\" paragraph style. Otherwise
433 use it's color attribute to create a character style whose name
434 is obtained from FN. Currently all attributes of FN other than
435 color are ignored.
437 The style name for a face FN is derived using the following
438 operations on the face name in that order - de-dash, CamelCase
439 and prefix with \"OrgSrc\". For example,
440 `font-lock-function-name-face' is associated with
441 \"OrgSrcFontLockFunctionNameFace\"."
442 (let* ((css-list (hfy-face-to-style fn))
443 (style-name ((lambda (fn)
444 (concat "OrgSrc"
445 (mapconcat
446 'capitalize (split-string
447 (hfy-face-or-def-to-name fn) "-")
448 ""))) fn))
449 (color-val (cdr (assoc "color" css-list)))
450 (background-color-val (cdr (assoc "background" css-list)))
451 (style (and org-e-odt-create-custom-styles-for-srcblocks
452 (cond
453 ((eq fn 'default)
454 (format org-src-block-paragraph-format
455 background-color-val color-val))
457 (format
459 <style:style style:name=\"%s\" style:family=\"text\">
460 <style:text-properties fo:color=\"%s\"/>
461 </style:style>" style-name color-val))))))
462 (cons style-name style)))
464 (defun org-e-odt-insert-custom-styles-for-srcblocks (styles)
465 "Save STYLES used for colorizing of source blocks.
466 Update styles.xml with styles that were collected as part of
467 `org-e-odt-hfy-face-to-css' callbacks."
468 (when styles
469 (with-current-buffer
470 (find-file-noselect (expand-file-name "styles.xml") t)
471 (goto-char (point-min))
472 (when (re-search-forward "</office:styles>" nil t)
473 (goto-char (match-beginning 0))
474 (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n")))))
476 (defun org-e-odt-remap-stylenames (style-name)
478 (cdr (assoc style-name '(("timestamp-wrapper" . "OrgTimestampWrapper")
479 ("timestamp" . "OrgTimestamp")
480 ("timestamp-kwd" . "OrgTimestampKeyword")
481 ("tag" . "OrgTag")
482 ("todo" . "OrgTodo")
483 ("done" . "OrgDone")
484 ("target" . "OrgTarget"))))
485 style-name))
487 (defun org-e-odt-format-fontify (text style &optional id)
488 (let* ((style-name
489 (cond
490 ((stringp style)
491 (org-e-odt-remap-stylenames style))
492 ((symbolp style)
493 (org-e-odt-get-style-name-for-entity 'character style))
494 ((listp style)
495 (assert (< 1 (length style)))
496 (let ((parent-style (pop style)))
497 (mapconcat (lambda (s)
498 ;; (assert (stringp s) t)
499 (org-e-odt-remap-stylenames s)) style "")
500 (org-e-odt-remap-stylenames parent-style)))
501 (t (error "Don't how to handle style %s" style)))))
502 (org-e-odt-format-tags-simple
503 '("<text:span text:style-name=\"%s\">" . "</text:span>")
504 text style-name)))
506 (defun org-e-odt-relocate-relative-path (path dir)
507 (if (file-name-absolute-p path) path
508 (file-relative-name (expand-file-name path dir)
509 (expand-file-name "eyecandy" dir))))
511 (defun org-e-odt-format-formula (element info)
512 (let* ((src (cond
513 ((eq (org-element-type element) 'link) ; FIXME
514 (let* ((type (org-element-property :type element))
515 (raw-path (org-element-property :path element)))
516 (cond
517 ((file-name-absolute-p raw-path)
518 (expand-file-name raw-path))
519 (t raw-path))))
520 ((member (org-element-type element)
521 '(latex-fragment latex-environment))
522 (let* ((latex-frag (org-remove-indentation
523 (org-element-property
524 :value element)))
525 (formula-link (org-e-odt-format-latex
526 latex-frag 'mathml)))
527 (and formula-link
528 (string-match "file:\\([^]]*\\)" formula-link)
529 (match-string 1 formula-link))))
530 (t (error "what is this?"))))
531 (caption-from
532 (case (org-element-type element)
533 (link (org-export-get-parent-paragraph element info))
534 (t element)))
535 (captions (org-e-odt-format-label caption-from info 'definition))
536 (caption (car captions))
537 (href
538 (org-e-odt-format-tags
539 "<draw:object xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
540 (file-name-directory (org-e-odt-copy-formula-file src))))
541 (embed-as (if caption 'paragraph 'character))
542 width height)
543 (cond
544 ((eq embed-as 'character)
545 (org-e-odt-format-entity "InlineFormula" href width height))
547 (let ((table-info nil)
548 (table-info
549 '(:alignment ["c" "c"]
550 :column-groups [nil nil]
551 :row-groups (0)
552 :special-column-p nil :width [8 1]))
553 (org-lparse-table-ncols 2)) ; FIXME
554 (org-e-odt-list-table ; FIXME
555 (list
556 (list
557 (org-e-odt-format-entity
558 "CaptionedDisplayFormula" href width height captions)
559 (let* ((org-e-odt-category-map-alist
560 '(("__Table__" "Table" "value")
561 ("__Figure__" "Illustration" "value")
562 ("__MathFormula__" "Text" "math-label")
563 ("__DvipngImage__" "Equation" "value")
564 ("__Listing__" "Listing" "value"))))
565 (car (org-e-odt-format-label caption-from info 'definition)))))
566 '(table (:attr_odt (":style \"OrgEquation\""))) info))))))
568 (defun org-e-odt-copy-formula-file (path)
569 "Returns the internal name of the file"
570 (let* ((src-file (expand-file-name
571 path (file-name-directory org-current-export-file)))
572 (target-dir (format "Formula-%04d/"
573 (incf org-e-odt-embedded-formulas-count)))
574 (target-file (concat target-dir "content.xml")))
575 (message "Embedding %s as %s ..."
576 (substring-no-properties path) target-file)
578 (make-directory target-dir)
579 (org-e-odt-create-manifest-file-entry
580 "application/vnd.oasis.opendocument.formula" target-dir "1.2")
582 (case (org-e-odt-is-formula-link-p src-file)
583 (mathml
584 (copy-file src-file target-file 'overwrite))
585 (odf
586 (org-e-odt-zip-extract-one src-file "content.xml" target-dir))
588 (error "%s is not a formula file" src-file)))
590 (org-e-odt-create-manifest-file-entry "text/xml" target-file)
591 target-file))
593 (defun org-e-odt-is-formula-link-p (file)
594 (let ((case-fold-search nil))
595 (cond
596 ((string-match "\\.\\(mathml\\|mml\\)\\'" file)
597 'mathml)
598 ((string-match "\\.odf\\'" file)
599 'odf))))
601 (defun org-e-odt-format-org-link (opt-plist type-1 path fragment desc attr
602 descp)
603 "Make a OpenDocument link.
604 OPT-PLIST is an options list.
605 TYPE-1 is the device-type of the link (THIS://foo.html).
606 PATH is the path of the link (http://THIS#location).
607 FRAGMENT is the fragment part of the link, if any (foo.html#THIS).
608 DESC is the link description, if any.
609 ATTR is a string of other attributes of the a element."
610 (declare (special org-lparse-par-open))
611 (save-match-data
612 (let* ((may-inline-p
613 (and (member type-1 '("http" "https" "file"))
614 (org-lparse-should-inline-p path descp)
615 (not fragment)))
616 (type (if (equal type-1 "id") "file" type-1))
617 (filename path)
618 (thefile path))
619 (cond
620 ;; check for inlined images
621 ((and (member type '("file"))
622 (not fragment)
623 (org-file-image-p
624 filename org-e-odt-inline-image-extensions)
625 (not descp))
626 (org-e-odt-format-inline-image thefile))
627 ;; check for embedded formulas
628 ((and (member type '("file"))
629 (not fragment)
630 (org-e-odt-is-formula-link-p filename)
631 (or (not descp)))
632 (org-e-odt-format-formula thefile))
633 ((string= type "coderef")
634 (let* ((ref fragment)
635 (lineno-or-ref (cdr (assoc ref org-export-code-refs)))
636 (desc (and descp desc))
637 (org-e-odt-suppress-xref nil)
638 (href (org-xml-format-href (concat "#coderef-" ref))))
639 (cond
640 ((and (numberp lineno-or-ref) (not desc))
641 (org-e-odt-format-link lineno-or-ref href))
642 ((and (numberp lineno-or-ref) desc
643 (string-match (regexp-quote (concat "(" ref ")")) desc))
644 (format (replace-match "%s" t t desc)
645 (org-e-odt-format-link lineno-or-ref href)))
647 (setq desc (format
648 (if (and desc (string-match
649 (regexp-quote (concat "(" ref ")"))
650 desc))
651 (replace-match "%s" t t desc)
652 (or desc "%s"))
653 lineno-or-ref))
654 (org-e-odt-format-link (org-xml-format-desc desc) href)))))
656 (when (string= type "file")
657 (setq thefile
658 (cond
659 ((file-name-absolute-p path)
660 (concat "file://" (expand-file-name path)))
661 (t (org-e-odt-relocate-relative-path
662 thefile org-current-export-file)))))
664 (when (and (member type '("" "http" "https" "file")) fragment)
665 (setq thefile (concat thefile "#" fragment)))
667 (setq thefile (org-xml-format-href thefile))
669 (when (not (member type '("" "file")))
670 (setq thefile (concat type ":" thefile)))
672 (let ((org-e-odt-suppress-xref nil))
673 (org-e-odt-format-link
674 (org-xml-format-desc desc) thefile attr)))))))
676 (defun org-e-odt-format-anchor (text name &optional class)
677 (org-e-odt-format-target text name))
679 (defun org-e-odt-format-bookmark (text id)
680 (if id
681 (org-e-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id)
682 text))
684 (defun org-e-odt-format-target (text id)
685 (let ((name (concat org-e-odt-bookmark-prefix id)))
686 (concat
687 (and id (org-e-odt-format-tags
688 "<text:bookmark-start text:name=\"%s\"/>" "" name))
689 (org-e-odt-format-bookmark text id)
690 (and id (org-e-odt-format-tags
691 "<text:bookmark-end text:name=\"%s\"/>" "" name)))))
693 (defun org-e-odt-format-footnote (n def)
694 (setq n (format "%d" n))
695 (let ((id (concat "fn" n))
696 (note-class "footnote")
697 (par-style "Footnote"))
698 (org-e-odt-format-tags-simple
699 '("<text:note text:id=\"%s\" text:note-class=\"%s\">" . "</text:note>")
700 (concat
701 (org-e-odt-format-tags-simple
702 '("<text:note-citation>" . "</text:note-citation>") n)
703 (org-e-odt-format-tags-simple
704 '("<text:note-body>" . "</text:note-body>") def))
705 id note-class)))
707 (defun org-e-odt-format-footnote-reference (n def refcnt)
708 (if (= refcnt 1)
709 (org-e-odt-format-footnote n def)
710 (org-e-odt-format-footnote-ref n)))
712 (defun org-e-odt-format-footnote-ref (n)
713 (setq n (format "%d" n))
714 (let ((note-class "footnote")
715 (ref-format "text")
716 (ref-name (concat "fn" n)))
717 (org-e-odt-format-tags-simple
718 '("<text:span text:style-name=\"%s\">" . "</text:span>")
719 (org-e-odt-format-tags-simple
720 '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" . "</text:note-ref>")
721 n note-class ref-format ref-name)
722 "OrgSuperscript")))
724 (defun org-e-odt-element-attributes (element info)
725 (let* ((raw-attr (org-element-property :attr_odt element))
726 (raw-attr (and raw-attr
727 (org-trim (mapconcat #'identity raw-attr " ")))))
728 (unless (and raw-attr (string-match "\\`(.*)\\'" raw-attr))
729 (setq raw-attr (format "(%s)" raw-attr)))
730 (ignore-errors (read raw-attr))))
732 (defun org-e-odt-format-object-description (title description)
733 (concat (and title (org-e-odt-format-tags
734 '("<svg:title>" . "</svg:title>")
735 (org-e-odt-encode-plain-text title t)))
736 (and description (org-e-odt-format-tags
737 '("<svg:desc>" . "</svg:desc>")
738 (org-e-odt-encode-plain-text description t)))))
740 (defun org-e-odt-format-frame (text width height style &optional
741 extra anchor-type)
742 (let ((frame-attrs
743 (concat
744 (if width (format " svg:width=\"%0.2fcm\"" width) "")
745 (if height (format " svg:height=\"%0.2fcm\"" height) "")
746 extra
747 (format " text:anchor-type=\"%s\"" (or anchor-type "paragraph")))))
748 (org-e-odt-format-tags
749 '("<draw:frame draw:style-name=\"%s\"%s>" . "</draw:frame>")
750 (concat text (org-e-odt-format-object-description
751 (get-text-property 0 :title text)
752 (get-text-property 0 :description text)))
753 style frame-attrs)))
755 (defun org-e-odt-format-textbox (text width height style &optional
756 extra anchor-type)
757 (org-e-odt-format-frame
758 (org-e-odt-format-tags
759 '("<draw:text-box %s>" . "</draw:text-box>")
760 text (concat (format " fo:min-height=\"%0.2fcm\"" (or height .2))
761 (unless width
762 (format " fo:min-width=\"%0.2fcm\"" (or width .2)))))
763 width nil style extra anchor-type))
765 (defun org-e-odt-merge-frame-params(default-frame-params user-frame-params)
766 (if (not user-frame-params) default-frame-params
767 (assert (= (length default-frame-params) 3))
768 (assert (= (length user-frame-params) 3))
769 (loop for user-frame-param in user-frame-params
770 for default-frame-param in default-frame-params
771 collect (or user-frame-param default-frame-param))))
773 (defun org-e-odt-copy-image-file (path)
774 "Returns the internal name of the file"
775 (let* ((image-type (file-name-extension path))
776 (media-type (format "image/%s" image-type))
777 (src-file (expand-file-name
778 path (file-name-directory org-current-export-file)))
779 (target-dir "Images/")
780 (target-file
781 (format "%s%04d.%s" target-dir
782 (incf org-e-odt-embedded-images-count) image-type)))
783 (message "Embedding %s as %s ..."
784 (substring-no-properties path) target-file)
786 (when (= 1 org-e-odt-embedded-images-count)
787 (make-directory target-dir)
788 (org-e-odt-create-manifest-file-entry "" target-dir))
790 (copy-file src-file target-file 'overwrite)
791 (org-e-odt-create-manifest-file-entry media-type target-file)
792 target-file))
794 (defun org-e-odt-do-image-size (probe-method file &optional dpi anchor-type)
795 (setq dpi (or dpi org-e-odt-pixels-per-inch))
796 (setq anchor-type (or anchor-type "paragraph"))
797 (flet ((size-in-cms (size-in-pixels)
798 (flet ((pixels-to-cms (pixels)
799 (let* ((cms-per-inch 2.54)
800 (inches (/ pixels dpi)))
801 (* cms-per-inch inches))))
802 (and size-in-pixels
803 (cons (pixels-to-cms (car size-in-pixels))
804 (pixels-to-cms (cdr size-in-pixels)))))))
805 (case probe-method
806 (emacs
807 (size-in-cms (ignore-errors ; Emacs could be in batch mode
808 (clear-image-cache)
809 (image-size (create-image file) 'pixels))))
810 (imagemagick
811 (size-in-cms
812 (let ((dim (shell-command-to-string
813 (format "identify -format \"%%w:%%h\" \"%s\"" file))))
814 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
815 (cons (string-to-number (match-string 1 dim))
816 (string-to-number (match-string 2 dim)))))))
818 (cdr (assoc-string anchor-type
819 org-e-odt-default-image-sizes-alist))))))
821 (defun org-e-odt-image-size-from-file (file &optional user-width
822 user-height scale dpi embed-as)
823 (unless (file-name-absolute-p file)
824 (setq file (expand-file-name
825 file (file-name-directory org-current-export-file))))
826 (let* (size width height)
827 (unless (and user-height user-width)
828 (loop for probe-method in org-e-odt-image-size-probe-method
829 until size
830 do (setq size (org-e-odt-do-image-size
831 probe-method file dpi embed-as)))
832 (or size (error "Cannot determine Image size. Aborting ..."))
833 (setq width (car size) height (cdr size)))
834 (cond
835 (scale
836 (setq width (* width scale) height (* height scale)))
837 ((and user-height user-width)
838 (setq width user-width height user-height))
839 (user-height
840 (setq width (* user-height (/ width height)) height user-height))
841 (user-width
842 (setq height (* user-width (/ height width)) width user-width))
843 (t (ignore)))
844 ;; ensure that an embedded image fits comfortably within a page
845 (let ((max-width (car org-e-odt-max-image-size))
846 (max-height (cdr org-e-odt-max-image-size)))
847 (when (or (> width max-width) (> height max-height))
848 (let* ((scale1 (/ max-width width))
849 (scale2 (/ max-height height))
850 (scale (min scale1 scale2)))
851 (setq width (* scale width) height (* scale height)))))
852 (cons width height)))
854 (defun org-e-odt-format-label (element info op)
855 (let* ((caption-from
856 (case (org-element-type element)
857 (link (org-export-get-parent-paragraph element info))
858 (t element)))
859 ;; get label and caption.
860 (label (org-element-property :name caption-from))
861 (caption (org-element-property :caption caption-from))
862 (short-caption (cdr caption))
863 ;; transcode captions.
864 (caption (and (car caption) (org-export-secondary-string
865 (car caption) 'e-odt info)))
866 (short-caption (and short-caption (org-export-secondary-string
867 short-caption 'e-odt info))))
868 (when (or label caption)
869 (let* ((default-category
870 (cond
871 ((eq (org-element-type element) 'table)
872 "__Table__")
873 ((org-e-odt-standalone-image-p element info)
874 "__Figure__")
875 ((member (org-element-type element)
876 '(latex-environment latex-fragment))
877 (let ((processing-type (plist-get info :LaTeX-fragments)))
878 (cond
879 ((eq processing-type 'dvipng) "__DvipngImage__")
880 ((eq processing-type 'mathjax) "__MathFormula__")
881 ((eq processing-type 't) "__MathFormula__")
882 (t (error "Handle LaTeX:verbatim")))))
883 ((eq (org-element-type element) 'src-block)
884 "__Listing__")
885 (t (error "Handle enumeration of %S" element))))
886 (predicate
887 (cond
888 ((member (org-element-type element)
889 '(table latex-environment src-block))
890 nil)
891 ((org-e-odt-standalone-image-p element info)
892 'org-e-odt-standalone-image-p)
893 (t (error "Handle enumeration of %S" element))))
894 (seqno (org-e-odt-enumerate-element
895 element info predicate)) ; FIXME
896 ;; handle label props.
897 (label-props (assoc default-category org-e-odt-category-map-alist))
898 ;; identify opendocument counter
899 (counter (nth 1 label-props))
900 ;; identify label style
901 (label-style (nth 2 label-props))
902 ;; grok language setting
903 (en-strings (assoc-default "en" org-e-odt-category-strings))
904 (lang (plist-get info :language)) ; FIXME
905 (lang-strings (assoc-default lang org-e-odt-category-strings))
906 ;; retrieve localized category sting
907 (pos (- (length org-e-odt-category-map-alist)
908 (length (memq label-props org-e-odt-category-map-alist))))
909 (category (or (nth pos lang-strings) (nth pos en-strings))))
910 (case op
911 (definition
912 ;; assign an internal label, if user has not provided one
913 (setq label (or label (format "%s-%s" default-category seqno)))
914 (setq label (org-solidify-link-text label))
916 (cons
917 (format-spec
918 (cadr (assoc-string label-style org-e-odt-label-styles t))
919 `((?e . ,category)
920 (?n . ,(org-e-odt-format-tags-simple
921 '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" . "</text:sequence>")
922 seqno label counter counter))
923 (?c . ,(or caption ""))))
924 short-caption))
925 (reference
926 (assert label)
927 (setq label (org-solidify-link-text label))
928 (let* ((fmt (cddr (assoc-string label-style org-e-odt-label-styles t)))
929 (fmt1 (car fmt))
930 (fmt2 (cadr fmt)))
931 (org-e-odt-format-tags-simple
932 '("<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">"
933 . "</text:sequence-ref>")
934 (format-spec fmt2 `((?e . ,category)
935 (?n . ,seqno))) fmt1 label)))
936 (t (error "Unknow %S on label" op)))))))
938 (defun org-e-odt-format-tags-1 (tag text prefix suffix &rest args)
939 (cond
940 ((consp tag)
941 (concat prefix (apply 'format (car tag) args) text suffix
942 (format (cdr tag))))
943 ((stringp tag) ; singleton tag
944 (concat prefix (apply 'format tag args) text))))
946 (defun org-e-odt-format-tags (tag text &rest args)
947 (apply 'org-e-odt-format-tags-1 tag text "\n" "\n" args))
949 (defun org-e-odt-format-tags-simple (tag text &rest args)
950 (apply 'org-e-odt-format-tags-1 tag text nil nil args))
952 (defun org-e-odt-init-outfile ()
953 (unless (executable-find "zip")
954 ;; Not at all OSes ship with zip by default
955 (error "Executable \"zip\" needed for creating OpenDocument files"))
957 (let* ((outdir (make-temp-file
958 (format org-e-odt-tmpdir-prefix 'odt) t)) ; FIXME
959 (content-file (expand-file-name "content.xml" outdir)))
961 ;; reset variables
962 (setq org-e-odt-manifest-file-entries nil
963 org-e-odt-embedded-images-count 0
964 org-e-odt-embedded-formulas-count 0
965 org-e-odt-section-count 0
966 org-e-odt-entity-labels-alist nil
967 org-e-odt-list-stack-stashed nil
968 org-e-odt-automatic-styles nil
969 org-e-odt-object-counters nil
970 org-e-odt-entity-counts-plist nil)
972 ;; let `htmlfontify' know that we are interested in collecting
973 ;; styles - FIXME
975 (setq hfy-user-sheet-assoc nil)
977 ;; init conten.xml
978 (with-current-buffer
979 (find-file-noselect content-file t)
980 (current-buffer))))
982 (defun org-e-odt-save-as-outfile (target opt-plist)
983 ;; write automatic styles
984 (org-e-odt-write-automatic-styles)
986 ;; update display levels
987 (org-e-odt-update-display-level org-e-odt-display-outline-level)
989 ;; write styles file
990 ;; (when (equal org-lparse-backend 'odt) FIXME
991 ;; )
993 ;; (org-e-odt-update-styles-file opt-plist)
995 ;; create mimetype file
996 (let ((mimetype (org-e-odt-write-mimetype-file ;; org-lparse-backend FIXME
997 'odt)))
998 (org-e-odt-create-manifest-file-entry mimetype "/" "1.2"))
1000 ;; create a manifest entry for content.xml
1001 (org-e-odt-create-manifest-file-entry "text/xml" "content.xml")
1003 ;; write out the manifest entries before zipping
1004 (org-e-odt-write-manifest-file)
1006 (let ((xml-files '("mimetype" "META-INF/manifest.xml" "content.xml"
1007 "meta.xml"))
1008 (zipdir default-directory))
1009 (when (or t (equal org-lparse-backend 'odt)) ; FIXME
1010 (push "styles.xml" xml-files))
1011 (message "Switching to directory %s" (expand-file-name zipdir))
1013 ;; save all xml files
1014 (mapc (lambda (file)
1015 (with-current-buffer
1016 (find-file-noselect (expand-file-name file) t)
1017 ;; prettify output if needed
1018 (when org-e-odt-prettify-xml
1019 (indent-region (point-min) (point-max)))
1020 (save-buffer 0)))
1021 xml-files)
1023 (let* ((target-name (file-name-nondirectory target))
1024 (target-dir (file-name-directory target))
1025 (cmds `(("zip" "-mX0" ,target-name "mimetype")
1026 ("zip" "-rmTq" ,target-name "."))))
1027 (when (file-exists-p target)
1028 ;; FIXME: If the file is locked this throws a cryptic error
1029 (delete-file target))
1031 (let ((coding-system-for-write 'no-conversion) exitcode err-string)
1032 (message "Creating odt file...")
1033 (mapc
1034 (lambda (cmd)
1035 (message "Running %s" (mapconcat 'identity cmd " "))
1036 (setq err-string
1037 (with-output-to-string
1038 (setq exitcode
1039 (apply 'call-process (car cmd)
1040 nil standard-output nil (cdr cmd)))))
1041 (or (zerop exitcode)
1042 (ignore (message "%s" err-string))
1043 (error "Unable to create odt file (%S)" exitcode)))
1044 cmds))
1046 ;; move the file from outdir to target-dir
1047 (rename-file target-name target-dir)
1049 ;; kill all xml buffers
1050 (mapc (lambda (file)
1051 (kill-buffer
1052 (find-file-noselect (expand-file-name file zipdir) t)))
1053 xml-files)
1055 (delete-directory zipdir)))
1056 (message "Created %s" target)
1057 (set-buffer (find-file-noselect target t)))
1060 (defun org-e-odt-create-manifest-file-entry (&rest args)
1061 (push args org-e-odt-manifest-file-entries))
1063 (defun org-e-odt-write-manifest-file ()
1064 (make-directory "META-INF")
1065 (let ((manifest-file (expand-file-name "META-INF/manifest.xml")))
1066 (with-current-buffer
1067 (find-file-noselect manifest-file t)
1068 (insert
1069 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
1070 <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
1071 (mapc
1072 (lambda (file-entry)
1073 (let* ((version (nth 2 file-entry))
1074 (extra (if version
1075 (format " manifest:version=\"%s\"" version)
1076 "")))
1077 (insert
1078 (format org-e-odt-manifest-file-entry-tag
1079 (nth 0 file-entry) (nth 1 file-entry) extra))))
1080 org-e-odt-manifest-file-entries)
1081 (insert "\n</manifest:manifest>"))))
1083 (defun org-e-odt-update-meta-file (info) ; FIXME opt-plist
1084 (let ((title (org-export-secondary-string
1085 (plist-get info :title) 'e-odt info))
1086 (author (or (let ((auth (plist-get info :author)))
1087 (and auth (org-export-secondary-string
1088 auth 'e-odt info))) ""))
1089 (date (org-e-odt-format-date (plist-get info :date)))
1090 (email (plist-get info :email))
1091 (keywords (plist-get info :keywords))
1092 (description (plist-get info :description)))
1093 (write-region
1094 (concat
1095 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
1096 <office:document-meta
1097 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
1098 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
1099 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
1100 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
1101 xmlns:ooo=\"http://openoffice.org/2004/office\"
1102 office:version=\"1.2\">
1103 <office:meta>\n"
1104 (org-e-odt-format-author author) "\n"
1105 (format "<meta:initial-creator>%s</meta:initial-creator>\n" author)
1106 (format "<dc:date>%s</dc:date>\n" date)
1107 (format "<meta:creation-date>%s</meta:creation-date>\n" date)
1108 (format "<meta:generator>%s</meta:generator>\n"
1109 (concat (and org-export-creator-info org-export-creator-string)))
1110 (format "<meta:keyword>%s</meta:keyword>\n" keywords)
1111 (format "<dc:subject>%s</dc:subject>\n" description)
1112 (format "<dc:title>%s</dc:title>\n" title)
1113 "\n"
1114 " </office:meta>\n" "</office:document-meta>")
1115 nil (expand-file-name "meta.xml")))
1117 ;; create a manifest entry for meta.xml
1118 (org-e-odt-create-manifest-file-entry "text/xml" "meta.xml"))
1120 (defun org-e-odt-update-styles-file (info)
1121 ;; write styles file
1122 (let ((styles-file (plist-get info :odt-styles-file)))
1123 (org-e-odt-copy-styles-file (and styles-file
1124 (read (org-trim styles-file))))
1126 ;; FIXME: Who is opening an empty styles.xml before this point?
1127 (with-current-buffer
1128 (find-file-noselect (expand-file-name "styles.xml") t)
1129 (revert-buffer t t)))
1131 ;; Write custom styles for source blocks
1132 (org-e-odt-insert-custom-styles-for-srcblocks
1133 (mapconcat
1134 (lambda (style)
1135 (format " %s\n" (cddr style)))
1136 hfy-user-sheet-assoc "")))
1138 (defun org-e-odt-write-mimetype-file (format)
1139 ;; create mimetype file
1140 (let ((mimetype
1141 (case format
1142 (odt "application/vnd.oasis.opendocument.text")
1143 (odf "application/vnd.oasis.opendocument.formula")
1144 (t (error "Unknown OpenDocument backend %S" org-lparse-backend)))))
1145 (write-region mimetype nil (expand-file-name "mimetype"))
1146 mimetype))
1148 (declare-function org-create-math-formula "org"
1149 (latex-frag &optional mathml-file))
1151 (defun org-e-odt-get (what &optional opt-plist)
1152 (case what
1153 (EXPORT-DIR (org-export-directory :html opt-plist))
1154 (TABLE-FIRST-COLUMN-AS-LABELS nil)
1155 (CODING-SYSTEM-FOR-WRITE 'utf-8)
1156 (CODING-SYSTEM-FOR-SAVE 'utf-8)
1157 (t (error "Unknown property: %s" what))))
1159 (defun org-e-odt-do-preprocess-latex-fragments ()
1160 "Convert LaTeX fragments to images."
1161 (let* ((latex-frag-opt (plist-get org-lparse-opt-plist :LaTeX-fragments))
1162 (latex-frag-opt ; massage the options
1163 (or (and (member latex-frag-opt '(mathjax t))
1164 (not (and (fboundp 'org-format-latex-mathml-available-p)
1165 (org-format-latex-mathml-available-p)))
1166 (prog1 org-lparse-latex-fragment-fallback
1167 (org-lparse-warn
1168 (concat
1169 "LaTeX to MathML converter not available. "
1170 (format "Using %S instead."
1171 org-lparse-latex-fragment-fallback)))))
1172 latex-frag-opt))
1173 cache-dir display-msg)
1174 (cond
1175 ((eq latex-frag-opt 'dvipng)
1176 (setq cache-dir "ltxpng/")
1177 (setq display-msg "Creating LaTeX image %s"))
1178 ((member latex-frag-opt '(mathjax t))
1179 (setq latex-frag-opt 'mathml)
1180 (setq cache-dir "ltxmathml/")
1181 (setq display-msg "Creating MathML formula %s")))
1182 (when (and org-current-export-file)
1183 (org-format-latex
1184 (concat cache-dir (file-name-sans-extension
1185 (file-name-nondirectory org-current-export-file)))
1186 org-current-export-dir nil display-msg
1187 nil nil latex-frag-opt))))
1189 (eval-after-load 'org-odt
1190 '(ad-deactivate 'org-format-latex-as-mathml))
1192 ; FIXME
1194 ;; (defadvice org-format-latex-as-mathml ; FIXME
1195 ;; (after org-e-odt-protect-latex-fragment activate)
1196 ;; "Encode LaTeX fragment as XML.
1197 ;; Do this when translation to MathML fails."
1198 ;; (when (or (not (> (length ad-return-value) 0))
1199 ;; (get-text-property 0 'org-protected ad-return-value))
1200 ;; (setq ad-return-value
1201 ;; (org-propertize (org-e-odt-encode-plain-text (ad-get-arg 0))
1202 ;; 'org-protected t))))
1204 (defun org-e-odt-zip-extract-one (archive member &optional target)
1205 (require 'arc-mode)
1206 (let* ((target (or target default-directory))
1207 (archive (expand-file-name archive))
1208 (archive-zip-extract
1209 (list "unzip" "-qq" "-o" "-d" target))
1210 exit-code command-output)
1211 (setq command-output
1212 (with-temp-buffer
1213 (setq exit-code (archive-zip-extract archive member))
1214 (buffer-string)))
1215 (unless (zerop exit-code)
1216 (message command-output)
1217 (error "Extraction failed"))))
1219 (defun org-e-odt-zip-extract (archive members &optional target)
1220 (when (atom members) (setq members (list members)))
1221 (mapc (lambda (member)
1222 (org-e-odt-zip-extract-one archive member target))
1223 members))
1225 (defun org-e-odt-copy-styles-file (&optional styles-file)
1226 ;; Non-availability of styles.xml is not a critical error. For now
1227 ;; throw an error purely for aesthetic reasons.
1228 (setq styles-file (or styles-file
1229 org-e-odt-styles-file
1230 (expand-file-name "OrgOdtStyles.xml"
1231 org-e-odt-styles-dir)
1232 (error "org-e-odt: Missing styles file?")))
1233 (cond
1234 ((listp styles-file)
1235 (let ((archive (nth 0 styles-file))
1236 (members (nth 1 styles-file)))
1237 (org-e-odt-zip-extract archive members)
1238 (mapc
1239 (lambda (member)
1240 (when (org-file-image-p member)
1241 (let* ((image-type (file-name-extension member))
1242 (media-type (format "image/%s" image-type)))
1243 (org-e-odt-create-manifest-file-entry media-type member))))
1244 members)))
1245 ((and (stringp styles-file) (file-exists-p styles-file))
1246 (let ((styles-file-type (file-name-extension styles-file)))
1247 (cond
1248 ((string= styles-file-type "xml")
1249 (copy-file styles-file (expand-file-name "styles.xml") t))
1250 ((member styles-file-type '("odt" "ott"))
1251 (org-e-odt-zip-extract styles-file "styles.xml")))))
1253 (error (format "Invalid specification of styles.xml file: %S"
1254 org-e-odt-styles-file))))
1256 ;; create a manifest entry for styles.xml
1257 (org-e-odt-create-manifest-file-entry "text/xml" "styles.xml"))
1259 (defun org-e-odt-configure-outline-numbering ()
1260 "Outline numbering is retained only upto LEVEL.
1261 To disable outline numbering pass a LEVEL of 0."
1262 (goto-char (point-min))
1263 (let ((regex
1264 "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
1265 (replacement
1266 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1267 (while (re-search-forward regex nil t)
1268 (unless (let ((sec-num (plist-get info :section-numbers))
1269 (level (string-to-number (match-string 2))))
1270 (if (wholenump sec-num) (<= level sec-num) sec-num))
1271 (replace-match replacement t nil))))
1272 (save-buffer 0))
1274 ;;;###autoload
1275 (defun org-export-as-odf (latex-frag &optional odf-file)
1276 "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
1277 Use `org-create-math-formula' to convert LATEX-FRAG first to
1278 MathML. When invoked as an interactive command, use
1279 `org-latex-regexps' to infer LATEX-FRAG from currently active
1280 region. If no LaTeX fragments are found, prompt for it. Push
1281 MathML source to kill ring, if `org-export-copy-to-kill-ring' is
1282 non-nil."
1283 (interactive
1284 `(,(let (frag)
1285 (setq frag (and (setq frag (and (region-active-p)
1286 (buffer-substring (region-beginning)
1287 (region-end))))
1288 (loop for e in org-latex-regexps
1289 thereis (when (string-match (nth 1 e) frag)
1290 (match-string (nth 2 e) frag)))))
1291 (read-string "LaTeX Fragment: " frag nil frag))
1292 ,(let ((odf-filename (expand-file-name
1293 (concat
1294 (file-name-sans-extension
1295 (or (file-name-nondirectory buffer-file-name)))
1296 "." "odf")
1297 (file-name-directory buffer-file-name))))
1298 (read-file-name "ODF filename: " nil odf-filename nil
1299 (file-name-nondirectory odf-filename)))))
1300 (let* ((org-lparse-backend 'odf)
1301 org-lparse-opt-plist
1302 (filename (or odf-file
1303 (expand-file-name
1304 (concat
1305 (file-name-sans-extension
1306 (or (file-name-nondirectory buffer-file-name)))
1307 "." "odf")
1308 (file-name-directory buffer-file-name))))
1309 (buffer (find-file-noselect (org-e-odt-init-outfile filename)))
1310 (coding-system-for-write 'utf-8)
1311 (save-buffer-coding-system 'utf-8))
1312 (set-buffer buffer)
1313 (set-buffer-file-coding-system coding-system-for-write)
1314 (let ((mathml (org-create-math-formula latex-frag)))
1315 (unless mathml (error "No Math formula created"))
1316 (insert mathml)
1317 (or (org-export-push-to-kill-ring
1318 (upcase (symbol-name org-lparse-backend)))
1319 (message "Exporting... done")))
1320 (org-e-odt-save-as-outfile filename nil ; FIXME
1323 ;;;###autoload
1324 (defun org-export-as-odf-and-open ()
1325 "Export LaTeX fragment as OpenDocument formula and immediately open it.
1326 Use `org-export-as-odf' to read LaTeX fragment and OpenDocument
1327 formula file."
1328 (interactive)
1329 (org-lparse-and-open
1330 nil nil nil (call-interactively 'org-export-as-odf)))
1335 ;;; Driver Starts here
1336 ;;; Dependencies
1338 (require 'format-spec)
1339 (eval-when-compile (require 'cl) (require 'table))
1343 ;;; Hooks
1345 ;; FIXME: it already exists in org-e-odt.el
1346 ;;; Function Declarations
1348 (declare-function org-element-property "org-element" (property element))
1349 (declare-function org-element-normalize-string "org-element" (s))
1350 (declare-function org-element-parse-secondary-string
1351 "org-element" (string restriction &optional buffer))
1352 (defvar org-element-string-restrictions)
1353 (defvar org-element-object-restrictions)
1355 (declare-function org-export-clean-table "org-export" (table specialp))
1356 (declare-function org-export-data "org-export" (data backend info))
1357 (declare-function org-export-directory "org-export" (type plist))
1358 (declare-function org-export-expand-macro "org-export" (macro info))
1359 (declare-function org-export-first-sibling-p "org-export" (headline info))
1360 (declare-function org-export-footnote-first-reference-p "org-export"
1361 (footnote-reference info))
1362 (declare-function org-export-get-coderef-format "org-export" (path desc))
1363 (declare-function org-export-get-footnote-definition "org-export"
1364 (footnote-reference info))
1365 (declare-function org-export-get-footnote-number "org-export" (footnote info))
1366 (declare-function org-export-get-previous-element "org-export" (blob info))
1367 (declare-function org-export-get-relative-level "org-export" (headline info))
1368 (declare-function org-export-handle-code
1369 "org-export" (element info &optional num-fmt ref-fmt delayed))
1370 (declare-function org-export-included-file "org-export" (keyword backend info))
1371 (declare-function org-export-inline-image-p "org-export"
1372 (link &optional extensions))
1373 (declare-function org-export-last-sibling-p "org-export" (headline info))
1374 (declare-function org-export-low-level-p "org-export" (headline info))
1375 (declare-function org-export-output-file-name
1376 "org-export" (extension &optional subtreep pub-dir))
1377 (declare-function org-export-resolve-coderef "org-export" (ref info))
1378 (declare-function org-export-resolve-fuzzy-link "org-export" (link info))
1379 (declare-function org-export-secondary-string "org-export"
1380 (secondary backend info))
1381 (declare-function org-export-solidify-link-text "org-export" (s))
1382 (declare-function org-export-table-format-info "org-export" (table))
1383 (declare-function
1384 org-export-to-buffer "org-export"
1385 (backend buffer &optional subtreep visible-only body-only ext-plist))
1386 (declare-function
1387 org-export-to-file "org-export"
1388 (backend file &optional subtreep visible-only body-only ext-plist))
1390 (declare-function org-id-find-id-file "org-id" (id))
1391 (declare-function htmlize-region "ext:htmlize" (beg end))
1392 (declare-function org-pop-to-buffer-same-window
1393 "org-compat" (&optional buffer-or-name norecord label))
1399 (declare-function hfy-face-to-style "htmlfontify" (fn))
1400 (declare-function hfy-face-or-def-to-name "htmlfontify" (fn))
1401 (declare-function archive-zip-extract "arc-mode.el" (archive name))
1403 ;;; Internal Variables
1405 ;;;; ODT Internal Variables
1407 (defconst org-e-odt-lib-dir
1408 (file-name-directory load-file-name)
1409 "Location of ODT exporter.
1410 Use this to infer values of `org-e-odt-styles-dir' and
1411 `org-e-odt-schema-dir'.")
1413 (defvar org-e-odt-data-dir
1414 (expand-file-name "../../etc/" org-e-odt-lib-dir)
1415 "Data directory for ODT exporter.
1416 Use this to infer values of `org-e-odt-styles-dir' and
1417 `org-e-odt-schema-dir'.")
1419 (defconst org-e-odt-special-string-regexps
1420 '(("\\\\-" . "&#x00ad;\\1") ; shy
1421 ("---\\([^-]\\)" . "&#x2014;\\1") ; mdash
1422 ("--\\([^-]\\)" . "&#x2013;\\1") ; ndash
1423 ("\\.\\.\\." . "&#x2026;")) ; hellip
1424 "Regular expressions for special string conversion.")
1426 (defconst org-e-odt-schema-dir-list
1427 (list
1428 (and org-e-odt-data-dir
1429 (expand-file-name "./schema/" org-e-odt-data-dir)) ; bail out
1430 (eval-when-compile
1431 (and (boundp 'org-e-odt-data-dir) org-e-odt-data-dir ; see make install
1432 (expand-file-name "./schema/" org-e-odt-data-dir))))
1433 "List of directories to search for OpenDocument schema files.
1434 Use this list to set the default value of
1435 `org-e-odt-schema-dir'. The entries in this list are
1436 populated heuristically based on the values of `org-e-odt-lib-dir'
1437 and `org-e-odt-data-dir'.")
1439 (defconst org-e-odt-styles-dir-list
1440 (list
1441 (and org-e-odt-data-dir
1442 (expand-file-name "./styles/" org-e-odt-data-dir)) ; bail out
1443 (eval-when-compile
1444 (and (boundp 'org-e-odt-data-dir) org-e-odt-data-dir ; see make install
1445 (expand-file-name "./styles/" org-e-odt-data-dir)))
1446 (expand-file-name "../../etc/styles/" org-e-odt-lib-dir) ; git
1447 (expand-file-name "./etc/styles/" org-e-odt-lib-dir) ; elpa
1448 (expand-file-name "./org/" data-directory) ; system
1450 "List of directories to search for OpenDocument styles files.
1451 See `org-e-odt-styles-dir'. The entries in this list are populated
1452 heuristically based on the values of `org-e-odt-lib-dir' and
1453 `org-e-odt-data-dir'.")
1455 (defconst org-e-odt-styles-dir
1456 (let* ((styles-dir
1457 (catch 'styles-dir
1458 (message "Debug (org-e-odt): Searching for OpenDocument styles files...")
1459 (mapc (lambda (styles-dir)
1460 (when styles-dir
1461 (message "Debug (org-e-odt): Trying %s..." styles-dir)
1462 (when (and (file-readable-p
1463 (expand-file-name
1464 "OrgOdtContentTemplate.xml" styles-dir))
1465 (file-readable-p
1466 (expand-file-name
1467 "OrgOdtStyles.xml" styles-dir)))
1468 (message "Debug (org-e-odt): Using styles under %s"
1469 styles-dir)
1470 (throw 'styles-dir styles-dir))))
1471 org-e-odt-styles-dir-list)
1472 nil)))
1473 (unless styles-dir
1474 (error "Error (org-e-odt): Cannot find factory styles files. Aborting."))
1475 styles-dir)
1476 "Directory that holds auxiliary XML files used by the ODT exporter.
1478 This directory contains the following XML files -
1479 \"OrgOdtStyles.xml\" and \"OrgOdtContentTemplate.xml\". These
1480 XML files are used as the default values of
1481 `org-e-odt-styles-file' and
1482 `org-e-odt-content-template-file'.
1484 The default value of this variable varies depending on the
1485 version of org in use and is initialized from
1486 `org-e-odt-styles-dir-list'. Note that the user could be using org
1487 from one of: org's own private git repository, GNU ELPA tar or
1488 standard Emacs.")
1490 (defconst org-e-odt-tmpdir-prefix "%s-")
1491 (defconst org-e-odt-bookmark-prefix "OrgXref.")
1493 (defconst org-e-odt-manifest-file-entry-tag
1495 <manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
1499 (defvar org-lparse-dyn-first-heading-pos) ; let bound during org-do-lparse
1501 (defvar org-e-odt-suppress-xref nil)
1502 (defvar org-e-odt-file-extensions
1503 '(("odt" . "OpenDocument Text")
1504 ("ott" . "OpenDocument Text Template")
1505 ("odm" . "OpenDocument Master Document")
1506 ("ods" . "OpenDocument Spreadsheet")
1507 ("ots" . "OpenDocument Spreadsheet Template")
1508 ("odg" . "OpenDocument Drawing (Graphics)")
1509 ("otg" . "OpenDocument Drawing Template")
1510 ("odp" . "OpenDocument Presentation")
1511 ("otp" . "OpenDocument Presentation Template")
1512 ("odi" . "OpenDocument Image")
1513 ("odf" . "OpenDocument Formula")
1514 ("odc" . "OpenDocument Chart")))
1516 (defvar org-e-odt-default-org-styles-alist
1517 '((paragraph . ((default . "Text_20_body")
1518 (fixedwidth . "OrgFixedWidthBlock")
1519 (verse . "OrgVerse")
1520 (quote . "Quotations")
1521 (blockquote . "Quotations")
1522 (center . "OrgCenter")
1523 (left . "OrgLeft")
1524 (right . "OrgRight")
1525 (title . "OrgTitle")
1526 (subtitle . "OrgSubtitle")
1527 (footnote . "Footnote")
1528 (src . "OrgSrcBlock")
1529 (illustration . "Illustration")
1530 (table . "Table")
1531 (listing . "Listing")
1532 (definition-term . "Text_20_body_20_bold")
1533 (horizontal-line . "Horizontal_20_Line")))
1534 (character . ((bold . "Bold")
1535 (emphasis . "Emphasis")
1536 (code . "OrgCode")
1537 (verbatim . "OrgCode")
1538 (strike . "Strikethrough")
1539 (underline . "Underline")
1540 (subscript . "OrgSubscript")
1541 (superscript . "OrgSuperscript")))
1542 (list . ((ordered . "OrgNumberedList")
1543 (unordered . "OrgBulletedList")
1544 (descriptive . "OrgDescriptionList"))))
1545 "Default styles for various entities.")
1547 (defvar org-e-odt-org-styles-alist org-e-odt-default-org-styles-alist)
1549 ;;;_. callbacks
1550 ;;;_. control callbacks
1551 ;;;_ , document body
1553 (defvar org-lparse-body-only) ; let bound during org-do-lparse
1554 (defvar org-lparse-opt-plist) ; bound during org-do-lparse
1555 (defvar org-lparse-list-stack) ; dynamically bound in org-do-lparse
1556 (defvar org-e-odt-list-stack-stashed)
1557 (defvar org-lparse-table-ncols)
1558 (defvar org-e-odt-table-rowgrp-open)
1559 (defvar org-e-odt-table-rownum)
1560 (defvar org-e-odt-table-cur-rowgrp-is-hdr)
1561 (defvar org-lparse-table-is-styled)
1562 (defvar org-lparse-table-rowgrp-info)
1563 (defvar org-lparse-table-colalign-vector)
1565 (defvar org-e-odt-table-style nil
1566 "Table style specified by \"#+ATTR_ODT: <style-name>\" line.
1567 This is set during `org-e-odt-begin-table'.")
1569 (defvar org-e-odt-table-style-spec nil
1570 "Entry for `org-e-odt-table-style' in `org-e-odt-table-styles'.")
1573 (defvar org-e-odt-table-style-format
1575 <style:style style:name=\"%s\" style:family=\"table\">
1576 <style:table-properties style:rel-width=\"%d%%\" fo:margin-top=\"0cm\" fo:margin-bottom=\"0.20cm\" table:align=\"center\"/>
1577 </style:style>
1579 "Template for auto-generated Table styles.")
1581 (defvar org-e-odt-automatic-styles '()
1582 "Registry of automatic styles for various OBJECT-TYPEs.
1583 The variable has the following form:
1584 \(\(OBJECT-TYPE-A
1585 \(\(OBJECT-NAME-A.1 OBJECT-PROPS-A.1\)
1586 \(OBJECT-NAME-A.2 OBJECT-PROPS-A.2\) ...\)\)
1587 \(OBJECT-TYPE-B
1588 \(\(OBJECT-NAME-B.1 OBJECT-PROPS-B.1\)
1589 \(OBJECT-NAME-B.2 OBJECT-PROPS-B.2\) ...\)\)
1590 ...\).
1592 OBJECT-TYPEs could be \"Section\", \"Table\", \"Figure\" etc.
1593 OBJECT-PROPS is (typically) a plist created by passing
1594 \"#+ATTR_ODT: \" option to `org-e-odt-parse-block-attributes'.
1596 Use `org-e-odt-add-automatic-style' to add update this variable.'")
1598 (defvar org-e-odt-object-counters nil
1599 "Running counters for various OBJECT-TYPEs.
1600 Use this to generate automatic names and style-names. See
1601 `org-e-odt-add-automatic-style'.")
1603 (defvar org-e-odt-table-indentedp nil)
1604 (defvar org-lparse-table-colalign-info)
1605 (defvar org-lparse-link-description-is-image nil)
1608 (defvar org-src-block-paragraph-format
1609 "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
1610 <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
1611 <style:background-image/>
1612 </style:paragraph-properties>
1613 <style:text-properties fo:color=\"%s\"/>
1614 </style:style>"
1615 "Custom paragraph style for colorized source and example blocks.
1616 This style is much the same as that of \"OrgFixedWidthBlock\"
1617 except that the foreground and background colors are set
1618 according to the default face identified by the `htmlfontify'.")
1620 (defvar hfy-optimisations)
1621 (defvar org-e-odt-embedded-formulas-count 0)
1622 (defvar org-e-odt-entity-frame-styles
1623 '(("As-CharImage" "__Figure__" ("OrgInlineImage" nil "as-char"))
1624 ("ParagraphImage" "__Figure__" ("OrgDisplayImage" nil "paragraph"))
1625 ("PageImage" "__Figure__" ("OrgPageImage" nil "page"))
1626 ("CaptionedAs-CharImage" "__Figure__"
1627 ("OrgCaptionedImage"
1628 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
1629 ("OrgInlineImage" nil "as-char"))
1630 ("CaptionedParagraphImage" "__Figure__"
1631 ("OrgCaptionedImage"
1632 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
1633 ("OrgImageCaptionFrame" nil "paragraph"))
1634 ("CaptionedPageImage" "__Figure__"
1635 ("OrgCaptionedImage"
1636 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
1637 ("OrgPageImageCaptionFrame" nil "page"))
1638 ("InlineFormula" "__MathFormula__" ("OrgInlineFormula" nil "as-char"))
1639 ("DisplayFormula" "__MathFormula__" ("OrgDisplayFormula" nil "as-char"))
1640 ("CaptionedDisplayFormula" "__MathFormula__"
1641 ("OrgCaptionedFormula" nil "paragraph")
1642 ("OrgFormulaCaptionFrame" nil "as-char"))))
1644 (defvar org-e-odt-embedded-images-count 0)
1646 (defvar org-e-odt-image-size-probe-method
1647 (append (and (executable-find "identify") '(imagemagick)) ; See Bug#10675
1648 '(emacs fixed))
1649 "Ordered list of methods for determining image sizes.")
1651 (defvar org-e-odt-default-image-sizes-alist
1652 '(("as-char" . (5 . 0.4))
1653 ("paragraph" . (5 . 5)))
1654 "Hardcoded image dimensions one for each of the anchor
1655 methods.")
1657 ;; A4 page size is 21.0 by 29.7 cms
1658 ;; The default page settings has 2cm margin on each of the sides. So
1659 ;; the effective text area is 17.0 by 25.7 cm
1660 (defvar org-e-odt-max-image-size '(17.0 . 20.0)
1661 "Limiting dimensions for an embedded image.")
1663 (defvar org-e-odt-entity-labels-alist nil
1664 "Associate Labels with the Labeled entities.
1665 Each element of the alist is of the form (LABEL-NAME
1666 CATEGORY-NAME SEQNO LABEL-STYLE-NAME). LABEL-NAME is same as
1667 that specified by \"#+LABEL: ...\" line. CATEGORY-NAME is the
1668 type of the entity that LABEL-NAME is attached to. CATEGORY-NAME
1669 can be one of \"Table\", \"Figure\" or \"Equation\". SEQNO is
1670 the unique number assigned to the referenced entity on a
1671 per-CATEGORY basis. It is generated sequentially and is 1-based.
1672 LABEL-STYLE-NAME is a key `org-e-odt-label-styles'.
1674 See `org-e-odt-add-label-definition' and
1675 `org-e-odt-fixup-label-references'.")
1677 (defvar org-e-odt-entity-counts-plist nil
1678 "Plist of running counters of SEQNOs for each of the CATEGORY-NAMEs.
1679 See `org-e-odt-entity-labels-alist' for known CATEGORY-NAMEs.")
1681 (defvar org-e-odt-label-styles
1682 '(("math-formula" "%c" "text" "(%n)")
1683 ("math-label" "(%n)" "text" "(%n)")
1684 ("category-and-value" "%e %n: %c" "category-and-value" "%e %n")
1685 ("value" "%e %n: %c" "value" "%n"))
1686 "Specify how labels are applied and referenced.
1687 This is an alist where each element is of the
1688 form (LABEL-STYLE-NAME LABEL-ATTACH-FMT LABEL-REF-MODE
1689 LABEL-REF-FMT).
1691 LABEL-ATTACH-FMT controls how labels and captions are attached to
1692 an entity. It may contain following specifiers - %e, %n and %c.
1693 %e is replaced with the CATEGORY-NAME. %n is replaced with
1694 \"<text:sequence ...> SEQNO </text:sequence>\". %c is replaced
1695 with CAPTION. See `org-e-odt-format-label-definition'.
1697 LABEL-REF-MODE and LABEL-REF-FMT controls how label references
1698 are generated. The following XML is generated for a label
1699 reference - \"<text:sequence-ref
1700 text:reference-format=\"LABEL-REF-MODE\" ...> LABEL-REF-FMT
1701 </text:sequence-ref>\". LABEL-REF-FMT may contain following
1702 specifiers - %e and %n. %e is replaced with the CATEGORY-NAME.
1703 %n is replaced with SEQNO. See
1704 `org-e-odt-format-label-reference'.")
1706 (defcustom org-e-odt-category-strings
1707 '(("en" "Table" "Figure" "Equation" "Equation" "Listing"))
1708 "Specify category strings for various captionable entities.
1709 Captionable entity can be one of a Table, an Embedded Image, a
1710 LaTeX fragment (generated with dvipng) or a Math Formula.
1712 For example, when `org-export-default-language' is \"en\", an
1713 embedded image will be captioned as \"Figure 1: Orgmode Logo\".
1714 If you want the images to be captioned instead as \"Illustration
1715 1: Orgmode Logo\", then modify the entry for \"en\" as shown
1716 below.
1718 \(setq org-e-odt-category-strings
1719 '\(\(\"en\" \"Table\" \"Illustration\"
1720 \"Equation\" \"Equation\"\)\)\)"
1721 :group 'org-export-e-odt
1722 :version "24.1"
1723 :type '(repeat (list (string :tag "Language tag")
1724 (choice :tag "Table"
1725 (const :tag "Use Default" nil)
1726 (string :tag "Category string"))
1727 (choice :tag "Figure"
1728 (const :tag "Use Default" nil)
1729 (string :tag "Category string"))
1730 (choice :tag "Math Formula"
1731 (const :tag "Use Default" nil)
1732 (string :tag "Category string"))
1733 (choice :tag "Dvipng Image"
1734 (const :tag "Use Default" nil)
1735 (string :tag "Category string"))
1736 (choice :tag "Listing"
1737 (const :tag "Use Default" nil)
1738 (string :tag "Category string")))))
1740 (defvar org-e-odt-category-map-alist
1741 '(("__Table__" "Table" "value")
1742 ("__Figure__" "Illustration" "value")
1743 ("__MathFormula__" "Text" "math-formula")
1744 ("__DvipngImage__" "Equation" "value")
1745 ("__Listing__" "Listing" "value")
1746 ;; ("__Table__" "Table" "category-and-value")
1747 ;; ("__Figure__" "Figure" "category-and-value")
1748 ;; ("__DvipngImage__" "Equation" "category-and-value")
1750 "Map a CATEGORY-HANDLE to OD-VARIABLE and LABEL-STYLE.
1751 This is a list where each entry is of the form \\(CATEGORY-HANDLE
1752 OD-VARIABLE LABEL-STYLE\\). CATEGORY_HANDLE identifies the
1753 captionable entity in question. OD-VARIABLE is the OpenDocument
1754 sequence counter associated with the entity. These counters are
1755 declared within
1756 \"<text:sequence-decls>...</text:sequence-decls>\" block of
1757 `org-e-odt-content-template-file'. LABEL-STYLE is a key
1758 into `org-e-odt-label-styles' and specifies how a given entity
1759 should be captioned and referenced.
1761 The position of a CATEGORY-HANDLE in this list is used as an
1762 index in to per-language entry for
1763 `org-e-odt-category-strings' to retrieve a CATEGORY-NAME.
1764 This CATEGORY-NAME is then used for qualifying the user-specified
1765 captions on export.")
1767 (defvar org-e-odt-manifest-file-entries nil)
1768 (defvar hfy-user-sheet-assoc) ; bound during org-do-lparse
1769 (defvar org-lparse-latex-fragment-fallback) ; set by org-do-lparse
1772 ;;;; HTML Internal Variables
1774 (defvar org-e-odt-option-alist
1776 ;; (:agenda-style nil nil org-agenda-export-html-style)
1777 ;; (:convert-org-links nil nil org-e-odt-link-org-files-as-html)
1778 ;; ;; FIXME Use (org-xml-encode-org-text-skip-links s) ??
1779 ;; ;; (:expand-quoted-html nil "@" org-e-odt-expand)
1780 ;; (:inline-images nil nil org-e-odt-inline-images)
1781 ;; ;; (:link-home nil nil org-e-odt-link-home) FIXME
1782 ;; ;; (:link-up nil nil org-e-odt-link-up) FIXME
1783 ;; (:style nil nil org-e-odt-style)
1784 ;; (:style-extra nil nil org-e-odt-style-extra)
1785 ;; (:style-include-default nil nil org-e-odt-style-include-default)
1786 ;; (:style-include-scripts nil nil org-e-odt-style-include-scripts)
1787 ;; ;; (:timestamp nil nil org-e-odt-with-timestamp)
1788 ;; (:html-extension nil nil org-e-odt-extension)
1789 ;; (:html-postamble nil nil org-e-odt-postamble)
1790 ;; (:html-preamble nil nil org-e-odt-preamble)
1791 ;; (:html-table-tag nil nil org-e-odt-table-tag)
1792 ;; (:xml-declaration nil nil org-e-odt-xml-declaration)
1793 (:odt-styles-file "ODT_STYLES_FILE" nil nil t)
1794 (:LaTeX-fragments nil "LaTeX" org-export-with-LaTeX-fragments))
1795 "Alist between export properties and ways to set them.
1797 The car of the alist is the property name, and the cdr is a list
1798 like \(KEYWORD OPTION DEFAULT BEHAVIOUR\) where:
1800 KEYWORD is a string representing a buffer keyword, or nil.
1801 OPTION is a string that could be found in an #+OPTIONS: line.
1802 DEFAULT is the default value for the property.
1803 BEHAVIOUR determine how Org should handle multiple keywords for
1804 the same property. It is a symbol among:
1805 nil Keep old value and discard the new one.
1806 t Replace old value with the new one.
1807 `space' Concatenate the values, separating them with a space.
1808 `newline' Concatenate the values, separating them with
1809 a newline.
1810 `split' Split values at white spaces, and cons them to the
1811 previous list.
1813 KEYWORD and OPTION have precedence over DEFAULT.
1815 All these properties should be back-end agnostic. For back-end
1816 specific properties, define a similar variable named
1817 `org-BACKEND-option-alist', replacing BACKEND with the name of
1818 the appropriate back-end. You can also redefine properties
1819 there, as they have precedence over these.")
1821 (defvar html-table-tag nil) ; dynamically scoped into this.
1823 ;; FIXME: it already exists in org-e-odt.el
1824 (defconst org-e-odt-cvt-link-fn
1826 "Function to convert link URLs to exportable URLs.
1827 Takes two arguments, TYPE and PATH.
1828 Returns exportable url as (TYPE PATH), or nil to signal that it
1829 didn't handle this case.
1830 Intended to be locally bound around a call to `org-export-as-html'." )
1835 (defvar org-e-odt-format-table-no-css)
1836 (defvar htmlize-buffer-places) ; from htmlize.el
1837 (defvar body-only) ; dynamically scoped into this.
1839 (defvar org-e-odt-table-rowgrp-open)
1840 (defvar org-e-odt-table-rownum)
1841 (defvar org-e-odt-table-cur-rowgrp-is-hdr)
1842 (defvar org-lparse-table-is-styled)
1845 (defvar org-e-odt-headline-formatter
1846 (lambda (level snumber todo todo-type priority
1847 title tags target extra-targets extra-class)
1848 (concat snumber " " title)))
1852 ;;; User Configuration Variables
1854 (defgroup org-export-e-odt nil
1855 "Options for exporting Org mode files to ODT."
1856 :tag "Org Export ODT"
1857 :group 'org-export)
1859 (defcustom org-e-odt-protect-char-alist
1860 '(("&" . "&amp;")
1861 ("<" . "&lt;")
1862 (">" . "&gt;"))
1863 "Alist of characters to be converted by `org-e-html-protect'."
1864 :group 'org-export-e-html
1865 :type '(repeat (cons (string :tag "Character")
1866 (string :tag "ODT equivalent"))))
1867 (defcustom org-e-odt-schema-dir
1868 (let* ((schema-dir
1869 (catch 'schema-dir
1870 (message "Debug (org-e-odt): Searching for OpenDocument schema files...")
1871 (mapc
1872 (lambda (schema-dir)
1873 (when schema-dir
1874 (message "Debug (org-e-odt): Trying %s..." schema-dir)
1875 (when (and (file-readable-p
1876 (expand-file-name "od-manifest-schema-v1.2-cs01.rnc"
1877 schema-dir))
1878 (file-readable-p
1879 (expand-file-name "od-schema-v1.2-cs01.rnc"
1880 schema-dir))
1881 (file-readable-p
1882 (expand-file-name "schemas.xml" schema-dir)))
1883 (message "Debug (org-e-odt): Using schema files under %s"
1884 schema-dir)
1885 (throw 'schema-dir schema-dir))))
1886 org-e-odt-schema-dir-list)
1887 (message "Debug (org-e-odt): No OpenDocument schema files installed")
1888 nil)))
1889 schema-dir)
1890 "Directory that contains OpenDocument schema files.
1892 This directory contains:
1893 1. rnc files for OpenDocument schema
1894 2. a \"schemas.xml\" file that specifies locating rules needed
1895 for auto validation of OpenDocument XML files.
1897 Use the customize interface to set this variable. This ensures
1898 that `rng-schema-locating-files' is updated and auto-validation
1899 of OpenDocument XML takes place based on the value
1900 `rng-nxml-auto-validate-flag'.
1902 The default value of this variable varies depending on the
1903 version of org in use and is initialized from
1904 `org-e-odt-schema-dir-list'. The OASIS schema files are available
1905 only in the org's private git repository. It is *not* bundled
1906 with GNU ELPA tar or standard Emacs distribution."
1907 :type '(choice
1908 (const :tag "Not set" nil)
1909 (directory :tag "Schema directory"))
1910 :group 'org-export-e-odt
1911 :version "24.1"
1912 :set
1913 (lambda (var value)
1914 "Set `org-e-odt-schema-dir'.
1915 Also add it to `rng-schema-locating-files'."
1916 (let ((schema-dir value))
1917 (set var
1918 (if (and
1919 (file-readable-p
1920 (expand-file-name "od-manifest-schema-v1.2-cs01.rnc" schema-dir))
1921 (file-readable-p
1922 (expand-file-name "od-schema-v1.2-cs01.rnc" schema-dir))
1923 (file-readable-p
1924 (expand-file-name "schemas.xml" schema-dir)))
1925 schema-dir
1926 (when value
1927 (message "Error (org-e-odt): %s has no OpenDocument schema files"
1928 value))
1929 nil)))
1930 (when org-e-odt-schema-dir
1931 (eval-after-load 'rng-loc
1932 '(add-to-list 'rng-schema-locating-files
1933 (expand-file-name "schemas.xml"
1934 org-e-odt-schema-dir))))))
1936 (defcustom org-e-odt-content-template-file nil
1937 "Template file for \"content.xml\".
1938 The exporter embeds the exported content just before
1939 \"</office:text>\" element.
1941 If unspecified, the file named \"OrgOdtContentTemplate.xml\"
1942 under `org-e-odt-styles-dir' is used."
1943 :type 'file
1944 :group 'org-export-e-odt
1945 :version "24.1")
1947 (defcustom org-e-odt-styles-file nil
1948 "Default styles file for use with ODT export.
1949 Valid values are one of:
1950 1. nil
1951 2. path to a styles.xml file
1952 3. path to a *.odt or a *.ott file
1953 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
1954 ...))
1956 In case of option 1, an in-built styles.xml is used. See
1957 `org-e-odt-styles-dir' for more information.
1959 In case of option 3, the specified file is unzipped and the
1960 styles.xml embedded therein is used.
1962 In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
1963 and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
1964 generated odt file. Use relative path for specifying the
1965 FILE-MEMBERS. styles.xml must be specified as one of the
1966 FILE-MEMBERS.
1968 Use options 1, 2 or 3 only if styles.xml alone suffices for
1969 achieving the desired formatting. Use option 4, if the styles.xml
1970 references additional files like header and footer images for
1971 achieving the desired formatting.
1973 Use \"#+ODT_STYLES_FILE: ...\" directive to set this variable on
1974 a per-file basis. For example,
1976 #+ODT_STYLES_FILE: \"/path/to/styles.xml\" or
1977 #+ODT_STYLES_FILE: (\"/path/to/file.ott\" (\"styles.xml\" \"image/hdr.png\"))."
1978 :group 'org-export-e-odt
1979 :version "24.1"
1980 :type
1981 '(choice
1982 (const :tag "Factory settings" nil)
1983 (file :must-match t :tag "styles.xml")
1984 (file :must-match t :tag "ODT or OTT file")
1985 (list :tag "ODT or OTT file + Members"
1986 (file :must-match t :tag "ODF Text or Text Template file")
1987 (cons :tag "Members"
1988 (file :tag " Member" "styles.xml")
1989 (repeat (file :tag "Member"))))))
1992 (defcustom org-e-odt-inline-image-extensions
1993 '("png" "jpeg" "jpg" "gif")
1994 "Extensions of image files that can be inlined into HTML."
1995 :type '(repeat (string :tag "Extension"))
1996 :group 'org-export-e-odt
1997 :version "24.1")
1999 (defcustom org-e-odt-pixels-per-inch display-pixels-per-inch
2000 "Scaling factor for converting images pixels to inches.
2001 Use this for sizing of embedded images. See Info node `(org)
2002 Images in ODT export' for more information."
2003 :type 'float
2004 :group 'org-export-e-odt
2005 :version "24.1")
2007 (defcustom org-e-odt-create-custom-styles-for-srcblocks t
2008 "Whether custom styles for colorized source blocks be automatically created.
2009 When this option is turned on, the exporter creates custom styles
2010 for source blocks based on the advice of `htmlfontify'. Creation
2011 of custom styles happen as part of `org-e-odt-hfy-face-to-css'.
2013 When this option is turned off exporter does not create such
2014 styles.
2016 Use the latter option if you do not want the custom styles to be
2017 based on your current display settings. It is necessary that the
2018 styles.xml already contains needed styles for colorizing to work.
2020 This variable is effective only if
2021 `org-e-odt-fontify-srcblocks' is turned on."
2022 :group 'org-export-e-odt
2023 :version "24.1"
2024 :type 'boolean)
2026 (defcustom org-e-odt-preferred-output-format nil
2027 "Automatically post-process to this format after exporting to \"odt\".
2028 Interactive commands `org-export-as-e-odt' and
2029 `org-export-as-e-odt-and-open' export first to \"odt\" format and
2030 then use `org-e-odt-convert-process' to convert the
2031 resulting document to this format. During customization of this
2032 variable, the list of valid values are populated based on
2033 `org-e-odt-convert-capabilities'."
2034 :group 'org-export-e-odt
2035 :version "24.1"
2036 :type '(choice :convert-widget
2037 (lambda (w)
2038 (apply 'widget-convert (widget-type w)
2039 (eval (car (widget-get w :args)))))
2040 `((const :tag "None" nil)
2041 ,@(mapcar (lambda (c)
2042 `(const :tag ,c ,c))
2043 (org-e-odt-reachable-formats "odt")))))
2045 (defcustom org-e-odt-table-styles
2046 '(("OrgEquation" "OrgEquation"
2047 ((use-first-column-styles . t)
2048 (use-last-column-styles . t))))
2049 "Specify how Table Styles should be derived from a Table Template.
2050 This is a list where each element is of the
2051 form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
2053 TABLE-STYLE-NAME is the style associated with the table through
2054 `org-e-odt-table-style'.
2056 TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
2057 TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
2058 below) that is included in
2059 `org-e-odt-content-template-file'.
2061 TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
2062 \"TableCell\"
2063 PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
2064 \"TableParagraph\"
2065 TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
2066 \"FirstRow\" | \"LastRow\" |
2067 \"EvenRow\" | \"OddRow\" |
2068 \"EvenColumn\" | \"OddColumn\" | \"\"
2069 where \"+\" above denotes string concatenation.
2071 TABLE-CELL-OPTIONS is an alist where each element is of the
2072 form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
2073 TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
2074 `use-last-row-styles' |
2075 `use-first-column-styles' |
2076 `use-last-column-styles' |
2077 `use-banding-rows-styles' |
2078 `use-banding-columns-styles' |
2079 `use-first-row-styles'
2080 ON-OR-OFF := `t' | `nil'
2082 For example, with the following configuration
2084 \(setq org-e-odt-table-styles
2085 '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
2086 \(\(use-first-row-styles . t\)
2087 \(use-first-column-styles . t\)\)\)
2088 \(\"TableWithHeaderColumns\" \"Custom\"
2089 \(\(use-first-column-styles . t\)\)\)\)\)
2091 1. A table associated with \"TableWithHeaderRowsAndColumns\"
2092 style will use the following table-cell styles -
2093 \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
2094 \"CustomTableCell\" and the following paragraph styles
2095 \"CustomFirstRowTableParagraph\",
2096 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
2097 as appropriate.
2099 2. A table associated with \"TableWithHeaderColumns\" style will
2100 use the following table-cell styles -
2101 \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
2102 following paragraph styles
2103 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
2104 as appropriate..
2106 Note that TABLE-TEMPLATE-NAME corresponds to the
2107 \"<table:table-template>\" elements contained within
2108 \"<office:styles>\". The entries (TABLE-STYLE-NAME
2109 TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
2110 \"table:template-name\" and \"table:use-first-row-styles\" etc
2111 attributes of \"<table:table>\" element. Refer ODF-1.2
2112 specification for more information. Also consult the
2113 implementation filed under `org-e-odt-get-table-cell-styles'.
2115 The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
2116 formatting of numbered display equations. Do not delete this
2117 style from the list."
2118 :group 'org-export-e-odt
2119 :version "24.1"
2120 :type '(choice
2121 (const :tag "None" nil)
2122 (repeat :tag "Table Styles"
2123 (list :tag "Table Style Specification"
2124 (string :tag "Table Style Name")
2125 (string :tag "Table Template Name")
2126 (alist :options (use-first-row-styles
2127 use-last-row-styles
2128 use-first-column-styles
2129 use-last-column-styles
2130 use-banding-rows-styles
2131 use-banding-columns-styles)
2132 :key-type symbol
2133 :value-type (const :tag "True" t))))))
2134 (defcustom org-e-odt-fontify-srcblocks t
2135 "Specify whether or not source blocks need to be fontified.
2136 Turn this option on if you want to colorize the source code
2137 blocks in the exported file. For colorization to work, you need
2138 to make available an enhanced version of `htmlfontify' library."
2139 :type 'boolean
2140 :group 'org-export-e-odt
2141 :version "24.1")
2143 (defcustom org-e-odt-prettify-xml t ; FIXME
2144 "Specify whether or not the xml output should be prettified.
2145 When this option is turned on, `indent-region' is run on all
2146 component xml buffers before they are saved. Turn this off for
2147 regular use. Turn this on if you need to examine the xml
2148 visually."
2149 :group 'org-export-e-odt
2150 :version "24.1"
2151 :type 'boolean)
2153 (defcustom org-e-odt-convert-processes
2154 '(("LibreOffice"
2155 "soffice --headless --convert-to %f%x --outdir %d %i")
2156 ("unoconv"
2157 "unoconv -f %f -o %d %i"))
2158 "Specify a list of document converters and their usage.
2159 The converters in this list are offered as choices while
2160 customizing `org-e-odt-convert-process'.
2162 This variable is a list where each element is of the
2163 form (CONVERTER-NAME CONVERTER-CMD). CONVERTER-NAME is the name
2164 of the converter. CONVERTER-CMD is the shell command for the
2165 converter and can contain format specifiers. These format
2166 specifiers are interpreted as below:
2168 %i input file name in full
2169 %I input file name as a URL
2170 %f format of the output file
2171 %o output file name in full
2172 %O output file name as a URL
2173 %d output dir in full
2174 %D output dir as a URL.
2175 %x extra options as set in `org-e-odt-convert-capabilities'."
2176 :group 'org-export-e-odt
2177 :version "24.1"
2178 :type
2179 '(choice
2180 (const :tag "None" nil)
2181 (alist :tag "Converters"
2182 :key-type (string :tag "Converter Name")
2183 :value-type (group (string :tag "Command line")))))
2185 (defcustom org-e-odt-convert-process "LibreOffice"
2186 "Use this converter to convert from \"odt\" format to other formats.
2187 During customization, the list of converter names are populated
2188 from `org-e-odt-convert-processes'."
2189 :group 'org-export-e-odt
2190 :version "24.1"
2191 :type '(choice :convert-widget
2192 (lambda (w)
2193 (apply 'widget-convert (widget-type w)
2194 (eval (car (widget-get w :args)))))
2195 `((const :tag "None" nil)
2196 ,@(mapcar (lambda (c)
2197 `(const :tag ,(car c) ,(car c)))
2198 org-e-odt-convert-processes))))
2200 (defcustom org-e-odt-convert-capabilities
2201 '(("Text"
2202 ("odt" "ott" "doc" "rtf" "docx")
2203 (("pdf" "pdf") ("odt" "odt") ("rtf" "rtf") ("ott" "ott")
2204 ("doc" "doc" ":\"MS Word 97\"") ("docx" "docx") ("html" "html")))
2205 ("Web"
2206 ("html")
2207 (("pdf" "pdf") ("odt" "odt") ("html" "html")))
2208 ("Spreadsheet"
2209 ("ods" "ots" "xls" "csv" "xlsx")
2210 (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv") ("ods" "ods")
2211 ("xls" "xls") ("xlsx" "xlsx")))
2212 ("Presentation"
2213 ("odp" "otp" "ppt" "pptx")
2214 (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("otp" "otp") ("ppt" "ppt")
2215 ("pptx" "pptx") ("odg" "odg"))))
2216 "Specify input and output formats of `org-e-odt-convert-process'.
2217 More correctly, specify the set of input and output formats that
2218 the user is actually interested in.
2220 This variable is an alist where each element is of the
2221 form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
2222 INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
2223 alist where each element is of the form (OUTPUT-FMT
2224 OUTPUT-FILE-EXTENSION EXTRA-OPTIONS).
2226 The variable is interpreted as follows:
2227 `org-e-odt-convert-process' can take any document that is in
2228 INPUT-FMT-LIST and produce any document that is in the
2229 OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
2230 OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
2231 serves dual purposes:
2232 - It is used for populating completion candidates during
2233 `org-e-odt-convert' commands.
2234 - It is used as the value of \"%f\" specifier in
2235 `org-e-odt-convert-process'.
2237 EXTRA-OPTIONS is used as the value of \"%x\" specifier in
2238 `org-e-odt-convert-process'.
2240 DOCUMENT-CLASS is used to group a set of file formats in
2241 INPUT-FMT-LIST in to a single class.
2243 Note that this variable inherently captures how LibreOffice based
2244 converters work. LibreOffice maps documents of various formats
2245 to classes like Text, Web, Spreadsheet, Presentation etc and
2246 allow document of a given class (irrespective of it's source
2247 format) to be converted to any of the export formats associated
2248 with that class.
2250 See default setting of this variable for an typical
2251 configuration."
2252 :group 'org-export-e-odt
2253 :version "24.1"
2254 :type
2255 '(choice
2256 (const :tag "None" nil)
2257 (alist :tag "Capabilities"
2258 :key-type (string :tag "Document Class")
2259 :value-type
2260 (group (repeat :tag "Input formats" (string :tag "Input format"))
2261 (alist :tag "Output formats"
2262 :key-type (string :tag "Output format")
2263 :value-type
2264 (group (string :tag "Output file extension")
2265 (choice
2266 (const :tag "None" nil)
2267 (string :tag "Extra options"))))))))
2269 ;;;; Debugging
2272 ;;;; Document
2274 ;;;; Document Header (Styles)
2276 ;;;; Document Header (Scripts)
2278 ;;;; Document Header (Mathjax)
2280 ;;;; Preamble
2282 ;;;; Postamble
2284 ;;;; Emphasis
2286 ;;;; Todos
2288 ;;;; Tags
2290 ;;;; Time-stamps
2291 ;;;; Statistics Cookie
2292 ;;;; Subscript
2293 ;;;; Superscript
2295 ;;;; Inline images
2297 ;;;; Block
2298 ;;;; Comment
2299 ;;;; Comment Block
2300 ;;;; Drawer
2301 ;;;; Dynamic Block
2302 ;;;; Emphasis
2303 ;;;; Entity
2304 ;;;; Example Block
2305 ;;;; Export Snippet
2306 ;;;; Export Block
2307 ;;;; Fixed Width
2308 ;;;; Footnotes
2310 ;;;; Headline
2311 ;;;; Horizontal Rule
2312 ;;;; Inline Babel Call
2313 ;;;; Inline Src Block
2314 ;;;; Inlinetask
2315 ;;;; Item
2316 ;;;; Keyword
2317 ;;;; Latex Environment
2318 ;;;; Latex Fragment
2319 ;;;; Line Break
2320 ;;;; Link
2321 ;;;; Babel Call
2322 ;;;; Macro
2323 ;;;; Paragraph
2324 ;;;; Plain List
2325 ;;;; Plain Text
2326 ;;;; Property Drawer
2327 ;;;; Quote Block
2328 ;;;; Quote Section
2329 ;;;; Section
2330 ;;;; Radio Target
2331 ;;;; Special Block
2332 ;;;; Src Block
2334 ;;;; Table
2336 ;;;; Target
2337 ;;;; Time-stamp
2339 ;;;; Verbatim
2340 ;;;; Verse Block
2341 ;;;; Headline
2343 ;;;; Links
2344 ;;;; Drawers
2345 ;;;; Inlinetasks
2346 ;;;; Publishing
2348 ;;;; Compilation
2352 ;;; User Configurable Variables (MAYBE)
2354 ;;;; Preamble
2356 ;;;; Headline
2358 ;;;; Emphasis
2360 (defcustom org-e-odt-format-headline-function nil
2361 "Function to format headline text.
2363 This function will be called with 5 arguments:
2364 TODO the todo keyword \(string or nil\).
2365 TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
2366 PRIORITY the priority of the headline \(integer or nil\)
2367 TEXT the main headline text \(string\).
2368 TAGS the tags string, separated with colons \(string or nil\).
2370 The function result will be used in the section format string.
2372 As an example, one could set the variable to the following, in
2373 order to reproduce the default set-up:
2375 \(defun org-e-odt-format-headline \(todo todo-type priority text tags\)
2376 \"Default format function for an headline.\"
2377 \(concat \(when todo
2378 \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo\)\)
2379 \(when priority
2380 \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
2381 text
2382 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
2383 :group 'org-export-e-odt
2384 :type 'function)
2386 ;;;; Footnotes
2388 ;;;; Time-stamps
2390 (defcustom org-e-odt-active-timestamp-format "\\textit{%s}"
2391 "A printf format string to be applied to active time-stamps."
2392 :group 'org-export-e-odt
2393 :type 'string)
2395 (defcustom org-e-odt-inactive-timestamp-format "\\textit{%s}"
2396 "A printf format string to be applied to inactive time-stamps."
2397 :group 'org-export-e-odt
2398 :type 'string)
2400 (defcustom org-e-odt-diary-timestamp-format "\\textit{%s}"
2401 "A printf format string to be applied to diary time-stamps."
2402 :group 'org-export-e-odt
2403 :type 'string)
2406 ;;;; Links
2408 (defcustom org-e-odt-inline-image-rules
2409 '(("file" . "\\.\\(jpeg\\|jpg\\|png\\|gif\\)\\'"))
2410 "Rules characterizing image files that can be inlined into HTML.
2412 A rule consists in an association whose key is the type of link
2413 to consider, and value is a regexp that will be matched against
2414 link's path.
2416 Note that, by default, the image extension *actually* allowed
2417 depend on the way the HTML file is processed. When used with
2418 pdflatex, pdf, jpg and png images are OK. When processing
2419 through dvi to Postscript, only ps and eps are allowed. The
2420 default we use here encompasses both."
2421 :group 'org-export-e-odt
2422 :type '(alist :key-type (string :tag "Type")
2423 :value-type (regexp :tag "Path")))
2425 ;;;; Tables
2427 (defcustom org-e-odt-table-caption-above t
2428 "When non-nil, place caption string at the beginning of the table.
2429 Otherwise, place it near the end."
2430 :group 'org-export-e-odt
2431 :type 'boolean)
2433 ;;;; Drawers
2435 (defcustom org-e-odt-format-drawer-function nil
2436 "Function called to format a drawer in HTML code.
2438 The function must accept two parameters:
2439 NAME the drawer name, like \"LOGBOOK\"
2440 CONTENTS the contents of the drawer.
2442 The function should return the string to be exported.
2444 For example, the variable could be set to the following function
2445 in order to mimic default behaviour:
2447 \(defun org-e-odt-format-drawer-default \(name contents\)
2448 \"Format a drawer element for HTML export.\"
2449 contents\)"
2450 :group 'org-export-e-odt
2451 :type 'function)
2454 ;;;; Inlinetasks
2456 (defcustom org-e-odt-format-inlinetask-function nil
2457 "Function called to format an inlinetask in HTML code.
2459 The function must accept six parameters:
2460 TODO the todo keyword, as a string
2461 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
2462 PRIORITY the inlinetask priority, as a string
2463 NAME the inlinetask name, as a string.
2464 TAGS the inlinetask tags, as a string.
2465 CONTENTS the contents of the inlinetask, as a string.
2467 The function should return the string to be exported.
2469 For example, the variable could be set to the following function
2470 in order to mimic default behaviour:
2472 \(defun org-e-odt-format-inlinetask \(todo type priority name tags contents\)
2473 \"Format an inline task element for HTML export.\"
2474 \(let \(\(full-title
2475 \(concat
2476 \(when todo
2477 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
2478 \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
2479 title
2480 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
2481 \(format \(concat \"\\\\begin{center}\\n\"
2482 \"\\\\fbox{\\n\"
2483 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
2484 \"%s\\n\\n\"
2485 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
2486 \"%s\"
2487 \"\\\\end{minipage}}\"
2488 \"\\\\end{center}\"\)
2489 full-title contents\)\)"
2490 :group 'org-export-e-odt
2491 :type 'function)
2494 ;; Src blocks
2496 ;;;; Plain text
2498 (defcustom org-e-odt-quotes
2499 '(("fr" ("\\(\\s-\\|[[(]\\)\"" . "«~") ("\\(\\S-\\)\"" . "~»") ("\\(\\s-\\|(\\)'" . "'"))
2500 ("en" ("\\(\\s-\\|[[(]\\)\"" . "``") ("\\(\\S-\\)\"" . "''") ("\\(\\s-\\|(\\)'" . "`")))
2501 "Alist for quotes to use when converting english double-quotes.
2503 The CAR of each item in this alist is the language code.
2504 The CDR of each item in this alist is a list of three CONS:
2505 - the first CONS defines the opening quote;
2506 - the second CONS defines the closing quote;
2507 - the last CONS defines single quotes.
2509 For each item in a CONS, the first string is a regexp
2510 for allowed characters before/after the quote, the second
2511 string defines the replacement string for this quote."
2512 :group 'org-export-e-odt
2513 :type '(list
2514 (cons :tag "Opening quote"
2515 (string :tag "Regexp for char before")
2516 (string :tag "Replacement quote "))
2517 (cons :tag "Closing quote"
2518 (string :tag "Regexp for char after ")
2519 (string :tag "Replacement quote "))
2520 (cons :tag "Single quote"
2521 (string :tag "Regexp for char before")
2522 (string :tag "Replacement quote "))))
2525 ;;;; Compilation
2529 ;;; Internal Functions (HTML)
2531 ;; (defun org-e-odt-format-inline-image (path &optional caption label attr)
2532 ;; ;; FIXME: alt text missing here?
2533 ;; (let ((inline-image (format "<img src=\"%s\" alt=\"%s\"/>"
2534 ;; path (file-name-nondirectory path))))
2535 ;; (if (not label) inline-image
2536 ;; (org-e-odt-format-section inline-image "figure" label))))
2538 ;;;; Bibliography
2540 (defun org-e-odt-bibliography ()
2541 "Find bibliography, cut it out and return it."
2542 (catch 'exit
2543 (let (beg end (cnt 1) bib)
2544 (save-excursion
2545 (goto-char (point-min))
2546 (when (re-search-forward
2547 "^[ \t]*<div \\(id\\|class\\)=\"bibliography\"" nil t)
2548 (setq beg (match-beginning 0))
2549 (while (re-search-forward "</?div\\>" nil t)
2550 (setq cnt (+ cnt (if (string= (match-string 0) "<div") +1 -1)))
2551 (when (= cnt 0)
2552 (and (looking-at ">") (forward-char 1))
2553 (setq bib (buffer-substring beg (point)))
2554 (delete-region beg (point))
2555 (throw 'exit bib))))
2556 nil))))
2558 ;;;; Table
2560 (defun org-e-odt-format-table (lines olines)
2561 (let ((org-e-odt-format-table-no-css nil))
2562 (org-lparse-format-table lines olines)))
2564 (defun org-e-odt-splice-attributes (tag attributes)
2565 "Read attributes in string ATTRIBUTES, add and replace in HTML tag TAG."
2566 (if (not attributes)
2568 (let (oldatt newatt)
2569 (setq oldatt (org-extract-attributes-from-string tag)
2570 tag (pop oldatt)
2571 newatt (cdr (org-extract-attributes-from-string attributes)))
2572 (while newatt
2573 (setq oldatt (plist-put oldatt (pop newatt) (pop newatt))))
2574 (if (string-match ">" tag)
2575 (setq tag
2576 (replace-match (concat (org-attributes-to-string oldatt) ">")
2577 t t tag)))
2578 tag)))
2580 (defun org-export-splice-style (style extra)
2581 "Splice EXTRA into STYLE, just before \"</style>\"."
2582 (if (and (stringp extra)
2583 (string-match "\\S-" extra)
2584 (string-match "</style>" style))
2585 (concat (substring style 0 (match-beginning 0))
2586 "\n" extra "\n"
2587 (substring style (match-beginning 0)))
2588 style))
2590 (defun org-e-odt-toc-entry-formatter
2591 (level snumber todo todo-type priority
2592 headline tags target extra-targets extra-class)
2593 (org-e-odt-format-toc-entry snumber todo headline tags target))
2595 (defun org-e-odt-make-string (n string)
2596 (let (out) (dotimes (i n out) (setq out (concat string out)))))
2598 (defun org-e-odt-toc-text (toc-entries)
2599 (let* ((prev-level (1- (nth 1 (car toc-entries))))
2600 (start-level prev-level))
2601 (mapconcat
2602 (lambda (entry)
2603 (let ((headline (nth 0 entry))
2604 (level (nth 1 entry)))
2605 (prog1 (org-e-odt-format-toc-item headline level prev-level)
2606 (setq prev-level level))))
2607 toc-entries "")))
2609 (defun* org-e-odt-format-toc-headline
2610 (todo todo-type priority text tags
2611 &key level section-number headline-label &allow-other-keys)
2612 ;; FIXME
2613 (setq text (concat
2614 (and org-export-with-section-numbers
2615 (concat section-number ". "))
2616 text
2617 (and tags
2618 (concat
2619 (org-e-odt-format-spaces 3)
2620 (org-e-odt-format-fontify tags "tag")))))
2621 (when todo
2622 (setq text (org-e-odt-format-fontify text "todo")))
2624 (let ((org-e-odt-suppress-xref t))
2625 (org-e-odt-format-link text (concat "#" headline-label))))
2627 (defun org-e-odt-toc (depth info)
2628 (assert (wholenump depth))
2629 (let* ((headlines (org-export-collect-headlines info depth))
2630 (toc-entries
2631 (loop for headline in headlines collect
2632 (list (org-e-odt-format-headline--wrap
2633 headline info 'org-e-odt-format-toc-headline)
2634 (org-export-get-relative-level headline info)))))
2635 (when toc-entries
2636 (let* ((lang-specific-heading "Table of Contents")) ; FIXME
2637 (concat
2638 (org-e-odt-begin-toc lang-specific-heading depth)
2639 (org-e-odt-toc-text toc-entries)
2640 (org-e-odt-end-toc))))))
2642 (defun org-e-odt-begin-outline (level1 snumber title tags
2643 target extra-targets extra-class)
2644 (let* ((class (format "outline-%d" level1))
2645 (class (if extra-class (concat class " " extra-class) class))
2646 (id (format "outline-container-%s"
2647 (org-lparse-suffix-from-snumber snumber)))
2648 (extra (concat (when id (format " id=\"%s\"" id))
2649 (when class (format " class=\"%s\"" class)))))
2650 (org-lparse-insert-tag "<div%s>" extra)
2651 (insert
2652 (org-lparse-format 'HEADING
2653 (org-lparse-format
2654 'HEADLINE title extra-targets tags snumber level1)
2655 level1 target))))
2657 (defun org-e-odt-end-outline ()
2658 (org-lparse-insert-tag "</div>"))
2660 (defun org-e-odt-suffix-from-snumber (snumber)
2661 (let* ((snu (replace-regexp-in-string "\\." "-" snumber))
2662 (href (cdr (assoc (concat "sec-" snu)
2663 org-export-preferred-target-alist))))
2664 (org-solidify-link-text (or href snu))))
2666 (defun org-e-odt-format-outline (contents level1 snumber title
2667 tags target extra-targets extra-class)
2670 ;; (defun org-e-odt-format-line (line)
2671 ;; (case org-lparse-dyn-current-environment
2672 ;; ((quote fixedwidth) (concat (org-e-odt-encode-plain-text line) "\n"))
2673 ;; (t (concat line "\n"))))
2675 (defun org-e-odt-fix-class-name (kwd) ; audit callers of this function
2676 "Turn todo keyword into a valid class name.
2677 Replaces invalid characters with \"_\"."
2678 (save-match-data
2679 (while (string-match "[^a-zA-Z0-9_]" kwd)
2680 (setq kwd (replace-match "_" t t kwd))))
2681 kwd)
2683 (defun org-e-odt-format-internal-link (text href &optional extra)
2684 (org-e-odt-format-link text (concat "#" href) extra))
2686 (defun org-e-odt-format-extra-targets (extra-targets)
2687 (if (not extra-targets) ""
2688 (mapconcat (lambda (x)
2689 (when x
2690 (setq x (org-solidify-link-text
2691 (if (org-uuidgen-p x) (concat "ID-" x) x)))
2692 (org-e-odt-format-anchor "" x))) extra-targets "")))
2694 (defun org-e-odt-format-org-tags (tags)
2695 (if (not tags) ""
2696 (org-e-odt-format-fontify
2697 (mapconcat
2698 (lambda (x)
2699 (org-e-odt-format-fontify
2700 x (concat "" ;; org-e-odt-tag-class-prefix
2701 (org-e-odt-fix-class-name x))))
2702 (org-split-string tags ":")
2703 (org-e-odt-format-spaces 1)) "tag")))
2705 (defun org-e-odt-format-section-number (&optional snumber level)
2706 ;; FIXME
2707 (and nil org-export-with-section-numbers
2708 ;; (not org-lparse-body-only)
2709 snumber level
2710 (org-e-odt-format-fontify snumber (format "section-number-%d" level))))
2712 ;; (defun org-e-odt-format-headline (title extra-targets tags
2713 ;; &optional snumber level)
2714 ;; (concat
2715 ;; (org-e-odt-format-extra-targets extra-targets)
2716 ;; (concat (org-e-odt-format-section-number snumber level) " ")
2717 ;; title
2718 ;; (and tags (concat (org-e-odt-format-spaces 3)
2719 ;; (org-e-odt-format-org-tags tags)))))
2721 ;; (defun org-e-odt-format-date (info)
2722 ;; (let ((date (plist-get info :date)))
2723 ;; (cond
2724 ;; ((and date (string-match "%" date))
2725 ;; (format-time-string date))
2726 ;; (date date)
2727 ;; (t (format-time-string "%Y-%m-%d %T %Z")))))
2731 ;;; Internal Functions (Ngz)
2733 (defun org-e-odt--caption/label-string (caption label info)
2734 "Return caption and label HTML string for floats.
2736 CAPTION is a cons cell of secondary strings, the car being the
2737 standard caption and the cdr its short form. LABEL is a string
2738 representing the label. INFO is a plist holding contextual
2739 information.
2741 If there's no caption nor label, return the empty string.
2743 For non-floats, see `org-e-odt--wrap-label'."
2744 (setq label nil) ;; FIXME
2746 (let ((label-str (if label (format "\\label{%s}" label) "")))
2747 (cond
2748 ((and (not caption) (not label)) "")
2749 ((not caption) (format "\\label{%s}\n" label))
2750 ;; Option caption format with short name.
2751 ((cdr caption)
2752 (format "\\caption[%s]{%s%s}\n"
2753 (org-export-secondary-string (cdr caption) 'e-odt info)
2754 label-str
2755 (org-export-secondary-string (car caption) 'e-odt info)))
2756 ;; Standard caption format.
2757 ;; (t (format "\\caption{%s%s}\n"
2758 ;; label-str
2759 ;; (org-export-secondary-string (car caption) 'e-odt info)))
2761 (t (org-export-secondary-string (car caption) 'e-odt info)))))
2763 (defun org-e-odt--find-verb-separator (s)
2764 "Return a character not used in string S.
2765 This is used to choose a separator for constructs like \\verb."
2766 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
2767 (loop for c across ll
2768 when (not (string-match (regexp-quote (char-to-string c)) s))
2769 return (char-to-string c))))
2771 (defun org-e-odt--quotation-marks (text info)
2772 "Export quotation marks depending on language conventions.
2773 TEXT is a string containing quotation marks to be replaced. INFO
2774 is a plist used as a communication channel."
2775 (mapc (lambda(l)
2776 (let ((start 0))
2777 (while (setq start (string-match (car l) text start))
2778 (let ((new-quote (concat (match-string 1 text) (cdr l))))
2779 (setq text (replace-match new-quote t t text))))))
2780 (cdr (or (assoc (plist-get info :language) org-e-odt-quotes)
2781 ;; Falls back on English.
2782 (assoc "en" org-e-odt-quotes))))
2783 text)
2785 (defun org-e-odt--wrap-label (element output)
2786 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
2787 This function shouldn't be used for floats. See
2788 `org-e-odt--caption/label-string'."
2789 ;; (let ((label (org-element-property :name element)))
2790 ;; (if (or (not output) (not label) (string= output "") (string= label ""))
2791 ;; output
2792 ;; (concat (format "\\label{%s}\n" label) output)))
2793 output)
2797 ;;; Transcode Helpers
2799 (defun* org-e-odt-format-headline
2800 (todo todo-type priority text tags
2801 &key level section-number headline-label &allow-other-keys)
2802 (concat (org-e-odt-todo todo) (and todo " ") text
2803 (and tags (org-e-odt-format-spaces 3))
2804 (and tags (org-e-odt-format-org-tags tags))))
2806 ;;;; Src Code
2808 (defun org-e-odt-htmlfontify-string (line)
2809 (let* ((hfy-html-quote-regex "\\([<\"&> ]\\)")
2810 (hfy-html-quote-map '(("\"" "&quot;")
2811 ("<" "&lt;")
2812 ("&" "&amp;")
2813 (">" "&gt;")
2814 (" " "<text:s/>")
2815 (" " "<text:tab/>")))
2816 (hfy-face-to-css 'org-e-odt-hfy-face-to-css)
2817 (hfy-optimisations-1 (copy-seq hfy-optimisations))
2818 (hfy-optimisations (add-to-list 'hfy-optimisations-1
2819 'body-text-only))
2820 (hfy-begin-span-handler
2821 (lambda (style text-block text-id text-begins-block-p)
2822 (insert (format "<text:span text:style-name=\"%s\">" style))))
2823 (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
2824 (htmlfontify-string line)))
2826 (defun org-e-odt-do-format-code
2827 (code &optional lang refs retain-labels num-start)
2828 (let* ((lang (or (assoc-default lang org-src-lang-modes) lang))
2829 (lang-mode (and lang (intern (format "%s-mode" lang))))
2830 (code-lines (org-split-string code "\n"))
2831 (code-length (length code-lines))
2832 (use-htmlfontify-p (and (functionp lang-mode)
2833 org-e-odt-fontify-srcblocks
2834 (require 'htmlfontify nil t)
2835 (fboundp 'htmlfontify-string)))
2836 (code (if (not use-htmlfontify-p) code
2837 (with-temp-buffer
2838 (insert code)
2839 (funcall lang-mode)
2840 (font-lock-fontify-buffer)
2841 (buffer-string))))
2842 (fontifier (if use-htmlfontify-p 'org-e-odt-htmlfontify-string
2843 'org-e-odt-encode-plain-text))
2844 (par-style (if use-htmlfontify-p "OrgSrcBlock"
2845 "OrgFixedWidthBlock"))
2846 (i 0))
2847 (assert (= code-length (length (org-split-string code "\n"))))
2848 (setq code
2849 (org-export-format-code
2850 code
2851 (lambda (loc line-num ref)
2852 (setq par-style
2853 (concat par-style (and (= (incf i) code-length) "LastLine")))
2855 (setq loc (concat loc (and ref retain-labels (format " (%s)" ref))))
2856 (setq loc (funcall fontifier loc))
2857 (when ref
2858 (setq loc (org-e-odt-format-target loc (concat "coderef-" ref))))
2859 (setq loc (org-e-odt-format-stylized-paragraph par-style loc))
2860 (if (not line-num) loc
2861 (org-e-odt-format-tags
2862 '("<text:list-item>" . "</text:list-item>") loc)))
2863 num-start refs))
2864 (cond
2865 ((not num-start) code)
2866 ((equal num-start 0)
2867 (org-e-odt-format-tags
2868 '("<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>"
2869 . "</text:list>") code " text:continue-numbering=\"false\""))
2870 (t (org-e-odt-format-tags
2871 '("<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>"
2872 . "</text:list>") code " text:continue-numbering=\"true\"")))))
2874 (defun org-e-odt-format-code (element info)
2875 (let* ((lang (org-element-property :language element))
2876 ;; Extract code and references.
2877 (code-info (org-export-unravel-code element))
2878 (code (car code-info))
2879 (refs (cdr code-info))
2880 ;; Does the src block contain labels?
2881 (retain-labels (org-element-property :retain-labels element))
2882 ;; Does it have line numbers?
2883 (num-start (case (org-element-property :number-lines element)
2884 (continued (org-export-get-loc element info))
2885 (new 0))))
2886 (org-e-odt-do-format-code code lang refs retain-labels num-start)))
2890 ;;; Template
2892 (defun org-e-odt-template (contents info)
2893 "Return complete document string after HTML conversion.
2894 CONTENTS is the transcoded contents string. RAW-DATA is the
2895 original parsed data. INFO is a plist holding export options."
2896 ;; write meta file
2897 (org-e-odt-update-meta-file info)
2898 (with-temp-buffer
2899 (insert-file-contents
2900 (or org-e-odt-content-template-file
2901 (expand-file-name "OrgOdtContentTemplate.xml"
2902 org-e-odt-styles-dir)))
2903 (goto-char (point-min))
2904 (re-search-forward "</office:text>" nil nil)
2905 (goto-char (match-beginning 0))
2907 ;; Title
2908 (insert (org-e-odt-format-preamble info))
2909 ;; Table of Contents
2910 (let ((depth (plist-get info :with-toc)))
2911 (when (wholenump depth) (insert (org-e-odt-toc depth info))))
2913 ;; Copy styles.xml. Also dump htmlfontify styles, if there is any.
2914 (org-e-odt-update-styles-file info)
2916 ;; Update styles.xml - take care of outline numbering
2917 (with-current-buffer
2918 (find-file-noselect (expand-file-name "styles.xml") t)
2919 ;; Don't make automatic backup of styles.xml file. This setting
2920 ;; prevents the backed-up styles.xml file from being zipped in to
2921 ;; odt file. This is more of a hackish fix. Better alternative
2922 ;; would be to fix the zip command so that the output odt file
2923 ;; includes only the needed files and excludes any auto-generated
2924 ;; extra files like backups and auto-saves etc etc. Note that
2925 ;; currently the zip command zips up the entire temp directory so
2926 ;; that any auto-generated files created under the hood ends up in
2927 ;; the resulting odt file.
2928 (set (make-local-variable 'backup-inhibited) t)
2929 (org-e-odt-configure-outline-numbering))
2931 ;; Contents
2932 (insert contents)
2933 (buffer-substring-no-properties (point-min) (point-max))))
2937 ;;; Transcode Functions
2939 ;;;; Block
2941 (defun org-e-odt-center-block (center-block contents info)
2942 "Transcode a CENTER-BLOCK element from Org to HTML.
2943 CONTENTS holds the contents of the block. INFO is a plist
2944 holding contextual information."
2945 (org-e-odt--wrap-label center-block contents))
2948 ;;;; Comment
2950 ;; Comments are ignored.
2953 ;;;; Comment Block
2955 ;; Comment Blocks are ignored.
2958 ;;;; Drawer
2960 (defun org-e-odt-drawer (drawer contents info)
2961 "Transcode a DRAWER element from Org to HTML.
2962 CONTENTS holds the contents of the block. INFO is a plist
2963 holding contextual information."
2964 (let* ((name (org-element-property :drawer-name drawer))
2965 (output (if (functionp org-e-odt-format-drawer-function)
2966 (funcall org-e-odt-format-drawer-function
2967 name contents)
2968 ;; If there's no user defined function: simply
2969 ;; display contents of the drawer.
2970 contents)))
2971 (org-e-odt--wrap-label drawer output)))
2974 ;;;; Dynamic Block
2976 (defun org-e-odt-dynamic-block (dynamic-block contents info)
2977 "Transcode a DYNAMIC-BLOCK element from Org to HTML.
2978 CONTENTS holds the contents of the block. INFO is a plist
2979 holding contextual information. See
2980 `org-export-data'."
2981 (org-e-odt--wrap-label dynamic-block contents))
2984 ;;;; Emphasis
2986 (defun org-e-odt-emphasis (emphasis contents info)
2987 "Transcode EMPHASIS from Org to HTML.
2988 CONTENTS is the contents of the emphasized text. INFO is a plist
2989 holding contextual information.."
2990 ;; (format (cdr (assoc (org-element-property :marker emphasis)
2991 ;; org-e-odt-emphasis-alist))
2992 ;; contents)
2993 (org-e-odt-format-fontify
2994 contents (cadr (assoc
2995 (org-element-property :marker emphasis)
2996 '(("*" bold)
2997 ("/" emphasis)
2998 ("_" underline)
2999 ("=" code)
3000 ("~" verbatim)
3001 ("+" strike))))))
3004 ;;;; Entity
3006 (defun org-e-odt-entity (entity contents info)
3007 "Transcode an ENTITY object from Org to HTML.
3008 CONTENTS are the definition itself. INFO is a plist holding
3009 contextual information."
3010 ;; (let ((ent (org-element-property :latex entity)))
3011 ;; (if (org-element-property :latex-math-p entity)
3012 ;; (format "$%s$" ent)
3013 ;; ent))
3014 (org-element-property :utf-8 entity))
3017 ;;;; Example Block
3019 (defun org-e-odt-example-block (example-block contents info)
3020 "Transcode a EXAMPLE-BLOCK element from Org to HTML.
3021 CONTENTS is nil. INFO is a plist holding contextual information."
3022 (let* ((options (or (org-element-property :options example-block) ""))
3023 (value (org-export-handle-code example-block info nil nil t)))
3024 (org-e-odt--wrap-label
3025 example-block (org-e-odt-format-source-code-or-example value nil))))
3028 ;;;; Export Snippet
3030 (defun org-e-odt-export-snippet (export-snippet contents info)
3031 "Transcode a EXPORT-SNIPPET object from Org to HTML.
3032 CONTENTS is nil. INFO is a plist holding contextual information."
3033 (when (eq (org-export-snippet-backend export-snippet) 'e-odt)
3034 (org-element-property :value export-snippet)))
3037 ;;;; Export Block
3039 (defun org-e-odt-export-block (export-block contents info)
3040 "Transcode a EXPORT-BLOCK element from Org to HTML.
3041 CONTENTS is nil. INFO is a plist holding contextual information."
3042 (when (string= (org-element-property :type export-block) "latex")
3043 (org-remove-indentation (org-element-property :value export-block))))
3046 ;;;; Fixed Width
3048 (defun org-e-odt-fixed-width (fixed-width contents info)
3049 "Transcode a FIXED-WIDTH element from Org to HTML.
3050 CONTENTS is nil. INFO is a plist holding contextual information."
3051 (let* ((value (org-element-normalize-string
3052 (replace-regexp-in-string
3053 "^[ \t]*: ?" ""
3054 (org-element-property :value fixed-width)))))
3055 (org-e-odt--wrap-label
3056 fixed-width (org-e-odt-format-source-code-or-example value nil))))
3059 ;;;; Footnote Definition
3061 ;; Footnote Definitions are ignored.
3064 ;;;; Footnote Reference
3066 (defun org-e-odt-footnote-def (raw info) ; FIXME
3067 (if (equal (org-element-type raw) 'org-data)
3068 (org-trim (org-export-data raw 'e-odt info)) ; fix paragraph
3069 ; style
3070 (org-e-odt-format-stylized-paragraph
3071 'footnote (org-trim (org-export-secondary-string raw 'e-odt info)))))
3073 (defvar org-e-odt-footnote-separator
3074 (org-e-odt-format-fontify "," 'superscript))
3076 (defun org-e-odt-footnote-reference (footnote-reference contents info)
3077 "Transcode a FOOTNOTE-REFERENCE element from Org to HTML.
3078 CONTENTS is nil. INFO is a plist holding contextual information."
3079 (concat
3080 ;; Insert separator between two footnotes in a row.
3081 (let ((prev (org-export-get-previous-element footnote-reference info)))
3082 (when (eq (org-element-type prev) 'footnote-reference)
3083 org-e-odt-footnote-separator))
3084 (cond
3085 ((not (org-export-footnote-first-reference-p footnote-reference info))
3086 (let* ((n (org-export-get-footnote-number footnote-reference info)))
3087 (org-e-odt-format-footnote-reference n "IGNORED" 100)))
3088 ;; Inline definitions are secondary strings.
3089 ((eq (org-element-property :type footnote-reference) 'inline)
3090 (let* ((raw (org-export-get-footnote-definition footnote-reference info))
3091 (n (org-export-get-footnote-number footnote-reference info))
3092 (def (org-e-odt-footnote-def raw info)))
3093 (org-e-odt-format-footnote-reference n def 1)))
3094 ;; Non-inline footnotes definitions are full Org data.
3096 (let* ((raw (org-export-get-footnote-definition footnote-reference info))
3097 (n (org-export-get-footnote-number footnote-reference info))
3098 (def (org-e-odt-footnote-def raw info)))
3099 (org-e-odt-format-footnote-reference n def 1))))))
3102 ;;;; Headline
3104 (defun org-e-odt-todo (todo)
3105 (when todo
3106 (org-e-odt-format-fontify
3107 (concat
3108 "" ; org-e-odt-todo-kwd-class-prefix
3109 (org-e-odt-fix-class-name todo))
3110 (list (if (member todo org-done-keywords) "done" "todo")
3111 todo))))
3113 (defun org-e-odt-format-headline--wrap (headline info
3114 &optional format-function
3115 &rest extra-keys)
3116 "Transcode an HEADLINE element from Org to ODT.
3117 CONTENTS holds the contents of the headline. INFO is a plist
3118 holding contextual information."
3119 (let* ((level (+ (org-export-get-relative-level headline info)))
3120 (headline-number (org-export-get-headline-number headline info))
3121 (section-number (and (org-export-numbered-headline-p headline info)
3122 (mapconcat 'number-to-string
3123 headline-number ".")))
3124 (todo (and (plist-get info :with-todo-keywords)
3125 (let ((todo (org-element-property
3126 :todo-keyword headline)))
3127 (and todo
3128 (org-export-secondary-string todo 'e-odt info)))))
3129 (todo-type (and todo (org-element-property :todo-type headline)))
3130 (priority (and (plist-get info :with-priority)
3131 (org-element-property :priority headline)))
3132 (text (org-export-secondary-string
3133 (org-element-property :title headline) 'e-odt info))
3134 (tags (and (plist-get info :with-tags)
3135 (org-element-property :tags headline)))
3136 (headline-label (concat "sec-" (mapconcat 'number-to-string
3137 headline-number "-")))
3138 (format-function (cond
3139 ((functionp format-function) format-function)
3140 ((functionp org-e-odt-format-headline-function)
3141 (function*
3142 (lambda (todo todo-type priority text tags
3143 &allow-other-keys)
3144 (funcall org-e-odt-format-headline-function
3145 todo todo-type priority text tags))))
3146 (t 'org-e-odt-format-headline))))
3147 (apply format-function
3148 todo todo-type priority text tags
3149 :headline-label headline-label :level level
3150 :section-number section-number extra-keys)))
3152 (defun org-e-odt-headline (headline contents info)
3153 "Transcode an HEADLINE element from Org to HTML.
3154 CONTENTS holds the contents of the headline. INFO is a plist
3155 holding contextual information."
3156 (let* ((numberedp (org-export-numbered-headline-p headline info))
3157 ;; Get level relative to current parsed data.
3158 (level (org-export-get-relative-level headline info))
3159 (text (org-export-secondary-string
3160 (org-element-property :title headline) 'e-odt info))
3161 ;; Create the headline text.
3162 (full-text (org-e-odt-format-headline--wrap headline info)))
3163 (cond
3164 ;; Case 1: This is a footnote section: ignore it.
3165 ((org-element-property :footnote-section-p headline) nil)
3166 ;; Case 2. This is a deep sub-tree: export it as a list item.
3167 ;; Also export as items headlines for which no section
3168 ;; format has been found.
3169 ((org-export-low-level-p headline info) ; FIXME (or (not section-fmt))
3170 ;; Build the real contents of the sub-tree.
3171 (let* ((type (if numberedp 'unordered 'unordered)) ; FIXME
3172 (itemized-body (org-e-odt-format-list-item
3173 contents type nil nil full-text)))
3174 (concat
3175 (and (org-export-first-sibling-p headline info)
3176 (org-e-odt-begin-plain-list type))
3177 itemized-body
3178 (and (org-export-last-sibling-p headline info)
3179 (org-e-odt-end-plain-list type)))))
3180 ;; Case 3. Standard headline. Export it as a section.
3182 (let* ((extra-ids (list (org-element-property :custom-id headline)
3183 (org-element-property :id headline)))
3184 (extra-ids nil) ; FIXME
3185 (id (concat "sec-" (mapconcat 'number-to-string
3186 (org-export-get-headline-number
3187 headline info) "-"))))
3188 (concat
3189 (org-e-odt-format-tags
3190 '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
3191 "</text:h>")
3192 (concat (org-e-odt-format-extra-targets extra-ids)
3193 (if (not id) full-text (org-e-odt-format-target full-text id) ))
3194 level level)
3195 contents))))))
3198 ;;;; Horizontal Rule
3200 (defun org-e-odt-horizontal-rule (horizontal-rule contents info)
3201 "Transcode an HORIZONTAL-RULE object from Org to HTML.
3202 CONTENTS is nil. INFO is a plist holding contextual information."
3203 (let ((attr (mapconcat #'identity
3204 (org-element-property :attr_odt horizontal-rule)
3205 " ")))
3206 (org-e-odt--wrap-label horizontal-rule
3207 (org-e-odt-format-horizontal-line))))
3210 ;;;; Inline Babel Call
3212 ;; Inline Babel Calls are ignored.
3215 ;;;; Inline Src Block
3217 (defun org-e-odt-inline-src-block (inline-src-block contents info)
3218 "Transcode an INLINE-SRC-BLOCK element from Org to HTML.
3219 CONTENTS holds the contents of the item. INFO is a plist holding
3220 contextual information."
3221 (let* ((org-lang (org-element-property :language inline-src-block))
3222 (code (org-element-property :value inline-src-block))
3223 (separator (org-e-odt--find-verb-separator code)))
3224 (error "FIXME")))
3227 ;;;; Inlinetask
3229 (defun org-e-odt-format-section (text class &optional id)
3230 (let ((extra (concat (when id (format " id=\"%s\"" id)))))
3231 (concat (format "<div class=\"%s\"%s>\n" class extra) text "</div>\n")))
3233 (defun org-e-odt-inlinetask (inlinetask contents info)
3234 "Transcode an INLINETASK element from Org to ODT.
3235 CONTENTS holds the contents of the block. INFO is a plist
3236 holding contextual information."
3237 (cond
3238 ;; If `org-e-odt-format-inlinetask-function' is provided, call it
3239 ;; with appropriate arguments.
3240 ((functionp org-e-odt-format-inlinetask-function)
3241 (let ((format-function
3242 (function*
3243 (lambda (todo todo-type priority text tags
3244 &key contents &allow-other-keys)
3245 (funcall org-e-odt-format-inlinetask-function
3246 todo todo-type priority text tags contents)))))
3247 (org-e-odt-format-headline--wrap
3248 inlinetask info format-function :contents contents)))
3249 ;; Otherwise, use a default template.
3250 (t (org-e-odt--wrap-label
3251 inlinetask
3252 (org-e-odt-format-stylized-paragraph
3253 nil (org-e-odt-format-textbox
3254 (concat (org-e-odt-format-stylized-paragraph
3255 "OrgInlineTaskHeading" (org-e-odt-format-headline--wrap
3256 inlinetask info))
3257 contents)
3258 nil nil "OrgInlineTaskFrame" " style:rel-width=\"100%\""))))))
3260 ;;;; Item
3262 (defun org-e-odt-format-list-item (contents type checkbox
3263 &optional term-counter-id
3264 headline)
3265 (when checkbox
3266 (setq checkbox
3267 (org-e-odt-format-fontify (case checkbox
3268 (on "[X]")
3269 (off "[&nbsp;]")
3270 (trans "[-]")) 'code)))
3271 (concat
3272 (org-e-odt-begin-list-item type term-counter-id headline)
3273 ;; FIXME checkbox (and checkbox " ")
3274 contents
3275 (org-e-odt-end-list-item type)))
3277 (defun org-e-odt-item (item contents info)
3278 "Transcode an ITEM element from Org to HTML.
3279 CONTENTS holds the contents of the item. INFO is a plist holding
3280 contextual information."
3281 ;; Grab `:level' from plain-list properties, which is always the
3282 ;; first element above current item.
3283 (let* ((plain-list (org-export-get-parent item info))
3284 (type (org-element-property :type plain-list))
3285 (level (org-element-property :level plain-list))
3286 (counter (org-element-property :counter item))
3287 (checkbox (org-element-property :checkbox item))
3288 (tag (let ((tag (org-element-property :tag item)))
3289 (and tag (org-export-secondary-string tag 'e-odt info)))))
3290 (org-e-odt-format-list-item
3291 contents type checkbox (or tag counter))))
3294 ;;;; Keyword
3296 (defun org-e-odt-keyword (keyword contents info)
3297 "Transcode a KEYWORD element from Org to HTML.
3298 CONTENTS is nil. INFO is a plist holding contextual information."
3299 (let ((key (org-element-property :key keyword))
3300 (value (org-element-property :value keyword)))
3301 (cond
3302 ((string= key "LATEX") value)
3303 ((string= key "INDEX") (format "\\index{%s}" value))
3304 ((string= key "TARGET") nil ; FIXME
3305 ;; (format "\\label{%s}" (org-export-solidify-link-text value))
3307 ((string= key "toc")
3308 (let ((value (downcase value)))
3309 (cond
3310 ((string-match "\\<headlines\\>" value)
3311 (let ((depth (or (and (string-match "[0-9]+" value)
3312 (string-to-number (match-string 0 value)))
3313 (plist-get info :with-toc))))
3314 (when (wholenump depth) (org-e-odt-toc depth info))))
3315 ((string= "tables" value) "FIXME")
3316 ((string= "figures" value) "FIXME")
3317 ((string= "listings" value)
3318 (cond
3319 ;; At the moment, src blocks with a caption are wrapped
3320 ;; into a figure environment.
3321 (t "FIXME")))))))))
3324 ;;;; Latex Environment
3326 (defun org-e-odt-format-latex (latex-frag processing-type)
3327 (let* ((prefix (case processing-type
3328 (dvipng "ltxpng/")
3329 (mathml "ltxmathml/")))
3330 (cache-relpath
3331 (concat prefix (file-name-sans-extension
3332 (file-name-nondirectory (buffer-file-name)))))
3333 (cache-dir (file-name-directory (buffer-file-name )))
3334 (display-msg (case processing-type
3335 (dvipng "Creating LaTeX Image...")
3336 (mathml "Creating MathML snippet..."))))
3337 (with-temp-buffer
3338 (insert latex-frag)
3339 (org-format-latex cache-relpath cache-dir nil display-msg
3340 nil nil processing-type)
3341 (buffer-string))))
3343 (defun org-e-odt-latex-environment (latex-environment contents info)
3344 "Transcode a LATEX-ENVIRONMENT element from Org to HTML.
3345 CONTENTS is nil. INFO is a plist holding contextual information."
3346 (org-e-odt--wrap-label
3347 latex-environment
3348 (let* ((latex-frag
3349 (org-remove-indentation
3350 (org-element-property :value latex-environment)))
3351 (processing-type (plist-get info :LaTeX-fragments))
3352 (caption (org-element-property :caption latex-environment))
3353 (short-caption (and (cdr caption) (org-export-secondary-string
3354 (cdr caption) 'e-odt info)))
3355 (caption (and (car caption) (org-export-secondary-string
3356 (car caption) 'e-odt info)))
3357 (label (org-element-property :name latex-environment))
3358 (attr nil) ; FIXME
3359 (label (org-element-property :name latex-environment)))
3360 (cond
3361 ((member processing-type '(t mathjax))
3362 (org-e-odt-format-formula latex-environment info))
3363 ((equal processing-type 'dvipng)
3364 (org-e-odt-format-stylized-paragraph
3365 nil (org-e-odt-link--inline-image latex-environment info)))
3366 (t latex-frag)))))
3369 ;;;; Latex Fragment
3372 ;; (when latex-frag ; FIXME
3373 ;; (setq href (org-propertize href :title "LaTeX Fragment"
3374 ;; :description latex-frag)))
3375 ;; handle verbatim
3376 ;; provide descriptions
3378 (defun org-e-odt-latex-fragment (latex-fragment contents info)
3379 "Transcode a LATEX-FRAGMENT object from Org to HTML.
3380 CONTENTS is nil. INFO is a plist holding contextual information."
3381 (let* ((latex-frag (org-element-property :value latex-fragment))
3382 (processing-type (plist-get info :LaTeX-fragments)))
3383 (cond
3384 ((member processing-type '(t mathjax))
3385 (org-e-odt-format-formula latex-fragment info))
3386 ((equal processing-type 'dvipng)
3387 (org-e-odt-link--inline-image latex-fragment info))
3388 (t latex-frag))))
3391 ;;;; Line Break
3393 (defun org-e-odt-line-break (line-break contents info)
3394 "Transcode a LINE-BREAK object from Org to HTML.
3395 CONTENTS is nil. INFO is a plist holding contextual information."
3396 "<text:line-break/>\n")
3399 ;;;; Link
3401 (defun org-e-odt-link--inline-image (element info)
3402 "Return HTML code for an inline image.
3403 LINK is the link pointing to the inline image. INFO is a plist
3404 used as a communication channel."
3405 (let* ((src (cond
3406 ((eq (org-element-type element) 'link)
3407 (let* ((type (org-element-property :type element))
3408 (raw-path (org-element-property :path element)))
3409 (cond ((member type '("http" "https"))
3410 (concat type ":" raw-path))
3411 ((file-name-absolute-p raw-path)
3412 (expand-file-name raw-path))
3413 (t raw-path))))
3414 ((member (org-element-type element)
3415 '(latex-fragment latex-environment))
3416 (let* ((latex-frag (org-remove-indentation
3417 (org-element-property
3418 :value element)))
3419 (formula-link (org-e-odt-format-latex
3420 latex-frag 'dvipng)))
3421 (and formula-link
3422 (string-match "file:\\([^]]*\\)" formula-link)
3423 (match-string 1 formula-link))))
3424 (t (error "what is this?"))))
3425 (href (org-e-odt-format-tags
3426 "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
3427 (org-e-odt-copy-image-file src)))
3428 ;; extract attributes from #+ATTR_ODT line.
3429 (attr-from (case (org-element-type element)
3430 (link (org-export-get-parent-paragraph element info))
3431 (t element)))
3432 ;; convert attributes to a plist.
3433 (attr-plist (org-e-odt-element-attributes attr-from info))
3434 ;; handle `:anchor', `:style' and `:attributes' properties.
3435 (user-frame-anchor
3436 (car (assoc-string (plist-get attr-plist :anchor)
3437 '(("as-char") ("paragraph") ("page")) t)))
3438 (user-frame-style
3439 (and user-frame-anchor (plist-get attr-plist :style)))
3440 (user-frame-attrs
3441 (and user-frame-anchor (plist-get attr-plist :attributes)))
3442 (user-frame-params
3443 (list user-frame-style user-frame-attrs user-frame-anchor))
3444 ;; (embed-as (or embed-as user-frame-anchor "paragraph"))
3445 ;; extrac
3446 ;; handle `:width', `:height' and `:scale' properties.
3447 (size (org-e-odt-image-size-from-file
3448 src (plist-get attr-plist :width)
3449 (plist-get attr-plist :height)
3450 (plist-get attr-plist :scale) nil ;; embed-as
3451 "paragraph" ; FIXME
3453 (width (car size)) (height (cdr size))
3454 (embed-as
3455 (case (org-element-type element)
3456 ((org-e-odt-standalone-image-p element info) "paragraph")
3457 (latex-fragment "as-char")
3458 (latex-environment "paragraph")
3459 (t "paragraph")))
3460 (captions (org-e-odt-format-label element info 'definition))
3461 (caption (car captions)) (short-caption (cdr captions))
3462 (entity (concat (and caption "Captioned") embed-as "Image")))
3463 (org-e-odt-format-entity entity href width height
3464 captions user-frame-params )))
3466 (defun org-e-odt-format-entity (entity href width height &optional
3467 captions user-frame-params)
3468 (let* ((caption (car captions)) (short-caption (cdr captions))
3469 (entity-style (assoc-string entity org-e-odt-entity-frame-styles t))
3470 default-frame-params frame-params)
3471 (cond
3472 ((not caption)
3473 (setq default-frame-params (nth 2 entity-style))
3474 (setq frame-params (org-e-odt-merge-frame-params
3475 default-frame-params user-frame-params))
3476 (apply 'org-e-odt-format-frame href width height frame-params))
3478 (setq default-frame-params (nth 3 entity-style))
3479 (setq frame-params (org-e-odt-merge-frame-params
3480 default-frame-params user-frame-params))
3481 (apply 'org-e-odt-format-textbox
3482 (org-e-odt-format-stylized-paragraph
3483 'illustration
3484 (concat
3485 (apply 'org-e-odt-format-frame href width height
3486 (let ((entity-style-1 (copy-sequence
3487 (nth 2 entity-style))))
3488 (setcar (cdr entity-style-1)
3489 (concat
3490 (cadr entity-style-1)
3491 (and short-caption
3492 (format " draw:name=\"%s\" "
3493 short-caption))))
3494 entity-style-1))
3495 caption))
3496 width height frame-params)))))
3498 (defvar org-e-odt-standalone-image-predicate
3499 (function (lambda (paragraph)
3500 (or (org-element-property :caption paragraph)
3501 (org-element-property :name paragraph)))))
3503 (defun org-e-odt-standalone-image-p (element info &optional predicate)
3504 "Test if ELEMENT is a standalone image for the purpose ODT export.
3505 INFO is a plist holding contextual information.
3507 Return non-nil, if ELEMENT is of type paragraph and it's sole
3508 content, save for whitespaces, is a link that qualifies as an
3509 inline image.
3511 Return non-nil, if ELEMENT is of type link and it's containing
3512 paragraph has no other content save for leading and trailing
3513 whitespaces.
3515 Return nil, otherwise.
3517 Bind `org-e-odt-standalone-image-predicate' to constrain
3518 paragraph further. For example, to check for only captioned
3519 standalone images, do the following.
3521 \(setq org-e-odt-standalone-image-predicate
3522 \(lambda \(paragraph\)
3523 \(org-element-property :caption paragraph\)\)\)
3525 (let ((paragraph (case (org-element-type element)
3526 (paragraph element)
3527 (link (and (org-export-inline-image-p
3528 element org-e-odt-inline-image-rules)
3529 (org-export-get-parent element info)))
3530 (t nil))))
3531 (when paragraph
3532 (assert (eq (org-element-type paragraph) 'paragraph))
3533 (when (or (not (and (boundp 'org-e-odt-standalone-image-predicate)
3534 (functionp org-e-odt-standalone-image-predicate)))
3535 (funcall org-e-odt-standalone-image-predicate paragraph))
3536 (let ((contents (org-element-contents paragraph)))
3537 (loop for x in contents
3538 with inline-image-count = 0
3539 always (cond
3540 ((eq (org-element-type x) 'plain-text)
3541 (not (org-string-nw-p x)))
3542 ((eq (org-element-type x) 'link)
3543 (when (org-export-inline-image-p
3544 x org-e-odt-inline-image-rules)
3545 (= (incf inline-image-count) 1)))
3546 (t nil))))))))
3548 (defun org-e-odt-link (link desc info)
3549 "Transcode a LINK object from Org to HTML.
3551 DESC is the description part of the link, or the empty string.
3552 INFO is a plist holding contextual information. See
3553 `org-export-data'."
3554 (let* ((type (org-element-property :type link))
3555 (raw-path (org-element-property :path link))
3556 ;; Ensure DESC really exists, or set it to nil.
3557 (desc (and (not (string= desc "")) desc))
3558 (imagep (org-export-inline-image-p
3559 link org-e-odt-inline-image-rules))
3560 (path (cond
3561 ((member type '("http" "https" "ftp" "mailto"))
3562 (concat type ":" raw-path))
3563 ((string= type "file")
3564 (when (string-match "\\(.+\\)::.+" raw-path)
3565 (setq raw-path (match-string 1 raw-path)))
3566 (if (file-name-absolute-p raw-path)
3567 (concat "file://" (expand-file-name raw-path))
3568 ;; TODO: Not implemented yet. Concat also:
3569 ;; (org-export-directory :HTML info)
3570 (concat "file://" raw-path)))
3571 (t raw-path)))
3572 protocol)
3573 (cond
3574 ;; Image file.
3575 ((and (not desc) (org-export-inline-image-p
3576 link org-e-odt-inline-image-rules))
3577 (org-e-odt-link--inline-image link info))
3578 ;; Radioed target: Target's name is obtained from original raw
3579 ;; link. Path is parsed and transcoded in order to have a proper
3580 ;; display of the contents.
3581 ((string= type "radio")
3582 (org-e-odt-format-internal-link
3583 (org-export-secondary-string
3584 (org-element-parse-secondary-string
3585 path (org-element-restriction 'radio-target))
3586 'e-odt info)
3587 (org-export-solidify-link-text path)))
3588 ;; Links pointing to an headline: Find destination and build
3589 ;; appropriate referencing command.
3590 ((member type '("custom-id" "fuzzy" "id"))
3591 (let ((destination (if (string= type "fuzzy")
3592 (org-export-resolve-fuzzy-link link info)
3593 (org-export-resolve-id-link link info))))
3594 (case (org-element-type destination)
3595 ;; Fuzzy link points nowhere.
3596 ('nil
3597 (org-e-odt-format-fontify
3598 (or desc (org-export-secondary-string
3599 (org-element-property :raw-link link)
3600 'e-odt info)) 'emphasis))
3601 ;; Fuzzy link points to an invisible target.
3602 (keyword nil)
3603 ;; LINK points to an headline. If headlines are numbered
3604 ;; and the link has no description, display headline's
3605 ;; number. Otherwise, display description or headline's
3606 ;; title.
3607 (headline
3608 (let* ((headline-no (org-export-get-headline-number destination info))
3609 (label (format "sec-%s" (mapconcat 'number-to-string
3610 headline-no "-")))
3611 (section-no (mapconcat 'number-to-string headline-no ".")))
3612 (setq desc
3613 (cond
3614 (desc desc)
3615 ((plist-get info :section-numbers) section-no)
3616 (t (org-export-secondary-string
3617 (org-element-property :title destination)
3618 'e-odt info))))
3619 (org-e-odt-format-internal-link desc label)))
3620 ;; Fuzzy link points to a target. Do as above.
3621 (otherwise
3622 ;; (unless desc
3623 ;; (setq number (cond
3624 ;; ((org-e-odt-standalone-image-p destination info)
3625 ;; (org-export-get-ordinal
3626 ;; (assoc 'link (org-element-contents destination))
3627 ;; info 'link 'org-e-odt-standalone-image-p))
3628 ;; (t (org-export-get-ordinal destination info))))
3629 ;; (setq desc (when number
3630 ;; (if (atom number) (number-to-string number)
3631 ;; (mapconcat 'number-to-string number ".")))))
3633 (let ((label-reference
3634 (org-e-odt-format-label destination info 'reference)))
3635 (assert label-reference)
3636 label-reference)))))
3637 ;; Coderef: replace link with the reference name or the
3638 ;; equivalent line number.
3639 ((string= type "coderef")
3640 (let* ((fmt (org-export-get-coderef-format path desc))
3641 (res (org-export-resolve-coderef path info))
3642 (org-e-odt-suppress-xref nil)
3643 (href (org-xml-format-href (concat "#coderef-" path))))
3644 (format fmt (org-e-odt-format-link res href))))
3645 ;; Link type is handled by a special function.
3646 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
3647 (funcall protocol (org-link-unescape path) desc 'html))
3648 ;; External link with a description part.
3649 ((and path desc) (org-e-odt-format-link desc path))
3650 ;; External link without a description part.
3651 (path (org-e-odt-format-link path path))
3652 ;; No path, only description. Try to do something useful.
3653 (t (org-e-odt-format-fontify desc 'emphasis)))))
3656 ;;;; Babel Call
3658 ;; Babel Calls are ignored.
3661 ;;;; Macro
3663 (defun org-e-odt-macro (macro contents info)
3664 "Transcode a MACRO element from Org to HTML.
3665 CONTENTS is nil. INFO is a plist holding contextual information."
3666 ;; Use available tools.
3667 (org-export-expand-macro macro info))
3670 ;;;; Paragraph
3672 (defun org-e-odt-paragraph (paragraph contents info)
3673 "Transcode a PARAGRAPH element from Org to HTML.
3674 CONTENTS is the contents of the paragraph, as a string. INFO is
3675 the plist used as a communication channel."
3676 (let* ((style nil) ; FIXME
3677 (class (cdr (assoc style '((footnote . "footnote")
3678 (verse . nil)))))
3679 (extra (if class (format " class=\"%s\"" class) ""))
3680 (parent (org-export-get-parent paragraph info))
3681 (parent-type (org-element-type parent))
3682 (style (case parent-type
3683 (quote-block 'quote)
3684 (center-block 'center)
3685 (footnote-definition 'footnote)
3686 (t nil))))
3687 (org-e-odt-format-stylized-paragraph style contents)))
3690 ;;;; Plain List
3692 (defun org-e-odt-plain-list (plain-list contents info)
3693 "Transcode a PLAIN-LIST element from Org to HTML.
3694 CONTENTS is the contents of the list. INFO is a plist holding
3695 contextual information."
3696 (let* (arg1 ;; FIXME
3697 (type (org-element-property :type plain-list))
3698 (attr (mapconcat #'identity
3699 (org-element-property :attr_odt plain-list)
3700 " ")))
3701 (org-e-odt--wrap-label
3702 plain-list (format "%s\n%s%s"
3703 (org-e-odt-begin-plain-list type)
3704 contents (org-e-odt-end-plain-list type)))))
3706 ;;;; Plain Text
3708 (defun org-e-odt-convert-special-strings (string)
3709 "Convert special characters in STRING to ODT."
3710 (let ((all org-e-odt-special-string-regexps)
3711 e a re rpl start)
3712 (while (setq a (pop all))
3713 (setq re (car a) rpl (cdr a) start 0)
3714 (while (string-match re string start)
3715 (setq string (replace-match rpl t nil string))))
3716 string))
3718 ;; (defun org-e-odt-encode-plain-text (s)
3719 ;; "Convert plain text characters to HTML equivalent.
3720 ;; Possible conversions are set in `org-export-html-protect-char-alist'."
3721 ;; (let ((cl org-e-odt-protect-char-alist) c)
3722 ;; (while (setq c (pop cl))
3723 ;; (let ((start 0))
3724 ;; (while (string-match (car c) s start)
3725 ;; (setq s (replace-match (cdr c) t t s)
3726 ;; start (1+ (match-beginning 0))))))
3727 ;; s))
3729 (defun org-e-odt-plain-text (text info)
3730 "Transcode a TEXT string from Org to HTML.
3731 TEXT is the string to transcode. INFO is a plist holding
3732 contextual information."
3733 (setq text (org-e-odt-encode-plain-text text t))
3734 ;; Protect %, #, &, $, ~, ^, _, { and }.
3735 ;; (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
3736 ;; (setq text
3737 ;; (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
3738 ;; Protect \
3739 ;; (setq text (replace-regexp-in-string
3740 ;; "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
3741 ;; "$\\backslash$" text nil t 1))
3742 ;; HTML into \HTML{} and TeX into \TeX{}.
3743 ;; (let ((case-fold-search nil)
3744 ;; (start 0))
3745 ;; (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
3746 ;; (setq text (replace-match
3747 ;; (format "\\%s{}" (match-string 1 text)) nil t text)
3748 ;; start (match-end 0))))
3749 ;; Handle quotation marks
3750 ;; (setq text (org-e-odt--quotation-marks text info))
3751 ;; Convert special strings.
3752 ;; (when (plist-get info :with-special-strings)
3753 ;; (while (string-match (regexp-quote "...") text)
3754 ;; (setq text (replace-match "\\ldots{}" nil t text))))
3755 (when (plist-get info :with-special-strings)
3756 (setq text (org-e-odt-convert-special-strings text)))
3757 ;; Handle break preservation if required.
3758 (when (plist-get info :preserve-breaks)
3759 (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
3760 text)))
3761 ;; Return value.
3762 text)
3765 ;;;; Property Drawer
3767 (defun org-e-odt-property-drawer (property-drawer contents info)
3768 "Transcode a PROPERTY-DRAWER element from Org to HTML.
3769 CONTENTS is nil. INFO is a plist holding contextual
3770 information."
3771 ;; The property drawer isn't exported but we want separating blank
3772 ;; lines nonetheless.
3776 ;;;; Quote Block
3778 (defun org-e-odt-quote-block (quote-block contents info)
3779 "Transcode a QUOTE-BLOCK element from Org to HTML.
3780 CONTENTS holds the contents of the block. INFO is a plist
3781 holding contextual information."
3782 (org-e-odt--wrap-label quote-block contents))
3785 ;;;; Quote Section
3787 (defun org-e-odt-quote-section (quote-section contents info)
3788 "Transcode a QUOTE-SECTION element from Org to HTML.
3789 CONTENTS is nil. INFO is a plist holding contextual information."
3790 (let ((value (org-remove-indentation
3791 (org-element-property :value quote-section))))
3792 (when value (org-e-odt-format-source-code-or-example value nil))))
3795 ;;;; Section
3797 (defun org-e-odt-section (section contents info) ; FIXME
3798 "Transcode a SECTION element from Org to HTML.
3799 CONTENTS holds the contents of the section. INFO is a plist
3800 holding contextual information."
3801 contents)
3803 ;;;; Radio Target
3805 (defun org-e-odt-radio-target (radio-target text info)
3806 "Transcode a RADIO-TARGET object from Org to HTML.
3807 TEXT is the text of the target. INFO is a plist holding
3808 contextual information."
3809 (org-e-odt-format-anchor
3810 text (org-export-solidify-link-text
3811 (org-element-property :value radio-target))))
3814 ;;;; Special Block
3816 (defun org-e-odt-special-block (special-block contents info)
3817 "Transcode a SPECIAL-BLOCK element from Org to HTML.
3818 CONTENTS holds the contents of the block. INFO is a plist
3819 holding contextual information."
3820 (let ((type (downcase (org-element-property :type special-block))))
3821 (org-e-odt--wrap-label
3822 special-block
3823 (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
3826 ;;;; Src Block
3828 (defun org-e-odt-src-block (src-block contents info)
3829 "Transcode a SRC-BLOCK element from Org to HTML.
3830 CONTENTS holds the contents of the item. INFO is a plist holding
3831 contextual information."
3832 (let* ((lang (org-element-property :language src-block))
3833 (caption (org-element-property :caption src-block))
3834 (short-caption (and (cdr caption) (org-export-secondary-string
3835 (cdr caption) 'e-odt info)))
3836 (caption (and (car caption) (org-export-secondary-string
3837 (car caption) 'e-odt info)))
3838 (label (org-element-property :name src-block)))
3839 ;; FIXME: Handle caption
3840 ;; caption-str (when caption)
3841 ;; (main (org-export-secondary-string (car caption) 'e-odt info))
3842 ;; (secondary (org-export-secondary-string (cdr caption) 'e-odt info))
3843 ;; (caption-str (org-e-odt--caption/label-string caption label info))
3844 (let* ((captions (org-e-odt-format-label src-block info 'definition))
3845 (caption (car captions)) (short-caption (cdr captions)))
3846 (concat
3847 (and caption (org-e-odt-format-stylized-paragraph 'listing caption))
3848 (org-e-odt-format-code src-block info)))))
3851 ;;;; Statistics Cookie
3853 (defun org-e-odt-statistics-cookie (statistics-cookie contents info)
3854 "Transcode a STATISTICS-COOKIE object from Org to HTML.
3855 CONTENTS is nil. INFO is a plist holding contextual information."
3856 (let ((cookie-value (org-element-property :value statistics-cookie)))
3857 (org-e-odt-format-fontify cookie-value 'code)))
3860 ;;;; Subscript
3862 (defun org-e-odt-subscript (subscript contents info)
3863 "Transcode a SUBSCRIPT object from Org to HTML.
3864 CONTENTS is the contents of the object. INFO is a plist holding
3865 contextual information."
3866 ;; (format (if (= (length contents) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents)
3867 (org-e-odt-format-fontify contents 'subscript))
3870 ;;;; Superscript
3872 (defun org-e-odt-superscript (superscript contents info)
3873 "Transcode a SUPERSCRIPT object from Org to HTML.
3874 CONTENTS is the contents of the object. INFO is a plist holding
3875 contextual information."
3876 ;; (format (if (= (length contents) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents)
3877 (org-e-odt-format-fontify contents 'superscript))
3880 ;;;; Table Cell
3882 (defun org-e-odt-table-style-spec (element info)
3883 (let* ((table (org-export-get-parent-table element info))
3884 (table-attributes (org-e-odt-element-attributes table info))
3885 (table-style (plist-get table-attributes :style)))
3886 (assoc table-style org-e-odt-table-styles)))
3888 (defun org-e-odt-get-table-cell-styles (table-cell info)
3889 "Retrieve styles applicable to a table cell.
3890 R and C are (zero-based) row and column numbers of the table
3891 cell. STYLE-SPEC is an entry in `org-e-odt-table-styles'
3892 applicable to the current table. It is `nil' if the table is not
3893 associated with any style attributes.
3895 Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
3897 When STYLE-SPEC is nil, style the table cell the conventional way
3898 - choose cell borders based on row and column groupings and
3899 choose paragraph alignment based on `org-col-cookies' text
3900 property. See also
3901 `org-e-odt-get-paragraph-style-cookie-for-table-cell'.
3903 When STYLE-SPEC is non-nil, ignore the above cookie and return
3904 styles congruent with the ODF-1.2 specification."
3905 (let* ((table-cell-address (org-export-table-cell-address table-cell info))
3906 (r (car table-cell-address)) (c (cdr table-cell-address))
3907 (style-spec (org-e-odt-table-style-spec table-cell info))
3908 (table-dimensions (org-export-table-dimensions
3909 (org-export-get-parent-table table-cell info)
3910 info)))
3911 (when style-spec
3912 ;; LibreOffice - particularly the Writer - honors neither table
3913 ;; templates nor custom table-cell styles. Inorder to retain
3914 ;; inter-operability with LibreOffice, only automatic styles are
3915 ;; used for styling of table-cells. The current implementation is
3916 ;; congruent with ODF-1.2 specification and hence is
3917 ;; future-compatible.
3919 ;; Additional Note: LibreOffice's AutoFormat facility for tables -
3920 ;; which recognizes as many as 16 different cell types - is much
3921 ;; richer. Unfortunately it is NOT amenable to easy configuration
3922 ;; by hand.
3923 (let* ((template-name (nth 1 style-spec))
3924 (cell-style-selectors (nth 2 style-spec))
3925 (cell-type
3926 (cond
3927 ((and (cdr (assoc 'use-first-column-styles cell-style-selectors))
3928 (= c 0)) "FirstColumn")
3929 ((and (cdr (assoc 'use-last-column-styles cell-style-selectors))
3930 (= (1+ c) (cdr table-dimensions)))
3931 "LastColumn")
3932 ((and (cdr (assoc 'use-first-row-styles cell-style-selectors))
3933 (= r 0)) "FirstRow")
3934 ((and (cdr (assoc 'use-last-row-styles cell-style-selectors))
3935 (= (1+ r) (car table-dimensions)))
3936 "LastRow")
3937 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
3938 (= (% r 2) 1)) "EvenRow")
3939 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
3940 (= (% r 2) 0)) "OddRow")
3941 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
3942 (= (% c 2) 1)) "EvenColumn")
3943 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
3944 (= (% c 2) 0)) "OddColumn")
3945 (t ""))))
3946 (concat template-name cell-type)))))
3948 (defun org-e-odt-table-cell (table-cell contents info)
3949 "Transcode a TABLE-CELL element from Org to ODT.
3950 CONTENTS is nil. INFO is a plist used as a communication
3951 channel."
3952 (let* ((table-cell-address (org-export-table-cell-address table-cell info))
3953 (r (car table-cell-address))
3954 (c (cdr table-cell-address))
3955 (horiz-span (or (org-export-table-cell-width table-cell info) 0))
3956 (table-row (org-export-get-parent table-cell info))
3957 (custom-style-prefix (org-e-odt-get-table-cell-styles
3958 table-cell info))
3959 (paragraph-style
3961 (and custom-style-prefix
3962 (format "%sTableParagraph" custom-style-prefix))
3963 (concat
3964 (cond
3965 ((and (= 1 (org-export-table-row-group table-row info))
3966 (org-export-table-has-header-p
3967 (org-export-get-parent-table table-row info) info))
3968 "OrgTableHeading")
3969 ((and (zerop c) t ;; (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS)
3971 "OrgTableHeading")
3972 (t "OrgTableContents"))
3973 (capitalize (symbol-name (org-export-table-cell-alignment
3974 table-cell info))))))
3975 (cell-style-name
3977 (and custom-style-prefix (format "%sTableCell"
3978 custom-style-prefix))
3979 (concat
3980 "OrgTblCell"
3981 (when (or (org-export-table-row-starts-rowgroup-p table-row info)
3982 (zerop r)) "T")
3983 (when (org-export-table-row-ends-rowgroup-p table-row info) "B")
3984 (when (and (org-export-table-cell-starts-colgroup-p table-cell info)
3985 (not (zerop c)) ) "L"))))
3986 (cell-attributes
3987 (concat
3988 (format " table:style-name=\"%s\"" cell-style-name)
3989 (and (> horiz-span 0)
3990 (format " table:number-columns-spanned=\"%d\""
3991 (1+ horiz-span))))))
3992 (unless contents (setq contents ""))
3993 (concat
3994 (org-e-odt-format-tags
3995 '("<table:table-cell%s>" . "</table:table-cell>")
3996 (org-e-odt-format-stylized-paragraph paragraph-style contents)
3997 cell-attributes)
3998 (let (s)
3999 (dotimes (i horiz-span s)
4000 (setq s (concat s "\n<table:covered-table-cell/>"))))
4001 "\n")))
4004 ;;;; Table Row
4006 (defun org-e-odt-table-row (table-row contents info)
4007 "Transcode a TABLE-ROW element from Org to ODT.
4008 CONTENTS is the contents of the row. INFO is a plist used as a
4009 communication channel."
4010 ;; Rules are ignored since table separators are deduced from
4011 ;; borders of the current row.
4012 (when (eq (org-element-property :type table-row) 'standard)
4013 (let* ((rowgroup-tags
4014 (if (and (= 1 (org-export-table-row-group table-row info))
4015 (org-export-table-has-header-p
4016 (org-export-get-parent-table table-row info) info))
4017 ;; If the row belongs to the first rowgroup and the
4018 ;; table has more than one row groups, then this row
4019 ;; belongs to the header row group.
4020 '("\n<table:table-header-rows>" . "\n</table:table-header-rows>")
4021 ;; Otherwise, it belongs to non-header row group.
4022 '("\n<table:table-rows>" . "\n</table:table-rows>"))))
4023 (concat
4024 ;; Does this row begin a rowgroup?
4025 (when (org-export-table-row-starts-rowgroup-p table-row info)
4026 (car rowgroup-tags))
4027 ;; Actual table row
4028 (org-e-odt-format-tags
4029 '("<table:table-row>" . "</table:table-row>") contents)
4030 ;; Does this row end a rowgroup?
4031 (when (org-export-table-row-ends-rowgroup-p table-row info)
4032 (cdr rowgroup-tags))))))
4035 ;;;; Table
4037 (defun org-e-odt-table-first-row-data-cells (table info)
4038 (let ((table-row
4039 (org-element-map
4040 table 'table-row
4041 (lambda (row)
4042 (unless (eq (org-element-property :type row) 'rule) row))
4043 info 'first-match))
4044 (special-column-p (org-export-table-has-special-column-p table)))
4045 (if (not special-column-p) (org-element-contents table-row)
4046 (cdr (org-element-contents table-row)))))
4048 (defun org-e-odt-table (table contents info)
4049 "Transcode a TABLE element from Org to HTML.
4050 CONTENTS is nil. INFO is a plist holding contextual information."
4051 (case (org-element-property :type table)
4052 (table.el nil)
4054 (let* ((captions (org-e-odt-format-label table info 'definition))
4055 (caption (car captions)) (short-caption (cdr captions))
4056 (attributes (org-e-odt-element-attributes table info))
4057 (custom-table-style (nth 1 (org-e-odt-table-style-spec table info)))
4058 (table-column-specs
4059 (function
4060 (lambda (table info)
4061 (let* ((table-style (or custom-table-style "OrgTable"))
4062 (column-style (format "%sColumn" table-style)))
4063 (mapconcat
4064 (lambda (table-cell)
4065 (let ((width (1+ (or (org-export-table-cell-width
4066 table-cell info) 0))))
4067 (org-e-odt-make-string
4068 width
4069 (org-e-odt-format-tags
4070 "<table:table-column table:style-name=\"%s\"/>"
4071 "" column-style))))
4072 (org-e-odt-table-first-row-data-cells table info) "\n"))))))
4073 (concat
4074 ;; caption.
4075 (when caption (org-e-odt-format-stylized-paragraph 'table caption))
4076 ;; begin table.
4077 (let* ((automatic-name
4078 (org-e-odt-add-automatic-style "Table" attributes)))
4079 (format
4080 "\n<table:table table:name=\"%s\" table:style-name=\"%s\">\n"
4081 (or short-caption (car automatic-name))
4082 (or custom-table-style (cdr automatic-name) "OrgTable")))
4083 ;; column specification.
4084 (funcall table-column-specs table info)
4085 ;; actual contents.
4086 "\n" contents
4087 ;; end table.
4088 "</table:table>")))))
4091 ;;;; Target
4093 (defun org-e-odt-target (target contents info)
4094 "Transcode a TARGET object from Org to HTML.
4095 CONTENTS is nil. INFO is a plist holding contextual
4096 information."
4097 (org-e-odt-format-anchor
4098 "" (org-export-solidify-link-text (org-element-property :value target))))
4101 ;;;; Time-stamp
4103 (defun org-e-odt-time-stamp (time-stamp contents info)
4104 "Transcode a TIME-STAMP object from Org to HTML.
4105 CONTENTS is nil. INFO is a plist holding contextual
4106 information."
4107 ;; (let ((value (org-element-property :value time-stamp))
4108 ;; (type (org-element-property :type time-stamp))
4109 ;; (appt-type (org-element-property :appt-type time-stamp)))
4110 ;; (concat (cond ((eq appt-type 'scheduled)
4111 ;; (format "\\textbf{\\textsc{%s}} " org-scheduled-string))
4112 ;; ((eq appt-type 'deadline)
4113 ;; (format "\\textbf{\\textsc{%s}} " org-deadline-string))
4114 ;; ((eq appt-type 'closed)
4115 ;; (format "\\textbf{\\textsc{%s}} " org-closed-string)))
4116 ;; (cond ((memq type '(active active-range))
4117 ;; (format org-e-odt-active-timestamp-format value))
4118 ;; ((memq type '(inactive inactive-range))
4119 ;; (format org-e-odt-inactive-timestamp-format value))
4120 ;; (t
4121 ;; (format org-e-odt-diary-timestamp-format value)))))
4122 (let ((value (org-element-property :value time-stamp))
4123 (type (org-element-property :type time-stamp))
4124 (appt-type (org-element-property :appt-type time-stamp)))
4125 (setq value (org-export-secondary-string value 'e-odt info))
4126 (org-e-odt-format-fontify
4127 (concat
4128 (org-e-odt-format-fontify
4129 (cond ((eq appt-type 'scheduled) org-scheduled-string)
4130 ((eq appt-type 'deadline) org-deadline-string)
4131 ((eq appt-type 'closed) org-closed-string)) "timestamp-kwd")
4132 ;; FIXME: (org-translate-time value)
4133 (org-e-odt-format-fontify value "timestamp"))
4134 "timestamp-wrapper")))
4137 ;;;; Verbatim
4139 (defun org-e-odt-verbatim (verbatim contents info)
4140 "Transcode a VERBATIM object from Org to HTML.
4141 CONTENTS is nil. INFO is a plist used as a communication
4142 channel."
4143 (org-e-odt-emphasis
4144 verbatim (org-element-property :value verbatim) info))
4147 ;;;; Verse Block
4149 (defun org-e-odt-verse-block (verse-block contents info)
4150 "Transcode a VERSE-BLOCK element from Org to HTML.
4151 CONTENTS is verse block contents. INFO is a plist holding
4152 contextual information."
4153 ;; Replace each newline character with line break. Also replace
4154 ;; each blank line with a line break.
4155 (setq contents (replace-regexp-in-string
4156 "^ *\\\\\\\\$" "<br/>\n"
4157 (replace-regexp-in-string
4158 "\\(\\\\\\\\\\)?[ \t]*\n" " <br/>\n" contents)))
4160 ;; Replace each white space at beginning of a line with a
4161 ;; non-breaking space.
4162 (while (string-match "^[ \t]+" contents)
4163 (let ((new-str (org-e-odt-format-spaces
4164 (length (match-string 0 contents)))))
4165 (setq contents (replace-match new-str nil t contents))))
4167 (org-e-odt--wrap-label
4168 verse-block (format "<p class=\"verse\">\n%s</p>" contents)))
4173 ;;; Filter Functions
4175 ;;;; Filter Settings
4176 ;;;; Filters
4178 ;;; Interactive functions
4180 (defun org-e-odt-export-to-odt
4181 (&optional subtreep visible-only body-only ext-plist pub-dir)
4182 "Export current buffer to a HTML file.
4184 If narrowing is active in the current buffer, only export its
4185 narrowed part.
4187 If a region is active, export that region.
4189 When optional argument SUBTREEP is non-nil, export the sub-tree
4190 at point, extracting information from the headline properties
4191 first.
4193 When optional argument VISIBLE-ONLY is non-nil, don't export
4194 contents of hidden elements.
4196 When optional argument BODY-ONLY is non-nil, only write code
4197 between \"\\begin{document}\" and \"\\end{document}\".
4199 EXT-PLIST, when provided, is a property list with external
4200 parameters overriding Org default settings, but still inferior to
4201 file-local settings.
4203 When optional argument PUB-DIR is set, use it as the publishing
4204 directory.
4206 Return output file's name."
4207 (interactive)
4208 (setq debug-on-error t)
4210 ;; (let* ((outfile (org-export-output-file-name ".html" subtreep pub-dir))
4211 ;; (outfile "content.xml"))
4212 ;; (org-export-to-file
4213 ;; 'e-odt outfile subtreep visible-only body-only ext-plist))
4215 (let* ((outbuf (org-e-odt-init-outfile))
4216 (target (org-export-output-file-name ".odt" subtreep pub-dir))
4217 (outdir (file-name-directory (buffer-file-name outbuf)))
4218 (default-directory outdir))
4220 ;; FIXME: for copying embedded images
4221 (setq org-current-export-file
4222 (file-name-directory
4223 (org-export-output-file-name ".odt" subtreep nil)))
4225 (org-export-to-buffer
4226 'e-odt outbuf
4227 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))
4229 (setq org-lparse-opt-plist nil) ; FIXME
4230 (org-e-odt-save-as-outfile target ;; info
4234 ;; return outfile
4235 (if (not org-e-odt-preferred-output-format) target
4236 (or (org-e-odt-convert target org-e-odt-preferred-output-format)
4237 target))))
4243 (defun org-e-odt-reachable-p (in-fmt out-fmt)
4244 "Return non-nil if IN-FMT can be converted to OUT-FMT."
4245 (catch 'done
4246 (let ((reachable-formats (org-e-odt-do-reachable-formats in-fmt)))
4247 (dolist (e reachable-formats)
4248 (let ((out-fmt-spec (assoc out-fmt (cdr e))))
4249 (when out-fmt-spec
4250 (throw 'done (cons (car e) out-fmt-spec))))))))
4252 (defun org-e-odt-do-convert (in-file out-fmt &optional prefix-arg)
4253 "Workhorse routine for `org-e-odt-convert'."
4254 (require 'browse-url)
4255 (let* ((in-file (expand-file-name (or in-file buffer-file-name)))
4256 (dummy (or (file-readable-p in-file)
4257 (error "Cannot read %s" in-file)))
4258 (in-fmt (file-name-extension in-file))
4259 (out-fmt (or out-fmt (error "Output format unspecified")))
4260 (how (or (org-e-odt-reachable-p in-fmt out-fmt)
4261 (error "Cannot convert from %s format to %s format?"
4262 in-fmt out-fmt)))
4263 (convert-process (car how))
4264 (out-file (concat (file-name-sans-extension in-file) "."
4265 (nth 1 (or (cdr how) out-fmt))))
4266 (extra-options (or (nth 2 (cdr how)) ""))
4267 (out-dir (file-name-directory in-file))
4268 (cmd (format-spec convert-process
4269 `((?i . ,(shell-quote-argument in-file))
4270 (?I . ,(browse-url-file-url in-file))
4271 (?f . ,out-fmt)
4272 (?o . ,out-file)
4273 (?O . ,(browse-url-file-url out-file))
4274 (?d . , (shell-quote-argument out-dir))
4275 (?D . ,(browse-url-file-url out-dir))
4276 (?x . ,extra-options)))))
4277 (when (file-exists-p out-file)
4278 (delete-file out-file))
4280 (message "Executing %s" cmd)
4281 (let ((cmd-output (shell-command-to-string cmd)))
4282 (message "%s" cmd-output))
4284 (cond
4285 ((file-exists-p out-file)
4286 (message "Exported to %s" out-file)
4287 (when prefix-arg
4288 (message "Opening %s..." out-file)
4289 (org-open-file out-file))
4290 out-file)
4292 (message "Export to %s failed" out-file)
4293 nil))))
4295 (defun org-e-odt-do-reachable-formats (in-fmt)
4296 "Return verbose info about formats to which IN-FMT can be converted.
4297 Return a list where each element is of the
4298 form (CONVERTER-PROCESS . OUTPUT-FMT-ALIST). See
4299 `org-e-odt-convert-processes' for CONVERTER-PROCESS and see
4300 `org-e-odt-convert-capabilities' for OUTPUT-FMT-ALIST."
4301 (let* ((converter
4302 (and org-e-odt-convert-process
4303 (cadr (assoc-string org-e-odt-convert-process
4304 org-e-odt-convert-processes t))))
4305 (capabilities
4306 (and org-e-odt-convert-process
4307 (cadr (assoc-string org-e-odt-convert-process
4308 org-e-odt-convert-processes t))
4309 org-e-odt-convert-capabilities))
4310 reachable-formats)
4311 (when converter
4312 (dolist (c capabilities)
4313 (when (member in-fmt (nth 1 c))
4314 (push (cons converter (nth 2 c)) reachable-formats))))
4315 reachable-formats))
4317 (defun org-e-odt-reachable-formats (in-fmt)
4318 "Return list of formats to which IN-FMT can be converted.
4319 The list of the form (OUTPUT-FMT-1 OUTPUT-FMT-2 ...)."
4320 (let (l)
4321 (mapc (lambda (e) (add-to-list 'l e))
4322 (apply 'append (mapcar
4323 (lambda (e) (mapcar 'car (cdr e)))
4324 (org-e-odt-do-reachable-formats in-fmt))))
4327 (defun org-e-odt-convert-read-params ()
4328 "Return IN-FILE and OUT-FMT params for `org-e-odt-do-convert'.
4329 This is a helper routine for interactive use."
4330 (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
4331 (in-file (read-file-name "File to be converted: "
4332 nil buffer-file-name t))
4333 (in-fmt (file-name-extension in-file))
4334 (out-fmt-choices (org-e-odt-reachable-formats in-fmt))
4335 (out-fmt
4336 (or (and out-fmt-choices
4337 (funcall input "Output format: "
4338 out-fmt-choices nil nil nil))
4339 (error
4340 "No known converter or no known output formats for %s files"
4341 in-fmt))))
4342 (list in-file out-fmt)))
4344 ;;;###autoload
4345 (defun org-e-odt-convert (&optional in-file out-fmt prefix-arg)
4346 "Convert IN-FILE to format OUT-FMT using a command line converter.
4347 IN-FILE is the file to be converted. If unspecified, it defaults
4348 to variable `buffer-file-name'. OUT-FMT is the desired output
4349 format. Use `org-e-odt-convert-process' as the converter.
4350 If PREFIX-ARG is non-nil then the newly converted file is opened
4351 using `org-open-file'."
4352 (interactive
4353 (append (org-e-odt-convert-read-params) current-prefix-arg))
4354 (org-e-odt-do-convert in-file out-fmt prefix-arg))
4356 ;;; FIXMES, TODOS, FOR REVIEW etc
4358 ;; (defun org-e-odt-discontinue-list ()
4359 ;; (let ((stashed-stack org-lparse-list-stack))
4360 ;; (loop for list-type in stashed-stack
4361 ;; do (org-lparse-end-list-item-1 list-type)
4362 ;; (org-lparse-end-list list-type))
4363 ;; (setq org-e-odt-list-stack-stashed stashed-stack)))
4365 ;; (defun org-e-odt-continue-list ()
4366 ;; (setq org-e-odt-list-stack-stashed (nreverse org-e-odt-list-stack-stashed))
4367 ;; (loop for list-type in org-e-odt-list-stack-stashed
4368 ;; do (org-lparse-begin-list list-type)
4369 ;; (org-lparse-begin-list-item list-type)))
4371 ;; FIXME: Begin indented table
4372 ;; (setq org-e-odt-table-indentedp (not (null org-lparse-list-stack)))
4373 ;; (setq org-e-odt-table-indentedp nil) ; FIXME
4374 ;; (when org-e-odt-table-indentedp
4375 ;; ;; Within the Org file, the table is appearing within a list item.
4376 ;; ;; OpenDocument doesn't allow table to appear within list items.
4377 ;; ;; Temporarily terminate the list, emit the table and then
4378 ;; ;; re-continue the list.
4379 ;; (org-e-odt-discontinue-list)
4380 ;; ;; Put the Table in an indented section.
4381 ;; (let ((level (length org-e-odt-list-stack-stashed)))
4382 ;; (org-e-odt-begin-section (format "OrgIndentedSection-Level-%d" level))))
4384 ;; FIXME: End indented table
4385 ;; (when org-e-odt-table-indentedp
4386 ;; (org-e-odt-end-section)
4387 ;; (org-e-odt-continue-list))
4390 ;;;; org-format-table-html
4391 ;;;; org-format-org-table-html
4392 ;;;; org-format-table-table-html
4393 ;;;; org-table-number-fraction
4394 ;;;; org-table-number-regexp
4395 ;;;; org-e-odt-table-caption-above
4397 ;;;; org-whitespace
4398 ;;;; "<span style=\"visibility:hidden;\">%s</span>"
4399 ;;;; Remove display properties
4401 ;;;; org-e-odt-with-timestamp
4402 ;;;; org-e-odt-html-helper-timestamp
4404 ;;;; org-export-as-html-and-open
4405 ;;;; org-export-as-html-batch
4406 ;;;; org-export-as-html-to-buffer
4407 ;;;; org-replace-region-by-html
4408 ;;;; org-export-region-as-html
4409 ;;;; org-export-as-html
4411 ;;;; (org-export-directory :html opt-plist)
4412 ;;;; (plist-get opt-plist :html-extension)
4413 ;;;; org-e-odt-toplevel-hlevel
4414 ;;;; org-e-odt-inline-image-extensions
4415 ;;;; org-e-odt-protect-char-alist
4416 ;;;; org-e-odt-table-use-header-tags-for-first-column
4417 ;;;; org-e-odt-todo-kwd-class-prefix
4418 ;;;; org-e-odt-tag-class-prefix
4419 ;;;; org-e-odt-footnote-separator
4422 ;;; Library Initializations
4424 (mapc
4425 (lambda (desc)
4426 ;; Let Org open all OpenDocument files using system-registered app
4427 (add-to-list 'org-file-apps
4428 (cons (concat "\\." (car desc) "\\'") 'system))
4429 ;; Let Emacs open all OpenDocument files in archive mode
4430 (add-to-list 'auto-mode-alist
4431 (cons (concat "\\." (car desc) "\\'") 'archive-mode)))
4432 org-e-odt-file-extensions)
4434 (defvar org-e-odt-display-outline-level 2)
4435 (defun org-e-odt-enumerate-element (element info &optional predicate n)
4436 (let* ((numbered-parent-headline-at-<=-n
4437 (function
4438 (lambda (element n info)
4439 (loop for x in (org-export-get-genealogy element info)
4440 thereis (and (eq (org-element-type x) 'headline)
4441 (<= (org-export-get-relative-level x info) n)
4442 (org-export-numbered-headline-p x info)
4443 x)))))
4444 (enumerate
4445 (function
4446 (lambda (element scope info &optional predicate)
4447 (let ((counter 0))
4448 (org-element-map
4449 (or scope (plist-get info :parse-tree))
4450 (org-element-type element)
4451 (lambda (el)
4452 (and (or (not predicate) (funcall predicate el info))
4453 (incf counter)
4454 (equal element el)
4455 counter))
4456 info 'first-match)))))
4457 (scope (funcall numbered-parent-headline-at-<=-n
4458 element (or n org-e-odt-display-outline-level) info))
4459 (ordinal (funcall enumerate element scope info predicate))
4460 (tag
4461 (concat
4462 ;; section number
4463 (and scope
4464 (mapconcat 'number-to-string
4465 (org-export-get-headline-number scope info) "."))
4466 ;; separator
4467 (and scope ".")
4468 ;; ordinal
4469 (number-to-string ordinal))))
4470 ;; (message "%s:\t%s" (org-element-property :name element) tag)
4471 tag))
4473 (provide 'org-e-odt)
4475 ;;; org-e-odt.el ends here