org-odt.el 65 KB

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