ox-odt.el 158 KB

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