org-odt.el 66 KB

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