org-e-odt.el 161 KB

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