org-odt.el 91 KB

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