ox-odt.el 158 KB

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