ox-odt.el 158 KB

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