org-e-odt.el 144 KB

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