org-odt.el 89 KB

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