ox-odt.el 159 KB

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