org-odt.el 59 KB

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