;;; org-odt.el --- Opendocument export for Org-mode ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 ;; Free Software Foundation, Inc. ;; Author: Jambunathan K (kjambunathan at gmail dot com) ;; Keywords: outlines, hypermedia, calendar, wp ;; Homepage: http://orgmode.org ;; Version: 7.01trans ;; ;; This file is part of GNU Emacs. ;; ;; GNU Emacs is free software: you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version. ;; GNU Emacs is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with GNU Emacs. If not, see . ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;;; Commentary: ;;; Code: (require 'org-exp) (eval-when-compile (require 'cl)) (declare-function org-id-find-id-file "org-id" (id)) (declare-function odtize-region "ext:odtize" (beg end)) (defgroup org-export-odt nil "Options specific for ODT export of Org-mode files." :tag "Org Export ODT" :group 'org-export) (defcustom org-export-odt-coding-system nil "Coding system for ODT export, defaults to `buffer-file-coding-system'." :group 'org-export-odt :type 'coding-system) (defcustom org-export-odt-extension "odt" "The extension for exported ODT files." :group 'org-export-odt :type 'string) (defcustom org-export-odt-xml-declaration '(("odt" . "") ("php" . "\"; ?>")) "The extension for exported ODT files. %s will be replaced with the charset of the exported file. This may be a string, or an alist with export extensions and corresponding declarations." :group 'org-export-odt :type '(choice (string :tag "Single declaration") (repeat :tag "Dependent on extension" (cons (string :tag "Extension") (string :tag "Declaration"))))) (defcustom org-export-odt-tag-class-prefix "" "Prefix to class names for TODO keywords. Each tag gets a class given by the tag itself, with this prefix. The default prefix is empty because it is nice to just use the keyword as a class name. But if you get into conflicts with other, existing CSS classes, then this prefix can be very useful." :group 'org-export-odt :type 'string) (defcustom org-export-odt-todo-kwd-class-prefix "" "Prefix to class names for TODO keywords. Each TODO keyword gets a class given by the keyword itself, with this prefix. The default prefix is empty because it is nice to just use the keyword as a class name. But if you get into conflicts with other, existing CSS classes, then this prefix can be very useful." :group 'org-export-odt :type 'string) (defcustom org-export-odt-title-format "\n%s\n" "Format for typesetting the document title in ODT export." :group 'org-export-odt :type 'string) (defcustom org-export-odt-toplevel-hlevel 2 "The level for level 1 headings in ODT export. This is also important for the classes that will be wrapped around headlines and outline structure. If this variable is 1, the top-level headlines will be

, and the corresponding classes will be outline-1, section-number-1, and outline-text-1. If this is 2, all of these will get a 2 instead. The default for this variable is 2, because we use

for formatting the document title." :group 'org-export-odt :type 'string) (defcustom org-export-odt-link-org-files-as-odt t "Non-nil means make file links to `file.org' point to `file.odt'. When org-mode is exporting an org-mode file to ODT, links to non-odt files are directly put into a href tag in ODT. However, links to other Org-mode files (recognized by the extension `.org.) should become links to the corresponding odt file, assuming that the linked org-mode file will also be converted to ODT. When nil, the links still point to the plain `.org' file." :group 'org-export-odt :type 'boolean) (defcustom org-export-odt-inline-images 'maybe "Non-nil means inline images into exported ODT pages. This is done using an tag. When nil, an anchor with href is used to link to the image. If this option is `maybe', then images in links with an empty description will be inlined, while images with a description will be linked only." :group 'org-export-odt :type '(choice (const :tag "Never" nil) (const :tag "Always" t) (const :tag "When there is no description" maybe))) (defcustom org-export-odt-inline-image-extensions '("png" "jpeg" "jpg" "gif") "Extensions of image files that can be inlined into ODT." :group 'org-export-odt :type '(repeat (string :tag "Extension"))) (defcustom org-odt-table-tag "" "The ODT tag that is used to start a table. This must be a tag, but you may change the options like borders and spacing." :group 'org-export-odt :type 'string) (defcustom org-odt-table-header-tags '(" " . "") "The opening tag for table header fields. This is customizable so that alignment options can be specified. %s will be filled with the scope of the field, either row or col. See also the variable `org-export-odt-table-use-header-tags-for-first-column'." :group 'org-export-tables :type '(cons (string :tag "Opening tag") (string :tag "Closing tag"))) (defcustom org-odt-table-data-tags '(" " . "") "The opening tag for table data fields. This is customizable so that alignment options can be specified." :group 'org-export-tables :type '(cons (string :tag "Opening tag") (string :tag "Closing tag"))) (defcustom org-odt-table-row-tags '("" . "") "The opening tag for table data fields. This is customizable so that alignment options can be specified. Instead of strings, these can be Lisp forms that will be evaluated for each row in order to construct the table row tags. During evaluation, the variable `head' will be true when this is a header line, nil when this is a body line. And the variable `nline' will contain the line number, starting from 1 in the first header line. For example (setq org-odt-table-row-tags (cons '(if head \"\" (if (= (mod nline 2) 1) \"\" \"\")) \"\")) will give even lines the class \"tr-even\" and odd lines the class \"tr-odd\"." :group 'org-export-tables :type '(cons (choice :tag "Opening tag" (string :tag "Specify") (sexp)) (choice :tag "Closing tag" (string :tag "Specify") (sexp)))) (defconst org-odt-table-header-rows-tags '("" . "")) (defconst org-odt-table-rows-tags '("" . "")) (defconst org-odt-table-column-tags '("" . ". Set by publishing functions. This may also be a function, building and inserting the preamble.") (defvar org-export-odt-postamble nil "Postamble, to be inserted just before . Set by publishing functions. This may also be a function, building and inserting the postamble.") (defvar org-export-odt-auto-preamble t "Should default preamble be inserted? Set by publishing functions.") (defvar org-export-odt-auto-postamble t "Should default postamble be inserted? Set by publishing functions.") ;;; Hooks (defvar org-export-odt-after-blockquotes-hook nil "Hook run during ODT export, after blockquote, verse, center are done.") (defvar org-export-odt-final-hook nil "Hook run at the end of ODT export, in the new buffer.") ;;; ODT export (defconst org-odt-sequence-ref-format "%s") (defun org-export-odt-preprocess (parameters) "Convert LaTeX fragments to images." (when (and org-current-export-file (plist-get parameters :LaTeX-fragments)) (org-format-latex (concat "ltxpng/" (file-name-sans-extension (file-name-nondirectory org-current-export-file))) org-current-export-dir nil "Creating LaTeX image %s" nil nil (cond ((eq (plist-get parameters :LaTeX-fragments) 'verbatim) 'verbatim) ((eq (plist-get parameters :LaTeX-fragments) 'mathjax ) 'mathjax) ((eq (plist-get parameters :LaTeX-fragments) t ) 'mathjax) ((eq (plist-get parameters :LaTeX-fragments) 'dvipng ) 'dvipng) (t nil)))) (goto-char (point-min)) (let (label l1) (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t) (org-if-unprotected-at (match-beginning 1) (setq label (match-string 1)) (save-match-data (if (string-match "\\`[a-z]\\{1,10\\}:\\(.+\\)" label) (setq l1 (substring label (match-beginning 1))) (setq l1 label))) ;; (replace-match (format "[[#%s][%s]]" label l1) t t) (replace-match (format org-odt-sequence-ref-format label l1) t t))))) ;;;###autoload (defun org-export-as-odt-and-open (arg) "Export the outline as ODT and immediately open it with a browser. If there is an active region, export only the region. The prefix ARG specifies how many levels of the outline should become headlines. The default is 3. Lower levels will become bulleted lists." (interactive "P") (org-export-as-odt arg 'hidden) (org-open-file buffer-file-name) (when org-export-kill-product-buffer-when-displayed (kill-buffer (current-buffer)))) ;;;###autoload (defun org-export-as-odt-batch () "Call the function `org-export-as-odt'. This function can be used in batch processing as: emacs --batch --load=$HOME/lib/emacs/org.el --eval \"(setq org-export-headline-levels 2)\" --visit=MyFile --funcall org-export-as-odt-batch" (org-export-as-odt org-export-headline-levels 'hidden)) ;;;###autoload (defun org-export-as-odt-to-buffer (arg) "Call `org-export-as-odt` with output to a temporary buffer. No file is created. The prefix ARG is passed through to `org-export-as-odt'." (interactive "P") (org-export-as-odt arg nil nil "*Org ODT Export*") (when org-export-show-temporary-export-buffer (switch-to-buffer-other-window "*Org ODT Export*"))) ;;;###autoload (defun org-replace-region-by-odt (beg end) "Assume the current region has org-mode syntax, and convert it to ODT. This can be used in any buffer. For example, you could write an itemized list in org-mode syntax in an ODT buffer and then use this command to convert it." (interactive "r") (let (reg odt buf pop-up-frames) (save-window-excursion (if (org-mode-p) (setq odt (org-export-region-as-odt beg end t 'string)) (setq reg (buffer-substring beg end) buf (get-buffer-create "*Org tmp*")) (with-current-buffer buf (erase-buffer) (insert reg) (org-mode) (setq odt (org-export-region-as-odt (point-min) (point-max) t 'string))) (kill-buffer buf))) (delete-region beg end) (insert odt))) ;;;###autoload (defun org-export-region-as-odt (beg end &optional body-only buffer) "Convert region from BEG to END in org-mode buffer to ODT. If prefix arg BODY-ONLY is set, omit file header, footer, and table of contents, and only produce the region of converted text, useful for cut-and-paste operations. If BUFFER is a buffer or a string, use/create that buffer as a target of the converted ODT. If BUFFER is the symbol `string', return the produced ODT as a string and leave not buffer behind. For example, a Lisp program could call this function in the following way: (setq odt (org-export-region-as-odt beg end t 'string)) When called interactively, the output buffer is selected, and shown in a window. A non-interactive call will only return the buffer." (interactive "r\nP") (when (interactive-p) (setq buffer "*Org ODT Export*")) (let ((transient-mark-mode t) (zmacs-regions t) ext-plist rtn) (setq ext-plist (plist-put ext-plist :ignore-subtree-p t)) (goto-char end) (set-mark (point)) ;; to activate the region (goto-char beg) (setq rtn (org-export-as-odt nil nil ext-plist buffer body-only)) (if (fboundp 'deactivate-mark) (deactivate-mark)) (if (and (interactive-p) (bufferp rtn)) (switch-to-buffer-other-window rtn) rtn))) (defvar odt-table-tag nil) ; dynamically scoped into this. (defvar org-par-open nil) ;;; org-odt-cvt-link-fn (defconst org-odt-cvt-link-fn nil "Function to convert link URLs to exportable URLs. Takes two arguments, TYPE and PATH. Returns exportable url as (TYPE PATH), or nil to signal that it didn't handle this case. Intended to be locally bound around a call to `org-export-as-odt'." ) (defun org-odt-cvt-org-as-odt (opt-plist type path) "Convert an org filename to an equivalent odt filename. If TYPE is not file, just return `nil'. See variable `org-export-odt-link-org-files-as-odt'" (save-match-data (and org-export-odt-link-org-files-as-odt (string= type "file") (string-match "\\.org$" path) (progn (list "file" (concat (substring path 0 (match-beginning 0)) "." (plist-get opt-plist :odt-extension))))))) ;;; org-odt-should-inline-p (defun org-odt-should-inline-p (filename descp) "Return non-nil if link FILENAME should be inlined. The decision to inline the FILENAME link is based on the current settings. DESCP is the boolean of whether there was a link description. See variables `org-export-odt-inline-images' and `org-export-odt-inline-image-extensions'." (declare (special org-export-odt-inline-images org-export-odt-inline-image-extensions)) (and (or (eq t org-export-odt-inline-images) (and org-export-odt-inline-images (not descp))) (org-file-image-p filename org-export-odt-inline-image-extensions))) ;;; org-odt-make-link (defun org-odt-make-link (opt-plist type path fragment desc attr may-inline-p &optional xref) "Make an ODT link. OPT-PLIST is an options list. TYPE is the device-type of the link (THIS://foo.odt) PATH is the path of the link (http://THIS#locationx) FRAGMENT is the fragment part of the link, if any (foo.odt#THIS) DESC is the link description, if any. ATTR is a string of other attributes of the a element. MAY-INLINE-P allows inlining it as an image." (setq xref (and org-export-odt-use-bookmarks-for-internal-links xref)) (declare (special org-par-open)) (save-match-data (let* ((filename path) ;;First pass. Just sanity stuff. (components-1 (cond ((string= type "file") (list type ;;Substitute just if original path was absolute. ;;(Otherwise path must remain relative) (if (file-name-absolute-p path) (concat "file://" (expand-file-name path)) path))) ((string= type "") (list nil path)) (t (list type path)))) ;;Second pass. Components converted so they can refer ;;to a remote site. (components-2 (or (and org-odt-cvt-link-fn (apply org-odt-cvt-link-fn opt-plist components-1)) (apply #'org-odt-cvt-org-as-odt opt-plist components-1) components-1)) (type (first components-2)) (thefile (second components-2))) ;;Third pass. Build final link except for leading type ;;spec. (cond ;; FIXME: for internal links assumes thefile as empty ((and (or (not type)) fragment) (setq thefile (let ((prefix (if xref org-export-odt-bookmark-prefix "#"))) (concat prefix fragment)))) ((or (string= type "http") (string= type "https")) (setq xref nil) (if fragment (setq thefile (concat thefile "#" fragment)))) (t (setq xref nil))) ;;Final URL-build, for all types. (setq thefile (let ((str (org-export-odt-format-href thefile))) (if (and type (not (string= "file" type))) (concat type ":" str) str))) (if (and may-inline-p ;;Can't inline a URL with a fragment. (not fragment)) (progn (message "image %s %s" thefile org-par-open) (org-export-odt-format-image thefile org-par-open)) ;; FIXME: how to handle attr in xref case. For now it is ;; ignored (org-odt-format-internal-link thefile (org-export-odt-format-desc desc) xref (and (not xref) (or attr ""))))))) (defconst org-odt-document-content-header " ") (defun org-odt-fill-tabs-and-spaces (line) (replace-regexp-in-string "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s) (cond ((string= s "\t") "") ((> (length s) 1) (format "" (length s))) (t " "))) line)) ;; ODT: FOOTNOTES (defun org-odt-get-footnote-definitions (lines) "Given a list of LINES, return a list of alist footnotes." (let ((list nil) line) (while (setq line (pop lines)) (if (string-match "^[ \t]*\\[\\([0-9]+\\)\\] \\(.+\\)" line) (push (cons (match-string 1 line) (match-string 2 line)) list))) list)) (defun org-odt-format-footnote (n def) (format " %s %s " n n def)) (defun org-odt-format-footnote-ref (n) (format " %s " n n)) ;; email ;; (when (and org-export-email-info email (string-match "\\S-" email)) ;; (if (listp (split-string email ",+ *")) ;; (mapc (lambda(e)) ;; (split-string email ",+ *")))) ;; lang-words: ("en" "Author" "Date" "Table of Contents" "Footnotes") ;; ODT: META FILE (defun org-export-odt-update-meta-file (opt-plist) (with-current-buffer (find-file-noselect "meta.xml" (file-name-directory buffer-file-name)) (let ((date (plist-get opt-plist :date)) (author (plist-get opt-plist :author)) (language (plist-get opt-plist :language)) (email (plist-get opt-plist :email)) (keywords (plist-get opt-plist :keywords)) (description (plist-get opt-plist :description)) (title (plist-get opt-plist :title))) (cond ((and date (string-match "%" date)) (setq date (format-time-string date))) (date) (t (setq date (format-time-string "%Y-%m-%d %T %Z")))) ;; FIXME: Revisit these two lines. Seems bogus to me (setq date (and org-export-time-stamp-file date)) (setq author (and org-export-author-info author)) (insert (format " %s \n" (or author "")) (format " %s \n" (or author "")) (format " %s \n" (or date "")) (format " %s \n" (or date "")) (when org-export-creator-info (format " Org-%s/Emacs-%s \n" org-version emacs-version)) (format " %s \n" (or keywords "")) ;; FIXME: Is subject same as description (format " %s \n" (or description "")) (format " %s \n" (or title "")))))) ;; ODT: TABLE OF CONTENTS (defcustom org-export-odt-use-bookmarks-for-toc t "Export Internal links as bookmarks?.") (defun org-odt-toc-parse-headline (line level-offset umax-toc lines org-last-level) (when (and (string-match org-todo-line-regexp line) (not (get-text-property 0 'org-protected line))) (let (level txt todo) (setq level (org-tr-level (- (match-end 1) (match-beginning 1) level-offset)) txt (save-match-data (org-odt-expand (org-export-cleanup-toc-line (match-string 3 line)))) todo (when org-export-mark-todo-in-toc (or (and (match-beginning 2) (not (member (match-string 2 line) org-done-keywords))) ;; TODO, not DONE (and (= level umax-toc) (org-search-todo-below line lines level))))) (when (<= level umax-toc) (org-odt-toc-ascend-descend-levels (- level org-last-level)) (org-odt-toc-insert-item todo txt (org-section-number level)) level)))) (defun org-odt-toc-check-any-target (line) (while (string-match org-any-target-regexp line) (setq line (replace-match (concat "@" (match-string 1 line) "@ ") t t line))) line) (defun org-odt-toc-ascend-descend-levels (cnt) (cond ((> cnt 0) (while (>= (setq cnt (1- cnt)) 0) (org-odt-open-list "OrgBulletedList") (org-odt-open-list-item)) (insert "\n")) ((< cnt 0) (setq cnt (- cnt)) (while (>= (setq cnt (1- cnt)) 0) (org-odt-close-list-item) (org-odt-close-list) (org-odt-close-list-item) (org-odt-open-list-item) (insert "\n"))) (t (org-odt-close-list-item) (org-odt-open-list-item)))) (defun org-odt-toc-insert-item (todo txt snumber) (when (string-match (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt) (setq txt (replace-match "    \\1" t nil txt))) (when (string-match quote-re0 txt) (setq txt (replace-match "" t t txt))) (when org-export-with-section-numbers (setq txt (concat snumber " " txt))) (while (string-match "<\\(<\\)+\\|>\\(>\\)+" txt) (setq txt (replace-match "" t t txt))) (org-odt-open-par) (let* ((href (replace-regexp-in-string "\\." "_" (format "sec-%s" snumber))) (href (or (cdr (assoc href org-export-preferred-target-alist)) href)) (prefix (if org-export-odt-use-bookmarks-for-toc org-export-odt-bookmark-prefix "#")) (href (format "%s%s" prefix href))) ;; FIXME: todo case is not handled right now (when todo (setq txt (format "%s" txt))) (insert (org-odt-format-internal-link href txt org-export-odt-use-bookmarks-for-toc ""))) (org-odt-close-par)) (defun org-odt-insert-toc (the-toc first-heading-pos) (when thetoc (goto-char (point-min)) (if (or (re-search-forward "

\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*

" nil t) (re-search-forward "\\[TABLE-OF-CONTENTS\\]" nil t)) (progn (goto-char (match-beginning 0)) (replace-match "")) (goto-char first-heading-pos) (when (looking-at "\\s-*

") (goto-char (match-end 0)) (insert "\n"))) (org-odt-open-section "table-of-contents") (mapc 'insert thetoc) (org-odt-close-section))) (defun org-odt-do-toc (h lines level-offset umax-toc) (let ((org-min-level (org-get-min-level lines level-offset)) (org-last-level org-min-level)) (with-temp-buffer (org-odt-insert-heading h org-export-odt-toplevel-hlevel) (org-odt-open-section "text-table-of-contents") ;; FIXME: This shouldn't be zero (setq org-last-level 0) (setq lines (mapcar '(lambda (line) (let ((l (org-odt-toc-parse-headline line level-offset umax-toc lines org-last-level))) (when l (setq org-last-level l) (setq line (org-odt-toc-check-any-target line)))) line) lines)) (while (> org-last-level (1- org-min-level)) (setq org-last-level (1- org-last-level)) (org-odt-close-list-item) (org-odt-close-list)) (org-odt-close-section) (buffer-string)))) ;; FINAL CLEANUP (defun org-odt-delete-empty-paragraphs () (interactive) (goto-char (point-min)) (let ((open "") (close "")) (while (re-search-forward (format "%s[ \r\n\t]*%s" open close) nil t) ;; (read-string "Enter:") (replace-match "")))) (defun org-odt-delete-empty-lists () (goto-char (point-min)) (while (re-search-forward "
  • [ \r\n\t]*
  • \n?" nil t) (replace-match ""))) (defun org-odt-fixup-whitespace-placeholders () (goto-char (point-min)) (let (beg end n) (while (setq beg (next-single-property-change (point) 'org-whitespace)) (setq n (get-text-property beg 'org-whitespace) end (next-single-property-change beg 'org-whitespace)) (goto-char beg) (delete-region beg end) (insert (format "%s" (make-string n ?x)))))) (defun org-odt-misc-cleanup () ;; Remove empty lines at the beginning of the file. (goto-char (point-min)) (when (looking-at "\\s-+\n") (replace-match "")) ;; Remove display properties (remove-text-properties (point-min) (point-max) '(display t))) (defun org-odt-final-cleanup () (org-odt-delete-empty-paragraphs) (org-odt-delete-empty-lists) (org-odt-fixup-whitespace-placeholders) (org-odt-misc-cleanup)) ;;; org-export-as-odt ;;;###autoload (defun org-export-as-odt (arg &optional hidden ext-plist to-buffer body-only pub-dir) "Export the outline as a pretty ODT file. If there is an active region, export only the region. The prefix ARG specifies how many levels of the outline should become headlines. The default is 3. Lower levels will become bulleted lists. HIDDEN is obsolete and does nothing. EXT-PLIST is a property list with external parameters overriding org-mode's default settings, but still inferior to file-local settings. When TO-BUFFER is non-nil, create a buffer with that name and export to that buffer. If TO-BUFFER is the symbol `string', don't leave any buffer behind but just return the resulting ODT as a string. When BODY-ONLY is set, don't produce the file header and footer, simply return the content of ..., without even the body tags themselves. When PUB-DIR is set, use this as the publishing directory." (interactive "P") (run-hooks 'org-export-first-hook) ;; Make sure we have a file name when we need it. (when (and (not (or to-buffer body-only)) (not buffer-file-name)) (if (buffer-base-buffer) (org-set-local 'buffer-file-name (with-current-buffer (buffer-base-buffer) buffer-file-name)) (error "Need a file name to be able to export"))) (message "Exporting...") (setq-default org-todo-line-regexp org-todo-line-regexp) (setq-default org-deadline-line-regexp org-deadline-line-regexp) (setq-default org-done-keywords org-done-keywords) (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp) (let* ((opt-plist (org-export-process-option-filters (org-combine-plists (org-default-export-plist) ext-plist (org-infile-export-plist)))) (body-only (or body-only (plist-get opt-plist :body-only))) (odt-extension (plist-get opt-plist :odt-extension)) (link-validate (plist-get opt-plist :link-validation-function)) valid thetoc first-heading-pos (odd org-odd-levels-only) (region-p (org-region-active-p)) (rbeg (and region-p (region-beginning))) (rend (and region-p (region-end))) (subtree-p (if (plist-get opt-plist :ignore-subtree-p) nil (when region-p (save-excursion (goto-char rbeg) (and (org-at-heading-p) (>= (org-end-of-subtree t t) rend)))))) (level-offset (if subtree-p (save-excursion (goto-char rbeg) (+ (funcall outline-level) (if org-odd-levels-only 1 0))) 0)) (opt-plist (setq org-export-opt-plist (if subtree-p (org-export-add-subtree-options opt-plist rbeg) opt-plist))) ;; The following two are dynamically scoped into other ;; routines below. (org-current-export-dir (or pub-dir (org-export-directory :odt opt-plist))) (org-current-export-file buffer-file-name) (level 0) (line "") (origline "") txt todo (umax nil) (umax-toc nil) (target (if to-buffer nil (expand-file-name (concat (file-name-sans-extension (or (and subtree-p (org-entry-get (region-beginning) "EXPORT_FILE_NAME" t)) (file-name-nondirectory buffer-file-name))) "." (or "odt" odt-extension)) (file-name-as-directory (or pub-dir (org-export-directory :odt opt-plist)))))) (current-dir (if buffer-file-name (file-name-directory buffer-file-name) default-directory)) (outdir (org-export-odt-outdir-init)) (filename (expand-file-name "content.xml" outdir)) (buffer (if to-buffer (cond ((eq to-buffer 'string) (get-buffer-create "*Org ODT Export*")) (t (get-buffer-create to-buffer))) (find-file-noselect filename))) (org-levels-open (make-vector org-level-max nil)) (language (plist-get opt-plist :language)) (title (or (and subtree-p (org-export-get-title-from-subtree)) (plist-get opt-plist :title) (and (not body-only) (not (plist-get opt-plist :skip-before-1st-heading)) (org-export-grab-title-from-buffer)) (and buffer-file-name (file-name-sans-extension (file-name-nondirectory buffer-file-name))) "UNTITLED")) (dummy (setq opt-plist (plist-put opt-plist :title title))) ;; ODT FIXME: figure this out ;; (odt-table-tag (plist-get opt-plist :odt-table-tag)) (quote-re0 (concat "^[ \t]*" org-quote-string "\\>")) (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)")) (inquote nil) (infixed nil) (inverse nil) (in-local-list nil) (local-list-type nil) (local-list-indent nil) (llt org-plain-list-ordered-item-terminator) (lang-words nil) (head-count 0) (start 0) (coding-system (and (boundp 'buffer-file-coding-system) buffer-file-coding-system)) (coding-system-for-write (or org-export-odt-coding-system coding-system)) (save-buffer-coding-system (or org-export-odt-coding-system coding-system)) (charset (and coding-system-for-write (fboundp 'coding-system-get) (coding-system-get coding-system-for-write 'mime-charset))) (region (buffer-substring (if region-p (region-beginning) (point-min)) (if region-p (region-end) (point-max)))) (org-export-have-math nil) (lines (org-split-string (org-export-preprocess-string region :emph-multiline t :for-odt t :skip-before-1st-heading (plist-get opt-plist :skip-before-1st-heading) :drawers (plist-get opt-plist :drawers) :todo-keywords (plist-get opt-plist :todo-keywords) :tags (plist-get opt-plist :tags) :priority (plist-get opt-plist :priority) :footnotes (plist-get opt-plist :footnotes) :timestamps (plist-get opt-plist :timestamps) :archived-trees (plist-get opt-plist :archived-trees) :select-tags (plist-get opt-plist :select-tags) :exclude-tags (plist-get opt-plist :exclude-tags) :add-text (plist-get opt-plist :text) :LaTeX-fragments (plist-get opt-plist :LaTeX-fragments)) "[\r\n]")) (mathjax "") table-open type table-buffer table-orig-buffer ind item-type starter rpl path attr desc descp desc1 desc2 link nodesc snumber fnc item-tag item-number footnotes footref-seen id-file href footnote-def footnote-list ) (setq footnote-list (org-odt-get-footnote-definitions lines)) (let ((inhibit-read-only t)) (org-unmodified (remove-text-properties (point-min) (point-max) '(:org-license-to-kill t)))) (message "Exporting...") (org-init-section-numbers) ;; Get the language-dependent settings (setq lang-words (or (assoc language org-export-language-setup) (assoc "en" org-export-language-setup))) ;; Switch to the output buffer (set-buffer buffer) (let ((inhibit-read-only t)) (erase-buffer)) (fundamental-mode) (org-install-letbind) (and (fboundp 'set-buffer-file-coding-system) (set-buffer-file-coding-system coding-system-for-write)) (let (style (case-fold-search nil) (org-odd-levels-only odd)) ;; create local variables for all options, to make sure all called ;; functions get the correct information (mapc (lambda (x) (set (make-local-variable (nth 2 x)) (plist-get opt-plist (car x)))) org-export-plist-vars) (setq umax (if arg (prefix-numeric-value arg) org-export-headline-levels)) (setq umax-toc (if (integerp org-export-with-toc) (min org-export-with-toc umax) umax)) (unless body-only (insert org-odt-document-content-header) (org-export-odt-insert-plist-item opt-plist :preamble opt-plist) ;; FIXME: (plist-get opt-plist :auto-preamble)? (when (and org-export-odt-auto-preamble title) (insert (format org-export-odt-title-format "Heading_20_1.title" (org-odt-expand title))))) (setq thetoc (and org-export-with-toc (not body-only) (org-odt-do-toc (nth 3 lang-words) lines level-offset umax-toc))) (setq head-count 0) (org-init-section-numbers) (org-odt-do-open-par) (while (setq line (pop lines) origline line) (catch 'nextline ;; end of quote section? (when (and inquote (string-match "^\\*+ " line)) (insert "\n") (org-odt-do-open-par) (setq inquote nil)) ;; inside a quote section? (when inquote (insert (org-odt-protect line) "\n") (throw 'nextline nil)) ;; Fixed-width, verbatim lines (examples) (when (and org-export-with-fixed-width (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line)) (when (not infixed) (setq infixed t) (org-odt-do-open-par "Example")) (insert (org-odt-protect (match-string 3 line)) "\n") (when (or (not lines) (not (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" (car lines)))) (setq infixed nil) (org-odt-do-open-par)) (throw 'nextline nil)) ;; Explicit list closure (when (equal "ORG-LIST-END" line) (while local-list-indent (org-odt-do-close-list-item (car local-list-type)) ;; (insert (format "\n" (car local-list-type))) (org-odt-close-list) (pop local-list-type) (pop local-list-indent)) (setq in-local-list nil) (org-odt-do-open-par) (throw 'nextline nil)) ;; Protected ODT ;; FIXME: take care of leading spaces? Or better still ;; find a better textual environment (when (get-text-property 0 'org-protected line) (let (par (ind (get-text-property 0 'original-indentation line))) (when (re-search-backward "\\(\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t) (setq par (match-string 1)) (replace-match "\\2\n")) ;; (insert line "\n") (insert (org-odt-fill-tabs-and-spaces line) "") (while (and lines (or (= (length (car lines)) 0) (not ind) (equal ind (get-text-property 0 'original-indentation (car lines)))) (or (= (length (car lines)) 0) (get-text-property 0 'org-protected (car lines)))) (org-odt-open-par "OrgSourceBlock") (insert (org-odt-fill-tabs-and-spaces (pop lines))) (org-odt-close-par)) ;; this terminates the example section (and par (org-odt-open-par "Text_20_body"))) (throw 'nextline nil)) ;; Blockquotes, verse, and center (setq style nil) (cond ((equal "ORG-BLOCKQUOTE-START" line) (setq style "Quotations")) ((equal "ORG-CENTER-START" line) (setq style "Orgmode-Center")) ((equal "ORG-VERSE-START" line) (setq inverse t) (setq style "Orgmode-Verse"))) (when style (org-odt-do-open-par style) (throw 'nextline nil)) (when (or (equal "ORG-BLOCKQUOTE-END" line) (equal "ORG-CENTER-END" line) (equal "ORG-VERSE-END" line)) (org-odt-do-open-par) (setq inverse nil) (throw 'nextline nil)) (run-hooks 'org-export-odt-after-blockquotes-hook) (when inverse (let ((i (org-get-string-indentation line))) (if (> i 0) ;; (setq line (concat (mapconcat 'identity ;; (make-list (* 2 i) "\\nbsp") "") ;; " " (org-trim line))) (setq line (concat (format "" (* 2 i)) " " (org-trim line)))) (unless (string-match "\\\\\\\\[ \t]*$" line) (setq line (concat line "\\\\"))))) ;; make targets to anchors (setq start 0) (while (string-match "<<]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line start) (cond ((get-text-property (match-beginning 1) 'org-protected line) (setq start (match-end 1))) ((match-end 2) (setq line (replace-match (format "@@" (org-solidify-link-text (match-string 1 line)) (org-solidify-link-text (match-string 1 line))) t t line))) ((and org-export-with-toc (equal (string-to-char line) ?*)) ;; FIXME: NOT DEPENDENT on TOC????????????????????? ;; headline that is radioed (setq line (replace-match (match-string 1 line) t t line))) (t (setq line (replace-match (concat "@" (match-string 1 line) "@ ") t t line))))) (setq line (org-odt-handle-time-stamps line)) ;; replace "&" by "&", "<" and ">" by "<" and ">" ;; handle @<..> ODT tags (replace "@>..<" by "<..>") ;; Also handle sub_superscripts and checkboxes (or (string-match org-table-hline-regexp line) (setq line (org-odt-expand line))) ;; Format the links (setq start 0) (while (string-match org-bracket-link-analytic-regexp++ line start) (setq start (match-beginning 0)) (setq path (save-match-data (org-link-unescape (match-string 3 line)))) (setq type (cond ((match-end 2) (match-string 2 line)) ((save-match-data (or (file-name-absolute-p path) (string-match "^\\.\\.?/" path))) "file") (t "internal"))) (setq path (org-extract-attributes (org-link-unescape path))) (setq nodesc (get-text-property 0 'org-no-description path)) (setq attr (get-text-property 0 'org-attributes path)) (setq desc1 (if (match-end 5) (match-string 5 line)) desc2 (if (match-end 2) (concat type ":" path) path) descp (and desc1 (not (equal desc1 desc2))) desc (or desc1 desc2)) ;; Make an image out of the description if that is so wanted (when (and descp (org-file-image-p desc org-export-odt-inline-image-extensions)) (save-match-data (if (string-match "^file:" desc) (setq desc (substring desc (match-end 0))))) (setq desc (org-add-props (concat "") '(org-protected t)))) (cond ((equal type "internal") (let ((frag-0 (if (= (string-to-char path) ?#) (substring path 1) path)) (xref nodesc)) (setq rpl (org-odt-make-link opt-plist "" "" (org-solidify-link-text (save-match-data (org-link-unescape frag-0)) nil) desc attr nil xref)))) ((and (equal type "id") (setq id-file (org-id-find-id-file path))) ;; This is an id: link to another file (if it was the same file, ;; it would have become an internal link...) (save-match-data (setq id-file (file-relative-name id-file (file-name-directory org-current-export-file))) (setq rpl (org-odt-make-link opt-plist "file" id-file (concat (if (org-uuidgen-p path) "ID-") path) desc attr nil)))) ((member type '("http" "https")) ;; standard URL, can inline as image (setq rpl (org-odt-make-link opt-plist type path nil desc attr (org-odt-should-inline-p path descp)))) ((member type '("ftp" "mailto" "news")) ;; standard URL, can't inline as image (setq rpl (org-odt-make-link opt-plist type path nil desc attr nil))) ((string= type "coderef") (let* ((coderef-str (format "coderef-%s" path)) (attr-1 (format "class=\"coderef\" onmouseover=\"CodeHighlightOn(this, '%s');\" onmouseout=\"CodeHighlightOff(this, '%s');\"" coderef-str coderef-str))) (setq rpl (org-odt-make-link opt-plist type "" coderef-str (format (org-export-get-coderef-format path (and descp desc)) (cdr (assoc path org-export-code-refs))) attr-1 nil)))) ((functionp (setq fnc (nth 2 (assoc type org-link-protocols)))) ;; The link protocol has a function for format the link (setq rpl (save-match-data (funcall fnc (org-link-unescape path) desc1 'odt)))) ((string= type "file") ;; FILE link (save-match-data (let* ((components (if (string-match "::\\(.*\\)" path) (list (replace-match "" t nil path) (match-string 1 path)) (list path nil))) ;;The proper path, without a fragment (path-1 (first components)) ;;The raw fragment (fragment-0 (second components)) ;;Check the fragment. If it can't be used as ;;target fragment we'll pass nil instead. (fragment-1 (if (and fragment-0 (not (string-match "^[0-9]*$" fragment-0)) (not (string-match "^\\*" fragment-0)) (not (string-match "^/.*/$" fragment-0))) (org-solidify-link-text (org-link-unescape fragment-0)) nil)) (desc-2 ;;Description minus "file:" and ".org" (if (string-match "^file:" desc) (let ((desc-1 (replace-match "" t t desc))) (if (string-match "\\.org$" desc-1) (replace-match "" t t desc-1) desc-1)) desc))) (setq rpl (if (and (functionp link-validate) (not (funcall link-validate path-1 current-dir))) desc (org-odt-make-link opt-plist "file" path-1 fragment-1 desc-2 attr (org-odt-should-inline-p path-1 descp))))))) (t ;; just publish the path, as default (setq rpl (concat "<" type ":" (save-match-data (org-link-unescape path)) ">")))) (setq line (replace-match rpl t t line) start (+ start (length rpl)))) ;; TODO items (if (and (string-match org-todo-line-regexp line) (match-beginning 2)) (setq line (concat (substring line 0 (match-beginning 2)) " " (org-export-odt-get-todo-kwd-class-name (match-string 2 line)) "" (substring line (match-end 2))))) ;; Does this contain a reference to a footnote? (when org-export-with-footnotes (setq start 0) (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start) (if (get-text-property (match-beginning 2) 'org-protected line) (setq start (match-end 2)) (let* ((n (match-string 2 line)) (def (cdr (assoc n footnote-list)))) (cond ((assoc n footref-seen) (setq line (replace-match (format "%s%s" (or (match-string 1 line) "") (org-odt-format-footnote-ref n)) t t line))) (t (push (cons n 1) footref-seen) (setq line (replace-match (format "%s%s" (or (match-string 1 line) "") (org-odt-format-footnote n def)) t t line)))))))) (cond ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line) ;; This is a headline (setq level (org-tr-level (- (match-end 1) (match-beginning 1) level-offset)) txt (match-string 2 line)) (if (string-match quote-re0 txt) (setq txt (replace-match "" t t txt))) (if (<= level (max umax umax-toc)) (setq head-count (+ head-count 1))) ;; FIXME: Empty lines before the first headline creates a ;; paragraph. Unless they are closed, it interferes with ;; the placement of table of contents. For now just close ;; such paragraphs. (org-odt-close-par-maybe) (setq first-heading-pos (or first-heading-pos (point))) (org-odt-level-start level txt umax (and org-export-with-toc (<= level umax)) head-count) ;; QUOTES (when (string-match quote-re line) (org-odt-close-par-maybe) (insert "
    ")
    	      (setq inquote t)))
    
    	   ((and org-export-with-tables
    		 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
    	    (when (not table-open)
    	      ;; New table starts
    	      (setq table-open t table-buffer nil table-orig-buffer nil))
    
    	    ;; Accumulate lines
    	    (setq table-buffer (cons line table-buffer)
    		  table-orig-buffer (cons origline table-orig-buffer))
    	    (when (or (not lines)
    		      (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
    					 (car lines))))
    	      (setq table-open nil
    		    table-buffer (nreverse table-buffer)
    		    table-orig-buffer (nreverse table-orig-buffer))
    	      (org-odt-close-par-maybe)
    	      (insert (org-format-table-odt table-buffer table-orig-buffer))))
    	   (t
    	    ;; Normal lines
    	    (when (string-match
    		   (cond
    		    ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
    		    ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
    		    ((= llt ?\)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
    		    (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
    		   line)
    	      (setq ind (or (get-text-property 0 'original-indentation line)
    			    (org-get-string-indentation line))
    		    item-type (if (match-beginning 4) "o" "u")
    		    starter (if (match-beginning 2)
    				(substring (match-string 2 line) 0 -1))
    		    line (substring line (match-beginning 5))
    		    item-number nil
    		    item-tag nil)
    	      (if (string-match "\\[@\\(?:start:\\)?\\([0-9]+\\)\\][ \t]?" line)
    		  (setq item-number (match-string 1 line)
    			line (replace-match "" t t line)))
    	      (if (and starter (string-match "\\(.*?\\) ::[ \t]*" line))
    		  (setq item-type "d"
    			item-tag (match-string 1 line)
    			line (substring line (match-end 0))))
    	      (cond
    	       ((and starter
    		     (or (not in-local-list)
    			 (> ind (car local-list-indent))))
    		;; Start new (level of) list
    
    
    		(org-odt-close-par-maybe)
    
    		(cond
    		 ((equal item-type "u") (org-odt-do-open-list item-type))
    		 ((equal item-type "o") (org-odt-do-open-list item-type))
    
    		 ;; TODO
    		 ((and (equal item-type "o") item-number)
    		  (format "
      \n
    1. \n" item-number)) ((equal item-type "d") (org-odt-do-open-description-list item-tag))) (push item-type local-list-type) (push ind local-list-indent) (setq in-local-list t)) ;; Continue list (starter ;; terminate any previous sublist but first ensure ;; list is not ill-formed. (let ((min-ind (apply 'min local-list-indent))) (when (< ind min-ind) (setq ind min-ind))) (while (< ind (car local-list-indent)) (org-odt-do-close-list-item (car local-list-type)) (org-odt-close-list) (pop local-list-type) (pop local-list-indent) (setq in-local-list local-list-indent)) ;; insert new item (org-odt-do-close-list-item (car local-list-type)) (cond ((equal (car local-list-type) "d") (org-odt-do-open-description-list-item item-tag)) ;; TODO ((and (equal item-type "o") item-number) (format "
    2. \n" item-number)) (t (org-odt-do-open-list-item))))) (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line) (setq line (replace-match (if (equal (match-string 1 line) "X") "[X]" "[X]") t t line)))) ;; Horizontal line (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line) (if org-par-open (insert "\n

      \n
      \n

      \n") (insert "\n


      \n")) (throw 'nextline nil)) ;; Empty lines start a new paragraph. If hand-formatted lists ;; are not fully interpreted, lines starting with "-", "+", "*" ;; also start a new paragraph. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-odt-do-open-par)) ;; Is this the start of a footnote? (when org-export-with-footnotes (when (and (boundp 'footnote-section-tag-regexp) (string-match (concat "^" footnote-section-tag-regexp) line)) ;; ignore this line (throw 'nextline nil)) (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line) (org-odt-close-par-maybe) ;; ignore this line (throw 'nextline nil))) ;; Check if the line break needs to be conserved (cond ((string-match "\\\\\\\\[ \t]*$" line) (setq line (replace-match "" t t line))) (org-export-preserve-breaks (setq line (concat line "")))) ;; Check if a paragraph should be started (let ((start 0)) (while (and org-par-open (string-match "\\\\par\\>" line start)) ;; Leave a space in the

      so that the footnote matcher ;; does not see this. (if (not (get-text-property (match-beginning 0) 'org-protected line)) (setq line (replace-match "

      " t t line))) (setq start (match-end 0)))) (insert line "\n"))))) ;; Properly close all local lists and other lists (when inquote (insert "

    \n") (org-odt-do-open-par)) (org-odt-level-start 1 nil umax (and org-export-with-toc (<= level umax)) head-count) ;; the to close the last text-... div. (when (and (> umax 0) first-heading-pos) (insert "
    \n")) (let ((bib (org-export-odt-get-bibliography))) (when bib (insert "\n" bib "\n"))) (unless body-only (org-export-odt-update-meta-file opt-plist) (org-export-odt-insert-plist-item opt-plist :postamble opt-plist) ;; odt code ;; (insert "\n") (org-odt-close-section) ;; odt code ;; (insert "\n") ;; (insert "\n\n") (insert "\n
    ") (insert "\n
    ") (insert "\n
    \n")) (unless (plist-get opt-plist :buffer-will-be-killed) (normal-mode) (if (eq major-mode (default-value 'major-mode)) (odt-mode))) (org-odt-insert-toc thetoc first-heading-pos) (org-odt-final-cleanup) ;; Run the hook (run-hooks 'org-export-odt-final-hook) (or to-buffer (progn (org-export-odt-zip outdir target) ;; FIXME: use find-file-noselect? (find-file target))) (goto-char (point-min)) (or (org-export-push-to-kill-ring "ODT") (message "Exporting... done")) (if (eq to-buffer 'string) (prog1 (buffer-substring (point-min) (point-max)) (kill-buffer (current-buffer))) (current-buffer))))) (defun org-export-odt-insert-plist-item (plist key &rest args) (let ((item (plist-get plist key))) (cond ((functionp item) (apply item args)) (item (insert item))))) (defun org-export-odt-format-href (s) "Make sure the S is valid as a href reference in an XODT document." (save-match-data (let ((start 0)) (while (string-match "&" s start) (setq start (+ (match-beginning 0) 3) s (replace-match "&" t t s))))) s) (defun org-export-odt-format-desc (s) "Make sure the S is valid as a description in a link." (if (and s (not (get-text-property 1 'org-protected s))) (save-match-data (org-odt-do-expand s)) s)) ;; DON'T DISTURB ;; (defun org-export-odt-format-image (src par-open) ;; "Create image tag with source and attributes." ;; (save-match-data ;; (if (string-match "^ltxpng/" src) ;; (format "\"%s\"/" ;; src (org-find-text-property-in-string 'org-latex-src src)) ;; (let* ((caption (org-find-text-property-in-string 'org-caption src)) ;; (attr (org-find-text-property-in-string 'org-attributes src)) ;; (label (org-find-text-property-in-string 'org-label src))) ;; (setq caption (and caption (org-odt-do-expand caption))) ;; (concat ;; (if caption ;; (format "%s
    ;;

    " ;; (if org-par-open "

    \n" "") ;; (if label (format "id=\"%s\" " label) ""))) ;; (format "" ;; src ;; (if (string-match "\\%s ;;
    %s" ;; (concat "\n

    " caption "

    ") ;; (if org-par-open "\n

    " "")))))))) (defun org-export-odt-format-image (src par-open) "Create image tag with source and attributes." (save-match-data (if (string-match "^ltxpng/" src) (format "\"%s\"/" src (org-find-text-property-in-string 'org-latex-src src)) (let* ((caption (org-find-text-property-in-string 'org-caption src)) (attr (org-find-text-property-in-string 'org-attributes src)) (label (org-find-text-property-in-string 'org-label src))) (setq caption (and caption (org-odt-do-expand caption))) (org-odt-embed-image src))))) (defun org-export-odt-get-bibliography () "Find bibliography, cut it out and return it." (catch 'exit (let (beg end (cnt 1) bib) (save-excursion (goto-char (point-min)) (when (re-search-forward "^[ \t]*

    " nil t) (setq cnt (+ cnt (if (string= (match-string 0) "") (forward-char 1)) (setq bib (buffer-substring beg (point))) (delete-region beg (point)) (throw 'exit bib)))) nil)))) (defvar org-table-number-regexp) ; defined in org-table.el (defun org-format-table-odt (lines olines) "Find out which ODT converter to use and return the ODT code." (if (stringp lines) (setq lines (org-split-string lines "\n"))) (if (string-match "^[ \t]*|" (car lines)) ;; A normal org table (org-format-org-table-odt lines) ;; Table made by table.el - test for spanning (let* ((hlines (delq nil (mapcar (lambda (x) (if (string-match "^[ \t]*\\+-" x) x nil)) lines))) (first (car hlines)) (ll (and (string-match "\\S-+" first) (match-string 0 first))) (re (concat "^[ \t]*" (regexp-quote ll))) (spanning (delq nil (mapcar (lambda (x) (not (string-match re x))) hlines)))) (if (and (not spanning) (not org-export-prefer-native-exporter-for-tables)) ;; We can use my own converter with ODT conversions (org-format-table-table-odt lines) ;; Need to use the code generator in table.el, with the original text. (org-format-table-table-odt-using-table-generate-source olines))))) (defvar org-table-number-fraction) ; defined in org-table.el (defconst org-odt-table-caption-format "Table %s %s") (defun org-format-org-table-odt (lines &optional splice) "Format a table into ODT." (require 'org-table) ;; Get rid of hlines at beginning and end (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines))) (setq lines (nreverse lines)) (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines))) (setq lines (nreverse lines)) (when org-export-table-remove-special-lines ;; Check if the table has a marking column. If yes remove the ;; column and the special lines (setq lines (org-table-clean-before-export lines))) (let* ((caption (org-find-text-property-in-string 'org-caption (car lines))) (label (org-find-text-property-in-string 'org-label (car lines))) ;; ODT FIXME: Attributes ;; (attributes (org-find-text-property-in-string 'org-attributes ;; (car lines))) ;; (odt-table-tag (org-export-splice-attributes ;; odt-table-tag attributes)) (head (and org-export-highlight-first-table-line (delq nil (mapcar (lambda (x) (string-match "^[ \t]*|-" x)) (cdr lines))))) (nline 0) fnum nfields i tbopen line fields odt gr colgropen rowstart rowend (style '())) (setq caption (and caption (org-odt-do-expand caption))) (if splice (setq head nil)) (unless splice (push (if head (car org-odt-table-header-rows-tags) (car org-odt-table-rows-tags)) odt)) (setq tbopen t) (while (setq line (pop lines)) (catch 'next-line (if (string-match "^[ \t]*|-" line) (progn (unless splice (push (if head (cdr org-odt-table-header-rows-tags) (cdr org-odt-table-rows-tags)) odt) (if lines (push (car org-odt-table-rows-tags) odt) (setq tbopen nil))) (setq head nil) ;; head ends here, first time around ;; ignore this line (throw 'next-line t))) ;; Break the line into fields (setq fields (org-split-string line "[ \t]*|[ \t]*")) (unless fnum (setq fnum (make-vector (length fields) 0) nfields (length fnum))) (setq nline (1+ nline) i -1 rowstart (eval (car org-odt-table-row-tags)) rowend (eval (cdr org-odt-table-row-tags))) (push (concat rowstart (mapconcat (lambda (x) (setq i (1+ i)) (if (and (< i nfields) ; make sure no rogue line causes an error here (string-match org-table-number-regexp x)) (incf (aref fnum i))) (cond ;; ODT FIXME (head (concat (format (car org-odt-table-header-tags) (or (nth i style) "Default")) x (cdr org-odt-table-header-tags))) ;; ((and (= i 0) org-export-odt-table-use-header-tags-for-first-column) ;; (concat ;; (format (car org-odt-table-header-tags) "row") ;; x ;; (cdr org-odt-table-header-tags))) (t (concat (format (car org-odt-table-data-tags) (or (nth i style) "Default")) x (cdr org-odt-table-data-tags))))) fields "") rowend) odt))) (unless splice (if tbopen (push (cdr org-odt-table-rows-tags) odt))) (unless splice (push "\n" odt)) (setq odt (nreverse odt)) (unless splice ;; ODT FIXME: colgroups ;; Put in col tags with the alignment (unfortunately often ignored...) (unless (car org-table-colgroup-info) (setq org-table-colgroup-info (cons :start (cdr org-table-colgroup-info)))) ;; (push (mapconcat ;; (lambda (x) ;; (setq gr (pop org-table-colgroup-info)) ;; (format "%s
    %s" ;; (if (memq gr '(:start :startend)) ;; (prog1 ;; (if colgropen "\n" "") ;; (setq colgropen t)) ;; "") ;; (if (> (/ (float x) nline) org-table-number-fraction) ;; "right" "left") ;; (if (memq gr '(:end :startend)) ;; (progn (setq colgropen nil) "") ;; "")) ;; ) ;; fnum "") ;; odt) ;; (if colgropen (setq odt (cons (car odt) (cons "" (cdr odt))))) ;; ODT FIXME: Revisit table:table-column (push (format (car org-odt-table-column-tags) (length fnum)) odt) ;; Since the output of ODT table formatter can also be used in ;; DocBook document, we want to always include the caption to make ;; DocBook XML file valid. ;; ODT FIXME: caption and labels (push (format org-odt-table-tag "TableName" "TableStyle") odt) (if label (push (format org-odt-table-caption-format label label (or caption "")) odt) (push (format org-odt-table-caption-format "FIXME" "FIXME" (or caption "")) odt)) ) (concat (mapconcat 'identity odt "\n") "\n"))) (defun org-export-splice-attributes (tag attributes) "Read attributes in string ATTRIBUTES, add and replace in ODT tag TAG." (if (not attributes) tag (let (oldatt newatt) (setq oldatt (org-extract-attributes-from-string tag) tag (pop oldatt) newatt (cdr (org-extract-attributes-from-string attributes))) (while newatt (setq oldatt (plist-put oldatt (pop newatt) (pop newatt)))) (if (string-match ">" tag) (setq tag (replace-match (concat (org-attributes-to-string oldatt) ">") t t tag))) tag))) (defun org-format-table-table-odt (lines) "Format a table generated by table.el into ODT. This conversion does *not* use `table-generate-source' from table.el. This has the advantage that Org-mode's ODT conversions can be used. But it has the disadvantage, that no cell- or row-spanning is allowed." (let (line field-buffer (head org-export-highlight-first-table-line) fields odt empty i) (setq odt (concat odt-table-tag "\n")) (while (setq line (pop lines)) (setq empty " ") (catch 'next-line (if (string-match "^[ \t]*\\+-" line) (progn (if field-buffer (progn (setq odt (concat odt "" (mapconcat (lambda (x) (if (equal x "") (setq x empty)) (if head (concat (format (car org-odt-table-header-tags) "col") x (cdr org-odt-table-header-tags)) (concat (car org-odt-table-data-tags) x (cdr org-odt-table-data-tags)))) field-buffer "\n") "\n")) (setq head nil) (setq field-buffer nil))) ;; Ignore this line (throw 'next-line t))) ;; Break the line into fields and store the fields (setq fields (org-split-string line "[ \t]*|[ \t]*")) (if field-buffer (setq field-buffer (mapcar (lambda (x) (concat x "
    " (pop fields))) field-buffer)) (setq field-buffer fields)))) (setq odt (concat odt "
    \n")) odt)) (defun org-format-table-table-odt-using-table-generate-source (lines) "Format a table into odt, using `table-generate-source' from table.el. This has the advantage that cell- or row-spanning is allowed. But it has the disadvantage, that Org-mode's ODT conversions cannot be used." (require 'table) (with-current-buffer (get-buffer-create " org-tmp1 ") (erase-buffer) (insert (mapconcat 'identity lines "\n")) (goto-char (point-min)) (if (not (re-search-forward "|[^+]" nil t)) (error "Error processing table")) (table-recognize-table) (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer)) (table-generate-source 'odt " org-tmp2 ") (set-buffer " org-tmp2 ") (buffer-substring (point-min) (point-max)))) (defun org-odt-handle-time-stamps (s) "Format time stamps in string S, or remove them." (catch 'exit (let (r b) (while (string-match org-maybe-keyword-time-regexp s) (or b (setq b (substring s 0 (match-beginning 0)))) (setq r (concat r (substring s 0 (match-beginning 0)) " @" (if (match-end 1) (format "@%s @" (match-string 1 s))) (format " @%s@" (substring (org-translate-time (match-string 3 s)) 1 -1)) "@") s (substring s (match-end 0)))) ;; Line break if line started and ended with time stamp stuff (if (not r) s (setq r (concat r s)) (unless (string-match "\\S-" (concat b s)) (setq r (concat r "@
    "))) r)))) (defvar odtize-buffer-places) ; from odtize.el (defun org-export-odtize-region-for-paste (beg end) "Convert the region to ODT, using odtize.el. This is much like `odtize-region-for-paste', only that it uses the settings define in the org-... variables." (let* ((odtize-output-type org-export-odtize-output-type) (odtize-css-name-prefix org-export-odtize-css-font-prefix) (odtbuf (odtize-region beg end))) (unwind-protect (with-current-buffer odtbuf (buffer-substring (plist-get odtize-buffer-places 'content-start) (plist-get odtize-buffer-places 'content-end))) (kill-buffer odtbuf)))) ;;;###autoload (defun org-export-odtize-generate-css () "Create the CSS for all font definitions in the current Emacs session. Use this to create face definitions in your CSS style file that can then be used by code snippets transformed by odtize. This command just produces a buffer that contains class definitions for all faces used in the current Emacs session. You can copy and paste the ones you need into your CSS file. If you then set `org-export-odtize-output-type' to `css', calls to the function `org-export-odtize-region-for-paste' will produce code that uses these same face definitions." (interactive) (require 'odtize) (and (get-buffer "*odt*") (kill-buffer "*odt*")) (with-temp-buffer (let ((fl (face-list)) (odtize-css-name-prefix "org-") (odtize-output-type 'css) f i) (while (setq f (pop fl) i (and f (face-attribute f :inherit))) (when (and (symbolp f) (or (not i) (not (listp i)))) (insert (org-add-props (copy-sequence "1") nil 'face f)))) (odtize-region (point-min) (point-max)))) (switch-to-buffer "*odt*") (goto-char (point-min)) (if (re-search-forward "" nil t) (delete-region (1+ (match-end 0)) (point-max))) (beginning-of-line 1) (if (looking-at " +") (replace-match "")) (goto-char (point-min))) (defun org-odt-protect (s) "convert & to &, < to < and > to >" ;; FIXME: Messes with verse element. Disable it for now ;; FIXME: If Disabled creates <<<>>> in TOC for Radioed Headlines, ;; so enable it Naturally verse environment is right now broken (when t (let ((start 0)) (while (string-match "&" s start) (setq s (replace-match "&" t t s) start (1+ (match-beginning 0)))) (while (string-match "<" s) (setq s (replace-match "<" t t s))) (while (string-match ">" s) (setq s (replace-match ">" t t s))) ;; (while (string-match "\"" s) ;; (setq s (replace-match """ t t s))) ) ) s) (defun org-odt-expand (string) "Prepare STRING for ODT export. Apply all active conversions. If there are links in the string, don't modify these." (let* ((re (concat org-bracket-link-regexp "\\|" (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))) m s l res) (if (string-match "^[ \t]*\\+-[-+]*\\+[ \t]*$" string) string (while (setq m (string-match re string)) (setq s (substring string 0 m) l (match-string 0 string) string (substring string (match-end 0))) (push (org-odt-do-expand s) res) (push l res)) (push (org-odt-do-expand string) res) (apply 'concat (nreverse res))))) (defcustom org-export-odt-emphasis-alist `(("*" "" "") ("/" "" "") ("_" "" "") ("=" "" "") ("~" "" "") ("+" "" "")) "A list of DocBook expressions to convert emphasis fontifiers. Each element of the list is a list of three elements. The first element is the character used as a marker for fontification. The second element is a formatting string to wrap fontified text with. The third element decides whether to protect converted text from other conversions." :group 'org-export-odt :type 'alist) ;; FIXME: rename, this is a general variable (defcustom org-export-odt-expand t "Non-nil means for ODT export, treat @<...> as ODT tag. When nil, these tags will be exported as plain text and therefore not be interpreted by a browser. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"." :group 'org-export-odt :type 'boolean) (defun org-odt-do-expand (s) "Apply all active conversions to translate special ASCII to ODT." (setq s (org-odt-protect s)) (if org-export-odt-expand (while (string-match "@<\\([^&]*\\)>" s) (setq s (replace-match "<\\1>" t nil s)))) (if org-export-with-emphasize (setq s (org-export-odt-convert-emphasize s))) ;; FIXME: -- to ndash conversion happens here. Disable it for now. (if (and nil org-export-with-special-strings) (setq s (org-export-odt-convert-special-strings s))) (if org-export-with-sub-superscripts (setq s (org-export-odt-convert-sub-super s))) (if org-export-with-TeX-macros (let ((start 0) wd rep) (while (setq start (string-match "\\\\\\([a-zA-Z]+[0-9]*\\)\\({}\\)?" s start)) (if (get-text-property (match-beginning 0) 'org-protected s) (setq start (match-end 0)) (setq wd (match-string 1 s)) (if (setq rep (org-entity-get-representation wd 'odt)) (setq s (replace-match rep t t s)) (setq start (+ start (length wd)))))))) s) (defun org-export-odt-convert-special-strings (string) "Convert special characters in STRING to ODT." (let ((all org-export-odt-special-string-regexps) e a re rpl start) (while (setq a (pop all)) (setq re (car a) rpl (cdr a) start 0) (while (string-match re string start) (if (get-text-property (match-beginning 0) 'org-protected string) (setq start (match-end 0)) (setq string (replace-match rpl t nil string))))) string)) (defun org-export-odt-convert-sub-super (string) "Convert sub- and superscripts in STRING to ODT." (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{}))) (while (string-match org-match-substring-regexp string s) (cond ((and requireb (match-end 8)) (setq s (match-end 2))) ((get-text-property (match-beginning 2) 'org-protected string) (setq s (match-end 2))) (t (setq s (match-end 1) key (if (string= (match-string 2 string) "_") "sub" "sup") c (or (match-string 8 string) (match-string 6 string) (match-string 5 string)) string (replace-match (concat (match-string 1 string) "<" key ">" c "") t t string))))) (while (string-match "\\\\\\([_^]\\)" string) (setq string (replace-match (match-string 1 string) t t string))) string)) (defun org-export-odt-convert-emphasize (string) "Apply emphasis for opendocument exporting." (let ((s 0) rpl) (while (string-match org-emph-re string s) (if (not (equal (substring string (match-beginning 3) (1+ (match-beginning 3))) (substring string (match-beginning 4) (1+ (match-beginning 4))))) (setq s (match-beginning 0) rpl (concat (match-string 1 string) (nth 1 (assoc (match-string 3 string) org-export-odt-emphasis-alist)) (match-string 4 string) (nth 2 (assoc (match-string 3 string) org-export-odt-emphasis-alist)) (match-string 5 string)) string (replace-match rpl t t string) s (+ s (- (length rpl) 2))) (setq s (1+ s)))) string)) (defun org-odt-do-open-par (&optional style) "Insert

    , but first close previous paragraph if any." (org-odt-close-par-maybe) (org-odt-open-par (or style "Text_20_body")) (setq org-par-open t)) (defun org-odt-close-par-maybe () "Close paragraph if there is one open." (when org-par-open (org-odt-close-par) (setq org-par-open nil))) ;; debugging utilities (defun org-odt-insert-comment (comment) (when t (insert (format "\n\n" comment)))) (defun org-odt-throw-error (msg) (throw 'error msg)) ;; headings (defun org-odt-open-section (name) (let ((style "Sect1")) (insert (format "\n" style name)))) (defun org-odt-close-section () (insert "\n\n")) (defun org-odt-insert-heading (heading outline-level) (insert (format "\n" outline-level outline-level) heading "\n\n")) ;; primitives begin (defun org-odt-open-par (&optional style) (setq style (or style "Text_20_body")) (insert (format "\n\n" style))) (defun org-odt-close-par () (insert "\n\n")) (defun org-odt-open-list (&optional style) (if style (insert (format "\n" style)) (insert ""))) (defun org-odt-close-list () (insert "\n")) (defun org-odt-open-list-item () (insert "\n")) (defun org-odt-close-list-item () (insert "\n")) ;; primitives end ;; compounds begin (defun org-odt-do-open-list (&optional item-type) (let (style-name) (cond ((equal item-type "u") (setq style-name "OrgBulletedList")) ((equal item-type "o") (setq style-name "OrgNumberedList")) ((equal item-type "d") (setq style-name "OrgDescriptionList"))) (org-odt-close-par-maybe) (org-odt-open-list style-name) (org-odt-do-open-list-item))) (defun org-odt-do-open-list-item (&optional style) (org-odt-open-list-item) (org-odt-do-open-par style)) (defun org-odt-do-open-description-list (item-tag) ;; (format "

    \n
    %s
    \n" item-tag) (org-odt-open-list "OrgDescriptionList") (org-odt-do-open-description-list-item item-tag)) (defun org-odt-do-open-description-list-item (item-tag) "" ;; (format "
    %s
    \n" item-tag) (org-odt-do-open-list-item "Text_20_body_20_bold") (insert item-tag) (org-odt-do-close-list-item) (org-odt-open-list-item) (org-odt-do-open-list)) (defun org-odt-do-close-list-item (&optional type) "Close
  • if necessary." ;; (insert (if (equal type "d") "
  • \n" "\n")) (org-odt-close-par-maybe) (org-odt-close-list-item) (when (equal type "d") (org-odt-close-list) (org-odt-close-list-item))) ;; compounds end (defvar in-local-list) (defvar local-list-indent) (defvar local-list-type) (defvar body-only) ; dynamically scoped into this. (defun org-odt-level-start (level title umax with-toc head-count) "Insert a new level in ODT export. When TITLE is nil, just close all open levels." (org-odt-close-par-maybe) (let* ((target (and title (org-get-text-property-any 0 'target title))) (extra-targets (and target (assoc target org-export-target-aliases))) (extra-class (and title (org-get-text-property-any 0 'odt-container-class title))) (preferred (and target (cdr (assoc target org-export-preferred-target-alist)))) (remove (or preferred target)) (l org-level-max) snumber snu href suffix) (setq extra-targets (remove remove extra-targets)) (setq extra-targets (mapconcat (lambda (x) (if (org-uuidgen-p x) (setq x (concat "ID-" x))) (format "" x x)) extra-targets "")) (while (>= l level) (if (aref org-levels-open (1- l)) (progn (org-odt-level-close l umax) (aset org-levels-open (1- l) nil))) (setq l (1- l))) (when title ;; If title is nil, this means this function is called to close ;; all levels, so the rest is done only if title is given (when (string-match (org-re "\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") title) (setq title (replace-match (if org-export-with-tags (save-match-data (concat "   " (mapconcat (lambda (x) (format "%s" (org-export-odt-get-tag-class-name x) x)) (org-split-string (match-string 1 title) ":") " ") "")) "") t t title))) (if (> level umax) (progn (if (aref org-levels-open (1- level)) (progn (org-odt-close-li) (if target (insert (format "
  • " target) extra-targets title "
    \n") (insert "
  • " title "
    \n"))) (aset org-levels-open (1- level) t) (org-odt-close-par-maybe) (if target (insert (format "
      \n
    • " target) extra-targets title "
      \n") (insert "
        \n
      • " title "
        \n")))) (aset org-levels-open (1- level) t) (setq snumber (org-section-number level) snu (replace-regexp-in-string "\\." "_" snumber)) (setq level (+ level org-export-odt-toplevel-hlevel -1)) (if (and org-export-with-section-numbers (not body-only)) (setq title (concat ;; interfering in bookmarks ;; (format "%s" ;; level snumber) " " title))) (unless (= head-count 1) (insert "\n\n")) (setq href (cdr (assoc (concat "sec-" snu) org-export-preferred-target-alist))) (setq suffix (or href snu)) (setq href (or href (concat "sec-" snu))) ;; suffix: 1.2 & href sec-1.2 ;; suffix: aabb & href aabb (insert (format "" "Sect1" suffix)) (org-odt-insert-heading (concat (format "%s" snumber) ;; (format "%s%s" snumber title) (let* ((frag href) (txt (concat (format org-odt-bookmark-tag frag) title))) (if (or org-export-odt-use-bookmarks-for-internal-links org-export-odt-use-bookmarks-for-toc) (setq txt (org-odt-bookmark-text-range frag txt))) txt)) level) (insert (format "" "Sect1" suffix)) (org-odt-do-open-par))))) (defun org-export-odt-get-tag-class-name (tag) "Turn tag into a valid class name. Replaces invalid characters with \"_\" and then prepends a prefix." (save-match-data (while (string-match "[^a-zA-Z0-9_]" tag) (setq tag (replace-match "_" t t tag)))) (concat org-export-odt-tag-class-prefix tag)) (defun org-export-odt-get-todo-kwd-class-name (kwd) "Turn todo keyword into a valid class name. Replaces invalid characters with \"_\" and then prepends a prefix." (save-match-data (while (string-match "[^a-zA-Z0-9_]" kwd) (setq kwd (replace-match "_" t t kwd)))) (concat org-export-odt-todo-kwd-class-prefix kwd)) (defun org-odt-level-close (level max-outline-level) "Terminate one level in ODT export." (if (<= level max-outline-level) (insert "\n") (org-odt-close-li) (insert "
      \n"))) ;; Zip and Unzipping of ODT files (defconst org-export-odt-tmpdir-prefix "odt-") ;; xml files generated on-the-fly (setq org-export-odt-save-list '("META-INF/manifest.xml" "content.xml" "meta.xml")) ;; xml files that are copied (setq org-export-odt-nosave-list '("styles.xml")) ;; xml files that contribute to the final odt file (setq org-export-odt-file-list (append org-export-odt-save-list org-export-odt-nosave-list)) (defun org-export-odt-outdir-init () (interactive) (let* ((outdir (make-temp-file org-export-odt-tmpdir-prefix t)) (content-file (expand-file-name "content.xml" outdir)) (manifest-file (expand-file-name "META-INF/manifest.xml" outdir)) (meta-file (expand-file-name "meta.xml" outdir)) (styles-file (expand-file-name "styles.xml" outdir)) (pictures-dir (expand-file-name "Pictures" outdir))) ;; manifest file (make-directory (file-name-directory manifest-file)) (with-current-buffer (find-file-noselect manifest-file) (erase-buffer) (insert (mapconcat 'identity (car org-export-odt-manifest-lines) "\n")) (insert "\n") (save-excursion (insert (mapconcat 'identity (cdr org-export-odt-manifest-lines) "\n")))) ;; meta file (with-current-buffer (find-file-noselect meta-file) (erase-buffer) (insert (mapconcat 'identity (car org-export-odt-meta-lines) "\n")) (insert "\n") (save-excursion (insert (mapconcat 'identity (cdr org-export-odt-meta-lines) "\n")))) ;; styles file (copy-file org-export-odt-styles-file styles-file t) ;; Pictures dir (make-directory pictures-dir) ;; content file (with-current-buffer (find-file-noselect content-file) (erase-buffer)) outdir)) ;; INLINE IMAGES ;; FIXME: Handle case where the same image is embedded multiple times ;; in the document (defconst org-odt-manifest-file-entry-tag "") (defun org-odt-update-manifest-file (media-type full-path) (with-current-buffer (find-file-noselect "META-INF/manifest.xml" outdir) (insert (format org-odt-manifest-file-entry-tag media-type full-path)))) ;; ;; ;; ;; ;; ;; ;; style-name => fr1 (inherits from Graphics) ;; name => graphics1 (possibly could be skipped) ;; anchor-type => paragraph ;; width => 12.435cm (can this be varied) ;; height => 10.054cm (can this be varied) ;; z-index => 0 (can this be varied) ;; INLINE IMAGES WITH CAPTIONS (SIMPLIFIED) ;; ;; ;; ;; ;; ;; ;; Illustration 1: FigureCaption ;; ;; ;; ;; INLINE IMAGES (TRUE FORM) ;; ;; ;; ;; Illustration 1: FigureCaption ;; ;; (defconst org-odt-frame-tag '("" . "")) (defconst org-odt-image-tag "") (defun org-odt-embed-image (path &optional width height) (with-temp-buffer (let* ((image-type (file-name-extension path)) (media-type (format "image/%s" type)) (full-path (format "Pictures/%s" (file-name-nondirectory path))) (width (or width 12)) (height (or height 10)) ;; in cms (z-index 0)) ;; copy the image to pictures dir ;; current-dir is dyanmically bound (message "current-dir: %s" current-dir) (copy-file (expand-file-name path current-dir) "Pictures" nil) ;; (org-odt-do-open-par "Standard") (insert (format (car org-odt-frame-tag) "Graphics" "graphics1" "paragraph" width height z-index)) (insert (format org-odt-image-tag full-path)) (insert (format (cdr org-odt-frame-tag))) ;; (org-odt-close-par) ;; add it to the save list (push full-path org-export-odt-file-list) (message "file-list: %s" org-export-odt-file-list) ;; update manifest file (org-odt-update-manifest-file media-type full-path)) (buffer-substring-no-properties (point-min) (point-max)))) ;; ODT: ANCHORS and LINKS (defconst org-odt-a-tag " %s ") ;; ODT: BOOKMARKS AND XREFS ;; Either marks a point or a marks a range ;; When it marks a point, it can be referenced using internet-style ;; link that are prefixed with a # ;; When it marks a range, it can be referenced using a OpenOffice ;; style xref (defconst org-odt-bookmark-tag "") (defconst org-odt-bookmark-start-end-tag '("" . "")) (defconst org-odt-bookmark-ref-tag " %s ") (defcustom org-export-odt-use-bookmarks-for-internal-links t "Export Internal links as bookmarks?.") (defconst org-export-odt-bookmark-prefix "__OrgXref_") (defun org-odt-bookmark-text-range (bmk-name text) (setq bmk-name (concat org-export-odt-bookmark-prefix bmk-name)) (concat (format (car org-odt-bookmark-start-end-tag) bmk-name) text (format (cdr org-odt-bookmark-start-end-tag) bmk-name))) (defun org-odt-bookmark-ref-begin (label) (insert (format (car org-odt-bookmark-ref-tag) label))) (defun org-odt-bookmark-ref-end () (insert (format (cdr org-odt-bookmark-ref-tag)))) (defun org-odt-format-internal-link (href desc xref &optional attr) (if xref (format org-odt-bookmark-ref-tag href desc) (format org-odt-a-tag href (or attr "") desc))) ;; PACKAGING (defun org-export-odt-zip (outdir target) ;; save all xml files (mapc (lambda (file) (with-current-buffer (find-file-noselect (expand-file-name file outdir)) ;; prettify output (indent-region (point-min) (point-max)) (save-buffer))) org-export-odt-save-list) (let* ((target-name (file-name-nondirectory target)) (target-dir (file-name-directory target)) (cmd (format "zip -r %s %s" target-name (mapconcat 'identity org-export-odt-file-list " ")))) (when (file-exists-p target) ;; FIXME: If the file is locked this throws an error (delete-file target)) ;; FIXME: check exit status (shell-command cmd nil) ;; move the file from outdir to target-dir (rename-file target-name target-dir) ;; kill all xml buffers (mapc (lambda (file) (kill-buffer (find-file-noselect (expand-file-name file outdir)))) org-export-odt-save-list) ;; delete the temporary directory created for zipping (delete-directory outdir))) ;; FIXME: Rationalize the path for styles file (defcustom org-export-odt-styles-file "~/src/base/org-mode/contrib/odt/styles.xml" "Default style file for use with ODT exporter." :group 'org-export-odt :type 'file) (defconst org-export-odt-manifest-lines '(("" "" "" "" "" "" "") . (""))) (defconst org-export-odt-meta-lines '(("" "" " ") . (" " ""))) (provide 'org-odt) ;; arch-tag: 8109d84d-eb8f-460b-b1a8-f45f3a6c7ea1 ;;; org-odt.el ends here