org-odt.el 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244
  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. (defgroup org-export-odt nil
  26. "Options specific for ODT export of Org-mode files."
  27. :tag "Org Export ODT"
  28. :group 'org-export)
  29. (defun org-odt-end-export ()
  30. (org-odt-fixup-label-references)
  31. ;; remove empty paragraphs
  32. (goto-char (point-min))
  33. (while (re-search-forward
  34. "<text:p\\( text:style-name=\"Text_20_body\"\\)?>[ \r\n\t]*</text:p>"
  35. nil t)
  36. (replace-match ""))
  37. (goto-char (point-min))
  38. ;; Convert whitespace place holders
  39. (goto-char (point-min))
  40. (let (beg end n)
  41. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  42. (setq n (get-text-property beg 'org-whitespace)
  43. end (next-single-property-change beg 'org-whitespace))
  44. (goto-char beg)
  45. (delete-region beg end)
  46. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  47. (make-string n ?x)))))
  48. ;; Remove empty lines at the beginning of the file.
  49. (goto-char (point-min))
  50. (when (looking-at "\\s-+\n") (replace-match ""))
  51. ;; Remove display properties
  52. (remove-text-properties (point-min) (point-max) '(display t)))
  53. (defvar org-odt-suppress-xref nil)
  54. (defconst org-export-odt-special-string-regexps
  55. '(("\\\\-" . "&#x00ad;\\1") ; shy
  56. ("---\\([^-]\\)" . "&#x2014;\\1") ; mdash
  57. ("--\\([^-]\\)" . "&#x2013;\\1") ; ndash
  58. ("\\.\\.\\." . "&#x2026;")) ; hellip
  59. "Regular expressions for special string conversion.")
  60. (defconst org-odt-lib-dir (file-name-directory load-file-name))
  61. (defconst org-odt-data-dir
  62. (let ((dir1 (expand-file-name "../odt" org-odt-lib-dir)) ; git
  63. (dir2 (expand-file-name "./contrib/odt" org-odt-lib-dir))) ; elpa
  64. (cond
  65. ((file-directory-p dir1) dir1)
  66. ((file-directory-p dir2) dir2)
  67. (t (error "Cannot find factory styles file. Check package dir layout"))))
  68. "Directory that holds auxiliary files used by the ODT exporter.
  69. The 'styles' subdir contains the following xml files -
  70. 'OrgOdtStyles.xml' and 'OrgOdtContentTemplate.xml' - which are
  71. used as factory settings of `org-export-odt-styles-file' and
  72. `org-export-odt-content-template-file'.
  73. The 'etc/schema' subdir contains rnc files for validating of
  74. OpenDocument xml files.")
  75. (defvar org-odt-file-extensions
  76. '(("odt" . "OpenDocument Text")
  77. ("ott" . "OpenDocument Text Template")
  78. ("odm" . "OpenDocument Master Document")
  79. ("ods" . "OpenDocument Spreadsheet")
  80. ("ots" . "OpenDocument Spreadsheet Template")
  81. ("odg" . "OpenDocument Drawing (Graphics)")
  82. ("otg" . "OpenDocument Drawing Template")
  83. ("odp" . "OpenDocument Presentation")
  84. ("otp" . "OpenDocument Presentation Template")
  85. ("odi" . "OpenDocument Image")
  86. ("odf" . "OpenDocument Formula")
  87. ("odc" . "OpenDocument Chart")
  88. ("doc" . "Microsoft Text")
  89. ("docx" . "Microsoft Text")
  90. ("xls" . "Microsoft Spreadsheet")
  91. ("xlsx" . "Microsoft Spreadsheet")
  92. ("ppt" . "Microsoft Presentation")
  93. ("pptx" . "Microsoft Presentation")))
  94. (defvar org-odt-ms-file-extensions
  95. '(("doc" . "Microsoft Text")
  96. ("docx" . "Microsoft Text")
  97. ("xls" . "Microsoft Spreadsheet")
  98. ("xlsx" . "Microsoft Spreadsheet")
  99. ("ppt" . "Microsoft Presentation")
  100. ("pptx" . "Microsoft Presentation")))
  101. ;; RelaxNG validation of OpenDocument xml files
  102. (eval-after-load 'rng-nxml
  103. '(setq rng-nxml-auto-validate-flag t))
  104. (eval-after-load 'rng-loc
  105. '(add-to-list 'rng-schema-locating-files
  106. (expand-file-name "etc/schema/schemas.xml" org-odt-data-dir)))
  107. (mapc
  108. (lambda (desc)
  109. ;; Let Org open all OpenDocument files using system-registered app
  110. (add-to-list 'org-file-apps
  111. (cons (concat "\\." (car desc) "\\'") 'system))
  112. ;; Let Emacs open all OpenDocument files in archive mode
  113. (add-to-list 'auto-mode-alist
  114. (cons (concat "\\." (car desc) "\\'") 'archive-mode)))
  115. org-odt-file-extensions)
  116. (mapc
  117. (lambda (desc)
  118. ;; Let Org open all Microsoft files using system-registered app
  119. (add-to-list 'org-file-apps
  120. (cons (concat "\\." (car desc) "\\'") 'system)))
  121. org-odt-ms-file-extensions)
  122. ;; register the odt exporter with the pre-processor
  123. (add-to-list 'org-export-backends 'odt)
  124. ;; register the odt exporter with org-lparse library
  125. (org-lparse-register-backend 'odt)
  126. (defun org-odt-unload-function ()
  127. (org-lparse-unregister-backend 'odt)
  128. (remove-hook 'org-export-preprocess-after-blockquote-hook
  129. 'org-export-odt-preprocess-latex-fragments)
  130. nil)
  131. (defcustom org-export-odt-content-template-file nil
  132. "Template file for \"content.xml\".
  133. The exporter embeds the exported content just before
  134. \"</office:text>\" element.
  135. If unspecified, the file named \"OrgOdtContentTemplate.xml\"
  136. under `org-odt-data-dir' is used."
  137. :type 'file
  138. :group 'org-export-odt)
  139. (defcustom org-export-odt-styles-file nil
  140. "Default styles file for use with ODT export.
  141. Valid values are one of:
  142. 1. nil
  143. 2. path to a styles.xml file
  144. 3. path to a *.odt or a *.ott file
  145. 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
  146. ...))
  147. In case of option 1, an in-built styles.xml is used. See
  148. `org-odt-data-dir' for more information.
  149. In case of option 3, the specified file is unzipped and the
  150. styles.xml embedded therein is used.
  151. In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
  152. and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
  153. generated odt file. Use relative path for specifying the
  154. FILE-MEMBERS. styles.xml must be specified as one of the
  155. FILE-MEMBERS.
  156. Use options 1, 2 or 3 only if styles.xml alone suffices for
  157. achieving the desired formatting. Use option 4, if the styles.xml
  158. references additional files like header and footer images for
  159. achieving the desired formattting.
  160. Use \"#+ODT_STYLES_FILE: ...\" directive to set this variable on
  161. a per-file basis. For example,
  162. #+ODT_STYLES_FILE: \"/path/to/styles.xml\" or
  163. #+ODT_STYLES_FILE: (\"/path/to/file.ott\" (\"styles.xml\" \"image/hdr.png\"))."
  164. :group 'org-export-odt
  165. :type
  166. '(choice
  167. (const :tag "Factory settings" nil)
  168. (file :must-match t :tag "styles.xml")
  169. (file :must-match t :tag "ODT or OTT file")
  170. (list :tag "ODT or OTT file + Members"
  171. (file :must-match t :tag "ODF Text or Text Template file")
  172. (cons :tag "Members"
  173. (file :tag " Member" "styles.xml")
  174. (repeat (file :tag "Member"))))))
  175. (eval-after-load 'org-exp
  176. '(add-to-list 'org-export-inbuffer-options-extra
  177. '("ODT_STYLES_FILE" :odt-styles-file)))
  178. (defconst org-export-odt-tmpdir-prefix "odt-")
  179. (defconst org-export-odt-bookmark-prefix "OrgXref.")
  180. (defvar org-export-odt-embed-images t
  181. "Should the images be copied in to the odt file or just linked?")
  182. (defvar org-export-odt-inline-images 'maybe) ; counterpart of
  183. ; `org-export-html-inline-images'
  184. (defcustom org-export-odt-inline-image-extensions
  185. '("png" "jpeg" "jpg" "gif")
  186. "Extensions of image files that can be inlined into HTML."
  187. :type '(repeat (string :tag "Extension"))
  188. :group 'org-export-odt)
  189. (defcustom org-export-odt-pixels-per-inch display-pixels-per-inch
  190. ;; FIXME add docstring
  191. ""
  192. :type 'float
  193. :group 'org-export-odt)
  194. (defvar org-export-odt-default-org-styles-alist
  195. '((paragraph . ((default . "Text_20_body")
  196. (fixedwidth . "OrgFixedWidthBlock")
  197. (verse . "OrgVerse")
  198. (quote . "Quotations")
  199. (blockquote . "Quotations")
  200. (center . "OrgCenter")
  201. (left . "OrgLeft")
  202. (right . "OrgRight")
  203. (title . "Heading_20_1.title")
  204. (footnote . "Footnote")
  205. (src . "OrgSrcBlock")
  206. (illustration . "Illustration")
  207. (table . "Table")
  208. (definition-term . "Text_20_body_20_bold")
  209. (horizontal-line . "Horizontal_20_Line")))
  210. (character . ((bold . "Bold")
  211. (emphasis . "Emphasis")
  212. (code . "OrgCode")
  213. (verbatim . "OrgCode")
  214. (strike . "Strikethrough")
  215. (underline . "Underline")
  216. (subscript . "OrgSubscript")
  217. (superscript . "OrgSuperscript")))
  218. (list . ((ordered . "OrgNumberedList")
  219. (unordered . "OrgBulletedList")
  220. (description . "OrgDescriptionList"))))
  221. "Default styles for various entities.")
  222. (defvar org-export-odt-org-styles-alist org-export-odt-default-org-styles-alist)
  223. (defun org-odt-get-style-name-for-entity (category &optional entity)
  224. (let ((entity (or entity 'default)))
  225. (or
  226. (cdr (assoc entity (cdr (assoc category
  227. org-export-odt-org-styles-alist))))
  228. (cdr (assoc entity (cdr (assoc category
  229. org-export-odt-default-org-styles-alist))))
  230. (error "Cannot determine style name for entity %s of type %s"
  231. entity category))))
  232. (defcustom org-export-odt-preferred-output-format nil
  233. "Automatically post-process to this format after exporting to \"odt\".
  234. Interactive commands `org-export-as-odt' and
  235. `org-export-as-odt-and-open' export first to \"odt\" format and
  236. then use `org-export-odt-convert-process' to convert the
  237. resulting document to this format. During customization of this
  238. variable, the list of valid values are populated based on
  239. `org-export-odt-convert-capabilities'."
  240. :group 'org-export-odt
  241. :type '(choice :convert-widget
  242. (lambda (w)
  243. (apply 'widget-convert (widget-type w)
  244. (eval (car (widget-get w :args)))))
  245. `((const :tag "None" nil)
  246. ,@(mapcar (lambda (c)
  247. `(const :tag ,c ,c))
  248. (org-lparse-reachable-formats "odt")))))
  249. ;;;###autoload
  250. (defun org-export-as-odt-and-open (arg)
  251. "Export the outline as ODT and immediately open it with a browser.
  252. If there is an active region, export only the region.
  253. The prefix ARG specifies how many levels of the outline should become
  254. headlines. The default is 3. Lower levels will become bulleted lists."
  255. (interactive "P")
  256. (org-lparse-and-open
  257. (or org-export-odt-preferred-output-format "odt") "odt" arg))
  258. ;;;###autoload
  259. (defun org-export-as-odt-batch ()
  260. "Call the function `org-lparse-batch'.
  261. This function can be used in batch processing as:
  262. emacs --batch
  263. --load=$HOME/lib/emacs/org.el
  264. --eval \"(setq org-export-headline-levels 2)\"
  265. --visit=MyFile --funcall org-export-as-odt-batch"
  266. (org-lparse-batch "odt"))
  267. ;;;###autoload
  268. (defun org-export-as-odt-to-buffer (arg)
  269. "Call `org-lparse-odt` with output to a temporary buffer.
  270. No file is created. The prefix ARG is passed through to `org-lparse-to-buffer'."
  271. (interactive "P")
  272. (org-lparse-to-buffer "odt" arg))
  273. ;;;###autoload
  274. (defun org-replace-region-by-odt (beg end)
  275. "Assume the current region has org-mode syntax, and convert it to ODT.
  276. This can be used in any buffer. For example, you could write an
  277. itemized list in org-mode syntax in an ODT buffer and then use this
  278. command to convert it."
  279. (interactive "r")
  280. (org-replace-region-by "odt" beg end))
  281. ;;;###autoload
  282. (defun org-export-region-as-odt (beg end &optional body-only buffer)
  283. "Convert region from BEG to END in org-mode buffer to ODT.
  284. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  285. contents, and only produce the region of converted text, useful for
  286. cut-and-paste operations.
  287. If BUFFER is a buffer or a string, use/create that buffer as a target
  288. of the converted ODT. If BUFFER is the symbol `string', return the
  289. produced ODT as a string and leave not buffer behind. For example,
  290. a Lisp program could call this function in the following way:
  291. (setq odt (org-export-region-as-odt beg end t 'string))
  292. When called interactively, the output buffer is selected, and shown
  293. in a window. A non-interactive call will only return the buffer."
  294. (interactive "r\nP")
  295. (org-lparse-region "odt" beg end body-only buffer))
  296. ;;; org-export-as-odt
  297. ;;;###autoload
  298. (defun org-export-as-odt (arg &optional hidden ext-plist
  299. to-buffer body-only pub-dir)
  300. "Export the outline as a OpenDocumentText file.
  301. If there is an active region, export only the region. The prefix
  302. ARG specifies how many levels of the outline should become
  303. headlines. The default is 3. Lower levels will become bulleted
  304. lists. HIDDEN is obsolete and does nothing.
  305. EXT-PLIST is a property list with external parameters overriding
  306. org-mode's default settings, but still inferior to file-local
  307. settings. When TO-BUFFER is non-nil, create a buffer with that
  308. name and export to that buffer. If TO-BUFFER is the symbol
  309. `string', don't leave any buffer behind but just return the
  310. resulting XML as a string. When BODY-ONLY is set, don't produce
  311. the file header and footer, simply return the content of
  312. <body>...</body>, without even the body tags themselves. When
  313. PUB-DIR is set, use this as the publishing directory."
  314. (interactive "P")
  315. (org-lparse (or org-export-odt-preferred-output-format "odt")
  316. "odt" arg hidden ext-plist to-buffer body-only pub-dir))
  317. (defvar org-odt-entity-control-callbacks-alist
  318. `((EXPORT
  319. . (org-odt-begin-export org-odt-end-export))
  320. (DOCUMENT-CONTENT
  321. . (org-odt-begin-document-content org-odt-end-document-content))
  322. (DOCUMENT-BODY
  323. . (org-odt-begin-document-body org-odt-end-document-body))
  324. (TOC
  325. . (org-odt-begin-toc org-odt-end-toc))
  326. (ENVIRONMENT
  327. . (org-odt-begin-environment org-odt-end-environment))
  328. (FOOTNOTE-DEFINITION
  329. . (org-odt-begin-footnote-definition org-odt-end-footnote-definition))
  330. (TABLE
  331. . (org-odt-begin-table org-odt-end-table))
  332. (TABLE-ROWGROUP
  333. . (org-odt-begin-table-rowgroup org-odt-end-table-rowgroup))
  334. (LIST
  335. . (org-odt-begin-list org-odt-end-list))
  336. (LIST-ITEM
  337. . (org-odt-begin-list-item org-odt-end-list-item))
  338. (OUTLINE
  339. . (org-odt-begin-outline org-odt-end-outline))
  340. (OUTLINE-TEXT
  341. . (org-odt-begin-outline-text org-odt-end-outline-text))
  342. (PARAGRAPH
  343. . (org-odt-begin-paragraph org-odt-end-paragraph)))
  344. "")
  345. (defvar org-odt-entity-format-callbacks-alist
  346. `((EXTRA-TARGETS . org-lparse-format-extra-targets)
  347. (ORG-TAGS . org-lparse-format-org-tags)
  348. (SECTION-NUMBER . org-lparse-format-section-number)
  349. (HEADLINE . org-odt-format-headline)
  350. (TOC-ENTRY . org-odt-format-toc-entry)
  351. (TOC-ITEM . org-odt-format-toc-item)
  352. (TAGS . org-odt-format-tags)
  353. (SPACES . org-odt-format-spaces)
  354. (TABS . org-odt-format-tabs)
  355. (LINE-BREAK . org-odt-format-line-break)
  356. (FONTIFY . org-odt-format-fontify)
  357. (TODO . org-lparse-format-todo)
  358. (LINK . org-odt-format-link)
  359. (INLINE-IMAGE . org-odt-format-inline-image)
  360. (ORG-LINK . org-odt-format-org-link)
  361. (HEADING . org-odt-format-heading)
  362. (ANCHOR . org-odt-format-anchor)
  363. (TABLE . org-lparse-format-table)
  364. (TABLE-ROW . org-odt-format-table-row)
  365. (TABLE-CELL . org-odt-format-table-cell)
  366. (FOOTNOTES-SECTION . ignore)
  367. (FOOTNOTE-REFERENCE . org-odt-format-footnote-reference)
  368. (HORIZONTAL-LINE . org-odt-format-horizontal-line)
  369. (COMMENT . org-odt-format-comment)
  370. (LINE . org-odt-format-line)
  371. (ORG-ENTITY . org-odt-format-org-entity))
  372. "")
  373. ;;;_. callbacks
  374. ;;;_. control callbacks
  375. ;;;_ , document body
  376. (defun org-odt-begin-office-body ()
  377. ;; automatic styles
  378. (insert-file-contents
  379. (or org-export-odt-content-template-file
  380. (expand-file-name "styles/OrgOdtContentTemplate.xml"
  381. org-odt-data-dir)))
  382. (goto-char (point-min))
  383. (re-search-forward "</office:text>" nil nil)
  384. (delete-region (match-beginning 0) (point-max)))
  385. ;; Following variable is let bound when `org-do-lparse' is in
  386. ;; progress. See org-html.el.
  387. (defvar org-lparse-toc)
  388. (defun org-odt-begin-document-body (opt-plist)
  389. (org-odt-begin-office-body)
  390. (let ((title (plist-get opt-plist :title)))
  391. (when title
  392. (insert
  393. (org-odt-format-stylized-paragraph 'title title))))
  394. ;; insert toc
  395. (when org-lparse-toc
  396. (insert "\n" org-lparse-toc "\n")))
  397. (defvar org-lparse-body-only) ; let bound during org-do-lparse
  398. (defvar org-lparse-to-buffer) ; let bound during org-do-lparse
  399. (defun org-odt-end-document-body (opt-plist)
  400. (unless org-lparse-body-only
  401. (org-lparse-insert-tag "</office:text>")
  402. (org-lparse-insert-tag "</office:body>")))
  403. (defun org-odt-begin-document-content (opt-plist)
  404. (ignore))
  405. (defun org-odt-end-document-content ()
  406. (org-lparse-insert-tag "</office:document-content>"))
  407. (defun org-odt-begin-outline (level1 snumber title tags
  408. target extra-targets class)
  409. (org-lparse-insert
  410. 'HEADING (org-lparse-format
  411. 'HEADLINE title extra-targets tags snumber level1)
  412. level1 target))
  413. (defun org-odt-end-outline ()
  414. (ignore))
  415. (defun org-odt-begin-outline-text (level1 snumber class)
  416. (ignore))
  417. (defun org-odt-end-outline-text ()
  418. (ignore))
  419. (defun org-odt-begin-paragraph (&optional style)
  420. (org-lparse-insert-tag
  421. "<text:p%s>" (org-odt-get-extra-attrs-for-paragraph-style style)))
  422. (defun org-odt-end-paragraph ()
  423. (org-lparse-insert-tag "</text:p>"))
  424. (defun org-odt-get-extra-attrs-for-paragraph-style (style)
  425. (let (style-name)
  426. (setq style-name
  427. (cond
  428. ((stringp style) style)
  429. ((symbolp style) (org-odt-get-style-name-for-entity
  430. 'paragraph style))))
  431. (unless style-name
  432. (error "Don't know how to handle paragraph style %s" style))
  433. (format " text:style-name=\"%s\"" style-name)))
  434. (defun org-odt-format-stylized-paragraph (style text)
  435. (org-odt-format-tags
  436. '("<text:p%s>" . "</text:p>") text
  437. (org-odt-get-extra-attrs-for-paragraph-style style)))
  438. (defun org-odt-begin-environment (style)
  439. (case style
  440. ((blockquote verse center quote)
  441. (org-lparse-begin-paragraph style)
  442. (list))
  443. ((fixedwidth native)
  444. (org-lparse-end-paragraph)
  445. (list))
  446. (t (error "Unknown environment %s" style))))
  447. (defun org-odt-end-environment (style)
  448. (case style
  449. ((blockquote verse center quote)
  450. (org-lparse-end-paragraph)
  451. (list))
  452. ((fixedwidth native)
  453. (org-lparse-begin-paragraph)
  454. (list))
  455. (t (error "Unknown environment %s" style))))
  456. (defvar org-lparse-list-level) ; dynamically bound in org-do-lparse
  457. (defun org-odt-begin-list (ltype)
  458. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  459. ltype))
  460. (let* ((style-name (org-odt-get-style-name-for-entity 'list ltype))
  461. (extra (concat (when (= org-lparse-list-level 1)
  462. " text:continue-numbering=\"false\"")
  463. (when style-name
  464. (format " text:style-name=\"%s\"" style-name)))))
  465. (case ltype
  466. ((ordered unordered description)
  467. (org-lparse-end-paragraph)
  468. (org-lparse-insert-tag "<text:list%s>" extra))
  469. (t (error "Unknown list type: %s" ltype)))))
  470. (defun org-odt-end-list (ltype)
  471. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  472. ltype))
  473. (if ltype
  474. (org-lparse-insert-tag "</text:list>")
  475. (error "Unknown list type: %s" ltype)))
  476. (defun org-odt-begin-list-item (ltype &optional arg headline)
  477. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  478. ltype))
  479. (case ltype
  480. (ordered
  481. (assert (not headline) t)
  482. (let* ((counter arg) (extra ""))
  483. (org-lparse-insert-tag "<text:list-item>")
  484. (org-lparse-begin-paragraph)))
  485. (unordered
  486. (let* ((id arg) (extra ""))
  487. (org-lparse-insert-tag "<text:list-item>")
  488. (org-lparse-begin-paragraph)
  489. (insert (if headline (org-odt-format-target headline id)
  490. (org-odt-format-bookmark "" id)))))
  491. (description
  492. (assert (not headline) t)
  493. (let ((term (or arg "(no term)")))
  494. (insert
  495. (org-odt-format-tags
  496. '("<text:list-item>" . "</text:list-item>")
  497. (org-odt-format-stylized-paragraph 'definition-term term)))
  498. (org-lparse-begin-list-item 'unordered)
  499. (org-lparse-begin-list 'description)
  500. (org-lparse-begin-list-item 'unordered)))
  501. (t (error "Unknown list type"))))
  502. (defun org-odt-end-list-item (ltype)
  503. (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
  504. ltype))
  505. (case ltype
  506. ((ordered unordered)
  507. (org-lparse-insert-tag "</text:list-item>"))
  508. (description
  509. (org-lparse-end-list-item-1)
  510. (org-lparse-end-list 'description)
  511. (org-lparse-end-list-item-1))
  512. (t (error "Unknown list type"))))
  513. ;; Following variables are let bound when table emission is in
  514. ;; progress. See org-lparse.el.
  515. (defvar org-lparse-table-begin-marker)
  516. (defvar org-lparse-table-ncols)
  517. (defvar org-lparse-table-rowgrp-open)
  518. (defvar org-lparse-table-rownum)
  519. (defvar org-lparse-table-cur-rowgrp-is-hdr)
  520. (defvar org-lparse-table-is-styled)
  521. (defvar org-lparse-table-rowgrp-info)
  522. (defvar org-lparse-table-colalign-vector)
  523. (defvar org-odt-table-style nil
  524. "Table style specified by \"#+ATTR_ODT: <style-name>\" line.
  525. This is set during `org-odt-begin-table'.")
  526. (defvar org-odt-table-style-spec nil
  527. "Entry for `org-odt-table-style' in `org-export-odt-table-styles'.")
  528. (defcustom org-export-odt-table-styles
  529. '(("OrgEquation" "OrgEquation"
  530. ((use-first-column-styles . t)
  531. (use-last-column-styles . t))))
  532. "Specify how Table Styles should be derived from a Table Template.
  533. This is a list where each element is of the
  534. form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
  535. TABLE-STYLE-NAME is the style associated with the table through
  536. `org-odt-table-style'.
  537. TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
  538. TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
  539. below) that is included in
  540. `org-export-odt-content-template-file'.
  541. TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
  542. \"TableCell\"
  543. PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
  544. \"TableParagraph\"
  545. TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
  546. \"FirstRow\" | \"LastRow\" |
  547. \"EvenRow\" | \"OddRow\" |
  548. \"EvenColumn\" | \"OddColumn\" | \"\"
  549. where \"+\" above denotes string concatenation.
  550. TABLE-CELL-OPTIONS is an alist where each element is of the
  551. form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
  552. TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
  553. `use-last-row-styles' |
  554. `use-first-column-styles' |
  555. `use-last-column-styles' |
  556. `use-banding-rows-styles' |
  557. `use-banding-columns-styles' |
  558. `use-first-row-styles'
  559. ON-OR-OFF := `t' | `nil'
  560. For example, with the following configuration
  561. \(setq org-export-odt-table-styles
  562. '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
  563. \(\(use-first-row-styles . t\)
  564. \(use-first-column-styles . t\)\)\)
  565. \(\"TableWithHeaderColumns\" \"Custom\"
  566. \(\(use-first-column-styles . t\)\)\)\)\)
  567. 1. A table associated with \"TableWithHeaderRowsAndColumns\"
  568. style will use the following table-cell styles -
  569. \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
  570. \"CustomTableCell\" and the following paragraph styles
  571. \"CustomFirstRowTableParagraph\",
  572. \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
  573. as appropriate.
  574. 2. A table associated with \"TableWithHeaderColumns\" style will
  575. use the following table-cell styles -
  576. \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
  577. following paragraph styles
  578. \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
  579. as appropriate..
  580. Note that TABLE-TEMPLATE-NAME corresponds to the
  581. \"<table:table-template>\" elements contained within
  582. \"<office:styles>\". The entries (TABLE-STYLE-NAME
  583. TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
  584. \"table:template-name\" and \"table:use-first-row-styles\" etc
  585. attributes of \"<table:table>\" element. Refer ODF-1.2
  586. specification for more information. Also consult the
  587. implementation filed under `org-odt-get-table-cell-styles'.
  588. The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
  589. formatting of numbered display equations. Do not delete this
  590. style from the list."
  591. :group 'org-export-odt
  592. :type '(choice
  593. (const :tag "None" nil)
  594. (repeat :tag "Table Styles"
  595. (list :tag "Table Style Specification"
  596. (string :tag "Table Style Name")
  597. (string :tag "Table Template Name")
  598. (alist :options (use-first-row-styles
  599. use-last-row-styles
  600. use-first-column-styles
  601. use-last-column-styles
  602. use-banding-rows-styles
  603. use-banding-columns-styles)
  604. :key-type symbol
  605. :value-type (const :tag "True" t))))))
  606. (defun org-odt-begin-table (caption label attributes)
  607. (setq org-odt-table-style attributes)
  608. (setq org-odt-table-style-spec
  609. (assoc org-odt-table-style org-export-odt-table-styles))
  610. (when label
  611. (insert
  612. (org-odt-format-stylized-paragraph
  613. 'table (org-odt-format-entity-caption label caption "Table"))))
  614. (org-lparse-insert-tag
  615. "<table:table table:name=\"%s\" table:style-name=\"%s\">"
  616. (or label "") (or (nth 1 org-odt-table-style-spec) "OrgTable"))
  617. (setq org-lparse-table-begin-marker (point)))
  618. (defvar org-lparse-table-colalign-info)
  619. (defun org-odt-end-table ()
  620. (goto-char org-lparse-table-begin-marker)
  621. (loop for level from 0 below org-lparse-table-ncols
  622. do (let* ((col-cookie (and org-lparse-table-is-styled
  623. (cdr (assoc (1+ level)
  624. org-lparse-table-colalign-info))))
  625. (extra-columns (or (nth 1 col-cookie) 0)))
  626. (dotimes (i (1+ extra-columns))
  627. (insert
  628. (org-odt-format-tags
  629. "<table:table-column table:style-name=\"%sColumn\"/>"
  630. "" (or (nth 1 org-odt-table-style-spec) "OrgTable"))))
  631. (insert "\n")))
  632. ;; fill style attributes for table cells
  633. (when org-lparse-table-is-styled
  634. (while (re-search-forward "@@\\(table-cell:p\\|table-cell:style-name\\)@@\\([0-9]+\\)@@\\([0-9]+\\)@@" nil t)
  635. (let* ((spec (match-string 1))
  636. (r (string-to-number (match-string 2)))
  637. (c (string-to-number (match-string 3)))
  638. (cell-styles (org-odt-get-table-cell-styles
  639. r c org-odt-table-style-spec))
  640. (table-cell-style (car cell-styles))
  641. (table-cell-paragraph-style (cdr cell-styles)))
  642. (cond
  643. ((equal spec "table-cell:p")
  644. (replace-match table-cell-paragraph-style t t))
  645. ((equal spec "table-cell:style-name")
  646. (replace-match table-cell-style t t))))))
  647. (goto-char (point-max))
  648. (org-lparse-insert-tag "</table:table>"))
  649. (defun org-odt-begin-table-rowgroup (&optional is-header-row)
  650. (when org-lparse-table-rowgrp-open
  651. (org-lparse-end 'TABLE-ROWGROUP))
  652. (org-lparse-insert-tag (if is-header-row
  653. "<table:table-header-rows>"
  654. "<table:table-rows>"))
  655. (setq org-lparse-table-rowgrp-open t)
  656. (setq org-lparse-table-cur-rowgrp-is-hdr is-header-row))
  657. (defun org-odt-end-table-rowgroup ()
  658. (when org-lparse-table-rowgrp-open
  659. (setq org-lparse-table-rowgrp-open nil)
  660. (org-lparse-insert-tag
  661. (if org-lparse-table-cur-rowgrp-is-hdr
  662. "</table:table-header-rows>" "</table:table-rows>"))))
  663. (defun org-odt-format-table-row (row)
  664. (org-odt-format-tags
  665. '("<table:table-row>" . "</table:table-row>") row))
  666. (defun org-odt-get-table-cell-styles (r c &optional style-spec)
  667. "Retrieve styles applicable to a table cell.
  668. R and C are (zero-based) row and column numbers of the table
  669. cell. STYLE-SPEC is an entry in `org-export-odt-table-styles'
  670. applicable to the current table. It is `nil' if the table is not
  671. associated with any style attributes.
  672. Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
  673. When STYLE-SPEC is nil, style the table cell the conventional way
  674. - choose cell borders based on row and column groupings and
  675. choose paragraph alignment based on `org-col-cookies' text
  676. property. See also
  677. `org-odt-get-paragraph-style-cookie-for-table-cell'.
  678. When STYLE-SPEC is non-nil, ignore the above cookie and return
  679. styles congruent with the ODF-1.2 specification."
  680. (cond
  681. (style-spec
  682. ;; LibreOffice - particularly the Writer - honors neither table
  683. ;; templates nor custom table-cell styles. Inorder to retain
  684. ;; inter-operability with LibreOffice, only automatic styles are
  685. ;; used for styling of table-cells. The current implementation is
  686. ;; congruent with ODF-1.2 specification and hence is
  687. ;; future-compatible.
  688. ;; Additional Note: LibreOffice's AutoFormat facility for tables -
  689. ;; which recognizes as many as 16 different cell types - is much
  690. ;; richer. Unfortunately it is NOT amenable to easy configuration
  691. ;; by hand.
  692. (let* ((template-name (nth 1 style-spec))
  693. (cell-style-selectors (nth 2 style-spec))
  694. (cell-type
  695. (cond
  696. ((and (cdr (assoc 'use-first-column-styles cell-style-selectors))
  697. (= c 0)) "FirstColumn")
  698. ((and (cdr (assoc 'use-last-column-styles cell-style-selectors))
  699. (= c (1- org-lparse-table-ncols))) "LastColumn")
  700. ((and (cdr (assoc 'use-first-row-styles cell-style-selectors))
  701. (= r 0)) "FirstRow")
  702. ((and (cdr (assoc 'use-last-row-styles cell-style-selectors))
  703. (= r org-lparse-table-rownum))
  704. "LastRow")
  705. ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
  706. (= (% r 2) 1)) "EvenRow")
  707. ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
  708. (= (% r 2) 0)) "OddRow")
  709. ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
  710. (= (% c 2) 1)) "EvenColumn")
  711. ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
  712. (= (% c 2) 0)) "OddColumn")
  713. (t ""))))
  714. (cons
  715. (concat template-name cell-type "TableCell")
  716. (concat template-name cell-type "TableParagraph"))))
  717. (t
  718. (cons
  719. (concat
  720. "OrgTblCell"
  721. (cond
  722. ((= r 0) "T")
  723. ((eq (cdr (assoc r org-lparse-table-rowgrp-info)) :start) "T")
  724. (t ""))
  725. (when (= r org-lparse-table-rownum) "B")
  726. (cond
  727. ((= c 0) "")
  728. ((or (memq (nth c org-table-colgroup-info) '(:start :startend))
  729. (memq (nth (1- c) org-table-colgroup-info) '(:end :startend))) "L")
  730. (t "")))
  731. (capitalize (aref org-lparse-table-colalign-vector c))))))
  732. (defun org-odt-get-paragraph-style-cookie-for-table-cell (r c)
  733. (concat
  734. (and (not org-odt-table-style-spec)
  735. (cond
  736. (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
  737. ((and (= c 0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS))
  738. "OrgTableHeading")
  739. (t "OrgTableContents")))
  740. (and org-lparse-table-is-styled
  741. (format "@@table-cell:p@@%03d@@%03d@@" r c))))
  742. (defun org-odt-get-style-name-cookie-for-table-cell (r c)
  743. (when org-lparse-table-is-styled
  744. (format "@@table-cell:style-name@@%03d@@%03d@@" r c)))
  745. (defun org-odt-format-table-cell (data r c horiz-span)
  746. (concat
  747. (let* ((paragraph-style-cookie
  748. (org-odt-get-paragraph-style-cookie-for-table-cell r c))
  749. (style-name-cookie
  750. (org-odt-get-style-name-cookie-for-table-cell r c))
  751. (extra (and style-name-cookie
  752. (format " table:style-name=\"%s\"" style-name-cookie)))
  753. (extra (concat extra
  754. (and (> horiz-span 0)
  755. (format " table:number-columns-spanned=\"%d\""
  756. (1+ horiz-span))))))
  757. (org-odt-format-tags
  758. '("<table:table-cell%s>" . "</table:table-cell>")
  759. (if org-lparse-list-table-p data
  760. (org-odt-format-stylized-paragraph paragraph-style-cookie data)) extra))
  761. (let (s)
  762. (dotimes (i horiz-span)
  763. (setq s (concat s "\n<table:covered-table-cell/>"))) s)
  764. "\n"))
  765. (defun org-odt-begin-footnote-definition (n)
  766. (org-lparse-begin-paragraph 'footnote))
  767. (defun org-odt-end-footnote-definition (n)
  768. (org-lparse-end-paragraph))
  769. (defun org-odt-begin-toc (lang-specific-heading)
  770. (insert
  771. (format "
  772. <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
  773. <text:table-of-content-source text:outline-level=\"10\">
  774. <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
  775. " lang-specific-heading))
  776. (loop for level from 1 upto 10
  777. do (insert (format
  778. "
  779. <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
  780. <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
  781. <text:index-entry-chapter/>
  782. <text:index-entry-text/>
  783. <text:index-entry-link-end/>
  784. </text:table-of-content-entry-template>
  785. " level level)))
  786. (insert
  787. (format "
  788. </text:table-of-content-source>
  789. <text:index-body>
  790. <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
  791. <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
  792. </text:index-title>
  793. " lang-specific-heading)))
  794. (defun org-odt-end-toc ()
  795. (insert "
  796. </text:index-body>
  797. </text:table-of-content>
  798. "))
  799. (defun org-odt-format-toc-entry (snumber todo headline tags href)
  800. (setq headline (concat
  801. (and org-export-with-section-numbers
  802. (concat snumber ". "))
  803. headline
  804. (and tags
  805. (concat
  806. (org-lparse-format 'SPACES 3)
  807. (org-lparse-format 'FONTIFY tags "tag")))))
  808. (when todo
  809. (setq headline (org-lparse-format 'FONTIFY headline "todo")))
  810. (let ((org-odt-suppress-xref t))
  811. (org-odt-format-link headline (concat "#" href))))
  812. (defun org-odt-format-toc-item (toc-entry level org-last-level)
  813. (let ((style (format "Contents_20_%d"
  814. (+ level (or (org-lparse-get 'TOPLEVEL-HLEVEL) 1) -1))))
  815. (insert "\n" (org-odt-format-stylized-paragraph style toc-entry) "\n")))
  816. ;; Following variable is let bound during 'ORG-LINK callback. See
  817. ;; org-html.el
  818. (defvar org-lparse-link-description-is-image nil)
  819. (defun org-odt-format-link (desc href &optional attr)
  820. (cond
  821. ((and (= (string-to-char href) ?#) (not org-odt-suppress-xref))
  822. (setq href (concat org-export-odt-bookmark-prefix (substring href 1)))
  823. (let ((xref-format "text"))
  824. (when (numberp desc)
  825. (setq desc (format "%d" desc) xref-format "number"))
  826. (org-odt-format-tags
  827. '("<text:bookmark-ref text:reference-format=\"%s\" text:ref-name=\"%s\">" .
  828. "</text:bookmark-ref>")
  829. desc xref-format href)))
  830. (org-lparse-link-description-is-image
  831. (org-odt-format-tags
  832. '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</draw:a>")
  833. desc href (or attr "")))
  834. (t
  835. (org-odt-format-tags
  836. '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</text:a>")
  837. desc href (or attr "")))))
  838. (defun org-odt-format-spaces (n)
  839. (cond
  840. ((= n 1) " ")
  841. ((> n 1) (concat
  842. " " (org-odt-format-tags "<text:s text:c=\"%d\"/>" "" (1- n))))
  843. (t "")))
  844. (defun org-odt-format-tabs (&optional n)
  845. (let ((tab "<text:tab/>")
  846. (n (or n 1)))
  847. (insert tab)))
  848. (defun org-odt-format-line-break ()
  849. (org-odt-format-tags "<text:line-break/>" ""))
  850. (defun org-odt-format-horizontal-line ()
  851. (org-odt-format-stylized-paragraph 'horizontal-line ""))
  852. (defun org-odt-format-line (line)
  853. (case org-lparse-dyn-current-environment
  854. (fixedwidth (concat
  855. (org-odt-format-stylized-paragraph
  856. 'fixedwidth (org-odt-fill-tabs-and-spaces
  857. (org-xml-encode-plain-text line))) "\n"))
  858. (t (concat line "\n"))))
  859. (defun org-odt-format-comment (fmt &rest args)
  860. (let ((comment (apply 'format fmt args)))
  861. (format "\n<!-- %s -->\n" comment)))
  862. (defun org-odt-format-org-entity (wd)
  863. (org-entity-get-representation wd 'utf8))
  864. (defun org-odt-fill-tabs-and-spaces (line)
  865. (replace-regexp-in-string
  866. "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
  867. (cond
  868. ((string= s "\t") (org-odt-format-tabs))
  869. (t (org-odt-format-spaces (length s))))) line))
  870. (defcustom org-export-odt-fontify-srcblocks t
  871. "Specify whether or not source blocks need to be fontified.
  872. Turn this option on if you want to colorize the source code
  873. blocks in the exported file. For colorization to work, you need
  874. to make available an enhanced version of `htmlfontify' library."
  875. :type 'boolean
  876. :group 'org-export-odt)
  877. (defun org-odt-format-source-line-with-line-number-and-label
  878. (line rpllbl num fontifier par-style)
  879. (let ((keep-label (not (numberp rpllbl)))
  880. (ref (org-find-text-property-in-string 'org-coderef line)))
  881. (setq line (concat line (and keep-label ref (format "(%s)" ref))))
  882. (setq line (funcall fontifier line))
  883. (when ref
  884. (setq line (org-odt-format-target line (concat "coderef-" ref))))
  885. (setq line (org-odt-format-stylized-paragraph par-style line))
  886. (when num
  887. (org-odt-format-tags '("<text:list-item>" . "</text:list-item>") line))))
  888. (defun org-odt-format-source-code-or-example-plain
  889. (lines lang caption textareap cols rows num cont rpllbl fmt)
  890. "Format source or example blocks much like fixedwidth blocks.
  891. Use this when `org-export-odt-fontify-srcblocks' option is turned
  892. off."
  893. (let* ((lines (org-split-string lines "[\r\n]"))
  894. (line-count (length lines))
  895. (i 0))
  896. (mapconcat
  897. (lambda (line)
  898. (incf i)
  899. (org-odt-format-source-line-with-line-number-and-label
  900. line rpllbl num (lambda (line)
  901. (org-odt-fill-tabs-and-spaces
  902. (org-xml-encode-plain-text line)))
  903. (if (= i line-count) "OrgFixedWidthBlockLastLine" "OrgFixedWidthBlock")))
  904. lines "\n")))
  905. (defvar org-src-block-paragraph-format
  906. "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
  907. <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
  908. <style:background-image/>
  909. </style:paragraph-properties>
  910. <style:text-properties fo:color=\"%s\"/>
  911. </style:style>"
  912. "Custom paragraph style for colorized source and example blocks.
  913. This style is much the same as that of \"OrgFixedWidthBlock\"
  914. except that the foreground and background colors are set
  915. according to the default face identified by the `htmlfontify'.")
  916. (defun org-odt-hfy-face-to-css (fn)
  917. "Create custom style for face FN.
  918. When FN is the default face, use it's foreground and background
  919. properties to create \"OrgSrcBlock\" paragraph style. Otherwise
  920. use it's color attribute to create a character style whose name
  921. is obtained from FN. Currently all attributes of FN other than
  922. color are ignored.
  923. The style name for a face FN is derived using the following
  924. operations on the face name in that order - de-dash, CamelCase
  925. and prefix with \"OrgSrc\". For example,
  926. `font-lock-function-name-face' is associated with
  927. \"OrgSrcFontLockFunctionNameFace\"."
  928. (let* ((css-list (hfy-face-to-style fn))
  929. (style-name ((lambda (fn)
  930. (concat "OrgSrc"
  931. (mapconcat
  932. 'capitalize (split-string
  933. (hfy-face-or-def-to-name fn) "-")
  934. ""))) fn))
  935. (color-val (cdr (assoc "color" css-list)))
  936. (background-color-val (cdr (assoc "background" css-list)))
  937. (style (and org-export-odt-create-custom-styles-for-srcblocks
  938. (cond
  939. ((eq fn 'default)
  940. (format org-src-block-paragraph-format
  941. background-color-val color-val))
  942. (t
  943. (format
  944. "
  945. <style:style style:name=\"%s\" style:family=\"text\">
  946. <style:text-properties fo:color=\"%s\"/>
  947. </style:style>" style-name color-val))))))
  948. (cons style-name style)))
  949. (defcustom org-export-odt-create-custom-styles-for-srcblocks t
  950. "Whether custom styles for colorized source blocks be automatically created.
  951. When this option is turned on, the exporter creates custom styles
  952. for source blocks based on the advice of `htmlfontify'. Creation
  953. of custom styles happen as part of `org-odt-hfy-face-to-css'.
  954. When this option is turned off exporter does not create such
  955. styles.
  956. Use the latter option if you do not want the custom styles to be
  957. based on your current display settings. It is necessary that the
  958. styles.xml already contains needed styles for colorizing to work.
  959. This variable is effective only if
  960. `org-export-odt-fontify-srcblocks' is turned on."
  961. :group 'org-export-odt
  962. :type 'boolean)
  963. (defun org-odt-insert-custom-styles-for-srcblocks (styles)
  964. "Save STYLES used for colorizing of source blocks.
  965. Update styles.xml with styles that were collected as part of
  966. `org-odt-hfy-face-to-css' callbacks."
  967. (when styles
  968. (with-current-buffer
  969. (find-file-noselect (expand-file-name "styles.xml") t)
  970. (goto-char (point-min))
  971. (when (re-search-forward "</office:styles>" nil t)
  972. (goto-char (match-beginning 0))
  973. (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n")))))
  974. (defun org-odt-format-source-code-or-example-colored
  975. (lines lang caption textareap cols rows num cont rpllbl fmt)
  976. "Format source or example blocks using `htmlfontify-string'.
  977. Use this routine when `org-export-odt-fontify-srcblocks' option
  978. is turned on."
  979. (let* ((lang-m (and lang (or (cdr (assoc lang org-src-lang-modes)) lang)))
  980. (mode (and lang-m (intern (concat (if (symbolp lang-m)
  981. (symbol-name lang-m)
  982. lang-m) "-mode"))))
  983. (org-inhibit-startup t)
  984. (org-startup-folded nil)
  985. (lines (with-temp-buffer
  986. (insert lines)
  987. (if (functionp mode) (funcall mode) (fundamental-mode))
  988. (font-lock-fontify-buffer)
  989. (buffer-string)))
  990. (hfy-html-quote-regex "\\([<\"&> ]\\)")
  991. (hfy-html-quote-map '(("\"" "&quot;")
  992. ("<" "&lt;")
  993. ("&" "&amp;")
  994. (">" "&gt;")
  995. (" " "<text:s/>")
  996. (" " "<text:tab/>")))
  997. (hfy-face-to-css 'org-odt-hfy-face-to-css)
  998. (hfy-optimisations-1 (copy-seq hfy-optimisations))
  999. (hfy-optimisations (add-to-list 'hfy-optimisations-1
  1000. 'body-text-only))
  1001. (hfy-begin-span-handler
  1002. (lambda (style text-block text-id text-begins-block-p)
  1003. (insert (format "<text:span text:style-name=\"%s\">" style))))
  1004. (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
  1005. (when (fboundp 'htmlfontify-string)
  1006. (let* ((lines (org-split-string lines "[\r\n]"))
  1007. (line-count (length lines))
  1008. (i 0))
  1009. (mapconcat
  1010. (lambda (line)
  1011. (incf i)
  1012. (org-odt-format-source-line-with-line-number-and-label
  1013. line rpllbl num 'htmlfontify-string
  1014. (if (= i line-count) "OrgSrcBlockLastLine" "OrgSrcBlock")))
  1015. lines "\n")))))
  1016. (defun org-odt-format-source-code-or-example (lines lang caption textareap
  1017. cols rows num cont
  1018. rpllbl fmt)
  1019. "Format source or example blocks for export.
  1020. Use `org-odt-format-source-code-or-example-plain' or
  1021. `org-odt-format-source-code-or-example-colored' depending on the
  1022. value of `org-export-odt-fontify-srcblocks."
  1023. (setq lines (org-export-number-lines
  1024. lines 0 0 num cont rpllbl fmt 'preprocess)
  1025. lines (funcall
  1026. (or (and org-export-odt-fontify-srcblocks
  1027. (or (featurep 'htmlfontify)
  1028. (require 'htmlfontify))
  1029. (fboundp 'htmlfontify-string)
  1030. 'org-odt-format-source-code-or-example-colored)
  1031. 'org-odt-format-source-code-or-example-plain)
  1032. lines lang caption textareap cols rows num cont rpllbl fmt))
  1033. (if (not num) lines
  1034. (let ((extra (format " text:continue-numbering=\"%s\""
  1035. (if cont "true" "false"))))
  1036. (org-odt-format-tags
  1037. '("<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>"
  1038. . "</text:list>") lines extra))))
  1039. (defun org-odt-remap-stylenames (style-name)
  1040. (or
  1041. (cdr (assoc style-name '(("timestamp-wrapper" . "OrgTimestampWrapper")
  1042. ("timestamp" . "OrgTimestamp")
  1043. ("timestamp-kwd" . "OrgTimestampKeyword")
  1044. ("tag" . "OrgTag")
  1045. ("todo" . "OrgTodo")
  1046. ("done" . "OrgDone")
  1047. ("target" . "OrgTarget"))))
  1048. style-name))
  1049. (defun org-odt-format-fontify (text style &optional id)
  1050. (let* ((style-name
  1051. (cond
  1052. ((stringp style)
  1053. (org-odt-remap-stylenames style))
  1054. ((symbolp style)
  1055. (org-odt-get-style-name-for-entity 'character style))
  1056. ((listp style)
  1057. (assert (< 1 (length style)))
  1058. (let ((parent-style (pop style)))
  1059. (mapconcat (lambda (s)
  1060. ;; (assert (stringp s) t)
  1061. (org-odt-remap-stylenames s)) style "")
  1062. (org-odt-remap-stylenames parent-style)))
  1063. (t (error "Don't how to handle style %s" style)))))
  1064. (org-odt-format-tags
  1065. '("<text:span text:style-name=\"%s\">" . "</text:span>")
  1066. text style-name)))
  1067. (defun org-odt-relocate-relative-path (path dir)
  1068. (if (file-name-absolute-p path) path
  1069. (file-relative-name (expand-file-name path dir)
  1070. (expand-file-name "eyecandy" dir))))
  1071. (defun org-odt-format-inline-image (thefile)
  1072. (let* ((thelink (if (file-name-absolute-p thefile) thefile
  1073. (org-xml-format-href
  1074. (org-odt-relocate-relative-path
  1075. thefile org-current-export-file))))
  1076. (href
  1077. (org-odt-format-tags
  1078. "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
  1079. (if org-export-odt-embed-images
  1080. (org-odt-copy-image-file thefile) thelink))))
  1081. (org-export-odt-format-image thefile href)))
  1082. (defun org-export-odt-format-formula (src href &optional embed-as)
  1083. "Create image tag with source and attributes."
  1084. (save-match-data
  1085. (let* ((caption (org-find-text-property-in-string 'org-caption src))
  1086. (caption (and caption (org-xml-format-desc caption)))
  1087. (label (org-find-text-property-in-string 'org-label src))
  1088. (embed-as (or embed-as
  1089. (and (org-find-text-property-in-string
  1090. 'org-latex-src src)
  1091. (org-find-text-property-in-string
  1092. 'org-latex-src-embed-type src))
  1093. 'paragraph))
  1094. width height)
  1095. (cond
  1096. ((eq embed-as 'character)
  1097. (org-odt-format-entity "InlineFormula" href width height))
  1098. (t
  1099. (org-lparse-end-paragraph)
  1100. (org-lparse-insert-list-table
  1101. `((,(org-odt-format-entity
  1102. (if caption "CaptionedDisplayFormula" "DisplayFormula")
  1103. href width height caption nil)
  1104. ,(if (not label) ""
  1105. (org-odt-format-entity-caption label nil "Equation"))))
  1106. nil nil nil "OrgEquation" nil '((1 "c" 8) (2 "c" 1)))
  1107. (throw 'nextline nil))))))
  1108. (defvar org-odt-embedded-formulas-count 0)
  1109. (defun org-odt-copy-formula-file (path)
  1110. "Returns the internal name of the file"
  1111. (let* ((src-file (expand-file-name
  1112. path (file-name-directory org-current-export-file)))
  1113. (target-dir (format "Formula-%04d/"
  1114. (incf org-odt-embedded-formulas-count)))
  1115. (target-file (concat target-dir "content.xml")))
  1116. (when (not org-lparse-to-buffer)
  1117. (message "Embedding %s as %s ..."
  1118. (substring-no-properties path) target-file)
  1119. (make-directory target-dir)
  1120. (org-odt-create-manifest-file-entry
  1121. "application/vnd.oasis.opendocument.formula" target-dir "1.2")
  1122. (copy-file src-file target-file 'overwrite)
  1123. (org-odt-create-manifest-file-entry "text/xml" target-file))
  1124. target-file))
  1125. (defun org-odt-format-inline-formula (thefile)
  1126. (let* ((thelink (if (file-name-absolute-p thefile) thefile
  1127. (org-xml-format-href
  1128. (org-odt-relocate-relative-path
  1129. thefile org-current-export-file))))
  1130. (href
  1131. (org-odt-format-tags
  1132. "<draw:object xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
  1133. (file-name-directory (org-odt-copy-formula-file thefile)))))
  1134. (org-export-odt-format-formula thefile href)))
  1135. (defun org-odt-is-formula-link-p (file)
  1136. (let ((case-fold-search nil))
  1137. (string-match "\\.mathml\\'" file)))
  1138. (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
  1139. descp)
  1140. "Make an HTML link.
  1141. OPT-PLIST is an options list.
  1142. TYPE is the device-type of the link (THIS://foo.html)
  1143. PATH is the path of the link (http://THIS#locationx)
  1144. FRAGMENT is the fragment part of the link, if any (foo.html#THIS)
  1145. DESC is the link description, if any.
  1146. ATTR is a string of other attributes of the a element.
  1147. MAY-INLINE-P allows inlining it as an image."
  1148. (declare (special org-lparse-par-open))
  1149. (save-match-data
  1150. (let* ((may-inline-p
  1151. (and (member type-1 '("http" "https" "file"))
  1152. (org-lparse-should-inline-p path descp)
  1153. (not fragment)))
  1154. (type (if (equal type-1 "id") "file" type-1))
  1155. (filename path)
  1156. (thefile path))
  1157. (cond
  1158. ;; check for inlined images
  1159. ((and (member type '("file"))
  1160. (not fragment)
  1161. (org-file-image-p
  1162. filename org-export-odt-inline-image-extensions)
  1163. (or (eq t org-export-odt-inline-images)
  1164. (and org-export-odt-inline-images (not descp))))
  1165. (org-odt-format-inline-image thefile))
  1166. ;; check for embedded formulas
  1167. ((and (member type '("file"))
  1168. (not fragment)
  1169. (org-odt-is-formula-link-p filename)
  1170. (or (not descp)))
  1171. (org-odt-format-inline-formula thefile))
  1172. ((string= type "coderef")
  1173. (let* ((ref fragment)
  1174. (lineno-or-ref (cdr (assoc ref org-export-code-refs)))
  1175. (desc (and descp desc))
  1176. (org-odt-suppress-xref nil)
  1177. (href (org-xml-format-href (concat "#coderef-" ref))))
  1178. (cond
  1179. ((and (numberp lineno-or-ref) (not desc))
  1180. (org-odt-format-link lineno-or-ref href))
  1181. ((and (numberp lineno-or-ref) desc
  1182. (string-match (regexp-quote (concat "(" ref ")")) desc))
  1183. (format (replace-match "%s" t t desc)
  1184. (org-odt-format-link lineno-or-ref href)))
  1185. (t
  1186. (setq desc (format
  1187. (if (and desc (string-match
  1188. (regexp-quote (concat "(" ref ")"))
  1189. desc))
  1190. (replace-match "%s" t t desc)
  1191. (or desc "%s"))
  1192. lineno-or-ref))
  1193. (org-odt-format-link (org-xml-format-desc desc) href)))))
  1194. (t
  1195. (when (string= type "file")
  1196. (setq thefile
  1197. (cond
  1198. ((file-name-absolute-p path)
  1199. (concat "file://" (expand-file-name path)))
  1200. (t (org-odt-relocate-relative-path
  1201. thefile org-current-export-file)))))
  1202. (when (and (member type '("" "http" "https" "file")) fragment)
  1203. (setq thefile (concat thefile "#" fragment)))
  1204. (setq thefile (org-xml-format-href thefile))
  1205. (when (not (member type '("" "file")))
  1206. (setq thefile (concat type ":" thefile)))
  1207. (let ((org-odt-suppress-xref nil))
  1208. (org-odt-format-link
  1209. (org-xml-format-desc desc) thefile attr)))))))
  1210. (defun org-odt-format-heading (text level &optional id)
  1211. (let* ((text (if id (org-odt-format-target text id) text)))
  1212. (org-odt-format-tags
  1213. '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
  1214. "</text:h>") text level level)))
  1215. (defun org-odt-format-headline (title extra-targets tags
  1216. &optional snumber level)
  1217. (concat
  1218. (org-lparse-format 'EXTRA-TARGETS extra-targets)
  1219. ;; No need to generate section numbers. They are auto-generated by
  1220. ;; the application
  1221. ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
  1222. title
  1223. (and tags (concat (org-lparse-format 'SPACES 3)
  1224. (org-lparse-format 'ORG-TAGS tags)))))
  1225. (defun org-odt-format-anchor (text name &optional class)
  1226. (org-odt-format-target text name))
  1227. (defun org-odt-format-bookmark (text id)
  1228. (if id
  1229. (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id)
  1230. text))
  1231. (defun org-odt-format-target (text id)
  1232. (let ((name (concat org-export-odt-bookmark-prefix id)))
  1233. (concat
  1234. (and id (org-odt-format-tags
  1235. "<text:bookmark-start text:name=\"%s\"/>" "" name))
  1236. (org-odt-format-bookmark text id)
  1237. (and id (org-odt-format-tags
  1238. "<text:bookmark-end text:name=\"%s\"/>" "" name)))))
  1239. (defun org-odt-format-footnote (n def)
  1240. (let ((id (concat "fn" n))
  1241. (note-class "footnote")
  1242. (par-style "Footnote"))
  1243. (org-odt-format-tags
  1244. '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
  1245. "</text:note>")
  1246. (concat
  1247. (org-odt-format-tags
  1248. '("<text:note-citation>" . "</text:note-citation>")
  1249. n)
  1250. (org-odt-format-tags
  1251. '("<text:note-body>" . "</text:note-body>")
  1252. def))
  1253. id note-class)))
  1254. (defun org-odt-format-footnote-reference (n def refcnt)
  1255. (if (= refcnt 1)
  1256. (org-odt-format-footnote n def)
  1257. (org-odt-format-footnote-ref n)))
  1258. (defun org-odt-format-footnote-ref (n)
  1259. (let ((note-class "footnote")
  1260. (ref-format "text")
  1261. (ref-name (concat "fn" n)))
  1262. (org-odt-format-tags
  1263. '("<text:span text:style-name=\"%s\">" . "</text:span>")
  1264. (org-odt-format-tags
  1265. '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" . "</text:note-ref>")
  1266. n note-class ref-format ref-name)
  1267. "OrgSuperscript")))
  1268. (defun org-odt-get-image-name (file-name)
  1269. (require 'sha1)
  1270. (file-relative-name
  1271. (expand-file-name
  1272. (concat (sha1 file-name) "." (file-name-extension file-name)) "Pictures")))
  1273. (defun org-export-odt-format-image (src href &optional embed-as)
  1274. "Create image tag with source and attributes."
  1275. (save-match-data
  1276. (let* ((caption (org-find-text-property-in-string 'org-caption src))
  1277. (caption (and caption (org-xml-format-desc caption)))
  1278. (attr (org-find-text-property-in-string 'org-attributes src))
  1279. (label (org-find-text-property-in-string 'org-label src))
  1280. (embed-as (or embed-as
  1281. (if (org-find-text-property-in-string
  1282. 'org-latex-src src)
  1283. (or (org-find-text-property-in-string
  1284. 'org-latex-src-embed-type src) 'character)
  1285. 'paragraph)))
  1286. (attr-plist (when attr (read attr)))
  1287. (size (org-odt-image-size-from-file
  1288. src (plist-get attr-plist :width)
  1289. (plist-get attr-plist :height)
  1290. (plist-get attr-plist :scale) nil embed-as))
  1291. (width (car size)) (height (cdr size)))
  1292. (cond
  1293. ((not (or caption label))
  1294. (case embed-as
  1295. (paragraph (org-odt-format-entity "DisplayImage" href width height))
  1296. (character (org-odt-format-entity "InlineImage" href width height))
  1297. (t (error "Unknown value for embed-as %S" embed-as))))
  1298. (t
  1299. (org-odt-format-entity
  1300. "CaptionedDisplayImage" href width height caption label))))))
  1301. (defun org-odt-format-frame (text width height style &optional
  1302. extra anchor-type)
  1303. (let ((frame-attrs
  1304. (concat
  1305. (if width (format " svg:width=\"%0.2fcm\"" width) "")
  1306. (if height (format " svg:height=\"%0.2fcm\"" height) "")
  1307. extra
  1308. (format " text:anchor-type=\"%s\"" (or anchor-type "paragraph")))))
  1309. (org-odt-format-tags
  1310. '("<draw:frame draw:style-name=\"%s\"%s>" . "</draw:frame>")
  1311. text style frame-attrs)))
  1312. (defun org-odt-format-textbox (text width height style &optional
  1313. extra anchor-type)
  1314. (org-odt-format-frame
  1315. (org-odt-format-tags
  1316. '("<draw:text-box %s>" . "</draw:text-box>")
  1317. text (concat (format " fo:min-height=\"%0.2fcm\"" (or height .2))
  1318. (format " fo:min-width=\"%0.2fcm\"" (or width .2))))
  1319. width nil style extra anchor-type))
  1320. (defun org-odt-format-inlinetask (heading content
  1321. &optional todo priority tags)
  1322. (org-odt-format-stylized-paragraph
  1323. nil (org-odt-format-textbox
  1324. (concat (org-odt-format-stylized-paragraph
  1325. "OrgInlineTaskHeading"
  1326. (org-lparse-format
  1327. 'HEADLINE (concat (org-lparse-format-todo todo) " " heading)
  1328. nil tags))
  1329. content) nil nil "OrgInlineTaskFrame" " style:rel-width=\"100%\"")))
  1330. (defvar org-odt-entity-frame-styles
  1331. '(("InlineImage" "Figure" ("OrgInlineImage" nil "as-char"))
  1332. ("DisplayImage" "Figure" ("OrgDisplayImage" nil "paragraph"))
  1333. ("CaptionedDisplayImage" "Figure"
  1334. ("OrgCaptionedImage"
  1335. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  1336. ("OrgImageCaptionFrame"))
  1337. ("InlineFormula" "Equation" ("OrgInlineFormula" nil "as-char"))
  1338. ("DisplayFormula" "Equation" ("OrgDisplayFormula" nil "as-char"))
  1339. ("CaptionedDisplayFormula" "Equation"
  1340. ("OrgCaptionedFormula" nil "paragraph")
  1341. ("OrgFormulaCaptionFrame" nil "as-char"))))
  1342. (defun org-odt-format-entity (entity href width height &optional caption label)
  1343. (let* ((entity-style (assoc entity org-odt-entity-frame-styles))
  1344. (entity-frame (apply 'org-odt-format-frame
  1345. href width height (nth 2 entity-style))))
  1346. (if (not (or caption label)) entity-frame
  1347. (apply 'org-odt-format-textbox
  1348. (org-odt-format-stylized-paragraph
  1349. 'illustration
  1350. (concat entity-frame (org-odt-format-entity-caption
  1351. label caption (nth 1 entity-style))))
  1352. width height (nth 3 entity-style)))))
  1353. (defvar org-odt-embedded-images-count 0)
  1354. (defun org-odt-copy-image-file (path)
  1355. "Returns the internal name of the file"
  1356. (let* ((image-type (file-name-extension path))
  1357. (media-type (format "image/%s" image-type))
  1358. (src-file (expand-file-name
  1359. path (file-name-directory org-current-export-file)))
  1360. (target-dir "Images/")
  1361. (target-file
  1362. (format "%s%04d.%s" target-dir
  1363. (incf org-odt-embedded-images-count) image-type)))
  1364. (when (not org-lparse-to-buffer)
  1365. (message "Embedding %s as %s ..."
  1366. (substring-no-properties path) target-file)
  1367. (when (= 1 org-odt-embedded-images-count)
  1368. (make-directory target-dir)
  1369. (org-odt-create-manifest-file-entry "" target-dir))
  1370. (copy-file src-file target-file 'overwrite)
  1371. (org-odt-create-manifest-file-entry media-type target-file))
  1372. target-file))
  1373. (defvar org-export-odt-image-size-probe-method
  1374. '(emacs imagemagick force)
  1375. "Ordered list of methods by for determining size of an embedded
  1376. image.")
  1377. (defvar org-export-odt-default-image-sizes-alist
  1378. '(("character" . (5 . 0.4))
  1379. ("paragraph" . (5 . 5)))
  1380. "Hardcoded image dimensions one for each of the anchor
  1381. methods.")
  1382. (defun org-odt-do-image-size (probe-method file &optional dpi anchor-type)
  1383. (setq dpi (or dpi org-export-odt-pixels-per-inch))
  1384. (setq anchor-type (or anchor-type "paragraph"))
  1385. (flet ((size-in-cms (size-in-pixels)
  1386. (flet ((pixels-to-cms (pixels)
  1387. (let* ((cms-per-inch 2.54)
  1388. (inches (/ pixels dpi)))
  1389. (* cms-per-inch inches))))
  1390. (and size-in-pixels
  1391. (cons (pixels-to-cms (car size-in-pixels))
  1392. (pixels-to-cms (cdr size-in-pixels)))))))
  1393. (case probe-method
  1394. (emacs
  1395. (size-in-cms (ignore-errors (image-size (create-image file) 'pixels))))
  1396. (imagemagick
  1397. (size-in-cms
  1398. (let ((dim (shell-command-to-string
  1399. (format "identify -format \"%%w:%%h\" \"%s\"" file))))
  1400. (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
  1401. (cons (string-to-number (match-string 1 dim))
  1402. (string-to-number (match-string 2 dim)))))))
  1403. (t
  1404. (cdr (assoc-string anchor-type
  1405. org-export-odt-default-image-sizes-alist))))))
  1406. (defun org-odt-image-size-from-file (file &optional user-width
  1407. user-height scale dpi embed-as)
  1408. (unless (file-name-absolute-p file)
  1409. (setq file (expand-file-name
  1410. file (file-name-directory org-current-export-file))))
  1411. (let* (size width height)
  1412. (unless (and user-height user-width)
  1413. (loop for probe-method in org-export-odt-image-size-probe-method
  1414. until size
  1415. do (setq size (org-odt-do-image-size
  1416. probe-method file dpi embed-as)))
  1417. (or size (error "Cannot determine Image size. Aborting ..."))
  1418. (setq width (car size) height (cdr size)))
  1419. (cond
  1420. (scale
  1421. (setq width (* width scale) height (* height scale)))
  1422. ((and user-height user-width)
  1423. (setq width user-width height user-height))
  1424. (user-height
  1425. (setq width (* user-height (/ width height)) height user-height))
  1426. (user-width
  1427. (setq height (* user-width (/ height width)) width user-width))
  1428. (t (ignore)))
  1429. (cons width height)))
  1430. (defvar org-odt-entity-labels-alist nil
  1431. "Associate Labels with the Labelled entities.
  1432. Each element of the alist is of the form (LABEL-NAME
  1433. CATEGORY-NAME SEQNO). LABEL-NAME is same as that specified by
  1434. \"#+LABEL: ...\" line. CATEGORY-NAME is the type of the entity
  1435. that LABEL-NAME is attached to. CATEGORY-NAME can be one of
  1436. \"Table\", \"Figure\" or \"Equation\". SEQNO is the unique
  1437. number assigned to the referenced entity on a per-CATEGORY basis.
  1438. It is generated sequentially and is 1-based.
  1439. Update this alist with `org-odt-add-label-definition' and
  1440. retrieve an entry with `org-odt-get-label-definition'.")
  1441. (defvar org-odt-entity-counts-plist nil
  1442. "Plist of running counters of SEQNOs for each of the CATEGORY-NAMEs.
  1443. See `org-odt-entity-labels-alist' for known CATEGORY-NAMEs.")
  1444. (defvar org-odt-label-def-ref-spec
  1445. '(("Equation" "(%n)" "text" "(%n)")
  1446. ("" "%e %n%c" "category-and-value" "%e %n"))
  1447. "Specify how labels are applied and referenced.
  1448. This is an alist where each element is of the form (CATEGORY-NAME
  1449. LABEL-APPLY-FMT LABEL-REF-MODE LABEL-REF-FMT). CATEGORY-NAME is
  1450. as defined in `org-odt-entity-labels-alist'. It can additionally
  1451. be an empty string in which case it is used as a catch-all
  1452. specifier.
  1453. LABEL-APPLY-FMT is used for applying labels and captions. It may
  1454. contain following specifiers - %e, %n and %c. %e is replaced
  1455. with the CATEGORY-NAME. %n is replaced with \"<text:sequence
  1456. ...> SEQNO </text:sequence>\". %c is replaced with CAPTION. See
  1457. `org-odt-format-label-definition'.
  1458. LABEL-REF-MODE and LABEL-REF-FMT are used for generating the
  1459. following label reference - \"<text:sequence-ref
  1460. text:reference-format=\"LABEL-REF-MODE\" ...> LABEL-REF-FMT
  1461. </text:sequence-ref>\". LABEL-REF-FMT may contain following
  1462. specifiers - %e and %n. %e is replaced with the CATEGORY-NAME. %n is
  1463. replaced with SEQNO. See `org-odt-format-label-reference'.")
  1464. (defun org-odt-add-label-definition (label category)
  1465. "Return (SEQNO . LABEL-APPLY-FMT).
  1466. See `org-odt-label-def-ref-spec'."
  1467. (setq label (substring-no-properties label))
  1468. (let (seqno label-props fmt (category-sym (intern category)))
  1469. (setq seqno (1+ (plist-get org-odt-entity-counts-plist category-sym))
  1470. org-odt-entity-counts-plist (plist-put org-odt-entity-counts-plist
  1471. category-sym seqno)
  1472. fmt (cadr (or (assoc-string category org-odt-label-def-ref-spec t)
  1473. (assoc-string "" org-odt-label-def-ref-spec t)))
  1474. label-props (list label category seqno))
  1475. (push label-props org-odt-entity-labels-alist)
  1476. (cons seqno fmt)))
  1477. (defun org-odt-get-label-definition (label)
  1478. "Return (LABEL-NAME CATEGORY-NAME SEQNO LABEL-REF-MODE LABEL-REF-FMT).
  1479. See `org-odt-entity-labels-alist' and
  1480. `org-odt-label-def-ref-spec'."
  1481. (let* ((label-props (assoc label org-odt-entity-labels-alist))
  1482. (category (nth 1 label-props)))
  1483. (unless label-props
  1484. (org-lparse-warn
  1485. (format "Unable to resolve reference to label \"%s\"" label)))
  1486. (when label-props
  1487. (append label-props
  1488. (cddr (or (assoc-string category org-odt-label-def-ref-spec t)
  1489. (assoc-string "" org-odt-label-def-ref-spec t)))))))
  1490. (defun org-odt-format-label-definition (label category caption)
  1491. (assert label)
  1492. (let* ((label-props (org-odt-add-label-definition label category))
  1493. (seqno (car label-props))
  1494. (fmt (cdr label-props)))
  1495. (or (format-spec
  1496. fmt
  1497. `((?e . ,category)
  1498. (?n . ,(org-odt-format-tags
  1499. '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" . "</text:sequence>")
  1500. (format "%d" seqno) label category category))
  1501. (?c . ,(or (and caption (concat ": " caption)) ""))))
  1502. caption "")))
  1503. (defun org-odt-format-label-reference (label category seqno fmt1 fmt2)
  1504. (assert label)
  1505. (save-match-data
  1506. (org-odt-format-tags
  1507. '("<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">"
  1508. . "</text:sequence-ref>")
  1509. (format-spec fmt2 `((?e . ,category)
  1510. (?n . ,(format "%d" seqno)))) fmt1 label)))
  1511. (defun org-odt-fixup-label-references ()
  1512. (goto-char (point-min))
  1513. (while (re-search-forward
  1514. "<text:sequence-ref text:ref-name=\"\\([^\"]+\\)\"/>" nil t)
  1515. (let* ((label (match-string 1))
  1516. (label-def (org-odt-get-label-definition label))
  1517. (rpl (and label-def
  1518. (apply 'org-odt-format-label-reference label-def))))
  1519. (when rpl (replace-match rpl t t)))))
  1520. (defun org-odt-format-entity-caption (label caption category)
  1521. (or (and label (org-odt-format-label-definition label category caption))
  1522. caption ""))
  1523. (defun org-odt-format-tags (tag text &rest args)
  1524. (let ((prefix (when org-lparse-encode-pending "@"))
  1525. (suffix (when org-lparse-encode-pending "@")))
  1526. (apply 'org-lparse-format-tags tag text prefix suffix args)))
  1527. (defun org-odt-init-outfile (filename)
  1528. (unless (executable-find "zip")
  1529. ;; Not at all OSes ship with zip by default
  1530. (error "Executable \"zip\" needed for creating OpenDocument files"))
  1531. (let* ((outdir (make-temp-file org-export-odt-tmpdir-prefix t))
  1532. (content-file (expand-file-name "content.xml" outdir)))
  1533. ;; init conten.xml
  1534. (with-current-buffer (find-file-noselect content-file t))
  1535. ;; reset variables
  1536. (setq org-odt-manifest-file-entries nil
  1537. org-odt-embedded-images-count 0
  1538. org-odt-embedded-formulas-count 0
  1539. org-odt-entity-labels-alist nil
  1540. org-odt-entity-counts-plist (list 'Table 0 'Equation 0 'Figure 0))
  1541. content-file))
  1542. (defcustom org-export-odt-prettify-xml nil
  1543. "Specify whether or not the xml output should be prettified.
  1544. When this option is turned on, `indent-region' is run on all
  1545. component xml buffers before they are saved. Turn this off for
  1546. regular use. Turn this on if you need to examine the xml
  1547. visually."
  1548. :group 'org-export-odt
  1549. :type 'boolean)
  1550. (defvar hfy-user-sheet-assoc) ; bound during org-do-lparse
  1551. (defun org-odt-save-as-outfile (target opt-plist)
  1552. ;; create mimetype file
  1553. (write-region "application/vnd.oasis.opendocument.text" nil
  1554. (expand-file-name "mimetype"))
  1555. ;; write meta file
  1556. (org-odt-update-meta-file opt-plist)
  1557. ;; write styles file
  1558. (let ((styles-file (plist-get opt-plist :odt-styles-file)))
  1559. (org-odt-copy-styles-file (and styles-file
  1560. (read (org-trim styles-file)))))
  1561. ;; Update styles.xml - take care of outline numbering
  1562. (with-current-buffer
  1563. (find-file-noselect (expand-file-name "styles.xml") t)
  1564. ;; Don't make automatic backup of styles.xml file. This setting
  1565. ;; prevents the backedup styles.xml file from being zipped in to
  1566. ;; odt file. This is more of a hackish fix. Better alternative
  1567. ;; would be to fix the zip command so that the output odt file
  1568. ;; includes only the needed files and excludes any auto-generated
  1569. ;; extra files like backups and auto-saves etc etc. Note that
  1570. ;; currently the zip command zips up the entire temp directory so
  1571. ;; that any auto-generated files created under the hood ends up in
  1572. ;; the resulting odt file.
  1573. (set (make-local-variable 'backup-inhibited) t)
  1574. ;; Import local setting of `org-export-with-section-numbers'
  1575. (org-lparse-bind-local-variables opt-plist)
  1576. (org-odt-configure-outline-numbering
  1577. (if org-export-with-section-numbers org-export-headline-levels 0)))
  1578. ;; Write custom stlyes for source blocks
  1579. (org-odt-insert-custom-styles-for-srcblocks
  1580. (mapconcat
  1581. (lambda (style)
  1582. (format " %s\n" (cddr style)))
  1583. hfy-user-sheet-assoc ""))
  1584. ;; create a manifest entry for content.xml
  1585. (org-odt-create-manifest-file-entry
  1586. "application/vnd.oasis.opendocument.text" "/" "1.2")
  1587. (org-odt-create-manifest-file-entry "text/xml" "content.xml")
  1588. ;; write out the manifest entries before zipping
  1589. (org-odt-write-manifest-file)
  1590. (let ((xml-files '("mimetype" "META-INF/manifest.xml" "content.xml"
  1591. "meta.xml" "styles.xml"))
  1592. (zipdir default-directory))
  1593. (message "Switching to directory %s" (expand-file-name zipdir))
  1594. ;; save all xml files
  1595. (mapc (lambda (file)
  1596. (with-current-buffer
  1597. (find-file-noselect (expand-file-name file) t)
  1598. ;; prettify output if needed
  1599. (when org-export-odt-prettify-xml
  1600. (indent-region (point-min) (point-max)))
  1601. (save-buffer 0)))
  1602. xml-files)
  1603. (let* ((target-name (file-name-nondirectory target))
  1604. (target-dir (file-name-directory target))
  1605. (cmds `(("zip" "-mX0" ,target-name "mimetype")
  1606. ("zip" "-rmTq" ,target-name "."))))
  1607. (when (file-exists-p target)
  1608. ;; FIXME: If the file is locked this throws a cryptic error
  1609. (delete-file target))
  1610. (let ((coding-system-for-write 'no-conversion) exitcode err-string)
  1611. (message "Creating odt file...")
  1612. (mapc
  1613. (lambda (cmd)
  1614. (message "Running %s" (mapconcat 'identity cmd " "))
  1615. (setq err-string
  1616. (with-output-to-string
  1617. (setq exitcode
  1618. (apply 'call-process (car cmd)
  1619. nil standard-output nil (cdr cmd)))))
  1620. (or (zerop exitcode)
  1621. (ignore (message "%s" err-string))
  1622. (error "Unable to create odt file (%S)" exitcode)))
  1623. cmds))
  1624. ;; move the file from outdir to target-dir
  1625. (rename-file target-name target-dir)
  1626. ;; kill all xml buffers
  1627. (mapc (lambda (file)
  1628. (kill-buffer
  1629. (find-file-noselect (expand-file-name file zipdir) t)))
  1630. xml-files)
  1631. (delete-directory zipdir)))
  1632. (message "Created %s" target)
  1633. (set-buffer (find-file-noselect target t)))
  1634. (defun org-odt-format-date (date)
  1635. (let ((warning-msg
  1636. "OpenDocument files require that dates be in ISO-8601 format. Please review your DATE options for compatibility."))
  1637. ;; If the user is not careful with the date specification, an
  1638. ;; invalid meta.xml will be emitted.
  1639. ;; For now honor user's diktat and let him off with a warning
  1640. ;; message. This is OK as LibreOffice (and possibly other
  1641. ;; apps) doesn't deem this deviation as critical and continue
  1642. ;; to load the file.
  1643. ;; FIXME: Surely there a better way to handle this. Revisit this
  1644. ;; later.
  1645. (cond
  1646. ((and date (string-match "%" date))
  1647. ;; Honor user's diktat. See comments above
  1648. (org-lparse-warn warning-msg)
  1649. (format-time-string date))
  1650. (date
  1651. ;; Honor user's diktat. See comments above
  1652. (org-lparse-warn warning-msg)
  1653. date)
  1654. (t
  1655. ;; ISO 8601 format
  1656. (let ((stamp (format-time-string "%Y-%m-%dT%H:%M:%S%z")))
  1657. (format "%s:%s" (substring stamp 0 -2) (substring stamp -2)))))))
  1658. (defconst org-odt-manifest-file-entry-tag
  1659. "
  1660. <manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
  1661. (defvar org-odt-manifest-file-entries nil)
  1662. (defun org-odt-create-manifest-file-entry (&rest args)
  1663. (push args org-odt-manifest-file-entries))
  1664. (defun org-odt-write-manifest-file ()
  1665. (make-directory "META-INF")
  1666. (let ((manifest-file (expand-file-name "META-INF/manifest.xml")))
  1667. (write-region
  1668. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  1669. <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n"
  1670. nil manifest-file)
  1671. (mapc
  1672. (lambda (file-entry)
  1673. (let* ((version (nth 2 file-entry))
  1674. (extra (if version
  1675. (format " manifest:version=\"%s\"" version)
  1676. "")))
  1677. (write-region
  1678. (format org-odt-manifest-file-entry-tag
  1679. (nth 0 file-entry) (nth 1 file-entry) extra)
  1680. nil manifest-file t))) org-odt-manifest-file-entries)
  1681. (write-region "\n</manifest:manifest>" nil manifest-file t)))
  1682. (defun org-odt-update-meta-file (opt-plist)
  1683. (let ((date (org-odt-format-date (plist-get opt-plist :date)))
  1684. (author (or (plist-get opt-plist :author) ""))
  1685. (email (plist-get opt-plist :email))
  1686. (keywords (plist-get opt-plist :keywords))
  1687. (description (plist-get opt-plist :description))
  1688. (title (plist-get opt-plist :title)))
  1689. (write-region
  1690. (concat
  1691. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  1692. <office:document-meta
  1693. xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
  1694. xmlns:xlink=\"http://www.w3.org/1999/xlink\"
  1695. xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
  1696. xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
  1697. xmlns:ooo=\"http://openoffice.org/2004/office\"
  1698. office:version=\"1.2\">
  1699. <office:meta>" "\n"
  1700. (org-odt-format-tags '("<dc:creator>" . "</dc:creator>") author)
  1701. (org-odt-format-tags
  1702. '("\n<meta:initial-creator>" . "</meta:initial-creator>") author)
  1703. (org-odt-format-tags '("\n<dc:date>" . "</dc:date>") date)
  1704. (org-odt-format-tags
  1705. '("\n<meta:creation-date>" . "</meta:creation-date>") date)
  1706. (org-odt-format-tags '("\n<meta:generator>" . "</meta:generator>")
  1707. (when org-export-creator-info
  1708. (format "Org-%s/Emacs-%s"
  1709. org-version emacs-version)))
  1710. (org-odt-format-tags '("\n<meta:keyword>" . "</meta:keyword>") keywords)
  1711. (org-odt-format-tags '("\n<dc:subject>" . "</dc:subject>") description)
  1712. (org-odt-format-tags '("\n<dc:title>" . "</dc:title>") title)
  1713. "\n"
  1714. " </office:meta>" "</office:document-meta>")
  1715. nil (expand-file-name "meta.xml")))
  1716. ;; create a manifest entry for meta.xml
  1717. (org-odt-create-manifest-file-entry "text/xml" "meta.xml"))
  1718. (defun org-odt-finalize-outfile ()
  1719. (org-odt-delete-empty-paragraphs))
  1720. (defun org-odt-delete-empty-paragraphs ()
  1721. (goto-char (point-min))
  1722. (let ((open "<text:p[^>]*>")
  1723. (close "</text:p>"))
  1724. (while (re-search-forward (format "%s[ \r\n\t]*%s" open close) nil t)
  1725. (replace-match ""))))
  1726. (defcustom org-export-odt-convert-processes
  1727. '(("BasicODConverter"
  1728. ("soffice" "-norestore" "-invisible" "-headless"
  1729. "\"macro:///BasicODConverter.Main.Convert(%I,%f,%O)\""))
  1730. ("unoconv"
  1731. ("unoconv" "-f" "%f" "-o" "%d" "%i")))
  1732. "Specify a list of document converters and their usage.
  1733. The converters in this list are offered as choices while
  1734. customizing `org-export-odt-convert-process'.
  1735. This variable is an alist where each element is of the
  1736. form (CONVERTER-NAME CONVERTER-PROCESS). CONVERTER-NAME is name
  1737. of the converter. CONVERTER-PROCESS specifies the command-line
  1738. syntax of the converter and is of the form (CONVERTER-PROGRAM
  1739. ARG1 ARG2 ...). CONVERTER-PROGRAM is the name of the executable.
  1740. ARG1, ARG2 etc are command line options that are passed to
  1741. CONVERTER-PROGRAM. Format specifiers can be used in the ARGs and
  1742. they are interpreted as below:
  1743. %i input file name in full
  1744. %I input file name as a URL
  1745. %f format of the output file
  1746. %o output file name in full
  1747. %O output file name as a URL
  1748. %d output dir in full
  1749. %D output dir as a URL."
  1750. :group 'org-export-odt
  1751. :type
  1752. '(choice
  1753. (const :tag "None" nil)
  1754. (alist :tag "Converters"
  1755. :key-type (string :tag "Converter Name")
  1756. :value-type (group (cons (string :tag "Executable")
  1757. (repeat (string :tag "Command line args")))))))
  1758. (defcustom org-export-odt-convert-process nil
  1759. "Use this converter to convert from \"odt\" format to other formats.
  1760. During customization, the list of converter names are populated
  1761. from `org-export-odt-convert-processes'."
  1762. :group 'org-export-odt
  1763. :type '(choice :convert-widget
  1764. (lambda (w)
  1765. (apply 'widget-convert (widget-type w)
  1766. (eval (car (widget-get w :args)))))
  1767. `((const :tag "None" nil)
  1768. ,@(mapcar (lambda (c)
  1769. `(const :tag ,(car c) ,(car c)))
  1770. org-export-odt-convert-processes))))
  1771. (defcustom org-export-odt-convert-capabilities
  1772. '(("Text"
  1773. ("odt" "ott" "doc" "rtf")
  1774. (("pdf" "pdf") ("odt" "odt") ("xhtml" "html") ("rtf" "rtf")
  1775. ("ott" "ott") ("doc" "doc") ("ooxml" "xml") ("html" "html")))
  1776. ("Web"
  1777. ("html" "xhtml") (("pdf" "pdf") ("odt" "txt") ("html" "html")))
  1778. ("Spreadsheet"
  1779. ("ods" "ots" "xls" "csv")
  1780. (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv")
  1781. ("ods" "ods") ("xls" "xls") ("xhtml" "xhtml") ("ooxml" "xml")))
  1782. ("Presentation"
  1783. ("odp" "otp" "ppt")
  1784. (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("xhtml" "xml")
  1785. ("otp" "otp") ("ppt" "ppt") ("odg" "odg") ("html" "html"))))
  1786. "Specify input and output formats of `org-export-odt-convert-process'.
  1787. More correctly, specify the set of input and output formats that
  1788. the user is actually interested in.
  1789. This variable is an alist where each element is of the
  1790. form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
  1791. INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
  1792. alist where each element is of the form (OUTPUT-FMT
  1793. OUTPUT-FILE-EXTENSION).
  1794. The variable is interpreted as follows:
  1795. `org-export-odt-convert-process' can take any document that is in
  1796. INPUT-FMT-LIST and produce any document that is in the
  1797. OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
  1798. OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
  1799. serves dual purposes:
  1800. - It is used for populating completion candidates during
  1801. `org-export-odt-convert' commands.
  1802. - It is used as the value of \"%f\" specifier in
  1803. `org-export-odt-convert-process'.
  1804. DOCUMENT-CLASS is used to group a set of file formats in
  1805. INPUT-FMT-LIST in to a single class.
  1806. Note that this variable inherently captures how LibreOffice based
  1807. converters work. LibreOffice maps documents of various formats
  1808. to classes like Text, Web, Spreadsheet, Presentation etc and
  1809. allow document of a given class (irrespective of it's source
  1810. format) to be converted to any of the export formats associated
  1811. with that class.
  1812. See default setting of this variable for an typical
  1813. configuration."
  1814. :group 'org-export-odt
  1815. :type
  1816. '(choice
  1817. (const :tag "None" nil)
  1818. (alist :key-type (string :tag "Document Class")
  1819. :value-type
  1820. (group (repeat :tag "Input formats" (string :tag "Input format"))
  1821. (alist :tag "Output formats"
  1822. :key-type (string :tag "Output format")
  1823. :value-type
  1824. (group (string :tag "Output file extension")))))))
  1825. (defun org-export-odt-convert (&optional in-file out-fmt prefix-arg)
  1826. "Convert IN-FILE to format OUT-FMT using a command line converter.
  1827. IN-FILE is the file to be converted. If unspecified, it defaults
  1828. to variable `buffer-file-name'. OUT-FMT is the desired output
  1829. format. Use `org-export-odt-convert-process' as the converter.
  1830. If PREFIX-ARG is non-nil then the newly converted file is opened
  1831. using `org-open-file'."
  1832. (interactive
  1833. (append (org-lparse-convert-read-params) current-prefix-arg))
  1834. (org-lparse-do-convert in-file out-fmt prefix-arg))
  1835. (defun org-odt-get (what &optional opt-plist)
  1836. (case what
  1837. (BACKEND 'odt)
  1838. (EXPORT-DIR (org-export-directory :html opt-plist))
  1839. (FILE-NAME-EXTENSION "odt")
  1840. (EXPORT-BUFFER-NAME "*Org ODT Export*")
  1841. (ENTITY-CONTROL org-odt-entity-control-callbacks-alist)
  1842. (ENTITY-FORMAT org-odt-entity-format-callbacks-alist)
  1843. (INIT-METHOD 'org-odt-init-outfile)
  1844. (FINAL-METHOD 'org-odt-finalize-outfile)
  1845. (SAVE-METHOD 'org-odt-save-as-outfile)
  1846. (CONVERT-METHOD
  1847. (and org-export-odt-convert-process
  1848. (cadr (assoc-string org-export-odt-convert-process
  1849. org-export-odt-convert-processes t))))
  1850. (CONVERT-CAPABILITIES
  1851. (and org-export-odt-convert-process
  1852. (cadr (assoc-string org-export-odt-convert-process
  1853. org-export-odt-convert-processes t))
  1854. org-export-odt-convert-capabilities))
  1855. (TOPLEVEL-HLEVEL 1)
  1856. (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps)
  1857. (INLINE-IMAGES 'maybe)
  1858. (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
  1859. (PLAIN-TEXT-MAP '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
  1860. (TABLE-FIRST-COLUMN-AS-LABELS nil)
  1861. (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY "," 'superscript))
  1862. (CODING-SYSTEM-FOR-WRITE 'utf-8)
  1863. (CODING-SYSTEM-FOR-SAVE 'utf-8)
  1864. (t (error "Unknown property: %s" what))))
  1865. (defvar org-lparse-latex-fragment-fallback) ; set by org-do-lparse
  1866. (defvar org-lparse-opt-plist) ; bound during org-do-lparse
  1867. (defun org-export-odt-do-preprocess-latex-fragments ()
  1868. "Convert LaTeX fragments to images."
  1869. (let* ((latex-frag-opt (plist-get org-lparse-opt-plist :LaTeX-fragments))
  1870. (latex-frag-opt ; massage the options
  1871. (or (and (member latex-frag-opt '(mathjax t))
  1872. (not (and (fboundp 'org-format-latex-mathml-available-p)
  1873. (org-format-latex-mathml-available-p)))
  1874. (prog1 org-lparse-latex-fragment-fallback
  1875. (org-lparse-warn
  1876. (concat
  1877. "LaTeX to MathML converter not available. "
  1878. (format "Using %S instead."
  1879. org-lparse-latex-fragment-fallback)))))
  1880. latex-frag-opt))
  1881. cache-dir display-msg)
  1882. (cond
  1883. ((eq latex-frag-opt 'dvipng)
  1884. (setq cache-dir "ltxpng/")
  1885. (setq display-msg "Creating LaTeX image %s"))
  1886. ((member latex-frag-opt '(mathjax t))
  1887. (setq latex-frag-opt 'mathml)
  1888. (setq cache-dir "ltxmathml/")
  1889. (setq display-msg "Creating MathML formula %s")))
  1890. (when (and org-current-export-file)
  1891. (org-format-latex
  1892. (concat cache-dir (file-name-sans-extension
  1893. (file-name-nondirectory org-current-export-file)))
  1894. org-current-export-dir nil display-msg
  1895. nil nil latex-frag-opt))))
  1896. (defun org-export-odt-preprocess-latex-fragments ()
  1897. (when (equal org-export-current-backend 'odt)
  1898. (org-export-odt-do-preprocess-latex-fragments)))
  1899. (defun org-export-odt-preprocess-label-references ()
  1900. (goto-char (point-min))
  1901. (let (label label-components category value pretty-label)
  1902. (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t)
  1903. (org-if-unprotected-at (match-beginning 1)
  1904. (replace-match
  1905. (let ((org-lparse-encode-pending t)
  1906. (label (match-string 1)))
  1907. ;; markup generated below is mostly an eye-candy. At
  1908. ;; pre-processing stage, there is no information on which
  1909. ;; entity a label reference points to. The actual markup
  1910. ;; is generated as part of `org-odt-fixup-label-references'
  1911. ;; which gets called at the fag end of export. By this
  1912. ;; time we would have seen and collected all the label
  1913. ;; definitions in `org-odt-entity-labels-alist'.
  1914. (org-odt-format-tags
  1915. "<text:sequence-ref text:ref-name=\"%s\"/>" "" label)) t t)))))
  1916. ;; process latex fragments as part of
  1917. ;; `org-export-preprocess-after-blockquote-hook'. Note that this hook
  1918. ;; is the one that is closest and well before the call to
  1919. ;; `org-export-attach-captions-and-attributes' in
  1920. ;; `org-export-preprocess-stirng'. The above arrangement permits
  1921. ;; captions, labels and attributes to be attached to png images
  1922. ;; generated out of latex equations.
  1923. (add-hook 'org-export-preprocess-after-blockquote-hook
  1924. 'org-export-odt-preprocess-latex-fragments)
  1925. (defun org-export-odt-preprocess (parameters)
  1926. (org-export-odt-preprocess-label-references))
  1927. (declare-function archive-zip-extract "arc-mode.el" (archive name))
  1928. (defun org-odt-zip-extract-one (archive member &optional target)
  1929. (require 'arc-mode)
  1930. (let* ((target (or target default-directory))
  1931. (archive (expand-file-name archive))
  1932. (archive-zip-extract
  1933. (list "unzip" "-qq" "-o" "-d" target))
  1934. exit-code command-output)
  1935. (setq command-output
  1936. (with-temp-buffer
  1937. (setq exit-code (archive-zip-extract archive member))
  1938. (buffer-string)))
  1939. (unless (zerop exit-code)
  1940. (message command-output)
  1941. (error "Extraction failed"))))
  1942. (defun org-odt-zip-extract (archive members &optional target)
  1943. (when (atom members) (setq members (list members)))
  1944. (mapc (lambda (member)
  1945. (org-odt-zip-extract-one archive member target))
  1946. members))
  1947. (defun org-odt-copy-styles-file (&optional styles-file)
  1948. ;; Non-availability of styles.xml is not a critical error. For now
  1949. ;; throw an error purely for aesthetic reasons.
  1950. (setq styles-file (or styles-file
  1951. org-export-odt-styles-file
  1952. (expand-file-name "styles/OrgOdtStyles.xml"
  1953. org-odt-data-dir)
  1954. (error "org-odt: Missing styles file?")))
  1955. (cond
  1956. ((listp styles-file)
  1957. (let ((archive (nth 0 styles-file))
  1958. (members (nth 1 styles-file)))
  1959. (org-odt-zip-extract archive members)
  1960. (mapc
  1961. (lambda (member)
  1962. (when (org-file-image-p member)
  1963. (let* ((image-type (file-name-extension member))
  1964. (media-type (format "image/%s" image-type)))
  1965. (org-odt-create-manifest-file-entry media-type member))))
  1966. members)))
  1967. ((and (stringp styles-file) (file-exists-p styles-file))
  1968. (let ((styles-file-type (file-name-extension styles-file)))
  1969. (cond
  1970. ((string= styles-file-type "xml")
  1971. (copy-file styles-file "styles.xml" t))
  1972. ((member styles-file-type '("odt" "ott"))
  1973. (org-odt-zip-extract styles-file "styles.xml")))))
  1974. (t
  1975. (error (format "Invalid specification of styles.xml file: %S"
  1976. org-export-odt-styles-file))))
  1977. ;; create a manifest entry for styles.xml
  1978. (org-odt-create-manifest-file-entry "text/xml" "styles.xml"))
  1979. (defvar org-export-odt-factory-settings
  1980. "d4328fb9d1b6cb211d4320ff546829f26700dc5e"
  1981. "SHA1 hash of OrgOdtStyles.xml.")
  1982. (defun org-odt-configure-outline-numbering (level)
  1983. "Outline numbering is retained only upto LEVEL.
  1984. To disable outline numbering pass a LEVEL of 0."
  1985. (goto-char (point-min))
  1986. (let ((regex
  1987. "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
  1988. (replacement
  1989. "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
  1990. (while (re-search-forward regex nil t)
  1991. (when (> (string-to-number (match-string 2)) level)
  1992. (replace-match replacement t nil))))
  1993. (save-buffer 0))
  1994. (provide 'org-odt)
  1995. ;;; org-odt.el ends here