org-odt.el 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461
  1. ;;; org-odt.el --- OpenDocumentText export for Org-mode
  2. ;; Copyright (C) 2010-2011 Free Software Foundation, Inc.
  3. ;; Author: Jambunathan K <kjambunathan at gmail dot com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;;
  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
  24. (require 'cl)
  25. ;; htmlfontify.el was introduce in Emacs 23.2
  26. (when (>= (string-to-number emacs-version) 23.2)
  27. (require 'htmlfontify)))
  28. (require 'org-lparse)
  29. (defgroup org-export-odt nil
  30. "Options specific for ODT export of Org-mode files."
  31. :tag "Org Export ODT"
  32. :group 'org-export)
  33. (defun org-odt-end-export ()
  34. (org-odt-fixup-label-references)
  35. ;; remove empty paragraphs
  36. (goto-char (point-min))
  37. (while (re-search-forward
  38. "<text:p\\( text:style-name=\"Text_20_body\"\\)?>[ \r\n\t]*</text:p>"
  39. nil t)
  40. (replace-match ""))
  41. (goto-char (point-min))
  42. ;; Convert whitespace place holders
  43. (goto-char (point-min))
  44. (let (beg end n)
  45. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  46. (setq n (get-text-property beg 'org-whitespace)
  47. end (next-single-property-change beg 'org-whitespace))
  48. (goto-char beg)
  49. (delete-region beg end)
  50. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  51. (make-string n ?x)))))
  52. ;; Remove empty lines at the beginning of the file.
  53. (goto-char (point-min))
  54. (when (looking-at "\\s-+\n") (replace-match ""))
  55. ;; Remove display properties
  56. (remove-text-properties (point-min) (point-max) '(display t)))
  57. (defvar org-odt-suppress-xref nil)
  58. (defconst org-export-odt-special-string-regexps
  59. '(("\\\\-" . "&#x00ad;\\1") ; shy
  60. ("---\\([^-]\\)" . "&#x2014;\\1") ; mdash
  61. ("--\\([^-]\\)" . "&#x2013;\\1") ; ndash
  62. ("\\.\\.\\." . "&#x2026;")) ; hellip
  63. "Regular expressions for special string conversion.")
  64. (defconst org-odt-lib-dir (file-name-directory load-file-name))
  65. (defconst org-odt-styles-dir
  66. (let* ((styles-dir1 (expand-file-name "../etc/styles/" org-odt-lib-dir))
  67. (styles-dir2 (expand-file-name "./etc/styles/" org-odt-lib-dir))
  68. (styles-dir
  69. (catch 'styles-dir
  70. (mapc (lambda (styles-dir)
  71. (when (and (file-readable-p
  72. (expand-file-name
  73. "OrgOdtContentTemplate.xml" styles-dir))
  74. (file-readable-p
  75. (expand-file-name
  76. "OrgOdtStyles.xml" styles-dir)))
  77. (throw 'styles-dir styles-dir)))
  78. (list styles-dir1 styles-dir2))
  79. nil)))
  80. (unless styles-dir
  81. (error "Cannot find factory styles file. Check package dir layout"))
  82. styles-dir)
  83. "Directory that holds auxiliary XML files used by the ODT exporter.
  84. This directory contains the following XML files -
  85. \"OrgOdtStyles.xml\" and \"OrgOdtContentTemplate.xml\". These
  86. XML files are used as the default values of
  87. `org-export-odt-styles-file' and
  88. `org-export-odt-content-template-file'.")
  89. (defcustom org-export-odt-schema-dir
  90. (let ((schema-dir (expand-file-name
  91. "../contrib/odt/etc/schema/" org-odt-lib-dir)))
  92. (if (and (file-readable-p
  93. (expand-file-name "od-manifest-schema-v1.2-cs01.rnc" schema-dir))
  94. (file-readable-p
  95. (expand-file-name "od-schema-v1.2-cs01.rnc" schema-dir))
  96. (file-readable-p
  97. (expand-file-name "schemas.xml" schema-dir)))
  98. schema-dir
  99. (prog1 nil (message "Unable to locate OpenDocument schema files."))))
  100. "Directory that contains OpenDocument schema files.
  101. This directory contains rnc files for OpenDocument schema. It
  102. also contains a \"schemas.xml\" that can be added to
  103. `rng-schema-locating-files' for auto validation of OpenDocument
  104. XML files. See also `rng-nxml-auto-validate-flag'."
  105. :type '(choice
  106. (const :tag "Not set" nil)
  107. (directory :tag "Schema directory"))
  108. :group 'org-export-odt
  109. :set
  110. (lambda (var value)
  111. "Set `org-export-odt-schema-dir'.
  112. Also add it to `rng-schema-locating-files'."
  113. (let ((schema-dir value))
  114. (set var
  115. (if (and
  116. (file-readable-p
  117. (expand-file-name "od-manifest-schema-v1.2-cs01.rnc" schema-dir))
  118. (file-readable-p
  119. (expand-file-name "od-schema-v1.2-cs01.rnc" schema-dir))
  120. (file-readable-p
  121. (expand-file-name "schemas.xml" schema-dir)))
  122. schema-dir
  123. (prog1 nil
  124. (message "Warning (org-odt): Unable to locate OpenDocument schema files.")))))
  125. (when org-export-odt-schema-dir
  126. (eval-after-load 'rng-loc
  127. '(add-to-list 'rng-schema-locating-files
  128. (expand-file-name "schemas.xml"
  129. org-export-odt-schema-dir))))))
  130. (defvar org-odt-file-extensions
  131. '(("odt" . "OpenDocument Text")
  132. ("ott" . "OpenDocument Text Template")
  133. ("odm" . "OpenDocument Master Document")
  134. ("ods" . "OpenDocument Spreadsheet")
  135. ("ots" . "OpenDocument Spreadsheet Template")
  136. ("odg" . "OpenDocument Drawing (Graphics)")
  137. ("otg" . "OpenDocument Drawing Template")
  138. ("odp" . "OpenDocument Presentation")
  139. ("otp" . "OpenDocument Presentation Template")
  140. ("odi" . "OpenDocument Image")
  141. ("odf" . "OpenDocument Formula")
  142. ("odc" . "OpenDocument Chart")))
  143. (mapc
  144. (lambda (desc)
  145. ;; Let Org open all OpenDocument files using system-registered app
  146. (add-to-list 'org-file-apps
  147. (cons (concat "\\." (car desc) "\\'") 'system))
  148. ;; Let Emacs open all OpenDocument files in archive mode
  149. (add-to-list 'auto-mode-alist
  150. (cons (concat "\\." (car desc) "\\'") 'archive-mode)))
  151. org-odt-file-extensions)
  152. ;; register the odt exporter with the pre-processor
  153. (add-to-list 'org-export-backends 'odt)
  154. ;; register the odt exporter with org-lparse library
  155. (org-lparse-register-backend 'odt)
  156. (defun org-odt-unload-function ()
  157. (org-lparse-unregister-backend 'odt)
  158. (remove-hook 'org-export-preprocess-after-blockquote-hook
  159. 'org-export-odt-preprocess-latex-fragments)
  160. nil)
  161. (defcustom org-export-odt-content-template-file nil
  162. "Template file for \"content.xml\".
  163. The exporter embeds the exported content just before
  164. \"</office:text>\" element.
  165. If unspecified, the file named \"OrgOdtContentTemplate.xml\"
  166. under `org-odt-styles-dir' is used."
  167. :type 'file
  168. :group 'org-export-odt)
  169. (defcustom org-export-odt-styles-file nil
  170. "Default styles file for use with ODT export.
  171. Valid values are one of:
  172. 1. nil
  173. 2. path to a styles.xml file
  174. 3. path to a *.odt or a *.ott file
  175. 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
  176. ...))
  177. In case of option 1, an in-built styles.xml is used. See
  178. `org-odt-styles-dir' for more information.
  179. In case of option 3, the specified file is unzipped and the
  180. styles.xml embedded therein is used.
  181. In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
  182. and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
  183. generated odt file. Use relative path for specifying the
  184. FILE-MEMBERS. styles.xml must be specified as one of the
  185. FILE-MEMBERS.
  186. Use options 1, 2 or 3 only if styles.xml alone suffices for
  187. achieving the desired formatting. Use option 4, if the styles.xml
  188. references additional files like header and footer images for
  189. achieving the desired formattting.
  190. Use \"#+ODT_STYLES_FILE: ...\" directive to set this variable on
  191. a per-file basis. For example,
  192. #+ODT_STYLES_FILE: \"/path/to/styles.xml\" or
  193. #+ODT_STYLES_FILE: (\"/path/to/file.ott\" (\"styles.xml\" \"image/hdr.png\"))."
  194. :group 'org-export-odt
  195. :type
  196. '(choice
  197. (const :tag "Factory settings" nil)
  198. (file :must-match t :tag "styles.xml")
  199. (file :must-match t :tag "ODT or OTT file")
  200. (list :tag "ODT or OTT file + Members"
  201. (file :must-match t :tag "ODF Text or Text Template file")
  202. (cons :tag "Members"
  203. (file :tag " Member" "styles.xml")
  204. (repeat (file :tag "Member"))))))
  205. (eval-after-load 'org-exp
  206. '(add-to-list 'org-export-inbuffer-options-extra
  207. '("ODT_STYLES_FILE" :odt-styles-file)))
  208. (defconst org-export-odt-tmpdir-prefix "%s-")
  209. (defconst org-export-odt-bookmark-prefix "OrgXref.")
  210. (defvar org-export-odt-embed-images t
  211. "Should the images be copied in to the odt file or just linked?")
  212. (defvar org-export-odt-inline-images 'maybe) ; counterpart of
  213. ; `org-export-html-inline-images'
  214. (defcustom org-export-odt-inline-image-extensions
  215. '("png" "jpeg" "jpg" "gif")
  216. "Extensions of image files that can be inlined into HTML."
  217. :type '(repeat (string :tag "Extension"))
  218. :group 'org-export-odt)
  219. (defcustom org-export-odt-pixels-per-inch display-pixels-per-inch
  220. ;; FIXME add docstring
  221. ""
  222. :type 'float
  223. :group 'org-export-odt)
  224. (defcustom org-export-odt-create-custom-styles-for-srcblocks t
  225. "Whether custom styles for colorized source blocks be automatically created.
  226. When this option is turned on, the exporter creates custom styles
  227. for source blocks based on the advice of `htmlfontify'. Creation
  228. of custom styles happen as part of `org-odt-hfy-face-to-css'.
  229. When this option is turned off exporter does not create such
  230. styles.
  231. Use the latter option if you do not want the custom styles to be
  232. based on your current display settings. It is necessary that the
  233. styles.xml already contains needed styles for colorizing to work.
  234. This variable is effective only if
  235. `org-export-odt-fontify-srcblocks' is turned on."
  236. :group 'org-export-odt
  237. :type 'boolean)
  238. (defvar org-export-odt-default-org-styles-alist
  239. '((paragraph . ((default . "Text_20_body")
  240. (fixedwidth . "OrgFixedWidthBlock")
  241. (verse . "OrgVerse")
  242. (quote . "Quotations")
  243. (blockquote . "Quotations")
  244. (center . "OrgCenter")
  245. (left . "OrgLeft")
  246. (right . "OrgRight")
  247. (title . "Heading_20_1.title")
  248. (footnote . "Footnote")
  249. (src . "OrgSrcBlock")
  250. (illustration . "Illustration")
  251. (table . "Table")
  252. (definition-term . "Text_20_body_20_bold")
  253. (horizontal-line . "Horizontal_20_Line")))
  254. (character . ((bold . "Bold")
  255. (emphasis . "Emphasis")
  256. (code . "OrgCode")
  257. (verbatim . "OrgCode")
  258. (strike . "Strikethrough")
  259. (underline . "Underline")
  260. (subscript . "OrgSubscript")
  261. (superscript . "OrgSuperscript")))
  262. (list . ((ordered . "OrgNumberedList")
  263. (unordered . "OrgBulletedList")
  264. (description . "OrgDescriptionList"))))
  265. "Default styles for various entities.")
  266. (defvar org-export-odt-org-styles-alist org-export-odt-default-org-styles-alist)
  267. (defun org-odt-get-style-name-for-entity (category &optional entity)
  268. (let ((entity (or entity 'default)))
  269. (or
  270. (cdr (assoc entity (cdr (assoc category
  271. org-export-odt-org-styles-alist))))
  272. (cdr (assoc entity (cdr (assoc category
  273. org-export-odt-default-org-styles-alist))))
  274. (error "Cannot determine style name for entity %s of type %s"
  275. entity category))))
  276. (defcustom org-export-odt-preferred-output-format nil
  277. "Automatically post-process to this format after exporting to \"odt\".
  278. Interactive commands `org-export-as-odt' and
  279. `org-export-as-odt-and-open' export first to \"odt\" format and
  280. then use `org-export-odt-convert-process' to convert the
  281. resulting document to this format. During customization of this
  282. variable, the list of valid values are populated based on
  283. `org-export-odt-convert-capabilities'."
  284. :group 'org-export-odt
  285. :type '(choice :convert-widget
  286. (lambda (w)
  287. (apply 'widget-convert (widget-type w)
  288. (eval (car (widget-get w :args)))))
  289. `((const :tag "None" nil)
  290. ,@(mapcar (lambda (c)
  291. `(const :tag ,c ,c))
  292. (org-lparse-reachable-formats "odt")))))
  293. ;;;###autoload
  294. (defun org-export-as-odt-and-open (arg)
  295. "Export the outline as ODT and immediately open it with a browser.
  296. If there is an active region, export only the region.
  297. The prefix ARG specifies how many levels of the outline should become
  298. headlines. The default is 3. Lower levels will become bulleted lists."
  299. (interactive "P")
  300. (org-lparse-and-open
  301. (or org-export-odt-preferred-output-format "odt") "odt" arg))
  302. ;;;###autoload
  303. (defun org-export-as-odt-batch ()
  304. "Call the function `org-lparse-batch'.
  305. This function can be used in batch processing as:
  306. emacs --batch
  307. --load=$HOME/lib/emacs/org.el
  308. --eval \"(setq org-export-headline-levels 2)\"
  309. --visit=MyFile --funcall org-export-as-odt-batch"
  310. (org-lparse-batch "odt"))
  311. ;;;###autoload
  312. (defun org-export-as-odt-to-buffer (arg)
  313. "Call `org-lparse-odt` with output to a temporary buffer.
  314. No file is created. The prefix ARG is passed through to `org-lparse-to-buffer'."
  315. (interactive "P")
  316. (org-lparse-to-buffer "odt" arg))
  317. ;;;###autoload
  318. (defun org-replace-region-by-odt (beg end)
  319. "Assume the current region has org-mode syntax, and convert it to ODT.
  320. This can be used in any buffer. For example, you could write an
  321. itemized list in org-mode syntax in an ODT buffer and then use this
  322. command to convert it."
  323. (interactive "r")
  324. (org-replace-region-by "odt" beg end))
  325. ;;;###autoload
  326. (defun org-export-region-as-odt (beg end &optional body-only buffer)
  327. "Convert region from BEG to END in org-mode buffer to ODT.
  328. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  329. contents, and only produce the region of converted text, useful for
  330. cut-and-paste operations.
  331. If BUFFER is a buffer or a string, use/create that buffer as a target
  332. of the converted ODT. If BUFFER is the symbol `string', return the
  333. produced ODT as a string and leave not buffer behind. For example,
  334. a Lisp program could call this function in the following way:
  335. (setq odt (org-export-region-as-odt beg end t 'string))
  336. When called interactively, the output buffer is selected, and shown
  337. in a window. A non-interactive call will only return the buffer."
  338. (interactive "r\nP")
  339. (org-lparse-region "odt" beg end body-only buffer))
  340. ;;; org-export-as-odt
  341. ;;;###autoload
  342. (defun org-export-as-odt (arg &optional hidden ext-plist
  343. to-buffer body-only pub-dir)
  344. "Export the outline as a OpenDocumentText file.
  345. If there is an active region, export only the region. The prefix
  346. ARG specifies how many levels of the outline should become
  347. headlines. The default is 3. Lower levels will become bulleted
  348. lists. HIDDEN is obsolete and does nothing.
  349. EXT-PLIST is a property list with external parameters overriding
  350. org-mode's default settings, but still inferior to file-local
  351. settings. When TO-BUFFER is non-nil, create a buffer with that
  352. name and export to that buffer. If TO-BUFFER is the symbol
  353. `string', don't leave any buffer behind but just return the
  354. resulting XML as a string. When BODY-ONLY is set, don't produce
  355. the file header and footer, simply return the content of
  356. <body>...</body>, without even the body tags themselves. When
  357. PUB-DIR is set, use this as the publishing directory."
  358. (interactive "P")
  359. (org-lparse (or org-export-odt-preferred-output-format "odt")
  360. "odt" arg hidden ext-plist to-buffer body-only pub-dir))
  361. (defvar org-odt-entity-control-callbacks-alist
  362. `((EXPORT
  363. . (org-odt-begin-export org-odt-end-export))
  364. (DOCUMENT-CONTENT
  365. . (org-odt-begin-document-content org-odt-end-document-content))
  366. (DOCUMENT-BODY
  367. . (org-odt-begin-document-body org-odt-end-document-body))
  368. (TOC
  369. . (org-odt-begin-toc org-odt-end-toc))
  370. (ENVIRONMENT
  371. . (org-odt-begin-environment org-odt-end-environment))
  372. (FOOTNOTE-DEFINITION
  373. . (org-odt-begin-footnote-definition org-odt-end-footnote-definition))
  374. (TABLE
  375. . (org-odt-begin-table org-odt-end-table))
  376. (TABLE-ROWGROUP
  377. . (org-odt-begin-table-rowgroup org-odt-end-table-rowgroup))
  378. (LIST
  379. . (org-odt-begin-list org-odt-end-list))
  380. (LIST-ITEM
  381. . (org-odt-begin-list-item org-odt-end-list-item))
  382. (OUTLINE
  383. . (org-odt-begin-outline org-odt-end-outline))
  384. (OUTLINE-TEXT
  385. . (org-odt-begin-outline-text org-odt-end-outline-text))
  386. (PARAGRAPH
  387. . (org-odt-begin-paragraph org-odt-end-paragraph)))
  388. "")
  389. (defvar org-odt-entity-format-callbacks-alist
  390. `((EXTRA-TARGETS . org-lparse-format-extra-targets)
  391. (ORG-TAGS . org-lparse-format-org-tags)
  392. (SECTION-NUMBER . org-lparse-format-section-number)
  393. (HEADLINE . org-odt-format-headline)
  394. (TOC-ENTRY . org-odt-format-toc-entry)
  395. (TOC-ITEM . org-odt-format-toc-item)
  396. (TAGS . org-odt-format-tags)
  397. (SPACES . org-odt-format-spaces)
  398. (TABS . org-odt-format-tabs)
  399. (LINE-BREAK . org-odt-format-line-break)
  400. (FONTIFY . org-odt-format-fontify)
  401. (TODO . org-lparse-format-todo)
  402. (LINK . org-odt-format-link)
  403. (INLINE-IMAGE . org-odt-format-inline-image)
  404. (ORG-LINK . org-odt-format-org-link)
  405. (HEADING . org-odt-format-heading)
  406. (ANCHOR . org-odt-format-anchor)
  407. (TABLE . org-lparse-format-table)
  408. (TABLE-ROW . org-odt-format-table-row)
  409. (TABLE-CELL . org-odt-format-table-cell)
  410. (FOOTNOTES-SECTION . ignore)
  411. (FOOTNOTE-REFERENCE . org-odt-format-footnote-reference)
  412. (HORIZONTAL-LINE . org-odt-format-horizontal-line)
  413. (COMMENT . org-odt-format-comment)
  414. (LINE . org-odt-format-line)
  415. (ORG-ENTITY . org-odt-format-org-entity))
  416. "")
  417. ;;;_. callbacks
  418. ;;;_. control callbacks
  419. ;;;_ , document body
  420. (defun org-odt-begin-office-body ()
  421. ;; automatic styles
  422. (insert-file-contents
  423. (or org-export-odt-content-template-file
  424. (expand-file-name "OrgOdtContentTemplate.xml"
  425. org-odt-styles-dir)))
  426. (goto-char (point-min))
  427. (re-search-forward "</office:text>" nil nil)
  428. (delete-region (match-beginning 0) (point-max)))
  429. ;; Following variable is let bound when `org-do-lparse' is in
  430. ;; progress. See org-html.el.
  431. (defvar org-lparse-toc)
  432. (defun org-odt-begin-document-body (opt-plist)
  433. (org-odt-begin-office-body)
  434. (let ((title (plist-get opt-plist :title)))
  435. (when title
  436. (insert
  437. (org-odt-format-stylized-paragraph 'title title))))
  438. ;; insert toc
  439. (when org-lparse-toc
  440. (insert "\n" org-lparse-toc "\n")))
  441. (defvar org-lparse-body-only) ; let bound during org-do-lparse
  442. (defvar org-lparse-to-buffer) ; let bound during org-do-lparse
  443. (defun org-odt-end-document-body (opt-plist)
  444. (unless org-lparse-body-only
  445. (org-lparse-insert-tag "</office:text>")
  446. (org-lparse-insert-tag "</office:body>")))
  447. (defun org-odt-begin-document-content (opt-plist)
  448. (ignore))
  449. (defun org-odt-end-document-content ()
  450. (org-lparse-insert-tag "</office:document-content>"))
  451. (defun org-odt-begin-outline (level1 snumber title tags
  452. target extra-targets class)
  453. (org-lparse-insert
  454. 'HEADING (org-lparse-format
  455. 'HEADLINE title extra-targets tags snumber level1)
  456. level1 target))
  457. (defun org-odt-end-outline ()
  458. (ignore))
  459. (defun org-odt-begin-outline-text (level1 snumber class)
  460. (ignore))
  461. (defun org-odt-end-outline-text ()
  462. (ignore))
  463. (defun org-odt-begin-paragraph (&optional style)
  464. (org-lparse-insert-tag
  465. "<text:p%s>" (org-odt-get-extra-attrs-for-paragraph-style style)))
  466. (defun org-odt-end-paragraph ()
  467. (org-lparse-insert-tag "</text:p>"))
  468. (defun org-odt-get-extra-attrs-for-paragraph-style (style)
  469. (let (style-name)
  470. (setq style-name
  471. (cond
  472. ((stringp style) style)
  473. ((symbolp style) (org-odt-get-style-name-for-entity
  474. 'paragraph style))))
  475. (unless style-name
  476. (error "Don't know how to handle paragraph style %s" style))
  477. (format " text:style-name=\"%s\"" style-name)))
  478. (defun org-odt-format-stylized-paragraph (style text)
  479. (org-odt-format-tags
  480. '("<text:p%s>" . "</text:p>") text
  481. (org-odt-get-extra-attrs-for-paragraph-style style)))
  482. (defvar org-lparse-opt-plist) ; bound during org-do-lparse
  483. (defun org-odt-format-author (&optional author)
  484. (when (setq author (or author (plist-get org-lparse-opt-plist :author)))
  485. (org-odt-format-tags '("<dc:creator>" . "</dc:creator>") author)))
  486. (defun org-odt-iso-date-from-org-timestamp (&optional org-ts)
  487. (save-match-data
  488. (let* ((time
  489. (and (stringp org-ts)
  490. (string-match org-ts-regexp0 org-ts)
  491. (apply 'encode-time
  492. (org-fix-decoded-time
  493. (org-parse-time-string (match-string 0 org-ts) t)))))
  494. (date (format-time-string "%Y-%m-%dT%H:%M:%S%z" time)))
  495. (format "%s:%s" (substring date 0 -2) (substring date -2)))))
  496. (defun org-odt-begin-annotation (&optional author date)
  497. (org-lparse-insert-tag "<office:annotation>")
  498. (when (setq author (org-odt-format-author author))
  499. (insert author))
  500. (insert (org-odt-format-tags
  501. '("<dc:date>" . "</dc:date>")
  502. (org-odt-iso-date-from-org-timestamp
  503. (or date (plist-get org-lparse-opt-plist :date)))))
  504. (org-lparse-begin-paragraph))
  505. (defun org-odt-end-annotation ()
  506. (org-lparse-insert-tag "</office:annotation>"))
  507. (defun org-odt-begin-environment (style env-options-plist)
  508. (case style
  509. (annotation
  510. (org-lparse-stash-save-paragraph-state)
  511. (org-odt-begin-annotation (plist-get env-options-plist 'author)
  512. (plist-get env-options-plist 'date)))
  513. ((blockquote verse center quote)
  514. (org-lparse-begin-paragraph style)
  515. (list))
  516. ((fixedwidth native)
  517. (org-lparse-end-paragraph)
  518. (list))
  519. (t (error "Unknown environment %s" style))))
  520. (defun org-odt-end-environment (style env-options-plist)
  521. (case style
  522. (annotation
  523. (org-lparse-end-paragraph)
  524. (org-odt-end-annotation)
  525. (org-lparse-stash-pop-paragraph-state))
  526. ((blockquote verse center quote)
  527. (org-lparse-end-paragraph)
  528. (list))
  529. ((fixedwidth native)
  530. (org-lparse-begin-paragraph)
  531. (list))
  532. (t (error "Unknown environment %s" style))))
  533. (defvar org-lparse-list-level) ; dynamically bound in org-do-lparse
  534. (defun org-odt-begin-list (ltype)
  535. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  536. ltype))
  537. (let* ((style-name (org-odt-get-style-name-for-entity 'list ltype))
  538. (extra (concat (when (= org-lparse-list-level 1)
  539. " text:continue-numbering=\"false\"")
  540. (when style-name
  541. (format " text:style-name=\"%s\"" style-name)))))
  542. (case ltype
  543. ((ordered unordered description)
  544. (org-lparse-end-paragraph)
  545. (org-lparse-insert-tag "<text:list%s>" extra))
  546. (t (error "Unknown list type: %s" ltype)))))
  547. (defun org-odt-end-list (ltype)
  548. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  549. ltype))
  550. (if ltype
  551. (org-lparse-insert-tag "</text:list>")
  552. (error "Unknown list type: %s" ltype)))
  553. (defun org-odt-begin-list-item (ltype &optional arg headline)
  554. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  555. ltype))
  556. (case ltype
  557. (ordered
  558. (assert (not headline) t)
  559. (let* ((counter arg) (extra ""))
  560. (org-lparse-insert-tag "<text:list-item>")
  561. (org-lparse-begin-paragraph)))
  562. (unordered
  563. (let* ((id arg) (extra ""))
  564. (org-lparse-insert-tag "<text:list-item>")
  565. (org-lparse-begin-paragraph)
  566. (insert (if headline (org-odt-format-target headline id)
  567. (org-odt-format-bookmark "" id)))))
  568. (description
  569. (assert (not headline) t)
  570. (let ((term (or arg "(no term)")))
  571. (insert
  572. (org-odt-format-tags
  573. '("<text:list-item>" . "</text:list-item>")
  574. (org-odt-format-stylized-paragraph 'definition-term term)))
  575. (org-lparse-begin-list-item 'unordered)
  576. (org-lparse-begin-list 'description)
  577. (org-lparse-begin-list-item 'unordered)))
  578. (t (error "Unknown list type"))))
  579. (defun org-odt-end-list-item (ltype)
  580. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  581. ltype))
  582. (case ltype
  583. ((ordered unordered)
  584. (org-lparse-insert-tag "</text:list-item>"))
  585. (description
  586. (org-lparse-end-list-item-1)
  587. (org-lparse-end-list 'description)
  588. (org-lparse-end-list-item-1))
  589. (t (error "Unknown list type"))))
  590. ;; Following variables are let bound when table emission is in
  591. ;; progress. See org-lparse.el.
  592. (defvar org-lparse-table-begin-marker)
  593. (defvar org-lparse-table-ncols)
  594. (defvar org-lparse-table-rowgrp-open)
  595. (defvar org-lparse-table-rownum)
  596. (defvar org-lparse-table-cur-rowgrp-is-hdr)
  597. (defvar org-lparse-table-is-styled)
  598. (defvar org-lparse-table-rowgrp-info)
  599. (defvar org-lparse-table-colalign-vector)
  600. (defvar org-odt-table-style nil
  601. "Table style specified by \"#+ATTR_ODT: <style-name>\" line.
  602. This is set during `org-odt-begin-table'.")
  603. (defvar org-odt-table-style-spec nil
  604. "Entry for `org-odt-table-style' in `org-export-odt-table-styles'.")
  605. (defcustom org-export-odt-table-styles
  606. '(("OrgEquation" "OrgEquation"
  607. ((use-first-column-styles . t)
  608. (use-last-column-styles . t))))
  609. "Specify how Table Styles should be derived from a Table Template.
  610. This is a list where each element is of the
  611. form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
  612. TABLE-STYLE-NAME is the style associated with the table through
  613. `org-odt-table-style'.
  614. TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
  615. TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
  616. below) that is included in
  617. `org-export-odt-content-template-file'.
  618. TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
  619. \"TableCell\"
  620. PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
  621. \"TableParagraph\"
  622. TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
  623. \"FirstRow\" | \"LastRow\" |
  624. \"EvenRow\" | \"OddRow\" |
  625. \"EvenColumn\" | \"OddColumn\" | \"\"
  626. where \"+\" above denotes string concatenation.
  627. TABLE-CELL-OPTIONS is an alist where each element is of the
  628. form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
  629. TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
  630. `use-last-row-styles' |
  631. `use-first-column-styles' |
  632. `use-last-column-styles' |
  633. `use-banding-rows-styles' |
  634. `use-banding-columns-styles' |
  635. `use-first-row-styles'
  636. ON-OR-OFF := `t' | `nil'
  637. For example, with the following configuration
  638. \(setq org-export-odt-table-styles
  639. '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
  640. \(\(use-first-row-styles . t\)
  641. \(use-first-column-styles . t\)\)\)
  642. \(\"TableWithHeaderColumns\" \"Custom\"
  643. \(\(use-first-column-styles . t\)\)\)\)\)
  644. 1. A table associated with \"TableWithHeaderRowsAndColumns\"
  645. style will use the following table-cell styles -
  646. \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
  647. \"CustomTableCell\" and the following paragraph styles
  648. \"CustomFirstRowTableParagraph\",
  649. \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
  650. as appropriate.
  651. 2. A table associated with \"TableWithHeaderColumns\" style will
  652. use the following table-cell styles -
  653. \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
  654. following paragraph styles
  655. \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
  656. as appropriate..
  657. Note that TABLE-TEMPLATE-NAME corresponds to the
  658. \"<table:table-template>\" elements contained within
  659. \"<office:styles>\". The entries (TABLE-STYLE-NAME
  660. TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
  661. \"table:template-name\" and \"table:use-first-row-styles\" etc
  662. attributes of \"<table:table>\" element. Refer ODF-1.2
  663. specification for more information. Also consult the
  664. implementation filed under `org-odt-get-table-cell-styles'.
  665. The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
  666. formatting of numbered display equations. Do not delete this
  667. style from the list."
  668. :group 'org-export-odt
  669. :type '(choice
  670. (const :tag "None" nil)
  671. (repeat :tag "Table Styles"
  672. (list :tag "Table Style Specification"
  673. (string :tag "Table Style Name")
  674. (string :tag "Table Template Name")
  675. (alist :options (use-first-row-styles
  676. use-last-row-styles
  677. use-first-column-styles
  678. use-last-column-styles
  679. use-banding-rows-styles
  680. use-banding-columns-styles)
  681. :key-type symbol
  682. :value-type (const :tag "True" t))))))
  683. (defun org-odt-begin-table (caption label attributes)
  684. (setq org-odt-table-style attributes)
  685. (setq org-odt-table-style-spec
  686. (assoc org-odt-table-style org-export-odt-table-styles))
  687. (when label
  688. (insert
  689. (org-odt-format-stylized-paragraph
  690. 'table (org-odt-format-entity-caption label caption "__Table__"))))
  691. (org-lparse-insert-tag
  692. "<table:table table:name=\"%s\" table:style-name=\"%s\">"
  693. (or label "") (or (nth 1 org-odt-table-style-spec) "OrgTable"))
  694. (setq org-lparse-table-begin-marker (point)))
  695. (defvar org-lparse-table-colalign-info)
  696. (defun org-odt-end-table ()
  697. (goto-char org-lparse-table-begin-marker)
  698. (loop for level from 0 below org-lparse-table-ncols
  699. do (let* ((col-cookie (and org-lparse-table-is-styled
  700. (cdr (assoc (1+ level)
  701. org-lparse-table-colalign-info))))
  702. (extra-columns (or (nth 1 col-cookie) 0)))
  703. (dotimes (i (1+ extra-columns))
  704. (insert
  705. (org-odt-format-tags
  706. "<table:table-column table:style-name=\"%sColumn\"/>"
  707. "" (or (nth 1 org-odt-table-style-spec) "OrgTable"))))
  708. (insert "\n")))
  709. ;; fill style attributes for table cells
  710. (when org-lparse-table-is-styled
  711. (while (re-search-forward "@@\\(table-cell:p\\|table-cell:style-name\\)@@\\([0-9]+\\)@@\\([0-9]+\\)@@" nil t)
  712. (let* ((spec (match-string 1))
  713. (r (string-to-number (match-string 2)))
  714. (c (string-to-number (match-string 3)))
  715. (cell-styles (org-odt-get-table-cell-styles
  716. r c org-odt-table-style-spec))
  717. (table-cell-style (car cell-styles))
  718. (table-cell-paragraph-style (cdr cell-styles)))
  719. (cond
  720. ((equal spec "table-cell:p")
  721. (replace-match table-cell-paragraph-style t t))
  722. ((equal spec "table-cell:style-name")
  723. (replace-match table-cell-style t t))))))
  724. (goto-char (point-max))
  725. (org-lparse-insert-tag "</table:table>"))
  726. (defun org-odt-begin-table-rowgroup (&optional is-header-row)
  727. (when org-lparse-table-rowgrp-open
  728. (org-lparse-end 'TABLE-ROWGROUP))
  729. (org-lparse-insert-tag (if is-header-row
  730. "<table:table-header-rows>"
  731. "<table:table-rows>"))
  732. (setq org-lparse-table-rowgrp-open t)
  733. (setq org-lparse-table-cur-rowgrp-is-hdr is-header-row))
  734. (defun org-odt-end-table-rowgroup ()
  735. (when org-lparse-table-rowgrp-open
  736. (setq org-lparse-table-rowgrp-open nil)
  737. (org-lparse-insert-tag
  738. (if org-lparse-table-cur-rowgrp-is-hdr
  739. "</table:table-header-rows>" "</table:table-rows>"))))
  740. (defun org-odt-format-table-row (row)
  741. (org-odt-format-tags
  742. '("<table:table-row>" . "</table:table-row>") row))
  743. (defun org-odt-get-table-cell-styles (r c &optional style-spec)
  744. "Retrieve styles applicable to a table cell.
  745. R and C are (zero-based) row and column numbers of the table
  746. cell. STYLE-SPEC is an entry in `org-export-odt-table-styles'
  747. applicable to the current table. It is `nil' if the table is not
  748. associated with any style attributes.
  749. Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
  750. When STYLE-SPEC is nil, style the table cell the conventional way
  751. - choose cell borders based on row and column groupings and
  752. choose paragraph alignment based on `org-col-cookies' text
  753. property. See also
  754. `org-odt-get-paragraph-style-cookie-for-table-cell'.
  755. When STYLE-SPEC is non-nil, ignore the above cookie and return
  756. styles congruent with the ODF-1.2 specification."
  757. (cond
  758. (style-spec
  759. ;; LibreOffice - particularly the Writer - honors neither table
  760. ;; templates nor custom table-cell styles. Inorder to retain
  761. ;; inter-operability with LibreOffice, only automatic styles are
  762. ;; used for styling of table-cells. The current implementation is
  763. ;; congruent with ODF-1.2 specification and hence is
  764. ;; future-compatible.
  765. ;; Additional Note: LibreOffice's AutoFormat facility for tables -
  766. ;; which recognizes as many as 16 different cell types - is much
  767. ;; richer. Unfortunately it is NOT amenable to easy configuration
  768. ;; by hand.
  769. (let* ((template-name (nth 1 style-spec))
  770. (cell-style-selectors (nth 2 style-spec))
  771. (cell-type
  772. (cond
  773. ((and (cdr (assoc 'use-first-column-styles cell-style-selectors))
  774. (= c 0)) "FirstColumn")
  775. ((and (cdr (assoc 'use-last-column-styles cell-style-selectors))
  776. (= c (1- org-lparse-table-ncols))) "LastColumn")
  777. ((and (cdr (assoc 'use-first-row-styles cell-style-selectors))
  778. (= r 0)) "FirstRow")
  779. ((and (cdr (assoc 'use-last-row-styles cell-style-selectors))
  780. (= r org-lparse-table-rownum))
  781. "LastRow")
  782. ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
  783. (= (% r 2) 1)) "EvenRow")
  784. ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
  785. (= (% r 2) 0)) "OddRow")
  786. ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
  787. (= (% c 2) 1)) "EvenColumn")
  788. ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
  789. (= (% c 2) 0)) "OddColumn")
  790. (t ""))))
  791. (cons
  792. (concat template-name cell-type "TableCell")
  793. (concat template-name cell-type "TableParagraph"))))
  794. (t
  795. (cons
  796. (concat
  797. "OrgTblCell"
  798. (cond
  799. ((= r 0) "T")
  800. ((eq (cdr (assoc r org-lparse-table-rowgrp-info)) :start) "T")
  801. (t ""))
  802. (when (= r org-lparse-table-rownum) "B")
  803. (cond
  804. ((= c 0) "")
  805. ((or (memq (nth c org-table-colgroup-info) '(:start :startend))
  806. (memq (nth (1- c) org-table-colgroup-info) '(:end :startend))) "L")
  807. (t "")))
  808. (capitalize (aref org-lparse-table-colalign-vector c))))))
  809. (defun org-odt-get-paragraph-style-cookie-for-table-cell (r c)
  810. (concat
  811. (and (not org-odt-table-style-spec)
  812. (cond
  813. (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
  814. ((and (= c 0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS))
  815. "OrgTableHeading")
  816. (t "OrgTableContents")))
  817. (and org-lparse-table-is-styled
  818. (format "@@table-cell:p@@%03d@@%03d@@" r c))))
  819. (defun org-odt-get-style-name-cookie-for-table-cell (r c)
  820. (when org-lparse-table-is-styled
  821. (format "@@table-cell:style-name@@%03d@@%03d@@" r c)))
  822. (defun org-odt-format-table-cell (data r c horiz-span)
  823. (concat
  824. (let* ((paragraph-style-cookie
  825. (org-odt-get-paragraph-style-cookie-for-table-cell r c))
  826. (style-name-cookie
  827. (org-odt-get-style-name-cookie-for-table-cell r c))
  828. (extra (and style-name-cookie
  829. (format " table:style-name=\"%s\"" style-name-cookie)))
  830. (extra (concat extra
  831. (and (> horiz-span 0)
  832. (format " table:number-columns-spanned=\"%d\""
  833. (1+ horiz-span))))))
  834. (org-odt-format-tags
  835. '("<table:table-cell%s>" . "</table:table-cell>")
  836. (if org-lparse-list-table-p data
  837. (org-odt-format-stylized-paragraph paragraph-style-cookie data)) extra))
  838. (let (s)
  839. (dotimes (i horiz-span)
  840. (setq s (concat s "\n<table:covered-table-cell/>"))) s)
  841. "\n"))
  842. (defun org-odt-begin-footnote-definition (n)
  843. (org-lparse-begin-paragraph 'footnote))
  844. (defun org-odt-end-footnote-definition (n)
  845. (org-lparse-end-paragraph))
  846. (defun org-odt-begin-toc (lang-specific-heading max-level)
  847. (insert
  848. (format "
  849. <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
  850. <text:table-of-content-source text:outline-level=\"%d\">
  851. <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
  852. " max-level lang-specific-heading))
  853. (loop for level from 1 upto 10
  854. do (insert (format
  855. "
  856. <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
  857. <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
  858. <text:index-entry-chapter/>
  859. <text:index-entry-text/>
  860. <text:index-entry-link-end/>
  861. </text:table-of-content-entry-template>
  862. " level level)))
  863. (insert
  864. (format "
  865. </text:table-of-content-source>
  866. <text:index-body>
  867. <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
  868. <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
  869. </text:index-title>
  870. " lang-specific-heading)))
  871. (defun org-odt-end-toc ()
  872. (insert "
  873. </text:index-body>
  874. </text:table-of-content>
  875. "))
  876. (defun org-odt-format-toc-entry (snumber todo headline tags href)
  877. (setq headline (concat
  878. (and org-export-with-section-numbers
  879. (concat snumber ". "))
  880. headline
  881. (and tags
  882. (concat
  883. (org-lparse-format 'SPACES 3)
  884. (org-lparse-format 'FONTIFY tags "tag")))))
  885. (when todo
  886. (setq headline (org-lparse-format 'FONTIFY headline "todo")))
  887. (let ((org-odt-suppress-xref t))
  888. (org-odt-format-link headline (concat "#" href))))
  889. (defun org-odt-format-toc-item (toc-entry level org-last-level)
  890. (let ((style (format "Contents_20_%d"
  891. (+ level (or (org-lparse-get 'TOPLEVEL-HLEVEL) 1) -1))))
  892. (insert "\n" (org-odt-format-stylized-paragraph style toc-entry) "\n")))
  893. ;; Following variable is let bound during 'ORG-LINK callback. See
  894. ;; org-html.el
  895. (defvar org-lparse-link-description-is-image nil)
  896. (defun org-odt-format-link (desc href &optional attr)
  897. (cond
  898. ((and (= (string-to-char href) ?#) (not org-odt-suppress-xref))
  899. (setq href (concat org-export-odt-bookmark-prefix (substring href 1)))
  900. (let ((xref-format "text"))
  901. (when (numberp desc)
  902. (setq desc (format "%d" desc) xref-format "number"))
  903. (org-odt-format-tags
  904. '("<text:bookmark-ref text:reference-format=\"%s\" text:ref-name=\"%s\">" .
  905. "</text:bookmark-ref>")
  906. desc xref-format href)))
  907. (org-lparse-link-description-is-image
  908. (org-odt-format-tags
  909. '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</draw:a>")
  910. desc href (or attr "")))
  911. (t
  912. (org-odt-format-tags
  913. '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</text:a>")
  914. desc href (or attr "")))))
  915. (defun org-odt-format-spaces (n)
  916. (cond
  917. ((= n 1) " ")
  918. ((> n 1) (concat
  919. " " (org-odt-format-tags "<text:s text:c=\"%d\"/>" "" (1- n))))
  920. (t "")))
  921. (defun org-odt-format-tabs (&optional n)
  922. (let ((tab "<text:tab/>")
  923. (n (or n 1)))
  924. (insert tab)))
  925. (defun org-odt-format-line-break ()
  926. (org-odt-format-tags "<text:line-break/>" ""))
  927. (defun org-odt-format-horizontal-line ()
  928. (org-odt-format-stylized-paragraph 'horizontal-line ""))
  929. (defun org-odt-encode-plain-text (line &optional no-whitespace-filling)
  930. (setq line (org-xml-encode-plain-text line))
  931. (if no-whitespace-filling line
  932. (org-odt-fill-tabs-and-spaces line)))
  933. (defun org-odt-format-line (line)
  934. (case org-lparse-dyn-current-environment
  935. (fixedwidth (concat
  936. (org-odt-format-stylized-paragraph
  937. 'fixedwidth (org-odt-encode-plain-text line)) "\n"))
  938. (t (concat line "\n"))))
  939. (defun org-odt-format-comment (fmt &rest args)
  940. (let ((comment (apply 'format fmt args)))
  941. (format "\n<!-- %s -->\n" comment)))
  942. (defun org-odt-format-org-entity (wd)
  943. (org-entity-get-representation wd 'utf8))
  944. (defun org-odt-fill-tabs-and-spaces (line)
  945. (replace-regexp-in-string
  946. "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
  947. (cond
  948. ((string= s "\t") (org-odt-format-tabs))
  949. (t (org-odt-format-spaces (length s))))) line))
  950. (defcustom org-export-odt-fontify-srcblocks t
  951. "Specify whether or not source blocks need to be fontified.
  952. Turn this option on if you want to colorize the source code
  953. blocks in the exported file. For colorization to work, you need
  954. to make available an enhanced version of `htmlfontify' library."
  955. :type 'boolean
  956. :group 'org-export-odt)
  957. (defun org-odt-format-source-line-with-line-number-and-label
  958. (line rpllbl num fontifier par-style)
  959. (let ((keep-label (not (numberp rpllbl)))
  960. (ref (org-find-text-property-in-string 'org-coderef line)))
  961. (setq line (concat line (and keep-label ref (format "(%s)" ref))))
  962. (setq line (funcall fontifier line))
  963. (when ref
  964. (setq line (org-odt-format-target line (concat "coderef-" ref))))
  965. (setq line (org-odt-format-stylized-paragraph par-style line))
  966. (if (not num) line
  967. (org-odt-format-tags '("<text:list-item>" . "</text:list-item>") line))))
  968. (defun org-odt-format-source-code-or-example-plain
  969. (lines lang caption textareap cols rows num cont rpllbl fmt)
  970. "Format source or example blocks much like fixedwidth blocks.
  971. Use this when `org-export-odt-fontify-srcblocks' option is turned
  972. off."
  973. (let* ((lines (org-split-string lines "[\r\n]"))
  974. (line-count (length lines))
  975. (i 0))
  976. (mapconcat
  977. (lambda (line)
  978. (incf i)
  979. (org-odt-format-source-line-with-line-number-and-label
  980. line rpllbl num 'org-odt-encode-plain-text
  981. (if (= i line-count) "OrgFixedWidthBlockLastLine"
  982. "OrgFixedWidthBlock")))
  983. lines "\n")))
  984. (defvar org-src-block-paragraph-format
  985. "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
  986. <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
  987. <style:background-image/>
  988. </style:paragraph-properties>
  989. <style:text-properties fo:color=\"%s\"/>
  990. </style:style>"
  991. "Custom paragraph style for colorized source and example blocks.
  992. This style is much the same as that of \"OrgFixedWidthBlock\"
  993. except that the foreground and background colors are set
  994. according to the default face identified by the `htmlfontify'.")
  995. (defun org-odt-hfy-face-to-css (fn)
  996. "Create custom style for face FN.
  997. When FN is the default face, use it's foreground and background
  998. properties to create \"OrgSrcBlock\" paragraph style. Otherwise
  999. use it's color attribute to create a character style whose name
  1000. is obtained from FN. Currently all attributes of FN other than
  1001. color are ignored.
  1002. The style name for a face FN is derived using the following
  1003. operations on the face name in that order - de-dash, CamelCase
  1004. and prefix with \"OrgSrc\". For example,
  1005. `font-lock-function-name-face' is associated with
  1006. \"OrgSrcFontLockFunctionNameFace\"."
  1007. (let* ((css-list (hfy-face-to-style fn))
  1008. (style-name ((lambda (fn)
  1009. (concat "OrgSrc"
  1010. (mapconcat
  1011. 'capitalize (split-string
  1012. (hfy-face-or-def-to-name fn) "-")
  1013. ""))) fn))
  1014. (color-val (cdr (assoc "color" css-list)))
  1015. (background-color-val (cdr (assoc "background" css-list)))
  1016. (style (and org-export-odt-create-custom-styles-for-srcblocks
  1017. (cond
  1018. ((eq fn 'default)
  1019. (format org-src-block-paragraph-format
  1020. background-color-val color-val))
  1021. (t
  1022. (format
  1023. "
  1024. <style:style style:name=\"%s\" style:family=\"text\">
  1025. <style:text-properties fo:color=\"%s\"/>
  1026. </style:style>" style-name color-val))))))
  1027. (cons style-name style)))
  1028. (defun org-odt-insert-custom-styles-for-srcblocks (styles)
  1029. "Save STYLES used for colorizing of source blocks.
  1030. Update styles.xml with styles that were collected as part of
  1031. `org-odt-hfy-face-to-css' callbacks."
  1032. (when styles
  1033. (with-current-buffer
  1034. (find-file-noselect (expand-file-name "styles.xml") t)
  1035. (goto-char (point-min))
  1036. (when (re-search-forward "</office:styles>" nil t)
  1037. (goto-char (match-beginning 0))
  1038. (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n")))))
  1039. (defun org-odt-format-source-code-or-example-colored
  1040. (lines lang caption textareap cols rows num cont rpllbl fmt)
  1041. "Format source or example blocks using `htmlfontify-string'.
  1042. Use this routine when `org-export-odt-fontify-srcblocks' option
  1043. is turned on."
  1044. (let* ((lang-m (and lang (or (cdr (assoc lang org-src-lang-modes)) lang)))
  1045. (mode (and lang-m (intern (concat (if (symbolp lang-m)
  1046. (symbol-name lang-m)
  1047. lang-m) "-mode"))))
  1048. (org-inhibit-startup t)
  1049. (org-startup-folded nil)
  1050. (lines (with-temp-buffer
  1051. (insert lines)
  1052. (if (functionp mode) (funcall mode) (fundamental-mode))
  1053. (font-lock-fontify-buffer)
  1054. (buffer-string)))
  1055. (hfy-html-quote-regex "\\([<\"&> ]\\)")
  1056. (hfy-html-quote-map '(("\"" "&quot;")
  1057. ("<" "&lt;")
  1058. ("&" "&amp;")
  1059. (">" "&gt;")
  1060. (" " "<text:s/>")
  1061. (" " "<text:tab/>")))
  1062. (hfy-face-to-css 'org-odt-hfy-face-to-css)
  1063. (hfy-optimisations-1 (copy-seq hfy-optimisations))
  1064. (hfy-optimisations (add-to-list 'hfy-optimisations-1
  1065. 'body-text-only))
  1066. (hfy-begin-span-handler
  1067. (lambda (style text-block text-id text-begins-block-p)
  1068. (insert (format "<text:span text:style-name=\"%s\">" style))))
  1069. (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
  1070. (when (fboundp 'htmlfontify-string)
  1071. (let* ((lines (org-split-string lines "[\r\n]"))
  1072. (line-count (length lines))
  1073. (i 0))
  1074. (mapconcat
  1075. (lambda (line)
  1076. (incf i)
  1077. (org-odt-format-source-line-with-line-number-and-label
  1078. line rpllbl num 'htmlfontify-string
  1079. (if (= i line-count) "OrgSrcBlockLastLine" "OrgSrcBlock")))
  1080. lines "\n")))))
  1081. (defun org-odt-format-source-code-or-example (lines lang caption textareap
  1082. cols rows num cont
  1083. rpllbl fmt)
  1084. "Format source or example blocks for export.
  1085. Use `org-odt-format-source-code-or-example-plain' or
  1086. `org-odt-format-source-code-or-example-colored' depending on the
  1087. value of `org-export-odt-fontify-srcblocks."
  1088. (setq lines (org-export-number-lines
  1089. lines 0 0 num cont rpllbl fmt 'preprocess)
  1090. lines (funcall
  1091. (or (and org-export-odt-fontify-srcblocks
  1092. (or (featurep 'htmlfontify)
  1093. (require 'htmlfontify nil t))
  1094. (fboundp 'htmlfontify-string)
  1095. 'org-odt-format-source-code-or-example-colored)
  1096. 'org-odt-format-source-code-or-example-plain)
  1097. lines lang caption textareap cols rows num cont rpllbl fmt))
  1098. (if (not num) lines
  1099. (let ((extra (format " text:continue-numbering=\"%s\""
  1100. (if cont "true" "false"))))
  1101. (org-odt-format-tags
  1102. '("<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>"
  1103. . "</text:list>") lines extra))))
  1104. (defun org-odt-remap-stylenames (style-name)
  1105. (or
  1106. (cdr (assoc style-name '(("timestamp-wrapper" . "OrgTimestampWrapper")
  1107. ("timestamp" . "OrgTimestamp")
  1108. ("timestamp-kwd" . "OrgTimestampKeyword")
  1109. ("tag" . "OrgTag")
  1110. ("todo" . "OrgTodo")
  1111. ("done" . "OrgDone")
  1112. ("target" . "OrgTarget"))))
  1113. style-name))
  1114. (defun org-odt-format-fontify (text style &optional id)
  1115. (let* ((style-name
  1116. (cond
  1117. ((stringp style)
  1118. (org-odt-remap-stylenames style))
  1119. ((symbolp style)
  1120. (org-odt-get-style-name-for-entity 'character style))
  1121. ((listp style)
  1122. (assert (< 1 (length style)))
  1123. (let ((parent-style (pop style)))
  1124. (mapconcat (lambda (s)
  1125. ;; (assert (stringp s) t)
  1126. (org-odt-remap-stylenames s)) style "")
  1127. (org-odt-remap-stylenames parent-style)))
  1128. (t (error "Don't how to handle style %s" style)))))
  1129. (org-odt-format-tags
  1130. '("<text:span text:style-name=\"%s\">" . "</text:span>")
  1131. text style-name)))
  1132. (defun org-odt-relocate-relative-path (path dir)
  1133. (if (file-name-absolute-p path) path
  1134. (file-relative-name (expand-file-name path dir)
  1135. (expand-file-name "eyecandy" dir))))
  1136. (defun org-odt-format-inline-image (thefile)
  1137. (let* ((thelink (if (file-name-absolute-p thefile) thefile
  1138. (org-xml-format-href
  1139. (org-odt-relocate-relative-path
  1140. thefile org-current-export-file))))
  1141. (href
  1142. (org-odt-format-tags
  1143. "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
  1144. (if org-export-odt-embed-images
  1145. (org-odt-copy-image-file thefile) thelink))))
  1146. (org-export-odt-format-image thefile href)))
  1147. (defun org-export-odt-format-formula (src href &optional embed-as)
  1148. "Create image tag with source and attributes."
  1149. (save-match-data
  1150. (let* ((caption (org-find-text-property-in-string 'org-caption src))
  1151. (caption (and caption (org-xml-format-desc caption)))
  1152. (label (org-find-text-property-in-string 'org-label src))
  1153. (latex-frag (org-find-text-property-in-string 'org-latex-src src))
  1154. (embed-as (or embed-as
  1155. (and latex-frag
  1156. (org-find-text-property-in-string
  1157. 'org-latex-src-embed-type src))
  1158. (if (or caption label) 'paragraph 'character)))
  1159. width height)
  1160. (when latex-frag
  1161. (setq href (org-propertize href :title "LaTeX Fragment"
  1162. :description latex-frag)))
  1163. (cond
  1164. ((eq embed-as 'character)
  1165. (org-odt-format-entity "InlineFormula" href width height))
  1166. (t
  1167. (org-lparse-end-paragraph)
  1168. (org-lparse-insert-list-table
  1169. `((,(org-odt-format-entity
  1170. (if caption "CaptionedDisplayFormula" "DisplayFormula")
  1171. href width height caption nil)
  1172. ,(if (not label) ""
  1173. (org-odt-format-entity-caption label nil "__MathFormula__"))))
  1174. nil nil nil "OrgEquation" nil '((1 "c" 8) (2 "c" 1)))
  1175. (throw 'nextline nil))))))
  1176. (defvar org-odt-embedded-formulas-count 0)
  1177. (defun org-odt-copy-formula-file (path)
  1178. "Returns the internal name of the file"
  1179. (let* ((src-file (expand-file-name
  1180. path (file-name-directory org-current-export-file)))
  1181. (target-dir (format "Formula-%04d/"
  1182. (incf org-odt-embedded-formulas-count)))
  1183. (target-file (concat target-dir "content.xml")))
  1184. (when (not org-lparse-to-buffer)
  1185. (message "Embedding %s as %s ..."
  1186. (substring-no-properties path) target-file)
  1187. (make-directory target-dir)
  1188. (org-odt-create-manifest-file-entry
  1189. "application/vnd.oasis.opendocument.formula" target-dir "1.2")
  1190. (case (org-odt-is-formula-link-p src-file)
  1191. (mathml
  1192. (copy-file src-file target-file 'overwrite))
  1193. (odf
  1194. (org-odt-zip-extract-one src-file "content.xml" target-dir))
  1195. (t
  1196. (error "%s is not a formula file" src-file)))
  1197. (org-odt-create-manifest-file-entry "text/xml" target-file))
  1198. target-file))
  1199. (defun org-odt-format-inline-formula (thefile)
  1200. (let* ((thelink (if (file-name-absolute-p thefile) thefile
  1201. (org-xml-format-href
  1202. (org-odt-relocate-relative-path
  1203. thefile org-current-export-file))))
  1204. (href
  1205. (org-odt-format-tags
  1206. "<draw:object xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
  1207. (file-name-directory (org-odt-copy-formula-file thefile)))))
  1208. (org-export-odt-format-formula thefile href)))
  1209. (defun org-odt-is-formula-link-p (file)
  1210. (let ((case-fold-search nil))
  1211. (cond
  1212. ((string-match "\\.\\(mathml\\|mml\\)\\'" file)
  1213. 'mathml)
  1214. ((string-match "\\.odf\\'" file)
  1215. 'odf))))
  1216. (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
  1217. descp)
  1218. "Make a OpenDocument link.
  1219. OPT-PLIST is an options list.
  1220. TYPE-1 is the device-type of the link (THIS://foo.html).
  1221. PATH is the path of the link (http://THIS#location).
  1222. FRAGMENT is the fragment part of the link, if any (foo.html#THIS).
  1223. DESC is the link description, if any.
  1224. ATTR is a string of other attributes of the a element."
  1225. (declare (special org-lparse-par-open))
  1226. (save-match-data
  1227. (let* ((may-inline-p
  1228. (and (member type-1 '("http" "https" "file"))
  1229. (org-lparse-should-inline-p path descp)
  1230. (not fragment)))
  1231. (type (if (equal type-1 "id") "file" type-1))
  1232. (filename path)
  1233. (thefile path))
  1234. (cond
  1235. ;; check for inlined images
  1236. ((and (member type '("file"))
  1237. (not fragment)
  1238. (org-file-image-p
  1239. filename org-export-odt-inline-image-extensions)
  1240. (or (eq t org-export-odt-inline-images)
  1241. (and org-export-odt-inline-images (not descp))))
  1242. (org-odt-format-inline-image thefile))
  1243. ;; check for embedded formulas
  1244. ((and (member type '("file"))
  1245. (not fragment)
  1246. (org-odt-is-formula-link-p filename)
  1247. (or (not descp)))
  1248. (org-odt-format-inline-formula thefile))
  1249. ((string= type "coderef")
  1250. (let* ((ref fragment)
  1251. (lineno-or-ref (cdr (assoc ref org-export-code-refs)))
  1252. (desc (and descp desc))
  1253. (org-odt-suppress-xref nil)
  1254. (href (org-xml-format-href (concat "#coderef-" ref))))
  1255. (cond
  1256. ((and (numberp lineno-or-ref) (not desc))
  1257. (org-odt-format-link lineno-or-ref href))
  1258. ((and (numberp lineno-or-ref) desc
  1259. (string-match (regexp-quote (concat "(" ref ")")) desc))
  1260. (format (replace-match "%s" t t desc)
  1261. (org-odt-format-link lineno-or-ref href)))
  1262. (t
  1263. (setq desc (format
  1264. (if (and desc (string-match
  1265. (regexp-quote (concat "(" ref ")"))
  1266. desc))
  1267. (replace-match "%s" t t desc)
  1268. (or desc "%s"))
  1269. lineno-or-ref))
  1270. (org-odt-format-link (org-xml-format-desc desc) href)))))
  1271. (t
  1272. (when (string= type "file")
  1273. (setq thefile
  1274. (cond
  1275. ((file-name-absolute-p path)
  1276. (concat "file://" (expand-file-name path)))
  1277. (t (org-odt-relocate-relative-path
  1278. thefile org-current-export-file)))))
  1279. (when (and (member type '("" "http" "https" "file")) fragment)
  1280. (setq thefile (concat thefile "#" fragment)))
  1281. (setq thefile (org-xml-format-href thefile))
  1282. (when (not (member type '("" "file")))
  1283. (setq thefile (concat type ":" thefile)))
  1284. (let ((org-odt-suppress-xref nil))
  1285. (org-odt-format-link
  1286. (org-xml-format-desc desc) thefile attr)))))))
  1287. (defun org-odt-format-heading (text level &optional id)
  1288. (let* ((text (if id (org-odt-format-target text id) text)))
  1289. (org-odt-format-tags
  1290. '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
  1291. "</text:h>") text level level)))
  1292. (defun org-odt-format-headline (title extra-targets tags
  1293. &optional snumber level)
  1294. (concat
  1295. (org-lparse-format 'EXTRA-TARGETS extra-targets)
  1296. ;; No need to generate section numbers. They are auto-generated by
  1297. ;; the application
  1298. ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
  1299. title
  1300. (and tags (concat (org-lparse-format 'SPACES 3)
  1301. (org-lparse-format 'ORG-TAGS tags)))))
  1302. (defun org-odt-format-anchor (text name &optional class)
  1303. (org-odt-format-target text name))
  1304. (defun org-odt-format-bookmark (text id)
  1305. (if id
  1306. (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id)
  1307. text))
  1308. (defun org-odt-format-target (text id)
  1309. (let ((name (concat org-export-odt-bookmark-prefix id)))
  1310. (concat
  1311. (and id (org-odt-format-tags
  1312. "<text:bookmark-start text:name=\"%s\"/>" "" name))
  1313. (org-odt-format-bookmark text id)
  1314. (and id (org-odt-format-tags
  1315. "<text:bookmark-end text:name=\"%s\"/>" "" name)))))
  1316. (defun org-odt-format-footnote (n def)
  1317. (let ((id (concat "fn" n))
  1318. (note-class "footnote")
  1319. (par-style "Footnote"))
  1320. (org-odt-format-tags
  1321. '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
  1322. "</text:note>")
  1323. (concat
  1324. (org-odt-format-tags
  1325. '("<text:note-citation>" . "</text:note-citation>")
  1326. n)
  1327. (org-odt-format-tags
  1328. '("<text:note-body>" . "</text:note-body>")
  1329. def))
  1330. id note-class)))
  1331. (defun org-odt-format-footnote-reference (n def refcnt)
  1332. (if (= refcnt 1)
  1333. (org-odt-format-footnote n def)
  1334. (org-odt-format-footnote-ref n)))
  1335. (defun org-odt-format-footnote-ref (n)
  1336. (let ((note-class "footnote")
  1337. (ref-format "text")
  1338. (ref-name (concat "fn" n)))
  1339. (org-odt-format-tags
  1340. '("<text:span text:style-name=\"%s\">" . "</text:span>")
  1341. (org-odt-format-tags
  1342. '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" . "</text:note-ref>")
  1343. n note-class ref-format ref-name)
  1344. "OrgSuperscript")))
  1345. (defun org-odt-get-image-name (file-name)
  1346. (require 'sha1)
  1347. (file-relative-name
  1348. (expand-file-name
  1349. (concat (sha1 file-name) "." (file-name-extension file-name)) "Pictures")))
  1350. (defun org-export-odt-format-image (src href &optional embed-as)
  1351. "Create image tag with source and attributes."
  1352. (save-match-data
  1353. (let* ((caption (org-find-text-property-in-string 'org-caption src))
  1354. (caption (and caption (org-xml-format-desc caption)))
  1355. (attr (org-find-text-property-in-string 'org-attributes src))
  1356. (label (org-find-text-property-in-string 'org-label src))
  1357. (latex-frag (org-find-text-property-in-string
  1358. 'org-latex-src src))
  1359. (category (and latex-frag "__DvipngImage__"))
  1360. (embed-as (or embed-as
  1361. (if latex-frag
  1362. (or (org-find-text-property-in-string
  1363. 'org-latex-src-embed-type src) 'character)
  1364. 'paragraph)))
  1365. (attr-plist (org-lparse-get-block-params attr))
  1366. (size (org-odt-image-size-from-file
  1367. src (plist-get attr-plist :width)
  1368. (plist-get attr-plist :height)
  1369. (plist-get attr-plist :scale) nil embed-as))
  1370. (width (car size)) (height (cdr size)))
  1371. (when latex-frag
  1372. (setq href (org-propertize href :title "LaTeX Fragment"
  1373. :description latex-frag)))
  1374. (cond
  1375. ((not (or caption label))
  1376. (case embed-as
  1377. (paragraph (org-odt-format-entity "DisplayImage" href width height))
  1378. (character (org-odt-format-entity "InlineImage" href width height))
  1379. (t (error "Unknown value for embed-as %S" embed-as))))
  1380. (t
  1381. (org-odt-format-entity
  1382. "CaptionedDisplayImage" href width height caption label category))))))
  1383. (defun org-odt-format-object-description (title description)
  1384. (concat (and title (org-odt-format-tags
  1385. '("<svg:title>" . "</svg:title>")
  1386. (org-odt-encode-plain-text title t)))
  1387. (and description (org-odt-format-tags
  1388. '("<svg:desc>" . "</svg:desc>")
  1389. (org-odt-encode-plain-text description t)))))
  1390. (defun org-odt-format-frame (text width height style &optional
  1391. extra anchor-type)
  1392. (let ((frame-attrs
  1393. (concat
  1394. (if width (format " svg:width=\"%0.2fcm\"" width) "")
  1395. (if height (format " svg:height=\"%0.2fcm\"" height) "")
  1396. extra
  1397. (format " text:anchor-type=\"%s\"" (or anchor-type "paragraph")))))
  1398. (org-odt-format-tags
  1399. '("<draw:frame draw:style-name=\"%s\"%s>" . "</draw:frame>")
  1400. (concat text (org-odt-format-object-description
  1401. (get-text-property 0 :title text)
  1402. (get-text-property 0 :description text)))
  1403. style frame-attrs)))
  1404. (defun org-odt-format-textbox (text width height style &optional
  1405. extra anchor-type)
  1406. (org-odt-format-frame
  1407. (org-odt-format-tags
  1408. '("<draw:text-box %s>" . "</draw:text-box>")
  1409. text (concat (format " fo:min-height=\"%0.2fcm\"" (or height .2))
  1410. (format " fo:min-width=\"%0.2fcm\"" (or width .2))))
  1411. width nil style extra anchor-type))
  1412. (defun org-odt-format-inlinetask (heading content
  1413. &optional todo priority tags)
  1414. (org-odt-format-stylized-paragraph
  1415. nil (org-odt-format-textbox
  1416. (concat (org-odt-format-stylized-paragraph
  1417. "OrgInlineTaskHeading"
  1418. (org-lparse-format
  1419. 'HEADLINE (concat (org-lparse-format-todo todo) " " heading)
  1420. nil tags))
  1421. content) nil nil "OrgInlineTaskFrame" " style:rel-width=\"100%\"")))
  1422. (defvar org-odt-entity-frame-styles
  1423. '(("InlineImage" "__Figure__" ("OrgInlineImage" nil "as-char"))
  1424. ("DisplayImage" "__Figure__" ("OrgDisplayImage" nil "paragraph"))
  1425. ("CaptionedDisplayImage" "__Figure__"
  1426. ("OrgCaptionedImage"
  1427. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  1428. ("OrgImageCaptionFrame"))
  1429. ("InlineFormula" "__MathFormula__" ("OrgInlineFormula" nil "as-char"))
  1430. ("DisplayFormula" "__MathFormula__" ("OrgDisplayFormula" nil "as-char"))
  1431. ("CaptionedDisplayFormula" "__MathFormula__"
  1432. ("OrgCaptionedFormula" nil "paragraph")
  1433. ("OrgFormulaCaptionFrame" nil "as-char"))))
  1434. (defun org-odt-format-entity (entity href width height
  1435. &optional caption label category)
  1436. (let* ((entity-style (assoc entity org-odt-entity-frame-styles))
  1437. (entity-frame (apply 'org-odt-format-frame
  1438. href width height (nth 2 entity-style))))
  1439. (if (not (or caption label)) entity-frame
  1440. (apply 'org-odt-format-textbox
  1441. (org-odt-format-stylized-paragraph
  1442. 'illustration
  1443. (concat entity-frame
  1444. (org-odt-format-entity-caption
  1445. label caption (or category (nth 1 entity-style)))))
  1446. width height (nth 3 entity-style)))))
  1447. (defvar org-odt-embedded-images-count 0)
  1448. (defun org-odt-copy-image-file (path)
  1449. "Returns the internal name of the file"
  1450. (let* ((image-type (file-name-extension path))
  1451. (media-type (format "image/%s" image-type))
  1452. (src-file (expand-file-name
  1453. path (file-name-directory org-current-export-file)))
  1454. (target-dir "Images/")
  1455. (target-file
  1456. (format "%s%04d.%s" target-dir
  1457. (incf org-odt-embedded-images-count) image-type)))
  1458. (when (not org-lparse-to-buffer)
  1459. (message "Embedding %s as %s ..."
  1460. (substring-no-properties path) target-file)
  1461. (when (= 1 org-odt-embedded-images-count)
  1462. (make-directory target-dir)
  1463. (org-odt-create-manifest-file-entry "" target-dir))
  1464. (copy-file src-file target-file 'overwrite)
  1465. (org-odt-create-manifest-file-entry media-type target-file))
  1466. target-file))
  1467. (defvar org-export-odt-image-size-probe-method
  1468. '(emacs imagemagick force)
  1469. "Ordered list of methods by for determining size of an embedded
  1470. image.")
  1471. (defvar org-export-odt-default-image-sizes-alist
  1472. '(("character" . (5 . 0.4))
  1473. ("paragraph" . (5 . 5)))
  1474. "Hardcoded image dimensions one for each of the anchor
  1475. methods.")
  1476. ;; A4 page size is 21.0 by 29.7 cms
  1477. ;; The default page settings has 2cm margin on each of the sides. So
  1478. ;; the effective text area is 17.0 by 25.7 cm
  1479. (defvar org-export-odt-max-image-size '(17.0 . 20.0)
  1480. "Limiting dimensions for an embedded image.")
  1481. (defun org-odt-do-image-size (probe-method file &optional dpi anchor-type)
  1482. (setq dpi (or dpi org-export-odt-pixels-per-inch))
  1483. (setq anchor-type (or anchor-type "paragraph"))
  1484. (flet ((size-in-cms (size-in-pixels)
  1485. (flet ((pixels-to-cms (pixels)
  1486. (let* ((cms-per-inch 2.54)
  1487. (inches (/ pixels dpi)))
  1488. (* cms-per-inch inches))))
  1489. (and size-in-pixels
  1490. (cons (pixels-to-cms (car size-in-pixels))
  1491. (pixels-to-cms (cdr size-in-pixels)))))))
  1492. (case probe-method
  1493. (emacs
  1494. (size-in-cms (ignore-errors (image-size (create-image file) 'pixels))))
  1495. (imagemagick
  1496. (size-in-cms
  1497. (let ((dim (shell-command-to-string
  1498. (format "identify -format \"%%w:%%h\" \"%s\"" file))))
  1499. (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
  1500. (cons (string-to-number (match-string 1 dim))
  1501. (string-to-number (match-string 2 dim)))))))
  1502. (t
  1503. (cdr (assoc-string anchor-type
  1504. org-export-odt-default-image-sizes-alist))))))
  1505. (defun org-odt-image-size-from-file (file &optional user-width
  1506. user-height scale dpi embed-as)
  1507. (unless (file-name-absolute-p file)
  1508. (setq file (expand-file-name
  1509. file (file-name-directory org-current-export-file))))
  1510. (let* (size width height)
  1511. (unless (and user-height user-width)
  1512. (loop for probe-method in org-export-odt-image-size-probe-method
  1513. until size
  1514. do (setq size (org-odt-do-image-size
  1515. probe-method file dpi embed-as)))
  1516. (or size (error "Cannot determine Image size. Aborting ..."))
  1517. (setq width (car size) height (cdr size)))
  1518. (cond
  1519. (scale
  1520. (setq width (* width scale) height (* height scale)))
  1521. ((and user-height user-width)
  1522. (setq width user-width height user-height))
  1523. (user-height
  1524. (setq width (* user-height (/ width height)) height user-height))
  1525. (user-width
  1526. (setq height (* user-width (/ height width)) width user-width))
  1527. (t (ignore)))
  1528. ;; ensure that an embedded image fits comfortably within a page
  1529. (let ((max-width (car org-export-odt-max-image-size))
  1530. (max-height (cdr org-export-odt-max-image-size)))
  1531. (when (or (> width max-width) (> height max-height))
  1532. (let* ((scale1 (/ max-width width))
  1533. (scale2 (/ max-height height))
  1534. (scale (min scale1 scale2)))
  1535. (setq width (* scale width) height (* scale height)))))
  1536. (cons width height)))
  1537. (defvar org-odt-entity-labels-alist nil
  1538. "Associate Labels with the Labelled entities.
  1539. Each element of the alist is of the form (LABEL-NAME
  1540. CATEGORY-NAME SEQNO LABEL-STYLE-NAME). LABEL-NAME is same as
  1541. that specified by \"#+LABEL: ...\" line. CATEGORY-NAME is the
  1542. type of the entity that LABEL-NAME is attached to. CATEGORY-NAME
  1543. can be one of \"Table\", \"Figure\" or \"Equation\". SEQNO is
  1544. the unique number assigned to the referenced entity on a
  1545. per-CATEGORY basis. It is generated sequentially and is 1-based.
  1546. LABEL-STYLE-NAME is a key `org-odt-label-styles'.
  1547. See `org-odt-add-label-definition' and
  1548. `org-odt-fixup-label-references'.")
  1549. (defvar org-odt-entity-counts-plist nil
  1550. "Plist of running counters of SEQNOs for each of the CATEGORY-NAMEs.
  1551. See `org-odt-entity-labels-alist' for known CATEGORY-NAMEs.")
  1552. (defvar org-odt-label-styles
  1553. '(("text" "(%n)" "text" "(%n)")
  1554. ("category-and-value" "%e %n%c" "category-and-value" "%e %n"))
  1555. "Specify how labels are applied and referenced.
  1556. This is an alist where each element is of the
  1557. form (LABEL-STYLE-NAME LABEL-ATTACH-FMT LABEL-REF-MODE
  1558. LABEL-REF-FMT).
  1559. LABEL-ATTACH-FMT controls how labels and captions are attached to
  1560. an entity. It may contain following specifiers - %e, %n and %c.
  1561. %e is replaced with the CATEGORY-NAME. %n is replaced with
  1562. \"<text:sequence ...> SEQNO </text:sequence>\". %c is replaced
  1563. with CAPTION. See `org-odt-format-label-definition'.
  1564. LABEL-REF-MODE and LABEL-REF-FMT controls how label references
  1565. are generated. The following XML is generated for a label
  1566. reference - \"<text:sequence-ref
  1567. text:reference-format=\"LABEL-REF-MODE\" ...> LABEL-REF-FMT
  1568. </text:sequence-ref>\". LABEL-REF-FMT may contain following
  1569. specifiers - %e and %n. %e is replaced with the CATEGORY-NAME.
  1570. %n is replaced with SEQNO. See
  1571. `org-odt-format-label-reference'.")
  1572. (defvar org-odt-category-map-alist
  1573. '(("__Table__" "Table" "category-and-value")
  1574. ("__Figure__" "Figure" "category-and-value")
  1575. ("__MathFormula__" "Equation" "text")
  1576. ("__DvipngImage__" "Equation" "category-and-value"))
  1577. "Map a CATEGORY-HANDLE to CATEGORY-NAME and LABEL-STYLE.
  1578. This is an alist where each element is of the form
  1579. \\(CATEGORY-HANDLE CATEGORY-NAME LABEL-STYLE\\). CATEGORY_HANDLE
  1580. could either be one of the internal handles (as seen above) or be
  1581. derived from the \"#+LABEL:<label-name>\" specification. See
  1582. `org-export-odt-get-category-from-label'. CATEGORY-NAME and
  1583. LABEL-STYLE are used for generating ODT labels. See
  1584. `org-odt-label-styles'.")
  1585. (defvar org-export-odt-user-categories
  1586. '("Illustration" "Table" "Text" "Drawing" "Equation" "Figure"))
  1587. (defvar org-export-odt-get-category-from-label nil
  1588. "Should category of label be inferred from label itself.
  1589. When this option is non-nil, a label is parsed in to two
  1590. component parts delimited by a \":\" (colon) as shown here -
  1591. #+LABEL:[CATEGORY-HANDLE:]EXTRA. The CATEGORY-HANDLE is mapped
  1592. to a CATEGORY-NAME and LABEL-STYLE using
  1593. `org-odt-category-map-alist'. (If no such map is provided and
  1594. CATEGORY-NAME is set to CATEGORY-HANDLE and LABEL-STYLE is set to
  1595. \"category-and-value\"). If CATEGORY-NAME so obtained is listed
  1596. under `org-export-odt-user-categories' then the user specified
  1597. styles are used. Otherwise styles as determined by the internal
  1598. CATEGORY-HANDLE is used. See
  1599. `org-odt-get-label-category-and-style' for details.")
  1600. (defun org-odt-get-label-category-and-style (label default-category)
  1601. "See `org-export-odt-get-category-from-label'."
  1602. (let ((default-category-map
  1603. (assoc default-category org-odt-category-map-alist))
  1604. user-category user-category-map category)
  1605. (cond
  1606. ((not org-export-odt-get-category-from-label)
  1607. default-category-map)
  1608. ((not (setq user-category
  1609. (save-match-data
  1610. (and (string-match "\\`\\(.*\\):.+" label)
  1611. (match-string 1 label)))))
  1612. default-category-map)
  1613. (t
  1614. (setq user-category-map
  1615. (or (assoc user-category org-odt-category-map-alist)
  1616. (list nil user-category "category-and-value"))
  1617. category (nth 1 user-category-map))
  1618. (if (member category org-export-odt-user-categories)
  1619. user-category-map
  1620. default-category-map)))))
  1621. (defun org-odt-add-label-definition (label default-category)
  1622. "Create an entry in `org-odt-entity-labels-alist' and return it."
  1623. (setq label (substring-no-properties label))
  1624. (let* ((label-props (org-odt-get-label-category-and-style
  1625. label default-category))
  1626. (category (nth 1 label-props))
  1627. (counter category)
  1628. (label-style (nth 2 label-props))
  1629. (sequence-var (intern (mapconcat
  1630. 'downcase
  1631. (org-split-string counter) "-")))
  1632. (seqno (1+ (or (plist-get org-odt-entity-counts-plist sequence-var)
  1633. 0)))
  1634. (label-props (list label category seqno label-style)))
  1635. (setq org-odt-entity-counts-plist
  1636. (plist-put org-odt-entity-counts-plist sequence-var seqno))
  1637. (push label-props org-odt-entity-labels-alist)
  1638. label-props))
  1639. (defun org-odt-format-label-definition (caption label category seqno label-style)
  1640. (assert label)
  1641. (format-spec
  1642. (cadr (assoc-string label-style org-odt-label-styles t))
  1643. `((?e . ,category)
  1644. (?n . ,(org-odt-format-tags
  1645. '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" . "</text:sequence>")
  1646. (format "%d" seqno) label category category))
  1647. (?c . ,(or (and caption (concat ": " caption)) "")))))
  1648. (defun org-odt-format-label-reference (label category seqno label-style)
  1649. (assert label)
  1650. (save-match-data
  1651. (let* ((fmt (cddr (assoc-string label-style org-odt-label-styles t)))
  1652. (fmt1 (car fmt))
  1653. (fmt2 (cadr fmt)))
  1654. (org-odt-format-tags
  1655. '("<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">"
  1656. . "</text:sequence-ref>")
  1657. (format-spec fmt2 `((?e . ,category)
  1658. (?n . ,(format "%d" seqno)))) fmt1 label))))
  1659. (defun org-odt-fixup-label-references ()
  1660. (goto-char (point-min))
  1661. (while (re-search-forward
  1662. "<text:sequence-ref text:ref-name=\"\\([^\"]+\\)\">[ \t\n]*</text:sequence-ref>"
  1663. nil t)
  1664. (let* ((label (match-string 1))
  1665. (label-def (assoc label org-odt-entity-labels-alist))
  1666. (rpl (and label-def
  1667. (apply 'org-odt-format-label-reference label-def))))
  1668. (if rpl (replace-match rpl t t)
  1669. (org-lparse-warn
  1670. (format "Unable to resolve reference to label \"%s\"" label))))))
  1671. (defun org-odt-format-entity-caption (label caption category)
  1672. (or (and label
  1673. (apply 'org-odt-format-label-definition
  1674. caption (org-odt-add-label-definition label category)))
  1675. caption ""))
  1676. (defun org-odt-format-tags (tag text &rest args)
  1677. (let ((prefix (when org-lparse-encode-pending "@"))
  1678. (suffix (when org-lparse-encode-pending "@")))
  1679. (apply 'org-lparse-format-tags tag text prefix suffix args)))
  1680. (defvar org-odt-manifest-file-entries nil)
  1681. (defun org-odt-init-outfile (filename)
  1682. (unless (executable-find "zip")
  1683. ;; Not at all OSes ship with zip by default
  1684. (error "Executable \"zip\" needed for creating OpenDocument files"))
  1685. (let* ((outdir (make-temp-file
  1686. (format org-export-odt-tmpdir-prefix org-lparse-backend) t))
  1687. (content-file (expand-file-name "content.xml" outdir)))
  1688. ;; init conten.xml
  1689. (with-current-buffer (find-file-noselect content-file t))
  1690. ;; reset variables
  1691. (setq org-odt-manifest-file-entries nil
  1692. org-odt-embedded-images-count 0
  1693. org-odt-embedded-formulas-count 0
  1694. org-odt-entity-labels-alist nil
  1695. org-odt-entity-counts-plist nil)
  1696. content-file))
  1697. (defcustom org-export-odt-prettify-xml nil
  1698. "Specify whether or not the xml output should be prettified.
  1699. When this option is turned on, `indent-region' is run on all
  1700. component xml buffers before they are saved. Turn this off for
  1701. regular use. Turn this on if you need to examine the xml
  1702. visually."
  1703. :group 'org-export-odt
  1704. :type 'boolean)
  1705. (defvar hfy-user-sheet-assoc) ; bound during org-do-lparse
  1706. (defun org-odt-save-as-outfile (target opt-plist)
  1707. ;; write meta file
  1708. (org-odt-update-meta-file opt-plist)
  1709. ;; write styles file
  1710. (when (equal org-lparse-backend 'odt)
  1711. (org-odt-update-styles-file opt-plist))
  1712. ;; create mimetype file
  1713. (let ((mimetype (org-odt-write-mimetype-file org-lparse-backend)))
  1714. (org-odt-create-manifest-file-entry mimetype "/" "1.2"))
  1715. ;; create a manifest entry for content.xml
  1716. (org-odt-create-manifest-file-entry "text/xml" "content.xml")
  1717. ;; write out the manifest entries before zipping
  1718. (org-odt-write-manifest-file)
  1719. (let ((xml-files '("mimetype" "META-INF/manifest.xml" "content.xml"
  1720. "meta.xml"))
  1721. (zipdir default-directory))
  1722. (when (equal org-lparse-backend 'odt)
  1723. (push "styles.xml" xml-files))
  1724. (message "Switching to directory %s" (expand-file-name zipdir))
  1725. ;; save all xml files
  1726. (mapc (lambda (file)
  1727. (with-current-buffer
  1728. (find-file-noselect (expand-file-name file) t)
  1729. ;; prettify output if needed
  1730. (when org-export-odt-prettify-xml
  1731. (indent-region (point-min) (point-max)))
  1732. (save-buffer 0)))
  1733. xml-files)
  1734. (let* ((target-name (file-name-nondirectory target))
  1735. (target-dir (file-name-directory target))
  1736. (cmds `(("zip" "-mX0" ,target-name "mimetype")
  1737. ("zip" "-rmTq" ,target-name "."))))
  1738. (when (file-exists-p target)
  1739. ;; FIXME: If the file is locked this throws a cryptic error
  1740. (delete-file target))
  1741. (let ((coding-system-for-write 'no-conversion) exitcode err-string)
  1742. (message "Creating odt file...")
  1743. (mapc
  1744. (lambda (cmd)
  1745. (message "Running %s" (mapconcat 'identity cmd " "))
  1746. (setq err-string
  1747. (with-output-to-string
  1748. (setq exitcode
  1749. (apply 'call-process (car cmd)
  1750. nil standard-output nil (cdr cmd)))))
  1751. (or (zerop exitcode)
  1752. (ignore (message "%s" err-string))
  1753. (error "Unable to create odt file (%S)" exitcode)))
  1754. cmds))
  1755. ;; move the file from outdir to target-dir
  1756. (rename-file target-name target-dir)
  1757. ;; kill all xml buffers
  1758. (mapc (lambda (file)
  1759. (kill-buffer
  1760. (find-file-noselect (expand-file-name file zipdir) t)))
  1761. xml-files)
  1762. (delete-directory zipdir)))
  1763. (message "Created %s" target)
  1764. (set-buffer (find-file-noselect target t)))
  1765. (defconst org-odt-manifest-file-entry-tag
  1766. "
  1767. <manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
  1768. (defun org-odt-create-manifest-file-entry (&rest args)
  1769. (push args org-odt-manifest-file-entries))
  1770. (defun org-odt-write-manifest-file ()
  1771. (make-directory "META-INF")
  1772. (let ((manifest-file (expand-file-name "META-INF/manifest.xml")))
  1773. (with-current-buffer
  1774. (find-file-noselect manifest-file t)
  1775. (insert
  1776. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  1777. <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
  1778. (mapc
  1779. (lambda (file-entry)
  1780. (let* ((version (nth 2 file-entry))
  1781. (extra (if version
  1782. (format " manifest:version=\"%s\"" version)
  1783. "")))
  1784. (insert
  1785. (format org-odt-manifest-file-entry-tag
  1786. (nth 0 file-entry) (nth 1 file-entry) extra))))
  1787. org-odt-manifest-file-entries)
  1788. (insert "\n</manifest:manifest>"))))
  1789. (defun org-odt-update-meta-file (opt-plist)
  1790. (let ((date (org-odt-iso-date-from-org-timestamp
  1791. (plist-get opt-plist :date)))
  1792. (author (or (plist-get opt-plist :author) ""))
  1793. (email (plist-get opt-plist :email))
  1794. (keywords (plist-get opt-plist :keywords))
  1795. (description (plist-get opt-plist :description))
  1796. (title (plist-get opt-plist :title)))
  1797. (write-region
  1798. (concat
  1799. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  1800. <office:document-meta
  1801. xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
  1802. xmlns:xlink=\"http://www.w3.org/1999/xlink\"
  1803. xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
  1804. xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
  1805. xmlns:ooo=\"http://openoffice.org/2004/office\"
  1806. office:version=\"1.2\">
  1807. <office:meta>" "\n"
  1808. (org-odt-format-author)
  1809. (org-odt-format-tags
  1810. '("\n<meta:initial-creator>" . "</meta:initial-creator>") author)
  1811. (org-odt-format-tags '("\n<dc:date>" . "</dc:date>") date)
  1812. (org-odt-format-tags
  1813. '("\n<meta:creation-date>" . "</meta:creation-date>") date)
  1814. (org-odt-format-tags '("\n<meta:generator>" . "</meta:generator>")
  1815. (when org-export-creator-info
  1816. (format "Org-%s/Emacs-%s"
  1817. org-version emacs-version)))
  1818. (org-odt-format-tags '("\n<meta:keyword>" . "</meta:keyword>") keywords)
  1819. (org-odt-format-tags '("\n<dc:subject>" . "</dc:subject>") description)
  1820. (org-odt-format-tags '("\n<dc:title>" . "</dc:title>") title)
  1821. "\n"
  1822. " </office:meta>" "</office:document-meta>")
  1823. nil (expand-file-name "meta.xml")))
  1824. ;; create a manifest entry for meta.xml
  1825. (org-odt-create-manifest-file-entry "text/xml" "meta.xml"))
  1826. (defun org-odt-update-styles-file (opt-plist)
  1827. ;; write styles file
  1828. (let ((styles-file (plist-get opt-plist :odt-styles-file)))
  1829. (org-odt-copy-styles-file (and styles-file
  1830. (read (org-trim styles-file)))))
  1831. ;; Update styles.xml - take care of outline numbering
  1832. (with-current-buffer
  1833. (find-file-noselect (expand-file-name "styles.xml") t)
  1834. ;; Don't make automatic backup of styles.xml file. This setting
  1835. ;; prevents the backedup styles.xml file from being zipped in to
  1836. ;; odt file. This is more of a hackish fix. Better alternative
  1837. ;; would be to fix the zip command so that the output odt file
  1838. ;; includes only the needed files and excludes any auto-generated
  1839. ;; extra files like backups and auto-saves etc etc. Note that
  1840. ;; currently the zip command zips up the entire temp directory so
  1841. ;; that any auto-generated files created under the hood ends up in
  1842. ;; the resulting odt file.
  1843. (set (make-local-variable 'backup-inhibited) t)
  1844. ;; Import local setting of `org-export-with-section-numbers'
  1845. (org-lparse-bind-local-variables opt-plist)
  1846. (org-odt-configure-outline-numbering
  1847. (if org-export-with-section-numbers org-export-headline-levels 0)))
  1848. ;; Write custom stlyes for source blocks
  1849. (org-odt-insert-custom-styles-for-srcblocks
  1850. (mapconcat
  1851. (lambda (style)
  1852. (format " %s\n" (cddr style)))
  1853. hfy-user-sheet-assoc "")))
  1854. (defun org-odt-write-mimetype-file (format)
  1855. ;; create mimetype file
  1856. (let ((mimetype
  1857. (case format
  1858. (odt "application/vnd.oasis.opendocument.text")
  1859. (odf "application/vnd.oasis.opendocument.formula")
  1860. (t (error "Unknown OpenDocument backend %S" org-lparse-backend)))))
  1861. (write-region mimetype nil (expand-file-name "mimetype"))
  1862. mimetype))
  1863. (defun org-odt-finalize-outfile ()
  1864. (org-odt-delete-empty-paragraphs))
  1865. (defun org-odt-delete-empty-paragraphs ()
  1866. (goto-char (point-min))
  1867. (let ((open "<text:p[^>]*>")
  1868. (close "</text:p>"))
  1869. (while (re-search-forward (format "%s[ \r\n\t]*%s" open close) nil t)
  1870. (replace-match ""))))
  1871. (defcustom org-export-odt-convert-processes
  1872. '(("BasicODConverter"
  1873. ("soffice" "-norestore" "-invisible" "-headless"
  1874. "\"macro:///BasicODConverter.Main.Convert(%I,%f,%O)\""))
  1875. ("unoconv"
  1876. ("unoconv" "-f" "%f" "-o" "%d" "%i")))
  1877. "Specify a list of document converters and their usage.
  1878. The converters in this list are offered as choices while
  1879. customizing `org-export-odt-convert-process'.
  1880. This variable is an alist where each element is of the
  1881. form (CONVERTER-NAME CONVERTER-PROCESS). CONVERTER-NAME is name
  1882. of the converter. CONVERTER-PROCESS specifies the command-line
  1883. syntax of the converter and is of the form (CONVERTER-PROGRAM
  1884. ARG1 ARG2 ...). CONVERTER-PROGRAM is the name of the executable.
  1885. ARG1, ARG2 etc are command line options that are passed to
  1886. CONVERTER-PROGRAM. Format specifiers can be used in the ARGs and
  1887. they are interpreted as below:
  1888. %i input file name in full
  1889. %I input file name as a URL
  1890. %f format of the output file
  1891. %o output file name in full
  1892. %O output file name as a URL
  1893. %d output dir in full
  1894. %D output dir as a URL."
  1895. :group 'org-export-odt
  1896. :type
  1897. '(choice
  1898. (const :tag "None" nil)
  1899. (alist :tag "Converters"
  1900. :key-type (string :tag "Converter Name")
  1901. :value-type (group (cons (string :tag "Executable")
  1902. (repeat (string :tag "Command line args")))))))
  1903. (defcustom org-export-odt-convert-process nil
  1904. "Use this converter to convert from \"odt\" format to other formats.
  1905. During customization, the list of converter names are populated
  1906. from `org-export-odt-convert-processes'."
  1907. :group 'org-export-odt
  1908. :type '(choice :convert-widget
  1909. (lambda (w)
  1910. (apply 'widget-convert (widget-type w)
  1911. (eval (car (widget-get w :args)))))
  1912. `((const :tag "None" nil)
  1913. ,@(mapcar (lambda (c)
  1914. `(const :tag ,(car c) ,(car c)))
  1915. org-export-odt-convert-processes))))
  1916. (defcustom org-export-odt-convert-capabilities
  1917. '(("Text"
  1918. ("odt" "ott" "doc" "rtf")
  1919. (("pdf" "pdf") ("odt" "odt") ("xhtml" "html") ("rtf" "rtf")
  1920. ("ott" "ott") ("doc" "doc") ("ooxml" "xml") ("html" "html")))
  1921. ("Web"
  1922. ("html" "xhtml") (("pdf" "pdf") ("odt" "txt") ("html" "html")))
  1923. ("Spreadsheet"
  1924. ("ods" "ots" "xls" "csv")
  1925. (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv")
  1926. ("ods" "ods") ("xls" "xls") ("xhtml" "xhtml") ("ooxml" "xml")))
  1927. ("Presentation"
  1928. ("odp" "otp" "ppt")
  1929. (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("xhtml" "xml")
  1930. ("otp" "otp") ("ppt" "ppt") ("odg" "odg") ("html" "html"))))
  1931. "Specify input and output formats of `org-export-odt-convert-process'.
  1932. More correctly, specify the set of input and output formats that
  1933. the user is actually interested in.
  1934. This variable is an alist where each element is of the
  1935. form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
  1936. INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
  1937. alist where each element is of the form (OUTPUT-FMT
  1938. OUTPUT-FILE-EXTENSION).
  1939. The variable is interpreted as follows:
  1940. `org-export-odt-convert-process' can take any document that is in
  1941. INPUT-FMT-LIST and produce any document that is in the
  1942. OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
  1943. OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
  1944. serves dual purposes:
  1945. - It is used for populating completion candidates during
  1946. `org-export-odt-convert' commands.
  1947. - It is used as the value of \"%f\" specifier in
  1948. `org-export-odt-convert-process'.
  1949. DOCUMENT-CLASS is used to group a set of file formats in
  1950. INPUT-FMT-LIST in to a single class.
  1951. Note that this variable inherently captures how LibreOffice based
  1952. converters work. LibreOffice maps documents of various formats
  1953. to classes like Text, Web, Spreadsheet, Presentation etc and
  1954. allow document of a given class (irrespective of it's source
  1955. format) to be converted to any of the export formats associated
  1956. with that class.
  1957. See default setting of this variable for an typical
  1958. configuration."
  1959. :group 'org-export-odt
  1960. :type
  1961. '(choice
  1962. (const :tag "None" nil)
  1963. (alist :key-type (string :tag "Document Class")
  1964. :value-type
  1965. (group (repeat :tag "Input formats" (string :tag "Input format"))
  1966. (alist :tag "Output formats"
  1967. :key-type (string :tag "Output format")
  1968. :value-type
  1969. (group (string :tag "Output file extension")))))))
  1970. (declare-function org-create-math-formula "org"
  1971. (latex-frag &optional mathml-file))
  1972. ;;;###autoload
  1973. (defun org-export-odt-convert (&optional in-file out-fmt prefix-arg)
  1974. "Convert IN-FILE to format OUT-FMT using a command line converter.
  1975. IN-FILE is the file to be converted. If unspecified, it defaults
  1976. to variable `buffer-file-name'. OUT-FMT is the desired output
  1977. format. Use `org-export-odt-convert-process' as the converter.
  1978. If PREFIX-ARG is non-nil then the newly converted file is opened
  1979. using `org-open-file'."
  1980. (interactive
  1981. (append (org-lparse-convert-read-params) current-prefix-arg))
  1982. (org-lparse-do-convert in-file out-fmt prefix-arg))
  1983. (defun org-odt-get (what &optional opt-plist)
  1984. (case what
  1985. (BACKEND 'odt)
  1986. (EXPORT-DIR (org-export-directory :html opt-plist))
  1987. (FILE-NAME-EXTENSION "odt")
  1988. (EXPORT-BUFFER-NAME "*Org ODT Export*")
  1989. (ENTITY-CONTROL org-odt-entity-control-callbacks-alist)
  1990. (ENTITY-FORMAT org-odt-entity-format-callbacks-alist)
  1991. (INIT-METHOD 'org-odt-init-outfile)
  1992. (FINAL-METHOD 'org-odt-finalize-outfile)
  1993. (SAVE-METHOD 'org-odt-save-as-outfile)
  1994. (CONVERT-METHOD
  1995. (and org-export-odt-convert-process
  1996. (cadr (assoc-string org-export-odt-convert-process
  1997. org-export-odt-convert-processes t))))
  1998. (CONVERT-CAPABILITIES
  1999. (and org-export-odt-convert-process
  2000. (cadr (assoc-string org-export-odt-convert-process
  2001. org-export-odt-convert-processes t))
  2002. org-export-odt-convert-capabilities))
  2003. (TOPLEVEL-HLEVEL 1)
  2004. (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps)
  2005. (INLINE-IMAGES 'maybe)
  2006. (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
  2007. (PLAIN-TEXT-MAP '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
  2008. (TABLE-FIRST-COLUMN-AS-LABELS nil)
  2009. (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY "," 'superscript))
  2010. (CODING-SYSTEM-FOR-WRITE 'utf-8)
  2011. (CODING-SYSTEM-FOR-SAVE 'utf-8)
  2012. (t (error "Unknown property: %s" what))))
  2013. (defvar org-lparse-latex-fragment-fallback) ; set by org-do-lparse
  2014. (defun org-export-odt-do-preprocess-latex-fragments ()
  2015. "Convert LaTeX fragments to images."
  2016. (let* ((latex-frag-opt (plist-get org-lparse-opt-plist :LaTeX-fragments))
  2017. (latex-frag-opt ; massage the options
  2018. (or (and (member latex-frag-opt '(mathjax t))
  2019. (not (and (fboundp 'org-format-latex-mathml-available-p)
  2020. (org-format-latex-mathml-available-p)))
  2021. (prog1 org-lparse-latex-fragment-fallback
  2022. (org-lparse-warn
  2023. (concat
  2024. "LaTeX to MathML converter not available. "
  2025. (format "Using %S instead."
  2026. org-lparse-latex-fragment-fallback)))))
  2027. latex-frag-opt))
  2028. cache-dir display-msg)
  2029. (cond
  2030. ((eq latex-frag-opt 'dvipng)
  2031. (setq cache-dir "ltxpng/")
  2032. (setq display-msg "Creating LaTeX image %s"))
  2033. ((member latex-frag-opt '(mathjax t))
  2034. (setq latex-frag-opt 'mathml)
  2035. (setq cache-dir "ltxmathml/")
  2036. (setq display-msg "Creating MathML formula %s")))
  2037. (when (and org-current-export-file)
  2038. (org-format-latex
  2039. (concat cache-dir (file-name-sans-extension
  2040. (file-name-nondirectory org-current-export-file)))
  2041. org-current-export-dir nil display-msg
  2042. nil nil latex-frag-opt))))
  2043. (defadvice org-format-latex-as-mathml
  2044. (after org-odt-protect-latex-fragment activate)
  2045. "Encode LaTeX fragment as XML.
  2046. Do this when translation to MathML fails."
  2047. (when (or (not (> (length ad-return-value) 0))
  2048. (get-text-property 0 'org-protected ad-return-value))
  2049. (setq ad-return-value
  2050. (org-propertize (org-odt-encode-plain-text (ad-get-arg 0))
  2051. 'org-protected t))))
  2052. (defun org-export-odt-preprocess-latex-fragments ()
  2053. (when (equal org-export-current-backend 'odt)
  2054. (org-export-odt-do-preprocess-latex-fragments)))
  2055. (defun org-export-odt-preprocess-label-references ()
  2056. (goto-char (point-min))
  2057. (let (label label-components category value pretty-label)
  2058. (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t)
  2059. (org-if-unprotected-at (match-beginning 1)
  2060. (replace-match
  2061. (let ((org-lparse-encode-pending t)
  2062. (label (match-string 1)))
  2063. ;; markup generated below is mostly an eye-candy. At
  2064. ;; pre-processing stage, there is no information on which
  2065. ;; entity a label reference points to. The actual markup
  2066. ;; is generated as part of `org-odt-fixup-label-references'
  2067. ;; which gets called at the fag end of export. By this
  2068. ;; time we would have seen and collected all the label
  2069. ;; definitions in `org-odt-entity-labels-alist'.
  2070. (org-odt-format-tags
  2071. '("<text:sequence-ref text:ref-name=\"%s\">" .
  2072. "</text:sequence-ref>")
  2073. "" (org-add-props label '(org-protected t)))) t t)))))
  2074. ;; process latex fragments as part of
  2075. ;; `org-export-preprocess-after-blockquote-hook'. Note that this hook
  2076. ;; is the one that is closest and well before the call to
  2077. ;; `org-export-attach-captions-and-attributes' in
  2078. ;; `org-export-preprocess-stirng'. The above arrangement permits
  2079. ;; captions, labels and attributes to be attached to png images
  2080. ;; generated out of latex equations.
  2081. (add-hook 'org-export-preprocess-after-blockquote-hook
  2082. 'org-export-odt-preprocess-latex-fragments)
  2083. (defun org-export-odt-preprocess (parameters)
  2084. (org-export-odt-preprocess-label-references))
  2085. (declare-function archive-zip-extract "arc-mode.el" (archive name))
  2086. (defun org-odt-zip-extract-one (archive member &optional target)
  2087. (require 'arc-mode)
  2088. (let* ((target (or target default-directory))
  2089. (archive (expand-file-name archive))
  2090. (archive-zip-extract
  2091. (list "unzip" "-qq" "-o" "-d" target))
  2092. exit-code command-output)
  2093. (setq command-output
  2094. (with-temp-buffer
  2095. (setq exit-code (archive-zip-extract archive member))
  2096. (buffer-string)))
  2097. (unless (zerop exit-code)
  2098. (message command-output)
  2099. (error "Extraction failed"))))
  2100. (defun org-odt-zip-extract (archive members &optional target)
  2101. (when (atom members) (setq members (list members)))
  2102. (mapc (lambda (member)
  2103. (org-odt-zip-extract-one archive member target))
  2104. members))
  2105. (defun org-odt-copy-styles-file (&optional styles-file)
  2106. ;; Non-availability of styles.xml is not a critical error. For now
  2107. ;; throw an error purely for aesthetic reasons.
  2108. (setq styles-file (or styles-file
  2109. org-export-odt-styles-file
  2110. (expand-file-name "OrgOdtStyles.xml"
  2111. org-odt-styles-dir)
  2112. (error "org-odt: Missing styles file?")))
  2113. (cond
  2114. ((listp styles-file)
  2115. (let ((archive (nth 0 styles-file))
  2116. (members (nth 1 styles-file)))
  2117. (org-odt-zip-extract archive members)
  2118. (mapc
  2119. (lambda (member)
  2120. (when (org-file-image-p member)
  2121. (let* ((image-type (file-name-extension member))
  2122. (media-type (format "image/%s" image-type)))
  2123. (org-odt-create-manifest-file-entry media-type member))))
  2124. members)))
  2125. ((and (stringp styles-file) (file-exists-p styles-file))
  2126. (let ((styles-file-type (file-name-extension styles-file)))
  2127. (cond
  2128. ((string= styles-file-type "xml")
  2129. (copy-file styles-file "styles.xml" t))
  2130. ((member styles-file-type '("odt" "ott"))
  2131. (org-odt-zip-extract styles-file "styles.xml")))))
  2132. (t
  2133. (error (format "Invalid specification of styles.xml file: %S"
  2134. org-export-odt-styles-file))))
  2135. ;; create a manifest entry for styles.xml
  2136. (org-odt-create-manifest-file-entry "text/xml" "styles.xml"))
  2137. (defvar org-export-odt-factory-settings
  2138. "d4328fb9d1b6cb211d4320ff546829f26700dc5e"
  2139. "SHA1 hash of OrgOdtStyles.xml.")
  2140. (defun org-odt-configure-outline-numbering (level)
  2141. "Outline numbering is retained only upto LEVEL.
  2142. To disable outline numbering pass a LEVEL of 0."
  2143. (goto-char (point-min))
  2144. (let ((regex
  2145. "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
  2146. (replacement
  2147. "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
  2148. (while (re-search-forward regex nil t)
  2149. (when (> (string-to-number (match-string 2)) level)
  2150. (replace-match replacement t nil))))
  2151. (save-buffer 0))
  2152. ;;;###autoload
  2153. (defun org-export-as-odf (latex-frag &optional odf-file)
  2154. "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
  2155. Use `org-create-math-formula' to convert LATEX-FRAG first to
  2156. MathML. When invoked as an interactive command, use
  2157. `org-latex-regexps' to infer LATEX-FRAG from currently active
  2158. region. If no LaTeX fragments are found, prompt for it. Push
  2159. MathML source to kill ring, if `org-export-copy-to-kill-ring' is
  2160. non-nil."
  2161. (interactive
  2162. `(,(let (frag)
  2163. (setq frag (and (setq frag (and (region-active-p)
  2164. (buffer-substring (region-beginning)
  2165. (region-end))))
  2166. (loop for e in org-latex-regexps
  2167. thereis (when (string-match (nth 1 e) frag)
  2168. (match-string (nth 2 e) frag)))))
  2169. (read-string "LaTeX Fragment: " frag nil frag))
  2170. ,(let ((odf-filename (expand-file-name
  2171. (concat
  2172. (file-name-sans-extension
  2173. (or (file-name-nondirectory buffer-file-name)))
  2174. "." "odf")
  2175. (file-name-directory buffer-file-name))))
  2176. (message "default val is %s" odf-filename)
  2177. (read-file-name "ODF filename: " nil odf-filename nil
  2178. (file-name-nondirectory odf-filename)))))
  2179. (let* ((org-lparse-backend 'odf)
  2180. org-lparse-opt-plist
  2181. (filename (or odf-file
  2182. (expand-file-name
  2183. (concat
  2184. (file-name-sans-extension
  2185. (or (file-name-nondirectory buffer-file-name)))
  2186. "." "odf")
  2187. (file-name-directory buffer-file-name))))
  2188. (buffer (find-file-noselect (org-odt-init-outfile filename)))
  2189. (coding-system-for-write 'utf-8)
  2190. (save-buffer-coding-system 'utf-8))
  2191. (set-buffer buffer)
  2192. (set-buffer-file-coding-system coding-system-for-write)
  2193. (let ((mathml (org-create-math-formula latex-frag)))
  2194. (unless mathml (error "No Math formula created"))
  2195. (insert mathml)
  2196. (or (org-export-push-to-kill-ring
  2197. (upcase (symbol-name org-lparse-backend)))
  2198. (message "Exporting... done")))
  2199. (org-odt-save-as-outfile filename nil)))
  2200. ;;;###autoload
  2201. (defun org-export-as-odf-and-open ()
  2202. "Export LaTeX fragment as OpenDocument formula and immediately open it.
  2203. Use `org-export-as-odf' to read LaTeX fragment and OpenDocument
  2204. formula file."
  2205. (interactive)
  2206. (org-lparse-and-open
  2207. nil nil nil (call-interactively 'org-export-as-odf)))
  2208. (provide 'org-odt)
  2209. ;;; org-odt.el ends here