org-e-odt.el 132 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705
  1. ;;; org-e-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 not 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 'table))
  22. (require 'format-spec)
  23. (require 'org-export)
  24. ;;; Define Back-End
  25. (org-export-define-backend e-odt
  26. ((bold . org-e-odt-bold)
  27. (center-block . org-e-odt-center-block)
  28. (clock . org-e-odt-clock)
  29. (code . org-e-odt-code)
  30. (drawer . org-e-odt-drawer)
  31. (dynamic-block . org-e-odt-dynamic-block)
  32. (entity . org-e-odt-entity)
  33. (example-block . org-e-odt-example-block)
  34. (export-block . org-e-odt-export-block)
  35. (export-snippet . org-e-odt-export-snippet)
  36. (fixed-width . org-e-odt-fixed-width)
  37. (footnote-definition . org-e-odt-footnote-definition)
  38. (footnote-reference . org-e-odt-footnote-reference)
  39. (headline . org-e-odt-headline)
  40. (horizontal-rule . org-e-odt-horizontal-rule)
  41. (inline-src-block . org-e-odt-inline-src-block)
  42. (inlinetask . org-e-odt-inlinetask)
  43. (italic . org-e-odt-italic)
  44. (item . org-e-odt-item)
  45. (keyword . org-e-odt-keyword)
  46. (latex-environment . org-e-odt-latex-environment)
  47. (latex-fragment . org-e-odt-latex-fragment)
  48. (line-break . org-e-odt-line-break)
  49. (link . org-e-odt-link)
  50. (macro . org-e-odt-macro)
  51. (paragraph . org-e-odt-paragraph)
  52. (plain-list . org-e-odt-plain-list)
  53. (plain-text . org-e-odt-plain-text)
  54. (planning . org-e-odt-planning)
  55. (property-drawer . org-e-odt-property-drawer)
  56. (quote-block . org-e-odt-quote-block)
  57. (quote-section . org-e-odt-quote-section)
  58. (radio-target . org-e-odt-radio-target)
  59. (section . org-e-odt-section)
  60. (special-block . org-e-odt-special-block)
  61. (src-block . org-e-odt-src-block)
  62. (statistics-cookie . org-e-odt-statistics-cookie)
  63. (strike-through . org-e-odt-strike-through)
  64. (subscript . org-e-odt-subscript)
  65. (superscript . org-e-odt-superscript)
  66. (table . org-e-odt-table)
  67. (table-cell . org-e-odt-table-cell)
  68. (table-row . org-e-odt-table-row)
  69. (target . org-e-odt-target)
  70. (template . org-e-odt-template)
  71. (timestamp . org-e-odt-timestamp)
  72. (underline . org-e-odt-underline)
  73. (verbatim . org-e-odt-verbatim)
  74. (verse-block . org-e-odt-verse-block))
  75. :export-block "ODT"
  76. :options-alist
  77. ((:odt-styles-file "ODT_STYLES_FILE" nil nil t)
  78. (:LaTeX-fragments nil "LaTeX" org-export-with-LaTeX-fragments)))
  79. ;;; Dependencies
  80. ;;; Hooks
  81. ;;; Function Declarations
  82. (declare-function org-id-find-id-file "org-id" (id))
  83. (declare-function hfy-face-to-style "htmlfontify" (fn))
  84. (declare-function hfy-face-or-def-to-name "htmlfontify" (fn))
  85. (declare-function archive-zip-extract "arc-mode.el" (archive name))
  86. (declare-function org-create-math-formula "org" (latex-frag &optional mathml-file))
  87. (declare-function browse-url-file-url "browse-url" (file))
  88. ;;; Internal Variables
  89. (defconst org-e-odt-special-string-regexps
  90. '(("\\\\-" . "&#x00ad;\\1") ; shy
  91. ("---\\([^-]\\)" . "&#x2014;\\1") ; mdash
  92. ("--\\([^-]\\)" . "&#x2013;\\1") ; ndash
  93. ("\\.\\.\\." . "&#x2026;")) ; hellip
  94. "Regular expressions for special string conversion.")
  95. (defconst org-e-odt-styles-dir
  96. (let* ((d (expand-file-name (org-find-library-dir "org-e-odt.el")))
  97. (styles-dir-list (list
  98. (concat d "../../etc/styles/") ;; Git
  99. (concat d "../../etc/org/") ;; system
  100. (concat d "../etc/org/"))) ;; ELPA org-plus
  101. styles-dir)
  102. (setq styles-dir
  103. (car
  104. (delq nil
  105. (mapcar (lambda (sd)
  106. (when (and (file-readable-p
  107. (expand-file-name
  108. "OrgOdtContentTemplate.xml" sd))
  109. (file-readable-p
  110. (expand-file-name
  111. "OrgOdtStyles.xml" sd)))
  112. sd))
  113. styles-dir-list))))
  114. (or styles-dir
  115. (error "Cannot find factory styles files for Org ODT, aborting")))
  116. "Directory that holds auxiliary XML files used by the ODT exporter.
  117. This directory contains the following XML files -
  118. \"OrgOdtStyles.xml\" and \"OrgOdtContentTemplate.xml\".
  119. These XML files are used as the default values of
  120. `org-export-odt-styles-file' and
  121. `org-export-odt-content-template-file'.")
  122. (defconst org-e-odt-bookmark-prefix "OrgXref.")
  123. (defconst org-e-odt-manifest-file-entry-tag
  124. "\n<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
  125. (defconst org-e-odt-file-extensions
  126. '(("odt" . "OpenDocument Text")
  127. ("ott" . "OpenDocument Text Template")
  128. ("odm" . "OpenDocument Master Document")
  129. ("ods" . "OpenDocument Spreadsheet")
  130. ("ots" . "OpenDocument Spreadsheet Template")
  131. ("odg" . "OpenDocument Drawing (Graphics)")
  132. ("otg" . "OpenDocument Drawing Template")
  133. ("odp" . "OpenDocument Presentation")
  134. ("otp" . "OpenDocument Presentation Template")
  135. ("odi" . "OpenDocument Image")
  136. ("odf" . "OpenDocument Formula")
  137. ("odc" . "OpenDocument Chart")))
  138. (defvar org-e-odt-table-style-format
  139. "
  140. <style:style style:name=\"%s\" style:family=\"table\">
  141. <style:table-properties style:rel-width=\"%d%%\" fo:margin-top=\"0cm\" fo:margin-bottom=\"0.20cm\" table:align=\"center\"/>
  142. </style:style>
  143. "
  144. "Template for auto-generated Table styles.")
  145. (defvar org-e-odt-automatic-styles '()
  146. "Registry of automatic styles for various OBJECT-TYPEs.
  147. The variable has the following form:
  148. \(\(OBJECT-TYPE-A
  149. \(\(OBJECT-NAME-A.1 OBJECT-PROPS-A.1\)
  150. \(OBJECT-NAME-A.2 OBJECT-PROPS-A.2\) ...\)\)
  151. \(OBJECT-TYPE-B
  152. \(\(OBJECT-NAME-B.1 OBJECT-PROPS-B.1\)
  153. \(OBJECT-NAME-B.2 OBJECT-PROPS-B.2\) ...\)\)
  154. ...\).
  155. OBJECT-TYPEs could be \"Section\", \"Table\", \"Figure\" etc.
  156. OBJECT-PROPS is (typically) a plist created by passing
  157. \"#+ATTR_ODT: \" option to `org-e-odt-parse-block-attributes'.
  158. Use `org-e-odt-add-automatic-style' to add update this variable.'")
  159. (defvar org-e-odt-object-counters nil
  160. "Running counters for various OBJECT-TYPEs.
  161. Use this to generate automatic names and style-names. See
  162. `org-e-odt-add-automatic-style'.")
  163. (defvar org-e-odt-src-block-paragraph-format
  164. "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
  165. <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
  166. <style:background-image/>
  167. </style:paragraph-properties>
  168. <style:text-properties fo:color=\"%s\"/>
  169. </style:style>"
  170. "Custom paragraph style for colorized source and example blocks.
  171. This style is much the same as that of \"OrgFixedWidthBlock\"
  172. except that the foreground and background colors are set
  173. according to the default face identified by the `htmlfontify'.")
  174. (defvar hfy-optimisations)
  175. (defvar org-e-odt-embedded-formulas-count 0)
  176. (defvar org-e-odt-entity-frame-styles
  177. '(("As-CharImage" "__Figure__" ("OrgInlineImage" nil "as-char"))
  178. ("ParagraphImage" "__Figure__" ("OrgDisplayImage" nil "paragraph"))
  179. ("PageImage" "__Figure__" ("OrgPageImage" nil "page"))
  180. ("CaptionedAs-CharImage" "__Figure__"
  181. ("OrgCaptionedImage"
  182. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  183. ("OrgInlineImage" nil "as-char"))
  184. ("CaptionedParagraphImage" "__Figure__"
  185. ("OrgCaptionedImage"
  186. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  187. ("OrgImageCaptionFrame" nil "paragraph"))
  188. ("CaptionedPageImage" "__Figure__"
  189. ("OrgCaptionedImage"
  190. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  191. ("OrgPageImageCaptionFrame" nil "page"))
  192. ("InlineFormula" "__MathFormula__" ("OrgInlineFormula" nil "as-char"))
  193. ("DisplayFormula" "__MathFormula__" ("OrgDisplayFormula" nil "as-char"))
  194. ("CaptionedDisplayFormula" "__MathFormula__"
  195. ("OrgCaptionedFormula" nil "paragraph")
  196. ("OrgFormulaCaptionFrame" nil "as-char"))))
  197. (defvar org-e-odt-embedded-images-count 0)
  198. (defvar org-e-odt-image-size-probe-method
  199. (append (and (executable-find "identify") '(imagemagick)) ; See Bug#10675
  200. '(emacs fixed))
  201. "Ordered list of methods for determining image sizes.")
  202. (defvar org-e-odt-default-image-sizes-alist
  203. '(("as-char" . (5 . 0.4))
  204. ("paragraph" . (5 . 5)))
  205. "Hardcoded image dimensions one for each of the anchor
  206. methods.")
  207. ;; A4 page size is 21.0 by 29.7 cms
  208. ;; The default page settings has 2cm margin on each of the sides. So
  209. ;; the effective text area is 17.0 by 25.7 cm
  210. (defvar org-e-odt-max-image-size '(17.0 . 20.0)
  211. "Limiting dimensions for an embedded image.")
  212. (defvar org-e-odt-label-styles
  213. '(("math-formula" "%c" "text" "(%n)")
  214. ("math-label" "(%n)" "text" "(%n)")
  215. ("category-and-value" "%e %n: %c" "category-and-value" "%e %n")
  216. ("value" "%e %n: %c" "value" "%n"))
  217. "Specify how labels are applied and referenced.
  218. This is an alist where each element is of the
  219. form (LABEL-STYLE-NAME LABEL-ATTACH-FMT LABEL-REF-MODE
  220. LABEL-REF-FMT).
  221. LABEL-ATTACH-FMT controls how labels and captions are attached to
  222. an entity. It may contain following specifiers - %e, %n and %c.
  223. %e is replaced with the CATEGORY-NAME. %n is replaced with
  224. \"<text:sequence ...> SEQNO </text:sequence>\". %c is replaced
  225. with CAPTION. See `org-e-odt-format-label-definition'.
  226. LABEL-REF-MODE and LABEL-REF-FMT controls how label references
  227. are generated. The following XML is generated for a label
  228. reference - \"<text:sequence-ref
  229. text:reference-format=\"LABEL-REF-MODE\" ...> LABEL-REF-FMT
  230. </text:sequence-ref>\". LABEL-REF-FMT may contain following
  231. specifiers - %e and %n. %e is replaced with the CATEGORY-NAME.
  232. %n is replaced with SEQNO. See
  233. `org-e-odt-format-label-reference'.")
  234. (defvar org-e-odt-category-map-alist
  235. '(("__Table__" "Table" "value" "Table")
  236. ("__Figure__" "Illustration" "value" "Figure")
  237. ("__MathFormula__" "Text" "math-formula" "Equation")
  238. ("__DvipngImage__" "Equation" "value" "Equation")
  239. ("__Listing__" "Listing" "value" "Listing")
  240. ;; ("__Table__" "Table" "category-and-value")
  241. ;; ("__Figure__" "Figure" "category-and-value")
  242. ;; ("__DvipngImage__" "Equation" "category-and-value")
  243. )
  244. "Map a CATEGORY-HANDLE to OD-VARIABLE and LABEL-STYLE.
  245. This is a list where each entry is of the form \\(CATEGORY-HANDLE
  246. OD-VARIABLE LABEL-STYLE CATEGORY-NAME\\). CATEGORY_HANDLE
  247. identifies the captionable entity in question. OD-VARIABLE is
  248. the OpenDocument sequence counter associated with the entity.
  249. These counters are declared within
  250. \"<text:sequence-decls>...</text:sequence-decls>\" block of
  251. `org-e-odt-content-template-file'. LABEL-STYLE is a key into
  252. `org-e-odt-label-styles' and specifies how a given entity should
  253. be captioned and referenced. CATEGORY-NAME is used for
  254. qualifying captions on export. You can modify the CATEGORY-NAME
  255. used in the exported document by modifying
  256. `org-export-dictionary'. For example, an embedded image in an
  257. English document is captioned as \"Figure 1: Orgmode Logo\", by
  258. default. If you want the image to be captioned as \"Illustration
  259. 1: Orgmode Logo\" instead, install an entry in
  260. `org-export-dictionary' which translates \"Figure\" to
  261. \"Illustration\" when the language is \"en\" and encoding is
  262. `:utf-8'.")
  263. (defvar org-e-odt-manifest-file-entries nil)
  264. (defvar hfy-user-sheet-assoc)
  265. (defvar org-e-odt-zip-dir nil
  266. "Temporary work directory for OpenDocument exporter.")
  267. ;;; User Configuration Variables
  268. (defgroup org-export-e-odt nil
  269. "Options for exporting Org mode files to ODT."
  270. :tag "Org Export ODT"
  271. :group 'org-export)
  272. ;;;; Debugging
  273. (defcustom org-e-odt-prettify-xml nil
  274. "Specify whether or not the xml output should be prettified.
  275. When this option is turned on, `indent-region' is run on all
  276. component xml buffers before they are saved. Turn this off for
  277. regular use. Turn this on if you need to examine the xml
  278. visually."
  279. :group 'org-export-e-odt
  280. :version "24.1"
  281. :type 'boolean)
  282. ;;;; Document schema
  283. (defcustom org-e-odt-schema-dir
  284. (let* ((d (expand-file-name (org-find-library-dir "org-e-odt.el")))
  285. (schema-dir-list (list (concat d "../etc/schema/"))) ;; ELPA and Git
  286. schema-dir)
  287. (setq schema-dir
  288. (car
  289. (delq nil
  290. (mapcar (lambda (sd)
  291. (when (and (file-readable-p
  292. (expand-file-name
  293. "od-manifest-schema-v1.2-cs01.rnc" sd))
  294. (file-readable-p
  295. (expand-file-name
  296. "od-schema-v1.2-cs01.rnc" sd))
  297. (file-readable-p
  298. (expand-file-name
  299. "schemas.xml" sd)))
  300. sd))
  301. schema-dir-list))))
  302. (or schema-dir
  303. (message "No OpenDocument schema files installed")))
  304. "Directory that contains OpenDocument schema files.
  305. This directory contains:
  306. 1. rnc files for OpenDocument schema
  307. 2. a \"schemas.xml\" file that specifies locating rules needed
  308. for auto validation of OpenDocument XML files.
  309. Use the customize interface to set this variable. This ensures
  310. that `rng-schema-locating-files' is updated and auto-validation
  311. of OpenDocument XML takes place based on the value
  312. `rng-nxml-auto-validate-flag'.
  313. The OASIS schema files are available only in the Org's private
  314. git repository. It is *not* bundled with GNU ELPA tar or
  315. standard Emacs distribution."
  316. :type '(choice
  317. (const :tag "Not set" nil)
  318. (directory :tag "Schema directory"))
  319. :group 'org-e-odt
  320. :version "24.1"
  321. :set
  322. (lambda (var value)
  323. "Set `org-e-odt-schema-dir'.
  324. Also add it to `rng-schema-locating-files'."
  325. (let ((schema-dir value))
  326. (set var
  327. (if (and
  328. (file-readable-p
  329. (expand-file-name "od-manifest-schema-v1.2-cs01.rnc" schema-dir))
  330. (file-readable-p
  331. (expand-file-name "od-schema-v1.2-cs01.rnc" schema-dir))
  332. (file-readable-p
  333. (expand-file-name "schemas.xml" schema-dir)))
  334. schema-dir
  335. (when value (message "No OpenDocument schema files")))))
  336. (when org-e-odt-schema-dir
  337. (eval-after-load 'rng-loc
  338. '(add-to-list 'rng-schema-locating-files
  339. (expand-file-name "schemas.xml"
  340. org-e-odt-schema-dir))))))
  341. ;;;; Document styles
  342. (defcustom org-e-odt-content-template-file nil
  343. "Template file for \"content.xml\".
  344. The exporter embeds the exported content just before
  345. \"</office:text>\" element.
  346. If unspecified, the file named \"OrgOdtContentTemplate.xml\"
  347. under `org-e-odt-styles-dir' is used."
  348. :type 'file
  349. :group 'org-export-e-odt
  350. :version "24.1")
  351. (defcustom org-e-odt-styles-file nil
  352. "Default styles file for use with ODT export.
  353. Valid values are one of:
  354. 1. nil
  355. 2. path to a styles.xml file
  356. 3. path to a *.odt or a *.ott file
  357. 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
  358. ...))
  359. In case of option 1, an in-built styles.xml is used. See
  360. `org-e-odt-styles-dir' for more information.
  361. In case of option 3, the specified file is unzipped and the
  362. styles.xml embedded therein is used.
  363. In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
  364. and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
  365. generated odt file. Use relative path for specifying the
  366. FILE-MEMBERS. styles.xml must be specified as one of the
  367. FILE-MEMBERS.
  368. Use options 1, 2 or 3 only if styles.xml alone suffices for
  369. achieving the desired formatting. Use option 4, if the styles.xml
  370. references additional files like header and footer images for
  371. achieving the desired formatting.
  372. Use \"#+ODT_STYLES_FILE: ...\" directive to set this variable on
  373. a per-file basis. For example,
  374. #+ODT_STYLES_FILE: \"/path/to/styles.xml\" or
  375. #+ODT_STYLES_FILE: (\"/path/to/file.ott\" (\"styles.xml\" \"image/hdr.png\"))."
  376. :group 'org-export-e-odt
  377. :version "24.1"
  378. :type
  379. '(choice
  380. (const :tag "Factory settings" nil)
  381. (file :must-match t :tag "styles.xml")
  382. (file :must-match t :tag "ODT or OTT file")
  383. (list :tag "ODT or OTT file + Members"
  384. (file :must-match t :tag "ODF Text or Text Template file")
  385. (cons :tag "Members"
  386. (file :tag " Member" "styles.xml")
  387. (repeat (file :tag "Member"))))))
  388. (defcustom org-e-odt-display-outline-level 2
  389. "Outline levels considered for enumerating captioned entities."
  390. :group 'org-export-e-odt
  391. :version "24.2"
  392. :type 'integer)
  393. ;;;; Document conversion
  394. (defcustom org-e-odt-convert-processes
  395. '(("LibreOffice"
  396. "soffice --headless --convert-to %f%x --outdir %d %i")
  397. ("unoconv"
  398. "unoconv -f %f -o %d %i"))
  399. "Specify a list of document converters and their usage.
  400. The converters in this list are offered as choices while
  401. customizing `org-e-odt-convert-process'.
  402. This variable is a list where each element is of the
  403. form (CONVERTER-NAME CONVERTER-CMD). CONVERTER-NAME is the name
  404. of the converter. CONVERTER-CMD is the shell command for the
  405. converter and can contain format specifiers. These format
  406. specifiers are interpreted as below:
  407. %i input file name in full
  408. %I input file name as a URL
  409. %f format of the output file
  410. %o output file name in full
  411. %O output file name as a URL
  412. %d output dir in full
  413. %D output dir as a URL.
  414. %x extra options as set in `org-e-odt-convert-capabilities'."
  415. :group 'org-export-e-odt
  416. :version "24.1"
  417. :type
  418. '(choice
  419. (const :tag "None" nil)
  420. (alist :tag "Converters"
  421. :key-type (string :tag "Converter Name")
  422. :value-type (group (string :tag "Command line")))))
  423. (defcustom org-e-odt-convert-process "LibreOffice"
  424. "Use this converter to convert from \"odt\" format to other formats.
  425. During customization, the list of converter names are populated
  426. from `org-e-odt-convert-processes'."
  427. :group 'org-export-e-odt
  428. :version "24.1"
  429. :type '(choice :convert-widget
  430. (lambda (w)
  431. (apply 'widget-convert (widget-type w)
  432. (eval (car (widget-get w :args)))))
  433. `((const :tag "None" nil)
  434. ,@(mapcar (lambda (c)
  435. `(const :tag ,(car c) ,(car c)))
  436. org-e-odt-convert-processes))))
  437. (defcustom org-e-odt-convert-capabilities
  438. '(("Text"
  439. ("odt" "ott" "doc" "rtf" "docx")
  440. (("pdf" "pdf") ("odt" "odt") ("rtf" "rtf") ("ott" "ott")
  441. ("doc" "doc" ":\"MS Word 97\"") ("docx" "docx") ("html" "html")))
  442. ("Web"
  443. ("html")
  444. (("pdf" "pdf") ("odt" "odt") ("html" "html")))
  445. ("Spreadsheet"
  446. ("ods" "ots" "xls" "csv" "xlsx")
  447. (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv") ("ods" "ods")
  448. ("xls" "xls") ("xlsx" "xlsx")))
  449. ("Presentation"
  450. ("odp" "otp" "ppt" "pptx")
  451. (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("otp" "otp") ("ppt" "ppt")
  452. ("pptx" "pptx") ("odg" "odg"))))
  453. "Specify input and output formats of `org-e-odt-convert-process'.
  454. More correctly, specify the set of input and output formats that
  455. the user is actually interested in.
  456. This variable is an alist where each element is of the
  457. form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
  458. INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
  459. alist where each element is of the form (OUTPUT-FMT
  460. OUTPUT-FILE-EXTENSION EXTRA-OPTIONS).
  461. The variable is interpreted as follows:
  462. `org-e-odt-convert-process' can take any document that is in
  463. INPUT-FMT-LIST and produce any document that is in the
  464. OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
  465. OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
  466. serves dual purposes:
  467. - It is used for populating completion candidates during
  468. `org-e-odt-convert' commands.
  469. - It is used as the value of \"%f\" specifier in
  470. `org-e-odt-convert-process'.
  471. EXTRA-OPTIONS is used as the value of \"%x\" specifier in
  472. `org-e-odt-convert-process'.
  473. DOCUMENT-CLASS is used to group a set of file formats in
  474. INPUT-FMT-LIST in to a single class.
  475. Note that this variable inherently captures how LibreOffice based
  476. converters work. LibreOffice maps documents of various formats
  477. to classes like Text, Web, Spreadsheet, Presentation etc and
  478. allow document of a given class (irrespective of it's source
  479. format) to be converted to any of the export formats associated
  480. with that class.
  481. See default setting of this variable for an typical
  482. configuration."
  483. :group 'org-export-e-odt
  484. :version "24.1"
  485. :type
  486. '(choice
  487. (const :tag "None" nil)
  488. (alist :tag "Capabilities"
  489. :key-type (string :tag "Document Class")
  490. :value-type
  491. (group (repeat :tag "Input formats" (string :tag "Input format"))
  492. (alist :tag "Output formats"
  493. :key-type (string :tag "Output format")
  494. :value-type
  495. (group (string :tag "Output file extension")
  496. (choice
  497. (const :tag "None" nil)
  498. (string :tag "Extra options"))))))))
  499. (defcustom org-e-odt-preferred-output-format nil
  500. "Automatically post-process to this format after exporting to \"odt\".
  501. Interactive commands `org-export-as-e-odt' and
  502. `org-export-as-e-odt-and-open' export first to \"odt\" format and
  503. then use `org-e-odt-convert-process' to convert the
  504. resulting document to this format. During customization of this
  505. variable, the list of valid values are populated based on
  506. `org-e-odt-convert-capabilities'."
  507. :group 'org-export-e-odt
  508. :version "24.1"
  509. :type '(choice :convert-widget
  510. (lambda (w)
  511. (apply 'widget-convert (widget-type w)
  512. (eval (car (widget-get w :args)))))
  513. `((const :tag "None" nil)
  514. ,@(mapcar (lambda (c)
  515. `(const :tag ,c ,c))
  516. (org-e-odt-reachable-formats "odt")))))
  517. ;;;; Drawers
  518. (defcustom org-e-odt-format-drawer-function nil
  519. "Function called to format a drawer in HTML code.
  520. The function must accept two parameters:
  521. NAME the drawer name, like \"LOGBOOK\"
  522. CONTENTS the contents of the drawer.
  523. The function should return the string to be exported.
  524. For example, the variable could be set to the following function
  525. in order to mimic default behaviour:
  526. \(defun org-e-odt-format-drawer-default \(name contents\)
  527. \"Format a drawer element for HTML export.\"
  528. contents\)"
  529. :group 'org-export-e-odt
  530. :type 'function)
  531. ;;;; Headline
  532. (defcustom org-e-odt-format-headline-function nil
  533. "Function to format headline text.
  534. This function will be called with 5 arguments:
  535. TODO the todo keyword \(string or nil\).
  536. TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
  537. PRIORITY the priority of the headline \(integer or nil\)
  538. TEXT the main headline text \(string\).
  539. TAGS the tags string, separated with colons \(string or nil\).
  540. The function result will be used in the section format string.
  541. As an example, one could set the variable to the following, in
  542. order to reproduce the default set-up:
  543. \(defun org-e-odt-format-headline \(todo todo-type priority text tags\)
  544. \"Default format function for an headline.\"
  545. \(concat \(when todo
  546. \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo\)\)
  547. \(when priority
  548. \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
  549. text
  550. \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
  551. :group 'org-export-e-odt
  552. :type 'function)
  553. ;;;; Inlinetasks
  554. (defcustom org-e-odt-format-inlinetask-function nil
  555. "Function called to format an inlinetask in HTML code.
  556. The function must accept six parameters:
  557. TODO the todo keyword, as a string
  558. TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
  559. PRIORITY the inlinetask priority, as a string
  560. NAME the inlinetask name, as a string.
  561. TAGS the inlinetask tags, as a string.
  562. CONTENTS the contents of the inlinetask, as a string.
  563. The function should return the string to be exported.
  564. For example, the variable could be set to the following function
  565. in order to mimic default behaviour:
  566. \(defun org-e-odt-format-inlinetask \(todo type priority name tags contents\)
  567. \"Format an inline task element for HTML export.\"
  568. \(let \(\(full-title
  569. \(concat
  570. \(when todo
  571. \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
  572. \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
  573. title
  574. \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
  575. \(format \(concat \"\\\\begin{center}\\n\"
  576. \"\\\\fbox{\\n\"
  577. \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
  578. \"%s\\n\\n\"
  579. \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
  580. \"%s\"
  581. \"\\\\end{minipage}}\"
  582. \"\\\\end{center}\"\)
  583. full-title contents\)\)"
  584. :group 'org-export-e-odt
  585. :type 'function)
  586. ;;;; Links
  587. (defcustom org-e-odt-inline-image-rules
  588. '(("file" . "\\.\\(jpeg\\|jpg\\|png\\|gif\\)\\'"))
  589. "Rules characterizing image files that can be inlined into HTML.
  590. A rule consists in an association whose key is the type of link
  591. to consider, and value is a regexp that will be matched against
  592. link's path.
  593. Note that, by default, the image extension *actually* allowed
  594. depend on the way the HTML file is processed. When used with
  595. pdflatex, pdf, jpg and png images are OK. When processing
  596. through dvi to Postscript, only ps and eps are allowed. The
  597. default we use here encompasses both."
  598. :group 'org-export-e-odt
  599. :type '(alist :key-type (string :tag "Type")
  600. :value-type (regexp :tag "Path")))
  601. (defcustom org-e-odt-pixels-per-inch display-pixels-per-inch
  602. "Scaling factor for converting images pixels to inches.
  603. Use this for sizing of embedded images. See Info node `(org)
  604. Images in ODT export' for more information."
  605. :type 'float
  606. :group 'org-export-e-odt
  607. :version "24.1")
  608. ;;;; Plain text
  609. (defcustom org-e-odt-quotes
  610. '(("fr"
  611. ("\\(\\s-\\|[[(]\\|^\\)\"" . "« ")
  612. ("\\(\\S-\\)\"" . "» ")
  613. ("\\(\\s-\\|(\\|^\\)'" . "'"))
  614. ("en"
  615. ("\\(\\s-\\|[[(]\\|^\\)\"" . "“")
  616. ("\\(\\S-\\)\"" . "”")
  617. ("\\(\\s-\\|(\\|^\\)'" . "‘")
  618. ("\\(\\S-\\)'" . "’")))
  619. "Alist for quotes to use when converting english double-quotes.
  620. The CAR of each item in this alist is the language code.
  621. The CDR of each item in this alist is a list of three CONS:
  622. - the first CONS defines the opening quote;
  623. - the second CONS defines the closing quote;
  624. - the last CONS defines single quotes.
  625. For each item in a CONS, the first string is a regexp
  626. for allowed characters before/after the quote, the second
  627. string defines the replacement string for this quote."
  628. :group 'org-export-e-odt
  629. :type '(list
  630. (cons :tag "Opening quote"
  631. (string :tag "Regexp for char before")
  632. (string :tag "Replacement quote "))
  633. (cons :tag "Closing quote"
  634. (string :tag "Regexp for char after ")
  635. (string :tag "Replacement quote "))
  636. (cons :tag "Single quote"
  637. (string :tag "Regexp for char before")
  638. (string :tag "Replacement quote "))))
  639. ;;;; Src Block
  640. (defcustom org-e-odt-create-custom-styles-for-srcblocks t
  641. "Whether custom styles for colorized source blocks be automatically created.
  642. When this option is turned on, the exporter creates custom styles
  643. for source blocks based on the advice of `htmlfontify'. Creation
  644. of custom styles happen as part of `org-e-odt-hfy-face-to-css'.
  645. When this option is turned off exporter does not create such
  646. styles.
  647. Use the latter option if you do not want the custom styles to be
  648. based on your current display settings. It is necessary that the
  649. styles.xml already contains needed styles for colorizing to work.
  650. This variable is effective only if
  651. `org-e-odt-fontify-srcblocks' is turned on."
  652. :group 'org-export-e-odt
  653. :version "24.1"
  654. :type 'boolean)
  655. (defcustom org-e-odt-fontify-srcblocks t
  656. "Specify whether or not source blocks need to be fontified.
  657. Turn this option on if you want to colorize the source code
  658. blocks in the exported file. For colorization to work, you need
  659. to make available an enhanced version of `htmlfontify' library."
  660. :type 'boolean
  661. :group 'org-export-e-odt
  662. :version "24.1")
  663. ;;;; Table
  664. (defcustom org-e-odt-table-caption-above t
  665. "When non-nil, place caption string at the beginning of the table.
  666. Otherwise, place it near the end."
  667. :group 'org-export-e-odt
  668. :type 'boolean)
  669. (defcustom org-e-odt-table-styles
  670. '(("OrgEquation" "OrgEquation"
  671. ((use-first-column-styles . t)
  672. (use-last-column-styles . t))))
  673. "Specify how Table Styles should be derived from a Table Template.
  674. This is a list where each element is of the
  675. form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
  676. TABLE-STYLE-NAME is the style associated with the table through
  677. \"#+ATTR_ODT: :style TABLE-STYLE-NAME\" line.
  678. TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
  679. TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
  680. below) that is included in
  681. `org-e-odt-content-template-file'.
  682. TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
  683. \"TableCell\"
  684. PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
  685. \"TableParagraph\"
  686. TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
  687. \"FirstRow\" | \"LastRow\" |
  688. \"EvenRow\" | \"OddRow\" |
  689. \"EvenColumn\" | \"OddColumn\" | \"\"
  690. where \"+\" above denotes string concatenation.
  691. TABLE-CELL-OPTIONS is an alist where each element is of the
  692. form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
  693. TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
  694. `use-last-row-styles' |
  695. `use-first-column-styles' |
  696. `use-last-column-styles' |
  697. `use-banding-rows-styles' |
  698. `use-banding-columns-styles' |
  699. `use-first-row-styles'
  700. ON-OR-OFF := `t' | `nil'
  701. For example, with the following configuration
  702. \(setq org-e-odt-table-styles
  703. '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
  704. \(\(use-first-row-styles . t\)
  705. \(use-first-column-styles . t\)\)\)
  706. \(\"TableWithHeaderColumns\" \"Custom\"
  707. \(\(use-first-column-styles . t\)\)\)\)\)
  708. 1. A table associated with \"TableWithHeaderRowsAndColumns\"
  709. style will use the following table-cell styles -
  710. \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
  711. \"CustomTableCell\" and the following paragraph styles
  712. \"CustomFirstRowTableParagraph\",
  713. \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
  714. as appropriate.
  715. 2. A table associated with \"TableWithHeaderColumns\" style will
  716. use the following table-cell styles -
  717. \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
  718. following paragraph styles
  719. \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
  720. as appropriate..
  721. Note that TABLE-TEMPLATE-NAME corresponds to the
  722. \"<table:table-template>\" elements contained within
  723. \"<office:styles>\". The entries (TABLE-STYLE-NAME
  724. TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
  725. \"table:template-name\" and \"table:use-first-row-styles\" etc
  726. attributes of \"<table:table>\" element. Refer ODF-1.2
  727. specification for more information. Also consult the
  728. implementation filed under `org-e-odt-get-table-cell-styles'.
  729. The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
  730. formatting of numbered display equations. Do not delete this
  731. style from the list."
  732. :group 'org-export-e-odt
  733. :version "24.1"
  734. :type '(choice
  735. (const :tag "None" nil)
  736. (repeat :tag "Table Styles"
  737. (list :tag "Table Style Specification"
  738. (string :tag "Table Style Name")
  739. (string :tag "Table Template Name")
  740. (alist :options (use-first-row-styles
  741. use-last-row-styles
  742. use-first-column-styles
  743. use-last-column-styles
  744. use-banding-rows-styles
  745. use-banding-columns-styles)
  746. :key-type symbol
  747. :value-type (const :tag "True" t))))))
  748. ;;; Internal functions
  749. ;;;; Date
  750. (defun org-e-odt--date (&optional org-ts fmt)
  751. (save-match-data
  752. (let* ((time
  753. (and (stringp org-ts)
  754. (string-match org-ts-regexp0 org-ts)
  755. (apply 'encode-time
  756. (org-fix-decoded-time
  757. (org-parse-time-string (match-string 0 org-ts) t)))))
  758. date)
  759. (cond
  760. (fmt (format-time-string fmt time))
  761. (t (setq date (format-time-string "%Y-%m-%dT%H:%M:%S%z" time))
  762. (format "%s:%s" (substring date 0 -2) (substring date -2)))))))
  763. ;;;; Frame
  764. (defun org-e-odt--frame (text width height style &optional extra
  765. anchor-type)
  766. (let ((frame-attrs
  767. (concat
  768. (if width (format " svg:width=\"%0.2fcm\"" width) "")
  769. (if height (format " svg:height=\"%0.2fcm\"" height) "")
  770. extra
  771. (format " text:anchor-type=\"%s\"" (or anchor-type "paragraph")))))
  772. (format
  773. "\n<draw:frame draw:style-name=\"%s\"%s>\n%s\n</draw:frame>"
  774. style frame-attrs
  775. (concat text
  776. (let ((title (get-text-property 0 :title text))
  777. (desc (get-text-property 0 :description text)))
  778. (concat (and title
  779. (format "<svg:title>%s</svg:title>"
  780. (org-e-odt-encode-plain-text title t)))
  781. (and desc
  782. (format "<svg:desc>%s</svg:desc>"
  783. (org-e-odt-encode-plain-text desc t)))))))))
  784. ;;;; Library wrappers
  785. (defun org-e-odt--adopt-elements (parent &rest children)
  786. (prog1 parent
  787. (mapc (lambda (child)
  788. (let ((parent-1 (org-element-adopt-element parent child nil)))
  789. (assert (eq parent-1 parent))))
  790. children)))
  791. (defun org-e-odt--zip-extract (archive members target)
  792. (when (atom members) (setq members (list members)))
  793. (mapc (lambda (archive member target)
  794. (require 'arc-mode)
  795. (let* ((--quote-file-name
  796. ;; This is shamelessly stolen from `archive-zip-extract'.
  797. (lambda (name)
  798. (if (or (not (memq system-type '(windows-nt ms-dos)))
  799. (and (boundp 'w32-quote-process-args)
  800. (null w32-quote-process-args)))
  801. (shell-quote-argument name)
  802. name)))
  803. (target (funcall --quote-file-name target))
  804. (archive (expand-file-name archive))
  805. (archive-zip-extract
  806. (list "unzip" "-qq" "-o" "-d" target))
  807. exit-code command-output)
  808. (setq command-output
  809. (with-temp-buffer
  810. (setq exit-code (archive-zip-extract archive member))
  811. (buffer-string)))
  812. (unless (zerop exit-code)
  813. (message command-output)
  814. (error "Extraction failed"))))
  815. members))
  816. ;;;; Textbox
  817. (defun org-e-odt--textbox (text width height style &optional
  818. extra anchor-type)
  819. (org-e-odt--frame
  820. (format "\n<draw:text-box %s>%s\n</draw:text-box>"
  821. (concat (format " fo:min-height=\"%0.2fcm\"" (or height .2))
  822. (and (not width)
  823. (format " fo:min-width=\"%0.2fcm\"" (or width .2))))
  824. text)
  825. width nil style extra anchor-type))
  826. ;;;; Table of Contents
  827. (defun org-e-odt-begin-toc (index-title depth)
  828. (concat
  829. (format "
  830. <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
  831. <text:table-of-content-source text:outline-level=\"%d\">
  832. <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
  833. " depth index-title)
  834. (let ((levels (number-sequence 1 10)))
  835. (mapconcat
  836. (lambda (level)
  837. (format
  838. "
  839. <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
  840. <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
  841. <text:index-entry-chapter/>
  842. <text:index-entry-text/>
  843. <text:index-entry-link-end/>
  844. </text:table-of-content-entry-template>
  845. " level level)) levels ""))
  846. (format "
  847. </text:table-of-content-source>
  848. <text:index-body>
  849. <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
  850. <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
  851. </text:index-title>
  852. " index-title)))
  853. (defun org-e-odt-end-toc ()
  854. (format "
  855. </text:index-body>
  856. </text:table-of-content>
  857. "))
  858. (defun* org-e-odt-format-toc-headline
  859. (todo todo-type priority text tags
  860. &key level section-number headline-label &allow-other-keys)
  861. (setq text (concat
  862. (and org-export-with-section-numbers
  863. (concat section-number ". "))
  864. text
  865. (and tags
  866. (concat
  867. "<text:tab/>"
  868. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  869. "OrgTag" tags)))))
  870. (when todo
  871. (setq text (format "<text:span text:style-name=\"%s\">%s</text:span>"
  872. "OrgTodo" text)))
  873. (org-e-odt-format-link text (concat "#" headline-label) t))
  874. (defun org-e-odt-toc (depth info)
  875. (assert (wholenump depth))
  876. (let* ((title (org-export-translate "Table of Contents" :utf-8 info))
  877. (headlines (org-export-collect-headlines info depth)))
  878. (when headlines
  879. (concat
  880. (org-e-odt-begin-toc title depth)
  881. (mapconcat
  882. (lambda (headline)
  883. (let* ((entry (org-e-odt-format-headline--wrap
  884. headline info 'org-e-odt-format-toc-headline))
  885. (level (org-export-get-relative-level headline info))
  886. (style (format "Contents_20_%d" level)))
  887. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  888. style entry)))
  889. headlines "\n")
  890. (org-e-odt-end-toc)))))
  891. ;;;; Document styles
  892. (defun org-e-odt-add-automatic-style (object-type &optional object-props)
  893. "Create an automatic style of type OBJECT-TYPE with param OBJECT-PROPS.
  894. OBJECT-PROPS is (typically) a plist created by passing
  895. \"#+ATTR_ODT: \" option of the object in question to
  896. `org-e-odt-parse-block-attributes'.
  897. Use `org-e-odt-object-counters' to generate an automatic
  898. OBJECT-NAME and STYLE-NAME. If OBJECT-PROPS is non-nil, add a
  899. new entry in `org-e-odt-automatic-styles'. Return (OBJECT-NAME
  900. . STYLE-NAME)."
  901. (assert (stringp object-type))
  902. (let* ((object (intern object-type))
  903. (seqvar object)
  904. (seqno (1+ (or (plist-get org-e-odt-object-counters seqvar) 0)))
  905. (object-name (format "%s%d" object-type seqno)) style-name)
  906. (setq org-e-odt-object-counters
  907. (plist-put org-e-odt-object-counters seqvar seqno))
  908. (when object-props
  909. (setq style-name (format "Org%s" object-name))
  910. (setq org-e-odt-automatic-styles
  911. (plist-put org-e-odt-automatic-styles object
  912. (append (list (list style-name object-props))
  913. (plist-get org-e-odt-automatic-styles object)))))
  914. (cons object-name style-name)))
  915. ;;;; Caption and Labels
  916. (defun org-e-odt--wrap-label (element output)
  917. "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
  918. This function shouldn't be used for floats. See
  919. `org-e-odt--caption/label-string'."
  920. ;; (let ((label (org-element-property :name element)))
  921. ;; (if (or (not output) (not label) (string= output "") (string= label ""))
  922. ;; output
  923. ;; (concat (format "\\label{%s}\n" label) output)))
  924. output)
  925. (defun org-e-odt--caption/label-string (caption label info)
  926. "Return caption and label HTML string for floats.
  927. CAPTION is a cons cell of secondary strings, the car being the
  928. standard caption and the cdr its short form. LABEL is a string
  929. representing the label. INFO is a plist holding contextual
  930. information.
  931. If there's no caption nor label, return the empty string.
  932. For non-floats, see `org-e-odt--wrap-label'."
  933. (setq label nil) ;; FIXME
  934. (let ((label-str (if label (format "\\label{%s}" label) "")))
  935. (cond
  936. ((and (not caption) (not label)) "")
  937. ((not caption) (format "\\label{%s}\n" label))
  938. ;; Option caption format with short name.
  939. ((cdr caption)
  940. (format "\\caption[%s]{%s%s}\n"
  941. (org-export-data (cdr caption) info)
  942. label-str
  943. (org-export-data (car caption) info)))
  944. ;; Standard caption format.
  945. ;; (t (format "\\caption{%s%s}\n"
  946. ;; label-str
  947. ;; (org-export-data (car caption) info)))
  948. (t (org-export-data (car caption) info)))))
  949. ;;;; Checkbox
  950. (defun org-e-odt--checkbox (item)
  951. "Return check-box string associated to ITEM."
  952. (let ((checkbox (org-element-property :checkbox item)))
  953. (if (not checkbox) ""
  954. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  955. "OrgCode" (case checkbox
  956. (on "[&#x2713;] ") ; CHECK MARK
  957. (off "[ ] ")
  958. (trans "[-] "))))))
  959. ;;; Template
  960. (defun org-e-odt-template (contents info)
  961. "Return complete document string after HTML conversion.
  962. CONTENTS is the transcoded contents string. RAW-DATA is the
  963. original parsed data. INFO is a plist holding export options."
  964. ;; Write meta file.
  965. (let ((title (org-export-data (plist-get info :title) info))
  966. (author (let ((author (plist-get info :author)))
  967. (if (not author) "" (org-export-data author info))))
  968. (date (org-e-odt--date
  969. (org-export-data (plist-get info :date) info)))
  970. (email (plist-get info :email))
  971. (keywords (plist-get info :keywords))
  972. (description (plist-get info :description)))
  973. (write-region
  974. (concat
  975. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  976. <office:document-meta
  977. xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
  978. xmlns:xlink=\"http://www.w3.org/1999/xlink\"
  979. xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
  980. xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
  981. xmlns:ooo=\"http://openoffice.org/2004/office\"
  982. office:version=\"1.2\">
  983. <office:meta>\n"
  984. (format "<dc:creator>%s</dc:creator>\n" author)
  985. (format "<meta:initial-creator>%s</meta:initial-creator>\n" author)
  986. (format "<dc:date>%s</dc:date>\n" date)
  987. (format "<meta:creation-date>%s</meta:creation-date>\n" date)
  988. (format "<meta:generator>%s</meta:generator>\n"
  989. (let ((creator-info (plist-get info :with-creator)))
  990. (if (or (not creator-info) (eq creator-info 'comment)) ""
  991. (plist-get info :creator))))
  992. (format "<meta:keyword>%s</meta:keyword>\n" keywords)
  993. (format "<dc:subject>%s</dc:subject>\n" description)
  994. (format "<dc:title>%s</dc:title>\n" title)
  995. "\n"
  996. " </office:meta>\n" "</office:document-meta>")
  997. nil (concat org-e-odt-zip-dir "meta.xml"))
  998. ;; Add meta.xml in to manifest.
  999. (org-e-odt-create-manifest-file-entry "text/xml" "meta.xml"))
  1000. ;; Update styles file.
  1001. ;; Copy styles.xml. Also dump htmlfontify styles, if there is any.
  1002. ;; Write styles file.
  1003. (let* ((styles-file (plist-get info :odt-styles-file))
  1004. (styles-file (and styles-file (read (org-trim styles-file))))
  1005. ;; Non-availability of styles.xml is not a critical
  1006. ;; error. For now throw an error purely for aesthetic
  1007. ;; reasons.
  1008. (styles-file (or styles-file
  1009. org-e-odt-styles-file
  1010. (expand-file-name "OrgOdtStyles.xml"
  1011. org-e-odt-styles-dir)
  1012. (error "org-e-odt: Missing styles file?"))))
  1013. (cond
  1014. ((listp styles-file)
  1015. (let ((archive (nth 0 styles-file))
  1016. (members (nth 1 styles-file)))
  1017. (org-e-odt--zip-extract archive members org-e-odt-zip-dir)
  1018. (mapc
  1019. (lambda (member)
  1020. (when (org-file-image-p member)
  1021. (let* ((image-type (file-name-extension member))
  1022. (media-type (format "image/%s" image-type)))
  1023. (org-e-odt-create-manifest-file-entry media-type member))))
  1024. members)))
  1025. ((and (stringp styles-file) (file-exists-p styles-file))
  1026. (let ((styles-file-type (file-name-extension styles-file)))
  1027. (cond
  1028. ((string= styles-file-type "xml")
  1029. (copy-file styles-file (concat org-e-odt-zip-dir "styles.xml") t))
  1030. ((member styles-file-type '("odt" "ott"))
  1031. (org-e-odt--zip-extract styles-file "styles.xml" org-e-odt-zip-dir)))))
  1032. (t
  1033. (error (format "Invalid specification of styles.xml file: %S"
  1034. org-e-odt-styles-file))))
  1035. ;; create a manifest entry for styles.xml
  1036. (org-e-odt-create-manifest-file-entry "text/xml" "styles.xml")
  1037. ;; FIXME: Who is opening an empty styles.xml before this point?
  1038. (with-current-buffer
  1039. (find-file-noselect (concat org-e-odt-zip-dir "styles.xml") t)
  1040. (revert-buffer t t)
  1041. ;; Write custom styles for source blocks
  1042. ;; Save STYLES used for colorizing of source blocks.
  1043. ;; Update styles.xml with styles that were collected as part of
  1044. ;; `org-e-odt-hfy-face-to-css' callbacks.
  1045. (let ((styles (mapconcat (lambda (style) (format " %s\n" (cddr style)))
  1046. hfy-user-sheet-assoc "")))
  1047. (when styles
  1048. (goto-char (point-min))
  1049. (when (re-search-forward "</office:styles>" nil t)
  1050. (goto-char (match-beginning 0))
  1051. (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n"))))
  1052. ;; Update styles.xml - take care of outline numbering
  1053. ;; Don't make automatic backup of styles.xml file. This setting
  1054. ;; prevents the backed-up styles.xml file from being zipped in to
  1055. ;; odt file. This is more of a hackish fix. Better alternative
  1056. ;; would be to fix the zip command so that the output odt file
  1057. ;; includes only the needed files and excludes any auto-generated
  1058. ;; extra files like backups and auto-saves etc etc. Note that
  1059. ;; currently the zip command zips up the entire temp directory so
  1060. ;; that any auto-generated files created under the hood ends up in
  1061. ;; the resulting odt file.
  1062. (set (make-local-variable 'backup-inhibited) t)
  1063. ;; Outline numbering is retained only upto LEVEL.
  1064. ;; To disable outline numbering pass a LEVEL of 0.
  1065. (goto-char (point-min))
  1066. (let ((regex
  1067. "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
  1068. (replacement
  1069. "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
  1070. (while (re-search-forward regex nil t)
  1071. (unless (let ((sec-num (plist-get info :section-numbers))
  1072. (level (string-to-number (match-string 2))))
  1073. (if (wholenump sec-num) (<= level sec-num) sec-num))
  1074. (replace-match replacement t nil))))
  1075. (save-buffer 0)))
  1076. ;; Update content.xml.
  1077. (with-temp-buffer
  1078. (insert-file-contents
  1079. (or org-e-odt-content-template-file
  1080. (expand-file-name "OrgOdtContentTemplate.xml"
  1081. org-e-odt-styles-dir)))
  1082. ;; Write automatic styles.
  1083. ;; - Position the cursor.
  1084. (goto-char (point-min))
  1085. (re-search-forward " </office:automatic-styles>" nil t)
  1086. (goto-char (match-beginning 0))
  1087. ;; - Dump automatic table styles
  1088. (loop for (style-name props) in
  1089. (plist-get org-e-odt-automatic-styles 'Table) do
  1090. (when (setq props (or (plist-get props :rel-width) 96))
  1091. (insert (format org-e-odt-table-style-format style-name props))))
  1092. ;; Update display level.
  1093. ;; - Remove existing sequence decls. Also position the cursor.
  1094. (goto-char (point-min))
  1095. (when (re-search-forward "<text:sequence-decls" nil t)
  1096. (delete-region (match-beginning 0)
  1097. (re-search-forward "</text:sequence-decls>" nil nil)))
  1098. ;; Update sequence decls according to user preference.
  1099. (insert
  1100. (format
  1101. "\n<text:sequence-decls>\n%s\n</text:sequence-decls>"
  1102. (mapconcat
  1103. (lambda (x)
  1104. (format
  1105. "<text:sequence-decl text:display-outline-level=\"%d\" text:name=\"%s\"/>"
  1106. org-e-odt-display-outline-level (nth 1 x)))
  1107. org-e-odt-category-map-alist "\n")))
  1108. ;; Position the cursor to document body.
  1109. (goto-char (point-min))
  1110. (re-search-forward "</office:text>" nil nil)
  1111. (goto-char (match-beginning 0))
  1112. ;; Preamble - Title, Author, Date etc.
  1113. (insert
  1114. (let* ((title (org-export-data (plist-get info :title) info))
  1115. (author (and (plist-get info :with-author)
  1116. (let ((auth (plist-get info :author)))
  1117. (and auth (org-export-data auth info)))))
  1118. (date (org-export-data (plist-get info :date) info))
  1119. (iso-date (org-e-odt--date date))
  1120. (date (org-e-odt--date date "%d %b %Y"))
  1121. (email (plist-get info :email))
  1122. ;; switch on or off above vars based on user settings
  1123. (author (and (plist-get info :with-author) (or author email)))
  1124. ;; (date (and (plist-get info :time-stamp-file) date))
  1125. (email (and (plist-get info :with-email) email)))
  1126. (concat
  1127. ;; title
  1128. (when title
  1129. (concat
  1130. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1131. "OrgTitle" (format "\n<text:title>%s</text:title>" title))
  1132. ;; separator
  1133. "\n<text:p text:style-name=\"OrgTitle\"/>"))
  1134. (cond
  1135. ((and author (not email))
  1136. ;; author only
  1137. (concat
  1138. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1139. "OrgSubtitle"
  1140. (format "<text:initial-creator>%s</text:initial-creator>" author))
  1141. ;; separator
  1142. "\n<text:p text:style-name=\"OrgSubtitle\"/>"))
  1143. ((and author email)
  1144. ;; author and email
  1145. (concat
  1146. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1147. "OrgSubtitle"
  1148. (org-e-odt-format-link
  1149. (format "<text:initial-creator>%s</text:initial-creator>" author)
  1150. (concat "mailto:" email)))
  1151. ;; separator
  1152. "\n<text:p text:style-name=\"OrgSubtitle\"/>")))
  1153. ;; date
  1154. (when date
  1155. (concat
  1156. (format
  1157. "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1158. "OrgSubtitle"
  1159. (format
  1160. "\n<text:date style:data-style-name=\"%s\" text:date-value=\"%s\">%s</text:date>"
  1161. "N75" iso-date date))
  1162. ;; separator
  1163. "<text:p text:style-name=\"OrgSubtitle\"/>")))))
  1164. ;; Table of Contents
  1165. (let ((depth (plist-get info :with-toc)))
  1166. (when (wholenump depth) (insert (org-e-odt-toc depth info))))
  1167. ;; Contents.
  1168. (insert contents)
  1169. ;; Return contents.
  1170. (buffer-substring-no-properties (point-min) (point-max))))
  1171. ;;; Transcode Functions
  1172. ;;;; Bold
  1173. (defun org-e-odt-bold (bold contents info)
  1174. "Transcode BOLD from Org to ODT.
  1175. CONTENTS is the text with bold markup. INFO is a plist holding
  1176. contextual information."
  1177. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1178. "Bold" contents))
  1179. ;;;; Center Block
  1180. (defun org-e-odt-center-block (center-block contents info)
  1181. "Transcode a CENTER-BLOCK element from Org to ODT.
  1182. CONTENTS holds the contents of the center block. INFO is a plist
  1183. holding contextual information."
  1184. (org-e-odt--wrap-label center-block contents))
  1185. ;;;; Clock
  1186. (defun org-e-odt-clock (clock contents info)
  1187. "Transcode a CLOCK element from Org to ODT.
  1188. CONTENTS is nil. INFO is a plist used as a communication
  1189. channel."
  1190. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1191. "OrgTimestampWrapper"
  1192. (concat
  1193. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1194. "OrgTimestampKeyword" org-clock-string)
  1195. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1196. "OrgTimestamp"
  1197. (concat (org-translate-time
  1198. (org-element-property :value clock))
  1199. (let ((time (org-element-property :time clock)))
  1200. (and time (format " (%s)" time))))))))
  1201. ;;;; Code
  1202. (defun org-e-odt-code (code contents info)
  1203. "Transcode a CODE object from Org to ODT.
  1204. CONTENTS is nil. INFO is a plist used as a communication
  1205. channel."
  1206. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1207. "OrgCode" (org-element-property :value code)))
  1208. ;;;; Comment
  1209. ;; Comments are ignored.
  1210. ;;;; Comment Block
  1211. ;; Comment Blocks are ignored.
  1212. ;;;; Drawer
  1213. (defun org-e-odt-drawer (drawer contents info)
  1214. "Transcode a DRAWER element from Org to ODT.
  1215. CONTENTS holds the contents of the block. INFO is a plist
  1216. holding contextual information."
  1217. (let* ((name (org-element-property :drawer-name drawer))
  1218. (output (if (functionp org-e-odt-format-drawer-function)
  1219. (funcall org-e-odt-format-drawer-function
  1220. name contents)
  1221. ;; If there's no user defined function: simply
  1222. ;; display contents of the drawer.
  1223. contents)))
  1224. (org-e-odt--wrap-label drawer output)))
  1225. ;;;; Dynamic Block
  1226. (defun org-e-odt-dynamic-block (dynamic-block contents info)
  1227. "Transcode a DYNAMIC-BLOCK element from Org to ODT.
  1228. CONTENTS holds the contents of the block. INFO is a plist
  1229. holding contextual information. See `org-export-data'."
  1230. (org-e-odt--wrap-label dynamic-block contents))
  1231. ;;;; Entity
  1232. (defun org-e-odt-entity (entity contents info)
  1233. "Transcode an ENTITY object from Org to ODT.
  1234. CONTENTS are the definition itself. INFO is a plist holding
  1235. contextual information."
  1236. ;; (let ((ent (org-element-property :latex entity)))
  1237. ;; (if (org-element-property :latex-math-p entity)
  1238. ;; (format "$%s$" ent)
  1239. ;; ent))
  1240. (org-element-property :utf-8 entity))
  1241. ;;;; Example Block
  1242. (defun org-e-odt-example-block (example-block contents info)
  1243. "Transcode a EXAMPLE-BLOCK element from Org to ODT.
  1244. CONTENTS is nil. INFO is a plist holding contextual information."
  1245. (org-e-odt--wrap-label
  1246. example-block (org-e-odt-format-code example-block info)))
  1247. ;;;; Export Snippet
  1248. (defun org-e-odt-export-snippet (export-snippet contents info)
  1249. "Transcode a EXPORT-SNIPPET object from Org to ODT.
  1250. CONTENTS is nil. INFO is a plist holding contextual information."
  1251. (when (eq (org-export-snippet-backend export-snippet) 'e-odt)
  1252. (org-element-property :value export-snippet)))
  1253. ;;;; Export Block
  1254. (defun org-e-odt-export-block (export-block contents info)
  1255. "Transcode a EXPORT-BLOCK element from Org to ODT.
  1256. CONTENTS is nil. INFO is a plist holding contextual information."
  1257. (when (string= (org-element-property :type export-block) "ODT")
  1258. (org-remove-indentation (org-element-property :value export-block))))
  1259. ;;;; Fixed Width
  1260. (defun org-e-odt-fixed-width (fixed-width contents info)
  1261. "Transcode a FIXED-WIDTH element from Org to ODT.
  1262. CONTENTS is nil. INFO is a plist holding contextual information."
  1263. (org-e-odt--wrap-label
  1264. fixed-width (org-e-odt-do-format-code
  1265. (org-element-property :value fixed-width))))
  1266. ;;;; Footnote Definition
  1267. ;; Footnote Definitions are ignored.
  1268. ;;;; Footnote Reference
  1269. (defun org-e-odt-footnote-reference (footnote-reference contents info)
  1270. "Transcode a FOOTNOTE-REFERENCE element from Org to ODT.
  1271. CONTENTS is nil. INFO is a plist holding contextual information."
  1272. (let ((--format-footnote-definition
  1273. (function
  1274. (lambda (n def)
  1275. (setq n (format "%d" n))
  1276. (let ((id (concat "fn" n))
  1277. (note-class "footnote")
  1278. (par-style "Footnote"))
  1279. (format
  1280. "<text:note text:id=\"%s\" text:note-class=\"%s\">%s</text:note>"
  1281. id note-class
  1282. (concat
  1283. (format "<text:note-citation>%s</text:note-citation>" n)
  1284. (format "<text:note-body>%s</text:note-body>" def)))))))
  1285. (--format-footnote-reference
  1286. (function
  1287. (lambda (n)
  1288. (setq n (format "%d" n))
  1289. (let ((note-class "footnote")
  1290. (ref-format "text")
  1291. (ref-name (concat "fn" n)))
  1292. (format
  1293. "<text:span text:style-name=\"%s\">%s</text:span>"
  1294. "OrgSuperscript"
  1295. (format "<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:note-ref>"
  1296. note-class ref-format ref-name n)))))))
  1297. (concat
  1298. ;; Insert separator between two footnotes in a row.
  1299. (let ((prev (org-export-get-previous-element footnote-reference info)))
  1300. (and (eq (org-element-type prev) 'footnote-reference)
  1301. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1302. "OrgSuperscript" ",")))
  1303. ;; Trancode footnote reference.
  1304. (let ((n (org-export-get-footnote-number footnote-reference info)))
  1305. (cond
  1306. ((not (org-export-footnote-first-reference-p footnote-reference info))
  1307. (funcall --format-footnote-reference n))
  1308. ;; Inline definitions are secondary strings.
  1309. ;; Non-inline footnotes definitions are full Org data.
  1310. (t
  1311. (let* ((raw (org-export-get-footnote-definition footnote-reference
  1312. info))
  1313. (def (let ((def (org-trim (org-export-data raw info))))
  1314. (if (eq (org-element-type raw) 'org-data) def
  1315. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1316. "Footnote" def)))))
  1317. (funcall --format-footnote-definition n def))))))))
  1318. ;;;; Headline
  1319. (defun* org-e-odt-format-headline
  1320. (todo todo-type priority text tags
  1321. &key level section-number headline-label &allow-other-keys)
  1322. (concat
  1323. ;; Todo.
  1324. (and todo
  1325. (concat
  1326. (let ((style (if (member todo org-done-keywords) "OrgDone" "OrgTodo")))
  1327. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1328. style todo)) " "))
  1329. ;; Title.
  1330. text
  1331. ;; Tags.
  1332. (and tags
  1333. (concat "<text:tab/>"
  1334. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1335. "OrgTag" (mapconcat 'org-trim tags " : "))))))
  1336. (defun org-e-odt-format-headline--wrap (headline info
  1337. &optional format-function
  1338. &rest extra-keys)
  1339. "Transcode an HEADLINE element from Org to ODT.
  1340. CONTENTS holds the contents of the headline. INFO is a plist
  1341. holding contextual information."
  1342. (let* ((level (+ (org-export-get-relative-level headline info)))
  1343. (headline-number (org-export-get-headline-number headline info))
  1344. (section-number (and (org-export-numbered-headline-p headline info)
  1345. (mapconcat 'number-to-string
  1346. headline-number ".")))
  1347. (todo (and (plist-get info :with-todo-keywords)
  1348. (let ((todo (org-element-property :todo-keyword headline)))
  1349. (and todo (org-export-data todo info)))))
  1350. (todo-type (and todo (org-element-property :todo-type headline)))
  1351. (priority (and (plist-get info :with-priority)
  1352. (org-element-property :priority headline)))
  1353. (text (org-export-data (org-element-property :title headline) info))
  1354. (tags (and (plist-get info :with-tags)
  1355. (org-export-get-tags headline info)))
  1356. (headline-label (concat "sec-" (mapconcat 'number-to-string
  1357. headline-number "-")))
  1358. (format-function (cond
  1359. ((functionp format-function) format-function)
  1360. ((functionp org-e-odt-format-headline-function)
  1361. (function*
  1362. (lambda (todo todo-type priority text tags
  1363. &allow-other-keys)
  1364. (funcall org-e-odt-format-headline-function
  1365. todo todo-type priority text tags))))
  1366. (t 'org-e-odt-format-headline))))
  1367. (apply format-function
  1368. todo todo-type priority text tags
  1369. :headline-label headline-label :level level
  1370. :section-number section-number extra-keys)))
  1371. (defun org-e-odt-begin-plain-list (ltype &optional continue-numbering)
  1372. (unless (member ltype '(ordered unordered descriptive))
  1373. (error "Unknown list type: %s" ltype))
  1374. (let ((style-name (assoc-default ltype
  1375. '((ordered . "OrgNumberedList")
  1376. (unordered . "OrgBulletedList")
  1377. (descriptive . "OrgDescriptionList")))))
  1378. (format "<text:list text:style-name=\"%s\" text:continue-numbering=\"%s\">"
  1379. style-name (if continue-numbering "true" "false"))))
  1380. (defun org-e-odt-headline (headline contents info)
  1381. "Transcode an HEADLINE element from Org to ODT.
  1382. CONTENTS holds the contents of the headline. INFO is a plist
  1383. holding contextual information."
  1384. (let* ((numberedp (org-export-numbered-headline-p headline info))
  1385. ;; Get level relative to current parsed data.
  1386. (level (org-export-get-relative-level headline info))
  1387. (text (org-export-data (org-element-property :title headline) info))
  1388. ;; Create the headline text.
  1389. (full-text (org-e-odt-format-headline--wrap headline info)))
  1390. (cond
  1391. ;; Case 1: This is a footnote section: ignore it.
  1392. ((org-element-property :footnote-section-p headline) nil)
  1393. ;; Case 2. This is a deep sub-tree: export it as a list item.
  1394. ;; Also export as items headlines for which no section
  1395. ;; format has been found.
  1396. ;; FIXME
  1397. ;; ((org-export-low-level-p headline info)
  1398. ;; ;; Build the real contents of the sub-tree.
  1399. ;; (let* ((type (if numberedp 'unordered 'unordered)) ; FIXME
  1400. ;; (itemized-body (org-e-odt-format-list-item
  1401. ;; contents type nil nil full-text)))
  1402. ;; (concat
  1403. ;; (and (org-export-first-sibling-p headline info)
  1404. ;; (org-e-odt-begin-plain-list type))
  1405. ;; itemized-body
  1406. ;; (and (org-export-last-sibling-p headline info)
  1407. ;; "</text:list>"))))
  1408. ;; Case 3. Standard headline. Export it as a section.
  1409. (t
  1410. (let* ((extra-ids (list (org-element-property :custom-id headline)
  1411. (org-element-property :id headline)))
  1412. (extra-ids nil) ; FIXME
  1413. (id (concat "sec-" (mapconcat 'number-to-string
  1414. (org-export-get-headline-number
  1415. headline info) "-"))))
  1416. (concat
  1417. (format
  1418. "\n<text:h text:style-name=\"%s\" text:outline-level=\"%s\">%s%s</text:h>"
  1419. (format "Heading_20_%s" level)
  1420. level
  1421. ;; Extra targets.
  1422. (mapconcat (lambda (x)
  1423. (when x
  1424. (let ((x (if (org-uuidgen-p x) (concat "ID-" x) x)))
  1425. (org-e-odt-format-target
  1426. "" (org-export-solidify-link-text x)))))
  1427. extra-ids "")
  1428. ;; Title.
  1429. (org-e-odt-format-target full-text id))
  1430. contents))))))
  1431. ;;;; Horizontal Rule
  1432. (defun org-e-odt-horizontal-rule (horizontal-rule contents info)
  1433. "Transcode an HORIZONTAL-RULE object from Org to ODT.
  1434. CONTENTS is nil. INFO is a plist holding contextual information."
  1435. (org-e-odt--wrap-label
  1436. horizontal-rule
  1437. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1438. "Horizontal_20_Line" "")))
  1439. ;;;; Inline Babel Call
  1440. ;; Inline Babel Calls are ignored.
  1441. ;;;; Inline Src Block
  1442. (defun org-e-odt--find-verb-separator (s)
  1443. "Return a character not used in string S.
  1444. This is used to choose a separator for constructs like \\verb."
  1445. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  1446. (loop for c across ll
  1447. when (not (string-match (regexp-quote (char-to-string c)) s))
  1448. return (char-to-string c))))
  1449. (defun org-e-odt-inline-src-block (inline-src-block contents info)
  1450. "Transcode an INLINE-SRC-BLOCK element from Org to ODT.
  1451. CONTENTS holds the contents of the item. INFO is a plist holding
  1452. contextual information."
  1453. (let* ((org-lang (org-element-property :language inline-src-block))
  1454. (code (org-element-property :value inline-src-block))
  1455. (separator (org-e-odt--find-verb-separator code)))
  1456. (error "FIXME")))
  1457. ;;;; Inlinetask
  1458. (defun org-e-odt-inlinetask (inlinetask contents info)
  1459. "Transcode an INLINETASK element from Org to ODT.
  1460. CONTENTS holds the contents of the block. INFO is a plist
  1461. holding contextual information."
  1462. (cond
  1463. ;; If `org-e-odt-format-inlinetask-function' is provided, call it
  1464. ;; with appropriate arguments.
  1465. ((functionp org-e-odt-format-inlinetask-function)
  1466. (let ((format-function
  1467. (function*
  1468. (lambda (todo todo-type priority text tags
  1469. &key contents &allow-other-keys)
  1470. (funcall org-e-odt-format-inlinetask-function
  1471. todo todo-type priority text tags contents)))))
  1472. (org-e-odt-format-headline--wrap
  1473. inlinetask info format-function :contents contents)))
  1474. ;; Otherwise, use a default template.
  1475. (t (org-e-odt--wrap-label
  1476. inlinetask
  1477. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1478. "Text_20_body"
  1479. (org-e-odt--textbox
  1480. (concat
  1481. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1482. "OrgInlineTaskHeading"
  1483. (org-e-odt-format-headline--wrap
  1484. inlinetask info))
  1485. contents)
  1486. nil nil "OrgInlineTaskFrame" " style:rel-width=\"100%\""))))))
  1487. ;;;; Italic
  1488. (defun org-e-odt-italic (italic contents info)
  1489. "Transcode ITALIC from Org to ODT.
  1490. CONTENTS is the text with italic markup. INFO is a plist holding
  1491. contextual information."
  1492. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1493. "Emphasis" contents))
  1494. ;;;; Item
  1495. (defun org-e-odt-item (item contents info)
  1496. "Transcode an ITEM element from Org to ODT.
  1497. CONTENTS holds the contents of the item. INFO is a plist holding
  1498. contextual information."
  1499. (let* ((plain-list (org-export-get-parent item))
  1500. (type (org-element-property :type plain-list))
  1501. (counter (org-element-property :counter item))
  1502. (tag (let ((tag (org-element-property :tag item)))
  1503. (and tag
  1504. (concat (org-e-odt--checkbox item)
  1505. (org-export-data tag info))))))
  1506. (case type
  1507. ((ordered unordered)
  1508. (format "\n<text:list-item>\n%s\n%s"
  1509. contents
  1510. (let* ((--element-has-a-table-p
  1511. (function
  1512. (lambda (element info)
  1513. (loop for el in (org-element-contents element)
  1514. thereis (eq (org-element-type el) 'table))))))
  1515. (cond
  1516. ((funcall --element-has-a-table-p item info)
  1517. "</text:list-header>")
  1518. (t "</text:list-item>")))))
  1519. (descriptive
  1520. (concat
  1521. (let ((term (or tag "(no term)")))
  1522. (concat
  1523. (format "\n<text:list-item>\n%s\n</text:list-item>"
  1524. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1525. "Text_20_body_20_bold" term))
  1526. (format
  1527. "\n<text:list-item>\n%s\n</text:list-item>"
  1528. (format "\n<text:list text:style-name=\"%s\" %s>\n%s\n</text:list>"
  1529. "OrgDescriptionList"
  1530. "text:continue-numbering=\"false\""
  1531. (format "\n<text:list-item>\n%s\n</text:list-item>"
  1532. contents)))))))
  1533. (t (error "Unknown list type: %S" type)))))
  1534. ;;;; Keyword
  1535. (defun org-e-odt-keyword (keyword contents info)
  1536. "Transcode a KEYWORD element from Org to ODT.
  1537. CONTENTS is nil. INFO is a plist holding contextual information."
  1538. (let ((key (org-element-property :key keyword))
  1539. (value (org-element-property :value keyword)))
  1540. (cond
  1541. ((string= key "LATEX") value)
  1542. ((string= key "INDEX") (format "\\index{%s}" value))
  1543. ((string= key "TARGET") nil ; FIXME
  1544. ;; (format "\\label{%s}" (org-export-solidify-link-text value))
  1545. )
  1546. ((string= key "toc")
  1547. (let ((value (downcase value)))
  1548. (cond
  1549. ((string-match "\\<headlines\\>" value)
  1550. (let ((depth (or (and (string-match "[0-9]+" value)
  1551. (string-to-number (match-string 0 value)))
  1552. (plist-get info :with-toc))))
  1553. (when (wholenump depth) (org-e-odt-toc depth info))))
  1554. ((string= "tables" value) "FIXME")
  1555. ((string= "figures" value) "FIXME")
  1556. ((string= "listings" value)
  1557. (cond
  1558. ;; At the moment, src blocks with a caption are wrapped
  1559. ;; into a figure environment.
  1560. (t "FIXME")))))))))
  1561. ;;;; Latex Environment
  1562. (eval-after-load 'org-e-odt
  1563. '(ad-deactivate 'org-format-latex-as-mathml))
  1564. ;; (defadvice org-format-latex-as-mathml ; FIXME
  1565. ;; (after org-e-odt-protect-latex-fragment activate)
  1566. ;; "Encode LaTeX fragment as XML.
  1567. ;; Do this when translation to MathML fails."
  1568. ;; (when (or (not (> (length ad-return-value) 0))
  1569. ;; (get-text-property 0 'org-protected ad-return-value))
  1570. ;; (setq ad-return-value
  1571. ;; (org-propertize (org-e-odt-encode-plain-text (ad-get-arg 0))
  1572. ;; 'org-protected t))))
  1573. (defun org-e-odt-format-latex (latex-frag processing-type info)
  1574. (let* ((prefix (case processing-type
  1575. (dvipng "ltxpng/")
  1576. (mathml "ltxmathml/")))
  1577. (input-file (plist-get info :input-file))
  1578. (cache-subdir
  1579. (concat prefix (file-name-sans-extension
  1580. (file-name-nondirectory input-file))))
  1581. (cache-dir (file-name-directory input-file))
  1582. (display-msg (case processing-type
  1583. (dvipng "Creating LaTeX Image...")
  1584. (mathml "Creating MathML snippet..."))))
  1585. (with-temp-buffer
  1586. (insert latex-frag)
  1587. (org-format-latex cache-subdir cache-dir nil display-msg
  1588. nil nil processing-type)
  1589. (buffer-string))))
  1590. (defun org-e-odt-latex-environment (latex-environment contents info)
  1591. "Transcode a LATEX-ENVIRONMENT element from Org to ODT.
  1592. CONTENTS is nil. INFO is a plist holding contextual information."
  1593. (org-e-odt--wrap-label
  1594. latex-environment
  1595. (let* ((latex-frag
  1596. (org-remove-indentation
  1597. (org-element-property :value latex-environment)))
  1598. (processing-type (plist-get info :LaTeX-fragments))
  1599. (caption (org-element-property :caption latex-environment))
  1600. (short-caption (and (cdr caption)
  1601. (org-export-data (cdr caption) info)))
  1602. (caption (and (car caption) (org-export-data (car caption) info)))
  1603. (label (org-element-property :name latex-environment))
  1604. (attr nil) ; FIXME
  1605. (label (org-element-property :name latex-environment)))
  1606. (when (memq processing-type '(t mathjax))
  1607. (unless (and (fboundp 'org-format-latex-mathml-available-p)
  1608. (org-format-latex-mathml-available-p))
  1609. (message "LaTeX to MathML converter not available. Trying dvinpng...")
  1610. (setq processing-type 'dvipng)))
  1611. (when (eq processing-type 'dvipng)
  1612. (unless (and (org-check-external-command "latex" "" t)
  1613. (org-check-external-command "dvipng" "" t))
  1614. (message "LaTeX to PNG converter not available. Using verbatim.")
  1615. (setq processing-type 'verbatim)))
  1616. (case processing-type
  1617. ((t mathjax)
  1618. (org-e-odt-format-formula latex-environment info))
  1619. (dvipng
  1620. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1621. "Text_20_body"
  1622. (org-e-odt-link--inline-image latex-environment info)))
  1623. (t (org-e-odt-do-format-code latex-frag))))))
  1624. ;;;; Latex Fragment
  1625. ;; (when latex-frag ; FIXME
  1626. ;; (setq href (org-propertize href :title "LaTeX Fragment"
  1627. ;; :description latex-frag)))
  1628. ;; handle verbatim
  1629. ;; provide descriptions
  1630. (defun org-e-odt-latex-fragment (latex-fragment contents info)
  1631. "Transcode a LATEX-FRAGMENT object from Org to ODT.
  1632. CONTENTS is nil. INFO is a plist holding contextual information."
  1633. (let* ((latex-frag (org-element-property :value latex-fragment))
  1634. (processing-type (plist-get info :LaTeX-fragments)))
  1635. (cond
  1636. ((member processing-type '(t mathjax))
  1637. (org-e-odt-format-formula latex-fragment info))
  1638. ((eq processing-type 'dvipng)
  1639. (org-e-odt-link--inline-image latex-fragment info))
  1640. (t (org-e-odt-encode-plain-text latex-frag t)))))
  1641. ;;;; Line Break
  1642. (defun org-e-odt-line-break (line-break contents info)
  1643. "Transcode a LINE-BREAK object from Org to ODT.
  1644. CONTENTS is nil. INFO is a plist holding contextual information."
  1645. "<text:line-break/>\n")
  1646. ;;;; Link
  1647. ;;;; Links :: Generic
  1648. (defun org-e-odt-format-link (desc href &optional suppress-xref)
  1649. (cond
  1650. ((and (= (string-to-char href) ?#) (not suppress-xref))
  1651. (setq href (substring href 1))
  1652. (let ((xref-format "text"))
  1653. (when (numberp desc)
  1654. (setq desc (format "%d" desc) xref-format "number"))
  1655. (when (listp desc)
  1656. (setq desc (mapconcat 'number-to-string desc ".") xref-format "chapter"))
  1657. (setq href (concat org-e-odt-bookmark-prefix href))
  1658. (format
  1659. "<text:bookmark-ref text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:bookmark-ref>"
  1660. xref-format href desc)))
  1661. ;; (org-lparse-link-description-is-image
  1662. ;; (format "\n<draw:a xlink:type=\"simple\" xlink:href=\"%s\">\n%s\n</draw:a>"
  1663. ;; href desc))
  1664. (t (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
  1665. href desc))))
  1666. (defun org-e-odt-format-internal-link (text href)
  1667. (org-e-odt-format-link text (concat "#" href)))
  1668. ;;;; Links :: Label references
  1669. (defun org-e-odt-enumerate-element (element info &optional predicate n)
  1670. (let* ((--numbered-parent-headline-at-<=-n
  1671. (function
  1672. (lambda (element n info)
  1673. (loop for x in (org-export-get-genealogy element)
  1674. thereis (and (eq (org-element-type x) 'headline)
  1675. (<= (org-export-get-relative-level x info) n)
  1676. (org-export-numbered-headline-p x info)
  1677. x)))))
  1678. (--enumerate
  1679. (function
  1680. (lambda (element scope info &optional predicate)
  1681. (let ((counter 0))
  1682. (org-element-map
  1683. (or scope (plist-get info :parse-tree))
  1684. (org-element-type element)
  1685. (lambda (el)
  1686. (and (or (not predicate) (funcall predicate el info))
  1687. (incf counter)
  1688. (eq element el)
  1689. counter))
  1690. info 'first-match)))))
  1691. (scope (funcall --numbered-parent-headline-at-<=-n
  1692. element (or n org-e-odt-display-outline-level) info))
  1693. (ordinal (funcall --enumerate element scope info predicate))
  1694. (tag
  1695. (concat
  1696. ;; Section number.
  1697. (and scope
  1698. (mapconcat 'number-to-string
  1699. (org-export-get-headline-number scope info) "."))
  1700. ;; Separator.
  1701. (and scope ".")
  1702. ;; Ordinal.
  1703. (number-to-string ordinal))))
  1704. tag))
  1705. (defun org-e-odt-format-label (element info op)
  1706. (let* ((caption-from
  1707. (case (org-element-type element)
  1708. (link (org-export-get-parent-element element))
  1709. (t element)))
  1710. ;; get label and caption.
  1711. (label (org-element-property :name caption-from))
  1712. (caption (org-element-property :caption caption-from))
  1713. (short-caption (cdr caption))
  1714. ;; transcode captions.
  1715. (caption (and (car caption) (org-export-data (car caption) info)))
  1716. (short-caption (and short-caption
  1717. (org-export-data short-caption info))))
  1718. (when (or label caption)
  1719. (let* ((default-category
  1720. (cond
  1721. ((eq (org-element-type element) 'table)
  1722. "__Table__")
  1723. ((org-e-odt-standalone-image-p element info)
  1724. "__Figure__")
  1725. ((member (org-element-type element)
  1726. '(latex-environment latex-fragment))
  1727. (let ((processing-type (plist-get info :LaTeX-fragments)))
  1728. (cond
  1729. ((eq processing-type 'dvipng) "__DvipngImage__")
  1730. ((eq processing-type 'mathjax) "__MathFormula__")
  1731. ((eq processing-type 't) "__MathFormula__")
  1732. (t (error "Handle LaTeX:verbatim")))))
  1733. ((eq (org-element-type element) 'src-block)
  1734. "__Listing__")
  1735. (t (error "Handle enumeration of %S" element))))
  1736. (predicate
  1737. (cond
  1738. ((member (org-element-type element)
  1739. '(table latex-environment src-block))
  1740. nil)
  1741. ((org-e-odt-standalone-image-p element info)
  1742. 'org-e-odt-standalone-image-p)
  1743. (t (error "Handle enumeration of %S" element))))
  1744. (seqno (org-e-odt-enumerate-element
  1745. element info predicate)) ; FIXME
  1746. ;; handle label props.
  1747. (label-props (assoc default-category org-e-odt-category-map-alist))
  1748. ;; identify opendocument counter
  1749. (counter (nth 1 label-props))
  1750. ;; identify label style
  1751. (label-style (nth 2 label-props))
  1752. ;; retrieve localized category sting
  1753. (category (org-export-translate (nth 3 label-props) :utf-8 info)))
  1754. (case op
  1755. (definition
  1756. ;; assign an internal label, if user has not provided one
  1757. (setq label (or label (format "%s-%s" default-category seqno)))
  1758. (setq label (org-export-solidify-link-text label))
  1759. (cons
  1760. (format-spec
  1761. (cadr (assoc-string label-style org-e-odt-label-styles t))
  1762. `((?e . ,category)
  1763. (?n . ,(format
  1764. "<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">%s</text:sequence>"
  1765. label counter counter seqno))
  1766. (?c . ,(or caption ""))))
  1767. short-caption))
  1768. (reference
  1769. (assert label)
  1770. (setq label (org-export-solidify-link-text label))
  1771. (let* ((fmt (cddr (assoc-string label-style org-e-odt-label-styles t)))
  1772. (fmt1 (car fmt))
  1773. (fmt2 (cadr fmt)))
  1774. (format "<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:sequence-ref>"
  1775. fmt1 label (format-spec fmt2 `((?e . ,category)
  1776. (?n . ,seqno))))))
  1777. (t (error "Unknow %S on label" op)))))))
  1778. ;;;; Links :: Embedded images
  1779. (defun org-e-odt-copy-image-file (path)
  1780. "Returns the internal name of the file"
  1781. (let* ((image-type (file-name-extension path))
  1782. (media-type (format "image/%s" image-type))
  1783. (target-dir "Images/")
  1784. (target-file
  1785. (format "%s%04d.%s" target-dir
  1786. (incf org-e-odt-embedded-images-count) image-type)))
  1787. (message "Embedding %s as %s ..."
  1788. (substring-no-properties path) target-file)
  1789. (when (= 1 org-e-odt-embedded-images-count)
  1790. (make-directory (concat org-e-odt-zip-dir target-dir))
  1791. (org-e-odt-create-manifest-file-entry "" target-dir))
  1792. (copy-file path (concat org-e-odt-zip-dir target-file) 'overwrite)
  1793. (org-e-odt-create-manifest-file-entry media-type target-file)
  1794. target-file))
  1795. (defun org-e-odt-image-size-from-file (file &optional user-width
  1796. user-height scale dpi embed-as)
  1797. (let* ((--pixels-to-cms
  1798. (function (lambda (pixels dpi)
  1799. (let ((cms-per-inch 2.54)
  1800. (inches (/ pixels dpi)))
  1801. (* cms-per-inch inches)))))
  1802. (--size-in-cms
  1803. (function
  1804. (lambda (size-in-pixels dpi)
  1805. (and size-in-pixels
  1806. (cons (funcall --pixels-to-cms (car size-in-pixels) dpi)
  1807. (funcall --pixels-to-cms (cdr size-in-pixels) dpi))))))
  1808. (dpi (or dpi org-e-odt-pixels-per-inch))
  1809. (anchor-type (or embed-as "paragraph"))
  1810. (user-width (and (not scale) user-width))
  1811. (user-height (and (not scale) user-height))
  1812. (size
  1813. (and
  1814. (not (and user-height user-width))
  1815. (or
  1816. ;; Use Imagemagick.
  1817. (and (executable-find "identify")
  1818. (let ((size-in-pixels
  1819. (let ((dim (shell-command-to-string
  1820. (format "identify -format \"%%w:%%h\" \"%s\""
  1821. file))))
  1822. (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
  1823. (cons (string-to-number (match-string 1 dim))
  1824. (string-to-number (match-string 2 dim)))))))
  1825. (funcall --size-in-cms size-in-pixels dpi)))
  1826. ;; Use Emacs.
  1827. (let ((size-in-pixels
  1828. (ignore-errors ; Emacs could be in batch mode
  1829. (clear-image-cache)
  1830. (image-size (create-image file) 'pixels))))
  1831. (funcall --size-in-cms size-in-pixels dpi))
  1832. ;; Use hard-coded values.
  1833. (cdr (assoc-string anchor-type
  1834. org-e-odt-default-image-sizes-alist))
  1835. ;; Error out.
  1836. (error "Cannot determine image size, aborting"))))
  1837. (width (car size)) (height (cdr size)))
  1838. (cond
  1839. (scale
  1840. (setq width (* width scale) height (* height scale)))
  1841. ((and user-height user-width)
  1842. (setq width user-width height user-height))
  1843. (user-height
  1844. (setq width (* user-height (/ width height)) height user-height))
  1845. (user-width
  1846. (setq height (* user-width (/ height width)) width user-width))
  1847. (t (ignore)))
  1848. ;; ensure that an embedded image fits comfortably within a page
  1849. (let ((max-width (car org-e-odt-max-image-size))
  1850. (max-height (cdr org-e-odt-max-image-size)))
  1851. (when (or (> width max-width) (> height max-height))
  1852. (let* ((scale1 (/ max-width width))
  1853. (scale2 (/ max-height height))
  1854. (scale (min scale1 scale2)))
  1855. (setq width (* scale width) height (* scale height)))))
  1856. (cons width height)))
  1857. ;;;; Links :: Math formula
  1858. (defun org-e-odt-format-formula (element info)
  1859. (let* ((src (cond
  1860. ((eq (org-element-type element) 'link) ; FIXME
  1861. (let* ((type (org-element-property :type element))
  1862. (raw-path (org-element-property :path element)))
  1863. (cond
  1864. ((file-name-absolute-p raw-path)
  1865. (expand-file-name raw-path))
  1866. (t raw-path))))
  1867. ((member (org-element-type element)
  1868. '(latex-fragment latex-environment))
  1869. (let* ((latex-frag (org-remove-indentation
  1870. (org-element-property :value element)))
  1871. (formula-link (org-e-odt-format-latex
  1872. latex-frag 'mathml info)))
  1873. (and formula-link
  1874. (string-match "file:\\([^]]*\\)" formula-link)
  1875. (match-string 1 formula-link))))
  1876. (t (error "what is this?"))))
  1877. (full-src (if (file-name-absolute-p src) src
  1878. (expand-file-name src (file-name-directory
  1879. (plist-get info :input-file)))))
  1880. (caption-from
  1881. (case (org-element-type element)
  1882. (link (org-export-get-parent-element element))
  1883. (t element)))
  1884. (captions (org-e-odt-format-label caption-from info 'definition))
  1885. (caption (car captions))
  1886. (href
  1887. (format "\n<draw:object %s xlink:href=\"%s\" xlink:type=\"simple\"/>"
  1888. " xlink:show=\"embed\" xlink:actuate=\"onLoad\""
  1889. (file-name-directory (org-e-odt-copy-formula-file full-src))))
  1890. (embed-as (if caption 'paragraph 'character))
  1891. width height)
  1892. (cond
  1893. ((eq embed-as 'character)
  1894. (org-e-odt-format-entity "InlineFormula" href width height))
  1895. (t
  1896. (let* ((equation (org-e-odt-format-entity
  1897. "CaptionedDisplayFormula" href width height captions))
  1898. (label
  1899. (let* ((org-e-odt-category-map-alist
  1900. '(("__Table__" "Table" "value")
  1901. ("__Figure__" "Illustration" "value")
  1902. ("__MathFormula__" "Text" "math-label")
  1903. ("__DvipngImage__" "Equation" "value")
  1904. ("__Listing__" "Listing" "value"))))
  1905. (car (org-e-odt-format-label caption-from info 'definition))))
  1906. (formula-tree
  1907. (org-e-odt--adopt-elements
  1908. `(table (:type org :attr_odt (":style \"OrgEquation\"")))
  1909. (org-e-odt--adopt-elements
  1910. `(table-row (:type standard))
  1911. `(table-cell nil "<c8>") `(table-cell nil "<c1>"))
  1912. (org-e-odt--adopt-elements
  1913. `(table-row (:type standard))
  1914. (org-e-odt--adopt-elements
  1915. `(table-cell nil) `(export-block
  1916. (:type "ODT" :value ,equation)))
  1917. (org-e-odt--adopt-elements
  1918. `(table-cell nil) `(export-block
  1919. (:type "ODT" :value ,label))))))
  1920. (formula-info
  1921. (org-export-collect-tree-properties
  1922. formula-tree (org-export-get-environment 'e-odt))))
  1923. (org-export-data formula-tree formula-info))))))
  1924. (defun org-e-odt-copy-formula-file (src-file)
  1925. "Returns the internal name of the file"
  1926. (let* ((target-dir (format "Formula-%04d/"
  1927. (incf org-e-odt-embedded-formulas-count)))
  1928. (target-file (concat target-dir "content.xml")))
  1929. ;; Create a directory for holding formula file. Also enter it in
  1930. ;; to manifest.
  1931. (make-directory (concat org-e-odt-zip-dir target-dir))
  1932. (org-e-odt-create-manifest-file-entry
  1933. "application/vnd.oasis.opendocument.formula" target-dir "1.2")
  1934. ;; Copy over the formula file from user directory to zip
  1935. ;; directory.
  1936. (message "Embedding %s as %s ..." src-file target-file)
  1937. (let ((case-fold-search nil))
  1938. (cond
  1939. ;; Case 1: Mathml.
  1940. ((string-match "\\.\\(mathml\\|mml\\)\\'" src-file)
  1941. (copy-file src-file (concat org-e-odt-zip-dir target-file) 'overwrite))
  1942. ;; Case 2: OpenDocument formula.
  1943. ((string-match "\\.odf\\'" src-file)
  1944. (org-e-odt--zip-extract src-file "content.xml"
  1945. (concat org-e-odt-zip-dir target-dir)))
  1946. (t (error "%s is not a formula file" src-file))))
  1947. ;; Enter the formula file in to manifest.
  1948. (org-e-odt-create-manifest-file-entry "text/xml" target-file)
  1949. target-file))
  1950. ;;;; Targets
  1951. (defun org-e-odt-format-target (text id)
  1952. (let ((name (concat org-e-odt-bookmark-prefix id)))
  1953. (concat
  1954. (and id (format "\n<text:bookmark-start text:name=\"%s\"/>" name))
  1955. (concat (and id (format "\n<text:bookmark text:name=\"%s\"/>" id)) text)
  1956. (and id (format "\n<text:bookmark-end text:name=\"%s\"/>" name)))))
  1957. (defun org-e-odt-link--inline-image (element info)
  1958. "Return HTML code for an inline image.
  1959. LINK is the link pointing to the inline image. INFO is a plist
  1960. used as a communication channel."
  1961. (let* ((src (cond
  1962. ((eq (org-element-type element) 'link)
  1963. (let* ((type (org-element-property :type element))
  1964. (raw-path (org-element-property :path element)))
  1965. (cond ((member type '("http" "https"))
  1966. (concat type ":" raw-path))
  1967. ((file-name-absolute-p raw-path)
  1968. (expand-file-name raw-path))
  1969. (t raw-path))))
  1970. ((member (org-element-type element)
  1971. '(latex-fragment latex-environment))
  1972. (let* ((latex-frag (org-remove-indentation
  1973. (org-element-property :value element)))
  1974. (formula-link (org-e-odt-format-latex
  1975. latex-frag 'dvipng info)))
  1976. (and formula-link
  1977. (string-match "file:\\([^]]*\\)" formula-link)
  1978. (match-string 1 formula-link))))
  1979. (t (error "what is this?"))))
  1980. (src-expanded (if (file-name-absolute-p src) src
  1981. (expand-file-name src (file-name-directory
  1982. (plist-get info :input-file)))))
  1983. (href (format
  1984. "\n<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>"
  1985. (org-e-odt-copy-image-file src-expanded)))
  1986. ;; extract attributes from #+ATTR_ODT line.
  1987. (attr-from (case (org-element-type element)
  1988. (link (org-export-get-parent-element element))
  1989. (t element)))
  1990. ;; convert attributes to a plist.
  1991. (attr-plist (org-export-read-attribute :attr_odt attr-from))
  1992. ;; handle `:anchor', `:style' and `:attributes' properties.
  1993. (user-frame-anchor
  1994. (car (assoc-string (plist-get attr-plist :anchor)
  1995. '(("as-char") ("paragraph") ("page")) t)))
  1996. (user-frame-style
  1997. (and user-frame-anchor (plist-get attr-plist :style)))
  1998. (user-frame-attrs
  1999. (and user-frame-anchor (plist-get attr-plist :attributes)))
  2000. (user-frame-params
  2001. (list user-frame-style user-frame-attrs user-frame-anchor))
  2002. ;; (embed-as (or embed-as user-frame-anchor "paragraph"))
  2003. ;; extrac
  2004. ;; handle `:width', `:height' and `:scale' properties.
  2005. (size (org-e-odt-image-size-from-file
  2006. src-expanded (plist-get attr-plist :width)
  2007. (plist-get attr-plist :height)
  2008. (plist-get attr-plist :scale) nil ;; embed-as
  2009. "paragraph" ; FIXME
  2010. ))
  2011. (width (car size)) (height (cdr size))
  2012. (embed-as
  2013. (case (org-element-type element)
  2014. ((org-e-odt-standalone-image-p element info) "paragraph")
  2015. (latex-fragment "as-char")
  2016. (latex-environment "paragraph")
  2017. (t "paragraph")))
  2018. (captions (org-e-odt-format-label element info 'definition))
  2019. (caption (car captions)) (short-caption (cdr captions))
  2020. (entity (concat (and caption "Captioned") embed-as "Image")))
  2021. (org-e-odt-format-entity entity href width height
  2022. captions user-frame-params )))
  2023. (defun org-e-odt-format-entity (entity href width height &optional
  2024. captions user-frame-params)
  2025. (let* ((caption (car captions)) (short-caption (cdr captions))
  2026. (entity-style (assoc-string entity org-e-odt-entity-frame-styles t))
  2027. default-frame-params frame-params
  2028. (--merge-frame-params
  2029. (function
  2030. (lambda (default-frame-params user-frame-params)
  2031. (if (not user-frame-params) default-frame-params
  2032. (assert (= (length default-frame-params) 3))
  2033. (assert (= (length user-frame-params) 3))
  2034. (loop for user-frame-param in user-frame-params
  2035. for default-frame-param in default-frame-params
  2036. collect (or user-frame-param default-frame-param)))))))
  2037. (cond
  2038. ((not caption)
  2039. (setq default-frame-params (nth 2 entity-style))
  2040. (setq frame-params (funcall --merge-frame-params
  2041. default-frame-params user-frame-params))
  2042. (apply 'org-e-odt--frame href width height frame-params))
  2043. (t
  2044. (setq default-frame-params (nth 3 entity-style))
  2045. (setq frame-params (funcall --merge-frame-params
  2046. default-frame-params user-frame-params))
  2047. (apply 'org-e-odt--textbox
  2048. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2049. "Illustration"
  2050. (concat
  2051. (apply 'org-e-odt--frame href width height
  2052. (let ((entity-style-1 (copy-sequence
  2053. (nth 2 entity-style))))
  2054. (setcar (cdr entity-style-1)
  2055. (concat
  2056. (cadr entity-style-1)
  2057. (and short-caption
  2058. (format " draw:name=\"%s\" "
  2059. short-caption))))
  2060. entity-style-1))
  2061. caption))
  2062. width height frame-params)))))
  2063. (defun org-e-odt-standalone-image-p (element info)
  2064. "Test if ELEMENT is a standalone image for the purpose ODT export.
  2065. INFO is a plist holding contextual information.
  2066. Return non-nil, if ELEMENT is of type paragraph and it's sole
  2067. content, save for whitespaces, is a link that qualifies as an
  2068. inline image.
  2069. Return non-nil, if ELEMENT is of type link and it's containing
  2070. paragraph has no other content save for leading and trailing
  2071. whitespaces.
  2072. Return nil, otherwise.
  2073. Bind `org-e-odt-standalone-image-predicate' to constrain
  2074. paragraph further. For example, to check for only captioned
  2075. standalone images, do the following.
  2076. \(setq org-e-odt-standalone-image-predicate
  2077. \(lambda \(paragraph\)
  2078. \(org-element-property :caption paragraph\)\)\)
  2079. "
  2080. (let ((--standalone-image-predicate
  2081. (function (lambda (paragraph)
  2082. (or (org-element-property :caption paragraph)
  2083. (org-element-property :name paragraph)))))
  2084. (paragraph (case (org-element-type element)
  2085. (paragraph element)
  2086. (link (and (org-export-inline-image-p
  2087. element org-e-odt-inline-image-rules)
  2088. (org-export-get-parent element)))
  2089. (t nil))))
  2090. (when paragraph
  2091. (assert (eq (org-element-type paragraph) 'paragraph))
  2092. (when (funcall --standalone-image-predicate paragraph)
  2093. (let ((contents (org-element-contents paragraph)))
  2094. (loop for x in contents
  2095. with inline-image-count = 0
  2096. always (cond
  2097. ((eq (org-element-type x) 'plain-text)
  2098. (not (org-string-nw-p x)))
  2099. ((eq (org-element-type x) 'link)
  2100. (when (org-export-inline-image-p
  2101. x org-e-odt-inline-image-rules)
  2102. (= (incf inline-image-count) 1)))
  2103. (t nil))))))))
  2104. (defun org-e-odt-link (link desc info)
  2105. "Transcode a LINK object from Org to ODT.
  2106. DESC is the description part of the link, or the empty string.
  2107. INFO is a plist holding contextual information. See
  2108. `org-export-data'."
  2109. (let* ((type (org-element-property :type link))
  2110. (raw-path (org-element-property :path link))
  2111. ;; Ensure DESC really exists, or set it to nil.
  2112. (desc (and (not (string= desc "")) desc))
  2113. (imagep (org-export-inline-image-p
  2114. link org-e-odt-inline-image-rules))
  2115. (path (cond
  2116. ((member type '("http" "https" "ftp" "mailto"))
  2117. (concat type ":" raw-path))
  2118. ((string= type "file")
  2119. (when (string-match "\\(.+\\)::.+" raw-path)
  2120. (setq raw-path (match-string 1 raw-path)))
  2121. (if (file-name-absolute-p raw-path)
  2122. (concat "file://" (expand-file-name raw-path))
  2123. (concat "file://" raw-path)))
  2124. (t raw-path)))
  2125. protocol)
  2126. (cond
  2127. ;; Image file.
  2128. ((and (not desc) (org-export-inline-image-p
  2129. link org-e-odt-inline-image-rules))
  2130. (org-e-odt-link--inline-image link info))
  2131. ;; Radio target: Transcode target's contents and use them as
  2132. ;; link's description.
  2133. ((string= type "radio")
  2134. (let ((destination (org-export-resolve-radio-link link info)))
  2135. (when destination
  2136. (org-e-odt-format-internal-link
  2137. (org-export-data (org-element-contents destination) info)
  2138. (org-export-solidify-link-text path)))))
  2139. ;; Links pointing to an headline: Find destination and build
  2140. ;; appropriate referencing command.
  2141. ((member type '("custom-id" "fuzzy" "id"))
  2142. (let ((destination (if (string= type "fuzzy")
  2143. (org-export-resolve-fuzzy-link link info)
  2144. (org-export-resolve-id-link link info))))
  2145. (case (org-element-type destination)
  2146. ;; Fuzzy link points nowhere.
  2147. ('nil
  2148. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2149. "Emphasis" (or desc (org-export-data
  2150. (org-element-property
  2151. :raw-link link) info))))
  2152. ;; Fuzzy link points to an invisible target.
  2153. (keyword nil)
  2154. ;; LINK points to an headline. Check if LINK should display
  2155. ;; section numbers.
  2156. (headline
  2157. (let* ((headline-no (org-export-get-headline-number destination info))
  2158. (label (format "sec-%s" (mapconcat 'number-to-string
  2159. headline-no "-")))
  2160. (desc
  2161. ;; Case 1: Headline is numbered and LINK has no
  2162. ;; description or LINK's description matches
  2163. ;; headline's title. Display section number.
  2164. (if (and (org-export-numbered-headline-p destination info)
  2165. (or (not desc)
  2166. (string= desc (org-element-property
  2167. :raw-value destination))))
  2168. headline-no
  2169. ;; Case 2: Either the headline is un-numbered or
  2170. ;; LINK has a custom description. Display LINK's
  2171. ;; description or headline's title.
  2172. (or desc (org-export-data (org-element-property
  2173. :title destination) info)))))
  2174. (org-e-odt-format-internal-link desc label)))
  2175. ;; Fuzzy link points to a target. Do as above.
  2176. (otherwise
  2177. ;; (unless desc
  2178. ;; (setq number (cond
  2179. ;; ((org-e-odt-standalone-image-p destination info)
  2180. ;; (org-export-get-ordinal
  2181. ;; (assoc 'link (org-element-contents destination))
  2182. ;; info 'link 'org-e-odt-standalone-image-p))
  2183. ;; (t (org-export-get-ordinal destination info))))
  2184. ;; (setq desc (when number
  2185. ;; (if (atom number) (number-to-string number)
  2186. ;; (mapconcat 'number-to-string number ".")))))
  2187. (let ((label-reference
  2188. (org-e-odt-format-label destination info 'reference)))
  2189. (assert label-reference)
  2190. label-reference)))))
  2191. ;; Coderef: replace link with the reference name or the
  2192. ;; equivalent line number.
  2193. ((string= type "coderef")
  2194. (let* ((fmt (org-export-get-coderef-format path desc))
  2195. (res (org-export-resolve-coderef path info))
  2196. (href (concat "#coderef-" path)))
  2197. (format fmt (org-e-odt-format-link res href))))
  2198. ;; Link type is handled by a special function.
  2199. ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
  2200. (funcall protocol (org-link-unescape path) desc 'odt))
  2201. ;; External link with a description part.
  2202. ((and path desc) (org-e-odt-format-link desc path))
  2203. ;; External link without a description part.
  2204. (path (org-e-odt-format-link path path))
  2205. ;; No path, only description. Try to do something useful.
  2206. (t (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2207. "Emphasis" desc)))))
  2208. ;;;; Babel Call
  2209. ;; Babel Calls are ignored.
  2210. ;;;; Macro
  2211. (defun org-e-odt-macro (macro contents info)
  2212. "Transcode a MACRO element from Org to ODT.
  2213. CONTENTS is nil. INFO is a plist holding contextual information."
  2214. ;; Use available tools.
  2215. (org-export-expand-macro macro info))
  2216. ;;;; Paragraph
  2217. (defun org-e-odt-paragraph (paragraph contents info)
  2218. "Transcode a PARAGRAPH element from Org to ODT.
  2219. CONTENTS is the contents of the paragraph, as a string. INFO is
  2220. the plist used as a communication channel."
  2221. (let* ((parent (org-export-get-parent paragraph))
  2222. (parent-type (org-element-type parent))
  2223. (style (case parent-type
  2224. (quote-block "Quotations")
  2225. (center-block "OrgCenter")
  2226. (footnote-definition "Footnote")
  2227. (t "Text_20_body"))))
  2228. ;; If this paragraph is a leading paragraph in a non-descriptive
  2229. ;; item and the item has a checkbox, splice the checkbox and
  2230. ;; paragraph contents together.
  2231. (when (and (eq (org-element-type parent) 'item)
  2232. (not (eq (org-element-property :type
  2233. (org-export-get-parent parent))
  2234. 'descriptive))
  2235. (eq paragraph (car (org-element-contents parent))))
  2236. (setq contents (concat (org-e-odt--checkbox parent) contents)))
  2237. (assert style)
  2238. (format "\n<text:p text:style-name=\"%s\">%s</text:p>" style contents)))
  2239. ;;;; Plain List
  2240. (defun org-e-odt-plain-list (plain-list contents info)
  2241. "Transcode a PLAIN-LIST element from Org to ODT.
  2242. CONTENTS is the contents of the list. INFO is a plist holding
  2243. contextual information."
  2244. (let* ((type (org-element-property :type plain-list))
  2245. (continue-numbering nil))
  2246. (assert (member type '(ordered unordered descriptive)))
  2247. (org-e-odt--wrap-label
  2248. plain-list
  2249. (format "\n<text:list text:style-name=\"%s\" %s>\n%s</text:list>"
  2250. (assoc-default type '((ordered . "OrgNumberedList")
  2251. (unordered . "OrgBulletedList")
  2252. (descriptive . "OrgDescriptionList")))
  2253. ;; If top-level list, re-start numbering. Otherwise,
  2254. ;; continue numbering.
  2255. (format "text:continue-numbering=\"%s\""
  2256. (let* ((parent (org-export-get-parent plain-list)))
  2257. (if (and parent (eq (org-element-type parent) 'item))
  2258. "true" "false")))
  2259. contents))))
  2260. ;;;; Plain Text
  2261. (defun org-e-odt-fill-tabs-and-spaces (line)
  2262. (replace-regexp-in-string
  2263. "\\([\t]\\|\\([ ]+\\)\\)"
  2264. (lambda (s)
  2265. (cond
  2266. ((string= s "\t") "<text:tab/>")
  2267. (t (let ((n (length s)))
  2268. (cond
  2269. ((= n 1) " ")
  2270. ((> n 1) (concat " " (format "<text:s text:c=\"%d\"/>" (1- n))))
  2271. (t ""))))))
  2272. line))
  2273. (defun org-e-odt-encode-plain-text (text &optional no-whitespace-filling)
  2274. (mapc
  2275. (lambda (pair)
  2276. (setq text (replace-regexp-in-string (car pair) (cdr pair) text t t)))
  2277. '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
  2278. (if no-whitespace-filling text
  2279. (org-e-odt-fill-tabs-and-spaces text)))
  2280. (defun org-e-odt--quotation-marks (text info)
  2281. "Export quotation marks depending on language conventions.
  2282. TEXT is a string containing quotation marks to be replaced. INFO
  2283. is a plist used as a communication channel."
  2284. (mapc (lambda(l)
  2285. (let ((start 0))
  2286. (while (setq start (string-match (car l) text start))
  2287. (let ((new-quote (concat (match-string 1 text) (cdr l))))
  2288. (setq text (replace-match new-quote t t text))))))
  2289. (cdr (or (assoc (plist-get info :language) org-e-odt-quotes)
  2290. ;; Falls back on English.
  2291. (assoc "en" org-e-odt-quotes))))
  2292. text)
  2293. (defun org-e-odt-plain-text (text info)
  2294. "Transcode a TEXT string from Org to ODT.
  2295. TEXT is the string to transcode. INFO is a plist holding
  2296. contextual information."
  2297. ;; Protect &, < and >.
  2298. (setq text (org-e-odt-encode-plain-text text t))
  2299. ;; Handle quotation marks
  2300. (setq text (org-e-odt--quotation-marks text info))
  2301. ;; Convert special strings.
  2302. (when (plist-get info :with-special-strings)
  2303. (mapc
  2304. (lambda (pair)
  2305. (setq text (replace-regexp-in-string (car pair) (cdr pair) text t nil)))
  2306. org-e-odt-special-string-regexps))
  2307. ;; Handle break preservation if required.
  2308. (when (plist-get info :preserve-breaks)
  2309. (setq text (replace-regexp-in-string
  2310. "\\(\\\\\\\\\\)?[ \t]*\n" "<text:line-break/>\n" text t)))
  2311. ;; Return value.
  2312. text)
  2313. ;;;; Planning
  2314. (defun org-e-odt-planning (planning contents info)
  2315. "Transcode a PLANNING element from Org to ODT.
  2316. CONTENTS is nil. INFO is a plist used as a communication
  2317. channel."
  2318. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2319. "OrgTimestampWrapper"
  2320. (concat
  2321. (let ((closed (org-element-property :closed planning)))
  2322. (when closed
  2323. (concat
  2324. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2325. "OrgTimestampKeyword" org-closed-string)
  2326. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2327. "OrgTimestamp" (org-translate-time closed)))))
  2328. (let ((deadline (org-element-property :deadline planning)))
  2329. (when deadline
  2330. (concat
  2331. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2332. "OrgTimestampKeyword" org-deadline-string)
  2333. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2334. "OrgTimestamp" (org-translate-time deadline)))))
  2335. (let ((scheduled (org-element-property :scheduled planning)))
  2336. (when scheduled
  2337. (concat
  2338. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2339. "OrgTimestampKeyword" org-scheduled-string)
  2340. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2341. "OrgTimestamp" (org-translate-time scheduled))))))))
  2342. ;;;; Property Drawer
  2343. (defun org-e-odt-property-drawer (property-drawer contents info)
  2344. "Transcode a PROPERTY-DRAWER element from Org to ODT.
  2345. CONTENTS is nil. INFO is a plist holding contextual
  2346. information."
  2347. ;; The property drawer isn't exported but we want separating blank
  2348. ;; lines nonetheless.
  2349. "")
  2350. ;;;; Quote Block
  2351. (defun org-e-odt-quote-block (quote-block contents info)
  2352. "Transcode a QUOTE-BLOCK element from Org to ODT.
  2353. CONTENTS holds the contents of the block. INFO is a plist
  2354. holding contextual information."
  2355. (org-e-odt--wrap-label quote-block contents))
  2356. ;;;; Quote Section
  2357. (defun org-e-odt-quote-section (quote-section contents info)
  2358. "Transcode a QUOTE-SECTION element from Org to ODT.
  2359. CONTENTS is nil. INFO is a plist holding contextual information."
  2360. (let ((value (org-remove-indentation
  2361. (org-element-property :value quote-section))))
  2362. (when value (org-e-odt-do-format-code value))))
  2363. ;;;; Section
  2364. (defun org-e-odt-format-section (text style &optional name)
  2365. (let ((default-name (car (org-e-odt-add-automatic-style "Section"))))
  2366. (format "\n<text:section text:style-name=\"%s\" %s>\n%s</text:section>"
  2367. style
  2368. (format "text:name=\"%s\"" (or name default-name))
  2369. text)))
  2370. (defun org-e-odt-section (section contents info) ; FIXME
  2371. "Transcode a SECTION element from Org to ODT.
  2372. CONTENTS holds the contents of the section. INFO is a plist
  2373. holding contextual information."
  2374. contents)
  2375. ;;;; Radio Target
  2376. (defun org-e-odt-radio-target (radio-target text info)
  2377. "Transcode a RADIO-TARGET object from Org to ODT.
  2378. TEXT is the text of the target. INFO is a plist holding
  2379. contextual information."
  2380. (org-e-odt-format-target
  2381. text (org-export-solidify-link-text
  2382. (org-element-property :value radio-target))))
  2383. ;;;; Special Block
  2384. (defun org-e-odt-special-block (special-block contents info)
  2385. "Transcode a SPECIAL-BLOCK element from Org to ODT.
  2386. CONTENTS holds the contents of the block. INFO is a plist
  2387. holding contextual information."
  2388. (let ((type (downcase (org-element-property :type special-block)))
  2389. (attributes (org-export-read-attribute :attr_odt special-block)))
  2390. (org-e-odt--wrap-label
  2391. special-block
  2392. (cond
  2393. ;; Annotation.
  2394. ((string= type "annotation")
  2395. (let ((author (or (plist-get attributes :author)
  2396. (let ((author (plist-get info :author)))
  2397. (and author (org-export-data author info)))))
  2398. (date (or (plist-get attributes :date)
  2399. (plist-get info :date))))
  2400. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2401. "Text_20_body"
  2402. (format "<office:annotation>\n%s\n</office:annotation>"
  2403. (concat
  2404. (and author
  2405. (format "<dc:creator>%s</dc:creator>" author))
  2406. (and date
  2407. (format "<dc:date>%s</dc:date>"
  2408. (org-e-odt--date date)))
  2409. contents)))))
  2410. ;; Textbox.
  2411. ((string= type "textbox")
  2412. (let ((width (plist-get attributes :width))
  2413. (height (plist-get attributes :height))
  2414. (style (plist-get attributes :style))
  2415. (extra (plist-get attributes :extra))
  2416. (anchor (plist-get attributes :anchor)))
  2417. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2418. "Text_20_body" (org-e-odt--textbox contents width height
  2419. style extra anchor))))
  2420. (t contents)))))
  2421. ;;;; Src Block
  2422. (defun org-e-odt-hfy-face-to-css (fn)
  2423. "Create custom style for face FN.
  2424. When FN is the default face, use it's foreground and background
  2425. properties to create \"OrgSrcBlock\" paragraph style. Otherwise
  2426. use it's color attribute to create a character style whose name
  2427. is obtained from FN. Currently all attributes of FN other than
  2428. color are ignored.
  2429. The style name for a face FN is derived using the following
  2430. operations on the face name in that order - de-dash, CamelCase
  2431. and prefix with \"OrgSrc\". For example,
  2432. `font-lock-function-name-face' is associated with
  2433. \"OrgSrcFontLockFunctionNameFace\"."
  2434. (let* ((css-list (hfy-face-to-style fn))
  2435. (style-name ((lambda (fn)
  2436. (concat "OrgSrc"
  2437. (mapconcat
  2438. 'capitalize (split-string
  2439. (hfy-face-or-def-to-name fn) "-")
  2440. ""))) fn))
  2441. (color-val (cdr (assoc "color" css-list)))
  2442. (background-color-val (cdr (assoc "background" css-list)))
  2443. (style (and org-e-odt-create-custom-styles-for-srcblocks
  2444. (cond
  2445. ((eq fn 'default)
  2446. (format org-e-odt-src-block-paragraph-format
  2447. background-color-val color-val))
  2448. (t
  2449. (format
  2450. "
  2451. <style:style style:name=\"%s\" style:family=\"text\">
  2452. <style:text-properties fo:color=\"%s\"/>
  2453. </style:style>" style-name color-val))))))
  2454. (cons style-name style)))
  2455. (defun org-e-odt-htmlfontify-string (line)
  2456. (let* ((hfy-html-quote-regex "\\([<\"&> ]\\)")
  2457. (hfy-html-quote-map '(("\"" "&quot;")
  2458. ("<" "&lt;")
  2459. ("&" "&amp;")
  2460. (">" "&gt;")
  2461. (" " "<text:s/>")
  2462. (" " "<text:tab/>")))
  2463. (hfy-face-to-css 'org-e-odt-hfy-face-to-css)
  2464. (hfy-optimisations-1 (copy-seq hfy-optimisations))
  2465. (hfy-optimisations (add-to-list 'hfy-optimisations-1
  2466. 'body-text-only))
  2467. (hfy-begin-span-handler
  2468. (lambda (style text-block text-id text-begins-block-p)
  2469. (insert (format "<text:span text:style-name=\"%s\">" style))))
  2470. (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
  2471. (with-no-warnings (htmlfontify-string line))))
  2472. (defun org-e-odt-do-format-code
  2473. (code &optional lang refs retain-labels num-start)
  2474. (let* ((lang (or (assoc-default lang org-src-lang-modes) lang))
  2475. (lang-mode (and lang (intern (format "%s-mode" lang))))
  2476. (code-lines (org-split-string code "\n"))
  2477. (code-length (length code-lines))
  2478. (use-htmlfontify-p (and (functionp lang-mode)
  2479. org-e-odt-fontify-srcblocks
  2480. (require 'htmlfontify nil t)
  2481. (fboundp 'htmlfontify-string)))
  2482. (code (if (not use-htmlfontify-p) code
  2483. (with-temp-buffer
  2484. (insert code)
  2485. (funcall lang-mode)
  2486. (font-lock-fontify-buffer)
  2487. (buffer-string))))
  2488. (fontifier (if use-htmlfontify-p 'org-e-odt-htmlfontify-string
  2489. 'org-e-odt-encode-plain-text))
  2490. (par-style (if use-htmlfontify-p "OrgSrcBlock"
  2491. "OrgFixedWidthBlock"))
  2492. (i 0))
  2493. (assert (= code-length (length (org-split-string code "\n"))))
  2494. (setq code
  2495. (org-export-format-code
  2496. code
  2497. (lambda (loc line-num ref)
  2498. (setq par-style
  2499. (concat par-style (and (= (incf i) code-length) "LastLine")))
  2500. (setq loc (concat loc (and ref retain-labels (format " (%s)" ref))))
  2501. (setq loc (funcall fontifier loc))
  2502. (when ref
  2503. (setq loc (org-e-odt-format-target loc (concat "coderef-" ref))))
  2504. (assert par-style)
  2505. (setq loc (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2506. par-style loc))
  2507. (if (not line-num) loc
  2508. (format "\n<text:list-item>%s\n</text:list-item>" loc)))
  2509. num-start refs))
  2510. (cond
  2511. ((not num-start) code)
  2512. ((= num-start 0)
  2513. (format
  2514. "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
  2515. " text:continue-numbering=\"false\"" code))
  2516. (t
  2517. (format
  2518. "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
  2519. " text:continue-numbering=\"true\"" code)))))
  2520. (defun org-e-odt-format-code (element info)
  2521. (let* ((lang (org-element-property :language element))
  2522. ;; Extract code and references.
  2523. (code-info (org-export-unravel-code element))
  2524. (code (car code-info))
  2525. (refs (cdr code-info))
  2526. ;; Does the src block contain labels?
  2527. (retain-labels (org-element-property :retain-labels element))
  2528. ;; Does it have line numbers?
  2529. (num-start (case (org-element-property :number-lines element)
  2530. (continued (org-export-get-loc element info))
  2531. (new 0))))
  2532. (org-e-odt-do-format-code code lang refs retain-labels num-start)))
  2533. (defun org-e-odt-src-block (src-block contents info)
  2534. "Transcode a SRC-BLOCK element from Org to ODT.
  2535. CONTENTS holds the contents of the item. INFO is a plist holding
  2536. contextual information."
  2537. (let* ((lang (org-element-property :language src-block))
  2538. (caption (org-element-property :caption src-block))
  2539. (short-caption (and (cdr caption)
  2540. (org-export-data (cdr caption) info)))
  2541. (caption (and (car caption) (org-export-data (car caption) info)))
  2542. (label (org-element-property :name src-block))
  2543. (attributes (org-export-read-attribute :attr_odt src-block)))
  2544. ;; FIXME: Handle caption
  2545. ;; caption-str (when caption)
  2546. ;; (main (org-export-data (car caption) info))
  2547. ;; (secondary (org-export-data (cdr caption) info))
  2548. ;; (caption-str (org-e-odt--caption/label-string caption label info))
  2549. (let* ((captions (org-e-odt-format-label src-block info 'definition))
  2550. (caption (car captions)) (short-caption (cdr captions)))
  2551. (concat
  2552. (and caption
  2553. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2554. "Listing" caption))
  2555. (let ((--src-block (org-e-odt-format-code src-block info)))
  2556. (if (not (plist-get attributes :textbox)) --src-block
  2557. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2558. "Text_20_body"
  2559. (org-e-odt--textbox --src-block nil nil nil))))))))
  2560. ;;;; Statistics Cookie
  2561. (defun org-e-odt-statistics-cookie (statistics-cookie contents info)
  2562. "Transcode a STATISTICS-COOKIE object from Org to ODT.
  2563. CONTENTS is nil. INFO is a plist holding contextual information."
  2564. (let ((cookie-value (org-element-property :value statistics-cookie)))
  2565. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2566. "OrgCode" cookie-value)))
  2567. ;;;; Strike-Through
  2568. (defun org-e-odt-strike-through (strike-through contents info)
  2569. "Transcode STRIKE-THROUGH from Org to ODT.
  2570. CONTENTS is the text with strike-through markup. INFO is a plist
  2571. holding contextual information."
  2572. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2573. "Strikethrough" contents))
  2574. ;;;; Subscript
  2575. (defun org-e-odt-subscript (subscript contents info)
  2576. "Transcode a SUBSCRIPT object from Org to ODT.
  2577. CONTENTS is the contents of the object. INFO is a plist holding
  2578. contextual information."
  2579. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2580. "OrgSubscript" contents))
  2581. ;;;; Superscript
  2582. (defun org-e-odt-superscript (superscript contents info)
  2583. "Transcode a SUPERSCRIPT object from Org to ODT.
  2584. CONTENTS is the contents of the object. INFO is a plist holding
  2585. contextual information."
  2586. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2587. "OrgSuperscript" contents))
  2588. ;;;; Table Cell
  2589. (defun org-e-odt-table-style-spec (element info)
  2590. (let* ((table (org-export-get-parent-table element))
  2591. (table-attributes (org-export-read-attribute :attr_odt table))
  2592. (table-style (plist-get table-attributes :style)))
  2593. (assoc table-style org-e-odt-table-styles)))
  2594. (defun org-e-odt-get-table-cell-styles (table-cell info)
  2595. "Retrieve styles applicable to a table cell.
  2596. R and C are (zero-based) row and column numbers of the table
  2597. cell. STYLE-SPEC is an entry in `org-e-odt-table-styles'
  2598. applicable to the current table. It is `nil' if the table is not
  2599. associated with any style attributes.
  2600. Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
  2601. When STYLE-SPEC is nil, style the table cell the conventional way
  2602. - choose cell borders based on row and column groupings and
  2603. choose paragraph alignment based on `org-col-cookies' text
  2604. property. See also
  2605. `org-e-odt-get-paragraph-style-cookie-for-table-cell'.
  2606. When STYLE-SPEC is non-nil, ignore the above cookie and return
  2607. styles congruent with the ODF-1.2 specification."
  2608. (let* ((table-cell-address (org-export-table-cell-address table-cell info))
  2609. (r (car table-cell-address)) (c (cdr table-cell-address))
  2610. (style-spec (org-e-odt-table-style-spec table-cell info))
  2611. (table-dimensions (org-export-table-dimensions
  2612. (org-export-get-parent-table table-cell)
  2613. info)))
  2614. (when style-spec
  2615. ;; LibreOffice - particularly the Writer - honors neither table
  2616. ;; templates nor custom table-cell styles. Inorder to retain
  2617. ;; inter-operability with LibreOffice, only automatic styles are
  2618. ;; used for styling of table-cells. The current implementation is
  2619. ;; congruent with ODF-1.2 specification and hence is
  2620. ;; future-compatible.
  2621. ;; Additional Note: LibreOffice's AutoFormat facility for tables -
  2622. ;; which recognizes as many as 16 different cell types - is much
  2623. ;; richer. Unfortunately it is NOT amenable to easy configuration
  2624. ;; by hand.
  2625. (let* ((template-name (nth 1 style-spec))
  2626. (cell-style-selectors (nth 2 style-spec))
  2627. (cell-type
  2628. (cond
  2629. ((and (cdr (assoc 'use-first-column-styles cell-style-selectors))
  2630. (= c 0)) "FirstColumn")
  2631. ((and (cdr (assoc 'use-last-column-styles cell-style-selectors))
  2632. (= (1+ c) (cdr table-dimensions)))
  2633. "LastColumn")
  2634. ((and (cdr (assoc 'use-first-row-styles cell-style-selectors))
  2635. (= r 0)) "FirstRow")
  2636. ((and (cdr (assoc 'use-last-row-styles cell-style-selectors))
  2637. (= (1+ r) (car table-dimensions)))
  2638. "LastRow")
  2639. ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
  2640. (= (% r 2) 1)) "EvenRow")
  2641. ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
  2642. (= (% r 2) 0)) "OddRow")
  2643. ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
  2644. (= (% c 2) 1)) "EvenColumn")
  2645. ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
  2646. (= (% c 2) 0)) "OddColumn")
  2647. (t ""))))
  2648. (concat template-name cell-type)))))
  2649. (defun org-e-odt-table-cell (table-cell contents info)
  2650. "Transcode a TABLE-CELL element from Org to ODT.
  2651. CONTENTS is nil. INFO is a plist used as a communication
  2652. channel."
  2653. (let* ((table-cell-address (org-export-table-cell-address table-cell info))
  2654. (r (car table-cell-address))
  2655. (c (cdr table-cell-address))
  2656. (horiz-span (or (org-export-table-cell-width table-cell info) 0))
  2657. (table-row (org-export-get-parent table-cell))
  2658. (custom-style-prefix (org-e-odt-get-table-cell-styles
  2659. table-cell info))
  2660. (paragraph-style
  2661. (or
  2662. (and custom-style-prefix
  2663. (format "%sTableParagraph" custom-style-prefix))
  2664. (concat
  2665. (cond
  2666. ((and (= 1 (org-export-table-row-group table-row info))
  2667. (org-export-table-has-header-p
  2668. (org-export-get-parent-table table-row) info))
  2669. "OrgTableHeading")
  2670. ((let* ((table (org-export-get-parent-table table-cell))
  2671. (table-attrs (org-export-read-attribute :attr_odt table))
  2672. (table-header-columns (plist-get table-attrs
  2673. :header-columns)))
  2674. (<= c (cond ((wholenump table-header-columns)
  2675. (- table-header-columns 1))
  2676. (table-header-columns 0)
  2677. (t -1))))
  2678. "OrgTableHeading")
  2679. (t "OrgTableContents"))
  2680. (capitalize (symbol-name (org-export-table-cell-alignment
  2681. table-cell info))))))
  2682. (cell-style-name
  2683. (or
  2684. (and custom-style-prefix (format "%sTableCell"
  2685. custom-style-prefix))
  2686. (concat
  2687. "OrgTblCell"
  2688. (when (or (org-export-table-row-starts-rowgroup-p table-row info)
  2689. (zerop r)) "T")
  2690. (when (org-export-table-row-ends-rowgroup-p table-row info) "B")
  2691. (when (and (org-export-table-cell-starts-colgroup-p table-cell info)
  2692. (not (zerop c)) ) "L"))))
  2693. (cell-attributes
  2694. (concat
  2695. (format " table:style-name=\"%s\"" cell-style-name)
  2696. (and (> horiz-span 0)
  2697. (format " table:number-columns-spanned=\"%d\""
  2698. (1+ horiz-span))))))
  2699. (unless contents (setq contents ""))
  2700. (concat
  2701. (assert paragraph-style)
  2702. (format "\n<table:table-cell%s>\n%s\n</table:table-cell>"
  2703. cell-attributes
  2704. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2705. paragraph-style contents))
  2706. (let (s)
  2707. (dotimes (i horiz-span s)
  2708. (setq s (concat s "\n<table:covered-table-cell/>"))))
  2709. "\n")))
  2710. ;;;; Table Row
  2711. (defun org-e-odt-table-row (table-row contents info)
  2712. "Transcode a TABLE-ROW element from Org to ODT.
  2713. CONTENTS is the contents of the row. INFO is a plist used as a
  2714. communication channel."
  2715. ;; Rules are ignored since table separators are deduced from
  2716. ;; borders of the current row.
  2717. (when (eq (org-element-property :type table-row) 'standard)
  2718. (let* ((rowgroup-tags
  2719. (if (and (= 1 (org-export-table-row-group table-row info))
  2720. (org-export-table-has-header-p
  2721. (org-export-get-parent-table table-row) info))
  2722. ;; If the row belongs to the first rowgroup and the
  2723. ;; table has more than one row groups, then this row
  2724. ;; belongs to the header row group.
  2725. '("\n<table:table-header-rows>" . "\n</table:table-header-rows>")
  2726. ;; Otherwise, it belongs to non-header row group.
  2727. '("\n<table:table-rows>" . "\n</table:table-rows>"))))
  2728. (concat
  2729. ;; Does this row begin a rowgroup?
  2730. (when (org-export-table-row-starts-rowgroup-p table-row info)
  2731. (car rowgroup-tags))
  2732. ;; Actual table row
  2733. (format "\n<table:table-row>\n%s\n</table:table-row>" contents)
  2734. ;; Does this row end a rowgroup?
  2735. (when (org-export-table-row-ends-rowgroup-p table-row info)
  2736. (cdr rowgroup-tags))))))
  2737. ;;;; Table
  2738. (defun org-e-odt-table-first-row-data-cells (table info)
  2739. (let ((table-row
  2740. (org-element-map
  2741. table 'table-row
  2742. (lambda (row)
  2743. (unless (eq (org-element-property :type row) 'rule) row))
  2744. info 'first-match))
  2745. (special-column-p (org-export-table-has-special-column-p table)))
  2746. (if (not special-column-p) (org-element-contents table-row)
  2747. (cdr (org-element-contents table-row)))))
  2748. (defun org-e-odt--table (table contents info)
  2749. "Transcode a TABLE element from Org to ODT.
  2750. CONTENTS is the contents of the table. INFO is a plist holding
  2751. contextual information."
  2752. (case (org-element-property :type table)
  2753. ;; Case 1: table.el doesn't support export to OD format. Strip
  2754. ;; such tables from export.
  2755. (table.el
  2756. (prog1 nil
  2757. (message
  2758. (concat
  2759. "(org-e-odt): Found table.el-type table in the source Org file."
  2760. " table.el doesn't support export to ODT format."
  2761. " Stripping the table from export."))))
  2762. ;; Case 2: Native Org tables.
  2763. (otherwise
  2764. (let* ((captions (org-e-odt-format-label table info 'definition))
  2765. (caption (car captions)) (short-caption (cdr captions))
  2766. (attributes (org-export-read-attribute :attr_odt table))
  2767. (custom-table-style (nth 1 (org-e-odt-table-style-spec table info)))
  2768. (table-column-specs
  2769. (function
  2770. (lambda (table info)
  2771. (let* ((table-style (or custom-table-style "OrgTable"))
  2772. (column-style (format "%sColumn" table-style)))
  2773. (mapconcat
  2774. (lambda (table-cell)
  2775. (let ((width (1+ (or (org-export-table-cell-width
  2776. table-cell info) 0)))
  2777. (s (format
  2778. "\n<table:table-column table:style-name=\"%s\"/>"
  2779. column-style))
  2780. out)
  2781. (dotimes (i width out) (setq out (concat s out)))))
  2782. (org-e-odt-table-first-row-data-cells table info) "\n"))))))
  2783. (concat
  2784. ;; caption.
  2785. (when caption
  2786. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2787. "Table" caption))
  2788. ;; begin table.
  2789. (let* ((automatic-name
  2790. (org-e-odt-add-automatic-style "Table" attributes)))
  2791. (format
  2792. "\n<table:table table:name=\"%s\" table:style-name=\"%s\">"
  2793. (or short-caption (car automatic-name))
  2794. (or custom-table-style (cdr automatic-name) "OrgTable")))
  2795. ;; column specification.
  2796. (funcall table-column-specs table info)
  2797. ;; actual contents.
  2798. "\n" contents
  2799. ;; end table.
  2800. "</table:table>")))))
  2801. (defun org-e-odt-table (table contents info)
  2802. "Transcode a TABLE element from Org to ODT.
  2803. CONTENTS is the contents of the table. INFO is a plist holding
  2804. contextual information."
  2805. (let* ((--get-previous-elements
  2806. (function
  2807. (lambda (blob info)
  2808. (let ((parent (org-export-get-parent blob)))
  2809. (cdr (member blob (reverse (org-element-contents parent))))))))
  2810. (--element-preceded-by-table-p
  2811. (function
  2812. (lambda (element info)
  2813. (loop for el in (funcall --get-previous-elements element info)
  2814. thereis (eq (org-element-type el) 'table)))))
  2815. (--walk-list-genealogy-and-collect-tags
  2816. (function
  2817. (lambda (table info)
  2818. (let* ((genealogy (org-export-get-genealogy table))
  2819. (list-genealogy
  2820. (when (eq (org-element-type (car genealogy)) 'item)
  2821. (loop for el in genealogy
  2822. when (member (org-element-type el)
  2823. '(item plain-list))
  2824. collect el))))
  2825. (loop for el in list-genealogy
  2826. with parent-list collect
  2827. (case (org-element-type el)
  2828. (plain-list
  2829. (setq parent-list el)
  2830. `("</text:list>"
  2831. . ,(let ((type (org-element-property :type el)))
  2832. (format
  2833. "<text:list text:style-name=\"%s\" %s>"
  2834. (assoc-default
  2835. type '((ordered . "OrgNumberedList")
  2836. (unordered . "OrgBulletedList")
  2837. (descriptive . "OrgDescriptionList")))
  2838. "text:continue-numbering=\"true\""))))
  2839. (item
  2840. (cond
  2841. ((not parent-list)
  2842. (if (funcall --element-preceded-by-table-p table info)
  2843. '("</text:list-header>" . "<text:list-header>")
  2844. '("</text:list-item>" . "<text:list-header>")))
  2845. ((funcall --element-preceded-by-table-p
  2846. parent-list info)
  2847. '("</text:list-header>" . "<text:list-header>"))
  2848. (t '("</text:list-item>" . "<text:list-item>"))))))))))
  2849. (close-open-tags (funcall --walk-list-genealogy-and-collect-tags
  2850. table info)))
  2851. ;; OpenDocument schema does not permit table to occur within a
  2852. ;; list item. So, to typeset an indented table, we make use of
  2853. ;; list continuations.
  2854. (concat "\n"
  2855. ;; Discontinue the list.
  2856. (mapconcat 'car close-open-tags "\n")
  2857. ;; Put the table in an indented section.
  2858. (let* ((table (org-e-odt--table table contents info))
  2859. (level (/ (length (mapcar 'car close-open-tags)) 2))
  2860. (style (format "OrgIndentedSection-Level-%d" level)))
  2861. (when table (org-e-odt-format-section table style)))
  2862. ;; Continue the list.
  2863. (mapconcat 'cdr (nreverse close-open-tags) "\n"))))
  2864. ;;;; Target
  2865. (defun org-e-odt-target (target contents info)
  2866. "Transcode a TARGET object from Org to ODT.
  2867. CONTENTS is nil. INFO is a plist holding contextual
  2868. information."
  2869. (org-e-odt-format-target
  2870. "" (org-export-solidify-link-text (org-element-property :value target))))
  2871. ;;;; Timestamp
  2872. (defun org-e-odt-timestamp (timestamp contents info)
  2873. "Transcode a TIMESTAMP object from Org to ODT.
  2874. CONTENTS is nil. INFO is a plist used as a communication
  2875. channel."
  2876. (let ((timestamp-1 (org-element-property :value timestamp))
  2877. (timestamp-2 (org-element-property :range-end timestamp)))
  2878. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2879. "OrgTimestampWrapper"
  2880. (concat
  2881. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2882. "OrgTimestamp" (org-translate-time timestamp-1))
  2883. (and timestamp-2
  2884. "&#x2013;"
  2885. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2886. "OrgTimestamp" (org-translate-time timestamp-2)))))))
  2887. ;;;; Underline
  2888. (defun org-e-odt-underline (underline contents info)
  2889. "Transcode UNDERLINE from Org to ODT.
  2890. CONTENTS is the text with underline markup. INFO is a plist
  2891. holding contextual information."
  2892. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2893. "Underline" contents))
  2894. ;;;; Verbatim
  2895. (defun org-e-odt-verbatim (verbatim contents info)
  2896. "Transcode a VERBATIM object from Org to ODT.
  2897. CONTENTS is nil. INFO is a plist used as a communication
  2898. channel."
  2899. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2900. "OrgCode" (org-element-property :value verbatim)))
  2901. ;;;; Verse Block
  2902. (defun org-e-odt-verse-block (verse-block contents info)
  2903. "Transcode a VERSE-BLOCK element from Org to ODT.
  2904. CONTENTS is verse block contents. INFO is a plist holding
  2905. contextual information."
  2906. ;; Add line breaks to each line of verse.
  2907. (setq contents (replace-regexp-in-string
  2908. "\\(<text:line-break/>\\)?[ \t]*\n"
  2909. "<text:line-break/>" contents))
  2910. ;; Replace tabs and spaces.
  2911. (setq contents (org-e-odt-fill-tabs-and-spaces contents))
  2912. ;; Surround it in a verse environment.
  2913. (org-e-odt--wrap-label
  2914. verse-block
  2915. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2916. "OrgVerse" contents)))
  2917. ;;; Interactive functions
  2918. (defun org-e-odt-create-manifest-file-entry (&rest args)
  2919. (push args org-e-odt-manifest-file-entries))
  2920. (defun org-e-odt-write-manifest-file ()
  2921. (make-directory (concat org-e-odt-zip-dir "META-INF"))
  2922. (let ((manifest-file (concat org-e-odt-zip-dir "META-INF/manifest.xml")))
  2923. (with-current-buffer
  2924. (let ((nxml-auto-insert-xml-declaration-flag nil))
  2925. (find-file-noselect manifest-file t))
  2926. (insert
  2927. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  2928. <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
  2929. (mapc
  2930. (lambda (file-entry)
  2931. (let* ((version (nth 2 file-entry))
  2932. (extra (if (not version) ""
  2933. (format " manifest:version=\"%s\"" version))))
  2934. (insert
  2935. (format org-e-odt-manifest-file-entry-tag
  2936. (nth 0 file-entry) (nth 1 file-entry) extra))))
  2937. org-e-odt-manifest-file-entries)
  2938. (insert "\n</manifest:manifest>"))))
  2939. (defmacro org-e-odt--export-wrap (out-file &rest body)
  2940. `(let* ((--out-file ,out-file)
  2941. (out-file-type (file-name-extension --out-file))
  2942. (org-e-odt-xml-files '("META-INF/manifest.xml" "content.xml"
  2943. "meta.xml" "styles.xml"))
  2944. ;; Initialize workarea. All files that end up in the
  2945. ;; exported get created here.
  2946. (org-e-odt-zip-dir (file-name-as-directory
  2947. (make-temp-file (format "%s-" out-file-type) t)))
  2948. (org-e-odt-manifest-file-entries nil)
  2949. (--cleanup-xml-buffers
  2950. (function
  2951. (lambda nil
  2952. ;; Kill all XML buffers.
  2953. (mapc (lambda (file)
  2954. (let ((buf (get-file-buffer
  2955. (concat org-e-odt-zip-dir file))))
  2956. (when buf
  2957. (set-buffer-modified-p nil)
  2958. (kill-buffer buf))))
  2959. org-e-odt-xml-files)
  2960. ;; Delete temporary directory and also other embedded
  2961. ;; files that get copied there.
  2962. (delete-directory org-e-odt-zip-dir t)))))
  2963. (org-condition-case-unless-debug
  2964. err
  2965. (progn
  2966. (unless (executable-find "zip")
  2967. ;; Not at all OSes ship with zip by default
  2968. (error "Executable \"zip\" needed for creating OpenDocument files"))
  2969. ;; Do export. This creates a bunch of xml files ready to be
  2970. ;; saved and zipped.
  2971. (progn ,@body)
  2972. ;; Create a manifest entry for content.xml.
  2973. (org-e-odt-create-manifest-file-entry "text/xml" "content.xml")
  2974. ;; Write mimetype file
  2975. (let* ((mimetypes
  2976. '(("odt" . "application/vnd.oasis.opendocument.text")
  2977. ("odf" . "application/vnd.oasis.opendocument.formula")))
  2978. (mimetype (cdr (assoc-string out-file-type mimetypes t))))
  2979. (unless mimetype
  2980. (error "Unknown OpenDocument backend %S" out-file-type))
  2981. (write-region mimetype nil (concat org-e-odt-zip-dir "mimetype"))
  2982. (org-e-odt-create-manifest-file-entry mimetype "/" "1.2"))
  2983. ;; Write out the manifest entries before zipping
  2984. (org-e-odt-write-manifest-file)
  2985. ;; Save all XML files.
  2986. (mapc (lambda (file)
  2987. (let ((buf (get-file-buffer (concat org-e-odt-zip-dir file))))
  2988. (when buf
  2989. (with-current-buffer buf
  2990. ;; Prettify output if needed.
  2991. (when org-e-odt-prettify-xml
  2992. (indent-region (point-min) (point-max)))
  2993. (save-buffer 0)))))
  2994. org-e-odt-xml-files)
  2995. ;; Run zip.
  2996. (let* ((target --out-file)
  2997. (target-name (file-name-nondirectory target))
  2998. (target-dir (file-name-directory target))
  2999. (cmds `(("zip" "-mX0" ,target-name "mimetype")
  3000. ("zip" "-rmTq" ,target-name "."))))
  3001. ;; If a file with same name as the desired output file
  3002. ;; exists, remove it.
  3003. (when (file-exists-p target)
  3004. (delete-file target))
  3005. ;; Zip up the xml files.
  3006. (let ((coding-system-for-write 'no-conversion) exitcode err-string)
  3007. (message "Creating ODT file...")
  3008. ;; Switch temporarily to content.xml. This way Zip
  3009. ;; process will inherit `org-e-odt-zip-dir' as the current
  3010. ;; directory.
  3011. (with-current-buffer
  3012. (find-file-noselect (concat org-e-odt-zip-dir "content.xml") t)
  3013. (mapc
  3014. (lambda (cmd)
  3015. (message "Running %s" (mapconcat 'identity cmd " "))
  3016. (setq err-string
  3017. (with-output-to-string
  3018. (setq exitcode
  3019. (apply 'call-process (car cmd)
  3020. nil standard-output nil (cdr cmd)))))
  3021. (or (zerop exitcode)
  3022. (error (concat "Unable to create OpenDocument file."
  3023. (format " Zip failed with error (%s)"
  3024. err-string)))))
  3025. cmds)
  3026. ;; Zip file is now in the rightful place.
  3027. (rename-file target-name target)))
  3028. (message "Created %s" target)
  3029. ;; Cleanup work directory and work files.
  3030. (funcall --cleanup-xml-buffers)
  3031. ;; Open the OpenDocument file in archive-mode for
  3032. ;; examination.
  3033. (find-file-noselect target t)
  3034. ;; Return exported file.
  3035. (cond
  3036. ;; Case 1: Conversion desired on exported file. Run the
  3037. ;; converter on the OpenDocument file. Return the
  3038. ;; converted file.
  3039. (org-e-odt-preferred-output-format
  3040. (or (org-e-odt-convert target org-e-odt-preferred-output-format)
  3041. target))
  3042. ;; Case 2: No further conversion. Return exported
  3043. ;; OpenDocument file.
  3044. (t target))))
  3045. ((quit error)
  3046. ;; Cleanup work directory and work files.
  3047. (funcall --cleanup-xml-buffers)
  3048. (message "OpenDocument export failed: %s"
  3049. (error-message-string err))))))
  3050. ;;;###autoload
  3051. (defun org-e-odt-export-as-odf (latex-frag &optional odf-file)
  3052. "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
  3053. Use `org-create-math-formula' to convert LATEX-FRAG first to
  3054. MathML. When invoked as an interactive command, use
  3055. `org-latex-regexps' to infer LATEX-FRAG from currently active
  3056. region. If no LaTeX fragments are found, prompt for it. Push
  3057. MathML source to kill ring, if `org-export-copy-to-kill-ring' is
  3058. non-nil."
  3059. (interactive
  3060. `(,(let (frag)
  3061. (setq frag (and (setq frag (and (region-active-p)
  3062. (buffer-substring (region-beginning)
  3063. (region-end))))
  3064. (loop for e in org-latex-regexps
  3065. thereis (when (string-match (nth 1 e) frag)
  3066. (match-string (nth 2 e) frag)))))
  3067. (read-string "LaTeX Fragment: " frag nil frag))
  3068. ,(let ((odf-filename (expand-file-name
  3069. (concat
  3070. (file-name-sans-extension
  3071. (or (file-name-nondirectory buffer-file-name)))
  3072. "." "odf")
  3073. (file-name-directory buffer-file-name))))
  3074. (read-file-name "ODF filename: " nil odf-filename nil
  3075. (file-name-nondirectory odf-filename)))))
  3076. (let ((filename (or odf-file
  3077. (expand-file-name
  3078. (concat
  3079. (file-name-sans-extension
  3080. (or (file-name-nondirectory buffer-file-name)))
  3081. "." "odf")
  3082. (file-name-directory buffer-file-name)))))
  3083. (org-e-odt--export-wrap
  3084. filename
  3085. (let* ((buffer (progn
  3086. (require 'nxml-mode)
  3087. (let ((nxml-auto-insert-xml-declaration-flag nil))
  3088. (find-file-noselect (concat org-e-odt-zip-dir
  3089. "content.xml") t))))
  3090. (coding-system-for-write 'utf-8)
  3091. (save-buffer-coding-system 'utf-8))
  3092. (set-buffer buffer)
  3093. (set-buffer-file-coding-system coding-system-for-write)
  3094. (let ((mathml (org-create-math-formula latex-frag)))
  3095. (unless mathml (error "No Math formula created"))
  3096. (insert mathml)
  3097. ;; Add MathML to kill ring, if needed.
  3098. (when org-export-copy-to-kill-ring
  3099. (org-kill-new (buffer-string))))))))
  3100. ;;;###autoload
  3101. (defun org-e-odt-export-as-odf-and-open ()
  3102. "Export LaTeX fragment as OpenDocument formula and immediately open it.
  3103. Use `org-e-odt-export-as-odf' to read LaTeX fragment and OpenDocument
  3104. formula file."
  3105. (interactive)
  3106. (org-open-file (call-interactively 'org-e-odt-export-as-odf)))
  3107. ;;;###autoload
  3108. (defun org-e-odt-export-to-odt
  3109. (&optional subtreep visible-only body-only ext-plist pub-dir)
  3110. "Export current buffer to a HTML file.
  3111. If narrowing is active in the current buffer, only export its
  3112. narrowed part.
  3113. If a region is active, export that region.
  3114. When optional argument SUBTREEP is non-nil, export the sub-tree
  3115. at point, extracting information from the headline properties
  3116. first.
  3117. When optional argument VISIBLE-ONLY is non-nil, don't export
  3118. contents of hidden elements.
  3119. When optional argument BODY-ONLY is non-nil, only write code
  3120. between \"\\begin{document}\" and \"\\end{document}\".
  3121. EXT-PLIST, when provided, is a property list with external
  3122. parameters overriding Org default settings, but still inferior to
  3123. file-local settings.
  3124. When optional argument PUB-DIR is set, use it as the publishing
  3125. directory.
  3126. Return output file's name."
  3127. (interactive)
  3128. (org-e-odt--export-wrap
  3129. (org-export-output-file-name ".odt" subtreep pub-dir)
  3130. (let* ((org-e-odt-embedded-images-count 0)
  3131. (org-e-odt-embedded-formulas-count 0)
  3132. (org-e-odt-automatic-styles nil)
  3133. (org-e-odt-object-counters nil)
  3134. ;; Let `htmlfontify' know that we are interested in collecting
  3135. ;; styles.
  3136. (hfy-user-sheet-assoc nil))
  3137. ;; Initialize content.xml and kick-off the export process.
  3138. (let ((out-buf (progn
  3139. (require 'nxml-mode)
  3140. (let ((nxml-auto-insert-xml-declaration-flag nil))
  3141. (find-file-noselect
  3142. (concat org-e-odt-zip-dir "content.xml") t)))))
  3143. (org-export-to-buffer 'e-odt out-buf subtreep visible-only body-only)))))
  3144. (defun org-e-odt-reachable-p (in-fmt out-fmt)
  3145. "Return non-nil if IN-FMT can be converted to OUT-FMT."
  3146. (catch 'done
  3147. (let ((reachable-formats (org-e-odt-do-reachable-formats in-fmt)))
  3148. (dolist (e reachable-formats)
  3149. (let ((out-fmt-spec (assoc out-fmt (cdr e))))
  3150. (when out-fmt-spec
  3151. (throw 'done (cons (car e) out-fmt-spec))))))))
  3152. (defun org-e-odt-do-convert (in-file out-fmt &optional prefix-arg)
  3153. "Workhorse routine for `org-e-odt-convert'."
  3154. (require 'browse-url)
  3155. (let* ((in-file (expand-file-name (or in-file buffer-file-name)))
  3156. (dummy (or (file-readable-p in-file)
  3157. (error "Cannot read %s" in-file)))
  3158. (in-fmt (file-name-extension in-file))
  3159. (out-fmt (or out-fmt (error "Output format unspecified")))
  3160. (how (or (org-e-odt-reachable-p in-fmt out-fmt)
  3161. (error "Cannot convert from %s format to %s format?"
  3162. in-fmt out-fmt)))
  3163. (convert-process (car how))
  3164. (out-file (concat (file-name-sans-extension in-file) "."
  3165. (nth 1 (or (cdr how) out-fmt))))
  3166. (extra-options (or (nth 2 (cdr how)) ""))
  3167. (out-dir (file-name-directory in-file))
  3168. (cmd (format-spec convert-process
  3169. `((?i . ,(shell-quote-argument in-file))
  3170. (?I . ,(browse-url-file-url in-file))
  3171. (?f . ,out-fmt)
  3172. (?o . ,out-file)
  3173. (?O . ,(browse-url-file-url out-file))
  3174. (?d . , (shell-quote-argument out-dir))
  3175. (?D . ,(browse-url-file-url out-dir))
  3176. (?x . ,extra-options)))))
  3177. (when (file-exists-p out-file)
  3178. (delete-file out-file))
  3179. (message "Executing %s" cmd)
  3180. (let ((cmd-output (shell-command-to-string cmd)))
  3181. (message "%s" cmd-output))
  3182. (cond
  3183. ((file-exists-p out-file)
  3184. (message "Exported to %s" out-file)
  3185. (when prefix-arg
  3186. (message "Opening %s..." out-file)
  3187. (org-open-file out-file))
  3188. out-file)
  3189. (t
  3190. (message "Export to %s failed" out-file)
  3191. nil))))
  3192. (defun org-e-odt-do-reachable-formats (in-fmt)
  3193. "Return verbose info about formats to which IN-FMT can be converted.
  3194. Return a list where each element is of the
  3195. form (CONVERTER-PROCESS . OUTPUT-FMT-ALIST). See
  3196. `org-e-odt-convert-processes' for CONVERTER-PROCESS and see
  3197. `org-e-odt-convert-capabilities' for OUTPUT-FMT-ALIST."
  3198. (let* ((converter
  3199. (and org-e-odt-convert-process
  3200. (cadr (assoc-string org-e-odt-convert-process
  3201. org-e-odt-convert-processes t))))
  3202. (capabilities
  3203. (and org-e-odt-convert-process
  3204. (cadr (assoc-string org-e-odt-convert-process
  3205. org-e-odt-convert-processes t))
  3206. org-e-odt-convert-capabilities))
  3207. reachable-formats)
  3208. (when converter
  3209. (dolist (c capabilities)
  3210. (when (member in-fmt (nth 1 c))
  3211. (push (cons converter (nth 2 c)) reachable-formats))))
  3212. reachable-formats))
  3213. (defun org-e-odt-reachable-formats (in-fmt)
  3214. "Return list of formats to which IN-FMT can be converted.
  3215. The list of the form (OUTPUT-FMT-1 OUTPUT-FMT-2 ...)."
  3216. (let (l)
  3217. (mapc (lambda (e) (add-to-list 'l e))
  3218. (apply 'append (mapcar
  3219. (lambda (e) (mapcar 'car (cdr e)))
  3220. (org-e-odt-do-reachable-formats in-fmt))))
  3221. l))
  3222. (defun org-e-odt-convert-read-params ()
  3223. "Return IN-FILE and OUT-FMT params for `org-e-odt-do-convert'.
  3224. This is a helper routine for interactive use."
  3225. (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
  3226. (in-file (read-file-name "File to be converted: "
  3227. nil buffer-file-name t))
  3228. (in-fmt (file-name-extension in-file))
  3229. (out-fmt-choices (org-e-odt-reachable-formats in-fmt))
  3230. (out-fmt
  3231. (or (and out-fmt-choices
  3232. (funcall input "Output format: "
  3233. out-fmt-choices nil nil nil))
  3234. (error
  3235. "No known converter or no known output formats for %s files"
  3236. in-fmt))))
  3237. (list in-file out-fmt)))
  3238. ;;;###autoload
  3239. (defun org-e-odt-convert (&optional in-file out-fmt prefix-arg)
  3240. "Convert IN-FILE to format OUT-FMT using a command line converter.
  3241. IN-FILE is the file to be converted. If unspecified, it defaults
  3242. to variable `buffer-file-name'. OUT-FMT is the desired output
  3243. format. Use `org-e-odt-convert-process' as the converter.
  3244. If PREFIX-ARG is non-nil then the newly converted file is opened
  3245. using `org-open-file'."
  3246. (interactive
  3247. (append (org-e-odt-convert-read-params) current-prefix-arg))
  3248. (org-e-odt-do-convert in-file out-fmt prefix-arg))
  3249. ;;; Library Initializations
  3250. (mapc
  3251. (lambda (desc)
  3252. ;; Let Org open all OpenDocument files using system-registered app
  3253. (add-to-list 'org-file-apps
  3254. (cons (concat "\\." (car desc) "\\'") 'system))
  3255. ;; Let Emacs open all OpenDocument files in archive mode
  3256. (add-to-list 'auto-mode-alist
  3257. (cons (concat "\\." (car desc) "\\'") 'archive-mode)))
  3258. org-e-odt-file-extensions)
  3259. (provide 'org-e-odt)
  3260. ;;; org-e-odt.el ends here