org-odt.el 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034
  1. ;;; org-odt.el --- OpenDocumentText export for Org-mode
  2. ;; Copyright (C) 2010-2011 Jambunathan <kjambunathan at gmail dot com>
  3. ;; Author: Jambunathan K <kjambunathan at gmail dot com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.8
  7. ;; This file is not (yet) part of GNU Emacs.
  8. ;; However, it is distributed under the same license.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20. ;;
  21. ;;; Commentary:
  22. ;;; Code:
  23. (eval-when-compile (require 'cl))
  24. (require 'org-lparse)
  25. (defun org-odt-end-export ()
  26. (org-odt-fixup-label-references)
  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 'OrgOdtContentTemplate.xml' - which are
  67. used as factory settings of `org-export-odt-styles-file' and
  68. `org-export-odt-content-template-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. (defun org-odt-unload-function ()
  123. (org-lparse-unregister-backend 'odt)
  124. (remove-hook 'org-export-preprocess-after-blockquote-hook
  125. 'org-export-odt-preprocess-latex-fragments)
  126. nil)
  127. (defcustom org-export-odt-content-template-file nil
  128. "Template file for \"content.xml\".
  129. The exporter embeds the exported content just before
  130. \"</office:text>\" element.
  131. If unspecified, the file named \"OrgOdtContentTemplate.xml\"
  132. under `org-odt-data-dir' is used."
  133. :type 'file
  134. :group 'org-export-odt)
  135. (defcustom org-export-odt-styles-file nil
  136. "Default styles file for use with ODT export.
  137. Valid values are one of:
  138. 1. nil
  139. 2. path to a styles.xml file
  140. 3. path to a *.odt or a *.ott file
  141. 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
  142. ...))
  143. In case of option 1, an in-built styles.xml is used. See
  144. `org-odt-data-dir' for more information.
  145. In case of option 3, the specified file is unzipped and the
  146. styles.xml embedded therein is used.
  147. In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
  148. and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
  149. generated odt file. Use relative path for specifying the
  150. FILE-MEMBERS. styles.xml must be specified as one of the
  151. FILE-MEMBERS.
  152. Use options 1, 2 or 3 only if styles.xml alone suffices for
  153. achieving the desired formatting. Use option 4, if the styles.xml
  154. references additional files like header and footer images for
  155. achieving the desired formattting."
  156. :group 'org-export-odt
  157. :type
  158. '(choice
  159. (const :tag "Factory settings" nil)
  160. (file :must-match t :tag "styles.xml")
  161. (file :must-match t :tag "ODT or OTT file")
  162. (list :tag "ODT or OTT file + Members"
  163. (file :must-match t :tag "ODF Text or Text Template file")
  164. (cons :tag "Members"
  165. (file :tag " Member" "styles.xml")
  166. (repeat (file :tag "Member"))))))
  167. (defconst org-export-odt-tmpdir-prefix "odt-")
  168. (defconst org-export-odt-bookmark-prefix "OrgXref.")
  169. (defcustom org-export-odt-use-bookmarks-for-internal-links t
  170. "Export Internal links as bookmarks?."
  171. :type 'boolean
  172. :group 'org-export-odt)
  173. (defcustom org-export-odt-embed-images t
  174. "Should the images be copied in to the odt file or just linked?"
  175. :type 'boolean
  176. :group 'org-export-odt)
  177. (defcustom org-odt-export-inline-images 'maybe
  178. "Non-nil means inline images into exported HTML pages.
  179. This is done using an <img> tag. When nil, an anchor with href is used to
  180. link to the image. If this option is `maybe', then images in links with
  181. an empty description will be inlined, while images with a description will
  182. be linked only."
  183. :group 'org-odt-export
  184. :type '(choice (const :tag "Never" nil)
  185. (const :tag "Always" t)
  186. (const :tag "When there is no description" maybe)))
  187. (defcustom org-odt-export-inline-image-extensions
  188. '("png" "jpeg" "jpg" "gif")
  189. "Extensions of image files that can be inlined into HTML."
  190. :type '(repeat (string :tag "Extension"))
  191. :group 'org-odt-export)
  192. (defcustom org-export-odt-pixels-per-inch display-pixels-per-inch
  193. ;; FIXME add docstring
  194. ""
  195. :type 'float
  196. :group 'org-export-odt)
  197. (defvar org-export-odt-default-org-styles-alist
  198. '((paragraph . ((default . "Text_20_body")
  199. (fixedwidth . "OrgFixedWidthBlock")
  200. (verse . "OrgVerse")
  201. (quote . "Quotations")
  202. (blockquote . "Quotations")
  203. (center . "OrgCenter")
  204. (left . "OrgLeft")
  205. (right . "OrgRight")
  206. (title . "Heading_20_1.title")
  207. (footnote . "Footnote")
  208. (src . "OrgSrcBlock")
  209. (illustration . "Illustration")
  210. (table . "Table")
  211. (definition-term . "Text_20_body_20_bold")
  212. (horizontal-line . "Horizontal_20_Line")))
  213. (character . ((bold . "Bold")
  214. (emphasis . "Emphasis")
  215. (code . "OrgCode")
  216. (verbatim . "OrgCode")
  217. (strike . "Strikethrough")
  218. (underline . "Underline")
  219. (subscript . "OrgSubscript")
  220. (superscript . "OrgSuperscript")))
  221. (list . ((ordered . "OrgNumberedList")
  222. (unordered . "OrgBulletedList")
  223. (description . "OrgDescriptionList"))))
  224. "Default styles for various entities.")
  225. (defvar org-export-odt-org-styles-alist org-export-odt-default-org-styles-alist)
  226. (defun org-odt-get-style-name-for-entity (category &optional entity)
  227. (let ((entity (or entity 'default)))
  228. (or
  229. (cdr (assoc entity (cdr (assoc category
  230. org-export-odt-org-styles-alist))))
  231. (cdr (assoc entity (cdr (assoc category
  232. org-export-odt-default-org-styles-alist))))
  233. (error "Cannot determine style name for entity %s of type %s"
  234. entity category))))
  235. (defcustom org-export-odt-preferred-output-format nil
  236. "Automatically post-process to this format after exporting to \"odt\".
  237. Interactive commands `org-export-as-odt' and
  238. `org-export-as-odt-and-open' export first to \"odt\" format and
  239. then use an external converter to convert the resulting document
  240. to this format.
  241. The converter used is that specified with CONVERT-METHOD option
  242. in `org-odt-get'. If the above option is unspecified then
  243. `org-lparse-convert-process' is used.
  244. The format specified here should be listed in OTHER-BACKENDS
  245. option of `org-odt-get' or `org-lparse-convert-capabilities' as
  246. appropriate."
  247. :group 'org-odt
  248. :type '(choice :convert-widget
  249. (lambda (w)
  250. (apply 'widget-convert (widget-type w)
  251. (eval (car (widget-get w :args)))))
  252. `((const :tag "None" nil)
  253. ,@(mapcar (lambda (c)
  254. `(const :tag ,(car c) ,(car c)))
  255. (org-lparse-get-other-backends "odt")))))
  256. ;;;###autoload
  257. (defun org-export-as-odt-and-open (arg)
  258. "Export the outline as ODT and immediately open it with a browser.
  259. If there is an active region, export only the region.
  260. The prefix ARG specifies how many levels of the outline should become
  261. headlines. The default is 3. Lower levels will become bulleted lists."
  262. (interactive "P")
  263. (org-lparse-and-open
  264. (or org-export-odt-preferred-output-format "odt") "odt" arg))
  265. ;;;###autoload
  266. (defun org-export-as-odt-batch ()
  267. "Call the function `org-lparse-batch'.
  268. This function can be used in batch processing as:
  269. emacs --batch
  270. --load=$HOME/lib/emacs/org.el
  271. --eval \"(setq org-export-headline-levels 2)\"
  272. --visit=MyFile --funcall org-export-as-odt-batch"
  273. (org-lparse-batch "odt"))
  274. ;;;###autoload
  275. (defun org-export-as-odt-to-buffer (arg)
  276. "Call `org-lparse-odt` with output to a temporary buffer.
  277. No file is created. The prefix ARG is passed through to `org-lparse-to-buffer'."
  278. (interactive "P")
  279. (org-lparse-to-buffer "odt" arg))
  280. ;;;###autoload
  281. (defun org-replace-region-by-odt (beg end)
  282. "Assume the current region has org-mode syntax, and convert it to ODT.
  283. This can be used in any buffer. For example, you could write an
  284. itemized list in org-mode syntax in an ODT buffer and then use this
  285. command to convert it."
  286. (interactive "r")
  287. (org-replace-region-by "odt" beg end))
  288. ;;;###autoload
  289. (defun org-export-region-as-odt (beg end &optional body-only buffer)
  290. "Convert region from BEG to END in org-mode buffer to ODT.
  291. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  292. contents, and only produce the region of converted text, useful for
  293. cut-and-paste operations.
  294. If BUFFER is a buffer or a string, use/create that buffer as a target
  295. of the converted ODT. If BUFFER is the symbol `string', return the
  296. produced ODT as a string and leave not buffer behind. For example,
  297. a Lisp program could call this function in the following way:
  298. (setq odt (org-export-region-as-odt beg end t 'string))
  299. When called interactively, the output buffer is selected, and shown
  300. in a window. A non-interactive call will only return the buffer."
  301. (interactive "r\nP")
  302. (org-lparse-region "odt" beg end body-only buffer))
  303. ;;; org-export-as-odt
  304. ;;;###autoload
  305. (defun org-export-as-odt (arg &optional hidden ext-plist
  306. to-buffer body-only pub-dir)
  307. "Export the outline as a OpenDocumentText file.
  308. If there is an active region, export only the region. The prefix
  309. ARG specifies how many levels of the outline should become
  310. headlines. The default is 3. Lower levels will become bulleted
  311. lists. HIDDEN is obsolete and does nothing.
  312. EXT-PLIST is a property list with external parameters overriding
  313. org-mode's default settings, but still inferior to file-local
  314. settings. When TO-BUFFER is non-nil, create a buffer with that
  315. name and export to that buffer. If TO-BUFFER is the symbol
  316. `string', don't leave any buffer behind but just return the
  317. resulting XML as a string. When BODY-ONLY is set, don't produce
  318. the file header and footer, simply return the content of
  319. <body>...</body>, without even the body tags themselves. When
  320. PUB-DIR is set, use this as the publishing directory."
  321. (interactive "P")
  322. (org-lparse (or org-export-odt-preferred-output-format "odt")
  323. "odt" arg hidden ext-plist to-buffer body-only pub-dir))
  324. (defvar org-odt-entity-control-callbacks-alist
  325. `((EXPORT
  326. . (org-odt-begin-export org-odt-end-export))
  327. (DOCUMENT-CONTENT
  328. . (org-odt-begin-document-content org-odt-end-document-content))
  329. (DOCUMENT-BODY
  330. . (org-odt-begin-document-body org-odt-end-document-body))
  331. (TOC
  332. . (org-odt-begin-toc org-odt-end-toc))
  333. (ENVIRONMENT
  334. . (org-odt-begin-environment org-odt-end-environment))
  335. (FOOTNOTE-DEFINITION
  336. . (org-odt-begin-footnote-definition org-odt-end-footnote-definition))
  337. (TABLE
  338. . (org-odt-begin-table org-odt-end-table))
  339. (TABLE-ROWGROUP
  340. . (org-odt-begin-table-rowgroup org-odt-end-table-rowgroup))
  341. (LIST
  342. . (org-odt-begin-list org-odt-end-list))
  343. (LIST-ITEM
  344. . (org-odt-begin-list-item org-odt-end-list-item))
  345. (OUTLINE
  346. . (org-odt-begin-outline org-odt-end-outline))
  347. (OUTLINE-TEXT
  348. . (org-odt-begin-outline-text org-odt-end-outline-text))
  349. (PARAGRAPH
  350. . (org-odt-begin-paragraph org-odt-end-paragraph)))
  351. "")
  352. (defvar org-odt-entity-format-callbacks-alist
  353. `((EXTRA-TARGETS . org-lparse-format-extra-targets)
  354. (ORG-TAGS . org-lparse-format-org-tags)
  355. (SECTION-NUMBER . org-lparse-format-section-number)
  356. (HEADLINE . org-odt-format-headline)
  357. (TOC-ENTRY . org-odt-format-toc-entry)
  358. (TOC-ITEM . org-odt-format-toc-item)
  359. (TAGS . org-odt-format-tags)
  360. (SPACES . org-odt-format-spaces)
  361. (TABS . org-odt-format-tabs)
  362. (LINE-BREAK . org-odt-format-line-break)
  363. (FONTIFY . org-odt-format-fontify)
  364. (TODO . org-lparse-format-todo)
  365. (LINK . org-odt-format-link)
  366. (INLINE-IMAGE . org-odt-format-inline-image)
  367. (ORG-LINK . org-odt-format-org-link)
  368. (HEADING . org-odt-format-heading)
  369. (ANCHOR . org-odt-format-anchor)
  370. (TABLE . org-lparse-format-table)
  371. (TABLE-ROW . org-odt-format-table-row)
  372. (TABLE-CELL . org-odt-format-table-cell)
  373. (FOOTNOTES-SECTION . ignore)
  374. (FOOTNOTE-REFERENCE . org-odt-format-footnote-reference)
  375. (HORIZONTAL-LINE . org-odt-format-horizontal-line)
  376. (COMMENT . org-odt-format-comment)
  377. (LINE . org-odt-format-line)
  378. (ORG-ENTITY . org-odt-format-org-entity))
  379. "")
  380. ;;;_. callbacks
  381. ;;;_. control callbacks
  382. ;;;_ , document body
  383. (defun org-odt-begin-office-body ()
  384. ;; automatic styles
  385. (insert-file-contents
  386. (or org-export-odt-content-template-file
  387. (expand-file-name "styles/OrgOdtContentTemplate.xml"
  388. org-odt-data-dir)))
  389. (goto-char (point-min))
  390. (re-search-forward "</office:text>" nil nil)
  391. (delete-region (match-beginning 0) (point-max)))
  392. ;; Following variable is let bound when `org-do-lparse' is in
  393. ;; progress. See org-html.el.
  394. (defvar org-lparse-toc)
  395. (defun org-odt-begin-document-body (opt-plist)
  396. (org-odt-begin-office-body)
  397. (let ((title (plist-get opt-plist :title)))
  398. (when title
  399. (insert
  400. (org-odt-format-stylized-paragraph 'title title))))
  401. ;; insert toc
  402. (when org-lparse-toc
  403. (insert "\n" org-lparse-toc "\n")))
  404. (defvar org-lparse-body-only) ; let bound during org-do-lparse
  405. (defvar org-lparse-to-buffer) ; let bound during org-do-lparse
  406. (defun org-odt-end-document-body (opt-plist)
  407. (unless org-lparse-body-only
  408. (org-lparse-insert-tag "</office:text>")
  409. (org-lparse-insert-tag "</office:body>")))
  410. (defun org-odt-begin-document-content (opt-plist)
  411. (ignore))
  412. (defun org-odt-end-document-content ()
  413. (org-lparse-insert-tag "</office:document-content>"))
  414. (defun org-odt-begin-outline (level1 snumber title tags
  415. target extra-targets class)
  416. (org-lparse-insert
  417. 'HEADING (org-lparse-format
  418. 'HEADLINE title extra-targets tags snumber level1)
  419. level1 target))
  420. (defun org-odt-end-outline ()
  421. (ignore))
  422. (defun org-odt-begin-outline-text (level1 snumber class)
  423. (ignore))
  424. (defun org-odt-end-outline-text ()
  425. (ignore))
  426. (defun org-odt-begin-paragraph (&optional style)
  427. (org-lparse-insert-tag
  428. "<text:p%s>" (org-odt-get-extra-attrs-for-paragraph-style style)))
  429. (defun org-odt-end-paragraph ()
  430. (org-lparse-insert-tag "</text:p>"))
  431. (defun org-odt-get-extra-attrs-for-paragraph-style (style)
  432. (let (style-name)
  433. (setq style-name
  434. (cond
  435. ((stringp style) style)
  436. ((symbolp style) (org-odt-get-style-name-for-entity
  437. 'paragraph style))))
  438. (unless style-name
  439. (error "Don't know how to handle paragraph style %s" style))
  440. (format " text:style-name=\"%s\"" style-name)))
  441. (defun org-odt-format-stylized-paragraph (style text)
  442. (org-odt-format-tags
  443. '("<text:p%s>" . "</text:p>") text
  444. (org-odt-get-extra-attrs-for-paragraph-style style)))
  445. (defun org-odt-begin-environment (style)
  446. (case style
  447. ((blockquote verse center quote)
  448. (org-lparse-begin-paragraph style)
  449. (list))
  450. ((fixedwidth native)
  451. (org-lparse-end-paragraph)
  452. (list))
  453. (t (error "Unknown environment %s" style))))
  454. (defun org-odt-end-environment (style)
  455. (case style
  456. ((blockquote verse center quote)
  457. (org-lparse-end-paragraph)
  458. (list))
  459. ((fixedwidth native)
  460. (org-lparse-begin-paragraph)
  461. (list))
  462. (t (error "Unknown environment %s" style))))
  463. (defvar org-lparse-list-level) ; dynamically bound in org-do-lparse
  464. (defun org-odt-begin-list (ltype)
  465. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  466. ltype))
  467. (let* ((style-name (org-odt-get-style-name-for-entity 'list ltype))
  468. (extra (concat (when (= org-lparse-list-level 1)
  469. " text:continue-numbering=\"false\"")
  470. (when style-name
  471. (format " text:style-name=\"%s\"" style-name)))))
  472. (case ltype
  473. ((ordered unordered description)
  474. (org-lparse-end-paragraph)
  475. (org-lparse-insert-tag "<text:list%s>" extra))
  476. (t (error "Unknown list type: %s" ltype)))))
  477. (defun org-odt-end-list (ltype)
  478. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  479. ltype))
  480. (if ltype
  481. (org-lparse-insert-tag "</text:list>")
  482. (error "Unknown list type: %s" ltype)))
  483. (defun org-odt-begin-list-item (ltype &optional arg headline)
  484. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  485. ltype))
  486. (case ltype
  487. (ordered
  488. (assert (not headline) t)
  489. (let* ((counter arg) (extra ""))
  490. (org-lparse-insert-tag "<text:list-item>")
  491. (org-lparse-begin-paragraph)))
  492. (unordered
  493. (let* ((id arg) (extra ""))
  494. (org-lparse-insert-tag "<text:list-item>")
  495. (org-lparse-begin-paragraph)
  496. (insert (if headline (org-odt-format-target headline id)
  497. (org-odt-format-bookmark "" id)))))
  498. (description
  499. (assert (not headline) t)
  500. (let ((term (or arg "(no term)")))
  501. (insert
  502. (org-odt-format-tags
  503. '("<text:list-item>" . "</text:list-item>")
  504. (org-odt-format-stylized-paragraph 'definition-term term)))
  505. (org-lparse-begin-list-item 'unordered)
  506. (org-lparse-begin-list 'description)
  507. (org-lparse-begin-list-item 'unordered)))
  508. (t (error "Unknown list type"))))
  509. (defun org-odt-end-list-item (ltype)
  510. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  511. ltype))
  512. (case ltype
  513. ((ordered unordered)
  514. (org-lparse-insert-tag "</text:list-item>"))
  515. (description
  516. (org-lparse-end-list-item-1)
  517. (org-lparse-end-list 'description)
  518. (org-lparse-end-list-item-1))
  519. (t (error "Unknown list type"))))
  520. ;; Following variables are let bound when table emission is in
  521. ;; progress. See org-lparse.el.
  522. (defvar org-lparse-table-begin-marker)
  523. (defvar org-lparse-table-ncols)
  524. (defvar org-lparse-table-rowgrp-open)
  525. (defvar org-lparse-table-rownum)
  526. (defvar org-lparse-table-cur-rowgrp-is-hdr)
  527. (defvar org-lparse-table-is-styled)
  528. (defvar org-lparse-table-rowgrp-info)
  529. (defvar org-lparse-table-colalign-vector)
  530. (defvar org-odt-table-style nil
  531. "Table style specified by \"#+ATTR_ODT: <style-name>\" line.
  532. This is set during `org-odt-begin-table'.")
  533. (defvar org-odt-table-style-spec nil
  534. "Entry for `org-odt-table-style' in `org-export-odt-table-styles'.")
  535. (defcustom org-export-odt-table-styles nil
  536. "Specify how Table Styles should be derived from a Table Template.
  537. This is a list where each element is of the
  538. form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
  539. TABLE-STYLE-NAME is the style associated with the table through
  540. `org-odt-table-style'.
  541. TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
  542. TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
  543. below) that is included in
  544. `org-export-odt-content-template-file'.
  545. TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
  546. \"TableCell\"
  547. PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
  548. \"TableParagraph\"
  549. TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
  550. \"FirstRow\" | \"LastRow\" |
  551. \"EvenRow\" | \"OddRow\" |
  552. \"EvenColumn\" | \"OddColumn\" | \"\"
  553. where \"+\" above denotes string concatenation.
  554. TABLE-CELL-OPTIONS is an alist where each element is of the
  555. form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
  556. TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
  557. `use-last-row-styles' |
  558. `use-first-column-styles' |
  559. `use-last-column-styles' |
  560. `use-banding-rows-styles' |
  561. `use-banding-columns-styles' |
  562. `use-first-row-styles'
  563. ON-OR-OFF := `t' | `nil'
  564. For example, with the following configuration
  565. \(setq org-export-odt-table-styles
  566. '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
  567. \(\(use-first-row-styles . t\)
  568. \(use-first-column-styles . t\)\)\)
  569. \(\"TableWithHeaderColumns\" \"Custom\"
  570. \(\(use-first-column-styles . t\)\)\)\)\)
  571. 1. A table associated with \"TableWithHeaderRowsAndColumns\"
  572. style will use the following table-cell styles -
  573. \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
  574. \"CustomTableCell\" and the following paragraph styles
  575. \"CustomFirstRowTableParagraph\",
  576. \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
  577. as appropriate.
  578. 2. A table associated with \"TableWithHeaderColumns\" style will
  579. use the following table-cell styles -
  580. \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
  581. following paragraph styles
  582. \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
  583. as appropriate..
  584. Note that TABLE-TEMPLATE-NAME corresponds to the
  585. \"<table:table-template>\" elements contained within
  586. \"<office:styles>\". The entries (TABLE-STYLE-NAME
  587. TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
  588. \"table:template-name\" and \"table:use-first-row-styles\" etc
  589. attributes of \"<table:table>\" element. Refer ODF-1.2
  590. specification for more information. Also consult the
  591. implementation filed under `org-odt-get-table-cell-styles'."
  592. :group 'org-export-odt
  593. :type '(choice
  594. (const :tag "None" nil)
  595. (repeat :tag "Table Styles"
  596. (list :tag "Table Style Specification"
  597. (string :tag "Table Style Name")
  598. (string :tag "Table Template Name")
  599. (alist :options (use-first-row-styles
  600. use-last-row-styles
  601. use-first-column-styles
  602. use-last-column-styles
  603. use-banding-rows-styles
  604. use-banding-columns-styles)
  605. :key-type symbol
  606. :value-type (const :tag "True" t))))))
  607. (defun org-odt-begin-table (caption label attributes)
  608. (setq org-odt-table-style attributes)
  609. (setq org-odt-table-style-spec
  610. (assoc org-odt-table-style org-export-odt-table-styles))
  611. (when label
  612. (insert
  613. (org-odt-format-stylized-paragraph
  614. 'table (org-odt-format-entity-caption label caption "Table"))))
  615. (org-lparse-insert-tag
  616. "<table:table table:name=\"%s\" table:style-name=\"%s\">"
  617. (or label "") (or (nth 1 org-odt-table-style-spec) "OrgTable"))
  618. (setq org-lparse-table-begin-marker (point)))
  619. (defun org-odt-end-table ()
  620. (goto-char org-lparse-table-begin-marker)
  621. (loop for level from 0 below org-lparse-table-ncols
  622. do (insert
  623. (org-odt-format-tags
  624. "<table:table-column table:style-name=\"%sColumn\"/>"
  625. "" (or (nth 1 org-odt-table-style-spec) "OrgTable"))))
  626. ;; fill style attributes for table cells
  627. (when org-lparse-table-is-styled
  628. (while (re-search-forward "@@\\(table-cell:p\\|table-cell:style-name\\)@@\\([0-9]+\\)@@\\([0-9]+\\)@@" nil t)
  629. (let* ((spec (match-string 1))
  630. (r (string-to-number (match-string 2)))
  631. (c (string-to-number (match-string 3)))
  632. (cell-styles (org-odt-get-table-cell-styles
  633. r c org-odt-table-style-spec))
  634. (table-cell-style (car cell-styles))
  635. (table-cell-paragraph-style (cdr cell-styles)))
  636. (cond
  637. ((equal spec "table-cell:p")
  638. (replace-match table-cell-paragraph-style t t))
  639. ((equal spec "table-cell:style-name")
  640. (replace-match table-cell-style t t))))))
  641. (goto-char (point-max))
  642. (org-lparse-insert-tag "</table:table>"))
  643. (defun org-odt-begin-table-rowgroup (&optional is-header-row)
  644. (when org-lparse-table-rowgrp-open
  645. (org-lparse-end 'TABLE-ROWGROUP))
  646. (org-lparse-insert-tag (if is-header-row
  647. "<table:table-header-rows>"
  648. "<table:table-rows>"))
  649. (setq org-lparse-table-rowgrp-open t)
  650. (setq org-lparse-table-cur-rowgrp-is-hdr is-header-row))
  651. (defun org-odt-end-table-rowgroup ()
  652. (when org-lparse-table-rowgrp-open
  653. (setq org-lparse-table-rowgrp-open nil)
  654. (org-lparse-insert-tag
  655. (if org-lparse-table-cur-rowgrp-is-hdr
  656. "</table:table-header-rows>" "</table:table-rows>"))))
  657. (defun org-odt-format-table-row (row)
  658. (org-odt-format-tags
  659. '("<table:table-row>" . "</table:table-row>") row))
  660. (defun org-odt-get-table-cell-styles (r c &optional style-spec)
  661. "Retrieve styles applicable to a table cell.
  662. R and C are (zero-based) row and column numbers of the table
  663. cell. STYLE-SPEC is an entry in `org-export-odt-table-styles'
  664. applicable to the current table. It is `nil' if the table is not
  665. associated with any style attributes.
  666. Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
  667. When STYLE-SPEC is nil, style the table cell the conventional way
  668. - choose cell borders based on row and column groupings and
  669. choose paragraph alignment based on `org-col-cookies' text
  670. property. See also
  671. `org-odt-get-paragraph-style-cookie-for-table-cell'.
  672. When STYLE-SPEC is non-nil, ignore the above cookie and return
  673. styles congruent with the ODF-1.2 specification."
  674. (cond
  675. (style-spec
  676. ;; LibreOffice - particularly the Writer - honors neither table
  677. ;; templates nor custom table-cell styles. Inorder to retain
  678. ;; inter-operability with LibreOffice, only automatic styles are
  679. ;; used for styling of table-cells. The current implementation is
  680. ;; congruent with ODF-1.2 specification and hence is
  681. ;; future-compatible.
  682. ;; Additional Note: LibreOffice's AutoFormat facility for tables -
  683. ;; which recognizes as many as 16 different cell types - is much
  684. ;; richer. Unfortunately it is NOT amenable to easy configuration
  685. ;; by hand.
  686. (let* ((template-name (nth 1 style-spec))
  687. (cell-style-selectors (nth 2 style-spec))
  688. (cell-type
  689. (cond
  690. ((and (cdr (assoc 'use-first-column-styles cell-style-selectors))
  691. (= c 0)) "FirstColumn")
  692. ((and (cdr (assoc 'use-last-column-styles cell-style-selectors))
  693. (= c (1- org-lparse-table-ncols))) "LastColumn")
  694. ((and (cdr (assoc 'use-first-row-styles cell-style-selectors))
  695. (= r 0)) "FirstRow")
  696. ((and (cdr (assoc 'use-last-row-styles cell-style-selectors))
  697. (= r org-lparse-table-rownum))
  698. "LastRow")
  699. ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
  700. (= (% r 2) 1)) "EvenRow")
  701. ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
  702. (= (% r 2) 0)) "OddRow")
  703. ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
  704. (= (% c 2) 1)) "EvenColumn")
  705. ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
  706. (= (% c 2) 0)) "OddColumn")
  707. (t ""))))
  708. (cons
  709. (concat template-name cell-type "TableCell")
  710. (concat template-name cell-type "TableParagraph"))))
  711. (t
  712. (cons
  713. (concat
  714. "OrgTblCell"
  715. (cond
  716. ((= r 0) "T")
  717. ((eq (cdr (assoc r org-lparse-table-rowgrp-info)) :start) "T")
  718. (t ""))
  719. (when (= r org-lparse-table-rownum) "B")
  720. (cond
  721. ((= c 0) "")
  722. ((or (memq (nth c org-table-colgroup-info) '(:start :startend))
  723. (memq (nth (1- c) org-table-colgroup-info) '(:end :startend))) "L")
  724. (t "")))
  725. (capitalize (aref org-lparse-table-colalign-vector c))))))
  726. (defun org-odt-get-paragraph-style-cookie-for-table-cell (r c)
  727. (concat
  728. (and (not org-odt-table-style-spec)
  729. (cond
  730. (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
  731. ((and (= c 0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS))
  732. "OrgTableHeading")
  733. (t "OrgTableContents")))
  734. (and org-lparse-table-is-styled
  735. (format "@@table-cell:p@@%03d@@%03d@@" r c))))
  736. (defun org-odt-get-style-name-cookie-for-table-cell (r c)
  737. (when org-lparse-table-is-styled
  738. (format "@@table-cell:style-name@@%03d@@%03d@@" r c)))
  739. (defun org-odt-format-table-cell (data r c)
  740. (let* ((paragraph-style-cookie
  741. (org-odt-get-paragraph-style-cookie-for-table-cell r c))
  742. (style-name-cookie
  743. (org-odt-get-style-name-cookie-for-table-cell r c))
  744. (extra (if style-name-cookie
  745. (format " table:style-name=\"%s\"" style-name-cookie) "")))
  746. (org-odt-format-tags
  747. '("<table:table-cell%s>" . "</table:table-cell>")
  748. (if org-lparse-list-table-p data
  749. (org-odt-format-stylized-paragraph paragraph-style-cookie data)) extra)))
  750. (defun org-odt-begin-footnote-definition (n)
  751. (org-lparse-begin-paragraph 'footnote))
  752. (defun org-odt-end-footnote-definition (n)
  753. (org-lparse-end-paragraph))
  754. (defun org-odt-begin-toc (lang-specific-heading)
  755. (insert
  756. (format "
  757. <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
  758. <text:table-of-content-source text:outline-level=\"10\">
  759. <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
  760. " lang-specific-heading))
  761. (loop for level from 1 upto 10
  762. do (insert (format
  763. "
  764. <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
  765. <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
  766. <text:index-entry-chapter/>
  767. <text:index-entry-text/>
  768. <text:index-entry-link-end/>
  769. </text:table-of-content-entry-template>
  770. " level level)))
  771. (insert
  772. (format "
  773. </text:table-of-content-source>
  774. <text:index-body>
  775. <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
  776. <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
  777. </text:index-title>
  778. " lang-specific-heading)))
  779. (defun org-odt-end-toc ()
  780. (insert "
  781. </text:index-body>
  782. </text:table-of-content>
  783. "))
  784. (defun org-odt-format-toc-entry (snumber todo headline tags href)
  785. (setq headline (concat
  786. (and org-export-with-section-numbers
  787. (concat snumber ". "))
  788. headline
  789. (and tags
  790. (concat
  791. (org-lparse-format 'SPACES 3)
  792. (org-lparse-format 'FONTIFY tags "tag")))))
  793. (when todo
  794. (setq headline (org-lparse-format 'FONTIFY headline "todo")))
  795. (let ((org-odt-suppress-xref t))
  796. (org-odt-format-link headline (concat "#" href))))
  797. (defun org-odt-format-toc-item (toc-entry level org-last-level)
  798. (let ((style (format "Contents_20_%d"
  799. (+ level (or (org-lparse-get 'TOPLEVEL-HLEVEL) 1) -1))))
  800. (insert "\n" (org-odt-format-stylized-paragraph style toc-entry) "\n")))
  801. ;; Following variable is let bound during 'ORG-LINK callback. See
  802. ;; org-html.el
  803. (defvar org-lparse-link-description-is-image nil)
  804. (defun org-odt-format-link (desc href &optional attr)
  805. (cond
  806. ((and (= (string-to-char href) ?#) (not org-odt-suppress-xref))
  807. (setq href (concat org-export-odt-bookmark-prefix (substring href 1)))
  808. (org-odt-format-tags
  809. '("<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"%s\">" .
  810. "</text:bookmark-ref>")
  811. desc href))
  812. (org-lparse-link-description-is-image
  813. (org-odt-format-tags
  814. '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</draw:a>")
  815. desc href (or attr "")))
  816. (t
  817. (org-odt-format-tags
  818. '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</text:a>")
  819. desc href (or attr "")))))
  820. (defun org-odt-format-spaces (n)
  821. (cond
  822. ((= n 1) " ")
  823. ((> n 1) (concat
  824. " " (org-odt-format-tags "<text:s text:c=\"%d\"/>" "" (1- n))))
  825. (t "")))
  826. (defun org-odt-format-tabs (&optional n)
  827. (let ((tab "<text:tab/>")
  828. (n (or n 1)))
  829. (insert tab)))
  830. (defun org-odt-format-line-break ()
  831. (org-odt-format-tags "<text:line-break/>" ""))
  832. (defun org-odt-format-horizontal-line ()
  833. (org-odt-format-stylized-paragraph 'horizontal-line ""))
  834. (defun org-odt-format-line (line)
  835. (case org-lparse-dyn-current-environment
  836. (fixedwidth (concat
  837. (org-odt-format-stylized-paragraph
  838. 'fixedwidth (org-odt-fill-tabs-and-spaces
  839. (org-xml-encode-plain-text line))) "\n"))
  840. (t (concat line "\n"))))
  841. (defun org-odt-format-comment (fmt &rest args)
  842. (let ((comment (apply 'format fmt args)))
  843. (format "\n<!-- %s -->\n" comment)))
  844. (defun org-odt-format-org-entity (wd)
  845. (org-entity-get-representation wd 'utf8))
  846. (defun org-odt-fill-tabs-and-spaces (line)
  847. (replace-regexp-in-string
  848. "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
  849. (cond
  850. ((string= s "\t") (org-odt-format-tabs))
  851. (t (org-odt-format-spaces (length s))))) line))
  852. (defcustom org-export-odt-use-htmlfontify t
  853. "Specify whether or not source blocks need to be fontified.
  854. Turn this option on if you want to colorize the source code
  855. blocks in the exported file. For colorization to work, you need
  856. to make available an enhanced version of `htmlfontify' library."
  857. :type 'boolean
  858. :group 'org-export-odt)
  859. (defun org-odt-format-source-code-or-example-plain
  860. (lines lang caption textareap cols rows num cont rpllbl fmt)
  861. "Format source or example blocks much like fixedwidth blocks.
  862. Use this when `org-export-odt-use-htmlfontify' option is turned
  863. off."
  864. (setq lines (org-export-number-lines (org-xml-encode-plain-text-lines lines)
  865. 0 0 num cont rpllbl fmt))
  866. (mapconcat
  867. (lambda (line)
  868. (org-odt-format-stylized-paragraph
  869. 'fixedwidth (org-odt-fill-tabs-and-spaces line)))
  870. (org-split-string lines "[\r\n]") "\n"))
  871. (defvar org-src-block-paragraph-format
  872. "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
  873. <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
  874. <style:background-image/>
  875. </style:paragraph-properties>
  876. <style:text-properties fo:color=\"%s\"/>
  877. </style:style>"
  878. "Custom paragraph style for colorized source and example blocks.
  879. This style is much the same as that of \"OrgFixedWidthBlock\"
  880. except that the foreground and background colors are set
  881. according to the default face identified by the `htmlfontify'.")
  882. (defun org-odt-hfy-face-to-css (fn)
  883. "Create custom style for face FN.
  884. When FN is the default face, use it's foreground and background
  885. properties to create \"OrgSrcBlock\" paragraph style. Otherwise
  886. use it's color attribute to create a character style whose name
  887. is obtained from FN. Currently all attributes of FN other than
  888. color are ignored.
  889. The style name for a face FN is derived using the following
  890. operations on the face name in that order - de-dash, CamelCase
  891. and prefix with \"OrgSrc\". For example,
  892. `font-lock-function-name-face' is associated with
  893. \"OrgSrcFontLockFunctionNameFace\"."
  894. (let* ((css-list (hfy-face-to-style fn))
  895. (style-name ((lambda (fn)
  896. (concat "OrgSrc"
  897. (mapconcat
  898. 'capitalize (split-string
  899. (hfy-face-or-def-to-name fn) "-")
  900. ""))) fn))
  901. (color-val (cdr (assoc "color" css-list)))
  902. (background-color-val (cdr (assoc "background" css-list)))
  903. (style (and org-export-odt-create-custom-styles-for-srcblocks
  904. (cond
  905. ((eq fn 'default)
  906. (format org-src-block-paragraph-format
  907. background-color-val color-val))
  908. (t
  909. (format
  910. "
  911. <style:style style:name=\"%s\" style:family=\"text\">
  912. <style:text-properties fo:color=\"%s\"/>
  913. </style:style>" style-name color-val))))))
  914. (cons style-name style)))
  915. (defcustom org-export-odt-create-custom-styles-for-srcblocks t
  916. "Whether custom styles for colorized source blocks be automatically created.
  917. When this option is turned on, the exporter creates custom styles
  918. for source blocks based on the advice of `htmlfontify'. Creation
  919. of custom styles happen as part of `org-odt-hfy-face-to-css'.
  920. When this option is turned off exporter does not create such
  921. styles.
  922. Use the latter option if you do not want the custom styles to be
  923. based on your current display settings. It is necessary that the
  924. styles.xml already contains needed styles for colorizing to work.
  925. This variable is effective only if
  926. `org-export-odt-use-htmlfontify' is turned on."
  927. :group 'org-export-odt
  928. :type 'boolean)
  929. (defun org-odt-insert-custom-styles-for-srcblocks (styles)
  930. "Save STYLES used for colorizing of source blocks.
  931. Update styles.xml with styles that were collected as part of
  932. `org-odt-hfy-face-to-css' callbacks."
  933. (when styles
  934. (with-current-buffer
  935. (find-file-noselect (expand-file-name "styles.xml") t)
  936. (goto-char (point-min))
  937. (when (re-search-forward "</office:styles>" nil t)
  938. (goto-char (match-beginning 0))
  939. (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n")))))
  940. (defun org-odt-format-source-code-or-example-colored
  941. (lines lang caption textareap cols rows num cont rpllbl fmt)
  942. "Format source or example blocks using `htmlfontify-string'.
  943. Use this routine when `org-export-odt-use-htmlfontify' option is
  944. turned on."
  945. (let* ((lang-m (and lang (or (cdr (assoc lang org-src-lang-modes)) lang)))
  946. (mode (and lang-m (intern (concat (if (symbolp lang-m)
  947. (symbol-name lang-m)
  948. lang-m) "-mode"))))
  949. (org-inhibit-startup t)
  950. (org-startup-folded nil)
  951. (lines (with-temp-buffer
  952. (insert lines)
  953. (if (functionp mode) (funcall mode) (fundamental-mode))
  954. (font-lock-fontify-buffer)
  955. (buffer-string)))
  956. (hfy-html-quote-regex "\\([<\"&> ]\\)")
  957. (hfy-html-quote-map '(("\"" "&quot;")
  958. ("<" "&lt;")
  959. ("&" "&amp;")
  960. (">" "&gt;")
  961. (" " "<text:s/>")
  962. (" " "<text:tab/>")))
  963. (hfy-face-to-css 'org-odt-hfy-face-to-css)
  964. (hfy-optimisations-1 (copy-seq hfy-optimisations))
  965. (hfy-optimisations (add-to-list 'hfy-optimisations-1
  966. 'body-text-only))
  967. (hfy-begin-span-handler
  968. (lambda (style text-block text-id text-begins-block-p)
  969. (insert (format "<text:span text:style-name=\"%s\">" style))))
  970. (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
  971. (when (fboundp 'htmlfontify-string)
  972. (mapconcat
  973. (lambda (line)
  974. (org-odt-format-stylized-paragraph 'src (htmlfontify-string line)))
  975. (org-split-string lines "[\r\n]") "\n"))))
  976. (defun org-odt-format-source-code-or-example (lines lang caption textareap
  977. cols rows num cont
  978. rpllbl fmt)
  979. "Format source or example blocks for export.
  980. Use `org-odt-format-source-code-or-example-plain' or
  981. `org-odt-format-source-code-or-example-colored' depending on the
  982. value of `org-export-odt-use-htmlfontify."
  983. (funcall
  984. (if (and org-export-odt-use-htmlfontify
  985. (or (featurep 'htmlfontify) (require 'htmlfontify))
  986. (fboundp 'htmlfontify-string))
  987. 'org-odt-format-source-code-or-example-colored
  988. 'org-odt-format-source-code-or-example-plain)
  989. lines lang caption textareap cols rows num cont rpllbl fmt))
  990. (defun org-xml-encode-plain-text-lines (rtn)
  991. (mapconcat 'org-xml-encode-plain-text (org-split-string rtn "[\r\n]") "\n"))
  992. (defun org-odt-remap-stylenames (style-name)
  993. (or
  994. (cdr (assoc style-name '(("timestamp-wrapper" . "OrgTimestampWrapper")
  995. ("timestamp" . "OrgTimestamp")
  996. ("timestamp-kwd" . "OrgTimestampKeyword")
  997. ("tag" . "OrgTag")
  998. ("todo" . "OrgTodo")
  999. ("done" . "OrgDone")
  1000. ("target" . "OrgTarget"))))
  1001. style-name))
  1002. (defun org-odt-format-fontify (text style &optional id)
  1003. (let* ((style-name
  1004. (cond
  1005. ((stringp style)
  1006. (org-odt-remap-stylenames style))
  1007. ((symbolp style)
  1008. (org-odt-get-style-name-for-entity 'character style))
  1009. ((listp style)
  1010. (assert (< 1 (length style)))
  1011. (let ((parent-style (pop style)))
  1012. (mapconcat (lambda (s)
  1013. ;; (assert (stringp s) t)
  1014. (org-odt-remap-stylenames s)) style "")
  1015. (org-odt-remap-stylenames parent-style)))
  1016. (t (error "Don't how to handle style %s" style)))))
  1017. (org-odt-format-tags
  1018. '("<text:span text:style-name=\"%s\">" . "</text:span>")
  1019. text style-name)))
  1020. (defun org-odt-relocate-relative-path (path dir)
  1021. (if (file-name-absolute-p path) path
  1022. (file-relative-name (expand-file-name path dir)
  1023. (expand-file-name "eyecandy" dir))))
  1024. (defun org-odt-format-inline-image (thefile)
  1025. (let* ((thelink (if (file-name-absolute-p thefile) thefile
  1026. (org-xml-format-href
  1027. (org-odt-relocate-relative-path
  1028. thefile org-current-export-file))))
  1029. (href
  1030. (org-odt-format-tags
  1031. "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
  1032. (if org-export-odt-embed-images
  1033. (org-odt-copy-image-file thefile) thelink))))
  1034. (org-export-odt-format-image thefile href)))
  1035. (defun org-export-odt-format-formula (src href &optional embed-as)
  1036. "Create image tag with source and attributes."
  1037. (save-match-data
  1038. (let* ((caption (org-find-text-property-in-string 'org-caption src))
  1039. (caption (and caption (org-xml-format-desc caption)))
  1040. (label (org-find-text-property-in-string 'org-label src))
  1041. (embed-as (or embed-as
  1042. (and (org-find-text-property-in-string
  1043. 'org-latex-src src)
  1044. (org-find-text-property-in-string
  1045. 'org-latex-src-embed-type src))
  1046. 'paragraph))
  1047. width height)
  1048. (cond
  1049. ((eq embed-as 'character)
  1050. (org-odt-format-entity "InlineFormula" href width height))
  1051. (t
  1052. (org-lparse-end-paragraph)
  1053. (org-lparse-insert-list-table
  1054. `((,(org-odt-format-entity
  1055. (if caption "CaptionedDisplayFormula" "DisplayFormula")
  1056. href width height caption nil)
  1057. ,(if (not label) ""
  1058. (org-odt-format-entity-caption label nil "Equation"))))
  1059. nil nil nil "OrgEquation" nil '((1 "c" 8) (2 "c" 1)))
  1060. (throw 'nextline nil))))))
  1061. (defvar org-odt-embedded-formulas-count 0)
  1062. (defun org-odt-copy-formula-file (path)
  1063. "Returns the internal name of the file"
  1064. (let* ((src-file (expand-file-name
  1065. path (file-name-directory org-current-export-file)))
  1066. (target-dir (format "Formula-%04d/"
  1067. (incf org-odt-embedded-formulas-count)))
  1068. (target-file (concat target-dir "content.xml")))
  1069. (when (not org-lparse-to-buffer)
  1070. (message "Embedding %s as %s ..."
  1071. (substring-no-properties path) target-file)
  1072. (make-directory target-dir)
  1073. (org-odt-create-manifest-file-entry
  1074. "application/vnd.oasis.opendocument.formula" target-dir "1.2")
  1075. (copy-file src-file target-file 'overwrite)
  1076. (org-odt-create-manifest-file-entry "text/xml" target-file))
  1077. target-file))
  1078. (defun org-odt-format-inline-formula (thefile)
  1079. (let* ((thelink (if (file-name-absolute-p thefile) thefile
  1080. (org-xml-format-href
  1081. (org-odt-relocate-relative-path
  1082. thefile org-current-export-file))))
  1083. (href
  1084. (org-odt-format-tags
  1085. "<draw:object xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
  1086. (file-name-directory (org-odt-copy-formula-file thefile)))))
  1087. (org-export-odt-format-formula thefile href)))
  1088. (defun org-odt-is-formula-link-p (file)
  1089. (member (downcase (file-name-extension file)) '("mathml")))
  1090. (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
  1091. descp)
  1092. "Make an HTML link.
  1093. OPT-PLIST is an options list.
  1094. TYPE is the device-type of the link (THIS://foo.html)
  1095. PATH is the path of the link (http://THIS#locationx)
  1096. FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
  1097. DESC is the link description, if any.
  1098. ATTR is a string of other attributes of the a element.
  1099. MAY-INLINE-P allows inlining it as an image."
  1100. (declare (special org-lparse-par-open))
  1101. (save-match-data
  1102. (let* ((may-inline-p
  1103. (and (member type-1 '("http" "https" "file"))
  1104. (org-lparse-should-inline-p path descp)
  1105. (not fragment)))
  1106. (type (if (equal type-1 "id") "file" type-1))
  1107. (filename path)
  1108. (thefile path))
  1109. (cond
  1110. ;; check for inlined images
  1111. ((and (member type '("file"))
  1112. (not fragment)
  1113. (org-file-image-p
  1114. filename org-odt-export-inline-image-extensions)
  1115. (or (eq t org-odt-export-inline-images)
  1116. (and org-odt-export-inline-images (not descp))))
  1117. (org-odt-format-inline-image thefile))
  1118. ;; check for embedded formulas
  1119. ((and (member type '("file"))
  1120. (not fragment)
  1121. (org-odt-is-formula-link-p filename)
  1122. (or (not descp)))
  1123. (org-odt-format-inline-formula thefile))
  1124. (t
  1125. (when (string= type "file")
  1126. (setq thefile
  1127. (cond
  1128. ((file-name-absolute-p path)
  1129. (concat "file://" (expand-file-name path)))
  1130. (t (org-odt-relocate-relative-path
  1131. thefile org-current-export-file)))))
  1132. (when (and (member type '("" "http" "https" "file" "coderef"))
  1133. fragment)
  1134. (setq thefile (concat thefile "#" fragment)))
  1135. (setq thefile (org-xml-format-href thefile))
  1136. (when (not (member type '("" "file" "coderef")))
  1137. (setq thefile (concat type ":" thefile)))
  1138. (let ((org-odt-suppress-xref (string= type "coderef")))
  1139. (org-odt-format-link
  1140. (org-xml-format-desc desc) thefile attr)))))))
  1141. (defun org-odt-format-heading (text level &optional id)
  1142. (let* ((text (if id (org-odt-format-target text id) text)))
  1143. (org-odt-format-tags
  1144. '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
  1145. "</text:h>") text level level)))
  1146. (defun org-odt-format-headline (title extra-targets tags
  1147. &optional snumber level)
  1148. (concat
  1149. (org-lparse-format 'EXTRA-TARGETS extra-targets)
  1150. ;; No need to generate section numbers. They are auto-generated by
  1151. ;; the application
  1152. ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
  1153. title
  1154. (and tags (concat (org-lparse-format 'SPACES 3)
  1155. (org-lparse-format 'ORG-TAGS tags)))))
  1156. (defun org-odt-format-anchor (text name &optional class)
  1157. (org-odt-format-target text name))
  1158. (defun org-odt-format-bookmark (text id)
  1159. (if id
  1160. (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id)
  1161. text))
  1162. (defun org-odt-format-target (text id)
  1163. (let ((name (concat org-export-odt-bookmark-prefix id)))
  1164. (concat
  1165. (and id (org-odt-format-tags
  1166. "<text:bookmark-start text:name=\"%s\"/>" "" name))
  1167. (org-odt-format-bookmark text id)
  1168. (and id (org-odt-format-tags
  1169. "<text:bookmark-end text:name=\"%s\"/>" "" name)))))
  1170. (defun org-odt-format-footnote (n def)
  1171. (let ((id (concat "fn" n))
  1172. (note-class "footnote")
  1173. (par-style "Footnote"))
  1174. (org-odt-format-tags
  1175. '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
  1176. "</text:note>")
  1177. (concat
  1178. (org-odt-format-tags
  1179. '("<text:note-citation>" . "</text:note-citation>")
  1180. n)
  1181. (org-odt-format-tags
  1182. '("<text:note-body>" . "</text:note-body>")
  1183. def))
  1184. id note-class)))
  1185. (defun org-odt-format-footnote-reference (n def refcnt)
  1186. (if (= refcnt 1)
  1187. (org-odt-format-footnote n def)
  1188. (org-odt-format-footnote-ref n)))
  1189. (defun org-odt-format-footnote-ref (n)
  1190. (let ((note-class "footnote")
  1191. (ref-format "text")
  1192. (ref-name (concat "fn" n)))
  1193. (org-odt-format-tags
  1194. '("<text:span text:style-name=\"%s\">" . "</text:span>")
  1195. (org-odt-format-tags
  1196. '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" . "</text:note-ref>")
  1197. n note-class ref-format ref-name)
  1198. "OrgSuperscript")))
  1199. (defun org-odt-get-image-name (file-name)
  1200. (require 'sha1)
  1201. (file-relative-name
  1202. (expand-file-name
  1203. (concat (sha1 file-name) "." (file-name-extension file-name)) "Pictures")))
  1204. (defun org-export-odt-format-image (src href &optional embed-as)
  1205. "Create image tag with source and attributes."
  1206. (save-match-data
  1207. (let* ((caption (org-find-text-property-in-string 'org-caption src))
  1208. (caption (and caption (org-xml-format-desc caption)))
  1209. (attr (org-find-text-property-in-string 'org-attributes src))
  1210. (label (org-find-text-property-in-string 'org-label src))
  1211. (embed-as (or embed-as
  1212. (if (org-find-text-property-in-string
  1213. 'org-latex-src src)
  1214. (or (org-find-text-property-in-string
  1215. 'org-latex-src-embed-type src) 'character)
  1216. 'paragraph)))
  1217. (attr-plist (when attr (read attr)))
  1218. (size (org-odt-image-size-from-file
  1219. src (plist-get attr-plist :width)
  1220. (plist-get attr-plist :height)
  1221. (plist-get attr-plist :scale) nil embed-as))
  1222. (width (car size)) (height (cdr size)))
  1223. (cond
  1224. ((not (or caption label))
  1225. (case embed-as
  1226. (paragraph (org-odt-format-entity "DisplayImage" href width height))
  1227. (character (org-odt-format-entity "InlineImage" href width height))
  1228. (t (error "Unknown value for embed-as %S" embed-as))))
  1229. (t
  1230. (org-odt-format-entity
  1231. "CaptionedDisplayImage" href width height caption label))))))
  1232. (defun org-odt-format-frame (text width height style &optional
  1233. extra anchor-type)
  1234. (let ((frame-attrs
  1235. (concat
  1236. (if width (format " svg:width=\"%0.2fcm\"" width) "")
  1237. (if height (format " svg:height=\"%0.2fcm\"" height) "")
  1238. extra
  1239. (format " text:anchor-type=\"%s\"" (or anchor-type "paragraph")))))
  1240. (org-odt-format-tags
  1241. '("<draw:frame draw:style-name=\"%s\"%s>" . "</draw:frame>")
  1242. text style frame-attrs)))
  1243. (defun org-odt-format-textbox (text width height style &optional
  1244. extra anchor-type)
  1245. (org-odt-format-frame
  1246. (org-odt-format-tags
  1247. '("<draw:text-box %s>" . "</draw:text-box>")
  1248. text (concat (format " fo:min-height=\"%0.2fcm\"" (or height .2))
  1249. (format " fo:min-width=\"%0.2fcm\"" (or width .2))))
  1250. width nil style extra anchor-type))
  1251. (defun org-odt-format-inlinetask (heading content
  1252. &optional todo priority tags)
  1253. (org-odt-format-stylized-paragraph
  1254. nil (org-odt-format-textbox
  1255. (concat (org-odt-format-stylized-paragraph
  1256. "OrgInlineTaskHeading"
  1257. (org-lparse-format
  1258. 'HEADLINE (concat (org-lparse-format-todo todo) " " heading)
  1259. nil tags))
  1260. content) nil nil "OrgInlineTaskFrame" " style:rel-width=\"100%\"")))
  1261. (defvar org-odt-entity-frame-styles
  1262. '(("InlineImage" "Figure" ("OrgInlineImage" nil "as-char"))
  1263. ("DisplayImage" "Figure" ("OrgDisplayImage" nil "paragraph"))
  1264. ("CaptionedDisplayImage" "Figure"
  1265. ("OrgCaptionedImage"
  1266. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  1267. ("OrgImageCaptionFrame"))
  1268. ("InlineFormula" "Equation" ("OrgInlineFormula" nil "as-char"))
  1269. ("DisplayFormula" "Equation" ("OrgDisplayFormula" nil "as-char"))
  1270. ("CaptionedDisplayFormula" "Equation"
  1271. ("OrgCaptionedFormula" nil "paragraph")
  1272. ("OrgFormulaCaptionFrame" nil "as-char"))))
  1273. (defun org-odt-format-entity (entity href width height &optional caption label)
  1274. (let* ((entity-style (assoc entity org-odt-entity-frame-styles))
  1275. (entity-frame (apply 'org-odt-format-frame
  1276. href width height (nth 2 entity-style))))
  1277. (if (not (or caption label)) entity-frame
  1278. (apply 'org-odt-format-textbox
  1279. (org-odt-format-stylized-paragraph
  1280. 'illustration
  1281. (concat entity-frame (org-odt-format-entity-caption
  1282. label caption (nth 1 entity-style))))
  1283. width height (nth 3 entity-style)))))
  1284. (defvar org-odt-embedded-images-count 0)
  1285. (defun org-odt-copy-image-file (path)
  1286. "Returns the internal name of the file"
  1287. (let* ((image-type (file-name-extension path))
  1288. (media-type (format "image/%s" image-type))
  1289. (src-file (expand-file-name
  1290. path (file-name-directory org-current-export-file)))
  1291. (target-dir "Images/")
  1292. (target-file
  1293. (format "%s%04d.%s" target-dir
  1294. (incf org-odt-embedded-images-count) image-type)))
  1295. (when (not org-lparse-to-buffer)
  1296. (message "Embedding %s as %s ..."
  1297. (substring-no-properties path) target-file)
  1298. (when (= 1 org-odt-embedded-images-count)
  1299. (make-directory target-dir)
  1300. (org-odt-create-manifest-file-entry "" target-dir))
  1301. (copy-file src-file target-file 'overwrite)
  1302. (org-odt-create-manifest-file-entry media-type target-file))
  1303. target-file))
  1304. (defvar org-export-odt-image-size-probe-method
  1305. '(emacs imagemagick force)
  1306. "Ordered list of methods by for determining size of an embedded
  1307. image.")
  1308. (defvar org-export-odt-default-image-sizes-alist
  1309. '(("character" . (5 . 0.4))
  1310. ("paragraph" . (5 . 5)))
  1311. "Hardcoded image dimensions one for each of the anchor
  1312. methods.")
  1313. (defun org-odt-do-image-size (probe-method file &optional dpi anchor-type)
  1314. (setq dpi (or dpi org-export-odt-pixels-per-inch))
  1315. (setq anchor-type (or anchor-type "paragraph"))
  1316. (flet ((size-in-cms (size-in-pixels)
  1317. (flet ((pixels-to-cms (pixels)
  1318. (let* ((cms-per-inch 2.54)
  1319. (inches (/ pixels dpi)))
  1320. (* cms-per-inch inches))))
  1321. (and size-in-pixels
  1322. (cons (pixels-to-cms (car size-in-pixels))
  1323. (pixels-to-cms (cdr size-in-pixels)))))))
  1324. (case probe-method
  1325. (emacs
  1326. (size-in-cms (ignore-errors (image-size (create-image file) 'pixels))))
  1327. (imagemagick
  1328. (size-in-cms
  1329. (let ((dim (shell-command-to-string
  1330. (format "identify -format \"%%w:%%h\" \"%s\"" file))))
  1331. (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
  1332. (cons (string-to-number (match-string 1 dim))
  1333. (string-to-number (match-string 2 dim)))))))
  1334. (t
  1335. (cdr (assoc-string anchor-type
  1336. org-export-odt-default-image-sizes-alist))))))
  1337. (defun org-odt-image-size-from-file (file &optional user-width
  1338. user-height scale dpi embed-as)
  1339. (unless (file-name-absolute-p file)
  1340. (setq file (expand-file-name
  1341. file (file-name-directory org-current-export-file))))
  1342. (let* (size width height)
  1343. (unless (and user-height user-width)
  1344. (loop for probe-method in org-export-odt-image-size-probe-method
  1345. until size
  1346. do (setq size (org-odt-do-image-size
  1347. probe-method file dpi embed-as)))
  1348. (or size (error "Cannot determine Image size. Aborting ..."))
  1349. (setq width (car size) height (cdr size)))
  1350. (cond
  1351. (scale
  1352. (setq width (* width scale) height (* height scale)))
  1353. ((and user-height user-width)
  1354. (setq width user-width height user-height))
  1355. (user-height
  1356. (setq width (* user-height (/ width height)) height user-height))
  1357. (user-width
  1358. (setq height (* user-width (/ height width)) width user-width))
  1359. (t (ignore)))
  1360. (cons width height)))
  1361. (defvar org-odt-entity-labels-alist nil
  1362. "Associate Labels with the Labelled entities.
  1363. Each element of the alist is of the form (LABEL-NAME
  1364. CATEGORY-NAME SEQNO). LABEL-NAME is same as that specified by
  1365. \"#+LABEL: ...\" line. CATEGORY-NAME is the type of the entity
  1366. that LABEL-NAME is attached to. CATEGORY-NAME can be one of
  1367. \"Table\", \"Figure\" or \"Equation\". SEQNO is the unique
  1368. number assigned to the referenced entity on a per-CATEGORY basis.
  1369. It is generated sequentially and is 1-based.
  1370. Update this alist with `org-odt-add-label-definition' and
  1371. retrieve an entry with `org-odt-get-label-definition'.")
  1372. (defvar org-odt-entity-counts-plist nil
  1373. "Plist of running counters of SEQNOs for each of the CATEGORY-NAMEs.
  1374. See `org-odt-entity-labels-alist' for known CATEGORY-NAMEs.")
  1375. (defvar org-odt-label-def-ref-spec
  1376. '(("Equation" "(%n)" "text" "(%n)")
  1377. ("" "%e %n%c" "category-and-value" "%e %n"))
  1378. "Specify how labels are applied and referenced.
  1379. This is an alist where each element is of the form (CATEGORY-NAME
  1380. LABEL-APPLY-FMT LABEL-REF-MODE LABEL-REF-FMT). CATEGORY-NAME is
  1381. as defined in `org-odt-entity-labels-alist'. It can additionally
  1382. be an empty string in which case it is used as a catch-all
  1383. specifier.
  1384. LABEL-APPLY-FMT is used for applying labels and captions. It may
  1385. contain following specifiers - %e, %n and %c. %e is replaced
  1386. with the CATEGORY-NAME. %n is replaced with \"<text:sequence
  1387. ...> SEQNO </text:sequence>\". %c is replaced with CAPTION. See
  1388. `org-odt-format-label-definition'.
  1389. LABEL-REF-MODE and LABEL-REF-FMT are used for generating the
  1390. following label reference - \"<text:sequence-ref
  1391. text:reference-format=\"LABEL-REF-MODE\" ...> LABEL-REF-FMT
  1392. </text:sequence-ref>\". LABEL-REF-FMT may contain following
  1393. specifiers - %e and %n. %e is replaced with the CATEGORY-NAME. %n is
  1394. replaced with SEQNO. See `org-odt-format-label-reference'.")
  1395. (defun org-odt-add-label-definition (label category)
  1396. "Return (SEQNO . LABEL-APPLY-FMT).
  1397. See `org-odt-label-def-ref-spec'."
  1398. (setq label (substring-no-properties label))
  1399. (let (seqno label-props fmt (category-sym (intern category)))
  1400. (setq seqno (1+ (plist-get org-odt-entity-counts-plist category-sym))
  1401. org-odt-entity-counts-plist (plist-put org-odt-entity-counts-plist
  1402. category-sym seqno)
  1403. fmt (cadr (or (assoc-string category org-odt-label-def-ref-spec t)
  1404. (assoc-string "" org-odt-label-def-ref-spec t)))
  1405. label-props (list label category seqno))
  1406. (push label-props org-odt-entity-labels-alist)
  1407. (cons seqno fmt)))
  1408. (defun org-odt-get-label-definition (label)
  1409. "Return (LABEL-NAME CATEGORY-NAME SEQNO LABEL-REF-MODE LABEL-REF-FMT).
  1410. See `org-odt-entity-labels-alist' and
  1411. `org-odt-label-def-ref-spec'."
  1412. (let* ((label-props (assoc label org-odt-entity-labels-alist))
  1413. (category (nth 1 label-props)))
  1414. (append label-props
  1415. (cddr (or (assoc-string category org-odt-label-def-ref-spec t)
  1416. (assoc-string "" org-odt-label-def-ref-spec t))))))
  1417. (defun org-odt-format-label-definition (label category caption)
  1418. (assert label)
  1419. (let* ((label-props (org-odt-add-label-definition label category))
  1420. (seqno (car label-props))
  1421. (fmt (cdr label-props)))
  1422. (or (format-spec
  1423. fmt
  1424. `((?e . ,category)
  1425. (?n . ,(org-odt-format-tags
  1426. '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" . "</text:sequence>")
  1427. (format "%d" seqno) label category category))
  1428. (?c . ,(or (and caption (concat ": " caption)) ""))))
  1429. caption "")))
  1430. (defun org-odt-format-label-reference (label category seqno fmt1 fmt2)
  1431. (assert label)
  1432. (save-match-data
  1433. (org-odt-format-tags
  1434. '("<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">"
  1435. . "</text:sequence-ref>")
  1436. (format-spec fmt2 `((?e . ,category)
  1437. (?n . ,(format "%d" seqno)))) fmt1 label)))
  1438. (defun org-odt-fixup-label-references ()
  1439. (goto-char (point-min))
  1440. (while (re-search-forward
  1441. "<text:sequence-ref text:ref-name=\"\\([^\"]+\\)\"/>" nil t)
  1442. (let* ((label (match-string 1)))
  1443. (replace-match
  1444. (apply 'org-odt-format-label-reference
  1445. (org-odt-get-label-definition label)) t t))))
  1446. (defun org-odt-format-entity-caption (label caption category)
  1447. (or (and label (org-odt-format-label-definition label category caption))
  1448. caption ""))
  1449. (defun org-odt-format-tags (tag text &rest args)
  1450. (let ((prefix (when org-lparse-encode-pending "@"))
  1451. (suffix (when org-lparse-encode-pending "@")))
  1452. (apply 'org-lparse-format-tags tag text prefix suffix args)))
  1453. (defun org-odt-init-outfile (filename)
  1454. (unless (executable-find "zip")
  1455. ;; Not at all OSes ship with zip by default
  1456. (error "Executable \"zip\" needed for creating OpenDocument files"))
  1457. (let* ((outdir (make-temp-file org-export-odt-tmpdir-prefix t))
  1458. (content-file (expand-file-name "content.xml" outdir)))
  1459. ;; init conten.xml
  1460. (with-current-buffer (find-file-noselect content-file t))
  1461. ;; reset variables
  1462. (setq org-odt-manifest-file-entries nil
  1463. org-odt-embedded-images-count 0
  1464. org-odt-embedded-formulas-count 0
  1465. org-odt-entity-labels-alist nil
  1466. org-odt-entity-counts-plist (list 'Table 0 'Equation 0 'Figure 0))
  1467. content-file))
  1468. (defcustom org-export-odt-prettify-xml nil
  1469. "Specify whether or not the xml output should be prettified.
  1470. When this option is turned on, `indent-region' is run on all
  1471. component xml buffers before they are saved. Turn this off for
  1472. regular use. Turn this on if you need to examine the xml
  1473. visually."
  1474. :group 'org-export-odt
  1475. :type 'boolean)
  1476. (defvar hfy-user-sheet-assoc) ; bound during org-do-lparse
  1477. (defun org-odt-save-as-outfile (target opt-plist)
  1478. ;; create mimetype file
  1479. (write-region "application/vnd.oasis.opendocument.text" nil
  1480. (expand-file-name "mimetype"))
  1481. ;; write meta file
  1482. (org-odt-update-meta-file opt-plist)
  1483. ;; write styles file
  1484. (org-odt-copy-styles-file)
  1485. ;; Update styles.xml - take care of outline numbering
  1486. (with-current-buffer
  1487. (find-file-noselect (expand-file-name "styles.xml") t)
  1488. ;; Don't make automatic backup of styles.xml file. This setting
  1489. ;; prevents the backedup styles.xml file from being zipped in to
  1490. ;; odt file. This is more of a hackish fix. Better alternative
  1491. ;; would be to fix the zip command so that the output odt file
  1492. ;; includes only the needed files and excludes any auto-generated
  1493. ;; extra files like backups and auto-saves etc etc. Note that
  1494. ;; currently the zip command zips up the entire temp directory so
  1495. ;; that any auto-generated files created under the hood ends up in
  1496. ;; the resulting odt file.
  1497. (set (make-local-variable 'backup-inhibited) t)
  1498. ;; Import local setting of `org-export-with-section-numbers'
  1499. (org-lparse-bind-local-variables opt-plist)
  1500. (org-odt-configure-outline-numbering
  1501. (if org-export-with-section-numbers org-export-headline-levels 0)))
  1502. ;; Write custom stlyes for source blocks
  1503. (org-odt-insert-custom-styles-for-srcblocks
  1504. (mapconcat
  1505. (lambda (style)
  1506. (format " %s\n" (cddr style)))
  1507. hfy-user-sheet-assoc ""))
  1508. ;; create a manifest entry for content.xml
  1509. (org-odt-create-manifest-file-entry
  1510. "application/vnd.oasis.opendocument.text" "/" "1.2")
  1511. (org-odt-create-manifest-file-entry "text/xml" "content.xml")
  1512. ;; write out the manifest entries before zipping
  1513. (org-odt-write-manifest-file)
  1514. (let ((xml-files '("mimetype" "META-INF/manifest.xml" "content.xml"
  1515. "meta.xml" "styles.xml"))
  1516. (zipdir default-directory))
  1517. (message "Switching to directory %s" (expand-file-name zipdir))
  1518. ;; save all xml files
  1519. (mapc (lambda (file)
  1520. (with-current-buffer
  1521. (find-file-noselect (expand-file-name file) t)
  1522. ;; prettify output if needed
  1523. (when org-export-odt-prettify-xml
  1524. (indent-region (point-min) (point-max)))
  1525. (save-buffer 0)))
  1526. xml-files)
  1527. (let* ((target-name (file-name-nondirectory target))
  1528. (target-dir (file-name-directory target))
  1529. (cmds `(("zip" "-mX0" ,target-name "mimetype")
  1530. ("zip" "-rmTq" ,target-name "."))))
  1531. (when (file-exists-p target)
  1532. ;; FIXME: If the file is locked this throws a cryptic error
  1533. (delete-file target))
  1534. (let ((coding-system-for-write 'no-conversion) exitcode)
  1535. (message "Creating odt file...")
  1536. (mapc
  1537. (lambda (cmd)
  1538. (message "Running %s" (mapconcat 'identity cmd " "))
  1539. (setq exitcode
  1540. (apply 'call-process (car cmd) nil nil nil (cdr cmd)))
  1541. (or (zerop exitcode)
  1542. (error "Unable to create odt file (%S)" exitcode)))
  1543. cmds))
  1544. ;; move the file from outdir to target-dir
  1545. (rename-file target-name target-dir)
  1546. ;; kill all xml buffers
  1547. (mapc (lambda (file)
  1548. (kill-buffer
  1549. (find-file-noselect (expand-file-name file zipdir) t)))
  1550. xml-files)
  1551. (delete-directory zipdir)))
  1552. (message "Created %s" target)
  1553. (set-buffer (find-file-noselect target t)))
  1554. (defun org-odt-format-date (date)
  1555. (let ((warning-msg
  1556. "OpenDocument files require that dates be in ISO-8601 format. Please review your DATE options for compatibility."))
  1557. ;; If the user is not careful with the date specification, an
  1558. ;; invalid meta.xml will be emitted.
  1559. ;; For now honor user's diktat and let him off with a warning
  1560. ;; message. This is OK as LibreOffice (and possibly other
  1561. ;; apps) doesn't deem this deviation as critical and continue
  1562. ;; to load the file.
  1563. ;; FIXME: Surely there a better way to handle this. Revisit this
  1564. ;; later.
  1565. (cond
  1566. ((and date (string-match "%" date))
  1567. ;; Honor user's diktat. See comments above
  1568. (org-lparse-warn warning-msg)
  1569. (format-time-string date))
  1570. (date
  1571. ;; Honor user's diktat. See comments above
  1572. (org-lparse-warn warning-msg)
  1573. date)
  1574. (t
  1575. ;; ISO 8601 format
  1576. (let ((stamp (format-time-string "%Y-%m-%dT%H:%M:%S%z")))
  1577. (format "%s:%s" (substring stamp 0 -2) (substring stamp -2)))))))
  1578. (defconst org-odt-manifest-file-entry-tag
  1579. "
  1580. <manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
  1581. (defvar org-odt-manifest-file-entries nil)
  1582. (defun org-odt-create-manifest-file-entry (&rest args)
  1583. (push args org-odt-manifest-file-entries))
  1584. (defun org-odt-write-manifest-file ()
  1585. (make-directory "META-INF")
  1586. (let ((manifest-file (expand-file-name "META-INF/manifest.xml")))
  1587. (write-region
  1588. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  1589. <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n"
  1590. nil manifest-file)
  1591. (mapc
  1592. (lambda (file-entry)
  1593. (let* ((version (nth 2 file-entry))
  1594. (extra (if version
  1595. (format " manifest:version=\"%s\"" version)
  1596. "")))
  1597. (write-region
  1598. (format org-odt-manifest-file-entry-tag
  1599. (nth 0 file-entry) (nth 1 file-entry) extra)
  1600. nil manifest-file t))) org-odt-manifest-file-entries)
  1601. (write-region "\n</manifest:manifest>" nil manifest-file t)))
  1602. (defun org-odt-update-meta-file (opt-plist)
  1603. (let ((date (org-odt-format-date (plist-get opt-plist :date)))
  1604. (author (or (plist-get opt-plist :author) ""))
  1605. (email (plist-get opt-plist :email))
  1606. (keywords (plist-get opt-plist :keywords))
  1607. (description (plist-get opt-plist :description))
  1608. (title (plist-get opt-plist :title)))
  1609. (write-region
  1610. (concat
  1611. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  1612. <office:document-meta
  1613. xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
  1614. xmlns:xlink=\"http://www.w3.org/1999/xlink\"
  1615. xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
  1616. xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
  1617. xmlns:ooo=\"http://openoffice.org/2004/office\"
  1618. office:version=\"1.2\">
  1619. <office:meta>" "\n"
  1620. (org-odt-format-tags '("<dc:creator>" . "</dc:creator>") author)
  1621. (org-odt-format-tags
  1622. '("\n<meta:initial-creator>" . "</meta:initial-creator>") author)
  1623. (org-odt-format-tags '("\n<dc:date>" . "</dc:date>") date)
  1624. (org-odt-format-tags
  1625. '("\n<meta:creation-date>" . "</meta:creation-date>") date)
  1626. (org-odt-format-tags '("\n<meta:generator>" . "</meta:generator>")
  1627. (when org-export-creator-info
  1628. (format "Org-%s/Emacs-%s"
  1629. org-version emacs-version)))
  1630. (org-odt-format-tags '("\n<meta:keyword>" . "</meta:keyword>") keywords)
  1631. (org-odt-format-tags '("\n<dc:subject>" . "</dc:subject>") description)
  1632. (org-odt-format-tags '("\n<dc:title>" . "</dc:title>") title)
  1633. "\n"
  1634. " </office:meta>" "</office:document-meta>")
  1635. nil (expand-file-name "meta.xml")))
  1636. ;; create a manifest entry for meta.xml
  1637. (org-odt-create-manifest-file-entry "text/xml" "meta.xml"))
  1638. (defun org-odt-finalize-outfile ()
  1639. (org-odt-delete-empty-paragraphs))
  1640. (defun org-odt-delete-empty-paragraphs ()
  1641. (goto-char (point-min))
  1642. (let ((open "<text:p[^>]*>")
  1643. (close "</text:p>"))
  1644. (while (re-search-forward (format "%s[ \r\n\t]*%s" open close) nil t)
  1645. (replace-match ""))))
  1646. (defun org-odt-get (what &optional opt-plist)
  1647. (case what
  1648. (BACKEND 'odt)
  1649. (EXPORT-DIR (org-export-directory :html opt-plist))
  1650. (FILE-NAME-EXTENSION "odt")
  1651. (EXPORT-BUFFER-NAME "*Org ODT Export*")
  1652. (ENTITY-CONTROL org-odt-entity-control-callbacks-alist)
  1653. (ENTITY-FORMAT org-odt-entity-format-callbacks-alist)
  1654. (INIT-METHOD 'org-odt-init-outfile)
  1655. (FINAL-METHOD 'org-odt-finalize-outfile)
  1656. (SAVE-METHOD 'org-odt-save-as-outfile)
  1657. ;; (OTHER-BACKENDS) ; see note in `org-xhtml-get'
  1658. ;; (CONVERT-METHOD) ; see note in `org-xhtml-get'
  1659. (TOPLEVEL-HLEVEL 1)
  1660. (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps)
  1661. (INLINE-IMAGES 'maybe)
  1662. (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
  1663. (PLAIN-TEXT-MAP '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
  1664. (TABLE-FIRST-COLUMN-AS-LABELS nil)
  1665. (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY "," 'superscript))
  1666. (CODING-SYSTEM-FOR-WRITE 'utf-8)
  1667. (CODING-SYSTEM-FOR-SAVE 'utf-8)
  1668. (t (error "Unknown property: %s" what))))
  1669. (defvar org-lparse-latex-fragment-fallback) ; set by org-do-lparse
  1670. (defvar org-lparse-opt-plist) ; bound during org-do-lparse
  1671. (defun org-export-odt-do-preprocess-latex-fragments ()
  1672. "Convert LaTeX fragments to images."
  1673. (let* ((latex-frag-opt (plist-get org-lparse-opt-plist :LaTeX-fragments))
  1674. (latex-frag-opt ; massage the options
  1675. (or (and (member latex-frag-opt '(mathjax t))
  1676. (not (and (fboundp 'org-format-latex-mathml-available-p)
  1677. (org-format-latex-mathml-available-p)))
  1678. (prog1 org-lparse-latex-fragment-fallback
  1679. (org-lparse-warn
  1680. (concat
  1681. "LaTeX to MathML converter not available. "
  1682. (format "Using %S instead."
  1683. org-lparse-latex-fragment-fallback)))))
  1684. latex-frag-opt))
  1685. cache-dir display-msg)
  1686. (cond
  1687. ((eq latex-frag-opt 'dvipng)
  1688. (setq cache-dir "ltxpng/")
  1689. (setq display-msg "Creating LaTeX image %s"))
  1690. ((member latex-frag-opt '(mathjax t))
  1691. (setq latex-frag-opt 'mathml)
  1692. (setq cache-dir "ltxmathml/")
  1693. (setq display-msg "Creating MathML formula %s")))
  1694. (when (and org-current-export-file)
  1695. (org-format-latex
  1696. (concat cache-dir (file-name-sans-extension
  1697. (file-name-nondirectory org-current-export-file)))
  1698. org-current-export-dir nil display-msg
  1699. nil nil latex-frag-opt))))
  1700. (defun org-export-odt-preprocess-latex-fragments ()
  1701. (when (equal org-export-current-backend 'odt)
  1702. (org-export-odt-do-preprocess-latex-fragments)))
  1703. (defun org-export-odt-preprocess-label-references ()
  1704. (goto-char (point-min))
  1705. (let (label label-components category value pretty-label)
  1706. (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t)
  1707. (org-if-unprotected-at (match-beginning 1)
  1708. (replace-match
  1709. (let ((org-lparse-encode-pending t)
  1710. (label (match-string 1)))
  1711. ;; markup generated below is mostly an eye-candy. At
  1712. ;; pre-processing stage, there is no information on which
  1713. ;; entity a label reference points to. The actual markup
  1714. ;; is generated as part of `org-odt-fixup-label-references'
  1715. ;; which gets called at the fag end of export. By this
  1716. ;; time we would have seen and collected all the label
  1717. ;; definitions in `org-odt-entity-labels-alist'.
  1718. (org-odt-format-tags
  1719. "<text:sequence-ref text:ref-name=\"%s\"/>" "" label)) t t)))))
  1720. ;; process latex fragments as part of
  1721. ;; `org-export-preprocess-after-blockquote-hook'. Note that this hook
  1722. ;; is the one that is closest and well before the call to
  1723. ;; `org-export-attach-captions-and-attributes' in
  1724. ;; `org-export-preprocess-stirng'. The above arrangement permits
  1725. ;; captions, labels and attributes to be attached to png images
  1726. ;; generated out of latex equations.
  1727. (add-hook 'org-export-preprocess-after-blockquote-hook
  1728. 'org-export-odt-preprocess-latex-fragments)
  1729. (defun org-export-odt-preprocess (parameters)
  1730. (org-export-odt-preprocess-label-references))
  1731. (declare-function archive-zip-extract "arc-mode.el" (archive name))
  1732. (defun org-odt-zip-extract-one (archive member &optional target)
  1733. (require 'arc-mode)
  1734. (let* ((target (or target default-directory))
  1735. (archive (expand-file-name archive))
  1736. (archive-zip-extract
  1737. (list "unzip" "-qq" "-o" "-d" target))
  1738. exit-code command-output)
  1739. (setq command-output
  1740. (with-temp-buffer
  1741. (setq exit-code (archive-zip-extract archive member))
  1742. (buffer-string)))
  1743. (unless (zerop exit-code)
  1744. (message command-output)
  1745. (error "Extraction failed"))))
  1746. (defun org-odt-zip-extract (archive members &optional target)
  1747. (when (atom members) (setq members (list members)))
  1748. (mapc (lambda (member)
  1749. (org-odt-zip-extract-one archive member target))
  1750. members))
  1751. (defun org-odt-copy-styles-file (&optional styles-file)
  1752. ;; Non-availability of styles.xml is not a critical error. For now
  1753. ;; throw an error purely for aesthetic reasons.
  1754. (setq styles-file (or styles-file
  1755. org-export-odt-styles-file
  1756. (expand-file-name "styles/OrgOdtStyles.xml"
  1757. org-odt-data-dir)
  1758. (error "org-odt: Missing styles file?")))
  1759. (cond
  1760. ((listp styles-file)
  1761. (let ((archive (nth 0 styles-file))
  1762. (members (nth 1 styles-file)))
  1763. (org-odt-zip-extract archive members)
  1764. (mapc
  1765. (lambda (member)
  1766. (when (org-file-image-p member)
  1767. (let* ((image-type (file-name-extension member))
  1768. (media-type (format "image/%s" image-type)))
  1769. (org-odt-create-manifest-file-entry media-type member))))
  1770. members)))
  1771. ((and (stringp styles-file) (file-exists-p styles-file))
  1772. (let ((styles-file-type (file-name-extension styles-file)))
  1773. (cond
  1774. ((string= styles-file-type "xml")
  1775. (copy-file styles-file "styles.xml" t))
  1776. ((member styles-file-type '("odt" "ott"))
  1777. (org-odt-zip-extract styles-file "styles.xml")))))
  1778. (t
  1779. (error (format "Invalid specification of styles.xml file: %S"
  1780. org-export-odt-styles-file))))
  1781. ;; create a manifest entry for styles.xml
  1782. (org-odt-create-manifest-file-entry "text/xml" "styles.xml"))
  1783. (defvar org-export-odt-factory-settings
  1784. "d4328fb9d1b6cb211d4320ff546829f26700dc5e"
  1785. "SHA1 hash of OrgOdtStyles.xml.")
  1786. (defun org-odt-configure-outline-numbering (level)
  1787. "Outline numbering is retained only upto LEVEL.
  1788. To disable outline numbering pass a LEVEL of 0."
  1789. (goto-char (point-min))
  1790. (let ((regex
  1791. "<text:outline-level-style\\(.*\\)text:level=\"\\([^\"]*\\)\"\\(.*\\)>")
  1792. (replacement
  1793. "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
  1794. (while (re-search-forward regex nil t)
  1795. (when (> (string-to-number (match-string 2)) level)
  1796. (replace-match replacement t nil))))
  1797. (save-buffer 0))
  1798. (provide 'org-odt)
  1799. ;;; org-odt.el ends here