ox-odt.el 158 KB

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