org-odt.el 65 KB

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