org-odt.el 91 KB

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