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. (let ((nxml-auto-insert-xml-declaration-flag nil))
  961. (find-file-noselect content-file t))
  962. (current-buffer))))
  963. (defun org-e-odt-save-as-outfile (target opt-plist)
  964. ;; write automatic styles
  965. (org-e-odt-write-automatic-styles)
  966. ;; update display levels
  967. (org-e-odt-update-display-level org-e-odt-display-outline-level)
  968. ;; write styles file
  969. ;; (when (equal org-lparse-backend 'odt) FIXME
  970. ;; )
  971. ;; (org-e-odt-update-styles-file opt-plist)
  972. ;; create mimetype file
  973. (let ((mimetype (org-e-odt-write-mimetype-file ;; org-lparse-backend FIXME
  974. 'odt)))
  975. (org-e-odt-create-manifest-file-entry mimetype "/" "1.2"))
  976. ;; create a manifest entry for content.xml
  977. (org-e-odt-create-manifest-file-entry "text/xml" "content.xml")
  978. ;; write out the manifest entries before zipping
  979. (org-e-odt-write-manifest-file)
  980. (let ((xml-files '("mimetype" "META-INF/manifest.xml" "content.xml"
  981. "meta.xml"))
  982. (zipdir default-directory))
  983. (when (or t (equal org-lparse-backend 'odt)) ; FIXME
  984. (push "styles.xml" xml-files))
  985. (message "Switching to directory %s" (expand-file-name zipdir))
  986. ;; save all xml files
  987. (mapc (lambda (file)
  988. (with-current-buffer
  989. (find-file-noselect (expand-file-name file) t)
  990. ;; prettify output if needed
  991. (when org-e-odt-prettify-xml
  992. (indent-region (point-min) (point-max)))
  993. (save-buffer 0)))
  994. xml-files)
  995. (let* ((target-name (file-name-nondirectory target))
  996. (target-dir (file-name-directory target))
  997. (cmds `(("zip" "-mX0" ,target-name "mimetype")
  998. ("zip" "-rmTq" ,target-name "."))))
  999. (when (file-exists-p target)
  1000. ;; FIXME: If the file is locked this throws a cryptic error
  1001. (delete-file target))
  1002. (let ((coding-system-for-write 'no-conversion) exitcode err-string)
  1003. (message "Creating odt file...")
  1004. (mapc
  1005. (lambda (cmd)
  1006. (message "Running %s" (mapconcat 'identity cmd " "))
  1007. (setq err-string
  1008. (with-output-to-string
  1009. (setq exitcode
  1010. (apply 'call-process (car cmd)
  1011. nil standard-output nil (cdr cmd)))))
  1012. (or (zerop exitcode)
  1013. (ignore (message "%s" err-string))
  1014. (error "Unable to create odt file (%S)" exitcode)))
  1015. cmds))
  1016. ;; move the file from outdir to target-dir
  1017. (rename-file target-name target-dir)
  1018. ;; kill all xml buffers
  1019. (mapc (lambda (file)
  1020. (kill-buffer
  1021. (find-file-noselect (expand-file-name file zipdir) t)))
  1022. xml-files)
  1023. (delete-directory zipdir)))
  1024. (message "Created %s" target)
  1025. (set-buffer (find-file-noselect target t)))
  1026. (defun org-e-odt-create-manifest-file-entry (&rest args)
  1027. (push args org-e-odt-manifest-file-entries))
  1028. (defun org-e-odt-write-manifest-file ()
  1029. (make-directory "META-INF")
  1030. (let ((manifest-file (expand-file-name "META-INF/manifest.xml")))
  1031. (with-current-buffer
  1032. (let ((nxml-auto-insert-xml-declaration-flag nil))
  1033. (find-file-noselect manifest-file t))
  1034. (insert
  1035. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  1036. <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
  1037. (mapc
  1038. (lambda (file-entry)
  1039. (let* ((version (nth 2 file-entry))
  1040. (extra (if version
  1041. (format " manifest:version=\"%s\"" version)
  1042. "")))
  1043. (insert
  1044. (format org-e-odt-manifest-file-entry-tag
  1045. (nth 0 file-entry) (nth 1 file-entry) extra))))
  1046. org-e-odt-manifest-file-entries)
  1047. (insert "\n</manifest:manifest>"))))
  1048. (defun org-e-odt-update-meta-file (info) ; FIXME opt-plist
  1049. (let ((title (org-export-data (plist-get info :title) info))
  1050. (author (or (let ((auth (plist-get info :author)))
  1051. (and auth (org-export-data auth info))) ""))
  1052. (date (org-e-odt-format-date (plist-get info :date)))
  1053. (email (plist-get info :email))
  1054. (keywords (plist-get info :keywords))
  1055. (description (plist-get info :description)))
  1056. (write-region
  1057. (concat
  1058. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  1059. <office:document-meta
  1060. xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
  1061. xmlns:xlink=\"http://www.w3.org/1999/xlink\"
  1062. xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
  1063. xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
  1064. xmlns:ooo=\"http://openoffice.org/2004/office\"
  1065. office:version=\"1.2\">
  1066. <office:meta>\n"
  1067. (org-e-odt-format-author author) "\n"
  1068. (format "<meta:initial-creator>%s</meta:initial-creator>\n" author)
  1069. (format "<dc:date>%s</dc:date>\n" date)
  1070. (format "<meta:creation-date>%s</meta:creation-date>\n" date)
  1071. (format "<meta:generator>%s</meta:generator>\n"
  1072. (concat (and org-export-creator-info org-export-creator-string)))
  1073. (format "<meta:keyword>%s</meta:keyword>\n" keywords)
  1074. (format "<dc:subject>%s</dc:subject>\n" description)
  1075. (format "<dc:title>%s</dc:title>\n" title)
  1076. "\n"
  1077. " </office:meta>\n" "</office:document-meta>")
  1078. nil (expand-file-name "meta.xml")))
  1079. ;; create a manifest entry for meta.xml
  1080. (org-e-odt-create-manifest-file-entry "text/xml" "meta.xml"))
  1081. (defun org-e-odt-update-styles-file (info)
  1082. ;; write styles file
  1083. (let ((styles-file (plist-get info :odt-styles-file)))
  1084. (org-e-odt-copy-styles-file (and styles-file
  1085. (read (org-trim styles-file))))
  1086. ;; FIXME: Who is opening an empty styles.xml before this point?
  1087. (with-current-buffer
  1088. (find-file-noselect (expand-file-name "styles.xml") t)
  1089. (revert-buffer t t)))
  1090. ;; Write custom styles for source blocks
  1091. (org-e-odt-insert-custom-styles-for-srcblocks
  1092. (mapconcat
  1093. (lambda (style)
  1094. (format " %s\n" (cddr style)))
  1095. hfy-user-sheet-assoc "")))
  1096. (defun org-e-odt-write-mimetype-file (format)
  1097. ;; create mimetype file
  1098. (let ((mimetype
  1099. (case format
  1100. (odt "application/vnd.oasis.opendocument.text")
  1101. (odf "application/vnd.oasis.opendocument.formula")
  1102. (t (error "Unknown OpenDocument backend %S" org-lparse-backend)))))
  1103. (write-region mimetype nil (expand-file-name "mimetype"))
  1104. mimetype))
  1105. (declare-function org-create-math-formula "org"
  1106. (latex-frag &optional mathml-file))
  1107. (defun org-e-odt-get (what &optional opt-plist)
  1108. (case what
  1109. (EXPORT-DIR (org-export-directory :html opt-plist))
  1110. (TABLE-FIRST-COLUMN-AS-LABELS nil)
  1111. (CODING-SYSTEM-FOR-WRITE 'utf-8)
  1112. (CODING-SYSTEM-FOR-SAVE 'utf-8)
  1113. (t (error "Unknown property: %s" what))))
  1114. (defun org-e-odt-do-preprocess-latex-fragments ()
  1115. "Convert LaTeX fragments to images."
  1116. (let* ((latex-frag-opt (plist-get org-lparse-opt-plist :LaTeX-fragments))
  1117. (latex-frag-opt ; massage the options
  1118. (or (and (member latex-frag-opt '(mathjax t))
  1119. (not (and (fboundp 'org-format-latex-mathml-available-p)
  1120. (org-format-latex-mathml-available-p)))
  1121. (prog1 org-lparse-latex-fragment-fallback
  1122. (org-lparse-warn
  1123. (concat
  1124. "LaTeX to MathML converter not available. "
  1125. (format "Using %S instead."
  1126. org-lparse-latex-fragment-fallback)))))
  1127. latex-frag-opt))
  1128. cache-dir display-msg)
  1129. (cond
  1130. ((eq latex-frag-opt 'dvipng)
  1131. (setq cache-dir "ltxpng/")
  1132. (setq display-msg "Creating LaTeX image %s"))
  1133. ((member latex-frag-opt '(mathjax t))
  1134. (setq latex-frag-opt 'mathml)
  1135. (setq cache-dir "ltxmathml/")
  1136. (setq display-msg "Creating MathML formula %s")))
  1137. (when (and org-current-export-file)
  1138. (org-format-latex
  1139. (concat cache-dir (file-name-sans-extension
  1140. (file-name-nondirectory org-current-export-file)))
  1141. org-current-export-dir nil display-msg
  1142. nil nil latex-frag-opt))))
  1143. (eval-after-load 'org-odt
  1144. '(ad-deactivate 'org-format-latex-as-mathml))
  1145. ; FIXME
  1146. ;; (defadvice org-format-latex-as-mathml ; FIXME
  1147. ;; (after org-e-odt-protect-latex-fragment activate)
  1148. ;; "Encode LaTeX fragment as XML.
  1149. ;; Do this when translation to MathML fails."
  1150. ;; (when (or (not (> (length ad-return-value) 0))
  1151. ;; (get-text-property 0 'org-protected ad-return-value))
  1152. ;; (setq ad-return-value
  1153. ;; (org-propertize (org-e-odt-encode-plain-text (ad-get-arg 0))
  1154. ;; 'org-protected t))))
  1155. (defun org-e-odt-zip-extract-one (archive member &optional target)
  1156. (require 'arc-mode)
  1157. (let* ((target (or target default-directory))
  1158. (archive (expand-file-name archive))
  1159. (archive-zip-extract
  1160. (list "unzip" "-qq" "-o" "-d" target))
  1161. exit-code command-output)
  1162. (setq command-output
  1163. (with-temp-buffer
  1164. (setq exit-code (archive-zip-extract archive member))
  1165. (buffer-string)))
  1166. (unless (zerop exit-code)
  1167. (message command-output)
  1168. (error "Extraction failed"))))
  1169. (defun org-e-odt-zip-extract (archive members &optional target)
  1170. (when (atom members) (setq members (list members)))
  1171. (mapc (lambda (member)
  1172. (org-e-odt-zip-extract-one archive member target))
  1173. members))
  1174. (defun org-e-odt-copy-styles-file (&optional styles-file)
  1175. ;; Non-availability of styles.xml is not a critical error. For now
  1176. ;; throw an error purely for aesthetic reasons.
  1177. (setq styles-file (or styles-file
  1178. org-e-odt-styles-file
  1179. (expand-file-name "OrgOdtStyles.xml"
  1180. org-e-odt-styles-dir)
  1181. (error "org-e-odt: Missing styles file?")))
  1182. (cond
  1183. ((listp styles-file)
  1184. (let ((archive (nth 0 styles-file))
  1185. (members (nth 1 styles-file)))
  1186. (org-e-odt-zip-extract archive members)
  1187. (mapc
  1188. (lambda (member)
  1189. (when (org-file-image-p member)
  1190. (let* ((image-type (file-name-extension member))
  1191. (media-type (format "image/%s" image-type)))
  1192. (org-e-odt-create-manifest-file-entry media-type member))))
  1193. members)))
  1194. ((and (stringp styles-file) (file-exists-p styles-file))
  1195. (let ((styles-file-type (file-name-extension styles-file)))
  1196. (cond
  1197. ((string= styles-file-type "xml")
  1198. (copy-file styles-file (expand-file-name "styles.xml") t))
  1199. ((member styles-file-type '("odt" "ott"))
  1200. (org-e-odt-zip-extract styles-file "styles.xml")))))
  1201. (t
  1202. (error (format "Invalid specification of styles.xml file: %S"
  1203. org-e-odt-styles-file))))
  1204. ;; create a manifest entry for styles.xml
  1205. (org-e-odt-create-manifest-file-entry "text/xml" "styles.xml"))
  1206. (defun org-e-odt-configure-outline-numbering ()
  1207. "Outline numbering is retained only upto LEVEL.
  1208. To disable outline numbering pass a LEVEL of 0."
  1209. (goto-char (point-min))
  1210. (let ((regex
  1211. "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
  1212. (replacement
  1213. "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
  1214. (while (re-search-forward regex nil t)
  1215. (unless (let ((sec-num (plist-get info :section-numbers))
  1216. (level (string-to-number (match-string 2))))
  1217. (if (wholenump sec-num) (<= level sec-num) sec-num))
  1218. (replace-match replacement t nil))))
  1219. (save-buffer 0))
  1220. ;;;###autoload
  1221. (defun org-export-as-odf (latex-frag &optional odf-file)
  1222. "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
  1223. Use `org-create-math-formula' to convert LATEX-FRAG first to
  1224. MathML. When invoked as an interactive command, use
  1225. `org-latex-regexps' to infer LATEX-FRAG from currently active
  1226. region. If no LaTeX fragments are found, prompt for it. Push
  1227. MathML source to kill ring, if `org-export-copy-to-kill-ring' is
  1228. non-nil."
  1229. (interactive
  1230. `(,(let (frag)
  1231. (setq frag (and (setq frag (and (region-active-p)
  1232. (buffer-substring (region-beginning)
  1233. (region-end))))
  1234. (loop for e in org-latex-regexps
  1235. thereis (when (string-match (nth 1 e) frag)
  1236. (match-string (nth 2 e) frag)))))
  1237. (read-string "LaTeX Fragment: " frag nil frag))
  1238. ,(let ((odf-filename (expand-file-name
  1239. (concat
  1240. (file-name-sans-extension
  1241. (or (file-name-nondirectory buffer-file-name)))
  1242. "." "odf")
  1243. (file-name-directory buffer-file-name))))
  1244. (read-file-name "ODF filename: " nil odf-filename nil
  1245. (file-name-nondirectory odf-filename)))))
  1246. (let* ((org-lparse-backend 'odf)
  1247. org-lparse-opt-plist
  1248. (filename (or odf-file
  1249. (expand-file-name
  1250. (concat
  1251. (file-name-sans-extension
  1252. (or (file-name-nondirectory buffer-file-name)))
  1253. "." "odf")
  1254. (file-name-directory buffer-file-name))))
  1255. (buffer (find-file-noselect (org-e-odt-init-outfile filename)))
  1256. (coding-system-for-write 'utf-8)
  1257. (save-buffer-coding-system 'utf-8))
  1258. (set-buffer buffer)
  1259. (set-buffer-file-coding-system coding-system-for-write)
  1260. (let ((mathml (org-create-math-formula latex-frag)))
  1261. (unless mathml (error "No Math formula created"))
  1262. (insert mathml)
  1263. (or (org-export-push-to-kill-ring
  1264. (upcase (symbol-name org-lparse-backend)))
  1265. (message "Exporting... done")))
  1266. (org-e-odt-save-as-outfile filename nil ; FIXME
  1267. )))
  1268. ;;;###autoload
  1269. (defun org-export-as-odf-and-open ()
  1270. "Export LaTeX fragment as OpenDocument formula and immediately open it.
  1271. Use `org-export-as-odf' to read LaTeX fragment and OpenDocument
  1272. formula file."
  1273. (interactive)
  1274. (org-lparse-and-open
  1275. nil nil nil (call-interactively 'org-export-as-odf)))
  1276. ;;; Driver Starts here
  1277. ;;; Dependencies
  1278. (require 'format-spec)
  1279. (eval-when-compile (require 'cl) (require 'table))
  1280. ;;; Hooks
  1281. ;; FIXME: it already exists in org-e-odt.el
  1282. ;;; Function Declarations
  1283. (declare-function org-element-property "org-element" (property element))
  1284. (declare-function org-element-normalize-string "org-element" (s))
  1285. (declare-function org-export-data "org-export" (data info))
  1286. (declare-function org-export-directory "org-export" (type plist))
  1287. (declare-function org-export-expand-macro "org-export" (macro info))
  1288. (declare-function org-export-first-sibling-p "org-export" (headline info))
  1289. (declare-function org-export-footnote-first-reference-p "org-export"
  1290. (footnote-reference info))
  1291. (declare-function org-export-get-coderef-format "org-export" (path desc))
  1292. (declare-function org-export-get-footnote-definition "org-export"
  1293. (footnote-reference info))
  1294. (declare-function org-export-get-footnote-number "org-export" (footnote info))
  1295. (declare-function org-export-get-previous-element "org-export" (blob info))
  1296. (declare-function org-export-get-relative-level "org-export" (headline info))
  1297. (declare-function org-export-handle-code
  1298. "org-export" (element info &optional num-fmt ref-fmt delayed))
  1299. (declare-function org-export-included-file "org-export" (keyword backend info))
  1300. (declare-function org-export-inline-image-p "org-export"
  1301. (link &optional extensions))
  1302. (declare-function org-export-last-sibling-p "org-export" (headline info))
  1303. (declare-function org-export-low-level-p "org-export" (headline info))
  1304. (declare-function org-export-output-file-name
  1305. "org-export" (extension &optional subtreep pub-dir))
  1306. (declare-function org-export-resolve-coderef "org-export" (ref info))
  1307. (declare-function org-export-resolve-fuzzy-link "org-export" (link info))
  1308. (declare-function org-export-resolve-radio-link "org-export" (link info))
  1309. (declare-function org-export-solidify-link-text "org-export" (s))
  1310. (declare-function
  1311. org-export-to-buffer "org-export"
  1312. (backend buffer &optional subtreep visible-only body-only ext-plist))
  1313. (declare-function
  1314. org-export-to-file "org-export"
  1315. (backend file &optional subtreep visible-only body-only ext-plist))
  1316. (declare-function org-id-find-id-file "org-id" (id))
  1317. (declare-function htmlize-region "ext:htmlize" (beg end))
  1318. (declare-function org-pop-to-buffer-same-window
  1319. "org-compat" (&optional buffer-or-name norecord label))
  1320. (declare-function hfy-face-to-style "htmlfontify" (fn))
  1321. (declare-function hfy-face-or-def-to-name "htmlfontify" (fn))
  1322. (declare-function archive-zip-extract "arc-mode.el" (archive name))
  1323. ;;; Internal Variables
  1324. ;;;; ODT Internal Variables
  1325. (defconst org-e-odt-lib-dir
  1326. (file-name-directory load-file-name)
  1327. "Location of ODT exporter.
  1328. Use this to infer values of `org-e-odt-styles-dir' and
  1329. `org-e-odt-schema-dir'.")
  1330. (defvar org-e-odt-data-dir
  1331. (expand-file-name "../../etc/" org-e-odt-lib-dir)
  1332. "Data directory for ODT exporter.
  1333. Use this to infer values of `org-e-odt-styles-dir' and
  1334. `org-e-odt-schema-dir'.")
  1335. (defconst org-e-odt-special-string-regexps
  1336. '(("\\\\-" . "&#x00ad;\\1") ; shy
  1337. ("---\\([^-]\\)" . "&#x2014;\\1") ; mdash
  1338. ("--\\([^-]\\)" . "&#x2013;\\1") ; ndash
  1339. ("\\.\\.\\." . "&#x2026;")) ; hellip
  1340. "Regular expressions for special string conversion.")
  1341. (defconst org-e-odt-schema-dir-list
  1342. (list
  1343. (and org-e-odt-data-dir
  1344. (expand-file-name "./schema/" org-e-odt-data-dir)) ; bail out
  1345. (eval-when-compile
  1346. (and (boundp 'org-e-odt-data-dir) org-e-odt-data-dir ; see make install
  1347. (expand-file-name "./schema/" org-e-odt-data-dir))))
  1348. "List of directories to search for OpenDocument schema files.
  1349. Use this list to set the default value of
  1350. `org-e-odt-schema-dir'. The entries in this list are
  1351. populated heuristically based on the values of `org-e-odt-lib-dir'
  1352. and `org-e-odt-data-dir'.")
  1353. (defconst org-e-odt-styles-dir-list
  1354. (list
  1355. (and org-e-odt-data-dir
  1356. (expand-file-name "./styles/" org-e-odt-data-dir)) ; bail out
  1357. (eval-when-compile
  1358. (and (boundp 'org-e-odt-data-dir) org-e-odt-data-dir ; see make install
  1359. (expand-file-name "./styles/" org-e-odt-data-dir)))
  1360. (expand-file-name "../../etc/styles/" org-e-odt-lib-dir) ; git
  1361. (expand-file-name "./etc/styles/" org-e-odt-lib-dir) ; elpa
  1362. (expand-file-name "./org/" data-directory) ; system
  1363. )
  1364. "List of directories to search for OpenDocument styles files.
  1365. See `org-e-odt-styles-dir'. The entries in this list are populated
  1366. heuristically based on the values of `org-e-odt-lib-dir' and
  1367. `org-e-odt-data-dir'.")
  1368. (defconst org-e-odt-styles-dir
  1369. (let* ((styles-dir
  1370. (catch 'styles-dir
  1371. (message "Debug (org-e-odt): Searching for OpenDocument styles files...")
  1372. (mapc (lambda (styles-dir)
  1373. (when styles-dir
  1374. (message "Debug (org-e-odt): Trying %s..." styles-dir)
  1375. (when (and (file-readable-p
  1376. (expand-file-name
  1377. "OrgOdtContentTemplate.xml" styles-dir))
  1378. (file-readable-p
  1379. (expand-file-name
  1380. "OrgOdtStyles.xml" styles-dir)))
  1381. (message "Debug (org-e-odt): Using styles under %s"
  1382. styles-dir)
  1383. (throw 'styles-dir styles-dir))))
  1384. org-e-odt-styles-dir-list)
  1385. nil)))
  1386. (unless styles-dir
  1387. (error "Error (org-e-odt): Cannot find factory styles files. Aborting."))
  1388. styles-dir)
  1389. "Directory that holds auxiliary XML files used by the ODT exporter.
  1390. This directory contains the following XML files -
  1391. \"OrgOdtStyles.xml\" and \"OrgOdtContentTemplate.xml\". These
  1392. XML files are used as the default values of
  1393. `org-e-odt-styles-file' and
  1394. `org-e-odt-content-template-file'.
  1395. The default value of this variable varies depending on the
  1396. version of org in use and is initialized from
  1397. `org-e-odt-styles-dir-list'. Note that the user could be using org
  1398. from one of: org's own private git repository, GNU ELPA tar or
  1399. standard Emacs.")
  1400. (defconst org-e-odt-tmpdir-prefix "%s-")
  1401. (defconst org-e-odt-bookmark-prefix "OrgXref.")
  1402. (defconst org-e-odt-manifest-file-entry-tag
  1403. "
  1404. <manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
  1405. (defvar org-lparse-dyn-first-heading-pos) ; let bound during org-do-lparse
  1406. (defvar org-e-odt-suppress-xref nil)
  1407. (defvar org-e-odt-file-extensions
  1408. '(("odt" . "OpenDocument Text")
  1409. ("ott" . "OpenDocument Text Template")
  1410. ("odm" . "OpenDocument Master Document")
  1411. ("ods" . "OpenDocument Spreadsheet")
  1412. ("ots" . "OpenDocument Spreadsheet Template")
  1413. ("odg" . "OpenDocument Drawing (Graphics)")
  1414. ("otg" . "OpenDocument Drawing Template")
  1415. ("odp" . "OpenDocument Presentation")
  1416. ("otp" . "OpenDocument Presentation Template")
  1417. ("odi" . "OpenDocument Image")
  1418. ("odf" . "OpenDocument Formula")
  1419. ("odc" . "OpenDocument Chart")))
  1420. (defconst org-e-odt-default-org-styles-alist
  1421. '((paragraph . ((default . "Text_20_body")
  1422. (fixedwidth . "OrgFixedWidthBlock")
  1423. (verse . "OrgVerse")
  1424. (quote . "Quotations")
  1425. (blockquote . "Quotations")
  1426. (center . "OrgCenter")
  1427. (left . "OrgLeft")
  1428. (right . "OrgRight")
  1429. (title . "OrgTitle")
  1430. (subtitle . "OrgSubtitle")
  1431. (footnote . "Footnote")
  1432. (src . "OrgSrcBlock")
  1433. (illustration . "Illustration")
  1434. (table . "Table")
  1435. (listing . "Listing")
  1436. (definition-term . "Text_20_body_20_bold")
  1437. (horizontal-line . "Horizontal_20_Line")))
  1438. (character . ((bold . "Bold")
  1439. (italic . "Emphasis")
  1440. (code . "OrgCode")
  1441. (verbatim . "OrgCode")
  1442. (strike . "Strikethrough")
  1443. (underline . "Underline")
  1444. (subscript . "OrgSubscript")
  1445. (superscript . "OrgSuperscript")))
  1446. (list . ((ordered . "OrgNumberedList")
  1447. (unordered . "OrgBulletedList")
  1448. (descriptive . "OrgDescriptionList"))))
  1449. "Default styles for various entities.")
  1450. (defvar org-e-odt-org-styles-alist org-e-odt-default-org-styles-alist)
  1451. ;;;_. callbacks
  1452. ;;;_. control callbacks
  1453. ;;;_ , document body
  1454. (defvar org-lparse-body-only) ; let bound during org-do-lparse
  1455. (defvar org-lparse-opt-plist) ; bound during org-do-lparse
  1456. (defvar org-lparse-list-stack) ; dynamically bound in org-do-lparse
  1457. (defvar org-e-odt-list-stack-stashed)
  1458. (defvar org-lparse-table-ncols)
  1459. (defvar org-e-odt-table-rowgrp-open)
  1460. (defvar org-e-odt-table-rownum)
  1461. (defvar org-e-odt-table-cur-rowgrp-is-hdr)
  1462. (defvar org-lparse-table-is-styled)
  1463. (defvar org-lparse-table-rowgrp-info)
  1464. (defvar org-lparse-table-colalign-vector)
  1465. (defvar org-e-odt-table-style nil
  1466. "Table style specified by \"#+ATTR_ODT: <style-name>\" line.
  1467. This is set during `org-e-odt-begin-table'.")
  1468. (defvar org-e-odt-table-style-spec nil
  1469. "Entry for `org-e-odt-table-style' in `org-e-odt-table-styles'.")
  1470. (defvar org-e-odt-table-style-format
  1471. "
  1472. <style:style style:name=\"%s\" style:family=\"table\">
  1473. <style:table-properties style:rel-width=\"%d%%\" fo:margin-top=\"0cm\" fo:margin-bottom=\"0.20cm\" table:align=\"center\"/>
  1474. </style:style>
  1475. "
  1476. "Template for auto-generated Table styles.")
  1477. (defvar org-e-odt-automatic-styles '()
  1478. "Registry of automatic styles for various OBJECT-TYPEs.
  1479. The variable has the following form:
  1480. \(\(OBJECT-TYPE-A
  1481. \(\(OBJECT-NAME-A.1 OBJECT-PROPS-A.1\)
  1482. \(OBJECT-NAME-A.2 OBJECT-PROPS-A.2\) ...\)\)
  1483. \(OBJECT-TYPE-B
  1484. \(\(OBJECT-NAME-B.1 OBJECT-PROPS-B.1\)
  1485. \(OBJECT-NAME-B.2 OBJECT-PROPS-B.2\) ...\)\)
  1486. ...\).
  1487. OBJECT-TYPEs could be \"Section\", \"Table\", \"Figure\" etc.
  1488. OBJECT-PROPS is (typically) a plist created by passing
  1489. \"#+ATTR_ODT: \" option to `org-e-odt-parse-block-attributes'.
  1490. Use `org-e-odt-add-automatic-style' to add update this variable.'")
  1491. (defvar org-e-odt-object-counters nil
  1492. "Running counters for various OBJECT-TYPEs.
  1493. Use this to generate automatic names and style-names. See
  1494. `org-e-odt-add-automatic-style'.")
  1495. (defvar org-e-odt-table-indentedp nil)
  1496. (defvar org-lparse-table-colalign-info)
  1497. (defvar org-lparse-link-description-is-image nil)
  1498. (defvar org-src-block-paragraph-format
  1499. "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
  1500. <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
  1501. <style:background-image/>
  1502. </style:paragraph-properties>
  1503. <style:text-properties fo:color=\"%s\"/>
  1504. </style:style>"
  1505. "Custom paragraph style for colorized source and example blocks.
  1506. This style is much the same as that of \"OrgFixedWidthBlock\"
  1507. except that the foreground and background colors are set
  1508. according to the default face identified by the `htmlfontify'.")
  1509. (defvar hfy-optimisations)
  1510. (defvar org-e-odt-embedded-formulas-count 0)
  1511. (defvar org-e-odt-entity-frame-styles
  1512. '(("As-CharImage" "__Figure__" ("OrgInlineImage" nil "as-char"))
  1513. ("ParagraphImage" "__Figure__" ("OrgDisplayImage" nil "paragraph"))
  1514. ("PageImage" "__Figure__" ("OrgPageImage" nil "page"))
  1515. ("CaptionedAs-CharImage" "__Figure__"
  1516. ("OrgCaptionedImage"
  1517. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  1518. ("OrgInlineImage" nil "as-char"))
  1519. ("CaptionedParagraphImage" "__Figure__"
  1520. ("OrgCaptionedImage"
  1521. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  1522. ("OrgImageCaptionFrame" nil "paragraph"))
  1523. ("CaptionedPageImage" "__Figure__"
  1524. ("OrgCaptionedImage"
  1525. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  1526. ("OrgPageImageCaptionFrame" nil "page"))
  1527. ("InlineFormula" "__MathFormula__" ("OrgInlineFormula" nil "as-char"))
  1528. ("DisplayFormula" "__MathFormula__" ("OrgDisplayFormula" nil "as-char"))
  1529. ("CaptionedDisplayFormula" "__MathFormula__"
  1530. ("OrgCaptionedFormula" nil "paragraph")
  1531. ("OrgFormulaCaptionFrame" nil "as-char"))))
  1532. (defvar org-e-odt-embedded-images-count 0)
  1533. (defvar org-e-odt-image-size-probe-method
  1534. (append (and (executable-find "identify") '(imagemagick)) ; See Bug#10675
  1535. '(emacs fixed))
  1536. "Ordered list of methods for determining image sizes.")
  1537. (defvar org-e-odt-default-image-sizes-alist
  1538. '(("as-char" . (5 . 0.4))
  1539. ("paragraph" . (5 . 5)))
  1540. "Hardcoded image dimensions one for each of the anchor
  1541. methods.")
  1542. ;; A4 page size is 21.0 by 29.7 cms
  1543. ;; The default page settings has 2cm margin on each of the sides. So
  1544. ;; the effective text area is 17.0 by 25.7 cm
  1545. (defvar org-e-odt-max-image-size '(17.0 . 20.0)
  1546. "Limiting dimensions for an embedded image.")
  1547. (defvar org-e-odt-entity-labels-alist nil
  1548. "Associate Labels with the Labeled entities.
  1549. Each element of the alist is of the form (LABEL-NAME
  1550. CATEGORY-NAME SEQNO LABEL-STYLE-NAME). LABEL-NAME is same as
  1551. that specified by \"#+LABEL: ...\" line. CATEGORY-NAME is the
  1552. type of the entity that LABEL-NAME is attached to. CATEGORY-NAME
  1553. can be one of \"Table\", \"Figure\" or \"Equation\". SEQNO is
  1554. the unique number assigned to the referenced entity on a
  1555. per-CATEGORY basis. It is generated sequentially and is 1-based.
  1556. LABEL-STYLE-NAME is a key `org-e-odt-label-styles'.
  1557. See `org-e-odt-add-label-definition' and
  1558. `org-e-odt-fixup-label-references'.")
  1559. (defvar org-e-odt-entity-counts-plist nil
  1560. "Plist of running counters of SEQNOs for each of the CATEGORY-NAMEs.
  1561. See `org-e-odt-entity-labels-alist' for known CATEGORY-NAMEs.")
  1562. (defvar org-e-odt-label-styles
  1563. '(("math-formula" "%c" "text" "(%n)")
  1564. ("math-label" "(%n)" "text" "(%n)")
  1565. ("category-and-value" "%e %n: %c" "category-and-value" "%e %n")
  1566. ("value" "%e %n: %c" "value" "%n"))
  1567. "Specify how labels are applied and referenced.
  1568. This is an alist where each element is of the
  1569. form (LABEL-STYLE-NAME LABEL-ATTACH-FMT LABEL-REF-MODE
  1570. LABEL-REF-FMT).
  1571. LABEL-ATTACH-FMT controls how labels and captions are attached to
  1572. an entity. It may contain following specifiers - %e, %n and %c.
  1573. %e is replaced with the CATEGORY-NAME. %n is replaced with
  1574. \"<text:sequence ...> SEQNO </text:sequence>\". %c is replaced
  1575. with CAPTION. See `org-e-odt-format-label-definition'.
  1576. LABEL-REF-MODE and LABEL-REF-FMT controls how label references
  1577. are generated. The following XML is generated for a label
  1578. reference - \"<text:sequence-ref
  1579. text:reference-format=\"LABEL-REF-MODE\" ...> LABEL-REF-FMT
  1580. </text:sequence-ref>\". LABEL-REF-FMT may contain following
  1581. specifiers - %e and %n. %e is replaced with the CATEGORY-NAME.
  1582. %n is replaced with SEQNO. See
  1583. `org-e-odt-format-label-reference'.")
  1584. (defcustom org-e-odt-category-strings
  1585. '(("en" "Table" "Figure" "Equation" "Equation" "Listing"))
  1586. "Specify category strings for various captionable entities.
  1587. Captionable entity can be one of a Table, an Embedded Image, a
  1588. LaTeX fragment (generated with dvipng) or a Math Formula.
  1589. For example, when `org-export-default-language' is \"en\", an
  1590. embedded image will be captioned as \"Figure 1: Orgmode Logo\".
  1591. If you want the images to be captioned instead as \"Illustration
  1592. 1: Orgmode Logo\", then modify the entry for \"en\" as shown
  1593. below.
  1594. \(setq org-e-odt-category-strings
  1595. '\(\(\"en\" \"Table\" \"Illustration\"
  1596. \"Equation\" \"Equation\"\)\)\)"
  1597. :group 'org-export-e-odt
  1598. :version "24.1"
  1599. :type '(repeat (list (string :tag "Language tag")
  1600. (choice :tag "Table"
  1601. (const :tag "Use Default" nil)
  1602. (string :tag "Category string"))
  1603. (choice :tag "Figure"
  1604. (const :tag "Use Default" nil)
  1605. (string :tag "Category string"))
  1606. (choice :tag "Math Formula"
  1607. (const :tag "Use Default" nil)
  1608. (string :tag "Category string"))
  1609. (choice :tag "Dvipng Image"
  1610. (const :tag "Use Default" nil)
  1611. (string :tag "Category string"))
  1612. (choice :tag "Listing"
  1613. (const :tag "Use Default" nil)
  1614. (string :tag "Category string")))))
  1615. (defvar org-e-odt-category-map-alist
  1616. '(("__Table__" "Table" "value")
  1617. ("__Figure__" "Illustration" "value")
  1618. ("__MathFormula__" "Text" "math-formula")
  1619. ("__DvipngImage__" "Equation" "value")
  1620. ("__Listing__" "Listing" "value")
  1621. ;; ("__Table__" "Table" "category-and-value")
  1622. ;; ("__Figure__" "Figure" "category-and-value")
  1623. ;; ("__DvipngImage__" "Equation" "category-and-value")
  1624. )
  1625. "Map a CATEGORY-HANDLE to OD-VARIABLE and LABEL-STYLE.
  1626. This is a list where each entry is of the form \\(CATEGORY-HANDLE
  1627. OD-VARIABLE LABEL-STYLE\\). CATEGORY_HANDLE identifies the
  1628. captionable entity in question. OD-VARIABLE is the OpenDocument
  1629. sequence counter associated with the entity. These counters are
  1630. declared within
  1631. \"<text:sequence-decls>...</text:sequence-decls>\" block of
  1632. `org-e-odt-content-template-file'. LABEL-STYLE is a key
  1633. into `org-e-odt-label-styles' and specifies how a given entity
  1634. should be captioned and referenced.
  1635. The position of a CATEGORY-HANDLE in this list is used as an
  1636. index in to per-language entry for
  1637. `org-e-odt-category-strings' to retrieve a CATEGORY-NAME.
  1638. This CATEGORY-NAME is then used for qualifying the user-specified
  1639. captions on export.")
  1640. (defvar org-e-odt-manifest-file-entries nil)
  1641. (defvar hfy-user-sheet-assoc) ; bound during org-do-lparse
  1642. (defvar org-lparse-latex-fragment-fallback) ; set by org-do-lparse
  1643. ;;;; HTML Internal Variables
  1644. (defvar html-table-tag nil) ; dynamically scoped into this.
  1645. ;; FIXME: it already exists in org-e-odt.el
  1646. (defconst org-e-odt-cvt-link-fn
  1647. nil
  1648. "Function to convert link URLs to exportable URLs.
  1649. Takes two arguments, TYPE and PATH.
  1650. Returns exportable url as (TYPE PATH), or nil to signal that it
  1651. didn't handle this case.
  1652. Intended to be locally bound around a call to `org-export-as-html'." )
  1653. (defvar org-e-odt-format-table-no-css)
  1654. (defvar htmlize-buffer-places) ; from htmlize.el
  1655. (defvar body-only) ; dynamically scoped into this.
  1656. (defvar org-e-odt-table-rowgrp-open)
  1657. (defvar org-e-odt-table-rownum)
  1658. (defvar org-e-odt-table-cur-rowgrp-is-hdr)
  1659. (defvar org-lparse-table-is-styled)
  1660. (defvar org-e-odt-headline-formatter
  1661. (lambda (level snumber todo todo-type priority
  1662. title tags target extra-targets extra-class)
  1663. (concat snumber " " title)))
  1664. ;;; User Configuration Variables
  1665. (defgroup org-export-e-odt nil
  1666. "Options for exporting Org mode files to ODT."
  1667. :tag "Org Export ODT"
  1668. :group 'org-export)
  1669. (defcustom org-e-odt-protect-char-alist
  1670. '(("&" . "&amp;")
  1671. ("<" . "&lt;")
  1672. (">" . "&gt;"))
  1673. "Alist of characters to be converted by `org-e-html-protect'."
  1674. :group 'org-export-e-html
  1675. :type '(repeat (cons (string :tag "Character")
  1676. (string :tag "ODT equivalent"))))
  1677. (defcustom org-e-odt-schema-dir
  1678. (let* ((schema-dir
  1679. (catch 'schema-dir
  1680. (message "Debug (org-e-odt): Searching for OpenDocument schema files...")
  1681. (mapc
  1682. (lambda (schema-dir)
  1683. (when schema-dir
  1684. (message "Debug (org-e-odt): Trying %s..." schema-dir)
  1685. (when (and (file-readable-p
  1686. (expand-file-name "od-manifest-schema-v1.2-cs01.rnc"
  1687. schema-dir))
  1688. (file-readable-p
  1689. (expand-file-name "od-schema-v1.2-cs01.rnc"
  1690. schema-dir))
  1691. (file-readable-p
  1692. (expand-file-name "schemas.xml" schema-dir)))
  1693. (message "Debug (org-e-odt): Using schema files under %s"
  1694. schema-dir)
  1695. (throw 'schema-dir schema-dir))))
  1696. org-e-odt-schema-dir-list)
  1697. (message "Debug (org-e-odt): No OpenDocument schema files installed")
  1698. nil)))
  1699. schema-dir)
  1700. "Directory that contains OpenDocument schema files.
  1701. This directory contains:
  1702. 1. rnc files for OpenDocument schema
  1703. 2. a \"schemas.xml\" file that specifies locating rules needed
  1704. for auto validation of OpenDocument XML files.
  1705. Use the customize interface to set this variable. This ensures
  1706. that `rng-schema-locating-files' is updated and auto-validation
  1707. of OpenDocument XML takes place based on the value
  1708. `rng-nxml-auto-validate-flag'.
  1709. The default value of this variable varies depending on the
  1710. version of org in use and is initialized from
  1711. `org-e-odt-schema-dir-list'. The OASIS schema files are available
  1712. only in the org's private git repository. It is *not* bundled
  1713. with GNU ELPA tar or standard Emacs distribution."
  1714. :type '(choice
  1715. (const :tag "Not set" nil)
  1716. (directory :tag "Schema directory"))
  1717. :group 'org-export-e-odt
  1718. :version "24.1"
  1719. :set
  1720. (lambda (var value)
  1721. "Set `org-e-odt-schema-dir'.
  1722. Also add it to `rng-schema-locating-files'."
  1723. (let ((schema-dir value))
  1724. (set var
  1725. (if (and
  1726. (file-readable-p
  1727. (expand-file-name "od-manifest-schema-v1.2-cs01.rnc" schema-dir))
  1728. (file-readable-p
  1729. (expand-file-name "od-schema-v1.2-cs01.rnc" schema-dir))
  1730. (file-readable-p
  1731. (expand-file-name "schemas.xml" schema-dir)))
  1732. schema-dir
  1733. (when value
  1734. (message "Error (org-e-odt): %s has no OpenDocument schema files"
  1735. value))
  1736. nil)))
  1737. (when org-e-odt-schema-dir
  1738. (eval-after-load 'rng-loc
  1739. '(add-to-list 'rng-schema-locating-files
  1740. (expand-file-name "schemas.xml"
  1741. org-e-odt-schema-dir))))))
  1742. (defcustom org-e-odt-content-template-file nil
  1743. "Template file for \"content.xml\".
  1744. The exporter embeds the exported content just before
  1745. \"</office:text>\" element.
  1746. If unspecified, the file named \"OrgOdtContentTemplate.xml\"
  1747. under `org-e-odt-styles-dir' is used."
  1748. :type 'file
  1749. :group 'org-export-e-odt
  1750. :version "24.1")
  1751. (defcustom org-e-odt-styles-file nil
  1752. "Default styles file for use with ODT export.
  1753. Valid values are one of:
  1754. 1. nil
  1755. 2. path to a styles.xml file
  1756. 3. path to a *.odt or a *.ott file
  1757. 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
  1758. ...))
  1759. In case of option 1, an in-built styles.xml is used. See
  1760. `org-e-odt-styles-dir' for more information.
  1761. In case of option 3, the specified file is unzipped and the
  1762. styles.xml embedded therein is used.
  1763. In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
  1764. and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
  1765. generated odt file. Use relative path for specifying the
  1766. FILE-MEMBERS. styles.xml must be specified as one of the
  1767. FILE-MEMBERS.
  1768. Use options 1, 2 or 3 only if styles.xml alone suffices for
  1769. achieving the desired formatting. Use option 4, if the styles.xml
  1770. references additional files like header and footer images for
  1771. achieving the desired formatting.
  1772. Use \"#+ODT_STYLES_FILE: ...\" directive to set this variable on
  1773. a per-file basis. For example,
  1774. #+ODT_STYLES_FILE: \"/path/to/styles.xml\" or
  1775. #+ODT_STYLES_FILE: (\"/path/to/file.ott\" (\"styles.xml\" \"image/hdr.png\"))."
  1776. :group 'org-export-e-odt
  1777. :version "24.1"
  1778. :type
  1779. '(choice
  1780. (const :tag "Factory settings" nil)
  1781. (file :must-match t :tag "styles.xml")
  1782. (file :must-match t :tag "ODT or OTT file")
  1783. (list :tag "ODT or OTT file + Members"
  1784. (file :must-match t :tag "ODF Text or Text Template file")
  1785. (cons :tag "Members"
  1786. (file :tag " Member" "styles.xml")
  1787. (repeat (file :tag "Member"))))))
  1788. (defcustom org-e-odt-inline-image-extensions
  1789. '("png" "jpeg" "jpg" "gif")
  1790. "Extensions of image files that can be inlined into HTML."
  1791. :type '(repeat (string :tag "Extension"))
  1792. :group 'org-export-e-odt
  1793. :version "24.1")
  1794. (defcustom org-e-odt-pixels-per-inch display-pixels-per-inch
  1795. "Scaling factor for converting images pixels to inches.
  1796. Use this for sizing of embedded images. See Info node `(org)
  1797. Images in ODT export' for more information."
  1798. :type 'float
  1799. :group 'org-export-e-odt
  1800. :version "24.1")
  1801. (defcustom org-e-odt-create-custom-styles-for-srcblocks t
  1802. "Whether custom styles for colorized source blocks be automatically created.
  1803. When this option is turned on, the exporter creates custom styles
  1804. for source blocks based on the advice of `htmlfontify'. Creation
  1805. of custom styles happen as part of `org-e-odt-hfy-face-to-css'.
  1806. When this option is turned off exporter does not create such
  1807. styles.
  1808. Use the latter option if you do not want the custom styles to be
  1809. based on your current display settings. It is necessary that the
  1810. styles.xml already contains needed styles for colorizing to work.
  1811. This variable is effective only if
  1812. `org-e-odt-fontify-srcblocks' is turned on."
  1813. :group 'org-export-e-odt
  1814. :version "24.1"
  1815. :type 'boolean)
  1816. (defcustom org-e-odt-preferred-output-format nil
  1817. "Automatically post-process to this format after exporting to \"odt\".
  1818. Interactive commands `org-export-as-e-odt' and
  1819. `org-export-as-e-odt-and-open' export first to \"odt\" format and
  1820. then use `org-e-odt-convert-process' to convert the
  1821. resulting document to this format. During customization of this
  1822. variable, the list of valid values are populated based on
  1823. `org-e-odt-convert-capabilities'."
  1824. :group 'org-export-e-odt
  1825. :version "24.1"
  1826. :type '(choice :convert-widget
  1827. (lambda (w)
  1828. (apply 'widget-convert (widget-type w)
  1829. (eval (car (widget-get w :args)))))
  1830. `((const :tag "None" nil)
  1831. ,@(mapcar (lambda (c)
  1832. `(const :tag ,c ,c))
  1833. (org-e-odt-reachable-formats "odt")))))
  1834. (defcustom org-e-odt-table-styles
  1835. '(("OrgEquation" "OrgEquation"
  1836. ((use-first-column-styles . t)
  1837. (use-last-column-styles . t))))
  1838. "Specify how Table Styles should be derived from a Table Template.
  1839. This is a list where each element is of the
  1840. form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
  1841. TABLE-STYLE-NAME is the style associated with the table through
  1842. `org-e-odt-table-style'.
  1843. TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
  1844. TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
  1845. below) that is included in
  1846. `org-e-odt-content-template-file'.
  1847. TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
  1848. \"TableCell\"
  1849. PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
  1850. \"TableParagraph\"
  1851. TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
  1852. \"FirstRow\" | \"LastRow\" |
  1853. \"EvenRow\" | \"OddRow\" |
  1854. \"EvenColumn\" | \"OddColumn\" | \"\"
  1855. where \"+\" above denotes string concatenation.
  1856. TABLE-CELL-OPTIONS is an alist where each element is of the
  1857. form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
  1858. TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
  1859. `use-last-row-styles' |
  1860. `use-first-column-styles' |
  1861. `use-last-column-styles' |
  1862. `use-banding-rows-styles' |
  1863. `use-banding-columns-styles' |
  1864. `use-first-row-styles'
  1865. ON-OR-OFF := `t' | `nil'
  1866. For example, with the following configuration
  1867. \(setq org-e-odt-table-styles
  1868. '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
  1869. \(\(use-first-row-styles . t\)
  1870. \(use-first-column-styles . t\)\)\)
  1871. \(\"TableWithHeaderColumns\" \"Custom\"
  1872. \(\(use-first-column-styles . t\)\)\)\)\)
  1873. 1. A table associated with \"TableWithHeaderRowsAndColumns\"
  1874. style will use the following table-cell styles -
  1875. \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
  1876. \"CustomTableCell\" and the following paragraph styles
  1877. \"CustomFirstRowTableParagraph\",
  1878. \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
  1879. as appropriate.
  1880. 2. A table associated with \"TableWithHeaderColumns\" style will
  1881. use the following table-cell styles -
  1882. \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
  1883. following paragraph styles
  1884. \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
  1885. as appropriate..
  1886. Note that TABLE-TEMPLATE-NAME corresponds to the
  1887. \"<table:table-template>\" elements contained within
  1888. \"<office:styles>\". The entries (TABLE-STYLE-NAME
  1889. TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
  1890. \"table:template-name\" and \"table:use-first-row-styles\" etc
  1891. attributes of \"<table:table>\" element. Refer ODF-1.2
  1892. specification for more information. Also consult the
  1893. implementation filed under `org-e-odt-get-table-cell-styles'.
  1894. The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
  1895. formatting of numbered display equations. Do not delete this
  1896. style from the list."
  1897. :group 'org-export-e-odt
  1898. :version "24.1"
  1899. :type '(choice
  1900. (const :tag "None" nil)
  1901. (repeat :tag "Table Styles"
  1902. (list :tag "Table Style Specification"
  1903. (string :tag "Table Style Name")
  1904. (string :tag "Table Template Name")
  1905. (alist :options (use-first-row-styles
  1906. use-last-row-styles
  1907. use-first-column-styles
  1908. use-last-column-styles
  1909. use-banding-rows-styles
  1910. use-banding-columns-styles)
  1911. :key-type symbol
  1912. :value-type (const :tag "True" t))))))
  1913. (defcustom org-e-odt-fontify-srcblocks t
  1914. "Specify whether or not source blocks need to be fontified.
  1915. Turn this option on if you want to colorize the source code
  1916. blocks in the exported file. For colorization to work, you need
  1917. to make available an enhanced version of `htmlfontify' library."
  1918. :type 'boolean
  1919. :group 'org-export-e-odt
  1920. :version "24.1")
  1921. (defcustom org-e-odt-prettify-xml t ; FIXME
  1922. "Specify whether or not the xml output should be prettified.
  1923. When this option is turned on, `indent-region' is run on all
  1924. component xml buffers before they are saved. Turn this off for
  1925. regular use. Turn this on if you need to examine the xml
  1926. visually."
  1927. :group 'org-export-e-odt
  1928. :version "24.1"
  1929. :type 'boolean)
  1930. (defcustom org-e-odt-convert-processes
  1931. '(("LibreOffice"
  1932. "soffice --headless --convert-to %f%x --outdir %d %i")
  1933. ("unoconv"
  1934. "unoconv -f %f -o %d %i"))
  1935. "Specify a list of document converters and their usage.
  1936. The converters in this list are offered as choices while
  1937. customizing `org-e-odt-convert-process'.
  1938. This variable is a list where each element is of the
  1939. form (CONVERTER-NAME CONVERTER-CMD). CONVERTER-NAME is the name
  1940. of the converter. CONVERTER-CMD is the shell command for the
  1941. converter and can contain format specifiers. These format
  1942. specifiers are interpreted as below:
  1943. %i input file name in full
  1944. %I input file name as a URL
  1945. %f format of the output file
  1946. %o output file name in full
  1947. %O output file name as a URL
  1948. %d output dir in full
  1949. %D output dir as a URL.
  1950. %x extra options as set in `org-e-odt-convert-capabilities'."
  1951. :group 'org-export-e-odt
  1952. :version "24.1"
  1953. :type
  1954. '(choice
  1955. (const :tag "None" nil)
  1956. (alist :tag "Converters"
  1957. :key-type (string :tag "Converter Name")
  1958. :value-type (group (string :tag "Command line")))))
  1959. (defcustom org-e-odt-convert-process "LibreOffice"
  1960. "Use this converter to convert from \"odt\" format to other formats.
  1961. During customization, the list of converter names are populated
  1962. from `org-e-odt-convert-processes'."
  1963. :group 'org-export-e-odt
  1964. :version "24.1"
  1965. :type '(choice :convert-widget
  1966. (lambda (w)
  1967. (apply 'widget-convert (widget-type w)
  1968. (eval (car (widget-get w :args)))))
  1969. `((const :tag "None" nil)
  1970. ,@(mapcar (lambda (c)
  1971. `(const :tag ,(car c) ,(car c)))
  1972. org-e-odt-convert-processes))))
  1973. (defcustom org-e-odt-convert-capabilities
  1974. '(("Text"
  1975. ("odt" "ott" "doc" "rtf" "docx")
  1976. (("pdf" "pdf") ("odt" "odt") ("rtf" "rtf") ("ott" "ott")
  1977. ("doc" "doc" ":\"MS Word 97\"") ("docx" "docx") ("html" "html")))
  1978. ("Web"
  1979. ("html")
  1980. (("pdf" "pdf") ("odt" "odt") ("html" "html")))
  1981. ("Spreadsheet"
  1982. ("ods" "ots" "xls" "csv" "xlsx")
  1983. (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv") ("ods" "ods")
  1984. ("xls" "xls") ("xlsx" "xlsx")))
  1985. ("Presentation"
  1986. ("odp" "otp" "ppt" "pptx")
  1987. (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("otp" "otp") ("ppt" "ppt")
  1988. ("pptx" "pptx") ("odg" "odg"))))
  1989. "Specify input and output formats of `org-e-odt-convert-process'.
  1990. More correctly, specify the set of input and output formats that
  1991. the user is actually interested in.
  1992. This variable is an alist where each element is of the
  1993. form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
  1994. INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
  1995. alist where each element is of the form (OUTPUT-FMT
  1996. OUTPUT-FILE-EXTENSION EXTRA-OPTIONS).
  1997. The variable is interpreted as follows:
  1998. `org-e-odt-convert-process' can take any document that is in
  1999. INPUT-FMT-LIST and produce any document that is in the
  2000. OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
  2001. OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
  2002. serves dual purposes:
  2003. - It is used for populating completion candidates during
  2004. `org-e-odt-convert' commands.
  2005. - It is used as the value of \"%f\" specifier in
  2006. `org-e-odt-convert-process'.
  2007. EXTRA-OPTIONS is used as the value of \"%x\" specifier in
  2008. `org-e-odt-convert-process'.
  2009. DOCUMENT-CLASS is used to group a set of file formats in
  2010. INPUT-FMT-LIST in to a single class.
  2011. Note that this variable inherently captures how LibreOffice based
  2012. converters work. LibreOffice maps documents of various formats
  2013. to classes like Text, Web, Spreadsheet, Presentation etc and
  2014. allow document of a given class (irrespective of it's source
  2015. format) to be converted to any of the export formats associated
  2016. with that class.
  2017. See default setting of this variable for an typical
  2018. configuration."
  2019. :group 'org-export-e-odt
  2020. :version "24.1"
  2021. :type
  2022. '(choice
  2023. (const :tag "None" nil)
  2024. (alist :tag "Capabilities"
  2025. :key-type (string :tag "Document Class")
  2026. :value-type
  2027. (group (repeat :tag "Input formats" (string :tag "Input format"))
  2028. (alist :tag "Output formats"
  2029. :key-type (string :tag "Output format")
  2030. :value-type
  2031. (group (string :tag "Output file extension")
  2032. (choice
  2033. (const :tag "None" nil)
  2034. (string :tag "Extra options"))))))))
  2035. ;;;; Debugging
  2036. ;;;; Document
  2037. ;;;; Document Header (Styles)
  2038. ;;;; Document Header (Scripts)
  2039. ;;;; Document Header (Mathjax)
  2040. ;;;; Preamble
  2041. ;;;; Postamble
  2042. ;;;; Emphasis
  2043. ;;;; Todos
  2044. ;;;; Tags
  2045. ;;;; Timestamps
  2046. ;;;; Statistics Cookie
  2047. ;;;; Subscript
  2048. ;;;; Superscript
  2049. ;;;; Inline images
  2050. ;;;; Block
  2051. ;;;; Comment
  2052. ;;;; Comment Block
  2053. ;;;; Drawer
  2054. ;;;; Dynamic Block
  2055. ;;;; Emphasis
  2056. ;;;; Entity
  2057. ;;;; Example Block
  2058. ;;;; Export Snippet
  2059. ;;;; Export Block
  2060. ;;;; Fixed Width
  2061. ;;;; Footnotes
  2062. ;;;; Headline
  2063. ;;;; Horizontal Rule
  2064. ;;;; Inline Babel Call
  2065. ;;;; Inline Src Block
  2066. ;;;; Inlinetask
  2067. ;;;; Item
  2068. ;;;; Keyword
  2069. ;;;; Latex Environment
  2070. ;;;; Latex Fragment
  2071. ;;;; Line Break
  2072. ;;;; Link
  2073. ;;;; Babel Call
  2074. ;;;; Macro
  2075. ;;;; Paragraph
  2076. ;;;; Plain List
  2077. ;;;; Plain Text
  2078. ;;;; Property Drawer
  2079. ;;;; Quote Block
  2080. ;;;; Quote Section
  2081. ;;;; Section
  2082. ;;;; Radio Target
  2083. ;;;; Special Block
  2084. ;;;; Src Block
  2085. ;;;; Table
  2086. ;;;; Target
  2087. ;;;; Timestamp
  2088. ;;;; Verbatim
  2089. ;;;; Verse Block
  2090. ;;;; Headline
  2091. ;;;; Links
  2092. ;;;; Drawers
  2093. ;;;; Inlinetasks
  2094. ;;;; Publishing
  2095. ;;;; Compilation
  2096. ;;; User Configurable Variables (MAYBE)
  2097. ;;;; Preamble
  2098. ;;;; Headline
  2099. ;;;; Emphasis
  2100. (defcustom org-e-odt-format-headline-function nil
  2101. "Function to format headline text.
  2102. This function will be called with 5 arguments:
  2103. TODO the todo keyword \(string or nil\).
  2104. TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
  2105. PRIORITY the priority of the headline \(integer or nil\)
  2106. TEXT the main headline text \(string\).
  2107. TAGS the tags string, separated with colons \(string or nil\).
  2108. The function result will be used in the section format string.
  2109. As an example, one could set the variable to the following, in
  2110. order to reproduce the default set-up:
  2111. \(defun org-e-odt-format-headline \(todo todo-type priority text tags\)
  2112. \"Default format function for an headline.\"
  2113. \(concat \(when todo
  2114. \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo\)\)
  2115. \(when priority
  2116. \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
  2117. text
  2118. \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
  2119. :group 'org-export-e-odt
  2120. :type 'function)
  2121. ;;;; Footnotes
  2122. ;;;; Timestamps
  2123. (defcustom org-e-odt-active-timestamp-format "\\textit{%s}"
  2124. "A printf format string to be applied to active timestamps."
  2125. :group 'org-export-e-odt
  2126. :type 'string)
  2127. (defcustom org-e-odt-inactive-timestamp-format "\\textit{%s}"
  2128. "A printf format string to be applied to inactive timestamps."
  2129. :group 'org-export-e-odt
  2130. :type 'string)
  2131. (defcustom org-e-odt-diary-timestamp-format "\\textit{%s}"
  2132. "A printf format string to be applied to diary timestamps."
  2133. :group 'org-export-e-odt
  2134. :type 'string)
  2135. ;;;; Links
  2136. (defcustom org-e-odt-inline-image-rules
  2137. '(("file" . "\\.\\(jpeg\\|jpg\\|png\\|gif\\)\\'"))
  2138. "Rules characterizing image files that can be inlined into HTML.
  2139. A rule consists in an association whose key is the type of link
  2140. to consider, and value is a regexp that will be matched against
  2141. link's path.
  2142. Note that, by default, the image extension *actually* allowed
  2143. depend on the way the HTML file is processed. When used with
  2144. pdflatex, pdf, jpg and png images are OK. When processing
  2145. through dvi to Postscript, only ps and eps are allowed. The
  2146. default we use here encompasses both."
  2147. :group 'org-export-e-odt
  2148. :type '(alist :key-type (string :tag "Type")
  2149. :value-type (regexp :tag "Path")))
  2150. ;;;; Tables
  2151. (defcustom org-e-odt-table-caption-above t
  2152. "When non-nil, place caption string at the beginning of the table.
  2153. Otherwise, place it near the end."
  2154. :group 'org-export-e-odt
  2155. :type 'boolean)
  2156. ;;;; Drawers
  2157. (defcustom org-e-odt-format-drawer-function nil
  2158. "Function called to format a drawer in HTML code.
  2159. The function must accept two parameters:
  2160. NAME the drawer name, like \"LOGBOOK\"
  2161. CONTENTS the contents of the drawer.
  2162. The function should return the string to be exported.
  2163. For example, the variable could be set to the following function
  2164. in order to mimic default behaviour:
  2165. \(defun org-e-odt-format-drawer-default \(name contents\)
  2166. \"Format a drawer element for HTML export.\"
  2167. contents\)"
  2168. :group 'org-export-e-odt
  2169. :type 'function)
  2170. ;;;; Inlinetasks
  2171. (defcustom org-e-odt-format-inlinetask-function nil
  2172. "Function called to format an inlinetask in HTML code.
  2173. The function must accept six parameters:
  2174. TODO the todo keyword, as a string
  2175. TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
  2176. PRIORITY the inlinetask priority, as a string
  2177. NAME the inlinetask name, as a string.
  2178. TAGS the inlinetask tags, as a string.
  2179. CONTENTS the contents of the inlinetask, as a string.
  2180. The function should return the string to be exported.
  2181. For example, the variable could be set to the following function
  2182. in order to mimic default behaviour:
  2183. \(defun org-e-odt-format-inlinetask \(todo type priority name tags contents\)
  2184. \"Format an inline task element for HTML export.\"
  2185. \(let \(\(full-title
  2186. \(concat
  2187. \(when todo
  2188. \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
  2189. \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
  2190. title
  2191. \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
  2192. \(format \(concat \"\\\\begin{center}\\n\"
  2193. \"\\\\fbox{\\n\"
  2194. \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
  2195. \"%s\\n\\n\"
  2196. \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
  2197. \"%s\"
  2198. \"\\\\end{minipage}}\"
  2199. \"\\\\end{center}\"\)
  2200. full-title contents\)\)"
  2201. :group 'org-export-e-odt
  2202. :type 'function)
  2203. ;; Src blocks
  2204. ;;;; Plain text
  2205. (defcustom org-e-odt-quotes
  2206. '(("fr"
  2207. ("\\(\\s-\\|[[(]\\|^\\)\"" . "« ")
  2208. ("\\(\\S-\\)\"" . "» ")
  2209. ("\\(\\s-\\|(\\|^\\)'" . "'"))
  2210. ("en"
  2211. ("\\(\\s-\\|[[(]\\|^\\)\"" . "“")
  2212. ("\\(\\S-\\)\"" . "”")
  2213. ("\\(\\s-\\|(\\|^\\)'" . "‘")
  2214. ("\\(\\S-\\)'" . "’")))
  2215. "Alist for quotes to use when converting english double-quotes.
  2216. The CAR of each item in this alist is the language code.
  2217. The CDR of each item in this alist is a list of three CONS:
  2218. - the first CONS defines the opening quote;
  2219. - the second CONS defines the closing quote;
  2220. - the last CONS defines single quotes.
  2221. For each item in a CONS, the first string is a regexp
  2222. for allowed characters before/after the quote, the second
  2223. string defines the replacement string for this quote."
  2224. :group 'org-export-e-odt
  2225. :type '(list
  2226. (cons :tag "Opening quote"
  2227. (string :tag "Regexp for char before")
  2228. (string :tag "Replacement quote "))
  2229. (cons :tag "Closing quote"
  2230. (string :tag "Regexp for char after ")
  2231. (string :tag "Replacement quote "))
  2232. (cons :tag "Single quote"
  2233. (string :tag "Regexp for char before")
  2234. (string :tag "Replacement quote "))))
  2235. ;;;; Compilation
  2236. ;;; Internal Functions (HTML)
  2237. ;; (defun org-e-odt-format-inline-image (path &optional caption label attr)
  2238. ;; ;; FIXME: alt text missing here?
  2239. ;; (let ((inline-image (format "<img src=\"%s\" alt=\"%s\"/>"
  2240. ;; path (file-name-nondirectory path))))
  2241. ;; (if (not label) inline-image
  2242. ;; (org-e-odt-format-section inline-image "figure" label))))
  2243. ;;;; Bibliography
  2244. (defun org-e-odt-bibliography ()
  2245. "Find bibliography, cut it out and return it."
  2246. (catch 'exit
  2247. (let (beg end (cnt 1) bib)
  2248. (save-excursion
  2249. (goto-char (point-min))
  2250. (when (re-search-forward
  2251. "^[ \t]*<div \\(id\\|class\\)=\"bibliography\"" nil t)
  2252. (setq beg (match-beginning 0))
  2253. (while (re-search-forward "</?div\\>" nil t)
  2254. (setq cnt (+ cnt (if (string= (match-string 0) "<div") +1 -1)))
  2255. (when (= cnt 0)
  2256. (and (looking-at ">") (forward-char 1))
  2257. (setq bib (buffer-substring beg (point)))
  2258. (delete-region beg (point))
  2259. (throw 'exit bib))))
  2260. nil))))
  2261. ;;;; Table
  2262. (defun org-e-odt-format-table (lines olines)
  2263. (let ((org-e-odt-format-table-no-css nil))
  2264. (org-lparse-format-table lines olines)))
  2265. (defun org-e-odt-splice-attributes (tag attributes)
  2266. "Read attributes in string ATTRIBUTES, add and replace in HTML tag TAG."
  2267. (if (not attributes)
  2268. tag
  2269. (let (oldatt newatt)
  2270. (setq oldatt (org-extract-attributes-from-string tag)
  2271. tag (pop oldatt)
  2272. newatt (cdr (org-extract-attributes-from-string attributes)))
  2273. (while newatt
  2274. (setq oldatt (plist-put oldatt (pop newatt) (pop newatt))))
  2275. (if (string-match ">" tag)
  2276. (setq tag
  2277. (replace-match (concat (org-attributes-to-string oldatt) ">")
  2278. t t tag)))
  2279. tag)))
  2280. (defun org-export-splice-style (style extra)
  2281. "Splice EXTRA into STYLE, just before \"</style>\"."
  2282. (if (and (stringp extra)
  2283. (string-match "\\S-" extra)
  2284. (string-match "</style>" style))
  2285. (concat (substring style 0 (match-beginning 0))
  2286. "\n" extra "\n"
  2287. (substring style (match-beginning 0)))
  2288. style))
  2289. (defun org-e-odt-toc-entry-formatter
  2290. (level snumber todo todo-type priority
  2291. headline tags target extra-targets extra-class)
  2292. (org-e-odt-format-toc-entry snumber todo headline tags target))
  2293. (defun org-e-odt-make-string (n string)
  2294. (let (out) (dotimes (i n out) (setq out (concat string out)))))
  2295. (defun org-e-odt-toc-text (toc-entries)
  2296. (let* ((prev-level (1- (nth 1 (car toc-entries))))
  2297. (start-level prev-level))
  2298. (mapconcat
  2299. (lambda (entry)
  2300. (let ((headline (nth 0 entry))
  2301. (level (nth 1 entry)))
  2302. (prog1 (org-e-odt-format-toc-item headline level prev-level)
  2303. (setq prev-level level))))
  2304. toc-entries "")))
  2305. (defun* org-e-odt-format-toc-headline
  2306. (todo todo-type priority text tags
  2307. &key level section-number headline-label &allow-other-keys)
  2308. ;; FIXME
  2309. (setq text (concat
  2310. (and org-export-with-section-numbers
  2311. (concat section-number ". "))
  2312. text
  2313. (and tags
  2314. (concat
  2315. (org-e-odt-format-spaces 3)
  2316. (org-e-odt-format-fontify tags "tag")))))
  2317. (when todo
  2318. (setq text (org-e-odt-format-fontify text "todo")))
  2319. (let ((org-e-odt-suppress-xref t))
  2320. (org-e-odt-format-link text (concat "#" headline-label))))
  2321. (defun org-e-odt-toc (depth info)
  2322. (assert (wholenump depth))
  2323. (let* ((headlines (org-export-collect-headlines info depth))
  2324. (toc-entries
  2325. (loop for headline in headlines collect
  2326. (list (org-e-odt-format-headline--wrap
  2327. headline info 'org-e-odt-format-toc-headline)
  2328. (org-export-get-relative-level headline info)))))
  2329. (when toc-entries
  2330. (let* ((lang-specific-heading "Table of Contents")) ; FIXME
  2331. (concat
  2332. (org-e-odt-begin-toc lang-specific-heading depth)
  2333. (org-e-odt-toc-text toc-entries)
  2334. (org-e-odt-end-toc))))))
  2335. (defun org-e-odt-begin-outline (level1 snumber title tags
  2336. target extra-targets extra-class)
  2337. (let* ((class (format "outline-%d" level1))
  2338. (class (if extra-class (concat class " " extra-class) class))
  2339. (id (format "outline-container-%s"
  2340. (org-lparse-suffix-from-snumber snumber)))
  2341. (extra (concat (when id (format " id=\"%s\"" id))
  2342. (when class (format " class=\"%s\"" class)))))
  2343. (org-lparse-insert-tag "<div%s>" extra)
  2344. (insert
  2345. (org-lparse-format 'HEADING
  2346. (org-lparse-format
  2347. 'HEADLINE title extra-targets tags snumber level1)
  2348. level1 target))))
  2349. (defun org-e-odt-end-outline ()
  2350. (org-lparse-insert-tag "</div>"))
  2351. (defun org-e-odt-suffix-from-snumber (snumber)
  2352. (let* ((snu (replace-regexp-in-string "\\." "-" snumber))
  2353. (href (cdr (assoc (concat "sec-" snu)
  2354. org-export-preferred-target-alist))))
  2355. (org-solidify-link-text (or href snu))))
  2356. (defun org-e-odt-format-outline (contents level1 snumber title
  2357. tags target extra-targets extra-class)
  2358. )
  2359. ;; (defun org-e-odt-format-line (line)
  2360. ;; (case org-lparse-dyn-current-environment
  2361. ;; ((quote fixedwidth) (concat (org-e-odt-encode-plain-text line) "\n"))
  2362. ;; (t (concat line "\n"))))
  2363. (defun org-e-odt-fix-class-name (kwd) ; audit callers of this function
  2364. "Turn todo keyword into a valid class name.
  2365. Replaces invalid characters with \"_\"."
  2366. (save-match-data
  2367. (while (string-match "[^a-zA-Z0-9_]" kwd)
  2368. (setq kwd (replace-match "_" t t kwd))))
  2369. kwd)
  2370. (defun org-e-odt-format-internal-link (text href &optional extra)
  2371. (org-e-odt-format-link text (concat "#" href) extra))
  2372. (defun org-e-odt-format-extra-targets (extra-targets)
  2373. (if (not extra-targets) ""
  2374. (mapconcat (lambda (x)
  2375. (when x
  2376. (setq x (org-solidify-link-text
  2377. (if (org-uuidgen-p x) (concat "ID-" x) x)))
  2378. (org-e-odt-format-anchor "" x))) extra-targets "")))
  2379. (defun org-e-odt-format-org-tags (tags)
  2380. (if (not tags) ""
  2381. (org-e-odt-format-fontify
  2382. (mapconcat
  2383. (lambda (x)
  2384. (org-e-odt-format-fontify
  2385. x (concat "" ;; org-e-odt-tag-class-prefix
  2386. (org-e-odt-fix-class-name x))))
  2387. tags
  2388. (org-e-odt-format-spaces 1)) "tag")))
  2389. (defun org-e-odt-format-section-number (&optional snumber level)
  2390. ;; FIXME
  2391. (and nil org-export-with-section-numbers
  2392. ;; (not org-lparse-body-only)
  2393. snumber level
  2394. (org-e-odt-format-fontify snumber (format "section-number-%d" level))))
  2395. ;; (defun org-e-odt-format-headline (title extra-targets tags
  2396. ;; &optional snumber level)
  2397. ;; (concat
  2398. ;; (org-e-odt-format-extra-targets extra-targets)
  2399. ;; (concat (org-e-odt-format-section-number snumber level) " ")
  2400. ;; title
  2401. ;; (and tags (concat (org-e-odt-format-spaces 3)
  2402. ;; (org-e-odt-format-org-tags tags)))))
  2403. ;; (defun org-e-odt-format-date (info)
  2404. ;; (let ((date (plist-get info :date)))
  2405. ;; (cond
  2406. ;; ((and date (string-match "%" date))
  2407. ;; (format-time-string date))
  2408. ;; (date date)
  2409. ;; (t (format-time-string "%Y-%m-%d %T %Z")))))
  2410. ;;; Internal Functions (Ngz)
  2411. (defun org-e-odt--caption/label-string (caption label info)
  2412. "Return caption and label HTML string for floats.
  2413. CAPTION is a cons cell of secondary strings, the car being the
  2414. standard caption and the cdr its short form. LABEL is a string
  2415. representing the label. INFO is a plist holding contextual
  2416. information.
  2417. If there's no caption nor label, return the empty string.
  2418. For non-floats, see `org-e-odt--wrap-label'."
  2419. (setq label nil) ;; FIXME
  2420. (let ((label-str (if label (format "\\label{%s}" label) "")))
  2421. (cond
  2422. ((and (not caption) (not label)) "")
  2423. ((not caption) (format "\\label{%s}\n" label))
  2424. ;; Option caption format with short name.
  2425. ((cdr caption)
  2426. (format "\\caption[%s]{%s%s}\n"
  2427. (org-export-data (cdr caption) info)
  2428. label-str
  2429. (org-export-data (car caption) info)))
  2430. ;; Standard caption format.
  2431. ;; (t (format "\\caption{%s%s}\n"
  2432. ;; label-str
  2433. ;; (org-export-data (car caption) info)))
  2434. (t (org-export-data (car caption) info)))))
  2435. (defun org-e-odt--find-verb-separator (s)
  2436. "Return a character not used in string S.
  2437. This is used to choose a separator for constructs like \\verb."
  2438. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  2439. (loop for c across ll
  2440. when (not (string-match (regexp-quote (char-to-string c)) s))
  2441. return (char-to-string c))))
  2442. (defun org-e-odt--quotation-marks (text info)
  2443. "Export quotation marks depending on language conventions.
  2444. TEXT is a string containing quotation marks to be replaced. INFO
  2445. is a plist used as a communication channel."
  2446. (mapc (lambda(l)
  2447. (let ((start 0))
  2448. (while (setq start (string-match (car l) text start))
  2449. (let ((new-quote (concat (match-string 1 text) (cdr l))))
  2450. (setq text (replace-match new-quote t t text))))))
  2451. (cdr (or (assoc (plist-get info :language) org-e-odt-quotes)
  2452. ;; Falls back on English.
  2453. (assoc "en" org-e-odt-quotes))))
  2454. text)
  2455. (defun org-e-odt--wrap-label (element output)
  2456. "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
  2457. This function shouldn't be used for floats. See
  2458. `org-e-odt--caption/label-string'."
  2459. ;; (let ((label (org-element-property :name element)))
  2460. ;; (if (or (not output) (not label) (string= output "") (string= label ""))
  2461. ;; output
  2462. ;; (concat (format "\\label{%s}\n" label) output)))
  2463. output)
  2464. ;;; Transcode Helpers
  2465. (defun* org-e-odt-format-headline
  2466. (todo todo-type priority text tags
  2467. &key level section-number headline-label &allow-other-keys)
  2468. (concat (org-e-odt-todo todo) (and todo " ") text
  2469. (and tags (org-e-odt-format-spaces 3))
  2470. (and tags (org-e-odt-format-org-tags tags))))
  2471. ;;;; Src Code
  2472. (defun org-e-odt-htmlfontify-string (line)
  2473. (let* ((hfy-html-quote-regex "\\([<\"&> ]\\)")
  2474. (hfy-html-quote-map '(("\"" "&quot;")
  2475. ("<" "&lt;")
  2476. ("&" "&amp;")
  2477. (">" "&gt;")
  2478. (" " "<text:s/>")
  2479. (" " "<text:tab/>")))
  2480. (hfy-face-to-css 'org-e-odt-hfy-face-to-css)
  2481. (hfy-optimisations-1 (copy-seq hfy-optimisations))
  2482. (hfy-optimisations (add-to-list 'hfy-optimisations-1
  2483. 'body-text-only))
  2484. (hfy-begin-span-handler
  2485. (lambda (style text-block text-id text-begins-block-p)
  2486. (insert (format "<text:span text:style-name=\"%s\">" style))))
  2487. (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
  2488. (htmlfontify-string line)))
  2489. (defun org-e-odt-do-format-code
  2490. (code &optional lang refs retain-labels num-start)
  2491. (let* ((lang (or (assoc-default lang org-src-lang-modes) lang))
  2492. (lang-mode (and lang (intern (format "%s-mode" lang))))
  2493. (code-lines (org-split-string code "\n"))
  2494. (code-length (length code-lines))
  2495. (use-htmlfontify-p (and (functionp lang-mode)
  2496. org-e-odt-fontify-srcblocks
  2497. (require 'htmlfontify nil t)
  2498. (fboundp 'htmlfontify-string)))
  2499. (code (if (not use-htmlfontify-p) code
  2500. (with-temp-buffer
  2501. (insert code)
  2502. (funcall lang-mode)
  2503. (font-lock-fontify-buffer)
  2504. (buffer-string))))
  2505. (fontifier (if use-htmlfontify-p 'org-e-odt-htmlfontify-string
  2506. 'org-e-odt-encode-plain-text))
  2507. (par-style (if use-htmlfontify-p "OrgSrcBlock"
  2508. "OrgFixedWidthBlock"))
  2509. (i 0))
  2510. (assert (= code-length (length (org-split-string code "\n"))))
  2511. (setq code
  2512. (org-export-format-code
  2513. code
  2514. (lambda (loc line-num ref)
  2515. (setq par-style
  2516. (concat par-style (and (= (incf i) code-length) "LastLine")))
  2517. (setq loc (concat loc (and ref retain-labels (format " (%s)" ref))))
  2518. (setq loc (funcall fontifier loc))
  2519. (when ref
  2520. (setq loc (org-e-odt-format-target loc (concat "coderef-" ref))))
  2521. (setq loc (org-e-odt-format-stylized-paragraph par-style loc))
  2522. (if (not line-num) loc
  2523. (org-e-odt-format-tags
  2524. '("<text:list-item>" . "</text:list-item>") loc)))
  2525. num-start refs))
  2526. (cond
  2527. ((not num-start) code)
  2528. ((equal num-start 0)
  2529. (org-e-odt-format-tags
  2530. '("<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>"
  2531. . "</text:list>") code " text:continue-numbering=\"false\""))
  2532. (t (org-e-odt-format-tags
  2533. '("<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>"
  2534. . "</text:list>") code " text:continue-numbering=\"true\"")))))
  2535. (defun org-e-odt-format-code (element info)
  2536. (let* ((lang (org-element-property :language element))
  2537. ;; Extract code and references.
  2538. (code-info (org-export-unravel-code element))
  2539. (code (car code-info))
  2540. (refs (cdr code-info))
  2541. ;; Does the src block contain labels?
  2542. (retain-labels (org-element-property :retain-labels element))
  2543. ;; Does it have line numbers?
  2544. (num-start (case (org-element-property :number-lines element)
  2545. (continued (org-export-get-loc element info))
  2546. (new 0))))
  2547. (org-e-odt-do-format-code code lang refs retain-labels num-start)))
  2548. ;;; Template
  2549. (defun org-e-odt-template (contents info)
  2550. "Return complete document string after HTML conversion.
  2551. CONTENTS is the transcoded contents string. RAW-DATA is the
  2552. original parsed data. INFO is a plist holding export options."
  2553. ;; write meta file
  2554. (org-e-odt-update-meta-file info)
  2555. (with-temp-buffer
  2556. (insert-file-contents
  2557. (or org-e-odt-content-template-file
  2558. (expand-file-name "OrgOdtContentTemplate.xml"
  2559. org-e-odt-styles-dir)))
  2560. (goto-char (point-min))
  2561. (re-search-forward "</office:text>" nil nil)
  2562. (goto-char (match-beginning 0))
  2563. ;; Title
  2564. (insert (org-e-odt-format-preamble info))
  2565. ;; Table of Contents
  2566. (let ((depth (plist-get info :with-toc)))
  2567. (when (wholenump depth) (insert (org-e-odt-toc depth info))))
  2568. ;; Copy styles.xml. Also dump htmlfontify styles, if there is any.
  2569. (org-e-odt-update-styles-file info)
  2570. ;; Update styles.xml - take care of outline numbering
  2571. (with-current-buffer
  2572. (find-file-noselect (expand-file-name "styles.xml") t)
  2573. ;; Don't make automatic backup of styles.xml file. This setting
  2574. ;; prevents the backed-up styles.xml file from being zipped in to
  2575. ;; odt file. This is more of a hackish fix. Better alternative
  2576. ;; would be to fix the zip command so that the output odt file
  2577. ;; includes only the needed files and excludes any auto-generated
  2578. ;; extra files like backups and auto-saves etc etc. Note that
  2579. ;; currently the zip command zips up the entire temp directory so
  2580. ;; that any auto-generated files created under the hood ends up in
  2581. ;; the resulting odt file.
  2582. (set (make-local-variable 'backup-inhibited) t)
  2583. (org-e-odt-configure-outline-numbering))
  2584. ;; Contents
  2585. (insert contents)
  2586. (buffer-substring-no-properties (point-min) (point-max))))
  2587. ;;; Transcode Functions
  2588. ;;;; Bold
  2589. (defun org-e-odt-bold (bold contents info)
  2590. "Transcode BOLD from Org to HTML.
  2591. CONTENTS is the text with bold markup. INFO is a plist holding
  2592. contextual information."
  2593. (org-e-odt-format-fontify contents 'bold))
  2594. ;;;; Center Block
  2595. (defun org-e-odt-center-block (center-block contents info)
  2596. "Transcode a CENTER-BLOCK element from Org to HTML.
  2597. CONTENTS holds the contents of the center block. INFO is a plist
  2598. holding contextual information."
  2599. (org-e-odt--wrap-label center-block contents))
  2600. ;;;; Clock
  2601. (defun org-e-odt-clock (clock contents info)
  2602. "Transcode a CLOCK element from Org to HTML.
  2603. CONTENTS is nil. INFO is a plist used as a communication
  2604. channel."
  2605. (org-e-odt-format-fontify
  2606. (concat (org-e-odt-format-fontify org-clock-string "timestamp-kwd")
  2607. (org-e-odt-format-fontify
  2608. (concat (org-translate-time (org-element-property :value clock))
  2609. (let ((time (org-element-property :time clock)))
  2610. (and time (format " (%s)" time))))
  2611. "timestamp"))
  2612. "timestamp-wrapper"))
  2613. ;;;; Code
  2614. (defun org-e-odt-code (code contents info)
  2615. "Transcode a CODE object from Org to HTML.
  2616. CONTENTS is nil. INFO is a plist used as a communication
  2617. channel."
  2618. (org-e-odt-format-fontify (org-element-property :value code) 'code))
  2619. ;;;; Comment
  2620. ;; Comments are ignored.
  2621. ;;;; Comment Block
  2622. ;; Comment Blocks are ignored.
  2623. ;;;; Drawer
  2624. (defun org-e-odt-drawer (drawer contents info)
  2625. "Transcode a DRAWER element from Org to HTML.
  2626. CONTENTS holds the contents of the block. INFO is a plist
  2627. holding contextual information."
  2628. (let* ((name (org-element-property :drawer-name drawer))
  2629. (output (if (functionp org-e-odt-format-drawer-function)
  2630. (funcall org-e-odt-format-drawer-function
  2631. name contents)
  2632. ;; If there's no user defined function: simply
  2633. ;; display contents of the drawer.
  2634. contents)))
  2635. (org-e-odt--wrap-label drawer output)))
  2636. ;;;; Dynamic Block
  2637. (defun org-e-odt-dynamic-block (dynamic-block contents info)
  2638. "Transcode a DYNAMIC-BLOCK element from Org to HTML.
  2639. CONTENTS holds the contents of the block. INFO is a plist
  2640. holding contextual information. See `org-export-data'."
  2641. (org-e-odt--wrap-label dynamic-block contents))
  2642. ;;;; Entity
  2643. (defun org-e-odt-entity (entity contents info)
  2644. "Transcode an ENTITY object from Org to HTML.
  2645. CONTENTS are the definition itself. INFO is a plist holding
  2646. contextual information."
  2647. ;; (let ((ent (org-element-property :latex entity)))
  2648. ;; (if (org-element-property :latex-math-p entity)
  2649. ;; (format "$%s$" ent)
  2650. ;; ent))
  2651. (org-element-property :utf-8 entity))
  2652. ;;;; Example Block
  2653. (defun org-e-odt-example-block (example-block contents info)
  2654. "Transcode a EXAMPLE-BLOCK element from Org to HTML.
  2655. CONTENTS is nil. INFO is a plist holding contextual information."
  2656. (let* ((options (or (org-element-property :options example-block) ""))
  2657. (value (org-export-handle-code example-block info nil nil t)))
  2658. (org-e-odt--wrap-label
  2659. example-block (org-e-odt-format-source-code-or-example value nil))))
  2660. ;;;; Export Snippet
  2661. (defun org-e-odt-export-snippet (export-snippet contents info)
  2662. "Transcode a EXPORT-SNIPPET object from Org to HTML.
  2663. CONTENTS is nil. INFO is a plist holding contextual information."
  2664. (when (eq (org-export-snippet-backend export-snippet) 'e-odt)
  2665. (org-element-property :value export-snippet)))
  2666. ;;;; Export Block
  2667. (defun org-e-odt-export-block (export-block contents info)
  2668. "Transcode a EXPORT-BLOCK element from Org to HTML.
  2669. CONTENTS is nil. INFO is a plist holding contextual information."
  2670. (when (string= (org-element-property :type export-block) "ODT")
  2671. (org-remove-indentation (org-element-property :value export-block))))
  2672. ;;;; Fixed Width
  2673. (defun org-e-odt-fixed-width (fixed-width contents info)
  2674. "Transcode a FIXED-WIDTH element from Org to HTML.
  2675. CONTENTS is nil. INFO is a plist holding contextual information."
  2676. (org-e-odt--wrap-label
  2677. fixed-width
  2678. (org-e-odt-format-source-code-or-example
  2679. (org-element-property :value fixed-width) nil)))
  2680. ;;;; Footnote Definition
  2681. ;; Footnote Definitions are ignored.
  2682. ;;;; Footnote Reference
  2683. (defun org-e-odt-footnote-def (raw info) ; FIXME
  2684. (if (equal (org-element-type raw) 'org-data)
  2685. (org-trim (org-export-data raw info)) ; fix paragraph style
  2686. (org-e-odt-format-stylized-paragraph
  2687. 'footnote (org-trim (org-export-data raw info)))))
  2688. (defvar org-e-odt-footnote-separator
  2689. (org-e-odt-format-fontify "," 'superscript))
  2690. (defun org-e-odt-footnote-reference (footnote-reference contents info)
  2691. "Transcode a FOOTNOTE-REFERENCE element from Org to HTML.
  2692. CONTENTS is nil. INFO is a plist holding contextual information."
  2693. (concat
  2694. ;; Insert separator between two footnotes in a row.
  2695. (let ((prev (org-export-get-previous-element footnote-reference info)))
  2696. (when (eq (org-element-type prev) 'footnote-reference)
  2697. org-e-odt-footnote-separator))
  2698. (cond
  2699. ((not (org-export-footnote-first-reference-p footnote-reference info))
  2700. (let* ((n (org-export-get-footnote-number footnote-reference info)))
  2701. (org-e-odt-format-footnote-reference n "IGNORED" 100)))
  2702. ;; Inline definitions are secondary strings.
  2703. ((eq (org-element-property :type footnote-reference) 'inline)
  2704. (let* ((raw (org-export-get-footnote-definition footnote-reference info))
  2705. (n (org-export-get-footnote-number footnote-reference info))
  2706. (def (org-e-odt-footnote-def raw info)))
  2707. (org-e-odt-format-footnote-reference n def 1)))
  2708. ;; Non-inline footnotes definitions are full Org data.
  2709. (t
  2710. (let* ((raw (org-export-get-footnote-definition footnote-reference info))
  2711. (n (org-export-get-footnote-number footnote-reference info))
  2712. (def (org-e-odt-footnote-def raw info)))
  2713. (org-e-odt-format-footnote-reference n def 1))))))
  2714. ;;;; Headline
  2715. (defun org-e-odt-todo (todo)
  2716. (when todo
  2717. (org-e-odt-format-fontify
  2718. (concat
  2719. "" ; org-e-odt-todo-kwd-class-prefix
  2720. (org-e-odt-fix-class-name todo))
  2721. (list (if (member todo org-done-keywords) "done" "todo")
  2722. todo))))
  2723. (defun org-e-odt-format-headline--wrap (headline info
  2724. &optional format-function
  2725. &rest extra-keys)
  2726. "Transcode an HEADLINE element from Org to ODT.
  2727. CONTENTS holds the contents of the headline. INFO is a plist
  2728. holding contextual information."
  2729. (let* ((level (+ (org-export-get-relative-level headline info)))
  2730. (headline-number (org-export-get-headline-number headline info))
  2731. (section-number (and (org-export-numbered-headline-p headline info)
  2732. (mapconcat 'number-to-string
  2733. headline-number ".")))
  2734. (todo (and (plist-get info :with-todo-keywords)
  2735. (let ((todo (org-element-property :todo-keyword headline)))
  2736. (and todo (org-export-data todo info)))))
  2737. (todo-type (and todo (org-element-property :todo-type headline)))
  2738. (priority (and (plist-get info :with-priority)
  2739. (org-element-property :priority headline)))
  2740. (text (org-export-data (org-element-property :title headline) info))
  2741. (tags (and (plist-get info :with-tags)
  2742. (org-element-property :tags headline)))
  2743. (headline-label (concat "sec-" (mapconcat 'number-to-string
  2744. headline-number "-")))
  2745. (format-function (cond
  2746. ((functionp format-function) format-function)
  2747. ((functionp org-e-odt-format-headline-function)
  2748. (function*
  2749. (lambda (todo todo-type priority text tags
  2750. &allow-other-keys)
  2751. (funcall org-e-odt-format-headline-function
  2752. todo todo-type priority text tags))))
  2753. (t 'org-e-odt-format-headline))))
  2754. (apply format-function
  2755. todo todo-type priority text tags
  2756. :headline-label headline-label :level level
  2757. :section-number section-number extra-keys)))
  2758. (defun org-e-odt-headline (headline contents info)
  2759. "Transcode an HEADLINE element from Org to HTML.
  2760. CONTENTS holds the contents of the headline. INFO is a plist
  2761. holding contextual information."
  2762. (let* ((numberedp (org-export-numbered-headline-p headline info))
  2763. ;; Get level relative to current parsed data.
  2764. (level (org-export-get-relative-level headline info))
  2765. (text (org-export-data (org-element-property :title headline) info))
  2766. ;; Create the headline text.
  2767. (full-text (org-e-odt-format-headline--wrap headline info)))
  2768. (cond
  2769. ;; Case 1: This is a footnote section: ignore it.
  2770. ((org-element-property :footnote-section-p headline) nil)
  2771. ;; Case 2. This is a deep sub-tree: export it as a list item.
  2772. ;; Also export as items headlines for which no section
  2773. ;; format has been found.
  2774. ((org-export-low-level-p headline info) ; FIXME (or (not section-fmt))
  2775. ;; Build the real contents of the sub-tree.
  2776. (let* ((type (if numberedp 'unordered 'unordered)) ; FIXME
  2777. (itemized-body (org-e-odt-format-list-item
  2778. contents type nil nil full-text)))
  2779. (concat
  2780. (and (org-export-first-sibling-p headline info)
  2781. (org-e-odt-begin-plain-list type))
  2782. itemized-body
  2783. (and (org-export-last-sibling-p headline info)
  2784. (org-e-odt-end-plain-list type)))))
  2785. ;; Case 3. Standard headline. Export it as a section.
  2786. (t
  2787. (let* ((extra-ids (list (org-element-property :custom-id headline)
  2788. (org-element-property :id headline)))
  2789. (extra-ids nil) ; FIXME
  2790. (id (concat "sec-" (mapconcat 'number-to-string
  2791. (org-export-get-headline-number
  2792. headline info) "-"))))
  2793. (concat
  2794. (org-e-odt-format-tags
  2795. '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
  2796. "</text:h>")
  2797. (concat (org-e-odt-format-extra-targets extra-ids)
  2798. (if (not id) full-text (org-e-odt-format-target full-text id) ))
  2799. level level)
  2800. contents))))))
  2801. ;;;; Horizontal Rule
  2802. (defun org-e-odt-horizontal-rule (horizontal-rule contents info)
  2803. "Transcode an HORIZONTAL-RULE object from Org to HTML.
  2804. CONTENTS is nil. INFO is a plist holding contextual information."
  2805. (let ((attr (mapconcat #'identity
  2806. (org-element-property :attr_odt horizontal-rule)
  2807. " ")))
  2808. (org-e-odt--wrap-label horizontal-rule
  2809. (org-e-odt-format-horizontal-line))))
  2810. ;;;; Inline Babel Call
  2811. ;; Inline Babel Calls are ignored.
  2812. ;;;; Inline Src Block
  2813. (defun org-e-odt-inline-src-block (inline-src-block contents info)
  2814. "Transcode an INLINE-SRC-BLOCK element from Org to HTML.
  2815. CONTENTS holds the contents of the item. INFO is a plist holding
  2816. contextual information."
  2817. (let* ((org-lang (org-element-property :language inline-src-block))
  2818. (code (org-element-property :value inline-src-block))
  2819. (separator (org-e-odt--find-verb-separator code)))
  2820. (error "FIXME")))
  2821. ;;;; Inlinetask
  2822. (defun org-e-odt-format-section (text class &optional id)
  2823. (let ((extra (concat (when id (format " id=\"%s\"" id)))))
  2824. (concat (format "<div class=\"%s\"%s>\n" class extra) text "</div>\n")))
  2825. (defun org-e-odt-inlinetask (inlinetask contents info)
  2826. "Transcode an INLINETASK element from Org to ODT.
  2827. CONTENTS holds the contents of the block. INFO is a plist
  2828. holding contextual information."
  2829. (cond
  2830. ;; If `org-e-odt-format-inlinetask-function' is provided, call it
  2831. ;; with appropriate arguments.
  2832. ((functionp org-e-odt-format-inlinetask-function)
  2833. (let ((format-function
  2834. (function*
  2835. (lambda (todo todo-type priority text tags
  2836. &key contents &allow-other-keys)
  2837. (funcall org-e-odt-format-inlinetask-function
  2838. todo todo-type priority text tags contents)))))
  2839. (org-e-odt-format-headline--wrap
  2840. inlinetask info format-function :contents contents)))
  2841. ;; Otherwise, use a default template.
  2842. (t (org-e-odt--wrap-label
  2843. inlinetask
  2844. (org-e-odt-format-stylized-paragraph
  2845. nil (org-e-odt-format-textbox
  2846. (concat (org-e-odt-format-stylized-paragraph
  2847. "OrgInlineTaskHeading" (org-e-odt-format-headline--wrap
  2848. inlinetask info))
  2849. contents)
  2850. nil nil "OrgInlineTaskFrame" " style:rel-width=\"100%\""))))))
  2851. ;;;; Italic
  2852. (defun org-e-odt-italic (italic contents info)
  2853. "Transcode ITALIC from Org to HTML.
  2854. CONTENTS is the text with italic markup. INFO is a plist holding
  2855. contextual information."
  2856. (org-e-odt-format-fontify contents 'italic))
  2857. ;;;; Item
  2858. (defun org-e-odt-format-list-item (contents type checkbox
  2859. &optional term-counter-id
  2860. headline)
  2861. (when checkbox
  2862. (setq checkbox
  2863. (org-e-odt-format-fontify (case checkbox
  2864. (on "[X]")
  2865. (off "[&nbsp;]")
  2866. (trans "[-]")) 'code)))
  2867. (concat
  2868. (org-e-odt-begin-list-item type term-counter-id headline)
  2869. ;; FIXME checkbox (and checkbox " ")
  2870. contents
  2871. (org-e-odt-end-list-item type)))
  2872. (defun org-e-odt-item (item contents info)
  2873. "Transcode an ITEM element from Org to HTML.
  2874. CONTENTS holds the contents of the item. INFO is a plist holding
  2875. contextual information."
  2876. (let* ((plain-list (org-export-get-parent item info))
  2877. (type (org-element-property :type plain-list))
  2878. (counter (org-element-property :counter item))
  2879. (checkbox (org-element-property :checkbox item))
  2880. (tag (let ((tag (org-element-property :tag item)))
  2881. (and tag (org-export-data tag info)))))
  2882. (org-e-odt-format-list-item
  2883. contents type checkbox (or tag counter))))
  2884. ;;;; Keyword
  2885. (defun org-e-odt-keyword (keyword contents info)
  2886. "Transcode a KEYWORD element from Org to HTML.
  2887. CONTENTS is nil. INFO is a plist holding contextual information."
  2888. (let ((key (org-element-property :key keyword))
  2889. (value (org-element-property :value keyword)))
  2890. (cond
  2891. ((string= key "LATEX") value)
  2892. ((string= key "INDEX") (format "\\index{%s}" value))
  2893. ((string= key "TARGET") nil ; FIXME
  2894. ;; (format "\\label{%s}" (org-export-solidify-link-text value))
  2895. )
  2896. ((string= key "toc")
  2897. (let ((value (downcase value)))
  2898. (cond
  2899. ((string-match "\\<headlines\\>" value)
  2900. (let ((depth (or (and (string-match "[0-9]+" value)
  2901. (string-to-number (match-string 0 value)))
  2902. (plist-get info :with-toc))))
  2903. (when (wholenump depth) (org-e-odt-toc depth info))))
  2904. ((string= "tables" value) "FIXME")
  2905. ((string= "figures" value) "FIXME")
  2906. ((string= "listings" value)
  2907. (cond
  2908. ;; At the moment, src blocks with a caption are wrapped
  2909. ;; into a figure environment.
  2910. (t "FIXME")))))))))
  2911. ;;;; Latex Environment
  2912. (defun org-e-odt-format-latex (latex-frag processing-type)
  2913. (let* ((prefix (case processing-type
  2914. (dvipng "ltxpng/")
  2915. (mathml "ltxmathml/")))
  2916. (cache-relpath
  2917. (concat prefix (file-name-sans-extension
  2918. (file-name-nondirectory (buffer-file-name)))))
  2919. (cache-dir (file-name-directory (buffer-file-name )))
  2920. (display-msg (case processing-type
  2921. (dvipng "Creating LaTeX Image...")
  2922. (mathml "Creating MathML snippet..."))))
  2923. (with-temp-buffer
  2924. (insert latex-frag)
  2925. (org-format-latex cache-relpath cache-dir nil display-msg
  2926. nil nil processing-type)
  2927. (buffer-string))))
  2928. (defun org-e-odt-latex-environment (latex-environment contents info)
  2929. "Transcode a LATEX-ENVIRONMENT element from Org to HTML.
  2930. CONTENTS is nil. INFO is a plist holding contextual information."
  2931. (org-e-odt--wrap-label
  2932. latex-environment
  2933. (let* ((latex-frag
  2934. (org-remove-indentation
  2935. (org-element-property :value latex-environment)))
  2936. (processing-type (plist-get info :LaTeX-fragments))
  2937. (caption (org-element-property :caption latex-environment))
  2938. (short-caption (and (cdr caption)
  2939. (org-export-data (cdr caption) info)))
  2940. (caption (and (car caption) (org-export-data (car caption) info)))
  2941. (label (org-element-property :name latex-environment))
  2942. (attr nil) ; FIXME
  2943. (label (org-element-property :name latex-environment)))
  2944. (cond
  2945. ((member processing-type '(t mathjax))
  2946. (org-e-odt-format-formula latex-environment info))
  2947. ((equal processing-type 'dvipng)
  2948. (org-e-odt-format-stylized-paragraph
  2949. nil (org-e-odt-link--inline-image latex-environment info)))
  2950. (t latex-frag)))))
  2951. ;;;; Latex Fragment
  2952. ;; (when latex-frag ; FIXME
  2953. ;; (setq href (org-propertize href :title "LaTeX Fragment"
  2954. ;; :description latex-frag)))
  2955. ;; handle verbatim
  2956. ;; provide descriptions
  2957. (defun org-e-odt-latex-fragment (latex-fragment contents info)
  2958. "Transcode a LATEX-FRAGMENT object from Org to HTML.
  2959. CONTENTS is nil. INFO is a plist holding contextual information."
  2960. (let* ((latex-frag (org-element-property :value latex-fragment))
  2961. (processing-type (plist-get info :LaTeX-fragments)))
  2962. (cond
  2963. ((member processing-type '(t mathjax))
  2964. (org-e-odt-format-formula latex-fragment info))
  2965. ((equal processing-type 'dvipng)
  2966. (org-e-odt-link--inline-image latex-fragment info))
  2967. (t latex-frag))))
  2968. ;;;; Line Break
  2969. (defun org-e-odt-line-break (line-break contents info)
  2970. "Transcode a LINE-BREAK object from Org to HTML.
  2971. CONTENTS is nil. INFO is a plist holding contextual information."
  2972. "<text:line-break/>\n")
  2973. ;;;; Link
  2974. (defun org-e-odt-link--inline-image (element info)
  2975. "Return HTML code for an inline image.
  2976. LINK is the link pointing to the inline image. INFO is a plist
  2977. used as a communication channel."
  2978. (let* ((src (cond
  2979. ((eq (org-element-type element) 'link)
  2980. (let* ((type (org-element-property :type element))
  2981. (raw-path (org-element-property :path element)))
  2982. (cond ((member type '("http" "https"))
  2983. (concat type ":" raw-path))
  2984. ((file-name-absolute-p raw-path)
  2985. (expand-file-name raw-path))
  2986. (t raw-path))))
  2987. ((member (org-element-type element)
  2988. '(latex-fragment latex-environment))
  2989. (let* ((latex-frag (org-remove-indentation
  2990. (org-element-property
  2991. :value element)))
  2992. (formula-link (org-e-odt-format-latex
  2993. latex-frag 'dvipng)))
  2994. (and formula-link
  2995. (string-match "file:\\([^]]*\\)" formula-link)
  2996. (match-string 1 formula-link))))
  2997. (t (error "what is this?"))))
  2998. (href (org-e-odt-format-tags
  2999. "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
  3000. (org-e-odt-copy-image-file src)))
  3001. ;; extract attributes from #+ATTR_ODT line.
  3002. (attr-from (case (org-element-type element)
  3003. (link (org-export-get-parent-paragraph element info))
  3004. (t element)))
  3005. ;; convert attributes to a plist.
  3006. (attr-plist (org-e-odt-element-attributes attr-from info))
  3007. ;; handle `:anchor', `:style' and `:attributes' properties.
  3008. (user-frame-anchor
  3009. (car (assoc-string (plist-get attr-plist :anchor)
  3010. '(("as-char") ("paragraph") ("page")) t)))
  3011. (user-frame-style
  3012. (and user-frame-anchor (plist-get attr-plist :style)))
  3013. (user-frame-attrs
  3014. (and user-frame-anchor (plist-get attr-plist :attributes)))
  3015. (user-frame-params
  3016. (list user-frame-style user-frame-attrs user-frame-anchor))
  3017. ;; (embed-as (or embed-as user-frame-anchor "paragraph"))
  3018. ;; extrac
  3019. ;; handle `:width', `:height' and `:scale' properties.
  3020. (size (org-e-odt-image-size-from-file
  3021. src (plist-get attr-plist :width)
  3022. (plist-get attr-plist :height)
  3023. (plist-get attr-plist :scale) nil ;; embed-as
  3024. "paragraph" ; FIXME
  3025. ))
  3026. (width (car size)) (height (cdr size))
  3027. (embed-as
  3028. (case (org-element-type element)
  3029. ((org-e-odt-standalone-image-p element info) "paragraph")
  3030. (latex-fragment "as-char")
  3031. (latex-environment "paragraph")
  3032. (t "paragraph")))
  3033. (captions (org-e-odt-format-label element info 'definition))
  3034. (caption (car captions)) (short-caption (cdr captions))
  3035. (entity (concat (and caption "Captioned") embed-as "Image")))
  3036. (org-e-odt-format-entity entity href width height
  3037. captions user-frame-params )))
  3038. (defun org-e-odt-format-entity (entity href width height &optional
  3039. captions user-frame-params)
  3040. (let* ((caption (car captions)) (short-caption (cdr captions))
  3041. (entity-style (assoc-string entity org-e-odt-entity-frame-styles t))
  3042. default-frame-params frame-params)
  3043. (cond
  3044. ((not caption)
  3045. (setq default-frame-params (nth 2 entity-style))
  3046. (setq frame-params (org-e-odt-merge-frame-params
  3047. default-frame-params user-frame-params))
  3048. (apply 'org-e-odt-format-frame href width height frame-params))
  3049. (t
  3050. (setq default-frame-params (nth 3 entity-style))
  3051. (setq frame-params (org-e-odt-merge-frame-params
  3052. default-frame-params user-frame-params))
  3053. (apply 'org-e-odt-format-textbox
  3054. (org-e-odt-format-stylized-paragraph
  3055. 'illustration
  3056. (concat
  3057. (apply 'org-e-odt-format-frame href width height
  3058. (let ((entity-style-1 (copy-sequence
  3059. (nth 2 entity-style))))
  3060. (setcar (cdr entity-style-1)
  3061. (concat
  3062. (cadr entity-style-1)
  3063. (and short-caption
  3064. (format " draw:name=\"%s\" "
  3065. short-caption))))
  3066. entity-style-1))
  3067. caption))
  3068. width height frame-params)))))
  3069. (defvar org-e-odt-standalone-image-predicate
  3070. (function (lambda (paragraph)
  3071. (or (org-element-property :caption paragraph)
  3072. (org-element-property :name paragraph)))))
  3073. (defun org-e-odt-standalone-image-p (element info &optional predicate)
  3074. "Test if ELEMENT is a standalone image for the purpose ODT export.
  3075. INFO is a plist holding contextual information.
  3076. Return non-nil, if ELEMENT is of type paragraph and it's sole
  3077. content, save for whitespaces, is a link that qualifies as an
  3078. inline image.
  3079. Return non-nil, if ELEMENT is of type link and it's containing
  3080. paragraph has no other content save for leading and trailing
  3081. whitespaces.
  3082. Return nil, otherwise.
  3083. Bind `org-e-odt-standalone-image-predicate' to constrain
  3084. paragraph further. For example, to check for only captioned
  3085. standalone images, do the following.
  3086. \(setq org-e-odt-standalone-image-predicate
  3087. \(lambda \(paragraph\)
  3088. \(org-element-property :caption paragraph\)\)\)
  3089. "
  3090. (let ((paragraph (case (org-element-type element)
  3091. (paragraph element)
  3092. (link (and (org-export-inline-image-p
  3093. element org-e-odt-inline-image-rules)
  3094. (org-export-get-parent element info)))
  3095. (t nil))))
  3096. (when paragraph
  3097. (assert (eq (org-element-type paragraph) 'paragraph))
  3098. (when (or (not (and (boundp 'org-e-odt-standalone-image-predicate)
  3099. (functionp org-e-odt-standalone-image-predicate)))
  3100. (funcall org-e-odt-standalone-image-predicate paragraph))
  3101. (let ((contents (org-element-contents paragraph)))
  3102. (loop for x in contents
  3103. with inline-image-count = 0
  3104. always (cond
  3105. ((eq (org-element-type x) 'plain-text)
  3106. (not (org-string-nw-p x)))
  3107. ((eq (org-element-type x) 'link)
  3108. (when (org-export-inline-image-p
  3109. x org-e-odt-inline-image-rules)
  3110. (= (incf inline-image-count) 1)))
  3111. (t nil))))))))
  3112. (defun org-e-odt-link (link desc info)
  3113. "Transcode a LINK object from Org to HTML.
  3114. DESC is the description part of the link, or the empty string.
  3115. INFO is a plist holding contextual information. See
  3116. `org-export-data'."
  3117. (let* ((type (org-element-property :type link))
  3118. (raw-path (org-element-property :path link))
  3119. ;; Ensure DESC really exists, or set it to nil.
  3120. (desc (and (not (string= desc "")) desc))
  3121. (imagep (org-export-inline-image-p
  3122. link org-e-odt-inline-image-rules))
  3123. (path (cond
  3124. ((member type '("http" "https" "ftp" "mailto"))
  3125. (concat type ":" raw-path))
  3126. ((string= type "file")
  3127. (when (string-match "\\(.+\\)::.+" raw-path)
  3128. (setq raw-path (match-string 1 raw-path)))
  3129. (if (file-name-absolute-p raw-path)
  3130. (concat "file://" (expand-file-name raw-path))
  3131. (concat "file://" raw-path)))
  3132. (t raw-path)))
  3133. protocol)
  3134. (cond
  3135. ;; Image file.
  3136. ((and (not desc) (org-export-inline-image-p
  3137. link org-e-odt-inline-image-rules))
  3138. (org-e-odt-link--inline-image link info))
  3139. ;; Radio target: Transcode target's contents and use them as
  3140. ;; link's description.
  3141. ((string= type "radio")
  3142. (let ((destination (org-export-resolve-radio-link link info)))
  3143. (when destination
  3144. (org-e-odt-format-internal-link
  3145. (org-export-data (org-element-contents destination) info)
  3146. (org-export-solidify-link-text path)))))
  3147. ;; Links pointing to an headline: Find destination and build
  3148. ;; appropriate referencing command.
  3149. ((member type '("custom-id" "fuzzy" "id"))
  3150. (let ((destination (if (string= type "fuzzy")
  3151. (org-export-resolve-fuzzy-link link info)
  3152. (org-export-resolve-id-link link info))))
  3153. (case (org-element-type destination)
  3154. ;; Fuzzy link points nowhere.
  3155. ('nil
  3156. (org-e-odt-format-fontify
  3157. (or desc (org-export-data
  3158. (org-element-property :raw-link link) info))
  3159. 'italic))
  3160. ;; Fuzzy link points to an invisible target.
  3161. (keyword nil)
  3162. ;; LINK points to an headline. If headlines are numbered
  3163. ;; and the link has no description, display headline's
  3164. ;; number. Otherwise, display description or headline's
  3165. ;; title.
  3166. (headline
  3167. (let* ((headline-no (org-export-get-headline-number destination info))
  3168. (label (format "sec-%s" (mapconcat 'number-to-string
  3169. headline-no "-")))
  3170. (section-no (mapconcat 'number-to-string headline-no ".")))
  3171. (setq desc
  3172. (cond
  3173. (desc desc)
  3174. ((plist-get info :section-numbers) section-no)
  3175. (t (org-export-data
  3176. (org-element-property :title destination) info))))
  3177. (org-e-odt-format-internal-link desc label)))
  3178. ;; Fuzzy link points to a target. Do as above.
  3179. (otherwise
  3180. ;; (unless desc
  3181. ;; (setq number (cond
  3182. ;; ((org-e-odt-standalone-image-p destination info)
  3183. ;; (org-export-get-ordinal
  3184. ;; (assoc 'link (org-element-contents destination))
  3185. ;; info 'link 'org-e-odt-standalone-image-p))
  3186. ;; (t (org-export-get-ordinal destination info))))
  3187. ;; (setq desc (when number
  3188. ;; (if (atom number) (number-to-string number)
  3189. ;; (mapconcat 'number-to-string number ".")))))
  3190. (let ((label-reference
  3191. (org-e-odt-format-label destination info 'reference)))
  3192. (assert label-reference)
  3193. label-reference)))))
  3194. ;; Coderef: replace link with the reference name or the
  3195. ;; equivalent line number.
  3196. ((string= type "coderef")
  3197. (let* ((fmt (org-export-get-coderef-format path desc))
  3198. (res (org-export-resolve-coderef path info))
  3199. (org-e-odt-suppress-xref nil)
  3200. (href (org-xml-format-href (concat "#coderef-" path))))
  3201. (format fmt (org-e-odt-format-link res href))))
  3202. ;; Link type is handled by a special function.
  3203. ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
  3204. (funcall protocol (org-link-unescape path) desc 'html))
  3205. ;; External link with a description part.
  3206. ((and path desc) (org-e-odt-format-link desc path))
  3207. ;; External link without a description part.
  3208. (path (org-e-odt-format-link path path))
  3209. ;; No path, only description. Try to do something useful.
  3210. (t (org-e-odt-format-fontify desc 'italic)))))
  3211. ;;;; Babel Call
  3212. ;; Babel Calls are ignored.
  3213. ;;;; Macro
  3214. (defun org-e-odt-macro (macro contents info)
  3215. "Transcode a MACRO element from Org to HTML.
  3216. CONTENTS is nil. INFO is a plist holding contextual information."
  3217. ;; Use available tools.
  3218. (org-export-expand-macro macro info))
  3219. ;;;; Paragraph
  3220. (defun org-e-odt-paragraph (paragraph contents info)
  3221. "Transcode a PARAGRAPH element from Org to HTML.
  3222. CONTENTS is the contents of the paragraph, as a string. INFO is
  3223. the plist used as a communication channel."
  3224. (let* ((style nil) ; FIXME
  3225. (class (cdr (assoc style '((footnote . "footnote")
  3226. (verse . nil)))))
  3227. (extra (if class (format " class=\"%s\"" class) ""))
  3228. (parent (org-export-get-parent paragraph info))
  3229. (parent-type (org-element-type parent))
  3230. (style (case parent-type
  3231. (quote-block 'quote)
  3232. (center-block 'center)
  3233. (footnote-definition 'footnote)
  3234. (t nil))))
  3235. (org-e-odt-format-stylized-paragraph style contents)))
  3236. ;;;; Plain List
  3237. (defun org-e-odt-plain-list (plain-list contents info)
  3238. "Transcode a PLAIN-LIST element from Org to HTML.
  3239. CONTENTS is the contents of the list. INFO is a plist holding
  3240. contextual information."
  3241. (let* (arg1 ;; FIXME
  3242. (type (org-element-property :type plain-list))
  3243. (attr (mapconcat #'identity
  3244. (org-element-property :attr_odt plain-list)
  3245. " ")))
  3246. (org-e-odt--wrap-label
  3247. plain-list (format "%s\n%s%s"
  3248. (org-e-odt-begin-plain-list type)
  3249. contents (org-e-odt-end-plain-list type)))))
  3250. ;;;; Plain Text
  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. (mapc
  3286. (lambda (pair)
  3287. (setq text (replace-regexp-in-string (car pair) (cdr pair) text t nil)))
  3288. org-e-odt-special-string-regexps))
  3289. ;; Handle break preservation if required.
  3290. (when (plist-get info :preserve-breaks)
  3291. (setq text (replace-regexp-in-string
  3292. "\\(\\\\\\\\\\)?[ \t]*\n" "<text:line-break/>\n" text t)))
  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 the contents of the table. INFO is a plist holding
  3566. contextual information."
  3567. (case (org-element-property :type table)
  3568. (table.el nil)
  3569. (t
  3570. (let* ((captions (org-e-odt-format-label table info 'definition))
  3571. (caption (car captions)) (short-caption (cdr captions))
  3572. (attributes (org-e-odt-element-attributes table info))
  3573. (custom-table-style (nth 1 (org-e-odt-table-style-spec table info)))
  3574. (table-column-specs
  3575. (function
  3576. (lambda (table info)
  3577. (let* ((table-style (or custom-table-style "OrgTable"))
  3578. (column-style (format "%sColumn" table-style)))
  3579. (mapconcat
  3580. (lambda (table-cell)
  3581. (let ((width (1+ (or (org-export-table-cell-width
  3582. table-cell info) 0))))
  3583. (org-e-odt-make-string
  3584. width
  3585. (org-e-odt-format-tags
  3586. "<table:table-column table:style-name=\"%s\"/>"
  3587. "" column-style))))
  3588. (org-e-odt-table-first-row-data-cells table info) "\n"))))))
  3589. (concat
  3590. ;; caption.
  3591. (when caption (org-e-odt-format-stylized-paragraph 'table caption))
  3592. ;; begin table.
  3593. (let* ((automatic-name
  3594. (org-e-odt-add-automatic-style "Table" attributes)))
  3595. (format
  3596. "\n<table:table table:name=\"%s\" table:style-name=\"%s\">\n"
  3597. (or short-caption (car automatic-name))
  3598. (or custom-table-style (cdr automatic-name) "OrgTable")))
  3599. ;; column specification.
  3600. (funcall table-column-specs table info)
  3601. ;; actual contents.
  3602. "\n" contents
  3603. ;; end table.
  3604. "</table:table>")))))
  3605. ;;;; Target
  3606. (defun org-e-odt-target (target contents info)
  3607. "Transcode a TARGET object from Org to HTML.
  3608. CONTENTS is nil. INFO is a plist holding contextual
  3609. information."
  3610. (org-e-odt-format-anchor
  3611. "" (org-export-solidify-link-text (org-element-property :value target))))
  3612. ;;;; Timestamp
  3613. (defun org-e-odt-timestamp (timestamp contents info)
  3614. "Transcode a TIMESTAMP object from Org to HTML.
  3615. CONTENTS is nil. INFO is a plist used as a communication
  3616. channel."
  3617. (org-e-odt-format-fontify
  3618. (org-e-odt-format-fontify
  3619. (org-translate-time (org-element-property :value timestamp))
  3620. "timestamp")
  3621. "timestamp-wrapper"))
  3622. ;;;; Underline
  3623. (defun org-e-odt-underline (underline contents info)
  3624. "Transcode UNDERLINE from Org to HTML.
  3625. CONTENTS is the text with underline markup. INFO is a plist
  3626. holding contextual information."
  3627. (org-e-odt-format-fontify contents 'underline))
  3628. ;;;; Verbatim
  3629. (defun org-e-odt-verbatim (verbatim contents info)
  3630. "Transcode a VERBATIM object from Org to HTML.
  3631. CONTENTS is nil. INFO is a plist used as a communication
  3632. channel."
  3633. (org-e-odt-format-fontify (org-element-property :value verbatim) 'verbatim))
  3634. ;;;; Verse Block
  3635. (defun org-e-odt-verse-block (verse-block contents info)
  3636. "Transcode a VERSE-BLOCK element from Org to HTML.
  3637. CONTENTS is verse block contents. INFO is a plist holding
  3638. contextual information."
  3639. ;; Replace each newline character with line break. Also replace
  3640. ;; each blank line with a line break.
  3641. (setq contents (replace-regexp-in-string
  3642. "^ *\\\\\\\\$" "<br/>\n"
  3643. (replace-regexp-in-string
  3644. "\\(\\\\\\\\\\)?[ \t]*\n" " <br/>\n" contents)))
  3645. ;; Replace each white space at beginning of a line with a
  3646. ;; non-breaking space.
  3647. (while (string-match "^[ \t]+" contents)
  3648. (let ((new-str (org-e-odt-format-spaces
  3649. (length (match-string 0 contents)))))
  3650. (setq contents (replace-match new-str nil t contents))))
  3651. (org-e-odt--wrap-label
  3652. verse-block (format "<p class=\"verse\">\n%s</p>" contents)))
  3653. ;;; Filter Functions
  3654. ;;;; Filter Settings
  3655. ;;;; Filters
  3656. ;;; Interactive functions
  3657. (defun org-e-odt-export-to-odt
  3658. (&optional subtreep visible-only body-only ext-plist pub-dir)
  3659. "Export current buffer to a HTML file.
  3660. If narrowing is active in the current buffer, only export its
  3661. narrowed part.
  3662. If a region is active, export that region.
  3663. When optional argument SUBTREEP is non-nil, export the sub-tree
  3664. at point, extracting information from the headline properties
  3665. first.
  3666. When optional argument VISIBLE-ONLY is non-nil, don't export
  3667. contents of hidden elements.
  3668. When optional argument BODY-ONLY is non-nil, only write code
  3669. between \"\\begin{document}\" and \"\\end{document}\".
  3670. EXT-PLIST, when provided, is a property list with external
  3671. parameters overriding Org default settings, but still inferior to
  3672. file-local settings.
  3673. When optional argument PUB-DIR is set, use it as the publishing
  3674. directory.
  3675. Return output file's name."
  3676. (interactive)
  3677. (setq debug-on-error t)
  3678. ;; (let* ((outfile (org-export-output-file-name ".html" subtreep pub-dir))
  3679. ;; (outfile "content.xml"))
  3680. ;; (org-export-to-file
  3681. ;; 'e-odt outfile subtreep visible-only body-only ext-plist))
  3682. (let* ((outbuf (org-e-odt-init-outfile))
  3683. (target (org-export-output-file-name ".odt" subtreep pub-dir))
  3684. (outdir (file-name-directory (buffer-file-name outbuf)))
  3685. (default-directory outdir))
  3686. ;; FIXME: for copying embedded images
  3687. (setq org-current-export-file
  3688. (file-name-directory
  3689. (org-export-output-file-name ".odt" subtreep nil)))
  3690. (org-export-to-buffer
  3691. 'e-odt outbuf
  3692. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))
  3693. (setq org-lparse-opt-plist nil) ; FIXME
  3694. (org-e-odt-save-as-outfile target ;; info
  3695. nil
  3696. )
  3697. ;; return outfile
  3698. (if (not org-e-odt-preferred-output-format) target
  3699. (or (org-e-odt-convert target org-e-odt-preferred-output-format)
  3700. target))))
  3701. (defun org-e-odt-reachable-p (in-fmt out-fmt)
  3702. "Return non-nil if IN-FMT can be converted to OUT-FMT."
  3703. (catch 'done
  3704. (let ((reachable-formats (org-e-odt-do-reachable-formats in-fmt)))
  3705. (dolist (e reachable-formats)
  3706. (let ((out-fmt-spec (assoc out-fmt (cdr e))))
  3707. (when out-fmt-spec
  3708. (throw 'done (cons (car e) out-fmt-spec))))))))
  3709. (defun org-e-odt-do-convert (in-file out-fmt &optional prefix-arg)
  3710. "Workhorse routine for `org-e-odt-convert'."
  3711. (require 'browse-url)
  3712. (let* ((in-file (expand-file-name (or in-file buffer-file-name)))
  3713. (dummy (or (file-readable-p in-file)
  3714. (error "Cannot read %s" in-file)))
  3715. (in-fmt (file-name-extension in-file))
  3716. (out-fmt (or out-fmt (error "Output format unspecified")))
  3717. (how (or (org-e-odt-reachable-p in-fmt out-fmt)
  3718. (error "Cannot convert from %s format to %s format?"
  3719. in-fmt out-fmt)))
  3720. (convert-process (car how))
  3721. (out-file (concat (file-name-sans-extension in-file) "."
  3722. (nth 1 (or (cdr how) out-fmt))))
  3723. (extra-options (or (nth 2 (cdr how)) ""))
  3724. (out-dir (file-name-directory in-file))
  3725. (cmd (format-spec convert-process
  3726. `((?i . ,(shell-quote-argument in-file))
  3727. (?I . ,(browse-url-file-url in-file))
  3728. (?f . ,out-fmt)
  3729. (?o . ,out-file)
  3730. (?O . ,(browse-url-file-url out-file))
  3731. (?d . , (shell-quote-argument out-dir))
  3732. (?D . ,(browse-url-file-url out-dir))
  3733. (?x . ,extra-options)))))
  3734. (when (file-exists-p out-file)
  3735. (delete-file out-file))
  3736. (message "Executing %s" cmd)
  3737. (let ((cmd-output (shell-command-to-string cmd)))
  3738. (message "%s" cmd-output))
  3739. (cond
  3740. ((file-exists-p out-file)
  3741. (message "Exported to %s" out-file)
  3742. (when prefix-arg
  3743. (message "Opening %s..." out-file)
  3744. (org-open-file out-file))
  3745. out-file)
  3746. (t
  3747. (message "Export to %s failed" out-file)
  3748. nil))))
  3749. (defun org-e-odt-do-reachable-formats (in-fmt)
  3750. "Return verbose info about formats to which IN-FMT can be converted.
  3751. Return a list where each element is of the
  3752. form (CONVERTER-PROCESS . OUTPUT-FMT-ALIST). See
  3753. `org-e-odt-convert-processes' for CONVERTER-PROCESS and see
  3754. `org-e-odt-convert-capabilities' for OUTPUT-FMT-ALIST."
  3755. (let* ((converter
  3756. (and org-e-odt-convert-process
  3757. (cadr (assoc-string org-e-odt-convert-process
  3758. org-e-odt-convert-processes t))))
  3759. (capabilities
  3760. (and org-e-odt-convert-process
  3761. (cadr (assoc-string org-e-odt-convert-process
  3762. org-e-odt-convert-processes t))
  3763. org-e-odt-convert-capabilities))
  3764. reachable-formats)
  3765. (when converter
  3766. (dolist (c capabilities)
  3767. (when (member in-fmt (nth 1 c))
  3768. (push (cons converter (nth 2 c)) reachable-formats))))
  3769. reachable-formats))
  3770. (defun org-e-odt-reachable-formats (in-fmt)
  3771. "Return list of formats to which IN-FMT can be converted.
  3772. The list of the form (OUTPUT-FMT-1 OUTPUT-FMT-2 ...)."
  3773. (let (l)
  3774. (mapc (lambda (e) (add-to-list 'l e))
  3775. (apply 'append (mapcar
  3776. (lambda (e) (mapcar 'car (cdr e)))
  3777. (org-e-odt-do-reachable-formats in-fmt))))
  3778. l))
  3779. (defun org-e-odt-convert-read-params ()
  3780. "Return IN-FILE and OUT-FMT params for `org-e-odt-do-convert'.
  3781. This is a helper routine for interactive use."
  3782. (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
  3783. (in-file (read-file-name "File to be converted: "
  3784. nil buffer-file-name t))
  3785. (in-fmt (file-name-extension in-file))
  3786. (out-fmt-choices (org-e-odt-reachable-formats in-fmt))
  3787. (out-fmt
  3788. (or (and out-fmt-choices
  3789. (funcall input "Output format: "
  3790. out-fmt-choices nil nil nil))
  3791. (error
  3792. "No known converter or no known output formats for %s files"
  3793. in-fmt))))
  3794. (list in-file out-fmt)))
  3795. ;;;###autoload
  3796. (defun org-e-odt-convert (&optional in-file out-fmt prefix-arg)
  3797. "Convert IN-FILE to format OUT-FMT using a command line converter.
  3798. IN-FILE is the file to be converted. If unspecified, it defaults
  3799. to variable `buffer-file-name'. OUT-FMT is the desired output
  3800. format. Use `org-e-odt-convert-process' as the converter.
  3801. If PREFIX-ARG is non-nil then the newly converted file is opened
  3802. using `org-open-file'."
  3803. (interactive
  3804. (append (org-e-odt-convert-read-params) current-prefix-arg))
  3805. (org-e-odt-do-convert in-file out-fmt prefix-arg))
  3806. ;;; FIXMES, TODOS, FOR REVIEW etc
  3807. ;; (defun org-e-odt-discontinue-list ()
  3808. ;; (let ((stashed-stack org-lparse-list-stack))
  3809. ;; (loop for list-type in stashed-stack
  3810. ;; do (org-lparse-end-list-item-1 list-type)
  3811. ;; (org-lparse-end-list list-type))
  3812. ;; (setq org-e-odt-list-stack-stashed stashed-stack)))
  3813. ;; (defun org-e-odt-continue-list ()
  3814. ;; (setq org-e-odt-list-stack-stashed (nreverse org-e-odt-list-stack-stashed))
  3815. ;; (loop for list-type in org-e-odt-list-stack-stashed
  3816. ;; do (org-lparse-begin-list list-type)
  3817. ;; (org-lparse-begin-list-item list-type)))
  3818. ;; FIXME: Begin indented table
  3819. ;; (setq org-e-odt-table-indentedp (not (null org-lparse-list-stack)))
  3820. ;; (setq org-e-odt-table-indentedp nil) ; FIXME
  3821. ;; (when org-e-odt-table-indentedp
  3822. ;; ;; Within the Org file, the table is appearing within a list item.
  3823. ;; ;; OpenDocument doesn't allow table to appear within list items.
  3824. ;; ;; Temporarily terminate the list, emit the table and then
  3825. ;; ;; re-continue the list.
  3826. ;; (org-e-odt-discontinue-list)
  3827. ;; ;; Put the Table in an indented section.
  3828. ;; (let ((level (length org-e-odt-list-stack-stashed)))
  3829. ;; (org-e-odt-begin-section (format "OrgIndentedSection-Level-%d" level))))
  3830. ;; FIXME: End indented table
  3831. ;; (when org-e-odt-table-indentedp
  3832. ;; (org-e-odt-end-section)
  3833. ;; (org-e-odt-continue-list))
  3834. ;;;; org-format-table-html
  3835. ;;;; org-format-org-table-html
  3836. ;;;; org-format-table-table-html
  3837. ;;;; org-table-number-fraction
  3838. ;;;; org-table-number-regexp
  3839. ;;;; org-e-odt-table-caption-above
  3840. ;;;; org-whitespace
  3841. ;;;; "<span style=\"visibility:hidden;\">%s</span>"
  3842. ;;;; Remove display properties
  3843. ;;;; org-e-odt-with-timestamp
  3844. ;;;; org-e-odt-html-helper-timestamp
  3845. ;;;; org-export-as-html-and-open
  3846. ;;;; org-export-as-html-batch
  3847. ;;;; org-export-as-html-to-buffer
  3848. ;;;; org-replace-region-by-html
  3849. ;;;; org-export-region-as-html
  3850. ;;;; org-export-as-html
  3851. ;;;; (org-export-directory :html opt-plist)
  3852. ;;;; (plist-get opt-plist :html-extension)
  3853. ;;;; org-e-odt-toplevel-hlevel
  3854. ;;;; org-e-odt-inline-image-extensions
  3855. ;;;; org-e-odt-protect-char-alist
  3856. ;;;; org-e-odt-table-use-header-tags-for-first-column
  3857. ;;;; org-e-odt-todo-kwd-class-prefix
  3858. ;;;; org-e-odt-tag-class-prefix
  3859. ;;;; org-e-odt-footnote-separator
  3860. ;;; Library Initializations
  3861. (mapc
  3862. (lambda (desc)
  3863. ;; Let Org open all OpenDocument files using system-registered app
  3864. (add-to-list 'org-file-apps
  3865. (cons (concat "\\." (car desc) "\\'") 'system))
  3866. ;; Let Emacs open all OpenDocument files in archive mode
  3867. (add-to-list 'auto-mode-alist
  3868. (cons (concat "\\." (car desc) "\\'") 'archive-mode)))
  3869. org-e-odt-file-extensions)
  3870. (defvar org-e-odt-display-outline-level 2)
  3871. (defun org-e-odt-enumerate-element (element info &optional predicate n)
  3872. (let* ((numbered-parent-headline-at-<=-n
  3873. (function
  3874. (lambda (element n info)
  3875. (loop for x in (org-export-get-genealogy element info)
  3876. thereis (and (eq (org-element-type x) 'headline)
  3877. (<= (org-export-get-relative-level x info) n)
  3878. (org-export-numbered-headline-p x info)
  3879. x)))))
  3880. (enumerate
  3881. (function
  3882. (lambda (element scope info &optional predicate)
  3883. (let ((counter 0))
  3884. (org-element-map
  3885. (or scope (plist-get info :parse-tree))
  3886. (org-element-type element)
  3887. (lambda (el)
  3888. (and (or (not predicate) (funcall predicate el info))
  3889. (incf counter)
  3890. (equal element el)
  3891. counter))
  3892. info 'first-match)))))
  3893. (scope (funcall numbered-parent-headline-at-<=-n
  3894. element (or n org-e-odt-display-outline-level) info))
  3895. (ordinal (funcall enumerate element scope info predicate))
  3896. (tag
  3897. (concat
  3898. ;; section number
  3899. (and scope
  3900. (mapconcat 'number-to-string
  3901. (org-export-get-headline-number scope info) "."))
  3902. ;; separator
  3903. (and scope ".")
  3904. ;; ordinal
  3905. (number-to-string ordinal))))
  3906. ;; (message "%s:\t%s" (org-element-property :name element) tag)
  3907. tag))
  3908. (provide 'org-e-odt)
  3909. ;;; org-e-odt.el ends here