org-e-odt.el 153 KB

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