org-odt.el 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509
  1. ;;; org-odt.el --- OpenDocumentText export for Org-mode
  2. ;; Copyright (C) 2010, 2011
  3. ;; Jambunathan <kjambunathan at gmail dot com>
  4. ;; Author: Jambunathan K <kjambunathan at gmail dot com>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; Homepage: http://orgmode.org
  7. ;; Version: 0.8
  8. ;; This file is not (yet) part of GNU Emacs.
  9. ;; However, it is distributed under the same license.
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  21. ;;
  22. ;;; Commentary:
  23. ;;; Code:
  24. (eval-when-compile (require 'cl))
  25. (require 'org-lparse)
  26. (defun org-odt-end-export ()
  27. ;; remove empty paragraphs
  28. (goto-char (point-min))
  29. (while (re-search-forward
  30. "<text:p\\( text:style-name=\"Text_20_body\"\\)?>[ \r\n\t]*</text:p>"
  31. nil t)
  32. (replace-match ""))
  33. (goto-char (point-min))
  34. ;; Convert whitespace place holders
  35. (goto-char (point-min))
  36. (let (beg end n)
  37. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  38. (setq n (get-text-property beg 'org-whitespace)
  39. end (next-single-property-change beg 'org-whitespace))
  40. (goto-char beg)
  41. (delete-region beg end)
  42. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  43. (make-string n ?x)))))
  44. ;; Remove empty lines at the beginning of the file.
  45. (goto-char (point-min))
  46. (when (looking-at "\\s-+\n") (replace-match ""))
  47. ;; Remove display properties
  48. (remove-text-properties (point-min) (point-max) '(display t)))
  49. (defvar org-odt-suppress-xref nil)
  50. (defconst org-export-odt-special-string-regexps
  51. '(("\\\\-" . "&#x00ad;\\1") ; shy
  52. ("---\\([^-]\\)" . "&#x2014;\\1") ; mdash
  53. ("--\\([^-]\\)" . "&#x2013;\\1") ; ndash
  54. ("\\.\\.\\." . "&#x2026;")) ; hellip
  55. "Regular expressions for special string conversion.")
  56. (defconst org-odt-lib-dir (file-name-directory load-file-name))
  57. (defconst org-odt-data-dir
  58. (let ((dir1 (expand-file-name "../odt" org-odt-lib-dir)) ; git
  59. (dir2 (expand-file-name "./contrib/odt" org-odt-lib-dir))) ; elpa
  60. (cond
  61. ((file-directory-p dir1) dir1)
  62. ((file-directory-p dir2) dir2)
  63. (t (error "Cannot find factory styles file. Check package dir layout")))))
  64. (defvar org-odt-file-extensions
  65. '(("odt" . "OpenDocument Text")
  66. ("ott" . "OpenDocument Text Template")
  67. ("odm" . "OpenDocument Master Document")
  68. ("ods" . "OpenDocument Spreadsheet")
  69. ("ots" . "OpenDocument Spreadsheet Template")
  70. ("odg" . "OpenDocument Drawing (Graphics)")
  71. ("otg" . "OpenDocument Drawing Template")
  72. ("odp" . "OpenDocument Presentation")
  73. ("otp" . "OpenDocument Presentation Template")
  74. ("odi" . "OpenDocument Image")
  75. ("odf" . "OpenDocument Formula")
  76. ("odc" . "OpenDocument Chart")
  77. ("doc" . "Microsoft Text")
  78. ("docx" . "Microsoft Text")
  79. ("xls" . "Microsoft Spreadsheet")
  80. ("xlsx" . "Microsoft Spreadsheet")
  81. ("ppt" . "Microsoft Presentation")
  82. ("pptx" . "Microsoft Presentation")))
  83. (defvar org-odt-ms-file-extensions
  84. '(("doc" . "Microsoft Text")
  85. ("docx" . "Microsoft Text")
  86. ("xls" . "Microsoft Spreadsheet")
  87. ("xlsx" . "Microsoft Spreadsheet")
  88. ("ppt" . "Microsoft Presentation")
  89. ("pptx" . "Microsoft Presentation")))
  90. ;; RelaxNG validation of OpenDocument xml files
  91. (eval-after-load 'rng-nxml
  92. '(setq rng-nxml-auto-validate-flag t))
  93. (eval-after-load 'rng-loc
  94. '(add-to-list 'rng-schema-locating-files
  95. (expand-file-name "etc/schema/schemas.xml" org-odt-data-dir)))
  96. (mapc
  97. (lambda (desc)
  98. ;; Let Org open all OpenDocument files using system-registered app
  99. (add-to-list 'org-file-apps
  100. (cons (concat "\\." (car desc) "\\'") 'system))
  101. ;; Let Emacs open all OpenDocument files in archive mode
  102. (add-to-list 'auto-mode-alist
  103. (cons (concat "\\." (car desc) "\\'") 'archive-mode)))
  104. org-odt-file-extensions)
  105. (mapc
  106. (lambda (desc)
  107. ;; Let Org open all Microsoft files using system-registered app
  108. (add-to-list 'org-file-apps
  109. (cons (concat "\\." (car desc) "\\'") 'system)))
  110. org-odt-ms-file-extensions)
  111. ;; register the odt exporter with the pre-processor
  112. (add-to-list 'org-export-backends 'odt)
  113. ;; register the odt exporter with org-lparse library
  114. (org-lparse-register-backend 'odt)
  115. (defcustom org-export-odt-automatic-styles-file nil
  116. "Default style file for use with ODT exporter."
  117. :type 'file
  118. :group 'org-export-odt)
  119. ;; TODO: Make configuration user-friendly.
  120. (defcustom org-export-odt-styles-file nil
  121. "Default style file for use with ODT exporter.
  122. Valid values are path to an styles.xml file or a path to a valid
  123. *.odt or a *.ott file or a list of the form (FILE (MEMBER1
  124. MEMBER2 ...)). In the last case, the specified FILE is unzipped
  125. and MEMBER1, MEMBER2 etc are copied in to the generated odt
  126. file. The last form is particularly useful if the styles.xml has
  127. reference to additional files like header and footer images.
  128. "
  129. :type 'file
  130. :group 'org-export-odt)
  131. (defconst org-export-odt-tmpdir-prefix "odt-")
  132. (defconst org-export-odt-bookmark-prefix "OrgXref.")
  133. (defcustom org-export-odt-use-bookmarks-for-internal-links t
  134. "Export Internal links as bookmarks?."
  135. :type 'boolean
  136. :group 'org-export-odt)
  137. (defcustom org-export-odt-embed-images t
  138. "Should the images be copied in to the odt file or just linked?"
  139. :type 'boolean
  140. :group 'org-export-odt)
  141. (defcustom org-odt-export-inline-images 'maybe
  142. "Non-nil means inline images into exported HTML pages.
  143. This is done using an <img> tag. When nil, an anchor with href is used to
  144. link to the image. If this option is `maybe', then images in links with
  145. an empty description will be inlined, while images with a description will
  146. be linked only."
  147. :group 'org-odt-export
  148. :type '(choice (const :tag "Never" nil)
  149. (const :tag "Always" t)
  150. (const :tag "When there is no description" maybe)))
  151. (defcustom org-odt-export-inline-image-extensions
  152. '("png" "jpeg" "jpg" "gif")
  153. "Extensions of image files that can be inlined into HTML."
  154. :type '(repeat (string :tag "Extension"))
  155. :group 'org-odt-export)
  156. (defcustom org-export-odt-pixels-per-inch display-pixels-per-inch
  157. ;; FIXME add docstring
  158. ""
  159. :type 'float
  160. :group 'org-export-odt)
  161. (defvar org-export-odt-default-org-styles-alist
  162. '((paragraph . ((default . "Text_20_body")
  163. (fixedwidth . "OrgSourceBlock")
  164. (verse . "OrgVerse")
  165. (quote . "Quotations")
  166. (blockquote . "Quotations")
  167. (center . "OrgCenter")
  168. (left . "OrgLeft")
  169. (right . "OrgRight")
  170. (title . "Heading_20_1.title")
  171. (footnote . "Footnote")
  172. (src . "OrgSourceBlock")
  173. (illustration . "Illustration")
  174. (table . "Table")
  175. (definition-term . "Text_20_body_20_bold")
  176. (horizontal-line . "Horizontal_20_Line")))
  177. (character . ((bold . "Bold")
  178. (emphasis . "Emphasis")
  179. (code . "OrgCode")
  180. (verbatim . "OrgCode")
  181. (strike . "Strikethrough")
  182. (underline . "Underline")
  183. (subscript . "OrgSubscript")
  184. (superscript . "OrgSuperscript")))
  185. (list . ((ordered . "OrgNumberedList")
  186. (unordered . "OrgBulletedList")
  187. (description . "OrgDescriptionList"))))
  188. "Default styles for various entities.")
  189. (defvar org-export-odt-org-styles-alist org-export-odt-default-org-styles-alist)
  190. (defun org-odt-get-style-name-for-entity (category &optional entity)
  191. (let ((entity (or entity 'default)))
  192. (or
  193. (cdr (assoc entity (cdr (assoc category
  194. org-export-odt-org-styles-alist))))
  195. (cdr (assoc entity (cdr (assoc category
  196. org-export-odt-default-org-styles-alist))))
  197. (error "Cannot determine style name for entity %s of type %s"
  198. entity category))))
  199. ;;;###autoload
  200. (defun org-export-as-odt-and-open (arg)
  201. "Export the outline as ODT and immediately open it with a browser.
  202. If there is an active region, export only the region.
  203. The prefix ARG specifies how many levels of the outline should become
  204. headlines. The default is 3. Lower levels will become bulleted lists."
  205. (interactive "P")
  206. (org-lparse-and-open "odt" "odt" arg))
  207. ;;;###autoload
  208. (defun org-export-as-odt-batch ()
  209. "Call the function `org-lparse-batch'.
  210. This function can be used in batch processing as:
  211. emacs --batch
  212. --load=$HOME/lib/emacs/org.el
  213. --eval \"(setq org-export-headline-levels 2)\"
  214. --visit=MyFile --funcall org-export-as-odt-batch"
  215. (org-lparse-batch "odt"))
  216. ;;;###autoload
  217. (defun org-export-as-odt-to-buffer (arg)
  218. "Call `org-lparse-odt` with output to a temporary buffer.
  219. No file is created. The prefix ARG is passed through to `org-lparse-to-buffer'."
  220. (interactive "P")
  221. (org-lparse-to-buffer "odt" arg))
  222. ;;;###autoload
  223. (defun org-replace-region-by-odt (beg end)
  224. "Assume the current region has org-mode syntax, and convert it to ODT.
  225. This can be used in any buffer. For example, you could write an
  226. itemized list in org-mode syntax in an ODT buffer and then use this
  227. command to convert it."
  228. (interactive "r")
  229. (org-replace-region-by "odt" beg end))
  230. ;;;###autoload
  231. (defun org-export-region-as-odt (beg end &optional body-only buffer)
  232. "Convert region from BEG to END in org-mode buffer to ODT.
  233. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  234. contents, and only produce the region of converted text, useful for
  235. cut-and-paste operations.
  236. If BUFFER is a buffer or a string, use/create that buffer as a target
  237. of the converted ODT. If BUFFER is the symbol `string', return the
  238. produced ODT as a string and leave not buffer behind. For example,
  239. a Lisp program could call this function in the following way:
  240. (setq odt (org-export-region-as-odt beg end t 'string))
  241. When called interactively, the output buffer is selected, and shown
  242. in a window. A non-interactive call will only return the buffer."
  243. (interactive "r\nP")
  244. (org-lparse-region "odt" beg end body-only buffer))
  245. ;;; org-export-as-odt
  246. ;;;###autoload
  247. (defun org-export-as-odt (arg &optional hidden ext-plist
  248. to-buffer body-only pub-dir)
  249. "Export the outline as a OpenDocumentText file.
  250. If there is an active region, export only the region. The prefix
  251. ARG specifies how many levels of the outline should become
  252. headlines. The default is 3. Lower levels will become bulleted
  253. lists. HIDDEN is obsolete and does nothing.
  254. EXT-PLIST is a property list with external parameters overriding
  255. org-mode's default settings, but still inferior to file-local
  256. settings. When TO-BUFFER is non-nil, create a buffer with that
  257. name and export to that buffer. If TO-BUFFER is the symbol
  258. `string', don't leave any buffer behind but just return the
  259. resulting XML as a string. When BODY-ONLY is set, don't produce
  260. the file header and footer, simply return the content of
  261. <body>...</body>, without even the body tags themselves. When
  262. PUB-DIR is set, use this as the publishing directory."
  263. (interactive "P")
  264. (org-lparse "odt" "odt" arg hidden ext-plist to-buffer body-only pub-dir))
  265. (defvar org-odt-entity-control-callbacks-alist
  266. `((EXPORT
  267. . (org-odt-begin-export org-odt-end-export))
  268. (DOCUMENT-CONTENT
  269. . (org-odt-begin-document-content org-odt-end-document-content))
  270. (DOCUMENT-BODY
  271. . (org-odt-begin-document-body org-odt-end-document-body))
  272. (TOC
  273. . (org-odt-begin-toc org-odt-end-toc))
  274. (ENVIRONMENT
  275. . (org-odt-begin-environment org-odt-end-environment))
  276. (FOOTNOTE-DEFINITION
  277. . (org-odt-begin-footnote-definition org-odt-end-footnote-definition))
  278. (TABLE
  279. . (org-odt-begin-table org-odt-end-table))
  280. (TABLE-ROWGROUP
  281. . (org-odt-begin-table-rowgroup org-odt-end-table-rowgroup))
  282. (LIST
  283. . (org-odt-begin-list org-odt-end-list))
  284. (LIST-ITEM
  285. . (org-odt-begin-list-item org-odt-end-list-item))
  286. (OUTLINE
  287. . (org-odt-begin-outline org-odt-end-outline))
  288. (OUTLINE-TEXT
  289. . (org-odt-begin-outline-text org-odt-end-outline-text))
  290. (PARAGRAPH
  291. . (org-odt-begin-paragraph org-odt-end-paragraph)))
  292. "")
  293. (defvar org-odt-entity-format-callbacks-alist
  294. `((EXTRA-TARGETS . org-lparse-format-extra-targets)
  295. (ORG-TAGS . org-lparse-format-org-tags)
  296. (SECTION-NUMBER . org-lparse-format-section-number)
  297. (HEADLINE . org-odt-format-headline)
  298. (TOC-ENTRY . org-odt-format-toc-entry)
  299. (TOC-ITEM . org-odt-format-toc-item)
  300. (TAGS . org-odt-format-tags)
  301. (SPACES . org-odt-format-spaces)
  302. (TABS . org-odt-format-tabs)
  303. (LINE-BREAK . org-odt-format-line-break)
  304. (FONTIFY . org-odt-format-fontify)
  305. (TODO . org-lparse-format-todo)
  306. (LINK . org-odt-format-link)
  307. (INLINE-IMAGE . org-odt-format-inline-image)
  308. (ORG-LINK . org-odt-format-org-link)
  309. (HEADING . org-odt-format-heading)
  310. (ANCHOR . org-odt-format-anchor)
  311. (TABLE . org-lparse-format-table)
  312. (TABLE-ROW . org-odt-format-table-row)
  313. (TABLE-CELL . org-odt-format-table-cell)
  314. (FOOTNOTES-SECTION . ignore)
  315. (FOOTNOTE-REFERENCE . org-odt-format-footnote-reference)
  316. (HORIZONTAL-LINE . org-odt-format-horizontal-line)
  317. (COMMENT . org-odt-format-comment)
  318. (LINE . org-odt-format-line)
  319. (ORG-ENTITY . org-odt-format-org-entity))
  320. "")
  321. ;;;_. callbacks
  322. ;;;_. control callbacks
  323. ;;;_ , document body
  324. (defun org-odt-begin-office-body ()
  325. (insert "
  326. <office:body>
  327. <office:text>
  328. <text:sequence-decls>
  329. <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Illustration\"/>
  330. <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Table\"/>
  331. <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Text\"/>
  332. <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Drawing\"/>
  333. </text:sequence-decls>"))
  334. ;; Following variable is let bound when `org-do-lparse' is in
  335. ;; progress. See org-html.el.
  336. (defvar org-lparse-toc)
  337. (defun org-odt-begin-document-body (opt-plist)
  338. (org-odt-begin-office-body)
  339. (let ((title (plist-get opt-plist :title)))
  340. (when title
  341. (insert
  342. (org-odt-format-stylized-paragraph 'title title))))
  343. ;; insert toc
  344. (when org-lparse-toc
  345. (insert "\n" org-lparse-toc "\n")))
  346. (defvar org-lparse-body-only) ; let bound during org-do-lparse
  347. (defvar org-lparse-to-buffer) ; let bound during org-do-lparse
  348. (defun org-odt-end-document-body (opt-plist)
  349. (unless org-lparse-body-only
  350. (org-lparse-insert-tag "</office:text>")
  351. (org-lparse-insert-tag "</office:body>")))
  352. (defconst org-odt-document-content-header
  353. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  354. <office:document-content
  355. xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
  356. xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\"
  357. xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\"
  358. xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\"
  359. xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\"
  360. xmlns:fo=\"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0\"
  361. xmlns:xlink=\"http://www.w3.org/1999/xlink\"
  362. xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
  363. xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
  364. xmlns:number=\"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0\"
  365. xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\"
  366. xmlns:chart=\"urn:oasis:names:tc:opendocument:xmlns:chart:1.0\"
  367. xmlns:dr3d=\"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0\"
  368. xmlns:math=\"http://www.w3.org/1998/Math/MathML\"
  369. xmlns:form=\"urn:oasis:names:tc:opendocument:xmlns:form:1.0\"
  370. xmlns:script=\"urn:oasis:names:tc:opendocument:xmlns:script:1.0\"
  371. xmlns:ooo=\"http://openoffice.org/2004/office\"
  372. xmlns:ooow=\"http://openoffice.org/2004/writer\"
  373. xmlns:oooc=\"http://openoffice.org/2004/calc\"
  374. xmlns:dom=\"http://www.w3.org/2001/xml-events\"
  375. xmlns:xforms=\"http://www.w3.org/2002/xforms\"
  376. xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
  377. xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
  378. xmlns:rpt=\"http://openoffice.org/2005/report\"
  379. xmlns:of=\"urn:oasis:names:tc:opendocument:xmlns:of:1.2\"
  380. xmlns:xodt=\"http://www.w3.org/1999/xodt\"
  381. xmlns:field=\"urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0\" office:version=\"1.2\">
  382. ")
  383. (defun org-odt-begin-document-content (opt-plist)
  384. ;; document header
  385. (insert org-odt-document-content-header)
  386. ;; automatic styles
  387. (insert-file-contents
  388. (or org-export-odt-automatic-styles-file
  389. (expand-file-name "styles/OrgOdtAutomaticStyles.xml"
  390. org-odt-data-dir)))
  391. (goto-char (point-max)))
  392. (defun org-odt-end-document-content ()
  393. (org-lparse-insert-tag "</office:document-content>"))
  394. (defun org-odt-begin-outline (level1 snumber title tags
  395. target extra-targets class)
  396. (org-lparse-insert
  397. 'HEADING (org-lparse-format
  398. 'HEADLINE title extra-targets tags snumber level1)
  399. level1 target))
  400. (defun org-odt-end-outline ()
  401. (ignore))
  402. (defun org-odt-begin-outline-text (level1 snumber class)
  403. (ignore))
  404. (defun org-odt-end-outline-text ()
  405. (ignore))
  406. (defun org-odt-begin-paragraph (&optional style)
  407. (org-lparse-insert-tag
  408. "<text:p%s>" (org-odt-get-extra-attrs-for-paragraph-style style)))
  409. (defun org-odt-end-paragraph ()
  410. (org-lparse-insert-tag "</text:p>"))
  411. (defun org-odt-get-extra-attrs-for-paragraph-style (style)
  412. (let (style-name)
  413. (setq style-name
  414. (cond
  415. ((stringp style) style)
  416. ((symbolp style) (org-odt-get-style-name-for-entity
  417. 'paragraph style))))
  418. (unless style-name
  419. (error "Don't know how to handle paragraph style %s" style))
  420. (format " text:style-name=\"%s\"" style-name)))
  421. (defun org-odt-format-stylized-paragraph (style text)
  422. (org-odt-format-tags
  423. '("<text:p%s>" . "</text:p>") text
  424. (org-odt-get-extra-attrs-for-paragraph-style style)))
  425. (defun org-odt-begin-environment (style)
  426. (case style
  427. ((blockquote verse center quote)
  428. (org-lparse-begin-paragraph style)
  429. (list))
  430. ((fixedwidth native)
  431. (org-lparse-end-paragraph)
  432. (list))
  433. (t (error "Unknown environment %s" style))))
  434. (defun org-odt-end-environment (style)
  435. (case style
  436. ((blockquote verse center quote)
  437. (org-lparse-end-paragraph)
  438. (list))
  439. ((fixedwidth native)
  440. (org-lparse-begin-paragraph)
  441. (list))
  442. (t (error "Unknown environment %s" style))))
  443. (defun org-odt-begin-list (ltype &optional arg1)
  444. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  445. ltype))
  446. (let* ((style-name (org-odt-get-style-name-for-entity 'list ltype))
  447. (extra (if style-name
  448. (format " text:style-name=\"%s\"" style-name) "")))
  449. ;; FIXME: Handle arg1 incase of ordered lists.
  450. (case ltype
  451. ((ordered unordered description)
  452. (org-lparse-end-paragraph)
  453. (org-lparse-insert-tag "<text:list%s>" extra))
  454. (t (error "Unknown list type: %s" ltype)))))
  455. (defun org-odt-end-list (ltype)
  456. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  457. ltype))
  458. (if ltype
  459. (org-lparse-insert-tag "</text:list>")
  460. (error "Unknown list type: %s" ltype)))
  461. (defun org-odt-begin-list-item (ltype &optional arg headline)
  462. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  463. ltype))
  464. (case ltype
  465. (ordered
  466. (assert (not headline) t)
  467. (let* ((counter arg) (extra ""))
  468. (org-lparse-insert-tag "<text:list-item>")
  469. (org-lparse-begin-paragraph)))
  470. (unordered
  471. (let* ((id arg) (extra ""))
  472. (org-lparse-insert-tag "<text:list-item>")
  473. (org-lparse-begin-paragraph)
  474. (insert (if headline (org-odt-format-target headline id)
  475. (org-odt-format-bookmark "" id)))))
  476. (description
  477. (assert (not headline) t)
  478. (let ((term (or arg "(no term)")))
  479. (insert
  480. (org-odt-format-tags
  481. '("<text:list-item>" . "</text:list-item>")
  482. (org-odt-format-stylized-paragraph 'definition-term term)))
  483. (org-lparse-begin 'LIST-ITEM 'unordered)
  484. (org-lparse-begin 'LIST 'description)
  485. (org-lparse-begin 'LIST-ITEM 'unordered)))
  486. (t (error "Unknown list type"))))
  487. (defun org-odt-end-list-item (ltype)
  488. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  489. ltype))
  490. (case ltype
  491. ((ordered unordered)
  492. (org-lparse-insert-tag "</text:list-item>"))
  493. (description
  494. (org-lparse-end-list-item)
  495. (org-lparse-end 'LIST 'description)
  496. (org-lparse-end-list-item))
  497. (t (error "Unknown list type"))))
  498. ;; Following variables are let bound when table emission is in
  499. ;; progress. See org-lparse.el.
  500. (defvar org-lparse-table-begin-marker)
  501. (defvar org-lparse-table-ncols)
  502. (defvar org-lparse-table-rowgrp-open)
  503. (defvar org-lparse-table-rownum)
  504. (defvar org-lparse-table-cur-rowgrp-is-hdr)
  505. (defvar org-lparse-table-is-styled)
  506. (defvar org-lparse-table-rowgrp-info)
  507. (defvar org-lparse-table-colalign-vector)
  508. (defun org-odt-begin-table (caption label attributes)
  509. (when label
  510. (insert
  511. (org-odt-format-stylized-paragraph
  512. 'table (org-odt-format-entity-caption label caption "Table"))))
  513. (org-lparse-insert-tag
  514. "<table:table table:name=\"%s\" table:style-name=\"%s\">"
  515. (or label "") "OrgTable")
  516. (setq org-lparse-table-begin-marker (point)))
  517. (defun org-odt-end-table ()
  518. (goto-char org-lparse-table-begin-marker)
  519. (loop for level from 0 below org-lparse-table-ncols
  520. do (insert (org-odt-format-tags "<table:table-column/>" "")))
  521. ;; fill style attributes for table cells
  522. (when org-lparse-table-is-styled
  523. (while (re-search-forward "@@\\(table-cell:p\\|table-cell:style-name\\)@@\\([0-9]+\\)@@\\([0-9]+\\)@@" nil t)
  524. (let ((spec (match-string 1))
  525. (r (string-to-number (match-string 2)))
  526. (c (string-to-number (match-string 3))))
  527. (cond
  528. ((equal spec "table-cell:p")
  529. (let ((style-name (org-odt-get-paragraph-style-for-table-cell r c)))
  530. (replace-match style-name t t)))
  531. ((equal spec "table-cell:style-name")
  532. (let ((style-name (org-odt-get-style-name-for-table-cell r c)))
  533. (replace-match style-name t t)))))))
  534. (goto-char (point-max))
  535. (org-lparse-insert-tag "</table:table>"))
  536. (defun org-odt-begin-table-rowgroup (&optional is-header-row)
  537. (when org-lparse-table-rowgrp-open
  538. (org-lparse-end 'TABLE-ROWGROUP))
  539. (org-lparse-insert-tag (if is-header-row
  540. "<table:table-header-rows>"
  541. "<table:table-rows>"))
  542. (setq org-lparse-table-rowgrp-open t)
  543. (setq org-lparse-table-cur-rowgrp-is-hdr is-header-row))
  544. (defun org-odt-end-table-rowgroup ()
  545. (when org-lparse-table-rowgrp-open
  546. (setq org-lparse-table-rowgrp-open nil)
  547. (org-lparse-insert-tag
  548. (if org-lparse-table-cur-rowgrp-is-hdr
  549. "</table:table-header-rows>" "</table:table-rows>"))))
  550. (defun org-odt-format-table-row (row)
  551. (org-odt-format-tags
  552. '("<table:table-row>" . "</table:table-row>") row))
  553. (defun org-odt-get-style-name-for-table-cell (r c)
  554. (concat
  555. "OrgTblCell"
  556. (cond
  557. ((= r 0) "T")
  558. ((eq (cdr (assoc r org-lparse-table-rowgrp-info)) :start) "T")
  559. (t ""))
  560. (when (= r org-lparse-table-rownum) "B")
  561. (cond
  562. ((= c 0) "")
  563. ((or (memq (nth c org-table-colgroup-info) '(:start :startend))
  564. (memq (nth (1- c) org-table-colgroup-info) '(:end :startend))) "L")
  565. (t ""))))
  566. (defun org-odt-get-paragraph-style-for-table-cell (r c)
  567. (capitalize (aref org-lparse-table-colalign-vector c)))
  568. (defun org-odt-format-table-cell (data r c)
  569. (if (not org-lparse-table-is-styled)
  570. (org-odt-format-tags
  571. '("<table:table-cell>" . "</table:table-cell>")
  572. (org-odt-format-stylized-paragraph
  573. (cond
  574. (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
  575. ((and (= c 0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS))
  576. "OrgTableHeading")
  577. (t "OrgTableContents"))
  578. data))
  579. (let* ((style-name-cookie
  580. (format "@@table-cell:style-name@@%03d@@%03d@@" r c))
  581. (paragraph-style-cookie
  582. (concat
  583. (cond
  584. (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
  585. ((and (= c 0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS))
  586. "OrgTableHeading")
  587. (t "OrgTableContents"))
  588. (format "@@table-cell:p@@%03d@@%03d@@" r c))))
  589. (org-odt-format-tags
  590. '("<table:table-cell table:style-name=\"%s\">" .
  591. "</table:table-cell>")
  592. (org-odt-format-stylized-paragraph paragraph-style-cookie data)
  593. style-name-cookie))))
  594. (defun org-odt-begin-footnote-definition (n)
  595. (org-lparse-begin-paragraph 'footnote))
  596. (defun org-odt-end-footnote-definition (n)
  597. (org-lparse-end-paragraph))
  598. (defun org-odt-begin-toc (lang-specific-heading)
  599. (insert
  600. (format "
  601. <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
  602. <text:table-of-content-source text:outline-level=\"10\">
  603. <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
  604. " lang-specific-heading))
  605. (loop for level from 1 upto 10
  606. do (insert (format
  607. "
  608. <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
  609. <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
  610. <text:index-entry-chapter/>
  611. <text:index-entry-text/>
  612. <text:index-entry-link-end/>
  613. </text:table-of-content-entry-template>
  614. " level level)))
  615. (insert "
  616. </text:table-of-content-source>
  617. <text:index-body>
  618. <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
  619. <text:p text:style-name=\"Contents_20_Heading\">Table of Contents</text:p>
  620. </text:index-title>
  621. "))
  622. (defun org-odt-end-toc ()
  623. (insert "
  624. </text:index-body>
  625. </text:table-of-content>
  626. "))
  627. (defun org-odt-format-toc-entry (snumber todo headline tags href)
  628. (setq headline (concat
  629. (and org-export-with-section-numbers
  630. (concat snumber ". "))
  631. headline
  632. (and tags
  633. (concat
  634. (org-lparse-format 'SPACES 3)
  635. (org-lparse-format 'FONTIFY tags "tag")))))
  636. (when todo
  637. (setq headline (org-lparse-format 'FONTIFY headline "todo")))
  638. (let ((org-odt-suppress-xref t))
  639. (org-odt-format-link headline (concat "#" href))))
  640. (defun org-odt-format-toc-item (toc-entry level org-last-level)
  641. (let ((style (format "Contents_20_%d"
  642. (+ level (or (org-lparse-get 'TOPLEVEL-HLEVEL) 1) -1))))
  643. (insert "\n" (org-odt-format-stylized-paragraph style toc-entry) "\n")))
  644. ;; Following variable is let bound during 'ORG-LINK callback. See
  645. ;; org-html.el
  646. (defvar org-lparse-link-description-is-image nil)
  647. (defun org-odt-format-link (desc href &optional attr)
  648. (cond
  649. ((and (= (string-to-char href) ?#) (not org-odt-suppress-xref))
  650. (setq href (concat org-export-odt-bookmark-prefix (substring href 1)))
  651. (org-odt-format-tags
  652. '("<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"%s\">" .
  653. "</text:bookmark-ref>")
  654. desc href))
  655. (org-lparse-link-description-is-image
  656. (org-odt-format-tags
  657. '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</draw:a>")
  658. desc href (or attr "")))
  659. (t
  660. (org-odt-format-tags
  661. '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</text:a>")
  662. desc href (or attr "")))))
  663. (defun org-odt-format-spaces (n)
  664. (org-odt-format-tags "<text:s text:c=\"%d\"/>" "" n))
  665. (defun org-odt-format-tabs (&optional n)
  666. (let ((tab "<text:tab/>")
  667. (n (or n 1)))
  668. (insert tab)))
  669. (defun org-odt-format-line-break ()
  670. (org-odt-format-tags "<text:line-break/>" ""))
  671. (defun org-odt-format-horizontal-line ()
  672. (org-odt-format-stylized-paragraph 'horizontal-line ""))
  673. (defun org-odt-format-line (line)
  674. (case org-lparse-dyn-current-environment
  675. (fixedwidth (concat (org-odt-format-source-code-or-example-line
  676. (org-xml-encode-plain-text line)) "\n"))
  677. (t (concat line "\n"))))
  678. (defun org-odt-format-comment (fmt &rest args)
  679. (let ((comment (apply 'format fmt args)))
  680. (format "\n<!-- %s -->\n" comment)))
  681. (defun org-odt-format-org-entity (wd)
  682. ;; FIXME: Seems to work. But is this correct?
  683. (let ((s (org-entity-get-representation wd 'utf8)))
  684. (and s (format "&#x%x;" (string-to-char s)))))
  685. (defun org-odt-fill-tabs-and-spaces (line)
  686. (replace-regexp-in-string
  687. "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
  688. (cond
  689. ((string= s "\t") (org-odt-format-tabs))
  690. ((> (length s) 1)
  691. (org-odt-format-spaces (length s)))
  692. (t " "))) line))
  693. (defun org-odt-format-source-code-or-example-line (line)
  694. (org-odt-format-stylized-paragraph 'src (org-odt-fill-tabs-and-spaces line)))
  695. (defun org-odt-format-example (lines)
  696. (mapconcat
  697. (lambda (line)
  698. (org-odt-format-source-code-or-example-line line))
  699. (org-split-string lines "[\r\n]") "\n"))
  700. (defun org-odt-format-source-code-or-example (lines lang caption textareap
  701. cols rows num cont
  702. rpllbl fmt)
  703. (org-odt-format-example (org-export-number-lines
  704. (org-xml-encode-plain-text-lines lines)
  705. 0 0 num cont rpllbl fmt)))
  706. (defun org-xml-encode-plain-text-lines (rtn)
  707. (mapconcat 'org-xml-encode-plain-text (org-split-string rtn "[\r\n]") "\n"))
  708. (defun org-odt-remap-stylenames (style-name)
  709. (or
  710. (cdr (assoc style-name '(("timestamp-wrapper" . "OrgTimestampWrapper")
  711. ("timestamp" . "OrgTimestamp")
  712. ("timestamp-kwd" . "OrgTimestampKeyword")
  713. ("tag" . "OrgTag")
  714. ("todo" . "OrgTodo")
  715. ("done" . "OrgDone")
  716. ("target" . "OrgTarget"))))
  717. style-name))
  718. (defun org-odt-format-fontify (text style &optional id)
  719. (let* ((style-name
  720. (cond
  721. ((stringp style)
  722. (org-odt-remap-stylenames style))
  723. ((symbolp style)
  724. (org-odt-get-style-name-for-entity 'character style))
  725. ((listp style)
  726. (assert (< 1 (length style)))
  727. (let ((parent-style (pop style)))
  728. (mapconcat (lambda (s)
  729. ;; (assert (stringp s) t)
  730. (org-odt-remap-stylenames s)) style "")
  731. (org-odt-remap-stylenames parent-style)))
  732. (t (error "Don't how to handle style %s" style)))))
  733. (org-odt-format-tags
  734. '("<text:span text:style-name=\"%s\">" . "</text:span>")
  735. text style-name)))
  736. (defun org-odt-relocate-relative-path (path dir)
  737. (if (file-name-absolute-p path) path
  738. (file-relative-name (expand-file-name path dir)
  739. (expand-file-name "eyecandy" dir))))
  740. (defun org-odt-format-inline-image (thefile)
  741. (let* ((thelink (if (file-name-absolute-p thefile) thefile
  742. (org-xml-format-href
  743. (org-odt-relocate-relative-path
  744. thefile org-current-export-file))))
  745. (href
  746. (org-odt-format-tags
  747. "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
  748. (if org-export-odt-embed-images
  749. (org-odt-copy-image-file thefile) thelink))))
  750. (org-export-odt-format-image thefile href)))
  751. (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
  752. descp)
  753. "Make an HTML link.
  754. OPT-PLIST is an options list.
  755. TYPE is the device-type of the link (THIS://foo.html)
  756. PATH is the path of the link (http://THIS#locationx)
  757. FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
  758. DESC is the link description, if any.
  759. ATTR is a string of other attributes of the a element.
  760. MAY-INLINE-P allows inlining it as an image."
  761. (declare (special org-lparse-par-open))
  762. (save-match-data
  763. (let* ((may-inline-p
  764. (and (member type-1 '("http" "https" "file"))
  765. (org-lparse-should-inline-p path descp)
  766. (not fragment)))
  767. (type (if (equal type-1 "id") "file" type-1))
  768. (filename path)
  769. (thefile path))
  770. (cond
  771. ;; check for inlined images
  772. ((and (member type '("file"))
  773. (not fragment)
  774. (org-file-image-p
  775. filename org-odt-export-inline-image-extensions)
  776. (or (eq t org-odt-export-inline-images)
  777. (and org-odt-export-inline-images (not descp))))
  778. ;; (when (and (string= type "file") (file-name-absolute-p path))
  779. ;; (setq thefile (concat "file://" (expand-file-name path))))
  780. ;; (setq thefile (org-xml-format-href thefile))
  781. ;; (org-export-html-format-image thefile)
  782. (org-odt-format-inline-image thefile))
  783. (t
  784. (when (string= type "file")
  785. (setq thefile
  786. (cond
  787. ((file-name-absolute-p path)
  788. (concat "file://" (expand-file-name path)))
  789. (t (org-odt-relocate-relative-path
  790. thefile org-current-export-file)))))
  791. (when (and (member type '("" "http" "https" "file" "coderef"))
  792. fragment)
  793. (setq thefile (concat thefile "#" fragment)))
  794. (setq thefile (org-xml-format-href thefile))
  795. (when (not (member type '("" "file" "coderef")))
  796. (setq thefile (concat type ":" thefile)))
  797. (let ((org-odt-suppress-xref (string= type "coderef")))
  798. (org-odt-format-link
  799. (org-xml-format-desc desc) thefile attr)))))))
  800. (defun org-odt-format-heading (text level &optional id)
  801. (let* ((text (if id (org-odt-format-target text id) text)))
  802. (org-odt-format-tags
  803. '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
  804. "</text:h>") text level level)))
  805. (defun org-odt-format-headline (title extra-targets tags
  806. &optional snumber level)
  807. (concat
  808. (org-lparse-format 'EXTRA-TARGETS extra-targets)
  809. ;; No need to generate section numbers. They are auto-generated by
  810. ;; the application
  811. ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
  812. title
  813. (and tags (concat (org-lparse-format 'SPACES 3)
  814. (org-lparse-format 'ORG-TAGS tags)))))
  815. (defun org-odt-format-anchor (text name &optional class)
  816. (org-odt-format-target text name))
  817. (defun org-odt-format-bookmark (text id)
  818. (if id
  819. (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id)
  820. text))
  821. (defun org-odt-format-target (text id)
  822. (let ((name (concat org-export-odt-bookmark-prefix id)))
  823. (concat
  824. (and id (org-odt-format-tags
  825. "<text:bookmark-start text:name=\"%s\"/>" "" name))
  826. (org-odt-format-bookmark text id)
  827. (and id (org-odt-format-tags
  828. "<text:bookmark-end text:name=\"%s\"/>" "" name)))))
  829. (defun org-odt-format-footnote (n def)
  830. (let ((id (concat "fn" n))
  831. (note-class "footnote")
  832. (par-style "Footnote"))
  833. (org-odt-format-tags
  834. '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
  835. "</text:note>")
  836. (concat
  837. (org-odt-format-tags
  838. '("<text:note-citation>" . "</text:note-citation>")
  839. n)
  840. (org-odt-format-tags
  841. '("<text:note-body>" . "</text:note-body>")
  842. def))
  843. id note-class)))
  844. (defun org-odt-format-footnote-reference (n def refcnt)
  845. (if (= refcnt 1)
  846. (org-odt-format-footnote n def)
  847. (org-odt-format-footnote-ref n)))
  848. (defun org-odt-format-footnote-ref (n)
  849. (let ((note-class "footnote")
  850. (ref-format "text")
  851. (ref-name (concat "fn" n)))
  852. (org-odt-format-tags
  853. '("<text:span text:style-name=\"%s\">" . "</text:span>")
  854. (org-odt-format-tags
  855. '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" . "</text:note-ref>")
  856. n note-class ref-format ref-name)
  857. "OrgSuperscript")))
  858. (defun org-odt-get-image-name (file-name)
  859. (require 'sha1)
  860. (file-relative-name
  861. (expand-file-name
  862. (concat (sha1 file-name) "." (file-name-extension file-name)) "Pictures")))
  863. (defun org-export-odt-format-image (src href
  864. ;; par-open
  865. )
  866. "Create image tag with source and attributes."
  867. (save-match-data
  868. (let (embed-as caption attr label attr-plist size width height)
  869. (cond
  870. ((string-match "^ltxpng/" src)
  871. ;; FIXME: Anyway the latex src can be embedded as an
  872. ;; annotation
  873. ;; (org-find-text-property-in-string 'org-latex-src src)
  874. (setq caption nil attr nil label nil embed-as 'character))
  875. (t
  876. (setq caption (org-find-text-property-in-string 'org-caption src)
  877. caption (and caption (org-xml-format-desc caption))
  878. attr (org-find-text-property-in-string 'org-attributes src)
  879. label (org-find-text-property-in-string 'org-label src)
  880. embed-as 'paragraph)))
  881. (setq attr-plist (when attr (read attr)))
  882. (setq size (org-odt-image-size-from-file
  883. src (plist-get attr-plist :width)
  884. (plist-get attr-plist :height)
  885. (plist-get attr-plist :scale) nil embed-as))
  886. (org-export-odt-do-format-image embed-as caption attr label
  887. size href))))
  888. (defun org-export-odt-do-format-image (embed-as caption attr label
  889. size href)
  890. "Create image tag with source and attributes."
  891. (save-match-data
  892. (let ((width (car size)) (height (cdr size))
  893. (draw-frame-pair
  894. '("<draw:frame draw:style-name=\"%s\"
  895. text:anchor-type=\"%s\"
  896. draw:z-index=\"%d\" %s>" . "</draw:frame>")))
  897. (cond
  898. ((and (not caption) (not label))
  899. (let (style-name anchor-type)
  900. (cond
  901. ((eq embed-as 'paragraph)
  902. (setq style-name "OrgGraphicsParagraph" anchor-type "paragraph"))
  903. ((eq embed-as 'character)
  904. (setq style-name "OrgGraphicsBaseline" anchor-type "as-char")))
  905. (org-odt-format-tags
  906. draw-frame-pair href style-name anchor-type 0
  907. (org-odt-image-attrs-from-size width height))))
  908. (t
  909. (concat
  910. ;; (when par-open (org-odt-close-par))
  911. (org-odt-format-tags
  912. draw-frame-pair
  913. (org-odt-format-tags
  914. '("<draw:text-box fo:min-height=\"%dcm\">" . "</draw:text-box>")
  915. (org-odt-format-stylized-paragraph
  916. 'illustration
  917. (concat
  918. (let ((extra " style:rel-width=\"100%\" style:rel-height=\"scale\""))
  919. (org-odt-format-tags
  920. draw-frame-pair href "OrgGraphicsParagraphContent" "paragraph" 2
  921. (concat (org-odt-image-attrs-from-size width height) extra)))
  922. (org-odt-format-entity-caption label caption)))
  923. height)
  924. "OrgFrame" "paragraph" 1
  925. (org-odt-image-attrs-from-size width))
  926. ;; (when par-open (org-odt-open-par))
  927. ))))))
  928. ;; xml files generated on-the-fly
  929. (defconst org-export-odt-save-list
  930. '("META-INF/manifest.xml" "content.xml" "meta.xml" "styles.xml"))
  931. ;; xml files that are copied
  932. (defconst org-export-odt-nosave-list '())
  933. ;; xml files that contribute to the final odt file
  934. (defvar org-export-odt-file-list nil)
  935. (defconst org-export-odt-manifest-lines
  936. '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  937. "<manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">"
  938. "<manifest:file-entry manifest:media-type=\"application/vnd.oasis.opendocument.text\" manifest:version=\"1.2\" manifest:full-path=\"/\"/>"
  939. "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"content.xml\"/>"
  940. "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"styles.xml\"/>"
  941. "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"meta.xml\"/>"
  942. "<manifest:file-entry manifest:media-type=\"\" manifest:full-path=\"Pictures/\"/>") . ("</manifest:manifest>")))
  943. (defconst org-export-odt-meta-lines
  944. '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  945. "<office:document-meta"
  946. " xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\""
  947. " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
  948. " xmlns:dc=\"http://purl.org/dc/elements/1.1/\""
  949. " xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\""
  950. " xmlns:ooo=\"http://openoffice.org/2004/office\" "
  951. " office:version=\"1.2\">"
  952. " <office:meta>") . (" </office:meta>" "</office:document-meta>")))
  953. (defun org-odt-copy-image-file (path &optional target-file)
  954. "Returns the internal name of the file"
  955. (let* ((image-type (file-name-extension path))
  956. (media-type (format "image/%s" image-type))
  957. (src-file (expand-file-name
  958. path (file-name-directory org-current-export-file)))
  959. (target-file (or target-file (org-odt-get-image-name src-file)))
  960. ;; FIXME
  961. (body-only nil))
  962. (when (not org-lparse-to-buffer)
  963. (message "Embedding %s as %s ..."
  964. (substring-no-properties path) target-file)
  965. (copy-file src-file target-file 'overwrite)
  966. (org-odt-update-manifest-file media-type target-file)
  967. (push target-file org-export-odt-file-list)) target-file))
  968. (defun org-odt-image-attrs-from-size (&optional width height)
  969. (concat
  970. (when width (format "svg:width=\"%0.2fcm\"" width))
  971. " "
  972. (when height (format "svg:height=\"%0.2fcm\"" height))))
  973. (defvar org-export-odt-image-size-probe-method
  974. '(emacs imagemagick force)
  975. "Ordered list of methods by for determining size of an embedded
  976. image.")
  977. (defvar org-export-odt-default-image-sizes-alist
  978. '(("character" . (5 . 0.4))
  979. ("paragraph" . (5 . 5)))
  980. "Hardcoded image dimensions one for each of the anchor
  981. methods.")
  982. (defun org-odt-do-image-size (probe-method file &optional dpi anchor-type)
  983. (setq dpi (or dpi org-export-odt-pixels-per-inch))
  984. (setq anchor-type (or anchor-type "paragraph"))
  985. (flet ((size-in-cms (size-in-pixels)
  986. (flet ((pixels-to-cms (pixels)
  987. (let* ((cms-per-inch 2.54)
  988. (inches (/ pixels dpi)))
  989. (* cms-per-inch inches))))
  990. (and size-in-pixels
  991. (cons (pixels-to-cms (car size-in-pixels))
  992. (pixels-to-cms (cdr size-in-pixels)))))))
  993. (case probe-method
  994. (emacs
  995. (size-in-cms (ignore-errors (image-size (create-image file) 'pixels))))
  996. (imagemagick
  997. (size-in-cms
  998. (let ((dim (shell-command-to-string
  999. (format "identify -format \"%%w:%%h\" \"%s\"" file))))
  1000. (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
  1001. (cons (string-to-number (match-string 1 dim))
  1002. (string-to-number (match-string 2 dim)))))))
  1003. (t
  1004. (cdr (assoc-string anchor-type
  1005. org-export-odt-default-image-sizes-alist))))))
  1006. (defun org-odt-image-size-from-file (file &optional user-width
  1007. user-height scale dpi embed-as)
  1008. (unless (file-name-absolute-p file)
  1009. (setq file (expand-file-name
  1010. file (file-name-directory org-current-export-file))))
  1011. (let* (size width height)
  1012. (unless (and user-height user-width)
  1013. (loop for probe-method in org-export-odt-image-size-probe-method
  1014. until size
  1015. do (setq size (org-odt-do-image-size
  1016. probe-method file dpi embed-as)))
  1017. (or size (error "Cannot determine Image size. Aborting ..."))
  1018. (setq width (car size) height (cdr size)))
  1019. (cond
  1020. (scale
  1021. (setq width (* width scale) height (* height scale)))
  1022. ((and user-height user-width)
  1023. (setq width user-width height user-height))
  1024. (user-height
  1025. (setq width (* user-height (/ width height)) height user-height))
  1026. (user-width
  1027. (setq height (* user-width (/ height width)) width user-width))
  1028. (t (ignore)))
  1029. (cons width height)))
  1030. (defvar org-odt-default-entity "Illustration")
  1031. (defun org-odt-format-entity-caption (label caption &optional default-entity)
  1032. (if (not label) (or caption "")
  1033. (let* ((label-components (org-odt-parse-label label))
  1034. (entity (car label-components))
  1035. (seqno (cdr label-components))
  1036. (caption (and caption (concat ": " caption))))
  1037. (unless seqno
  1038. (setq seqno label
  1039. entity (or default-entity org-odt-default-entity)))
  1040. (concat
  1041. entity " "
  1042. (org-odt-format-tags
  1043. '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" . "</text:sequence>")
  1044. seqno label entity entity)
  1045. caption))))
  1046. (defun org-odt-format-tags (tag text &rest args)
  1047. (let ((prefix (when org-lparse-encode-pending "@"))
  1048. (suffix (when org-lparse-encode-pending "@")))
  1049. (apply 'org-lparse-format-tags tag text prefix suffix args)))
  1050. (defun org-odt-init-outfile (filename)
  1051. (let* ((outdir (make-temp-file org-export-odt-tmpdir-prefix t))
  1052. (mimetype-file (expand-file-name "mimetype" outdir))
  1053. (content-file (expand-file-name "content.xml" outdir))
  1054. (manifest-file (expand-file-name "META-INF/manifest.xml" outdir))
  1055. (meta-file (expand-file-name "meta.xml" outdir))
  1056. (styles-file (expand-file-name "styles.xml" outdir))
  1057. (pictures-dir (expand-file-name "Pictures" outdir))
  1058. (body-only nil))
  1059. ;; content file
  1060. (with-current-buffer (find-file-noselect content-file t)
  1061. (erase-buffer))
  1062. ;; FIXME: How to factor in body-only here
  1063. (unless body-only
  1064. ;; manifest file
  1065. (make-directory (file-name-directory manifest-file))
  1066. (with-current-buffer (find-file-noselect manifest-file t)
  1067. (erase-buffer)
  1068. (insert (mapconcat 'identity (car org-export-odt-manifest-lines) "\n"))
  1069. (insert "\n")
  1070. (save-excursion
  1071. (insert (mapconcat 'identity (cdr org-export-odt-manifest-lines) "\n"))))
  1072. ;; meta file
  1073. (with-current-buffer (find-file-noselect meta-file t)
  1074. (erase-buffer)
  1075. (insert (mapconcat 'identity (car org-export-odt-meta-lines) "\n"))
  1076. (insert "\n")
  1077. (save-excursion
  1078. (insert (mapconcat 'identity (cdr org-export-odt-meta-lines) "\n"))))
  1079. ;; styles file
  1080. ;; (copy-file org-export-odt-styles-file styles-file t)
  1081. ;; Pictures dir
  1082. (make-directory pictures-dir)
  1083. ;; initialize list of files that contribute to the odt file
  1084. (setq org-export-odt-file-list
  1085. (append org-export-odt-save-list org-export-odt-nosave-list)))
  1086. content-file))
  1087. (defconst org-export-odt-mimetype-lines
  1088. '("application/vnd.oasis.opendocument.text"))
  1089. (defconst org-odt-manifest-file-entry-tag
  1090. "<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"/>")
  1091. (defun org-odt-save-as-outfile (target opt-plist)
  1092. ;; write meta file
  1093. (org-odt-update-meta-file opt-plist)
  1094. ;; write styles file
  1095. (org-odt-copy-styles-file)
  1096. ;; Update styles.xml - take care of outline numbering
  1097. (with-current-buffer
  1098. (find-file-noselect (expand-file-name "styles.xml") t)
  1099. ;; Don't make automatic backup of styles.xml file. This setting
  1100. ;; prevents the backedup styles.xml file from being zipped in to
  1101. ;; odt file. This is more of a hackish fix. Better alternative
  1102. ;; would be to fix the zip command so that the output odt file
  1103. ;; includes only the needed files and excludes any auto-generated
  1104. ;; extra files like backups and auto-saves etc etc. Note that
  1105. ;; currently the zip command zips up the entire temp directory so
  1106. ;; that any auto-generated files created under the hood ends up in
  1107. ;; the resulting odt file.
  1108. (set (make-local-variable 'backup-inhibited) t)
  1109. ;; Import local setting of `org-export-with-section-numbers'
  1110. (org-lparse-bind-local-variables opt-plist)
  1111. (org-odt-configure-outline-numbering
  1112. (if org-export-with-section-numbers org-export-headline-levels 0)))
  1113. (let ((zipdir default-directory))
  1114. (message "Switching to directory %s" (expand-file-name zipdir))
  1115. ;; save all xml files
  1116. (mapc (lambda (file)
  1117. (with-current-buffer
  1118. (find-file-noselect (expand-file-name file) t)
  1119. ;; prettify output
  1120. (indent-region (point-min) (point-max))
  1121. (save-buffer)))
  1122. org-export-odt-save-list)
  1123. (let* ((target-name (file-name-nondirectory target))
  1124. (target-dir (file-name-directory target))
  1125. (cmd (format "zip -rmTq %s %s" target-name ".")))
  1126. (when (file-exists-p target)
  1127. ;; FIXME: If the file is locked this throws a cryptic error
  1128. (delete-file target))
  1129. (let ((coding-system-for-write 'no-conversion) exitcode)
  1130. (message "Creating odt file using \"%s\"" cmd)
  1131. (setq exitcode
  1132. (apply 'call-process
  1133. "zip"
  1134. nil
  1135. nil
  1136. nil
  1137. (append (list "-rmTq") (list target-name "."))))
  1138. (or (zerop exitcode)
  1139. (error "Unable to create odt file (%S)" exitcode)))
  1140. ;; move the file from outdir to target-dir
  1141. (rename-file target-name target-dir)
  1142. ;; kill all xml buffers
  1143. (mapc (lambda (file)
  1144. (kill-buffer
  1145. (find-file-noselect (expand-file-name file zipdir) t)))
  1146. org-export-odt-save-list)
  1147. (delete-directory zipdir)))
  1148. (message "Created %s" target)
  1149. (set-buffer (find-file-noselect target t)))
  1150. (defun org-odt-update-meta-file (opt-plist)
  1151. (with-current-buffer
  1152. (find-file-noselect (expand-file-name "meta.xml") t)
  1153. (let ((date (or (plist-get opt-plist :effective-date) ""))
  1154. (author (or (plist-get opt-plist :author) ""))
  1155. (email (plist-get opt-plist :email))
  1156. (keywords (plist-get opt-plist :keywords))
  1157. (description (plist-get opt-plist :description))
  1158. (title (plist-get opt-plist :title)))
  1159. (insert
  1160. "\n"
  1161. (org-odt-format-tags '("<dc:creator>" . "</dc:creator>") author)
  1162. (org-odt-format-tags
  1163. '("\n<meta:initial-creator>" . "</meta:initial-creator>") author)
  1164. (org-odt-format-tags '("\n<dc:date>" . "</dc:date>") date)
  1165. (org-odt-format-tags
  1166. '("\n<meta:creation-date>" . "</meta:creation-date>") date)
  1167. (org-odt-format-tags '("\n<meta:generator>" . "</meta:generator>")
  1168. (when org-export-creator-info
  1169. (format "Org-%s/Emacs-%s"
  1170. org-version emacs-version)))
  1171. (org-odt-format-tags '("\n<meta:keyword>" . "</meta:keyword>") keywords)
  1172. (org-odt-format-tags '("\n<dc:subject>" . "</dc:subject>") description)
  1173. (org-odt-format-tags '("\n<dc:title>" . "</dc:title>") title)
  1174. "\n"))))
  1175. (defun org-odt-update-manifest-file (media-type full-path)
  1176. (with-current-buffer
  1177. (find-file-noselect (expand-file-name "META-INF/manifest.xml") t)
  1178. (insert (format org-odt-manifest-file-entry-tag media-type full-path))))
  1179. (defun org-odt-finalize-outfile ()
  1180. (message "org-newodt: Finalizing outfile")
  1181. (org-odt-delete-empty-paragraphs))
  1182. (defun org-odt-delete-empty-paragraphs ()
  1183. (goto-char (point-min))
  1184. (let ((open "<text:p[^>]*>")
  1185. (close "</text:p>"))
  1186. (while (re-search-forward (format "%s[ \r\n\t]*%s" open close) nil t)
  1187. (replace-match ""))))
  1188. (defun org-odt-get (what &optional opt-plist)
  1189. (case what
  1190. (BACKEND 'odt)
  1191. (EXPORT-DIR (org-export-directory :html opt-plist))
  1192. (FILE-NAME-EXTENSION "odt")
  1193. (EXPORT-BUFFER-NAME "*Org ODT Export*")
  1194. (ENTITY-CONTROL org-odt-entity-control-callbacks-alist)
  1195. (ENTITY-FORMAT org-odt-entity-format-callbacks-alist)
  1196. (INIT-METHOD 'org-odt-init-outfile)
  1197. (FINAL-METHOD 'org-odt-finalize-outfile)
  1198. (SAVE-METHOD 'org-odt-save-as-outfile)
  1199. (OTHER-BACKENDS
  1200. '("bib" "doc" "doc6" "doc95" "html" "xhtml" "latex" "odt" "ott" "pdf" "rtf"
  1201. "sdw" "sdw3" "sdw4" "stw " "sxw" "mediawiki" "text" "txt" "uot" "vor"
  1202. "vor3" "vor4" "docbook" "ooxml" "ppt" "odp"))
  1203. (CONVERT-METHOD org-lparse-convert-process)
  1204. (TOPLEVEL-HLEVEL 1)
  1205. (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps)
  1206. (INLINE-IMAGES 'maybe)
  1207. (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
  1208. (PLAIN-TEXT-MAP '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
  1209. (TABLE-FIRST-COLUMN-AS-LABELS nil)
  1210. (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY "," 'superscript))
  1211. (CODING-SYSTEM-FOR-WRITE 'utf-8)
  1212. (CODING-SYSTEM-FOR-SAVE 'utf-8)
  1213. (t (error "Unknown property: %s" what))))
  1214. (defun org-odt-parse-label (label)
  1215. (save-match-data
  1216. (if (not (string-match "\\`[a-zA-Z]+:\\(.+\\)" label))
  1217. (cons label nil)
  1218. (cons
  1219. (capitalize (substring label 0 (1- (match-beginning 1))))
  1220. (substring label (match-beginning 1))))))
  1221. (defvar org-lparse-latex-fragment-fallback) ; set by org-do-lparse
  1222. (defun org-export-odt-preprocess (parameters)
  1223. "Convert LaTeX fragments to images."
  1224. (when (and org-current-export-file
  1225. (plist-get parameters :LaTeX-fragments))
  1226. (org-format-latex
  1227. (concat "ltxpng/" (file-name-sans-extension
  1228. (file-name-nondirectory
  1229. org-current-export-file)))
  1230. org-current-export-dir nil "Creating LaTeX image %s"
  1231. nil nil
  1232. (cond
  1233. ((eq (plist-get parameters :LaTeX-fragments) 'verbatim) 'verbatim)
  1234. ;; Investigate MathToWeb for converting TeX equations to MathML
  1235. ;; See http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01755.html
  1236. ((or (eq (plist-get parameters :LaTeX-fragments) 'mathjax )
  1237. (eq (plist-get parameters :LaTeX-fragments) t ))
  1238. (org-lparse-warn
  1239. (concat
  1240. "Use of MathJax is incompatible with ODT exporter. "
  1241. (format "Using %S instead." org-lparse-latex-fragment-fallback)))
  1242. org-lparse-latex-fragment-fallback)
  1243. ((eq (plist-get parameters :LaTeX-fragments) 'dvipng ) 'dvipng)
  1244. (t nil))))
  1245. (goto-char (point-min))
  1246. (let (label label-components category value pretty-label)
  1247. (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t)
  1248. (org-if-unprotected-at (match-beginning 1)
  1249. (setq label (match-string 1)
  1250. label-components (org-odt-parse-label label)
  1251. category (car label-components)
  1252. value (cdr label-components)
  1253. pretty-label (if value (concat category " " value) label))
  1254. (replace-match
  1255. (let ((org-lparse-encode-pending t))
  1256. (org-odt-format-tags
  1257. '("<text:sequence-ref text:reference-format=\"category-and-value\" text:ref-name=\"%s\">"
  1258. . "</text:sequence-ref>") pretty-label label)) t t)))))
  1259. (declare-function archive-zip-extract "arc-mode.el" (archive name))
  1260. (defun org-odt-zip-extract-one (archive member &optional target)
  1261. (require 'arc-mode)
  1262. (let* ((target (or target default-directory))
  1263. (archive (expand-file-name archive))
  1264. (archive-zip-extract
  1265. (list "unzip" "-qq" "-o" "-d" target))
  1266. exit-code command-output)
  1267. (setq command-output
  1268. (with-temp-buffer
  1269. (setq exit-code (archive-zip-extract archive member))
  1270. (buffer-string)))
  1271. (unless (zerop exit-code)
  1272. (message command-output)
  1273. (error "Extraction failed"))))
  1274. (defun org-odt-zip-extract (archive members &optional target)
  1275. (when (atom members) (setq members (list members)))
  1276. (mapc (lambda (member)
  1277. (org-odt-zip-extract-one archive member target))
  1278. members))
  1279. (defun org-odt-copy-styles-file (&optional styles-file)
  1280. ;; Non-availability of styles.xml is not a critical error. For now
  1281. ;; throw an error purely for aesthetic reasons.
  1282. (setq styles-file (or styles-file
  1283. org-export-odt-styles-file
  1284. (expand-file-name "styles/OrgOdtStyles.xml"
  1285. org-odt-data-dir)
  1286. (error "org-odt: Missing styles file?")))
  1287. (cond
  1288. ((listp styles-file)
  1289. (let ((archive (nth 0 styles-file))
  1290. (members (nth 1 styles-file)))
  1291. (org-odt-zip-extract archive members)
  1292. (mapc
  1293. (lambda (member)
  1294. (when (org-file-image-p member)
  1295. (let* ((image-type (file-name-extension member))
  1296. (media-type (format "image/%s" image-type)))
  1297. (org-odt-update-manifest-file media-type member))))
  1298. members)))
  1299. ((and (stringp styles-file) (file-exists-p styles-file))
  1300. (let ((styles-file-type (file-name-extension styles-file)))
  1301. (cond
  1302. ((string= styles-file-type "xml")
  1303. (copy-file styles-file "styles.xml" t))
  1304. ((member styles-file-type '("odt" "ott"))
  1305. (org-odt-zip-extract styles-file "styles.xml")))))
  1306. (t
  1307. (error (format "Invalid specification of styles.xml file: %S"
  1308. org-export-odt-styles-file)))))
  1309. (defvar org-export-odt-factory-settings
  1310. "d4328fb9d1b6cb211d4320ff546829f26700dc5e"
  1311. "SHA1 hash of OrgOdtStyles.xml.")
  1312. (defun org-odt-configure-outline-numbering (level)
  1313. "Outline numbering is retained only upto LEVEL.
  1314. To disable outline numbering pass a LEVEL of 0."
  1315. (if (not (string= org-export-odt-factory-settings (sha1 (current-buffer))))
  1316. (org-lparse-warn
  1317. "org-odt: Using custom styles file? Consider tweaking styles.xml for better output. To suppress this warning update `org-export-odt-factory-settings'")
  1318. (goto-char (point-min))
  1319. (let ((regex
  1320. "<text:outline-level-style\\(.*\\)text:level=\"\\(.*\\)\"\\(.*\\)>")
  1321. (replacement
  1322. "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
  1323. (while (re-search-forward regex nil t)
  1324. (when (> (string-to-number (match-string 1)) level)
  1325. (replace-match replacement t nil))))
  1326. (save-buffer 0)))
  1327. (provide 'org-odt)
  1328. ;;; org-odt.el ends here