ox-odt.el 159 KB

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