ox-odt.el 158 KB

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