org-e-odt.el 161 KB

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