org-e-odt.el 144 KB

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