org-e-odt.el 132 KB

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