org-odt.el 103 KB

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