ox-odt.el 160 KB

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