org-e-odt: Few cleanups
[org-mode.git] / EXPERIMENTAL / org-e-odt.el
blobfc768781bd8b223cbc8272ea2bb498afe52e6108
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 (defgroup org-export-e-odt nil
31 "Options specific for ODT export of Org-mode files."
32 :tag "Org Export ODT"
33 :group 'org-export
34 :version "24.1")
36 ;; FIXMES
37 ;; org-export-preprocess-after-blockquote-hook
38 ;; org-export-e-odt-preprocess-latex-fragments
39 ;; org-export-as-e-odt-and-open
40 ;; org-export-as-e-odt-batch
41 ;; org-export-as-e-odt
43 (defun org-e-odt-get-style-name-for-entity (category &optional entity)
44 (let ((entity (or entity 'default)))
45 (or
46 (cdr (assoc entity (cdr (assoc category
47 org-export-e-odt-org-styles-alist))))
48 (cdr (assoc entity (cdr (assoc category
49 org-export-e-odt-default-org-styles-alist))))
50 (error "Cannot determine style name for entity %s of type %s"
51 entity category))))
54 ;; Following variable is let bound when `org-do-lparse' is in
55 ;; progress. See org-html.el.
57 (defun org-e-odt-format-preamble (info)
58 (let* ((title (plist-get info :title))
59 (author (plist-get info :author))
60 (date (plist-get info :date))
61 (iso-date (org-e-odt-format-date date))
62 (date (org-e-odt-format-date date "%d %b %Y"))
63 (email (plist-get info :email))
64 ;; switch on or off above vars based on user settings
65 (author (and (plist-get info :with-author) (or author email)))
66 (email (and (plist-get info :with-email) email))
67 ;; (date (and (plist-get info :time-stamp-file) date))
69 (concat
70 ;; title
71 (when title
72 (concat
73 (org-e-odt-format-stylized-paragraph
74 'title (format "\n<text:title>%s</text:title>" title))
75 ;; separator
76 "\n<text:p text:style-name=\"OrgTitle\"/>"))
77 (cond
78 ((and author (not email))
79 ;; author only
80 (concat
81 (org-e-odt-format-stylized-paragraph
82 'subtitle
83 (format "<text:initial-creator>%s</text:initial-creator>" author))
84 ;; separator
85 "\n<text:p text:style-name=\"OrgSubtitle\"/>"))
86 ((and author email)
87 ;; author and email
88 (concat
89 (org-e-odt-format-stylized-paragraph
90 'subtitle
91 (org-e-odt-format-link
92 (format "<text:initial-creator>%s</text:initial-creator>" author)
93 (concat "mailto:" email)))
94 ;; separator
95 "\n<text:p text:style-name=\"OrgSubtitle\"/>")))
96 ;; date
97 (when date
98 (concat
99 (org-e-odt-format-stylized-paragraph
100 'subtitle
101 (org-odt-format-tags
102 '("<text:date style:data-style-name=\"%s\" text:date-value=\"%s\">"
103 . "</text:date>")
104 date "N75" iso-date))
105 ;; separator
106 "<text:p text:style-name=\"OrgSubtitle\"/>")))))
108 (defun org-e-odt-begin-section (style &optional name)
109 (let ((default-name (car (org-e-odt-add-automatic-style "Section"))))
110 (format "<text:section text:style-name=\"%s\" text:name=\"%s\">"
111 style (or name default-name))))
113 (defun org-e-odt-end-section ()
114 "</text:section>")
116 (defun org-e-odt-begin-paragraph (&optional style)
117 (format "<text:p%s>" (org-e-odt-get-extra-attrs-for-paragraph-style style)))
119 (defun org-e-odt-end-paragraph ()
120 "</text:p>")
122 (defun org-e-odt-get-extra-attrs-for-paragraph-style (style)
123 (let (style-name)
124 (setq style-name
125 (cond
126 ((stringp style) style)
127 ((symbolp style) (org-e-odt-get-style-name-for-entity
128 'paragraph style))))
129 (unless style-name
130 (error "Don't know how to handle paragraph style %s" style))
131 (format " text:style-name=\"%s\"" style-name)))
133 (defun org-e-odt-format-stylized-paragraph (style text)
134 (format "\n<text:p%s>%s</text:p>"
135 (org-e-odt-get-extra-attrs-for-paragraph-style style)
136 text))
138 (defun org-e-odt-format-author (&optional author )
139 (when (setq author (or author (plist-get org-lparse-opt-plist :author)))
140 (format "<dc:creator>%s</dc:creator>" author)))
142 (defun org-e-odt-format-date (&optional org-ts fmt)
143 (save-match-data
144 (let* ((time
145 (and (stringp org-ts)
146 (string-match org-ts-regexp0 org-ts)
147 (apply 'encode-time
148 (org-fix-decoded-time
149 (org-parse-time-string (match-string 0 org-ts) t)))))
150 date)
151 (cond
152 (fmt (format-time-string fmt time))
153 (t (setq date (format-time-string "%Y-%m-%dT%H:%M:%S%z" time))
154 (format "%s:%s" (substring date 0 -2) (substring date -2)))))))
156 (defun org-e-odt-begin-annotation (&optional author date)
157 (concat
158 "<office:annotation>\n"
159 (and author (org-e-odt-format-author author))
160 (org-e-odt-format-tags
161 '("<dc:date>" . "</dc:date>")
162 (org-e-odt-format-date
163 (or date (plist-get org-lparse-opt-plist :date))))
164 (org-e-odt-begin-paragraph)))
166 (defun org-e-odt-end-annotation ()
167 "</office:annotation>")
169 (defun org-e-odt-begin-plain-list (ltype)
170 (let* ((style-name (org-e-odt-get-style-name-for-entity 'list ltype))
171 (extra (concat
172 ;; (if (or org-lparse-list-table-p
173 ;; (and (= 1 (length org-lparse-list-stack))
174 ;; (null org-e-odt-list-stack-stashed)))
175 ;; " text:continue-numbering=\"false\""
176 ;; " text:continue-numbering=\"true\"")
178 " text:continue-numbering=\"true\""
180 (when style-name
181 (format " text:style-name=\"%s\"" style-name)))))
182 (case ltype
183 ((ordered unordered descriptive)
184 (concat
185 ;; (org-e-odt-end-paragraph)
186 (format "<text:list%s>" extra)))
187 (t (error "Unknown list type: %s" ltype)))))
189 (defun org-e-odt-end-plain-list (ltype)
190 (if ltype "</text:list>"
191 (error "Unknown list type: %s" ltype)))
193 (defun org-e-odt-begin-list-item (ltype &optional arg headline)
194 (case ltype
195 (ordered
196 (assert (not headline) t)
197 (let* ((counter arg) (extra ""))
198 (concat "<text:list-item>" ;; (org-e-odt-begin-paragraph)
200 ;; (if (= (length org-lparse-list-stack)
201 ;; (length org-e-odt-list-stack-stashed))
202 ;; "<text:list-header>" "<text:list-item>")
204 (unordered
205 (let* ((id arg) (extra ""))
206 (concat
207 "<text:list-item>"
208 ;; (org-e-odt-begin-paragraph)
209 (if headline (org-e-odt-format-target headline id)
210 (org-e-odt-format-bookmark "" id)))
211 ;; (if (= (length org-lparse-list-stack)
212 ;; (length org-e-odt-list-stack-stashed))
213 ;; "<text:list-header>" "<text:list-item>")
215 (descriptive
216 (assert (not headline) t)
217 (let ((term (or arg "(no term)")))
218 (concat
219 (org-e-odt-format-tags
220 '("<text:list-item>" . "</text:list-item>")
221 (org-e-odt-format-stylized-paragraph 'definition-term term))
222 (org-e-odt-begin-list-item 'unordered)
223 (org-e-odt-begin-plain-list 'descriptive)
224 (org-e-odt-begin-list-item 'unordered))))
225 (t (error "Unknown list type"))))
227 (defun org-e-odt-end-list-item (ltype)
228 (case ltype
229 ((ordered unordered)
230 ;; (org-lparse-insert-tag
231 ;; (if (= (length org-lparse-list-stack)
232 ;; (length org-e-odt-list-stack-stashed))
233 ;; (prog1 "</text:list-header>"
234 ;; (setq org-e-odt-list-stack-stashed nil))
235 ;; "</text:list-item>")
236 "</text:list-item>"
237 ;; )
239 (descriptive
240 (concat
241 (org-e-odt-end-list-item 'unordered)
242 (org-e-odt-end-plain-list 'descriptive)
243 (org-e-odt-end-list-item 'unordered)
245 (t (error "Unknown list type"))))
247 (defun org-e-odt-discontinue-list ()
248 (let ((stashed-stack org-lparse-list-stack))
249 (loop for list-type in stashed-stack
250 do (org-lparse-end-list-item-1 list-type)
251 (org-lparse-end-list list-type))
252 (setq org-e-odt-list-stack-stashed stashed-stack)))
254 (defun org-e-odt-continue-list ()
255 (setq org-e-odt-list-stack-stashed (nreverse org-e-odt-list-stack-stashed))
256 (loop for list-type in org-e-odt-list-stack-stashed
257 do (org-lparse-begin-list list-type)
258 (org-lparse-begin-list-item list-type)))
260 (defun org-e-odt-write-automatic-styles ()
261 "Write automatic styles to \"content.xml\"."
262 (with-current-buffer
263 (find-file-noselect (expand-file-name "content.xml") t)
264 ;; position the cursor
265 (goto-char (point-min))
266 (re-search-forward " </office:automatic-styles>" nil t)
267 (goto-char (match-beginning 0))
268 ;; write automatic table styles
269 (loop for (style-name props) in
270 (plist-get org-e-odt-automatic-styles 'Table) do
271 (when (setq props (or (plist-get props :rel-width) 96))
272 (insert (format org-e-odt-table-style-format style-name props))))))
274 (defun org-e-odt-add-automatic-style (object-type &optional object-props)
275 "Create an automatic style of type OBJECT-TYPE with param OBJECT-PROPS.
276 OBJECT-PROPS is (typically) a plist created by passing
277 \"#+ATTR_ODT: \" option of the object in question to
278 `org-lparse-get-block-params'.
280 Use `org-e-odt-object-counters' to generate an automatic
281 OBJECT-NAME and STYLE-NAME. If OBJECT-PROPS is non-nil, add a
282 new entry in `org-e-odt-automatic-styles'. Return (OBJECT-NAME
283 . STYLE-NAME)."
284 (assert (stringp object-type))
285 (let* ((object (intern object-type))
286 (seqvar object)
287 (seqno (1+ (or (plist-get org-e-odt-object-counters seqvar) 0)))
288 (object-name (format "%s%d" object-type seqno)) style-name)
289 (setq org-e-odt-object-counters
290 (plist-put org-e-odt-object-counters seqvar seqno))
291 (when object-props
292 (setq style-name (format "Org%s" object-name))
293 (setq org-e-odt-automatic-styles
294 (plist-put org-e-odt-automatic-styles object
295 (append (list (list style-name object-props))
296 (plist-get org-e-odt-automatic-styles object)))))
297 (cons object-name style-name)))
299 (defun org-e-odt-format-table-columns ()
300 (let* ((num-cols (length (plist-get table-info :alignment)))
301 (col-nos (loop for i from 0 below num-cols collect i))
302 (levels )
303 (col-widths (plist-get table-info :width))
304 (style (or (nth 1 org-e-odt-table-style-spec) "OrgTable")))
305 (mapconcat
306 (lambda (c)
307 (let* ((width (or (and org-lparse-table-is-styled (aref col-widths c))
308 0)))
309 (org-e-odt-make-string
310 (1+ width)
311 (org-e-odt-format-tags
312 "<table:table-column table:style-name=\"%sColumn\"/>" "" style))))
313 col-nos "\n")))
316 (defun org-e-odt-begin-table (caption label attributes)
317 ;; (setq org-e-odt-table-indentedp (not (null org-lparse-list-stack)))
318 (setq org-e-odt-table-indentedp nil) ; FIXME
319 (when org-e-odt-table-indentedp
320 ;; Within the Org file, the table is appearing within a list item.
321 ;; OpenDocument doesn't allow table to appear within list items.
322 ;; Temporarily terminate the list, emit the table and then
323 ;; re-continue the list.
324 (org-e-odt-discontinue-list)
325 ;; Put the Table in an indented section.
326 (let ((level (length org-e-odt-list-stack-stashed)))
327 (org-e-odt-begin-section (format "OrgIndentedSection-Level-%d" level))))
328 (setq attributes (org-lparse-get-block-params attributes))
329 (setq org-e-odt-table-style (plist-get attributes :style))
330 (setq org-e-odt-table-style-spec
331 (assoc org-e-odt-table-style org-export-e-odt-table-styles))
332 (concat
333 (org-e-odt-format-stylized-paragraph
334 'table (org-e-odt-format-entity-caption label caption "__Table__"))
335 (let ((name-and-style (org-e-odt-add-automatic-style "Table" attributes)))
336 (format
337 "\n<table:table table:name=\"%s\" table:style-name=\"%s\">\n"
338 (car name-and-style) (or (nth 1 org-e-odt-table-style-spec)
339 (cdr name-and-style) "OrgTable")))
340 (org-e-odt-format-table-columns) "\n")
342 ;; (org-e-html-pp table-info)
346 (defun org-e-odt-end-table ()
347 (concat
348 "</table:table>"
349 ;; (when org-e-odt-table-indentedp
350 ;; (org-e-odt-end-section)
351 ;; (org-e-odt-continue-list))
354 (defun org-e-odt-begin-table-rowgroup (&optional is-header-row)
355 (prog1
356 (concat (when org-e-odt-table-rowgrp-open
357 (org-e-odt-end-table-rowgroup))
358 (if is-header-row "<table:table-header-rows>"
359 "<table:table-rows>"))
360 (setq org-e-odt-table-rowgrp-open t)
361 (setq org-e-odt-table-cur-rowgrp-is-hdr is-header-row)))
363 (defun org-e-odt-end-table-rowgroup ()
364 (when org-e-odt-table-rowgrp-open
365 (setq org-e-odt-table-rowgrp-open nil)
366 (if org-e-odt-table-cur-rowgrp-is-hdr
367 "</table:table-header-rows>" "</table:table-rows>")))
369 (defun org-e-odt-format-table-row (row)
370 (org-e-odt-format-tags
371 '("<table:table-row>" . "</table:table-row>") row))
373 (defun org-e-odt-get-column-alignment (c)
374 (let ((colalign-vector (plist-get table-info :alignment)))
375 ;; FIXME
376 (assoc-default (aref colalign-vector c)
377 '(("l" . "left")
378 ("r" . "right")
379 ("c" . "center")))))
381 (defun org-e-odt-get-table-cell-styles (r c &optional style-spec)
382 "Retrieve styles applicable to a table cell.
383 R and C are (zero-based) row and column numbers of the table
384 cell. STYLE-SPEC is an entry in `org-export-e-odt-table-styles'
385 applicable to the current table. It is `nil' if the table is not
386 associated with any style attributes.
388 Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
390 When STYLE-SPEC is nil, style the table cell the conventional way
391 - choose cell borders based on row and column groupings and
392 choose paragraph alignment based on `org-col-cookies' text
393 property. See also
394 `org-e-odt-get-paragraph-style-cookie-for-table-cell'.
396 When STYLE-SPEC is non-nil, ignore the above cookie and return
397 styles congruent with the ODF-1.2 specification."
398 (cond
399 (style-spec
401 ;; LibreOffice - particularly the Writer - honors neither table
402 ;; templates nor custom table-cell styles. Inorder to retain
403 ;; inter-operability with LibreOffice, only automatic styles are
404 ;; used for styling of table-cells. The current implementation is
405 ;; congruent with ODF-1.2 specification and hence is
406 ;; future-compatible.
408 ;; Additional Note: LibreOffice's AutoFormat facility for tables -
409 ;; which recognizes as many as 16 different cell types - is much
410 ;; richer. Unfortunately it is NOT amenable to easy configuration
411 ;; by hand.
413 (let* ((template-name (nth 1 style-spec))
414 (cell-style-selectors (nth 2 style-spec))
415 (cell-type
416 (cond
417 ((and (cdr (assoc 'use-first-column-styles cell-style-selectors))
418 (= c 0)) "FirstColumn")
419 ((and (cdr (assoc 'use-last-column-styles cell-style-selectors))
420 (= c (1- org-lparse-table-ncols))) "LastColumn")
421 ((and (cdr (assoc 'use-first-row-styles cell-style-selectors))
422 (= r 0)) "FirstRow")
423 ((and (cdr (assoc 'use-last-row-styles cell-style-selectors))
424 (= r org-e-odt-table-rownum))
425 "LastRow")
426 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
427 (= (% r 2) 1)) "EvenRow")
428 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
429 (= (% r 2) 0)) "OddRow")
430 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
431 (= (% c 2) 1)) "EvenColumn")
432 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
433 (= (% c 2) 0)) "OddColumn")
434 (t ""))))
435 (cons
436 (concat template-name cell-type "TableCell")
437 (concat template-name cell-type "TableParagraph"))))
439 (cons
440 (concat
441 "OrgTblCell"
442 (cond
443 ((= r 0) "T")
444 ((eq (cdr (assoc r nil ;; org-lparse-table-rowgrp-info FIXME
445 )) :start) "T")
446 (t ""))
447 (when (= r org-e-odt-table-rownum) "B")
448 (cond
449 ((= c 0) "")
450 ((or (memq (nth c org-table-colgroup-info) '(:start :startend))
451 (memq (nth (1- c) org-table-colgroup-info) '(:end :startend))) "L")
452 (t "")))
453 (capitalize (org-e-odt-get-column-alignment c))))))
455 (defun org-e-odt-get-paragraph-style-cookie-for-table-cell (r c)
456 (concat
457 (and (not org-e-odt-table-style-spec)
458 (cond
459 (org-e-odt-table-cur-rowgrp-is-hdr "OrgTableHeading")
460 ((and (= c 0) nil
461 ;; (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS)
463 "OrgTableHeading")
464 (t "OrgTableContents")))
465 (and org-lparse-table-is-styled
466 (cdr (org-e-odt-get-table-cell-styles
467 r c org-e-odt-table-style-spec)))))
469 (defun org-e-odt-get-style-name-cookie-for-table-cell (r c)
470 (when org-lparse-table-is-styled
471 (let* ((cell-styles (org-e-odt-get-table-cell-styles
472 r c org-e-odt-table-style-spec))
473 (table-cell-style (car cell-styles)))
474 table-cell-style)))
476 (defun org-e-odt-format-table-cell (data r c horiz-span)
477 (concat
478 (let* ((paragraph-style-cookie
479 (org-e-odt-get-paragraph-style-cookie-for-table-cell r c))
480 (style-name-cookie
481 (org-e-odt-get-style-name-cookie-for-table-cell r c))
482 (extra (and style-name-cookie
483 (format " table:style-name=\"%s\"" style-name-cookie)))
484 (extra (concat extra
485 (and (> horiz-span 0)
486 (format " table:number-columns-spanned=\"%d\""
487 (1+ horiz-span))))))
488 (org-e-odt-format-tags
489 '("<table:table-cell%s>" . "</table:table-cell>")
490 (if org-lparse-list-table-p data
491 (org-e-odt-format-stylized-paragraph paragraph-style-cookie data)) extra))
492 (let (s)
493 (dotimes (i horiz-span)
494 (setq s (concat s "\n<table:covered-table-cell/>"))) s)
495 "\n"))
497 (defun org-e-odt-begin-toc (lang-specific-heading max-level)
498 (concat
499 (format "
500 <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
501 <text:table-of-content-source text:outline-level=\"%d\">
502 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
503 " max-level lang-specific-heading)
505 (let ((entry-templates ""))
506 (loop for level from 1 upto 10
507 do (setq entry-templates
508 (concat entry-templates
509 (format
511 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
512 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
513 <text:index-entry-chapter/>
514 <text:index-entry-text/>
515 <text:index-entry-link-end/>
516 </text:table-of-content-entry-template>
517 " level level))))
518 entry-templates)
520 (format "
521 </text:table-of-content-source>
523 <text:index-body>
524 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
525 <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
526 </text:index-title>
527 " lang-specific-heading)))
529 (defun org-e-odt-end-toc ()
530 (format "
531 </text:index-body>
532 </text:table-of-content>
535 (defun org-e-odt-format-toc-entry (snumber todo headline tags href)
537 ;; FIXME
538 (setq headline (concat
539 (and org-export-with-section-numbers
540 (concat snumber ". "))
541 headline
542 (and tags
543 (concat
544 (org-e-odt-format-spaces 3)
545 (org-e-odt-format-fontify tags "tag")))))
546 (when todo
547 (setq headline (org-e-odt-format-fontify headline "todo")))
549 (let ((org-e-odt-suppress-xref t))
550 (org-e-odt-format-link headline (concat "#" href))))
552 (defun org-e-odt-format-toc-item (toc-entry level org-last-level)
553 (let ((style (format "Contents_20_%d"
554 (+ level (or ;; (org-lparse-get 'TOPLEVEL-HLEVEL)
556 1) -1))))
557 (concat "\n" (org-e-odt-format-stylized-paragraph style toc-entry) "\n")))
559 ;; Following variable is let bound during 'ORG-LINK callback. See
560 ;; org-html.el
562 (defun org-e-odt-format-link (desc href &optional attr)
563 (cond
564 ((and (= (string-to-char href) ?#) (not org-e-odt-suppress-xref))
565 (setq href (concat org-export-e-odt-bookmark-prefix (substring href 1)))
566 (let ((xref-format "text"))
567 (when (numberp desc)
568 (setq desc (format "%d" desc) xref-format "number"))
569 (org-e-odt-format-tags
570 '("<text:bookmark-ref text:reference-format=\"%s\" text:ref-name=\"%s\">" .
571 "</text:bookmark-ref>")
572 desc xref-format href)))
573 (org-lparse-link-description-is-image
574 (org-e-odt-format-tags
575 '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</draw:a>")
576 desc href (or attr "")))
578 (org-e-odt-format-tags
579 '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</text:a>")
580 desc href (or attr "")))))
582 (defun org-e-odt-format-spaces (n)
583 (cond
584 ((= n 1) " ")
585 ((> n 1) (concat
586 " " (org-e-odt-format-tags "<text:s text:c=\"%d\"/>" "" (1- n))))
587 (t "")))
589 (defun org-e-odt-format-tabs (&optional n)
590 (let ((tab "<text:tab/>")
591 (n (or n 1)))
592 (insert tab)))
594 (defun org-e-odt-format-line-break ()
595 (org-e-odt-format-tags "<text:line-break/>" ""))
597 (defun org-e-odt-format-horizontal-line ()
598 (org-e-odt-format-stylized-paragraph 'horizontal-line ""))
600 (defun org-e-odt-encode-plain-text (line &optional no-whitespace-filling)
601 (setq line (org-e-html-encode-plain-text line))
602 (if no-whitespace-filling line
603 (org-e-odt-fill-tabs-and-spaces line)))
605 (defun org-e-odt-format-line (line)
606 (case org-lparse-dyn-current-environment
607 (fixedwidth (concat
608 (org-e-odt-format-stylized-paragraph
609 'fixedwidth (org-e-odt-encode-plain-text line)) "\n"))
610 (t (concat line "\n"))))
612 (defun org-e-odt-format-comment (fmt &rest args)
613 (let ((comment (apply 'format fmt args)))
614 (format "\n<!-- %s -->\n" comment)))
616 (defun org-e-odt-format-org-entity (wd)
617 (org-entity-get-representation wd 'utf8))
619 (defun org-e-odt-fill-tabs-and-spaces (line)
620 (replace-regexp-in-string
621 "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
622 (cond
623 ((string= s "\t") (org-e-odt-format-tabs))
624 (t (org-e-odt-format-spaces (length s))))) line))
627 (defun org-e-odt-format-source-line-with-line-number-and-label
628 (line rpllbl num fontifier par-style)
630 (let ((keep-label (not (numberp rpllbl)))
631 (ref (org-find-text-property-in-string 'org-coderef line)))
632 (setq line (concat line (and keep-label ref (format "(%s)" ref))))
633 (setq line (funcall fontifier line))
634 (when ref
635 (setq line (org-e-odt-format-target line (concat "coderef-" ref))))
636 (setq line (org-e-odt-format-stylized-paragraph par-style line))
637 (if (not num) line
638 (org-e-odt-format-tags '("<text:list-item>" . "</text:list-item>") line))))
640 (defun org-e-odt-format-source-code-or-example-plain
641 (lines lang caption textareap cols rows num cont rpllbl fmt)
642 "Format source or example blocks much like fixedwidth blocks.
643 Use this when `org-export-e-odt-fontify-srcblocks' option is turned
644 off."
645 (let* ((lines (org-split-string lines "[\r\n]"))
646 (line-count (length lines))
647 (i 0))
648 (mapconcat
649 (lambda (line)
650 (incf i)
651 (org-e-odt-format-source-line-with-line-number-and-label
652 line rpllbl num 'org-e-odt-encode-plain-text
653 (if (= i line-count) "OrgFixedWidthBlockLastLine"
654 "OrgFixedWidthBlock")))
655 lines "\n")))
657 (defun org-e-odt-hfy-face-to-css (fn)
658 "Create custom style for face FN.
659 When FN is the default face, use it's foreground and background
660 properties to create \"OrgSrcBlock\" paragraph style. Otherwise
661 use it's color attribute to create a character style whose name
662 is obtained from FN. Currently all attributes of FN other than
663 color are ignored.
665 The style name for a face FN is derived using the following
666 operations on the face name in that order - de-dash, CamelCase
667 and prefix with \"OrgSrc\". For example,
668 `font-lock-function-name-face' is associated with
669 \"OrgSrcFontLockFunctionNameFace\"."
670 (let* ((css-list (hfy-face-to-style fn))
671 (style-name ((lambda (fn)
672 (concat "OrgSrc"
673 (mapconcat
674 'capitalize (split-string
675 (hfy-face-or-def-to-name fn) "-")
676 ""))) fn))
677 (color-val (cdr (assoc "color" css-list)))
678 (background-color-val (cdr (assoc "background" css-list)))
679 (style (and org-export-e-odt-create-custom-styles-for-srcblocks
680 (cond
681 ((eq fn 'default)
682 (format org-src-block-paragraph-format
683 background-color-val color-val))
685 (format
687 <style:style style:name=\"%s\" style:family=\"text\">
688 <style:text-properties fo:color=\"%s\"/>
689 </style:style>" style-name color-val))))))
690 (cons style-name style)))
692 (defun org-e-odt-insert-custom-styles-for-srcblocks (styles)
693 "Save STYLES used for colorizing of source blocks.
694 Update styles.xml with styles that were collected as part of
695 `org-e-odt-hfy-face-to-css' callbacks."
696 (when styles
697 (with-current-buffer
698 (find-file-noselect (expand-file-name "styles.xml") t)
699 (goto-char (point-min))
700 (when (re-search-forward "</office:styles>" nil t)
701 (goto-char (match-beginning 0))
702 (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n")))))
704 (defun org-e-odt-format-source-code-or-example-colored
705 (lines lang caption textareap cols rows num cont rpllbl fmt)
706 "Format source or example blocks using `htmlfontify-string'.
707 Use this routine when `org-export-e-odt-fontify-srcblocks' option
708 is turned on."
709 (let* ((lang-m (and lang (or (cdr (assoc lang org-src-lang-modes)) lang)))
710 (mode (and lang-m (intern (concat (if (symbolp lang-m)
711 (symbol-name lang-m)
712 lang-m) "-mode"))))
713 (org-inhibit-startup t)
714 (org-startup-folded nil)
715 (lines (with-temp-buffer
716 (insert lines)
717 (if (functionp mode) (funcall mode) (fundamental-mode))
718 (font-lock-fontify-buffer)
719 (buffer-string)))
720 (hfy-html-quote-regex "\\([<\"&> ]\\)")
721 (hfy-html-quote-map '(("\"" "&quot;")
722 ("<" "&lt;")
723 ("&" "&amp;")
724 (">" "&gt;")
725 (" " "<text:s/>")
726 (" " "<text:tab/>")))
727 (hfy-face-to-css 'org-e-odt-hfy-face-to-css)
728 (hfy-optimisations-1 (copy-seq hfy-optimisations))
729 (hfy-optimisations (add-to-list 'hfy-optimisations-1
730 'body-text-only))
731 (hfy-begin-span-handler
732 (lambda (style text-block text-id text-begins-block-p)
733 (insert (format "<text:span text:style-name=\"%s\">" style))))
734 (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
735 (when (fboundp 'htmlfontify-string)
736 (let* ((lines (org-split-string lines "[\r\n]"))
737 (line-count (length lines))
738 (i 0))
739 (mapconcat
740 (lambda (line)
741 (incf i)
742 (org-e-odt-format-source-line-with-line-number-and-label
743 line rpllbl num 'htmlfontify-string
744 (if (= i line-count) "OrgSrcBlockLastLine" "OrgSrcBlock")))
745 lines "\n")))))
747 (defun org-e-odt-format-source-code-or-example (lines lang
748 &optional
749 caption textareap
750 cols rows num cont
751 rpllbl fmt)
752 "Format source or example blocks for export.
753 Use `org-e-odt-format-source-code-or-example-plain' or
754 `org-e-odt-format-source-code-or-example-colored' depending on the
755 value of `org-export-e-odt-fontify-srcblocks."
756 (setq ;; lines (org-export-number-lines
757 ;; lines 0 0 num cont rpllbl fmt 'preprocess) FIXME
758 lines (funcall
759 (or (and org-export-e-odt-fontify-srcblocks
760 (or (featurep 'htmlfontify)
761 ;; htmlfontify.el was introduced in Emacs 23.2
762 ;; So load it with some caution
763 (require 'htmlfontify nil t))
764 (fboundp 'htmlfontify-string)
765 'org-e-odt-format-source-code-or-example-colored)
766 'org-e-odt-format-source-code-or-example-plain)
767 lines lang caption textareap cols rows num cont rpllbl fmt))
768 (if (not num) lines
769 (let ((extra (format " text:continue-numbering=\"%s\""
770 (if cont "true" "false"))))
771 (org-e-odt-format-tags
772 '("<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>"
773 . "</text:list>") lines extra))))
775 (defun org-e-odt-remap-stylenames (style-name)
777 (cdr (assoc style-name '(("timestamp-wrapper" . "OrgTimestampWrapper")
778 ("timestamp" . "OrgTimestamp")
779 ("timestamp-kwd" . "OrgTimestampKeyword")
780 ("tag" . "OrgTag")
781 ("todo" . "OrgTodo")
782 ("done" . "OrgDone")
783 ("target" . "OrgTarget"))))
784 style-name))
786 (defun org-e-odt-format-fontify (text style &optional id)
787 (let* ((style-name
788 (cond
789 ((stringp style)
790 (org-e-odt-remap-stylenames style))
791 ((symbolp style)
792 (org-e-odt-get-style-name-for-entity 'character style))
793 ((listp style)
794 (assert (< 1 (length style)))
795 (let ((parent-style (pop style)))
796 (mapconcat (lambda (s)
797 ;; (assert (stringp s) t)
798 (org-e-odt-remap-stylenames s)) style "")
799 (org-e-odt-remap-stylenames parent-style)))
800 (t (error "Don't how to handle style %s" style)))))
801 (org-e-odt-format-tags
802 '("<text:span text:style-name=\"%s\">" . "</text:span>")
803 text style-name)))
805 (defun org-e-odt-relocate-relative-path (path dir)
806 (if (file-name-absolute-p path) path
807 (file-relative-name (expand-file-name path dir)
808 (expand-file-name "eyecandy" dir))))
810 (defun org-e-odt-format-inline-image (thefile
811 &optional caption label attrs ; FIXME - CLA
813 (let* ((thelink (if (file-name-absolute-p thefile) thefile
814 (org-xml-format-href
815 (org-e-odt-relocate-relative-path
816 thefile org-current-export-file))))
817 (href
818 (org-e-odt-format-tags
819 "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
820 (if org-export-e-odt-embed-images
821 (org-e-odt-copy-image-file thefile) thelink))))
822 (org-export-e-odt-format-image thefile href)))
824 (defun org-export-e-odt-format-formula (src href)
825 (save-match-data
826 (let* ((caption (org-find-text-property-in-string 'org-caption src))
827 (caption (and caption (org-xml-format-desc caption)))
828 (label (org-find-text-property-in-string 'org-label src))
829 (latex-frag (org-find-text-property-in-string 'org-latex-src src))
830 (embed-as (or (and latex-frag
831 (org-find-text-property-in-string
832 'org-latex-src-embed-type src))
833 (if (or caption label) 'paragraph 'character)))
834 width height)
835 (when latex-frag
836 (setq href (org-propertize href :title "LaTeX Fragment"
837 :description latex-frag)))
838 (cond
839 ((eq embed-as 'character)
840 (org-e-odt-format-entity "InlineFormula" href width height))
842 (org-lparse-end-paragraph)
843 (org-lparse-insert-list-table
844 `((,(org-e-odt-format-entity
845 (if caption "CaptionedDisplayFormula" "DisplayFormula")
846 href width height :caption caption :label nil)
847 ,(if (not label) ""
848 (org-e-odt-format-entity-caption label nil "__MathFormula__"))))
849 nil nil nil ":style \"OrgEquation\"" nil '((1 "c" 8) (2 "c" 1)))
850 (throw 'nextline nil))))))
852 (defun org-e-odt-copy-formula-file (path)
853 "Returns the internal name of the file"
854 (let* ((src-file (expand-file-name
855 path (file-name-directory org-current-export-file)))
856 (target-dir (format "Formula-%04d/"
857 (incf org-e-odt-embedded-formulas-count)))
858 (target-file (concat target-dir "content.xml")))
859 (message "Embedding %s as %s ..."
860 (substring-no-properties path) target-file)
862 (make-directory target-dir)
863 (org-e-odt-create-manifest-file-entry
864 "application/vnd.oasis.opendocument.formula" target-dir "1.2")
866 (case (org-e-odt-is-formula-link-p src-file)
867 (mathml
868 (copy-file src-file target-file 'overwrite))
869 (odf
870 (org-e-odt-zip-extract-one src-file "content.xml" target-dir))
872 (error "%s is not a formula file" src-file)))
874 (org-e-odt-create-manifest-file-entry "text/xml" target-file)
875 target-file))
877 (defun org-e-odt-format-inline-formula (thefile)
878 (let* ((thelink (if (file-name-absolute-p thefile) thefile
879 (org-xml-format-href
880 (org-e-odt-relocate-relative-path
881 thefile org-current-export-file))))
882 (href
883 (org-e-odt-format-tags
884 "<draw:object xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
885 (file-name-directory (org-e-odt-copy-formula-file thefile)))))
886 (org-export-e-odt-format-formula thefile href)))
888 (defun org-e-odt-is-formula-link-p (file)
889 (let ((case-fold-search nil))
890 (cond
891 ((string-match "\\.\\(mathml\\|mml\\)\\'" file)
892 'mathml)
893 ((string-match "\\.odf\\'" file)
894 'odf))))
896 (defun org-e-odt-format-org-link (opt-plist type-1 path fragment desc attr
897 descp)
898 "Make a OpenDocument link.
899 OPT-PLIST is an options list.
900 TYPE-1 is the device-type of the link (THIS://foo.html).
901 PATH is the path of the link (http://THIS#location).
902 FRAGMENT is the fragment part of the link, if any (foo.html#THIS).
903 DESC is the link description, if any.
904 ATTR is a string of other attributes of the a element."
905 (declare (special org-lparse-par-open))
906 (save-match-data
907 (let* ((may-inline-p
908 (and (member type-1 '("http" "https" "file"))
909 (org-lparse-should-inline-p path descp)
910 (not fragment)))
911 (type (if (equal type-1 "id") "file" type-1))
912 (filename path)
913 (thefile path))
914 (cond
915 ;; check for inlined images
916 ((and (member type '("file"))
917 (not fragment)
918 (org-file-image-p
919 filename org-export-e-odt-inline-image-extensions)
920 (or (eq t org-export-e-odt-inline-images)
921 (and org-export-e-odt-inline-images (not descp))))
922 (org-e-odt-format-inline-image thefile))
923 ;; check for embedded formulas
924 ((and (member type '("file"))
925 (not fragment)
926 (org-e-odt-is-formula-link-p filename)
927 (or (not descp)))
928 (org-e-odt-format-inline-formula thefile))
929 ((string= type "coderef")
930 (let* ((ref fragment)
931 (lineno-or-ref (cdr (assoc ref org-export-code-refs)))
932 (desc (and descp desc))
933 (org-e-odt-suppress-xref nil)
934 (href (org-xml-format-href (concat "#coderef-" ref))))
935 (cond
936 ((and (numberp lineno-or-ref) (not desc))
937 (org-e-odt-format-link lineno-or-ref href))
938 ((and (numberp lineno-or-ref) desc
939 (string-match (regexp-quote (concat "(" ref ")")) desc))
940 (format (replace-match "%s" t t desc)
941 (org-e-odt-format-link lineno-or-ref href)))
943 (setq desc (format
944 (if (and desc (string-match
945 (regexp-quote (concat "(" ref ")"))
946 desc))
947 (replace-match "%s" t t desc)
948 (or desc "%s"))
949 lineno-or-ref))
950 (org-e-odt-format-link (org-xml-format-desc desc) href)))))
952 (when (string= type "file")
953 (setq thefile
954 (cond
955 ((file-name-absolute-p path)
956 (concat "file://" (expand-file-name path)))
957 (t (org-e-odt-relocate-relative-path
958 thefile org-current-export-file)))))
960 (when (and (member type '("" "http" "https" "file")) fragment)
961 (setq thefile (concat thefile "#" fragment)))
963 (setq thefile (org-xml-format-href thefile))
965 (when (not (member type '("" "file")))
966 (setq thefile (concat type ":" thefile)))
968 (let ((org-e-odt-suppress-xref nil))
969 (org-e-odt-format-link
970 (org-xml-format-desc desc) thefile attr)))))))
972 (defun org-e-odt-format-heading (text level &optional id)
973 (let* ((text (if id (org-e-odt-format-target text id) text)))
974 (org-e-odt-format-tags
975 '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
976 "</text:h>") text level level)))
978 (defun org-e-odt-format-headline (title extra-targets tags
979 &optional snumber level)
980 (concat
981 (org-e-odt-format-extra-targets extra-targets)
983 ;; No need to generate section numbers. They are auto-generated by
984 ;; the application
986 ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
987 title
988 (and tags (concat (org-e-odt-format-spaces 3)
989 (org-e-odt-format-org-tags tags)))))
991 (defun org-e-odt-format-anchor (text name &optional class)
992 (org-e-odt-format-target text name))
994 (defun org-e-odt-format-bookmark (text id)
995 (if id
996 (org-e-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id)
997 text))
999 (defun org-e-odt-format-target (text id)
1000 (let ((name (concat org-export-e-odt-bookmark-prefix id)))
1001 (concat
1002 (and id (org-e-odt-format-tags
1003 "<text:bookmark-start text:name=\"%s\"/>" "" name))
1004 (org-e-odt-format-bookmark text id)
1005 (and id (org-e-odt-format-tags
1006 "<text:bookmark-end text:name=\"%s\"/>" "" name)))))
1008 (defun org-e-odt-format-footnote (n def)
1009 (setq n (format "%d" n))
1010 (let ((id (concat "fn" n))
1011 (note-class "footnote")
1012 (par-style "Footnote"))
1013 (org-e-odt-format-tags
1014 '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
1015 "</text:note>")
1016 (concat
1017 (org-e-odt-format-tags
1018 '("<text:note-citation>" . "</text:note-citation>")
1020 (org-e-odt-format-tags
1021 '("<text:note-body>" . "</text:note-body>")
1022 def))
1023 id note-class)))
1025 (defun org-e-odt-format-footnote-reference (n def refcnt)
1026 (if (= refcnt 1)
1027 (org-e-odt-format-footnote n def)
1028 (org-e-odt-format-footnote-ref n)))
1030 (defun org-e-odt-format-footnote-ref (n)
1031 (setq n (format "%d" n))
1032 (let ((note-class "footnote")
1033 (ref-format "text")
1034 (ref-name (concat "fn" n)))
1035 (org-e-odt-format-tags
1036 '("<text:span text:style-name=\"%s\">" . "</text:span>")
1037 (org-e-odt-format-tags
1038 '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" . "</text:note-ref>")
1039 n note-class ref-format ref-name)
1040 "OrgSuperscript")))
1042 (defun org-e-odt-get-image-name (file-name)
1043 (require 'sha1)
1044 (file-relative-name
1045 (expand-file-name
1046 (concat (sha1 file-name) "." (file-name-extension file-name)) "Pictures")))
1048 (defun org-export-e-odt-format-image (src href)
1049 "Create image tag with source and attributes."
1050 (save-match-data
1051 (let* ((caption (org-find-text-property-in-string 'org-caption src))
1052 (caption (and caption (org-xml-format-desc caption)))
1053 (attr (org-find-text-property-in-string 'org-attributes src))
1054 (label (org-find-text-property-in-string 'org-label src))
1055 (latex-frag (org-find-text-property-in-string
1056 'org-latex-src src))
1057 (category (and latex-frag "__DvipngImage__"))
1058 (attr-plist (org-lparse-get-block-params attr))
1059 (user-frame-anchor
1060 (car (assoc-string (plist-get attr-plist :anchor)
1061 '(("as-char") ("paragraph") ("page")) t)))
1062 (user-frame-style
1063 (and user-frame-anchor (plist-get attr-plist :style)))
1064 (user-frame-attrs
1065 (and user-frame-anchor (plist-get attr-plist :attributes)))
1066 (user-frame-params
1067 (list user-frame-style user-frame-attrs user-frame-anchor))
1068 (embed-as (cond
1069 (latex-frag
1070 (symbol-name
1071 (case (org-find-text-property-in-string
1072 'org-latex-src-embed-type src)
1073 (paragraph 'paragraph)
1074 (t 'as-char))))
1075 (user-frame-anchor)
1076 (t "paragraph")))
1077 (size (org-e-odt-image-size-from-file
1078 src (plist-get attr-plist :width)
1079 (plist-get attr-plist :height)
1080 (plist-get attr-plist :scale) nil embed-as))
1081 (width (car size)) (height (cdr size)))
1082 (when latex-frag
1083 (setq href (org-propertize href :title "LaTeX Fragment"
1084 :description latex-frag)))
1085 (let ((frame-style-handle (concat (and (or caption label) "Captioned")
1086 embed-as "Image")))
1087 (org-e-odt-format-entity
1088 frame-style-handle href width height
1089 :caption caption :label label :category category
1090 :user-frame-params user-frame-params)))))
1092 (defun org-e-odt-format-object-description (title description)
1093 (concat (and title (org-e-odt-format-tags
1094 '("<svg:title>" . "</svg:title>")
1095 (org-e-odt-encode-plain-text title t)))
1096 (and description (org-e-odt-format-tags
1097 '("<svg:desc>" . "</svg:desc>")
1098 (org-e-odt-encode-plain-text description t)))))
1100 (defun org-e-odt-format-frame (text width height style &optional
1101 extra anchor-type)
1102 (let ((frame-attrs
1103 (concat
1104 (if width (format " svg:width=\"%0.2fcm\"" width) "")
1105 (if height (format " svg:height=\"%0.2fcm\"" height) "")
1106 extra
1107 (format " text:anchor-type=\"%s\"" (or anchor-type "paragraph")))))
1108 (org-e-odt-format-tags
1109 '("<draw:frame draw:style-name=\"%s\"%s>" . "</draw:frame>")
1110 (concat text (org-e-odt-format-object-description
1111 (get-text-property 0 :title text)
1112 (get-text-property 0 :description text)))
1113 style frame-attrs)))
1115 (defun org-e-odt-format-textbox (text width height style &optional
1116 extra anchor-type)
1117 (org-e-odt-format-frame
1118 (org-e-odt-format-tags
1119 '("<draw:text-box %s>" . "</draw:text-box>")
1120 text (concat (format " fo:min-height=\"%0.2fcm\"" (or height .2))
1121 (unless width
1122 (format " fo:min-width=\"%0.2fcm\"" (or width .2)))))
1123 width nil style extra anchor-type))
1125 (defun org-e-odt-format-inlinetask (heading content
1126 &optional todo priority tags)
1127 (org-e-odt-format-stylized-paragraph
1128 nil (org-e-odt-format-textbox
1129 (concat (org-e-odt-format-stylized-paragraph
1130 "OrgInlineTaskHeading"
1131 (org-lparse-format
1132 'HEADLINE (concat (org-lparse-format-todo todo) " " heading)
1133 nil tags))
1134 content) nil nil "OrgInlineTaskFrame" " style:rel-width=\"100%\"")))
1137 (defun org-e-odt-merge-frame-params(default-frame-params user-frame-params)
1138 (if (not user-frame-params) default-frame-params
1139 (assert (= (length default-frame-params) 3))
1140 (assert (= (length user-frame-params) 3))
1141 (loop for user-frame-param in user-frame-params
1142 for default-frame-param in default-frame-params
1143 collect (or user-frame-param default-frame-param))))
1145 (defun* org-e-odt-format-entity (entity href width height
1146 &key caption label category
1147 user-frame-params)
1148 (let* ((entity-style (assoc-string entity org-e-odt-entity-frame-styles t))
1149 default-frame-params frame-params)
1150 (cond
1151 ((not (or caption label))
1152 (setq default-frame-params (nth 2 entity-style))
1153 (setq frame-params (org-e-odt-merge-frame-params
1154 default-frame-params user-frame-params))
1155 (apply 'org-e-odt-format-frame href width height frame-params))
1157 (setq default-frame-params (nth 3 entity-style))
1158 (setq frame-params (org-e-odt-merge-frame-params
1159 default-frame-params user-frame-params))
1160 (apply 'org-e-odt-format-textbox
1161 (org-e-odt-format-stylized-paragraph
1162 'illustration
1163 (concat
1164 (apply 'org-e-odt-format-frame href width height
1165 (nth 2 entity-style))
1166 (org-e-odt-format-entity-caption
1167 label caption (or category (nth 1 entity-style)))))
1168 width height frame-params)))))
1170 (defun org-e-odt-copy-image-file (path)
1171 "Returns the internal name of the file"
1172 (let* ((image-type (file-name-extension path))
1173 (media-type (format "image/%s" image-type))
1174 (src-file (expand-file-name
1175 path (file-name-directory org-current-export-file)))
1176 (target-dir "Images/")
1177 (target-file
1178 (format "%s%04d.%s" target-dir
1179 (incf org-e-odt-embedded-images-count) image-type)))
1180 (message "Embedding %s as %s ..."
1181 (substring-no-properties path) target-file)
1183 (when (= 1 org-e-odt-embedded-images-count)
1184 (make-directory target-dir)
1185 (org-e-odt-create-manifest-file-entry "" target-dir))
1187 (copy-file src-file target-file 'overwrite)
1188 (org-e-odt-create-manifest-file-entry media-type target-file)
1189 target-file))
1191 (defun org-e-odt-do-image-size (probe-method file &optional dpi anchor-type)
1192 (setq dpi (or dpi org-export-e-odt-pixels-per-inch))
1193 (setq anchor-type (or anchor-type "paragraph"))
1194 (flet ((size-in-cms (size-in-pixels)
1195 (flet ((pixels-to-cms (pixels)
1196 (let* ((cms-per-inch 2.54)
1197 (inches (/ pixels dpi)))
1198 (* cms-per-inch inches))))
1199 (and size-in-pixels
1200 (cons (pixels-to-cms (car size-in-pixels))
1201 (pixels-to-cms (cdr size-in-pixels)))))))
1202 (case probe-method
1203 (emacs
1204 (size-in-cms (ignore-errors ; Emacs could be in batch mode
1205 (clear-image-cache)
1206 (image-size (create-image file) 'pixels))))
1207 (imagemagick
1208 (size-in-cms
1209 (let ((dim (shell-command-to-string
1210 (format "identify -format \"%%w:%%h\" \"%s\"" file))))
1211 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
1212 (cons (string-to-number (match-string 1 dim))
1213 (string-to-number (match-string 2 dim)))))))
1215 (cdr (assoc-string anchor-type
1216 org-export-e-odt-default-image-sizes-alist))))))
1218 (defun org-e-odt-image-size-from-file (file &optional user-width
1219 user-height scale dpi embed-as)
1220 (unless (file-name-absolute-p file)
1221 (setq file (expand-file-name
1222 file (file-name-directory org-current-export-file))))
1223 (let* (size width height)
1224 (unless (and user-height user-width)
1225 (loop for probe-method in org-export-e-odt-image-size-probe-method
1226 until size
1227 do (setq size (org-e-odt-do-image-size
1228 probe-method file dpi embed-as)))
1229 (or size (error "Cannot determine Image size. Aborting ..."))
1230 (setq width (car size) height (cdr size)))
1231 (cond
1232 (scale
1233 (setq width (* width scale) height (* height scale)))
1234 ((and user-height user-width)
1235 (setq width user-width height user-height))
1236 (user-height
1237 (setq width (* user-height (/ width height)) height user-height))
1238 (user-width
1239 (setq height (* user-width (/ height width)) width user-width))
1240 (t (ignore)))
1241 ;; ensure that an embedded image fits comfortably within a page
1242 (let ((max-width (car org-export-e-odt-max-image-size))
1243 (max-height (cdr org-export-e-odt-max-image-size)))
1244 (when (or (> width max-width) (> height max-height))
1245 (let* ((scale1 (/ max-width width))
1246 (scale2 (/ max-height height))
1247 (scale (min scale1 scale2)))
1248 (setq width (* scale width) height (* scale height)))))
1249 (cons width height)))
1251 (defun org-e-odt-get-label-category-and-style (label default-category)
1252 "See `org-export-e-odt-get-category-from-label'."
1253 (let ((default-category-map
1254 (assoc default-category org-e-odt-category-map-alist))
1255 user-category user-category-map category)
1256 (cond
1257 ((not org-export-e-odt-get-category-from-label)
1258 default-category-map)
1259 ((not (setq user-category
1260 (save-match-data
1261 (and (string-match "\\`\\(.*\\):.+" label)
1262 (match-string 1 label)))))
1263 default-category-map)
1265 (setq user-category-map
1266 (or (assoc user-category org-e-odt-category-map-alist)
1267 (list nil user-category "category-and-value"))
1268 category (nth 1 user-category-map))
1269 (if (member category org-export-e-odt-user-categories)
1270 user-category-map
1271 default-category-map)))))
1273 (defun org-e-odt-add-label-definition (label default-category)
1274 "Create an entry in `org-e-odt-entity-labels-alist' and return it."
1275 (setq label (substring-no-properties label))
1276 (let* ((label-props (org-e-odt-get-label-category-and-style
1277 label default-category))
1278 (category (nth 1 label-props))
1279 (counter category)
1280 (label-style (nth 2 label-props))
1281 (sequence-var (intern (mapconcat
1282 'downcase
1283 (org-split-string counter) "-")))
1284 (seqno (1+ (or (plist-get org-e-odt-entity-counts-plist sequence-var)
1285 0)))
1286 (label-props (list label category seqno label-style)))
1287 (setq org-e-odt-entity-counts-plist
1288 (plist-put org-e-odt-entity-counts-plist sequence-var seqno))
1289 (push label-props org-e-odt-entity-labels-alist)
1290 label-props))
1292 (defun org-e-odt-format-label-definition (caption label category seqno label-style)
1293 (assert label)
1294 (format-spec
1295 (cadr (assoc-string label-style org-e-odt-label-styles t))
1296 `((?e . ,category)
1297 (?n . ,(org-e-odt-format-tags
1298 '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" . "</text:sequence>")
1299 (format "%d" seqno) label category category))
1300 (?c . ,(or (and caption (concat ": " caption)) "")))))
1302 (defun org-e-odt-format-label-reference (label category seqno label-style)
1303 (assert label)
1304 (save-match-data
1305 (let* ((fmt (cddr (assoc-string label-style org-e-odt-label-styles t)))
1306 (fmt1 (car fmt))
1307 (fmt2 (cadr fmt)))
1308 (org-e-odt-format-tags
1309 '("<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">"
1310 . "</text:sequence-ref>")
1311 (format-spec fmt2 `((?e . ,category)
1312 (?n . ,(format "%d" seqno)))) fmt1 label))))
1314 (defun org-e-odt-fixup-label-references ()
1315 (goto-char (point-min))
1316 (while (re-search-forward
1317 "<text:sequence-ref text:ref-name=\"\\([^\"]+\\)\">[ \t\n]*</text:sequence-ref>"
1318 nil t)
1319 (let* ((label (match-string 1))
1320 (label-def (assoc label org-e-odt-entity-labels-alist))
1321 (rpl (and label-def
1322 (apply 'org-e-odt-format-label-reference label-def))))
1323 (if rpl (replace-match rpl t t)
1324 (org-lparse-warn
1325 (format "Unable to resolve reference to label \"%s\"" label))))))
1327 (defun org-e-odt-format-entity-caption (label caption category)
1328 (or (and label
1329 (apply 'org-e-odt-format-label-definition
1330 caption (org-e-odt-add-label-definition label category)))
1331 caption ""))
1333 (defun org-e-odt-format-tags-1 (tag text prefix suffix &rest args)
1334 (cond
1335 ((consp tag)
1336 (concat prefix (apply 'format (car tag) args) text suffix
1337 (format (cdr tag))))
1338 ((stringp tag) ; singleton tag
1339 (concat prefix (apply 'format tag args) text))))
1341 (defun org-e-odt-format-tags (tag text &rest args)
1342 (apply 'org-e-odt-format-tags-1 tag text "\n" "\n" args))
1344 (defun org-e-odt-init-outfile ()
1345 (unless (executable-find "zip")
1346 ;; Not at all OSes ship with zip by default
1347 (error "Executable \"zip\" needed for creating OpenDocument files"))
1349 (let* ((outdir (make-temp-file
1350 (format org-export-e-odt-tmpdir-prefix org-lparse-backend) t))
1351 (content-file (expand-file-name "content.xml" outdir)))
1353 ;; reset variables
1354 (setq org-e-odt-manifest-file-entries nil
1355 org-e-odt-embedded-images-count 0
1356 org-e-odt-embedded-formulas-count 0
1357 org-e-odt-section-count 0
1358 org-e-odt-entity-labels-alist nil
1359 org-e-odt-list-stack-stashed nil
1360 org-e-odt-automatic-styles nil
1361 org-e-odt-object-counters nil
1362 org-e-odt-entity-counts-plist nil)
1364 ;; let `htmlfontify' know that we are interested in collecting
1365 ;; styles - FIXME
1367 (setq hfy-user-sheet-assoc nil)
1369 ;; init conten.xml
1370 (with-current-buffer
1371 (find-file-noselect content-file t)
1372 (current-buffer))))
1376 (defun org-e-odt-save-as-outfile (target opt-plist)
1377 ;; write automatic styles
1378 (org-e-odt-write-automatic-styles)
1380 ;; write styles file
1381 ;; (when (equal org-lparse-backend 'odt) FIXME
1382 ;; )
1384 (org-e-odt-update-styles-file opt-plist)
1386 ;; create mimetype file
1387 (let ((mimetype (org-e-odt-write-mimetype-file ;; org-lparse-backend FIXME
1388 'odt)))
1389 (org-e-odt-create-manifest-file-entry mimetype "/" "1.2"))
1391 ;; create a manifest entry for content.xml
1392 (org-e-odt-create-manifest-file-entry "text/xml" "content.xml")
1394 ;; write out the manifest entries before zipping
1395 (org-e-odt-write-manifest-file)
1397 (let ((xml-files '("mimetype" "META-INF/manifest.xml" "content.xml"
1398 "meta.xml"))
1399 (zipdir default-directory))
1400 (when (or t (equal org-lparse-backend 'odt)) ; FIXME
1401 (push "styles.xml" xml-files))
1402 (message "Switching to directory %s" (expand-file-name zipdir))
1404 ;; save all xml files
1405 (mapc (lambda (file)
1406 (with-current-buffer
1407 (find-file-noselect (expand-file-name file) t)
1408 ;; prettify output if needed
1409 (when org-export-e-odt-prettify-xml
1410 (indent-region (point-min) (point-max)))
1411 (save-buffer 0)))
1412 xml-files)
1414 (let* ((target-name (file-name-nondirectory target))
1415 (target-dir (file-name-directory target))
1416 (cmds `(("zip" "-mX0" ,target-name "mimetype")
1417 ("zip" "-rmTq" ,target-name "."))))
1418 (when (file-exists-p target)
1419 ;; FIXME: If the file is locked this throws a cryptic error
1420 (delete-file target))
1422 (let ((coding-system-for-write 'no-conversion) exitcode err-string)
1423 (message "Creating odt file...")
1424 (mapc
1425 (lambda (cmd)
1426 (message "Running %s" (mapconcat 'identity cmd " "))
1427 (setq err-string
1428 (with-output-to-string
1429 (setq exitcode
1430 (apply 'call-process (car cmd)
1431 nil standard-output nil (cdr cmd)))))
1432 (or (zerop exitcode)
1433 (ignore (message "%s" err-string))
1434 (error "Unable to create odt file (%S)" exitcode)))
1435 cmds))
1437 ;; move the file from outdir to target-dir
1438 (rename-file target-name target-dir)
1440 ;; kill all xml buffers
1441 (mapc (lambda (file)
1442 (kill-buffer
1443 (find-file-noselect (expand-file-name file zipdir) t)))
1444 xml-files)
1446 (delete-directory zipdir)))
1447 (message "Created %s" target)
1448 (set-buffer (find-file-noselect target t)))
1451 (defun org-e-odt-create-manifest-file-entry (&rest args)
1452 (push args org-e-odt-manifest-file-entries))
1454 (defun org-e-odt-write-manifest-file ()
1455 (make-directory "META-INF")
1456 (let ((manifest-file (expand-file-name "META-INF/manifest.xml")))
1457 (with-current-buffer
1458 (find-file-noselect manifest-file t)
1459 (insert
1460 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
1461 <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
1462 (mapc
1463 (lambda (file-entry)
1464 (let* ((version (nth 2 file-entry))
1465 (extra (if version
1466 (format " manifest:version=\"%s\"" version)
1467 "")))
1468 (insert
1469 (format org-e-odt-manifest-file-entry-tag
1470 (nth 0 file-entry) (nth 1 file-entry) extra))))
1471 org-e-odt-manifest-file-entries)
1472 (insert "\n</manifest:manifest>"))))
1474 (defun org-e-odt-update-meta-file (info) ; FIXME opt-plist
1475 (let ((date (org-e-odt-format-date (plist-get info :date)))
1476 (author (or (plist-get info :author) ""))
1477 (email (plist-get info :email))
1478 (keywords (plist-get info :keywords))
1479 (description (plist-get info :description))
1480 (title (plist-get info :title)))
1481 (write-region
1482 (concat
1483 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
1484 <office:document-meta
1485 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
1486 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
1487 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
1488 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
1489 xmlns:ooo=\"http://openoffice.org/2004/office\"
1490 office:version=\"1.2\">
1491 <office:meta>\n"
1492 (org-e-odt-format-author author) "\n"
1493 (format "<meta:initial-creator>%s</meta:initial-creator>\n" author)
1494 (format "<dc:date>%s</dc:date>\n" date)
1495 (format "<meta:creation-date>%s</meta:creation-date>\n" date)
1496 (format "<meta:generator>%s</meta:generator>\n"
1497 (when org-export-creator-info
1498 (format "Org-%s/Emacs-%s"
1499 org-version emacs-version)))
1500 (format "<meta:keyword>%s</meta:keyword>\n" keywords)
1501 (format "<dc:subject>%s</dc:subject>\n" description)
1502 (format "<dc:title>%s</dc:title>\n" title)
1503 "\n"
1504 " </office:meta>\n" "</office:document-meta>")
1505 nil (expand-file-name "meta.xml")))
1507 ;; create a manifest entry for meta.xml
1508 (org-e-odt-create-manifest-file-entry "text/xml" "meta.xml"))
1510 (defun org-e-odt-update-styles-file (opt-plist)
1511 ;; write styles file
1512 (let ((styles-file (plist-get opt-plist :odt-styles-file)))
1513 (org-e-odt-copy-styles-file (and styles-file
1514 (read (org-trim styles-file)))))
1516 ;; Update styles.xml - take care of outline numbering
1517 (with-current-buffer
1518 (find-file-noselect (expand-file-name "styles.xml") t)
1519 ;; Don't make automatic backup of styles.xml file. This setting
1520 ;; prevents the backed-up styles.xml file from being zipped in to
1521 ;; odt file. This is more of a hackish fix. Better alternative
1522 ;; would be to fix the zip command so that the output odt file
1523 ;; includes only the needed files and excludes any auto-generated
1524 ;; extra files like backups and auto-saves etc etc. Note that
1525 ;; currently the zip command zips up the entire temp directory so
1526 ;; that any auto-generated files created under the hood ends up in
1527 ;; the resulting odt file.
1528 (set (make-local-variable 'backup-inhibited) t)
1530 ;; Import local setting of `org-export-with-section-numbers'
1531 (org-e-odt-configure-outline-numbering
1532 (if org-export-with-section-numbers org-export-headline-levels 0)))
1534 ;; Write custom styles for source blocks
1535 (org-e-odt-insert-custom-styles-for-srcblocks
1536 (mapconcat
1537 (lambda (style)
1538 (format " %s\n" (cddr style)))
1539 hfy-user-sheet-assoc "")))
1541 (defun org-e-odt-write-mimetype-file (format)
1542 ;; create mimetype file
1543 (let ((mimetype
1544 (case format
1545 (odt "application/vnd.oasis.opendocument.text")
1546 (odf "application/vnd.oasis.opendocument.formula")
1547 (t (error "Unknown OpenDocument backend %S" org-lparse-backend)))))
1548 (write-region mimetype nil (expand-file-name "mimetype"))
1549 mimetype))
1551 (defun org-e-odt-finalize-outfile ()
1552 (org-e-odt-delete-empty-paragraphs))
1554 (defun org-e-odt-delete-empty-paragraphs ()
1555 (goto-char (point-min))
1556 (let ((open "<text:p[^>]*>")
1557 (close "</text:p>"))
1558 (while (re-search-forward (format "%s[ \r\n\t]*%s" open close) nil t)
1559 (replace-match ""))))
1561 (declare-function org-create-math-formula "org"
1562 (latex-frag &optional mathml-file))
1564 ;;;###autoload
1565 (defun org-export-e-odt-convert (&optional in-file out-fmt prefix-arg)
1566 "Convert IN-FILE to format OUT-FMT using a command line converter.
1567 IN-FILE is the file to be converted. If unspecified, it defaults
1568 to variable `buffer-file-name'. OUT-FMT is the desired output
1569 format. Use `org-export-e-odt-convert-process' as the converter.
1570 If PREFIX-ARG is non-nil then the newly converted file is opened
1571 using `org-open-file'."
1572 (interactive
1573 (append (org-lparse-convert-read-params) current-prefix-arg))
1574 (org-lparse-do-convert in-file out-fmt prefix-arg))
1576 (defun org-e-odt-get (what &optional opt-plist)
1577 (case what
1578 (BACKEND 'odt)
1579 (EXPORT-DIR (org-export-directory :html opt-plist))
1580 (FILE-NAME-EXTENSION "odt")
1581 (EXPORT-BUFFER-NAME "*Org ODT Export*")
1582 (ENTITY-CONTROL org-e-odt-entity-control-callbacks-alist)
1583 (ENTITY-FORMAT org-e-odt-entity-format-callbacks-alist)
1584 (INIT-METHOD 'org-e-odt-init-outfile)
1585 (FINAL-METHOD 'org-e-odt-finalize-outfile)
1586 (SAVE-METHOD 'org-e-odt-save-as-outfile)
1587 (CONVERT-METHOD
1588 (and org-export-e-odt-convert-process
1589 (cadr (assoc-string org-export-e-odt-convert-process
1590 org-export-e-odt-convert-processes t))))
1591 (CONVERT-CAPABILITIES
1592 (and org-export-e-odt-convert-process
1593 (cadr (assoc-string org-export-e-odt-convert-process
1594 org-export-e-odt-convert-processes t))
1595 org-export-e-odt-convert-capabilities))
1596 (TOPLEVEL-HLEVEL 1)
1597 (SPECIAL-STRING-REGEXPS org-export-e-odt-special-string-regexps)
1598 (INLINE-IMAGES 'maybe)
1599 (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
1600 (PLAIN-TEXT-MAP '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
1601 (TABLE-FIRST-COLUMN-AS-LABELS nil)
1602 (FOOTNOTE-SEPARATOR )
1603 (CODING-SYSTEM-FOR-WRITE 'utf-8)
1604 (CODING-SYSTEM-FOR-SAVE 'utf-8)
1605 (t (error "Unknown property: %s" what))))
1607 (defun org-export-e-odt-do-preprocess-latex-fragments ()
1608 "Convert LaTeX fragments to images."
1609 (let* ((latex-frag-opt (plist-get org-lparse-opt-plist :LaTeX-fragments))
1610 (latex-frag-opt ; massage the options
1611 (or (and (member latex-frag-opt '(mathjax t))
1612 (not (and (fboundp 'org-format-latex-mathml-available-p)
1613 (org-format-latex-mathml-available-p)))
1614 (prog1 org-lparse-latex-fragment-fallback
1615 (org-lparse-warn
1616 (concat
1617 "LaTeX to MathML converter not available. "
1618 (format "Using %S instead."
1619 org-lparse-latex-fragment-fallback)))))
1620 latex-frag-opt))
1621 cache-dir display-msg)
1622 (cond
1623 ((eq latex-frag-opt 'dvipng)
1624 (setq cache-dir "ltxpng/")
1625 (setq display-msg "Creating LaTeX image %s"))
1626 ((member latex-frag-opt '(mathjax t))
1627 (setq latex-frag-opt 'mathml)
1628 (setq cache-dir "ltxmathml/")
1629 (setq display-msg "Creating MathML formula %s")))
1630 (when (and org-current-export-file)
1631 (org-format-latex
1632 (concat cache-dir (file-name-sans-extension
1633 (file-name-nondirectory org-current-export-file)))
1634 org-current-export-dir nil display-msg
1635 nil nil latex-frag-opt))))
1637 (defadvice org-format-latex-as-mathml
1638 (after org-e-odt-protect-latex-fragment activate)
1639 "Encode LaTeX fragment as XML.
1640 Do this when translation to MathML fails."
1641 (when (or (not (> (length ad-return-value) 0))
1642 (get-text-property 0 'org-protected ad-return-value))
1643 (setq ad-return-value
1644 (org-propertize (org-e-odt-encode-plain-text (ad-get-arg 0))
1645 'org-protected t))))
1647 (defun org-export-e-odt-preprocess-latex-fragments ()
1648 (when (equal org-export-current-backend 'odt)
1649 (org-export-e-odt-do-preprocess-latex-fragments)))
1651 (defun org-export-e-odt-preprocess-label-references ()
1652 (goto-char (point-min))
1653 (let (label label-components category value pretty-label)
1654 (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t)
1655 (org-if-unprotected-at (match-beginning 1)
1656 (replace-match
1657 (let ((org-lparse-encode-pending t)
1658 (label (match-string 1)))
1659 ;; markup generated below is mostly an eye-candy. At
1660 ;; pre-processing stage, there is no information on which
1661 ;; entity a label reference points to. The actual markup
1662 ;; is generated as part of `org-e-odt-fixup-label-references'
1663 ;; which gets called at the fag end of export. By this
1664 ;; time we would have seen and collected all the label
1665 ;; definitions in `org-e-odt-entity-labels-alist'.
1666 (org-e-odt-format-tags
1667 '("<text:sequence-ref text:ref-name=\"%s\">" .
1668 "</text:sequence-ref>")
1669 "" (org-add-props label '(org-protected t)))) t t)))))
1671 ;; process latex fragments as part of
1672 ;; `org-export-preprocess-after-blockquote-hook'. Note that this hook
1673 ;; is the one that is closest and well before the call to
1674 ;; `org-export-attach-captions-and-attributes' in
1675 ;; `org-export-preprocess-string'. The above arrangement permits
1676 ;; captions, labels and attributes to be attached to png images
1677 ;; generated out of latex equations.
1678 (add-hook 'org-export-preprocess-after-blockquote-hook
1679 'org-export-e-odt-preprocess-latex-fragments)
1681 (defun org-export-e-odt-preprocess (parameters)
1682 (org-export-e-odt-preprocess-label-references))
1685 (defun org-e-odt-zip-extract-one (archive member &optional target)
1686 (require 'arc-mode)
1687 (let* ((target (or target default-directory))
1688 (archive (expand-file-name archive))
1689 (archive-zip-extract
1690 (list "unzip" "-qq" "-o" "-d" target))
1691 exit-code command-output)
1692 (setq command-output
1693 (with-temp-buffer
1694 (setq exit-code (archive-zip-extract archive member))
1695 (buffer-string)))
1696 (unless (zerop exit-code)
1697 (message command-output)
1698 (error "Extraction failed"))))
1700 (defun org-e-odt-zip-extract (archive members &optional target)
1701 (when (atom members) (setq members (list members)))
1702 (mapc (lambda (member)
1703 (org-e-odt-zip-extract-one archive member target))
1704 members))
1706 (defun org-e-odt-copy-styles-file (&optional styles-file)
1707 ;; Non-availability of styles.xml is not a critical error. For now
1708 ;; throw an error purely for aesthetic reasons.
1709 (setq styles-file (or styles-file
1710 org-export-e-odt-styles-file
1711 (expand-file-name "OrgOdtStyles.xml"
1712 org-e-odt-styles-dir)
1713 (error "org-e-odt: Missing styles file?")))
1714 (cond
1715 ((listp styles-file)
1716 (let ((archive (nth 0 styles-file))
1717 (members (nth 1 styles-file)))
1718 (org-e-odt-zip-extract archive members)
1719 (mapc
1720 (lambda (member)
1721 (when (org-file-image-p member)
1722 (let* ((image-type (file-name-extension member))
1723 (media-type (format "image/%s" image-type)))
1724 (org-e-odt-create-manifest-file-entry media-type member))))
1725 members)))
1726 ((and (stringp styles-file) (file-exists-p styles-file))
1727 (let ((styles-file-type (file-name-extension styles-file)))
1728 (cond
1729 ((string= styles-file-type "xml")
1730 (copy-file styles-file "styles.xml" t))
1731 ((member styles-file-type '("odt" "ott"))
1732 (org-e-odt-zip-extract styles-file "styles.xml")))))
1734 (error (format "Invalid specification of styles.xml file: %S"
1735 org-export-e-odt-styles-file))))
1737 ;; create a manifest entry for styles.xml
1738 (org-e-odt-create-manifest-file-entry "text/xml" "styles.xml"))
1740 (defun org-e-odt-configure-outline-numbering (level)
1741 "Outline numbering is retained only upto LEVEL.
1742 To disable outline numbering pass a LEVEL of 0."
1743 (goto-char (point-min))
1744 (let ((regex
1745 "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
1746 (replacement
1747 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
1748 (while (re-search-forward regex nil t)
1749 (when (> (string-to-number (match-string 2)) level)
1750 (replace-match replacement t nil))))
1751 (save-buffer 0))
1753 ;;;###autoload
1754 (defun org-export-as-odf (latex-frag &optional odf-file)
1755 "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
1756 Use `org-create-math-formula' to convert LATEX-FRAG first to
1757 MathML. When invoked as an interactive command, use
1758 `org-latex-regexps' to infer LATEX-FRAG from currently active
1759 region. If no LaTeX fragments are found, prompt for it. Push
1760 MathML source to kill ring, if `org-export-copy-to-kill-ring' is
1761 non-nil."
1762 (interactive
1763 `(,(let (frag)
1764 (setq frag (and (setq frag (and (region-active-p)
1765 (buffer-substring (region-beginning)
1766 (region-end))))
1767 (loop for e in org-latex-regexps
1768 thereis (when (string-match (nth 1 e) frag)
1769 (match-string (nth 2 e) frag)))))
1770 (read-string "LaTeX Fragment: " frag nil frag))
1771 ,(let ((odf-filename (expand-file-name
1772 (concat
1773 (file-name-sans-extension
1774 (or (file-name-nondirectory buffer-file-name)))
1775 "." "odf")
1776 (file-name-directory buffer-file-name))))
1777 (read-file-name "ODF filename: " nil odf-filename nil
1778 (file-name-nondirectory odf-filename)))))
1779 (let* ((org-lparse-backend 'odf)
1780 org-lparse-opt-plist
1781 (filename (or odf-file
1782 (expand-file-name
1783 (concat
1784 (file-name-sans-extension
1785 (or (file-name-nondirectory buffer-file-name)))
1786 "." "odf")
1787 (file-name-directory buffer-file-name))))
1788 (buffer (find-file-noselect (org-e-odt-init-outfile filename)))
1789 (coding-system-for-write 'utf-8)
1790 (save-buffer-coding-system 'utf-8))
1791 (set-buffer buffer)
1792 (set-buffer-file-coding-system coding-system-for-write)
1793 (let ((mathml (org-create-math-formula latex-frag)))
1794 (unless mathml (error "No Math formula created"))
1795 (insert mathml)
1796 (or (org-export-push-to-kill-ring
1797 (upcase (symbol-name org-lparse-backend)))
1798 (message "Exporting... done")))
1799 (org-e-odt-save-as-outfile filename nil ; FIXME
1802 ;;;###autoload
1803 (defun org-export-as-odf-and-open ()
1804 "Export LaTeX fragment as OpenDocument formula and immediately open it.
1805 Use `org-export-as-odf' to read LaTeX fragment and OpenDocument
1806 formula file."
1807 (interactive)
1808 (org-lparse-and-open
1809 nil nil nil (call-interactively 'org-export-as-odf)))
1814 ;;; Driver Starts here
1815 ;;; Dependencies
1817 (require 'format-spec)
1818 (eval-when-compile (require 'cl) (require 'table))
1822 ;;; Hooks
1824 (defvar org-e-odt-after-blockquotes-hook nil
1825 "Hook run during HTML export, after blockquote, verse, center are done.")
1827 (defvar org-e-odt-final-hook nil
1828 "Hook run at the end of HTML export, in the new buffer.")
1830 ;; FIXME: it already exists in org-e-odt.el
1831 ;;; Function Declarations
1833 (declare-function org-element-property "org-element" (property element))
1834 (declare-function org-element-normalize-string "org-element" (s))
1835 (declare-function org-element-parse-secondary-string
1836 "org-element" (string restriction &optional buffer))
1837 (defvar org-element-string-restrictions)
1839 (declare-function org-export-clean-table "org-export" (table specialp))
1840 (declare-function org-export-data "org-export" (data backend info))
1841 (declare-function org-export-directory "org-export" (type plist))
1842 (declare-function org-export-expand-macro "org-export" (macro info))
1843 (declare-function org-export-first-sibling-p "org-export" (headline info))
1844 (declare-function org-export-footnote-first-reference-p "org-export"
1845 (footnote-reference info))
1846 (declare-function org-export-get-coderef-format "org-export" (path desc))
1847 (declare-function org-export-get-footnote-definition "org-export"
1848 (footnote-reference info))
1849 (declare-function org-export-get-footnote-number "org-export" (footnote info))
1850 (declare-function org-export-get-previous-element "org-export" (blob info))
1851 (declare-function org-export-get-relative-level "org-export" (headline info))
1852 (declare-function org-export-handle-code
1853 "org-export" (element info &optional num-fmt ref-fmt delayed))
1854 (declare-function org-export-included-file "org-export" (keyword backend info))
1855 (declare-function org-export-inline-image-p "org-export"
1856 (link &optional extensions))
1857 (declare-function org-export-last-sibling-p "org-export" (headline info))
1858 (declare-function org-export-low-level-p "org-export" (headline info))
1859 (declare-function org-export-output-file-name
1860 "org-export" (extension &optional subtreep pub-dir))
1861 (declare-function org-export-resolve-coderef "org-export" (ref info))
1862 (declare-function org-export-resolve-fuzzy-link "org-export" (link info))
1863 (declare-function org-export-secondary-string "org-export"
1864 (secondary backend info))
1865 (declare-function org-export-solidify-link-text "org-export" (s))
1866 (declare-function org-export-table-format-info "org-export" (table))
1867 (declare-function
1868 org-export-to-buffer "org-export"
1869 (backend buffer &optional subtreep visible-only body-only ext-plist))
1870 (declare-function
1871 org-export-to-file "org-export"
1872 (backend file &optional subtreep visible-only body-only ext-plist))
1874 (declare-function org-id-find-id-file "org-id" (id))
1875 (declare-function htmlize-region "ext:htmlize" (beg end))
1876 (declare-function org-pop-to-buffer-same-window
1877 "org-compat" (&optional buffer-or-name norecord label))
1883 (declare-function hfy-face-to-style "htmlfontify" (fn))
1884 (declare-function hfy-face-or-def-to-name "htmlfontify" (fn))
1885 (declare-function archive-zip-extract "arc-mode.el" (archive name))
1887 ;;; Internal Variables
1889 ;;;; ODT Internal Variables
1891 (defconst org-e-odt-lib-dir
1892 (file-name-directory load-file-name)
1893 "Location of ODT exporter.
1894 Use this to infer values of `org-e-odt-styles-dir' and
1895 `org-export-e-odt-schema-dir'.")
1897 (defvar org-e-odt-data-dir
1898 (expand-file-name "../etc/" org-e-odt-lib-dir)
1899 "Data directory for ODT exporter.
1900 Use this to infer values of `org-e-odt-styles-dir' and
1901 `org-export-e-odt-schema-dir'.")
1906 (defconst org-export-e-odt-special-string-regexps
1907 '(("\\\\-" . "&#x00ad;\\1") ; shy
1908 ("---\\([^-]\\)" . "&#x2014;\\1") ; mdash
1909 ("--\\([^-]\\)" . "&#x2013;\\1") ; ndash
1910 ("\\.\\.\\." . "&#x2026;")) ; hellip
1911 "Regular expressions for special string conversion.")
1913 (defconst org-e-odt-schema-dir-list
1914 (list
1915 (and org-e-odt-data-dir
1916 (expand-file-name "./schema/" org-e-odt-data-dir)) ; bail out
1917 (eval-when-compile
1918 (and (boundp 'org-e-odt-data-dir) org-e-odt-data-dir ; see make install
1919 (expand-file-name "./schema/" org-e-odt-data-dir)))
1920 (expand-file-name "../contrib/odt/etc/schema/" org-e-odt-lib-dir) ; git
1922 "List of directories to search for OpenDocument schema files.
1923 Use this list to set the default value of
1924 `org-export-e-odt-schema-dir'. The entries in this list are
1925 populated heuristically based on the values of `org-e-odt-lib-dir'
1926 and `org-e-odt-data-dir'.")
1929 (defconst org-e-odt-styles-dir-list
1930 (list
1931 (and org-e-odt-data-dir
1932 (expand-file-name "./styles/" org-e-odt-data-dir)) ; bail out
1933 (eval-when-compile
1934 (and (boundp 'org-e-odt-data-dir) org-e-odt-data-dir ; see make install
1935 (expand-file-name "./styles/" org-e-odt-data-dir)))
1936 (expand-file-name "../etc/styles/" org-e-odt-lib-dir) ; git
1937 (expand-file-name "./etc/styles/" org-e-odt-lib-dir) ; elpa
1938 (expand-file-name "./org/" data-directory) ; system
1940 "List of directories to search for OpenDocument styles files.
1941 See `org-e-odt-styles-dir'. The entries in this list are populated
1942 heuristically based on the values of `org-e-odt-lib-dir' and
1943 `org-e-odt-data-dir'.")
1945 (defconst org-e-odt-styles-dir
1946 (let* ((styles-dir
1947 (catch 'styles-dir
1948 (message "Debug (org-e-odt): Searching for OpenDocument styles files...")
1949 (mapc (lambda (styles-dir)
1950 (when styles-dir
1951 (message "Debug (org-e-odt): Trying %s..." styles-dir)
1952 (when (and (file-readable-p
1953 (expand-file-name
1954 "OrgOdtContentTemplate.xml" styles-dir))
1955 (file-readable-p
1956 (expand-file-name
1957 "OrgOdtStyles.xml" styles-dir)))
1958 (message "Debug (org-e-odt): Using styles under %s"
1959 styles-dir)
1960 (throw 'styles-dir styles-dir))))
1961 org-e-odt-styles-dir-list)
1962 nil)))
1963 (unless styles-dir
1964 (error "Error (org-e-odt): Cannot find factory styles files. Aborting."))
1965 styles-dir)
1966 "Directory that holds auxiliary XML files used by the ODT exporter.
1968 This directory contains the following XML files -
1969 \"OrgOdtStyles.xml\" and \"OrgOdtContentTemplate.xml\". These
1970 XML files are used as the default values of
1971 `org-export-e-odt-styles-file' and
1972 `org-export-e-odt-content-template-file'.
1974 The default value of this variable varies depending on the
1975 version of org in use and is initialized from
1976 `org-e-odt-styles-dir-list'. Note that the user could be using org
1977 from one of: org's own private git repository, GNU ELPA tar or
1978 standard Emacs.")
1980 (defconst org-export-e-odt-tmpdir-prefix "%s-")
1981 (defconst org-export-e-odt-bookmark-prefix "OrgXref.")
1983 (defconst org-e-odt-manifest-file-entry-tag
1985 <manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
1989 (defvar org-lparse-dyn-first-heading-pos) ; let bound during org-do-lparse
1991 (defvar org-e-odt-suppress-xref nil)
1992 (defvar org-e-odt-file-extensions
1993 '(("odt" . "OpenDocument Text")
1994 ("ott" . "OpenDocument Text Template")
1995 ("odm" . "OpenDocument Master Document")
1996 ("ods" . "OpenDocument Spreadsheet")
1997 ("ots" . "OpenDocument Spreadsheet Template")
1998 ("odg" . "OpenDocument Drawing (Graphics)")
1999 ("otg" . "OpenDocument Drawing Template")
2000 ("odp" . "OpenDocument Presentation")
2001 ("otp" . "OpenDocument Presentation Template")
2002 ("odi" . "OpenDocument Image")
2003 ("odf" . "OpenDocument Formula")
2004 ("odc" . "OpenDocument Chart")))
2006 (defvar org-export-e-odt-embed-images t
2007 "Should the images be copied in to the odt file or just linked?")
2009 (defvar org-export-e-odt-inline-images 'maybe)
2010 (defvar org-export-e-odt-default-org-styles-alist
2011 '((paragraph . ((default . "Text_20_body")
2012 (fixedwidth . "OrgFixedWidthBlock")
2013 (verse . "OrgVerse")
2014 (quote . "Quotations")
2015 (blockquote . "Quotations")
2016 (center . "OrgCenter")
2017 (left . "OrgLeft")
2018 (right . "OrgRight")
2019 (title . "OrgTitle")
2020 (subtitle . "OrgSubtitle")
2021 (footnote . "Footnote")
2022 (src . "OrgSrcBlock")
2023 (illustration . "Illustration")
2024 (table . "Table")
2025 (definition-term . "Text_20_body_20_bold")
2026 (horizontal-line . "Horizontal_20_Line")))
2027 (character . ((bold . "Bold")
2028 (emphasis . "Emphasis")
2029 (code . "OrgCode")
2030 (verbatim . "OrgCode")
2031 (strike . "Strikethrough")
2032 (underline . "Underline")
2033 (subscript . "OrgSubscript")
2034 (superscript . "OrgSuperscript")))
2035 (list . ((ordered . "OrgNumberedList")
2036 (unordered . "OrgBulletedList")
2037 (descriptive . "OrgDescriptionList"))))
2038 "Default styles for various entities.")
2040 (defvar org-export-e-odt-org-styles-alist org-export-e-odt-default-org-styles-alist)
2042 ;;;_. callbacks
2043 ;;;_. control callbacks
2044 ;;;_ , document body
2046 (defvar org-lparse-body-only) ; let bound during org-do-lparse
2047 (defvar org-lparse-opt-plist) ; bound during org-do-lparse
2048 (defvar org-lparse-list-stack) ; dynamically bound in org-do-lparse
2049 (defvar org-e-odt-list-stack-stashed)
2050 (defvar org-lparse-table-ncols)
2051 (defvar org-e-odt-table-rowgrp-open)
2052 (defvar org-e-odt-table-rownum)
2053 (defvar org-e-odt-table-cur-rowgrp-is-hdr)
2054 (defvar org-lparse-table-is-styled)
2055 (defvar org-lparse-table-rowgrp-info)
2056 (defvar org-lparse-table-colalign-vector)
2058 (defvar org-e-odt-table-style nil
2059 "Table style specified by \"#+ATTR_ODT: <style-name>\" line.
2060 This is set during `org-e-odt-begin-table'.")
2062 (defvar org-e-odt-table-style-spec nil
2063 "Entry for `org-e-odt-table-style' in `org-export-e-odt-table-styles'.")
2066 (defvar org-e-odt-table-style-format
2068 <style:style style:name=\"%s\" style:family=\"table\">
2069 <style:table-properties style:rel-width=\"%d%%\" fo:margin-top=\"0cm\" fo:margin-bottom=\"0.20cm\" table:align=\"center\"/>
2070 </style:style>
2072 "Template for auto-generated Table styles.")
2074 (defvar org-e-odt-automatic-styles '()
2075 "Registry of automatic styles for various OBJECT-TYPEs.
2076 The variable has the following form:
2077 \(\(OBJECT-TYPE-A
2078 \(\(OBJECT-NAME-A.1 OBJECT-PROPS-A.1\)
2079 \(OBJECT-NAME-A.2 OBJECT-PROPS-A.2\) ...\)\)
2080 \(OBJECT-TYPE-B
2081 \(\(OBJECT-NAME-B.1 OBJECT-PROPS-B.1\)
2082 \(OBJECT-NAME-B.2 OBJECT-PROPS-B.2\) ...\)\)
2083 ...\).
2085 OBJECT-TYPEs could be \"Section\", \"Table\", \"Figure\" etc.
2086 OBJECT-PROPS is (typically) a plist created by passing
2087 \"#+ATTR_ODT: \" option to `org-lparse-get-block-params'.
2089 Use `org-e-odt-add-automatic-style' to add update this variable.'")
2091 (defvar org-e-odt-object-counters nil
2092 "Running counters for various OBJECT-TYPEs.
2093 Use this to generate automatic names and style-names. See
2094 `org-e-odt-add-automatic-style'.")
2096 (defvar org-e-odt-table-indentedp nil)
2097 (defvar org-lparse-table-colalign-info)
2098 (defvar org-lparse-link-description-is-image nil)
2101 (defvar org-src-block-paragraph-format
2102 "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
2103 <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
2104 <style:background-image/>
2105 </style:paragraph-properties>
2106 <style:text-properties fo:color=\"%s\"/>
2107 </style:style>"
2108 "Custom paragraph style for colorized source and example blocks.
2109 This style is much the same as that of \"OrgFixedWidthBlock\"
2110 except that the foreground and background colors are set
2111 according to the default face identified by the `htmlfontify'.")
2113 (defvar hfy-optimisations)
2114 (defvar org-e-odt-embedded-formulas-count 0)
2115 (defvar org-e-odt-entity-frame-styles
2116 '(("As-CharImage" "__Figure__" ("OrgInlineImage" nil "as-char"))
2117 ("ParagraphImage" "__Figure__" ("OrgDisplayImage" nil "paragraph"))
2118 ("PageImage" "__Figure__" ("OrgPageImage" nil "page"))
2119 ("CaptionedAs-CharImage" "__Figure__"
2120 ("OrgCaptionedImage"
2121 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
2122 ("OrgInlineImage" nil "as-char"))
2123 ("CaptionedParagraphImage" "__Figure__"
2124 ("OrgCaptionedImage"
2125 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
2126 ("OrgImageCaptionFrame" nil "paragraph"))
2127 ("CaptionedPageImage" "__Figure__"
2128 ("OrgCaptionedImage"
2129 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
2130 ("OrgPageImageCaptionFrame" nil "page"))
2131 ("InlineFormula" "__MathFormula__" ("OrgInlineFormula" nil "as-char"))
2132 ("DisplayFormula" "__MathFormula__" ("OrgDisplayFormula" nil "as-char"))
2133 ("CaptionedDisplayFormula" "__MathFormula__"
2134 ("OrgCaptionedFormula" nil "paragraph")
2135 ("OrgFormulaCaptionFrame" nil "as-char"))))
2137 (defvar org-e-odt-embedded-images-count 0)
2139 (defvar org-export-e-odt-image-size-probe-method
2140 (append (and (executable-find "identify") '(imagemagick)) ; See Bug#10675
2141 '(emacs fixed))
2142 "Ordered list of methods for determining image sizes.")
2144 (defvar org-export-e-odt-default-image-sizes-alist
2145 '(("as-char" . (5 . 0.4))
2146 ("paragraph" . (5 . 5)))
2147 "Hardcoded image dimensions one for each of the anchor
2148 methods.")
2150 ;; A4 page size is 21.0 by 29.7 cms
2151 ;; The default page settings has 2cm margin on each of the sides. So
2152 ;; the effective text area is 17.0 by 25.7 cm
2153 (defvar org-export-e-odt-max-image-size '(17.0 . 20.0)
2154 "Limiting dimensions for an embedded image.")
2156 (defvar org-e-odt-entity-labels-alist nil
2157 "Associate Labels with the Labeled entities.
2158 Each element of the alist is of the form (LABEL-NAME
2159 CATEGORY-NAME SEQNO LABEL-STYLE-NAME). LABEL-NAME is same as
2160 that specified by \"#+LABEL: ...\" line. CATEGORY-NAME is the
2161 type of the entity that LABEL-NAME is attached to. CATEGORY-NAME
2162 can be one of \"Table\", \"Figure\" or \"Equation\". SEQNO is
2163 the unique number assigned to the referenced entity on a
2164 per-CATEGORY basis. It is generated sequentially and is 1-based.
2165 LABEL-STYLE-NAME is a key `org-e-odt-label-styles'.
2167 See `org-e-odt-add-label-definition' and
2168 `org-e-odt-fixup-label-references'.")
2170 (defvar org-e-odt-entity-counts-plist nil
2171 "Plist of running counters of SEQNOs for each of the CATEGORY-NAMEs.
2172 See `org-e-odt-entity-labels-alist' for known CATEGORY-NAMEs.")
2174 (defvar org-e-odt-label-styles
2175 '(("text" "(%n)" "text" "(%n)")
2176 ("category-and-value" "%e %n%c" "category-and-value" "%e %n")
2177 ("value" "%e %n%c" "value" "%n"))
2178 "Specify how labels are applied and referenced.
2179 This is an alist where each element is of the
2180 form (LABEL-STYLE-NAME LABEL-ATTACH-FMT LABEL-REF-MODE
2181 LABEL-REF-FMT).
2183 LABEL-ATTACH-FMT controls how labels and captions are attached to
2184 an entity. It may contain following specifiers - %e, %n and %c.
2185 %e is replaced with the CATEGORY-NAME. %n is replaced with
2186 \"<text:sequence ...> SEQNO </text:sequence>\". %c is replaced
2187 with CAPTION. See `org-e-odt-format-label-definition'.
2189 LABEL-REF-MODE and LABEL-REF-FMT controls how label references
2190 are generated. The following XML is generated for a label
2191 reference - \"<text:sequence-ref
2192 text:reference-format=\"LABEL-REF-MODE\" ...> LABEL-REF-FMT
2193 </text:sequence-ref>\". LABEL-REF-FMT may contain following
2194 specifiers - %e and %n. %e is replaced with the CATEGORY-NAME.
2195 %n is replaced with SEQNO. See
2196 `org-e-odt-format-label-reference'.")
2198 (defvar org-e-odt-category-map-alist
2199 '(("__Table__" "Table" "value")
2200 ("__Figure__" "Figure" "value")
2201 ("__MathFormula__" "Equation" "text")
2202 ("__DvipngImage__" "Equation" "value")
2203 ;; ("__Table__" "Table" "category-and-value")
2204 ;; ("__Figure__" "Figure" "category-and-value")
2205 ;; ("__DvipngImage__" "Equation" "category-and-value")
2207 "Map a CATEGORY-HANDLE to CATEGORY-NAME and LABEL-STYLE.
2208 This is an alist where each element is of the form
2209 \\(CATEGORY-HANDLE CATEGORY-NAME LABEL-STYLE\\). CATEGORY_HANDLE
2210 could either be one of the internal handles (as seen above) or be
2211 derived from the \"#+LABEL:<label-name>\" specification. See
2212 `org-export-e-odt-get-category-from-label'. CATEGORY-NAME and
2213 LABEL-STYLE are used for generating ODT labels. See
2214 `org-e-odt-label-styles'.")
2216 (defvar org-export-e-odt-user-categories
2217 '("Illustration" "Table" "Text" "Drawing" "Equation" "Figure"))
2219 (defvar org-export-e-odt-get-category-from-label nil
2220 "Should category of label be inferred from label itself.
2221 When this option is non-nil, a label is parsed in to two
2222 component parts delimited by a \":\" (colon) as shown here -
2223 #+LABEL:[CATEGORY-HANDLE:]EXTRA. The CATEGORY-HANDLE is mapped
2224 to a CATEGORY-NAME and LABEL-STYLE using
2225 `org-e-odt-category-map-alist'. (If no such map is provided and
2226 CATEGORY-NAME is set to CATEGORY-HANDLE and LABEL-STYLE is set to
2227 \"category-and-value\"). If CATEGORY-NAME so obtained is listed
2228 under `org-export-e-odt-user-categories' then the user specified
2229 styles are used. Otherwise styles as determined by the internal
2230 CATEGORY-HANDLE is used. See
2231 `org-e-odt-get-label-category-and-style' for details.")
2233 (defvar org-e-odt-manifest-file-entries nil)
2234 (defvar hfy-user-sheet-assoc) ; bound during org-do-lparse
2235 (defvar org-lparse-latex-fragment-fallback) ; set by org-do-lparse
2238 ;;;; HTML Internal Variables
2240 (defvar org-e-odt-option-alist
2242 ;; (:agenda-style nil nil org-agenda-export-html-style)
2243 ;; (:convert-org-links nil nil org-e-odt-link-org-files-as-html)
2244 ;; ;; FIXME Use (org-xml-encode-org-text-skip-links s) ??
2245 ;; ;; (:expand-quoted-html nil "@" org-e-odt-expand)
2246 ;; (:inline-images nil nil org-e-odt-inline-images)
2247 ;; ;; (:link-home nil nil org-e-odt-link-home) FIXME
2248 ;; ;; (:link-up nil nil org-e-odt-link-up) FIXME
2249 ;; (:style nil nil org-e-odt-style)
2250 ;; (:style-extra nil nil org-e-odt-style-extra)
2251 ;; (:style-include-default nil nil org-e-odt-style-include-default)
2252 ;; (:style-include-scripts nil nil org-e-odt-style-include-scripts)
2253 ;; ;; (:timestamp nil nil org-e-odt-with-timestamp)
2254 ;; (:html-extension nil nil org-e-odt-extension)
2255 ;; (:html-postamble nil nil org-e-odt-postamble)
2256 ;; (:html-preamble nil nil org-e-odt-preamble)
2257 ;; (:html-table-tag nil nil org-e-odt-table-tag)
2258 ;; (:xml-declaration nil nil org-e-odt-xml-declaration)
2259 (:LaTeX-fragments nil "LaTeX" org-export-with-LaTeX-fragments))
2260 "Alist between export properties and ways to set them.
2262 The car of the alist is the property name, and the cdr is a list
2263 like \(KEYWORD OPTION DEFAULT BEHAVIOUR\) where:
2265 KEYWORD is a string representing a buffer keyword, or nil.
2266 OPTION is a string that could be found in an #+OPTIONS: line.
2267 DEFAULT is the default value for the property.
2268 BEHAVIOUR determine how Org should handle multiple keywords for
2269 the same property. It is a symbol among:
2270 nil Keep old value and discard the new one.
2271 t Replace old value with the new one.
2272 `space' Concatenate the values, separating them with a space.
2273 `newline' Concatenate the values, separating them with
2274 a newline.
2275 `split' Split values at white spaces, and cons them to the
2276 previous list.
2278 KEYWORD and OPTION have precedence over DEFAULT.
2280 All these properties should be back-end agnostic. For back-end
2281 specific properties, define a similar variable named
2282 `org-BACKEND-option-alist', replacing BACKEND with the name of
2283 the appropriate back-end. You can also redefine properties
2284 there, as they have precedence over these.")
2286 (defvar html-table-tag nil) ; dynamically scoped into this.
2288 ;; FIXME: it already exists in org-e-odt.el
2289 (defconst org-e-odt-cvt-link-fn
2291 "Function to convert link URLs to exportable URLs.
2292 Takes two arguments, TYPE and PATH.
2293 Returns exportable url as (TYPE PATH), or nil to signal that it
2294 didn't handle this case.
2295 Intended to be locally bound around a call to `org-export-as-html'." )
2300 (defvar org-e-odt-format-table-no-css)
2301 (defvar htmlize-buffer-places) ; from htmlize.el
2302 (defvar body-only) ; dynamically scoped into this.
2304 (defvar org-e-odt-table-rowgrp-open)
2305 (defvar org-e-odt-table-rownum)
2306 (defvar org-e-odt-table-cur-rowgrp-is-hdr)
2307 (defvar org-lparse-table-is-styled)
2310 (defvar org-e-odt-headline-formatter
2311 (lambda (level snumber todo todo-type priority
2312 title tags target extra-targets extra-class)
2313 (concat snumber " " title)))
2317 ;;; User Configuration Variables
2319 (defgroup org-export-e-odt nil
2320 "Options for exporting Org mode files to HTML."
2321 :tag "Org Export HTML"
2322 :group 'org-export)
2324 (defcustom org-e-odt-protect-char-alist
2325 '(("&" . "&amp;")
2326 ("<" . "&lt;")
2327 (">" . "&gt;"))
2328 "Alist of characters to be converted by `org-e-html-protect'."
2329 :group 'org-export-e-html
2330 :type '(repeat (cons (string :tag "Character")
2331 (string :tag "ODT equivalent"))))
2332 (defcustom org-export-e-odt-schema-dir
2333 (let* ((schema-dir
2334 (catch 'schema-dir
2335 (message "Debug (org-e-odt): Searching for OpenDocument schema files...")
2336 (mapc
2337 (lambda (schema-dir)
2338 (when schema-dir
2339 (message "Debug (org-e-odt): Trying %s..." schema-dir)
2340 (when (and (file-readable-p
2341 (expand-file-name "od-manifest-schema-v1.2-cs01.rnc"
2342 schema-dir))
2343 (file-readable-p
2344 (expand-file-name "od-schema-v1.2-cs01.rnc"
2345 schema-dir))
2346 (file-readable-p
2347 (expand-file-name "schemas.xml" schema-dir)))
2348 (message "Debug (org-e-odt): Using schema files under %s"
2349 schema-dir)
2350 (throw 'schema-dir schema-dir))))
2351 org-e-odt-schema-dir-list)
2352 (message "Debug (org-e-odt): No OpenDocument schema files installed")
2353 nil)))
2354 schema-dir)
2355 "Directory that contains OpenDocument schema files.
2357 This directory contains:
2358 1. rnc files for OpenDocument schema
2359 2. a \"schemas.xml\" file that specifies locating rules needed
2360 for auto validation of OpenDocument XML files.
2362 Use the customize interface to set this variable. This ensures
2363 that `rng-schema-locating-files' is updated and auto-validation
2364 of OpenDocument XML takes place based on the value
2365 `rng-nxml-auto-validate-flag'.
2367 The default value of this variable varies depending on the
2368 version of org in use and is initialized from
2369 `org-e-odt-schema-dir-list'. The OASIS schema files are available
2370 only in the org's private git repository. It is *not* bundled
2371 with GNU ELPA tar or standard Emacs distribution."
2372 :type '(choice
2373 (const :tag "Not set" nil)
2374 (directory :tag "Schema directory"))
2375 :group 'org-export-e-odt
2376 :version "24.1"
2377 :set
2378 (lambda (var value)
2379 "Set `org-export-e-odt-schema-dir'.
2380 Also add it to `rng-schema-locating-files'."
2381 (let ((schema-dir value))
2382 (set var
2383 (if (and
2384 (file-readable-p
2385 (expand-file-name "od-manifest-schema-v1.2-cs01.rnc" schema-dir))
2386 (file-readable-p
2387 (expand-file-name "od-schema-v1.2-cs01.rnc" schema-dir))
2388 (file-readable-p
2389 (expand-file-name "schemas.xml" schema-dir)))
2390 schema-dir
2391 (when value
2392 (message "Error (org-e-odt): %s has no OpenDocument schema files"
2393 value))
2394 nil)))
2395 (when org-export-e-odt-schema-dir
2396 (eval-after-load 'rng-loc
2397 '(add-to-list 'rng-schema-locating-files
2398 (expand-file-name "schemas.xml"
2399 org-export-e-odt-schema-dir))))))
2401 (defcustom org-export-e-odt-content-template-file nil
2402 "Template file for \"content.xml\".
2403 The exporter embeds the exported content just before
2404 \"</office:text>\" element.
2406 If unspecified, the file named \"OrgOdtContentTemplate.xml\"
2407 under `org-e-odt-styles-dir' is used."
2408 :type 'file
2409 :group 'org-export-e-odt
2410 :version "24.1")
2412 (defcustom org-export-e-odt-styles-file nil
2413 "Default styles file for use with ODT export.
2414 Valid values are one of:
2415 1. nil
2416 2. path to a styles.xml file
2417 3. path to a *.odt or a *.ott file
2418 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
2419 ...))
2421 In case of option 1, an in-built styles.xml is used. See
2422 `org-e-odt-styles-dir' for more information.
2424 In case of option 3, the specified file is unzipped and the
2425 styles.xml embedded therein is used.
2427 In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
2428 and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
2429 generated odt file. Use relative path for specifying the
2430 FILE-MEMBERS. styles.xml must be specified as one of the
2431 FILE-MEMBERS.
2433 Use options 1, 2 or 3 only if styles.xml alone suffices for
2434 achieving the desired formatting. Use option 4, if the styles.xml
2435 references additional files like header and footer images for
2436 achieving the desired formatting.
2438 Use \"#+ODT_STYLES_FILE: ...\" directive to set this variable on
2439 a per-file basis. For example,
2441 #+ODT_STYLES_FILE: \"/path/to/styles.xml\" or
2442 #+ODT_STYLES_FILE: (\"/path/to/file.ott\" (\"styles.xml\" \"image/hdr.png\"))."
2443 :group 'org-export-e-odt
2444 :version "24.1"
2445 :type
2446 '(choice
2447 (const :tag "Factory settings" nil)
2448 (file :must-match t :tag "styles.xml")
2449 (file :must-match t :tag "ODT or OTT file")
2450 (list :tag "ODT or OTT file + Members"
2451 (file :must-match t :tag "ODF Text or Text Template file")
2452 (cons :tag "Members"
2453 (file :tag " Member" "styles.xml")
2454 (repeat (file :tag "Member"))))))
2457 (defcustom org-export-e-odt-inline-image-extensions
2458 '("png" "jpeg" "jpg" "gif")
2459 "Extensions of image files that can be inlined into HTML."
2460 :type '(repeat (string :tag "Extension"))
2461 :group 'org-export-e-odt
2462 :version "24.1")
2464 (defcustom org-export-e-odt-pixels-per-inch display-pixels-per-inch
2465 "Scaling factor for converting images pixels to inches.
2466 Use this for sizing of embedded images. See Info node `(org)
2467 Images in ODT export' for more information."
2468 :type 'float
2469 :group 'org-export-e-odt
2470 :version "24.1")
2472 (defcustom org-export-e-odt-create-custom-styles-for-srcblocks t
2473 "Whether custom styles for colorized source blocks be automatically created.
2474 When this option is turned on, the exporter creates custom styles
2475 for source blocks based on the advice of `htmlfontify'. Creation
2476 of custom styles happen as part of `org-e-odt-hfy-face-to-css'.
2478 When this option is turned off exporter does not create such
2479 styles.
2481 Use the latter option if you do not want the custom styles to be
2482 based on your current display settings. It is necessary that the
2483 styles.xml already contains needed styles for colorizing to work.
2485 This variable is effective only if
2486 `org-export-e-odt-fontify-srcblocks' is turned on."
2487 :group 'org-export-e-odt
2488 :version "24.1"
2489 :type 'boolean)
2491 (defcustom org-export-e-odt-preferred-output-format nil
2492 "Automatically post-process to this format after exporting to \"odt\".
2493 Interactive commands `org-export-as-e-odt' and
2494 `org-export-as-e-odt-and-open' export first to \"odt\" format and
2495 then use `org-export-e-odt-convert-process' to convert the
2496 resulting document to this format. During customization of this
2497 variable, the list of valid values are populated based on
2498 `org-export-e-odt-convert-capabilities'."
2499 :group 'org-export-e-odt
2500 :version "24.1"
2501 :type '(choice :convert-widget
2502 (lambda (w)
2503 (apply 'widget-convert (widget-type w)
2504 (eval (car (widget-get w :args)))))
2505 `((const :tag "None" nil)
2506 ,@(mapcar (lambda (c)
2507 `(const :tag ,c ,c))
2508 (org-lparse-reachable-formats "odt")))))
2510 (defcustom org-export-e-odt-table-styles
2511 '(("OrgEquation" "OrgEquation"
2512 ((use-first-column-styles . t)
2513 (use-last-column-styles . t))))
2514 "Specify how Table Styles should be derived from a Table Template.
2515 This is a list where each element is of the
2516 form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
2518 TABLE-STYLE-NAME is the style associated with the table through
2519 `org-e-odt-table-style'.
2521 TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
2522 TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
2523 below) that is included in
2524 `org-export-e-odt-content-template-file'.
2526 TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
2527 \"TableCell\"
2528 PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
2529 \"TableParagraph\"
2530 TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
2531 \"FirstRow\" | \"LastRow\" |
2532 \"EvenRow\" | \"OddRow\" |
2533 \"EvenColumn\" | \"OddColumn\" | \"\"
2534 where \"+\" above denotes string concatenation.
2536 TABLE-CELL-OPTIONS is an alist where each element is of the
2537 form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
2538 TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
2539 `use-last-row-styles' |
2540 `use-first-column-styles' |
2541 `use-last-column-styles' |
2542 `use-banding-rows-styles' |
2543 `use-banding-columns-styles' |
2544 `use-first-row-styles'
2545 ON-OR-OFF := `t' | `nil'
2547 For example, with the following configuration
2549 \(setq org-export-e-odt-table-styles
2550 '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
2551 \(\(use-first-row-styles . t\)
2552 \(use-first-column-styles . t\)\)\)
2553 \(\"TableWithHeaderColumns\" \"Custom\"
2554 \(\(use-first-column-styles . t\)\)\)\)\)
2556 1. A table associated with \"TableWithHeaderRowsAndColumns\"
2557 style will use the following table-cell styles -
2558 \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
2559 \"CustomTableCell\" and the following paragraph styles
2560 \"CustomFirstRowTableParagraph\",
2561 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
2562 as appropriate.
2564 2. A table associated with \"TableWithHeaderColumns\" style will
2565 use the following table-cell styles -
2566 \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
2567 following paragraph styles
2568 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
2569 as appropriate..
2571 Note that TABLE-TEMPLATE-NAME corresponds to the
2572 \"<table:table-template>\" elements contained within
2573 \"<office:styles>\". The entries (TABLE-STYLE-NAME
2574 TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
2575 \"table:template-name\" and \"table:use-first-row-styles\" etc
2576 attributes of \"<table:table>\" element. Refer ODF-1.2
2577 specification for more information. Also consult the
2578 implementation filed under `org-e-odt-get-table-cell-styles'.
2580 The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
2581 formatting of numbered display equations. Do not delete this
2582 style from the list."
2583 :group 'org-export-e-odt
2584 :version "24.1"
2585 :type '(choice
2586 (const :tag "None" nil)
2587 (repeat :tag "Table Styles"
2588 (list :tag "Table Style Specification"
2589 (string :tag "Table Style Name")
2590 (string :tag "Table Template Name")
2591 (alist :options (use-first-row-styles
2592 use-last-row-styles
2593 use-first-column-styles
2594 use-last-column-styles
2595 use-banding-rows-styles
2596 use-banding-columns-styles)
2597 :key-type symbol
2598 :value-type (const :tag "True" t))))))
2599 (defcustom org-export-e-odt-fontify-srcblocks t
2600 "Specify whether or not source blocks need to be fontified.
2601 Turn this option on if you want to colorize the source code
2602 blocks in the exported file. For colorization to work, you need
2603 to make available an enhanced version of `htmlfontify' library."
2604 :type 'boolean
2605 :group 'org-export-e-odt
2606 :version "24.1")
2608 (defcustom org-export-e-odt-prettify-xml t ; FIXME
2609 "Specify whether or not the xml output should be prettified.
2610 When this option is turned on, `indent-region' is run on all
2611 component xml buffers before they are saved. Turn this off for
2612 regular use. Turn this on if you need to examine the xml
2613 visually."
2614 :group 'org-export-e-odt
2615 :version "24.1"
2616 :type 'boolean)
2618 (defcustom org-export-e-odt-convert-processes
2619 '(("LibreOffice"
2620 "soffice --headless --convert-to %f%x --outdir %d %i")
2621 ("unoconv"
2622 "unoconv -f %f -o %d %i"))
2623 "Specify a list of document converters and their usage.
2624 The converters in this list are offered as choices while
2625 customizing `org-export-e-odt-convert-process'.
2627 This variable is a list where each element is of the
2628 form (CONVERTER-NAME CONVERTER-CMD). CONVERTER-NAME is the name
2629 of the converter. CONVERTER-CMD is the shell command for the
2630 converter and can contain format specifiers. These format
2631 specifiers are interpreted as below:
2633 %i input file name in full
2634 %I input file name as a URL
2635 %f format of the output file
2636 %o output file name in full
2637 %O output file name as a URL
2638 %d output dir in full
2639 %D output dir as a URL.
2640 %x extra options as set in `org-export-e-odt-convert-capabilities'."
2641 :group 'org-export-e-odt
2642 :version "24.1"
2643 :type
2644 '(choice
2645 (const :tag "None" nil)
2646 (alist :tag "Converters"
2647 :key-type (string :tag "Converter Name")
2648 :value-type (group (string :tag "Command line")))))
2650 (defcustom org-export-e-odt-convert-process "LibreOffice"
2651 "Use this converter to convert from \"odt\" format to other formats.
2652 During customization, the list of converter names are populated
2653 from `org-export-e-odt-convert-processes'."
2654 :group 'org-export-e-odt
2655 :version "24.1"
2656 :type '(choice :convert-widget
2657 (lambda (w)
2658 (apply 'widget-convert (widget-type w)
2659 (eval (car (widget-get w :args)))))
2660 `((const :tag "None" nil)
2661 ,@(mapcar (lambda (c)
2662 `(const :tag ,(car c) ,(car c)))
2663 org-export-e-odt-convert-processes))))
2665 (defcustom org-export-e-odt-convert-capabilities
2666 '(("Text"
2667 ("odt" "ott" "doc" "rtf" "docx")
2668 (("pdf" "pdf") ("odt" "odt") ("rtf" "rtf") ("ott" "ott")
2669 ("doc" "doc" ":\"MS Word 97\"") ("docx" "docx") ("html" "html")))
2670 ("Web"
2671 ("html")
2672 (("pdf" "pdf") ("odt" "odt") ("html" "html")))
2673 ("Spreadsheet"
2674 ("ods" "ots" "xls" "csv" "xlsx")
2675 (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv") ("ods" "ods")
2676 ("xls" "xls") ("xlsx" "xlsx")))
2677 ("Presentation"
2678 ("odp" "otp" "ppt" "pptx")
2679 (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("otp" "otp") ("ppt" "ppt")
2680 ("pptx" "pptx") ("odg" "odg"))))
2681 "Specify input and output formats of `org-export-e-odt-convert-process'.
2682 More correctly, specify the set of input and output formats that
2683 the user is actually interested in.
2685 This variable is an alist where each element is of the
2686 form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
2687 INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
2688 alist where each element is of the form (OUTPUT-FMT
2689 OUTPUT-FILE-EXTENSION EXTRA-OPTIONS).
2691 The variable is interpreted as follows:
2692 `org-export-e-odt-convert-process' can take any document that is in
2693 INPUT-FMT-LIST and produce any document that is in the
2694 OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
2695 OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
2696 serves dual purposes:
2697 - It is used for populating completion candidates during
2698 `org-export-e-odt-convert' commands.
2699 - It is used as the value of \"%f\" specifier in
2700 `org-export-e-odt-convert-process'.
2702 EXTRA-OPTIONS is used as the value of \"%x\" specifier in
2703 `org-export-e-odt-convert-process'.
2705 DOCUMENT-CLASS is used to group a set of file formats in
2706 INPUT-FMT-LIST in to a single class.
2708 Note that this variable inherently captures how LibreOffice based
2709 converters work. LibreOffice maps documents of various formats
2710 to classes like Text, Web, Spreadsheet, Presentation etc and
2711 allow document of a given class (irrespective of it's source
2712 format) to be converted to any of the export formats associated
2713 with that class.
2715 See default setting of this variable for an typical
2716 configuration."
2717 :group 'org-export-e-odt
2718 :version "24.1"
2719 :type
2720 '(choice
2721 (const :tag "None" nil)
2722 (alist :tag "Capabilities"
2723 :key-type (string :tag "Document Class")
2724 :value-type
2725 (group (repeat :tag "Input formats" (string :tag "Input format"))
2726 (alist :tag "Output formats"
2727 :key-type (string :tag "Output format")
2728 :value-type
2729 (group (string :tag "Output file extension")
2730 (choice
2731 (const :tag "None" nil)
2732 (string :tag "Extra options"))))))))
2734 ;;;; Debugging
2737 ;;;; Document
2739 ;;;; Document Header (Styles)
2741 ;;;; Document Header (Scripts)
2743 ;;;; Document Header (Mathjax)
2745 ;;;; Preamble
2747 ;;;; Postamble
2749 ;;;; Emphasis
2751 ;;;; Todos
2753 ;;;; Tags
2755 ;;;; Time-stamps
2756 ;;;; Statistics Cookie
2757 ;;;; Subscript
2758 ;;;; Superscript
2760 ;;;; Inline images
2762 ;;;; Block
2763 ;;;; Comment
2764 ;;;; Comment Block
2765 ;;;; Drawer
2766 ;;;; Dynamic Block
2767 ;;;; Emphasis
2768 ;;;; Entity
2769 ;;;; Example Block
2770 ;;;; Export Snippet
2771 ;;;; Export Block
2772 ;;;; Fixed Width
2773 ;;;; Footnotes
2775 ;;;; Headline
2776 ;;;; Horizontal Rule
2777 ;;;; Inline Babel Call
2778 ;;;; Inline Src Block
2779 ;;;; Inlinetask
2780 ;;;; Item
2781 ;;;; Keyword
2782 ;;;; Latex Environment
2783 ;;;; Latex Fragment
2784 ;;;; Line Break
2785 ;;;; Link
2786 ;;;; Babel Call
2787 ;;;; Macro
2788 ;;;; Paragraph
2789 ;;;; Plain List
2790 ;;;; Plain Text
2791 ;;;; Property Drawer
2792 ;;;; Quote Block
2793 ;;;; Quote Section
2794 ;;;; Section
2795 ;;;; Radio Target
2796 ;;;; Special Block
2797 ;;;; Src Block
2799 ;;;; Table
2801 ;;;; Target
2802 ;;;; Time-stamp
2804 ;;;; Verbatim
2805 ;;;; Verse Block
2806 ;;;; Headline
2808 ;;;; Links
2809 ;;;; Drawers
2810 ;;;; Inlinetasks
2811 ;;;; Publishing
2813 ;;;; Compilation
2817 ;;; User Configurable Variables (MAYBE)
2819 ;;;; Preamble
2821 ;;;; Headline
2823 ;;;; Emphasis
2825 (defcustom org-e-odt-format-headline-function nil
2826 "Function to format headline text.
2828 This function will be called with 5 arguments:
2829 TODO the todo keyword \(string or nil\).
2830 TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
2831 PRIORITY the priority of the headline \(integer or nil\)
2832 TEXT the main headline text \(string\).
2833 TAGS the tags string, separated with colons \(string or nil\).
2835 The function result will be used in the section format string.
2837 As an example, one could set the variable to the following, in
2838 order to reproduce the default set-up:
2840 \(defun org-e-odt-format-headline \(todo todo-type priority text tags\)
2841 \"Default format function for an headline.\"
2842 \(concat \(when todo
2843 \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo\)\)
2844 \(when priority
2845 \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
2846 text
2847 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
2848 :group 'org-export-e-odt
2849 :type 'function)
2851 ;;;; Footnotes
2853 ;;;; Time-stamps
2855 (defcustom org-e-odt-active-timestamp-format "\\textit{%s}"
2856 "A printf format string to be applied to active time-stamps."
2857 :group 'org-export-e-odt
2858 :type 'string)
2860 (defcustom org-e-odt-inactive-timestamp-format "\\textit{%s}"
2861 "A printf format string to be applied to inactive time-stamps."
2862 :group 'org-export-e-odt
2863 :type 'string)
2865 (defcustom org-e-odt-diary-timestamp-format "\\textit{%s}"
2866 "A printf format string to be applied to diary time-stamps."
2867 :group 'org-export-e-odt
2868 :type 'string)
2871 ;;;; Links
2873 (defcustom org-e-odt-image-default-option "width=.9\\linewidth"
2874 "Default option for images."
2875 :group 'org-export-e-odt
2876 :type 'string)
2878 (defcustom org-e-odt-default-figure-position "htb"
2879 "Default position for latex figures."
2880 :group 'org-export-e-odt
2881 :type 'string)
2883 (defcustom org-e-odt-inline-image-rules
2884 '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\)\\'"))
2885 "Rules characterizing image files that can be inlined into HTML.
2887 A rule consists in an association whose key is the type of link
2888 to consider, and value is a regexp that will be matched against
2889 link's path.
2891 Note that, by default, the image extension *actually* allowed
2892 depend on the way the HTML file is processed. When used with
2893 pdflatex, pdf, jpg and png images are OK. When processing
2894 through dvi to Postscript, only ps and eps are allowed. The
2895 default we use here encompasses both."
2896 :group 'org-export-e-odt
2897 :type '(alist :key-type (string :tag "Type")
2898 :value-type (regexp :tag "Path")))
2900 ;;;; Tables
2902 (defcustom org-e-odt-table-caption-above t
2903 "When non-nil, place caption string at the beginning of the table.
2904 Otherwise, place it near the end."
2905 :group 'org-export-e-odt
2906 :type 'boolean)
2908 ;;;; Drawers
2910 (defcustom org-e-odt-format-drawer-function nil
2911 "Function called to format a drawer in HTML code.
2913 The function must accept two parameters:
2914 NAME the drawer name, like \"LOGBOOK\"
2915 CONTENTS the contents of the drawer.
2917 The function should return the string to be exported.
2919 For example, the variable could be set to the following function
2920 in order to mimic default behaviour:
2922 \(defun org-e-odt-format-drawer-default \(name contents\)
2923 \"Format a drawer element for HTML export.\"
2924 contents\)"
2925 :group 'org-export-e-odt
2926 :type 'function)
2929 ;;;; Inlinetasks
2931 (defcustom org-e-odt-format-inlinetask-function nil
2932 "Function called to format an inlinetask in HTML code.
2934 The function must accept six parameters:
2935 TODO the todo keyword, as a string
2936 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
2937 PRIORITY the inlinetask priority, as a string
2938 NAME the inlinetask name, as a string.
2939 TAGS the inlinetask tags, as a string.
2940 CONTENTS the contents of the inlinetask, as a string.
2942 The function should return the string to be exported.
2944 For example, the variable could be set to the following function
2945 in order to mimic default behaviour:
2947 \(defun org-e-odt-format-inlinetask \(todo type priority name tags contents\)
2948 \"Format an inline task element for HTML export.\"
2949 \(let \(\(full-title
2950 \(concat
2951 \(when todo
2952 \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
2953 \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
2954 title
2955 \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
2956 \(format \(concat \"\\\\begin{center}\\n\"
2957 \"\\\\fbox{\\n\"
2958 \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
2959 \"%s\\n\\n\"
2960 \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
2961 \"%s\"
2962 \"\\\\end{minipage}}\"
2963 \"\\\\end{center}\"\)
2964 full-title contents\)\)"
2965 :group 'org-export-e-odt
2966 :type 'function)
2969 ;; Src blocks
2971 ;;;; Plain text
2973 (defcustom org-e-odt-quotes
2974 '(("fr" ("\\(\\s-\\|[[(]\\)\"" . "«~") ("\\(\\S-\\)\"" . "~»") ("\\(\\s-\\|(\\)'" . "'"))
2975 ("en" ("\\(\\s-\\|[[(]\\)\"" . "``") ("\\(\\S-\\)\"" . "''") ("\\(\\s-\\|(\\)'" . "`")))
2976 "Alist for quotes to use when converting english double-quotes.
2978 The CAR of each item in this alist is the language code.
2979 The CDR of each item in this alist is a list of three CONS:
2980 - the first CONS defines the opening quote;
2981 - the second CONS defines the closing quote;
2982 - the last CONS defines single quotes.
2984 For each item in a CONS, the first string is a regexp
2985 for allowed characters before/after the quote, the second
2986 string defines the replacement string for this quote."
2987 :group 'org-export-e-odt
2988 :type '(list
2989 (cons :tag "Opening quote"
2990 (string :tag "Regexp for char before")
2991 (string :tag "Replacement quote "))
2992 (cons :tag "Closing quote"
2993 (string :tag "Regexp for char after ")
2994 (string :tag "Replacement quote "))
2995 (cons :tag "Single quote"
2996 (string :tag "Regexp for char before")
2997 (string :tag "Replacement quote "))))
3000 ;;;; Compilation
3004 ;;; Internal Functions (HTML)
3006 ;; (defun org-e-odt-format-inline-image (path &optional caption label attr)
3007 ;; ;; FIXME: alt text missing here?
3008 ;; (let ((inline-image (format "<img src=\"%s\" alt=\"%s\"/>"
3009 ;; path (file-name-nondirectory path))))
3010 ;; (if (not label) inline-image
3011 ;; (org-e-odt-format-section inline-image "figure" label))))
3013 (defun org-e-odt-format-image (src)
3014 "Create image tag with source and attributes."
3015 (save-match-data
3016 (let* ((caption (org-find-text-property-in-string 'org-caption src))
3017 (attr (org-find-text-property-in-string 'org-attributes src))
3018 (label (org-find-text-property-in-string 'org-label src))
3019 (caption (and caption (org-xml-encode-org-text caption)))
3020 (img-extras (if (string-match "^ltxpng/" src)
3021 (format " alt=\"%s\""
3022 (org-find-text-property-in-string
3023 'org-latex-src src))
3024 (if (string-match "\\<alt=" (or attr ""))
3025 (concat " " attr )
3026 (concat " " attr " alt=\"" src "\""))))
3027 (img (format "<img src=\"%s\"%s />" src img-extras))
3028 (extra (concat
3029 (and label
3030 (format "id=\"%s\" " (org-solidify-link-text label)))
3031 "class=\"figure\"")))
3032 (if caption
3033 (with-temp-buffer
3034 (with-org-lparse-preserve-paragraph-state
3035 (insert
3036 (org-lparse-format
3037 '("<div %s>" . "\n</div>")
3038 (concat
3039 (org-lparse-format '("\n<p>" . "</p>") img)
3040 (org-lparse-format '("\n<p>" . "</p>") caption))
3041 extra)))
3042 (buffer-string))
3043 img))))
3045 ;;;; Bibliography
3047 (defun org-e-odt-bibliography ()
3048 "Find bibliography, cut it out and return it."
3049 (catch 'exit
3050 (let (beg end (cnt 1) bib)
3051 (save-excursion
3052 (goto-char (point-min))
3053 (when (re-search-forward
3054 "^[ \t]*<div \\(id\\|class\\)=\"bibliography\"" nil t)
3055 (setq beg (match-beginning 0))
3056 (while (re-search-forward "</?div\\>" nil t)
3057 (setq cnt (+ cnt (if (string= (match-string 0) "<div") +1 -1)))
3058 (when (= cnt 0)
3059 (and (looking-at ">") (forward-char 1))
3060 (setq bib (buffer-substring beg (point)))
3061 (delete-region beg (point))
3062 (throw 'exit bib))))
3063 nil))))
3065 ;;;; Table
3067 (defun org-e-odt-format-table (lines olines)
3068 (let ((org-e-odt-format-table-no-css nil))
3069 (org-lparse-format-table lines olines)))
3071 (defun org-e-odt-splice-attributes (tag attributes)
3072 "Read attributes in string ATTRIBUTES, add and replace in HTML tag TAG."
3073 (if (not attributes)
3075 (let (oldatt newatt)
3076 (setq oldatt (org-extract-attributes-from-string tag)
3077 tag (pop oldatt)
3078 newatt (cdr (org-extract-attributes-from-string attributes)))
3079 (while newatt
3080 (setq oldatt (plist-put oldatt (pop newatt) (pop newatt))))
3081 (if (string-match ">" tag)
3082 (setq tag
3083 (replace-match (concat (org-attributes-to-string oldatt) ">")
3084 t t tag)))
3085 tag)))
3087 (defun org-export-splice-style (style extra)
3088 "Splice EXTRA into STYLE, just before \"</style>\"."
3089 (if (and (stringp extra)
3090 (string-match "\\S-" extra)
3091 (string-match "</style>" style))
3092 (concat (substring style 0 (match-beginning 0))
3093 "\n" extra "\n"
3094 (substring style (match-beginning 0)))
3095 style))
3097 ;; (defun org-e-odt-format-toc-entry (snumber todo headline tags href)
3098 ;; (setq headline (concat
3099 ;; ;; section number
3100 ;; (and org-export-with-section-numbers (concat snumber " "))
3101 ;; ;; headline
3102 ;; headline
3103 ;; ;; tags
3104 ;; (and tags (concat
3105 ;; (org-e-odt-format-spaces 3)
3106 ;; (org-e-odt-format-fontify tags "tag")))))
3107 ;; ;; fontify headline based on TODO keyword
3108 ;; (when todo (setq headline (org-e-odt-format-fontify headline "todo")))
3109 ;; (org-e-odt-format-link headline (concat "#" href)))
3111 (defun org-e-odt-toc-entry-formatter
3112 (level snumber todo todo-type priority
3113 headline tags target extra-targets extra-class)
3114 (org-e-odt-format-toc-entry snumber todo headline tags target))
3116 (defun org-e-odt-make-string (n string)
3117 (let (out) (dotimes (i n out) (setq out (concat string out)))))
3119 (defun org-e-odt-toc-text (toc-entries)
3120 (let* ((prev-level (1- (nth 1 (car toc-entries))))
3121 (start-level prev-level))
3122 (mapconcat
3123 (lambda (entry)
3124 (let ((headline (nth 0 entry))
3125 (level (nth 1 entry)))
3126 (prog1 (org-e-odt-format-toc-item headline level prev-level)
3127 (setq prev-level level))))
3128 toc-entries "")))
3130 (defun org-e-odt-toc (depth info)
3131 (assert (wholenump depth))
3132 (let* ((headlines (org-export-collect-headlines info depth))
3133 (toc-entries
3134 (loop for headline in headlines collect
3135 (list (org-e-odt-headline-text
3136 headline info 'org-e-odt-toc-entry-formatter)
3137 (org-export-get-relative-level headline info)))))
3138 (when toc-entries
3139 (let* ((lang-specific-heading "Table of Contents")) ; FIXME
3140 (concat
3141 (org-e-odt-begin-toc lang-specific-heading depth)
3142 (org-e-odt-toc-text toc-entries)
3143 (org-e-odt-end-toc))))))
3145 (defun org-e-odt-begin-outline (level1 snumber title tags
3146 target extra-targets extra-class)
3147 (let* ((class (format "outline-%d" level1))
3148 (class (if extra-class (concat class " " extra-class) class))
3149 (id (format "outline-container-%s"
3150 (org-lparse-suffix-from-snumber snumber)))
3151 (extra (concat (when id (format " id=\"%s\"" id))
3152 (when class (format " class=\"%s\"" class)))))
3153 (org-lparse-insert-tag "<div%s>" extra)
3154 (insert
3155 (org-lparse-format 'HEADING
3156 (org-lparse-format
3157 'HEADLINE title extra-targets tags snumber level1)
3158 level1 target))))
3160 (defun org-e-odt-end-outline ()
3161 (org-lparse-insert-tag "</div>"))
3163 (defun org-e-odt-suffix-from-snumber (snumber)
3164 (let* ((snu (replace-regexp-in-string "\\." "-" snumber))
3165 (href (cdr (assoc (concat "sec-" snu)
3166 org-export-preferred-target-alist))))
3167 (org-solidify-link-text (or href snu))))
3169 (defun org-e-odt-format-outline (contents level1 snumber title
3170 tags target extra-targets extra-class)
3171 (concat
3172 (org-e-odt-format-heading
3173 (org-e-odt-format-headline title extra-targets tags snumber level1)
3174 level1 target)
3175 contents))
3177 ;; (defun org-e-odt-format-line (line)
3178 ;; (case org-lparse-dyn-current-environment
3179 ;; ((quote fixedwidth) (concat (org-e-odt-encode-plain-text line) "\n"))
3180 ;; (t (concat line "\n"))))
3182 (defun org-e-odt-fix-class-name (kwd) ; audit callers of this function
3183 "Turn todo keyword into a valid class name.
3184 Replaces invalid characters with \"_\"."
3185 (save-match-data
3186 (while (string-match "[^a-zA-Z0-9_]" kwd)
3187 (setq kwd (replace-match "_" t t kwd))))
3188 kwd)
3190 (defun org-e-odt-format-internal-link (text href &optional extra)
3191 (org-e-odt-format-link text (concat "#" href) extra))
3193 (defun org-e-odt-format-extra-targets (extra-targets)
3194 (if (not extra-targets) ""
3195 (mapconcat (lambda (x)
3196 (when x
3197 (setq x (org-solidify-link-text
3198 (if (org-uuidgen-p x) (concat "ID-" x) x)))
3199 (org-e-odt-format-anchor "" x))) extra-targets "")))
3201 (defun org-e-odt-format-org-tags (tags)
3202 (if (not tags) ""
3203 (org-e-odt-format-fontify
3204 (mapconcat
3205 (lambda (x)
3206 (org-e-odt-format-fontify
3207 x (concat "" ;; org-e-odt-tag-class-prefix
3208 (org-e-odt-fix-class-name x))))
3209 (org-split-string tags ":")
3210 (org-e-odt-format-spaces 1)) "tag")))
3212 (defun org-e-odt-format-section-number (&optional snumber level)
3213 ;; FIXME
3214 (and nil org-export-with-section-numbers
3215 ;; (not org-lparse-body-only)
3216 snumber level
3217 (org-e-odt-format-fontify snumber (format "section-number-%d" level))))
3219 ;; (defun org-e-odt-format-headline (title extra-targets tags
3220 ;; &optional snumber level)
3221 ;; (concat
3222 ;; (org-e-odt-format-extra-targets extra-targets)
3223 ;; (concat (org-e-odt-format-section-number snumber level) " ")
3224 ;; title
3225 ;; (and tags (concat (org-e-odt-format-spaces 3)
3226 ;; (org-e-odt-format-org-tags tags)))))
3228 (defun org-e-odt-get-coding-system-for-write ()
3229 (or org-e-odt-coding-system
3230 (and (boundp 'buffer-file-coding-system) buffer-file-coding-system)))
3232 (defun org-e-odt-get-coding-system-for-save ()
3233 (or org-e-odt-coding-system
3234 (and (boundp 'buffer-file-coding-system) buffer-file-coding-system)))
3236 ;; (defun org-e-odt-format-date (info)
3237 ;; (let ((date (plist-get info :date)))
3238 ;; (cond
3239 ;; ((and date (string-match "%" date))
3240 ;; (format-time-string date))
3241 ;; (date date)
3242 ;; (t (format-time-string "%Y-%m-%d %T %Z")))))
3246 ;;; Internal Functions (Ngz)
3248 (defun org-e-odt--caption/label-string (caption label info)
3249 "Return caption and label HTML string for floats.
3251 CAPTION is a cons cell of secondary strings, the car being the
3252 standard caption and the cdr its short form. LABEL is a string
3253 representing the label. INFO is a plist holding contextual
3254 information.
3256 If there's no caption nor label, return the empty string.
3258 For non-floats, see `org-e-odt--wrap-label'."
3259 (setq label nil) ;; FIXME
3261 (let ((label-str (if label (format "\\label{%s}" label) "")))
3262 (cond
3263 ((and (not caption) (not label)) "")
3264 ((not caption) (format "\\label{%s}\n" label))
3265 ;; Option caption format with short name.
3266 ((cdr caption)
3267 (format "\\caption[%s]{%s%s}\n"
3268 (org-export-secondary-string (cdr caption) 'e-odt info)
3269 label-str
3270 (org-export-secondary-string (car caption) 'e-odt info)))
3271 ;; Standard caption format.
3272 ;; (t (format "\\caption{%s%s}\n"
3273 ;; label-str
3274 ;; (org-export-secondary-string (car caption) 'e-odt info)))
3276 (t (org-export-secondary-string (car caption) 'e-odt info)))))
3278 (defun org-e-odt--find-verb-separator (s)
3279 "Return a character not used in string S.
3280 This is used to choose a separator for constructs like \\verb."
3281 (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
3282 (loop for c across ll
3283 when (not (string-match (regexp-quote (char-to-string c)) s))
3284 return (char-to-string c))))
3286 (defun org-e-odt--make-option-string (options)
3287 "Return a comma separated string of keywords and values.
3288 OPTIONS is an alist where the key is the options keyword as
3289 a string, and the value a list containing the keyword value, or
3290 nil."
3291 (mapconcat (lambda (pair)
3292 (concat (first pair)
3293 (when (> (length (second pair)) 0)
3294 (concat "=" (second pair)))))
3295 options
3296 ","))
3298 (defun org-e-odt--quotation-marks (text info)
3299 "Export quotation marks depending on language conventions.
3300 TEXT is a string containing quotation marks to be replaced. INFO
3301 is a plist used as a communication channel."
3302 (mapc (lambda(l)
3303 (let ((start 0))
3304 (while (setq start (string-match (car l) text start))
3305 (let ((new-quote (concat (match-string 1 text) (cdr l))))
3306 (setq text (replace-match new-quote t t text))))))
3307 (cdr (or (assoc (plist-get info :language) org-e-odt-quotes)
3308 ;; Falls back on English.
3309 (assoc "en" org-e-odt-quotes))))
3310 text)
3312 (defun org-e-odt--wrap-label (element output)
3313 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
3314 This function shouldn't be used for floats. See
3315 `org-e-odt--caption/label-string'."
3316 ;; (let ((label (org-element-property :name element)))
3317 ;; (if (or (not output) (not label) (string= output "") (string= label ""))
3318 ;; output
3319 ;; (concat (format "\\label{%s}\n" label) output)))
3320 output)
3324 ;;; Template
3326 (defun org-e-odt-template (contents info)
3327 "Return complete document string after HTML conversion.
3328 CONTENTS is the transcoded contents string. RAW-DATA is the
3329 original parsed data. INFO is a plist holding export options."
3332 ;; write meta file
3333 (org-e-odt-update-meta-file info)
3336 (with-temp-buffer
3337 (insert-file-contents
3338 (or org-export-e-odt-content-template-file
3339 (expand-file-name "OrgOdtContentTemplate.xml"
3340 org-e-odt-styles-dir)))
3341 (goto-char (point-min))
3342 (re-search-forward "</office:text>" nil nil)
3343 (goto-char (match-beginning 0))
3345 ;; Title
3346 (insert (org-e-odt-format-preamble info))
3347 ;; Table of Contents
3348 (let ((depth (plist-get info :with-toc)))
3349 (when (wholenump depth) (org-e-odt-toc depth info)))
3351 ;; Contents
3352 (insert contents)
3353 (buffer-substring-no-properties (point-min) (point-max))))
3357 ;;; Transcode Functions
3359 ;;;; Block
3361 (defun org-e-odt-center-block (center-block contents info)
3362 "Transcode a CENTER-BLOCK element from Org to HTML.
3363 CONTENTS holds the contents of the block. INFO is a plist
3364 holding contextual information."
3365 (org-e-odt--wrap-label center-block contents))
3368 ;;;; Comment
3370 ;; Comments are ignored.
3373 ;;;; Comment Block
3375 ;; Comment Blocks are ignored.
3378 ;;;; Drawer
3380 (defun org-e-odt-drawer (drawer contents info)
3381 "Transcode a DRAWER element from Org to HTML.
3382 CONTENTS holds the contents of the block. INFO is a plist
3383 holding contextual information."
3384 (let* ((name (org-element-property :drawer-name drawer))
3385 (output (if (functionp org-e-odt-format-drawer-function)
3386 (funcall org-e-odt-format-drawer-function
3387 name contents)
3388 ;; If there's no user defined function: simply
3389 ;; display contents of the drawer.
3390 contents)))
3391 (org-e-odt--wrap-label drawer output)))
3394 ;;;; Dynamic Block
3396 (defun org-e-odt-dynamic-block (dynamic-block contents info)
3397 "Transcode a DYNAMIC-BLOCK element from Org to HTML.
3398 CONTENTS holds the contents of the block. INFO is a plist
3399 holding contextual information. See
3400 `org-export-data'."
3401 (org-e-odt--wrap-label dynamic-block contents))
3404 ;;;; Emphasis
3406 (defun org-e-odt-emphasis (emphasis contents info)
3407 "Transcode EMPHASIS from Org to HTML.
3408 CONTENTS is the contents of the emphasized text. INFO is a plist
3409 holding contextual information.."
3410 ;; (format (cdr (assoc (org-element-property :marker emphasis)
3411 ;; org-e-odt-emphasis-alist))
3412 ;; contents)
3413 (org-e-odt-format-fontify
3414 contents (cadr (assoc
3415 (org-element-property :marker emphasis)
3416 '(("*" bold)
3417 ("/" emphasis)
3418 ("_" underline)
3419 ("=" code)
3420 ("~" verbatim)
3421 ("+" strike))))))
3424 ;;;; Entity
3426 (defun org-e-odt-entity (entity contents info)
3427 "Transcode an ENTITY object from Org to HTML.
3428 CONTENTS are the definition itself. INFO is a plist holding
3429 contextual information."
3430 ;; (let ((ent (org-element-property :latex entity)))
3431 ;; (if (org-element-property :latex-math-p entity)
3432 ;; (format "$%s$" ent)
3433 ;; ent))
3434 (org-element-property :utf-8 entity))
3437 ;;;; Example Block
3439 (defun org-e-odt-example-block (example-block contents info)
3440 "Transcode a EXAMPLE-BLOCK element from Org to HTML.
3441 CONTENTS is nil. INFO is a plist holding contextual information."
3442 (let* ((options (or (org-element-property :options example-block) ""))
3443 (value (org-export-handle-code example-block info)))
3444 (org-e-odt--wrap-label
3445 example-block (org-e-odt-format-source-code-or-example value nil))))
3448 ;;;; Export Snippet
3450 (defun org-e-odt-export-snippet (export-snippet contents info)
3451 "Transcode a EXPORT-SNIPPET object from Org to HTML.
3452 CONTENTS is nil. INFO is a plist holding contextual information."
3453 (when (eq (org-export-snippet-backend export-snippet) 'e-odt)
3454 (org-element-property :value export-snippet)))
3457 ;;;; Export Block
3459 (defun org-e-odt-export-block (export-block contents info)
3460 "Transcode a EXPORT-BLOCK element from Org to HTML.
3461 CONTENTS is nil. INFO is a plist holding contextual information."
3462 (when (string= (org-element-property :type export-block) "latex")
3463 (org-remove-indentation (org-element-property :value export-block))))
3466 ;;;; Fixed Width
3468 (defun org-e-odt-fixed-width (fixed-width contents info)
3469 "Transcode a FIXED-WIDTH element from Org to HTML.
3470 CONTENTS is nil. INFO is a plist holding contextual information."
3471 (let* ((value (org-element-normalize-string
3472 (replace-regexp-in-string
3473 "^[ \t]*: ?" ""
3474 (org-element-property :value fixed-width)))))
3475 (org-e-odt--wrap-label
3476 fixed-width (org-e-odt-format-source-code-or-example value nil))))
3479 ;;;; Footnote Definition
3481 ;; Footnote Definitions are ignored.
3484 ;;;; Footnote Reference
3486 (defun org-e-odt-footnote-def (raw info) ; FIXME
3487 (if (equal (org-element-type raw) 'org-data)
3488 (org-trim (org-export-data raw 'e-odt info))
3489 (org-odt-format-stylized-paragraph
3490 'footnote (org-trim (org-export-secondary-string raw 'e-odt info)))))
3492 (defvar org-e-odt-footnote-separator
3493 (org-e-odt-format-fontify "," 'superscript))
3495 (defun org-e-odt-footnote-reference (footnote-reference contents info)
3496 "Transcode a FOOTNOTE-REFERENCE element from Org to HTML.
3497 CONTENTS is nil. INFO is a plist holding contextual information."
3498 (concat
3499 ;; Insert separator between two footnotes in a row.
3500 (let ((prev (org-export-get-previous-element footnote-reference info)))
3501 (when (and (listp prev) (eq (car prev) 'footnote-reference))
3502 org-e-odt-footnote-separator))
3503 (cond
3504 ((not (org-export-footnote-first-reference-p footnote-reference info))
3505 (let* ((n (org-export-get-footnote-number footnote-reference info)))
3506 (org-e-odt-format-footnote-reference n "IGNORED" 100)))
3507 ;; Inline definitions are secondary strings.
3508 ((eq (org-element-property :type footnote-reference) 'inline)
3509 (let* ((raw (org-export-get-footnote-definition footnote-reference info))
3510 (n (org-export-get-footnote-number footnote-reference info))
3511 (def (org-e-odt-footnote-def raw info)))
3512 (org-e-odt-format-footnote-reference n def 1)))
3513 ;; Non-inline footnotes definitions are full Org data.
3515 (let* ((raw (org-export-get-footnote-definition footnote-reference info))
3516 (n (org-export-get-footnote-number footnote-reference info))
3517 (def (org-e-odt-footnote-def raw info)))
3518 (org-e-odt-format-footnote-reference n def 1))))))
3521 ;;;; Headline
3523 (defun org-e-odt-todo (todo)
3524 (when todo
3525 (org-e-odt-format-fontify
3526 (concat
3527 "" ; org-e-odt-todo-kwd-class-prefix
3528 (org-e-odt-fix-class-name todo))
3529 (list (if (member todo org-done-keywords) "done" "todo")
3530 todo))))
3532 (defun org-e-odt-headline-text (headline info &optional formatter)
3533 "Transcode an HEADLINE element from Org to HTML.
3534 CONTENTS holds the contents of the headline. INFO is a plist
3535 holding contextual information."
3536 (let* ((numberedp (plist-get info :section-numbers))
3537 (level (org-export-get-relative-level headline info))
3538 (todo (and (plist-get info :with-todo-keywords)
3539 (let ((todo (org-element-property
3540 :todo-keyword headline)))
3541 (and todo
3542 (org-export-secondary-string todo 'e-odt info)))))
3543 (todo-type (and todo (org-element-property :todo-type headline)))
3544 (priority (and (plist-get info :with-priority)
3545 (org-element-property :priority headline)))
3546 (text (org-export-secondary-string
3547 (org-element-property :title headline) 'e-odt info))
3548 (tags (and (plist-get info :with-tags)
3549 (org-element-property :tags headline)))
3551 (headline-no (org-export-get-headline-number headline info))
3552 (headline-label
3553 (format "sec-%s" (mapconcat 'number-to-string headline-no "-")))
3554 (headline-labels (list headline-label))
3555 (headline-no (org-export-get-headline-number headline info))
3556 (section-no (mapconcat 'number-to-string headline-no "."))
3557 (primary-target (car (last headline-labels)))
3558 (secondary-targets (butlast headline-labels))
3559 (extra-class nil)
3560 (formatter (or (and (functionp formatter) formatter)
3561 org-e-odt-headline-formatter)))
3562 (funcall formatter level section-no todo todo-type priority
3563 text tags primary-target secondary-targets extra-class)))
3565 (defun org-e-odt-headline (headline contents info)
3566 "Transcode an HEADLINE element from Org to HTML.
3567 CONTENTS holds the contents of the headline. INFO is a plist
3568 holding contextual information."
3569 (let* ((class (plist-get info :latex-class))
3570 (numberedp (plist-get info :section-numbers))
3571 ;; Get level relative to current parsed data.
3572 (level (org-export-get-relative-level headline info))
3573 ;; (class-sectionning (assoc class org-e-odt-classes))
3574 ;; Section formatting will set two placeholders: one for the
3575 ;; title and the other for the contents.
3576 ;; (section-fmt
3577 ;; (let ((sec (if (and (symbolp (nth 2 class-sectionning))
3578 ;; (fboundp (nth 2 class-sectionning)))
3579 ;; (funcall (nth 2 class-sectionning) level numberedp)
3580 ;; (nth (1+ level) class-sectionning))))
3581 ;; (cond
3582 ;; ;; No section available for that LEVEL.
3583 ;; ((not sec) nil)
3584 ;; ;; Section format directly returned by a function.
3585 ;; ((stringp sec) sec)
3586 ;; ;; (numbered-section . unnumbered-section)
3587 ;; ((not (consp (cdr sec)))
3588 ;; (concat (funcall (if numberedp #'car #'cdr) sec) "\n%s"))
3589 ;; ;; (numbered-open numbered-close)
3590 ;; ((= (length sec) 2)
3591 ;; (when numberedp (concat (car sec) "\n%s" (nth 1 sec))))
3592 ;; ;; (num-in num-out no-num-in no-num-out)
3593 ;; ((= (length sec) 4)
3594 ;; (if numberedp
3595 ;; (concat (car sec) "\n%s" (nth 1 sec))
3596 ;; (concat (nth 2 sec) "\n%s" (nth 3 sec)))))))
3597 (text (org-export-secondary-string
3598 (org-element-property :title headline) 'e-odt info))
3599 (todo (and (plist-get info :with-todo-keywords)
3600 (let ((todo (org-element-property
3601 :todo-keyword headline)))
3602 (and todo
3603 (org-export-secondary-string todo 'e-odt info)))))
3604 (todo-type (and todo (org-element-property :todo-type headline)))
3605 (tags (and (plist-get info :with-tags)
3606 (org-element-property :tags headline)))
3607 (priority (and (plist-get info :with-priority)
3608 (org-element-property :priority headline)))
3609 ;; Create the headline text.
3610 (full-text (if (functionp org-e-odt-format-headline-function)
3611 ;; User-defined formatting function.
3612 (funcall org-e-odt-format-headline-function
3613 todo todo-type priority text tags)
3614 ;; Default formatting.
3615 (concat
3616 ;; (when todo
3617 ;; (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
3618 (org-e-odt-todo todo) " "
3619 (when priority (format "\\framebox{\\#%c} " priority))
3620 text
3621 ;; (when tags (format "\\hfill{}\\textsc{%s}" tags))
3623 ;; Associate some \label to the headline for internal links.
3624 ;; (headline-label
3625 ;; (format "\\label{sec-%s}\n"
3626 ;; (mapconcat 'number-to-string
3627 ;; (org-export-get-headline-number headline info)
3628 ;; "-")))
3630 ;; FIXME - begin
3631 (headline-no (org-export-get-headline-number headline info))
3632 (headline-label
3633 (format "sec-%s" (mapconcat 'number-to-string headline-no "-")))
3634 (headline-labels (list headline-label))
3635 (headline-no (org-export-get-headline-number headline info))
3636 (section-no (mapconcat 'number-to-string headline-no "."))
3637 ;; FIXME - end
3639 (pre-blanks (make-string
3640 (org-element-property :pre-blank headline) 10)))
3641 (cond
3642 ;; Case 1: This is a footnote section: ignore it.
3643 ((org-element-property :footnote-section-p headline) nil)
3644 ;; Case 2. This is a deep sub-tree: export it as a list item.
3645 ;; Also export as items headlines for which no section
3646 ;; format has been found.
3647 ((org-export-low-level-p headline info) ; FIXME (or (not section-fmt))
3648 ;; Build the real contents of the sub-tree.
3649 (let* ((type (if numberedp 'unordered 'unordered)) ; FIXME
3650 (itemized-body (org-e-odt-format-list-item
3651 contents type nil nil full-text)))
3652 (concat
3653 (and (org-export-first-sibling-p headline info)
3654 (org-e-odt-begin-plain-list type))
3655 itemized-body
3656 (and (org-export-last-sibling-p headline info)
3657 (org-e-odt-end-plain-list type)))))
3658 ;; Case 3. Standard headline. Export it as a section.
3660 ;; (format section-fmt full-text
3661 ;; (concat headline-label pre-blanks contents))
3663 (org-e-odt-format-outline contents level section-no full-text tags
3664 (car (last headline-labels))
3665 (butlast headline-labels) nil)))))
3668 ;;;; Horizontal Rule
3670 (defun org-e-odt-horizontal-rule (horizontal-rule contents info)
3671 "Transcode an HORIZONTAL-RULE object from Org to HTML.
3672 CONTENTS is nil. INFO is a plist holding contextual information."
3673 (let ((attr (mapconcat #'identity
3674 (org-element-property :attr_odt horizontal-rule)
3675 " ")))
3676 (org-e-odt--wrap-label horizontal-rule
3677 (org-e-odt-format-horizontal-line))))
3680 ;;;; Inline Babel Call
3682 ;; Inline Babel Calls are ignored.
3685 ;;;; Inline Src Block
3687 (defun org-e-odt-inline-src-block (inline-src-block contents info)
3688 "Transcode an INLINE-SRC-BLOCK element from Org to HTML.
3689 CONTENTS holds the contents of the item. INFO is a plist holding
3690 contextual information."
3691 (let* ((org-lang (org-element-property :language inline-src-block))
3692 (code (org-element-property :value inline-src-block))
3693 (separator (org-e-odt--find-verb-separator code)))
3694 (error "FIXME")))
3697 ;;;; Inlinetask
3699 (defun org-e-odt-format-section (text class &optional id)
3700 (let ((extra (concat (when id (format " id=\"%s\"" id)))))
3701 (concat (format "<div class=\"%s\"%s>\n" class extra) text "</div>\n")))
3703 (defun org-e-odt-inlinetask (inlinetask contents info)
3704 "Transcode an INLINETASK element from Org to HTML.
3705 CONTENTS holds the contents of the block. INFO is a plist
3706 holding contextual information."
3707 (let ((title (org-export-secondary-string
3708 (org-element-property :title inlinetask) 'e-odt info))
3709 (todo (and (plist-get info :with-todo-keywords)
3710 (let ((todo (org-element-property
3711 :todo-keyword inlinetask)))
3712 (and todo
3713 (org-export-secondary-string todo 'e-odt info)))))
3714 (todo-type (org-element-property :todo-type inlinetask))
3715 (tags (and (plist-get info :with-tags)
3716 (org-element-property :tags inlinetask)))
3717 (priority (and (plist-get info :with-priority)
3718 (org-element-property :priority inlinetask))))
3719 ;; If `org-e-odt-format-inlinetask-function' is provided, call it
3720 ;; with appropriate arguments.
3721 (if (functionp org-e-odt-format-inlinetask-function)
3722 (funcall org-e-odt-format-inlinetask-function
3723 todo todo-type priority title tags contents)
3724 ;; Otherwise, use a default template.
3725 (org-e-odt--wrap-label
3726 inlinetask
3727 (let ((full-title
3728 (concat
3729 (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo))
3730 (when priority (format "\\framebox{\\#%c} " priority))
3731 title
3732 (when tags (format "\\hfill{}\\textsc{%s}" tags)))))
3733 (format (concat "\\begin{center}\n"
3734 "\\fbox{\n"
3735 "\\begin{minipage}[c]{.6\\textwidth}\n"
3736 "%s\n\n"
3737 "\\rule[.8em]{\\textwidth}{2pt}\n\n"
3738 "%s"
3739 "\\end{minipage}\n"
3740 "}\n"
3741 "\\end{center}")
3742 full-title contents))))))
3745 ;;;; Item
3747 (defun org-e-odt-format-list-item (contents type checkbox
3748 &optional term-counter-id
3749 headline)
3750 (when checkbox
3751 (setq checkbox
3752 (org-e-odt-format-fontify (case checkbox
3753 (on "[X]")
3754 (off "[&nbsp;]")
3755 (trans "[-]")) 'code)))
3756 (concat
3757 (org-e-odt-begin-list-item type term-counter-id headline)
3758 ;; FIXME checkbox (and checkbox " ")
3759 contents
3760 (org-e-odt-end-list-item type)))
3762 (defun org-e-odt-item (item contents info)
3763 "Transcode an ITEM element from Org to HTML.
3764 CONTENTS holds the contents of the item. INFO is a plist holding
3765 contextual information."
3766 ;; Grab `:level' from plain-list properties, which is always the
3767 ;; first element above current item.
3768 (let* ((plain-list (org-export-get-parent item info))
3769 (type (org-element-property :type plain-list))
3770 (level (org-element-property :level plain-list))
3771 (counter (org-element-property :counter item))
3772 (checkbox (org-element-property :checkbox item))
3773 (tag (let ((tag (org-element-property :tag item)))
3774 (and tag (org-export-secondary-string tag 'e-odt info)))))
3775 (org-e-odt-format-list-item
3776 contents type checkbox (or tag counter))))
3779 ;;;; Keyword
3781 (defun org-e-odt-keyword (keyword contents info)
3782 "Transcode a KEYWORD element from Org to HTML.
3783 CONTENTS is nil. INFO is a plist holding contextual information."
3784 (let ((key (downcase (org-element-property :key keyword)))
3785 (value (org-element-property :value keyword)))
3786 (cond
3787 ((string= key "latex") value)
3788 ((string= key "index") (format "\\index{%s}" value))
3789 ((string= key "target")
3790 (format "\\label{%s}" (org-export-solidify-link-text value)))
3791 ((string= key "toc")
3792 (let ((value (downcase value)))
3793 (cond
3794 ((string-match "\\<headlines\\>" value)
3795 (let ((depth (or (and (string-match "[0-9]+" value)
3796 (string-to-number (match-string 0 value)))
3797 (plist-get info :with-toc))))
3798 (when (wholenump depth) (org-e-odt-toc depth info))))
3799 ((string= "tables" value) "FIXME")
3800 ((string= "figures" value) "FIXME")
3801 ((string= "listings" value)
3802 (cond
3803 ;; At the moment, src blocks with a caption are wrapped
3804 ;; into a figure environment.
3805 (t "FIXME")))))))))
3808 ;;;; Latex Environment
3810 (defun org-e-odt-format-latex (latex-frag processing-type)
3811 (let* ((prefix (case processing-type
3812 (dvipng "ltxpng/")
3813 (mathml "ltxmathml/")))
3814 (cache-relpath
3815 (concat prefix (file-name-sans-extension
3816 (file-name-nondirectory (buffer-file-name)))))
3817 (cache-dir (file-name-directory (buffer-file-name )))
3818 (display-msg (case processing-type
3819 (dvipng "Creating LaTeX Image...")
3820 (mathml "Creating MathML snippet..."))))
3821 (with-temp-buffer
3822 (insert latex-frag)
3823 (org-format-latex cache-relpath cache-dir nil display-msg
3824 nil nil processing-type)
3825 (buffer-string))))
3827 (defun org-e-odt-latex-environment (latex-environment contents info)
3828 "Transcode a LATEX-ENVIRONMENT element from Org to HTML.
3829 CONTENTS is nil. INFO is a plist holding contextual information."
3830 (org-e-odt--wrap-label
3831 latex-environment
3832 (let ((latex-frag
3833 (org-remove-indentation
3834 (org-element-property :value latex-environment)))
3835 (processing-type (plist-get info :LaTeX-fragments)))
3836 (cond
3837 ((member processing-type '(t mathjax))
3838 (org-e-odt-format-latex latex-frag 'mathml))
3839 ((equal processing-type 'dvipng)
3840 (let* ((formula-link (org-e-odt-format-latex
3841 latex-frag processing-type)))
3842 (when (and formula-link
3843 (string-match "file:\\([^]]*\\)" formula-link))
3844 (org-e-odt-format-inline-image (match-string 1 formula-link)))))
3846 latex-frag)))))
3849 ;;;; Latex Fragment
3851 (defun org-e-odt-latex-fragment (latex-fragment contents info)
3852 "Transcode a LATEX-FRAGMENT object from Org to HTML.
3853 CONTENTS is nil. INFO is a plist holding contextual information."
3854 ;; (org-element-property :value latex-fragment)
3855 (let* ((latex-frag (org-element-property :value latex-fragment)))
3856 (cond
3857 ((string-match "\\\\ref{\\([^{}\n]+\\)}" latex-frag)
3858 (let* ((label (match-string 1 latex-frag))
3859 (href (and label (org-export-solidify-link-text label)))
3860 (text (if (string-match "\\`[a-z]\\{1,10\\}:\\(.+\\)" label)
3861 (substring label (match-beginning 1))
3862 label)))
3863 (org-e-odt-format-internal-link text href)))
3864 (t (let ((processing-type (plist-get info :LaTeX-fragments)))
3865 (cond
3866 ((member processing-type '(t mathjax))
3867 (org-e-odt-format-latex latex-frag 'mathjax))
3868 ((equal processing-type 'dvipng)
3869 (let* ((formula-link (org-e-odt-format-latex
3870 latex-frag processing-type)))
3871 (when (and formula-link
3872 (string-match "file:\\([^]]*\\)" formula-link))
3873 (org-e-odt-format-inline-image
3874 (match-string 1 formula-link)))))
3875 (t latex-frag)))))))
3878 ;;;; Line Break
3880 (defun org-e-odt-line-break (line-break contents info)
3881 "Transcode a LINE-BREAK object from Org to HTML.
3882 CONTENTS is nil. INFO is a plist holding contextual information."
3883 "<text:line-break/>\n")
3886 ;;;; Link
3888 (defun org-e-odt-link--inline-image (link info)
3889 "Return HTML code for an inline image.
3890 LINK is the link pointing to the inline image. INFO is a plist
3891 used as a communication channel."
3892 (let* ((parent (org-export-get-parent-paragraph link info))
3893 (path (let ((raw-path (org-element-property :path link)))
3894 (if (not (file-name-absolute-p raw-path)) raw-path
3895 (expand-file-name raw-path))))
3896 (caption (org-e-odt--caption/label-string
3897 (org-element-property :caption parent)
3898 (org-element-property :name parent)
3899 info))
3900 (label (org-element-property :name parent))
3901 ;; Retrieve latex attributes from the element around.
3902 (attr (let ((raw-attr
3903 (mapconcat #'identity
3904 (org-element-property :attr_odt parent)
3905 " ")))
3906 (unless (string= raw-attr "") raw-attr))))
3907 ;; Now clear ATTR from any special keyword and set a default
3908 ;; value if nothing is left.
3909 (setq attr (if (not attr) "" (org-trim attr)))
3910 ;; Return proper string, depending on DISPOSITION.
3911 (let ((href (and label (org-export-solidify-link-text label))))
3912 (org-e-odt-format-inline-image path caption href attr))))
3914 (defun org-e-odt-link (link desc info)
3915 "Transcode a LINK object from Org to HTML.
3917 DESC is the description part of the link, or the empty string.
3918 INFO is a plist holding contextual information. See
3919 `org-export-data'."
3920 (let* ((type (org-element-property :type link))
3921 (raw-path (org-element-property :path link))
3922 ;; Ensure DESC really exists, or set it to nil.
3923 (desc (and (not (string= desc "")) desc))
3924 (imagep (org-export-inline-image-p
3925 link org-e-odt-inline-image-rules))
3926 (path (cond
3927 ((member type '("http" "https" "ftp" "mailto"))
3928 (concat type ":" raw-path))
3929 ((string= type "file")
3930 (when (string-match "\\(.+\\)::.+" raw-path)
3931 (setq raw-path (match-string 1 raw-path)))
3932 (if (file-name-absolute-p raw-path)
3933 (concat "file://" (expand-file-name raw-path))
3934 ;; TODO: Not implemented yet. Concat also:
3935 ;; (org-export-directory :HTML info)
3936 (concat "file://" raw-path)))
3937 (t raw-path)))
3938 protocol)
3939 (cond
3940 ;; Image file.
3941 (imagep (org-e-odt-link--inline-image link info))
3942 ;; Target or radioed target: replace link with the normalized
3943 ;; custom-id/target name.
3944 ((member type '("target" "radio"))
3945 (org-e-odt-format-internal-link
3946 (or desc (org-export-secondary-string path 'e-odt info))
3947 (org-export-solidify-link-text path)))
3948 ;; Links pointing to an headline: Find destination and build
3949 ;; appropriate referencing commanding.
3950 ((member type '("custom-id" "fuzzy" "id"))
3951 (let ((destination (if (string= type "fuzzy")
3952 (org-export-resolve-fuzzy-link link info)
3953 (org-export-resolve-id-link link info))))
3954 ;; Fuzzy link points to a target. Do as above.
3955 (case (org-element-type destination)
3956 (target
3957 (org-e-odt-format-internal-link
3958 (or desc
3959 (org-export-secondary-string
3960 (org-element-property :raw-link link)
3961 'e-odt info))
3962 (org-export-solidify-link-text
3963 (org-element-property :raw-value destination))))
3964 ;; Fuzzy link points to an headline. If headlines are
3965 ;; numbered and the link has no description, display
3966 ;; headline's number. Otherwise, display description or
3967 ;; headline's title.
3968 (headline
3969 (let ((label
3970 (format "sec-%s"
3971 (mapconcat
3972 'number-to-string
3973 (org-export-get-headline-number destination info)
3974 "-"))))
3975 (if (and (plist-get info :section-numbers) (not desc))
3976 (format "\\ref{%s}" label)
3977 (org-e-odt-format-internal-link
3978 (or desc
3979 (org-export-secondary-string
3980 (org-element-property :title destination)
3981 'e-odt info)) label))))
3982 ;; Fuzzy link points nowhere.
3983 (otherwise
3984 (org-e-odt-format-fontify
3985 (or desc
3986 (org-export-secondary-string
3987 (org-element-property :raw-link link)
3988 'e-odt info)) 'emphasis)))))
3989 ;; Coderef: replace link with the reference name or the
3990 ;; equivalent line number.
3991 ((string= type "coderef")
3992 (format (org-export-get-coderef-format path (or desc ""))
3993 (org-export-resolve-coderef path info)))
3994 ;; Link type is handled by a special function.
3995 ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
3996 (funcall protocol (org-link-unescape path) desc 'html))
3997 ;; External link with a description part.
3998 ((and path desc) (org-e-odt-format-link desc path))
3999 ;; External link without a description part.
4000 (path (org-e-odt-format-link path path))
4001 ;; No path, only description. Try to do something useful.
4002 (t (org-e-odt-format-fontify desc 'emphasis)))))
4005 ;;;; Babel Call
4007 ;; Babel Calls are ignored.
4010 ;;;; Macro
4012 (defun org-e-odt-macro (macro contents info)
4013 "Transcode a MACRO element from Org to HTML.
4014 CONTENTS is nil. INFO is a plist holding contextual information."
4015 ;; Use available tools.
4016 (org-export-expand-macro macro info))
4019 ;;;; Paragraph
4021 (defun org-e-odt-paragraph (paragraph contents info)
4022 "Transcode a PARAGRAPH element from Org to HTML.
4023 CONTENTS is the contents of the paragraph, as a string. INFO is
4024 the plist used as a communication channel."
4025 (let* ((style nil) ; FIXME
4026 (class (cdr (assoc style '((footnote . "footnote")
4027 (verse . nil)))))
4028 (extra (if class (format " class=\"%s\"" class) ""))
4029 (parent (car (org-export-get-genealogy paragraph info)))
4030 (parent-type (org-element-type parent))
4031 (style (case parent-type
4032 (quote-block 'quote)
4033 (center-block 'center)
4034 (t nil))))
4035 (org-e-odt-format-stylized-paragraph style contents)))
4038 ;;;; Plain List
4040 (defun org-e-odt-plain-list (plain-list contents info)
4041 "Transcode a PLAIN-LIST element from Org to HTML.
4042 CONTENTS is the contents of the list. INFO is a plist holding
4043 contextual information."
4044 (let* (arg1 ;; FIXME
4045 (type (org-element-property :type plain-list))
4046 (attr (mapconcat #'identity
4047 (org-element-property :attr_odt plain-list)
4048 " ")))
4049 (org-e-odt--wrap-label
4050 plain-list (format "%s\n%s%s"
4051 (org-e-odt-begin-plain-list type)
4052 contents (org-e-odt-end-plain-list type)))))
4054 ;;;; Plain Text
4056 (defun org-e-odt-convert-special-strings (string)
4057 "Convert special characters in STRING to ODT."
4058 (let ((all org-export-e-odt-special-string-regexps)
4059 e a re rpl start)
4060 (while (setq a (pop all))
4061 (setq re (car a) rpl (cdr a) start 0)
4062 (while (string-match re string start)
4063 (setq string (replace-match rpl t nil string))))
4064 string))
4066 ;; (defun org-e-odt-encode-plain-text (s)
4067 ;; "Convert plain text characters to HTML equivalent.
4068 ;; Possible conversions are set in `org-export-html-protect-char-alist'."
4069 ;; (let ((cl org-e-odt-protect-char-alist) c)
4070 ;; (while (setq c (pop cl))
4071 ;; (let ((start 0))
4072 ;; (while (string-match (car c) s start)
4073 ;; (setq s (replace-match (cdr c) t t s)
4074 ;; start (1+ (match-beginning 0))))))
4075 ;; s))
4077 (defun org-e-odt-plain-text (text info)
4078 "Transcode a TEXT string from Org to HTML.
4079 TEXT is the string to transcode. INFO is a plist holding
4080 contextual information."
4081 (setq text (org-e-odt-encode-plain-text text t))
4082 ;; Protect %, #, &, $, ~, ^, _, { and }.
4083 ;; (while (string-match "\\([^\\]\\|^\\)\\([%$#&{}~^_]\\)" text)
4084 ;; (setq text
4085 ;; (replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
4086 ;; Protect \
4087 ;; (setq text (replace-regexp-in-string
4088 ;; "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
4089 ;; "$\\backslash$" text nil t 1))
4090 ;; HTML into \HTML{} and TeX into \TeX{}.
4091 ;; (let ((case-fold-search nil)
4092 ;; (start 0))
4093 ;; (while (string-match "\\<\\(\\(?:La\\)?TeX\\)\\>" text start)
4094 ;; (setq text (replace-match
4095 ;; (format "\\%s{}" (match-string 1 text)) nil t text)
4096 ;; start (match-end 0))))
4097 ;; Handle quotation marks
4098 ;; (setq text (org-e-odt--quotation-marks text info))
4099 ;; Convert special strings.
4100 ;; (when (plist-get info :with-special-strings)
4101 ;; (while (string-match (regexp-quote "...") text)
4102 ;; (setq text (replace-match "\\ldots{}" nil t text))))
4103 (when (plist-get info :with-special-strings)
4104 (setq text (org-e-odt-convert-special-strings text)))
4105 ;; Handle break preservation if required.
4106 (when (plist-get info :preserve-breaks)
4107 (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n"
4108 text)))
4109 ;; Return value.
4110 text)
4113 ;;;; Property Drawer
4115 (defun org-e-odt-property-drawer (property-drawer contents info)
4116 "Transcode a PROPERTY-DRAWER element from Org to HTML.
4117 CONTENTS is nil. INFO is a plist holding contextual
4118 information."
4119 ;; The property drawer isn't exported but we want separating blank
4120 ;; lines nonetheless.
4124 ;;;; Quote Block
4126 (defun org-e-odt-quote-block (quote-block contents info)
4127 "Transcode a QUOTE-BLOCK element from Org to HTML.
4128 CONTENTS holds the contents of the block. INFO is a plist
4129 holding contextual information."
4130 (org-e-odt--wrap-label quote-block contents))
4133 ;;;; Quote Section
4135 (defun org-e-odt-quote-section (quote-section contents info)
4136 "Transcode a QUOTE-SECTION element from Org to HTML.
4137 CONTENTS is nil. INFO is a plist holding contextual information."
4138 (let ((value (org-remove-indentation
4139 (org-element-property :value quote-section))))
4140 (when value (org-e-odt-format-source-code-or-example value nil))))
4143 ;;;; Section
4145 (defun org-e-odt-section (section contents info) ; FIXME
4146 "Transcode a SECTION element from Org to HTML.
4147 CONTENTS holds the contents of the section. INFO is a plist
4148 holding contextual information."
4149 contents)
4151 ;;;; Radio Target
4153 (defun org-e-odt-radio-target (radio-target text info)
4154 "Transcode a RADIO-TARGET object from Org to HTML.
4155 TEXT is the text of the target. INFO is a plist holding
4156 contextual information."
4157 (org-e-odt-format-anchor
4158 text (org-export-solidify-link-text
4159 (org-element-property :raw-value radio-target))))
4162 ;;;; Special Block
4164 (defun org-e-odt-special-block (special-block contents info)
4165 "Transcode a SPECIAL-BLOCK element from Org to HTML.
4166 CONTENTS holds the contents of the block. INFO is a plist
4167 holding contextual information."
4168 (let ((type (downcase (org-element-property :type special-block))))
4169 (org-e-odt--wrap-label
4170 special-block
4171 (format "\\begin{%s}\n%s\\end{%s}" type contents type))))
4174 ;;;; Src Block
4176 (defun org-e-odt-src-block (src-block contents info)
4177 "Transcode a SRC-BLOCK element from Org to HTML.
4178 CONTENTS holds the contents of the item. INFO is a plist holding
4179 contextual information."
4180 (let* ((lang (org-element-property :language src-block))
4181 (code (org-export-handle-code src-block info))
4182 (caption (org-element-property :caption src-block))
4183 (label (org-element-property :name src-block)))
4184 ;; FIXME: Handle caption
4186 ;; caption-str (when caption)
4187 ;; (main (org-export-secondary-string (car caption) 'e-odt info))
4188 ;; (secondary (org-export-secondary-string (cdr caption) 'e-odt info))
4189 ;; (caption-str (org-e-odt--caption/label-string caption label info))
4190 (org-e-odt-format-source-code-or-example code lang)))
4193 ;;;; Statistics Cookie
4195 (defun org-e-odt-statistics-cookie (statistics-cookie contents info)
4196 "Transcode a STATISTICS-COOKIE object from Org to HTML.
4197 CONTENTS is nil. INFO is a plist holding contextual information."
4198 (let ((cookie-value (org-element-property :value statistics-cookie)))
4199 (org-e-odt-format-fontify cookie-value 'code)))
4202 ;;;; Subscript
4204 (defun org-e-odt-subscript (subscript contents info)
4205 "Transcode a SUBSCRIPT object from Org to HTML.
4206 CONTENTS is the contents of the object. INFO is a plist holding
4207 contextual information."
4208 ;; (format (if (= (length contents) 1) "$_%s$" "$_{\\mathrm{%s}}$") contents)
4209 (org-e-odt-format-fontify contents 'subscript))
4212 ;;;; Superscript
4214 (defun org-e-odt-superscript (superscript contents info)
4215 "Transcode a SUPERSCRIPT object from Org to HTML.
4216 CONTENTS is the contents of the object. INFO is a plist holding
4217 contextual information."
4218 ;; (format (if (= (length contents) 1) "$^%s$" "$^{\\mathrm{%s}}$") contents)
4219 (org-e-odt-format-fontify contents 'superscript))
4222 ;;;; Table
4224 (defun org-e-odt-get-colwidth (c)
4225 (let ((col-widths (plist-get table-info :width)))
4226 (or (and org-lparse-table-is-styled (aref col-widths c)) 0)))
4228 (defun org-e-odt-table-row (fields &optional text-for-empty-fields)
4229 (incf org-e-odt-table-rownum)
4230 (let ((i -1))
4231 (org-e-odt-format-table-row
4232 (mapconcat
4233 (lambda (x)
4234 (when (and (string= x "") text-for-empty-fields)
4235 (setq x text-for-empty-fields))
4236 (incf i)
4237 (let ((horiz-span (org-e-odt-get-colwidth i)))
4238 (org-e-odt-format-table-cell
4239 x org-e-odt-table-rownum i horiz-span)))
4240 fields "\n"))))
4242 (defun org-e-odt-table-preamble ()
4243 (let ((colgroup-vector (plist-get table-info :column-groups)) ;; FIXME
4244 c gr colgropen preamble)
4245 (unless (aref colgroup-vector 0)
4246 (setf (aref colgroup-vector 0) 'start))
4247 (dotimes (c columns-number preamble)
4248 (setq gr (aref colgroup-vector c))
4249 (setq preamble
4250 (concat
4251 preamble
4252 (when (memq gr '(start start-end))
4253 (prog1 (if colgropen "</colgroup>\n<colgroup>" "\n<colgroup>")
4254 (setq colgropen t)))
4255 (let* ((colalign-vector (plist-get table-info :alignment)) ;; FIXME
4256 (align (cdr (assoc (aref colalign-vector c)
4257 '(("l" . "left")
4258 ("r" . "right")
4259 ("c" . "center")))))
4260 (alignspec (if (and (boundp 'org-e-odt-format-table-no-css)
4261 org-e-odt-format-table-no-css)
4262 " align=\"%s\"" " class=\"%s\""))
4263 (extra (format alignspec align)))
4264 (format "<col%s />" extra))
4265 (when (memq gr '(end start-end))
4266 (setq colgropen nil)
4267 "</colgroup>"))))
4268 (concat preamble (if colgropen "</colgroup>"))))
4270 (defun org-e-odt-list-table (lines caption label attributes)
4271 (setq lines (org-e-odt-org-table-to-list-table lines))
4272 (let* ((splice nil) head
4273 (org-e-odt-table-rownum -1)
4274 i (cnt 0)
4275 fields line
4276 org-e-odt-table-cur-rowgrp-is-hdr
4277 org-e-odt-table-rowgrp-open
4279 (org-lparse-table-style 'org-table)
4280 org-lparse-table-is-styled)
4281 (cond
4282 (splice
4283 (setq org-lparse-table-is-styled nil)
4284 (mapconcat 'org-e-odt-table-row lines "\n"))
4286 (setq org-lparse-table-is-styled t)
4288 (concat
4289 (org-e-odt-begin-table caption label attributes)
4290 ;; FIXME (org-e-odt-table-preamble)
4291 (org-e-odt-begin-table-rowgroup head)
4293 (mapconcat
4294 (lambda (line)
4295 (cond
4296 ((equal line 'hline) (org-e-odt-begin-table-rowgroup))
4297 (t (org-e-odt-table-row line))))
4298 lines "\n")
4300 (org-e-odt-end-table-rowgroup)
4301 (org-e-odt-end-table))))))
4303 (defun org-e-odt-transcode-table-row (row)
4304 (if (string-match org-table-hline-regexp row) 'hline
4305 (mapcar
4306 (lambda (cell)
4307 (org-export-secondary-string
4308 (let ((cell (org-element-parse-secondary-string
4309 cell
4310 (cdr (assq 'table org-element-string-restrictions)))))
4311 cell)
4312 'e-odt info))
4313 (org-split-string row "[ \t]*|[ \t]*"))))
4315 (defun org-e-odt-org-table-to-list-table (lines &optional splice)
4316 "Convert org-table to list-table.
4317 LINES is a list of the form (ROW1 ROW2 ROW3 ...) where each
4318 element is a `string' representing a single row of org-table.
4319 Thus each ROW has vertical separators \"|\" separating the table
4320 fields. A ROW could also be a row-group separator of the form
4321 \"|---...|\". Return a list of the form (ROW1 ROW2 ROW3
4322 ...). ROW could either be symbol `'hline' or a list of the
4323 form (FIELD1 FIELD2 FIELD3 ...) as appropriate."
4324 (let (line lines-1)
4325 (cond
4326 (splice
4327 (while (setq line (pop lines))
4328 (unless (string-match "^[ \t]*|-" line)
4329 (push (org-e-odt-transcode-table-row line) lines-1))))
4330 (t (while (setq line (pop lines))
4331 (cond
4332 ((string-match "^[ \t]*|-" line)
4333 (when lines (push 'hline lines-1)))
4334 (t (push (org-e-odt-transcode-table-row line) lines-1))))))
4335 (nreverse lines-1)))
4337 (defun org-e-odt-table-table (raw-table)
4338 (require 'table)
4339 (with-current-buffer (get-buffer-create "*org-export-table*")
4340 (erase-buffer))
4341 (let ((output (with-temp-buffer
4342 (insert raw-table)
4343 (goto-char 1)
4344 (re-search-forward "^[ \t]*|[^|]" nil t)
4345 (table-generate-source 'html "*org-export-table*")
4346 (with-current-buffer "*org-export-table*"
4347 (org-trim (buffer-string))))))
4348 (kill-buffer (get-buffer "*org-export-table*"))
4349 output))
4351 (defun org-e-odt-table (table contents info)
4352 "Transcode a TABLE element from Org to HTML.
4353 CONTENTS is nil. INFO is a plist holding contextual information."
4354 (let* ((label (org-element-property :name table))
4355 (caption (org-e-odt--caption/label-string
4356 (org-element-property :caption table) label info))
4357 (attr (mapconcat #'identity
4358 (org-element-property :attr_odt table)
4359 " "))
4360 (raw-table (org-element-property :raw-table table))
4361 (table-type (org-element-property :type table)))
4362 (case table-type
4363 (table.el
4364 ;; (org-e-odt-table-table raw-table)
4367 (let* ((table-info (org-export-table-format-info raw-table))
4368 (columns-number (length (plist-get table-info :alignment)))
4369 (lines (org-split-string
4370 (org-export-clean-table
4371 raw-table (plist-get table-info :special-column-p)) "\n"))
4373 (genealogy (org-export-get-genealogy table info))
4374 (parent (car genealogy))
4375 (parent-type (org-element-type parent)))
4376 (org-e-odt-list-table lines caption label attr))))))
4379 ;;;; Target
4381 (defun org-e-odt-target (target text info)
4382 "Transcode a TARGET object from Org to HTML.
4383 TEXT is the text of the target. INFO is a plist holding
4384 contextual information."
4385 (org-e-odt-format-anchor
4386 text (org-export-solidify-link-text
4387 (org-element-property :raw-value target))))
4390 ;;;; Time-stamp
4392 (defun org-e-odt-time-stamp (time-stamp contents info)
4393 "Transcode a TIME-STAMP object from Org to HTML.
4394 CONTENTS is nil. INFO is a plist holding contextual
4395 information."
4396 ;; (let ((value (org-element-property :value time-stamp))
4397 ;; (type (org-element-property :type time-stamp))
4398 ;; (appt-type (org-element-property :appt-type time-stamp)))
4399 ;; (concat (cond ((eq appt-type 'scheduled)
4400 ;; (format "\\textbf{\\textsc{%s}} " org-scheduled-string))
4401 ;; ((eq appt-type 'deadline)
4402 ;; (format "\\textbf{\\textsc{%s}} " org-deadline-string))
4403 ;; ((eq appt-type 'closed)
4404 ;; (format "\\textbf{\\textsc{%s}} " org-closed-string)))
4405 ;; (cond ((memq type '(active active-range))
4406 ;; (format org-e-odt-active-timestamp-format value))
4407 ;; ((memq type '(inactive inactive-range))
4408 ;; (format org-e-odt-inactive-timestamp-format value))
4409 ;; (t
4410 ;; (format org-e-odt-diary-timestamp-format value)))))
4411 (let ((value (org-element-property :value time-stamp))
4412 (type (org-element-property :type time-stamp))
4413 (appt-type (org-element-property :appt-type time-stamp)))
4414 (setq value (org-export-secondary-string value 'e-odt info))
4415 (org-e-odt-format-fontify
4416 (concat
4417 (org-e-odt-format-fontify
4418 (cond ((eq appt-type 'scheduled) org-scheduled-string)
4419 ((eq appt-type 'deadline) org-deadline-string)
4420 ((eq appt-type 'closed) org-closed-string)) "timestamp-kwd")
4421 ;; FIXME: (org-translate-time value)
4422 (org-e-odt-format-fontify value "timestamp"))
4423 "timestamp-wrapper")))
4426 ;;;; Verbatim
4428 (defun org-e-odt-verbatim (verbatim contents info)
4429 "Transcode a VERBATIM object from Org to HTML.
4430 CONTENTS is nil. INFO is a plist used as a communication
4431 channel."
4432 (org-e-odt-emphasis
4433 verbatim (org-element-property :value verbatim) info))
4436 ;;;; Verse Block
4438 (defun org-e-odt-verse-block (verse-block contents info)
4439 "Transcode a VERSE-BLOCK element from Org to HTML.
4440 CONTENTS is nil. INFO is a plist holding contextual information."
4441 ;; Replace each newline character with line break. Also replace
4442 ;; each blank line with a line break.
4443 (setq contents (replace-regexp-in-string
4444 "^ *\\\\\\\\$" "<br/>\n"
4445 (replace-regexp-in-string
4446 "\\(\\\\\\\\\\)?[ \t]*\n" " <br/>\n"
4447 (org-remove-indentation
4448 (org-export-secondary-string
4449 (org-element-property :value verse-block)
4450 'e-odt info)))))
4452 ;; Replace each white space at beginning of a line with a
4453 ;; non-breaking space.
4454 (while (string-match "^[ \t]+" contents)
4455 (let ((new-str (org-e-odt-format-spaces
4456 (length (match-string 0 contents)))))
4457 (setq contents (replace-match new-str nil t contents))))
4459 (org-e-odt--wrap-label
4460 verse-block (format "<p class=\"verse\">\n%s</p>" contents)))
4465 ;;; Filter Functions
4467 ;;;; Filter Settings
4468 ;;;; Filters
4470 ;;; Interactive functions
4472 (defun org-e-odt-export-to-odt
4473 (&optional subtreep visible-only body-only ext-plist pub-dir)
4474 "Export current buffer to a HTML file.
4476 If narrowing is active in the current buffer, only export its
4477 narrowed part.
4479 If a region is active, export that region.
4481 When optional argument SUBTREEP is non-nil, export the sub-tree
4482 at point, extracting information from the headline properties
4483 first.
4485 When optional argument VISIBLE-ONLY is non-nil, don't export
4486 contents of hidden elements.
4488 When optional argument BODY-ONLY is non-nil, only write code
4489 between \"\\begin{document}\" and \"\\end{document}\".
4491 EXT-PLIST, when provided, is a property list with external
4492 parameters overriding Org default settings, but still inferior to
4493 file-local settings.
4495 When optional argument PUB-DIR is set, use it as the publishing
4496 directory.
4498 Return output file's name."
4499 (interactive)
4501 ;; FIXME
4502 (with-current-buffer (get-buffer-create "*debug*")
4503 (erase-buffer))
4505 ;; (let* ((outfile (org-export-output-file-name ".html" subtreep pub-dir))
4506 ;; (outfile "content.xml"))
4507 ;; (org-export-to-file
4508 ;; 'e-odt outfile subtreep visible-only body-only ext-plist))
4510 (let* ((outbuf (org-e-odt-init-outfile))
4511 (target (org-export-output-file-name ".odt" subtreep pub-dir))
4512 (outdir (file-name-directory (buffer-file-name outbuf)))
4513 (default-directory outdir))
4515 ;; FIXME: for copying embedded images
4516 (setq org-current-export-file
4517 (file-name-directory
4518 (org-export-output-file-name ".odt" subtreep nil)))
4520 (org-export-to-buffer
4521 'e-odt outbuf
4522 (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))
4524 (setq org-lparse-opt-plist nil) ; FIXME
4525 (org-e-odt-save-as-outfile target ;; info
4529 ;; return outfile
4530 target))
4535 ;;; FIXMES, TODOS, FOR REVIEW etc
4537 ;;;; org-format-table-html
4538 ;;;; org-format-org-table-html
4539 ;;;; org-format-table-table-html
4540 ;;;; org-table-number-fraction
4541 ;;;; org-table-number-regexp
4542 ;;;; org-e-odt-table-caption-above
4544 ;;;; org-whitespace
4545 ;;;; "<span style=\"visibility:hidden;\">%s</span>"
4546 ;;;; Remove display properties
4547 ;;;; org-e-odt-final-hook
4549 ;;;; org-e-odt-with-timestamp
4550 ;;;; org-e-odt-html-helper-timestamp
4552 ;;;; org-export-as-html-and-open
4553 ;;;; org-export-as-html-batch
4554 ;;;; org-export-as-html-to-buffer
4555 ;;;; org-replace-region-by-html
4556 ;;;; org-export-region-as-html
4557 ;;;; org-export-as-html
4559 ;;;; (org-export-directory :html opt-plist)
4560 ;;;; (plist-get opt-plist :html-extension)
4561 ;;;; org-e-odt-toplevel-hlevel
4562 ;;;; org-e-odt-special-string-regexps
4563 ;;;; org-e-odt-coding-system
4564 ;;;; org-e-odt-coding-system
4565 ;;;; org-e-odt-inline-images
4566 ;;;; org-e-odt-inline-image-extensions
4567 ;;;; org-e-odt-protect-char-alist
4568 ;;;; org-e-odt-table-use-header-tags-for-first-column
4569 ;;;; org-e-odt-todo-kwd-class-prefix
4570 ;;;; org-e-odt-tag-class-prefix
4571 ;;;; org-e-odt-footnote-separator
4574 ;;; Library Initializations
4576 (mapc
4577 (lambda (desc)
4578 ;; Let Org open all OpenDocument files using system-registered app
4579 (add-to-list 'org-file-apps
4580 (cons (concat "\\." (car desc) "\\'") 'system))
4581 ;; Let Emacs open all OpenDocument files in archive mode
4582 (add-to-list 'auto-mode-alist
4583 (cons (concat "\\." (car desc) "\\'") 'archive-mode)))
4584 org-e-odt-file-extensions)
4586 ;; FIXME
4587 ;; (eval-after-load 'org-exp
4588 ;; '(add-to-list 'org-export-inbuffer-options-extra
4589 ;; '("ODT_STYLES_FILE" :odt-styles-file)))
4591 (provide 'org-e-odt)
4593 ;;; org-e-odt.el ends here