org-e-odt.el 158 KB

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