org-e-odt.el 140 KB

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