org-odt.el 57 KB

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