org-odt.el 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  1. ;;; org-odt.el --- OpenDocumentText export for Org-mode
  2. ;; Copyright (C) 2010-2011
  3. ;; Jambunathan <kjambunathan at gmail dot com>
  4. ;; Author: Jambunathan K <kjambunathan at gmail dot com>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; Homepage: http://orgmode.org
  7. ;; Version: 0.8
  8. ;; This file is not (yet) part of GNU Emacs.
  9. ;; However, it is distributed under the same license.
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  21. ;;
  22. ;;; Commentary:
  23. ;;; Code:
  24. (eval-when-compile (require 'cl))
  25. (require 'org-lparse)
  26. (defun org-odt-end-export ()
  27. ;; remove empty paragraphs
  28. (goto-char (point-min))
  29. (while (re-search-forward
  30. "<text:p\\( text:style-name=\"Text_20_body\"\\)?>[ \r\n\t]*</text:p>"
  31. nil t)
  32. (replace-match ""))
  33. (goto-char (point-min))
  34. ;; Convert whitespace place holders
  35. (goto-char (point-min))
  36. (let (beg end n)
  37. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  38. (setq n (get-text-property beg 'org-whitespace)
  39. end (next-single-property-change beg 'org-whitespace))
  40. (goto-char beg)
  41. (delete-region beg end)
  42. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  43. (make-string n ?x)))))
  44. ;; Remove empty lines at the beginning of the file.
  45. (goto-char (point-min))
  46. (when (looking-at "\\s-+\n") (replace-match ""))
  47. ;; Remove display properties
  48. (remove-text-properties (point-min) (point-max) '(display t)))
  49. (defvar org-odt-suppress-xref nil)
  50. (defconst org-export-odt-special-string-regexps
  51. '(("\\\\-" . "&#x00ad;\\1") ; shy
  52. ("---\\([^-]\\)" . "&#x2014;\\1") ; mdash
  53. ("--\\([^-]\\)" . "&#x2013;\\1") ; ndash
  54. ("\\.\\.\\." . "&#x2026;")) ; hellip
  55. "Regular expressions for special string conversion.")
  56. (defconst org-odt-lib-dir (file-name-directory load-file-name))
  57. (defconst org-odt-data-dir
  58. (let ((dir1 (expand-file-name "../odt" org-odt-lib-dir)) ; git
  59. (dir2 (expand-file-name "./contrib/odt" org-odt-lib-dir))) ; elpa
  60. (cond
  61. ((file-directory-p dir1) dir1)
  62. ((file-directory-p dir2) dir2)
  63. (t (error "Cannot find factory styles file. Check package dir layout")))))
  64. (defvar org-odt-file-extensions
  65. '(("odt" . "OpenDocument Text")
  66. ("ott" . "OpenDocument Text Template")
  67. ("odm" . "OpenDocument Master Document")
  68. ("ods" . "OpenDocument Spreadsheet")
  69. ("ots" . "OpenDocument Spreadsheet Template")
  70. ("odg" . "OpenDocument Drawing (Graphics)")
  71. ("otg" . "OpenDocument Drawing Template")
  72. ("odp" . "OpenDocument Presentation")
  73. ("otp" . "OpenDocument Presentation Template")
  74. ("odi" . "OpenDocument Image")
  75. ("odf" . "OpenDocument Formula")
  76. ("odc" . "OpenDocument Chart")
  77. ("doc" . "Microsoft Text")
  78. ("docx" . "Microsoft Text")
  79. ("xls" . "Microsoft Spreadsheet")
  80. ("xlsx" . "Microsoft Spreadsheet")
  81. ("ppt" . "Microsoft Presentation")
  82. ("pptx" . "Microsoft Presentation")))
  83. (defvar org-odt-ms-file-extensions
  84. '(("doc" . "Microsoft Text")
  85. ("docx" . "Microsoft Text")
  86. ("xls" . "Microsoft Spreadsheet")
  87. ("xlsx" . "Microsoft Spreadsheet")
  88. ("ppt" . "Microsoft Presentation")
  89. ("pptx" . "Microsoft Presentation")))
  90. ;; RelaxNG validation of OpenDocument xml files
  91. (eval-after-load 'rng-nxml
  92. '(setq rng-nxml-auto-validate-flag t))
  93. (eval-after-load 'rng-loc
  94. '(add-to-list 'rng-schema-locating-files
  95. (expand-file-name "etc/schema/schemas.xml" org-odt-data-dir)))
  96. (mapc
  97. (lambda (desc)
  98. ;; Let Org open all OpenDocument files using system-registered app
  99. (add-to-list 'org-file-apps
  100. (cons (concat "\\." (car desc) "\\'") 'system))
  101. ;; Let Emacs open all OpenDocument files in archive mode
  102. (add-to-list 'auto-mode-alist
  103. (cons (concat "\\." (car desc) "\\'") 'archive-mode)))
  104. org-odt-file-extensions)
  105. (mapc
  106. (lambda (desc)
  107. ;; Let Org open all Microsoft files using system-registered app
  108. (add-to-list 'org-file-apps
  109. (cons (concat "\\." (car desc) "\\'") 'system)))
  110. org-odt-ms-file-extensions)
  111. ;; register the odt exporter
  112. (add-to-list 'org-export-backends 'odt)
  113. (defcustom org-export-odt-automatic-styles-file nil
  114. "Default style file for use with ODT exporter."
  115. :group 'org-export-odt
  116. :type 'file)
  117. ;; TODO: Make configuration user-friendly.
  118. (defcustom org-export-odt-styles-file nil
  119. "Default style file for use with ODT exporter.
  120. Valid values are path to an styles.xml file or a path to a valid
  121. *.odt or a *.ott file or a list of the form (FILE (MEMBER1
  122. MEMBER2 ...)). In the last case, the specified FILE is unzipped
  123. and MEMBER1, MEMBER2 etc are copied in to the generated odt
  124. file. The last form is particularly useful if the styles.xml has
  125. reference to additional files like header and footer images.
  126. "
  127. :group 'org-export-odt
  128. :type 'file)
  129. (defconst org-export-odt-tmpdir-prefix "odt-")
  130. (defconst org-export-odt-bookmark-prefix "OrgXref.")
  131. (defcustom org-export-odt-use-bookmarks-for-internal-links t
  132. "Export Internal links as bookmarks?."
  133. :group 'org-export-odt
  134. :type 'boolean)
  135. (defcustom org-export-odt-embed-images t
  136. "Should the images be copied in to the odt file or just linked?"
  137. :group 'org-export-odt
  138. :type 'boolean)
  139. (defcustom org-odt-export-inline-images 'maybe
  140. "Non-nil means inline images into exported HTML pages.
  141. This is done using an <img> tag. When nil, an anchor with href is used to
  142. link to the image. If this option is `maybe', then images in links with
  143. an empty description will be inlined, while images with a description will
  144. be linked only."
  145. :group 'org-odt-export
  146. :type '(choice (const :tag "Never" nil)
  147. (const :tag "Always" t)
  148. (const :tag "When there is no description" maybe)))
  149. (defcustom org-odt-export-inline-image-extensions
  150. '("png" "jpeg" "jpg" "gif")
  151. "Extensions of image files that can be inlined into HTML."
  152. :group 'org-odt-export
  153. :type '(repeat (string :tag "Extension")))
  154. (defcustom org-export-odt-pixels-per-inch display-pixels-per-inch
  155. ""
  156. :group 'org-export-odt
  157. :type 'float)
  158. (defvar org-export-odt-default-org-styles-alist
  159. '((paragraph . ((default . "Text_20_body")
  160. (fixedwidth . "OrgSourceBlock")
  161. (verse . "OrgVerse")
  162. (quote . "Quotations")
  163. (blockquote . "Quotations")
  164. (center . "OrgCenter")
  165. (left . "OrgLeft")
  166. (right . "OrgRight")
  167. (title . "Heading_20_1.title")
  168. (footnote . "Footnote")
  169. (src . "OrgSourceBlock")
  170. (illustration . "Illustration")
  171. (table . "Table")
  172. (definition-term . "Text_20_body_20_bold")
  173. (horizontal-line . "Horizontal_20_Line")))
  174. (character . ((bold . "Bold")
  175. (emphasis . "Emphasis")
  176. (code . "OrgCode")
  177. (verbatim . "OrgCode")
  178. (strike . "Strikethrough")
  179. (underline . "Underline")
  180. (subscript . "OrgSubscript")
  181. (superscript . "OrgSuperscript")))
  182. (list . ((ordered . "OrgNumberedList")
  183. (unordered . "OrgBulletedList")
  184. (description . "OrgDescriptionList"))))
  185. "Default styles for various entities.")
  186. (defvar org-export-odt-org-styles-alist org-export-odt-default-org-styles-alist)
  187. (defun org-odt-get-style-name-for-entity (category &optional entity)
  188. (let ((entity (or entity 'default)))
  189. (or
  190. (cdr (assoc entity (cdr (assoc category
  191. org-export-odt-org-styles-alist))))
  192. (cdr (assoc entity (cdr (assoc category
  193. org-export-odt-default-org-styles-alist))))
  194. (error "Cannot determine style name for entity %s of type %s"
  195. entity category))))
  196. ;;;###autoload
  197. (defun org-export-as-odt-and-open (arg)
  198. "Export the outline as ODT and immediately open it with a browser.
  199. If there is an active region, export only the region.
  200. The prefix ARG specifies how many levels of the outline should become
  201. headlines. The default is 3. Lower levels will become bulleted lists."
  202. (interactive "P")
  203. (org-lparse-and-open "odt" "odt" arg))
  204. ;;;###autoload
  205. (defun org-export-as-odt-batch ()
  206. "Call the function `org-lparse-batch'.
  207. This function can be used in batch processing as:
  208. emacs --batch
  209. --load=$HOME/lib/emacs/org.el
  210. --eval \"(setq org-export-headline-levels 2)\"
  211. --visit=MyFile --funcall org-export-as-odt-batch"
  212. (org-lparse-batch "odt"))
  213. ;;;###autoload
  214. (defun org-export-as-odt-to-buffer (arg)
  215. "Call `org-lparse-odt` with output to a temporary buffer.
  216. No file is created. The prefix ARG is passed through to `org-lparse-to-buffer'."
  217. (interactive "P")
  218. (org-lparse-to-buffer "odt" arg))
  219. ;;;###autoload
  220. (defun org-replace-region-by-odt (beg end)
  221. "Assume the current region has org-mode syntax, and convert it to ODT.
  222. This can be used in any buffer. For example, you could write an
  223. itemized list in org-mode syntax in an ODT buffer and then use this
  224. command to convert it."
  225. (interactive "r")
  226. (org-replace-region-by "odt" beg end))
  227. ;;;###autoload
  228. (defun org-export-region-as-odt (beg end &optional body-only buffer)
  229. "Convert region from BEG to END in org-mode buffer to ODT.
  230. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  231. contents, and only produce the region of converted text, useful for
  232. cut-and-paste operations.
  233. If BUFFER is a buffer or a string, use/create that buffer as a target
  234. of the converted ODT. If BUFFER is the symbol `string', return the
  235. produced ODT as a string and leave not buffer behind. For example,
  236. a Lisp program could call this function in the following way:
  237. (setq odt (org-export-region-as-odt beg end t 'string))
  238. When called interactively, the output buffer is selected, and shown
  239. in a window. A non-interactive call will only return the buffer."
  240. (interactive "r\nP")
  241. (org-lparse-region "odt" beg end body-only buffer))
  242. ;;; org-export-as-odt
  243. ;;;###autoload
  244. (defun org-export-as-odt (arg &optional hidden ext-plist
  245. to-buffer body-only pub-dir)
  246. "Export the outline as a OpenDocumentText file.
  247. If there is an active region, export only the region. The prefix
  248. ARG specifies how many levels of the outline should become
  249. headlines. The default is 3. Lower levels will become bulleted
  250. lists. HIDDEN is obsolete and does nothing.
  251. EXT-PLIST is a property list with external parameters overriding
  252. org-mode's default settings, but still inferior to file-local
  253. settings. When TO-BUFFER is non-nil, create a buffer with that
  254. name and export to that buffer. If TO-BUFFER is the symbol
  255. `string', don't leave any buffer behind but just return the
  256. resulting XML as a string. When BODY-ONLY is set, don't produce
  257. the file header and footer, simply return the content of
  258. <body>...</body>, without even the body tags themselves. When
  259. PUB-DIR is set, use this as the publishing directory."
  260. (interactive "P")
  261. (org-lparse "odt" "odt" arg hidden ext-plist to-buffer body-only pub-dir))
  262. (defvar org-odt-entity-control-callbacks-alist
  263. `((EXPORT
  264. . (org-odt-begin-export org-odt-end-export))
  265. (DOCUMENT-CONTENT
  266. . (org-odt-begin-document-content org-odt-end-document-content))
  267. (DOCUMENT-BODY
  268. . (org-odt-begin-document-body org-odt-end-document-body))
  269. (TOC
  270. . (org-odt-begin-toc org-odt-end-toc))
  271. (ENVIRONMENT
  272. . (org-odt-begin-environment org-odt-end-environment))
  273. (FOOTNOTE-DEFINITION
  274. . (org-odt-begin-footnote-definition org-odt-end-footnote-definition))
  275. (TABLE
  276. . (org-odt-begin-table org-odt-end-table))
  277. (TABLE-ROWGROUP
  278. . (org-odt-begin-table-rowgroup org-odt-end-table-rowgroup))
  279. (LIST
  280. . (org-odt-begin-list org-odt-end-list))
  281. (LIST-ITEM
  282. . (org-odt-begin-list-item org-odt-end-list-item))
  283. (OUTLINE
  284. . (org-odt-begin-outline org-odt-end-outline))
  285. (OUTLINE-TEXT
  286. . (org-odt-begin-outline-text org-odt-end-outline-text))
  287. (PARAGRAPH
  288. . (org-odt-begin-paragraph org-odt-end-paragraph)))
  289. "")
  290. (defvar org-odt-entity-format-callbacks-alist
  291. `((EXTRA-TARGETS . org-lparse-format-extra-targets)
  292. (ORG-TAGS . org-lparse-format-org-tags)
  293. (SECTION-NUMBER . org-lparse-format-section-number)
  294. (HEADLINE . org-odt-format-headline)
  295. (TOC-ENTRY . org-odt-format-toc-entry)
  296. (TOC-ITEM . org-odt-format-toc-item)
  297. (TAGS . org-odt-format-tags)
  298. (SPACES . org-odt-format-spaces)
  299. (TABS . org-odt-format-tabs)
  300. (LINE-BREAK . org-odt-format-line-break)
  301. (FONTIFY . org-odt-format-fontify)
  302. (TODO . org-lparse-format-todo)
  303. (LINK . org-odt-format-link)
  304. (INLINE-IMAGE . org-odt-format-inline-image)
  305. (ORG-LINK . org-odt-format-org-link)
  306. (HEADING . org-odt-format-heading)
  307. (ANCHOR . org-odt-format-anchor)
  308. (TABLE . org-lparse-format-table)
  309. (TABLE-ROW . org-odt-format-table-row)
  310. (TABLE-CELL . org-odt-format-table-cell)
  311. (FOOTNOTES-SECTION . ignore)
  312. (FOOTNOTE-REFERENCE . org-odt-format-footnote-reference)
  313. (HORIZONTAL-LINE . org-odt-format-horizontal-line)
  314. (COMMENT . org-odt-format-comment)
  315. (LINE . org-odt-format-line)
  316. (ORG-ENTITY . org-odt-format-org-entity))
  317. "")
  318. ;;;_. callbacks
  319. ;;;_. control callbacks
  320. ;;;_ , document body
  321. (defun org-odt-begin-office-body ()
  322. (insert "
  323. <office:body>
  324. <office:text>
  325. <text:sequence-decls>
  326. <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Illustration\"/>
  327. <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Table\"/>
  328. <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Text\"/>
  329. <text:sequence-decl text:display-outline-level=\"0\" text:name=\"Drawing\"/>
  330. </text:sequence-decls>"))
  331. ;; Following variable is let bound when `org-do-lparse' is in
  332. ;; progress. See org-html.el.
  333. (defvar org-lparse-toc)
  334. (defun org-odt-begin-document-body (opt-plist)
  335. (org-odt-begin-office-body)
  336. (let ((title (plist-get opt-plist :title)))
  337. (when title
  338. (insert
  339. (org-odt-format-stylized-paragraph 'title title))))
  340. ;; insert toc
  341. (when org-lparse-toc
  342. (insert "\n" org-lparse-toc "\n")))
  343. (defvar org-lparse-body-only) ; let bound during org-do-lparse
  344. (defvar org-lparse-to-buffer) ; let bound during org-do-lparse
  345. (defun org-odt-end-document-body (opt-plist)
  346. (unless org-lparse-body-only
  347. (org-lparse-insert-tag "</office:text>")
  348. (org-lparse-insert-tag "</office:body>")))
  349. (defconst org-odt-document-content-header
  350. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  351. <office:document-content
  352. xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
  353. xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\"
  354. xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\"
  355. xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\"
  356. xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\"
  357. xmlns:fo=\"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0\"
  358. xmlns:xlink=\"http://www.w3.org/1999/xlink\"
  359. xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
  360. xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
  361. xmlns:number=\"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0\"
  362. xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\"
  363. xmlns:chart=\"urn:oasis:names:tc:opendocument:xmlns:chart:1.0\"
  364. xmlns:dr3d=\"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0\"
  365. xmlns:math=\"http://www.w3.org/1998/Math/MathML\"
  366. xmlns:form=\"urn:oasis:names:tc:opendocument:xmlns:form:1.0\"
  367. xmlns:script=\"urn:oasis:names:tc:opendocument:xmlns:script:1.0\"
  368. xmlns:ooo=\"http://openoffice.org/2004/office\"
  369. xmlns:ooow=\"http://openoffice.org/2004/writer\"
  370. xmlns:oooc=\"http://openoffice.org/2004/calc\"
  371. xmlns:dom=\"http://www.w3.org/2001/xml-events\"
  372. xmlns:xforms=\"http://www.w3.org/2002/xforms\"
  373. xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
  374. xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
  375. xmlns:rpt=\"http://openoffice.org/2005/report\"
  376. xmlns:of=\"urn:oasis:names:tc:opendocument:xmlns:of:1.2\"
  377. xmlns:xodt=\"http://www.w3.org/1999/xodt\"
  378. xmlns:field=\"urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0\" office:version=\"1.2\">
  379. ")
  380. (defun org-odt-begin-document-content (opt-plist)
  381. ;; document header
  382. (insert org-odt-document-content-header)
  383. ;; automatic styles
  384. (insert-file-contents
  385. (or org-export-odt-automatic-styles-file
  386. (expand-file-name "styles/OrgOdtAutomaticStyles.xml"
  387. org-odt-data-dir)))
  388. (goto-char (point-max)))
  389. (defun org-odt-end-document-content ()
  390. (org-lparse-insert-tag "</office:document-content>"))
  391. (defun org-odt-begin-outline (level1 snumber title tags
  392. target extra-targets class)
  393. (org-lparse-insert
  394. 'HEADING (org-lparse-format
  395. 'HEADLINE title extra-targets tags snumber level1)
  396. level1 target))
  397. (defun org-odt-end-outline ()
  398. (ignore))
  399. (defun org-odt-begin-outline-text (level1 snumber class)
  400. (ignore))
  401. (defun org-odt-end-outline-text ()
  402. (ignore))
  403. (defun org-odt-begin-paragraph (&optional style)
  404. (org-lparse-insert-tag
  405. "<text:p%s>" (org-odt-get-extra-attrs-for-paragraph-style style)))
  406. (defun org-odt-end-paragraph ()
  407. (org-lparse-insert-tag "</text:p>"))
  408. (defun org-odt-get-extra-attrs-for-paragraph-style (style)
  409. (let (style-name)
  410. (setq style-name
  411. (cond
  412. ((stringp style) style)
  413. ((symbolp style) (org-odt-get-style-name-for-entity
  414. 'paragraph style))))
  415. (unless style-name
  416. (error "Don't know how to handle paragraph style %s" style))
  417. (format " text:style-name=\"%s\"" style-name)))
  418. (defun org-odt-format-stylized-paragraph (style text)
  419. (org-odt-format-tags
  420. '("<text:p%s>" . "</text:p>") text
  421. (org-odt-get-extra-attrs-for-paragraph-style style)))
  422. (defun org-odt-begin-environment (style)
  423. (case style
  424. ((blockquote verse center quote)
  425. (org-lparse-begin-paragraph style)
  426. (list))
  427. ((fixedwidth native)
  428. (org-lparse-end-paragraph)
  429. (list))
  430. (t (error "Unknown environment %s" style))))
  431. (defun org-odt-end-environment (style)
  432. (case style
  433. ((blockquote verse center quote)
  434. (org-lparse-end-paragraph)
  435. (list))
  436. ((fixedwidth native)
  437. (org-lparse-begin-paragraph)
  438. (list))
  439. (t (error "Unknown environment %s" style))))
  440. (defun org-odt-begin-list (ltype &optional arg1)
  441. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  442. ltype))
  443. (let* ((style-name (org-odt-get-style-name-for-entity 'list ltype))
  444. (extra (if style-name
  445. (format " text:style-name=\"%s\"" style-name) "")))
  446. ;; FIXME: Handle arg1 incase of ordered lists.
  447. (case ltype
  448. ((ordered unordered description)
  449. (org-lparse-end-paragraph)
  450. (org-lparse-insert-tag "<text:list%s>" extra))
  451. (t (error "Unknown list type: %s" ltype)))))
  452. (defun org-odt-end-list (ltype)
  453. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  454. ltype))
  455. (if ltype
  456. (org-lparse-insert-tag "</text:list>")
  457. (error "Unknown list type: %s" ltype)))
  458. (defun org-odt-begin-list-item (ltype &optional arg headline)
  459. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  460. ltype))
  461. (case ltype
  462. (ordered
  463. (assert (not headline) t)
  464. (let* ((counter arg) (extra ""))
  465. (org-lparse-insert-tag "<text:list-item>")
  466. (org-lparse-begin-paragraph)))
  467. (unordered
  468. (let* ((id arg) (extra ""))
  469. (org-lparse-insert-tag "<text:list-item>")
  470. (org-lparse-begin-paragraph)
  471. (insert (if headline (org-odt-format-target headline id)
  472. (org-odt-format-bookmark "" id)))))
  473. (description
  474. (assert (not headline) t)
  475. (let ((term (or arg "(no term)")))
  476. (insert
  477. (org-odt-format-tags
  478. '("<text:list-item>" . "</text:list-item>")
  479. (org-odt-format-stylized-paragraph 'definition-term term)))
  480. (org-lparse-begin 'LIST-ITEM 'unordered)
  481. (org-lparse-begin 'LIST 'description)
  482. (org-lparse-begin 'LIST-ITEM 'unordered)))
  483. (t (error "Unknown list type"))))
  484. (defun org-odt-end-list-item (ltype)
  485. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  486. ltype))
  487. (case ltype
  488. ((ordered unordered)
  489. (org-lparse-insert-tag "</text:list-item>"))
  490. (description
  491. (org-lparse-end-list-item)
  492. (org-lparse-end 'LIST 'description)
  493. (org-lparse-end-list-item))
  494. (t (error "Unknown list type"))))
  495. ;; Following variables are let bound when table emission is in
  496. ;; progress. See org-lparse.el.
  497. (defvar org-lparse-table-begin-marker)
  498. (defvar org-lparse-table-ncols)
  499. (defvar org-lparse-table-rowgrp-open)
  500. (defvar org-lparse-table-rownum)
  501. (defvar org-lparse-table-cur-rowgrp-is-hdr)
  502. (defvar org-lparse-table-is-styled)
  503. (defvar org-lparse-table-rowgrp-info)
  504. (defvar org-lparse-table-colalign-vector)
  505. (defun org-odt-begin-table (caption label attributes)
  506. (when label
  507. (insert
  508. (org-odt-format-stylized-paragraph
  509. 'table (org-odt-format-entity-caption label caption "Table"))))
  510. (org-lparse-insert-tag
  511. "<table:table table:name=\"%s\" table:style-name=\"%s\">"
  512. (or label "") "OrgTable")
  513. (setq org-lparse-table-begin-marker (point)))
  514. (defun org-odt-end-table ()
  515. (goto-char org-lparse-table-begin-marker)
  516. (loop for level from 0 below org-lparse-table-ncols
  517. do (insert (org-odt-format-tags "<table:table-column/>" "")))
  518. ;; fill style attributes for table cells
  519. (when org-lparse-table-is-styled
  520. (while (re-search-forward "@@\\(table-cell:p\\|table-cell:style-name\\)@@\\([0-9]+\\)@@\\([0-9]+\\)@@" nil t)
  521. (let ((spec (match-string 1))
  522. (r (string-to-number (match-string 2)))
  523. (c (string-to-number (match-string 3))))
  524. (cond
  525. ((equal spec "table-cell:p")
  526. (let ((style-name (org-odt-get-paragraph-style-for-table-cell r c)))
  527. (replace-match style-name t t)))
  528. ((equal spec "table-cell:style-name")
  529. (let ((style-name (org-odt-get-style-name-for-table-cell r c)))
  530. (replace-match style-name t t)))))))
  531. (goto-char (point-max))
  532. (org-lparse-insert-tag "</table:table>"))
  533. (defun org-odt-begin-table-rowgroup (&optional is-header-row)
  534. (when org-lparse-table-rowgrp-open
  535. (org-lparse-end 'TABLE-ROWGROUP))
  536. (org-lparse-insert-tag (if is-header-row
  537. "<table:table-header-rows>"
  538. "<table:table-rows>"))
  539. (setq org-lparse-table-rowgrp-open t)
  540. (setq org-lparse-table-cur-rowgrp-is-hdr is-header-row))
  541. (defun org-odt-end-table-rowgroup ()
  542. (when org-lparse-table-rowgrp-open
  543. (setq org-lparse-table-rowgrp-open nil)
  544. (org-lparse-insert-tag
  545. (if org-lparse-table-cur-rowgrp-is-hdr
  546. "</table:table-header-rows>" "</table:table-rows>"))))
  547. (defun org-odt-format-table-row (row)
  548. (org-odt-format-tags
  549. '("<table:table-row>" . "</table:table-row>") row))
  550. (defun org-odt-get-style-name-for-table-cell (r c)
  551. (concat
  552. "OrgTblCell"
  553. (cond
  554. ((= r 0) "T")
  555. ((eq (cdr (assoc r org-lparse-table-rowgrp-info)) :start) "T")
  556. (t ""))
  557. (when (= r org-lparse-table-rownum) "B")
  558. (cond
  559. ((= c 0) "")
  560. ((or (memq (nth c org-table-colgroup-info) '(:start :startend))
  561. (memq (nth (1- c) org-table-colgroup-info) '(:end :startend))) "L")
  562. (t ""))))
  563. (defun org-odt-get-paragraph-style-for-table-cell (r c)
  564. (capitalize (aref org-lparse-table-colalign-vector c)))
  565. (defun org-odt-format-table-cell (data r c)
  566. (if (not org-lparse-table-is-styled)
  567. (org-odt-format-tags
  568. '("<table:table-cell>" . "</table:table-cell>")
  569. (org-odt-format-stylized-paragraph
  570. (cond
  571. (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
  572. ((and (= c 0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS))
  573. "OrgTableHeading")
  574. (t "OrgTableContents"))
  575. data))
  576. (let* ((style-name-cookie
  577. (format "@@table-cell:style-name@@%03d@@%03d@@" r c))
  578. (paragraph-style-cookie
  579. (concat
  580. (cond
  581. (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
  582. ((and (= c 0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS))
  583. "OrgTableHeading")
  584. (t "OrgTableContents"))
  585. (format "@@table-cell:p@@%03d@@%03d@@" r c))))
  586. (org-odt-format-tags
  587. '("<table:table-cell table:style-name=\"%s\">" .
  588. "</table:table-cell>")
  589. (org-odt-format-stylized-paragraph paragraph-style-cookie data)
  590. style-name-cookie))))
  591. (defun org-odt-begin-footnote-definition (n)
  592. (org-lparse-begin-paragraph 'footnote))
  593. (defun org-odt-end-footnote-definition (n)
  594. (org-lparse-end-paragraph))
  595. (defun org-odt-begin-toc (lang-specific-heading)
  596. (insert
  597. (format "
  598. <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
  599. <text:table-of-content-source text:outline-level=\"10\">
  600. <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
  601. " lang-specific-heading))
  602. (loop for level from 1 upto 10
  603. do (insert (format
  604. "
  605. <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
  606. <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
  607. <text:index-entry-chapter/>
  608. <text:index-entry-text/>
  609. <text:index-entry-link-end/>
  610. </text:table-of-content-entry-template>
  611. " level level)))
  612. (insert "
  613. </text:table-of-content-source>
  614. <text:index-body>
  615. <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
  616. <text:p text:style-name=\"Contents_20_Heading\">Table of Contents</text:p>
  617. </text:index-title>
  618. "))
  619. (defun org-odt-end-toc ()
  620. (insert "
  621. </text:index-body>
  622. </text:table-of-content>
  623. "))
  624. (defun org-odt-format-toc-entry (snumber todo headline tags href)
  625. (setq headline (concat
  626. (and org-export-with-section-numbers
  627. (concat snumber ". "))
  628. headline
  629. (and tags
  630. (concat
  631. (org-lparse-format 'SPACES 3)
  632. (org-lparse-format 'FONTIFY tags "tag")))))
  633. (when todo
  634. (setq headline (org-lparse-format 'FONTIFY headline "todo")))
  635. (let ((org-odt-suppress-xref t))
  636. (org-odt-format-link headline (concat "#" href))))
  637. (defun org-odt-format-toc-item (toc-entry level org-last-level)
  638. (let ((style (format "Contents_20_%d"
  639. (+ level (or (org-lparse-get 'TOPLEVEL-HLEVEL) 1) -1))))
  640. (insert "\n" (org-odt-format-stylized-paragraph style toc-entry) "\n")))
  641. ;; Following variable is let bound during 'ORG-LINK callback. See
  642. ;; org-html.el
  643. (defvar org-lparse-link-description-is-image nil)
  644. (defun org-odt-format-link (desc href &optional attr)
  645. (cond
  646. ((and (= (string-to-char href) ?#) (not org-odt-suppress-xref))
  647. (setq href (concat org-export-odt-bookmark-prefix (substring href 1)))
  648. (org-odt-format-tags
  649. '("<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"%s\">" .
  650. "</text:bookmark-ref>")
  651. desc href))
  652. (org-lparse-link-description-is-image
  653. (org-odt-format-tags
  654. '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</draw:a>")
  655. desc href (or attr "")))
  656. (t
  657. (org-odt-format-tags
  658. '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</text:a>")
  659. desc href (or attr "")))))
  660. (defun org-odt-format-spaces (n)
  661. (org-odt-format-tags "<text:s text:c=\"%d\"/>" "" n))
  662. (defun org-odt-format-tabs (&optional n)
  663. (let ((tab "<text:tab/>")
  664. (n (or n 1)))
  665. (insert tab)))
  666. (defun org-odt-format-line-break ()
  667. (org-odt-format-tags "<text:line-break/>" ""))
  668. (defun org-odt-format-horizontal-line ()
  669. (org-odt-format-stylized-paragraph 'horizontal-line ""))
  670. (defun org-odt-format-line (line)
  671. (case org-lparse-dyn-current-environment
  672. (fixedwidth (concat (org-odt-format-source-code-or-example-line
  673. (org-xml-encode-plain-text line)) "\n"))
  674. (t (concat line "\n"))))
  675. (defun org-odt-format-comment (fmt &rest args)
  676. (let ((comment (apply 'format fmt args)))
  677. (format "\n<!-- %s -->\n" comment)))
  678. (defun org-odt-format-org-entity (wd)
  679. ;; FIXME: Seems to work. But is this correct?
  680. (let ((s (org-entity-get-representation wd 'utf8)))
  681. (and s (format "&#x%x;" (string-to-char s)))))
  682. (defun org-odt-fill-tabs-and-spaces (line)
  683. (replace-regexp-in-string
  684. "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
  685. (cond
  686. ((string= s "\t") (org-odt-format-tabs))
  687. ((> (length s) 1)
  688. (org-odt-format-spaces (length s)))
  689. (t " "))) line))
  690. (defun org-odt-format-source-code-or-example-line (line)
  691. (org-odt-format-stylized-paragraph 'src (org-odt-fill-tabs-and-spaces line)))
  692. (defun org-odt-format-example (lines)
  693. (mapconcat
  694. (lambda (line)
  695. (org-odt-format-source-code-or-example-line line))
  696. (org-split-string lines "[\r\n]") "\n"))
  697. (defun org-odt-format-source-code-or-example (lines lang caption textareap
  698. cols rows num cont
  699. rpllbl fmt)
  700. (org-odt-format-example (org-export-number-lines
  701. (org-xml-encode-plain-text-lines lines)
  702. 0 0 num cont rpllbl fmt)))
  703. (defun org-xml-encode-plain-text-lines (rtn)
  704. (mapconcat 'org-xml-encode-plain-text (org-split-string rtn "[\r\n]") "\n"))
  705. (defun org-odt-remap-stylenames (style-name)
  706. (or
  707. (cdr (assoc style-name '(("timestamp-wrapper" . "OrgTimestampWrapper")
  708. ("timestamp" . "OrgTimestamp")
  709. ("timestamp-kwd" . "OrgTimestampKeyword")
  710. ("tag" . "OrgTag")
  711. ("todo" . "OrgTodo")
  712. ("done" . "OrgDone")
  713. ("target" . "OrgTarget"))))
  714. style-name))
  715. (defun org-odt-format-fontify (text style &optional id)
  716. (let* ((style-name
  717. (cond
  718. ((stringp style)
  719. (org-odt-remap-stylenames style))
  720. ((symbolp style)
  721. (org-odt-get-style-name-for-entity 'character style))
  722. ((listp style)
  723. (assert (< 1 (length style)))
  724. (let ((parent-style (pop style)))
  725. (mapconcat (lambda (s)
  726. ;; (assert (stringp s) t)
  727. (org-odt-remap-stylenames s)) style "")
  728. (org-odt-remap-stylenames parent-style)))
  729. (t (error "Don't how to handle style %s" style)))))
  730. (org-odt-format-tags
  731. '("<text:span text:style-name=\"%s\">" . "</text:span>")
  732. text style-name)))
  733. (defun org-odt-relocate-relative-path (path dir)
  734. (if (file-name-absolute-p path) path
  735. (file-relative-name (expand-file-name path dir)
  736. (expand-file-name "eyecandy" dir))))
  737. (defun org-odt-format-inline-image (thefile)
  738. (let* ((thelink (if (file-name-absolute-p thefile) thefile
  739. (org-xml-format-href
  740. (org-odt-relocate-relative-path
  741. thefile org-current-export-file))))
  742. (href
  743. (org-odt-format-tags
  744. "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
  745. (if org-export-odt-embed-images
  746. (org-odt-copy-image-file thefile) thelink))))
  747. (org-export-odt-format-image thefile href)))
  748. (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
  749. descp)
  750. "Make an HTML link.
  751. OPT-PLIST is an options list.
  752. TYPE is the device-type of the link (THIS://foo.html)
  753. PATH is the path of the link (http://THIS#locationx)
  754. FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
  755. DESC is the link description, if any.
  756. ATTR is a string of other attributes of the a element.
  757. MAY-INLINE-P allows inlining it as an image."
  758. (declare (special org-lparse-par-open))
  759. (save-match-data
  760. (let* ((may-inline-p
  761. (and (member type-1 '("http" "https" "file"))
  762. (org-lparse-should-inline-p path descp)
  763. (not fragment)))
  764. (type (if (equal type-1 "id") "file" type-1))
  765. (filename path)
  766. (thefile path))
  767. (cond
  768. ;; check for inlined images
  769. ((and (member type '("file"))
  770. (not fragment)
  771. (org-file-image-p
  772. filename org-odt-export-inline-image-extensions)
  773. (or (eq t org-odt-export-inline-images)
  774. (and org-odt-export-inline-images (not descp))))
  775. ;; (when (and (string= type "file") (file-name-absolute-p path))
  776. ;; (setq thefile (concat "file://" (expand-file-name path))))
  777. ;; (setq thefile (org-xml-format-href thefile))
  778. ;; (org-export-html-format-image thefile)
  779. (org-odt-format-inline-image thefile))
  780. (t
  781. (when (string= type "file")
  782. (setq thefile
  783. (cond
  784. ((file-name-absolute-p path)
  785. (concat "file://" (expand-file-name path)))
  786. (t (org-odt-relocate-relative-path
  787. thefile org-current-export-file)))))
  788. (when (and (member type '("" "http" "https" "file" "coderef"))
  789. fragment)
  790. (setq thefile (concat thefile "#" fragment)))
  791. (setq thefile (org-xml-format-href thefile))
  792. (when (not (member type '("" "file" "coderef")))
  793. (setq thefile (concat type ":" thefile)))
  794. (let ((org-odt-suppress-xref (string= type "coderef")))
  795. (org-odt-format-link
  796. (org-xml-format-desc desc) thefile attr)))))))
  797. (defun org-odt-format-heading (text level &optional id)
  798. (let* ((text (if id (org-odt-format-target text id) text)))
  799. (org-odt-format-tags
  800. '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
  801. "</text:h>") text level level)))
  802. (defun org-odt-format-headline (title extra-targets tags
  803. &optional snumber level)
  804. (concat
  805. (org-lparse-format 'EXTRA-TARGETS extra-targets)
  806. ;; No need to generate section numbers. They are auto-generated by
  807. ;; the application
  808. ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
  809. title
  810. (and tags (concat (org-lparse-format 'SPACES 3)
  811. (org-lparse-format 'ORG-TAGS tags)))))
  812. (defun org-odt-format-anchor (text name &optional class)
  813. (org-odt-format-target text name))
  814. (defun org-odt-format-bookmark (text id)
  815. (if id
  816. (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id)
  817. text))
  818. (defun org-odt-format-target (text id)
  819. (let ((name (concat org-export-odt-bookmark-prefix id)))
  820. (concat
  821. (and id (org-odt-format-tags
  822. "<text:bookmark-start text:name=\"%s\"/>" "" name))
  823. (org-odt-format-bookmark text id)
  824. (and id (org-odt-format-tags
  825. "<text:bookmark-end text:name=\"%s\"/>" "" name)))))
  826. (defun org-odt-format-footnote (n def)
  827. (let ((id (concat "fn" n))
  828. (note-class "footnote")
  829. (par-style "Footnote"))
  830. (org-odt-format-tags
  831. '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
  832. "</text:note>")
  833. (concat
  834. (org-odt-format-tags
  835. '("<text:note-citation>" . "</text:note-citation>")
  836. n)
  837. (org-odt-format-tags
  838. '("<text:note-body>" . "</text:note-body>")
  839. def))
  840. id note-class)))
  841. (defun org-odt-format-footnote-reference (n def refcnt)
  842. (if (= refcnt 1)
  843. (org-odt-format-footnote n def)
  844. (org-odt-format-footnote-ref n)))
  845. (defun org-odt-format-footnote-ref (n)
  846. (let ((note-class "footnote")
  847. (ref-format "text")
  848. (ref-name (concat "fn" n)))
  849. (org-odt-format-tags
  850. '("<text:span text:style-name=\"%s\">" . "</text:span>")
  851. (org-odt-format-tags
  852. '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" . "</text:note-ref>")
  853. n note-class ref-format ref-name)
  854. "OrgSuperscript")))
  855. (defun org-odt-get-image-name (file-name)
  856. (require 'sha1)
  857. (file-relative-name
  858. (expand-file-name
  859. (concat (sha1 file-name) "." (file-name-extension file-name)) "Pictures")))
  860. (defun org-export-odt-format-image (src href
  861. ;; par-open
  862. )
  863. "Create image tag with source and attributes."
  864. (save-match-data
  865. (let (embed-as caption attr label attr-plist size width height)
  866. (cond
  867. ((string-match "^ltxpng/" src)
  868. ;; FIXME: Anyway the latex src can be embedded as an
  869. ;; annotation
  870. ;; (org-find-text-property-in-string 'org-latex-src src)
  871. (setq caption nil attr nil label nil embed-as 'character))
  872. (t
  873. (setq caption (org-find-text-property-in-string 'org-caption src)
  874. caption (and caption (org-xml-format-desc caption))
  875. attr (org-find-text-property-in-string 'org-attributes src)
  876. label (org-find-text-property-in-string 'org-label src)
  877. embed-as 'paragraph)))
  878. (setq attr-plist (when attr (read attr)))
  879. (setq size (org-odt-image-size-from-file
  880. src (plist-get attr-plist :width)
  881. (plist-get attr-plist :height)
  882. (plist-get attr-plist :scale) nil embed-as))
  883. (org-export-odt-do-format-image embed-as caption attr label
  884. size href))))
  885. (defun org-export-odt-do-format-image (embed-as caption attr label
  886. size href)
  887. "Create image tag with source and attributes."
  888. (save-match-data
  889. (let ((width (car size)) (height (cdr size))
  890. (draw-frame-pair
  891. '("<draw:frame draw:style-name=\"%s\"
  892. text:anchor-type=\"%s\"
  893. draw:z-index=\"%d\" %s>" . "</draw:frame>")))
  894. (cond
  895. ((and (not caption) (not label))
  896. (let (style-name anchor-type)
  897. (cond
  898. ((eq embed-as 'paragraph)
  899. (setq style-name "OrgGraphicsParagraph" anchor-type "paragraph"))
  900. ((eq embed-as 'character)
  901. (setq style-name "OrgGraphicsBaseline" anchor-type "as-char")))
  902. (org-odt-format-tags
  903. draw-frame-pair href style-name anchor-type 0
  904. (org-odt-image-attrs-from-size width height))))
  905. (t
  906. (concat
  907. ;; (when par-open (org-odt-close-par))
  908. (org-odt-format-tags
  909. draw-frame-pair
  910. (org-odt-format-tags
  911. '("<draw:text-box fo:min-height=\"%dcm\">" . "</draw:text-box>")
  912. (org-odt-format-stylized-paragraph
  913. 'illustration
  914. (concat
  915. (let ((extra " style:rel-width=\"100%\" style:rel-height=\"scale\""))
  916. (org-odt-format-tags
  917. draw-frame-pair href "OrgGraphicsParagraphContent" "paragraph" 2
  918. (concat (org-odt-image-attrs-from-size width height) extra)))
  919. (org-odt-format-entity-caption label caption)))
  920. height)
  921. "OrgFrame" "paragraph" 1
  922. (org-odt-image-attrs-from-size width))
  923. ;; (when par-open (org-odt-open-par))
  924. ))))))
  925. ;; xml files generated on-the-fly
  926. (defconst org-export-odt-save-list
  927. '("META-INF/manifest.xml" "content.xml" "meta.xml" "styles.xml"))
  928. ;; xml files that are copied
  929. (defconst org-export-odt-nosave-list '())
  930. ;; xml files that contribute to the final odt file
  931. (defvar org-export-odt-file-list nil)
  932. (defconst org-export-odt-manifest-lines
  933. '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  934. "<manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">"
  935. "<manifest:file-entry manifest:media-type=\"application/vnd.oasis.opendocument.text\" manifest:version=\"1.2\" manifest:full-path=\"/\"/>"
  936. "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"content.xml\"/>"
  937. "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"styles.xml\"/>"
  938. "<manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"meta.xml\"/>"
  939. "<manifest:file-entry manifest:media-type=\"\" manifest:full-path=\"Pictures/\"/>") . ("</manifest:manifest>")))
  940. (defconst org-export-odt-meta-lines
  941. '(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  942. "<office:document-meta"
  943. " xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\""
  944. " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
  945. " xmlns:dc=\"http://purl.org/dc/elements/1.1/\""
  946. " xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\""
  947. " xmlns:ooo=\"http://openoffice.org/2004/office\" "
  948. " office:version=\"1.2\">"
  949. " <office:meta>") . (" </office:meta>" "</office:document-meta>")))
  950. (defun org-odt-copy-image-file (path &optional target-file)
  951. "Returns the internal name of the file"
  952. (let* ((image-type (file-name-extension path))
  953. (media-type (format "image/%s" image-type))
  954. (src-file (expand-file-name
  955. path (file-name-directory org-current-export-file)))
  956. (target-file (or target-file (org-odt-get-image-name src-file)))
  957. ;; FIXME
  958. (body-only nil))
  959. (when (not org-lparse-to-buffer)
  960. (message "Embedding %s as %s ..."
  961. (substring-no-properties path) target-file)
  962. (copy-file src-file target-file 'overwrite)
  963. (org-odt-update-manifest-file media-type target-file)
  964. (push target-file org-export-odt-file-list)) target-file))
  965. (defun org-odt-image-attrs-from-size (&optional width height)
  966. (concat
  967. (when width (format "svg:width=\"%0.2fcm\"" width))
  968. " "
  969. (when height (format "svg:height=\"%0.2fcm\"" height))))
  970. (defvar org-export-odt-image-size-probe-method
  971. '(emacs imagemagick force)
  972. "Ordered list of methods by for determining size of an embedded
  973. image.")
  974. (defvar org-export-odt-default-image-sizes-alist
  975. '(("character" . (5 . 0.4))
  976. ("paragraph" . (5 . 5)))
  977. "Hardcoded image dimensions one for each of the anchor
  978. methods.")
  979. (defun org-odt-do-image-size (probe-method file &optional dpi anchor-type)
  980. (setq dpi (or dpi org-export-odt-pixels-per-inch))
  981. (setq anchor-type (or anchor-type "paragraph"))
  982. (flet ((size-in-cms (size-in-pixels)
  983. (flet ((pixels-to-cms (pixels)
  984. (let* ((cms-per-inch 2.54)
  985. (inches (/ pixels dpi)))
  986. (* cms-per-inch inches))))
  987. (and size-in-pixels
  988. (cons (pixels-to-cms (car size-in-pixels))
  989. (pixels-to-cms (cdr size-in-pixels)))))))
  990. (case probe-method
  991. (emacs
  992. (size-in-cms (ignore-errors (image-size (create-image file) 'pixels))))
  993. (imagemagick
  994. (size-in-cms
  995. (let ((dim (shell-command-to-string
  996. (format "identify -format \"%%w:%%h\" \"%s\"" file))))
  997. (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
  998. (cons (string-to-number (match-string 1 dim))
  999. (string-to-number (match-string 2 dim)))))))
  1000. (t
  1001. (cdr (assoc-string anchor-type
  1002. org-export-odt-default-image-sizes-alist))))))
  1003. (defun org-odt-image-size-from-file (file &optional user-width
  1004. user-height scale dpi embed-as)
  1005. (unless (file-name-absolute-p file)
  1006. (setq file (expand-file-name
  1007. file (file-name-directory org-current-export-file))))
  1008. (let* (size width height)
  1009. (unless (and user-height user-width)
  1010. (loop for probe-method in org-export-odt-image-size-probe-method
  1011. until size
  1012. do (setq size (org-odt-do-image-size
  1013. probe-method file dpi embed-as)))
  1014. (or size (error "Cannot determine Image size. Aborting ..."))
  1015. (setq width (car size) height (cdr size)))
  1016. (cond
  1017. (scale
  1018. (setq width (* width scale) height (* height scale)))
  1019. ((and user-height user-width)
  1020. (setq width user-width height user-height))
  1021. (user-height
  1022. (setq width (* user-height (/ width height)) height user-height))
  1023. (user-width
  1024. (setq height (* user-width (/ height width)) width user-width))
  1025. (t (ignore)))
  1026. (cons width height)))
  1027. (defvar org-odt-default-entity "Illustration")
  1028. (defun org-odt-format-entity-caption (label caption &optional default-entity)
  1029. (if (not label) (or caption "")
  1030. (let* ((label-components (org-odt-parse-label label))
  1031. (entity (car label-components))
  1032. (seqno (cdr label-components))
  1033. (caption (and caption (concat ": " caption))))
  1034. (unless seqno
  1035. (setq seqno label
  1036. entity (or default-entity org-odt-default-entity)))
  1037. (concat
  1038. entity " "
  1039. (org-odt-format-tags
  1040. '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" . "</text:sequence>")
  1041. seqno label entity entity)
  1042. caption))))
  1043. (defun org-odt-format-tags (tag text &rest args)
  1044. (let ((prefix (when org-lparse-encode-pending "@"))
  1045. (suffix (when org-lparse-encode-pending "@")))
  1046. (apply 'org-lparse-format-tags tag text prefix suffix args)))
  1047. (defun org-odt-init-outfile (filename)
  1048. (let* ((outdir (make-temp-file org-export-odt-tmpdir-prefix t))
  1049. (mimetype-file (expand-file-name "mimetype" outdir))
  1050. (content-file (expand-file-name "content.xml" outdir))
  1051. (manifest-file (expand-file-name "META-INF/manifest.xml" outdir))
  1052. (meta-file (expand-file-name "meta.xml" outdir))
  1053. (styles-file (expand-file-name "styles.xml" outdir))
  1054. (pictures-dir (expand-file-name "Pictures" outdir))
  1055. (body-only nil))
  1056. ;; content file
  1057. (with-current-buffer (find-file-noselect content-file t)
  1058. (erase-buffer))
  1059. ;; FIXME: How to factor in body-only here
  1060. (unless body-only
  1061. ;; manifest file
  1062. (make-directory (file-name-directory manifest-file))
  1063. (with-current-buffer (find-file-noselect manifest-file t)
  1064. (erase-buffer)
  1065. (insert (mapconcat 'identity (car org-export-odt-manifest-lines) "\n"))
  1066. (insert "\n")
  1067. (save-excursion
  1068. (insert (mapconcat 'identity (cdr org-export-odt-manifest-lines) "\n"))))
  1069. ;; meta file
  1070. (with-current-buffer (find-file-noselect meta-file t)
  1071. (erase-buffer)
  1072. (insert (mapconcat 'identity (car org-export-odt-meta-lines) "\n"))
  1073. (insert "\n")
  1074. (save-excursion
  1075. (insert (mapconcat 'identity (cdr org-export-odt-meta-lines) "\n"))))
  1076. ;; styles file
  1077. ;; (copy-file org-export-odt-styles-file styles-file t)
  1078. ;; Pictures dir
  1079. (make-directory pictures-dir)
  1080. ;; initialize list of files that contribute to the odt file
  1081. (setq org-export-odt-file-list
  1082. (append org-export-odt-save-list org-export-odt-nosave-list)))
  1083. content-file))
  1084. (defconst org-export-odt-mimetype-lines
  1085. '("application/vnd.oasis.opendocument.text"))
  1086. (defconst org-odt-manifest-file-entry-tag
  1087. "<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"/>")
  1088. (defun org-odt-save-as-outfile (target opt-plist)
  1089. ;; write meta file
  1090. (org-odt-update-meta-file opt-plist)
  1091. ;; write styles file
  1092. (org-odt-copy-styles-file)
  1093. ;; Update styles.xml - take care of outline numbering
  1094. (with-current-buffer
  1095. (find-file-noselect (expand-file-name "styles.xml") t)
  1096. ;; Don't make automatic backup of styles.xml file. This setting
  1097. ;; prevents the backedup styles.xml file from being zipped in to
  1098. ;; odt file. This is more of a hackish fix. Better alternative
  1099. ;; would be to fix the zip command so that the output odt file
  1100. ;; includes only the needed files and excludes any auto-generated
  1101. ;; extra files like backups and auto-saves etc etc. Note that
  1102. ;; currently the zip command zips up the entire temp directory so
  1103. ;; that any auto-generated files created under the hood ends up in
  1104. ;; the resulting odt file.
  1105. (set (make-local-variable 'backup-inhibited) t)
  1106. ;; Import local setting of `org-export-with-section-numbers'
  1107. (org-lparse-bind-local-variables opt-plist)
  1108. (org-odt-configure-outline-numbering
  1109. (if org-export-with-section-numbers org-export-headline-levels 0)))
  1110. (let ((zipdir default-directory))
  1111. (message "Switching to directory %s" (expand-file-name zipdir))
  1112. ;; save all xml files
  1113. (mapc (lambda (file)
  1114. (with-current-buffer
  1115. (find-file-noselect (expand-file-name file) t)
  1116. ;; prettify output
  1117. (indent-region (point-min) (point-max))
  1118. (save-buffer)))
  1119. org-export-odt-save-list)
  1120. (let* ((target-name (file-name-nondirectory target))
  1121. (target-dir (file-name-directory target))
  1122. (cmd (format "zip -rmTq %s %s" target-name ".")))
  1123. (when (file-exists-p target)
  1124. ;; FIXME: If the file is locked this throws a cryptic error
  1125. (delete-file target))
  1126. (let ((coding-system-for-write 'no-conversion) exitcode)
  1127. (message "Creating odt file using \"%s\"" cmd)
  1128. (setq exitcode
  1129. (apply 'call-process
  1130. "zip"
  1131. nil
  1132. nil
  1133. nil
  1134. (append (list "-rmTq") (list target-name "."))))
  1135. (or (zerop exitcode)
  1136. (error "Unable to create odt file (%S)" exitcode)))
  1137. ;; move the file from outdir to target-dir
  1138. (rename-file target-name target-dir)
  1139. ;; kill all xml buffers
  1140. (mapc (lambda (file)
  1141. (kill-buffer
  1142. (find-file-noselect (expand-file-name file zipdir) t)))
  1143. org-export-odt-save-list)
  1144. (delete-directory zipdir)))
  1145. (message "Created %s" target)
  1146. (set-buffer (find-file-noselect target t)))
  1147. (defun org-odt-update-meta-file (opt-plist)
  1148. (with-current-buffer
  1149. (find-file-noselect (expand-file-name "meta.xml") t)
  1150. (let ((date (or (plist-get opt-plist :effective-date) ""))
  1151. (author (or (plist-get opt-plist :author) ""))
  1152. (email (plist-get opt-plist :email))
  1153. (keywords (plist-get opt-plist :keywords))
  1154. (description (plist-get opt-plist :description))
  1155. (title (plist-get opt-plist :title)))
  1156. (insert
  1157. "\n"
  1158. (org-odt-format-tags '("<dc:creator>" . "</dc:creator>") author)
  1159. (org-odt-format-tags
  1160. '("\n<meta:initial-creator>" . "</meta:initial-creator>") author)
  1161. (org-odt-format-tags '("\n<dc:date>" . "</dc:date>") date)
  1162. (org-odt-format-tags
  1163. '("\n<meta:creation-date>" . "</meta:creation-date>") date)
  1164. (org-odt-format-tags '("\n<meta:generator>" . "</meta:generator>")
  1165. (when org-export-creator-info
  1166. (format "Org-%s/Emacs-%s"
  1167. org-version emacs-version)))
  1168. (org-odt-format-tags '("\n<meta:keyword>" . "</meta:keyword>") keywords)
  1169. (org-odt-format-tags '("\n<dc:subject>" . "</dc:subject>") description)
  1170. (org-odt-format-tags '("\n<dc:title>" . "</dc:title>") title)
  1171. "\n"))))
  1172. (defun org-odt-update-manifest-file (media-type full-path)
  1173. (with-current-buffer
  1174. (find-file-noselect (expand-file-name "META-INF/manifest.xml") t)
  1175. (insert (format org-odt-manifest-file-entry-tag media-type full-path))))
  1176. (defun org-odt-finalize-outfile ()
  1177. (message "org-newodt: Finalizing outfile")
  1178. (org-odt-delete-empty-paragraphs))
  1179. (defun org-odt-delete-empty-paragraphs ()
  1180. (goto-char (point-min))
  1181. (let ((open "<text:p[^>]*>")
  1182. (close "</text:p>"))
  1183. (while (re-search-forward (format "%s[ \r\n\t]*%s" open close) nil t)
  1184. (replace-match ""))))
  1185. (defun org-odt-get (what &optional opt-plist)
  1186. (case what
  1187. (BACKEND 'odt)
  1188. (EXPORT-DIR (org-export-directory :html opt-plist))
  1189. (FILE-NAME-EXTENSION "odt")
  1190. (EXPORT-BUFFER-NAME "*Org ODT Export*")
  1191. (ENTITY-CONTROL org-odt-entity-control-callbacks-alist)
  1192. (ENTITY-FORMAT org-odt-entity-format-callbacks-alist)
  1193. (INIT-METHOD 'org-odt-init-outfile)
  1194. (FINAL-METHOD 'org-odt-finalize-outfile)
  1195. (SAVE-METHOD 'org-odt-save-as-outfile)
  1196. (OTHER-BACKENDS
  1197. '("bib" "doc" "doc6" "doc95" "html" "xhtml" "latex" "odt" "ott" "pdf" "rtf"
  1198. "sdw" "sdw3" "sdw4" "stw " "sxw" "mediawiki" "text" "txt" "uot" "vor"
  1199. "vor3" "vor4" "docbook" "ooxml" "ppt" "odp"))
  1200. (CONVERT-METHOD org-export-convert-process)
  1201. (TOPLEVEL-HLEVEL 1)
  1202. (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps)
  1203. (INLINE-IMAGES 'maybe)
  1204. (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
  1205. (PLAIN-TEXT-MAP '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
  1206. (TABLE-FIRST-COLUMN-AS-LABELS nil)
  1207. (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY "," 'superscript))
  1208. (t (error "Unknown property: %s" what))))
  1209. (defun org-odt-parse-label (label)
  1210. (save-match-data
  1211. (if (not (string-match "\\`[a-zA-Z]+:\\(.+\\)" label))
  1212. (cons label nil)
  1213. (cons
  1214. (capitalize (substring label 0 (1- (match-beginning 1))))
  1215. (substring label (match-beginning 1))))))
  1216. (defvar org-lparse-latex-fragment-fallback) ; set by org-do-lparse
  1217. (defun org-export-odt-preprocess (parameters)
  1218. "Convert LaTeX fragments to images."
  1219. (when (and org-current-export-file
  1220. (plist-get parameters :LaTeX-fragments))
  1221. (org-format-latex
  1222. (concat "ltxpng/" (file-name-sans-extension
  1223. (file-name-nondirectory
  1224. org-current-export-file)))
  1225. org-current-export-dir nil "Creating LaTeX image %s"
  1226. nil nil
  1227. (cond
  1228. ((eq (plist-get parameters :LaTeX-fragments) 'verbatim) 'verbatim)
  1229. ;; Investigate MathToWeb for converting TeX equations to MathML
  1230. ;; See http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01755.html
  1231. ((or (eq (plist-get parameters :LaTeX-fragments) 'mathjax )
  1232. (eq (plist-get parameters :LaTeX-fragments) t ))
  1233. (org-lparse-warn
  1234. (concat
  1235. "Use of MathJax is incompatible with ODT exporter. "
  1236. (format "Using %S instead." org-lparse-latex-fragment-fallback)))
  1237. org-lparse-latex-fragment-fallback)
  1238. ((eq (plist-get parameters :LaTeX-fragments) 'dvipng ) 'dvipng)
  1239. (t nil))))
  1240. (goto-char (point-min))
  1241. (let (label label-components category value pretty-label)
  1242. (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t)
  1243. (org-if-unprotected-at (match-beginning 1)
  1244. (setq label (match-string 1)
  1245. label-components (org-odt-parse-label label)
  1246. category (car label-components)
  1247. value (cdr label-components)
  1248. pretty-label (if value (concat category " " value) label))
  1249. (replace-match
  1250. (let ((org-lparse-encode-pending t))
  1251. (org-odt-format-tags
  1252. '("<text:sequence-ref text:reference-format=\"category-and-value\" text:ref-name=\"%s\">"
  1253. . "</text:sequence-ref>") pretty-label label)) t t)))))
  1254. (declare-function archive-zip-extract "arc-mode.el" (archive name))
  1255. (defun org-odt-zip-extract-one (archive member &optional target)
  1256. (require 'arc-mode)
  1257. (let* ((target (or target default-directory))
  1258. (archive (expand-file-name archive))
  1259. (archive-zip-extract
  1260. (list "unzip" "-qq" "-o" "-d" target))
  1261. exit-code command-output)
  1262. (setq command-output
  1263. (with-temp-buffer
  1264. (setq exit-code (archive-zip-extract archive member))
  1265. (buffer-string)))
  1266. (unless (zerop exit-code)
  1267. (message command-output)
  1268. (error "Extraction failed"))))
  1269. (defun org-odt-zip-extract (archive members &optional target)
  1270. (when (atom members) (setq members (list members)))
  1271. (mapc (lambda (member)
  1272. (org-odt-zip-extract-one archive member target))
  1273. members))
  1274. (defun org-odt-copy-styles-file (&optional styles-file)
  1275. ;; Non-availability of styles.xml is not a critical error. For now
  1276. ;; throw an error purely for aesthetic reasons.
  1277. (setq styles-file (or styles-file
  1278. org-export-odt-styles-file
  1279. (expand-file-name "styles/OrgOdtStyles.xml"
  1280. org-odt-data-dir)
  1281. (error "org-odt: Missing styles file?")))
  1282. (cond
  1283. ((listp styles-file)
  1284. (let ((archive (nth 0 styles-file))
  1285. (members (nth 1 styles-file)))
  1286. (org-odt-zip-extract archive members)
  1287. (mapc
  1288. (lambda (member)
  1289. (when (org-file-image-p member)
  1290. (let* ((image-type (file-name-extension member))
  1291. (media-type (format "image/%s" image-type)))
  1292. (org-odt-update-manifest-file media-type member))))
  1293. members)))
  1294. ((and (stringp styles-file) (file-exists-p styles-file))
  1295. (let ((styles-file-type (file-name-extension styles-file)))
  1296. (cond
  1297. ((string= styles-file-type "xml")
  1298. (copy-file styles-file "styles.xml" t))
  1299. ((member styles-file-type '("odt" "ott"))
  1300. (org-odt-zip-extract styles-file "styles.xml")))))
  1301. (t
  1302. (error (format "Invalid specification of styles.xml file: %S"
  1303. org-export-odt-styles-file)))))
  1304. (defvar org-export-odt-factory-settings
  1305. "d4328fb9d1b6cb211d4320ff546829f26700dc5e"
  1306. "SHA1 hash of OrgOdtStyles.xml.")
  1307. (defun org-odt-configure-outline-numbering (level)
  1308. "Outline numbering is retained only upto LEVEL.
  1309. To disable outline numbering pass a LEVEL of 0."
  1310. (if (not (string= org-export-odt-factory-settings (sha1 (current-buffer))))
  1311. (org-lparse-warn
  1312. "org-odt: Using custom styles file? Consider tweaking styles.xml for better output. To suppress this warning update `org-export-odt-factory-settings'")
  1313. (goto-char (point-min))
  1314. (let ((regex
  1315. "<text:outline-level-style\\(.*\\)text:level=\"\\(.*\\)\"\\(.*\\)>")
  1316. (replacement
  1317. "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
  1318. (while (re-search-forward regex nil t)
  1319. (when (> (string-to-number (match-string 1)) level)
  1320. (replace-match replacement t nil))))
  1321. (save-buffer 0)))
  1322. (provide 'org-odt)
  1323. ;;; org-odt.el ends here