ox-odt.el 160 KB

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