org-e-odt.el 147 KB

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