ox-odt.el 158 KB

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