org-odt.el 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772
  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-1)
  553. (org-lparse-end-list 'description)
  554. (org-lparse-end-list-item-1))
  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-get-paragraph-style-cookie-for-table-cell (r c)
  629. (concat
  630. (cond
  631. (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
  632. ((and (= c 0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS))
  633. "OrgTableHeading")
  634. (t "OrgTableContents"))
  635. (and org-lparse-table-is-styled
  636. (format "@@table-cell:p@@%03d@@%03d@@" r c))))
  637. (defun org-odt-get-style-name-cookie-for-table-cell (r c)
  638. (when org-lparse-table-is-styled
  639. (format "@@table-cell:style-name@@%03d@@%03d@@" r c)))
  640. (defun org-odt-format-table-cell (data r c)
  641. (let* ((paragraph-style-cookie
  642. (org-odt-get-paragraph-style-cookie-for-table-cell r c))
  643. (style-name-cookie
  644. (org-odt-get-style-name-cookie-for-table-cell r c))
  645. (extra (if style-name-cookie
  646. (format " table:style-name=\"%s\"" style-name-cookie) "")))
  647. (org-odt-format-tags
  648. '("<table:table-cell%s>" . "</table:table-cell>")
  649. (if org-lparse-list-table-p data
  650. (org-odt-format-stylized-paragraph paragraph-style-cookie data)) extra)))
  651. (defun org-odt-begin-footnote-definition (n)
  652. (org-lparse-begin-paragraph 'footnote))
  653. (defun org-odt-end-footnote-definition (n)
  654. (org-lparse-end-paragraph))
  655. (defun org-odt-begin-toc (lang-specific-heading)
  656. (insert
  657. (format "
  658. <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
  659. <text:table-of-content-source text:outline-level=\"10\">
  660. <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
  661. " lang-specific-heading))
  662. (loop for level from 1 upto 10
  663. do (insert (format
  664. "
  665. <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
  666. <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
  667. <text:index-entry-chapter/>
  668. <text:index-entry-text/>
  669. <text:index-entry-link-end/>
  670. </text:table-of-content-entry-template>
  671. " level level)))
  672. (insert
  673. (format "
  674. </text:table-of-content-source>
  675. <text:index-body>
  676. <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
  677. <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
  678. </text:index-title>
  679. " lang-specific-heading)))
  680. (defun org-odt-end-toc ()
  681. (insert "
  682. </text:index-body>
  683. </text:table-of-content>
  684. "))
  685. (defun org-odt-format-toc-entry (snumber todo headline tags href)
  686. (setq headline (concat
  687. (and org-export-with-section-numbers
  688. (concat snumber ". "))
  689. headline
  690. (and tags
  691. (concat
  692. (org-lparse-format 'SPACES 3)
  693. (org-lparse-format 'FONTIFY tags "tag")))))
  694. (when todo
  695. (setq headline (org-lparse-format 'FONTIFY headline "todo")))
  696. (let ((org-odt-suppress-xref t))
  697. (org-odt-format-link headline (concat "#" href))))
  698. (defun org-odt-format-toc-item (toc-entry level org-last-level)
  699. (let ((style (format "Contents_20_%d"
  700. (+ level (or (org-lparse-get 'TOPLEVEL-HLEVEL) 1) -1))))
  701. (insert "\n" (org-odt-format-stylized-paragraph style toc-entry) "\n")))
  702. ;; Following variable is let bound during 'ORG-LINK callback. See
  703. ;; org-html.el
  704. (defvar org-lparse-link-description-is-image nil)
  705. (defun org-odt-format-link (desc href &optional attr)
  706. (cond
  707. ((and (= (string-to-char href) ?#) (not org-odt-suppress-xref))
  708. (setq href (concat org-export-odt-bookmark-prefix (substring href 1)))
  709. (org-odt-format-tags
  710. '("<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"%s\">" .
  711. "</text:bookmark-ref>")
  712. desc href))
  713. (org-lparse-link-description-is-image
  714. (org-odt-format-tags
  715. '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</draw:a>")
  716. desc href (or attr "")))
  717. (t
  718. (org-odt-format-tags
  719. '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</text:a>")
  720. desc href (or attr "")))))
  721. (defun org-odt-format-spaces (n)
  722. (cond
  723. ((= n 1) " ")
  724. ((> n 1) (concat
  725. " " (org-odt-format-tags "<text:s text:c=\"%d\"/>" "" (1- n))))
  726. (t "")))
  727. (defun org-odt-format-tabs (&optional n)
  728. (let ((tab "<text:tab/>")
  729. (n (or n 1)))
  730. (insert tab)))
  731. (defun org-odt-format-line-break ()
  732. (org-odt-format-tags "<text:line-break/>" ""))
  733. (defun org-odt-format-horizontal-line ()
  734. (org-odt-format-stylized-paragraph 'horizontal-line ""))
  735. (defun org-odt-format-line (line)
  736. (case org-lparse-dyn-current-environment
  737. (fixedwidth (concat
  738. (org-odt-format-stylized-paragraph
  739. 'fixedwidth (org-odt-fill-tabs-and-spaces
  740. (org-xml-encode-plain-text line))) "\n"))
  741. (t (concat line "\n"))))
  742. (defun org-odt-format-comment (fmt &rest args)
  743. (let ((comment (apply 'format fmt args)))
  744. (format "\n<!-- %s -->\n" comment)))
  745. (defun org-odt-format-org-entity (wd)
  746. (org-entity-get-representation wd 'utf8))
  747. (defun org-odt-fill-tabs-and-spaces (line)
  748. (replace-regexp-in-string
  749. "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
  750. (cond
  751. ((string= s "\t") (org-odt-format-tabs))
  752. (t (org-odt-format-spaces (length s))))) line))
  753. (defcustom org-export-odt-use-htmlfontify t
  754. "Specify whether or not source blocks need to be fontified.
  755. Turn this option on if you want to colorize the source code
  756. blocks in the exported file. For colorization to work, you need
  757. to make available an enhanced version of `htmlfontify' library."
  758. :type 'boolean
  759. :group 'org-export-odt)
  760. (defun org-odt-format-source-code-or-example-plain
  761. (lines lang caption textareap cols rows num cont rpllbl fmt)
  762. "Format source or example blocks much like fixedwidth blocks.
  763. Use this when `org-export-odt-use-htmlfontify' option is turned
  764. off."
  765. (setq lines (org-export-number-lines (org-xml-encode-plain-text-lines lines)
  766. 0 0 num cont rpllbl fmt))
  767. (mapconcat
  768. (lambda (line)
  769. (org-odt-format-stylized-paragraph
  770. 'fixedwidth (org-odt-fill-tabs-and-spaces line)))
  771. (org-split-string lines "[\r\n]") "\n"))
  772. (defvar org-src-block-paragraph-format
  773. "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
  774. <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
  775. <style:background-image/>
  776. </style:paragraph-properties>
  777. <style:text-properties fo:color=\"%s\"/>
  778. </style:style>"
  779. "Custom paragraph style for colorized source and example blocks.
  780. This style is much the same as that of \"OrgFixedWidthBlock\"
  781. except that the foreground and background colors are set
  782. according to the default face identified by the `htmlfontify'.")
  783. (defun org-odt-hfy-face-to-css (fn)
  784. "Create custom style for face FN.
  785. When FN is the default face, use it's foreground and background
  786. properties to create \"OrgSrcBlock\" paragraph style. Otherwise
  787. use it's color attribute to create a character style whose name
  788. is obtained from FN. Currently all attributes of FN other than
  789. color are ignored.
  790. The style name for a face FN is derived using the following
  791. operations on the face name in that order - de-dash, CamelCase
  792. and prefix with \"OrgSrc\". For example,
  793. `font-lock-function-name-face' is associated with
  794. \"OrgSrcFontLockFunctionNameFace\"."
  795. (let* ((css-list (hfy-face-to-style fn))
  796. (style-name ((lambda (fn)
  797. (concat "OrgSrc"
  798. (mapconcat
  799. 'capitalize (split-string
  800. (hfy-face-or-def-to-name fn) "-")
  801. ""))) fn))
  802. (color-val (cdr (assoc "color" css-list)))
  803. (background-color-val (cdr (assoc "background" css-list)))
  804. (style (and org-export-odt-create-custom-styles-for-srcblocks
  805. (cond
  806. ((eq fn 'default)
  807. (format org-src-block-paragraph-format
  808. background-color-val color-val))
  809. (t
  810. (format
  811. "
  812. <style:style style:name=\"%s\" style:family=\"text\">
  813. <style:text-properties fo:color=\"%s\"/>
  814. </style:style>" style-name color-val))))))
  815. (cons style-name style)))
  816. (defcustom org-export-odt-create-custom-styles-for-srcblocks t
  817. "Whether custom styles for colorized source blocks be automatically created.
  818. When this option is turned on, the exporter creates custom styles
  819. for source blocks based on the advice of `htmlfontify'. Creation
  820. of custom styles happen as part of `org-odt-hfy-face-to-css'.
  821. When this option is turned off exporter does not create such
  822. styles.
  823. Use the latter option if you do not want the custom styles to be
  824. based on your current display settings. It is necessary that the
  825. styles.xml already contains needed styles for colorizing to work.
  826. This variable is effective only if
  827. `org-export-odt-use-htmlfontify' is turned on."
  828. :group 'org-export-odt
  829. :type 'boolean)
  830. (defun org-odt-insert-custom-styles-for-srcblocks (styles)
  831. "Save STYLES used for colorizing of source blocks.
  832. Update styles.xml with styles that were collected as part of
  833. `org-odt-hfy-face-to-css' callbacks."
  834. (when styles
  835. (with-current-buffer
  836. (find-file-noselect (expand-file-name "styles.xml") t)
  837. (goto-char (point-min))
  838. (when (re-search-forward "</office:styles>" nil t)
  839. (goto-char (match-beginning 0))
  840. (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n")))))
  841. (defun org-odt-format-source-code-or-example-colored
  842. (lines lang caption textareap cols rows num cont rpllbl fmt)
  843. "Format source or example blocks using `htmlfontify-string'.
  844. Use this routine when `org-export-odt-use-htmlfontify' option is
  845. turned on."
  846. (let* ((lang-m (and lang (or (cdr (assoc lang org-src-lang-modes)) lang)))
  847. (mode (and lang-m (intern (concat (if (symbolp lang-m)
  848. (symbol-name lang-m)
  849. lang-m) "-mode"))))
  850. (org-inhibit-startup t)
  851. (org-startup-folded nil)
  852. (lines (with-temp-buffer
  853. (insert lines)
  854. (if (functionp mode) (funcall mode) (fundamental-mode))
  855. (font-lock-fontify-buffer)
  856. (buffer-string)))
  857. (hfy-html-quote-regex "\\([<\"&> ]\\)")
  858. (hfy-html-quote-map '(("\"" "&quot;")
  859. ("<" "&lt;")
  860. ("&" "&amp;")
  861. (">" "&gt;")
  862. (" " "<text:s/>")
  863. (" " "<text:tab/>")))
  864. (hfy-face-to-css 'org-odt-hfy-face-to-css)
  865. (hfy-optimisations-1 (copy-seq hfy-optimisations))
  866. (hfy-optimisations (add-to-list 'hfy-optimisations-1
  867. 'body-text-only))
  868. (hfy-begin-span-handler
  869. (lambda (style text-block text-id text-begins-block-p)
  870. (insert (format "<text:span text:style-name=\"%s\">" style))))
  871. (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
  872. (when (fboundp 'htmlfontify-string)
  873. (mapconcat
  874. (lambda (line)
  875. (org-odt-format-stylized-paragraph 'src (htmlfontify-string line)))
  876. (org-split-string lines "[\r\n]") "\n"))))
  877. (defun org-odt-format-source-code-or-example (lines lang caption textareap
  878. cols rows num cont
  879. rpllbl fmt)
  880. "Format source or example blocks for export.
  881. Use `org-odt-format-source-code-or-example-plain' or
  882. `org-odt-format-source-code-or-example-colored' depending on the
  883. value of `org-export-odt-use-htmlfontify."
  884. (funcall
  885. (if (and org-export-odt-use-htmlfontify
  886. (or (featurep 'htmlfontify) (require 'htmlfontify))
  887. (fboundp 'htmlfontify-string))
  888. 'org-odt-format-source-code-or-example-colored
  889. 'org-odt-format-source-code-or-example-plain)
  890. lines lang caption textareap cols rows num cont rpllbl fmt))
  891. (defun org-xml-encode-plain-text-lines (rtn)
  892. (mapconcat 'org-xml-encode-plain-text (org-split-string rtn "[\r\n]") "\n"))
  893. (defun org-odt-remap-stylenames (style-name)
  894. (or
  895. (cdr (assoc style-name '(("timestamp-wrapper" . "OrgTimestampWrapper")
  896. ("timestamp" . "OrgTimestamp")
  897. ("timestamp-kwd" . "OrgTimestampKeyword")
  898. ("tag" . "OrgTag")
  899. ("todo" . "OrgTodo")
  900. ("done" . "OrgDone")
  901. ("target" . "OrgTarget"))))
  902. style-name))
  903. (defun org-odt-format-fontify (text style &optional id)
  904. (let* ((style-name
  905. (cond
  906. ((stringp style)
  907. (org-odt-remap-stylenames style))
  908. ((symbolp style)
  909. (org-odt-get-style-name-for-entity 'character style))
  910. ((listp style)
  911. (assert (< 1 (length style)))
  912. (let ((parent-style (pop style)))
  913. (mapconcat (lambda (s)
  914. ;; (assert (stringp s) t)
  915. (org-odt-remap-stylenames s)) style "")
  916. (org-odt-remap-stylenames parent-style)))
  917. (t (error "Don't how to handle style %s" style)))))
  918. (org-odt-format-tags
  919. '("<text:span text:style-name=\"%s\">" . "</text:span>")
  920. text style-name)))
  921. (defun org-odt-relocate-relative-path (path dir)
  922. (if (file-name-absolute-p path) path
  923. (file-relative-name (expand-file-name path dir)
  924. (expand-file-name "eyecandy" dir))))
  925. (defun org-odt-format-inline-image (thefile)
  926. (let* ((thelink (if (file-name-absolute-p thefile) thefile
  927. (org-xml-format-href
  928. (org-odt-relocate-relative-path
  929. thefile org-current-export-file))))
  930. (href
  931. (org-odt-format-tags
  932. "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
  933. (if org-export-odt-embed-images
  934. (org-odt-copy-image-file thefile) thelink))))
  935. (org-export-odt-format-image thefile href)))
  936. (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
  937. descp)
  938. "Make an HTML link.
  939. OPT-PLIST is an options list.
  940. TYPE is the device-type of the link (THIS://foo.html)
  941. PATH is the path of the link (http://THIS#locationx)
  942. FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
  943. DESC is the link description, if any.
  944. ATTR is a string of other attributes of the a element.
  945. MAY-INLINE-P allows inlining it as an image."
  946. (declare (special org-lparse-par-open))
  947. (save-match-data
  948. (let* ((may-inline-p
  949. (and (member type-1 '("http" "https" "file"))
  950. (org-lparse-should-inline-p path descp)
  951. (not fragment)))
  952. (type (if (equal type-1 "id") "file" type-1))
  953. (filename path)
  954. (thefile path))
  955. (cond
  956. ;; check for inlined images
  957. ((and (member type '("file"))
  958. (not fragment)
  959. (org-file-image-p
  960. filename org-odt-export-inline-image-extensions)
  961. (or (eq t org-odt-export-inline-images)
  962. (and org-odt-export-inline-images (not descp))))
  963. ;; (when (and (string= type "file") (file-name-absolute-p path))
  964. ;; (setq thefile (concat "file://" (expand-file-name path))))
  965. ;; (setq thefile (org-xml-format-href thefile))
  966. ;; (org-export-html-format-image thefile)
  967. (org-odt-format-inline-image thefile))
  968. (t
  969. (when (string= type "file")
  970. (setq thefile
  971. (cond
  972. ((file-name-absolute-p path)
  973. (concat "file://" (expand-file-name path)))
  974. (t (org-odt-relocate-relative-path
  975. thefile org-current-export-file)))))
  976. (when (and (member type '("" "http" "https" "file" "coderef"))
  977. fragment)
  978. (setq thefile (concat thefile "#" fragment)))
  979. (setq thefile (org-xml-format-href thefile))
  980. (when (not (member type '("" "file" "coderef")))
  981. (setq thefile (concat type ":" thefile)))
  982. (let ((org-odt-suppress-xref (string= type "coderef")))
  983. (org-odt-format-link
  984. (org-xml-format-desc desc) thefile attr)))))))
  985. (defun org-odt-format-heading (text level &optional id)
  986. (let* ((text (if id (org-odt-format-target text id) text)))
  987. (org-odt-format-tags
  988. '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
  989. "</text:h>") text level level)))
  990. (defun org-odt-format-headline (title extra-targets tags
  991. &optional snumber level)
  992. (concat
  993. (org-lparse-format 'EXTRA-TARGETS extra-targets)
  994. ;; No need to generate section numbers. They are auto-generated by
  995. ;; the application
  996. ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
  997. title
  998. (and tags (concat (org-lparse-format 'SPACES 3)
  999. (org-lparse-format 'ORG-TAGS tags)))))
  1000. (defun org-odt-format-anchor (text name &optional class)
  1001. (org-odt-format-target text name))
  1002. (defun org-odt-format-bookmark (text id)
  1003. (if id
  1004. (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id)
  1005. text))
  1006. (defun org-odt-format-target (text id)
  1007. (let ((name (concat org-export-odt-bookmark-prefix id)))
  1008. (concat
  1009. (and id (org-odt-format-tags
  1010. "<text:bookmark-start text:name=\"%s\"/>" "" name))
  1011. (org-odt-format-bookmark text id)
  1012. (and id (org-odt-format-tags
  1013. "<text:bookmark-end text:name=\"%s\"/>" "" name)))))
  1014. (defun org-odt-format-footnote (n def)
  1015. (let ((id (concat "fn" n))
  1016. (note-class "footnote")
  1017. (par-style "Footnote"))
  1018. (org-odt-format-tags
  1019. '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
  1020. "</text:note>")
  1021. (concat
  1022. (org-odt-format-tags
  1023. '("<text:note-citation>" . "</text:note-citation>")
  1024. n)
  1025. (org-odt-format-tags
  1026. '("<text:note-body>" . "</text:note-body>")
  1027. def))
  1028. id note-class)))
  1029. (defun org-odt-format-footnote-reference (n def refcnt)
  1030. (if (= refcnt 1)
  1031. (org-odt-format-footnote n def)
  1032. (org-odt-format-footnote-ref n)))
  1033. (defun org-odt-format-footnote-ref (n)
  1034. (let ((note-class "footnote")
  1035. (ref-format "text")
  1036. (ref-name (concat "fn" n)))
  1037. (org-odt-format-tags
  1038. '("<text:span text:style-name=\"%s\">" . "</text:span>")
  1039. (org-odt-format-tags
  1040. '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" . "</text:note-ref>")
  1041. n note-class ref-format ref-name)
  1042. "OrgSuperscript")))
  1043. (defun org-odt-get-image-name (file-name)
  1044. (require 'sha1)
  1045. (file-relative-name
  1046. (expand-file-name
  1047. (concat (sha1 file-name) "." (file-name-extension file-name)) "Pictures")))
  1048. (defun org-export-odt-format-image (src href)
  1049. "Create image tag with source and attributes."
  1050. (save-match-data
  1051. (let* ((caption (org-find-text-property-in-string 'org-caption src))
  1052. (caption (and caption (org-xml-format-desc caption)))
  1053. (attr (org-find-text-property-in-string 'org-attributes src))
  1054. (label (org-find-text-property-in-string 'org-label src))
  1055. (embed-as (if (string-match "^ltxpng/" src) 'character 'paragraph))
  1056. (attr-plist (when attr (read attr)))
  1057. (size (org-odt-image-size-from-file
  1058. src (plist-get attr-plist :width)
  1059. (plist-get attr-plist :height)
  1060. (plist-get attr-plist :scale) nil embed-as)))
  1061. (org-export-odt-do-format-image
  1062. embed-as caption attr label size href))))
  1063. (defun org-odt-format-textbox (text style)
  1064. (let ((draw-frame-pair
  1065. '("<draw:frame draw:style-name=\"%s\"
  1066. text:anchor-type=\"paragraph\"
  1067. style:rel-width=\"100%%\"
  1068. draw:z-index=\"0\">" . "</draw:frame>")))
  1069. (org-odt-format-tags
  1070. draw-frame-pair
  1071. (org-odt-format-tags
  1072. '("<draw:text-box fo:min-height=\"%dcm\">" . "</draw:text-box>")
  1073. text 0) style)))
  1074. (defun org-odt-format-inlinetask (heading content
  1075. &optional todo priority tags)
  1076. (org-odt-format-stylized-paragraph
  1077. nil (org-odt-format-textbox
  1078. (concat (org-odt-format-stylized-paragraph
  1079. "OrgInlineTaskHeading"
  1080. (org-lparse-format
  1081. 'HEADLINE (concat (org-lparse-format-todo todo) " " heading)
  1082. nil tags))
  1083. content) "OrgInlineTaskFrame")))
  1084. (defun org-export-odt-do-format-image (embed-as caption attr label
  1085. size href)
  1086. "Create image tag with source and attributes."
  1087. (save-match-data
  1088. (let ((width (car size)) (height (cdr size))
  1089. (draw-frame-pair
  1090. '("<draw:frame draw:style-name=\"%s\"
  1091. text:anchor-type=\"%s\"
  1092. draw:z-index=\"%d\" %s>" . "</draw:frame>")))
  1093. (cond
  1094. ((and (not caption) (not label))
  1095. (let (style-name anchor-type)
  1096. (cond
  1097. ((eq embed-as 'paragraph)
  1098. (setq style-name "OrgGraphicsParagraph" anchor-type "paragraph"))
  1099. ((eq embed-as 'character)
  1100. (setq style-name "OrgGraphicsBaseline" anchor-type "as-char")))
  1101. (org-odt-format-tags
  1102. draw-frame-pair href style-name anchor-type 0
  1103. (org-odt-image-attrs-from-size width height))))
  1104. (t
  1105. (concat
  1106. ;; (when par-open (org-odt-close-par))
  1107. (org-odt-format-tags
  1108. draw-frame-pair
  1109. (org-odt-format-tags
  1110. '("<draw:text-box fo:min-height=\"%dcm\">" . "</draw:text-box>")
  1111. (org-odt-format-stylized-paragraph
  1112. 'illustration
  1113. (concat
  1114. (let ((extra " style:rel-width=\"100%\" style:rel-height=\"scale\""))
  1115. (org-odt-format-tags
  1116. draw-frame-pair href "OrgGraphicsParagraphContent" "paragraph" 2
  1117. (concat (org-odt-image-attrs-from-size width height) extra)))
  1118. (org-odt-format-entity-caption label caption)))
  1119. height)
  1120. "OrgFrame" "paragraph" 1
  1121. (org-odt-image-attrs-from-size width))
  1122. ;; (when par-open (org-odt-open-par))
  1123. ))))))
  1124. (defvar org-odt-embedded-images-count 0)
  1125. (defun org-odt-copy-image-file (path)
  1126. "Returns the internal name of the file"
  1127. (let* ((image-type (file-name-extension path))
  1128. (media-type (format "image/%s" image-type))
  1129. (src-file (expand-file-name
  1130. path (file-name-directory org-current-export-file)))
  1131. (target-dir "Images/")
  1132. (target-file
  1133. (format "%s%04d.%s" target-dir
  1134. (incf org-odt-embedded-images-count) image-type)))
  1135. (when (not org-lparse-to-buffer)
  1136. (message "Embedding %s as %s ..."
  1137. (substring-no-properties path) target-file)
  1138. (when (= 1 org-odt-embedded-images-count)
  1139. (make-directory target-dir)
  1140. (org-odt-create-manifest-file-entry "" target-dir))
  1141. (copy-file src-file target-file 'overwrite)
  1142. (org-odt-create-manifest-file-entry media-type target-file))
  1143. target-file))
  1144. (defun org-odt-image-attrs-from-size (&optional width height)
  1145. (concat
  1146. (when width (format "svg:width=\"%0.2fcm\"" width))
  1147. " "
  1148. (when height (format "svg:height=\"%0.2fcm\"" height))))
  1149. (defvar org-export-odt-image-size-probe-method
  1150. '(emacs imagemagick force)
  1151. "Ordered list of methods by for determining size of an embedded
  1152. image.")
  1153. (defvar org-export-odt-default-image-sizes-alist
  1154. '(("character" . (5 . 0.4))
  1155. ("paragraph" . (5 . 5)))
  1156. "Hardcoded image dimensions one for each of the anchor
  1157. methods.")
  1158. (defun org-odt-do-image-size (probe-method file &optional dpi anchor-type)
  1159. (setq dpi (or dpi org-export-odt-pixels-per-inch))
  1160. (setq anchor-type (or anchor-type "paragraph"))
  1161. (flet ((size-in-cms (size-in-pixels)
  1162. (flet ((pixels-to-cms (pixels)
  1163. (let* ((cms-per-inch 2.54)
  1164. (inches (/ pixels dpi)))
  1165. (* cms-per-inch inches))))
  1166. (and size-in-pixels
  1167. (cons (pixels-to-cms (car size-in-pixels))
  1168. (pixels-to-cms (cdr size-in-pixels)))))))
  1169. (case probe-method
  1170. (emacs
  1171. (size-in-cms (ignore-errors (image-size (create-image file) 'pixels))))
  1172. (imagemagick
  1173. (size-in-cms
  1174. (let ((dim (shell-command-to-string
  1175. (format "identify -format \"%%w:%%h\" \"%s\"" file))))
  1176. (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
  1177. (cons (string-to-number (match-string 1 dim))
  1178. (string-to-number (match-string 2 dim)))))))
  1179. (t
  1180. (cdr (assoc-string anchor-type
  1181. org-export-odt-default-image-sizes-alist))))))
  1182. (defun org-odt-image-size-from-file (file &optional user-width
  1183. user-height scale dpi embed-as)
  1184. (unless (file-name-absolute-p file)
  1185. (setq file (expand-file-name
  1186. file (file-name-directory org-current-export-file))))
  1187. (let* (size width height)
  1188. (unless (and user-height user-width)
  1189. (loop for probe-method in org-export-odt-image-size-probe-method
  1190. until size
  1191. do (setq size (org-odt-do-image-size
  1192. probe-method file dpi embed-as)))
  1193. (or size (error "Cannot determine Image size. Aborting ..."))
  1194. (setq width (car size) height (cdr size)))
  1195. (cond
  1196. (scale
  1197. (setq width (* width scale) height (* height scale)))
  1198. ((and user-height user-width)
  1199. (setq width user-width height user-height))
  1200. (user-height
  1201. (setq width (* user-height (/ width height)) height user-height))
  1202. (user-width
  1203. (setq height (* user-width (/ height width)) width user-width))
  1204. (t (ignore)))
  1205. (cons width height)))
  1206. (defvar org-odt-default-entity "Illustration")
  1207. (defun org-odt-format-entity-caption (label caption &optional default-entity)
  1208. (if (not label) (or caption "")
  1209. (let* ((label-components (org-odt-parse-label label))
  1210. (entity (car label-components))
  1211. (seqno (cdr label-components))
  1212. (caption (and caption (concat ": " caption))))
  1213. (unless seqno
  1214. (setq seqno label
  1215. entity (or default-entity org-odt-default-entity)))
  1216. (concat
  1217. entity " "
  1218. (org-odt-format-tags
  1219. '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" . "</text:sequence>")
  1220. seqno label entity entity)
  1221. caption))))
  1222. (defun org-odt-format-tags (tag text &rest args)
  1223. (let ((prefix (when org-lparse-encode-pending "@"))
  1224. (suffix (when org-lparse-encode-pending "@")))
  1225. (apply 'org-lparse-format-tags tag text prefix suffix args)))
  1226. (defun org-odt-init-outfile (filename)
  1227. (unless (executable-find "zip")
  1228. ;; Not at all OSes ship with zip by default
  1229. (error "Executable \"zip\" needed for creating OpenDocument files"))
  1230. (let* ((outdir (make-temp-file org-export-odt-tmpdir-prefix t))
  1231. (content-file (expand-file-name "content.xml" outdir)))
  1232. ;; init conten.xml
  1233. (with-current-buffer (find-file-noselect content-file t))
  1234. ;; reset variables
  1235. (setq org-odt-manifest-file-entries nil
  1236. org-odt-embedded-images-count 0)
  1237. content-file))
  1238. (defcustom org-export-odt-prettify-xml nil
  1239. "Specify whether or not the xml output should be prettified.
  1240. When this option is turned on, `indent-region' is run on all
  1241. component xml buffers before they are saved. Turn this off for
  1242. regular use. Turn this on if you need to examine the xml
  1243. visually."
  1244. :group 'org-export-odt
  1245. :type 'boolean)
  1246. (defvar hfy-user-sheet-assoc) ; bound during org-do-lparse
  1247. (defun org-odt-save-as-outfile (target opt-plist)
  1248. ;; create mimetype file
  1249. (write-region "application/vnd.oasis.opendocument.text" nil
  1250. (expand-file-name "mimetype"))
  1251. ;; write meta file
  1252. (org-odt-update-meta-file opt-plist)
  1253. ;; write styles file
  1254. (org-odt-copy-styles-file)
  1255. ;; Update styles.xml - take care of outline numbering
  1256. (with-current-buffer
  1257. (find-file-noselect (expand-file-name "styles.xml") t)
  1258. ;; Don't make automatic backup of styles.xml file. This setting
  1259. ;; prevents the backedup styles.xml file from being zipped in to
  1260. ;; odt file. This is more of a hackish fix. Better alternative
  1261. ;; would be to fix the zip command so that the output odt file
  1262. ;; includes only the needed files and excludes any auto-generated
  1263. ;; extra files like backups and auto-saves etc etc. Note that
  1264. ;; currently the zip command zips up the entire temp directory so
  1265. ;; that any auto-generated files created under the hood ends up in
  1266. ;; the resulting odt file.
  1267. (set (make-local-variable 'backup-inhibited) t)
  1268. ;; Import local setting of `org-export-with-section-numbers'
  1269. (org-lparse-bind-local-variables opt-plist)
  1270. (org-odt-configure-outline-numbering
  1271. (if org-export-with-section-numbers org-export-headline-levels 0)))
  1272. ;; Write custom stlyes for source blocks
  1273. (org-odt-insert-custom-styles-for-srcblocks
  1274. (mapconcat
  1275. (lambda (style)
  1276. (format " %s\n" (cddr style)))
  1277. hfy-user-sheet-assoc ""))
  1278. ;; create a manifest entry for content.xml
  1279. (org-odt-create-manifest-file-entry
  1280. "application/vnd.oasis.opendocument.text" "/" "1.2")
  1281. (org-odt-create-manifest-file-entry "text/xml" "content.xml")
  1282. ;; write out the manifest entries before zipping
  1283. (org-odt-write-manifest-file)
  1284. (let ((xml-files '("mimetype" "META-INF/manifest.xml" "content.xml"
  1285. "meta.xml" "styles.xml"))
  1286. (zipdir default-directory))
  1287. (message "Switching to directory %s" (expand-file-name zipdir))
  1288. ;; save all xml files
  1289. (mapc (lambda (file)
  1290. (with-current-buffer
  1291. (find-file-noselect (expand-file-name file) t)
  1292. ;; prettify output if needed
  1293. (when org-export-odt-prettify-xml
  1294. (indent-region (point-min) (point-max)))
  1295. (save-buffer 0)))
  1296. xml-files)
  1297. (let* ((target-name (file-name-nondirectory target))
  1298. (target-dir (file-name-directory target))
  1299. (cmds `(("zip" "-mX0" ,target-name "mimetype")
  1300. ("zip" "-rmTq" ,target-name "."))))
  1301. (when (file-exists-p target)
  1302. ;; FIXME: If the file is locked this throws a cryptic error
  1303. (delete-file target))
  1304. (let ((coding-system-for-write 'no-conversion) exitcode)
  1305. (message "Creating odt file...")
  1306. (mapc
  1307. (lambda (cmd)
  1308. (message "Running %s" (mapconcat 'identity cmd " "))
  1309. (setq exitcode
  1310. (apply 'call-process (car cmd) nil nil nil (cdr cmd)))
  1311. (or (zerop exitcode)
  1312. (error "Unable to create odt file (%S)" exitcode)))
  1313. cmds))
  1314. ;; move the file from outdir to target-dir
  1315. (rename-file target-name target-dir)
  1316. ;; kill all xml buffers
  1317. (mapc (lambda (file)
  1318. (kill-buffer
  1319. (find-file-noselect (expand-file-name file zipdir) t)))
  1320. xml-files)
  1321. (delete-directory zipdir)))
  1322. (message "Created %s" target)
  1323. (set-buffer (find-file-noselect target t)))
  1324. (defun org-odt-format-date (date)
  1325. (let ((warning-msg
  1326. "OpenDocument files require that dates be in ISO-8601 format. Please review your DATE options for compatibility."))
  1327. ;; If the user is not careful with the date specification, an
  1328. ;; invalid meta.xml will be emitted.
  1329. ;; For now honor user's diktat and let him off with a warning
  1330. ;; message. This is OK as LibreOffice (and possibly other
  1331. ;; apps) doesn't deem this deviation as critical and continue
  1332. ;; to load the file.
  1333. ;; FIXME: Surely there a better way to handle this. Revisit this
  1334. ;; later.
  1335. (cond
  1336. ((and date (string-match "%" date))
  1337. ;; Honor user's diktat. See comments above
  1338. (org-lparse-warn warning-msg)
  1339. (format-time-string date))
  1340. (date
  1341. ;; Honor user's diktat. See comments above
  1342. (org-lparse-warn warning-msg)
  1343. date)
  1344. (t
  1345. ;; ISO 8601 format
  1346. (let ((stamp (format-time-string "%Y-%m-%dT%H:%M:%S%z")))
  1347. (format "%s:%s" (substring stamp 0 -2) (substring stamp -2)))))))
  1348. (defconst org-odt-manifest-file-entry-tag
  1349. "
  1350. <manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
  1351. (defvar org-odt-manifest-file-entries nil)
  1352. (defun org-odt-create-manifest-file-entry (&rest args)
  1353. (push args org-odt-manifest-file-entries))
  1354. (defun org-odt-write-manifest-file ()
  1355. (make-directory "META-INF")
  1356. (let ((manifest-file (expand-file-name "META-INF/manifest.xml")))
  1357. (write-region
  1358. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  1359. <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n"
  1360. nil manifest-file)
  1361. (mapc
  1362. (lambda (file-entry)
  1363. (let* ((version (nth 2 file-entry))
  1364. (extra (if version
  1365. (format " manifest:version=\"%s\"" version)
  1366. "")))
  1367. (write-region
  1368. (format org-odt-manifest-file-entry-tag
  1369. (nth 0 file-entry) (nth 1 file-entry) extra)
  1370. nil manifest-file t))) org-odt-manifest-file-entries)
  1371. (write-region "\n</manifest:manifest>" nil manifest-file t)))
  1372. (defun org-odt-update-meta-file (opt-plist)
  1373. (let ((date (org-odt-format-date (plist-get opt-plist :date)))
  1374. (author (or (plist-get opt-plist :author) ""))
  1375. (email (plist-get opt-plist :email))
  1376. (keywords (plist-get opt-plist :keywords))
  1377. (description (plist-get opt-plist :description))
  1378. (title (plist-get opt-plist :title)))
  1379. (write-region
  1380. (concat
  1381. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  1382. <office:document-meta
  1383. xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
  1384. xmlns:xlink=\"http://www.w3.org/1999/xlink\"
  1385. xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
  1386. xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
  1387. xmlns:ooo=\"http://openoffice.org/2004/office\"
  1388. office:version=\"1.2\">
  1389. <office:meta>" "\n"
  1390. (org-odt-format-tags '("<dc:creator>" . "</dc:creator>") author)
  1391. (org-odt-format-tags
  1392. '("\n<meta:initial-creator>" . "</meta:initial-creator>") author)
  1393. (org-odt-format-tags '("\n<dc:date>" . "</dc:date>") date)
  1394. (org-odt-format-tags
  1395. '("\n<meta:creation-date>" . "</meta:creation-date>") date)
  1396. (org-odt-format-tags '("\n<meta:generator>" . "</meta:generator>")
  1397. (when org-export-creator-info
  1398. (format "Org-%s/Emacs-%s"
  1399. org-version emacs-version)))
  1400. (org-odt-format-tags '("\n<meta:keyword>" . "</meta:keyword>") keywords)
  1401. (org-odt-format-tags '("\n<dc:subject>" . "</dc:subject>") description)
  1402. (org-odt-format-tags '("\n<dc:title>" . "</dc:title>") title)
  1403. "\n"
  1404. " </office:meta>" "</office:document-meta>")
  1405. nil (expand-file-name "meta.xml")))
  1406. ;; create a manifest entry for meta.xml
  1407. (org-odt-create-manifest-file-entry "text/xml" "meta.xml"))
  1408. (defun org-odt-finalize-outfile ()
  1409. (org-odt-delete-empty-paragraphs))
  1410. (defun org-odt-delete-empty-paragraphs ()
  1411. (goto-char (point-min))
  1412. (let ((open "<text:p[^>]*>")
  1413. (close "</text:p>"))
  1414. (while (re-search-forward (format "%s[ \r\n\t]*%s" open close) nil t)
  1415. (replace-match ""))))
  1416. (defun org-odt-get (what &optional opt-plist)
  1417. (case what
  1418. (BACKEND 'odt)
  1419. (EXPORT-DIR (org-export-directory :html opt-plist))
  1420. (FILE-NAME-EXTENSION "odt")
  1421. (EXPORT-BUFFER-NAME "*Org ODT Export*")
  1422. (ENTITY-CONTROL org-odt-entity-control-callbacks-alist)
  1423. (ENTITY-FORMAT org-odt-entity-format-callbacks-alist)
  1424. (INIT-METHOD 'org-odt-init-outfile)
  1425. (FINAL-METHOD 'org-odt-finalize-outfile)
  1426. (SAVE-METHOD 'org-odt-save-as-outfile)
  1427. ;; (OTHER-BACKENDS) ; see note in `org-xhtml-get'
  1428. ;; (CONVERT-METHOD) ; see note in `org-xhtml-get'
  1429. (TOPLEVEL-HLEVEL 1)
  1430. (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps)
  1431. (INLINE-IMAGES 'maybe)
  1432. (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
  1433. (PLAIN-TEXT-MAP '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
  1434. (TABLE-FIRST-COLUMN-AS-LABELS nil)
  1435. (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY "," 'superscript))
  1436. (CODING-SYSTEM-FOR-WRITE 'utf-8)
  1437. (CODING-SYSTEM-FOR-SAVE 'utf-8)
  1438. (t (error "Unknown property: %s" what))))
  1439. (defun org-odt-parse-label (label)
  1440. (save-match-data
  1441. (if (not (string-match "\\`[a-zA-Z]+:\\(.+\\)" label))
  1442. (cons label nil)
  1443. (cons
  1444. (capitalize (substring label 0 (1- (match-beginning 1))))
  1445. (substring label (match-beginning 1))))))
  1446. (defvar org-lparse-latex-fragment-fallback) ; set by org-do-lparse
  1447. (defvar org-lparse-opt-plist) ; bound during org-do-lparse
  1448. (defun org-export-odt-do-preprocess-latex-fragments ()
  1449. "Convert LaTeX fragments to images."
  1450. (let* ((latex-frag-opt (plist-get org-lparse-opt-plist :LaTeX-fragments))
  1451. (latex-frag-opt-1 ; massage the options
  1452. (or (and (member latex-frag-opt '(mathjax t))
  1453. (prog1 org-lparse-latex-fragment-fallback
  1454. (org-lparse-warn
  1455. (concat
  1456. "Use of MathJax is incompatible with ODT exporter. "
  1457. (format "Using %S instead."
  1458. org-lparse-latex-fragment-fallback)))))
  1459. latex-frag-opt)))
  1460. (when (and org-current-export-file latex-frag-opt-1)
  1461. ;; Investigate MathToWeb for converting TeX equations to MathML
  1462. ;; http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01755.html
  1463. (org-format-latex
  1464. (concat "ltxpng/" (file-name-sans-extension
  1465. (file-name-nondirectory
  1466. org-current-export-file)))
  1467. org-current-export-dir nil "Creating LaTeX image %s"
  1468. nil nil latex-frag-opt-1))))
  1469. (defun org-export-odt-preprocess-latex-fragments ()
  1470. (when (equal org-export-current-backend 'odt)
  1471. (org-export-odt-do-preprocess-latex-fragments)))
  1472. (defun org-export-odt-preprocess-label-references ()
  1473. (goto-char (point-min))
  1474. (let (label label-components category value pretty-label)
  1475. (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t)
  1476. (org-if-unprotected-at (match-beginning 1)
  1477. (setq label (match-string 1)
  1478. label-components (org-odt-parse-label label)
  1479. category (car label-components)
  1480. value (cdr label-components)
  1481. pretty-label (if value (concat category " " value) label))
  1482. (replace-match
  1483. (let ((org-lparse-encode-pending t))
  1484. (org-odt-format-tags
  1485. '("<text:sequence-ref text:reference-format=\"category-and-value\" text:ref-name=\"%s\">"
  1486. . "</text:sequence-ref>") pretty-label label)) t t)))))
  1487. ;; process latex fragments as part of
  1488. ;; `org-export-preprocess-after-blockquote-hook'. Note that this hook
  1489. ;; is the one that is closest and well before the call to
  1490. ;; `org-export-attach-captions-and-attributes' in
  1491. ;; `org-export-preprocess-stirng'. The above arrangement permits
  1492. ;; captions, labels and attributes to be attached to png images
  1493. ;; generated out of latex equations.
  1494. (add-hook 'org-export-preprocess-after-blockquote-hook
  1495. 'org-export-odt-preprocess-latex-fragments)
  1496. (defun org-export-odt-preprocess (parameters)
  1497. (org-export-odt-preprocess-label-references))
  1498. (declare-function archive-zip-extract "arc-mode.el" (archive name))
  1499. (defun org-odt-zip-extract-one (archive member &optional target)
  1500. (require 'arc-mode)
  1501. (let* ((target (or target default-directory))
  1502. (archive (expand-file-name archive))
  1503. (archive-zip-extract
  1504. (list "unzip" "-qq" "-o" "-d" target))
  1505. exit-code command-output)
  1506. (setq command-output
  1507. (with-temp-buffer
  1508. (setq exit-code (archive-zip-extract archive member))
  1509. (buffer-string)))
  1510. (unless (zerop exit-code)
  1511. (message command-output)
  1512. (error "Extraction failed"))))
  1513. (defun org-odt-zip-extract (archive members &optional target)
  1514. (when (atom members) (setq members (list members)))
  1515. (mapc (lambda (member)
  1516. (org-odt-zip-extract-one archive member target))
  1517. members))
  1518. (defun org-odt-copy-styles-file (&optional styles-file)
  1519. ;; Non-availability of styles.xml is not a critical error. For now
  1520. ;; throw an error purely for aesthetic reasons.
  1521. (setq styles-file (or styles-file
  1522. org-export-odt-styles-file
  1523. (expand-file-name "styles/OrgOdtStyles.xml"
  1524. org-odt-data-dir)
  1525. (error "org-odt: Missing styles file?")))
  1526. (cond
  1527. ((listp styles-file)
  1528. (let ((archive (nth 0 styles-file))
  1529. (members (nth 1 styles-file)))
  1530. (org-odt-zip-extract archive members)
  1531. (mapc
  1532. (lambda (member)
  1533. (when (org-file-image-p member)
  1534. (let* ((image-type (file-name-extension member))
  1535. (media-type (format "image/%s" image-type)))
  1536. (org-odt-create-manifest-file-entry media-type member))))
  1537. members)))
  1538. ((and (stringp styles-file) (file-exists-p styles-file))
  1539. (let ((styles-file-type (file-name-extension styles-file)))
  1540. (cond
  1541. ((string= styles-file-type "xml")
  1542. (copy-file styles-file "styles.xml" t))
  1543. ((member styles-file-type '("odt" "ott"))
  1544. (org-odt-zip-extract styles-file "styles.xml")))))
  1545. (t
  1546. (error (format "Invalid specification of styles.xml file: %S"
  1547. org-export-odt-styles-file))))
  1548. ;; create a manifest entry for styles.xml
  1549. (org-odt-create-manifest-file-entry "text/xml" "styles.xml"))
  1550. (defvar org-export-odt-factory-settings
  1551. "d4328fb9d1b6cb211d4320ff546829f26700dc5e"
  1552. "SHA1 hash of OrgOdtStyles.xml.")
  1553. (defun org-odt-configure-outline-numbering (level)
  1554. "Outline numbering is retained only upto LEVEL.
  1555. To disable outline numbering pass a LEVEL of 0."
  1556. (goto-char (point-min))
  1557. (let ((regex
  1558. "<text:outline-level-style\\(.*\\)text:level=\"\\([^\"]*\\)\"\\(.*\\)>")
  1559. (replacement
  1560. "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
  1561. (while (re-search-forward regex nil t)
  1562. (when (> (string-to-number (match-string 2)) level)
  1563. (replace-match replacement t nil))))
  1564. (save-buffer 0))
  1565. (provide 'org-odt)
  1566. ;;; org-odt.el ends here