org-odt.el 78 KB

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