org-odt.el 103 KB

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