org-e-odt.el 154 KB

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