org-e-odt.el 134 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762
  1. ;;; org-e-odt.el --- OpenDocument Text exporter for Org-mode
  2. ;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
  3. ;; Author: Jambunathan K <kjambunathan at gmail dot com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;; This file is not part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;; Code:
  19. (eval-when-compile
  20. (require 'cl)
  21. (require 'table))
  22. (require 'format-spec)
  23. (require 'org-export)
  24. ;;; Define Back-End
  25. (org-export-define-backend e-odt
  26. ((bold . org-e-odt-bold)
  27. (center-block . org-e-odt-center-block)
  28. (clock . org-e-odt-clock)
  29. (code . org-e-odt-code)
  30. (drawer . org-e-odt-drawer)
  31. (dynamic-block . org-e-odt-dynamic-block)
  32. (entity . org-e-odt-entity)
  33. (example-block . org-e-odt-example-block)
  34. (export-block . org-e-odt-export-block)
  35. (export-snippet . org-e-odt-export-snippet)
  36. (fixed-width . org-e-odt-fixed-width)
  37. (footnote-definition . org-e-odt-footnote-definition)
  38. (footnote-reference . org-e-odt-footnote-reference)
  39. (headline . org-e-odt-headline)
  40. (horizontal-rule . org-e-odt-horizontal-rule)
  41. (inline-src-block . org-e-odt-inline-src-block)
  42. (inlinetask . org-e-odt-inlinetask)
  43. (italic . org-e-odt-italic)
  44. (item . org-e-odt-item)
  45. (keyword . org-e-odt-keyword)
  46. (latex-environment . org-e-odt-latex-environment)
  47. (latex-fragment . org-e-odt-latex-fragment)
  48. (line-break . org-e-odt-line-break)
  49. (link . org-e-odt-link)
  50. (macro . org-e-odt-macro)
  51. (paragraph . org-e-odt-paragraph)
  52. (plain-list . org-e-odt-plain-list)
  53. (plain-text . org-e-odt-plain-text)
  54. (planning . org-e-odt-planning)
  55. (property-drawer . org-e-odt-property-drawer)
  56. (quote-block . org-e-odt-quote-block)
  57. (quote-section . org-e-odt-quote-section)
  58. (radio-target . org-e-odt-radio-target)
  59. (section . org-e-odt-section)
  60. (special-block . org-e-odt-special-block)
  61. (src-block . org-e-odt-src-block)
  62. (statistics-cookie . org-e-odt-statistics-cookie)
  63. (strike-through . org-e-odt-strike-through)
  64. (subscript . org-e-odt-subscript)
  65. (superscript . org-e-odt-superscript)
  66. (table . org-e-odt-table)
  67. (table-cell . org-e-odt-table-cell)
  68. (table-row . org-e-odt-table-row)
  69. (target . org-e-odt-target)
  70. (template . org-e-odt-template)
  71. (timestamp . org-e-odt-timestamp)
  72. (underline . org-e-odt-underline)
  73. (verbatim . org-e-odt-verbatim)
  74. (verse-block . org-e-odt-verse-block))
  75. :export-block "ODT"
  76. :options-alist
  77. ((:odt-styles-file "ODT_STYLES_FILE" nil nil t)
  78. (:LaTeX-fragments nil "LaTeX" org-export-with-LaTeX-fragments)))
  79. ;;; Dependencies
  80. ;;; Hooks
  81. ;;; Function Declarations
  82. (declare-function org-id-find-id-file "org-id" (id))
  83. (declare-function hfy-face-to-style "htmlfontify" (fn))
  84. (declare-function hfy-face-or-def-to-name "htmlfontify" (fn))
  85. (declare-function archive-zip-extract "arc-mode.el" (archive name))
  86. (declare-function org-create-math-formula "org" (latex-frag &optional 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. ;;;; Library wrappers
  834. (defun org-e-odt--adopt-elements (parent &rest children)
  835. (prog1 parent
  836. (mapc (lambda (child)
  837. (let ((parent-1 (org-element-adopt-element parent child nil)))
  838. (assert (eq parent-1 parent))))
  839. children)))
  840. (defun org-e-odt--zip-extract (archive members target)
  841. (when (atom members) (setq members (list members)))
  842. (mapc (lambda (archive member target)
  843. (require 'arc-mode)
  844. (let* ((--quote-file-name
  845. ;; This is shamelessly stolen from `archive-zip-extract'.
  846. (lambda (name)
  847. (if (or (not (memq system-type '(windows-nt ms-dos)))
  848. (and (boundp 'w32-quote-process-args)
  849. (null w32-quote-process-args)))
  850. (shell-quote-argument name)
  851. name)))
  852. (target (funcall --quote-file-name target))
  853. (archive (expand-file-name archive))
  854. (archive-zip-extract
  855. (list "unzip" "-qq" "-o" "-d" target))
  856. exit-code command-output)
  857. (setq command-output
  858. (with-temp-buffer
  859. (setq exit-code (archive-zip-extract archive member))
  860. (buffer-string)))
  861. (unless (zerop exit-code)
  862. (message command-output)
  863. (error "Extraction failed"))))
  864. members))
  865. ;;;; Textbox
  866. (defun org-e-odt--textbox (text width height style &optional
  867. extra anchor-type)
  868. (org-e-odt--frame
  869. (format "\n<draw:text-box %s>%s\n</draw:text-box>"
  870. (concat (format " fo:min-height=\"%0.2fcm\"" (or height .2))
  871. (and (not width)
  872. (format " fo:min-width=\"%0.2fcm\"" (or width .2))))
  873. text)
  874. width nil style extra anchor-type))
  875. ;;;; Table of Contents
  876. (defun org-e-odt-begin-toc (index-title depth)
  877. (concat
  878. (format "
  879. <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
  880. <text:table-of-content-source text:outline-level=\"%d\">
  881. <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
  882. " depth index-title)
  883. (let ((levels (number-sequence 1 10)))
  884. (mapconcat
  885. (lambda (level)
  886. (format
  887. "
  888. <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
  889. <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
  890. <text:index-entry-chapter/>
  891. <text:index-entry-text/>
  892. <text:index-entry-link-end/>
  893. </text:table-of-content-entry-template>
  894. " level level)) levels ""))
  895. (format "
  896. </text:table-of-content-source>
  897. <text:index-body>
  898. <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
  899. <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
  900. </text:index-title>
  901. " index-title)))
  902. (defun org-e-odt-end-toc ()
  903. (format "
  904. </text:index-body>
  905. </text:table-of-content>
  906. "))
  907. (defun* org-e-odt-format-toc-headline
  908. (todo todo-type priority text tags
  909. &key level section-number headline-label &allow-other-keys)
  910. (setq text (concat
  911. (and org-export-with-section-numbers
  912. (concat section-number ". "))
  913. text
  914. (and tags
  915. (concat
  916. "<text:tab/>"
  917. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  918. "OrgTag" tags)))))
  919. (when todo
  920. (setq text (format "<text:span text:style-name=\"%s\">%s</text:span>"
  921. "OrgTodo" text)))
  922. (org-e-odt-format-link text (concat "#" headline-label) t))
  923. (defun org-e-odt-toc (depth info)
  924. (assert (wholenump depth))
  925. (let* ((title (org-export-translate "Table of Contents" :utf-8 info))
  926. (headlines (org-export-collect-headlines info depth)))
  927. (when headlines
  928. (concat
  929. (org-e-odt-begin-toc title depth)
  930. (mapconcat
  931. (lambda (headline)
  932. (let* ((entry (org-e-odt-format-headline--wrap
  933. headline info 'org-e-odt-format-toc-headline))
  934. (level (org-export-get-relative-level headline info))
  935. (style (format "Contents_20_%d" level)))
  936. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  937. style entry)))
  938. headlines "\n")
  939. (org-e-odt-end-toc)))))
  940. ;;;; Document styles
  941. (defun org-e-odt-add-automatic-style (object-type &optional object-props)
  942. "Create an automatic style of type OBJECT-TYPE with param OBJECT-PROPS.
  943. OBJECT-PROPS is (typically) a plist created by passing
  944. \"#+ATTR_ODT: \" option of the object in question to
  945. `org-e-odt-parse-block-attributes'.
  946. Use `org-e-odt-object-counters' to generate an automatic
  947. OBJECT-NAME and STYLE-NAME. If OBJECT-PROPS is non-nil, add a
  948. new entry in `org-e-odt-automatic-styles'. Return (OBJECT-NAME
  949. . STYLE-NAME)."
  950. (assert (stringp object-type))
  951. (let* ((object (intern object-type))
  952. (seqvar object)
  953. (seqno (1+ (or (plist-get org-e-odt-object-counters seqvar) 0)))
  954. (object-name (format "%s%d" object-type seqno)) style-name)
  955. (setq org-e-odt-object-counters
  956. (plist-put org-e-odt-object-counters seqvar seqno))
  957. (when object-props
  958. (setq style-name (format "Org%s" object-name))
  959. (setq org-e-odt-automatic-styles
  960. (plist-put org-e-odt-automatic-styles object
  961. (append (list (list style-name object-props))
  962. (plist-get org-e-odt-automatic-styles object)))))
  963. (cons object-name style-name)))
  964. ;;;; Caption and Labels
  965. (defun org-e-odt--wrap-label (element output)
  966. "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
  967. This function shouldn't be used for floats. See
  968. `org-e-odt--caption/label-string'."
  969. ;; (let ((label (org-element-property :name element)))
  970. ;; (if (or (not output) (not label) (string= output "") (string= label ""))
  971. ;; output
  972. ;; (concat (format "\\label{%s}\n" label) output)))
  973. output)
  974. (defun org-e-odt--caption/label-string (caption label info)
  975. "Return caption and label HTML string for floats.
  976. CAPTION is a cons cell of secondary strings, the car being the
  977. standard caption and the cdr its short form. LABEL is a string
  978. representing the label. INFO is a plist holding contextual
  979. information.
  980. If there's no caption nor label, return the empty string.
  981. For non-floats, see `org-e-odt--wrap-label'."
  982. (setq label nil) ;; FIXME
  983. (let ((label-str (if label (format "\\label{%s}" label) "")))
  984. (cond
  985. ((and (not caption) (not label)) "")
  986. ((not caption) (format "\\label{%s}\n" label))
  987. ;; Option caption format with short name.
  988. ((cdr caption)
  989. (format "\\caption[%s]{%s%s}\n"
  990. (org-export-data (cdr caption) info)
  991. label-str
  992. (org-export-data (car caption) info)))
  993. ;; Standard caption format.
  994. ;; (t (format "\\caption{%s%s}\n"
  995. ;; label-str
  996. ;; (org-export-data (car caption) info)))
  997. (t (org-export-data (car caption) info)))))
  998. ;;;; Checkbox
  999. (defun org-e-odt--checkbox (item)
  1000. "Return check-box string associated to ITEM."
  1001. (let ((checkbox (org-element-property :checkbox item)))
  1002. (if (not checkbox) ""
  1003. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1004. "OrgCode" (case checkbox
  1005. (on "[&#x2713;] ") ; CHECK MARK
  1006. (off "[ ] ")
  1007. (trans "[-] "))))))
  1008. ;;; Template
  1009. (defun org-e-odt-template (contents info)
  1010. "Return complete document string after HTML conversion.
  1011. CONTENTS is the transcoded contents string. RAW-DATA is the
  1012. original parsed data. INFO is a plist holding export options."
  1013. ;; Write meta file.
  1014. (let ((title (org-export-data (plist-get info :title) info))
  1015. (author (let ((author (plist-get info :author)))
  1016. (if (not author) "" (org-export-data author info))))
  1017. (date (org-e-odt--date
  1018. (org-export-data (plist-get info :date) info)))
  1019. (email (plist-get info :email))
  1020. (keywords (plist-get info :keywords))
  1021. (description (plist-get info :description)))
  1022. (write-region
  1023. (concat
  1024. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  1025. <office:document-meta
  1026. xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
  1027. xmlns:xlink=\"http://www.w3.org/1999/xlink\"
  1028. xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
  1029. xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
  1030. xmlns:ooo=\"http://openoffice.org/2004/office\"
  1031. office:version=\"1.2\">
  1032. <office:meta>\n"
  1033. (format "<dc:creator>%s</dc:creator>\n" author)
  1034. (format "<meta:initial-creator>%s</meta:initial-creator>\n" author)
  1035. (format "<dc:date>%s</dc:date>\n" date)
  1036. (format "<meta:creation-date>%s</meta:creation-date>\n" date)
  1037. (format "<meta:generator>%s</meta:generator>\n"
  1038. (let ((creator-info (plist-get info :with-creator)))
  1039. (if (or (not creator-info) (eq creator-info 'comment)) ""
  1040. (plist-get info :creator))))
  1041. (format "<meta:keyword>%s</meta:keyword>\n" keywords)
  1042. (format "<dc:subject>%s</dc:subject>\n" description)
  1043. (format "<dc:title>%s</dc:title>\n" title)
  1044. "\n"
  1045. " </office:meta>\n" "</office:document-meta>")
  1046. nil (concat org-e-odt-zip-dir "meta.xml"))
  1047. ;; Add meta.xml in to manifest.
  1048. (org-e-odt-create-manifest-file-entry "text/xml" "meta.xml"))
  1049. ;; Update styles file.
  1050. ;; Copy styles.xml. Also dump htmlfontify styles, if there is any.
  1051. ;; Write styles file.
  1052. (let* ((styles-file (plist-get info :odt-styles-file))
  1053. (styles-file (and styles-file (read (org-trim styles-file))))
  1054. ;; Non-availability of styles.xml is not a critical
  1055. ;; error. For now throw an error purely for aesthetic
  1056. ;; reasons.
  1057. (styles-file (or styles-file
  1058. org-e-odt-styles-file
  1059. (expand-file-name "OrgOdtStyles.xml"
  1060. org-e-odt-styles-dir)
  1061. (error "org-e-odt: Missing styles file?"))))
  1062. (cond
  1063. ((listp styles-file)
  1064. (let ((archive (nth 0 styles-file))
  1065. (members (nth 1 styles-file)))
  1066. (org-e-odt--zip-extract archive members org-e-odt-zip-dir)
  1067. (mapc
  1068. (lambda (member)
  1069. (when (org-file-image-p member)
  1070. (let* ((image-type (file-name-extension member))
  1071. (media-type (format "image/%s" image-type)))
  1072. (org-e-odt-create-manifest-file-entry media-type member))))
  1073. members)))
  1074. ((and (stringp styles-file) (file-exists-p styles-file))
  1075. (let ((styles-file-type (file-name-extension styles-file)))
  1076. (cond
  1077. ((string= styles-file-type "xml")
  1078. (copy-file styles-file (concat org-e-odt-zip-dir "styles.xml") t))
  1079. ((member styles-file-type '("odt" "ott"))
  1080. (org-e-odt--zip-extract styles-file "styles.xml" org-e-odt-zip-dir)))))
  1081. (t
  1082. (error (format "Invalid specification of styles.xml file: %S"
  1083. org-e-odt-styles-file))))
  1084. ;; create a manifest entry for styles.xml
  1085. (org-e-odt-create-manifest-file-entry "text/xml" "styles.xml")
  1086. ;; FIXME: Who is opening an empty styles.xml before this point?
  1087. (with-current-buffer
  1088. (find-file-noselect (concat org-e-odt-zip-dir "styles.xml") t)
  1089. (revert-buffer t t)
  1090. ;; Write custom styles for source blocks
  1091. ;; Save STYLES used for colorizing of source blocks.
  1092. ;; Update styles.xml with styles that were collected as part of
  1093. ;; `org-e-odt-hfy-face-to-css' callbacks.
  1094. (let ((styles (mapconcat (lambda (style) (format " %s\n" (cddr style)))
  1095. hfy-user-sheet-assoc "")))
  1096. (when styles
  1097. (goto-char (point-min))
  1098. (when (re-search-forward "</office:styles>" nil t)
  1099. (goto-char (match-beginning 0))
  1100. (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n"))))
  1101. ;; Update styles.xml - take care of outline numbering
  1102. ;; Don't make automatic backup of styles.xml file. This setting
  1103. ;; prevents the backed-up styles.xml file from being zipped in to
  1104. ;; odt file. This is more of a hackish fix. Better alternative
  1105. ;; would be to fix the zip command so that the output odt file
  1106. ;; includes only the needed files and excludes any auto-generated
  1107. ;; extra files like backups and auto-saves etc etc. Note that
  1108. ;; currently the zip command zips up the entire temp directory so
  1109. ;; that any auto-generated files created under the hood ends up in
  1110. ;; the resulting odt file.
  1111. (set (make-local-variable 'backup-inhibited) t)
  1112. ;; Outline numbering is retained only upto LEVEL.
  1113. ;; To disable outline numbering pass a LEVEL of 0.
  1114. (goto-char (point-min))
  1115. (let ((regex
  1116. "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
  1117. (replacement
  1118. "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
  1119. (while (re-search-forward regex nil t)
  1120. (unless (let ((sec-num (plist-get info :section-numbers))
  1121. (level (string-to-number (match-string 2))))
  1122. (if (wholenump sec-num) (<= level sec-num) sec-num))
  1123. (replace-match replacement t nil))))
  1124. (save-buffer 0)))
  1125. ;; Update content.xml.
  1126. (with-temp-buffer
  1127. (insert-file-contents
  1128. (or org-e-odt-content-template-file
  1129. (expand-file-name "OrgOdtContentTemplate.xml"
  1130. org-e-odt-styles-dir)))
  1131. ;; Write automatic styles.
  1132. ;; - Position the cursor.
  1133. (goto-char (point-min))
  1134. (re-search-forward " </office:automatic-styles>" nil t)
  1135. (goto-char (match-beginning 0))
  1136. ;; - Dump automatic table styles
  1137. (loop for (style-name props) in
  1138. (plist-get org-e-odt-automatic-styles 'Table) do
  1139. (when (setq props (or (plist-get props :rel-width) 96))
  1140. (insert (format org-e-odt-table-style-format style-name props))))
  1141. ;; Update display level.
  1142. ;; - Remove existing sequence decls. Also position the cursor.
  1143. (goto-char (point-min))
  1144. (when (re-search-forward "<text:sequence-decls" nil t)
  1145. (delete-region (match-beginning 0)
  1146. (re-search-forward "</text:sequence-decls>" nil nil)))
  1147. ;; Update sequence decls according to user preference.
  1148. (insert
  1149. (format
  1150. "\n<text:sequence-decls>\n%s\n</text:sequence-decls>"
  1151. (mapconcat
  1152. (lambda (x)
  1153. (format
  1154. "<text:sequence-decl text:display-outline-level=\"%d\" text:name=\"%s\"/>"
  1155. org-e-odt-display-outline-level (nth 1 x)))
  1156. org-e-odt-category-map-alist "\n")))
  1157. ;; Position the cursor to document body.
  1158. (goto-char (point-min))
  1159. (re-search-forward "</office:text>" nil nil)
  1160. (goto-char (match-beginning 0))
  1161. ;; Preamble - Title, Author, Date etc.
  1162. (insert
  1163. (let* ((title (org-export-data (plist-get info :title) info))
  1164. (author (and (plist-get info :with-author)
  1165. (let ((auth (plist-get info :author)))
  1166. (and auth (org-export-data auth info)))))
  1167. (date (org-export-data (plist-get info :date) info))
  1168. (iso-date (org-e-odt--date date))
  1169. (date (org-e-odt--date date "%d %b %Y"))
  1170. (email (plist-get info :email))
  1171. ;; switch on or off above vars based on user settings
  1172. (author (and (plist-get info :with-author) (or author email)))
  1173. ;; (date (and (plist-get info :time-stamp-file) date))
  1174. (email (and (plist-get info :with-email) email)))
  1175. (concat
  1176. ;; title
  1177. (when title
  1178. (concat
  1179. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1180. "OrgTitle" (format "\n<text:title>%s</text:title>" title))
  1181. ;; separator
  1182. "\n<text:p text:style-name=\"OrgTitle\"/>"))
  1183. (cond
  1184. ((and author (not email))
  1185. ;; author only
  1186. (concat
  1187. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1188. "OrgSubtitle"
  1189. (format "<text:initial-creator>%s</text:initial-creator>" author))
  1190. ;; separator
  1191. "\n<text:p text:style-name=\"OrgSubtitle\"/>"))
  1192. ((and author email)
  1193. ;; author and email
  1194. (concat
  1195. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1196. "OrgSubtitle"
  1197. (org-e-odt-format-link
  1198. (format "<text:initial-creator>%s</text:initial-creator>" author)
  1199. (concat "mailto:" email)))
  1200. ;; separator
  1201. "\n<text:p text:style-name=\"OrgSubtitle\"/>")))
  1202. ;; date
  1203. (when date
  1204. (concat
  1205. (format
  1206. "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1207. "OrgSubtitle"
  1208. (format
  1209. "\n<text:date style:data-style-name=\"%s\" text:date-value=\"%s\">%s</text:date>"
  1210. "N75" iso-date date))
  1211. ;; separator
  1212. "<text:p text:style-name=\"OrgSubtitle\"/>")))))
  1213. ;; Table of Contents
  1214. (let ((depth (plist-get info :with-toc)))
  1215. (when (wholenump depth) (insert (org-e-odt-toc depth info))))
  1216. ;; Contents.
  1217. (insert contents)
  1218. ;; Return contents.
  1219. (buffer-substring-no-properties (point-min) (point-max))))
  1220. ;;; Transcode Functions
  1221. ;;;; Bold
  1222. (defun org-e-odt-bold (bold contents info)
  1223. "Transcode BOLD from Org to ODT.
  1224. CONTENTS is the text with bold markup. INFO is a plist holding
  1225. contextual information."
  1226. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1227. "Bold" contents))
  1228. ;;;; Center Block
  1229. (defun org-e-odt-center-block (center-block contents info)
  1230. "Transcode a CENTER-BLOCK element from Org to ODT.
  1231. CONTENTS holds the contents of the center block. INFO is a plist
  1232. holding contextual information."
  1233. (org-e-odt--wrap-label center-block contents))
  1234. ;;;; Clock
  1235. (defun org-e-odt-clock (clock contents info)
  1236. "Transcode a CLOCK element from Org to ODT.
  1237. CONTENTS is nil. INFO is a plist used as a communication
  1238. channel."
  1239. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1240. "OrgTimestampWrapper"
  1241. (concat
  1242. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1243. "OrgTimestampKeyword" org-clock-string)
  1244. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1245. "OrgTimestamp"
  1246. (concat (org-translate-time
  1247. (org-element-property :value clock))
  1248. (let ((time (org-element-property :time clock)))
  1249. (and time (format " (%s)" time))))))))
  1250. ;;;; Code
  1251. (defun org-e-odt-code (code contents info)
  1252. "Transcode a CODE object from Org to ODT.
  1253. CONTENTS is nil. INFO is a plist used as a communication
  1254. channel."
  1255. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1256. "OrgCode" (org-element-property :value code)))
  1257. ;;;; Comment
  1258. ;; Comments are ignored.
  1259. ;;;; Comment Block
  1260. ;; Comment Blocks are ignored.
  1261. ;;;; Drawer
  1262. (defun org-e-odt-drawer (drawer contents info)
  1263. "Transcode a DRAWER element from Org to ODT.
  1264. CONTENTS holds the contents of the block. INFO is a plist
  1265. holding contextual information."
  1266. (let* ((name (org-element-property :drawer-name drawer))
  1267. (output (if (functionp org-e-odt-format-drawer-function)
  1268. (funcall org-e-odt-format-drawer-function
  1269. name contents)
  1270. ;; If there's no user defined function: simply
  1271. ;; display contents of the drawer.
  1272. contents)))
  1273. (org-e-odt--wrap-label drawer output)))
  1274. ;;;; Dynamic Block
  1275. (defun org-e-odt-dynamic-block (dynamic-block contents info)
  1276. "Transcode a DYNAMIC-BLOCK element from Org to ODT.
  1277. CONTENTS holds the contents of the block. INFO is a plist
  1278. holding contextual information. See `org-export-data'."
  1279. (org-e-odt--wrap-label dynamic-block contents))
  1280. ;;;; Entity
  1281. (defun org-e-odt-entity (entity contents info)
  1282. "Transcode an ENTITY object from Org to ODT.
  1283. CONTENTS are the definition itself. INFO is a plist holding
  1284. contextual information."
  1285. ;; (let ((ent (org-element-property :latex entity)))
  1286. ;; (if (org-element-property :latex-math-p entity)
  1287. ;; (format "$%s$" ent)
  1288. ;; ent))
  1289. (org-element-property :utf-8 entity))
  1290. ;;;; Example Block
  1291. (defun org-e-odt-example-block (example-block contents info)
  1292. "Transcode a EXAMPLE-BLOCK element from Org to ODT.
  1293. CONTENTS is nil. INFO is a plist holding contextual information."
  1294. (org-e-odt--wrap-label
  1295. example-block (org-e-odt-format-code example-block info)))
  1296. ;;;; Export Snippet
  1297. (defun org-e-odt-export-snippet (export-snippet contents info)
  1298. "Transcode a EXPORT-SNIPPET object from Org to ODT.
  1299. CONTENTS is nil. INFO is a plist holding contextual information."
  1300. (when (eq (org-export-snippet-backend export-snippet) 'e-odt)
  1301. (org-element-property :value export-snippet)))
  1302. ;;;; Export Block
  1303. (defun org-e-odt-export-block (export-block contents info)
  1304. "Transcode a EXPORT-BLOCK element from Org to ODT.
  1305. CONTENTS is nil. INFO is a plist holding contextual information."
  1306. (when (string= (org-element-property :type export-block) "ODT")
  1307. (org-remove-indentation (org-element-property :value export-block))))
  1308. ;;;; Fixed Width
  1309. (defun org-e-odt-fixed-width (fixed-width contents info)
  1310. "Transcode a FIXED-WIDTH element from Org to ODT.
  1311. CONTENTS is nil. INFO is a plist holding contextual information."
  1312. (org-e-odt--wrap-label
  1313. fixed-width (org-e-odt-do-format-code
  1314. (org-element-property :value fixed-width))))
  1315. ;;;; Footnote Definition
  1316. ;; Footnote Definitions are ignored.
  1317. ;;;; Footnote Reference
  1318. (defun org-e-odt-footnote-reference (footnote-reference contents info)
  1319. "Transcode a FOOTNOTE-REFERENCE element from Org to ODT.
  1320. CONTENTS is nil. INFO is a plist holding contextual information."
  1321. (let ((--format-footnote-definition
  1322. (function
  1323. (lambda (n def)
  1324. (setq n (format "%d" n))
  1325. (let ((id (concat "fn" n))
  1326. (note-class "footnote")
  1327. (par-style "Footnote"))
  1328. (format
  1329. "<text:note text:id=\"%s\" text:note-class=\"%s\">%s</text:note>"
  1330. id note-class
  1331. (concat
  1332. (format "<text:note-citation>%s</text:note-citation>" n)
  1333. (format "<text:note-body>%s</text:note-body>" def)))))))
  1334. (--format-footnote-reference
  1335. (function
  1336. (lambda (n)
  1337. (setq n (format "%d" n))
  1338. (let ((note-class "footnote")
  1339. (ref-format "text")
  1340. (ref-name (concat "fn" n)))
  1341. (format
  1342. "<text:span text:style-name=\"%s\">%s</text:span>"
  1343. "OrgSuperscript"
  1344. (format "<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:note-ref>"
  1345. note-class ref-format ref-name n)))))))
  1346. (concat
  1347. ;; Insert separator between two footnotes in a row.
  1348. (let ((prev (org-export-get-previous-element footnote-reference info)))
  1349. (and (eq (org-element-type prev) 'footnote-reference)
  1350. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1351. "OrgSuperscript" ",")))
  1352. ;; Trancode footnote reference.
  1353. (let ((n (org-export-get-footnote-number footnote-reference info)))
  1354. (cond
  1355. ((not (org-export-footnote-first-reference-p footnote-reference info))
  1356. (funcall --format-footnote-reference n))
  1357. ;; Inline definitions are secondary strings.
  1358. ;; Non-inline footnotes definitions are full Org data.
  1359. (t
  1360. (let* ((raw (org-export-get-footnote-definition footnote-reference
  1361. info))
  1362. (def (let ((def (org-trim (org-export-data raw info))))
  1363. (if (eq (org-element-type raw) 'org-data) def
  1364. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1365. "Footnote" def)))))
  1366. (funcall --format-footnote-definition n def))))))))
  1367. ;;;; Headline
  1368. (defun* org-e-odt-format-headline
  1369. (todo todo-type priority text tags
  1370. &key level section-number headline-label &allow-other-keys)
  1371. (concat
  1372. ;; Todo.
  1373. (and todo
  1374. (concat
  1375. (let ((style (if (member todo org-done-keywords) "OrgDone" "OrgTodo")))
  1376. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1377. style todo)) " "))
  1378. ;; Title.
  1379. text
  1380. ;; Tags.
  1381. (and tags
  1382. (concat "<text:tab/>"
  1383. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1384. "OrgTag" (mapconcat 'org-trim tags " : "))))))
  1385. (defun org-e-odt-format-headline--wrap (headline info
  1386. &optional format-function
  1387. &rest extra-keys)
  1388. "Transcode an HEADLINE element from Org to ODT.
  1389. CONTENTS holds the contents of the headline. INFO is a plist
  1390. holding contextual information."
  1391. (let* ((level (+ (org-export-get-relative-level headline info)))
  1392. (headline-number (org-export-get-headline-number headline info))
  1393. (section-number (and (org-export-numbered-headline-p headline info)
  1394. (mapconcat 'number-to-string
  1395. headline-number ".")))
  1396. (todo (and (plist-get info :with-todo-keywords)
  1397. (let ((todo (org-element-property :todo-keyword headline)))
  1398. (and todo (org-export-data todo info)))))
  1399. (todo-type (and todo (org-element-property :todo-type headline)))
  1400. (priority (and (plist-get info :with-priority)
  1401. (org-element-property :priority headline)))
  1402. (text (org-export-data (org-element-property :title headline) info))
  1403. (tags (and (plist-get info :with-tags)
  1404. (org-export-get-tags headline info)))
  1405. (headline-label (concat "sec-" (mapconcat 'number-to-string
  1406. headline-number "-")))
  1407. (format-function (cond
  1408. ((functionp format-function) format-function)
  1409. ((functionp org-e-odt-format-headline-function)
  1410. (function*
  1411. (lambda (todo todo-type priority text tags
  1412. &allow-other-keys)
  1413. (funcall org-e-odt-format-headline-function
  1414. todo todo-type priority text tags))))
  1415. (t 'org-e-odt-format-headline))))
  1416. (apply format-function
  1417. todo todo-type priority text tags
  1418. :headline-label headline-label :level level
  1419. :section-number section-number extra-keys)))
  1420. (defun org-e-odt-begin-plain-list (ltype &optional continue-numbering)
  1421. (unless (member ltype '(ordered unordered descriptive))
  1422. (error "Unknown list type: %s" ltype))
  1423. (let ((style-name (assoc-default ltype
  1424. '((ordered . "OrgNumberedList")
  1425. (unordered . "OrgBulletedList")
  1426. (descriptive . "OrgDescriptionList")))))
  1427. (format "<text:list text:style-name=\"%s\" text:continue-numbering=\"%s\">"
  1428. style-name (if continue-numbering "true" "false"))))
  1429. (defun org-e-odt-headline (headline contents info)
  1430. "Transcode an HEADLINE element from Org to ODT.
  1431. CONTENTS holds the contents of the headline. INFO is a plist
  1432. holding contextual information."
  1433. (let* ((numberedp (org-export-numbered-headline-p headline info))
  1434. ;; Get level relative to current parsed data.
  1435. (level (org-export-get-relative-level headline info))
  1436. (text (org-export-data (org-element-property :title headline) info))
  1437. ;; Create the headline text.
  1438. (full-text (org-e-odt-format-headline--wrap headline info)))
  1439. (cond
  1440. ;; Case 1: This is a footnote section: ignore it.
  1441. ((org-element-property :footnote-section-p headline) nil)
  1442. ;; Case 2. This is a deep sub-tree: export it as a list item.
  1443. ;; Also export as items headlines for which no section
  1444. ;; format has been found.
  1445. ;; FIXME
  1446. ;; ((org-export-low-level-p headline info)
  1447. ;; ;; Build the real contents of the sub-tree.
  1448. ;; (let* ((type (if numberedp 'unordered 'unordered)) ; FIXME
  1449. ;; (itemized-body (org-e-odt-format-list-item
  1450. ;; contents type nil nil full-text)))
  1451. ;; (concat
  1452. ;; (and (org-export-first-sibling-p headline info)
  1453. ;; (org-e-odt-begin-plain-list type))
  1454. ;; itemized-body
  1455. ;; (and (org-export-last-sibling-p headline info)
  1456. ;; "</text:list>"))))
  1457. ;; Case 3. Standard headline. Export it as a section.
  1458. (t
  1459. (let* ((extra-ids (list (org-element-property :custom-id headline)
  1460. (org-element-property :id headline)))
  1461. (extra-ids nil) ; FIXME
  1462. (id (concat "sec-" (mapconcat 'number-to-string
  1463. (org-export-get-headline-number
  1464. headline info) "-"))))
  1465. (concat
  1466. (format
  1467. "\n<text:h text:style-name=\"%s\" text:outline-level=\"%s\">%s%s</text:h>"
  1468. (format "Heading_20_%s" level)
  1469. level
  1470. ;; Extra targets.
  1471. (mapconcat (lambda (x)
  1472. (when x
  1473. (let ((x (if (org-uuidgen-p x) (concat "ID-" x) x)))
  1474. (org-e-odt-format-target
  1475. "" (org-export-solidify-link-text x)))))
  1476. extra-ids "")
  1477. ;; Title.
  1478. (org-e-odt-format-target full-text id))
  1479. contents))))))
  1480. ;;;; Horizontal Rule
  1481. (defun org-e-odt-horizontal-rule (horizontal-rule contents info)
  1482. "Transcode an HORIZONTAL-RULE object from Org to ODT.
  1483. CONTENTS is nil. INFO is a plist holding contextual information."
  1484. (org-e-odt--wrap-label
  1485. horizontal-rule
  1486. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1487. "Horizontal_20_Line" "")))
  1488. ;;;; Inline Babel Call
  1489. ;; Inline Babel Calls are ignored.
  1490. ;;;; Inline Src Block
  1491. (defun org-e-odt--find-verb-separator (s)
  1492. "Return a character not used in string S.
  1493. This is used to choose a separator for constructs like \\verb."
  1494. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  1495. (loop for c across ll
  1496. when (not (string-match (regexp-quote (char-to-string c)) s))
  1497. return (char-to-string c))))
  1498. (defun org-e-odt-inline-src-block (inline-src-block contents info)
  1499. "Transcode an INLINE-SRC-BLOCK element from Org to ODT.
  1500. CONTENTS holds the contents of the item. INFO is a plist holding
  1501. contextual information."
  1502. (let* ((org-lang (org-element-property :language inline-src-block))
  1503. (code (org-element-property :value inline-src-block))
  1504. (separator (org-e-odt--find-verb-separator code)))
  1505. (error "FIXME")))
  1506. ;;;; Inlinetask
  1507. (defun org-e-odt-inlinetask (inlinetask contents info)
  1508. "Transcode an INLINETASK element from Org to ODT.
  1509. CONTENTS holds the contents of the block. INFO is a plist
  1510. holding contextual information."
  1511. (cond
  1512. ;; If `org-e-odt-format-inlinetask-function' is provided, call it
  1513. ;; with appropriate arguments.
  1514. ((functionp org-e-odt-format-inlinetask-function)
  1515. (let ((format-function
  1516. (function*
  1517. (lambda (todo todo-type priority text tags
  1518. &key contents &allow-other-keys)
  1519. (funcall org-e-odt-format-inlinetask-function
  1520. todo todo-type priority text tags contents)))))
  1521. (org-e-odt-format-headline--wrap
  1522. inlinetask info format-function :contents contents)))
  1523. ;; Otherwise, use a default template.
  1524. (t (org-e-odt--wrap-label
  1525. inlinetask
  1526. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1527. "Text_20_body"
  1528. (org-e-odt--textbox
  1529. (concat
  1530. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1531. "OrgInlineTaskHeading"
  1532. (org-e-odt-format-headline--wrap
  1533. inlinetask info))
  1534. contents)
  1535. nil nil "OrgInlineTaskFrame" " style:rel-width=\"100%\""))))))
  1536. ;;;; Italic
  1537. (defun org-e-odt-italic (italic contents info)
  1538. "Transcode ITALIC from Org to ODT.
  1539. CONTENTS is the text with italic markup. INFO is a plist holding
  1540. contextual information."
  1541. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1542. "Emphasis" contents))
  1543. ;;;; Item
  1544. (defun org-e-odt-item (item contents info)
  1545. "Transcode an ITEM element from Org to ODT.
  1546. CONTENTS holds the contents of the item. INFO is a plist holding
  1547. contextual information."
  1548. (let* ((plain-list (org-export-get-parent item))
  1549. (type (org-element-property :type plain-list))
  1550. (counter (org-element-property :counter item))
  1551. (tag (let ((tag (org-element-property :tag item)))
  1552. (and tag
  1553. (concat (org-e-odt--checkbox item)
  1554. (org-export-data tag info))))))
  1555. (case type
  1556. ((ordered unordered)
  1557. (format "\n<text:list-item>\n%s\n%s"
  1558. contents
  1559. (let* ((--element-has-a-table-p
  1560. (function
  1561. (lambda (element info)
  1562. (loop for el in (org-element-contents element)
  1563. thereis (eq (org-element-type el) 'table))))))
  1564. (cond
  1565. ((funcall --element-has-a-table-p item info)
  1566. "</text:list-header>")
  1567. (t "</text:list-item>")))))
  1568. (descriptive
  1569. (concat
  1570. (let ((term (or tag "(no term)")))
  1571. (concat
  1572. (format "\n<text:list-item>\n%s\n</text:list-item>"
  1573. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1574. "Text_20_body_20_bold" term))
  1575. (format
  1576. "\n<text:list-item>\n%s\n</text:list-item>"
  1577. (format "\n<text:list text:style-name=\"%s\" %s>\n%s\n</text:list>"
  1578. "OrgDescriptionList"
  1579. "text:continue-numbering=\"false\""
  1580. (format "\n<text:list-item>\n%s\n</text:list-item>"
  1581. contents)))))))
  1582. (t (error "Unknown list type: %S" type)))))
  1583. ;;;; Keyword
  1584. (defun org-e-odt-keyword (keyword contents info)
  1585. "Transcode a KEYWORD element from Org to ODT.
  1586. CONTENTS is nil. INFO is a plist holding contextual information."
  1587. (let ((key (org-element-property :key keyword))
  1588. (value (org-element-property :value keyword)))
  1589. (cond
  1590. ((string= key "LATEX") value)
  1591. ((string= key "INDEX") (format "\\index{%s}" value))
  1592. ((string= key "TARGET") nil ; FIXME
  1593. ;; (format "\\label{%s}" (org-export-solidify-link-text value))
  1594. )
  1595. ((string= key "toc")
  1596. (let ((value (downcase value)))
  1597. (cond
  1598. ((string-match "\\<headlines\\>" value)
  1599. (let ((depth (or (and (string-match "[0-9]+" value)
  1600. (string-to-number (match-string 0 value)))
  1601. (plist-get info :with-toc))))
  1602. (when (wholenump depth) (org-e-odt-toc depth info))))
  1603. ((string= "tables" value) "FIXME")
  1604. ((string= "figures" value) "FIXME")
  1605. ((string= "listings" value)
  1606. (cond
  1607. ;; At the moment, src blocks with a caption are wrapped
  1608. ;; into a figure environment.
  1609. (t "FIXME")))))))))
  1610. ;;;; Latex Environment
  1611. (eval-after-load 'org-odt
  1612. '(ad-deactivate 'org-format-latex-as-mathml))
  1613. ;; (defadvice org-format-latex-as-mathml ; FIXME
  1614. ;; (after org-e-odt-protect-latex-fragment activate)
  1615. ;; "Encode LaTeX fragment as XML.
  1616. ;; Do this when translation to MathML fails."
  1617. ;; (when (or (not (> (length ad-return-value) 0))
  1618. ;; (get-text-property 0 'org-protected ad-return-value))
  1619. ;; (setq ad-return-value
  1620. ;; (org-propertize (org-e-odt-encode-plain-text (ad-get-arg 0))
  1621. ;; 'org-protected t))))
  1622. (defun org-e-odt-format-latex (latex-frag processing-type info)
  1623. (let* ((prefix (case processing-type
  1624. (dvipng "ltxpng/")
  1625. (mathml "ltxmathml/")))
  1626. (input-file (plist-get info :input-file))
  1627. (cache-subdir
  1628. (concat prefix (file-name-sans-extension
  1629. (file-name-nondirectory input-file))))
  1630. (cache-dir (file-name-directory input-file))
  1631. (display-msg (case processing-type
  1632. (dvipng "Creating LaTeX Image...")
  1633. (mathml "Creating MathML snippet..."))))
  1634. (with-temp-buffer
  1635. (insert latex-frag)
  1636. (org-format-latex cache-subdir cache-dir nil display-msg
  1637. nil nil processing-type)
  1638. (buffer-string))))
  1639. (defun org-e-odt-latex-environment (latex-environment contents info)
  1640. "Transcode a LATEX-ENVIRONMENT element from Org to ODT.
  1641. CONTENTS is nil. INFO is a plist holding contextual information."
  1642. (org-e-odt--wrap-label
  1643. latex-environment
  1644. (let* ((latex-frag
  1645. (org-remove-indentation
  1646. (org-element-property :value latex-environment)))
  1647. (processing-type (plist-get info :LaTeX-fragments))
  1648. (caption (org-element-property :caption latex-environment))
  1649. (short-caption (and (cdr caption)
  1650. (org-export-data (cdr caption) info)))
  1651. (caption (and (car caption) (org-export-data (car caption) info)))
  1652. (label (org-element-property :name latex-environment))
  1653. (attr nil) ; FIXME
  1654. (label (org-element-property :name latex-environment)))
  1655. (when (memq processing-type '(t mathjax))
  1656. (unless (and (fboundp 'org-format-latex-mathml-available-p)
  1657. (org-format-latex-mathml-available-p))
  1658. (message "LaTeX to MathML converter not available. Trying dvinpng...")
  1659. (setq processing-type 'dvipng)))
  1660. (when (eq processing-type 'dvipng)
  1661. (unless (and (org-check-external-command "latex" "" t)
  1662. (org-check-external-command "dvipng" "" t))
  1663. (message "LaTeX to PNG converter not available. Using verbatim.")
  1664. (setq processing-type 'verbatim)))
  1665. (case processing-type
  1666. ((t mathjax)
  1667. (org-e-odt-format-formula latex-environment info))
  1668. (dvipng
  1669. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1670. "Text_20_body"
  1671. (org-e-odt-link--inline-image latex-environment info)))
  1672. (t (org-e-odt-do-format-code latex-frag))))))
  1673. ;;;; Latex Fragment
  1674. ;; (when latex-frag ; FIXME
  1675. ;; (setq href (org-propertize href :title "LaTeX Fragment"
  1676. ;; :description latex-frag)))
  1677. ;; handle verbatim
  1678. ;; provide descriptions
  1679. (defun org-e-odt-latex-fragment (latex-fragment contents info)
  1680. "Transcode a LATEX-FRAGMENT object from Org to ODT.
  1681. CONTENTS is nil. INFO is a plist holding contextual information."
  1682. (let* ((latex-frag (org-element-property :value latex-fragment))
  1683. (processing-type (plist-get info :LaTeX-fragments)))
  1684. (cond
  1685. ((member processing-type '(t mathjax))
  1686. (org-e-odt-format-formula latex-fragment info))
  1687. ((eq processing-type 'dvipng)
  1688. (org-e-odt-link--inline-image latex-fragment info))
  1689. (t (org-e-odt-encode-plain-text latex-frag t)))))
  1690. ;;;; Line Break
  1691. (defun org-e-odt-line-break (line-break contents info)
  1692. "Transcode a LINE-BREAK object from Org to ODT.
  1693. CONTENTS is nil. INFO is a plist holding contextual information."
  1694. "<text:line-break/>\n")
  1695. ;;;; Link
  1696. ;;;; Links :: Generic
  1697. (defun org-e-odt-format-link (desc href &optional suppress-xref)
  1698. (cond
  1699. ((and (= (string-to-char href) ?#) (not suppress-xref))
  1700. (setq href (substring href 1))
  1701. (let ((xref-format "text"))
  1702. (when (numberp desc)
  1703. (setq desc (format "%d" desc) xref-format "number"))
  1704. (when (listp desc)
  1705. (setq desc (mapconcat 'number-to-string desc ".") xref-format "chapter"))
  1706. (setq href (concat org-e-odt-bookmark-prefix href))
  1707. (format
  1708. "<text:bookmark-ref text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:bookmark-ref>"
  1709. xref-format href desc)))
  1710. ;; (org-lparse-link-description-is-image
  1711. ;; (format "\n<draw:a xlink:type=\"simple\" xlink:href=\"%s\">\n%s\n</draw:a>"
  1712. ;; href desc))
  1713. (t (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
  1714. href desc))))
  1715. (defun org-e-odt-format-internal-link (text href)
  1716. (org-e-odt-format-link text (concat "#" href)))
  1717. ;;;; Links :: Label references
  1718. (defun org-e-odt-enumerate-element (element info &optional predicate n)
  1719. (let* ((--numbered-parent-headline-at-<=-n
  1720. (function
  1721. (lambda (element n info)
  1722. (loop for x in (org-export-get-genealogy element)
  1723. thereis (and (eq (org-element-type x) 'headline)
  1724. (<= (org-export-get-relative-level x info) n)
  1725. (org-export-numbered-headline-p x info)
  1726. x)))))
  1727. (--enumerate
  1728. (function
  1729. (lambda (element scope info &optional predicate)
  1730. (let ((counter 0))
  1731. (org-element-map
  1732. (or scope (plist-get info :parse-tree))
  1733. (org-element-type element)
  1734. (lambda (el)
  1735. (and (or (not predicate) (funcall predicate el info))
  1736. (incf counter)
  1737. (eq element el)
  1738. counter))
  1739. info 'first-match)))))
  1740. (scope (funcall --numbered-parent-headline-at-<=-n
  1741. element (or n org-e-odt-display-outline-level) info))
  1742. (ordinal (funcall --enumerate element scope info predicate))
  1743. (tag
  1744. (concat
  1745. ;; Section number.
  1746. (and scope
  1747. (mapconcat 'number-to-string
  1748. (org-export-get-headline-number scope info) "."))
  1749. ;; Separator.
  1750. (and scope ".")
  1751. ;; Ordinal.
  1752. (number-to-string ordinal))))
  1753. tag))
  1754. (defun org-e-odt-format-label (element info op)
  1755. (let* ((caption-from
  1756. (case (org-element-type element)
  1757. (link (org-export-get-parent-element element))
  1758. (t element)))
  1759. ;; get label and caption.
  1760. (label (org-element-property :name caption-from))
  1761. (caption (org-element-property :caption caption-from))
  1762. (short-caption (cdr caption))
  1763. ;; transcode captions.
  1764. (caption (and (car caption) (org-export-data (car caption) info)))
  1765. (short-caption (and short-caption
  1766. (org-export-data short-caption info))))
  1767. (when (or label caption)
  1768. (let* ((default-category
  1769. (cond
  1770. ((eq (org-element-type element) 'table)
  1771. "__Table__")
  1772. ((org-e-odt-standalone-image-p element info)
  1773. "__Figure__")
  1774. ((member (org-element-type element)
  1775. '(latex-environment latex-fragment))
  1776. (let ((processing-type (plist-get info :LaTeX-fragments)))
  1777. (cond
  1778. ((eq processing-type 'dvipng) "__DvipngImage__")
  1779. ((eq processing-type 'mathjax) "__MathFormula__")
  1780. ((eq processing-type 't) "__MathFormula__")
  1781. (t (error "Handle LaTeX:verbatim")))))
  1782. ((eq (org-element-type element) 'src-block)
  1783. "__Listing__")
  1784. (t (error "Handle enumeration of %S" element))))
  1785. (predicate
  1786. (cond
  1787. ((member (org-element-type element)
  1788. '(table latex-environment src-block))
  1789. nil)
  1790. ((org-e-odt-standalone-image-p element info)
  1791. 'org-e-odt-standalone-image-p)
  1792. (t (error "Handle enumeration of %S" element))))
  1793. (seqno (org-e-odt-enumerate-element
  1794. element info predicate)) ; FIXME
  1795. ;; handle label props.
  1796. (label-props (assoc default-category org-e-odt-category-map-alist))
  1797. ;; identify opendocument counter
  1798. (counter (nth 1 label-props))
  1799. ;; identify label style
  1800. (label-style (nth 2 label-props))
  1801. ;; retrieve localized category sting
  1802. (category (org-export-translate (nth 3 label-props) :utf-8 info)))
  1803. (case op
  1804. (definition
  1805. ;; assign an internal label, if user has not provided one
  1806. (setq label (or label (format "%s-%s" default-category seqno)))
  1807. (setq label (org-export-solidify-link-text label))
  1808. (cons
  1809. (format-spec
  1810. (cadr (assoc-string label-style org-e-odt-label-styles t))
  1811. `((?e . ,category)
  1812. (?n . ,(format
  1813. "<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">%s</text:sequence>"
  1814. label counter counter seqno))
  1815. (?c . ,(or caption ""))))
  1816. short-caption))
  1817. (reference
  1818. (assert label)
  1819. (setq label (org-export-solidify-link-text label))
  1820. (let* ((fmt (cddr (assoc-string label-style org-e-odt-label-styles t)))
  1821. (fmt1 (car fmt))
  1822. (fmt2 (cadr fmt)))
  1823. (format "<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:sequence-ref>"
  1824. fmt1 label (format-spec fmt2 `((?e . ,category)
  1825. (?n . ,seqno))))))
  1826. (t (error "Unknow %S on label" op)))))))
  1827. ;;;; Links :: Embedded images
  1828. (defun org-e-odt-copy-image-file (path)
  1829. "Returns the internal name of the file"
  1830. (let* ((image-type (file-name-extension path))
  1831. (media-type (format "image/%s" image-type))
  1832. (target-dir "Images/")
  1833. (target-file
  1834. (format "%s%04d.%s" target-dir
  1835. (incf org-e-odt-embedded-images-count) image-type)))
  1836. (message "Embedding %s as %s ..."
  1837. (substring-no-properties path) target-file)
  1838. (when (= 1 org-e-odt-embedded-images-count)
  1839. (make-directory (concat org-e-odt-zip-dir target-dir))
  1840. (org-e-odt-create-manifest-file-entry "" target-dir))
  1841. (copy-file path (concat org-e-odt-zip-dir target-file) 'overwrite)
  1842. (org-e-odt-create-manifest-file-entry media-type target-file)
  1843. target-file))
  1844. (defun org-e-odt-image-size-from-file (file &optional user-width
  1845. user-height scale dpi embed-as)
  1846. (let* ((--pixels-to-cms
  1847. (function (lambda (pixels dpi)
  1848. (let ((cms-per-inch 2.54)
  1849. (inches (/ pixels dpi)))
  1850. (* cms-per-inch inches)))))
  1851. (--size-in-cms
  1852. (function
  1853. (lambda (size-in-pixels dpi)
  1854. (and size-in-pixels
  1855. (cons (funcall --pixels-to-cms (car size-in-pixels) dpi)
  1856. (funcall --pixels-to-cms (cdr size-in-pixels) dpi))))))
  1857. (dpi (or dpi org-e-odt-pixels-per-inch))
  1858. (anchor-type (or embed-as "paragraph"))
  1859. (user-width (and (not scale) user-width))
  1860. (user-height (and (not scale) user-height))
  1861. (size
  1862. (and
  1863. (not (and user-height user-width))
  1864. (or
  1865. ;; Use Imagemagick.
  1866. (and (executable-find "identify")
  1867. (let ((size-in-pixels
  1868. (let ((dim (shell-command-to-string
  1869. (format "identify -format \"%%w:%%h\" \"%s\""
  1870. file))))
  1871. (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
  1872. (cons (string-to-number (match-string 1 dim))
  1873. (string-to-number (match-string 2 dim)))))))
  1874. (funcall --size-in-cms size-in-pixels dpi)))
  1875. ;; Use Emacs.
  1876. (let ((size-in-pixels
  1877. (ignore-errors ; Emacs could be in batch mode
  1878. (clear-image-cache)
  1879. (image-size (create-image file) 'pixels))))
  1880. (funcall --size-in-cms size-in-pixels dpi))
  1881. ;; Use hard-coded values.
  1882. (cdr (assoc-string anchor-type
  1883. org-e-odt-default-image-sizes-alist))
  1884. ;; Error out.
  1885. (error "Cannot determine Image size. Aborting ..."))))
  1886. (width (car size)) (height (cdr size)))
  1887. (cond
  1888. (scale
  1889. (setq width (* width scale) height (* height scale)))
  1890. ((and user-height user-width)
  1891. (setq width user-width height user-height))
  1892. (user-height
  1893. (setq width (* user-height (/ width height)) height user-height))
  1894. (user-width
  1895. (setq height (* user-width (/ height width)) width user-width))
  1896. (t (ignore)))
  1897. ;; ensure that an embedded image fits comfortably within a page
  1898. (let ((max-width (car org-e-odt-max-image-size))
  1899. (max-height (cdr org-e-odt-max-image-size)))
  1900. (when (or (> width max-width) (> height max-height))
  1901. (let* ((scale1 (/ max-width width))
  1902. (scale2 (/ max-height height))
  1903. (scale (min scale1 scale2)))
  1904. (setq width (* scale width) height (* scale height)))))
  1905. (cons width height)))
  1906. ;;;; Links :: Math formula
  1907. (defun org-e-odt-format-formula (element info)
  1908. (let* ((src (cond
  1909. ((eq (org-element-type element) 'link) ; FIXME
  1910. (let* ((type (org-element-property :type element))
  1911. (raw-path (org-element-property :path element)))
  1912. (cond
  1913. ((file-name-absolute-p raw-path)
  1914. (expand-file-name raw-path))
  1915. (t raw-path))))
  1916. ((member (org-element-type element)
  1917. '(latex-fragment latex-environment))
  1918. (let* ((latex-frag (org-remove-indentation
  1919. (org-element-property :value element)))
  1920. (formula-link (org-e-odt-format-latex
  1921. latex-frag 'mathml info)))
  1922. (and formula-link
  1923. (string-match "file:\\([^]]*\\)" formula-link)
  1924. (match-string 1 formula-link))))
  1925. (t (error "what is this?"))))
  1926. (full-src (if (file-name-absolute-p src) src
  1927. (expand-file-name src (file-name-directory
  1928. (plist-get info :input-file)))))
  1929. (caption-from
  1930. (case (org-element-type element)
  1931. (link (org-export-get-parent-element element))
  1932. (t element)))
  1933. (captions (org-e-odt-format-label caption-from info 'definition))
  1934. (caption (car captions))
  1935. (href
  1936. (format "\n<draw:object %s xlink:href=\"%s\" xlink:type=\"simple\"/>"
  1937. " xlink:show=\"embed\" xlink:actuate=\"onLoad\""
  1938. (file-name-directory (org-e-odt-copy-formula-file full-src))))
  1939. (embed-as (if caption 'paragraph 'character))
  1940. width height)
  1941. (cond
  1942. ((eq embed-as 'character)
  1943. (org-e-odt-format-entity "InlineFormula" href width height))
  1944. (t
  1945. (let* ((equation (org-e-odt-format-entity
  1946. "CaptionedDisplayFormula" href width height captions))
  1947. (label
  1948. (let* ((org-e-odt-category-map-alist
  1949. '(("__Table__" "Table" "value")
  1950. ("__Figure__" "Illustration" "value")
  1951. ("__MathFormula__" "Text" "math-label")
  1952. ("__DvipngImage__" "Equation" "value")
  1953. ("__Listing__" "Listing" "value"))))
  1954. (car (org-e-odt-format-label caption-from info 'definition))))
  1955. (formula-tree
  1956. (org-e-odt--adopt-elements
  1957. `(table (:type org :attr_odt (":style \"OrgEquation\"")))
  1958. (org-e-odt--adopt-elements
  1959. `(table-row (:type standard))
  1960. `(table-cell nil "<c8>") `(table-cell nil "<c1>"))
  1961. (org-e-odt--adopt-elements
  1962. `(table-row (:type standard))
  1963. (org-e-odt--adopt-elements
  1964. `(table-cell nil) `(export-block
  1965. (:type "ODT" :value ,equation)))
  1966. (org-e-odt--adopt-elements
  1967. `(table-cell nil) `(export-block
  1968. (:type "ODT" :value ,label))))))
  1969. (formula-info
  1970. (org-export-collect-tree-properties
  1971. formula-tree (org-export-get-environment 'e-odt))))
  1972. (org-export-data formula-tree formula-info))))))
  1973. (defun org-e-odt-copy-formula-file (src-file)
  1974. "Returns the internal name of the file"
  1975. (let* ((target-dir (format "Formula-%04d/"
  1976. (incf org-e-odt-embedded-formulas-count)))
  1977. (target-file (concat target-dir "content.xml")))
  1978. ;; Create a directory for holding formula file. Also enter it in
  1979. ;; to manifest.
  1980. (make-directory (concat org-e-odt-zip-dir target-dir))
  1981. (org-e-odt-create-manifest-file-entry
  1982. "application/vnd.oasis.opendocument.formula" target-dir "1.2")
  1983. ;; Copy over the formula file from user directory to zip
  1984. ;; directory.
  1985. (message "Embedding %s as %s ..." src-file target-file)
  1986. (let ((case-fold-search nil))
  1987. (cond
  1988. ;; Case 1: Mathml.
  1989. ((string-match "\\.\\(mathml\\|mml\\)\\'" src-file)
  1990. (copy-file src-file (concat org-e-odt-zip-dir target-file) 'overwrite))
  1991. ;; Case 2: OpenDocument formula.
  1992. ((string-match "\\.odf\\'" src-file)
  1993. (org-e-odt--zip-extract src-file "content.xml"
  1994. (concat org-e-odt-zip-dir target-dir)))
  1995. (t (error "%s is not a formula file" src-file))))
  1996. ;; Enter the formula file in to manifest.
  1997. (org-e-odt-create-manifest-file-entry "text/xml" target-file)
  1998. target-file))
  1999. ;;;; Targets
  2000. (defun org-e-odt-format-target (text id)
  2001. (let ((name (concat org-e-odt-bookmark-prefix id)))
  2002. (concat
  2003. (and id (format "\n<text:bookmark-start text:name=\"%s\"/>" name))
  2004. (concat (and id (format "\n<text:bookmark text:name=\"%s\"/>" id)) text)
  2005. (and id (format "\n<text:bookmark-end text:name=\"%s\"/>" name)))))
  2006. (defun org-e-odt-link--inline-image (element info)
  2007. "Return HTML code for an inline image.
  2008. LINK is the link pointing to the inline image. INFO is a plist
  2009. used as a communication channel."
  2010. (let* ((src (cond
  2011. ((eq (org-element-type element) 'link)
  2012. (let* ((type (org-element-property :type element))
  2013. (raw-path (org-element-property :path element)))
  2014. (cond ((member type '("http" "https"))
  2015. (concat type ":" raw-path))
  2016. ((file-name-absolute-p raw-path)
  2017. (expand-file-name raw-path))
  2018. (t raw-path))))
  2019. ((member (org-element-type element)
  2020. '(latex-fragment latex-environment))
  2021. (let* ((latex-frag (org-remove-indentation
  2022. (org-element-property :value element)))
  2023. (formula-link (org-e-odt-format-latex
  2024. latex-frag 'dvipng info)))
  2025. (and formula-link
  2026. (string-match "file:\\([^]]*\\)" formula-link)
  2027. (match-string 1 formula-link))))
  2028. (t (error "what is this?"))))
  2029. (src-expanded (if (file-name-absolute-p src) src
  2030. (expand-file-name src (file-name-directory
  2031. (plist-get info :input-file)))))
  2032. (href (format
  2033. "\n<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>"
  2034. (org-e-odt-copy-image-file src-expanded)))
  2035. ;; extract attributes from #+ATTR_ODT line.
  2036. (attr-from (case (org-element-type element)
  2037. (link (org-export-get-parent-element element))
  2038. (t element)))
  2039. ;; convert attributes to a plist.
  2040. (attr-plist (org-export-read-attribute :attr_odt attr-from))
  2041. ;; handle `:anchor', `:style' and `:attributes' properties.
  2042. (user-frame-anchor
  2043. (car (assoc-string (plist-get attr-plist :anchor)
  2044. '(("as-char") ("paragraph") ("page")) t)))
  2045. (user-frame-style
  2046. (and user-frame-anchor (plist-get attr-plist :style)))
  2047. (user-frame-attrs
  2048. (and user-frame-anchor (plist-get attr-plist :attributes)))
  2049. (user-frame-params
  2050. (list user-frame-style user-frame-attrs user-frame-anchor))
  2051. ;; (embed-as (or embed-as user-frame-anchor "paragraph"))
  2052. ;; extrac
  2053. ;; handle `:width', `:height' and `:scale' properties.
  2054. (size (org-e-odt-image-size-from-file
  2055. src-expanded (plist-get attr-plist :width)
  2056. (plist-get attr-plist :height)
  2057. (plist-get attr-plist :scale) nil ;; embed-as
  2058. "paragraph" ; FIXME
  2059. ))
  2060. (width (car size)) (height (cdr size))
  2061. (embed-as
  2062. (case (org-element-type element)
  2063. ((org-e-odt-standalone-image-p element info) "paragraph")
  2064. (latex-fragment "as-char")
  2065. (latex-environment "paragraph")
  2066. (t "paragraph")))
  2067. (captions (org-e-odt-format-label element info 'definition))
  2068. (caption (car captions)) (short-caption (cdr captions))
  2069. (entity (concat (and caption "Captioned") embed-as "Image")))
  2070. (org-e-odt-format-entity entity href width height
  2071. captions user-frame-params )))
  2072. (defun org-e-odt-format-entity (entity href width height &optional
  2073. captions user-frame-params)
  2074. (let* ((caption (car captions)) (short-caption (cdr captions))
  2075. (entity-style (assoc-string entity org-e-odt-entity-frame-styles t))
  2076. default-frame-params frame-params
  2077. (--merge-frame-params
  2078. (function
  2079. (lambda (default-frame-params user-frame-params)
  2080. (if (not user-frame-params) default-frame-params
  2081. (assert (= (length default-frame-params) 3))
  2082. (assert (= (length user-frame-params) 3))
  2083. (loop for user-frame-param in user-frame-params
  2084. for default-frame-param in default-frame-params
  2085. collect (or user-frame-param default-frame-param)))))))
  2086. (cond
  2087. ((not caption)
  2088. (setq default-frame-params (nth 2 entity-style))
  2089. (setq frame-params (funcall --merge-frame-params
  2090. default-frame-params user-frame-params))
  2091. (apply 'org-e-odt--frame href width height frame-params))
  2092. (t
  2093. (setq default-frame-params (nth 3 entity-style))
  2094. (setq frame-params (funcall --merge-frame-params
  2095. default-frame-params user-frame-params))
  2096. (apply 'org-e-odt--textbox
  2097. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2098. "Illustration"
  2099. (concat
  2100. (apply 'org-e-odt--frame href width height
  2101. (let ((entity-style-1 (copy-sequence
  2102. (nth 2 entity-style))))
  2103. (setcar (cdr entity-style-1)
  2104. (concat
  2105. (cadr entity-style-1)
  2106. (and short-caption
  2107. (format " draw:name=\"%s\" "
  2108. short-caption))))
  2109. entity-style-1))
  2110. caption))
  2111. width height frame-params)))))
  2112. (defun org-e-odt-standalone-image-p (element info)
  2113. "Test if ELEMENT is a standalone image for the purpose ODT export.
  2114. INFO is a plist holding contextual information.
  2115. Return non-nil, if ELEMENT is of type paragraph and it's sole
  2116. content, save for whitespaces, is a link that qualifies as an
  2117. inline image.
  2118. Return non-nil, if ELEMENT is of type link and it's containing
  2119. paragraph has no other content save for leading and trailing
  2120. whitespaces.
  2121. Return nil, otherwise.
  2122. Bind `org-e-odt-standalone-image-predicate' to constrain
  2123. paragraph further. For example, to check for only captioned
  2124. standalone images, do the following.
  2125. \(setq org-e-odt-standalone-image-predicate
  2126. \(lambda \(paragraph\)
  2127. \(org-element-property :caption paragraph\)\)\)
  2128. "
  2129. (let ((--standalone-image-predicate
  2130. (function (lambda (paragraph)
  2131. (or (org-element-property :caption paragraph)
  2132. (org-element-property :name paragraph)))))
  2133. (paragraph (case (org-element-type element)
  2134. (paragraph element)
  2135. (link (and (org-export-inline-image-p
  2136. element org-e-odt-inline-image-rules)
  2137. (org-export-get-parent element)))
  2138. (t nil))))
  2139. (when paragraph
  2140. (assert (eq (org-element-type paragraph) 'paragraph))
  2141. (when (funcall --standalone-image-predicate paragraph)
  2142. (let ((contents (org-element-contents paragraph)))
  2143. (loop for x in contents
  2144. with inline-image-count = 0
  2145. always (cond
  2146. ((eq (org-element-type x) 'plain-text)
  2147. (not (org-string-nw-p x)))
  2148. ((eq (org-element-type x) 'link)
  2149. (when (org-export-inline-image-p
  2150. x org-e-odt-inline-image-rules)
  2151. (= (incf inline-image-count) 1)))
  2152. (t nil))))))))
  2153. (defun org-e-odt-link (link desc info)
  2154. "Transcode a LINK object from Org to ODT.
  2155. DESC is the description part of the link, or the empty string.
  2156. INFO is a plist holding contextual information. See
  2157. `org-export-data'."
  2158. (let* ((type (org-element-property :type link))
  2159. (raw-path (org-element-property :path link))
  2160. ;; Ensure DESC really exists, or set it to nil.
  2161. (desc (and (not (string= desc "")) desc))
  2162. (imagep (org-export-inline-image-p
  2163. link org-e-odt-inline-image-rules))
  2164. (path (cond
  2165. ((member type '("http" "https" "ftp" "mailto"))
  2166. (concat type ":" raw-path))
  2167. ((string= type "file")
  2168. (when (string-match "\\(.+\\)::.+" raw-path)
  2169. (setq raw-path (match-string 1 raw-path)))
  2170. (if (file-name-absolute-p raw-path)
  2171. (concat "file://" (expand-file-name raw-path))
  2172. (concat "file://" raw-path)))
  2173. (t raw-path)))
  2174. protocol)
  2175. (cond
  2176. ;; Image file.
  2177. ((and (not desc) (org-export-inline-image-p
  2178. link org-e-odt-inline-image-rules))
  2179. (org-e-odt-link--inline-image link info))
  2180. ;; Radio target: Transcode target's contents and use them as
  2181. ;; link's description.
  2182. ((string= type "radio")
  2183. (let ((destination (org-export-resolve-radio-link link info)))
  2184. (when destination
  2185. (org-e-odt-format-internal-link
  2186. (org-export-data (org-element-contents destination) info)
  2187. (org-export-solidify-link-text path)))))
  2188. ;; Links pointing to an headline: Find destination and build
  2189. ;; appropriate referencing command.
  2190. ((member type '("custom-id" "fuzzy" "id"))
  2191. (let ((destination (if (string= type "fuzzy")
  2192. (org-export-resolve-fuzzy-link link info)
  2193. (org-export-resolve-id-link link info))))
  2194. (case (org-element-type destination)
  2195. ;; Fuzzy link points nowhere.
  2196. ('nil
  2197. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2198. "Emphasis" (or desc (org-export-data
  2199. (org-element-property
  2200. :raw-link link) info))))
  2201. ;; Fuzzy link points to an invisible target.
  2202. (keyword nil)
  2203. ;; LINK points to an headline. Check if LINK should display
  2204. ;; section numbers.
  2205. (headline
  2206. (let* ((headline-no (org-export-get-headline-number destination info))
  2207. (label (format "sec-%s" (mapconcat 'number-to-string
  2208. headline-no "-")))
  2209. (desc
  2210. ;; Case 1: Headline is numbered and LINK has no
  2211. ;; description or LINK's description matches
  2212. ;; headline's title. Display section number.
  2213. (if (and (org-export-numbered-headline-p destination info)
  2214. (or (not desc)
  2215. (string= desc (org-element-property
  2216. :raw-value destination))))
  2217. headline-no
  2218. ;; Case 2: Either the headline is un-numbered or
  2219. ;; LINK has a custom description. Display LINK's
  2220. ;; description or headline's title.
  2221. (or desc (org-export-data (org-element-property
  2222. :title destination) info)))))
  2223. (org-e-odt-format-internal-link desc label)))
  2224. ;; Fuzzy link points to a target. Do as above.
  2225. (otherwise
  2226. ;; (unless desc
  2227. ;; (setq number (cond
  2228. ;; ((org-e-odt-standalone-image-p destination info)
  2229. ;; (org-export-get-ordinal
  2230. ;; (assoc 'link (org-element-contents destination))
  2231. ;; info 'link 'org-e-odt-standalone-image-p))
  2232. ;; (t (org-export-get-ordinal destination info))))
  2233. ;; (setq desc (when number
  2234. ;; (if (atom number) (number-to-string number)
  2235. ;; (mapconcat 'number-to-string number ".")))))
  2236. (let ((label-reference
  2237. (org-e-odt-format-label destination info 'reference)))
  2238. (assert label-reference)
  2239. label-reference)))))
  2240. ;; Coderef: replace link with the reference name or the
  2241. ;; equivalent line number.
  2242. ((string= type "coderef")
  2243. (let* ((fmt (org-export-get-coderef-format path desc))
  2244. (res (org-export-resolve-coderef path info))
  2245. (href (concat "#coderef-" path)))
  2246. (format fmt (org-e-odt-format-link res href))))
  2247. ;; Link type is handled by a special function.
  2248. ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
  2249. (funcall protocol (org-link-unescape path) desc 'odt))
  2250. ;; External link with a description part.
  2251. ((and path desc) (org-e-odt-format-link desc path))
  2252. ;; External link without a description part.
  2253. (path (org-e-odt-format-link path path))
  2254. ;; No path, only description. Try to do something useful.
  2255. (t (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2256. "Emphasis" desc)))))
  2257. ;;;; Babel Call
  2258. ;; Babel Calls are ignored.
  2259. ;;;; Macro
  2260. (defun org-e-odt-macro (macro contents info)
  2261. "Transcode a MACRO element from Org to ODT.
  2262. CONTENTS is nil. INFO is a plist holding contextual information."
  2263. ;; Use available tools.
  2264. (org-export-expand-macro macro info))
  2265. ;;;; Paragraph
  2266. (defun org-e-odt-paragraph (paragraph contents info)
  2267. "Transcode a PARAGRAPH element from Org to ODT.
  2268. CONTENTS is the contents of the paragraph, as a string. INFO is
  2269. the plist used as a communication channel."
  2270. (let* ((parent (org-export-get-parent paragraph))
  2271. (parent-type (org-element-type parent))
  2272. (style (case parent-type
  2273. (quote-block "Quotations")
  2274. (center-block "OrgCenter")
  2275. (footnote-definition "Footnote")
  2276. (t "Text_20_body"))))
  2277. ;; If this paragraph is a leading paragraph in a non-descriptive
  2278. ;; item and the item has a checkbox, splice the checkbox and
  2279. ;; paragraph contents together.
  2280. (when (and (eq (org-element-type parent) 'item)
  2281. (not (eq (org-element-property :type
  2282. (org-export-get-parent parent))
  2283. 'descriptive))
  2284. (eq paragraph (car (org-element-contents parent))))
  2285. (setq contents (concat (org-e-odt--checkbox parent) contents)))
  2286. (assert style)
  2287. (format "\n<text:p text:style-name=\"%s\">%s</text:p>" style contents)))
  2288. ;;;; Plain List
  2289. (defun org-e-odt-plain-list (plain-list contents info)
  2290. "Transcode a PLAIN-LIST element from Org to ODT.
  2291. CONTENTS is the contents of the list. INFO is a plist holding
  2292. contextual information."
  2293. (let* ((type (org-element-property :type plain-list))
  2294. (continue-numbering nil))
  2295. (assert (member type '(ordered unordered descriptive)))
  2296. (org-e-odt--wrap-label
  2297. plain-list
  2298. (format "\n<text:list text:style-name=\"%s\" %s>\n%s</text:list>"
  2299. (assoc-default type '((ordered . "OrgNumberedList")
  2300. (unordered . "OrgBulletedList")
  2301. (descriptive . "OrgDescriptionList")))
  2302. ;; If top-level list, re-start numbering. Otherwise,
  2303. ;; continue numbering.
  2304. (format "text:continue-numbering=\"%s\""
  2305. (let* ((parent (org-export-get-parent plain-list)))
  2306. (if (and parent (eq (org-element-type parent) 'item))
  2307. "true" "false")))
  2308. contents))))
  2309. ;;;; Plain Text
  2310. (defun org-e-odt-fill-tabs-and-spaces (line)
  2311. (replace-regexp-in-string
  2312. "\\([\t]\\|\\([ ]+\\)\\)"
  2313. (lambda (s)
  2314. (cond
  2315. ((string= s "\t") "<text:tab/>")
  2316. (t (let ((n (length s)))
  2317. (cond
  2318. ((= n 1) " ")
  2319. ((> n 1) (concat " " (format "<text:s text:c=\"%d\"/>" (1- n))))
  2320. (t ""))))))
  2321. line))
  2322. (defun org-e-odt-encode-plain-text (text &optional no-whitespace-filling)
  2323. (mapc
  2324. (lambda (pair)
  2325. (setq text (replace-regexp-in-string (car pair) (cdr pair) text t t)))
  2326. '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
  2327. (if no-whitespace-filling text
  2328. (org-e-odt-fill-tabs-and-spaces text)))
  2329. (defun org-e-odt--quotation-marks (text info)
  2330. "Export quotation marks depending on language conventions.
  2331. TEXT is a string containing quotation marks to be replaced. INFO
  2332. is a plist used as a communication channel."
  2333. (mapc (lambda(l)
  2334. (let ((start 0))
  2335. (while (setq start (string-match (car l) text start))
  2336. (let ((new-quote (concat (match-string 1 text) (cdr l))))
  2337. (setq text (replace-match new-quote t t text))))))
  2338. (cdr (or (assoc (plist-get info :language) org-e-odt-quotes)
  2339. ;; Falls back on English.
  2340. (assoc "en" org-e-odt-quotes))))
  2341. text)
  2342. (defun org-e-odt-plain-text (text info)
  2343. "Transcode a TEXT string from Org to ODT.
  2344. TEXT is the string to transcode. INFO is a plist holding
  2345. contextual information."
  2346. ;; Protect &, < and >.
  2347. (setq text (org-e-odt-encode-plain-text text t))
  2348. ;; Handle quotation marks
  2349. (setq text (org-e-odt--quotation-marks text info))
  2350. ;; Convert special strings.
  2351. (when (plist-get info :with-special-strings)
  2352. (mapc
  2353. (lambda (pair)
  2354. (setq text (replace-regexp-in-string (car pair) (cdr pair) text t nil)))
  2355. org-e-odt-special-string-regexps))
  2356. ;; Handle break preservation if required.
  2357. (when (plist-get info :preserve-breaks)
  2358. (setq text (replace-regexp-in-string
  2359. "\\(\\\\\\\\\\)?[ \t]*\n" "<text:line-break/>\n" text t)))
  2360. ;; Return value.
  2361. text)
  2362. ;;;; Planning
  2363. (defun org-e-odt-planning (planning contents info)
  2364. "Transcode a PLANNING element from Org to ODT.
  2365. CONTENTS is nil. INFO is a plist used as a communication
  2366. channel."
  2367. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2368. "OrgTimestampWrapper"
  2369. (concat
  2370. (let ((closed (org-element-property :closed planning)))
  2371. (when closed
  2372. (concat
  2373. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2374. "OrgTimestampKeyword" org-closed-string)
  2375. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2376. "OrgTimestamp" (org-translate-time closed)))))
  2377. (let ((deadline (org-element-property :deadline planning)))
  2378. (when deadline
  2379. (concat
  2380. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2381. "OrgTimestampKeyword" org-deadline-string)
  2382. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2383. "OrgTimestamp" (org-translate-time deadline)))))
  2384. (let ((scheduled (org-element-property :scheduled planning)))
  2385. (when scheduled
  2386. (concat
  2387. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2388. "OrgTimestampKeyword" org-scheduled-string)
  2389. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2390. "OrgTimestamp" (org-translate-time scheduled))))))))
  2391. ;;;; Property Drawer
  2392. (defun org-e-odt-property-drawer (property-drawer contents info)
  2393. "Transcode a PROPERTY-DRAWER element from Org to ODT.
  2394. CONTENTS is nil. INFO is a plist holding contextual
  2395. information."
  2396. ;; The property drawer isn't exported but we want separating blank
  2397. ;; lines nonetheless.
  2398. "")
  2399. ;;;; Quote Block
  2400. (defun org-e-odt-quote-block (quote-block contents info)
  2401. "Transcode a QUOTE-BLOCK element from Org to ODT.
  2402. CONTENTS holds the contents of the block. INFO is a plist
  2403. holding contextual information."
  2404. (org-e-odt--wrap-label quote-block contents))
  2405. ;;;; Quote Section
  2406. (defun org-e-odt-quote-section (quote-section contents info)
  2407. "Transcode a QUOTE-SECTION element from Org to ODT.
  2408. CONTENTS is nil. INFO is a plist holding contextual information."
  2409. (let ((value (org-remove-indentation
  2410. (org-element-property :value quote-section))))
  2411. (when value (org-e-odt-do-format-code value))))
  2412. ;;;; Section
  2413. (defun org-e-odt-format-section (text style &optional name)
  2414. (let ((default-name (car (org-e-odt-add-automatic-style "Section"))))
  2415. (format "\n<text:section text:style-name=\"%s\" %s>\n%s</text:section>"
  2416. style
  2417. (format "text:name=\"%s\"" (or name default-name))
  2418. text)))
  2419. (defun org-e-odt-section (section contents info) ; FIXME
  2420. "Transcode a SECTION element from Org to ODT.
  2421. CONTENTS holds the contents of the section. INFO is a plist
  2422. holding contextual information."
  2423. contents)
  2424. ;;;; Radio Target
  2425. (defun org-e-odt-radio-target (radio-target text info)
  2426. "Transcode a RADIO-TARGET object from Org to ODT.
  2427. TEXT is the text of the target. INFO is a plist holding
  2428. contextual information."
  2429. (org-e-odt-format-target
  2430. text (org-export-solidify-link-text
  2431. (org-element-property :value radio-target))))
  2432. ;;;; Special Block
  2433. (defun org-e-odt-special-block (special-block contents info)
  2434. "Transcode a SPECIAL-BLOCK element from Org to ODT.
  2435. CONTENTS holds the contents of the block. INFO is a plist
  2436. holding contextual information."
  2437. (let ((type (downcase (org-element-property :type special-block)))
  2438. (attributes (org-export-read-attribute :attr_odt special-block)))
  2439. (org-e-odt--wrap-label
  2440. special-block
  2441. (cond
  2442. ;; Annotation.
  2443. ((string= type "annotation")
  2444. (let ((author (or (plist-get attributes :author)
  2445. (let ((author (plist-get info :author)))
  2446. (and author (org-export-data author info)))))
  2447. (date (or (plist-get attributes :date)
  2448. (plist-get info :date))))
  2449. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2450. "Text_20_body"
  2451. (format "<office:annotation>\n%s\n</office:annotation>"
  2452. (concat
  2453. (and author
  2454. (format "<dc:creator>%s</dc:creator>" author))
  2455. (and date
  2456. (format "<dc:date>%s</dc:date>"
  2457. (org-e-odt--date date)))
  2458. contents)))))
  2459. ;; Textbox.
  2460. ((string= type "textbox")
  2461. (let ((width (plist-get attributes :width))
  2462. (height (plist-get attributes :height))
  2463. (style (plist-get attributes :style))
  2464. (extra (plist-get attributes :extra))
  2465. (anchor (plist-get attributes :anchor)))
  2466. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2467. "Text_20_body" (org-e-odt--textbox contents width height
  2468. style extra anchor))))
  2469. (t contents)))))
  2470. ;;;; Src Block
  2471. (defun org-e-odt-hfy-face-to-css (fn)
  2472. "Create custom style for face FN.
  2473. When FN is the default face, use it's foreground and background
  2474. properties to create \"OrgSrcBlock\" paragraph style. Otherwise
  2475. use it's color attribute to create a character style whose name
  2476. is obtained from FN. Currently all attributes of FN other than
  2477. color are ignored.
  2478. The style name for a face FN is derived using the following
  2479. operations on the face name in that order - de-dash, CamelCase
  2480. and prefix with \"OrgSrc\". For example,
  2481. `font-lock-function-name-face' is associated with
  2482. \"OrgSrcFontLockFunctionNameFace\"."
  2483. (let* ((css-list (hfy-face-to-style fn))
  2484. (style-name ((lambda (fn)
  2485. (concat "OrgSrc"
  2486. (mapconcat
  2487. 'capitalize (split-string
  2488. (hfy-face-or-def-to-name fn) "-")
  2489. ""))) fn))
  2490. (color-val (cdr (assoc "color" css-list)))
  2491. (background-color-val (cdr (assoc "background" css-list)))
  2492. (style (and org-e-odt-create-custom-styles-for-srcblocks
  2493. (cond
  2494. ((eq fn 'default)
  2495. (format org-e-odt-src-block-paragraph-format
  2496. background-color-val color-val))
  2497. (t
  2498. (format
  2499. "
  2500. <style:style style:name=\"%s\" style:family=\"text\">
  2501. <style:text-properties fo:color=\"%s\"/>
  2502. </style:style>" style-name color-val))))))
  2503. (cons style-name style)))
  2504. (defun org-e-odt-htmlfontify-string (line)
  2505. (let* ((hfy-html-quote-regex "\\([<\"&> ]\\)")
  2506. (hfy-html-quote-map '(("\"" "&quot;")
  2507. ("<" "&lt;")
  2508. ("&" "&amp;")
  2509. (">" "&gt;")
  2510. (" " "<text:s/>")
  2511. (" " "<text:tab/>")))
  2512. (hfy-face-to-css 'org-e-odt-hfy-face-to-css)
  2513. (hfy-optimisations-1 (copy-seq hfy-optimisations))
  2514. (hfy-optimisations (add-to-list 'hfy-optimisations-1
  2515. 'body-text-only))
  2516. (hfy-begin-span-handler
  2517. (lambda (style text-block text-id text-begins-block-p)
  2518. (insert (format "<text:span text:style-name=\"%s\">" style))))
  2519. (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
  2520. (with-no-warnings (htmlfontify-string line))))
  2521. (defun org-e-odt-do-format-code
  2522. (code &optional lang refs retain-labels num-start)
  2523. (let* ((lang (or (assoc-default lang org-src-lang-modes) lang))
  2524. (lang-mode (and lang (intern (format "%s-mode" lang))))
  2525. (code-lines (org-split-string code "\n"))
  2526. (code-length (length code-lines))
  2527. (use-htmlfontify-p (and (functionp lang-mode)
  2528. org-e-odt-fontify-srcblocks
  2529. (require 'htmlfontify nil t)
  2530. (fboundp 'htmlfontify-string)))
  2531. (code (if (not use-htmlfontify-p) code
  2532. (with-temp-buffer
  2533. (insert code)
  2534. (funcall lang-mode)
  2535. (font-lock-fontify-buffer)
  2536. (buffer-string))))
  2537. (fontifier (if use-htmlfontify-p 'org-e-odt-htmlfontify-string
  2538. 'org-e-odt-encode-plain-text))
  2539. (par-style (if use-htmlfontify-p "OrgSrcBlock"
  2540. "OrgFixedWidthBlock"))
  2541. (i 0))
  2542. (assert (= code-length (length (org-split-string code "\n"))))
  2543. (setq code
  2544. (org-export-format-code
  2545. code
  2546. (lambda (loc line-num ref)
  2547. (setq par-style
  2548. (concat par-style (and (= (incf i) code-length) "LastLine")))
  2549. (setq loc (concat loc (and ref retain-labels (format " (%s)" ref))))
  2550. (setq loc (funcall fontifier loc))
  2551. (when ref
  2552. (setq loc (org-e-odt-format-target loc (concat "coderef-" ref))))
  2553. (assert par-style)
  2554. (setq loc (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2555. par-style loc))
  2556. (if (not line-num) loc
  2557. (format "\n<text:list-item>%s\n</text:list-item>" loc)))
  2558. num-start refs))
  2559. (cond
  2560. ((not num-start) code)
  2561. ((= num-start 0)
  2562. (format
  2563. "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
  2564. " text:continue-numbering=\"false\"" code))
  2565. (t
  2566. (format
  2567. "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
  2568. " text:continue-numbering=\"true\"" code)))))
  2569. (defun org-e-odt-format-code (element info)
  2570. (let* ((lang (org-element-property :language element))
  2571. ;; Extract code and references.
  2572. (code-info (org-export-unravel-code element))
  2573. (code (car code-info))
  2574. (refs (cdr code-info))
  2575. ;; Does the src block contain labels?
  2576. (retain-labels (org-element-property :retain-labels element))
  2577. ;; Does it have line numbers?
  2578. (num-start (case (org-element-property :number-lines element)
  2579. (continued (org-export-get-loc element info))
  2580. (new 0))))
  2581. (org-e-odt-do-format-code code lang refs retain-labels num-start)))
  2582. (defun org-e-odt-src-block (src-block contents info)
  2583. "Transcode a SRC-BLOCK element from Org to ODT.
  2584. CONTENTS holds the contents of the item. INFO is a plist holding
  2585. contextual information."
  2586. (let* ((lang (org-element-property :language src-block))
  2587. (caption (org-element-property :caption src-block))
  2588. (short-caption (and (cdr caption)
  2589. (org-export-data (cdr caption) info)))
  2590. (caption (and (car caption) (org-export-data (car caption) info)))
  2591. (label (org-element-property :name src-block))
  2592. (attributes (org-export-read-attribute :attr_odt src-block)))
  2593. ;; FIXME: Handle caption
  2594. ;; caption-str (when caption)
  2595. ;; (main (org-export-data (car caption) info))
  2596. ;; (secondary (org-export-data (cdr caption) info))
  2597. ;; (caption-str (org-e-odt--caption/label-string caption label info))
  2598. (let* ((captions (org-e-odt-format-label src-block info 'definition))
  2599. (caption (car captions)) (short-caption (cdr captions)))
  2600. (concat
  2601. (and caption
  2602. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2603. "Listing" caption))
  2604. (let ((--src-block (org-e-odt-format-code src-block info)))
  2605. (if (not (plist-get attributes :textbox)) --src-block
  2606. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2607. "Text_20_body"
  2608. (org-e-odt--textbox --src-block nil nil nil))))))))
  2609. ;;;; Statistics Cookie
  2610. (defun org-e-odt-statistics-cookie (statistics-cookie contents info)
  2611. "Transcode a STATISTICS-COOKIE object from Org to ODT.
  2612. CONTENTS is nil. INFO is a plist holding contextual information."
  2613. (let ((cookie-value (org-element-property :value statistics-cookie)))
  2614. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2615. "OrgCode" cookie-value)))
  2616. ;;;; Strike-Through
  2617. (defun org-e-odt-strike-through (strike-through contents info)
  2618. "Transcode STRIKE-THROUGH from Org to ODT.
  2619. CONTENTS is the text with strike-through markup. INFO is a plist
  2620. holding contextual information."
  2621. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2622. "Strikethrough" contents))
  2623. ;;;; Subscript
  2624. (defun org-e-odt-subscript (subscript contents info)
  2625. "Transcode a SUBSCRIPT object from Org to ODT.
  2626. CONTENTS is the contents of the object. INFO is a plist holding
  2627. contextual information."
  2628. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2629. "OrgSubscript" contents))
  2630. ;;;; Superscript
  2631. (defun org-e-odt-superscript (superscript contents info)
  2632. "Transcode a SUPERSCRIPT object from Org to ODT.
  2633. CONTENTS is the contents of the object. INFO is a plist holding
  2634. contextual information."
  2635. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2636. "OrgSuperscript" contents))
  2637. ;;;; Table Cell
  2638. (defun org-e-odt-table-style-spec (element info)
  2639. (let* ((table (org-export-get-parent-table element))
  2640. (table-attributes (org-export-read-attribute :attr_odt table))
  2641. (table-style (plist-get table-attributes :style)))
  2642. (assoc table-style org-e-odt-table-styles)))
  2643. (defun org-e-odt-get-table-cell-styles (table-cell info)
  2644. "Retrieve styles applicable to a table cell.
  2645. R and C are (zero-based) row and column numbers of the table
  2646. cell. STYLE-SPEC is an entry in `org-e-odt-table-styles'
  2647. applicable to the current table. It is `nil' if the table is not
  2648. associated with any style attributes.
  2649. Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
  2650. When STYLE-SPEC is nil, style the table cell the conventional way
  2651. - choose cell borders based on row and column groupings and
  2652. choose paragraph alignment based on `org-col-cookies' text
  2653. property. See also
  2654. `org-e-odt-get-paragraph-style-cookie-for-table-cell'.
  2655. When STYLE-SPEC is non-nil, ignore the above cookie and return
  2656. styles congruent with the ODF-1.2 specification."
  2657. (let* ((table-cell-address (org-export-table-cell-address table-cell info))
  2658. (r (car table-cell-address)) (c (cdr table-cell-address))
  2659. (style-spec (org-e-odt-table-style-spec table-cell info))
  2660. (table-dimensions (org-export-table-dimensions
  2661. (org-export-get-parent-table table-cell)
  2662. info)))
  2663. (when style-spec
  2664. ;; LibreOffice - particularly the Writer - honors neither table
  2665. ;; templates nor custom table-cell styles. Inorder to retain
  2666. ;; inter-operability with LibreOffice, only automatic styles are
  2667. ;; used for styling of table-cells. The current implementation is
  2668. ;; congruent with ODF-1.2 specification and hence is
  2669. ;; future-compatible.
  2670. ;; Additional Note: LibreOffice's AutoFormat facility for tables -
  2671. ;; which recognizes as many as 16 different cell types - is much
  2672. ;; richer. Unfortunately it is NOT amenable to easy configuration
  2673. ;; by hand.
  2674. (let* ((template-name (nth 1 style-spec))
  2675. (cell-style-selectors (nth 2 style-spec))
  2676. (cell-type
  2677. (cond
  2678. ((and (cdr (assoc 'use-first-column-styles cell-style-selectors))
  2679. (= c 0)) "FirstColumn")
  2680. ((and (cdr (assoc 'use-last-column-styles cell-style-selectors))
  2681. (= (1+ c) (cdr table-dimensions)))
  2682. "LastColumn")
  2683. ((and (cdr (assoc 'use-first-row-styles cell-style-selectors))
  2684. (= r 0)) "FirstRow")
  2685. ((and (cdr (assoc 'use-last-row-styles cell-style-selectors))
  2686. (= (1+ r) (car table-dimensions)))
  2687. "LastRow")
  2688. ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
  2689. (= (% r 2) 1)) "EvenRow")
  2690. ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
  2691. (= (% r 2) 0)) "OddRow")
  2692. ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
  2693. (= (% c 2) 1)) "EvenColumn")
  2694. ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
  2695. (= (% c 2) 0)) "OddColumn")
  2696. (t ""))))
  2697. (concat template-name cell-type)))))
  2698. (defun org-e-odt-table-cell (table-cell contents info)
  2699. "Transcode a TABLE-CELL element from Org to ODT.
  2700. CONTENTS is nil. INFO is a plist used as a communication
  2701. channel."
  2702. (let* ((table-cell-address (org-export-table-cell-address table-cell info))
  2703. (r (car table-cell-address))
  2704. (c (cdr table-cell-address))
  2705. (horiz-span (or (org-export-table-cell-width table-cell info) 0))
  2706. (table-row (org-export-get-parent table-cell))
  2707. (custom-style-prefix (org-e-odt-get-table-cell-styles
  2708. table-cell info))
  2709. (paragraph-style
  2710. (or
  2711. (and custom-style-prefix
  2712. (format "%sTableParagraph" custom-style-prefix))
  2713. (concat
  2714. (cond
  2715. ((and (= 1 (org-export-table-row-group table-row info))
  2716. (org-export-table-has-header-p
  2717. (org-export-get-parent-table table-row) info))
  2718. "OrgTableHeading")
  2719. ((let* ((table (org-export-get-parent-table table-cell))
  2720. (table-attrs (org-export-read-attribute :attr_odt table))
  2721. (table-header-columns (plist-get table-attrs
  2722. :header-columns)))
  2723. (<= c (cond ((wholenump table-header-columns)
  2724. (- table-header-columns 1))
  2725. (table-header-columns 0)
  2726. (t -1))))
  2727. "OrgTableHeading")
  2728. (t "OrgTableContents"))
  2729. (capitalize (symbol-name (org-export-table-cell-alignment
  2730. table-cell info))))))
  2731. (cell-style-name
  2732. (or
  2733. (and custom-style-prefix (format "%sTableCell"
  2734. custom-style-prefix))
  2735. (concat
  2736. "OrgTblCell"
  2737. (when (or (org-export-table-row-starts-rowgroup-p table-row info)
  2738. (zerop r)) "T")
  2739. (when (org-export-table-row-ends-rowgroup-p table-row info) "B")
  2740. (when (and (org-export-table-cell-starts-colgroup-p table-cell info)
  2741. (not (zerop c)) ) "L"))))
  2742. (cell-attributes
  2743. (concat
  2744. (format " table:style-name=\"%s\"" cell-style-name)
  2745. (and (> horiz-span 0)
  2746. (format " table:number-columns-spanned=\"%d\""
  2747. (1+ horiz-span))))))
  2748. (unless contents (setq contents ""))
  2749. (concat
  2750. (assert paragraph-style)
  2751. (format "\n<table:table-cell%s>\n%s\n</table:table-cell>"
  2752. cell-attributes
  2753. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2754. paragraph-style contents))
  2755. (let (s)
  2756. (dotimes (i horiz-span s)
  2757. (setq s (concat s "\n<table:covered-table-cell/>"))))
  2758. "\n")))
  2759. ;;;; Table Row
  2760. (defun org-e-odt-table-row (table-row contents info)
  2761. "Transcode a TABLE-ROW element from Org to ODT.
  2762. CONTENTS is the contents of the row. INFO is a plist used as a
  2763. communication channel."
  2764. ;; Rules are ignored since table separators are deduced from
  2765. ;; borders of the current row.
  2766. (when (eq (org-element-property :type table-row) 'standard)
  2767. (let* ((rowgroup-tags
  2768. (if (and (= 1 (org-export-table-row-group table-row info))
  2769. (org-export-table-has-header-p
  2770. (org-export-get-parent-table table-row) info))
  2771. ;; If the row belongs to the first rowgroup and the
  2772. ;; table has more than one row groups, then this row
  2773. ;; belongs to the header row group.
  2774. '("\n<table:table-header-rows>" . "\n</table:table-header-rows>")
  2775. ;; Otherwise, it belongs to non-header row group.
  2776. '("\n<table:table-rows>" . "\n</table:table-rows>"))))
  2777. (concat
  2778. ;; Does this row begin a rowgroup?
  2779. (when (org-export-table-row-starts-rowgroup-p table-row info)
  2780. (car rowgroup-tags))
  2781. ;; Actual table row
  2782. (format "\n<table:table-row>\n%s\n</table:table-row>" contents)
  2783. ;; Does this row end a rowgroup?
  2784. (when (org-export-table-row-ends-rowgroup-p table-row info)
  2785. (cdr rowgroup-tags))))))
  2786. ;;;; Table
  2787. (defun org-e-odt-table-first-row-data-cells (table info)
  2788. (let ((table-row
  2789. (org-element-map
  2790. table 'table-row
  2791. (lambda (row)
  2792. (unless (eq (org-element-property :type row) 'rule) row))
  2793. info 'first-match))
  2794. (special-column-p (org-export-table-has-special-column-p table)))
  2795. (if (not special-column-p) (org-element-contents table-row)
  2796. (cdr (org-element-contents table-row)))))
  2797. (defun org-e-odt--table (table contents info)
  2798. "Transcode a TABLE element from Org to ODT.
  2799. CONTENTS is the contents of the table. INFO is a plist holding
  2800. contextual information."
  2801. (case (org-element-property :type table)
  2802. ;; Case 1: table.el doesn't support export to OD format. Strip
  2803. ;; such tables from export.
  2804. (table.el
  2805. (prog1 nil
  2806. (message
  2807. (concat
  2808. "(org-e-odt): Found table.el-type table in the source Org file."
  2809. " table.el doesn't support export to ODT format."
  2810. " Stripping the table from export."))))
  2811. ;; Case 2: Native Org tables.
  2812. (otherwise
  2813. (let* ((captions (org-e-odt-format-label table info 'definition))
  2814. (caption (car captions)) (short-caption (cdr captions))
  2815. (attributes (org-export-read-attribute :attr_odt table))
  2816. (custom-table-style (nth 1 (org-e-odt-table-style-spec table info)))
  2817. (table-column-specs
  2818. (function
  2819. (lambda (table info)
  2820. (let* ((table-style (or custom-table-style "OrgTable"))
  2821. (column-style (format "%sColumn" table-style)))
  2822. (mapconcat
  2823. (lambda (table-cell)
  2824. (let ((width (1+ (or (org-export-table-cell-width
  2825. table-cell info) 0)))
  2826. (s (format
  2827. "\n<table:table-column table:style-name=\"%s\"/>"
  2828. column-style))
  2829. out)
  2830. (dotimes (i width out) (setq out (concat s out)))))
  2831. (org-e-odt-table-first-row-data-cells table info) "\n"))))))
  2832. (concat
  2833. ;; caption.
  2834. (when caption
  2835. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2836. "Table" caption))
  2837. ;; begin table.
  2838. (let* ((automatic-name
  2839. (org-e-odt-add-automatic-style "Table" attributes)))
  2840. (format
  2841. "\n<table:table table:name=\"%s\" table:style-name=\"%s\">"
  2842. (or short-caption (car automatic-name))
  2843. (or custom-table-style (cdr automatic-name) "OrgTable")))
  2844. ;; column specification.
  2845. (funcall table-column-specs table info)
  2846. ;; actual contents.
  2847. "\n" contents
  2848. ;; end table.
  2849. "</table:table>")))))
  2850. (defun org-e-odt-table (table contents info)
  2851. "Transcode a TABLE element from Org to ODT.
  2852. CONTENTS is the contents of the table. INFO is a plist holding
  2853. contextual information."
  2854. (let* ((--get-previous-elements
  2855. (function
  2856. (lambda (blob info)
  2857. (let ((parent (org-export-get-parent blob)))
  2858. (cdr (member blob (reverse (org-element-contents parent))))))))
  2859. (--element-preceded-by-table-p
  2860. (function
  2861. (lambda (element info)
  2862. (loop for el in (funcall --get-previous-elements element info)
  2863. thereis (eq (org-element-type el) 'table)))))
  2864. (--walk-list-genealogy-and-collect-tags
  2865. (function
  2866. (lambda (table info)
  2867. (let* ((genealogy (org-export-get-genealogy table))
  2868. (list-genealogy
  2869. (when (eq (org-element-type (car genealogy)) 'item)
  2870. (loop for el in genealogy
  2871. when (member (org-element-type el)
  2872. '(item plain-list))
  2873. collect el))))
  2874. (loop for el in list-genealogy
  2875. with parent-list collect
  2876. (case (org-element-type el)
  2877. (plain-list
  2878. (setq parent-list el)
  2879. `("</text:list>"
  2880. . ,(let ((type (org-element-property :type el)))
  2881. (format
  2882. "<text:list text:style-name=\"%s\" %s>"
  2883. (assoc-default
  2884. type '((ordered . "OrgNumberedList")
  2885. (unordered . "OrgBulletedList")
  2886. (descriptive . "OrgDescriptionList")))
  2887. "text:continue-numbering=\"true\""))))
  2888. (item
  2889. (cond
  2890. ((not parent-list)
  2891. (if (funcall --element-preceded-by-table-p table info)
  2892. '("</text:list-header>" . "<text:list-header>")
  2893. '("</text:list-item>" . "<text:list-header>")))
  2894. ((funcall --element-preceded-by-table-p
  2895. parent-list info)
  2896. '("</text:list-header>" . "<text:list-header>"))
  2897. (t '("</text:list-item>" . "<text:list-item>"))))))))))
  2898. (close-open-tags (funcall --walk-list-genealogy-and-collect-tags
  2899. table info)))
  2900. ;; OpenDocument schema does not permit table to occur within a
  2901. ;; list item. So, to typeset an indented table, we make use of
  2902. ;; list continuations.
  2903. (concat "\n"
  2904. ;; Discontinue the list.
  2905. (mapconcat 'car close-open-tags "\n")
  2906. ;; Put the table in an indented section.
  2907. (let* ((table (org-e-odt--table table contents info))
  2908. (level (/ (length (mapcar 'car close-open-tags)) 2))
  2909. (style (format "OrgIndentedSection-Level-%d" level)))
  2910. (when table (org-e-odt-format-section table style)))
  2911. ;; Continue the list.
  2912. (mapconcat 'cdr (nreverse close-open-tags) "\n"))))
  2913. ;;;; Target
  2914. (defun org-e-odt-target (target contents info)
  2915. "Transcode a TARGET object from Org to ODT.
  2916. CONTENTS is nil. INFO is a plist holding contextual
  2917. information."
  2918. (org-e-odt-format-target
  2919. "" (org-export-solidify-link-text (org-element-property :value target))))
  2920. ;;;; Timestamp
  2921. (defun org-e-odt-timestamp (timestamp contents info)
  2922. "Transcode a TIMESTAMP object from Org to ODT.
  2923. CONTENTS is nil. INFO is a plist used as a communication
  2924. channel."
  2925. (let ((timestamp-1 (org-element-property :value timestamp))
  2926. (timestamp-2 (org-element-property :range-end timestamp)))
  2927. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2928. "OrgTimestampWrapper"
  2929. (concat
  2930. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2931. "OrgTimestamp" (org-translate-time timestamp-1))
  2932. (and timestamp-2
  2933. "&#x2013;"
  2934. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2935. "OrgTimestamp" (org-translate-time timestamp-2)))))))
  2936. ;;;; Underline
  2937. (defun org-e-odt-underline (underline contents info)
  2938. "Transcode UNDERLINE from Org to ODT.
  2939. CONTENTS is the text with underline markup. INFO is a plist
  2940. holding contextual information."
  2941. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2942. "Underline" contents))
  2943. ;;;; Verbatim
  2944. (defun org-e-odt-verbatim (verbatim contents info)
  2945. "Transcode a VERBATIM object from Org to ODT.
  2946. CONTENTS is nil. INFO is a plist used as a communication
  2947. channel."
  2948. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2949. "OrgCode" (org-element-property :value verbatim)))
  2950. ;;;; Verse Block
  2951. (defun org-e-odt-verse-block (verse-block contents info)
  2952. "Transcode a VERSE-BLOCK element from Org to ODT.
  2953. CONTENTS is verse block contents. INFO is a plist holding
  2954. contextual information."
  2955. ;; Add line breaks to each line of verse.
  2956. (setq contents (replace-regexp-in-string
  2957. "\\(<text:line-break/>\\)?[ \t]*\n"
  2958. "<text:line-break/>" contents))
  2959. ;; Replace tabs and spaces.
  2960. (setq contents (org-e-odt-fill-tabs-and-spaces contents))
  2961. ;; Surround it in a verse environment.
  2962. (org-e-odt--wrap-label
  2963. verse-block
  2964. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2965. "OrgVerse" contents)))
  2966. ;;; Interactive functions
  2967. (defun org-e-odt-create-manifest-file-entry (&rest args)
  2968. (push args org-e-odt-manifest-file-entries))
  2969. (defun org-e-odt-write-manifest-file ()
  2970. (make-directory (concat org-e-odt-zip-dir "META-INF"))
  2971. (let ((manifest-file (concat org-e-odt-zip-dir "META-INF/manifest.xml")))
  2972. (with-current-buffer
  2973. (let ((nxml-auto-insert-xml-declaration-flag nil))
  2974. (find-file-noselect manifest-file t))
  2975. (insert
  2976. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  2977. <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
  2978. (mapc
  2979. (lambda (file-entry)
  2980. (let* ((version (nth 2 file-entry))
  2981. (extra (if (not version) ""
  2982. (format " manifest:version=\"%s\"" version))))
  2983. (insert
  2984. (format org-e-odt-manifest-file-entry-tag
  2985. (nth 0 file-entry) (nth 1 file-entry) extra))))
  2986. org-e-odt-manifest-file-entries)
  2987. (insert "\n</manifest:manifest>"))))
  2988. (defmacro org-e-odt--export-wrap (out-file &rest body)
  2989. `(let* ((--out-file ,out-file)
  2990. (out-file-type (file-name-extension --out-file))
  2991. (org-e-odt-xml-files '("META-INF/manifest.xml" "content.xml"
  2992. "meta.xml" "styles.xml"))
  2993. ;; Initialize workarea. All files that end up in the
  2994. ;; exported get created here.
  2995. (org-e-odt-zip-dir (file-name-as-directory
  2996. (make-temp-file (format "%s-" out-file-type) t)))
  2997. (org-e-odt-manifest-file-entries nil)
  2998. (--cleanup-xml-buffers
  2999. (function
  3000. (lambda nil
  3001. ;; Kill all XML buffers.
  3002. (mapc (lambda (file)
  3003. (let ((buf (get-file-buffer
  3004. (concat org-e-odt-zip-dir file))))
  3005. (when buf
  3006. (set-buffer-modified-p nil)
  3007. (kill-buffer buf))))
  3008. org-e-odt-xml-files)
  3009. ;; Delete temporary directory and also other embedded
  3010. ;; files that get copied there.
  3011. (delete-directory org-e-odt-zip-dir t)))))
  3012. (org-condition-case-unless-debug
  3013. err
  3014. (progn
  3015. (unless (executable-find "zip")
  3016. ;; Not at all OSes ship with zip by default
  3017. (error "Executable \"zip\" needed for creating OpenDocument files"))
  3018. ;; Do export. This creates a bunch of xml files ready to be
  3019. ;; saved and zipped.
  3020. (progn ,@body)
  3021. ;; Create a manifest entry for content.xml.
  3022. (org-e-odt-create-manifest-file-entry "text/xml" "content.xml")
  3023. ;; Write mimetype file
  3024. (let* ((mimetypes
  3025. '(("odt" . "application/vnd.oasis.opendocument.text")
  3026. ("odf" . "application/vnd.oasis.opendocument.formula")))
  3027. (mimetype (cdr (assoc-string out-file-type mimetypes t))))
  3028. (unless mimetype
  3029. (error "Unknown OpenDocument backend %S" out-file-type))
  3030. (write-region mimetype nil (concat org-e-odt-zip-dir "mimetype"))
  3031. (org-e-odt-create-manifest-file-entry mimetype "/" "1.2"))
  3032. ;; Write out the manifest entries before zipping
  3033. (org-e-odt-write-manifest-file)
  3034. ;; Save all XML files.
  3035. (mapc (lambda (file)
  3036. (let ((buf (get-file-buffer (concat org-e-odt-zip-dir file))))
  3037. (when buf
  3038. (with-current-buffer buf
  3039. ;; Prettify output if needed.
  3040. (when org-e-odt-prettify-xml
  3041. (indent-region (point-min) (point-max)))
  3042. (save-buffer 0)))))
  3043. org-e-odt-xml-files)
  3044. ;; Run zip.
  3045. (let* ((target --out-file)
  3046. (target-name (file-name-nondirectory target))
  3047. (target-dir (file-name-directory target))
  3048. (cmds `(("zip" "-mX0" ,target-name "mimetype")
  3049. ("zip" "-rmTq" ,target-name "."))))
  3050. ;; If a file with same name as the desired output file
  3051. ;; exists, remove it.
  3052. (when (file-exists-p target)
  3053. (delete-file target))
  3054. ;; Zip up the xml files.
  3055. (let ((coding-system-for-write 'no-conversion) exitcode err-string)
  3056. (message "Creating ODT file...")
  3057. ;; Switch temporarily to content.xml. This way Zip
  3058. ;; process will inherit `org-e-odt-zip-dir' as the current
  3059. ;; directory.
  3060. (with-current-buffer
  3061. (find-file-noselect (concat org-e-odt-zip-dir "content.xml") t)
  3062. (mapc
  3063. (lambda (cmd)
  3064. (message "Running %s" (mapconcat 'identity cmd " "))
  3065. (setq err-string
  3066. (with-output-to-string
  3067. (setq exitcode
  3068. (apply 'call-process (car cmd)
  3069. nil standard-output nil (cdr cmd)))))
  3070. (or (zerop exitcode)
  3071. (error (concat "Unable to create OpenDocument file."
  3072. (format " Zip failed with error (%s)"
  3073. err-string)))))
  3074. cmds)
  3075. ;; Zip file is now in the rightful place.
  3076. (rename-file target-name target)))
  3077. (message "Created %s" target)
  3078. ;; Cleanup work directory and work files.
  3079. (funcall --cleanup-xml-buffers)
  3080. ;; Open the OpenDocument file in archive-mode for
  3081. ;; examination.
  3082. (find-file-noselect target t)
  3083. ;; Return exported file.
  3084. (cond
  3085. ;; Case 1: Conversion desired on exported file. Run the
  3086. ;; converter on the OpenDocument file. Return the
  3087. ;; converted file.
  3088. (org-e-odt-preferred-output-format
  3089. (or (org-e-odt-convert target org-e-odt-preferred-output-format)
  3090. target))
  3091. ;; Case 2: No further conversion. Return exported
  3092. ;; OpenDocument file.
  3093. (t target))))
  3094. ((quit error)
  3095. ;; Cleanup work directory and work files.
  3096. (funcall --cleanup-xml-buffers)
  3097. (message "OpenDocument export failed: %s"
  3098. (error-message-string err))))))
  3099. ;;;###autoload
  3100. (defun org-e-odt-export-as-odf (latex-frag &optional odf-file)
  3101. "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
  3102. Use `org-create-math-formula' to convert LATEX-FRAG first to
  3103. MathML. When invoked as an interactive command, use
  3104. `org-latex-regexps' to infer LATEX-FRAG from currently active
  3105. region. If no LaTeX fragments are found, prompt for it. Push
  3106. MathML source to kill ring, if `org-export-copy-to-kill-ring' is
  3107. non-nil."
  3108. (interactive
  3109. `(,(let (frag)
  3110. (setq frag (and (setq frag (and (region-active-p)
  3111. (buffer-substring (region-beginning)
  3112. (region-end))))
  3113. (loop for e in org-latex-regexps
  3114. thereis (when (string-match (nth 1 e) frag)
  3115. (match-string (nth 2 e) frag)))))
  3116. (read-string "LaTeX Fragment: " frag nil frag))
  3117. ,(let ((odf-filename (expand-file-name
  3118. (concat
  3119. (file-name-sans-extension
  3120. (or (file-name-nondirectory buffer-file-name)))
  3121. "." "odf")
  3122. (file-name-directory buffer-file-name))))
  3123. (read-file-name "ODF filename: " nil odf-filename nil
  3124. (file-name-nondirectory odf-filename)))))
  3125. (let ((filename (or odf-file
  3126. (expand-file-name
  3127. (concat
  3128. (file-name-sans-extension
  3129. (or (file-name-nondirectory buffer-file-name)))
  3130. "." "odf")
  3131. (file-name-directory buffer-file-name)))))
  3132. (org-e-odt--export-wrap
  3133. filename
  3134. (let* ((buffer (progn
  3135. (require 'nxml-mode)
  3136. (let ((nxml-auto-insert-xml-declaration-flag nil))
  3137. (find-file-noselect (concat org-e-odt-zip-dir
  3138. "content.xml") t))))
  3139. (coding-system-for-write 'utf-8)
  3140. (save-buffer-coding-system 'utf-8))
  3141. (set-buffer buffer)
  3142. (set-buffer-file-coding-system coding-system-for-write)
  3143. (let ((mathml (org-create-math-formula latex-frag)))
  3144. (unless mathml (error "No Math formula created"))
  3145. (insert mathml)
  3146. ;; Add MathML to kill ring, if needed.
  3147. (when org-export-copy-to-kill-ring
  3148. (org-kill-new (buffer-string))))))))
  3149. ;;;###autoload
  3150. (defun org-e-odt-export-as-odf-and-open ()
  3151. "Export LaTeX fragment as OpenDocument formula and immediately open it.
  3152. Use `org-e-odt-export-as-odf' to read LaTeX fragment and OpenDocument
  3153. formula file."
  3154. (interactive)
  3155. (org-open-file (call-interactively 'org-e-odt-export-as-odf)))
  3156. ;;;###autoload
  3157. (defun org-e-odt-export-to-odt
  3158. (&optional subtreep visible-only body-only ext-plist pub-dir)
  3159. "Export current buffer to a HTML file.
  3160. If narrowing is active in the current buffer, only export its
  3161. narrowed part.
  3162. If a region is active, export that region.
  3163. When optional argument SUBTREEP is non-nil, export the sub-tree
  3164. at point, extracting information from the headline properties
  3165. first.
  3166. When optional argument VISIBLE-ONLY is non-nil, don't export
  3167. contents of hidden elements.
  3168. When optional argument BODY-ONLY is non-nil, only write code
  3169. between \"\\begin{document}\" and \"\\end{document}\".
  3170. EXT-PLIST, when provided, is a property list with external
  3171. parameters overriding Org default settings, but still inferior to
  3172. file-local settings.
  3173. When optional argument PUB-DIR is set, use it as the publishing
  3174. directory.
  3175. Return output file's name."
  3176. (interactive)
  3177. (org-e-odt--export-wrap
  3178. (org-export-output-file-name ".odt" subtreep pub-dir)
  3179. (let* ((org-e-odt-embedded-images-count 0)
  3180. (org-e-odt-embedded-formulas-count 0)
  3181. (org-e-odt-automatic-styles nil)
  3182. (org-e-odt-object-counters nil)
  3183. ;; Let `htmlfontify' know that we are interested in collecting
  3184. ;; styles.
  3185. (hfy-user-sheet-assoc nil))
  3186. ;; Initialize content.xml and kick-off the export process.
  3187. (let ((out-buf (progn
  3188. (require 'nxml-mode)
  3189. (let ((nxml-auto-insert-xml-declaration-flag nil))
  3190. (find-file-noselect
  3191. (concat org-e-odt-zip-dir "content.xml") t)))))
  3192. (org-export-to-buffer 'e-odt out-buf subtreep visible-only body-only)))))
  3193. (defun org-e-odt-reachable-p (in-fmt out-fmt)
  3194. "Return non-nil if IN-FMT can be converted to OUT-FMT."
  3195. (catch 'done
  3196. (let ((reachable-formats (org-e-odt-do-reachable-formats in-fmt)))
  3197. (dolist (e reachable-formats)
  3198. (let ((out-fmt-spec (assoc out-fmt (cdr e))))
  3199. (when out-fmt-spec
  3200. (throw 'done (cons (car e) out-fmt-spec))))))))
  3201. (defun org-e-odt-do-convert (in-file out-fmt &optional prefix-arg)
  3202. "Workhorse routine for `org-e-odt-convert'."
  3203. (require 'browse-url)
  3204. (let* ((in-file (expand-file-name (or in-file buffer-file-name)))
  3205. (dummy (or (file-readable-p in-file)
  3206. (error "Cannot read %s" in-file)))
  3207. (in-fmt (file-name-extension in-file))
  3208. (out-fmt (or out-fmt (error "Output format unspecified")))
  3209. (how (or (org-e-odt-reachable-p in-fmt out-fmt)
  3210. (error "Cannot convert from %s format to %s format?"
  3211. in-fmt out-fmt)))
  3212. (convert-process (car how))
  3213. (out-file (concat (file-name-sans-extension in-file) "."
  3214. (nth 1 (or (cdr how) out-fmt))))
  3215. (extra-options (or (nth 2 (cdr how)) ""))
  3216. (out-dir (file-name-directory in-file))
  3217. (cmd (format-spec convert-process
  3218. `((?i . ,(shell-quote-argument in-file))
  3219. (?I . ,(browse-url-file-url in-file))
  3220. (?f . ,out-fmt)
  3221. (?o . ,out-file)
  3222. (?O . ,(browse-url-file-url out-file))
  3223. (?d . , (shell-quote-argument out-dir))
  3224. (?D . ,(browse-url-file-url out-dir))
  3225. (?x . ,extra-options)))))
  3226. (when (file-exists-p out-file)
  3227. (delete-file out-file))
  3228. (message "Executing %s" cmd)
  3229. (let ((cmd-output (shell-command-to-string cmd)))
  3230. (message "%s" cmd-output))
  3231. (cond
  3232. ((file-exists-p out-file)
  3233. (message "Exported to %s" out-file)
  3234. (when prefix-arg
  3235. (message "Opening %s..." out-file)
  3236. (org-open-file out-file))
  3237. out-file)
  3238. (t
  3239. (message "Export to %s failed" out-file)
  3240. nil))))
  3241. (defun org-e-odt-do-reachable-formats (in-fmt)
  3242. "Return verbose info about formats to which IN-FMT can be converted.
  3243. Return a list where each element is of the
  3244. form (CONVERTER-PROCESS . OUTPUT-FMT-ALIST). See
  3245. `org-e-odt-convert-processes' for CONVERTER-PROCESS and see
  3246. `org-e-odt-convert-capabilities' for OUTPUT-FMT-ALIST."
  3247. (let* ((converter
  3248. (and org-e-odt-convert-process
  3249. (cadr (assoc-string org-e-odt-convert-process
  3250. org-e-odt-convert-processes t))))
  3251. (capabilities
  3252. (and org-e-odt-convert-process
  3253. (cadr (assoc-string org-e-odt-convert-process
  3254. org-e-odt-convert-processes t))
  3255. org-e-odt-convert-capabilities))
  3256. reachable-formats)
  3257. (when converter
  3258. (dolist (c capabilities)
  3259. (when (member in-fmt (nth 1 c))
  3260. (push (cons converter (nth 2 c)) reachable-formats))))
  3261. reachable-formats))
  3262. (defun org-e-odt-reachable-formats (in-fmt)
  3263. "Return list of formats to which IN-FMT can be converted.
  3264. The list of the form (OUTPUT-FMT-1 OUTPUT-FMT-2 ...)."
  3265. (let (l)
  3266. (mapc (lambda (e) (add-to-list 'l e))
  3267. (apply 'append (mapcar
  3268. (lambda (e) (mapcar 'car (cdr e)))
  3269. (org-e-odt-do-reachable-formats in-fmt))))
  3270. l))
  3271. (defun org-e-odt-convert-read-params ()
  3272. "Return IN-FILE and OUT-FMT params for `org-e-odt-do-convert'.
  3273. This is a helper routine for interactive use."
  3274. (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
  3275. (in-file (read-file-name "File to be converted: "
  3276. nil buffer-file-name t))
  3277. (in-fmt (file-name-extension in-file))
  3278. (out-fmt-choices (org-e-odt-reachable-formats in-fmt))
  3279. (out-fmt
  3280. (or (and out-fmt-choices
  3281. (funcall input "Output format: "
  3282. out-fmt-choices nil nil nil))
  3283. (error
  3284. "No known converter or no known output formats for %s files"
  3285. in-fmt))))
  3286. (list in-file out-fmt)))
  3287. ;;;###autoload
  3288. (defun org-e-odt-convert (&optional in-file out-fmt prefix-arg)
  3289. "Convert IN-FILE to format OUT-FMT using a command line converter.
  3290. IN-FILE is the file to be converted. If unspecified, it defaults
  3291. to variable `buffer-file-name'. OUT-FMT is the desired output
  3292. format. Use `org-e-odt-convert-process' as the converter.
  3293. If PREFIX-ARG is non-nil then the newly converted file is opened
  3294. using `org-open-file'."
  3295. (interactive
  3296. (append (org-e-odt-convert-read-params) current-prefix-arg))
  3297. (org-e-odt-do-convert in-file out-fmt prefix-arg))
  3298. ;;; Library Initializations
  3299. (mapc
  3300. (lambda (desc)
  3301. ;; Let Org open all OpenDocument files using system-registered app
  3302. (add-to-list 'org-file-apps
  3303. (cons (concat "\\." (car desc) "\\'") 'system))
  3304. ;; Let Emacs open all OpenDocument files in archive mode
  3305. (add-to-list 'auto-mode-alist
  3306. (cons (concat "\\." (car desc) "\\'") 'archive-mode)))
  3307. org-e-odt-file-extensions)
  3308. (provide 'org-e-odt)
  3309. ;;; org-e-odt.el ends here