org-e-html/org-e-odt: Use new table infrastructure. First cut and slightly broken.
[org-mode.git] / EXPERIMENTAL / org-e-odt.el
blobc9d3beafcd964441d3a5f366aa9d703f8cc51455
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 (when org-export-creator-info
1110 (format "Org-%s/Emacs-%s"
1111 org-version emacs-version)))
1112 (format "<meta:keyword>%s</meta:keyword>\n" keywords)
1113 (format "<dc:subject>%s</dc:subject>\n" description)
1114 (format "<dc:title>%s</dc:title>\n" title)
1115 "\n"
1116 " </office:meta>\n" "</office:document-meta>")
1117 nil (expand-file-name "meta.xml")))
1119 ;; create a manifest entry for meta.xml
1120 (org-e-odt-create-manifest-file-entry "text/xml" "meta.xml"))
1122 (defun org-e-odt-update-styles-file (info)
1123 ;; write styles file
1124 (let ((styles-file (plist-get info :odt-styles-file)))
1125 (org-e-odt-copy-styles-file (and styles-file
1126 (read (org-trim styles-file))))
1128 ;; FIXME: Who is opening an empty styles.xml before this point?
1129 (with-current-buffer
1130 (find-file-noselect (expand-file-name "styles.xml") t)
1131 (revert-buffer t t)))
1133 ;; Write custom styles for source blocks
1134 (org-e-odt-insert-custom-styles-for-srcblocks
1135 (mapconcat
1136 (lambda (style)
1137 (format " %s\n" (cddr style)))
1138 hfy-user-sheet-assoc "")))
1140 (defun org-e-odt-write-mimetype-file (format)
1141 ;; create mimetype file
1142 (let ((mimetype
1143 (case format
1144 (odt "application/vnd.oasis.opendocument.text")
1145 (odf "application/vnd.oasis.opendocument.formula")
1146 (t (error "Unknown OpenDocument backend %S" org-lparse-backend)))))
1147 (write-region mimetype nil (expand-file-name "mimetype"))
1148 mimetype))
1150 (declare-function org-create-math-formula "org"
1151 (latex-frag &optional mathml-file))
1153 (defun org-e-odt-get (what &optional opt-plist)
1154 (case what
1155 (EXPORT-DIR (org-export-directory :html opt-plist))
1156 (TABLE-FIRST-COLUMN-AS-LABELS nil)
1157 (CODING-SYSTEM-FOR-WRITE 'utf-8)
1158 (CODING-SYSTEM-FOR-SAVE 'utf-8)
1159 (t (error "Unknown property: %s" what))))
1161 (defun org-e-odt-do-preprocess-latex-fragments ()
1162 "Convert LaTeX fragments to images."
1163 (let* ((latex-frag-opt (plist-get org-lparse-opt-plist :LaTeX-fragments))
1164 (latex-frag-opt ; massage the options
1165 (or (and (member latex-frag-opt '(mathjax t))
1166 (not (and (fboundp 'org-format-latex-mathml-available-p)
1167 (org-format-latex-mathml-available-p)))
1168 (prog1 org-lparse-latex-fragment-fallback
1169 (org-lparse-warn
1170 (concat
1171 "LaTeX to MathML converter not available. "
1172 (format "Using %S instead."
1173 org-lparse-latex-fragment-fallback)))))
1174 latex-frag-opt))
1175 cache-dir display-msg)
1176 (cond
1177 ((eq latex-frag-opt 'dvipng)
1178 (setq cache-dir "ltxpng/")
1179 (setq display-msg "Creating LaTeX image %s"))
1180 ((member latex-frag-opt '(mathjax t))
1181 (setq latex-frag-opt 'mathml)
1182 (setq cache-dir "ltxmathml/")
1183 (setq display-msg "Creating MathML formula %s")))
1184 (when (and org-current-export-file)
1185 (org-format-latex
1186 (concat cache-dir (file-name-sans-extension
1187 (file-name-nondirectory org-current-export-file)))
1188 org-current-export-dir nil display-msg
1189 nil nil latex-frag-opt))))
1191 (eval-after-load 'org-odt
1192 '(ad-deactivate 'org-format-latex-as-mathml))
1194 ; FIXME
1196 ;; (defadvice org-format-latex-as-mathml ; FIXME
1197 ;; (after org-e-odt-protect-latex-fragment activate)
1198 ;; "Encode LaTeX fragment as XML.
1199 ;; Do this when translation to MathML fails."
1200 ;; (when (or (not (> (length ad-return-value) 0))
1201 ;; (get-text-property 0 'org-protected ad-return-value))
1202 ;; (setq ad-return-value
1203 ;; (org-propertize (org-e-odt-encode-plain-text (ad-get-arg 0))
1204 ;; 'org-protected t))))
1206 (defun org-e-odt-zip-extract-one (archive member &optional target)
1207 (require 'arc-mode)
1208 (let* ((target (or target default-directory))
1209 (archive (expand-file-name archive))
1210 (archive-zip-extract
1211 (list "unzip" "-qq" "-o" "-d" target))
1212 exit-code command-output)
1213 (setq command-output
1214 (with-temp-buffer
1215 (setq exit-code (archive-zip-extract archive member))
1216 (buffer-string)))
1217 (unless (zerop exit-code)
1218 (message command-output)
1219 (error "Extraction failed"))))
1221 (defun org-e-odt-zip-extract (archive members &optional target)
1222 (when (atom members) (setq members (list members)))
1223 (mapc (lambda (member)
1224 (org-e-odt-zip-extract-one archive member target))
1225 members))
1227 (defun org-e-odt-copy-styles-file (&optional styles-file)
1228 ;; Non-availability of styles.xml is not a critical error. For now
1229 ;; throw an error purely for aesthetic reasons.
1230 (setq styles-file (or styles-file
1231 org-e-odt-styles-file
1232 (expand-file-name "OrgOdtStyles.xml"
1233 org-e-odt-styles-dir)
1234 (error "org-e-odt: Missing styles file?")))
1235 (cond
1236 ((listp styles-file)
1237 (let ((archive (nth 0 styles-file))
1238 (members (nth 1 styles-file)))
1239 (org-e-odt-zip-extract archive members)
1240 (mapc
1241 (lambda (member)
1242 (when (org-file-image-p member)
1243 (let* ((image-type (file-name-extension member))
1244 (media-type (format "image/%s" image-type)))
1245 (org-e-odt-create-manifest-file-entry media-type member))))
1246 members)))
1247 ((and (stringp styles-file) (file-exists-p styles-file))
1248 (let ((styles-file-type (file-name-extension styles-file)))
1249 (cond
1250 ((string= styles-file-type "xml")
1251 (copy-file styles-file (expand-file-name "styles.xml") t))
1252 ((member styles-file-type '("odt" "ott"))
1253 (org-e-odt-zip-extract styles-file "styles.xml")))))
1255 (error (format "Invalid specification of styles.xml file: %S"
1256 org-e-odt-styles-file))))
1258 ;; create a manifest entry for styles.xml
1259 (org-e-odt-create-manifest-file-entry "text/xml" "styles.xml"))
1261 (defun org-e-odt-configure-outline-numbering ()
1262 "Outline numbering is retained only upto LEVEL.
1263 To disable outline numbering pass a LEVEL of 0."
1264 (goto-char (point-min))
1265 (let ((regex
1266 "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
1267 (replacement
1268 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1269 (while (re-search-forward regex nil t)
1270 (unless (let ((sec-num (plist-get info :section-numbers))
1271 (level (string-to-number (match-string 2))))
1272 (if (wholenump sec-num) (<= level sec-num) sec-num))
1273 (replace-match replacement t nil))))
1274 (save-buffer 0))
1276 ;;;###autoload
1277 (defun org-export-as-odf (latex-frag &optional odf-file)
1278 "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
1279 Use `org-create-math-formula' to convert LATEX-FRAG first to
1280 MathML. When invoked as an interactive command, use
1281 `org-latex-regexps' to infer LATEX-FRAG from currently active
1282 region. If no LaTeX fragments are found, prompt for it. Push
1283 MathML source to kill ring, if `org-export-copy-to-kill-ring' is
1284 non-nil."
1285 (interactive
1286 `(,(let (frag)
1287 (setq frag (and (setq frag (and (region-active-p)
1288 (buffer-substring (region-beginning)
1289 (region-end))))
1290 (loop for e in org-latex-regexps
1291 thereis (when (string-match (nth 1 e) frag)
1292 (match-string (nth 2 e) frag)))))
1293 (read-string "LaTeX Fragment: " frag nil frag))
1294 ,(let ((odf-filename (expand-file-name
1295 (concat
1296 (file-name-sans-extension
1297 (or (file-name-nondirectory buffer-file-name)))
1298 "." "odf")
1299 (file-name-directory buffer-file-name))))
1300 (read-file-name "ODF filename: " nil odf-filename nil
1301 (file-name-nondirectory odf-filename)))))
1302 (let* ((org-lparse-backend 'odf)
1303 org-lparse-opt-plist
1304 (filename (or odf-file
1305 (expand-file-name
1306 (concat
1307 (file-name-sans-extension
1308 (or (file-name-nondirectory buffer-file-name)))
1309 "." "odf")
1310 (file-name-directory buffer-file-name))))
1311 (buffer (find-file-noselect (org-e-odt-init-outfile filename)))
1312 (coding-system-for-write 'utf-8)
1313 (save-buffer-coding-system 'utf-8))
1314 (set-buffer buffer)
1315 (set-buffer-file-coding-system coding-system-for-write)
1316 (let ((mathml (org-create-math-formula latex-frag)))
1317 (unless mathml (error "No Math formula created"))
1318 (insert mathml)
1319 (or (org-export-push-to-kill-ring
1320 (upcase (symbol-name org-lparse-backend)))
1321 (message "Exporting... done")))
1322 (org-e-odt-save-as-outfile filename nil ; FIXME
1325 ;;;###autoload
1326 (defun org-export-as-odf-and-open ()
1327 "Export LaTeX fragment as OpenDocument formula and immediately open it.
1328 Use `org-export-as-odf' to read LaTeX fragment and OpenDocument
1329 formula file."
1330 (interactive)
1331 (org-lparse-and-open
1332 nil nil nil (call-interactively 'org-export-as-odf)))
1337 ;;; Driver Starts here
1338 ;;; Dependencies
1340 (require 'format-spec)
1341 (eval-when-compile (require 'cl) (require 'table))
1345 ;;; Hooks
1347 ;; FIXME: it already exists in org-e-odt.el
1348 ;;; Function Declarations
1350 (declare-function org-element-property "org-element" (property element))
1351 (declare-function org-element-normalize-string "org-element" (s))
1352 (declare-function org-element-parse-secondary-string
1353 "org-element" (string restriction &optional buffer))
1354 (defvar org-element-string-restrictions)
1355 (defvar org-element-object-restrictions)
1357 (declare-function org-export-clean-table "org-export" (table specialp))
1358 (declare-function org-export-data "org-export" (data backend info))
1359 (declare-function org-export-directory "org-export" (type plist))
1360 (declare-function org-export-expand-macro "org-export" (macro info))
1361 (declare-function org-export-first-sibling-p "org-export" (headline info))
1362 (declare-function org-export-footnote-first-reference-p "org-export"
1363 (footnote-reference info))
1364 (declare-function org-export-get-coderef-format "org-export" (path desc))
1365 (declare-function org-export-get-footnote-definition "org-export"
1366 (footnote-reference info))
1367 (declare-function org-export-get-footnote-number "org-export" (footnote info))
1368 (declare-function org-export-get-previous-element "org-export" (blob info))
1369 (declare-function org-export-get-relative-level "org-export" (headline info))
1370 (declare-function org-export-handle-code
1371 "org-export" (element info &optional num-fmt ref-fmt delayed))
1372 (declare-function org-export-included-file "org-export" (keyword backend info))
1373 (declare-function org-export-inline-image-p "org-export"
1374 (link &optional extensions))
1375 (declare-function org-export-last-sibling-p "org-export" (headline info))
1376 (declare-function org-export-low-level-p "org-export" (headline info))
1377 (declare-function org-export-output-file-name
1378 "org-export" (extension &optional subtreep pub-dir))
1379 (declare-function org-export-resolve-coderef "org-export" (ref info))
1380 (declare-function org-export-resolve-fuzzy-link "org-export" (link info))
1381 (declare-function org-export-secondary-string "org-export"
1382 (secondary backend info))
1383 (declare-function org-export-solidify-link-text "org-export" (s))
1384 (declare-function org-export-table-format-info "org-export" (table))
1385 (declare-function
1386 org-export-to-buffer "org-export"
1387 (backend buffer &optional subtreep visible-only body-only ext-plist))
1388 (declare-function
1389 org-export-to-file "org-export"
1390 (backend file &optional subtreep visible-only body-only ext-plist))
1392 (declare-function org-id-find-id-file "org-id" (id))
1393 (declare-function htmlize-region "ext:htmlize" (beg end))
1394 (declare-function org-pop-to-buffer-same-window
1395 "org-compat" (&optional buffer-or-name norecord label))
1401 (declare-function hfy-face-to-style "htmlfontify" (fn))
1402 (declare-function hfy-face-or-def-to-name "htmlfontify" (fn))
1403 (declare-function archive-zip-extract "arc-mode.el" (archive name))
1405 ;;; Internal Variables
1407 ;;;; ODT Internal Variables
1409 (defconst org-e-odt-lib-dir
1410 (file-name-directory load-file-name)
1411 "Location of ODT exporter.
1412 Use this to infer values of `org-e-odt-styles-dir' and
1413 `org-e-odt-schema-dir'.")
1415 (defvar org-e-odt-data-dir
1416 (expand-file-name "../etc/" org-e-odt-lib-dir)
1417 "Data directory for ODT exporter.
1418 Use this to infer values of `org-e-odt-styles-dir' and
1419 `org-e-odt-schema-dir'.")
1421 (defconst org-e-odt-special-string-regexps
1422 '(("\\\\-" . "&#x00ad;\\1") ; shy
1423 ("---\\([^-]\\)" . "&#x2014;\\1") ; mdash
1424 ("--\\([^-]\\)" . "&#x2013;\\1") ; ndash
1425 ("\\.\\.\\." . "&#x2026;")) ; hellip
1426 "Regular expressions for special string conversion.")
1428 (defconst org-e-odt-schema-dir-list
1429 (list
1430 (and org-e-odt-data-dir
1431 (expand-file-name "./schema/" org-e-odt-data-dir)) ; bail out
1432 (eval-when-compile
1433 (and (boundp 'org-e-odt-data-dir) org-e-odt-data-dir ; see make install
1434 (expand-file-name "./schema/" org-e-odt-data-dir)))
1435 (expand-file-name "../contrib/odt/etc/schema/" org-e-odt-lib-dir) ; git
1437 "List of directories to search for OpenDocument schema files.
1438 Use this list to set the default value of
1439 `org-e-odt-schema-dir'. The entries in this list are
1440 populated heuristically based on the values of `org-e-odt-lib-dir'
1441 and `org-e-odt-data-dir'.")
1444 (defconst org-e-odt-styles-dir-list
1445 (list
1446 (and org-e-odt-data-dir
1447 (expand-file-name "./styles/" org-e-odt-data-dir)) ; bail out
1448 (eval-when-compile
1449 (and (boundp 'org-e-odt-data-dir) org-e-odt-data-dir ; see make install
1450 (expand-file-name "./styles/" org-e-odt-data-dir)))
1451 (expand-file-name "../etc/styles/" org-e-odt-lib-dir) ; git
1452 (expand-file-name "./etc/styles/" org-e-odt-lib-dir) ; elpa
1453 (expand-file-name "./org/" data-directory) ; system
1455 "List of directories to search for OpenDocument styles files.
1456 See `org-e-odt-styles-dir'. The entries in this list are populated
1457 heuristically based on the values of `org-e-odt-lib-dir' and
1458 `org-e-odt-data-dir'.")
1460 (defconst org-e-odt-styles-dir
1461 (let* ((styles-dir
1462 (catch 'styles-dir
1463 (message "Debug (org-e-odt): Searching for OpenDocument styles files...")
1464 (mapc (lambda (styles-dir)
1465 (when styles-dir
1466 (message "Debug (org-e-odt): Trying %s..." styles-dir)
1467 (when (and (file-readable-p
1468 (expand-file-name
1469 "OrgOdtContentTemplate.xml" styles-dir))
1470 (file-readable-p
1471 (expand-file-name
1472 "OrgOdtStyles.xml" styles-dir)))
1473 (message "Debug (org-e-odt): Using styles under %s"
1474 styles-dir)
1475 (throw 'styles-dir styles-dir))))
1476 org-e-odt-styles-dir-list)
1477 nil)))
1478 (unless styles-dir
1479 (error "Error (org-e-odt): Cannot find factory styles files. Aborting."))
1480 styles-dir)
1481 "Directory that holds auxiliary XML files used by the ODT exporter.
1483 This directory contains the following XML files -
1484 \"OrgOdtStyles.xml\" and \"OrgOdtContentTemplate.xml\". These
1485 XML files are used as the default values of
1486 `org-e-odt-styles-file' and
1487 `org-e-odt-content-template-file'.
1489 The default value of this variable varies depending on the
1490 version of org in use and is initialized from
1491 `org-e-odt-styles-dir-list'. Note that the user could be using org
1492 from one of: org's own private git repository, GNU ELPA tar or
1493 standard Emacs.")
1495 (defconst org-e-odt-tmpdir-prefix "%s-")
1496 (defconst org-e-odt-bookmark-prefix "OrgXref.")
1498 (defconst org-e-odt-manifest-file-entry-tag
1500 <manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
1504 (defvar org-lparse-dyn-first-heading-pos) ; let bound during org-do-lparse
1506 (defvar org-e-odt-suppress-xref nil)
1507 (defvar org-e-odt-file-extensions
1508 '(("odt" . "OpenDocument Text")
1509 ("ott" . "OpenDocument Text Template")
1510 ("odm" . "OpenDocument Master Document")
1511 ("ods" . "OpenDocument Spreadsheet")
1512 ("ots" . "OpenDocument Spreadsheet Template")
1513 ("odg" . "OpenDocument Drawing (Graphics)")
1514 ("otg" . "OpenDocument Drawing Template")
1515 ("odp" . "OpenDocument Presentation")
1516 ("otp" . "OpenDocument Presentation Template")
1517 ("odi" . "OpenDocument Image")
1518 ("odf" . "OpenDocument Formula")
1519 ("odc" . "OpenDocument Chart")))
1521 (defvar org-e-odt-default-org-styles-alist
1522 '((paragraph . ((default . "Text_20_body")
1523 (fixedwidth . "OrgFixedWidthBlock")
1524 (verse . "OrgVerse")
1525 (quote . "Quotations")
1526 (blockquote . "Quotations")
1527 (center . "OrgCenter")
1528 (left . "OrgLeft")
1529 (right . "OrgRight")
1530 (title . "OrgTitle")
1531 (subtitle . "OrgSubtitle")
1532 (footnote . "Footnote")
1533 (src . "OrgSrcBlock")
1534 (illustration . "Illustration")
1535 (table . "Table")
1536 (listing . "Listing")
1537 (definition-term . "Text_20_body_20_bold")
1538 (horizontal-line . "Horizontal_20_Line")))
1539 (character . ((bold . "Bold")
1540 (emphasis . "Emphasis")
1541 (code . "OrgCode")
1542 (verbatim . "OrgCode")
1543 (strike . "Strikethrough")
1544 (underline . "Underline")
1545 (subscript . "OrgSubscript")
1546 (superscript . "OrgSuperscript")))
1547 (list . ((ordered . "OrgNumberedList")
1548 (unordered . "OrgBulletedList")
1549 (descriptive . "OrgDescriptionList"))))
1550 "Default styles for various entities.")
1552 (defvar org-e-odt-org-styles-alist org-e-odt-default-org-styles-alist)
1554 ;;;_. callbacks
1555 ;;;_. control callbacks
1556 ;;;_ , document body
1558 (defvar org-lparse-body-only) ; let bound during org-do-lparse
1559 (defvar org-lparse-opt-plist) ; bound during org-do-lparse
1560 (defvar org-lparse-list-stack) ; dynamically bound in org-do-lparse
1561 (defvar org-e-odt-list-stack-stashed)
1562 (defvar org-lparse-table-ncols)
1563 (defvar org-e-odt-table-rowgrp-open)
1564 (defvar org-e-odt-table-rownum)
1565 (defvar org-e-odt-table-cur-rowgrp-is-hdr)
1566 (defvar org-lparse-table-is-styled)
1567 (defvar org-lparse-table-rowgrp-info)
1568 (defvar org-lparse-table-colalign-vector)
1570 (defvar org-e-odt-table-style nil
1571 "Table style specified by \"#+ATTR_ODT: <style-name>\" line.
1572 This is set during `org-e-odt-begin-table'.")
1574 (defvar org-e-odt-table-style-spec nil
1575 "Entry for `org-e-odt-table-style' in `org-e-odt-table-styles'.")
1578 (defvar org-e-odt-table-style-format
1580 <style:style style:name=\"%s\" style:family=\"table\">
1581 <style:table-properties style:rel-width=\"%d%%\" fo:margin-top=\"0cm\" fo:margin-bottom=\"0.20cm\" table:align=\"center\"/>
1582 </style:style>
1584 "Template for auto-generated Table styles.")
1586 (defvar org-e-odt-automatic-styles '()
1587 "Registry of automatic styles for various OBJECT-TYPEs.
1588 The variable has the following form:
1589 \(\(OBJECT-TYPE-A
1590 \(\(OBJECT-NAME-A.1 OBJECT-PROPS-A.1\)
1591 \(OBJECT-NAME-A.2 OBJECT-PROPS-A.2\) ...\)\)
1592 \(OBJECT-TYPE-B
1593 \(\(OBJECT-NAME-B.1 OBJECT-PROPS-B.1\)
1594 \(OBJECT-NAME-B.2 OBJECT-PROPS-B.2\) ...\)\)
1595 ...\).
1597 OBJECT-TYPEs could be \"Section\", \"Table\", \"Figure\" etc.
1598 OBJECT-PROPS is (typically) a plist created by passing
1599 \"#+ATTR_ODT: \" option to `org-e-odt-parse-block-attributes'.
1601 Use `org-e-odt-add-automatic-style' to add update this variable.'")
1603 (defvar org-e-odt-object-counters nil
1604 "Running counters for various OBJECT-TYPEs.
1605 Use this to generate automatic names and style-names. See
1606 `org-e-odt-add-automatic-style'.")
1608 (defvar org-e-odt-table-indentedp nil)
1609 (defvar org-lparse-table-colalign-info)
1610 (defvar org-lparse-link-description-is-image nil)
1613 (defvar org-src-block-paragraph-format
1614 "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
1615 <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
1616 <style:background-image/>
1617 </style:paragraph-properties>
1618 <style:text-properties fo:color=\"%s\"/>
1619 </style:style>"
1620 "Custom paragraph style for colorized source and example blocks.
1621 This style is much the same as that of \"OrgFixedWidthBlock\"
1622 except that the foreground and background colors are set
1623 according to the default face identified by the `htmlfontify'.")
1625 (defvar hfy-optimisations)
1626 (defvar org-e-odt-embedded-formulas-count 0)
1627 (defvar org-e-odt-entity-frame-styles
1628 '(("As-CharImage" "__Figure__" ("OrgInlineImage" nil "as-char"))
1629 ("ParagraphImage" "__Figure__" ("OrgDisplayImage" nil "paragraph"))
1630 ("PageImage" "__Figure__" ("OrgPageImage" nil "page"))
1631 ("CaptionedAs-CharImage" "__Figure__"
1632 ("OrgCaptionedImage"
1633 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
1634 ("OrgInlineImage" nil "as-char"))
1635 ("CaptionedParagraphImage" "__Figure__"
1636 ("OrgCaptionedImage"
1637 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
1638 ("OrgImageCaptionFrame" nil "paragraph"))
1639 ("CaptionedPageImage" "__Figure__"
1640 ("OrgCaptionedImage"
1641 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
1642 ("OrgPageImageCaptionFrame" nil "page"))
1643 ("InlineFormula" "__MathFormula__" ("OrgInlineFormula" nil "as-char"))
1644 ("DisplayFormula" "__MathFormula__" ("OrgDisplayFormula" nil "as-char"))
1645 ("CaptionedDisplayFormula" "__MathFormula__"
1646 ("OrgCaptionedFormula" nil "paragraph")
1647 ("OrgFormulaCaptionFrame" nil "as-char"))))
1649 (defvar org-e-odt-embedded-images-count 0)
1651 (defvar org-e-odt-image-size-probe-method
1652 (append (and (executable-find "identify") '(imagemagick)) ; See Bug#10675
1653 '(emacs fixed))
1654 "Ordered list of methods for determining image sizes.")
1656 (defvar org-e-odt-default-image-sizes-alist
1657 '(("as-char" . (5 . 0.4))
1658 ("paragraph" . (5 . 5)))
1659 "Hardcoded image dimensions one for each of the anchor
1660 methods.")
1662 ;; A4 page size is 21.0 by 29.7 cms
1663 ;; The default page settings has 2cm margin on each of the sides. So
1664 ;; the effective text area is 17.0 by 25.7 cm
1665 (defvar org-e-odt-max-image-size '(17.0 . 20.0)
1666 "Limiting dimensions for an embedded image.")
1668 (defvar org-e-odt-entity-labels-alist nil
1669 "Associate Labels with the Labeled entities.
1670 Each element of the alist is of the form (LABEL-NAME
1671 CATEGORY-NAME SEQNO LABEL-STYLE-NAME). LABEL-NAME is same as
1672 that specified by \"#+LABEL: ...\" line. CATEGORY-NAME is the
1673 type of the entity that LABEL-NAME is attached to. CATEGORY-NAME
1674 can be one of \"Table\", \"Figure\" or \"Equation\". SEQNO is
1675 the unique number assigned to the referenced entity on a
1676 per-CATEGORY basis. It is generated sequentially and is 1-based.
1677 LABEL-STYLE-NAME is a key `org-e-odt-label-styles'.
1679 See `org-e-odt-add-label-definition' and
1680 `org-e-odt-fixup-label-references'.")
1682 (defvar org-e-odt-entity-counts-plist nil
1683 "Plist of running counters of SEQNOs for each of the CATEGORY-NAMEs.
1684 See `org-e-odt-entity-labels-alist' for known CATEGORY-NAMEs.")
1686 (defvar org-e-odt-label-styles
1687 '(("math-formula" "%c" "text" "(%n)")
1688 ("math-label" "(%n)" "text" "(%n)")
1689 ("category-and-value" "%e %n: %c" "category-and-value" "%e %n")
1690 ("value" "%e %n: %c" "value" "%n"))
1691 "Specify how labels are applied and referenced.
1692 This is an alist where each element is of the
1693 form (LABEL-STYLE-NAME LABEL-ATTACH-FMT LABEL-REF-MODE
1694 LABEL-REF-FMT).
1696 LABEL-ATTACH-FMT controls how labels and captions are attached to
1697 an entity. It may contain following specifiers - %e, %n and %c.
1698 %e is replaced with the CATEGORY-NAME. %n is replaced with
1699 \"<text:sequence ...> SEQNO </text:sequence>\". %c is replaced
1700 with CAPTION. See `org-e-odt-format-label-definition'.
1702 LABEL-REF-MODE and LABEL-REF-FMT controls how label references
1703 are generated. The following XML is generated for a label
1704 reference - \"<text:sequence-ref
1705 text:reference-format=\"LABEL-REF-MODE\" ...> LABEL-REF-FMT
1706 </text:sequence-ref>\". LABEL-REF-FMT may contain following
1707 specifiers - %e and %n. %e is replaced with the CATEGORY-NAME.
1708 %n is replaced with SEQNO. See
1709 `org-e-odt-format-label-reference'.")
1711 (defcustom org-e-odt-category-strings
1712 '(("en" "Table" "Figure" "Equation" "Equation" "Listing"))
1713 "Specify category strings for various captionable entities.
1714 Captionable entity can be one of a Table, an Embedded Image, a
1715 LaTeX fragment (generated with dvipng) or a Math Formula.
1717 For example, when `org-export-default-language' is \"en\", an
1718 embedded image will be captioned as \"Figure 1: Orgmode Logo\".
1719 If you want the images to be captioned instead as \"Illustration
1720 1: Orgmode Logo\", then modify the entry for \"en\" as shown
1721 below.
1723 \(setq org-e-odt-category-strings
1724 '\(\(\"en\" \"Table\" \"Illustration\"
1725 \"Equation\" \"Equation\"\)\)\)"
1726 :group 'org-export-e-odt
1727 :version "24.1"
1728 :type '(repeat (list (string :tag "Language tag")
1729 (choice :tag "Table"
1730 (const :tag "Use Default" nil)
1731 (string :tag "Category string"))
1732 (choice :tag "Figure"
1733 (const :tag "Use Default" nil)
1734 (string :tag "Category string"))
1735 (choice :tag "Math Formula"
1736 (const :tag "Use Default" nil)
1737 (string :tag "Category string"))
1738 (choice :tag "Dvipng Image"
1739 (const :tag "Use Default" nil)
1740 (string :tag "Category string"))
1741 (choice :tag "Listing"
1742 (const :tag "Use Default" nil)
1743 (string :tag "Category string")))))
1745 (defvar org-e-odt-category-map-alist
1746 '(("__Table__" "Table" "value")
1747 ("__Figure__" "Illustration" "value")
1748 ("__MathFormula__" "Text" "math-formula")
1749 ("__DvipngImage__" "Equation" "value")
1750 ("__Listing__" "Listing" "value")
1751 ;; ("__Table__" "Table" "category-and-value")
1752 ;; ("__Figure__" "Figure" "category-and-value")
1753 ;; ("__DvipngImage__" "Equation" "category-and-value")
1755 "Map a CATEGORY-HANDLE to OD-VARIABLE and LABEL-STYLE.
1756 This is a list where each entry is of the form \\(CATEGORY-HANDLE
1757 OD-VARIABLE LABEL-STYLE\\). CATEGORY_HANDLE identifies the
1758 captionable entity in question. OD-VARIABLE is the OpenDocument
1759 sequence counter associated with the entity. These counters are
1760 declared within
1761 \"<text:sequence-decls>...</text:sequence-decls>\" block of
1762 `org-e-odt-content-template-file'. LABEL-STYLE is a key
1763 into `org-e-odt-label-styles' and specifies how a given entity
1764 should be captioned and referenced.
1766 The position of a CATEGORY-HANDLE in this list is used as an
1767 index in to per-language entry for
1768 `org-e-odt-category-strings' to retrieve a CATEGORY-NAME.
1769 This CATEGORY-NAME is then used for qualifying the user-specified
1770 captions on export.")
1772 (defvar org-e-odt-manifest-file-entries nil)
1773 (defvar hfy-user-sheet-assoc) ; bound during org-do-lparse
1774 (defvar org-lparse-latex-fragment-fallback) ; set by org-do-lparse
1777 ;;;; HTML Internal Variables
1779 (defvar org-e-odt-option-alist
1781 ;; (:agenda-style nil nil org-agenda-export-html-style)
1782 ;; (:convert-org-links nil nil org-e-odt-link-org-files-as-html)
1783 ;; ;; FIXME Use (org-xml-encode-org-text-skip-links s) ??
1784 ;; ;; (:expand-quoted-html nil "@" org-e-odt-expand)
1785 ;; (:inline-images nil nil org-e-odt-inline-images)
1786 ;; ;; (:link-home nil nil org-e-odt-link-home) FIXME
1787 ;; ;; (:link-up nil nil org-e-odt-link-up) FIXME
1788 ;; (:style nil nil org-e-odt-style)
1789 ;; (:style-extra nil nil org-e-odt-style-extra)
1790 ;; (:style-include-default nil nil org-e-odt-style-include-default)
1791 ;; (:style-include-scripts nil nil org-e-odt-style-include-scripts)
1792 ;; ;; (:timestamp nil nil org-e-odt-with-timestamp)
1793 ;; (:html-extension nil nil org-e-odt-extension)
1794 ;; (:html-postamble nil nil org-e-odt-postamble)
1795 ;; (:html-preamble nil nil org-e-odt-preamble)
1796 ;; (:html-table-tag nil nil org-e-odt-table-tag)
1797 ;; (:xml-declaration nil nil org-e-odt-xml-declaration)
1798 (:odt-styles-file "ODT_STYLES_FILE" nil nil t)
1799 (:LaTeX-fragments nil "LaTeX" org-export-with-LaTeX-fragments))
1800 "Alist between export properties and ways to set them.
1802 The car of the alist is the property name, and the cdr is a list
1803 like \(KEYWORD OPTION DEFAULT BEHAVIOUR\) where:
1805 KEYWORD is a string representing a buffer keyword, or nil.
1806 OPTION is a string that could be found in an #+OPTIONS: line.
1807 DEFAULT is the default value for the property.
1808 BEHAVIOUR determine how Org should handle multiple keywords for
1809 the same property. It is a symbol among:
1810 nil Keep old value and discard the new one.
1811 t Replace old value with the new one.
1812 `space' Concatenate the values, separating them with a space.
1813 `newline' Concatenate the values, separating them with
1814 a newline.
1815 `split' Split values at white spaces, and cons them to the
1816 previous list.
1818 KEYWORD and OPTION have precedence over DEFAULT.
1820 All these properties should be back-end agnostic. For back-end
1821 specific properties, define a similar variable named
1822 `org-BACKEND-option-alist', replacing BACKEND with the name of
1823 the appropriate back-end. You can also redefine properties
1824 there, as they have precedence over these.")
1826 (defvar html-table-tag nil) ; dynamically scoped into this.
1828 ;; FIXME: it already exists in org-e-odt.el
1829 (defconst org-e-odt-cvt-link-fn
1831 "Function to convert link URLs to exportable URLs.
1832 Takes two arguments, TYPE and PATH.
1833 Returns exportable url as (TYPE PATH), or nil to signal that it
1834 didn't handle this case.
1835 Intended to be locally bound around a call to `org-export-as-html'." )
1840 (defvar org-e-odt-format-table-no-css)
1841 (defvar htmlize-buffer-places) ; from htmlize.el
1842 (defvar body-only) ; dynamically scoped into this.
1844 (defvar org-e-odt-table-rowgrp-open)
1845 (defvar org-e-odt-table-rownum)
1846 (defvar org-e-odt-table-cur-rowgrp-is-hdr)
1847 (defvar org-lparse-table-is-styled)
1850 (defvar org-e-odt-headline-formatter
1851 (lambda (level snumber todo todo-type priority
1852 title tags target extra-targets extra-class)
1853 (concat snumber " " title)))
1857 ;;; User Configuration Variables
1859 (defgroup org-export-e-odt nil
1860 "Options for exporting Org mode files to ODT."
1861 :tag "Org Export ODT"
1862 :group 'org-export)
1864 (defcustom org-e-odt-protect-char-alist
1865 '(("&" . "&amp;")
1866 ("<" . "&lt;")
1867 (">" . "&gt;"))
1868 "Alist of characters to be converted by `org-e-html-protect'."
1869 :group 'org-export-e-html
1870 :type '(repeat (cons (string :tag "Character")
1871 (string :tag "ODT equivalent"))))
1872 (defcustom org-e-odt-schema-dir
1873 (let* ((schema-dir
1874 (catch 'schema-dir
1875 (message "Debug (org-e-odt): Searching for OpenDocument schema files...")
1876 (mapc
1877 (lambda (schema-dir)
1878 (when schema-dir
1879 (message "Debug (org-e-odt): Trying %s..." schema-dir)
1880 (when (and (file-readable-p
1881 (expand-file-name "od-manifest-schema-v1.2-cs01.rnc"
1882 schema-dir))
1883 (file-readable-p
1884 (expand-file-name "od-schema-v1.2-cs01.rnc"
1885 schema-dir))
1886 (file-readable-p
1887 (expand-file-name "schemas.xml" schema-dir)))
1888 (message "Debug (org-e-odt): Using schema files under %s"
1889 schema-dir)
1890 (throw 'schema-dir schema-dir))))
1891 org-e-odt-schema-dir-list)
1892 (message "Debug (org-e-odt): No OpenDocument schema files installed")
1893 nil)))
1894 schema-dir)
1895 "Directory that contains OpenDocument schema files.
1897 This directory contains:
1898 1. rnc files for OpenDocument schema
1899 2. a \"schemas.xml\" file that specifies locating rules needed
1900 for auto validation of OpenDocument XML files.
1902 Use the customize interface to set this variable. This ensures
1903 that `rng-schema-locating-files' is updated and auto-validation
1904 of OpenDocument XML takes place based on the value
1905 `rng-nxml-auto-validate-flag'.
1907 The default value of this variable varies depending on the
1908 version of org in use and is initialized from
1909 `org-e-odt-schema-dir-list'. The OASIS schema files are available
1910 only in the org's private git repository. It is *not* bundled
1911 with GNU ELPA tar or standard Emacs distribution."
1912 :type '(choice
1913 (const :tag "Not set" nil)
1914 (directory :tag "Schema directory"))
1915 :group 'org-export-e-odt
1916 :version "24.1"
1917 :set
1918 (lambda (var value)
1919 "Set `org-e-odt-schema-dir'.
1920 Also add it to `rng-schema-locating-files'."
1921 (let ((schema-dir value))
1922 (set var
1923 (if (and
1924 (file-readable-p
1925 (expand-file-name "od-manifest-schema-v1.2-cs01.rnc" schema-dir))
1926 (file-readable-p
1927 (expand-file-name "od-schema-v1.2-cs01.rnc" schema-dir))
1928 (file-readable-p
1929 (expand-file-name "schemas.xml" schema-dir)))
1930 schema-dir
1931 (when value
1932 (message "Error (org-e-odt): %s has no OpenDocument schema files"
1933 value))
1934 nil)))
1935 (when org-e-odt-schema-dir
1936 (eval-after-load 'rng-loc
1937 '(add-to-list 'rng-schema-locating-files
1938 (expand-file-name "schemas.xml"
1939 org-e-odt-schema-dir))))))
1941 (defcustom org-e-odt-content-template-file nil
1942 "Template file for \"content.xml\".
1943 The exporter embeds the exported content just before
1944 \"</office:text>\" element.
1946 If unspecified, the file named \"OrgOdtContentTemplate.xml\"
1947 under `org-e-odt-styles-dir' is used."
1948 :type 'file
1949 :group 'org-export-e-odt
1950 :version "24.1")
1952 (defcustom org-e-odt-styles-file nil
1953 "Default styles file for use with ODT export.
1954 Valid values are one of:
1955 1. nil
1956 2. path to a styles.xml file
1957 3. path to a *.odt or a *.ott file
1958 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
1959 ...))
1961 In case of option 1, an in-built styles.xml is used. See
1962 `org-e-odt-styles-dir' for more information.
1964 In case of option 3, the specified file is unzipped and the
1965 styles.xml embedded therein is used.
1967 In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
1968 and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
1969 generated odt file. Use relative path for specifying the
1970 FILE-MEMBERS. styles.xml must be specified as one of the
1971 FILE-MEMBERS.
1973 Use options 1, 2 or 3 only if styles.xml alone suffices for
1974 achieving the desired formatting. Use option 4, if the styles.xml
1975 references additional files like header and footer images for
1976 achieving the desired formatting.
1978 Use \"#+ODT_STYLES_FILE: ...\" directive to set this variable on
1979 a per-file basis. For example,
1981 #+ODT_STYLES_FILE: \"/path/to/styles.xml\" or
1982 #+ODT_STYLES_FILE: (\"/path/to/file.ott\" (\"styles.xml\" \"image/hdr.png\"))."
1983 :group 'org-export-e-odt
1984 :version "24.1"
1985 :type
1986 '(choice
1987 (const :tag "Factory settings" nil)
1988 (file :must-match t :tag "styles.xml")
1989 (file :must-match t :tag "ODT or OTT file")
1990 (list :tag "ODT or OTT file + Members"
1991 (file :must-match t :tag "ODF Text or Text Template file")
1992 (cons :tag "Members"
1993 (file :tag " Member" "styles.xml")
1994 (repeat (file :tag "Member"))))))
1997 (defcustom org-e-odt-inline-image-extensions
1998 '("png" "jpeg" "jpg" "gif")
1999 "Extensions of image files that can be inlined into HTML."
2000 :type '(repeat (string :tag "Extension"))
2001 :group 'org-export-e-odt
2002 :version "24.1")
2004 (defcustom org-e-odt-pixels-per-inch display-pixels-per-inch
2005 "Scaling factor for converting images pixels to inches.
2006 Use this for sizing of embedded images. See Info node `(org)
2007 Images in ODT export' for more information."
2008 :type 'float
2009 :group 'org-export-e-odt
2010 :version "24.1")
2012 (defcustom org-e-odt-create-custom-styles-for-srcblocks t
2013 "Whether custom styles for colorized source blocks be automatically created.
2014 When this option is turned on, the exporter creates custom styles
2015 for source blocks based on the advice of `htmlfontify'. Creation
2016 of custom styles happen as part of `org-e-odt-hfy-face-to-css'.
2018 When this option is turned off exporter does not create such
2019 styles.
2021 Use the latter option if you do not want the custom styles to be
2022 based on your current display settings. It is necessary that the
2023 styles.xml already contains needed styles for colorizing to work.
2025 This variable is effective only if
2026 `org-e-odt-fontify-srcblocks' is turned on."
2027 :group 'org-export-e-odt
2028 :version "24.1"
2029 :type 'boolean)
2031 (defcustom org-e-odt-preferred-output-format nil
2032 "Automatically post-process to this format after exporting to \"odt\".
2033 Interactive commands `org-export-as-e-odt' and
2034 `org-export-as-e-odt-and-open' export first to \"odt\" format and
2035 then use `org-e-odt-convert-process' to convert the
2036 resulting document to this format. During customization of this
2037 variable, the list of valid values are populated based on
2038 `org-e-odt-convert-capabilities'."
2039 :group 'org-export-e-odt
2040 :version "24.1"
2041 :type '(choice :convert-widget
2042 (lambda (w)
2043 (apply 'widget-convert (widget-type w)
2044 (eval (car (widget-get w :args)))))
2045 `((const :tag "None" nil)
2046 ,@(mapcar (lambda (c)
2047 `(const :tag ,c ,c))
2048 (org-e-odt-reachable-formats "odt")))))
2050 (defcustom org-e-odt-table-styles
2051 '(("OrgEquation" "OrgEquation"
2052 ((use-first-column-styles . t)
2053 (use-last-column-styles . t))))
2054 "Specify how Table Styles should be derived from a Table Template.
2055 This is a list where each element is of the
2056 form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
2058 TABLE-STYLE-NAME is the style associated with the table through
2059 `org-e-odt-table-style'.
2061 TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
2062 TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
2063 below) that is included in
2064 `org-e-odt-content-template-file'.
2066 TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
2067 \"TableCell\"
2068 PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
2069 \"TableParagraph\"
2070 TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
2071 \"FirstRow\" | \"LastRow\" |
2072 \"EvenRow\" | \"OddRow\" |
2073 \"EvenColumn\" | \"OddColumn\" | \"\"
2074 where \"+\" above denotes string concatenation.
2076 TABLE-CELL-OPTIONS is an alist where each element is of the
2077 form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
2078 TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
2079 `use-last-row-styles' |
2080 `use-first-column-styles' |
2081 `use-last-column-styles' |
2082 `use-banding-rows-styles' |
2083 `use-banding-columns-styles' |
2084 `use-first-row-styles'
2085 ON-OR-OFF := `t' | `nil'
2087 For example, with the following configuration
2089 \(setq org-e-odt-table-styles
2090 '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
2091 \(\(use-first-row-styles . t\)
2092 \(use-first-column-styles . t\)\)\)
2093 \(\"TableWithHeaderColumns\" \"Custom\"
2094 \(\(use-first-column-styles . t\)\)\)\)\)
2096 1. A table associated with \"TableWithHeaderRowsAndColumns\"
2097 style will use the following table-cell styles -
2098 \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
2099 \"CustomTableCell\" and the following paragraph styles
2100 \"CustomFirstRowTableParagraph\",
2101 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
2102 as appropriate.
2104 2. A table associated with \"TableWithHeaderColumns\" style will
2105 use the following table-cell styles -
2106 \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
2107 following paragraph styles
2108 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
2109 as appropriate..
2111 Note that TABLE-TEMPLATE-NAME corresponds to the
2112 \"<table:table-template>\" elements contained within
2113 \"<office:styles>\". The entries (TABLE-STYLE-NAME
2114 TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
2115 \"table:template-name\" and \"table:use-first-row-styles\" etc
2116 attributes of \"<table:table>\" element. Refer ODF-1.2
2117 specification for more information. Also consult the
2118 implementation filed under `org-e-odt-get-table-cell-styles'.
2120 The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
2121 formatting of numbered display equations. Do not delete this
2122 style from the list."
2123 :group 'org-export-e-odt
2124 :version "24.1"
2125 :type '(choice
2126 (const :tag "None" nil)
2127 (repeat :tag "Table Styles"
2128 (list :tag "Table Style Specification"
2129 (string :tag "Table Style Name")
2130 (string :tag "Table Template Name")
2131 (alist :options (use-first-row-styles
2132 use-last-row-styles
2133 use-first-column-styles
2134 use-last-column-styles
2135 use-banding-rows-styles
2136 use-banding-columns-styles)
2137 :key-type symbol
2138 :value-type (const :tag "True" t))))))
2139 (defcustom org-e-odt-fontify-srcblocks t
2140 "Specify whether or not source blocks need to be fontified.
2141 Turn this option on if you want to colorize the source code
2142 blocks in the exported file. For colorization to work, you need
2143 to make available an enhanced version of `htmlfontify' library."
2144 :type 'boolean
2145 :group 'org-export-e-odt
2146 :version "24.1")
2148 (defcustom org-e-odt-prettify-xml t ; FIXME
2149 "Specify whether or not the xml output should be prettified.
2150 When this option is turned on, `indent-region' is run on all
2151 component xml buffers before they are saved. Turn this off for
2152 regular use. Turn this on if you need to examine the xml
2153 visually."
2154 :group 'org-export-e-odt
2155 :version "24.1"
2156 :type 'boolean)
2158 (defcustom org-e-odt-convert-processes
2159 '(("LibreOffice"
2160 "soffice --headless --convert-to %f%x --outdir %d %i")
2161 ("unoconv"
2162 "unoconv -f %f -o %d %i"))
2163 "Specify a list of document converters and their usage.
2164 The converters in this list are offered as choices while
2165 customizing `org-e-odt-convert-process'.
2167 This variable is a list where each element is of the
2168 form (CONVERTER-NAME CONVERTER-CMD). CONVERTER-NAME is the name
2169 of the converter. CONVERTER-CMD is the shell command for the
2170 converter and can contain format specifiers. These format
2171 specifiers are interpreted as below:
2173 %i input file name in full
2174 %I input file name as a URL
2175 %f format of the output file
2176 %o output file name in full
2177 %O output file name as a URL
2178 %d output dir in full
2179 %D output dir as a URL.
2180 %x extra options as set in `org-e-odt-convert-capabilities'."
2181 :group 'org-export-e-odt
2182 :version "24.1"
2183 :type
2184 '(choice
2185 (const :tag "None" nil)
2186 (alist :tag "Converters"
2187 :key-type (string :tag "Converter Name")
2188 :value-type (group (string :tag "Command line")))))
2190 (defcustom org-e-odt-convert-process "LibreOffice"
2191 "Use this converter to convert from \"odt\" format to other formats.
2192 During customization, the list of converter names are populated
2193 from `org-e-odt-convert-processes'."
2194 :group 'org-export-e-odt
2195 :version "24.1"
2196 :type '(choice :convert-widget
2197 (lambda (w)
2198 (apply 'widget-convert (widget-type w)
2199 (eval (car (widget-get w :args)))))
2200 `((const :tag "None" nil)
2201 ,@(mapcar (lambda (c)
2202 `(const :tag ,(car c) ,(car c)))
2203 org-e-odt-convert-processes))))
2205 (defcustom org-e-odt-convert-capabilities
2206 '(("Text"
2207 ("odt" "ott" "doc" "rtf" "docx")
2208 (("pdf" "pdf") ("odt" "odt") ("rtf" "rtf") ("ott" "ott")
2209 ("doc" "doc" ":\"MS Word 97\"") ("docx" "docx") ("html" "html")))
2210 ("Web"
2211 ("html")
2212 (("pdf" "pdf") ("odt" "odt") ("html" "html")))
2213 ("Spreadsheet"
2214 ("ods" "ots" "xls" "csv" "xlsx")
2215 (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv") ("ods" "ods")
2216 ("xls" "xls") ("xlsx" "xlsx")))
2217 ("Presentation"
2218 ("odp" "otp" "ppt" "pptx")
2219 (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("otp" "otp") ("ppt" "ppt")
2220 ("pptx" "pptx") ("odg" "odg"))))
2221 "Specify input and output formats of `org-e-odt-convert-process'.
2222 More correctly, specify the set of input and output formats that
2223 the user is actually interested in.
2225 This variable is an alist where each element is of the
2226 form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
2227 INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
2228 alist where each element is of the form (OUTPUT-FMT
2229 OUTPUT-FILE-EXTENSION EXTRA-OPTIONS).
2231 The variable is interpreted as follows:
2232 `org-e-odt-convert-process' can take any document that is in
2233 INPUT-FMT-LIST and produce any document that is in the
2234 OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
2235 OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
2236 serves dual purposes:
2237 - It is used for populating completion candidates during
2238 `org-e-odt-convert' commands.
2239 - It is used as the value of \"%f\" specifier in
2240 `org-e-odt-convert-process'.
2242 EXTRA-OPTIONS is used as the value of \"%x\" specifier in
2243 `org-e-odt-convert-process'.
2245 DOCUMENT-CLASS is used to group a set of file formats in
2246 INPUT-FMT-LIST in to a single class.
2248 Note that this variable inherently captures how LibreOffice based
2249 converters work. LibreOffice maps documents of various formats
2250 to classes like Text, Web, Spreadsheet, Presentation etc and
2251 allow document of a given class (irrespective of it's source
2252 format) to be converted to any of the export formats associated
2253 with that class.
2255 See default setting of this variable for an typical
2256 configuration."
2257 :group 'org-export-e-odt
2258 :version "24.1"
2259 :type
2260 '(choice
2261 (const :tag "None" nil)
2262 (alist :tag "Capabilities"
2263 :key-type (string :tag "Document Class")
2264 :value-type
2265 (group (repeat :tag "Input formats" (string :tag "Input format"))
2266 (alist :tag "Output formats"
2267 :key-type (string :tag "Output format")
2268 :value-type
2269 (group (string :tag "Output file extension")
2270 (choice
2271 (const :tag "None" nil)
2272 (string :tag "Extra options"))))))))
2274 ;;;; Debugging
2277 ;;;; Document
2279 ;;;; Document Header (Styles)
2281 ;;;; Document Header (Scripts)
2283 ;;;; Document Header (Mathjax)
2285 ;;;; Preamble
2287 ;;;; Postamble
2289 ;;;; Emphasis
2291 ;;;; Todos
2293 ;;;; Tags
2295 ;;;; Time-stamps
2296 ;;;; Statistics Cookie
2297 ;;;; Subscript
2298 ;;;; Superscript
2300 ;;;; Inline images
2302 ;;;; Block
2303 ;;;; Comment
2304 ;;;; Comment Block
2305 ;;;; Drawer
2306 ;;;; Dynamic Block
2307 ;;;; Emphasis
2308 ;;;; Entity
2309 ;;;; Example Block
2310 ;;;; Export Snippet
2311 ;;;; Export Block
2312 ;;;; Fixed Width
2313 ;;;; Footnotes
2315 ;;;; Headline
2316 ;;;; Horizontal Rule
2317 ;;;; Inline Babel Call
2318 ;;;; Inline Src Block
2319 ;;;; Inlinetask
2320 ;;;; Item
2321 ;;;; Keyword
2322 ;;;; Latex Environment
2323 ;;;; Latex Fragment
2324 ;;;; Line Break
2325 ;;;; Link
2326 ;;;; Babel Call
2327 ;;;; Macro
2328 ;;;; Paragraph
2329 ;;;; Plain List
2330 ;;;; Plain Text
2331 ;;;; Property Drawer
2332 ;;;; Quote Block
2333 ;;;; Quote Section
2334 ;;;; Section
2335 ;;;; Radio Target
2336 ;;;; Special Block
2337 ;;;; Src Block
2339 ;;;; Table
2341 ;;;; Target
2342 ;;;; Time-stamp
2344 ;;;; Verbatim
2345 ;;;; Verse Block
2346 ;;;; Headline
2348 ;;;; Links
2349 ;;;; Drawers
2350 ;;;; Inlinetasks
2351 ;;;; Publishing
2353 ;;;; Compilation
2357 ;;; User Configurable Variables (MAYBE)
2359 ;;;; Preamble
2361 ;;;; Headline
2363 ;;;; Emphasis
2365 (defcustom org-e-odt-format-headline-function nil
2366 "Function to format headline text.
2368 This function will be called with 5 arguments:
2369 TODO the todo keyword \(string or nil\).
2370 TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
2371 PRIORITY the priority of the headline \(integer or nil\)
2372 TEXT the main headline text \(string\).
2373 TAGS the tags string, separated with colons \(string or nil\).
2375 The function result will be used in the section format string.
2377 As an example, one could set the variable to the following, in
2378 order to reproduce the default set-up:
2380 \(defun org-e-odt-format-headline \(todo todo-type priority text tags\)
2381 \"Default format function for an headline.\"
2382 \(concat \(when todo
2383 \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo\)\)
2384 \(when priority
2385 \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
2386 text
2387 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
2388 :group 'org-export-e-odt
2389 :type 'function)
2391 ;;;; Footnotes
2393 ;;;; Time-stamps
2395 (defcustom org-e-odt-active-timestamp-format "\\textit{%s}"
2396 "A printf format string to be applied to active time-stamps."
2397 :group 'org-export-e-odt
2398 :type 'string)
2400 (defcustom org-e-odt-inactive-timestamp-format "\\textit{%s}"
2401 "A printf format string to be applied to inactive time-stamps."
2402 :group 'org-export-e-odt
2403 :type 'string)
2405 (defcustom org-e-odt-diary-timestamp-format "\\textit{%s}"
2406 "A printf format string to be applied to diary time-stamps."
2407 :group 'org-export-e-odt
2408 :type 'string)
2411 ;;;; Links
2413 (defcustom org-e-odt-inline-image-rules
2414 '(("file" . "\\.\\(jpeg\\|jpg\\|png\\|gif\\)\\'"))
2415 "Rules characterizing image files that can be inlined into HTML.
2417 A rule consists in an association whose key is the type of link
2418 to consider, and value is a regexp that will be matched against
2419 link's path.
2421 Note that, by default, the image extension *actually* allowed
2422 depend on the way the HTML file is processed. When used with
2423 pdflatex, pdf, jpg and png images are OK. When processing
2424 through dvi to Postscript, only ps and eps are allowed. The
2425 default we use here encompasses both."
2426 :group 'org-export-e-odt
2427 :type '(alist :key-type (string :tag "Type")
2428 :value-type (regexp :tag "Path")))
2430 ;;;; Tables
2432 (defcustom org-e-odt-table-caption-above t
2433 "When non-nil, place caption string at the beginning of the table.
2434 Otherwise, place it near the end."
2435 :group 'org-export-e-odt
2436 :type 'boolean)
2438 ;;;; Drawers
2440 (defcustom org-e-odt-format-drawer-function nil
2441 "Function called to format a drawer in HTML code.
2443 The function must accept two parameters:
2444 NAME the drawer name, like \"LOGBOOK\"
2445 CONTENTS the contents of the drawer.
2447 The function should return the string to be exported.
2449 For example, the variable could be set to the following function
2450 in order to mimic default behaviour:
2452 \(defun org-e-odt-format-drawer-default \(name contents\)
2453 \"Format a drawer element for HTML export.\"
2454 contents\)"
2455 :group 'org-export-e-odt
2456 :type 'function)
2459 ;;;; Inlinetasks
2461 (defcustom org-e-odt-format-inlinetask-function nil
2462 "Function called to format an inlinetask in HTML code.
2464 The function must accept six parameters:
2465 TODO the todo keyword, as a string
2466 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
2467 PRIORITY the inlinetask priority, as a string
2468 NAME the inlinetask name, as a string.
2469 TAGS the inlinetask tags, as a string.
2470 CONTENTS the contents of the inlinetask, as a string.
2472 The function should return the string to be exported.
2474 For example, the variable could be set to the following function
2475 in order to mimic default behaviour:
2477 \(defun org-e-odt-format-inlinetask \(todo type priority name tags contents\)
2478 \"Format an inline task element for HTML export.\"
2479 \(let \(\(full-title
2480 \(concat
2481 \(when todo
2482 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
2483 \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
2484 title
2485 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
2486 \(format \(concat \"\\\\begin{center}\\n\"
2487 \"\\\\fbox{\\n\"
2488 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
2489 \"%s\\n\\n\"
2490 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
2491 \"%s\"
2492 \"\\\\end{minipage}}\"
2493 \"\\\\end{center}\"\)
2494 full-title contents\)\)"
2495 :group 'org-export-e-odt
2496 :type 'function)
2499 ;; Src blocks
2501 ;;;; Plain text
2503 (defcustom org-e-odt-quotes
2504 '(("fr" ("\\(\\s-\\|[[(]\\)\"" . "«~") ("\\(\\S-\\)\"" . "~»") ("\\(\\s-\\|(\\)'" . "'"))
2505 ("en" ("\\(\\s-\\|[[(]\\)\"" . "``") ("\\(\\S-\\)\"" . "''") ("\\(\\s-\\|(\\)'" . "`")))
2506 "Alist for quotes to use when converting english double-quotes.
2508 The CAR of each item in this alist is the language code.
2509 The CDR of each item in this alist is a list of three CONS:
2510 - the first CONS defines the opening quote;
2511 - the second CONS defines the closing quote;
2512 - the last CONS defines single quotes.
2514 For each item in a CONS, the first string is a regexp
2515 for allowed characters before/after the quote, the second
2516 string defines the replacement string for this quote."
2517 :group 'org-export-e-odt
2518 :type '(list
2519 (cons :tag "Opening quote"
2520 (string :tag "Regexp for char before")
2521 (string :tag "Replacement quote "))
2522 (cons :tag "Closing quote"
2523 (string :tag "Regexp for char after ")
2524 (string :tag "Replacement quote "))
2525 (cons :tag "Single quote"
2526 (string :tag "Regexp for char before")
2527 (string :tag "Replacement quote "))))
2530 ;;;; Compilation
2534 ;;; Internal Functions (HTML)
2536 ;; (defun org-e-odt-format-inline-image (path &optional caption label attr)
2537 ;; ;; FIXME: alt text missing here?
2538 ;; (let ((inline-image (format "<img src=\"%s\" alt=\"%s\"/>"
2539 ;; path (file-name-nondirectory path))))
2540 ;; (if (not label) inline-image
2541 ;; (org-e-odt-format-section inline-image "figure" label))))
2543 ;;;; Bibliography
2545 (defun org-e-odt-bibliography ()
2546 "Find bibliography, cut it out and return it."
2547 (catch 'exit
2548 (let (beg end (cnt 1) bib)
2549 (save-excursion
2550 (goto-char (point-min))
2551 (when (re-search-forward
2552 "^[ \t]*<div \\(id\\|class\\)=\"bibliography\"" nil t)
2553 (setq beg (match-beginning 0))
2554 (while (re-search-forward "</?div\\>" nil t)
2555 (setq cnt (+ cnt (if (string= (match-string 0) "<div") +1 -1)))
2556 (when (= cnt 0)
2557 (and (looking-at ">") (forward-char 1))
2558 (setq bib (buffer-substring beg (point)))
2559 (delete-region beg (point))
2560 (throw 'exit bib))))
2561 nil))))
2563 ;;;; Table
2565 (defun org-e-odt-format-table (lines olines)
2566 (let ((org-e-odt-format-table-no-css nil))
2567 (org-lparse-format-table lines olines)))
2569 (defun org-e-odt-splice-attributes (tag attributes)
2570 "Read attributes in string ATTRIBUTES, add and replace in HTML tag TAG."
2571 (if (not attributes)
2573 (let (oldatt newatt)
2574 (setq oldatt (org-extract-attributes-from-string tag)
2575 tag (pop oldatt)
2576 newatt (cdr (org-extract-attributes-from-string attributes)))
2577 (while newatt
2578 (setq oldatt (plist-put oldatt (pop newatt) (pop newatt))))
2579 (if (string-match ">" tag)
2580 (setq tag
2581 (replace-match (concat (org-attributes-to-string oldatt) ">")
2582 t t tag)))
2583 tag)))
2585 (defun org-export-splice-style (style extra)
2586 "Splice EXTRA into STYLE, just before \"</style>\"."
2587 (if (and (stringp extra)
2588 (string-match "\\S-" extra)
2589 (string-match "</style>" style))
2590 (concat (substring style 0 (match-beginning 0))
2591 "\n" extra "\n"
2592 (substring style (match-beginning 0)))
2593 style))
2595 (defun org-e-odt-toc-entry-formatter
2596 (level snumber todo todo-type priority
2597 headline tags target extra-targets extra-class)
2598 (org-e-odt-format-toc-entry snumber todo headline tags target))
2600 (defun org-e-odt-make-string (n string)
2601 (let (out) (dotimes (i n out) (setq out (concat string out)))))
2603 (defun org-e-odt-toc-text (toc-entries)
2604 (let* ((prev-level (1- (nth 1 (car toc-entries))))
2605 (start-level prev-level))
2606 (mapconcat
2607 (lambda (entry)
2608 (let ((headline (nth 0 entry))
2609 (level (nth 1 entry)))
2610 (prog1 (org-e-odt-format-toc-item headline level prev-level)
2611 (setq prev-level level))))
2612 toc-entries "")))
2614 (defun* org-e-odt-format-toc-headline
2615 (todo todo-type priority text tags
2616 &key level section-number headline-label &allow-other-keys)
2617 ;; FIXME
2618 (setq text (concat
2619 (and org-export-with-section-numbers
2620 (concat section-number ". "))
2621 text
2622 (and tags
2623 (concat
2624 (org-e-odt-format-spaces 3)
2625 (org-e-odt-format-fontify tags "tag")))))
2626 (when todo
2627 (setq text (org-e-odt-format-fontify text "todo")))
2629 (let ((org-e-odt-suppress-xref t))
2630 (org-e-odt-format-link text (concat "#" headline-label))))
2632 (defun org-e-odt-toc (depth info)
2633 (assert (wholenump depth))
2634 (let* ((headlines (org-export-collect-headlines info depth))
2635 (toc-entries
2636 (loop for headline in headlines collect
2637 (list (org-e-odt-format-headline--wrap
2638 headline info 'org-e-odt-format-toc-headline)
2639 (org-export-get-relative-level headline info)))))
2640 (when toc-entries
2641 (let* ((lang-specific-heading "Table of Contents")) ; FIXME
2642 (concat
2643 (org-e-odt-begin-toc lang-specific-heading depth)
2644 (org-e-odt-toc-text toc-entries)
2645 (org-e-odt-end-toc))))))
2647 (defun org-e-odt-begin-outline (level1 snumber title tags
2648 target extra-targets extra-class)
2649 (let* ((class (format "outline-%d" level1))
2650 (class (if extra-class (concat class " " extra-class) class))
2651 (id (format "outline-container-%s"
2652 (org-lparse-suffix-from-snumber snumber)))
2653 (extra (concat (when id (format " id=\"%s\"" id))
2654 (when class (format " class=\"%s\"" class)))))
2655 (org-lparse-insert-tag "<div%s>" extra)
2656 (insert
2657 (org-lparse-format 'HEADING
2658 (org-lparse-format
2659 'HEADLINE title extra-targets tags snumber level1)
2660 level1 target))))
2662 (defun org-e-odt-end-outline ()
2663 (org-lparse-insert-tag "</div>"))
2665 (defun org-e-odt-suffix-from-snumber (snumber)
2666 (let* ((snu (replace-regexp-in-string "\\." "-" snumber))
2667 (href (cdr (assoc (concat "sec-" snu)
2668 org-export-preferred-target-alist))))
2669 (org-solidify-link-text (or href snu))))
2671 (defun org-e-odt-format-outline (contents level1 snumber title
2672 tags target extra-targets extra-class)
2675 ;; (defun org-e-odt-format-line (line)
2676 ;; (case org-lparse-dyn-current-environment
2677 ;; ((quote fixedwidth) (concat (org-e-odt-encode-plain-text line) "\n"))
2678 ;; (t (concat line "\n"))))
2680 (defun org-e-odt-fix-class-name (kwd) ; audit callers of this function
2681 "Turn todo keyword into a valid class name.
2682 Replaces invalid characters with \"_\"."
2683 (save-match-data
2684 (while (string-match "[^a-zA-Z0-9_]" kwd)
2685 (setq kwd (replace-match "_" t t kwd))))
2686 kwd)
2688 (defun org-e-odt-format-internal-link (text href &optional extra)
2689 (org-e-odt-format-link text (concat "#" href) extra))
2691 (defun org-e-odt-format-extra-targets (extra-targets)
2692 (if (not extra-targets) ""
2693 (mapconcat (lambda (x)
2694 (when x
2695 (setq x (org-solidify-link-text
2696 (if (org-uuidgen-p x) (concat "ID-" x) x)))
2697 (org-e-odt-format-anchor "" x))) extra-targets "")))
2699 (defun org-e-odt-format-org-tags (tags)
2700 (if (not tags) ""
2701 (org-e-odt-format-fontify
2702 (mapconcat
2703 (lambda (x)
2704 (org-e-odt-format-fontify
2705 x (concat "" ;; org-e-odt-tag-class-prefix
2706 (org-e-odt-fix-class-name x))))
2707 (org-split-string tags ":")
2708 (org-e-odt-format-spaces 1)) "tag")))
2710 (defun org-e-odt-format-section-number (&optional snumber level)
2711 ;; FIXME
2712 (and nil org-export-with-section-numbers
2713 ;; (not org-lparse-body-only)
2714 snumber level
2715 (org-e-odt-format-fontify snumber (format "section-number-%d" level))))
2717 ;; (defun org-e-odt-format-headline (title extra-targets tags
2718 ;; &optional snumber level)
2719 ;; (concat
2720 ;; (org-e-odt-format-extra-targets extra-targets)
2721 ;; (concat (org-e-odt-format-section-number snumber level) " ")
2722 ;; title
2723 ;; (and tags (concat (org-e-odt-format-spaces 3)
2724 ;; (org-e-odt-format-org-tags tags)))))
2726 (defun org-e-odt-get-coding-system-for-write ()
2727 (or org-e-odt-coding-system
2728 (and (boundp 'buffer-file-coding-system) buffer-file-coding-system)))
2730 (defun org-e-odt-get-coding-system-for-save ()
2731 (or org-e-odt-coding-system
2732 (and (boundp 'buffer-file-coding-system) buffer-file-coding-system)))
2734 ;; (defun org-e-odt-format-date (info)
2735 ;; (let ((date (plist-get info :date)))
2736 ;; (cond
2737 ;; ((and date (string-match "%" date))
2738 ;; (format-time-string date))
2739 ;; (date date)
2740 ;; (t (format-time-string "%Y-%m-%d %T %Z")))))
2744 ;;; Internal Functions (Ngz)
2746 (defun org-e-odt--caption/label-string (caption label info)
2747 "Return caption and label HTML string for floats.
2749 CAPTION is a cons cell of secondary strings, the car being the
2750 standard caption and the cdr its short form. LABEL is a string
2751 representing the label. INFO is a plist holding contextual
2752 information.
2754 If there's no caption nor label, return the empty string.
2756 For non-floats, see `org-e-odt--wrap-label'."
2757 (setq label nil) ;; FIXME
2759 (let ((label-str (if label (format "\\label{%s}" label) "")))
2760 (cond
2761 ((and (not caption) (not label)) "")
2762 ((not caption) (format "\\label{%s}\n" label))
2763 ;; Option caption format with short name.
2764 ((cdr caption)
2765 (format "\\caption[%s]{%s%s}\n"
2766 (org-export-secondary-string (cdr caption) 'e-odt info)
2767 label-str
2768 (org-export-secondary-string (car caption) 'e-odt info)))
2769 ;; Standard caption format.
2770 ;; (t (format "\\caption{%s%s}\n"
2771 ;; label-str
2772 ;; (org-export-secondary-string (car caption) 'e-odt info)))
2774 (t (org-export-secondary-string (car caption) 'e-odt info)))))
2776 (defun org-e-odt--find-verb-separator (s)
2777 "Return a character not used in string S.
2778 This is used to choose a separator for constructs like \\verb."
2779 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
2780 (loop for c across ll
2781 when (not (string-match (regexp-quote (char-to-string c)) s))
2782 return (char-to-string c))))
2784 (defun org-e-odt--quotation-marks (text info)
2785 "Export quotation marks depending on language conventions.
2786 TEXT is a string containing quotation marks to be replaced. INFO
2787 is a plist used as a communication channel."
2788 (mapc (lambda(l)
2789 (let ((start 0))
2790 (while (setq start (string-match (car l) text start))
2791 (let ((new-quote (concat (match-string 1 text) (cdr l))))
2792 (setq text (replace-match new-quote t t text))))))
2793 (cdr (or (assoc (plist-get info :language) org-e-odt-quotes)
2794 ;; Falls back on English.
2795 (assoc "en" org-e-odt-quotes))))
2796 text)
2798 (defun org-e-odt--wrap-label (element output)
2799 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
2800 This function shouldn't be used for floats. See
2801 `org-e-odt--caption/label-string'."
2802 ;; (let ((label (org-element-property :name element)))
2803 ;; (if (or (not output) (not label) (string= output "") (string= label ""))
2804 ;; output
2805 ;; (concat (format "\\label{%s}\n" label) output)))
2806 output)
2810 ;;; Transcode Helpers
2812 (defun* org-e-odt-format-headline
2813 (todo todo-type priority text tags
2814 &key level section-number headline-label &allow-other-keys)
2815 (concat (org-e-odt-todo todo) (and todo " ") text
2816 (and tags (org-e-odt-format-spaces 3))
2817 (and tags (org-e-odt-format-org-tags tags))))
2819 ;;;; Src Code
2821 (defun org-e-odt-htmlfontify-string (line)
2822 (let* ((hfy-html-quote-regex "\\([<\"&> ]\\)")
2823 (hfy-html-quote-map '(("\"" "&quot;")
2824 ("<" "&lt;")
2825 ("&" "&amp;")
2826 (">" "&gt;")
2827 (" " "<text:s/>")
2828 (" " "<text:tab/>")))
2829 (hfy-face-to-css 'org-e-odt-hfy-face-to-css)
2830 (hfy-optimisations-1 (copy-seq hfy-optimisations))
2831 (hfy-optimisations (add-to-list 'hfy-optimisations-1
2832 'body-text-only))
2833 (hfy-begin-span-handler
2834 (lambda (style text-block text-id text-begins-block-p)
2835 (insert (format "<text:span text:style-name=\"%s\">" style))))
2836 (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
2837 (htmlfontify-string line)))
2839 (defun org-e-odt-do-format-code
2840 (code &optional lang refs retain-labels num-start)
2841 (let* ((lang (or (assoc-default lang org-src-lang-modes) lang))
2842 (lang-mode (and lang (intern (format "%s-mode" lang))))
2843 (code-lines (org-split-string code "\n"))
2844 (code-length (length code-lines))
2845 (use-htmlfontify-p (and (functionp lang-mode)
2846 org-e-odt-fontify-srcblocks
2847 (require 'htmlfontify nil t)
2848 (fboundp 'htmlfontify-string)))
2849 (code (if (not use-htmlfontify-p) code
2850 (with-temp-buffer
2851 (insert code)
2852 (funcall lang-mode)
2853 (font-lock-fontify-buffer)
2854 (buffer-string))))
2855 (fontifier (if use-htmlfontify-p 'org-e-odt-htmlfontify-string
2856 'org-e-odt-encode-plain-text))
2857 (par-style (if use-htmlfontify-p "OrgSrcBlock"
2858 "OrgFixedWidthBlock"))
2859 (i 0))
2860 (assert (= code-length (length (org-split-string code "\n"))))
2861 (setq code
2862 (org-export-format-code
2863 code
2864 (lambda (loc line-num ref)
2865 (setq par-style
2866 (concat par-style (and (= (incf i) code-length) "LastLine")))
2868 (setq loc (concat loc (and ref retain-labels (format " (%s)" ref))))
2869 (setq loc (funcall fontifier loc))
2870 (when ref
2871 (setq loc (org-e-odt-format-target loc (concat "coderef-" ref))))
2872 (setq loc (org-e-odt-format-stylized-paragraph par-style loc))
2873 (if (not line-num) loc
2874 (org-e-odt-format-tags
2875 '("<text:list-item>" . "</text:list-item>") loc)))
2876 num-start refs))
2877 (cond
2878 ((not num-start) code)
2879 ((equal num-start 0)
2880 (org-e-odt-format-tags
2881 '("<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>"
2882 . "</text:list>") code " text:continue-numbering=\"false\""))
2883 (t (org-e-odt-format-tags
2884 '("<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>"
2885 . "</text:list>") code " text:continue-numbering=\"true\"")))))
2887 (defun org-e-odt-format-code (element info)
2888 (let* ((lang (org-element-property :language element))
2889 ;; Extract code and references.
2890 (code-info (org-export-unravel-code element))
2891 (code (car code-info))
2892 (refs (cdr code-info))
2893 ;; Does the src block contain labels?
2894 (retain-labels (org-element-property :retain-labels element))
2895 ;; Does it have line numbers?
2896 (num-start (case (org-element-property :number-lines element)
2897 (continued (org-export-get-loc element info))
2898 (new 0))))
2899 (org-e-odt-do-format-code code lang refs retain-labels num-start)))
2903 ;;; Template
2905 (defun org-e-odt-template (contents info)
2906 "Return complete document string after HTML conversion.
2907 CONTENTS is the transcoded contents string. RAW-DATA is the
2908 original parsed data. INFO is a plist holding export options."
2909 ;; write meta file
2910 (org-e-odt-update-meta-file info)
2911 (with-temp-buffer
2912 (insert-file-contents
2913 (or org-e-odt-content-template-file
2914 (expand-file-name "OrgOdtContentTemplate.xml"
2915 org-e-odt-styles-dir)))
2916 (goto-char (point-min))
2917 (re-search-forward "</office:text>" nil nil)
2918 (goto-char (match-beginning 0))
2920 ;; Title
2921 (insert (org-e-odt-format-preamble info))
2922 ;; Table of Contents
2923 (let ((depth (plist-get info :with-toc)))
2924 (when (wholenump depth) (insert (org-e-odt-toc depth info))))
2926 ;; Copy styles.xml. Also dump htmlfontify styles, if there is any.
2927 (org-e-odt-update-styles-file info)
2929 ;; Update styles.xml - take care of outline numbering
2930 (with-current-buffer
2931 (find-file-noselect (expand-file-name "styles.xml") t)
2932 ;; Don't make automatic backup of styles.xml file. This setting
2933 ;; prevents the backed-up styles.xml file from being zipped in to
2934 ;; odt file. This is more of a hackish fix. Better alternative
2935 ;; would be to fix the zip command so that the output odt file
2936 ;; includes only the needed files and excludes any auto-generated
2937 ;; extra files like backups and auto-saves etc etc. Note that
2938 ;; currently the zip command zips up the entire temp directory so
2939 ;; that any auto-generated files created under the hood ends up in
2940 ;; the resulting odt file.
2941 (set (make-local-variable 'backup-inhibited) t)
2942 (org-e-odt-configure-outline-numbering))
2944 ;; Contents
2945 (insert contents)
2946 (buffer-substring-no-properties (point-min) (point-max))))
2950 ;;; Transcode Functions
2952 ;;;; Block
2954 (defun org-e-odt-center-block (center-block contents info)
2955 "Transcode a CENTER-BLOCK element from Org to HTML.
2956 CONTENTS holds the contents of the block. INFO is a plist
2957 holding contextual information."
2958 (org-e-odt--wrap-label center-block contents))
2961 ;;;; Comment
2963 ;; Comments are ignored.
2966 ;;;; Comment Block
2968 ;; Comment Blocks are ignored.
2971 ;;;; Drawer
2973 (defun org-e-odt-drawer (drawer contents info)
2974 "Transcode a DRAWER element from Org to HTML.
2975 CONTENTS holds the contents of the block. INFO is a plist
2976 holding contextual information."
2977 (let* ((name (org-element-property :drawer-name drawer))
2978 (output (if (functionp org-e-odt-format-drawer-function)
2979 (funcall org-e-odt-format-drawer-function
2980 name contents)
2981 ;; If there's no user defined function: simply
2982 ;; display contents of the drawer.
2983 contents)))
2984 (org-e-odt--wrap-label drawer output)))
2987 ;;;; Dynamic Block
2989 (defun org-e-odt-dynamic-block (dynamic-block contents info)
2990 "Transcode a DYNAMIC-BLOCK element from Org to HTML.
2991 CONTENTS holds the contents of the block. INFO is a plist
2992 holding contextual information. See
2993 `org-export-data'."
2994 (org-e-odt--wrap-label dynamic-block contents))
2997 ;;;; Emphasis
2999 (defun org-e-odt-emphasis (emphasis contents info)
3000 "Transcode EMPHASIS from Org to HTML.
3001 CONTENTS is the contents of the emphasized text. INFO is a plist
3002 holding contextual information.."
3003 ;; (format (cdr (assoc (org-element-property :marker emphasis)
3004 ;; org-e-odt-emphasis-alist))
3005 ;; contents)
3006 (org-e-odt-format-fontify
3007 contents (cadr (assoc
3008 (org-element-property :marker emphasis)
3009 '(("*" bold)
3010 ("/" emphasis)
3011 ("_" underline)
3012 ("=" code)
3013 ("~" verbatim)
3014 ("+" strike))))))
3017 ;;;; Entity
3019 (defun org-e-odt-entity (entity contents info)
3020 "Transcode an ENTITY object from Org to HTML.
3021 CONTENTS are the definition itself. INFO is a plist holding
3022 contextual information."
3023 ;; (let ((ent (org-element-property :latex entity)))
3024 ;; (if (org-element-property :latex-math-p entity)
3025 ;; (format "$%s$" ent)
3026 ;; ent))
3027 (org-element-property :utf-8 entity))
3030 ;;;; Example Block
3032 (defun org-e-odt-example-block (example-block contents info)
3033 "Transcode a EXAMPLE-BLOCK element from Org to HTML.
3034 CONTENTS is nil. INFO is a plist holding contextual information."
3035 (let* ((options (or (org-element-property :options example-block) ""))
3036 (value (org-export-handle-code example-block info nil nil t)))
3037 (org-e-odt--wrap-label
3038 example-block (org-e-odt-format-source-code-or-example value nil))))
3041 ;;;; Export Snippet
3043 (defun org-e-odt-export-snippet (export-snippet contents info)
3044 "Transcode a EXPORT-SNIPPET object from Org to HTML.
3045 CONTENTS is nil. INFO is a plist holding contextual information."
3046 (when (eq (org-export-snippet-backend export-snippet) 'e-odt)
3047 (org-element-property :value export-snippet)))
3050 ;;;; Export Block
3052 (defun org-e-odt-export-block (export-block contents info)
3053 "Transcode a EXPORT-BLOCK element from Org to HTML.
3054 CONTENTS is nil. INFO is a plist holding contextual information."
3055 (when (string= (org-element-property :type export-block) "latex")
3056 (org-remove-indentation (org-element-property :value export-block))))
3059 ;;;; Fixed Width
3061 (defun org-e-odt-fixed-width (fixed-width contents info)
3062 "Transcode a FIXED-WIDTH element from Org to HTML.
3063 CONTENTS is nil. INFO is a plist holding contextual information."
3064 (let* ((value (org-element-normalize-string
3065 (replace-regexp-in-string
3066 "^[ \t]*: ?" ""
3067 (org-element-property :value fixed-width)))))
3068 (org-e-odt--wrap-label
3069 fixed-width (org-e-odt-format-source-code-or-example value nil))))
3072 ;;;; Footnote Definition
3074 ;; Footnote Definitions are ignored.
3077 ;;;; Footnote Reference
3079 (defun org-e-odt-footnote-def (raw info) ; FIXME
3080 (if (equal (org-element-type raw) 'org-data)
3081 (org-trim (org-export-data raw 'e-odt info)) ; fix paragraph
3082 ; style
3083 (org-e-odt-format-stylized-paragraph
3084 'footnote (org-trim (org-export-secondary-string raw 'e-odt info)))))
3086 (defvar org-e-odt-footnote-separator
3087 (org-e-odt-format-fontify "," 'superscript))
3089 (defun org-e-odt-footnote-reference (footnote-reference contents info)
3090 "Transcode a FOOTNOTE-REFERENCE element from Org to HTML.
3091 CONTENTS is nil. INFO is a plist holding contextual information."
3092 (concat
3093 ;; Insert separator between two footnotes in a row.
3094 (let ((prev (org-export-get-previous-element footnote-reference info)))
3095 (when (eq (org-element-type prev) 'footnote-reference)
3096 org-e-odt-footnote-separator))
3097 (cond
3098 ((not (org-export-footnote-first-reference-p footnote-reference info))
3099 (let* ((n (org-export-get-footnote-number footnote-reference info)))
3100 (org-e-odt-format-footnote-reference n "IGNORED" 100)))
3101 ;; Inline definitions are secondary strings.
3102 ((eq (org-element-property :type footnote-reference) 'inline)
3103 (let* ((raw (org-export-get-footnote-definition footnote-reference info))
3104 (n (org-export-get-footnote-number footnote-reference info))
3105 (def (org-e-odt-footnote-def raw info)))
3106 (org-e-odt-format-footnote-reference n def 1)))
3107 ;; Non-inline footnotes definitions are full Org data.
3109 (let* ((raw (org-export-get-footnote-definition footnote-reference info))
3110 (n (org-export-get-footnote-number footnote-reference info))
3111 (def (org-e-odt-footnote-def raw info)))
3112 (org-e-odt-format-footnote-reference n def 1))))))
3115 ;;;; Headline
3117 (defun org-e-odt-todo (todo)
3118 (when todo
3119 (org-e-odt-format-fontify
3120 (concat
3121 "" ; org-e-odt-todo-kwd-class-prefix
3122 (org-e-odt-fix-class-name todo))
3123 (list (if (member todo org-done-keywords) "done" "todo")
3124 todo))))
3126 (defun org-e-odt-format-headline--wrap (headline info
3127 &optional format-function
3128 &rest extra-keys)
3129 "Transcode an HEADLINE element from Org to ODT.
3130 CONTENTS holds the contents of the headline. INFO is a plist
3131 holding contextual information."
3132 (let* ((level (+ (org-export-get-relative-level headline info)))
3133 (headline-number (org-export-get-headline-number headline info))
3134 (section-number (and (org-export-numbered-headline-p headline info)
3135 (mapconcat 'number-to-string
3136 headline-number ".")))
3137 (todo (and (plist-get info :with-todo-keywords)
3138 (let ((todo (org-element-property
3139 :todo-keyword headline)))
3140 (and todo
3141 (org-export-secondary-string todo 'e-odt info)))))
3142 (todo-type (and todo (org-element-property :todo-type headline)))
3143 (priority (and (plist-get info :with-priority)
3144 (org-element-property :priority headline)))
3145 (text (org-export-secondary-string
3146 (org-element-property :title headline) 'e-odt info))
3147 (tags (and (plist-get info :with-tags)
3148 (org-element-property :tags headline)))
3149 (headline-label (concat "sec-" (mapconcat 'number-to-string
3150 headline-number "-")))
3151 (format-function (cond
3152 ((functionp format-function) format-function)
3153 ((functionp org-e-odt-format-headline-function)
3154 (function*
3155 (lambda (todo todo-type priority text tags
3156 &allow-other-keys)
3157 (funcall org-e-odt-format-headline-function
3158 todo todo-type priority text tags))))
3159 (t 'org-e-odt-format-headline))))
3160 (apply format-function
3161 todo todo-type priority text tags
3162 :headline-label headline-label :level level
3163 :section-number section-number extra-keys)))
3165 (defun org-e-odt-headline (headline contents info)
3166 "Transcode an HEADLINE element from Org to HTML.
3167 CONTENTS holds the contents of the headline. INFO is a plist
3168 holding contextual information."
3169 (let* ((numberedp (org-export-numbered-headline-p headline info))
3170 ;; Get level relative to current parsed data.
3171 (level (org-export-get-relative-level headline info))
3172 (text (org-export-secondary-string
3173 (org-element-property :title headline) 'e-odt info))
3174 ;; Create the headline text.
3175 (full-text (org-e-odt-format-headline--wrap headline info)))
3176 (cond
3177 ;; Case 1: This is a footnote section: ignore it.
3178 ((org-element-property :footnote-section-p headline) nil)
3179 ;; Case 2. This is a deep sub-tree: export it as a list item.
3180 ;; Also export as items headlines for which no section
3181 ;; format has been found.
3182 ((org-export-low-level-p headline info) ; FIXME (or (not section-fmt))
3183 ;; Build the real contents of the sub-tree.
3184 (let* ((type (if numberedp 'unordered 'unordered)) ; FIXME
3185 (itemized-body (org-e-odt-format-list-item
3186 contents type nil nil full-text)))
3187 (concat
3188 (and (org-export-first-sibling-p headline info)
3189 (org-e-odt-begin-plain-list type))
3190 itemized-body
3191 (and (org-export-last-sibling-p headline info)
3192 (org-e-odt-end-plain-list type)))))
3193 ;; Case 3. Standard headline. Export it as a section.
3195 (let* ((extra-ids (list (org-element-property :custom-id headline)
3196 (org-element-property :id headline)))
3197 (extra-ids nil) ; FIXME
3198 (id (concat "sec-" (mapconcat 'number-to-string
3199 (org-export-get-headline-number
3200 headline info) "-"))))
3201 (concat
3202 (org-e-odt-format-tags
3203 '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
3204 "</text:h>")
3205 (concat (org-e-odt-format-extra-targets extra-ids)
3206 (if (not id) full-text (org-e-odt-format-target full-text id) ))
3207 level level)
3208 contents))))))
3211 ;;;; Horizontal Rule
3213 (defun org-e-odt-horizontal-rule (horizontal-rule contents info)
3214 "Transcode an HORIZONTAL-RULE object from Org to HTML.
3215 CONTENTS is nil. INFO is a plist holding contextual information."
3216 (let ((attr (mapconcat #'identity
3217 (org-element-property :attr_odt horizontal-rule)
3218 " ")))
3219 (org-e-odt--wrap-label horizontal-rule
3220 (org-e-odt-format-horizontal-line))))
3223 ;;;; Inline Babel Call
3225 ;; Inline Babel Calls are ignored.
3228 ;;;; Inline Src Block
3230 (defun org-e-odt-inline-src-block (inline-src-block contents info)
3231 "Transcode an INLINE-SRC-BLOCK element from Org to HTML.
3232 CONTENTS holds the contents of the item. INFO is a plist holding
3233 contextual information."
3234 (let* ((org-lang (org-element-property :language inline-src-block))
3235 (code (org-element-property :value inline-src-block))
3236 (separator (org-e-odt--find-verb-separator code)))
3237 (error "FIXME")))
3240 ;;;; Inlinetask
3242 (defun org-e-odt-format-section (text class &optional id)
3243 (let ((extra (concat (when id (format " id=\"%s\"" id)))))
3244 (concat (format "<div class=\"%s\"%s>\n" class extra) text "</div>\n")))
3246 (defun org-e-odt-inlinetask (inlinetask contents info)
3247 "Transcode an INLINETASK element from Org to ODT.
3248 CONTENTS holds the contents of the block. INFO is a plist
3249 holding contextual information."
3250 (cond
3251 ;; If `org-e-odt-format-inlinetask-function' is provided, call it
3252 ;; with appropriate arguments.
3253 ((functionp org-e-odt-format-inlinetask-function)
3254 (let ((format-function
3255 (function*
3256 (lambda (todo todo-type priority text tags
3257 &key contents &allow-other-keys)
3258 (funcall org-e-odt-format-inlinetask-function
3259 todo todo-type priority text tags contents)))))
3260 (org-e-odt-format-headline--wrap
3261 inlinetask info format-function :contents contents)))
3262 ;; Otherwise, use a default template.
3263 (t (org-e-odt--wrap-label
3264 inlinetask
3265 (org-e-odt-format-stylized-paragraph
3266 nil (org-e-odt-format-textbox
3267 (concat (org-e-odt-format-stylized-paragraph
3268 "OrgInlineTaskHeading" (org-e-odt-format-headline--wrap
3269 inlinetask info))
3270 contents)
3271 nil nil "OrgInlineTaskFrame" " style:rel-width=\"100%\""))))))
3273 ;;;; Item
3275 (defun org-e-odt-format-list-item (contents type checkbox
3276 &optional term-counter-id
3277 headline)
3278 (when checkbox
3279 (setq checkbox
3280 (org-e-odt-format-fontify (case checkbox
3281 (on "[X]")
3282 (off "[&nbsp;]")
3283 (trans "[-]")) 'code)))
3284 (concat
3285 (org-e-odt-begin-list-item type term-counter-id headline)
3286 ;; FIXME checkbox (and checkbox " ")
3287 contents
3288 (org-e-odt-end-list-item type)))
3290 (defun org-e-odt-item (item contents info)
3291 "Transcode an ITEM element from Org to HTML.
3292 CONTENTS holds the contents of the item. INFO is a plist holding
3293 contextual information."
3294 ;; Grab `:level' from plain-list properties, which is always the
3295 ;; first element above current item.
3296 (let* ((plain-list (org-export-get-parent item info))
3297 (type (org-element-property :type plain-list))
3298 (level (org-element-property :level plain-list))
3299 (counter (org-element-property :counter item))
3300 (checkbox (org-element-property :checkbox item))
3301 (tag (let ((tag (org-element-property :tag item)))
3302 (and tag (org-export-secondary-string tag 'e-odt info)))))
3303 (org-e-odt-format-list-item
3304 contents type checkbox (or tag counter))))
3307 ;;;; Keyword
3309 (defun org-e-odt-keyword (keyword contents info)
3310 "Transcode a KEYWORD element from Org to HTML.
3311 CONTENTS is nil. INFO is a plist holding contextual information."
3312 (let ((key (org-element-property :key keyword))
3313 (value (org-element-property :value keyword)))
3314 (cond
3315 ((string= key "LATEX") value)
3316 ((string= key "INDEX") (format "\\index{%s}" value))
3317 ((string= key "TARGET") nil ; FIXME
3318 ;; (format "\\label{%s}" (org-export-solidify-link-text value))
3320 ((string= key "toc")
3321 (let ((value (downcase value)))
3322 (cond
3323 ((string-match "\\<headlines\\>" value)
3324 (let ((depth (or (and (string-match "[0-9]+" value)
3325 (string-to-number (match-string 0 value)))
3326 (plist-get info :with-toc))))
3327 (when (wholenump depth) (org-e-odt-toc depth info))))
3328 ((string= "tables" value) "FIXME")
3329 ((string= "figures" value) "FIXME")
3330 ((string= "listings" value)
3331 (cond
3332 ;; At the moment, src blocks with a caption are wrapped
3333 ;; into a figure environment.
3334 (t "FIXME")))))))))
3337 ;;;; Latex Environment
3339 (defun org-e-odt-format-latex (latex-frag processing-type)
3340 (let* ((prefix (case processing-type
3341 (dvipng "ltxpng/")
3342 (mathml "ltxmathml/")))
3343 (cache-relpath
3344 (concat prefix (file-name-sans-extension
3345 (file-name-nondirectory (buffer-file-name)))))
3346 (cache-dir (file-name-directory (buffer-file-name )))
3347 (display-msg (case processing-type
3348 (dvipng "Creating LaTeX Image...")
3349 (mathml "Creating MathML snippet..."))))
3350 (with-temp-buffer
3351 (insert latex-frag)
3352 (org-format-latex cache-relpath cache-dir nil display-msg
3353 nil nil processing-type)
3354 (buffer-string))))
3356 (defun org-e-odt-latex-environment (latex-environment contents info)
3357 "Transcode a LATEX-ENVIRONMENT element from Org to HTML.
3358 CONTENTS is nil. INFO is a plist holding contextual information."
3359 (org-e-odt--wrap-label
3360 latex-environment
3361 (let* ((latex-frag
3362 (org-remove-indentation
3363 (org-element-property :value latex-environment)))
3364 (processing-type (plist-get info :LaTeX-fragments))
3365 (caption (org-element-property :caption latex-environment))
3366 (short-caption (and (cdr caption) (org-export-secondary-string
3367 (cdr caption) 'e-odt info)))
3368 (caption (and (car caption) (org-export-secondary-string
3369 (car caption) 'e-odt info)))
3370 (label (org-element-property :name latex-environment))
3371 (attr nil) ; FIXME
3372 (label (org-element-property :name latex-environment)))
3373 (cond
3374 ((member processing-type '(t mathjax))
3375 (org-e-odt-format-formula latex-environment info))
3376 ((equal processing-type 'dvipng)
3377 (org-e-odt-format-stylized-paragraph
3378 nil (org-e-odt-link--inline-image latex-environment info)))
3379 (t latex-frag)))))
3382 ;;;; Latex Fragment
3385 ;; (when latex-frag ; FIXME
3386 ;; (setq href (org-propertize href :title "LaTeX Fragment"
3387 ;; :description latex-frag)))
3388 ;; handle verbatim
3389 ;; provide descriptions
3391 (defun org-e-odt-latex-fragment (latex-fragment contents info)
3392 "Transcode a LATEX-FRAGMENT object from Org to HTML.
3393 CONTENTS is nil. INFO is a plist holding contextual information."
3394 (let* ((latex-frag (org-element-property :value latex-fragment))
3395 (processing-type (plist-get info :LaTeX-fragments)))
3396 (cond
3397 ((member processing-type '(t mathjax))
3398 (org-e-odt-format-formula latex-fragment info))
3399 ((equal processing-type 'dvipng)
3400 (org-e-odt-link--inline-image latex-fragment info))
3401 (t latex-frag))))
3404 ;;;; Line Break
3406 (defun org-e-odt-line-break (line-break contents info)
3407 "Transcode a LINE-BREAK object from Org to HTML.
3408 CONTENTS is nil. INFO is a plist holding contextual information."
3409 "<text:line-break/>\n")
3412 ;;;; Link
3414 (defun org-e-odt-link--inline-image (element info)
3415 "Return HTML code for an inline image.
3416 LINK is the link pointing to the inline image. INFO is a plist
3417 used as a communication channel."
3418 (let* ((src (cond
3419 ((eq (org-element-type element) 'link)
3420 (let* ((type (org-element-property :type element))
3421 (raw-path (org-element-property :path element)))
3422 (cond ((member type '("http" "https"))
3423 (concat type ":" raw-path))
3424 ((file-name-absolute-p raw-path)
3425 (expand-file-name raw-path))
3426 (t raw-path))))
3427 ((member (org-element-type element)
3428 '(latex-fragment latex-environment))
3429 (let* ((latex-frag (org-remove-indentation
3430 (org-element-property
3431 :value element)))
3432 (formula-link (org-e-odt-format-latex
3433 latex-frag 'dvipng)))
3434 (and formula-link
3435 (string-match "file:\\([^]]*\\)" formula-link)
3436 (match-string 1 formula-link))))
3437 (t (error "what is this?"))))
3438 (href (org-e-odt-format-tags
3439 "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
3440 (org-e-odt-copy-image-file src)))
3441 ;; extract attributes from #+ATTR_ODT line.
3442 (attr-from (case (org-element-type element)
3443 (link (org-export-get-parent-paragraph element info))
3444 (t element)))
3445 ;; convert attributes to a plist.
3446 (attr-plist (org-e-odt-element-attributes attr-from info))
3447 ;; handle `:anchor', `:style' and `:attributes' properties.
3448 (user-frame-anchor
3449 (car (assoc-string (plist-get attr-plist :anchor)
3450 '(("as-char") ("paragraph") ("page")) t)))
3451 (user-frame-style
3452 (and user-frame-anchor (plist-get attr-plist :style)))
3453 (user-frame-attrs
3454 (and user-frame-anchor (plist-get attr-plist :attributes)))
3455 (user-frame-params
3456 (list user-frame-style user-frame-attrs user-frame-anchor))
3457 ;; (embed-as (or embed-as user-frame-anchor "paragraph"))
3458 ;; extrac
3459 ;; handle `:width', `:height' and `:scale' properties.
3460 (size (org-e-odt-image-size-from-file
3461 src (plist-get attr-plist :width)
3462 (plist-get attr-plist :height)
3463 (plist-get attr-plist :scale) nil ;; embed-as
3464 "paragraph" ; FIXME
3466 (width (car size)) (height (cdr size))
3467 (embed-as
3468 (case (org-element-type element)
3469 ((org-e-odt-standalone-image-p element info) "paragraph")
3470 (latex-fragment "as-char")
3471 (latex-environment "paragraph")
3472 (t "paragraph")))
3473 (captions (org-e-odt-format-label element info 'definition))
3474 (caption (car captions)) (short-caption (cdr captions))
3475 (entity (concat (and caption "Captioned") embed-as "Image")))
3476 (org-e-odt-format-entity entity href width height
3477 captions user-frame-params )))
3479 (defun org-e-odt-format-entity (entity href width height &optional
3480 captions user-frame-params)
3481 (let* ((caption (car captions)) (short-caption (cdr captions))
3482 (entity-style (assoc-string entity org-e-odt-entity-frame-styles t))
3483 default-frame-params frame-params)
3484 (cond
3485 ((not caption)
3486 (setq default-frame-params (nth 2 entity-style))
3487 (setq frame-params (org-e-odt-merge-frame-params
3488 default-frame-params user-frame-params))
3489 (apply 'org-e-odt-format-frame href width height frame-params))
3491 (setq default-frame-params (nth 3 entity-style))
3492 (setq frame-params (org-e-odt-merge-frame-params
3493 default-frame-params user-frame-params))
3494 (apply 'org-e-odt-format-textbox
3495 (org-e-odt-format-stylized-paragraph
3496 'illustration
3497 (concat
3498 (apply 'org-e-odt-format-frame href width height
3499 (let ((entity-style-1 (copy-sequence
3500 (nth 2 entity-style))))
3501 (setcar (cdr entity-style-1)
3502 (concat
3503 (cadr entity-style-1)
3504 (and short-caption
3505 (format " draw:name=\"%s\" "
3506 short-caption))))
3507 entity-style-1))
3508 caption))
3509 width height frame-params)))))
3511 (defvar org-e-odt-standalone-image-predicate
3512 (function (lambda (paragraph)
3513 (or (org-element-property :caption paragraph)
3514 (org-element-property :name paragraph)))))
3516 (defun org-e-odt-standalone-image-p (element info &optional predicate)
3517 "Test if ELEMENT is a standalone image for the purpose ODT export.
3518 INFO is a plist holding contextual information.
3520 Return non-nil, if ELEMENT is of type paragraph and it's sole
3521 content, save for whitespaces, is a link that qualifies as an
3522 inline image.
3524 Return non-nil, if ELEMENT is of type link and it's containing
3525 paragraph has no other content save for leading and trailing
3526 whitespaces.
3528 Return nil, otherwise.
3530 Bind `org-e-odt-standalone-image-predicate' to constrain
3531 paragraph further. For example, to check for only captioned
3532 standalone images, do the following.
3534 \(setq org-e-odt-standalone-image-predicate
3535 \(lambda \(paragraph\)
3536 \(org-element-property :caption paragraph\)\)\)
3538 (let ((paragraph (case (org-element-type element)
3539 (paragraph element)
3540 (link (and (org-export-inline-image-p
3541 element org-e-odt-inline-image-rules)
3542 (org-export-get-parent element info)))
3543 (t nil))))
3544 (when paragraph
3545 (assert (eq (org-element-type paragraph) 'paragraph))
3546 (when (or (not (and (boundp 'org-e-odt-standalone-image-predicate)
3547 (functionp org-e-odt-standalone-image-predicate)))
3548 (funcall org-e-odt-standalone-image-predicate paragraph))
3549 (let ((contents (org-element-contents paragraph)))
3550 (loop for x in contents
3551 with inline-image-count = 0
3552 always (cond
3553 ((eq (org-element-type x) 'plain-text)
3554 (not (org-string-nw-p x)))
3555 ((eq (org-element-type x) 'link)
3556 (when (org-export-inline-image-p
3557 x org-e-odt-inline-image-rules)
3558 (= (incf inline-image-count) 1)))
3559 (t nil))))))))
3561 (defun org-e-odt-link (link desc info)
3562 "Transcode a LINK object from Org to HTML.
3564 DESC is the description part of the link, or the empty string.
3565 INFO is a plist holding contextual information. See
3566 `org-export-data'."
3567 (let* ((type (org-element-property :type link))
3568 (raw-path (org-element-property :path link))
3569 ;; Ensure DESC really exists, or set it to nil.
3570 (desc (and (not (string= desc "")) desc))
3571 (imagep (org-export-inline-image-p
3572 link org-e-odt-inline-image-rules))
3573 (path (cond
3574 ((member type '("http" "https" "ftp" "mailto"))
3575 (concat type ":" raw-path))
3576 ((string= type "file")
3577 (when (string-match "\\(.+\\)::.+" raw-path)
3578 (setq raw-path (match-string 1 raw-path)))
3579 (if (file-name-absolute-p raw-path)
3580 (concat "file://" (expand-file-name raw-path))
3581 ;; TODO: Not implemented yet. Concat also:
3582 ;; (org-export-directory :HTML info)
3583 (concat "file://" raw-path)))
3584 (t raw-path)))
3585 protocol)
3586 (cond
3587 ;; Image file.
3588 ((and (not desc) (org-export-inline-image-p
3589 link org-e-odt-inline-image-rules))
3590 (org-e-odt-link--inline-image link info))
3591 ;; Radioed target: Target's name is obtained from original raw
3592 ;; link. Path is parsed and transcoded in order to have a proper
3593 ;; display of the contents.
3594 ((string= type "radio")
3595 (org-e-odt-format-internal-link
3596 (org-export-secondary-string
3597 (org-element-parse-secondary-string
3598 path (cdr (assq 'radio-target org-element-object-restrictions)))
3599 'e-odt info)
3600 (org-export-solidify-link-text path)))
3601 ;; Links pointing to an headline: Find destination and build
3602 ;; appropriate referencing command.
3603 ((member type '("custom-id" "fuzzy" "id"))
3604 (let ((destination (if (string= type "fuzzy")
3605 (org-export-resolve-fuzzy-link link info)
3606 (org-export-resolve-id-link link info))))
3607 (case (org-element-type destination)
3608 ;; Fuzzy link points nowhere.
3609 ('nil
3610 (org-e-odt-format-fontify
3611 (or desc (org-export-secondary-string
3612 (org-element-property :raw-link link)
3613 'e-odt info)) 'emphasis))
3614 ;; Fuzzy link points to an invisible target.
3615 (keyword nil)
3616 ;; LINK points to an headline. If headlines are numbered
3617 ;; and the link has no description, display headline's
3618 ;; number. Otherwise, display description or headline's
3619 ;; title.
3620 (headline
3621 (let* ((headline-no (org-export-get-headline-number destination info))
3622 (label (format "sec-%s" (mapconcat 'number-to-string
3623 headline-no "-")))
3624 (section-no (mapconcat 'number-to-string headline-no ".")))
3625 (setq desc
3626 (cond
3627 (desc desc)
3628 ((plist-get info :section-numbers) section-no)
3629 (t (org-export-secondary-string
3630 (org-element-property :title destination)
3631 'e-odt info))))
3632 (org-e-odt-format-internal-link desc label)))
3633 ;; Fuzzy link points to a target. Do as above.
3634 (otherwise
3635 ;; (unless desc
3636 ;; (setq number (cond
3637 ;; ((org-e-odt-standalone-image-p destination info)
3638 ;; (org-export-get-ordinal
3639 ;; (assoc 'link (org-element-contents destination))
3640 ;; info 'link 'org-e-odt-standalone-image-p))
3641 ;; (t (org-export-get-ordinal destination info))))
3642 ;; (setq desc (when number
3643 ;; (if (atom number) (number-to-string number)
3644 ;; (mapconcat 'number-to-string number ".")))))
3646 (let ((label-reference
3647 (org-e-odt-format-label destination info 'reference)))
3648 (assert label-reference)
3649 label-reference)))))
3650 ;; Coderef: replace link with the reference name or the
3651 ;; equivalent line number.
3652 ((string= type "coderef")
3653 (let* ((fmt (org-export-get-coderef-format path (or desc "%s")))
3654 (res (org-export-resolve-coderef path info))
3655 (org-e-odt-suppress-xref nil)
3656 (href (org-xml-format-href (concat "#coderef-" path))))
3657 (format fmt (org-e-odt-format-link res href))))
3658 ;; Link type is handled by a special function.
3659 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
3660 (funcall protocol (org-link-unescape path) desc 'html))
3661 ;; External link with a description part.
3662 ((and path desc) (org-e-odt-format-link desc path))
3663 ;; External link without a description part.
3664 (path (org-e-odt-format-link path path))
3665 ;; No path, only description. Try to do something useful.
3666 (t (org-e-odt-format-fontify desc 'emphasis)))))
3669 ;;;; Babel Call
3671 ;; Babel Calls are ignored.
3674 ;;;; Macro
3676 (defun org-e-odt-macro (macro contents info)
3677 "Transcode a MACRO element from Org to HTML.
3678 CONTENTS is nil. INFO is a plist holding contextual information."
3679 ;; Use available tools.
3680 (org-export-expand-macro macro info))
3683 ;;;; Paragraph
3685 (defun org-e-odt-paragraph (paragraph contents info)
3686 "Transcode a PARAGRAPH element from Org to HTML.
3687 CONTENTS is the contents of the paragraph, as a string. INFO is
3688 the plist used as a communication channel."
3689 (let* ((style nil) ; FIXME
3690 (class (cdr (assoc style '((footnote . "footnote")
3691 (verse . nil)))))
3692 (extra (if class (format " class=\"%s\"" class) ""))
3693 (parent (org-export-get-parent paragraph info))
3694 (parent-type (org-element-type parent))
3695 (style (case parent-type
3696 (quote-block 'quote)
3697 (center-block 'center)
3698 (footnote-definition 'footnote)
3699 (t nil))))
3700 (org-e-odt-format-stylized-paragraph style contents)))
3703 ;;;; Plain List
3705 (defun org-e-odt-plain-list (plain-list contents info)
3706 "Transcode a PLAIN-LIST element from Org to HTML.
3707 CONTENTS is the contents of the list. INFO is a plist holding
3708 contextual information."
3709 (let* (arg1 ;; FIXME
3710 (type (org-element-property :type plain-list))
3711 (attr (mapconcat #'identity
3712 (org-element-property :attr_odt plain-list)
3713 " ")))
3714 (org-e-odt--wrap-label
3715 plain-list (format "%s\n%s%s"
3716 (org-e-odt-begin-plain-list type)
3717 contents (org-e-odt-end-plain-list type)))))
3719 ;;;; Plain Text
3721 (defun org-e-odt-convert-special-strings (string)
3722 "Convert special characters in STRING to ODT."
3723 (let ((all org-e-odt-special-string-regexps)
3724 e a re rpl start)
3725 (while (setq a (pop all))
3726 (setq re (car a) rpl (cdr a) start 0)
3727 (while (string-match re string start)
3728 (setq string (replace-match rpl t nil string))))
3729 string))
3731 ;; (defun org-e-odt-encode-plain-text (s)
3732 ;; "Convert plain text characters to HTML equivalent.
3733 ;; Possible conversions are set in `org-export-html-protect-char-alist'."
3734 ;; (let ((cl org-e-odt-protect-char-alist) c)
3735 ;; (while (setq c (pop cl))
3736 ;; (let ((start 0))
3737 ;; (while (string-match (car c) s start)
3738 ;; (setq s (replace-match (cdr c) t t s)
3739 ;; start (1+ (match-beginning 0))))))
3740 ;; s))
3742 (defun org-e-odt-plain-text (text info)
3743 "Transcode a TEXT string from Org to HTML.
3744 TEXT is the string to transcode. INFO is a plist holding
3745 contextual information."
3746 (setq text (org-e-odt-encode-plain-text text t))
3747 ;; Protect %, #, &, $, ~, ^, _, { and }.
3748 ;; (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
3749 ;; (setq text
3750 ;; (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
3751 ;; Protect \
3752 ;; (setq text (replace-regexp-in-string
3753 ;; "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
3754 ;; "$\\backslash$" text nil t 1))
3755 ;; HTML into \HTML{} and TeX into \TeX{}.
3756 ;; (let ((case-fold-search nil)
3757 ;; (start 0))
3758 ;; (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
3759 ;; (setq text (replace-match
3760 ;; (format "\\%s{}" (match-string 1 text)) nil t text)
3761 ;; start (match-end 0))))
3762 ;; Handle quotation marks
3763 ;; (setq text (org-e-odt--quotation-marks text info))
3764 ;; Convert special strings.
3765 ;; (when (plist-get info :with-special-strings)
3766 ;; (while (string-match (regexp-quote "...") text)
3767 ;; (setq text (replace-match "\\ldots{}" nil t text))))
3768 (when (plist-get info :with-special-strings)
3769 (setq text (org-e-odt-convert-special-strings text)))
3770 ;; Handle break preservation if required.
3771 (when (plist-get info :preserve-breaks)
3772 (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
3773 text)))
3774 ;; Return value.
3775 text)
3778 ;;;; Property Drawer
3780 (defun org-e-odt-property-drawer (property-drawer contents info)
3781 "Transcode a PROPERTY-DRAWER element from Org to HTML.
3782 CONTENTS is nil. INFO is a plist holding contextual
3783 information."
3784 ;; The property drawer isn't exported but we want separating blank
3785 ;; lines nonetheless.
3789 ;;;; Quote Block
3791 (defun org-e-odt-quote-block (quote-block contents info)
3792 "Transcode a QUOTE-BLOCK element from Org to HTML.
3793 CONTENTS holds the contents of the block. INFO is a plist
3794 holding contextual information."
3795 (org-e-odt--wrap-label quote-block contents))
3798 ;;;; Quote Section
3800 (defun org-e-odt-quote-section (quote-section contents info)
3801 "Transcode a QUOTE-SECTION element from Org to HTML.
3802 CONTENTS is nil. INFO is a plist holding contextual information."
3803 (let ((value (org-remove-indentation
3804 (org-element-property :value quote-section))))
3805 (when value (org-e-odt-format-source-code-or-example value nil))))
3808 ;;;; Section
3810 (defun org-e-odt-section (section contents info) ; FIXME
3811 "Transcode a SECTION element from Org to HTML.
3812 CONTENTS holds the contents of the section. INFO is a plist
3813 holding contextual information."
3814 contents)
3816 ;;;; Radio Target
3818 (defun org-e-odt-radio-target (radio-target text info)
3819 "Transcode a RADIO-TARGET object from Org to HTML.
3820 TEXT is the text of the target. INFO is a plist holding
3821 contextual information."
3822 (org-e-odt-format-anchor
3823 text (org-export-solidify-link-text
3824 (org-element-property :value radio-target))))
3827 ;;;; Special Block
3829 (defun org-e-odt-special-block (special-block contents info)
3830 "Transcode a SPECIAL-BLOCK element from Org to HTML.
3831 CONTENTS holds the contents of the block. INFO is a plist
3832 holding contextual information."
3833 (let ((type (downcase (org-element-property :type special-block))))
3834 (org-e-odt--wrap-label
3835 special-block
3836 (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
3839 ;;;; Src Block
3841 (defun org-e-odt-src-block (src-block contents info)
3842 "Transcode a SRC-BLOCK element from Org to HTML.
3843 CONTENTS holds the contents of the item. INFO is a plist holding
3844 contextual information."
3845 (let* ((lang (org-element-property :language src-block))
3846 (caption (org-element-property :caption src-block))
3847 (short-caption (and (cdr caption) (org-export-secondary-string
3848 (cdr caption) 'e-odt info)))
3849 (caption (and (car caption) (org-export-secondary-string
3850 (car caption) 'e-odt info)))
3851 (label (org-element-property :name src-block)))
3852 ;; FIXME: Handle caption
3853 ;; caption-str (when caption)
3854 ;; (main (org-export-secondary-string (car caption) 'e-odt info))
3855 ;; (secondary (org-export-secondary-string (cdr caption) 'e-odt info))
3856 ;; (caption-str (org-e-odt--caption/label-string caption label info))
3857 (let* ((captions (org-e-odt-format-label src-block info 'definition))
3858 (caption (car captions)) (short-caption (cdr captions)))
3859 (concat
3860 (and caption (org-e-odt-format-stylized-paragraph 'listing caption))
3861 (org-e-odt-format-code src-block info)))))
3864 ;;;; Statistics Cookie
3866 (defun org-e-odt-statistics-cookie (statistics-cookie contents info)
3867 "Transcode a STATISTICS-COOKIE object from Org to HTML.
3868 CONTENTS is nil. INFO is a plist holding contextual information."
3869 (let ((cookie-value (org-element-property :value statistics-cookie)))
3870 (org-e-odt-format-fontify cookie-value 'code)))
3873 ;;;; Subscript
3875 (defun org-e-odt-subscript (subscript contents info)
3876 "Transcode a SUBSCRIPT object from Org to HTML.
3877 CONTENTS is the contents of the object. INFO is a plist holding
3878 contextual information."
3879 ;; (format (if (= (length contents) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents)
3880 (org-e-odt-format-fontify contents 'subscript))
3883 ;;;; Superscript
3885 (defun org-e-odt-superscript (superscript contents info)
3886 "Transcode a SUPERSCRIPT object from Org to HTML.
3887 CONTENTS is the contents of the object. INFO is a plist holding
3888 contextual information."
3889 ;; (format (if (= (length contents) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents)
3890 (org-e-odt-format-fontify contents 'superscript))
3893 ;;;; Table Cell
3895 (defun org-e-odt-table-style-spec (element info)
3896 (let* ((table (org-export-get-parent-table element info))
3897 (table-attributes (org-e-odt-element-attributes table info))
3898 (table-style (plist-get table-attributes :style)))
3899 (assoc table-style org-e-odt-table-styles)))
3901 (defun org-e-odt-get-table-cell-styles (table-cell info)
3902 "Retrieve styles applicable to a table cell.
3903 R and C are (zero-based) row and column numbers of the table
3904 cell. STYLE-SPEC is an entry in `org-e-odt-table-styles'
3905 applicable to the current table. It is `nil' if the table is not
3906 associated with any style attributes.
3908 Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
3910 When STYLE-SPEC is nil, style the table cell the conventional way
3911 - choose cell borders based on row and column groupings and
3912 choose paragraph alignment based on `org-col-cookies' text
3913 property. See also
3914 `org-e-odt-get-paragraph-style-cookie-for-table-cell'.
3916 When STYLE-SPEC is non-nil, ignore the above cookie and return
3917 styles congruent with the ODF-1.2 specification."
3918 (let* ((table-cell-address (org-export-table-cell-address table-cell info))
3919 (r (car table-cell-address)) (c (cdr table-cell-address))
3920 (style-spec (org-e-odt-table-style-spec table-cell info))
3921 (table-dimensions (org-export-table-dimensions
3922 (org-export-get-parent-table table-cell info)
3923 info)))
3924 (when style-spec
3925 ;; LibreOffice - particularly the Writer - honors neither table
3926 ;; templates nor custom table-cell styles. Inorder to retain
3927 ;; inter-operability with LibreOffice, only automatic styles are
3928 ;; used for styling of table-cells. The current implementation is
3929 ;; congruent with ODF-1.2 specification and hence is
3930 ;; future-compatible.
3932 ;; Additional Note: LibreOffice's AutoFormat facility for tables -
3933 ;; which recognizes as many as 16 different cell types - is much
3934 ;; richer. Unfortunately it is NOT amenable to easy configuration
3935 ;; by hand.
3936 (let* ((template-name (nth 1 style-spec))
3937 (cell-style-selectors (nth 2 style-spec))
3938 (cell-type
3939 (cond
3940 ((and (cdr (assoc 'use-first-column-styles cell-style-selectors))
3941 (= c 0)) "FirstColumn")
3942 ((and (cdr (assoc 'use-last-column-styles cell-style-selectors))
3943 (= (1+ c) (cdr table-dimensions)))
3944 "LastColumn")
3945 ((and (cdr (assoc 'use-first-row-styles cell-style-selectors))
3946 (= r 0)) "FirstRow")
3947 ((and (cdr (assoc 'use-last-row-styles cell-style-selectors))
3948 (= (1+ r) (car table-dimensions)))
3949 "LastRow")
3950 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
3951 (= (% r 2) 1)) "EvenRow")
3952 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
3953 (= (% r 2) 0)) "OddRow")
3954 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
3955 (= (% c 2) 1)) "EvenColumn")
3956 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
3957 (= (% c 2) 0)) "OddColumn")
3958 (t ""))))
3959 (concat template-name cell-type)))))
3961 (defun org-e-odt-table-cell (table-cell contents info)
3962 "Transcode a TABLE-CELL element from Org to ODT.
3963 CONTENTS is nil. INFO is a plist used as a communication
3964 channel."
3965 (let* ((value (org-export-secondary-string
3966 (org-element-property :value table-cell) 'e-odt info))
3968 (table-cell-address (org-export-table-cell-address table-cell info))
3969 (r (car table-cell-address))
3970 (c (cdr table-cell-address))
3971 (horiz-span (or (org-export-table-cell-width table-cell info) 0))
3972 (table-row (org-export-get-parent table-cell info))
3973 (custom-style-prefix (org-e-odt-get-table-cell-styles
3974 table-cell info))
3975 (paragraph-style
3977 (and custom-style-prefix
3978 (format "%sTableParagraph" custom-style-prefix))
3979 (concat
3980 (cond
3981 ((and (= 1 (org-export-table-row-group table-row info))
3982 (org-export-table-has-header-p
3983 (org-export-get-parent-table table-row info) info))
3984 "OrgTableHeading")
3985 ((and (zerop c) t ;; (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS)
3987 "OrgTableHeading")
3988 (t "OrgTableContents"))
3989 (capitalize (symbol-name (org-export-table-cell-alignment
3990 table-cell info))))))
3991 (cell-style-name
3993 (and custom-style-prefix (format "%sTableCell"
3994 custom-style-prefix))
3995 (concat
3996 "OrgTblCell"
3997 (when (or (org-export-table-row-starts-rowgroup-p table-row info)
3998 (zerop r)) "T")
3999 (when (org-export-table-row-ends-rowgroup-p table-row info) "B")
4000 (when (and (org-export-table-cell-starts-colgroup-p table-cell info)
4001 (not (zerop c)) ) "L"))))
4002 (cell-attributes
4003 (concat
4004 (format " table:style-name=\"%s\"" cell-style-name)
4005 (and (> horiz-span 0)
4006 (format " table:number-columns-spanned=\"%d\""
4007 (1+ horiz-span))))))
4008 (concat
4009 (org-e-odt-format-tags
4010 '("<table:table-cell%s>" . "</table:table-cell>")
4011 (org-e-odt-format-stylized-paragraph paragraph-style value) cell-attributes)
4012 (let (s)
4013 (dotimes (i horiz-span s)
4014 (setq s (concat s "\n<table:covered-table-cell/>"))))
4015 "\n")))
4018 ;;;; Table Row
4020 (defun org-e-odt-table-row (table-row contents info)
4021 "Transcode a TABLE-ROW element from Org to ODT.
4022 CONTENTS is the contents of the row. INFO is a plist used as a
4023 communication channel."
4024 ;; Rules are ignored since table separators are deduced from
4025 ;; borders of the current row.
4026 (when (eq (org-element-property :type table-row) 'standard)
4027 (let* ((rowgroup-tags
4028 (if (and (= 1 (org-export-table-row-group table-row info))
4029 (org-export-table-has-header-p
4030 (org-export-get-parent-table table-row info) info))
4031 ;; If the row belongs to the first rowgroup and the
4032 ;; table has more than one row groups, then this row
4033 ;; belongs to the header row group.
4034 '("\n<table:table-header-rows>" . "\n</table:table-header-rows>")
4035 ;; Otherwise, it belongs to non-header row group.
4036 '("\n<table:table-rows>" . "\n</table:table-rows>"))))
4037 (concat
4038 ;; Does this row begin a rowgroup?
4039 (when (org-export-table-row-starts-rowgroup-p table-row info)
4040 (car rowgroup-tags))
4041 ;; Actual table row
4042 (org-e-odt-format-tags
4043 '("<table:table-row>" . "</table:table-row>") contents)
4044 ;; Does this row end a rowgroup?
4045 (when (org-export-table-row-ends-rowgroup-p table-row info)
4046 (cdr rowgroup-tags))))))
4049 ;;;; Table
4051 (defun org-e-odt-table (table contents info)
4052 "Transcode a TABLE element from Org to HTML.
4053 CONTENTS is nil. INFO is a plist holding contextual information."
4054 (case (org-element-property :type table)
4055 (table.el nil)
4057 (let* ((captions (org-e-odt-format-label table info 'definition))
4058 (caption (car captions)) (short-caption (cdr captions))
4059 (attributes (org-e-odt-element-attributes table info))
4060 (custom-table-style (nth 1 (org-e-odt-table-style-spec table info)))
4061 (table-column-specs
4062 (function
4063 (lambda (table info)
4064 (let* ((table-style (or custom-table-style "OrgTable"))
4065 (column-style (format "%sColumn" table-style)))
4066 (mapconcat
4067 (lambda (table-column-properties)
4068 (let ((width (1+ (or (plist-get table-column-properties
4069 :width) 0))))
4070 (org-e-odt-make-string
4071 width
4072 (org-e-odt-format-tags
4073 "<table:table-column table:style-name=\"%s\"/>"
4074 "" column-style))))
4075 (org-export-table-column-properties table info) "\n"))))))
4076 (concat
4077 ;; caption.
4078 (when caption (org-e-odt-format-stylized-paragraph 'table caption))
4079 ;; begin table.
4080 (let* ((automatic-name
4081 (org-e-odt-add-automatic-style "Table" attributes)))
4082 (format
4083 "\n<table:table table:name=\"%s\" table:style-name=\"%s\">\n"
4084 (or short-caption (car automatic-name))
4085 (or custom-table-style (cdr automatic-name) "OrgTable")))
4086 ;; column specification.
4087 (funcall table-column-specs table info)
4088 ;; actual contents.
4089 "\n" contents
4090 ;; end table.
4091 "</table:table>")))))
4094 ;;;; Target
4096 (defun org-e-odt-target (target contents info)
4097 "Transcode a TARGET object from Org to HTML.
4098 CONTENTS is nil. INFO is a plist holding contextual
4099 information."
4100 (org-e-odt-format-anchor
4101 "" (org-export-solidify-link-text (org-element-property :value target))))
4104 ;;;; Time-stamp
4106 (defun org-e-odt-time-stamp (time-stamp contents info)
4107 "Transcode a TIME-STAMP object from Org to HTML.
4108 CONTENTS is nil. INFO is a plist holding contextual
4109 information."
4110 ;; (let ((value (org-element-property :value time-stamp))
4111 ;; (type (org-element-property :type time-stamp))
4112 ;; (appt-type (org-element-property :appt-type time-stamp)))
4113 ;; (concat (cond ((eq appt-type 'scheduled)
4114 ;; (format "\\textbf{\\textsc{%s}} " org-scheduled-string))
4115 ;; ((eq appt-type 'deadline)
4116 ;; (format "\\textbf{\\textsc{%s}} " org-deadline-string))
4117 ;; ((eq appt-type 'closed)
4118 ;; (format "\\textbf{\\textsc{%s}} " org-closed-string)))
4119 ;; (cond ((memq type '(active active-range))
4120 ;; (format org-e-odt-active-timestamp-format value))
4121 ;; ((memq type '(inactive inactive-range))
4122 ;; (format org-e-odt-inactive-timestamp-format value))
4123 ;; (t
4124 ;; (format org-e-odt-diary-timestamp-format value)))))
4125 (let ((value (org-element-property :value time-stamp))
4126 (type (org-element-property :type time-stamp))
4127 (appt-type (org-element-property :appt-type time-stamp)))
4128 (setq value (org-export-secondary-string value 'e-odt info))
4129 (org-e-odt-format-fontify
4130 (concat
4131 (org-e-odt-format-fontify
4132 (cond ((eq appt-type 'scheduled) org-scheduled-string)
4133 ((eq appt-type 'deadline) org-deadline-string)
4134 ((eq appt-type 'closed) org-closed-string)) "timestamp-kwd")
4135 ;; FIXME: (org-translate-time value)
4136 (org-e-odt-format-fontify value "timestamp"))
4137 "timestamp-wrapper")))
4140 ;;;; Verbatim
4142 (defun org-e-odt-verbatim (verbatim contents info)
4143 "Transcode a VERBATIM object from Org to HTML.
4144 CONTENTS is nil. INFO is a plist used as a communication
4145 channel."
4146 (org-e-odt-emphasis
4147 verbatim (org-element-property :value verbatim) info))
4150 ;;;; Verse Block
4152 (defun org-e-odt-verse-block (verse-block contents info)
4153 "Transcode a VERSE-BLOCK element from Org to HTML.
4154 CONTENTS is nil. INFO is a plist holding contextual information."
4155 ;; Replace each newline character with line break. Also replace
4156 ;; each blank line with a line break.
4157 (setq contents (replace-regexp-in-string
4158 "^ *\\\\\\\\$" "<br/>\n"
4159 (replace-regexp-in-string
4160 "\\(\\\\\\\\\\)?[ \t]*\n" " <br/>\n"
4161 (org-remove-indentation
4162 (org-export-secondary-string
4163 (org-element-property :value verse-block)
4164 'e-odt info)))))
4166 ;; Replace each white space at beginning of a line with a
4167 ;; non-breaking space.
4168 (while (string-match "^[ \t]+" contents)
4169 (let ((new-str (org-e-odt-format-spaces
4170 (length (match-string 0 contents)))))
4171 (setq contents (replace-match new-str nil t contents))))
4173 (org-e-odt--wrap-label
4174 verse-block (format "<p class=\"verse\">\n%s</p>" contents)))
4179 ;;; Filter Functions
4181 ;;;; Filter Settings
4182 ;;;; Filters
4184 ;;; Interactive functions
4186 (defun org-e-odt-export-to-odt
4187 (&optional subtreep visible-only body-only ext-plist pub-dir)
4188 "Export current buffer to a HTML file.
4190 If narrowing is active in the current buffer, only export its
4191 narrowed part.
4193 If a region is active, export that region.
4195 When optional argument SUBTREEP is non-nil, export the sub-tree
4196 at point, extracting information from the headline properties
4197 first.
4199 When optional argument VISIBLE-ONLY is non-nil, don't export
4200 contents of hidden elements.
4202 When optional argument BODY-ONLY is non-nil, only write code
4203 between \"\\begin{document}\" and \"\\end{document}\".
4205 EXT-PLIST, when provided, is a property list with external
4206 parameters overriding Org default settings, but still inferior to
4207 file-local settings.
4209 When optional argument PUB-DIR is set, use it as the publishing
4210 directory.
4212 Return output file's name."
4213 (interactive)
4214 (setq debug-on-error t)
4216 ;; (let* ((outfile (org-export-output-file-name ".html" subtreep pub-dir))
4217 ;; (outfile "content.xml"))
4218 ;; (org-export-to-file
4219 ;; 'e-odt outfile subtreep visible-only body-only ext-plist))
4221 (let* ((outbuf (org-e-odt-init-outfile))
4222 (target (org-export-output-file-name ".odt" subtreep pub-dir))
4223 (outdir (file-name-directory (buffer-file-name outbuf)))
4224 (default-directory outdir))
4226 ;; FIXME: for copying embedded images
4227 (setq org-current-export-file
4228 (file-name-directory
4229 (org-export-output-file-name ".odt" subtreep nil)))
4231 (org-export-to-buffer
4232 'e-odt outbuf
4233 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))
4235 (setq org-lparse-opt-plist nil) ; FIXME
4236 (org-e-odt-save-as-outfile target ;; info
4240 ;; return outfile
4241 (if (not org-e-odt-preferred-output-format) target
4242 (or (org-e-odt-convert target org-e-odt-preferred-output-format)
4243 target))))
4249 (defun org-e-odt-reachable-p (in-fmt out-fmt)
4250 "Return non-nil if IN-FMT can be converted to OUT-FMT."
4251 (catch 'done
4252 (let ((reachable-formats (org-e-odt-do-reachable-formats in-fmt)))
4253 (dolist (e reachable-formats)
4254 (let ((out-fmt-spec (assoc out-fmt (cdr e))))
4255 (when out-fmt-spec
4256 (throw 'done (cons (car e) out-fmt-spec))))))))
4258 (defun org-e-odt-do-convert (in-file out-fmt &optional prefix-arg)
4259 "Workhorse routine for `org-e-odt-convert'."
4260 (require 'browse-url)
4261 (let* ((in-file (expand-file-name (or in-file buffer-file-name)))
4262 (dummy (or (file-readable-p in-file)
4263 (error "Cannot read %s" in-file)))
4264 (in-fmt (file-name-extension in-file))
4265 (out-fmt (or out-fmt (error "Output format unspecified")))
4266 (how (or (org-e-odt-reachable-p in-fmt out-fmt)
4267 (error "Cannot convert from %s format to %s format?"
4268 in-fmt out-fmt)))
4269 (convert-process (car how))
4270 (out-file (concat (file-name-sans-extension in-file) "."
4271 (nth 1 (or (cdr how) out-fmt))))
4272 (extra-options (or (nth 2 (cdr how)) ""))
4273 (out-dir (file-name-directory in-file))
4274 (cmd (format-spec convert-process
4275 `((?i . ,(shell-quote-argument in-file))
4276 (?I . ,(browse-url-file-url in-file))
4277 (?f . ,out-fmt)
4278 (?o . ,out-file)
4279 (?O . ,(browse-url-file-url out-file))
4280 (?d . , (shell-quote-argument out-dir))
4281 (?D . ,(browse-url-file-url out-dir))
4282 (?x . ,extra-options)))))
4283 (when (file-exists-p out-file)
4284 (delete-file out-file))
4286 (message "Executing %s" cmd)
4287 (let ((cmd-output (shell-command-to-string cmd)))
4288 (message "%s" cmd-output))
4290 (cond
4291 ((file-exists-p out-file)
4292 (message "Exported to %s" out-file)
4293 (when prefix-arg
4294 (message "Opening %s..." out-file)
4295 (org-open-file out-file))
4296 out-file)
4298 (message "Export to %s failed" out-file)
4299 nil))))
4301 (defun org-e-odt-do-reachable-formats (in-fmt)
4302 "Return verbose info about formats to which IN-FMT can be converted.
4303 Return a list where each element is of the
4304 form (CONVERTER-PROCESS . OUTPUT-FMT-ALIST). See
4305 `org-e-odt-convert-processes' for CONVERTER-PROCESS and see
4306 `org-e-odt-convert-capabilities' for OUTPUT-FMT-ALIST."
4307 (let* ((converter
4308 (and org-e-odt-convert-process
4309 (cadr (assoc-string org-e-odt-convert-process
4310 org-e-odt-convert-processes t))))
4311 (capabilities
4312 (and org-e-odt-convert-process
4313 (cadr (assoc-string org-e-odt-convert-process
4314 org-e-odt-convert-processes t))
4315 org-e-odt-convert-capabilities))
4316 reachable-formats)
4317 (when converter
4318 (dolist (c capabilities)
4319 (when (member in-fmt (nth 1 c))
4320 (push (cons converter (nth 2 c)) reachable-formats))))
4321 reachable-formats))
4323 (defun org-e-odt-reachable-formats (in-fmt)
4324 "Return list of formats to which IN-FMT can be converted.
4325 The list of the form (OUTPUT-FMT-1 OUTPUT-FMT-2 ...)."
4326 (let (l)
4327 (mapc (lambda (e) (add-to-list 'l e))
4328 (apply 'append (mapcar
4329 (lambda (e) (mapcar 'car (cdr e)))
4330 (org-e-odt-do-reachable-formats in-fmt))))
4333 (defun org-e-odt-convert-read-params ()
4334 "Return IN-FILE and OUT-FMT params for `org-e-odt-do-convert'.
4335 This is a helper routine for interactive use."
4336 (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
4337 (in-file (read-file-name "File to be converted: "
4338 nil buffer-file-name t))
4339 (in-fmt (file-name-extension in-file))
4340 (out-fmt-choices (org-e-odt-reachable-formats in-fmt))
4341 (out-fmt
4342 (or (and out-fmt-choices
4343 (funcall input "Output format: "
4344 out-fmt-choices nil nil nil))
4345 (error
4346 "No known converter or no known output formats for %s files"
4347 in-fmt))))
4348 (list in-file out-fmt)))
4350 ;;;###autoload
4351 (defun org-e-odt-convert (&optional in-file out-fmt prefix-arg)
4352 "Convert IN-FILE to format OUT-FMT using a command line converter.
4353 IN-FILE is the file to be converted. If unspecified, it defaults
4354 to variable `buffer-file-name'. OUT-FMT is the desired output
4355 format. Use `org-e-odt-convert-process' as the converter.
4356 If PREFIX-ARG is non-nil then the newly converted file is opened
4357 using `org-open-file'."
4358 (interactive
4359 (append (org-e-odt-convert-read-params) current-prefix-arg))
4360 (org-e-odt-do-convert in-file out-fmt prefix-arg))
4362 ;;; FIXMES, TODOS, FOR REVIEW etc
4364 ;; (defun org-e-odt-discontinue-list ()
4365 ;; (let ((stashed-stack org-lparse-list-stack))
4366 ;; (loop for list-type in stashed-stack
4367 ;; do (org-lparse-end-list-item-1 list-type)
4368 ;; (org-lparse-end-list list-type))
4369 ;; (setq org-e-odt-list-stack-stashed stashed-stack)))
4371 ;; (defun org-e-odt-continue-list ()
4372 ;; (setq org-e-odt-list-stack-stashed (nreverse org-e-odt-list-stack-stashed))
4373 ;; (loop for list-type in org-e-odt-list-stack-stashed
4374 ;; do (org-lparse-begin-list list-type)
4375 ;; (org-lparse-begin-list-item list-type)))
4377 ;; FIXME: Begin indented table
4378 ;; (setq org-e-odt-table-indentedp (not (null org-lparse-list-stack)))
4379 ;; (setq org-e-odt-table-indentedp nil) ; FIXME
4380 ;; (when org-e-odt-table-indentedp
4381 ;; ;; Within the Org file, the table is appearing within a list item.
4382 ;; ;; OpenDocument doesn't allow table to appear within list items.
4383 ;; ;; Temporarily terminate the list, emit the table and then
4384 ;; ;; re-continue the list.
4385 ;; (org-e-odt-discontinue-list)
4386 ;; ;; Put the Table in an indented section.
4387 ;; (let ((level (length org-e-odt-list-stack-stashed)))
4388 ;; (org-e-odt-begin-section (format "OrgIndentedSection-Level-%d" level))))
4390 ;; FIXME: End indented table
4391 ;; (when org-e-odt-table-indentedp
4392 ;; (org-e-odt-end-section)
4393 ;; (org-e-odt-continue-list))
4396 ;;;; org-format-table-html
4397 ;;;; org-format-org-table-html
4398 ;;;; org-format-table-table-html
4399 ;;;; org-table-number-fraction
4400 ;;;; org-table-number-regexp
4401 ;;;; org-e-odt-table-caption-above
4403 ;;;; org-whitespace
4404 ;;;; "<span style=\"visibility:hidden;\">%s</span>"
4405 ;;;; Remove display properties
4407 ;;;; org-e-odt-with-timestamp
4408 ;;;; org-e-odt-html-helper-timestamp
4410 ;;;; org-export-as-html-and-open
4411 ;;;; org-export-as-html-batch
4412 ;;;; org-export-as-html-to-buffer
4413 ;;;; org-replace-region-by-html
4414 ;;;; org-export-region-as-html
4415 ;;;; org-export-as-html
4417 ;;;; (org-export-directory :html opt-plist)
4418 ;;;; (plist-get opt-plist :html-extension)
4419 ;;;; org-e-odt-toplevel-hlevel
4420 ;;;; org-e-odt-coding-system
4421 ;;;; org-e-odt-coding-system
4422 ;;;; org-e-odt-inline-image-extensions
4423 ;;;; org-e-odt-protect-char-alist
4424 ;;;; org-e-odt-table-use-header-tags-for-first-column
4425 ;;;; org-e-odt-todo-kwd-class-prefix
4426 ;;;; org-e-odt-tag-class-prefix
4427 ;;;; org-e-odt-footnote-separator
4430 ;;; Library Initializations
4432 (mapc
4433 (lambda (desc)
4434 ;; Let Org open all OpenDocument files using system-registered app
4435 (add-to-list 'org-file-apps
4436 (cons (concat "\\." (car desc) "\\'") 'system))
4437 ;; Let Emacs open all OpenDocument files in archive mode
4438 (add-to-list 'auto-mode-alist
4439 (cons (concat "\\." (car desc) "\\'") 'archive-mode)))
4440 org-e-odt-file-extensions)
4442 (defvar org-e-odt-display-outline-level 2)
4443 (defun org-e-odt-enumerate-element (element info &optional predicate n)
4444 (let* ((numbered-parent-headline-at-<=-n
4445 (function
4446 (lambda (element n info)
4447 (loop for x in (org-export-get-genealogy element info)
4448 thereis (and (eq (org-element-type x) 'headline)
4449 (<= (org-export-get-relative-level x info) n)
4450 (org-export-numbered-headline-p x info)
4451 x)))))
4452 (enumerate
4453 (function
4454 (lambda (element scope info &optional predicate)
4455 (let ((counter 0))
4456 (org-element-map
4457 (or scope (plist-get info :parse-tree))
4458 (org-element-type element)
4459 (lambda (el)
4460 (and (or (not predicate) (funcall predicate el info))
4461 (incf counter)
4462 (equal element el)
4463 counter))
4464 info 'first-match)))))
4465 (scope (funcall numbered-parent-headline-at-<=-n
4466 element (or n org-e-odt-display-outline-level) info))
4467 (ordinal (funcall enumerate element scope info predicate))
4468 (tag
4469 (concat
4470 ;; section number
4471 (and scope
4472 (mapconcat 'number-to-string
4473 (org-export-get-headline-number scope info) "."))
4474 ;; separator
4475 (and scope ".")
4476 ;; ordinal
4477 (number-to-string ordinal))))
4478 ;; (message "%s:\t%s" (org-element-property :name element) tag)
4479 tag))
4481 (provide 'org-e-odt)
4483 ;;; org-e-odt.el ends here