org-e-odt.el 161 KB

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