org-e-odt.el 155 KB

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