ox-odt.el 159 KB

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