ox-odt.el 159 KB

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