ox-odt.el 158 KB

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