org-e-odt.el 154 KB

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