org-element: Rename `time-stamp' object type into `timestamp'
[org-mode/org-mode-NeilSmithlineMods.git] / contrib / lisp / org-e-odt.el
blobf949f9e22a13ea0643f3e83f93474e018a810737
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 (plist-get info :title) info))
51 (author (and (plist-get info :with-author)
52 (let ((auth (plist-get info :author)))
53 (and auth (org-export-secondary-string auth info)))))
54 (date (plist-get info :date))
55 (iso-date (org-e-odt-format-date date))
56 (date (org-e-odt-format-date date "%d %b %Y"))
57 (email (plist-get info :email))
58 ;; switch on or off above vars based on user settings
59 (author (and (plist-get info :with-author) (or author email)))
60 ;; (date (and (plist-get info :time-stamp-file) date))
61 (email (and (plist-get info :with-email) email)))
62 (concat
63 ;; title
64 (when title
65 (concat
66 (org-e-odt-format-stylized-paragraph
67 'title (format "\n<text:title>%s</text:title>" title))
68 ;; separator
69 "\n<text:p text:style-name=\"OrgTitle\"/>"))
70 (cond
71 ((and author (not email))
72 ;; author only
73 (concat
74 (org-e-odt-format-stylized-paragraph
75 'subtitle
76 (format "<text:initial-creator>%s</text:initial-creator>" author))
77 ;; separator
78 "\n<text:p text:style-name=\"OrgSubtitle\"/>"))
79 ((and author email)
80 ;; author and email
81 (concat
82 (org-e-odt-format-stylized-paragraph
83 'subtitle
84 (org-e-odt-format-link
85 (format "<text:initial-creator>%s</text:initial-creator>" author)
86 (concat "mailto:" email)))
87 ;; separator
88 "\n<text:p text:style-name=\"OrgSubtitle\"/>")))
89 ;; date
90 (when date
91 (concat
92 (org-e-odt-format-stylized-paragraph
93 'subtitle
94 (org-e-odt-format-tags
95 '("<text:date style:data-style-name=\"%s\" text:date-value=\"%s\">"
96 . "</text:date>")
97 date "N75" iso-date))
98 ;; separator
99 "<text:p text:style-name=\"OrgSubtitle\"/>")))))
101 (defun org-e-odt-begin-section (style &optional name)
102 (let ((default-name (car (org-e-odt-add-automatic-style "Section"))))
103 (format "<text:section text:style-name=\"%s\" text:name=\"%s\">"
104 style (or name default-name))))
106 (defun org-e-odt-end-section ()
107 "</text:section>")
109 (defun org-e-odt-begin-paragraph (&optional style)
110 (format "<text:p%s>" (org-e-odt-get-extra-attrs-for-paragraph-style style)))
112 (defun org-e-odt-end-paragraph ()
113 "</text:p>")
115 (defun org-e-odt-get-extra-attrs-for-paragraph-style (style)
116 (let (style-name)
117 (setq style-name
118 (cond
119 ((stringp style) style)
120 ((symbolp style) (org-e-odt-get-style-name-for-entity
121 'paragraph style))))
122 (unless style-name
123 (error "Don't know how to handle paragraph style %s" style))
124 (format " text:style-name=\"%s\"" style-name)))
126 (defun org-e-odt-format-stylized-paragraph (style text)
127 (format "\n<text:p%s>%s</text:p>"
128 (org-e-odt-get-extra-attrs-for-paragraph-style style)
129 text))
131 (defun org-e-odt-format-author (&optional author )
132 (when (setq author (or author (plist-get org-lparse-opt-plist :author)))
133 (format "<dc:creator>%s</dc:creator>" author)))
135 (defun org-e-odt-format-date (&optional org-ts fmt)
136 (save-match-data
137 (let* ((time
138 (and (stringp org-ts)
139 (string-match org-ts-regexp0 org-ts)
140 (apply 'encode-time
141 (org-fix-decoded-time
142 (org-parse-time-string (match-string 0 org-ts) t)))))
143 date)
144 (cond
145 (fmt (format-time-string fmt time))
146 (t (setq date (format-time-string "%Y-%m-%dT%H:%M:%S%z" time))
147 (format "%s:%s" (substring date 0 -2) (substring date -2)))))))
149 (defun org-e-odt-begin-annotation (&optional author date)
150 (concat
151 "<office:annotation>\n"
152 (and author (org-e-odt-format-author author))
153 (org-e-odt-format-tags
154 '("<dc:date>" . "</dc:date>")
155 (org-e-odt-format-date
156 (or date (plist-get org-lparse-opt-plist :date))))
157 (org-e-odt-begin-paragraph)))
159 (defun org-e-odt-end-annotation ()
160 "</office:annotation>")
162 (defun org-e-odt-begin-plain-list (ltype)
163 (let* ((style-name (org-e-odt-get-style-name-for-entity 'list ltype))
164 (extra (concat
165 ;; (if (or org-lparse-list-table-p
166 ;; (and (= 1 (length org-lparse-list-stack))
167 ;; (null org-e-odt-list-stack-stashed)))
168 ;; " text:continue-numbering=\"false\""
169 ;; " text:continue-numbering=\"true\"")
171 " text:continue-numbering=\"true\""
173 (when style-name
174 (format " text:style-name=\"%s\"" style-name)))))
175 (case ltype
176 ((ordered unordered descriptive)
177 (concat
178 ;; (org-e-odt-end-paragraph)
179 (format "<text:list%s>" extra)))
180 (t (error "Unknown list type: %s" ltype)))))
182 (defun org-e-odt-end-plain-list (ltype)
183 (if ltype "</text:list>"
184 (error "Unknown list type: %s" ltype)))
186 (defun org-e-odt-begin-list-item (ltype &optional arg headline)
187 (case ltype
188 (ordered
189 (assert (not headline) t)
190 (let* ((counter arg) (extra ""))
191 (concat "<text:list-item>" ;; (org-e-odt-begin-paragraph)
193 ;; (if (= (length org-lparse-list-stack)
194 ;; (length org-e-odt-list-stack-stashed))
195 ;; "<text:list-header>" "<text:list-item>")
197 (unordered
198 (let* ((id arg) (extra ""))
199 (concat
200 "<text:list-item>"
201 ;; (org-e-odt-begin-paragraph)
202 (if headline (org-e-odt-format-target headline id)
203 (org-e-odt-format-bookmark "" id)))
204 ;; (if (= (length org-lparse-list-stack)
205 ;; (length org-e-odt-list-stack-stashed))
206 ;; "<text:list-header>" "<text:list-item>")
208 (descriptive
209 (assert (not headline) t)
210 (let ((term (or arg "(no term)")))
211 (concat
212 (org-e-odt-format-tags
213 '("<text:list-item>" . "</text:list-item>")
214 (org-e-odt-format-stylized-paragraph 'definition-term term))
215 (org-e-odt-begin-list-item 'unordered)
216 (org-e-odt-begin-plain-list 'descriptive)
217 (org-e-odt-begin-list-item 'unordered))))
218 (t (error "Unknown list type"))))
220 (defun org-e-odt-end-list-item (ltype)
221 (case ltype
222 ((ordered unordered)
223 ;; (org-lparse-insert-tag
224 ;; (if (= (length org-lparse-list-stack)
225 ;; (length org-e-odt-list-stack-stashed))
226 ;; (prog1 "</text:list-header>"
227 ;; (setq org-e-odt-list-stack-stashed nil))
228 ;; "</text:list-item>")
229 "</text:list-item>"
230 ;; )
232 (descriptive
233 (concat
234 (org-e-odt-end-list-item 'unordered)
235 (org-e-odt-end-plain-list 'descriptive)
236 (org-e-odt-end-list-item 'unordered)
238 (t (error "Unknown list type"))))
240 (defun org-e-odt-write-automatic-styles ()
241 "Write automatic styles to \"content.xml\"."
242 (with-current-buffer
243 (find-file-noselect (expand-file-name "content.xml") t)
244 ;; position the cursor
245 (goto-char (point-min))
246 (re-search-forward " </office:automatic-styles>" nil t)
247 (goto-char (match-beginning 0))
248 ;; write automatic table styles
249 (loop for (style-name props) in
250 (plist-get org-e-odt-automatic-styles 'Table) do
251 (when (setq props (or (plist-get props :rel-width) 96))
252 (insert (format org-e-odt-table-style-format style-name props))))))
254 (defun org-e-odt-update-display-level (&optional level)
255 (with-current-buffer
256 (find-file-noselect (expand-file-name "content.xml") t)
257 ;; position the cursor.
258 (goto-char (point-min))
259 ;; remove existing sequence decls.
260 (when (re-search-forward "<text:sequence-decls" nil t)
261 (delete-region (match-beginning 0)
262 (re-search-forward "</text:sequence-decls>" nil nil)))
263 ;; insert new ones.
264 (insert "
265 <text:sequence-decls>")
266 (loop for x in org-e-odt-category-map-alist
267 do (insert (format "
268 <text:sequence-decl text:display-outline-level=\"%d\" text:name=\"%s\"/>"
269 level (nth 1 x))))
270 (insert "
271 </text:sequence-decls>")))
273 (defun org-e-odt-add-automatic-style (object-type &optional object-props)
274 "Create an automatic style of type OBJECT-TYPE with param OBJECT-PROPS.
275 OBJECT-PROPS is (typically) a plist created by passing
276 \"#+ATTR_ODT: \" option of the object in question to
277 `org-e-odt-parse-block-attributes'.
279 Use `org-e-odt-object-counters' to generate an automatic
280 OBJECT-NAME and STYLE-NAME. If OBJECT-PROPS is non-nil, add a
281 new entry in `org-e-odt-automatic-styles'. Return (OBJECT-NAME
282 . STYLE-NAME)."
283 (assert (stringp object-type))
284 (let* ((object (intern object-type))
285 (seqvar object)
286 (seqno (1+ (or (plist-get org-e-odt-object-counters seqvar) 0)))
287 (object-name (format "%s%d" object-type seqno)) style-name)
288 (setq org-e-odt-object-counters
289 (plist-put org-e-odt-object-counters seqvar seqno))
290 (when object-props
291 (setq style-name (format "Org%s" object-name))
292 (setq org-e-odt-automatic-styles
293 (plist-put org-e-odt-automatic-styles object
294 (append (list (list style-name object-props))
295 (plist-get org-e-odt-automatic-styles object)))))
296 (cons object-name style-name)))
298 (defun org-e-odt-begin-toc (lang-specific-heading max-level)
299 (concat
300 (format "
301 <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
302 <text:table-of-content-source text:outline-level=\"%d\">
303 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
304 " max-level lang-specific-heading)
306 (let ((entry-templates ""))
307 (loop for level from 1 upto 10
308 do (setq entry-templates
309 (concat entry-templates
310 (format
312 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
313 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
314 <text:index-entry-chapter/>
315 <text:index-entry-text/>
316 <text:index-entry-link-end/>
317 </text:table-of-content-entry-template>
318 " level level))))
319 entry-templates)
321 (format "
322 </text:table-of-content-source>
324 <text:index-body>
325 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
326 <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
327 </text:index-title>
328 " lang-specific-heading)))
330 (defun org-e-odt-end-toc ()
331 (format "
332 </text:index-body>
333 </text:table-of-content>
336 (defun org-e-odt-format-toc-entry (snumber todo headline tags href)
338 ;; FIXME
339 (setq headline (concat
340 (and org-export-with-section-numbers
341 (concat snumber ". "))
342 headline
343 (and tags
344 (concat
345 (org-e-odt-format-spaces 3)
346 (org-e-odt-format-fontify tags "tag")))))
347 (when todo
348 (setq headline (org-e-odt-format-fontify headline "todo")))
350 (let ((org-e-odt-suppress-xref t))
351 (org-e-odt-format-link headline (concat "#" href))))
353 (defun org-e-odt-format-toc-item (toc-entry level org-last-level)
354 (let ((style (format "Contents_20_%d" level)))
355 (concat "\n" (org-e-odt-format-stylized-paragraph style toc-entry) "\n")))
357 ;; Following variable is let bound during 'ORG-LINK callback. See
358 ;; org-html.el
360 (defun org-e-odt-format-link (desc href &optional attr)
361 (cond
362 ((and (= (string-to-char href) ?#) (not org-e-odt-suppress-xref))
363 (setq href (substring href 1))
364 (let ((xref-format "text"))
365 (when (numberp desc)
366 (setq desc (format "%d" desc) xref-format "number"))
367 (when (listp desc)
368 (setq desc (mapconcat 'identity desc ".") xref-format "chapter"))
369 (setq href (concat org-e-odt-bookmark-prefix href))
370 (org-e-odt-format-tags-simple
371 '("<text:bookmark-ref text:reference-format=\"%s\" text:ref-name=\"%s\">" .
372 "</text:bookmark-ref>")
373 desc xref-format href)))
374 (org-lparse-link-description-is-image
375 (org-e-odt-format-tags
376 '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</draw:a>")
377 desc href (or attr "")))
379 (org-e-odt-format-tags-simple
380 '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</text:a>")
381 desc href (or attr "")))))
383 (defun org-e-odt-format-spaces (n)
384 (cond
385 ((= n 1) " ")
386 ((> n 1) (concat
387 " " (org-e-odt-format-tags "<text:s text:c=\"%d\"/>" "" (1- n))))
388 (t "")))
390 (defun org-e-odt-format-tabs (&optional n)
391 (let ((tab "<text:tab/>")
392 (n (or n 1)))
393 (insert tab)))
395 (defun org-e-odt-format-line-break ()
396 (org-e-odt-format-tags "<text:line-break/>" ""))
398 (defun org-e-odt-format-horizontal-line ()
399 (org-e-odt-format-stylized-paragraph 'horizontal-line ""))
401 (defun org-e-odt-encode-plain-text (line &optional no-whitespace-filling)
402 (setq line (org-e-html-encode-plain-text line))
403 (if no-whitespace-filling line
404 (org-e-odt-fill-tabs-and-spaces line)))
406 (defun org-e-odt-format-line (line)
407 (case org-lparse-dyn-current-environment
408 (fixedwidth (concat
409 (org-e-odt-format-stylized-paragraph
410 'fixedwidth (org-e-odt-encode-plain-text line)) "\n"))
411 (t (concat line "\n"))))
413 (defun org-e-odt-format-comment (fmt &rest args)
414 (let ((comment (apply 'format fmt args)))
415 (format "\n<!-- %s -->\n" comment)))
417 (defun org-e-odt-format-org-entity (wd)
418 (org-entity-get-representation wd 'utf8))
420 (defun org-e-odt-fill-tabs-and-spaces (line)
421 (replace-regexp-in-string
422 "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
423 (cond
424 ((string= s "\t") (org-e-odt-format-tabs))
425 (t (org-e-odt-format-spaces (length s))))) line))
427 (defun org-e-odt-hfy-face-to-css (fn)
428 "Create custom style for face FN.
429 When FN is the default face, use it's foreground and background
430 properties to create \"OrgSrcBlock\" paragraph style. Otherwise
431 use it's color attribute to create a character style whose name
432 is obtained from FN. Currently all attributes of FN other than
433 color are ignored.
435 The style name for a face FN is derived using the following
436 operations on the face name in that order - de-dash, CamelCase
437 and prefix with \"OrgSrc\". For example,
438 `font-lock-function-name-face' is associated with
439 \"OrgSrcFontLockFunctionNameFace\"."
440 (let* ((css-list (hfy-face-to-style fn))
441 (style-name ((lambda (fn)
442 (concat "OrgSrc"
443 (mapconcat
444 'capitalize (split-string
445 (hfy-face-or-def-to-name fn) "-")
446 ""))) fn))
447 (color-val (cdr (assoc "color" css-list)))
448 (background-color-val (cdr (assoc "background" css-list)))
449 (style (and org-e-odt-create-custom-styles-for-srcblocks
450 (cond
451 ((eq fn 'default)
452 (format org-src-block-paragraph-format
453 background-color-val color-val))
455 (format
457 <style:style style:name=\"%s\" style:family=\"text\">
458 <style:text-properties fo:color=\"%s\"/>
459 </style:style>" style-name color-val))))))
460 (cons style-name style)))
462 (defun org-e-odt-insert-custom-styles-for-srcblocks (styles)
463 "Save STYLES used for colorizing of source blocks.
464 Update styles.xml with styles that were collected as part of
465 `org-e-odt-hfy-face-to-css' callbacks."
466 (when styles
467 (with-current-buffer
468 (find-file-noselect (expand-file-name "styles.xml") t)
469 (goto-char (point-min))
470 (when (re-search-forward "</office:styles>" nil t)
471 (goto-char (match-beginning 0))
472 (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n")))))
474 (defun org-e-odt-remap-stylenames (style-name)
476 (cdr (assoc style-name '(("timestamp-wrapper" . "OrgTimestampWrapper")
477 ("timestamp" . "OrgTimestamp")
478 ("timestamp-kwd" . "OrgTimestampKeyword")
479 ("tag" . "OrgTag")
480 ("todo" . "OrgTodo")
481 ("done" . "OrgDone")
482 ("target" . "OrgTarget"))))
483 style-name))
485 (defun org-e-odt-format-fontify (text style &optional id)
486 (let* ((style-name
487 (cond
488 ((stringp style)
489 (org-e-odt-remap-stylenames style))
490 ((symbolp style)
491 (org-e-odt-get-style-name-for-entity 'character style))
492 ((listp style)
493 (assert (< 1 (length style)))
494 (let ((parent-style (pop style)))
495 (mapconcat (lambda (s)
496 ;; (assert (stringp s) t)
497 (org-e-odt-remap-stylenames s)) style "")
498 (org-e-odt-remap-stylenames parent-style)))
499 (t (error "Don't how to handle style %s" style)))))
500 (org-e-odt-format-tags-simple
501 '("<text:span text:style-name=\"%s\">" . "</text:span>")
502 text style-name)))
504 (defun org-e-odt-relocate-relative-path (path dir)
505 (if (file-name-absolute-p path) path
506 (file-relative-name (expand-file-name path dir)
507 (expand-file-name "eyecandy" dir))))
509 (defun org-e-odt-format-formula (element info)
510 (let* ((src (cond
511 ((eq (org-element-type element) 'link) ; FIXME
512 (let* ((type (org-element-property :type element))
513 (raw-path (org-element-property :path element)))
514 (cond
515 ((file-name-absolute-p raw-path)
516 (expand-file-name raw-path))
517 (t raw-path))))
518 ((member (org-element-type element)
519 '(latex-fragment latex-environment))
520 (let* ((latex-frag (org-remove-indentation
521 (org-element-property
522 :value element)))
523 (formula-link (org-e-odt-format-latex
524 latex-frag 'mathml)))
525 (and formula-link
526 (string-match "file:\\([^]]*\\)" formula-link)
527 (match-string 1 formula-link))))
528 (t (error "what is this?"))))
529 (caption-from
530 (case (org-element-type element)
531 (link (org-export-get-parent-paragraph element info))
532 (t element)))
533 (captions (org-e-odt-format-label caption-from info 'definition))
534 (caption (car captions))
535 (href
536 (org-e-odt-format-tags
537 "<draw:object xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
538 (file-name-directory (org-e-odt-copy-formula-file src))))
539 (embed-as (if caption 'paragraph 'character))
540 width height)
541 (cond
542 ((eq embed-as 'character)
543 (org-e-odt-format-entity "InlineFormula" href width height))
545 (let ((table-info nil)
546 (table-info
547 '(:alignment ["c" "c"]
548 :column-groups [nil nil]
549 :row-groups (0)
550 :special-column-p nil :width [8 1]))
551 (org-lparse-table-ncols 2)) ; FIXME
552 (org-e-odt-list-table ; FIXME
553 (list
554 (list
555 (org-e-odt-format-entity
556 "CaptionedDisplayFormula" href width height captions)
557 (let* ((org-e-odt-category-map-alist
558 '(("__Table__" "Table" "value")
559 ("__Figure__" "Illustration" "value")
560 ("__MathFormula__" "Text" "math-label")
561 ("__DvipngImage__" "Equation" "value")
562 ("__Listing__" "Listing" "value"))))
563 (car (org-e-odt-format-label caption-from info 'definition)))))
564 '(table (:attr_odt (":style \"OrgEquation\""))) info))))))
566 (defun org-e-odt-copy-formula-file (path)
567 "Returns the internal name of the file"
568 (let* ((src-file (expand-file-name
569 path (file-name-directory org-current-export-file)))
570 (target-dir (format "Formula-%04d/"
571 (incf org-e-odt-embedded-formulas-count)))
572 (target-file (concat target-dir "content.xml")))
573 (message "Embedding %s as %s ..."
574 (substring-no-properties path) target-file)
576 (make-directory target-dir)
577 (org-e-odt-create-manifest-file-entry
578 "application/vnd.oasis.opendocument.formula" target-dir "1.2")
580 (case (org-e-odt-is-formula-link-p src-file)
581 (mathml
582 (copy-file src-file target-file 'overwrite))
583 (odf
584 (org-e-odt-zip-extract-one src-file "content.xml" target-dir))
586 (error "%s is not a formula file" src-file)))
588 (org-e-odt-create-manifest-file-entry "text/xml" target-file)
589 target-file))
591 (defun org-e-odt-is-formula-link-p (file)
592 (let ((case-fold-search nil))
593 (cond
594 ((string-match "\\.\\(mathml\\|mml\\)\\'" file)
595 'mathml)
596 ((string-match "\\.odf\\'" file)
597 'odf))))
599 (defun org-e-odt-format-org-link (opt-plist type-1 path fragment desc attr
600 descp)
601 "Make a OpenDocument link.
602 OPT-PLIST is an options list.
603 TYPE-1 is the device-type of the link (THIS://foo.html).
604 PATH is the path of the link (http://THIS#location).
605 FRAGMENT is the fragment part of the link, if any (foo.html#THIS).
606 DESC is the link description, if any.
607 ATTR is a string of other attributes of the a element."
608 (declare (special org-lparse-par-open))
609 (save-match-data
610 (let* ((may-inline-p
611 (and (member type-1 '("http" "https" "file"))
612 (org-lparse-should-inline-p path descp)
613 (not fragment)))
614 (type (if (equal type-1 "id") "file" type-1))
615 (filename path)
616 (thefile path))
617 (cond
618 ;; check for inlined images
619 ((and (member type '("file"))
620 (not fragment)
621 (org-file-image-p
622 filename org-e-odt-inline-image-extensions)
623 (not descp))
624 (org-e-odt-format-inline-image thefile))
625 ;; check for embedded formulas
626 ((and (member type '("file"))
627 (not fragment)
628 (org-e-odt-is-formula-link-p filename)
629 (or (not descp)))
630 (org-e-odt-format-formula thefile))
631 ((string= type "coderef")
632 (let* ((ref fragment)
633 (lineno-or-ref (cdr (assoc ref org-export-code-refs)))
634 (desc (and descp desc))
635 (org-e-odt-suppress-xref nil)
636 (href (org-xml-format-href (concat "#coderef-" ref))))
637 (cond
638 ((and (numberp lineno-or-ref) (not desc))
639 (org-e-odt-format-link lineno-or-ref href))
640 ((and (numberp lineno-or-ref) desc
641 (string-match (regexp-quote (concat "(" ref ")")) desc))
642 (format (replace-match "%s" t t desc)
643 (org-e-odt-format-link lineno-or-ref href)))
645 (setq desc (format
646 (if (and desc (string-match
647 (regexp-quote (concat "(" ref ")"))
648 desc))
649 (replace-match "%s" t t desc)
650 (or desc "%s"))
651 lineno-or-ref))
652 (org-e-odt-format-link (org-xml-format-desc desc) href)))))
654 (when (string= type "file")
655 (setq thefile
656 (cond
657 ((file-name-absolute-p path)
658 (concat "file://" (expand-file-name path)))
659 (t (org-e-odt-relocate-relative-path
660 thefile org-current-export-file)))))
662 (when (and (member type '("" "http" "https" "file")) fragment)
663 (setq thefile (concat thefile "#" fragment)))
665 (setq thefile (org-xml-format-href thefile))
667 (when (not (member type '("" "file")))
668 (setq thefile (concat type ":" thefile)))
670 (let ((org-e-odt-suppress-xref nil))
671 (org-e-odt-format-link
672 (org-xml-format-desc desc) thefile attr)))))))
674 (defun org-e-odt-format-anchor (text name &optional class)
675 (org-e-odt-format-target text name))
677 (defun org-e-odt-format-bookmark (text id)
678 (if id
679 (org-e-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id)
680 text))
682 (defun org-e-odt-format-target (text id)
683 (let ((name (concat org-e-odt-bookmark-prefix id)))
684 (concat
685 (and id (org-e-odt-format-tags
686 "<text:bookmark-start text:name=\"%s\"/>" "" name))
687 (org-e-odt-format-bookmark text id)
688 (and id (org-e-odt-format-tags
689 "<text:bookmark-end text:name=\"%s\"/>" "" name)))))
691 (defun org-e-odt-format-footnote (n def)
692 (setq n (format "%d" n))
693 (let ((id (concat "fn" n))
694 (note-class "footnote")
695 (par-style "Footnote"))
696 (org-e-odt-format-tags-simple
697 '("<text:note text:id=\"%s\" text:note-class=\"%s\">" . "</text:note>")
698 (concat
699 (org-e-odt-format-tags-simple
700 '("<text:note-citation>" . "</text:note-citation>") n)
701 (org-e-odt-format-tags-simple
702 '("<text:note-body>" . "</text:note-body>") def))
703 id note-class)))
705 (defun org-e-odt-format-footnote-reference (n def refcnt)
706 (if (= refcnt 1)
707 (org-e-odt-format-footnote n def)
708 (org-e-odt-format-footnote-ref n)))
710 (defun org-e-odt-format-footnote-ref (n)
711 (setq n (format "%d" n))
712 (let ((note-class "footnote")
713 (ref-format "text")
714 (ref-name (concat "fn" n)))
715 (org-e-odt-format-tags-simple
716 '("<text:span text:style-name=\"%s\">" . "</text:span>")
717 (org-e-odt-format-tags-simple
718 '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" . "</text:note-ref>")
719 n note-class ref-format ref-name)
720 "OrgSuperscript")))
722 (defun org-e-odt-element-attributes (element info)
723 (let* ((raw-attr (org-element-property :attr_odt element))
724 (raw-attr (and raw-attr
725 (org-trim (mapconcat #'identity raw-attr " ")))))
726 (unless (and raw-attr (string-match "\\`(.*)\\'" raw-attr))
727 (setq raw-attr (format "(%s)" raw-attr)))
728 (ignore-errors (read raw-attr))))
730 (defun org-e-odt-format-object-description (title description)
731 (concat (and title (org-e-odt-format-tags
732 '("<svg:title>" . "</svg:title>")
733 (org-e-odt-encode-plain-text title t)))
734 (and description (org-e-odt-format-tags
735 '("<svg:desc>" . "</svg:desc>")
736 (org-e-odt-encode-plain-text description t)))))
738 (defun org-e-odt-format-frame (text width height style &optional
739 extra anchor-type)
740 (let ((frame-attrs
741 (concat
742 (if width (format " svg:width=\"%0.2fcm\"" width) "")
743 (if height (format " svg:height=\"%0.2fcm\"" height) "")
744 extra
745 (format " text:anchor-type=\"%s\"" (or anchor-type "paragraph")))))
746 (org-e-odt-format-tags
747 '("<draw:frame draw:style-name=\"%s\"%s>" . "</draw:frame>")
748 (concat text (org-e-odt-format-object-description
749 (get-text-property 0 :title text)
750 (get-text-property 0 :description text)))
751 style frame-attrs)))
753 (defun org-e-odt-format-textbox (text width height style &optional
754 extra anchor-type)
755 (org-e-odt-format-frame
756 (org-e-odt-format-tags
757 '("<draw:text-box %s>" . "</draw:text-box>")
758 text (concat (format " fo:min-height=\"%0.2fcm\"" (or height .2))
759 (unless width
760 (format " fo:min-width=\"%0.2fcm\"" (or width .2)))))
761 width nil style extra anchor-type))
763 (defun org-e-odt-merge-frame-params(default-frame-params user-frame-params)
764 (if (not user-frame-params) default-frame-params
765 (assert (= (length default-frame-params) 3))
766 (assert (= (length user-frame-params) 3))
767 (loop for user-frame-param in user-frame-params
768 for default-frame-param in default-frame-params
769 collect (or user-frame-param default-frame-param))))
771 (defun org-e-odt-copy-image-file (path)
772 "Returns the internal name of the file"
773 (let* ((image-type (file-name-extension path))
774 (media-type (format "image/%s" image-type))
775 (src-file (expand-file-name
776 path (file-name-directory org-current-export-file)))
777 (target-dir "Images/")
778 (target-file
779 (format "%s%04d.%s" target-dir
780 (incf org-e-odt-embedded-images-count) image-type)))
781 (message "Embedding %s as %s ..."
782 (substring-no-properties path) target-file)
784 (when (= 1 org-e-odt-embedded-images-count)
785 (make-directory target-dir)
786 (org-e-odt-create-manifest-file-entry "" target-dir))
788 (copy-file src-file target-file 'overwrite)
789 (org-e-odt-create-manifest-file-entry media-type target-file)
790 target-file))
792 (defun org-e-odt-do-image-size (probe-method file &optional dpi anchor-type)
793 (setq dpi (or dpi org-e-odt-pixels-per-inch))
794 (setq anchor-type (or anchor-type "paragraph"))
795 (flet ((size-in-cms (size-in-pixels)
796 (flet ((pixels-to-cms (pixels)
797 (let* ((cms-per-inch 2.54)
798 (inches (/ pixels dpi)))
799 (* cms-per-inch inches))))
800 (and size-in-pixels
801 (cons (pixels-to-cms (car size-in-pixels))
802 (pixels-to-cms (cdr size-in-pixels)))))))
803 (case probe-method
804 (emacs
805 (size-in-cms (ignore-errors ; Emacs could be in batch mode
806 (clear-image-cache)
807 (image-size (create-image file) 'pixels))))
808 (imagemagick
809 (size-in-cms
810 (let ((dim (shell-command-to-string
811 (format "identify -format \"%%w:%%h\" \"%s\"" file))))
812 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
813 (cons (string-to-number (match-string 1 dim))
814 (string-to-number (match-string 2 dim)))))))
816 (cdr (assoc-string anchor-type
817 org-e-odt-default-image-sizes-alist))))))
819 (defun org-e-odt-image-size-from-file (file &optional user-width
820 user-height scale dpi embed-as)
821 (unless (file-name-absolute-p file)
822 (setq file (expand-file-name
823 file (file-name-directory org-current-export-file))))
824 (let* (size width height)
825 (unless (and user-height user-width)
826 (loop for probe-method in org-e-odt-image-size-probe-method
827 until size
828 do (setq size (org-e-odt-do-image-size
829 probe-method file dpi embed-as)))
830 (or size (error "Cannot determine Image size. Aborting ..."))
831 (setq width (car size) height (cdr size)))
832 (cond
833 (scale
834 (setq width (* width scale) height (* height scale)))
835 ((and user-height user-width)
836 (setq width user-width height user-height))
837 (user-height
838 (setq width (* user-height (/ width height)) height user-height))
839 (user-width
840 (setq height (* user-width (/ height width)) width user-width))
841 (t (ignore)))
842 ;; ensure that an embedded image fits comfortably within a page
843 (let ((max-width (car org-e-odt-max-image-size))
844 (max-height (cdr org-e-odt-max-image-size)))
845 (when (or (> width max-width) (> height max-height))
846 (let* ((scale1 (/ max-width width))
847 (scale2 (/ max-height height))
848 (scale (min scale1 scale2)))
849 (setq width (* scale width) height (* scale height)))))
850 (cons width height)))
852 (defun org-e-odt-format-label (element info op)
853 (let* ((caption-from
854 (case (org-element-type element)
855 (link (org-export-get-parent-paragraph element info))
856 (t element)))
857 ;; get label and caption.
858 (label (org-element-property :name caption-from))
859 (caption (org-element-property :caption caption-from))
860 (short-caption (cdr caption))
861 ;; transcode captions.
862 (caption (and (car caption)
863 (org-export-secondary-string (car caption) info)))
864 (short-caption (and short-caption
865 (org-export-secondary-string short-caption info))))
866 (when (or label caption)
867 (let* ((default-category
868 (cond
869 ((eq (org-element-type element) 'table)
870 "__Table__")
871 ((org-e-odt-standalone-image-p element info)
872 "__Figure__")
873 ((member (org-element-type element)
874 '(latex-environment latex-fragment))
875 (let ((processing-type (plist-get info :LaTeX-fragments)))
876 (cond
877 ((eq processing-type 'dvipng) "__DvipngImage__")
878 ((eq processing-type 'mathjax) "__MathFormula__")
879 ((eq processing-type 't) "__MathFormula__")
880 (t (error "Handle LaTeX:verbatim")))))
881 ((eq (org-element-type element) 'src-block)
882 "__Listing__")
883 (t (error "Handle enumeration of %S" element))))
884 (predicate
885 (cond
886 ((member (org-element-type element)
887 '(table latex-environment src-block))
888 nil)
889 ((org-e-odt-standalone-image-p element info)
890 'org-e-odt-standalone-image-p)
891 (t (error "Handle enumeration of %S" element))))
892 (seqno (org-e-odt-enumerate-element
893 element info predicate)) ; FIXME
894 ;; handle label props.
895 (label-props (assoc default-category org-e-odt-category-map-alist))
896 ;; identify opendocument counter
897 (counter (nth 1 label-props))
898 ;; identify label style
899 (label-style (nth 2 label-props))
900 ;; grok language setting
901 (en-strings (assoc-default "en" org-e-odt-category-strings))
902 (lang (plist-get info :language)) ; FIXME
903 (lang-strings (assoc-default lang org-e-odt-category-strings))
904 ;; retrieve localized category sting
905 (pos (- (length org-e-odt-category-map-alist)
906 (length (memq label-props org-e-odt-category-map-alist))))
907 (category (or (nth pos lang-strings) (nth pos en-strings))))
908 (case op
909 (definition
910 ;; assign an internal label, if user has not provided one
911 (setq label (or label (format "%s-%s" default-category seqno)))
912 (setq label (org-solidify-link-text label))
914 (cons
915 (format-spec
916 (cadr (assoc-string label-style org-e-odt-label-styles t))
917 `((?e . ,category)
918 (?n . ,(org-e-odt-format-tags-simple
919 '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" . "</text:sequence>")
920 seqno label counter counter))
921 (?c . ,(or caption ""))))
922 short-caption))
923 (reference
924 (assert label)
925 (setq label (org-solidify-link-text label))
926 (let* ((fmt (cddr (assoc-string label-style org-e-odt-label-styles t)))
927 (fmt1 (car fmt))
928 (fmt2 (cadr fmt)))
929 (org-e-odt-format-tags-simple
930 '("<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">"
931 . "</text:sequence-ref>")
932 (format-spec fmt2 `((?e . ,category)
933 (?n . ,seqno))) fmt1 label)))
934 (t (error "Unknow %S on label" op)))))))
936 (defun org-e-odt-format-tags-1 (tag text prefix suffix &rest args)
937 (cond
938 ((consp tag)
939 (concat prefix (apply 'format (car tag) args) text suffix
940 (format (cdr tag))))
941 ((stringp tag) ; singleton tag
942 (concat prefix (apply 'format tag args) text))))
944 (defun org-e-odt-format-tags (tag text &rest args)
945 (apply 'org-e-odt-format-tags-1 tag text "\n" "\n" args))
947 (defun org-e-odt-format-tags-simple (tag text &rest args)
948 (apply 'org-e-odt-format-tags-1 tag text nil nil args))
950 (defun org-e-odt-init-outfile ()
951 (unless (executable-find "zip")
952 ;; Not at all OSes ship with zip by default
953 (error "Executable \"zip\" needed for creating OpenDocument files"))
955 (let* ((outdir (make-temp-file
956 (format org-e-odt-tmpdir-prefix 'odt) t)) ; FIXME
957 (content-file (expand-file-name "content.xml" outdir)))
959 ;; reset variables
960 (setq org-e-odt-manifest-file-entries nil
961 org-e-odt-embedded-images-count 0
962 org-e-odt-embedded-formulas-count 0
963 org-e-odt-section-count 0
964 org-e-odt-entity-labels-alist nil
965 org-e-odt-list-stack-stashed nil
966 org-e-odt-automatic-styles nil
967 org-e-odt-object-counters nil
968 org-e-odt-entity-counts-plist nil)
970 ;; let `htmlfontify' know that we are interested in collecting
971 ;; styles - FIXME
973 (setq hfy-user-sheet-assoc nil)
975 ;; init conten.xml
976 (with-current-buffer
977 (find-file-noselect content-file t)
978 (current-buffer))))
980 (defun org-e-odt-save-as-outfile (target opt-plist)
981 ;; write automatic styles
982 (org-e-odt-write-automatic-styles)
984 ;; update display levels
985 (org-e-odt-update-display-level org-e-odt-display-outline-level)
987 ;; write styles file
988 ;; (when (equal org-lparse-backend 'odt) FIXME
989 ;; )
991 ;; (org-e-odt-update-styles-file opt-plist)
993 ;; create mimetype file
994 (let ((mimetype (org-e-odt-write-mimetype-file ;; org-lparse-backend FIXME
995 'odt)))
996 (org-e-odt-create-manifest-file-entry mimetype "/" "1.2"))
998 ;; create a manifest entry for content.xml
999 (org-e-odt-create-manifest-file-entry "text/xml" "content.xml")
1001 ;; write out the manifest entries before zipping
1002 (org-e-odt-write-manifest-file)
1004 (let ((xml-files '("mimetype" "META-INF/manifest.xml" "content.xml"
1005 "meta.xml"))
1006 (zipdir default-directory))
1007 (when (or t (equal org-lparse-backend 'odt)) ; FIXME
1008 (push "styles.xml" xml-files))
1009 (message "Switching to directory %s" (expand-file-name zipdir))
1011 ;; save all xml files
1012 (mapc (lambda (file)
1013 (with-current-buffer
1014 (find-file-noselect (expand-file-name file) t)
1015 ;; prettify output if needed
1016 (when org-e-odt-prettify-xml
1017 (indent-region (point-min) (point-max)))
1018 (save-buffer 0)))
1019 xml-files)
1021 (let* ((target-name (file-name-nondirectory target))
1022 (target-dir (file-name-directory target))
1023 (cmds `(("zip" "-mX0" ,target-name "mimetype")
1024 ("zip" "-rmTq" ,target-name "."))))
1025 (when (file-exists-p target)
1026 ;; FIXME: If the file is locked this throws a cryptic error
1027 (delete-file target))
1029 (let ((coding-system-for-write 'no-conversion) exitcode err-string)
1030 (message "Creating odt file...")
1031 (mapc
1032 (lambda (cmd)
1033 (message "Running %s" (mapconcat 'identity cmd " "))
1034 (setq err-string
1035 (with-output-to-string
1036 (setq exitcode
1037 (apply 'call-process (car cmd)
1038 nil standard-output nil (cdr cmd)))))
1039 (or (zerop exitcode)
1040 (ignore (message "%s" err-string))
1041 (error "Unable to create odt file (%S)" exitcode)))
1042 cmds))
1044 ;; move the file from outdir to target-dir
1045 (rename-file target-name target-dir)
1047 ;; kill all xml buffers
1048 (mapc (lambda (file)
1049 (kill-buffer
1050 (find-file-noselect (expand-file-name file zipdir) t)))
1051 xml-files)
1053 (delete-directory zipdir)))
1054 (message "Created %s" target)
1055 (set-buffer (find-file-noselect target t)))
1058 (defun org-e-odt-create-manifest-file-entry (&rest args)
1059 (push args org-e-odt-manifest-file-entries))
1061 (defun org-e-odt-write-manifest-file ()
1062 (make-directory "META-INF")
1063 (let ((manifest-file (expand-file-name "META-INF/manifest.xml")))
1064 (with-current-buffer
1065 (find-file-noselect manifest-file t)
1066 (insert
1067 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
1068 <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
1069 (mapc
1070 (lambda (file-entry)
1071 (let* ((version (nth 2 file-entry))
1072 (extra (if version
1073 (format " manifest:version=\"%s\"" version)
1074 "")))
1075 (insert
1076 (format org-e-odt-manifest-file-entry-tag
1077 (nth 0 file-entry) (nth 1 file-entry) extra))))
1078 org-e-odt-manifest-file-entries)
1079 (insert "\n</manifest:manifest>"))))
1081 (defun org-e-odt-update-meta-file (info) ; FIXME opt-plist
1082 (let ((title (org-export-secondary-string (plist-get info :title) info))
1083 (author (or (let ((auth (plist-get info :author)))
1084 (and auth (org-export-secondary-string auth info))) ""))
1085 (date (org-e-odt-format-date (plist-get info :date)))
1086 (email (plist-get info :email))
1087 (keywords (plist-get info :keywords))
1088 (description (plist-get info :description)))
1089 (write-region
1090 (concat
1091 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
1092 <office:document-meta
1093 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
1094 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
1095 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
1096 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
1097 xmlns:ooo=\"http://openoffice.org/2004/office\"
1098 office:version=\"1.2\">
1099 <office:meta>\n"
1100 (org-e-odt-format-author author) "\n"
1101 (format "<meta:initial-creator>%s</meta:initial-creator>\n" author)
1102 (format "<dc:date>%s</dc:date>\n" date)
1103 (format "<meta:creation-date>%s</meta:creation-date>\n" date)
1104 (format "<meta:generator>%s</meta:generator>\n"
1105 (concat (and org-export-creator-info org-export-creator-string)))
1106 (format "<meta:keyword>%s</meta:keyword>\n" keywords)
1107 (format "<dc:subject>%s</dc:subject>\n" description)
1108 (format "<dc:title>%s</dc:title>\n" title)
1109 "\n"
1110 " </office:meta>\n" "</office:document-meta>")
1111 nil (expand-file-name "meta.xml")))
1113 ;; create a manifest entry for meta.xml
1114 (org-e-odt-create-manifest-file-entry "text/xml" "meta.xml"))
1116 (defun org-e-odt-update-styles-file (info)
1117 ;; write styles file
1118 (let ((styles-file (plist-get info :odt-styles-file)))
1119 (org-e-odt-copy-styles-file (and styles-file
1120 (read (org-trim styles-file))))
1122 ;; FIXME: Who is opening an empty styles.xml before this point?
1123 (with-current-buffer
1124 (find-file-noselect (expand-file-name "styles.xml") t)
1125 (revert-buffer t t)))
1127 ;; Write custom styles for source blocks
1128 (org-e-odt-insert-custom-styles-for-srcblocks
1129 (mapconcat
1130 (lambda (style)
1131 (format " %s\n" (cddr style)))
1132 hfy-user-sheet-assoc "")))
1134 (defun org-e-odt-write-mimetype-file (format)
1135 ;; create mimetype file
1136 (let ((mimetype
1137 (case format
1138 (odt "application/vnd.oasis.opendocument.text")
1139 (odf "application/vnd.oasis.opendocument.formula")
1140 (t (error "Unknown OpenDocument backend %S" org-lparse-backend)))))
1141 (write-region mimetype nil (expand-file-name "mimetype"))
1142 mimetype))
1144 (declare-function org-create-math-formula "org"
1145 (latex-frag &optional mathml-file))
1147 (defun org-e-odt-get (what &optional opt-plist)
1148 (case what
1149 (EXPORT-DIR (org-export-directory :html opt-plist))
1150 (TABLE-FIRST-COLUMN-AS-LABELS nil)
1151 (CODING-SYSTEM-FOR-WRITE 'utf-8)
1152 (CODING-SYSTEM-FOR-SAVE 'utf-8)
1153 (t (error "Unknown property: %s" what))))
1155 (defun org-e-odt-do-preprocess-latex-fragments ()
1156 "Convert LaTeX fragments to images."
1157 (let* ((latex-frag-opt (plist-get org-lparse-opt-plist :LaTeX-fragments))
1158 (latex-frag-opt ; massage the options
1159 (or (and (member latex-frag-opt '(mathjax t))
1160 (not (and (fboundp 'org-format-latex-mathml-available-p)
1161 (org-format-latex-mathml-available-p)))
1162 (prog1 org-lparse-latex-fragment-fallback
1163 (org-lparse-warn
1164 (concat
1165 "LaTeX to MathML converter not available. "
1166 (format "Using %S instead."
1167 org-lparse-latex-fragment-fallback)))))
1168 latex-frag-opt))
1169 cache-dir display-msg)
1170 (cond
1171 ((eq latex-frag-opt 'dvipng)
1172 (setq cache-dir "ltxpng/")
1173 (setq display-msg "Creating LaTeX image %s"))
1174 ((member latex-frag-opt '(mathjax t))
1175 (setq latex-frag-opt 'mathml)
1176 (setq cache-dir "ltxmathml/")
1177 (setq display-msg "Creating MathML formula %s")))
1178 (when (and org-current-export-file)
1179 (org-format-latex
1180 (concat cache-dir (file-name-sans-extension
1181 (file-name-nondirectory org-current-export-file)))
1182 org-current-export-dir nil display-msg
1183 nil nil latex-frag-opt))))
1185 (eval-after-load 'org-odt
1186 '(ad-deactivate 'org-format-latex-as-mathml))
1188 ; FIXME
1190 ;; (defadvice org-format-latex-as-mathml ; FIXME
1191 ;; (after org-e-odt-protect-latex-fragment activate)
1192 ;; "Encode LaTeX fragment as XML.
1193 ;; Do this when translation to MathML fails."
1194 ;; (when (or (not (> (length ad-return-value) 0))
1195 ;; (get-text-property 0 'org-protected ad-return-value))
1196 ;; (setq ad-return-value
1197 ;; (org-propertize (org-e-odt-encode-plain-text (ad-get-arg 0))
1198 ;; 'org-protected t))))
1200 (defun org-e-odt-zip-extract-one (archive member &optional target)
1201 (require 'arc-mode)
1202 (let* ((target (or target default-directory))
1203 (archive (expand-file-name archive))
1204 (archive-zip-extract
1205 (list "unzip" "-qq" "-o" "-d" target))
1206 exit-code command-output)
1207 (setq command-output
1208 (with-temp-buffer
1209 (setq exit-code (archive-zip-extract archive member))
1210 (buffer-string)))
1211 (unless (zerop exit-code)
1212 (message command-output)
1213 (error "Extraction failed"))))
1215 (defun org-e-odt-zip-extract (archive members &optional target)
1216 (when (atom members) (setq members (list members)))
1217 (mapc (lambda (member)
1218 (org-e-odt-zip-extract-one archive member target))
1219 members))
1221 (defun org-e-odt-copy-styles-file (&optional styles-file)
1222 ;; Non-availability of styles.xml is not a critical error. For now
1223 ;; throw an error purely for aesthetic reasons.
1224 (setq styles-file (or styles-file
1225 org-e-odt-styles-file
1226 (expand-file-name "OrgOdtStyles.xml"
1227 org-e-odt-styles-dir)
1228 (error "org-e-odt: Missing styles file?")))
1229 (cond
1230 ((listp styles-file)
1231 (let ((archive (nth 0 styles-file))
1232 (members (nth 1 styles-file)))
1233 (org-e-odt-zip-extract archive members)
1234 (mapc
1235 (lambda (member)
1236 (when (org-file-image-p member)
1237 (let* ((image-type (file-name-extension member))
1238 (media-type (format "image/%s" image-type)))
1239 (org-e-odt-create-manifest-file-entry media-type member))))
1240 members)))
1241 ((and (stringp styles-file) (file-exists-p styles-file))
1242 (let ((styles-file-type (file-name-extension styles-file)))
1243 (cond
1244 ((string= styles-file-type "xml")
1245 (copy-file styles-file (expand-file-name "styles.xml") t))
1246 ((member styles-file-type '("odt" "ott"))
1247 (org-e-odt-zip-extract styles-file "styles.xml")))))
1249 (error (format "Invalid specification of styles.xml file: %S"
1250 org-e-odt-styles-file))))
1252 ;; create a manifest entry for styles.xml
1253 (org-e-odt-create-manifest-file-entry "text/xml" "styles.xml"))
1255 (defun org-e-odt-configure-outline-numbering ()
1256 "Outline numbering is retained only upto LEVEL.
1257 To disable outline numbering pass a LEVEL of 0."
1258 (goto-char (point-min))
1259 (let ((regex
1260 "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
1261 (replacement
1262 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1263 (while (re-search-forward regex nil t)
1264 (unless (let ((sec-num (plist-get info :section-numbers))
1265 (level (string-to-number (match-string 2))))
1266 (if (wholenump sec-num) (<= level sec-num) sec-num))
1267 (replace-match replacement t nil))))
1268 (save-buffer 0))
1270 ;;;###autoload
1271 (defun org-export-as-odf (latex-frag &optional odf-file)
1272 "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
1273 Use `org-create-math-formula' to convert LATEX-FRAG first to
1274 MathML. When invoked as an interactive command, use
1275 `org-latex-regexps' to infer LATEX-FRAG from currently active
1276 region. If no LaTeX fragments are found, prompt for it. Push
1277 MathML source to kill ring, if `org-export-copy-to-kill-ring' is
1278 non-nil."
1279 (interactive
1280 `(,(let (frag)
1281 (setq frag (and (setq frag (and (region-active-p)
1282 (buffer-substring (region-beginning)
1283 (region-end))))
1284 (loop for e in org-latex-regexps
1285 thereis (when (string-match (nth 1 e) frag)
1286 (match-string (nth 2 e) frag)))))
1287 (read-string "LaTeX Fragment: " frag nil frag))
1288 ,(let ((odf-filename (expand-file-name
1289 (concat
1290 (file-name-sans-extension
1291 (or (file-name-nondirectory buffer-file-name)))
1292 "." "odf")
1293 (file-name-directory buffer-file-name))))
1294 (read-file-name "ODF filename: " nil odf-filename nil
1295 (file-name-nondirectory odf-filename)))))
1296 (let* ((org-lparse-backend 'odf)
1297 org-lparse-opt-plist
1298 (filename (or odf-file
1299 (expand-file-name
1300 (concat
1301 (file-name-sans-extension
1302 (or (file-name-nondirectory buffer-file-name)))
1303 "." "odf")
1304 (file-name-directory buffer-file-name))))
1305 (buffer (find-file-noselect (org-e-odt-init-outfile filename)))
1306 (coding-system-for-write 'utf-8)
1307 (save-buffer-coding-system 'utf-8))
1308 (set-buffer buffer)
1309 (set-buffer-file-coding-system coding-system-for-write)
1310 (let ((mathml (org-create-math-formula latex-frag)))
1311 (unless mathml (error "No Math formula created"))
1312 (insert mathml)
1313 (or (org-export-push-to-kill-ring
1314 (upcase (symbol-name org-lparse-backend)))
1315 (message "Exporting... done")))
1316 (org-e-odt-save-as-outfile filename nil ; FIXME
1319 ;;;###autoload
1320 (defun org-export-as-odf-and-open ()
1321 "Export LaTeX fragment as OpenDocument formula and immediately open it.
1322 Use `org-export-as-odf' to read LaTeX fragment and OpenDocument
1323 formula file."
1324 (interactive)
1325 (org-lparse-and-open
1326 nil nil nil (call-interactively 'org-export-as-odf)))
1331 ;;; Driver Starts here
1332 ;;; Dependencies
1334 (require 'format-spec)
1335 (eval-when-compile (require 'cl) (require 'table))
1339 ;;; Hooks
1341 ;; FIXME: it already exists in org-e-odt.el
1342 ;;; Function Declarations
1344 (declare-function org-element-property "org-element" (property element))
1345 (declare-function org-element-normalize-string "org-element" (s))
1346 (declare-function org-element-parse-secondary-string
1347 "org-element" (string restriction &optional buffer))
1348 (defvar org-element-string-restrictions)
1349 (defvar org-element-object-restrictions)
1351 (declare-function org-export-data "org-export" (data info))
1352 (declare-function org-export-directory "org-export" (type plist))
1353 (declare-function org-export-expand-macro "org-export" (macro info))
1354 (declare-function org-export-first-sibling-p "org-export" (headline info))
1355 (declare-function org-export-footnote-first-reference-p "org-export"
1356 (footnote-reference info))
1357 (declare-function org-export-get-coderef-format "org-export" (path desc))
1358 (declare-function org-export-get-footnote-definition "org-export"
1359 (footnote-reference info))
1360 (declare-function org-export-get-footnote-number "org-export" (footnote info))
1361 (declare-function org-export-get-previous-element "org-export" (blob info))
1362 (declare-function org-export-get-relative-level "org-export" (headline info))
1363 (declare-function org-export-handle-code
1364 "org-export" (element info &optional num-fmt ref-fmt delayed))
1365 (declare-function org-export-included-file "org-export" (keyword backend info))
1366 (declare-function org-export-inline-image-p "org-export"
1367 (link &optional extensions))
1368 (declare-function org-export-last-sibling-p "org-export" (headline info))
1369 (declare-function org-export-low-level-p "org-export" (headline info))
1370 (declare-function org-export-output-file-name
1371 "org-export" (extension &optional subtreep pub-dir))
1372 (declare-function org-export-resolve-coderef "org-export" (ref info))
1373 (declare-function org-export-resolve-fuzzy-link "org-export" (link info))
1374 (declare-function org-export-secondary-string "org-export" (secondary info))
1375 (declare-function org-export-solidify-link-text "org-export" (s))
1376 (declare-function
1377 org-export-to-buffer "org-export"
1378 (backend buffer &optional subtreep visible-only body-only ext-plist))
1379 (declare-function
1380 org-export-to-file "org-export"
1381 (backend file &optional subtreep visible-only body-only ext-plist))
1383 (declare-function org-id-find-id-file "org-id" (id))
1384 (declare-function htmlize-region "ext:htmlize" (beg end))
1385 (declare-function org-pop-to-buffer-same-window
1386 "org-compat" (&optional buffer-or-name norecord label))
1392 (declare-function hfy-face-to-style "htmlfontify" (fn))
1393 (declare-function hfy-face-or-def-to-name "htmlfontify" (fn))
1394 (declare-function archive-zip-extract "arc-mode.el" (archive name))
1396 ;;; Internal Variables
1398 ;;;; ODT Internal Variables
1400 (defconst org-e-odt-lib-dir
1401 (file-name-directory load-file-name)
1402 "Location of ODT exporter.
1403 Use this to infer values of `org-e-odt-styles-dir' and
1404 `org-e-odt-schema-dir'.")
1406 (defvar org-e-odt-data-dir
1407 (expand-file-name "../../etc/" org-e-odt-lib-dir)
1408 "Data directory for ODT exporter.
1409 Use this to infer values of `org-e-odt-styles-dir' and
1410 `org-e-odt-schema-dir'.")
1412 (defconst org-e-odt-special-string-regexps
1413 '(("\\\\-" . "&#x00ad;\\1") ; shy
1414 ("---\\([^-]\\)" . "&#x2014;\\1") ; mdash
1415 ("--\\([^-]\\)" . "&#x2013;\\1") ; ndash
1416 ("\\.\\.\\." . "&#x2026;")) ; hellip
1417 "Regular expressions for special string conversion.")
1419 (defconst org-e-odt-schema-dir-list
1420 (list
1421 (and org-e-odt-data-dir
1422 (expand-file-name "./schema/" org-e-odt-data-dir)) ; bail out
1423 (eval-when-compile
1424 (and (boundp 'org-e-odt-data-dir) org-e-odt-data-dir ; see make install
1425 (expand-file-name "./schema/" org-e-odt-data-dir))))
1426 "List of directories to search for OpenDocument schema files.
1427 Use this list to set the default value of
1428 `org-e-odt-schema-dir'. The entries in this list are
1429 populated heuristically based on the values of `org-e-odt-lib-dir'
1430 and `org-e-odt-data-dir'.")
1432 (defconst org-e-odt-styles-dir-list
1433 (list
1434 (and org-e-odt-data-dir
1435 (expand-file-name "./styles/" org-e-odt-data-dir)) ; bail out
1436 (eval-when-compile
1437 (and (boundp 'org-e-odt-data-dir) org-e-odt-data-dir ; see make install
1438 (expand-file-name "./styles/" org-e-odt-data-dir)))
1439 (expand-file-name "../../etc/styles/" org-e-odt-lib-dir) ; git
1440 (expand-file-name "./etc/styles/" org-e-odt-lib-dir) ; elpa
1441 (expand-file-name "./org/" data-directory) ; system
1443 "List of directories to search for OpenDocument styles files.
1444 See `org-e-odt-styles-dir'. The entries in this list are populated
1445 heuristically based on the values of `org-e-odt-lib-dir' and
1446 `org-e-odt-data-dir'.")
1448 (defconst org-e-odt-styles-dir
1449 (let* ((styles-dir
1450 (catch 'styles-dir
1451 (message "Debug (org-e-odt): Searching for OpenDocument styles files...")
1452 (mapc (lambda (styles-dir)
1453 (when styles-dir
1454 (message "Debug (org-e-odt): Trying %s..." styles-dir)
1455 (when (and (file-readable-p
1456 (expand-file-name
1457 "OrgOdtContentTemplate.xml" styles-dir))
1458 (file-readable-p
1459 (expand-file-name
1460 "OrgOdtStyles.xml" styles-dir)))
1461 (message "Debug (org-e-odt): Using styles under %s"
1462 styles-dir)
1463 (throw 'styles-dir styles-dir))))
1464 org-e-odt-styles-dir-list)
1465 nil)))
1466 (unless styles-dir
1467 (error "Error (org-e-odt): Cannot find factory styles files. Aborting."))
1468 styles-dir)
1469 "Directory that holds auxiliary XML files used by the ODT exporter.
1471 This directory contains the following XML files -
1472 \"OrgOdtStyles.xml\" and \"OrgOdtContentTemplate.xml\". These
1473 XML files are used as the default values of
1474 `org-e-odt-styles-file' and
1475 `org-e-odt-content-template-file'.
1477 The default value of this variable varies depending on the
1478 version of org in use and is initialized from
1479 `org-e-odt-styles-dir-list'. Note that the user could be using org
1480 from one of: org's own private git repository, GNU ELPA tar or
1481 standard Emacs.")
1483 (defconst org-e-odt-tmpdir-prefix "%s-")
1484 (defconst org-e-odt-bookmark-prefix "OrgXref.")
1486 (defconst org-e-odt-manifest-file-entry-tag
1488 <manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
1492 (defvar org-lparse-dyn-first-heading-pos) ; let bound during org-do-lparse
1494 (defvar org-e-odt-suppress-xref nil)
1495 (defvar org-e-odt-file-extensions
1496 '(("odt" . "OpenDocument Text")
1497 ("ott" . "OpenDocument Text Template")
1498 ("odm" . "OpenDocument Master Document")
1499 ("ods" . "OpenDocument Spreadsheet")
1500 ("ots" . "OpenDocument Spreadsheet Template")
1501 ("odg" . "OpenDocument Drawing (Graphics)")
1502 ("otg" . "OpenDocument Drawing Template")
1503 ("odp" . "OpenDocument Presentation")
1504 ("otp" . "OpenDocument Presentation Template")
1505 ("odi" . "OpenDocument Image")
1506 ("odf" . "OpenDocument Formula")
1507 ("odc" . "OpenDocument Chart")))
1509 (defvar org-e-odt-default-org-styles-alist
1510 '((paragraph . ((default . "Text_20_body")
1511 (fixedwidth . "OrgFixedWidthBlock")
1512 (verse . "OrgVerse")
1513 (quote . "Quotations")
1514 (blockquote . "Quotations")
1515 (center . "OrgCenter")
1516 (left . "OrgLeft")
1517 (right . "OrgRight")
1518 (title . "OrgTitle")
1519 (subtitle . "OrgSubtitle")
1520 (footnote . "Footnote")
1521 (src . "OrgSrcBlock")
1522 (illustration . "Illustration")
1523 (table . "Table")
1524 (listing . "Listing")
1525 (definition-term . "Text_20_body_20_bold")
1526 (horizontal-line . "Horizontal_20_Line")))
1527 (character . ((bold . "Bold")
1528 (emphasis . "Emphasis")
1529 (code . "OrgCode")
1530 (verbatim . "OrgCode")
1531 (strike . "Strikethrough")
1532 (underline . "Underline")
1533 (subscript . "OrgSubscript")
1534 (superscript . "OrgSuperscript")))
1535 (list . ((ordered . "OrgNumberedList")
1536 (unordered . "OrgBulletedList")
1537 (descriptive . "OrgDescriptionList"))))
1538 "Default styles for various entities.")
1540 (defvar org-e-odt-org-styles-alist org-e-odt-default-org-styles-alist)
1542 ;;;_. callbacks
1543 ;;;_. control callbacks
1544 ;;;_ , document body
1546 (defvar org-lparse-body-only) ; let bound during org-do-lparse
1547 (defvar org-lparse-opt-plist) ; bound during org-do-lparse
1548 (defvar org-lparse-list-stack) ; dynamically bound in org-do-lparse
1549 (defvar org-e-odt-list-stack-stashed)
1550 (defvar org-lparse-table-ncols)
1551 (defvar org-e-odt-table-rowgrp-open)
1552 (defvar org-e-odt-table-rownum)
1553 (defvar org-e-odt-table-cur-rowgrp-is-hdr)
1554 (defvar org-lparse-table-is-styled)
1555 (defvar org-lparse-table-rowgrp-info)
1556 (defvar org-lparse-table-colalign-vector)
1558 (defvar org-e-odt-table-style nil
1559 "Table style specified by \"#+ATTR_ODT: <style-name>\" line.
1560 This is set during `org-e-odt-begin-table'.")
1562 (defvar org-e-odt-table-style-spec nil
1563 "Entry for `org-e-odt-table-style' in `org-e-odt-table-styles'.")
1566 (defvar org-e-odt-table-style-format
1568 <style:style style:name=\"%s\" style:family=\"table\">
1569 <style:table-properties style:rel-width=\"%d%%\" fo:margin-top=\"0cm\" fo:margin-bottom=\"0.20cm\" table:align=\"center\"/>
1570 </style:style>
1572 "Template for auto-generated Table styles.")
1574 (defvar org-e-odt-automatic-styles '()
1575 "Registry of automatic styles for various OBJECT-TYPEs.
1576 The variable has the following form:
1577 \(\(OBJECT-TYPE-A
1578 \(\(OBJECT-NAME-A.1 OBJECT-PROPS-A.1\)
1579 \(OBJECT-NAME-A.2 OBJECT-PROPS-A.2\) ...\)\)
1580 \(OBJECT-TYPE-B
1581 \(\(OBJECT-NAME-B.1 OBJECT-PROPS-B.1\)
1582 \(OBJECT-NAME-B.2 OBJECT-PROPS-B.2\) ...\)\)
1583 ...\).
1585 OBJECT-TYPEs could be \"Section\", \"Table\", \"Figure\" etc.
1586 OBJECT-PROPS is (typically) a plist created by passing
1587 \"#+ATTR_ODT: \" option to `org-e-odt-parse-block-attributes'.
1589 Use `org-e-odt-add-automatic-style' to add update this variable.'")
1591 (defvar org-e-odt-object-counters nil
1592 "Running counters for various OBJECT-TYPEs.
1593 Use this to generate automatic names and style-names. See
1594 `org-e-odt-add-automatic-style'.")
1596 (defvar org-e-odt-table-indentedp nil)
1597 (defvar org-lparse-table-colalign-info)
1598 (defvar org-lparse-link-description-is-image nil)
1601 (defvar org-src-block-paragraph-format
1602 "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
1603 <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
1604 <style:background-image/>
1605 </style:paragraph-properties>
1606 <style:text-properties fo:color=\"%s\"/>
1607 </style:style>"
1608 "Custom paragraph style for colorized source and example blocks.
1609 This style is much the same as that of \"OrgFixedWidthBlock\"
1610 except that the foreground and background colors are set
1611 according to the default face identified by the `htmlfontify'.")
1613 (defvar hfy-optimisations)
1614 (defvar org-e-odt-embedded-formulas-count 0)
1615 (defvar org-e-odt-entity-frame-styles
1616 '(("As-CharImage" "__Figure__" ("OrgInlineImage" nil "as-char"))
1617 ("ParagraphImage" "__Figure__" ("OrgDisplayImage" nil "paragraph"))
1618 ("PageImage" "__Figure__" ("OrgPageImage" nil "page"))
1619 ("CaptionedAs-CharImage" "__Figure__"
1620 ("OrgCaptionedImage"
1621 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
1622 ("OrgInlineImage" nil "as-char"))
1623 ("CaptionedParagraphImage" "__Figure__"
1624 ("OrgCaptionedImage"
1625 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
1626 ("OrgImageCaptionFrame" nil "paragraph"))
1627 ("CaptionedPageImage" "__Figure__"
1628 ("OrgCaptionedImage"
1629 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
1630 ("OrgPageImageCaptionFrame" nil "page"))
1631 ("InlineFormula" "__MathFormula__" ("OrgInlineFormula" nil "as-char"))
1632 ("DisplayFormula" "__MathFormula__" ("OrgDisplayFormula" nil "as-char"))
1633 ("CaptionedDisplayFormula" "__MathFormula__"
1634 ("OrgCaptionedFormula" nil "paragraph")
1635 ("OrgFormulaCaptionFrame" nil "as-char"))))
1637 (defvar org-e-odt-embedded-images-count 0)
1639 (defvar org-e-odt-image-size-probe-method
1640 (append (and (executable-find "identify") '(imagemagick)) ; See Bug#10675
1641 '(emacs fixed))
1642 "Ordered list of methods for determining image sizes.")
1644 (defvar org-e-odt-default-image-sizes-alist
1645 '(("as-char" . (5 . 0.4))
1646 ("paragraph" . (5 . 5)))
1647 "Hardcoded image dimensions one for each of the anchor
1648 methods.")
1650 ;; A4 page size is 21.0 by 29.7 cms
1651 ;; The default page settings has 2cm margin on each of the sides. So
1652 ;; the effective text area is 17.0 by 25.7 cm
1653 (defvar org-e-odt-max-image-size '(17.0 . 20.0)
1654 "Limiting dimensions for an embedded image.")
1656 (defvar org-e-odt-entity-labels-alist nil
1657 "Associate Labels with the Labeled entities.
1658 Each element of the alist is of the form (LABEL-NAME
1659 CATEGORY-NAME SEQNO LABEL-STYLE-NAME). LABEL-NAME is same as
1660 that specified by \"#+LABEL: ...\" line. CATEGORY-NAME is the
1661 type of the entity that LABEL-NAME is attached to. CATEGORY-NAME
1662 can be one of \"Table\", \"Figure\" or \"Equation\". SEQNO is
1663 the unique number assigned to the referenced entity on a
1664 per-CATEGORY basis. It is generated sequentially and is 1-based.
1665 LABEL-STYLE-NAME is a key `org-e-odt-label-styles'.
1667 See `org-e-odt-add-label-definition' and
1668 `org-e-odt-fixup-label-references'.")
1670 (defvar org-e-odt-entity-counts-plist nil
1671 "Plist of running counters of SEQNOs for each of the CATEGORY-NAMEs.
1672 See `org-e-odt-entity-labels-alist' for known CATEGORY-NAMEs.")
1674 (defvar org-e-odt-label-styles
1675 '(("math-formula" "%c" "text" "(%n)")
1676 ("math-label" "(%n)" "text" "(%n)")
1677 ("category-and-value" "%e %n: %c" "category-and-value" "%e %n")
1678 ("value" "%e %n: %c" "value" "%n"))
1679 "Specify how labels are applied and referenced.
1680 This is an alist where each element is of the
1681 form (LABEL-STYLE-NAME LABEL-ATTACH-FMT LABEL-REF-MODE
1682 LABEL-REF-FMT).
1684 LABEL-ATTACH-FMT controls how labels and captions are attached to
1685 an entity. It may contain following specifiers - %e, %n and %c.
1686 %e is replaced with the CATEGORY-NAME. %n is replaced with
1687 \"<text:sequence ...> SEQNO </text:sequence>\". %c is replaced
1688 with CAPTION. See `org-e-odt-format-label-definition'.
1690 LABEL-REF-MODE and LABEL-REF-FMT controls how label references
1691 are generated. The following XML is generated for a label
1692 reference - \"<text:sequence-ref
1693 text:reference-format=\"LABEL-REF-MODE\" ...> LABEL-REF-FMT
1694 </text:sequence-ref>\". LABEL-REF-FMT may contain following
1695 specifiers - %e and %n. %e is replaced with the CATEGORY-NAME.
1696 %n is replaced with SEQNO. See
1697 `org-e-odt-format-label-reference'.")
1699 (defcustom org-e-odt-category-strings
1700 '(("en" "Table" "Figure" "Equation" "Equation" "Listing"))
1701 "Specify category strings for various captionable entities.
1702 Captionable entity can be one of a Table, an Embedded Image, a
1703 LaTeX fragment (generated with dvipng) or a Math Formula.
1705 For example, when `org-export-default-language' is \"en\", an
1706 embedded image will be captioned as \"Figure 1: Orgmode Logo\".
1707 If you want the images to be captioned instead as \"Illustration
1708 1: Orgmode Logo\", then modify the entry for \"en\" as shown
1709 below.
1711 \(setq org-e-odt-category-strings
1712 '\(\(\"en\" \"Table\" \"Illustration\"
1713 \"Equation\" \"Equation\"\)\)\)"
1714 :group 'org-export-e-odt
1715 :version "24.1"
1716 :type '(repeat (list (string :tag "Language tag")
1717 (choice :tag "Table"
1718 (const :tag "Use Default" nil)
1719 (string :tag "Category string"))
1720 (choice :tag "Figure"
1721 (const :tag "Use Default" nil)
1722 (string :tag "Category string"))
1723 (choice :tag "Math Formula"
1724 (const :tag "Use Default" nil)
1725 (string :tag "Category string"))
1726 (choice :tag "Dvipng Image"
1727 (const :tag "Use Default" nil)
1728 (string :tag "Category string"))
1729 (choice :tag "Listing"
1730 (const :tag "Use Default" nil)
1731 (string :tag "Category string")))))
1733 (defvar org-e-odt-category-map-alist
1734 '(("__Table__" "Table" "value")
1735 ("__Figure__" "Illustration" "value")
1736 ("__MathFormula__" "Text" "math-formula")
1737 ("__DvipngImage__" "Equation" "value")
1738 ("__Listing__" "Listing" "value")
1739 ;; ("__Table__" "Table" "category-and-value")
1740 ;; ("__Figure__" "Figure" "category-and-value")
1741 ;; ("__DvipngImage__" "Equation" "category-and-value")
1743 "Map a CATEGORY-HANDLE to OD-VARIABLE and LABEL-STYLE.
1744 This is a list where each entry is of the form \\(CATEGORY-HANDLE
1745 OD-VARIABLE LABEL-STYLE\\). CATEGORY_HANDLE identifies the
1746 captionable entity in question. OD-VARIABLE is the OpenDocument
1747 sequence counter associated with the entity. These counters are
1748 declared within
1749 \"<text:sequence-decls>...</text:sequence-decls>\" block of
1750 `org-e-odt-content-template-file'. LABEL-STYLE is a key
1751 into `org-e-odt-label-styles' and specifies how a given entity
1752 should be captioned and referenced.
1754 The position of a CATEGORY-HANDLE in this list is used as an
1755 index in to per-language entry for
1756 `org-e-odt-category-strings' to retrieve a CATEGORY-NAME.
1757 This CATEGORY-NAME is then used for qualifying the user-specified
1758 captions on export.")
1760 (defvar org-e-odt-manifest-file-entries nil)
1761 (defvar hfy-user-sheet-assoc) ; bound during org-do-lparse
1762 (defvar org-lparse-latex-fragment-fallback) ; set by org-do-lparse
1765 ;;;; HTML Internal Variables
1767 (defvar org-e-odt-option-alist
1769 ;; (:agenda-style nil nil org-agenda-export-html-style)
1770 ;; (:convert-org-links nil nil org-e-odt-link-org-files-as-html)
1771 ;; ;; FIXME Use (org-xml-encode-org-text-skip-links s) ??
1772 ;; ;; (:expand-quoted-html nil "@" org-e-odt-expand)
1773 ;; (:inline-images nil nil org-e-odt-inline-images)
1774 ;; ;; (:link-home nil nil org-e-odt-link-home) FIXME
1775 ;; ;; (:link-up nil nil org-e-odt-link-up) FIXME
1776 ;; (:style nil nil org-e-odt-style)
1777 ;; (:style-extra nil nil org-e-odt-style-extra)
1778 ;; (:style-include-default nil nil org-e-odt-style-include-default)
1779 ;; (:style-include-scripts nil nil org-e-odt-style-include-scripts)
1780 ;; ;; (:timestamp nil nil org-e-odt-with-timestamp)
1781 ;; (:html-extension nil nil org-e-odt-extension)
1782 ;; (:html-postamble nil nil org-e-odt-postamble)
1783 ;; (:html-preamble nil nil org-e-odt-preamble)
1784 ;; (:html-table-tag nil nil org-e-odt-table-tag)
1785 ;; (:xml-declaration nil nil org-e-odt-xml-declaration)
1786 (:odt-styles-file "ODT_STYLES_FILE" nil nil t)
1787 (:LaTeX-fragments nil "LaTeX" org-export-with-LaTeX-fragments))
1788 "Alist between export properties and ways to set them.
1790 The car of the alist is the property name, and the cdr is a list
1791 like \(KEYWORD OPTION DEFAULT BEHAVIOUR\) where:
1793 KEYWORD is a string representing a buffer keyword, or nil.
1794 OPTION is a string that could be found in an #+OPTIONS: line.
1795 DEFAULT is the default value for the property.
1796 BEHAVIOUR determine how Org should handle multiple keywords for
1797 the same property. It is a symbol among:
1798 nil Keep old value and discard the new one.
1799 t Replace old value with the new one.
1800 `space' Concatenate the values, separating them with a space.
1801 `newline' Concatenate the values, separating them with
1802 a newline.
1803 `split' Split values at white spaces, and cons them to the
1804 previous list.
1806 KEYWORD and OPTION have precedence over DEFAULT.
1808 All these properties should be back-end agnostic. For back-end
1809 specific properties, define a similar variable named
1810 `org-BACKEND-option-alist', replacing BACKEND with the name of
1811 the appropriate back-end. You can also redefine properties
1812 there, as they have precedence over these.")
1814 (defvar html-table-tag nil) ; dynamically scoped into this.
1816 ;; FIXME: it already exists in org-e-odt.el
1817 (defconst org-e-odt-cvt-link-fn
1819 "Function to convert link URLs to exportable URLs.
1820 Takes two arguments, TYPE and PATH.
1821 Returns exportable url as (TYPE PATH), or nil to signal that it
1822 didn't handle this case.
1823 Intended to be locally bound around a call to `org-export-as-html'." )
1828 (defvar org-e-odt-format-table-no-css)
1829 (defvar htmlize-buffer-places) ; from htmlize.el
1830 (defvar body-only) ; dynamically scoped into this.
1832 (defvar org-e-odt-table-rowgrp-open)
1833 (defvar org-e-odt-table-rownum)
1834 (defvar org-e-odt-table-cur-rowgrp-is-hdr)
1835 (defvar org-lparse-table-is-styled)
1838 (defvar org-e-odt-headline-formatter
1839 (lambda (level snumber todo todo-type priority
1840 title tags target extra-targets extra-class)
1841 (concat snumber " " title)))
1845 ;;; User Configuration Variables
1847 (defgroup org-export-e-odt nil
1848 "Options for exporting Org mode files to ODT."
1849 :tag "Org Export ODT"
1850 :group 'org-export)
1852 (defcustom org-e-odt-protect-char-alist
1853 '(("&" . "&amp;")
1854 ("<" . "&lt;")
1855 (">" . "&gt;"))
1856 "Alist of characters to be converted by `org-e-html-protect'."
1857 :group 'org-export-e-html
1858 :type '(repeat (cons (string :tag "Character")
1859 (string :tag "ODT equivalent"))))
1860 (defcustom org-e-odt-schema-dir
1861 (let* ((schema-dir
1862 (catch 'schema-dir
1863 (message "Debug (org-e-odt): Searching for OpenDocument schema files...")
1864 (mapc
1865 (lambda (schema-dir)
1866 (when schema-dir
1867 (message "Debug (org-e-odt): Trying %s..." schema-dir)
1868 (when (and (file-readable-p
1869 (expand-file-name "od-manifest-schema-v1.2-cs01.rnc"
1870 schema-dir))
1871 (file-readable-p
1872 (expand-file-name "od-schema-v1.2-cs01.rnc"
1873 schema-dir))
1874 (file-readable-p
1875 (expand-file-name "schemas.xml" schema-dir)))
1876 (message "Debug (org-e-odt): Using schema files under %s"
1877 schema-dir)
1878 (throw 'schema-dir schema-dir))))
1879 org-e-odt-schema-dir-list)
1880 (message "Debug (org-e-odt): No OpenDocument schema files installed")
1881 nil)))
1882 schema-dir)
1883 "Directory that contains OpenDocument schema files.
1885 This directory contains:
1886 1. rnc files for OpenDocument schema
1887 2. a \"schemas.xml\" file that specifies locating rules needed
1888 for auto validation of OpenDocument XML files.
1890 Use the customize interface to set this variable. This ensures
1891 that `rng-schema-locating-files' is updated and auto-validation
1892 of OpenDocument XML takes place based on the value
1893 `rng-nxml-auto-validate-flag'.
1895 The default value of this variable varies depending on the
1896 version of org in use and is initialized from
1897 `org-e-odt-schema-dir-list'. The OASIS schema files are available
1898 only in the org's private git repository. It is *not* bundled
1899 with GNU ELPA tar or standard Emacs distribution."
1900 :type '(choice
1901 (const :tag "Not set" nil)
1902 (directory :tag "Schema directory"))
1903 :group 'org-export-e-odt
1904 :version "24.1"
1905 :set
1906 (lambda (var value)
1907 "Set `org-e-odt-schema-dir'.
1908 Also add it to `rng-schema-locating-files'."
1909 (let ((schema-dir value))
1910 (set var
1911 (if (and
1912 (file-readable-p
1913 (expand-file-name "od-manifest-schema-v1.2-cs01.rnc" schema-dir))
1914 (file-readable-p
1915 (expand-file-name "od-schema-v1.2-cs01.rnc" schema-dir))
1916 (file-readable-p
1917 (expand-file-name "schemas.xml" schema-dir)))
1918 schema-dir
1919 (when value
1920 (message "Error (org-e-odt): %s has no OpenDocument schema files"
1921 value))
1922 nil)))
1923 (when org-e-odt-schema-dir
1924 (eval-after-load 'rng-loc
1925 '(add-to-list 'rng-schema-locating-files
1926 (expand-file-name "schemas.xml"
1927 org-e-odt-schema-dir))))))
1929 (defcustom org-e-odt-content-template-file nil
1930 "Template file for \"content.xml\".
1931 The exporter embeds the exported content just before
1932 \"</office:text>\" element.
1934 If unspecified, the file named \"OrgOdtContentTemplate.xml\"
1935 under `org-e-odt-styles-dir' is used."
1936 :type 'file
1937 :group 'org-export-e-odt
1938 :version "24.1")
1940 (defcustom org-e-odt-styles-file nil
1941 "Default styles file for use with ODT export.
1942 Valid values are one of:
1943 1. nil
1944 2. path to a styles.xml file
1945 3. path to a *.odt or a *.ott file
1946 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
1947 ...))
1949 In case of option 1, an in-built styles.xml is used. See
1950 `org-e-odt-styles-dir' for more information.
1952 In case of option 3, the specified file is unzipped and the
1953 styles.xml embedded therein is used.
1955 In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
1956 and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
1957 generated odt file. Use relative path for specifying the
1958 FILE-MEMBERS. styles.xml must be specified as one of the
1959 FILE-MEMBERS.
1961 Use options 1, 2 or 3 only if styles.xml alone suffices for
1962 achieving the desired formatting. Use option 4, if the styles.xml
1963 references additional files like header and footer images for
1964 achieving the desired formatting.
1966 Use \"#+ODT_STYLES_FILE: ...\" directive to set this variable on
1967 a per-file basis. For example,
1969 #+ODT_STYLES_FILE: \"/path/to/styles.xml\" or
1970 #+ODT_STYLES_FILE: (\"/path/to/file.ott\" (\"styles.xml\" \"image/hdr.png\"))."
1971 :group 'org-export-e-odt
1972 :version "24.1"
1973 :type
1974 '(choice
1975 (const :tag "Factory settings" nil)
1976 (file :must-match t :tag "styles.xml")
1977 (file :must-match t :tag "ODT or OTT file")
1978 (list :tag "ODT or OTT file + Members"
1979 (file :must-match t :tag "ODF Text or Text Template file")
1980 (cons :tag "Members"
1981 (file :tag " Member" "styles.xml")
1982 (repeat (file :tag "Member"))))))
1985 (defcustom org-e-odt-inline-image-extensions
1986 '("png" "jpeg" "jpg" "gif")
1987 "Extensions of image files that can be inlined into HTML."
1988 :type '(repeat (string :tag "Extension"))
1989 :group 'org-export-e-odt
1990 :version "24.1")
1992 (defcustom org-e-odt-pixels-per-inch display-pixels-per-inch
1993 "Scaling factor for converting images pixels to inches.
1994 Use this for sizing of embedded images. See Info node `(org)
1995 Images in ODT export' for more information."
1996 :type 'float
1997 :group 'org-export-e-odt
1998 :version "24.1")
2000 (defcustom org-e-odt-create-custom-styles-for-srcblocks t
2001 "Whether custom styles for colorized source blocks be automatically created.
2002 When this option is turned on, the exporter creates custom styles
2003 for source blocks based on the advice of `htmlfontify'. Creation
2004 of custom styles happen as part of `org-e-odt-hfy-face-to-css'.
2006 When this option is turned off exporter does not create such
2007 styles.
2009 Use the latter option if you do not want the custom styles to be
2010 based on your current display settings. It is necessary that the
2011 styles.xml already contains needed styles for colorizing to work.
2013 This variable is effective only if
2014 `org-e-odt-fontify-srcblocks' is turned on."
2015 :group 'org-export-e-odt
2016 :version "24.1"
2017 :type 'boolean)
2019 (defcustom org-e-odt-preferred-output-format nil
2020 "Automatically post-process to this format after exporting to \"odt\".
2021 Interactive commands `org-export-as-e-odt' and
2022 `org-export-as-e-odt-and-open' export first to \"odt\" format and
2023 then use `org-e-odt-convert-process' to convert the
2024 resulting document to this format. During customization of this
2025 variable, the list of valid values are populated based on
2026 `org-e-odt-convert-capabilities'."
2027 :group 'org-export-e-odt
2028 :version "24.1"
2029 :type '(choice :convert-widget
2030 (lambda (w)
2031 (apply 'widget-convert (widget-type w)
2032 (eval (car (widget-get w :args)))))
2033 `((const :tag "None" nil)
2034 ,@(mapcar (lambda (c)
2035 `(const :tag ,c ,c))
2036 (org-e-odt-reachable-formats "odt")))))
2038 (defcustom org-e-odt-table-styles
2039 '(("OrgEquation" "OrgEquation"
2040 ((use-first-column-styles . t)
2041 (use-last-column-styles . t))))
2042 "Specify how Table Styles should be derived from a Table Template.
2043 This is a list where each element is of the
2044 form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
2046 TABLE-STYLE-NAME is the style associated with the table through
2047 `org-e-odt-table-style'.
2049 TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
2050 TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
2051 below) that is included in
2052 `org-e-odt-content-template-file'.
2054 TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
2055 \"TableCell\"
2056 PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
2057 \"TableParagraph\"
2058 TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
2059 \"FirstRow\" | \"LastRow\" |
2060 \"EvenRow\" | \"OddRow\" |
2061 \"EvenColumn\" | \"OddColumn\" | \"\"
2062 where \"+\" above denotes string concatenation.
2064 TABLE-CELL-OPTIONS is an alist where each element is of the
2065 form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
2066 TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
2067 `use-last-row-styles' |
2068 `use-first-column-styles' |
2069 `use-last-column-styles' |
2070 `use-banding-rows-styles' |
2071 `use-banding-columns-styles' |
2072 `use-first-row-styles'
2073 ON-OR-OFF := `t' | `nil'
2075 For example, with the following configuration
2077 \(setq org-e-odt-table-styles
2078 '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
2079 \(\(use-first-row-styles . t\)
2080 \(use-first-column-styles . t\)\)\)
2081 \(\"TableWithHeaderColumns\" \"Custom\"
2082 \(\(use-first-column-styles . t\)\)\)\)\)
2084 1. A table associated with \"TableWithHeaderRowsAndColumns\"
2085 style will use the following table-cell styles -
2086 \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
2087 \"CustomTableCell\" and the following paragraph styles
2088 \"CustomFirstRowTableParagraph\",
2089 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
2090 as appropriate.
2092 2. A table associated with \"TableWithHeaderColumns\" style will
2093 use the following table-cell styles -
2094 \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
2095 following paragraph styles
2096 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
2097 as appropriate..
2099 Note that TABLE-TEMPLATE-NAME corresponds to the
2100 \"<table:table-template>\" elements contained within
2101 \"<office:styles>\". The entries (TABLE-STYLE-NAME
2102 TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
2103 \"table:template-name\" and \"table:use-first-row-styles\" etc
2104 attributes of \"<table:table>\" element. Refer ODF-1.2
2105 specification for more information. Also consult the
2106 implementation filed under `org-e-odt-get-table-cell-styles'.
2108 The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
2109 formatting of numbered display equations. Do not delete this
2110 style from the list."
2111 :group 'org-export-e-odt
2112 :version "24.1"
2113 :type '(choice
2114 (const :tag "None" nil)
2115 (repeat :tag "Table Styles"
2116 (list :tag "Table Style Specification"
2117 (string :tag "Table Style Name")
2118 (string :tag "Table Template Name")
2119 (alist :options (use-first-row-styles
2120 use-last-row-styles
2121 use-first-column-styles
2122 use-last-column-styles
2123 use-banding-rows-styles
2124 use-banding-columns-styles)
2125 :key-type symbol
2126 :value-type (const :tag "True" t))))))
2127 (defcustom org-e-odt-fontify-srcblocks t
2128 "Specify whether or not source blocks need to be fontified.
2129 Turn this option on if you want to colorize the source code
2130 blocks in the exported file. For colorization to work, you need
2131 to make available an enhanced version of `htmlfontify' library."
2132 :type 'boolean
2133 :group 'org-export-e-odt
2134 :version "24.1")
2136 (defcustom org-e-odt-prettify-xml t ; FIXME
2137 "Specify whether or not the xml output should be prettified.
2138 When this option is turned on, `indent-region' is run on all
2139 component xml buffers before they are saved. Turn this off for
2140 regular use. Turn this on if you need to examine the xml
2141 visually."
2142 :group 'org-export-e-odt
2143 :version "24.1"
2144 :type 'boolean)
2146 (defcustom org-e-odt-convert-processes
2147 '(("LibreOffice"
2148 "soffice --headless --convert-to %f%x --outdir %d %i")
2149 ("unoconv"
2150 "unoconv -f %f -o %d %i"))
2151 "Specify a list of document converters and their usage.
2152 The converters in this list are offered as choices while
2153 customizing `org-e-odt-convert-process'.
2155 This variable is a list where each element is of the
2156 form (CONVERTER-NAME CONVERTER-CMD). CONVERTER-NAME is the name
2157 of the converter. CONVERTER-CMD is the shell command for the
2158 converter and can contain format specifiers. These format
2159 specifiers are interpreted as below:
2161 %i input file name in full
2162 %I input file name as a URL
2163 %f format of the output file
2164 %o output file name in full
2165 %O output file name as a URL
2166 %d output dir in full
2167 %D output dir as a URL.
2168 %x extra options as set in `org-e-odt-convert-capabilities'."
2169 :group 'org-export-e-odt
2170 :version "24.1"
2171 :type
2172 '(choice
2173 (const :tag "None" nil)
2174 (alist :tag "Converters"
2175 :key-type (string :tag "Converter Name")
2176 :value-type (group (string :tag "Command line")))))
2178 (defcustom org-e-odt-convert-process "LibreOffice"
2179 "Use this converter to convert from \"odt\" format to other formats.
2180 During customization, the list of converter names are populated
2181 from `org-e-odt-convert-processes'."
2182 :group 'org-export-e-odt
2183 :version "24.1"
2184 :type '(choice :convert-widget
2185 (lambda (w)
2186 (apply 'widget-convert (widget-type w)
2187 (eval (car (widget-get w :args)))))
2188 `((const :tag "None" nil)
2189 ,@(mapcar (lambda (c)
2190 `(const :tag ,(car c) ,(car c)))
2191 org-e-odt-convert-processes))))
2193 (defcustom org-e-odt-convert-capabilities
2194 '(("Text"
2195 ("odt" "ott" "doc" "rtf" "docx")
2196 (("pdf" "pdf") ("odt" "odt") ("rtf" "rtf") ("ott" "ott")
2197 ("doc" "doc" ":\"MS Word 97\"") ("docx" "docx") ("html" "html")))
2198 ("Web"
2199 ("html")
2200 (("pdf" "pdf") ("odt" "odt") ("html" "html")))
2201 ("Spreadsheet"
2202 ("ods" "ots" "xls" "csv" "xlsx")
2203 (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv") ("ods" "ods")
2204 ("xls" "xls") ("xlsx" "xlsx")))
2205 ("Presentation"
2206 ("odp" "otp" "ppt" "pptx")
2207 (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("otp" "otp") ("ppt" "ppt")
2208 ("pptx" "pptx") ("odg" "odg"))))
2209 "Specify input and output formats of `org-e-odt-convert-process'.
2210 More correctly, specify the set of input and output formats that
2211 the user is actually interested in.
2213 This variable is an alist where each element is of the
2214 form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
2215 INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
2216 alist where each element is of the form (OUTPUT-FMT
2217 OUTPUT-FILE-EXTENSION EXTRA-OPTIONS).
2219 The variable is interpreted as follows:
2220 `org-e-odt-convert-process' can take any document that is in
2221 INPUT-FMT-LIST and produce any document that is in the
2222 OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
2223 OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
2224 serves dual purposes:
2225 - It is used for populating completion candidates during
2226 `org-e-odt-convert' commands.
2227 - It is used as the value of \"%f\" specifier in
2228 `org-e-odt-convert-process'.
2230 EXTRA-OPTIONS is used as the value of \"%x\" specifier in
2231 `org-e-odt-convert-process'.
2233 DOCUMENT-CLASS is used to group a set of file formats in
2234 INPUT-FMT-LIST in to a single class.
2236 Note that this variable inherently captures how LibreOffice based
2237 converters work. LibreOffice maps documents of various formats
2238 to classes like Text, Web, Spreadsheet, Presentation etc and
2239 allow document of a given class (irrespective of it's source
2240 format) to be converted to any of the export formats associated
2241 with that class.
2243 See default setting of this variable for an typical
2244 configuration."
2245 :group 'org-export-e-odt
2246 :version "24.1"
2247 :type
2248 '(choice
2249 (const :tag "None" nil)
2250 (alist :tag "Capabilities"
2251 :key-type (string :tag "Document Class")
2252 :value-type
2253 (group (repeat :tag "Input formats" (string :tag "Input format"))
2254 (alist :tag "Output formats"
2255 :key-type (string :tag "Output format")
2256 :value-type
2257 (group (string :tag "Output file extension")
2258 (choice
2259 (const :tag "None" nil)
2260 (string :tag "Extra options"))))))))
2262 ;;;; Debugging
2265 ;;;; Document
2267 ;;;; Document Header (Styles)
2269 ;;;; Document Header (Scripts)
2271 ;;;; Document Header (Mathjax)
2273 ;;;; Preamble
2275 ;;;; Postamble
2277 ;;;; Emphasis
2279 ;;;; Todos
2281 ;;;; Tags
2283 ;;;; Timestamps
2284 ;;;; Statistics Cookie
2285 ;;;; Subscript
2286 ;;;; Superscript
2288 ;;;; Inline images
2290 ;;;; Block
2291 ;;;; Comment
2292 ;;;; Comment Block
2293 ;;;; Drawer
2294 ;;;; Dynamic Block
2295 ;;;; Emphasis
2296 ;;;; Entity
2297 ;;;; Example Block
2298 ;;;; Export Snippet
2299 ;;;; Export Block
2300 ;;;; Fixed Width
2301 ;;;; Footnotes
2303 ;;;; Headline
2304 ;;;; Horizontal Rule
2305 ;;;; Inline Babel Call
2306 ;;;; Inline Src Block
2307 ;;;; Inlinetask
2308 ;;;; Item
2309 ;;;; Keyword
2310 ;;;; Latex Environment
2311 ;;;; Latex Fragment
2312 ;;;; Line Break
2313 ;;;; Link
2314 ;;;; Babel Call
2315 ;;;; Macro
2316 ;;;; Paragraph
2317 ;;;; Plain List
2318 ;;;; Plain Text
2319 ;;;; Property Drawer
2320 ;;;; Quote Block
2321 ;;;; Quote Section
2322 ;;;; Section
2323 ;;;; Radio Target
2324 ;;;; Special Block
2325 ;;;; Src Block
2327 ;;;; Table
2329 ;;;; Target
2330 ;;;; Timestamp
2332 ;;;; Verbatim
2333 ;;;; Verse Block
2334 ;;;; Headline
2336 ;;;; Links
2337 ;;;; Drawers
2338 ;;;; Inlinetasks
2339 ;;;; Publishing
2341 ;;;; Compilation
2345 ;;; User Configurable Variables (MAYBE)
2347 ;;;; Preamble
2349 ;;;; Headline
2351 ;;;; Emphasis
2353 (defcustom org-e-odt-format-headline-function nil
2354 "Function to format headline text.
2356 This function will be called with 5 arguments:
2357 TODO the todo keyword \(string or nil\).
2358 TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
2359 PRIORITY the priority of the headline \(integer or nil\)
2360 TEXT the main headline text \(string\).
2361 TAGS the tags string, separated with colons \(string or nil\).
2363 The function result will be used in the section format string.
2365 As an example, one could set the variable to the following, in
2366 order to reproduce the default set-up:
2368 \(defun org-e-odt-format-headline \(todo todo-type priority text tags\)
2369 \"Default format function for an headline.\"
2370 \(concat \(when todo
2371 \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo\)\)
2372 \(when priority
2373 \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
2374 text
2375 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
2376 :group 'org-export-e-odt
2377 :type 'function)
2379 ;;;; Footnotes
2381 ;;;; Timestamps
2383 (defcustom org-e-odt-active-timestamp-format "\\textit{%s}"
2384 "A printf format string to be applied to active timestamps."
2385 :group 'org-export-e-odt
2386 :type 'string)
2388 (defcustom org-e-odt-inactive-timestamp-format "\\textit{%s}"
2389 "A printf format string to be applied to inactive timestamps."
2390 :group 'org-export-e-odt
2391 :type 'string)
2393 (defcustom org-e-odt-diary-timestamp-format "\\textit{%s}"
2394 "A printf format string to be applied to diary timestamps."
2395 :group 'org-export-e-odt
2396 :type 'string)
2399 ;;;; Links
2401 (defcustom org-e-odt-inline-image-rules
2402 '(("file" . "\\.\\(jpeg\\|jpg\\|png\\|gif\\)\\'"))
2403 "Rules characterizing image files that can be inlined into HTML.
2405 A rule consists in an association whose key is the type of link
2406 to consider, and value is a regexp that will be matched against
2407 link's path.
2409 Note that, by default, the image extension *actually* allowed
2410 depend on the way the HTML file is processed. When used with
2411 pdflatex, pdf, jpg and png images are OK. When processing
2412 through dvi to Postscript, only ps and eps are allowed. The
2413 default we use here encompasses both."
2414 :group 'org-export-e-odt
2415 :type '(alist :key-type (string :tag "Type")
2416 :value-type (regexp :tag "Path")))
2418 ;;;; Tables
2420 (defcustom org-e-odt-table-caption-above t
2421 "When non-nil, place caption string at the beginning of the table.
2422 Otherwise, place it near the end."
2423 :group 'org-export-e-odt
2424 :type 'boolean)
2426 ;;;; Drawers
2428 (defcustom org-e-odt-format-drawer-function nil
2429 "Function called to format a drawer in HTML code.
2431 The function must accept two parameters:
2432 NAME the drawer name, like \"LOGBOOK\"
2433 CONTENTS the contents of the drawer.
2435 The function should return the string to be exported.
2437 For example, the variable could be set to the following function
2438 in order to mimic default behaviour:
2440 \(defun org-e-odt-format-drawer-default \(name contents\)
2441 \"Format a drawer element for HTML export.\"
2442 contents\)"
2443 :group 'org-export-e-odt
2444 :type 'function)
2447 ;;;; Inlinetasks
2449 (defcustom org-e-odt-format-inlinetask-function nil
2450 "Function called to format an inlinetask in HTML code.
2452 The function must accept six parameters:
2453 TODO the todo keyword, as a string
2454 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
2455 PRIORITY the inlinetask priority, as a string
2456 NAME the inlinetask name, as a string.
2457 TAGS the inlinetask tags, as a string.
2458 CONTENTS the contents of the inlinetask, as a string.
2460 The function should return the string to be exported.
2462 For example, the variable could be set to the following function
2463 in order to mimic default behaviour:
2465 \(defun org-e-odt-format-inlinetask \(todo type priority name tags contents\)
2466 \"Format an inline task element for HTML export.\"
2467 \(let \(\(full-title
2468 \(concat
2469 \(when todo
2470 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
2471 \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
2472 title
2473 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
2474 \(format \(concat \"\\\\begin{center}\\n\"
2475 \"\\\\fbox{\\n\"
2476 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
2477 \"%s\\n\\n\"
2478 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
2479 \"%s\"
2480 \"\\\\end{minipage}}\"
2481 \"\\\\end{center}\"\)
2482 full-title contents\)\)"
2483 :group 'org-export-e-odt
2484 :type 'function)
2487 ;; Src blocks
2489 ;;;; Plain text
2491 (defcustom org-e-odt-quotes
2492 '(("fr" ("\\(\\s-\\|[[(]\\)\"" . "«~") ("\\(\\S-\\)\"" . "~»") ("\\(\\s-\\|(\\)'" . "'"))
2493 ("en" ("\\(\\s-\\|[[(]\\)\"" . "``") ("\\(\\S-\\)\"" . "''") ("\\(\\s-\\|(\\)'" . "`")))
2494 "Alist for quotes to use when converting english double-quotes.
2496 The CAR of each item in this alist is the language code.
2497 The CDR of each item in this alist is a list of three CONS:
2498 - the first CONS defines the opening quote;
2499 - the second CONS defines the closing quote;
2500 - the last CONS defines single quotes.
2502 For each item in a CONS, the first string is a regexp
2503 for allowed characters before/after the quote, the second
2504 string defines the replacement string for this quote."
2505 :group 'org-export-e-odt
2506 :type '(list
2507 (cons :tag "Opening quote"
2508 (string :tag "Regexp for char before")
2509 (string :tag "Replacement quote "))
2510 (cons :tag "Closing quote"
2511 (string :tag "Regexp for char after ")
2512 (string :tag "Replacement quote "))
2513 (cons :tag "Single quote"
2514 (string :tag "Regexp for char before")
2515 (string :tag "Replacement quote "))))
2518 ;;;; Compilation
2522 ;;; Internal Functions (HTML)
2524 ;; (defun org-e-odt-format-inline-image (path &optional caption label attr)
2525 ;; ;; FIXME: alt text missing here?
2526 ;; (let ((inline-image (format "<img src=\"%s\" alt=\"%s\"/>"
2527 ;; path (file-name-nondirectory path))))
2528 ;; (if (not label) inline-image
2529 ;; (org-e-odt-format-section inline-image "figure" label))))
2531 ;;;; Bibliography
2533 (defun org-e-odt-bibliography ()
2534 "Find bibliography, cut it out and return it."
2535 (catch 'exit
2536 (let (beg end (cnt 1) bib)
2537 (save-excursion
2538 (goto-char (point-min))
2539 (when (re-search-forward
2540 "^[ \t]*<div \\(id\\|class\\)=\"bibliography\"" nil t)
2541 (setq beg (match-beginning 0))
2542 (while (re-search-forward "</?div\\>" nil t)
2543 (setq cnt (+ cnt (if (string= (match-string 0) "<div") +1 -1)))
2544 (when (= cnt 0)
2545 (and (looking-at ">") (forward-char 1))
2546 (setq bib (buffer-substring beg (point)))
2547 (delete-region beg (point))
2548 (throw 'exit bib))))
2549 nil))))
2551 ;;;; Table
2553 (defun org-e-odt-format-table (lines olines)
2554 (let ((org-e-odt-format-table-no-css nil))
2555 (org-lparse-format-table lines olines)))
2557 (defun org-e-odt-splice-attributes (tag attributes)
2558 "Read attributes in string ATTRIBUTES, add and replace in HTML tag TAG."
2559 (if (not attributes)
2561 (let (oldatt newatt)
2562 (setq oldatt (org-extract-attributes-from-string tag)
2563 tag (pop oldatt)
2564 newatt (cdr (org-extract-attributes-from-string attributes)))
2565 (while newatt
2566 (setq oldatt (plist-put oldatt (pop newatt) (pop newatt))))
2567 (if (string-match ">" tag)
2568 (setq tag
2569 (replace-match (concat (org-attributes-to-string oldatt) ">")
2570 t t tag)))
2571 tag)))
2573 (defun org-export-splice-style (style extra)
2574 "Splice EXTRA into STYLE, just before \"</style>\"."
2575 (if (and (stringp extra)
2576 (string-match "\\S-" extra)
2577 (string-match "</style>" style))
2578 (concat (substring style 0 (match-beginning 0))
2579 "\n" extra "\n"
2580 (substring style (match-beginning 0)))
2581 style))
2583 (defun org-e-odt-toc-entry-formatter
2584 (level snumber todo todo-type priority
2585 headline tags target extra-targets extra-class)
2586 (org-e-odt-format-toc-entry snumber todo headline tags target))
2588 (defun org-e-odt-make-string (n string)
2589 (let (out) (dotimes (i n out) (setq out (concat string out)))))
2591 (defun org-e-odt-toc-text (toc-entries)
2592 (let* ((prev-level (1- (nth 1 (car toc-entries))))
2593 (start-level prev-level))
2594 (mapconcat
2595 (lambda (entry)
2596 (let ((headline (nth 0 entry))
2597 (level (nth 1 entry)))
2598 (prog1 (org-e-odt-format-toc-item headline level prev-level)
2599 (setq prev-level level))))
2600 toc-entries "")))
2602 (defun* org-e-odt-format-toc-headline
2603 (todo todo-type priority text tags
2604 &key level section-number headline-label &allow-other-keys)
2605 ;; FIXME
2606 (setq text (concat
2607 (and org-export-with-section-numbers
2608 (concat section-number ". "))
2609 text
2610 (and tags
2611 (concat
2612 (org-e-odt-format-spaces 3)
2613 (org-e-odt-format-fontify tags "tag")))))
2614 (when todo
2615 (setq text (org-e-odt-format-fontify text "todo")))
2617 (let ((org-e-odt-suppress-xref t))
2618 (org-e-odt-format-link text (concat "#" headline-label))))
2620 (defun org-e-odt-toc (depth info)
2621 (assert (wholenump depth))
2622 (let* ((headlines (org-export-collect-headlines info depth))
2623 (toc-entries
2624 (loop for headline in headlines collect
2625 (list (org-e-odt-format-headline--wrap
2626 headline info 'org-e-odt-format-toc-headline)
2627 (org-export-get-relative-level headline info)))))
2628 (when toc-entries
2629 (let* ((lang-specific-heading "Table of Contents")) ; FIXME
2630 (concat
2631 (org-e-odt-begin-toc lang-specific-heading depth)
2632 (org-e-odt-toc-text toc-entries)
2633 (org-e-odt-end-toc))))))
2635 (defun org-e-odt-begin-outline (level1 snumber title tags
2636 target extra-targets extra-class)
2637 (let* ((class (format "outline-%d" level1))
2638 (class (if extra-class (concat class " " extra-class) class))
2639 (id (format "outline-container-%s"
2640 (org-lparse-suffix-from-snumber snumber)))
2641 (extra (concat (when id (format " id=\"%s\"" id))
2642 (when class (format " class=\"%s\"" class)))))
2643 (org-lparse-insert-tag "<div%s>" extra)
2644 (insert
2645 (org-lparse-format 'HEADING
2646 (org-lparse-format
2647 'HEADLINE title extra-targets tags snumber level1)
2648 level1 target))))
2650 (defun org-e-odt-end-outline ()
2651 (org-lparse-insert-tag "</div>"))
2653 (defun org-e-odt-suffix-from-snumber (snumber)
2654 (let* ((snu (replace-regexp-in-string "\\." "-" snumber))
2655 (href (cdr (assoc (concat "sec-" snu)
2656 org-export-preferred-target-alist))))
2657 (org-solidify-link-text (or href snu))))
2659 (defun org-e-odt-format-outline (contents level1 snumber title
2660 tags target extra-targets extra-class)
2663 ;; (defun org-e-odt-format-line (line)
2664 ;; (case org-lparse-dyn-current-environment
2665 ;; ((quote fixedwidth) (concat (org-e-odt-encode-plain-text line) "\n"))
2666 ;; (t (concat line "\n"))))
2668 (defun org-e-odt-fix-class-name (kwd) ; audit callers of this function
2669 "Turn todo keyword into a valid class name.
2670 Replaces invalid characters with \"_\"."
2671 (save-match-data
2672 (while (string-match "[^a-zA-Z0-9_]" kwd)
2673 (setq kwd (replace-match "_" t t kwd))))
2674 kwd)
2676 (defun org-e-odt-format-internal-link (text href &optional extra)
2677 (org-e-odt-format-link text (concat "#" href) extra))
2679 (defun org-e-odt-format-extra-targets (extra-targets)
2680 (if (not extra-targets) ""
2681 (mapconcat (lambda (x)
2682 (when x
2683 (setq x (org-solidify-link-text
2684 (if (org-uuidgen-p x) (concat "ID-" x) x)))
2685 (org-e-odt-format-anchor "" x))) extra-targets "")))
2687 (defun org-e-odt-format-org-tags (tags)
2688 (if (not tags) ""
2689 (org-e-odt-format-fontify
2690 (mapconcat
2691 (lambda (x)
2692 (org-e-odt-format-fontify
2693 x (concat "" ;; org-e-odt-tag-class-prefix
2694 (org-e-odt-fix-class-name x))))
2695 (org-split-string tags ":")
2696 (org-e-odt-format-spaces 1)) "tag")))
2698 (defun org-e-odt-format-section-number (&optional snumber level)
2699 ;; FIXME
2700 (and nil org-export-with-section-numbers
2701 ;; (not org-lparse-body-only)
2702 snumber level
2703 (org-e-odt-format-fontify snumber (format "section-number-%d" level))))
2705 ;; (defun org-e-odt-format-headline (title extra-targets tags
2706 ;; &optional snumber level)
2707 ;; (concat
2708 ;; (org-e-odt-format-extra-targets extra-targets)
2709 ;; (concat (org-e-odt-format-section-number snumber level) " ")
2710 ;; title
2711 ;; (and tags (concat (org-e-odt-format-spaces 3)
2712 ;; (org-e-odt-format-org-tags tags)))))
2714 ;; (defun org-e-odt-format-date (info)
2715 ;; (let ((date (plist-get info :date)))
2716 ;; (cond
2717 ;; ((and date (string-match "%" date))
2718 ;; (format-time-string date))
2719 ;; (date date)
2720 ;; (t (format-time-string "%Y-%m-%d %T %Z")))))
2724 ;;; Internal Functions (Ngz)
2726 (defun org-e-odt--caption/label-string (caption label info)
2727 "Return caption and label HTML string for floats.
2729 CAPTION is a cons cell of secondary strings, the car being the
2730 standard caption and the cdr its short form. LABEL is a string
2731 representing the label. INFO is a plist holding contextual
2732 information.
2734 If there's no caption nor label, return the empty string.
2736 For non-floats, see `org-e-odt--wrap-label'."
2737 (setq label nil) ;; FIXME
2739 (let ((label-str (if label (format "\\label{%s}" label) "")))
2740 (cond
2741 ((and (not caption) (not label)) "")
2742 ((not caption) (format "\\label{%s}\n" label))
2743 ;; Option caption format with short name.
2744 ((cdr caption)
2745 (format "\\caption[%s]{%s%s}\n"
2746 (org-export-secondary-string (cdr caption) info)
2747 label-str
2748 (org-export-secondary-string (car caption) info)))
2749 ;; Standard caption format.
2750 ;; (t (format "\\caption{%s%s}\n"
2751 ;; label-str
2752 ;; (org-export-secondary-string (car caption) info)))
2754 (t (org-export-secondary-string (car caption) info)))))
2756 (defun org-e-odt--find-verb-separator (s)
2757 "Return a character not used in string S.
2758 This is used to choose a separator for constructs like \\verb."
2759 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
2760 (loop for c across ll
2761 when (not (string-match (regexp-quote (char-to-string c)) s))
2762 return (char-to-string c))))
2764 (defun org-e-odt--quotation-marks (text info)
2765 "Export quotation marks depending on language conventions.
2766 TEXT is a string containing quotation marks to be replaced. INFO
2767 is a plist used as a communication channel."
2768 (mapc (lambda(l)
2769 (let ((start 0))
2770 (while (setq start (string-match (car l) text start))
2771 (let ((new-quote (concat (match-string 1 text) (cdr l))))
2772 (setq text (replace-match new-quote t t text))))))
2773 (cdr (or (assoc (plist-get info :language) org-e-odt-quotes)
2774 ;; Falls back on English.
2775 (assoc "en" org-e-odt-quotes))))
2776 text)
2778 (defun org-e-odt--wrap-label (element output)
2779 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
2780 This function shouldn't be used for floats. See
2781 `org-e-odt--caption/label-string'."
2782 ;; (let ((label (org-element-property :name element)))
2783 ;; (if (or (not output) (not label) (string= output "") (string= label ""))
2784 ;; output
2785 ;; (concat (format "\\label{%s}\n" label) output)))
2786 output)
2790 ;;; Transcode Helpers
2792 (defun* org-e-odt-format-headline
2793 (todo todo-type priority text tags
2794 &key level section-number headline-label &allow-other-keys)
2795 (concat (org-e-odt-todo todo) (and todo " ") text
2796 (and tags (org-e-odt-format-spaces 3))
2797 (and tags (org-e-odt-format-org-tags tags))))
2799 ;;;; Src Code
2801 (defun org-e-odt-htmlfontify-string (line)
2802 (let* ((hfy-html-quote-regex "\\([<\"&> ]\\)")
2803 (hfy-html-quote-map '(("\"" "&quot;")
2804 ("<" "&lt;")
2805 ("&" "&amp;")
2806 (">" "&gt;")
2807 (" " "<text:s/>")
2808 (" " "<text:tab/>")))
2809 (hfy-face-to-css 'org-e-odt-hfy-face-to-css)
2810 (hfy-optimisations-1 (copy-seq hfy-optimisations))
2811 (hfy-optimisations (add-to-list 'hfy-optimisations-1
2812 'body-text-only))
2813 (hfy-begin-span-handler
2814 (lambda (style text-block text-id text-begins-block-p)
2815 (insert (format "<text:span text:style-name=\"%s\">" style))))
2816 (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
2817 (htmlfontify-string line)))
2819 (defun org-e-odt-do-format-code
2820 (code &optional lang refs retain-labels num-start)
2821 (let* ((lang (or (assoc-default lang org-src-lang-modes) lang))
2822 (lang-mode (and lang (intern (format "%s-mode" lang))))
2823 (code-lines (org-split-string code "\n"))
2824 (code-length (length code-lines))
2825 (use-htmlfontify-p (and (functionp lang-mode)
2826 org-e-odt-fontify-srcblocks
2827 (require 'htmlfontify nil t)
2828 (fboundp 'htmlfontify-string)))
2829 (code (if (not use-htmlfontify-p) code
2830 (with-temp-buffer
2831 (insert code)
2832 (funcall lang-mode)
2833 (font-lock-fontify-buffer)
2834 (buffer-string))))
2835 (fontifier (if use-htmlfontify-p 'org-e-odt-htmlfontify-string
2836 'org-e-odt-encode-plain-text))
2837 (par-style (if use-htmlfontify-p "OrgSrcBlock"
2838 "OrgFixedWidthBlock"))
2839 (i 0))
2840 (assert (= code-length (length (org-split-string code "\n"))))
2841 (setq code
2842 (org-export-format-code
2843 code
2844 (lambda (loc line-num ref)
2845 (setq par-style
2846 (concat par-style (and (= (incf i) code-length) "LastLine")))
2848 (setq loc (concat loc (and ref retain-labels (format " (%s)" ref))))
2849 (setq loc (funcall fontifier loc))
2850 (when ref
2851 (setq loc (org-e-odt-format-target loc (concat "coderef-" ref))))
2852 (setq loc (org-e-odt-format-stylized-paragraph par-style loc))
2853 (if (not line-num) loc
2854 (org-e-odt-format-tags
2855 '("<text:list-item>" . "</text:list-item>") loc)))
2856 num-start refs))
2857 (cond
2858 ((not num-start) code)
2859 ((equal num-start 0)
2860 (org-e-odt-format-tags
2861 '("<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>"
2862 . "</text:list>") code " text:continue-numbering=\"false\""))
2863 (t (org-e-odt-format-tags
2864 '("<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>"
2865 . "</text:list>") code " text:continue-numbering=\"true\"")))))
2867 (defun org-e-odt-format-code (element info)
2868 (let* ((lang (org-element-property :language element))
2869 ;; Extract code and references.
2870 (code-info (org-export-unravel-code element))
2871 (code (car code-info))
2872 (refs (cdr code-info))
2873 ;; Does the src block contain labels?
2874 (retain-labels (org-element-property :retain-labels element))
2875 ;; Does it have line numbers?
2876 (num-start (case (org-element-property :number-lines element)
2877 (continued (org-export-get-loc element info))
2878 (new 0))))
2879 (org-e-odt-do-format-code code lang refs retain-labels num-start)))
2883 ;;; Template
2885 (defun org-e-odt-template (contents info)
2886 "Return complete document string after HTML conversion.
2887 CONTENTS is the transcoded contents string. RAW-DATA is the
2888 original parsed data. INFO is a plist holding export options."
2889 ;; write meta file
2890 (org-e-odt-update-meta-file info)
2891 (with-temp-buffer
2892 (insert-file-contents
2893 (or org-e-odt-content-template-file
2894 (expand-file-name "OrgOdtContentTemplate.xml"
2895 org-e-odt-styles-dir)))
2896 (goto-char (point-min))
2897 (re-search-forward "</office:text>" nil nil)
2898 (goto-char (match-beginning 0))
2900 ;; Title
2901 (insert (org-e-odt-format-preamble info))
2902 ;; Table of Contents
2903 (let ((depth (plist-get info :with-toc)))
2904 (when (wholenump depth) (insert (org-e-odt-toc depth info))))
2906 ;; Copy styles.xml. Also dump htmlfontify styles, if there is any.
2907 (org-e-odt-update-styles-file info)
2909 ;; Update styles.xml - take care of outline numbering
2910 (with-current-buffer
2911 (find-file-noselect (expand-file-name "styles.xml") t)
2912 ;; Don't make automatic backup of styles.xml file. This setting
2913 ;; prevents the backed-up styles.xml file from being zipped in to
2914 ;; odt file. This is more of a hackish fix. Better alternative
2915 ;; would be to fix the zip command so that the output odt file
2916 ;; includes only the needed files and excludes any auto-generated
2917 ;; extra files like backups and auto-saves etc etc. Note that
2918 ;; currently the zip command zips up the entire temp directory so
2919 ;; that any auto-generated files created under the hood ends up in
2920 ;; the resulting odt file.
2921 (set (make-local-variable 'backup-inhibited) t)
2922 (org-e-odt-configure-outline-numbering))
2924 ;; Contents
2925 (insert contents)
2926 (buffer-substring-no-properties (point-min) (point-max))))
2930 ;;; Transcode Functions
2932 ;;;; Bold
2934 (defun org-e-odt-bold (bold contents info)
2935 "Transcode BOLD from Org to HTML.
2936 CONTENTS is the text with bold markup. INFO is a plist holding
2937 contextual information."
2938 (org-e-odt-format-fontify contents 'bold))
2941 ;;;; Center Block
2943 (defun org-e-odt-center-block (center-block contents info)
2944 "Transcode a CENTER-BLOCK element from Org to HTML.
2945 CONTENTS holds the contents of the center block. INFO is a plist
2946 holding contextual information."
2947 (org-e-odt--wrap-label center-block contents))
2950 ;;;; Clock
2952 (defun org-e-odt-clock (clock contents info)
2953 "Transcode a CLOCK element from Org to HTML.
2954 CONTENTS is nil. INFO is a plist used as a communication
2955 channel."
2956 (org-e-odt-format-fontify
2957 (concat (org-e-odt-format-fontify org-clock-string "timestamp-kwd")
2958 (org-e-odt-format-fontify
2959 (concat (org-translate-time (org-element-property :value clock))
2960 (let ((time (org-element-property :time clock)))
2961 (and time (format " (%s)" time))))
2962 "timestamp"))
2963 "timestamp-wrapper"))
2966 ;;;; Code
2968 (defun org-e-odt-code (code contents info)
2969 "Transcode a CODE object from Org to HTML.
2970 CONTENTS is nil. INFO is a plist used as a communication
2971 channel."
2972 (org-e-odt-format-fontify (org-element-property :value code) 'code))
2975 ;;;; Comment
2977 ;; Comments are ignored.
2980 ;;;; Comment Block
2982 ;; Comment Blocks are ignored.
2985 ;;;; Drawer
2987 (defun org-e-odt-drawer (drawer contents info)
2988 "Transcode a DRAWER element from Org to HTML.
2989 CONTENTS holds the contents of the block. INFO is a plist
2990 holding contextual information."
2991 (let* ((name (org-element-property :drawer-name drawer))
2992 (output (if (functionp org-e-odt-format-drawer-function)
2993 (funcall org-e-odt-format-drawer-function
2994 name contents)
2995 ;; If there's no user defined function: simply
2996 ;; display contents of the drawer.
2997 contents)))
2998 (org-e-odt--wrap-label drawer output)))
3001 ;;;; Dynamic Block
3003 (defun org-e-odt-dynamic-block (dynamic-block contents info)
3004 "Transcode a DYNAMIC-BLOCK element from Org to HTML.
3005 CONTENTS holds the contents of the block. INFO is a plist
3006 holding contextual information. See `org-export-data'."
3007 (org-e-odt--wrap-label dynamic-block contents))
3010 ;;;; Entity
3012 (defun org-e-odt-entity (entity contents info)
3013 "Transcode an ENTITY object from Org to HTML.
3014 CONTENTS are the definition itself. INFO is a plist holding
3015 contextual information."
3016 ;; (let ((ent (org-element-property :latex entity)))
3017 ;; (if (org-element-property :latex-math-p entity)
3018 ;; (format "$%s$" ent)
3019 ;; ent))
3020 (org-element-property :utf-8 entity))
3023 ;;;; Example Block
3025 (defun org-e-odt-example-block (example-block contents info)
3026 "Transcode a EXAMPLE-BLOCK element from Org to HTML.
3027 CONTENTS is nil. INFO is a plist holding contextual information."
3028 (let* ((options (or (org-element-property :options example-block) ""))
3029 (value (org-export-handle-code example-block info nil nil t)))
3030 (org-e-odt--wrap-label
3031 example-block (org-e-odt-format-source-code-or-example value nil))))
3034 ;;;; Export Snippet
3036 (defun org-e-odt-export-snippet (export-snippet contents info)
3037 "Transcode a EXPORT-SNIPPET object from Org to HTML.
3038 CONTENTS is nil. INFO is a plist holding contextual information."
3039 (when (eq (org-export-snippet-backend export-snippet) 'e-odt)
3040 (org-element-property :value export-snippet)))
3043 ;;;; Export Block
3045 (defun org-e-odt-export-block (export-block contents info)
3046 "Transcode a EXPORT-BLOCK element from Org to HTML.
3047 CONTENTS is nil. INFO is a plist holding contextual information."
3048 (when (string= (org-element-property :type export-block) "latex")
3049 (org-remove-indentation (org-element-property :value export-block))))
3052 ;;;; Fixed Width
3054 (defun org-e-odt-fixed-width (fixed-width contents info)
3055 "Transcode a FIXED-WIDTH element from Org to HTML.
3056 CONTENTS is nil. INFO is a plist holding contextual information."
3057 (let* ((value (org-element-normalize-string
3058 (replace-regexp-in-string
3059 "^[ \t]*: ?" ""
3060 (org-element-property :value fixed-width)))))
3061 (org-e-odt--wrap-label
3062 fixed-width (org-e-odt-format-source-code-or-example value nil))))
3065 ;;;; Footnote Definition
3067 ;; Footnote Definitions are ignored.
3070 ;;;; Footnote Reference
3072 (defun org-e-odt-footnote-def (raw info) ; FIXME
3073 (if (equal (org-element-type raw) 'org-data)
3074 (org-trim (org-export-data raw info)) ; fix paragraph style
3075 (org-e-odt-format-stylized-paragraph
3076 'footnote (org-trim (org-export-secondary-string raw info)))))
3078 (defvar org-e-odt-footnote-separator
3079 (org-e-odt-format-fontify "," 'superscript))
3081 (defun org-e-odt-footnote-reference (footnote-reference contents info)
3082 "Transcode a FOOTNOTE-REFERENCE element from Org to HTML.
3083 CONTENTS is nil. INFO is a plist holding contextual information."
3084 (concat
3085 ;; Insert separator between two footnotes in a row.
3086 (let ((prev (org-export-get-previous-element footnote-reference info)))
3087 (when (eq (org-element-type prev) 'footnote-reference)
3088 org-e-odt-footnote-separator))
3089 (cond
3090 ((not (org-export-footnote-first-reference-p footnote-reference info))
3091 (let* ((n (org-export-get-footnote-number footnote-reference info)))
3092 (org-e-odt-format-footnote-reference n "IGNORED" 100)))
3093 ;; Inline definitions are secondary strings.
3094 ((eq (org-element-property :type footnote-reference) 'inline)
3095 (let* ((raw (org-export-get-footnote-definition footnote-reference info))
3096 (n (org-export-get-footnote-number footnote-reference info))
3097 (def (org-e-odt-footnote-def raw info)))
3098 (org-e-odt-format-footnote-reference n def 1)))
3099 ;; Non-inline footnotes definitions are full Org data.
3101 (let* ((raw (org-export-get-footnote-definition footnote-reference info))
3102 (n (org-export-get-footnote-number footnote-reference info))
3103 (def (org-e-odt-footnote-def raw info)))
3104 (org-e-odt-format-footnote-reference n def 1))))))
3107 ;;;; Headline
3109 (defun org-e-odt-todo (todo)
3110 (when todo
3111 (org-e-odt-format-fontify
3112 (concat
3113 "" ; org-e-odt-todo-kwd-class-prefix
3114 (org-e-odt-fix-class-name todo))
3115 (list (if (member todo org-done-keywords) "done" "todo")
3116 todo))))
3118 (defun org-e-odt-format-headline--wrap (headline info
3119 &optional format-function
3120 &rest extra-keys)
3121 "Transcode an HEADLINE element from Org to ODT.
3122 CONTENTS holds the contents of the headline. INFO is a plist
3123 holding contextual information."
3124 (let* ((level (+ (org-export-get-relative-level headline info)))
3125 (headline-number (org-export-get-headline-number headline info))
3126 (section-number (and (org-export-numbered-headline-p headline info)
3127 (mapconcat 'number-to-string
3128 headline-number ".")))
3129 (todo (and (plist-get info :with-todo-keywords)
3130 (let ((todo (org-element-property
3131 :todo-keyword headline)))
3132 (and todo
3133 (org-export-secondary-string todo info)))))
3134 (todo-type (and todo (org-element-property :todo-type headline)))
3135 (priority (and (plist-get info :with-priority)
3136 (org-element-property :priority headline)))
3137 (text (org-export-secondary-string
3138 (org-element-property :title headline) info))
3139 (tags (and (plist-get info :with-tags)
3140 (org-element-property :tags headline)))
3141 (headline-label (concat "sec-" (mapconcat 'number-to-string
3142 headline-number "-")))
3143 (format-function (cond
3144 ((functionp format-function) format-function)
3145 ((functionp org-e-odt-format-headline-function)
3146 (function*
3147 (lambda (todo todo-type priority text tags
3148 &allow-other-keys)
3149 (funcall org-e-odt-format-headline-function
3150 todo todo-type priority text tags))))
3151 (t 'org-e-odt-format-headline))))
3152 (apply format-function
3153 todo todo-type priority text tags
3154 :headline-label headline-label :level level
3155 :section-number section-number extra-keys)))
3157 (defun org-e-odt-headline (headline contents info)
3158 "Transcode an HEADLINE element from Org to HTML.
3159 CONTENTS holds the contents of the headline. INFO is a plist
3160 holding contextual information."
3161 (let* ((numberedp (org-export-numbered-headline-p headline info))
3162 ;; Get level relative to current parsed data.
3163 (level (org-export-get-relative-level headline info))
3164 (text (org-export-secondary-string
3165 (org-element-property :title headline) info))
3166 ;; Create the headline text.
3167 (full-text (org-e-odt-format-headline--wrap headline info)))
3168 (cond
3169 ;; Case 1: This is a footnote section: ignore it.
3170 ((org-element-property :footnote-section-p headline) nil)
3171 ;; Case 2. This is a deep sub-tree: export it as a list item.
3172 ;; Also export as items headlines for which no section
3173 ;; format has been found.
3174 ((org-export-low-level-p headline info) ; FIXME (or (not section-fmt))
3175 ;; Build the real contents of the sub-tree.
3176 (let* ((type (if numberedp 'unordered 'unordered)) ; FIXME
3177 (itemized-body (org-e-odt-format-list-item
3178 contents type nil nil full-text)))
3179 (concat
3180 (and (org-export-first-sibling-p headline info)
3181 (org-e-odt-begin-plain-list type))
3182 itemized-body
3183 (and (org-export-last-sibling-p headline info)
3184 (org-e-odt-end-plain-list type)))))
3185 ;; Case 3. Standard headline. Export it as a section.
3187 (let* ((extra-ids (list (org-element-property :custom-id headline)
3188 (org-element-property :id headline)))
3189 (extra-ids nil) ; FIXME
3190 (id (concat "sec-" (mapconcat 'number-to-string
3191 (org-export-get-headline-number
3192 headline info) "-"))))
3193 (concat
3194 (org-e-odt-format-tags
3195 '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
3196 "</text:h>")
3197 (concat (org-e-odt-format-extra-targets extra-ids)
3198 (if (not id) full-text (org-e-odt-format-target full-text id) ))
3199 level level)
3200 contents))))))
3203 ;;;; Horizontal Rule
3205 (defun org-e-odt-horizontal-rule (horizontal-rule contents info)
3206 "Transcode an HORIZONTAL-RULE object from Org to HTML.
3207 CONTENTS is nil. INFO is a plist holding contextual information."
3208 (let ((attr (mapconcat #'identity
3209 (org-element-property :attr_odt horizontal-rule)
3210 " ")))
3211 (org-e-odt--wrap-label horizontal-rule
3212 (org-e-odt-format-horizontal-line))))
3215 ;;;; Inline Babel Call
3217 ;; Inline Babel Calls are ignored.
3220 ;;;; Inline Src Block
3222 (defun org-e-odt-inline-src-block (inline-src-block contents info)
3223 "Transcode an INLINE-SRC-BLOCK element from Org to HTML.
3224 CONTENTS holds the contents of the item. INFO is a plist holding
3225 contextual information."
3226 (let* ((org-lang (org-element-property :language inline-src-block))
3227 (code (org-element-property :value inline-src-block))
3228 (separator (org-e-odt--find-verb-separator code)))
3229 (error "FIXME")))
3232 ;;;; Inlinetask
3234 (defun org-e-odt-format-section (text class &optional id)
3235 (let ((extra (concat (when id (format " id=\"%s\"" id)))))
3236 (concat (format "<div class=\"%s\"%s>\n" class extra) text "</div>\n")))
3238 (defun org-e-odt-inlinetask (inlinetask contents info)
3239 "Transcode an INLINETASK element from Org to ODT.
3240 CONTENTS holds the contents of the block. INFO is a plist
3241 holding contextual information."
3242 (cond
3243 ;; If `org-e-odt-format-inlinetask-function' is provided, call it
3244 ;; with appropriate arguments.
3245 ((functionp org-e-odt-format-inlinetask-function)
3246 (let ((format-function
3247 (function*
3248 (lambda (todo todo-type priority text tags
3249 &key contents &allow-other-keys)
3250 (funcall org-e-odt-format-inlinetask-function
3251 todo todo-type priority text tags contents)))))
3252 (org-e-odt-format-headline--wrap
3253 inlinetask info format-function :contents contents)))
3254 ;; Otherwise, use a default template.
3255 (t (org-e-odt--wrap-label
3256 inlinetask
3257 (org-e-odt-format-stylized-paragraph
3258 nil (org-e-odt-format-textbox
3259 (concat (org-e-odt-format-stylized-paragraph
3260 "OrgInlineTaskHeading" (org-e-odt-format-headline--wrap
3261 inlinetask info))
3262 contents)
3263 nil nil "OrgInlineTaskFrame" " style:rel-width=\"100%\""))))))
3265 ;;;; Italic
3267 (defun org-e-odt-italic (italic contents info)
3268 "Transcode ITALIC from Org to HTML.
3269 CONTENTS is the text with italic markup. INFO is a plist holding
3270 contextual information."
3271 (org-e-odt-format-fontify contents 'italic))
3274 ;;;; Item
3276 (defun org-e-odt-format-list-item (contents type checkbox
3277 &optional term-counter-id
3278 headline)
3279 (when checkbox
3280 (setq checkbox
3281 (org-e-odt-format-fontify (case checkbox
3282 (on "[X]")
3283 (off "[&nbsp;]")
3284 (trans "[-]")) 'code)))
3285 (concat
3286 (org-e-odt-begin-list-item type term-counter-id headline)
3287 ;; FIXME checkbox (and checkbox " ")
3288 contents
3289 (org-e-odt-end-list-item type)))
3291 (defun org-e-odt-item (item contents info)
3292 "Transcode an ITEM element from Org to HTML.
3293 CONTENTS holds the contents of the item. INFO is a plist holding
3294 contextual information."
3295 ;; Grab `:level' from plain-list properties, which is always the
3296 ;; first element above current item.
3297 (let* ((plain-list (org-export-get-parent item info))
3298 (type (org-element-property :type plain-list))
3299 (level (org-element-property :level plain-list))
3300 (counter (org-element-property :counter item))
3301 (checkbox (org-element-property :checkbox item))
3302 (tag (let ((tag (org-element-property :tag item)))
3303 (and tag (org-export-secondary-string tag info)))))
3304 (org-e-odt-format-list-item
3305 contents type checkbox (or tag counter))))
3308 ;;;; Keyword
3310 (defun org-e-odt-keyword (keyword contents info)
3311 "Transcode a KEYWORD element from Org to HTML.
3312 CONTENTS is nil. INFO is a plist holding contextual information."
3313 (let ((key (org-element-property :key keyword))
3314 (value (org-element-property :value keyword)))
3315 (cond
3316 ((string= key "LATEX") value)
3317 ((string= key "INDEX") (format "\\index{%s}" value))
3318 ((string= key "TARGET") nil ; FIXME
3319 ;; (format "\\label{%s}" (org-export-solidify-link-text value))
3321 ((string= key "toc")
3322 (let ((value (downcase value)))
3323 (cond
3324 ((string-match "\\<headlines\\>" value)
3325 (let ((depth (or (and (string-match "[0-9]+" value)
3326 (string-to-number (match-string 0 value)))
3327 (plist-get info :with-toc))))
3328 (when (wholenump depth) (org-e-odt-toc depth info))))
3329 ((string= "tables" value) "FIXME")
3330 ((string= "figures" value) "FIXME")
3331 ((string= "listings" value)
3332 (cond
3333 ;; At the moment, src blocks with a caption are wrapped
3334 ;; into a figure environment.
3335 (t "FIXME")))))))))
3338 ;;;; Latex Environment
3340 (defun org-e-odt-format-latex (latex-frag processing-type)
3341 (let* ((prefix (case processing-type
3342 (dvipng "ltxpng/")
3343 (mathml "ltxmathml/")))
3344 (cache-relpath
3345 (concat prefix (file-name-sans-extension
3346 (file-name-nondirectory (buffer-file-name)))))
3347 (cache-dir (file-name-directory (buffer-file-name )))
3348 (display-msg (case processing-type
3349 (dvipng "Creating LaTeX Image...")
3350 (mathml "Creating MathML snippet..."))))
3351 (with-temp-buffer
3352 (insert latex-frag)
3353 (org-format-latex cache-relpath cache-dir nil display-msg
3354 nil nil processing-type)
3355 (buffer-string))))
3357 (defun org-e-odt-latex-environment (latex-environment contents info)
3358 "Transcode a LATEX-ENVIRONMENT element from Org to HTML.
3359 CONTENTS is nil. INFO is a plist holding contextual information."
3360 (org-e-odt--wrap-label
3361 latex-environment
3362 (let* ((latex-frag
3363 (org-remove-indentation
3364 (org-element-property :value latex-environment)))
3365 (processing-type (plist-get info :LaTeX-fragments))
3366 (caption (org-element-property :caption latex-environment))
3367 (short-caption (and (cdr caption)
3368 (org-export-secondary-string (cdr caption) info)))
3369 (caption (and (car caption)
3370 (org-export-secondary-string (car caption) info)))
3371 (label (org-element-property :name latex-environment))
3372 (attr nil) ; FIXME
3373 (label (org-element-property :name latex-environment)))
3374 (cond
3375 ((member processing-type '(t mathjax))
3376 (org-e-odt-format-formula latex-environment info))
3377 ((equal processing-type 'dvipng)
3378 (org-e-odt-format-stylized-paragraph
3379 nil (org-e-odt-link--inline-image latex-environment info)))
3380 (t latex-frag)))))
3383 ;;;; Latex Fragment
3386 ;; (when latex-frag ; FIXME
3387 ;; (setq href (org-propertize href :title "LaTeX Fragment"
3388 ;; :description latex-frag)))
3389 ;; handle verbatim
3390 ;; provide descriptions
3392 (defun org-e-odt-latex-fragment (latex-fragment contents info)
3393 "Transcode a LATEX-FRAGMENT object from Org to HTML.
3394 CONTENTS is nil. INFO is a plist holding contextual information."
3395 (let* ((latex-frag (org-element-property :value latex-fragment))
3396 (processing-type (plist-get info :LaTeX-fragments)))
3397 (cond
3398 ((member processing-type '(t mathjax))
3399 (org-e-odt-format-formula latex-fragment info))
3400 ((equal processing-type 'dvipng)
3401 (org-e-odt-link--inline-image latex-fragment info))
3402 (t latex-frag))))
3405 ;;;; Line Break
3407 (defun org-e-odt-line-break (line-break contents info)
3408 "Transcode a LINE-BREAK object from Org to HTML.
3409 CONTENTS is nil. INFO is a plist holding contextual information."
3410 "<text:line-break/>\n")
3413 ;;;; Link
3415 (defun org-e-odt-link--inline-image (element info)
3416 "Return HTML code for an inline image.
3417 LINK is the link pointing to the inline image. INFO is a plist
3418 used as a communication channel."
3419 (let* ((src (cond
3420 ((eq (org-element-type element) 'link)
3421 (let* ((type (org-element-property :type element))
3422 (raw-path (org-element-property :path element)))
3423 (cond ((member type '("http" "https"))
3424 (concat type ":" raw-path))
3425 ((file-name-absolute-p raw-path)
3426 (expand-file-name raw-path))
3427 (t raw-path))))
3428 ((member (org-element-type element)
3429 '(latex-fragment latex-environment))
3430 (let* ((latex-frag (org-remove-indentation
3431 (org-element-property
3432 :value element)))
3433 (formula-link (org-e-odt-format-latex
3434 latex-frag 'dvipng)))
3435 (and formula-link
3436 (string-match "file:\\([^]]*\\)" formula-link)
3437 (match-string 1 formula-link))))
3438 (t (error "what is this?"))))
3439 (href (org-e-odt-format-tags
3440 "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
3441 (org-e-odt-copy-image-file src)))
3442 ;; extract attributes from #+ATTR_ODT line.
3443 (attr-from (case (org-element-type element)
3444 (link (org-export-get-parent-paragraph element info))
3445 (t element)))
3446 ;; convert attributes to a plist.
3447 (attr-plist (org-e-odt-element-attributes attr-from info))
3448 ;; handle `:anchor', `:style' and `:attributes' properties.
3449 (user-frame-anchor
3450 (car (assoc-string (plist-get attr-plist :anchor)
3451 '(("as-char") ("paragraph") ("page")) t)))
3452 (user-frame-style
3453 (and user-frame-anchor (plist-get attr-plist :style)))
3454 (user-frame-attrs
3455 (and user-frame-anchor (plist-get attr-plist :attributes)))
3456 (user-frame-params
3457 (list user-frame-style user-frame-attrs user-frame-anchor))
3458 ;; (embed-as (or embed-as user-frame-anchor "paragraph"))
3459 ;; extrac
3460 ;; handle `:width', `:height' and `:scale' properties.
3461 (size (org-e-odt-image-size-from-file
3462 src (plist-get attr-plist :width)
3463 (plist-get attr-plist :height)
3464 (plist-get attr-plist :scale) nil ;; embed-as
3465 "paragraph" ; FIXME
3467 (width (car size)) (height (cdr size))
3468 (embed-as
3469 (case (org-element-type element)
3470 ((org-e-odt-standalone-image-p element info) "paragraph")
3471 (latex-fragment "as-char")
3472 (latex-environment "paragraph")
3473 (t "paragraph")))
3474 (captions (org-e-odt-format-label element info 'definition))
3475 (caption (car captions)) (short-caption (cdr captions))
3476 (entity (concat (and caption "Captioned") embed-as "Image")))
3477 (org-e-odt-format-entity entity href width height
3478 captions user-frame-params )))
3480 (defun org-e-odt-format-entity (entity href width height &optional
3481 captions user-frame-params)
3482 (let* ((caption (car captions)) (short-caption (cdr captions))
3483 (entity-style (assoc-string entity org-e-odt-entity-frame-styles t))
3484 default-frame-params frame-params)
3485 (cond
3486 ((not caption)
3487 (setq default-frame-params (nth 2 entity-style))
3488 (setq frame-params (org-e-odt-merge-frame-params
3489 default-frame-params user-frame-params))
3490 (apply 'org-e-odt-format-frame href width height frame-params))
3492 (setq default-frame-params (nth 3 entity-style))
3493 (setq frame-params (org-e-odt-merge-frame-params
3494 default-frame-params user-frame-params))
3495 (apply 'org-e-odt-format-textbox
3496 (org-e-odt-format-stylized-paragraph
3497 'illustration
3498 (concat
3499 (apply 'org-e-odt-format-frame href width height
3500 (let ((entity-style-1 (copy-sequence
3501 (nth 2 entity-style))))
3502 (setcar (cdr entity-style-1)
3503 (concat
3504 (cadr entity-style-1)
3505 (and short-caption
3506 (format " draw:name=\"%s\" "
3507 short-caption))))
3508 entity-style-1))
3509 caption))
3510 width height frame-params)))))
3512 (defvar org-e-odt-standalone-image-predicate
3513 (function (lambda (paragraph)
3514 (or (org-element-property :caption paragraph)
3515 (org-element-property :name paragraph)))))
3517 (defun org-e-odt-standalone-image-p (element info &optional predicate)
3518 "Test if ELEMENT is a standalone image for the purpose ODT export.
3519 INFO is a plist holding contextual information.
3521 Return non-nil, if ELEMENT is of type paragraph and it's sole
3522 content, save for whitespaces, is a link that qualifies as an
3523 inline image.
3525 Return non-nil, if ELEMENT is of type link and it's containing
3526 paragraph has no other content save for leading and trailing
3527 whitespaces.
3529 Return nil, otherwise.
3531 Bind `org-e-odt-standalone-image-predicate' to constrain
3532 paragraph further. For example, to check for only captioned
3533 standalone images, do the following.
3535 \(setq org-e-odt-standalone-image-predicate
3536 \(lambda \(paragraph\)
3537 \(org-element-property :caption paragraph\)\)\)
3539 (let ((paragraph (case (org-element-type element)
3540 (paragraph element)
3541 (link (and (org-export-inline-image-p
3542 element org-e-odt-inline-image-rules)
3543 (org-export-get-parent element info)))
3544 (t nil))))
3545 (when paragraph
3546 (assert (eq (org-element-type paragraph) 'paragraph))
3547 (when (or (not (and (boundp 'org-e-odt-standalone-image-predicate)
3548 (functionp org-e-odt-standalone-image-predicate)))
3549 (funcall org-e-odt-standalone-image-predicate paragraph))
3550 (let ((contents (org-element-contents paragraph)))
3551 (loop for x in contents
3552 with inline-image-count = 0
3553 always (cond
3554 ((eq (org-element-type x) 'plain-text)
3555 (not (org-string-nw-p x)))
3556 ((eq (org-element-type x) 'link)
3557 (when (org-export-inline-image-p
3558 x org-e-odt-inline-image-rules)
3559 (= (incf inline-image-count) 1)))
3560 (t nil))))))))
3562 (defun org-e-odt-link (link desc info)
3563 "Transcode a LINK object from Org to HTML.
3565 DESC is the description part of the link, or the empty string.
3566 INFO is a plist holding contextual information. See
3567 `org-export-data'."
3568 (let* ((type (org-element-property :type link))
3569 (raw-path (org-element-property :path link))
3570 ;; Ensure DESC really exists, or set it to nil.
3571 (desc (and (not (string= desc "")) desc))
3572 (imagep (org-export-inline-image-p
3573 link org-e-odt-inline-image-rules))
3574 (path (cond
3575 ((member type '("http" "https" "ftp" "mailto"))
3576 (concat type ":" raw-path))
3577 ((string= type "file")
3578 (when (string-match "\\(.+\\)::.+" raw-path)
3579 (setq raw-path (match-string 1 raw-path)))
3580 (if (file-name-absolute-p raw-path)
3581 (concat "file://" (expand-file-name raw-path))
3582 ;; TODO: Not implemented yet. Concat also:
3583 ;; (org-export-directory :HTML info)
3584 (concat "file://" raw-path)))
3585 (t raw-path)))
3586 protocol)
3587 (cond
3588 ;; Image file.
3589 ((and (not desc) (org-export-inline-image-p
3590 link org-e-odt-inline-image-rules))
3591 (org-e-odt-link--inline-image link info))
3592 ;; Radioed target: Target's name is obtained from original raw
3593 ;; link. Path is parsed and transcoded in order to have a proper
3594 ;; display of the contents.
3595 ((string= type "radio")
3596 (org-e-odt-format-internal-link
3597 (org-export-secondary-string
3598 (org-element-parse-secondary-string
3599 path (org-element-restriction 'radio-target))
3600 info)
3601 (org-export-solidify-link-text path)))
3602 ;; Links pointing to an headline: Find destination and build
3603 ;; appropriate referencing command.
3604 ((member type '("custom-id" "fuzzy" "id"))
3605 (let ((destination (if (string= type "fuzzy")
3606 (org-export-resolve-fuzzy-link link info)
3607 (org-export-resolve-id-link link info))))
3608 (case (org-element-type destination)
3609 ;; Fuzzy link points nowhere.
3610 ('nil
3611 (org-e-odt-format-fontify
3612 (or desc (org-export-secondary-string
3613 (org-element-property :raw-link link) info))
3614 'emphasis))
3615 ;; Fuzzy link points to an invisible target.
3616 (keyword nil)
3617 ;; LINK points to an headline. If headlines are numbered
3618 ;; and the link has no description, display headline's
3619 ;; number. Otherwise, display description or headline's
3620 ;; title.
3621 (headline
3622 (let* ((headline-no (org-export-get-headline-number destination info))
3623 (label (format "sec-%s" (mapconcat 'number-to-string
3624 headline-no "-")))
3625 (section-no (mapconcat 'number-to-string headline-no ".")))
3626 (setq desc
3627 (cond
3628 (desc desc)
3629 ((plist-get info :section-numbers) section-no)
3630 (t (org-export-secondary-string
3631 (org-element-property :title destination) 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 desc))
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 ;;;; Planning
3780 (defun org-e-odt-planning (planning contents info)
3781 "Transcode a PLANNING element from Org to HTML.
3782 CONTENTS is nil. INFO is a plist used as a communication
3783 channel."
3784 (org-e-odt-format-fontify
3785 (concat
3786 (let ((closed (org-element-property :closed planning)))
3787 (when closed
3788 (concat (org-e-odt-format-fontify org-closed-string "timestamp-kwd")
3789 (org-e-odt-format-fontify (org-translate-time closed)
3790 "timestamp"))))
3791 (let ((deadline (org-element-property :deadline planning)))
3792 (when deadline
3793 (concat (org-e-odt-format-fontify org-deadline-string "timestamp-kwd")
3794 (org-e-odt-format-fontify (org-translate-time deadline)
3795 "timestamp"))))
3796 (let ((scheduled (org-element-property :scheduled planning)))
3797 (when scheduled
3798 (concat (org-e-odt-format-fontify org-scheduled-string "timestamp-kwd")
3799 (org-e-odt-format-fontify (org-translate-time scheduled)
3800 "timestamp")))))
3801 "timestamp-wrapper"))
3804 ;;;; Property Drawer
3806 (defun org-e-odt-property-drawer (property-drawer contents info)
3807 "Transcode a PROPERTY-DRAWER element from Org to HTML.
3808 CONTENTS is nil. INFO is a plist holding contextual
3809 information."
3810 ;; The property drawer isn't exported but we want separating blank
3811 ;; lines nonetheless.
3815 ;;;; Quote Block
3817 (defun org-e-odt-quote-block (quote-block contents info)
3818 "Transcode a QUOTE-BLOCK element from Org to HTML.
3819 CONTENTS holds the contents of the block. INFO is a plist
3820 holding contextual information."
3821 (org-e-odt--wrap-label quote-block contents))
3824 ;;;; Quote Section
3826 (defun org-e-odt-quote-section (quote-section contents info)
3827 "Transcode a QUOTE-SECTION element from Org to HTML.
3828 CONTENTS is nil. INFO is a plist holding contextual information."
3829 (let ((value (org-remove-indentation
3830 (org-element-property :value quote-section))))
3831 (when value (org-e-odt-format-source-code-or-example value nil))))
3834 ;;;; Section
3836 (defun org-e-odt-section (section contents info) ; FIXME
3837 "Transcode a SECTION element from Org to HTML.
3838 CONTENTS holds the contents of the section. INFO is a plist
3839 holding contextual information."
3840 contents)
3842 ;;;; Radio Target
3844 (defun org-e-odt-radio-target (radio-target text info)
3845 "Transcode a RADIO-TARGET object from Org to HTML.
3846 TEXT is the text of the target. INFO is a plist holding
3847 contextual information."
3848 (org-e-odt-format-anchor
3849 text (org-export-solidify-link-text
3850 (org-element-property :value radio-target))))
3853 ;;;; Special Block
3855 (defun org-e-odt-special-block (special-block contents info)
3856 "Transcode a SPECIAL-BLOCK element from Org to HTML.
3857 CONTENTS holds the contents of the block. INFO is a plist
3858 holding contextual information."
3859 (let ((type (downcase (org-element-property :type special-block))))
3860 (org-e-odt--wrap-label
3861 special-block
3862 (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
3865 ;;;; Src Block
3867 (defun org-e-odt-src-block (src-block contents info)
3868 "Transcode a SRC-BLOCK element from Org to HTML.
3869 CONTENTS holds the contents of the item. INFO is a plist holding
3870 contextual information."
3871 (let* ((lang (org-element-property :language src-block))
3872 (caption (org-element-property :caption src-block))
3873 (short-caption (and (cdr caption)
3874 (org-export-secondary-string (cdr caption) info)))
3875 (caption (and (car caption)
3876 (org-export-secondary-string (car caption) info)))
3877 (label (org-element-property :name src-block)))
3878 ;; FIXME: Handle caption
3879 ;; caption-str (when caption)
3880 ;; (main (org-export-secondary-string (car caption) info))
3881 ;; (secondary (org-export-secondary-string (cdr caption) info))
3882 ;; (caption-str (org-e-odt--caption/label-string caption label info))
3883 (let* ((captions (org-e-odt-format-label src-block info 'definition))
3884 (caption (car captions)) (short-caption (cdr captions)))
3885 (concat
3886 (and caption (org-e-odt-format-stylized-paragraph 'listing caption))
3887 (org-e-odt-format-code src-block info)))))
3890 ;;;; Statistics Cookie
3892 (defun org-e-odt-statistics-cookie (statistics-cookie contents info)
3893 "Transcode a STATISTICS-COOKIE object from Org to HTML.
3894 CONTENTS is nil. INFO is a plist holding contextual information."
3895 (let ((cookie-value (org-element-property :value statistics-cookie)))
3896 (org-e-odt-format-fontify cookie-value 'code)))
3899 ;;;; Strike-Through
3901 (defun org-e-odt-strike-through (strike-through contents info)
3902 "Transcode STRIKE-THROUGH from Org to HTML.
3903 CONTENTS is the text with strike-through markup. INFO is a plist
3904 holding contextual information."
3905 (org-e-odt-format-fontify contents 'strike))
3908 ;;;; Subscript
3910 (defun org-e-odt-subscript (subscript contents info)
3911 "Transcode a SUBSCRIPT object from Org to HTML.
3912 CONTENTS is the contents of the object. INFO is a plist holding
3913 contextual information."
3914 ;; (format (if (= (length contents) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents)
3915 (org-e-odt-format-fontify contents 'subscript))
3918 ;;;; Superscript
3920 (defun org-e-odt-superscript (superscript contents info)
3921 "Transcode a SUPERSCRIPT object from Org to HTML.
3922 CONTENTS is the contents of the object. INFO is a plist holding
3923 contextual information."
3924 ;; (format (if (= (length contents) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents)
3925 (org-e-odt-format-fontify contents 'superscript))
3928 ;;;; Table Cell
3930 (defun org-e-odt-table-style-spec (element info)
3931 (let* ((table (org-export-get-parent-table element info))
3932 (table-attributes (org-e-odt-element-attributes table info))
3933 (table-style (plist-get table-attributes :style)))
3934 (assoc table-style org-e-odt-table-styles)))
3936 (defun org-e-odt-get-table-cell-styles (table-cell info)
3937 "Retrieve styles applicable to a table cell.
3938 R and C are (zero-based) row and column numbers of the table
3939 cell. STYLE-SPEC is an entry in `org-e-odt-table-styles'
3940 applicable to the current table. It is `nil' if the table is not
3941 associated with any style attributes.
3943 Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
3945 When STYLE-SPEC is nil, style the table cell the conventional way
3946 - choose cell borders based on row and column groupings and
3947 choose paragraph alignment based on `org-col-cookies' text
3948 property. See also
3949 `org-e-odt-get-paragraph-style-cookie-for-table-cell'.
3951 When STYLE-SPEC is non-nil, ignore the above cookie and return
3952 styles congruent with the ODF-1.2 specification."
3953 (let* ((table-cell-address (org-export-table-cell-address table-cell info))
3954 (r (car table-cell-address)) (c (cdr table-cell-address))
3955 (style-spec (org-e-odt-table-style-spec table-cell info))
3956 (table-dimensions (org-export-table-dimensions
3957 (org-export-get-parent-table table-cell info)
3958 info)))
3959 (when style-spec
3960 ;; LibreOffice - particularly the Writer - honors neither table
3961 ;; templates nor custom table-cell styles. Inorder to retain
3962 ;; inter-operability with LibreOffice, only automatic styles are
3963 ;; used for styling of table-cells. The current implementation is
3964 ;; congruent with ODF-1.2 specification and hence is
3965 ;; future-compatible.
3967 ;; Additional Note: LibreOffice's AutoFormat facility for tables -
3968 ;; which recognizes as many as 16 different cell types - is much
3969 ;; richer. Unfortunately it is NOT amenable to easy configuration
3970 ;; by hand.
3971 (let* ((template-name (nth 1 style-spec))
3972 (cell-style-selectors (nth 2 style-spec))
3973 (cell-type
3974 (cond
3975 ((and (cdr (assoc 'use-first-column-styles cell-style-selectors))
3976 (= c 0)) "FirstColumn")
3977 ((and (cdr (assoc 'use-last-column-styles cell-style-selectors))
3978 (= (1+ c) (cdr table-dimensions)))
3979 "LastColumn")
3980 ((and (cdr (assoc 'use-first-row-styles cell-style-selectors))
3981 (= r 0)) "FirstRow")
3982 ((and (cdr (assoc 'use-last-row-styles cell-style-selectors))
3983 (= (1+ r) (car table-dimensions)))
3984 "LastRow")
3985 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
3986 (= (% r 2) 1)) "EvenRow")
3987 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
3988 (= (% r 2) 0)) "OddRow")
3989 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
3990 (= (% c 2) 1)) "EvenColumn")
3991 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
3992 (= (% c 2) 0)) "OddColumn")
3993 (t ""))))
3994 (concat template-name cell-type)))))
3996 (defun org-e-odt-table-cell (table-cell contents info)
3997 "Transcode a TABLE-CELL element from Org to ODT.
3998 CONTENTS is nil. INFO is a plist used as a communication
3999 channel."
4000 (let* ((table-cell-address (org-export-table-cell-address table-cell info))
4001 (r (car table-cell-address))
4002 (c (cdr table-cell-address))
4003 (horiz-span (or (org-export-table-cell-width table-cell info) 0))
4004 (table-row (org-export-get-parent table-cell info))
4005 (custom-style-prefix (org-e-odt-get-table-cell-styles
4006 table-cell info))
4007 (paragraph-style
4009 (and custom-style-prefix
4010 (format "%sTableParagraph" custom-style-prefix))
4011 (concat
4012 (cond
4013 ((and (= 1 (org-export-table-row-group table-row info))
4014 (org-export-table-has-header-p
4015 (org-export-get-parent-table table-row info) info))
4016 "OrgTableHeading")
4017 ((and (zerop c) t ;; (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS)
4019 "OrgTableHeading")
4020 (t "OrgTableContents"))
4021 (capitalize (symbol-name (org-export-table-cell-alignment
4022 table-cell info))))))
4023 (cell-style-name
4025 (and custom-style-prefix (format "%sTableCell"
4026 custom-style-prefix))
4027 (concat
4028 "OrgTblCell"
4029 (when (or (org-export-table-row-starts-rowgroup-p table-row info)
4030 (zerop r)) "T")
4031 (when (org-export-table-row-ends-rowgroup-p table-row info) "B")
4032 (when (and (org-export-table-cell-starts-colgroup-p table-cell info)
4033 (not (zerop c)) ) "L"))))
4034 (cell-attributes
4035 (concat
4036 (format " table:style-name=\"%s\"" cell-style-name)
4037 (and (> horiz-span 0)
4038 (format " table:number-columns-spanned=\"%d\""
4039 (1+ horiz-span))))))
4040 (unless contents (setq contents ""))
4041 (concat
4042 (org-e-odt-format-tags
4043 '("<table:table-cell%s>" . "</table:table-cell>")
4044 (org-e-odt-format-stylized-paragraph paragraph-style contents)
4045 cell-attributes)
4046 (let (s)
4047 (dotimes (i horiz-span s)
4048 (setq s (concat s "\n<table:covered-table-cell/>"))))
4049 "\n")))
4052 ;;;; Table Row
4054 (defun org-e-odt-table-row (table-row contents info)
4055 "Transcode a TABLE-ROW element from Org to ODT.
4056 CONTENTS is the contents of the row. INFO is a plist used as a
4057 communication channel."
4058 ;; Rules are ignored since table separators are deduced from
4059 ;; borders of the current row.
4060 (when (eq (org-element-property :type table-row) 'standard)
4061 (let* ((rowgroup-tags
4062 (if (and (= 1 (org-export-table-row-group table-row info))
4063 (org-export-table-has-header-p
4064 (org-export-get-parent-table table-row info) info))
4065 ;; If the row belongs to the first rowgroup and the
4066 ;; table has more than one row groups, then this row
4067 ;; belongs to the header row group.
4068 '("\n<table:table-header-rows>" . "\n</table:table-header-rows>")
4069 ;; Otherwise, it belongs to non-header row group.
4070 '("\n<table:table-rows>" . "\n</table:table-rows>"))))
4071 (concat
4072 ;; Does this row begin a rowgroup?
4073 (when (org-export-table-row-starts-rowgroup-p table-row info)
4074 (car rowgroup-tags))
4075 ;; Actual table row
4076 (org-e-odt-format-tags
4077 '("<table:table-row>" . "</table:table-row>") contents)
4078 ;; Does this row end a rowgroup?
4079 (when (org-export-table-row-ends-rowgroup-p table-row info)
4080 (cdr rowgroup-tags))))))
4083 ;;;; Table
4085 (defun org-e-odt-table-first-row-data-cells (table info)
4086 (let ((table-row
4087 (org-element-map
4088 table 'table-row
4089 (lambda (row)
4090 (unless (eq (org-element-property :type row) 'rule) row))
4091 info 'first-match))
4092 (special-column-p (org-export-table-has-special-column-p table)))
4093 (if (not special-column-p) (org-element-contents table-row)
4094 (cdr (org-element-contents table-row)))))
4096 (defun org-e-odt-table (table contents info)
4097 "Transcode a TABLE element from Org to HTML.
4098 CONTENTS is nil. INFO is a plist holding contextual information."
4099 (case (org-element-property :type table)
4100 (table.el nil)
4102 (let* ((captions (org-e-odt-format-label table info 'definition))
4103 (caption (car captions)) (short-caption (cdr captions))
4104 (attributes (org-e-odt-element-attributes table info))
4105 (custom-table-style (nth 1 (org-e-odt-table-style-spec table info)))
4106 (table-column-specs
4107 (function
4108 (lambda (table info)
4109 (let* ((table-style (or custom-table-style "OrgTable"))
4110 (column-style (format "%sColumn" table-style)))
4111 (mapconcat
4112 (lambda (table-cell)
4113 (let ((width (1+ (or (org-export-table-cell-width
4114 table-cell info) 0))))
4115 (org-e-odt-make-string
4116 width
4117 (org-e-odt-format-tags
4118 "<table:table-column table:style-name=\"%s\"/>"
4119 "" column-style))))
4120 (org-e-odt-table-first-row-data-cells table info) "\n"))))))
4121 (concat
4122 ;; caption.
4123 (when caption (org-e-odt-format-stylized-paragraph 'table caption))
4124 ;; begin table.
4125 (let* ((automatic-name
4126 (org-e-odt-add-automatic-style "Table" attributes)))
4127 (format
4128 "\n<table:table table:name=\"%s\" table:style-name=\"%s\">\n"
4129 (or short-caption (car automatic-name))
4130 (or custom-table-style (cdr automatic-name) "OrgTable")))
4131 ;; column specification.
4132 (funcall table-column-specs table info)
4133 ;; actual contents.
4134 "\n" contents
4135 ;; end table.
4136 "</table:table>")))))
4139 ;;;; Target
4141 (defun org-e-odt-target (target contents info)
4142 "Transcode a TARGET object from Org to HTML.
4143 CONTENTS is nil. INFO is a plist holding contextual
4144 information."
4145 (org-e-odt-format-anchor
4146 "" (org-export-solidify-link-text (org-element-property :value target))))
4149 ;;;; Timestamp
4151 (defun org-e-odt-timestamp (timestamp contents info)
4152 "Transcode a TIMESTAMP object from Org to HTML.
4153 CONTENTS is nil. INFO is a plist used as a communication
4154 channel."
4155 (org-e-odt-format-fontify
4156 (org-e-odt-format-fontify
4157 (org-translate-time (org-element-property :value timestamp))
4158 "timestamp")
4159 "timestamp-wrapper"))
4162 ;;;; Underline
4164 (defun org-e-odt-underline (underline contents info)
4165 "Transcode UNDERLINE from Org to HTML.
4166 CONTENTS is the text with underline markup. INFO is a plist
4167 holding contextual information."
4168 (org-e-odt-format-fontify contents 'underline))
4171 ;;;; Verbatim
4173 (defun org-e-odt-verbatim (verbatim contents info)
4174 "Transcode a VERBATIM object from Org to HTML.
4175 CONTENTS is nil. INFO is a plist used as a communication
4176 channel."
4177 (org-e-odt-format-fontify (org-element-property :value verbatim) 'verbatim))
4180 ;;;; Verse Block
4182 (defun org-e-odt-verse-block (verse-block contents info)
4183 "Transcode a VERSE-BLOCK element from Org to HTML.
4184 CONTENTS is verse block contents. INFO is a plist holding
4185 contextual information."
4186 ;; Replace each newline character with line break. Also replace
4187 ;; each blank line with a line break.
4188 (setq contents (replace-regexp-in-string
4189 "^ *\\\\\\\\$" "<br/>\n"
4190 (replace-regexp-in-string
4191 "\\(\\\\\\\\\\)?[ \t]*\n" " <br/>\n" contents)))
4193 ;; Replace each white space at beginning of a line with a
4194 ;; non-breaking space.
4195 (while (string-match "^[ \t]+" contents)
4196 (let ((new-str (org-e-odt-format-spaces
4197 (length (match-string 0 contents)))))
4198 (setq contents (replace-match new-str nil t contents))))
4200 (org-e-odt--wrap-label
4201 verse-block (format "<p class=\"verse\">\n%s</p>" contents)))
4206 ;;; Filter Functions
4208 ;;;; Filter Settings
4209 ;;;; Filters
4211 ;;; Interactive functions
4213 (defun org-e-odt-export-to-odt
4214 (&optional subtreep visible-only body-only ext-plist pub-dir)
4215 "Export current buffer to a HTML file.
4217 If narrowing is active in the current buffer, only export its
4218 narrowed part.
4220 If a region is active, export that region.
4222 When optional argument SUBTREEP is non-nil, export the sub-tree
4223 at point, extracting information from the headline properties
4224 first.
4226 When optional argument VISIBLE-ONLY is non-nil, don't export
4227 contents of hidden elements.
4229 When optional argument BODY-ONLY is non-nil, only write code
4230 between \"\\begin{document}\" and \"\\end{document}\".
4232 EXT-PLIST, when provided, is a property list with external
4233 parameters overriding Org default settings, but still inferior to
4234 file-local settings.
4236 When optional argument PUB-DIR is set, use it as the publishing
4237 directory.
4239 Return output file's name."
4240 (interactive)
4241 (setq debug-on-error t)
4243 ;; (let* ((outfile (org-export-output-file-name ".html" subtreep pub-dir))
4244 ;; (outfile "content.xml"))
4245 ;; (org-export-to-file
4246 ;; 'e-odt outfile subtreep visible-only body-only ext-plist))
4248 (let* ((outbuf (org-e-odt-init-outfile))
4249 (target (org-export-output-file-name ".odt" subtreep pub-dir))
4250 (outdir (file-name-directory (buffer-file-name outbuf)))
4251 (default-directory outdir))
4253 ;; FIXME: for copying embedded images
4254 (setq org-current-export-file
4255 (file-name-directory
4256 (org-export-output-file-name ".odt" subtreep nil)))
4258 (org-export-to-buffer
4259 'e-odt outbuf
4260 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))
4262 (setq org-lparse-opt-plist nil) ; FIXME
4263 (org-e-odt-save-as-outfile target ;; info
4267 ;; return outfile
4268 (if (not org-e-odt-preferred-output-format) target
4269 (or (org-e-odt-convert target org-e-odt-preferred-output-format)
4270 target))))
4276 (defun org-e-odt-reachable-p (in-fmt out-fmt)
4277 "Return non-nil if IN-FMT can be converted to OUT-FMT."
4278 (catch 'done
4279 (let ((reachable-formats (org-e-odt-do-reachable-formats in-fmt)))
4280 (dolist (e reachable-formats)
4281 (let ((out-fmt-spec (assoc out-fmt (cdr e))))
4282 (when out-fmt-spec
4283 (throw 'done (cons (car e) out-fmt-spec))))))))
4285 (defun org-e-odt-do-convert (in-file out-fmt &optional prefix-arg)
4286 "Workhorse routine for `org-e-odt-convert'."
4287 (require 'browse-url)
4288 (let* ((in-file (expand-file-name (or in-file buffer-file-name)))
4289 (dummy (or (file-readable-p in-file)
4290 (error "Cannot read %s" in-file)))
4291 (in-fmt (file-name-extension in-file))
4292 (out-fmt (or out-fmt (error "Output format unspecified")))
4293 (how (or (org-e-odt-reachable-p in-fmt out-fmt)
4294 (error "Cannot convert from %s format to %s format?"
4295 in-fmt out-fmt)))
4296 (convert-process (car how))
4297 (out-file (concat (file-name-sans-extension in-file) "."
4298 (nth 1 (or (cdr how) out-fmt))))
4299 (extra-options (or (nth 2 (cdr how)) ""))
4300 (out-dir (file-name-directory in-file))
4301 (cmd (format-spec convert-process
4302 `((?i . ,(shell-quote-argument in-file))
4303 (?I . ,(browse-url-file-url in-file))
4304 (?f . ,out-fmt)
4305 (?o . ,out-file)
4306 (?O . ,(browse-url-file-url out-file))
4307 (?d . , (shell-quote-argument out-dir))
4308 (?D . ,(browse-url-file-url out-dir))
4309 (?x . ,extra-options)))))
4310 (when (file-exists-p out-file)
4311 (delete-file out-file))
4313 (message "Executing %s" cmd)
4314 (let ((cmd-output (shell-command-to-string cmd)))
4315 (message "%s" cmd-output))
4317 (cond
4318 ((file-exists-p out-file)
4319 (message "Exported to %s" out-file)
4320 (when prefix-arg
4321 (message "Opening %s..." out-file)
4322 (org-open-file out-file))
4323 out-file)
4325 (message "Export to %s failed" out-file)
4326 nil))))
4328 (defun org-e-odt-do-reachable-formats (in-fmt)
4329 "Return verbose info about formats to which IN-FMT can be converted.
4330 Return a list where each element is of the
4331 form (CONVERTER-PROCESS . OUTPUT-FMT-ALIST). See
4332 `org-e-odt-convert-processes' for CONVERTER-PROCESS and see
4333 `org-e-odt-convert-capabilities' for OUTPUT-FMT-ALIST."
4334 (let* ((converter
4335 (and org-e-odt-convert-process
4336 (cadr (assoc-string org-e-odt-convert-process
4337 org-e-odt-convert-processes t))))
4338 (capabilities
4339 (and org-e-odt-convert-process
4340 (cadr (assoc-string org-e-odt-convert-process
4341 org-e-odt-convert-processes t))
4342 org-e-odt-convert-capabilities))
4343 reachable-formats)
4344 (when converter
4345 (dolist (c capabilities)
4346 (when (member in-fmt (nth 1 c))
4347 (push (cons converter (nth 2 c)) reachable-formats))))
4348 reachable-formats))
4350 (defun org-e-odt-reachable-formats (in-fmt)
4351 "Return list of formats to which IN-FMT can be converted.
4352 The list of the form (OUTPUT-FMT-1 OUTPUT-FMT-2 ...)."
4353 (let (l)
4354 (mapc (lambda (e) (add-to-list 'l e))
4355 (apply 'append (mapcar
4356 (lambda (e) (mapcar 'car (cdr e)))
4357 (org-e-odt-do-reachable-formats in-fmt))))
4360 (defun org-e-odt-convert-read-params ()
4361 "Return IN-FILE and OUT-FMT params for `org-e-odt-do-convert'.
4362 This is a helper routine for interactive use."
4363 (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
4364 (in-file (read-file-name "File to be converted: "
4365 nil buffer-file-name t))
4366 (in-fmt (file-name-extension in-file))
4367 (out-fmt-choices (org-e-odt-reachable-formats in-fmt))
4368 (out-fmt
4369 (or (and out-fmt-choices
4370 (funcall input "Output format: "
4371 out-fmt-choices nil nil nil))
4372 (error
4373 "No known converter or no known output formats for %s files"
4374 in-fmt))))
4375 (list in-file out-fmt)))
4377 ;;;###autoload
4378 (defun org-e-odt-convert (&optional in-file out-fmt prefix-arg)
4379 "Convert IN-FILE to format OUT-FMT using a command line converter.
4380 IN-FILE is the file to be converted. If unspecified, it defaults
4381 to variable `buffer-file-name'. OUT-FMT is the desired output
4382 format. Use `org-e-odt-convert-process' as the converter.
4383 If PREFIX-ARG is non-nil then the newly converted file is opened
4384 using `org-open-file'."
4385 (interactive
4386 (append (org-e-odt-convert-read-params) current-prefix-arg))
4387 (org-e-odt-do-convert in-file out-fmt prefix-arg))
4389 ;;; FIXMES, TODOS, FOR REVIEW etc
4391 ;; (defun org-e-odt-discontinue-list ()
4392 ;; (let ((stashed-stack org-lparse-list-stack))
4393 ;; (loop for list-type in stashed-stack
4394 ;; do (org-lparse-end-list-item-1 list-type)
4395 ;; (org-lparse-end-list list-type))
4396 ;; (setq org-e-odt-list-stack-stashed stashed-stack)))
4398 ;; (defun org-e-odt-continue-list ()
4399 ;; (setq org-e-odt-list-stack-stashed (nreverse org-e-odt-list-stack-stashed))
4400 ;; (loop for list-type in org-e-odt-list-stack-stashed
4401 ;; do (org-lparse-begin-list list-type)
4402 ;; (org-lparse-begin-list-item list-type)))
4404 ;; FIXME: Begin indented table
4405 ;; (setq org-e-odt-table-indentedp (not (null org-lparse-list-stack)))
4406 ;; (setq org-e-odt-table-indentedp nil) ; FIXME
4407 ;; (when org-e-odt-table-indentedp
4408 ;; ;; Within the Org file, the table is appearing within a list item.
4409 ;; ;; OpenDocument doesn't allow table to appear within list items.
4410 ;; ;; Temporarily terminate the list, emit the table and then
4411 ;; ;; re-continue the list.
4412 ;; (org-e-odt-discontinue-list)
4413 ;; ;; Put the Table in an indented section.
4414 ;; (let ((level (length org-e-odt-list-stack-stashed)))
4415 ;; (org-e-odt-begin-section (format "OrgIndentedSection-Level-%d" level))))
4417 ;; FIXME: End indented table
4418 ;; (when org-e-odt-table-indentedp
4419 ;; (org-e-odt-end-section)
4420 ;; (org-e-odt-continue-list))
4423 ;;;; org-format-table-html
4424 ;;;; org-format-org-table-html
4425 ;;;; org-format-table-table-html
4426 ;;;; org-table-number-fraction
4427 ;;;; org-table-number-regexp
4428 ;;;; org-e-odt-table-caption-above
4430 ;;;; org-whitespace
4431 ;;;; "<span style=\"visibility:hidden;\">%s</span>"
4432 ;;;; Remove display properties
4434 ;;;; org-e-odt-with-timestamp
4435 ;;;; org-e-odt-html-helper-timestamp
4437 ;;;; org-export-as-html-and-open
4438 ;;;; org-export-as-html-batch
4439 ;;;; org-export-as-html-to-buffer
4440 ;;;; org-replace-region-by-html
4441 ;;;; org-export-region-as-html
4442 ;;;; org-export-as-html
4444 ;;;; (org-export-directory :html opt-plist)
4445 ;;;; (plist-get opt-plist :html-extension)
4446 ;;;; org-e-odt-toplevel-hlevel
4447 ;;;; org-e-odt-inline-image-extensions
4448 ;;;; org-e-odt-protect-char-alist
4449 ;;;; org-e-odt-table-use-header-tags-for-first-column
4450 ;;;; org-e-odt-todo-kwd-class-prefix
4451 ;;;; org-e-odt-tag-class-prefix
4452 ;;;; org-e-odt-footnote-separator
4455 ;;; Library Initializations
4457 (mapc
4458 (lambda (desc)
4459 ;; Let Org open all OpenDocument files using system-registered app
4460 (add-to-list 'org-file-apps
4461 (cons (concat "\\." (car desc) "\\'") 'system))
4462 ;; Let Emacs open all OpenDocument files in archive mode
4463 (add-to-list 'auto-mode-alist
4464 (cons (concat "\\." (car desc) "\\'") 'archive-mode)))
4465 org-e-odt-file-extensions)
4467 (defvar org-e-odt-display-outline-level 2)
4468 (defun org-e-odt-enumerate-element (element info &optional predicate n)
4469 (let* ((numbered-parent-headline-at-<=-n
4470 (function
4471 (lambda (element n info)
4472 (loop for x in (org-export-get-genealogy element info)
4473 thereis (and (eq (org-element-type x) 'headline)
4474 (<= (org-export-get-relative-level x info) n)
4475 (org-export-numbered-headline-p x info)
4476 x)))))
4477 (enumerate
4478 (function
4479 (lambda (element scope info &optional predicate)
4480 (let ((counter 0))
4481 (org-element-map
4482 (or scope (plist-get info :parse-tree))
4483 (org-element-type element)
4484 (lambda (el)
4485 (and (or (not predicate) (funcall predicate el info))
4486 (incf counter)
4487 (equal element el)
4488 counter))
4489 info 'first-match)))))
4490 (scope (funcall numbered-parent-headline-at-<=-n
4491 element (or n org-e-odt-display-outline-level) info))
4492 (ordinal (funcall enumerate element scope info predicate))
4493 (tag
4494 (concat
4495 ;; section number
4496 (and scope
4497 (mapconcat 'number-to-string
4498 (org-export-get-headline-number scope info) "."))
4499 ;; separator
4500 (and scope ".")
4501 ;; ordinal
4502 (number-to-string ordinal))))
4503 ;; (message "%s:\t%s" (org-element-property :name element) tag)
4504 tag))
4506 (provide 'org-e-odt)
4508 ;;; org-e-odt.el ends here