org-e-odt.el 144 KB

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