org-e-odt.el 141 KB

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