org-odt.el 72 KB

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