ox.el 239 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218
  1. ;;; ox.el --- Generic Export Engine for Org Mode
  2. ;; Copyright (C) 2012-2014 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;; This library implements a generic export engine for Org, built on
  19. ;; its syntactical parser: Org Elements.
  20. ;;
  21. ;; Besides that parser, the generic exporter is made of three distinct
  22. ;; parts:
  23. ;;
  24. ;; - The communication channel consists in a property list, which is
  25. ;; created and updated during the process. Its use is to offer
  26. ;; every piece of information, would it be about initial environment
  27. ;; or contextual data, all in a single place. The exhaustive list
  28. ;; of properties is given in "The Communication Channel" section of
  29. ;; this file.
  30. ;;
  31. ;; - The transcoder walks the parse tree, ignores or treat as plain
  32. ;; text elements and objects according to export options, and
  33. ;; eventually calls back-end specific functions to do the real
  34. ;; transcoding, concatenating their return value along the way.
  35. ;;
  36. ;; - The filter system is activated at the very beginning and the very
  37. ;; end of the export process, and each time an element or an object
  38. ;; has been converted. It is the entry point to fine-tune standard
  39. ;; output from back-end transcoders. See "The Filter System"
  40. ;; section for more information.
  41. ;;
  42. ;; The core function is `org-export-as'. It returns the transcoded
  43. ;; buffer as a string.
  44. ;;
  45. ;; An export back-end is defined with `org-export-define-backend'.
  46. ;; This function can also support specific buffer keywords, OPTION
  47. ;; keyword's items and filters. Refer to function's documentation for
  48. ;; more information.
  49. ;;
  50. ;; If the new back-end shares most properties with another one,
  51. ;; `org-export-define-derived-backend' can be used to simplify the
  52. ;; process.
  53. ;;
  54. ;; Any back-end can define its own variables. Among them, those
  55. ;; customizable should belong to the `org-export-BACKEND' group.
  56. ;;
  57. ;; Tools for common tasks across back-ends are implemented in the
  58. ;; following part of the file.
  59. ;;
  60. ;; Then, a wrapper macro for asynchronous export,
  61. ;; `org-export-async-start', along with tools to display results. are
  62. ;; given in the penultimate part.
  63. ;;
  64. ;; Eventually, a dispatcher (`org-export-dispatch') for standard
  65. ;; back-ends is provided in the last one.
  66. ;;; Code:
  67. (eval-when-compile (require 'cl))
  68. (require 'org-element)
  69. (require 'org-macro)
  70. (require 'ob-exp)
  71. (declare-function org-publish "ox-publish" (project &optional force async))
  72. (declare-function org-publish-all "ox-publish" (&optional force async))
  73. (declare-function
  74. org-publish-current-file "ox-publish" (&optional force async))
  75. (declare-function org-publish-current-project "ox-publish"
  76. (&optional force async))
  77. (defvar org-publish-project-alist)
  78. (defvar org-table-number-fraction)
  79. (defvar org-table-number-regexp)
  80. ;;; Internal Variables
  81. ;;
  82. ;; Among internal variables, the most important is
  83. ;; `org-export-options-alist'. This variable define the global export
  84. ;; options, shared between every exporter, and how they are acquired.
  85. (defconst org-export-max-depth 19
  86. "Maximum nesting depth for headlines, counting from 0.")
  87. (defconst org-export-options-alist
  88. '((:author "AUTHOR" nil user-full-name t)
  89. (:creator "CREATOR" nil org-export-creator-string)
  90. (:date "DATE" nil nil t)
  91. (:description "DESCRIPTION" nil nil newline)
  92. (:email "EMAIL" nil user-mail-address t)
  93. (:exclude-tags "EXCLUDE_TAGS" nil org-export-exclude-tags split)
  94. (:headline-levels nil "H" org-export-headline-levels)
  95. (:keywords "KEYWORDS" nil nil space)
  96. (:language "LANGUAGE" nil org-export-default-language t)
  97. (:preserve-breaks nil "\\n" org-export-preserve-breaks)
  98. (:section-numbers nil "num" org-export-with-section-numbers)
  99. (:select-tags "SELECT_TAGS" nil org-export-select-tags split)
  100. (:time-stamp-file nil "timestamp" org-export-time-stamp-file)
  101. (:title "TITLE" nil nil space)
  102. (:with-archived-trees nil "arch" org-export-with-archived-trees)
  103. (:with-author nil "author" org-export-with-author)
  104. (:with-clocks nil "c" org-export-with-clocks)
  105. (:with-creator nil "creator" org-export-with-creator)
  106. (:with-date nil "date" org-export-with-date)
  107. (:with-drawers nil "d" org-export-with-drawers)
  108. (:with-email nil "email" org-export-with-email)
  109. (:with-emphasize nil "*" org-export-with-emphasize)
  110. (:with-entities nil "e" org-export-with-entities)
  111. (:with-fixed-width nil ":" org-export-with-fixed-width)
  112. (:with-footnotes nil "f" org-export-with-footnotes)
  113. (:with-inlinetasks nil "inline" org-export-with-inlinetasks)
  114. (:with-latex nil "tex" org-export-with-latex)
  115. (:with-planning nil "p" org-export-with-planning)
  116. (:with-priority nil "pri" org-export-with-priority)
  117. (:with-smart-quotes nil "'" org-export-with-smart-quotes)
  118. (:with-special-strings nil "-" org-export-with-special-strings)
  119. (:with-statistics-cookies nil "stat" org-export-with-statistics-cookies)
  120. (:with-sub-superscript nil "^" org-export-with-sub-superscripts)
  121. (:with-toc nil "toc" org-export-with-toc)
  122. (:with-tables nil "|" org-export-with-tables)
  123. (:with-tags nil "tags" org-export-with-tags)
  124. (:with-tasks nil "tasks" org-export-with-tasks)
  125. (:with-timestamps nil "<" org-export-with-timestamps)
  126. (:with-todo-keywords nil "todo" org-export-with-todo-keywords))
  127. "Alist between export properties and ways to set them.
  128. The CAR of the alist is the property name, and the CDR is a list
  129. like (KEYWORD OPTION DEFAULT BEHAVIOR) where:
  130. KEYWORD is a string representing a buffer keyword, or nil. Each
  131. property defined this way can also be set, during subtree
  132. export, through a headline property named after the keyword
  133. with the \"EXPORT_\" prefix (i.e. DATE keyword and EXPORT_DATE
  134. property).
  135. OPTION is a string that could be found in an #+OPTIONS: line.
  136. DEFAULT is the default value for the property.
  137. BEHAVIOR determines how Org should handle multiple keywords for
  138. the same property. It is a symbol among:
  139. nil Keep old value and discard the new one.
  140. t Replace old value with the new one.
  141. `space' Concatenate the values, separating them with a space.
  142. `newline' Concatenate the values, separating them with
  143. a newline.
  144. `split' Split values at white spaces, and cons them to the
  145. previous list.
  146. Values set through KEYWORD and OPTION have precedence over
  147. DEFAULT.
  148. All these properties should be back-end agnostic. Back-end
  149. specific properties are set through `org-export-define-backend'.
  150. Properties redefined there have precedence over these.")
  151. (defconst org-export-special-keywords '("FILETAGS" "SETUPFILE" "OPTIONS")
  152. "List of in-buffer keywords that require special treatment.
  153. These keywords are not directly associated to a property. The
  154. way they are handled must be hard-coded into
  155. `org-export--get-inbuffer-options' function.")
  156. (defconst org-export-filters-alist
  157. '((:filter-bold . org-export-filter-bold-functions)
  158. (:filter-babel-call . org-export-filter-babel-call-functions)
  159. (:filter-center-block . org-export-filter-center-block-functions)
  160. (:filter-clock . org-export-filter-clock-functions)
  161. (:filter-code . org-export-filter-code-functions)
  162. (:filter-comment . org-export-filter-comment-functions)
  163. (:filter-comment-block . org-export-filter-comment-block-functions)
  164. (:filter-diary-sexp . org-export-filter-diary-sexp-functions)
  165. (:filter-drawer . org-export-filter-drawer-functions)
  166. (:filter-dynamic-block . org-export-filter-dynamic-block-functions)
  167. (:filter-entity . org-export-filter-entity-functions)
  168. (:filter-example-block . org-export-filter-example-block-functions)
  169. (:filter-export-block . org-export-filter-export-block-functions)
  170. (:filter-export-snippet . org-export-filter-export-snippet-functions)
  171. (:filter-final-output . org-export-filter-final-output-functions)
  172. (:filter-fixed-width . org-export-filter-fixed-width-functions)
  173. (:filter-footnote-definition . org-export-filter-footnote-definition-functions)
  174. (:filter-footnote-reference . org-export-filter-footnote-reference-functions)
  175. (:filter-headline . org-export-filter-headline-functions)
  176. (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions)
  177. (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions)
  178. (:filter-inline-src-block . org-export-filter-inline-src-block-functions)
  179. (:filter-inlinetask . org-export-filter-inlinetask-functions)
  180. (:filter-italic . org-export-filter-italic-functions)
  181. (:filter-item . org-export-filter-item-functions)
  182. (:filter-keyword . org-export-filter-keyword-functions)
  183. (:filter-latex-environment . org-export-filter-latex-environment-functions)
  184. (:filter-latex-fragment . org-export-filter-latex-fragment-functions)
  185. (:filter-line-break . org-export-filter-line-break-functions)
  186. (:filter-link . org-export-filter-link-functions)
  187. (:filter-node-property . org-export-filter-node-property-functions)
  188. (:filter-options . org-export-filter-options-functions)
  189. (:filter-paragraph . org-export-filter-paragraph-functions)
  190. (:filter-parse-tree . org-export-filter-parse-tree-functions)
  191. (:filter-plain-list . org-export-filter-plain-list-functions)
  192. (:filter-plain-text . org-export-filter-plain-text-functions)
  193. (:filter-planning . org-export-filter-planning-functions)
  194. (:filter-property-drawer . org-export-filter-property-drawer-functions)
  195. (:filter-quote-block . org-export-filter-quote-block-functions)
  196. (:filter-quote-section . org-export-filter-quote-section-functions)
  197. (:filter-radio-target . org-export-filter-radio-target-functions)
  198. (:filter-section . org-export-filter-section-functions)
  199. (:filter-special-block . org-export-filter-special-block-functions)
  200. (:filter-src-block . org-export-filter-src-block-functions)
  201. (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions)
  202. (:filter-strike-through . org-export-filter-strike-through-functions)
  203. (:filter-subscript . org-export-filter-subscript-functions)
  204. (:filter-superscript . org-export-filter-superscript-functions)
  205. (:filter-table . org-export-filter-table-functions)
  206. (:filter-table-cell . org-export-filter-table-cell-functions)
  207. (:filter-table-row . org-export-filter-table-row-functions)
  208. (:filter-target . org-export-filter-target-functions)
  209. (:filter-timestamp . org-export-filter-timestamp-functions)
  210. (:filter-underline . org-export-filter-underline-functions)
  211. (:filter-verbatim . org-export-filter-verbatim-functions)
  212. (:filter-verse-block . org-export-filter-verse-block-functions))
  213. "Alist between filters properties and initial values.
  214. The key of each association is a property name accessible through
  215. the communication channel. Its value is a configurable global
  216. variable defining initial filters.
  217. This list is meant to install user specified filters. Back-end
  218. developers may install their own filters using
  219. `org-export-define-backend'. Filters defined there will always
  220. be prepended to the current list, so they always get applied
  221. first.")
  222. (defconst org-export-default-inline-image-rule
  223. `(("file" .
  224. ,(format "\\.%s\\'"
  225. (regexp-opt
  226. '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
  227. "xpm" "pbm" "pgm" "ppm") t))))
  228. "Default rule for link matching an inline image.
  229. This rule applies to links with no description. By default, it
  230. will be considered as an inline image if it targets a local file
  231. whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
  232. \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
  233. See `org-export-inline-image-p' for more information about
  234. rules.")
  235. (defvar org-export-async-debug nil
  236. "Non-nil means asynchronous export process should leave data behind.
  237. This data is found in the appropriate \"*Org Export Process*\"
  238. buffer, and in files prefixed with \"org-export-process\" and
  239. located in `temporary-file-directory'.
  240. When non-nil, it will also set `debug-on-error' to a non-nil
  241. value in the external process.")
  242. (defvar org-export-stack-contents nil
  243. "Record asynchronously generated export results and processes.
  244. This is an alist: its CAR is the source of the
  245. result (destination file or buffer for a finished process,
  246. original buffer for a running one) and its CDR is a list
  247. containing the back-end used, as a symbol, and either a process
  248. or the time at which it finished. It is used to build the menu
  249. from `org-export-stack'.")
  250. (defvar org-export--registered-backends nil
  251. "List of backends currently available in the exporter.
  252. This variable is set with `org-export-define-backend' and
  253. `org-export-define-derived-backend' functions.")
  254. (defvar org-export-dispatch-last-action nil
  255. "Last command called from the dispatcher.
  256. The value should be a list. Its CAR is the action, as a symbol,
  257. and its CDR is a list of export options.")
  258. (defvar org-export-dispatch-last-position (make-marker)
  259. "The position where the last export command was created using the dispatcher.
  260. This marker will be used with `C-u C-c C-e' to make sure export repetition
  261. uses the same subtree if the previous command was restricted to a subtree.")
  262. ;; For compatibility with Org < 8
  263. (defvar org-export-current-backend nil
  264. "Name, if any, of the back-end used during an export process.
  265. Its value is a symbol such as `html', `latex', `ascii', or nil if
  266. the back-end is anonymous (see `org-export-create-backend') or if
  267. there is no export process in progress.
  268. It can be used to teach Babel blocks how to act differently
  269. according to the back-end used.")
  270. ;;; User-configurable Variables
  271. ;;
  272. ;; Configuration for the masses.
  273. ;;
  274. ;; They should never be accessed directly, as their value is to be
  275. ;; stored in a property list (cf. `org-export-options-alist').
  276. ;; Back-ends will read their value from there instead.
  277. (defgroup org-export nil
  278. "Options for exporting Org mode files."
  279. :tag "Org Export"
  280. :group 'org)
  281. (defgroup org-export-general nil
  282. "General options for export engine."
  283. :tag "Org Export General"
  284. :group 'org-export)
  285. (defcustom org-export-with-archived-trees 'headline
  286. "Whether sub-trees with the ARCHIVE tag should be exported.
  287. This can have three different values:
  288. nil Do not export, pretend this tree is not present.
  289. t Do export the entire tree.
  290. `headline' Only export the headline, but skip the tree below it.
  291. This option can also be set with the OPTIONS keyword,
  292. e.g. \"arch:nil\"."
  293. :group 'org-export-general
  294. :type '(choice
  295. (const :tag "Not at all" nil)
  296. (const :tag "Headline only" headline)
  297. (const :tag "Entirely" t)))
  298. (defcustom org-export-with-author t
  299. "Non-nil means insert author name into the exported file.
  300. This option can also be set with the OPTIONS keyword,
  301. e.g. \"author:nil\"."
  302. :group 'org-export-general
  303. :type 'boolean)
  304. (defcustom org-export-with-clocks nil
  305. "Non-nil means export CLOCK keywords.
  306. This option can also be set with the OPTIONS keyword,
  307. e.g. \"c:t\"."
  308. :group 'org-export-general
  309. :type 'boolean)
  310. (defcustom org-export-with-creator 'comment
  311. "Non-nil means the postamble should contain a creator sentence.
  312. The sentence can be set in `org-export-creator-string' and
  313. defaults to \"Generated by Org mode XX in Emacs XXX.\".
  314. If the value is `comment' insert it as a comment."
  315. :group 'org-export-general
  316. :type '(choice
  317. (const :tag "No creator sentence" nil)
  318. (const :tag "Sentence as a comment" comment)
  319. (const :tag "Insert the sentence" t)))
  320. (defcustom org-export-with-date t
  321. "Non-nil means insert date in the exported document.
  322. This option can also be set with the OPTIONS keyword,
  323. e.g. \"date:nil\"."
  324. :group 'org-export-general
  325. :type 'boolean)
  326. (defcustom org-export-date-timestamp-format nil
  327. "Time-stamp format string to use for DATE keyword.
  328. The format string, when specified, only applies if date consists
  329. in a single time-stamp. Otherwise its value will be ignored.
  330. See `format-time-string' for details on how to build this
  331. string."
  332. :group 'org-export-general
  333. :type '(choice
  334. (string :tag "Time-stamp format string")
  335. (const :tag "No format string" nil)))
  336. (defcustom org-export-creator-string
  337. (format "Emacs %s (Org mode %s)"
  338. emacs-version
  339. (if (fboundp 'org-version) (org-version) "unknown version"))
  340. "Information about the creator of the document.
  341. This option can also be set on with the CREATOR keyword."
  342. :group 'org-export-general
  343. :type '(string :tag "Creator string"))
  344. (defcustom org-export-with-drawers '(not "LOGBOOK")
  345. "Non-nil means export contents of standard drawers.
  346. When t, all drawers are exported. This may also be a list of
  347. drawer names to export. If that list starts with `not', only
  348. drawers with such names will be ignored.
  349. This variable doesn't apply to properties drawers.
  350. This option can also be set with the OPTIONS keyword,
  351. e.g. \"d:nil\"."
  352. :group 'org-export-general
  353. :version "24.4"
  354. :package-version '(Org . "8.0")
  355. :type '(choice
  356. (const :tag "All drawers" t)
  357. (const :tag "None" nil)
  358. (repeat :tag "Selected drawers"
  359. (string :tag "Drawer name"))
  360. (list :tag "Ignored drawers"
  361. (const :format "" not)
  362. (repeat :tag "Specify names of drawers to ignore during export"
  363. :inline t
  364. (string :tag "Drawer name")))))
  365. (defcustom org-export-with-email nil
  366. "Non-nil means insert author email into the exported file.
  367. This option can also be set with the OPTIONS keyword,
  368. e.g. \"email:t\"."
  369. :group 'org-export-general
  370. :type 'boolean)
  371. (defcustom org-export-with-emphasize t
  372. "Non-nil means interpret *word*, /word/, _word_ and +word+.
  373. If the export target supports emphasizing text, the word will be
  374. typeset in bold, italic, with an underline or strike-through,
  375. respectively.
  376. This option can also be set with the OPTIONS keyword,
  377. e.g. \"*:nil\"."
  378. :group 'org-export-general
  379. :type 'boolean)
  380. (defcustom org-export-exclude-tags '("noexport")
  381. "Tags that exclude a tree from export.
  382. All trees carrying any of these tags will be excluded from
  383. export. This is without condition, so even subtrees inside that
  384. carry one of the `org-export-select-tags' will be removed.
  385. This option can also be set with the EXCLUDE_TAGS keyword."
  386. :group 'org-export-general
  387. :type '(repeat (string :tag "Tag")))
  388. (defcustom org-export-with-fixed-width t
  389. "Non-nil means lines starting with \":\" will be in fixed width font.
  390. This can be used to have pre-formatted text, fragments of code
  391. etc. For example:
  392. : ;; Some Lisp examples
  393. : (while (defc cnt)
  394. : (ding))
  395. will be looking just like this in also HTML. See also the QUOTE
  396. keyword. Not all export backends support this.
  397. This option can also be set with the OPTIONS keyword,
  398. e.g. \"::nil\"."
  399. :group 'org-export-general
  400. :type 'boolean)
  401. (defcustom org-export-with-footnotes t
  402. "Non-nil means Org footnotes should be exported.
  403. This option can also be set with the OPTIONS keyword,
  404. e.g. \"f:nil\"."
  405. :group 'org-export-general
  406. :type 'boolean)
  407. (defcustom org-export-with-latex t
  408. "Non-nil means process LaTeX environments and fragments.
  409. This option can also be set with the OPTIONS line,
  410. e.g. \"tex:verbatim\". Allowed values are:
  411. nil Ignore math snippets.
  412. `verbatim' Keep everything in verbatim.
  413. t Allow export of math snippets."
  414. :group 'org-export-general
  415. :version "24.4"
  416. :package-version '(Org . "8.0")
  417. :type '(choice
  418. (const :tag "Do not process math in any way" nil)
  419. (const :tag "Interpret math snippets" t)
  420. (const :tag "Leave math verbatim" verbatim)))
  421. (defcustom org-export-headline-levels 3
  422. "The last level which is still exported as a headline.
  423. Inferior levels will usually produce itemize or enumerate lists
  424. when exported, but back-end behaviour may differ.
  425. This option can also be set with the OPTIONS keyword,
  426. e.g. \"H:2\"."
  427. :group 'org-export-general
  428. :type 'integer)
  429. (defcustom org-export-default-language "en"
  430. "The default language for export and clocktable translations, as a string.
  431. This may have an association in
  432. `org-clock-clocktable-language-setup',
  433. `org-export-smart-quotes-alist' and `org-export-dictionary'.
  434. This option can also be set with the LANGUAGE keyword."
  435. :group 'org-export-general
  436. :type '(string :tag "Language"))
  437. (defcustom org-export-preserve-breaks nil
  438. "Non-nil means preserve all line breaks when exporting.
  439. This option can also be set with the OPTIONS keyword,
  440. e.g. \"\\n:t\"."
  441. :group 'org-export-general
  442. :type 'boolean)
  443. (defcustom org-export-with-entities t
  444. "Non-nil means interpret entities when exporting.
  445. For example, HTML export converts \\alpha to &alpha; and \\AA to
  446. &Aring;.
  447. For a list of supported names, see the constant `org-entities'
  448. and the user option `org-entities-user'.
  449. This option can also be set with the OPTIONS keyword,
  450. e.g. \"e:nil\"."
  451. :group 'org-export-general
  452. :type 'boolean)
  453. (defcustom org-export-with-inlinetasks t
  454. "Non-nil means inlinetasks should be exported.
  455. This option can also be set with the OPTIONS keyword,
  456. e.g. \"inline:nil\"."
  457. :group 'org-export-general
  458. :version "24.4"
  459. :package-version '(Org . "8.0")
  460. :type 'boolean)
  461. (defcustom org-export-with-planning nil
  462. "Non-nil means include planning info in export.
  463. Planning info is the line containing either SCHEDULED:,
  464. DEADLINE:, CLOSED: time-stamps, or a combination of them.
  465. This option can also be set with the OPTIONS keyword,
  466. e.g. \"p:t\"."
  467. :group 'org-export-general
  468. :version "24.4"
  469. :package-version '(Org . "8.0")
  470. :type 'boolean)
  471. (defcustom org-export-with-priority nil
  472. "Non-nil means include priority cookies in export.
  473. This option can also be set with the OPTIONS keyword,
  474. e.g. \"pri:t\"."
  475. :group 'org-export-general
  476. :type 'boolean)
  477. (defcustom org-export-with-section-numbers t
  478. "Non-nil means add section numbers to headlines when exporting.
  479. When set to an integer n, numbering will only happen for
  480. headlines whose relative level is higher or equal to n.
  481. This option can also be set with the OPTIONS keyword,
  482. e.g. \"num:t\"."
  483. :group 'org-export-general
  484. :type 'boolean)
  485. (defcustom org-export-select-tags '("export")
  486. "Tags that select a tree for export.
  487. If any such tag is found in a buffer, all trees that do not carry
  488. one of these tags will be ignored during export. Inside trees
  489. that are selected like this, you can still deselect a subtree by
  490. tagging it with one of the `org-export-exclude-tags'.
  491. This option can also be set with the SELECT_TAGS keyword."
  492. :group 'org-export-general
  493. :type '(repeat (string :tag "Tag")))
  494. (defcustom org-export-with-smart-quotes nil
  495. "Non-nil means activate smart quotes during export.
  496. This option can also be set with the OPTIONS keyword,
  497. e.g., \"':t\".
  498. When setting this to non-nil, you need to take care of
  499. using the correct Babel package when exporting to LaTeX.
  500. E.g., you can load Babel for french like this:
  501. #+LATEX_HEADER: \\usepackage[french]{babel}"
  502. :group 'org-export-general
  503. :version "24.4"
  504. :package-version '(Org . "8.0")
  505. :type 'boolean)
  506. (defcustom org-export-with-special-strings t
  507. "Non-nil means interpret \"\\-\", \"--\" and \"---\" for export.
  508. When this option is turned on, these strings will be exported as:
  509. Org HTML LaTeX UTF-8
  510. -----+----------+--------+-------
  511. \\- &shy; \\-
  512. -- &ndash; -- –
  513. --- &mdash; --- —
  514. ... &hellip; \\ldots …
  515. This option can also be set with the OPTIONS keyword,
  516. e.g. \"-:nil\"."
  517. :group 'org-export-general
  518. :type 'boolean)
  519. (defcustom org-export-with-statistics-cookies t
  520. "Non-nil means include statistics cookies in export.
  521. This option can also be set with the OPTIONS keyword,
  522. e.g. \"stat:nil\""
  523. :group 'org-export-general
  524. :version "24.4"
  525. :package-version '(Org . "8.0")
  526. :type 'boolean)
  527. (defcustom org-export-with-sub-superscripts t
  528. "Non-nil means interpret \"_\" and \"^\" for export.
  529. If you want to control how Org displays those characters, see
  530. `org-use-sub-superscripts'. `org-export-with-sub-superscripts'
  531. used to be an alias for `org-use-sub-superscripts' in Org <8.0,
  532. it is not anymore.
  533. When this option is turned on, you can use TeX-like syntax for
  534. sub- and superscripts and see them exported correctly.
  535. You can also set the option with #+OPTIONS: ^:t
  536. Several characters after \"_\" or \"^\" will be considered as a
  537. single item - so grouping with {} is normally not needed. For
  538. example, the following things will be parsed as single sub- or
  539. superscripts:
  540. 10^24 or 10^tau several digits will be considered 1 item.
  541. 10^-12 or 10^-tau a leading sign with digits or a word
  542. x^2-y^3 will be read as x^2 - y^3, because items are
  543. terminated by almost any nonword/nondigit char.
  544. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  545. Still, ambiguity is possible. So when in doubt, use {} to enclose
  546. the sub/superscript. If you set this variable to the symbol `{}',
  547. the braces are *required* in order to trigger interpretations as
  548. sub/superscript. This can be helpful in documents that need \"_\"
  549. frequently in plain text."
  550. :group 'org-export-general
  551. :version "24.4"
  552. :package-version '(Org . "8.0")
  553. :type '(choice
  554. (const :tag "Interpret them" t)
  555. (const :tag "Curly brackets only" {})
  556. (const :tag "Do not interpret them" nil)))
  557. (defcustom org-export-with-toc t
  558. "Non-nil means create a table of contents in exported files.
  559. The TOC contains headlines with levels up
  560. to`org-export-headline-levels'. When an integer, include levels
  561. up to N in the toc, this may then be different from
  562. `org-export-headline-levels', but it will not be allowed to be
  563. larger than the number of headline levels. When nil, no table of
  564. contents is made.
  565. This option can also be set with the OPTIONS keyword,
  566. e.g. \"toc:nil\" or \"toc:3\"."
  567. :group 'org-export-general
  568. :type '(choice
  569. (const :tag "No Table of Contents" nil)
  570. (const :tag "Full Table of Contents" t)
  571. (integer :tag "TOC to level")))
  572. (defcustom org-export-with-tables t
  573. "If non-nil, lines starting with \"|\" define a table.
  574. For example:
  575. | Name | Address | Birthday |
  576. |-------------+----------+-----------|
  577. | Arthur Dent | England | 29.2.2100 |
  578. This option can also be set with the OPTIONS keyword,
  579. e.g. \"|:nil\"."
  580. :group 'org-export-general
  581. :type 'boolean)
  582. (defcustom org-export-with-tags t
  583. "If nil, do not export tags, just remove them from headlines.
  584. If this is the symbol `not-in-toc', tags will be removed from
  585. table of contents entries, but still be shown in the headlines of
  586. the document.
  587. This option can also be set with the OPTIONS keyword,
  588. e.g. \"tags:nil\"."
  589. :group 'org-export-general
  590. :type '(choice
  591. (const :tag "Off" nil)
  592. (const :tag "Not in TOC" not-in-toc)
  593. (const :tag "On" t)))
  594. (defcustom org-export-with-tasks t
  595. "Non-nil means include TODO items for export.
  596. This may have the following values:
  597. t include tasks independent of state.
  598. `todo' include only tasks that are not yet done.
  599. `done' include only tasks that are already done.
  600. nil ignore all tasks.
  601. list of keywords include tasks with these keywords.
  602. This option can also be set with the OPTIONS keyword,
  603. e.g. \"tasks:nil\"."
  604. :group 'org-export-general
  605. :type '(choice
  606. (const :tag "All tasks" t)
  607. (const :tag "No tasks" nil)
  608. (const :tag "Not-done tasks" todo)
  609. (const :tag "Only done tasks" done)
  610. (repeat :tag "Specific TODO keywords"
  611. (string :tag "Keyword"))))
  612. (defcustom org-export-time-stamp-file t
  613. "Non-nil means insert a time stamp into the exported file.
  614. The time stamp shows when the file was created. This option can
  615. also be set with the OPTIONS keyword, e.g. \"timestamp:nil\"."
  616. :group 'org-export-general
  617. :type 'boolean)
  618. (defcustom org-export-with-timestamps t
  619. "Non nil means allow timestamps in export.
  620. It can be set to any of the following values:
  621. t export all timestamps.
  622. `active' export active timestamps only.
  623. `inactive' export inactive timestamps only.
  624. nil do not export timestamps
  625. This only applies to timestamps isolated in a paragraph
  626. containing only timestamps. Other timestamps are always
  627. exported.
  628. This option can also be set with the OPTIONS keyword, e.g.
  629. \"<:nil\"."
  630. :group 'org-export-general
  631. :type '(choice
  632. (const :tag "All timestamps" t)
  633. (const :tag "Only active timestamps" active)
  634. (const :tag "Only inactive timestamps" inactive)
  635. (const :tag "No timestamp" nil)))
  636. (defcustom org-export-with-todo-keywords t
  637. "Non-nil means include TODO keywords in export.
  638. When nil, remove all these keywords from the export. This option
  639. can also be set with the OPTIONS keyword, e.g. \"todo:nil\"."
  640. :group 'org-export-general
  641. :type 'boolean)
  642. (defcustom org-export-allow-bind-keywords nil
  643. "Non-nil means BIND keywords can define local variable values.
  644. This is a potential security risk, which is why the default value
  645. is nil. You can also allow them through local buffer variables."
  646. :group 'org-export-general
  647. :version "24.4"
  648. :package-version '(Org . "8.0")
  649. :type 'boolean)
  650. (defcustom org-export-snippet-translation-alist nil
  651. "Alist between export snippets back-ends and exporter back-ends.
  652. This variable allows to provide shortcuts for export snippets.
  653. For example, with a value of '\(\(\"h\" . \"html\"\)\), the
  654. HTML back-end will recognize the contents of \"@@h:<b>@@\" as
  655. HTML code while every other back-end will ignore it."
  656. :group 'org-export-general
  657. :version "24.4"
  658. :package-version '(Org . "8.0")
  659. :type '(repeat
  660. (cons (string :tag "Shortcut")
  661. (string :tag "Back-end"))))
  662. (defcustom org-export-coding-system nil
  663. "Coding system for the exported file."
  664. :group 'org-export-general
  665. :version "24.4"
  666. :package-version '(Org . "8.0")
  667. :type 'coding-system)
  668. (defcustom org-export-copy-to-kill-ring 'if-interactive
  669. "Should we push exported content to the kill ring?"
  670. :group 'org-export-general
  671. :version "24.3"
  672. :type '(choice
  673. (const :tag "Always" t)
  674. (const :tag "When export is done interactively" if-interactive)
  675. (const :tag "Never" nil)))
  676. (defcustom org-export-initial-scope 'buffer
  677. "The initial scope when exporting with `org-export-dispatch'.
  678. This variable can be either set to `buffer' or `subtree'."
  679. :group 'org-export-general
  680. :type '(choice
  681. (const :tag "Export current buffer" buffer)
  682. (const :tag "Export current subtree" subtree)))
  683. (defcustom org-export-show-temporary-export-buffer t
  684. "Non-nil means show buffer after exporting to temp buffer.
  685. When Org exports to a file, the buffer visiting that file is ever
  686. shown, but remains buried. However, when exporting to
  687. a temporary buffer, that buffer is popped up in a second window.
  688. When this variable is nil, the buffer remains buried also in
  689. these cases."
  690. :group 'org-export-general
  691. :type 'boolean)
  692. (defcustom org-export-in-background nil
  693. "Non-nil means export and publishing commands will run in background.
  694. Results from an asynchronous export are never displayed
  695. automatically. But you can retrieve them with \\[org-export-stack]."
  696. :group 'org-export-general
  697. :version "24.4"
  698. :package-version '(Org . "8.0")
  699. :type 'boolean)
  700. (defcustom org-export-async-init-file user-init-file
  701. "File used to initialize external export process.
  702. Value must be an absolute file name. It defaults to user's
  703. initialization file. Though, a specific configuration makes the
  704. process faster and the export more portable."
  705. :group 'org-export-general
  706. :version "24.4"
  707. :package-version '(Org . "8.0")
  708. :type '(file :must-match t))
  709. (defcustom org-export-dispatch-use-expert-ui nil
  710. "Non-nil means using a non-intrusive `org-export-dispatch'.
  711. In that case, no help buffer is displayed. Though, an indicator
  712. for current export scope is added to the prompt (\"b\" when
  713. output is restricted to body only, \"s\" when it is restricted to
  714. the current subtree, \"v\" when only visible elements are
  715. considered for export, \"f\" when publishing functions should be
  716. passed the FORCE argument and \"a\" when the export should be
  717. asynchronous). Also, \[?] allows to switch back to standard
  718. mode."
  719. :group 'org-export-general
  720. :version "24.4"
  721. :package-version '(Org . "8.0")
  722. :type 'boolean)
  723. ;;; Defining Back-ends
  724. ;;
  725. ;; An export back-end is a structure with `org-export-backend' type
  726. ;; and `name', `parent', `transcoders', `options', `filters', `blocks'
  727. ;; and `menu' slots.
  728. ;;
  729. ;; At the lowest level, a back-end is created with
  730. ;; `org-export-create-backend' function.
  731. ;;
  732. ;; A named back-end can be registered with
  733. ;; `org-export-register-backend' function. A registered back-end can
  734. ;; later be referred to by its name, with `org-export-get-backend'
  735. ;; function. Also, such a back-end can become the parent of a derived
  736. ;; back-end from which slot values will be inherited by default.
  737. ;; `org-export-derived-backend-p' can check if a given back-end is
  738. ;; derived from a list of back-end names.
  739. ;;
  740. ;; `org-export-get-all-transcoders', `org-export-get-all-options' and
  741. ;; `org-export-get-all-filters' return the full alist of transcoders,
  742. ;; options and filters, including those inherited from ancestors.
  743. ;;
  744. ;; At a higher level, `org-export-define-backend' is the standard way
  745. ;; to define an export back-end. If the new back-end is similar to
  746. ;; a registered back-end, `org-export-define-derived-backend' may be
  747. ;; used instead.
  748. ;;
  749. ;; Eventually `org-export-barf-if-invalid-backend' returns an error
  750. ;; when a given back-end hasn't been registered yet.
  751. (defstruct (org-export-backend (:constructor org-export-create-backend)
  752. (:copier nil))
  753. name parent transcoders options filters blocks menu)
  754. (defun org-export-get-backend (name)
  755. "Return export back-end named after NAME.
  756. NAME is a symbol. Return nil if no such back-end is found."
  757. (catch 'found
  758. (dolist (b org-export--registered-backends)
  759. (when (eq (org-export-backend-name b) name)
  760. (throw 'found b)))))
  761. (defun org-export-register-backend (backend)
  762. "Register BACKEND as a known export back-end.
  763. BACKEND is a structure with `org-export-backend' type."
  764. ;; Refuse to register an unnamed back-end.
  765. (unless (org-export-backend-name backend)
  766. (error "Cannot register a unnamed export back-end"))
  767. ;; Refuse to register a back-end with an unknown parent.
  768. (let ((parent (org-export-backend-parent backend)))
  769. (when (and parent (not (org-export-get-backend parent)))
  770. (error "Cannot use unknown \"%s\" back-end as a parent" parent)))
  771. ;; Register dedicated export blocks in the parser.
  772. (dolist (name (org-export-backend-blocks backend))
  773. (add-to-list 'org-element-block-name-alist
  774. (cons name 'org-element-export-block-parser)))
  775. ;; If a back-end with the same name as BACKEND is already
  776. ;; registered, replace it with BACKEND. Otherwise, simply add
  777. ;; BACKEND to the list of registered back-ends.
  778. (let ((old (org-export-get-backend (org-export-backend-name backend))))
  779. (if old (setcar (memq old org-export--registered-backends) backend)
  780. (push backend org-export--registered-backends))))
  781. (defun org-export-barf-if-invalid-backend (backend)
  782. "Signal an error if BACKEND isn't defined."
  783. (unless (org-export-backend-p backend)
  784. (error "Unknown \"%s\" back-end: Aborting export" backend)))
  785. (defun org-export-derived-backend-p (backend &rest backends)
  786. "Non-nil if BACKEND is derived from one of BACKENDS.
  787. BACKEND is an export back-end, as returned by, e.g.,
  788. `org-export-create-backend', or a symbol referring to
  789. a registered back-end. BACKENDS is constituted of symbols."
  790. (when (symbolp backend) (setq backend (org-export-get-backend backend)))
  791. (when backend
  792. (catch 'exit
  793. (while (org-export-backend-parent backend)
  794. (when (memq (org-export-backend-name backend) backends)
  795. (throw 'exit t))
  796. (setq backend
  797. (org-export-get-backend (org-export-backend-parent backend))))
  798. (memq (org-export-backend-name backend) backends))))
  799. (defun org-export-get-all-transcoders (backend)
  800. "Return full translation table for BACKEND.
  801. BACKEND is an export back-end, as return by, e.g,,
  802. `org-export-create-backend'. Return value is an alist where
  803. keys are element or object types, as symbols, and values are
  804. transcoders.
  805. Unlike to `org-export-backend-transcoders', this function
  806. also returns transcoders inherited from parent back-ends,
  807. if any."
  808. (when (symbolp backend) (setq backend (org-export-get-backend backend)))
  809. (when backend
  810. (let ((transcoders (org-export-backend-transcoders backend))
  811. parent)
  812. (while (setq parent (org-export-backend-parent backend))
  813. (setq backend (org-export-get-backend parent))
  814. (setq transcoders
  815. (append transcoders (org-export-backend-transcoders backend))))
  816. transcoders)))
  817. (defun org-export-get-all-options (backend)
  818. "Return export options for BACKEND.
  819. BACKEND is an export back-end, as return by, e.g,,
  820. `org-export-create-backend'. See `org-export-options-alist'
  821. for the shape of the return value.
  822. Unlike to `org-export-backend-options', this function also
  823. returns options inherited from parent back-ends, if any."
  824. (when (symbolp backend) (setq backend (org-export-get-backend backend)))
  825. (when backend
  826. (let ((options (org-export-backend-options backend))
  827. parent)
  828. (while (setq parent (org-export-backend-parent backend))
  829. (setq backend (org-export-get-backend parent))
  830. (setq options (append options (org-export-backend-options backend))))
  831. options)))
  832. (defun org-export-get-all-filters (backend)
  833. "Return complete list of filters for BACKEND.
  834. BACKEND is an export back-end, as return by, e.g,,
  835. `org-export-create-backend'. Return value is an alist where
  836. keys are symbols and values lists of functions.
  837. Unlike to `org-export-backend-filters', this function also
  838. returns filters inherited from parent back-ends, if any."
  839. (when (symbolp backend) (setq backend (org-export-get-backend backend)))
  840. (when backend
  841. (let ((filters (org-export-backend-filters backend))
  842. parent)
  843. (while (setq parent (org-export-backend-parent backend))
  844. (setq backend (org-export-get-backend parent))
  845. (setq filters (append filters (org-export-backend-filters backend))))
  846. filters)))
  847. (defun org-export-define-backend (backend transcoders &rest body)
  848. "Define a new back-end BACKEND.
  849. TRANSCODERS is an alist between object or element types and
  850. functions handling them.
  851. These functions should return a string without any trailing
  852. space, or nil. They must accept three arguments: the object or
  853. element itself, its contents or nil when it isn't recursive and
  854. the property list used as a communication channel.
  855. Contents, when not nil, are stripped from any global indentation
  856. \(although the relative one is preserved). They also always end
  857. with a single newline character.
  858. If, for a given type, no function is found, that element or
  859. object type will simply be ignored, along with any blank line or
  860. white space at its end. The same will happen if the function
  861. returns the nil value. If that function returns the empty
  862. string, the type will be ignored, but the blank lines or white
  863. spaces will be kept.
  864. In addition to element and object types, one function can be
  865. associated to the `template' (or `inner-template') symbol and
  866. another one to the `plain-text' symbol.
  867. The former returns the final transcoded string, and can be used
  868. to add a preamble and a postamble to document's body. It must
  869. accept two arguments: the transcoded string and the property list
  870. containing export options. A function associated to `template'
  871. will not be applied if export has option \"body-only\".
  872. A function associated to `inner-template' is always applied.
  873. The latter, when defined, is to be called on every text not
  874. recognized as an element or an object. It must accept two
  875. arguments: the text string and the information channel. It is an
  876. appropriate place to protect special chars relative to the
  877. back-end.
  878. BODY can start with pre-defined keyword arguments. The following
  879. keywords are understood:
  880. :export-block
  881. String, or list of strings, representing block names that
  882. will not be parsed. This is used to specify blocks that will
  883. contain raw code specific to the back-end. These blocks
  884. still have to be handled by the relative `export-block' type
  885. translator.
  886. :filters-alist
  887. Alist between filters and function, or list of functions,
  888. specific to the back-end. See `org-export-filters-alist' for
  889. a list of all allowed filters. Filters defined here
  890. shouldn't make a back-end test, as it may prevent back-ends
  891. derived from this one to behave properly.
  892. :menu-entry
  893. Menu entry for the export dispatcher. It should be a list
  894. like:
  895. '(KEY DESCRIPTION-OR-ORDINAL ACTION-OR-MENU)
  896. where :
  897. KEY is a free character selecting the back-end.
  898. DESCRIPTION-OR-ORDINAL is either a string or a number.
  899. If it is a string, is will be used to name the back-end in
  900. its menu entry. If it is a number, the following menu will
  901. be displayed as a sub-menu of the back-end with the same
  902. KEY. Also, the number will be used to determine in which
  903. order such sub-menus will appear (lowest first).
  904. ACTION-OR-MENU is either a function or an alist.
  905. If it is an action, it will be called with four
  906. arguments (booleans): ASYNC, SUBTREEP, VISIBLE-ONLY and
  907. BODY-ONLY. See `org-export-as' for further explanations on
  908. some of them.
  909. If it is an alist, associations should follow the
  910. pattern:
  911. '(KEY DESCRIPTION ACTION)
  912. where KEY, DESCRIPTION and ACTION are described above.
  913. Valid values include:
  914. '(?m \"My Special Back-end\" my-special-export-function)
  915. or
  916. '(?l \"Export to LaTeX\"
  917. \(?p \"As PDF file\" org-latex-export-to-pdf)
  918. \(?o \"As PDF file and open\"
  919. \(lambda (a s v b)
  920. \(if a (org-latex-export-to-pdf t s v b)
  921. \(org-open-file
  922. \(org-latex-export-to-pdf nil s v b)))))))
  923. or the following, which will be added to the previous
  924. sub-menu,
  925. '(?l 1
  926. \((?B \"As TEX buffer (Beamer)\" org-beamer-export-as-latex)
  927. \(?P \"As PDF file (Beamer)\" org-beamer-export-to-pdf)))
  928. :options-alist
  929. Alist between back-end specific properties introduced in
  930. communication channel and how their value are acquired. See
  931. `org-export-options-alist' for more information about
  932. structure of the values."
  933. (declare (indent 1))
  934. (let (blocks filters menu-entry options contents)
  935. (while (keywordp (car body))
  936. (case (pop body)
  937. (:export-block (let ((names (pop body)))
  938. (setq blocks (if (consp names) (mapcar 'upcase names)
  939. (list (upcase names))))))
  940. (:filters-alist (setq filters (pop body)))
  941. (:menu-entry (setq menu-entry (pop body)))
  942. (:options-alist (setq options (pop body)))
  943. (t (pop body))))
  944. (org-export-register-backend
  945. (org-export-create-backend :name backend
  946. :transcoders transcoders
  947. :options options
  948. :filters filters
  949. :blocks blocks
  950. :menu menu-entry))))
  951. (defun org-export-define-derived-backend (child parent &rest body)
  952. "Create a new back-end as a variant of an existing one.
  953. CHILD is the name of the derived back-end. PARENT is the name of
  954. the parent back-end.
  955. BODY can start with pre-defined keyword arguments. The following
  956. keywords are understood:
  957. :export-block
  958. String, or list of strings, representing block names that
  959. will not be parsed. This is used to specify blocks that will
  960. contain raw code specific to the back-end. These blocks
  961. still have to be handled by the relative `export-block' type
  962. translator.
  963. :filters-alist
  964. Alist of filters that will overwrite or complete filters
  965. defined in PARENT back-end. See `org-export-filters-alist'
  966. for a list of allowed filters.
  967. :menu-entry
  968. Menu entry for the export dispatcher. See
  969. `org-export-define-backend' for more information about the
  970. expected value.
  971. :options-alist
  972. Alist of back-end specific properties that will overwrite or
  973. complete those defined in PARENT back-end. Refer to
  974. `org-export-options-alist' for more information about
  975. structure of the values.
  976. :translate-alist
  977. Alist of element and object types and transcoders that will
  978. overwrite or complete transcode table from PARENT back-end.
  979. Refer to `org-export-define-backend' for detailed information
  980. about transcoders.
  981. As an example, here is how one could define \"my-latex\" back-end
  982. as a variant of `latex' back-end with a custom template function:
  983. \(org-export-define-derived-backend 'my-latex 'latex
  984. :translate-alist '((template . my-latex-template-fun)))
  985. The back-end could then be called with, for example:
  986. \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
  987. (declare (indent 2))
  988. (let (blocks filters menu-entry options transcoders contents)
  989. (while (keywordp (car body))
  990. (case (pop body)
  991. (:export-block (let ((names (pop body)))
  992. (setq blocks (if (consp names) (mapcar 'upcase names)
  993. (list (upcase names))))))
  994. (:filters-alist (setq filters (pop body)))
  995. (:menu-entry (setq menu-entry (pop body)))
  996. (:options-alist (setq options (pop body)))
  997. (:translate-alist (setq transcoders (pop body)))
  998. (t (pop body))))
  999. (org-export-register-backend
  1000. (org-export-create-backend :name child
  1001. :parent parent
  1002. :transcoders transcoders
  1003. :options options
  1004. :filters filters
  1005. :blocks blocks
  1006. :menu menu-entry))))
  1007. ;;; The Communication Channel
  1008. ;;
  1009. ;; During export process, every function has access to a number of
  1010. ;; properties. They are of two types:
  1011. ;;
  1012. ;; 1. Environment options are collected once at the very beginning of
  1013. ;; the process, out of the original buffer and configuration.
  1014. ;; Collecting them is handled by `org-export-get-environment'
  1015. ;; function.
  1016. ;;
  1017. ;; Most environment options are defined through the
  1018. ;; `org-export-options-alist' variable.
  1019. ;;
  1020. ;; 2. Tree properties are extracted directly from the parsed tree,
  1021. ;; just before export, by `org-export-collect-tree-properties'.
  1022. ;;
  1023. ;; Here is the full list of properties available during transcode
  1024. ;; process, with their category and their value type.
  1025. ;;
  1026. ;; + `:author' :: Author's name.
  1027. ;; - category :: option
  1028. ;; - type :: string
  1029. ;;
  1030. ;; + `:back-end' :: Current back-end used for transcoding.
  1031. ;; - category :: tree
  1032. ;; - type :: symbol
  1033. ;;
  1034. ;; + `:creator' :: String to write as creation information.
  1035. ;; - category :: option
  1036. ;; - type :: string
  1037. ;;
  1038. ;; + `:date' :: String to use as date.
  1039. ;; - category :: option
  1040. ;; - type :: string
  1041. ;;
  1042. ;; + `:description' :: Description text for the current data.
  1043. ;; - category :: option
  1044. ;; - type :: string
  1045. ;;
  1046. ;; + `:email' :: Author's email.
  1047. ;; - category :: option
  1048. ;; - type :: string
  1049. ;;
  1050. ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
  1051. ;; process.
  1052. ;; - category :: option
  1053. ;; - type :: list of strings
  1054. ;;
  1055. ;; + `:export-options' :: List of export options available for current
  1056. ;; process.
  1057. ;; - category :: none
  1058. ;; - type :: list of symbols, among `subtree', `body-only' and
  1059. ;; `visible-only'.
  1060. ;;
  1061. ;; + `:exported-data' :: Hash table used for memoizing
  1062. ;; `org-export-data'.
  1063. ;; - category :: tree
  1064. ;; - type :: hash table
  1065. ;;
  1066. ;; + `:filetags' :: List of global tags for buffer. Used by
  1067. ;; `org-export-get-tags' to get tags with inheritance.
  1068. ;; - category :: option
  1069. ;; - type :: list of strings
  1070. ;;
  1071. ;; + `:footnote-definition-alist' :: Alist between footnote labels and
  1072. ;; their definition, as parsed data. Only non-inlined footnotes
  1073. ;; are represented in this alist. Also, every definition isn't
  1074. ;; guaranteed to be referenced in the parse tree. The purpose of
  1075. ;; this property is to preserve definitions from oblivion
  1076. ;; (i.e. when the parse tree comes from a part of the original
  1077. ;; buffer), it isn't meant for direct use in a back-end. To
  1078. ;; retrieve a definition relative to a reference, use
  1079. ;; `org-export-get-footnote-definition' instead.
  1080. ;; - category :: option
  1081. ;; - type :: alist (STRING . LIST)
  1082. ;;
  1083. ;; + `:headline-levels' :: Maximum level being exported as an
  1084. ;; headline. Comparison is done with the relative level of
  1085. ;; headlines in the parse tree, not necessarily with their
  1086. ;; actual level.
  1087. ;; - category :: option
  1088. ;; - type :: integer
  1089. ;;
  1090. ;; + `:headline-offset' :: Difference between relative and real level
  1091. ;; of headlines in the parse tree. For example, a value of -1
  1092. ;; means a level 2 headline should be considered as level
  1093. ;; 1 (cf. `org-export-get-relative-level').
  1094. ;; - category :: tree
  1095. ;; - type :: integer
  1096. ;;
  1097. ;; + `:headline-numbering' :: Alist between headlines and their
  1098. ;; numbering, as a list of numbers
  1099. ;; (cf. `org-export-get-headline-number').
  1100. ;; - category :: tree
  1101. ;; - type :: alist (INTEGER . LIST)
  1102. ;;
  1103. ;; + `:id-alist' :: Alist between ID strings and destination file's
  1104. ;; path, relative to current directory. It is used by
  1105. ;; `org-export-resolve-id-link' to resolve ID links targeting an
  1106. ;; external file.
  1107. ;; - category :: option
  1108. ;; - type :: alist (STRING . STRING)
  1109. ;;
  1110. ;; + `:ignore-list' :: List of elements and objects that should be
  1111. ;; ignored during export.
  1112. ;; - category :: tree
  1113. ;; - type :: list of elements and objects
  1114. ;;
  1115. ;; + `:input-file' :: Full path to input file, if any.
  1116. ;; - category :: option
  1117. ;; - type :: string or nil
  1118. ;;
  1119. ;; + `:keywords' :: List of keywords attached to data.
  1120. ;; - category :: option
  1121. ;; - type :: string
  1122. ;;
  1123. ;; + `:language' :: Default language used for translations.
  1124. ;; - category :: option
  1125. ;; - type :: string
  1126. ;;
  1127. ;; + `:parse-tree' :: Whole parse tree, available at any time during
  1128. ;; transcoding.
  1129. ;; - category :: option
  1130. ;; - type :: list (as returned by `org-element-parse-buffer')
  1131. ;;
  1132. ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
  1133. ;; all line breaks.
  1134. ;; - category :: option
  1135. ;; - type :: symbol (nil, t)
  1136. ;;
  1137. ;; + `:section-numbers' :: Non-nil means transcoding should add
  1138. ;; section numbers to headlines.
  1139. ;; - category :: option
  1140. ;; - type :: symbol (nil, t)
  1141. ;;
  1142. ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
  1143. ;; in transcoding. When such a tag is present, subtrees without
  1144. ;; it are de facto excluded from the process. See
  1145. ;; `use-select-tags'.
  1146. ;; - category :: option
  1147. ;; - type :: list of strings
  1148. ;;
  1149. ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
  1150. ;; a time stamp in the output.
  1151. ;; - category :: option
  1152. ;; - type :: symbol (nil, t)
  1153. ;;
  1154. ;; + `:translate-alist' :: Alist between element and object types and
  1155. ;; transcoding functions relative to the current back-end.
  1156. ;; Special keys `inner-template', `template' and `plain-text' are
  1157. ;; also possible.
  1158. ;; - category :: option
  1159. ;; - type :: alist (SYMBOL . FUNCTION)
  1160. ;;
  1161. ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
  1162. ;; also be transcoded. If it is set to the `headline' symbol,
  1163. ;; only the archived headline's name is retained.
  1164. ;; - category :: option
  1165. ;; - type :: symbol (nil, t, `headline')
  1166. ;;
  1167. ;; + `:with-author' :: Non-nil means author's name should be included
  1168. ;; in the output.
  1169. ;; - category :: option
  1170. ;; - type :: symbol (nil, t)
  1171. ;;
  1172. ;; + `:with-clocks' :: Non-nil means clock keywords should be exported.
  1173. ;; - category :: option
  1174. ;; - type :: symbol (nil, t)
  1175. ;;
  1176. ;; + `:with-creator' :: Non-nil means a creation sentence should be
  1177. ;; inserted at the end of the transcoded string. If the value
  1178. ;; is `comment', it should be commented.
  1179. ;; - category :: option
  1180. ;; - type :: symbol (`comment', nil, t)
  1181. ;;
  1182. ;; + `:with-date' :: Non-nil means output should contain a date.
  1183. ;; - category :: option
  1184. ;; - type :. symbol (nil, t)
  1185. ;;
  1186. ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
  1187. ;; its value is a list of names, only drawers with such names
  1188. ;; will be transcoded. If that list starts with `not', drawer
  1189. ;; with these names will be skipped.
  1190. ;; - category :: option
  1191. ;; - type :: symbol (nil, t) or list of strings
  1192. ;;
  1193. ;; + `:with-email' :: Non-nil means output should contain author's
  1194. ;; email.
  1195. ;; - category :: option
  1196. ;; - type :: symbol (nil, t)
  1197. ;;
  1198. ;; + `:with-emphasize' :: Non-nil means emphasized text should be
  1199. ;; interpreted.
  1200. ;; - category :: option
  1201. ;; - type :: symbol (nil, t)
  1202. ;;
  1203. ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
  1204. ;; strings starting with a colon as a fixed-with (verbatim) area.
  1205. ;; - category :: option
  1206. ;; - type :: symbol (nil, t)
  1207. ;;
  1208. ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
  1209. ;; footnotes.
  1210. ;; - category :: option
  1211. ;; - type :: symbol (nil, t)
  1212. ;;
  1213. ;; + `:with-latex' :: Non-nil means `latex-environment' elements and
  1214. ;; `latex-fragment' objects should appear in export output. When
  1215. ;; this property is set to `verbatim', they will be left as-is.
  1216. ;; - category :: option
  1217. ;; - type :: symbol (`verbatim', nil, t)
  1218. ;;
  1219. ;; + `:with-planning' :: Non-nil means transcoding should include
  1220. ;; planning info.
  1221. ;; - category :: option
  1222. ;; - type :: symbol (nil, t)
  1223. ;;
  1224. ;; + `:with-priority' :: Non-nil means transcoding should include
  1225. ;; priority cookies.
  1226. ;; - category :: option
  1227. ;; - type :: symbol (nil, t)
  1228. ;;
  1229. ;; + `:with-smart-quotes' :: Non-nil means activate smart quotes in
  1230. ;; plain text.
  1231. ;; - category :: option
  1232. ;; - type :: symbol (nil, t)
  1233. ;;
  1234. ;; + `:with-special-strings' :: Non-nil means transcoding should
  1235. ;; interpret special strings in plain text.
  1236. ;; - category :: option
  1237. ;; - type :: symbol (nil, t)
  1238. ;;
  1239. ;; + `:with-sub-superscript' :: Non-nil means transcoding should
  1240. ;; interpret subscript and superscript. With a value of "{}",
  1241. ;; only interpret those using curly brackets.
  1242. ;; - category :: option
  1243. ;; - type :: symbol (nil, {}, t)
  1244. ;;
  1245. ;; + `:with-tables' :: Non-nil means transcoding should interpret
  1246. ;; tables.
  1247. ;; - category :: option
  1248. ;; - type :: symbol (nil, t)
  1249. ;;
  1250. ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
  1251. ;; headlines. A `not-in-toc' value will remove them from the
  1252. ;; table of contents, if any, nonetheless.
  1253. ;; - category :: option
  1254. ;; - type :: symbol (nil, t, `not-in-toc')
  1255. ;;
  1256. ;; + `:with-tasks' :: Non-nil means transcoding should include
  1257. ;; headlines with a TODO keyword. A `todo' value will only
  1258. ;; include headlines with a todo type keyword while a `done'
  1259. ;; value will do the contrary. If a list of strings is provided,
  1260. ;; only tasks with keywords belonging to that list will be kept.
  1261. ;; - category :: option
  1262. ;; - type :: symbol (t, todo, done, nil) or list of strings
  1263. ;;
  1264. ;; + `:with-timestamps' :: Non-nil means transcoding should include
  1265. ;; time stamps. Special value `active' (resp. `inactive') ask to
  1266. ;; export only active (resp. inactive) timestamps. Otherwise,
  1267. ;; completely remove them.
  1268. ;; - category :: option
  1269. ;; - type :: symbol: (`active', `inactive', t, nil)
  1270. ;;
  1271. ;; + `:with-toc' :: Non-nil means that a table of contents has to be
  1272. ;; added to the output. An integer value limits its depth.
  1273. ;; - category :: option
  1274. ;; - type :: symbol (nil, t or integer)
  1275. ;;
  1276. ;; + `:with-todo-keywords' :: Non-nil means transcoding should
  1277. ;; include TODO keywords.
  1278. ;; - category :: option
  1279. ;; - type :: symbol (nil, t)
  1280. ;;;; Environment Options
  1281. ;;
  1282. ;; Environment options encompass all parameters defined outside the
  1283. ;; scope of the parsed data. They come from five sources, in
  1284. ;; increasing precedence order:
  1285. ;;
  1286. ;; - Global variables,
  1287. ;; - Buffer's attributes,
  1288. ;; - Options keyword symbols,
  1289. ;; - Buffer keywords,
  1290. ;; - Subtree properties.
  1291. ;;
  1292. ;; The central internal function with regards to environment options
  1293. ;; is `org-export-get-environment'. It updates global variables with
  1294. ;; "#+BIND:" keywords, then retrieve and prioritize properties from
  1295. ;; the different sources.
  1296. ;;
  1297. ;; The internal functions doing the retrieval are:
  1298. ;; `org-export--get-global-options',
  1299. ;; `org-export--get-buffer-attributes',
  1300. ;; `org-export--parse-option-keyword',
  1301. ;; `org-export--get-subtree-options' and
  1302. ;; `org-export--get-inbuffer-options'
  1303. ;;
  1304. ;; Also, `org-export--list-bound-variables' collects bound variables
  1305. ;; along with their value in order to set them as buffer local
  1306. ;; variables later in the process.
  1307. (defun org-export-get-environment (&optional backend subtreep ext-plist)
  1308. "Collect export options from the current buffer.
  1309. Optional argument BACKEND is an export back-end, as returned by
  1310. `org-export-create-backend'.
  1311. When optional argument SUBTREEP is non-nil, assume the export is
  1312. done against the current sub-tree.
  1313. Third optional argument EXT-PLIST is a property list with
  1314. external parameters overriding Org default settings, but still
  1315. inferior to file-local settings."
  1316. ;; First install #+BIND variables since these must be set before
  1317. ;; global options are read.
  1318. (dolist (pair (org-export--list-bound-variables))
  1319. (org-set-local (car pair) (nth 1 pair)))
  1320. ;; Get and prioritize export options...
  1321. (org-combine-plists
  1322. ;; ... from global variables...
  1323. (org-export--get-global-options backend)
  1324. ;; ... from an external property list...
  1325. ext-plist
  1326. ;; ... from in-buffer settings...
  1327. (org-export--get-inbuffer-options backend)
  1328. ;; ... and from subtree, when appropriate.
  1329. (and subtreep (org-export--get-subtree-options backend))
  1330. ;; Eventually add misc. properties.
  1331. (list
  1332. :back-end
  1333. backend
  1334. :translate-alist (org-export-get-all-transcoders backend)
  1335. :footnote-definition-alist
  1336. ;; Footnotes definitions must be collected in the original
  1337. ;; buffer, as there's no insurance that they will still be in
  1338. ;; the parse tree, due to possible narrowing.
  1339. (let (alist)
  1340. (org-with-wide-buffer
  1341. (goto-char (point-min))
  1342. (while (re-search-forward org-footnote-definition-re nil t)
  1343. (let ((def (save-match-data (org-element-at-point))))
  1344. (when (eq (org-element-type def) 'footnote-definition)
  1345. (push
  1346. (cons (org-element-property :label def)
  1347. (let ((cbeg (org-element-property :contents-begin def)))
  1348. (when cbeg
  1349. (org-element--parse-elements
  1350. cbeg (org-element-property :contents-end def)
  1351. nil nil nil nil (list 'org-data nil)))))
  1352. alist))))
  1353. alist))
  1354. :id-alist
  1355. ;; Collect id references.
  1356. (let (alist)
  1357. (org-with-wide-buffer
  1358. (goto-char (point-min))
  1359. (while (re-search-forward "\\[\\[id:\\S-+?\\]" nil t)
  1360. (let ((link (org-element-context)))
  1361. (when (eq (org-element-type link) 'link)
  1362. (let* ((id (org-element-property :path link))
  1363. (file (org-id-find-id-file id)))
  1364. (when file
  1365. (push (cons id (file-relative-name file)) alist)))))))
  1366. alist))))
  1367. (defun org-export--parse-option-keyword (options &optional backend)
  1368. "Parse an OPTIONS line and return values as a plist.
  1369. Optional argument BACKEND is an export back-end, as returned by,
  1370. e.g., `org-export-create-backend'. It specifies which back-end
  1371. specific items to read, if any."
  1372. (let* ((all
  1373. ;; Priority is given to back-end specific options.
  1374. (append (and backend (org-export-get-all-options backend))
  1375. org-export-options-alist))
  1376. plist)
  1377. (dolist (option all)
  1378. (let ((property (car option))
  1379. (item (nth 2 option)))
  1380. (when (and item
  1381. (not (plist-member plist property))
  1382. (string-match (concat "\\(\\`\\|[ \t]\\)"
  1383. (regexp-quote item)
  1384. ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
  1385. options))
  1386. (setq plist (plist-put plist
  1387. property
  1388. (car (read-from-string
  1389. (match-string 2 options))))))))
  1390. plist))
  1391. (defun org-export--get-subtree-options (&optional backend)
  1392. "Get export options in subtree at point.
  1393. Optional argument BACKEND is an export back-end, as returned by,
  1394. e.g., `org-export-create-backend'. It specifies back-end used
  1395. for export. Return options as a plist."
  1396. ;; For each buffer keyword, create a headline property setting the
  1397. ;; same property in communication channel. The name for the property
  1398. ;; is the keyword with "EXPORT_" appended to it.
  1399. (org-with-wide-buffer
  1400. (let (prop plist)
  1401. ;; Make sure point is at a heading.
  1402. (if (org-at-heading-p) (org-up-heading-safe) (org-back-to-heading t))
  1403. ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
  1404. ;; title as its fallback value.
  1405. (when (setq prop (or (org-entry-get (point) "EXPORT_TITLE")
  1406. (progn (looking-at org-todo-line-regexp)
  1407. (org-match-string-no-properties 3))))
  1408. (setq plist
  1409. (plist-put
  1410. plist :title
  1411. (org-element-parse-secondary-string
  1412. prop (org-element-restriction 'keyword)))))
  1413. ;; EXPORT_OPTIONS are parsed in a non-standard way.
  1414. (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
  1415. (setq plist
  1416. (nconc plist (org-export--parse-option-keyword prop backend))))
  1417. ;; Handle other keywords. TITLE keyword is excluded as it has
  1418. ;; been handled already.
  1419. (let ((seen '("TITLE")))
  1420. (mapc
  1421. (lambda (option)
  1422. (let ((property (car option))
  1423. (keyword (nth 1 option)))
  1424. (when (and keyword (not (member keyword seen)))
  1425. (let* ((subtree-prop (concat "EXPORT_" keyword))
  1426. ;; Export properties are not case-sensitive.
  1427. (value (let ((case-fold-search t))
  1428. (org-entry-get (point) subtree-prop))))
  1429. (push keyword seen)
  1430. (when (and value (not (plist-member plist property)))
  1431. (setq plist
  1432. (plist-put
  1433. plist
  1434. property
  1435. (cond
  1436. ;; Parse VALUE if required.
  1437. ((member keyword org-element-document-properties)
  1438. (org-element-parse-secondary-string
  1439. value (org-element-restriction 'keyword)))
  1440. ;; If BEHAVIOR is `split' expected value is
  1441. ;; a list of strings, not a string.
  1442. ((eq (nth 4 option) 'split) (org-split-string value))
  1443. (t value)))))))))
  1444. ;; Look for both general keywords and back-end specific
  1445. ;; options, with priority given to the latter.
  1446. (append (and backend (org-export-get-all-options backend))
  1447. org-export-options-alist)))
  1448. ;; Return value.
  1449. plist)))
  1450. (defun org-export--get-inbuffer-options (&optional backend)
  1451. "Return current buffer export options, as a plist.
  1452. Optional argument BACKEND, when non-nil, is an export back-end,
  1453. as returned by, e.g., `org-export-create-backend'. It specifies
  1454. which back-end specific options should also be read in the
  1455. process.
  1456. Assume buffer is in Org mode. Narrowing, if any, is ignored."
  1457. (let* (plist
  1458. get-options ; For byte-compiler.
  1459. (case-fold-search t)
  1460. (options (append
  1461. ;; Priority is given to back-end specific options.
  1462. (and backend (org-export-get-all-options backend))
  1463. org-export-options-alist))
  1464. (regexp (format "^[ \t]*#\\+%s:"
  1465. (regexp-opt (nconc (delq nil (mapcar 'cadr options))
  1466. org-export-special-keywords))))
  1467. (find-properties
  1468. (lambda (keyword)
  1469. ;; Return all properties associated to KEYWORD.
  1470. (let (properties)
  1471. (dolist (option options properties)
  1472. (when (equal (nth 1 option) keyword)
  1473. (pushnew (car option) properties))))))
  1474. (get-options
  1475. (lambda (&optional files plist)
  1476. ;; Recursively read keywords in buffer. FILES is a list
  1477. ;; of files read so far. PLIST is the current property
  1478. ;; list obtained.
  1479. (org-with-wide-buffer
  1480. (goto-char (point-min))
  1481. (while (re-search-forward regexp nil t)
  1482. (let ((element (org-element-at-point)))
  1483. (when (eq (org-element-type element) 'keyword)
  1484. (let ((key (org-element-property :key element))
  1485. (val (org-element-property :value element)))
  1486. (cond
  1487. ;; Options in `org-export-special-keywords'.
  1488. ((equal key "SETUPFILE")
  1489. (let ((file (expand-file-name
  1490. (org-remove-double-quotes (org-trim val)))))
  1491. ;; Avoid circular dependencies.
  1492. (unless (member file files)
  1493. (with-temp-buffer
  1494. (insert (org-file-contents file 'noerror))
  1495. (let ((org-inhibit-startup t)) (org-mode))
  1496. (setq plist (funcall get-options
  1497. (cons file files) plist))))))
  1498. ((equal key "OPTIONS")
  1499. (setq plist
  1500. (org-combine-plists
  1501. plist
  1502. (org-export--parse-option-keyword val backend))))
  1503. ((equal key "FILETAGS")
  1504. (setq plist
  1505. (org-combine-plists
  1506. plist
  1507. (list :filetags
  1508. (org-uniquify
  1509. (append (org-split-string val ":")
  1510. (plist-get plist :filetags)))))))
  1511. (t
  1512. ;; Options in `org-export-options-alist'.
  1513. (dolist (property (funcall find-properties key))
  1514. (let ((behaviour (nth 4 (assq property options))))
  1515. (setq plist
  1516. (plist-put
  1517. plist property
  1518. ;; Handle value depending on specified
  1519. ;; BEHAVIOR.
  1520. (case behaviour
  1521. (space
  1522. (if (not (plist-get plist property))
  1523. (org-trim val)
  1524. (concat (plist-get plist property)
  1525. " "
  1526. (org-trim val))))
  1527. (newline
  1528. (org-trim
  1529. (concat (plist-get plist property)
  1530. "\n"
  1531. (org-trim val))))
  1532. (split `(,@(plist-get plist property)
  1533. ,@(org-split-string val)))
  1534. ('t val)
  1535. (otherwise
  1536. (if (not (plist-member plist property)) val
  1537. (plist-get plist property))))))))))))))
  1538. ;; Return final value.
  1539. plist))))
  1540. ;; Read options in the current buffer.
  1541. (setq plist (funcall get-options
  1542. (and buffer-file-name (list buffer-file-name)) nil))
  1543. ;; Parse keywords specified in `org-element-document-properties'
  1544. ;; and return PLIST.
  1545. (dolist (keyword org-element-document-properties plist)
  1546. (dolist (property (funcall find-properties keyword))
  1547. (let ((value (plist-get plist property)))
  1548. (when (stringp value)
  1549. (setq plist
  1550. (plist-put plist property
  1551. (org-element-parse-secondary-string
  1552. value (org-element-restriction 'keyword))))))))))
  1553. (defun org-export--get-buffer-attributes ()
  1554. "Return properties related to buffer attributes, as a plist."
  1555. ;; Store full path of input file name, or nil. For internal use.
  1556. (let ((visited-file (buffer-file-name (buffer-base-buffer))))
  1557. (list :input-file visited-file
  1558. :title (if (not visited-file) (buffer-name (buffer-base-buffer))
  1559. (file-name-sans-extension
  1560. (file-name-nondirectory visited-file))))))
  1561. (defun org-export--get-global-options (&optional backend)
  1562. "Return global export options as a plist.
  1563. Optional argument BACKEND, if non-nil, is an export back-end, as
  1564. returned by, e.g., `org-export-create-backend'. It specifies
  1565. which back-end specific export options should also be read in the
  1566. process."
  1567. (let (plist
  1568. ;; Priority is given to back-end specific options.
  1569. (all (append (and backend (org-export-get-all-options backend))
  1570. org-export-options-alist)))
  1571. (dolist (cell all plist)
  1572. (let ((prop (car cell))
  1573. (default-value (nth 3 cell)))
  1574. (unless (or (not default-value) (plist-member plist prop))
  1575. (setq plist
  1576. (plist-put
  1577. plist
  1578. prop
  1579. ;; Eval default value provided. If keyword is
  1580. ;; a member of `org-element-document-properties',
  1581. ;; parse it as a secondary string before storing it.
  1582. (let ((value (eval (nth 3 cell))))
  1583. (if (not (stringp value)) value
  1584. (let ((keyword (nth 1 cell)))
  1585. (if (member keyword org-element-document-properties)
  1586. (org-element-parse-secondary-string
  1587. value (org-element-restriction 'keyword))
  1588. value)))))))))))
  1589. (defun org-export--list-bound-variables ()
  1590. "Return variables bound from BIND keywords in current buffer.
  1591. Also look for BIND keywords in setup files. The return value is
  1592. an alist where associations are (VARIABLE-NAME VALUE)."
  1593. (when org-export-allow-bind-keywords
  1594. (let* (collect-bind ; For byte-compiler.
  1595. (collect-bind
  1596. (lambda (files alist)
  1597. ;; Return an alist between variable names and their
  1598. ;; value. FILES is a list of setup files names read so
  1599. ;; far, used to avoid circular dependencies. ALIST is
  1600. ;; the alist collected so far.
  1601. (let ((case-fold-search t))
  1602. (org-with-wide-buffer
  1603. (goto-char (point-min))
  1604. (while (re-search-forward
  1605. "^[ \t]*#\\+\\(BIND\\|SETUPFILE\\):" nil t)
  1606. (let ((element (org-element-at-point)))
  1607. (when (eq (org-element-type element) 'keyword)
  1608. (let ((val (org-element-property :value element)))
  1609. (if (equal (org-element-property :key element) "BIND")
  1610. (push (read (format "(%s)" val)) alist)
  1611. ;; Enter setup file.
  1612. (let ((file (expand-file-name
  1613. (org-remove-double-quotes val))))
  1614. (unless (member file files)
  1615. (with-temp-buffer
  1616. (let ((org-inhibit-startup t)) (org-mode))
  1617. (insert (org-file-contents file 'noerror))
  1618. (setq alist
  1619. (funcall collect-bind
  1620. (cons file files)
  1621. alist))))))))))
  1622. alist)))))
  1623. ;; Return value in appropriate order of appearance.
  1624. (nreverse (funcall collect-bind nil nil)))))
  1625. ;;;; Tree Properties
  1626. ;;
  1627. ;; Tree properties are information extracted from parse tree. They
  1628. ;; are initialized at the beginning of the transcoding process by
  1629. ;; `org-export-collect-tree-properties'.
  1630. ;;
  1631. ;; Dedicated functions focus on computing the value of specific tree
  1632. ;; properties during initialization. Thus,
  1633. ;; `org-export--populate-ignore-list' lists elements and objects that
  1634. ;; should be skipped during export, `org-export--get-min-level' gets
  1635. ;; the minimal exportable level, used as a basis to compute relative
  1636. ;; level for headlines. Eventually
  1637. ;; `org-export--collect-headline-numbering' builds an alist between
  1638. ;; headlines and their numbering.
  1639. (defun org-export-collect-tree-properties (data info)
  1640. "Extract tree properties from parse tree.
  1641. DATA is the parse tree from which information is retrieved. INFO
  1642. is a list holding export options.
  1643. Following tree properties are set or updated:
  1644. `:exported-data' Hash table used to memoize results from
  1645. `org-export-data'.
  1646. `:footnote-definition-alist' List of footnotes definitions in
  1647. original buffer and current parse tree.
  1648. `:headline-offset' Offset between true level of headlines and
  1649. local level. An offset of -1 means a headline
  1650. of level 2 should be considered as a level
  1651. 1 headline in the context.
  1652. `:headline-numbering' Alist of all headlines as key an the
  1653. associated numbering as value.
  1654. `:ignore-list' List of elements that should be ignored during
  1655. export.
  1656. Return updated plist."
  1657. ;; Install the parse tree in the communication channel, in order to
  1658. ;; use `org-export-get-genealogy' and al.
  1659. (setq info (plist-put info :parse-tree data))
  1660. ;; Get the list of elements and objects to ignore, and put it into
  1661. ;; `:ignore-list'. Do not overwrite any user ignore that might have
  1662. ;; been done during parse tree filtering.
  1663. (setq info
  1664. (plist-put info
  1665. :ignore-list
  1666. (append (org-export--populate-ignore-list data info)
  1667. (plist-get info :ignore-list))))
  1668. ;; Compute `:headline-offset' in order to be able to use
  1669. ;; `org-export-get-relative-level'.
  1670. (setq info
  1671. (plist-put info
  1672. :headline-offset
  1673. (- 1 (org-export--get-min-level data info))))
  1674. ;; Update footnotes definitions list with definitions in parse tree.
  1675. ;; This is required since buffer expansion might have modified
  1676. ;; boundaries of footnote definitions contained in the parse tree.
  1677. ;; This way, definitions in `footnote-definition-alist' are bound to
  1678. ;; match those in the parse tree.
  1679. (let ((defs (plist-get info :footnote-definition-alist)))
  1680. (org-element-map data 'footnote-definition
  1681. (lambda (fn)
  1682. (push (cons (org-element-property :label fn)
  1683. `(org-data nil ,@(org-element-contents fn)))
  1684. defs)))
  1685. (setq info (plist-put info :footnote-definition-alist defs)))
  1686. ;; Properties order doesn't matter: get the rest of the tree
  1687. ;; properties.
  1688. (nconc
  1689. `(:headline-numbering ,(org-export--collect-headline-numbering data info)
  1690. :exported-data ,(make-hash-table :test 'eq :size 4001))
  1691. info))
  1692. (defun org-export--get-min-level (data options)
  1693. "Return minimum exportable headline's level in DATA.
  1694. DATA is parsed tree as returned by `org-element-parse-buffer'.
  1695. OPTIONS is a plist holding export options."
  1696. (catch 'exit
  1697. (let ((min-level 10000))
  1698. (mapc
  1699. (lambda (blob)
  1700. (when (and (eq (org-element-type blob) 'headline)
  1701. (not (org-element-property :footnote-section-p blob))
  1702. (not (memq blob (plist-get options :ignore-list))))
  1703. (setq min-level (min (org-element-property :level blob) min-level)))
  1704. (when (= min-level 1) (throw 'exit 1)))
  1705. (org-element-contents data))
  1706. ;; If no headline was found, for the sake of consistency, set
  1707. ;; minimum level to 1 nonetheless.
  1708. (if (= min-level 10000) 1 min-level))))
  1709. (defun org-export--collect-headline-numbering (data options)
  1710. "Return numbering of all exportable headlines in a parse tree.
  1711. DATA is the parse tree. OPTIONS is the plist holding export
  1712. options.
  1713. Return an alist whose key is a headline and value is its
  1714. associated numbering \(in the shape of a list of numbers\) or nil
  1715. for a footnotes section."
  1716. (let ((numbering (make-vector org-export-max-depth 0)))
  1717. (org-element-map data 'headline
  1718. (lambda (headline)
  1719. (unless (org-element-property :footnote-section-p headline)
  1720. (let ((relative-level
  1721. (1- (org-export-get-relative-level headline options))))
  1722. (cons
  1723. headline
  1724. (loop for n across numbering
  1725. for idx from 0 to org-export-max-depth
  1726. when (< idx relative-level) collect n
  1727. when (= idx relative-level) collect (aset numbering idx (1+ n))
  1728. when (> idx relative-level) do (aset numbering idx 0))))))
  1729. options)))
  1730. (defun org-export--populate-ignore-list (data options)
  1731. "Return list of elements and objects to ignore during export.
  1732. DATA is the parse tree to traverse. OPTIONS is the plist holding
  1733. export options."
  1734. (let* (ignore
  1735. walk-data
  1736. ;; First find trees containing a select tag, if any.
  1737. (selected (org-export--selected-trees data options))
  1738. (walk-data
  1739. (lambda (data)
  1740. ;; Collect ignored elements or objects into IGNORE-LIST.
  1741. (let ((type (org-element-type data)))
  1742. (if (org-export--skip-p data options selected) (push data ignore)
  1743. (if (and (eq type 'headline)
  1744. (eq (plist-get options :with-archived-trees) 'headline)
  1745. (org-element-property :archivedp data))
  1746. ;; If headline is archived but tree below has
  1747. ;; to be skipped, add it to ignore list.
  1748. (mapc (lambda (e) (push e ignore))
  1749. (org-element-contents data))
  1750. ;; Move into secondary string, if any.
  1751. (let ((sec-prop
  1752. (cdr (assq type org-element-secondary-value-alist))))
  1753. (when sec-prop
  1754. (mapc walk-data (org-element-property sec-prop data))))
  1755. ;; Move into recursive objects/elements.
  1756. (mapc walk-data (org-element-contents data))))))))
  1757. ;; Main call.
  1758. (funcall walk-data data)
  1759. ;; Return value.
  1760. ignore))
  1761. (defun org-export--selected-trees (data info)
  1762. "Return list of headlines and inlinetasks with a select tag in their tree.
  1763. DATA is parsed data as returned by `org-element-parse-buffer'.
  1764. INFO is a plist holding export options."
  1765. (let* (selected-trees
  1766. walk-data ; For byte-compiler.
  1767. (walk-data
  1768. (function
  1769. (lambda (data genealogy)
  1770. (let ((type (org-element-type data)))
  1771. (cond
  1772. ((memq type '(headline inlinetask))
  1773. (let ((tags (org-element-property :tags data)))
  1774. (if (loop for tag in (plist-get info :select-tags)
  1775. thereis (member tag tags))
  1776. ;; When a select tag is found, mark full
  1777. ;; genealogy and every headline within the tree
  1778. ;; as acceptable.
  1779. (setq selected-trees
  1780. (append
  1781. genealogy
  1782. (org-element-map data '(headline inlinetask)
  1783. 'identity)
  1784. selected-trees))
  1785. ;; If at a headline, continue searching in tree,
  1786. ;; recursively.
  1787. (when (eq type 'headline)
  1788. (mapc (lambda (el)
  1789. (funcall walk-data el (cons data genealogy)))
  1790. (org-element-contents data))))))
  1791. ((or (eq type 'org-data)
  1792. (memq type org-element-greater-elements))
  1793. (mapc (lambda (el) (funcall walk-data el genealogy))
  1794. (org-element-contents data)))))))))
  1795. (funcall walk-data data nil)
  1796. selected-trees))
  1797. (defun org-export--skip-p (blob options selected)
  1798. "Non-nil when element or object BLOB should be skipped during export.
  1799. OPTIONS is the plist holding export options. SELECTED, when
  1800. non-nil, is a list of headlines or inlinetasks belonging to
  1801. a tree with a select tag."
  1802. (case (org-element-type blob)
  1803. (clock (not (plist-get options :with-clocks)))
  1804. (drawer
  1805. (let ((with-drawers-p (plist-get options :with-drawers)))
  1806. (or (not with-drawers-p)
  1807. (and (consp with-drawers-p)
  1808. ;; If `:with-drawers' value starts with `not', ignore
  1809. ;; every drawer whose name belong to that list.
  1810. ;; Otherwise, ignore drawers whose name isn't in that
  1811. ;; list.
  1812. (let ((name (org-element-property :drawer-name blob)))
  1813. (if (eq (car with-drawers-p) 'not)
  1814. (member-ignore-case name (cdr with-drawers-p))
  1815. (not (member-ignore-case name with-drawers-p))))))))
  1816. ((footnote-definition footnote-reference)
  1817. (not (plist-get options :with-footnotes)))
  1818. ((headline inlinetask)
  1819. (let ((with-tasks (plist-get options :with-tasks))
  1820. (todo (org-element-property :todo-keyword blob))
  1821. (todo-type (org-element-property :todo-type blob))
  1822. (archived (plist-get options :with-archived-trees))
  1823. (tags (org-element-property :tags blob)))
  1824. (or
  1825. (and (eq (org-element-type blob) 'inlinetask)
  1826. (not (plist-get options :with-inlinetasks)))
  1827. ;; Ignore subtrees with an exclude tag.
  1828. (loop for k in (plist-get options :exclude-tags)
  1829. thereis (member k tags))
  1830. ;; When a select tag is present in the buffer, ignore any tree
  1831. ;; without it.
  1832. (and selected (not (memq blob selected)))
  1833. ;; Ignore commented sub-trees.
  1834. (org-element-property :commentedp blob)
  1835. ;; Ignore archived subtrees if `:with-archived-trees' is nil.
  1836. (and (not archived) (org-element-property :archivedp blob))
  1837. ;; Ignore tasks, if specified by `:with-tasks' property.
  1838. (and todo
  1839. (or (not with-tasks)
  1840. (and (memq with-tasks '(todo done))
  1841. (not (eq todo-type with-tasks)))
  1842. (and (consp with-tasks) (not (member todo with-tasks))))))))
  1843. ((latex-environment latex-fragment) (not (plist-get options :with-latex)))
  1844. (planning (not (plist-get options :with-planning)))
  1845. (statistics-cookie (not (plist-get options :with-statistics-cookies)))
  1846. (table-cell
  1847. (and (org-export-table-has-special-column-p
  1848. (org-export-get-parent-table blob))
  1849. (not (org-export-get-previous-element blob options))))
  1850. (table-row (org-export-table-row-is-special-p blob options))
  1851. (timestamp
  1852. ;; `:with-timestamps' only applies to isolated timestamps
  1853. ;; objects, i.e. timestamp objects in a paragraph containing only
  1854. ;; timestamps and whitespaces.
  1855. (when (let ((parent (org-export-get-parent-element blob)))
  1856. (and (memq (org-element-type parent) '(paragraph verse-block))
  1857. (not (org-element-map parent
  1858. (cons 'plain-text
  1859. (remq 'timestamp org-element-all-objects))
  1860. (lambda (obj)
  1861. (or (not (stringp obj)) (org-string-nw-p obj)))
  1862. options t))))
  1863. (case (plist-get options :with-timestamps)
  1864. ('nil t)
  1865. (active
  1866. (not (memq (org-element-property :type blob) '(active active-range))))
  1867. (inactive
  1868. (not (memq (org-element-property :type blob)
  1869. '(inactive inactive-range)))))))))
  1870. ;;; The Transcoder
  1871. ;;
  1872. ;; `org-export-data' reads a parse tree (obtained with, i.e.
  1873. ;; `org-element-parse-buffer') and transcodes it into a specified
  1874. ;; back-end output. It takes care of filtering out elements or
  1875. ;; objects according to export options and organizing the output blank
  1876. ;; lines and white space are preserved. The function memoizes its
  1877. ;; results, so it is cheap to call it within transcoders.
  1878. ;;
  1879. ;; It is possible to modify locally the back-end used by
  1880. ;; `org-export-data' or even use a temporary back-end by using
  1881. ;; `org-export-data-with-backend'.
  1882. ;;
  1883. ;; Internally, three functions handle the filtering of objects and
  1884. ;; elements during the export. In particular,
  1885. ;; `org-export-ignore-element' marks an element or object so future
  1886. ;; parse tree traversals skip it, `org-export--interpret-p' tells which
  1887. ;; elements or objects should be seen as real Org syntax and
  1888. ;; `org-export-expand' transforms the others back into their original
  1889. ;; shape
  1890. ;;
  1891. ;; `org-export-transcoder' is an accessor returning appropriate
  1892. ;; translator function for a given element or object.
  1893. (defun org-export-transcoder (blob info)
  1894. "Return appropriate transcoder for BLOB.
  1895. INFO is a plist containing export directives."
  1896. (let ((type (org-element-type blob)))
  1897. ;; Return contents only for complete parse trees.
  1898. (if (eq type 'org-data) (lambda (blob contents info) contents)
  1899. (let ((transcoder (cdr (assq type (plist-get info :translate-alist)))))
  1900. (and (functionp transcoder) transcoder)))))
  1901. (defun org-export-data (data info)
  1902. "Convert DATA into current back-end format.
  1903. DATA is a parse tree, an element or an object or a secondary
  1904. string. INFO is a plist holding export options.
  1905. Return transcoded string."
  1906. (let ((memo (gethash data (plist-get info :exported-data) 'no-memo)))
  1907. (if (not (eq memo 'no-memo)) memo
  1908. (let* ((type (org-element-type data))
  1909. (results
  1910. (cond
  1911. ;; Ignored element/object.
  1912. ((memq data (plist-get info :ignore-list)) nil)
  1913. ;; Plain text.
  1914. ((eq type 'plain-text)
  1915. (org-export-filter-apply-functions
  1916. (plist-get info :filter-plain-text)
  1917. (let ((transcoder (org-export-transcoder data info)))
  1918. (if transcoder (funcall transcoder data info) data))
  1919. info))
  1920. ;; Uninterpreted element/object: change it back to Org
  1921. ;; syntax and export again resulting raw string.
  1922. ((not (org-export--interpret-p data info))
  1923. (org-export-data
  1924. (org-export-expand
  1925. data
  1926. (mapconcat (lambda (blob) (org-export-data blob info))
  1927. (org-element-contents data)
  1928. ""))
  1929. info))
  1930. ;; Secondary string.
  1931. ((not type)
  1932. (mapconcat (lambda (obj) (org-export-data obj info)) data ""))
  1933. ;; Element/Object without contents or, as a special case,
  1934. ;; headline with archive tag and archived trees restricted
  1935. ;; to title only.
  1936. ((or (not (org-element-contents data))
  1937. (and (eq type 'headline)
  1938. (eq (plist-get info :with-archived-trees) 'headline)
  1939. (org-element-property :archivedp data)))
  1940. (let ((transcoder (org-export-transcoder data info)))
  1941. (or (and (functionp transcoder)
  1942. (funcall transcoder data nil info))
  1943. ;; Export snippets never return a nil value so
  1944. ;; that white spaces following them are never
  1945. ;; ignored.
  1946. (and (eq type 'export-snippet) ""))))
  1947. ;; Element/Object with contents.
  1948. (t
  1949. (let ((transcoder (org-export-transcoder data info)))
  1950. (when transcoder
  1951. (let* ((greaterp (memq type org-element-greater-elements))
  1952. (objectp
  1953. (and (not greaterp)
  1954. (memq type org-element-recursive-objects)))
  1955. (contents
  1956. (mapconcat
  1957. (lambda (element) (org-export-data element info))
  1958. (org-element-contents
  1959. (if (or greaterp objectp) data
  1960. ;; Elements directly containing objects
  1961. ;; must have their indentation normalized
  1962. ;; first.
  1963. (org-element-normalize-contents
  1964. data
  1965. ;; When normalizing contents of the first
  1966. ;; paragraph in an item or a footnote
  1967. ;; definition, ignore first line's
  1968. ;; indentation: there is none and it
  1969. ;; might be misleading.
  1970. (when (eq type 'paragraph)
  1971. (let ((parent (org-export-get-parent data)))
  1972. (and
  1973. (eq (car (org-element-contents parent))
  1974. data)
  1975. (memq (org-element-type parent)
  1976. '(footnote-definition item))))))))
  1977. "")))
  1978. (funcall transcoder data
  1979. (if (not greaterp) contents
  1980. (org-element-normalize-string contents))
  1981. info))))))))
  1982. ;; Final result will be memoized before being returned.
  1983. (puthash
  1984. data
  1985. (cond
  1986. ((not results) nil)
  1987. ((memq type '(org-data plain-text nil)) results)
  1988. ;; Append the same white space between elements or objects as in
  1989. ;; the original buffer, and call appropriate filters.
  1990. (t
  1991. (let ((results
  1992. (org-export-filter-apply-functions
  1993. (plist-get info (intern (format ":filter-%s" type)))
  1994. (let ((post-blank (or (org-element-property :post-blank data)
  1995. 0)))
  1996. (if (memq type org-element-all-elements)
  1997. (concat (org-element-normalize-string results)
  1998. (make-string post-blank ?\n))
  1999. (concat results (make-string post-blank ? ))))
  2000. info)))
  2001. results)))
  2002. (plist-get info :exported-data))))))
  2003. (defun org-export-data-with-backend (data backend info)
  2004. "Convert DATA into BACKEND format.
  2005. DATA is an element, an object, a secondary string or a string.
  2006. BACKEND is a symbol. INFO is a plist used as a communication
  2007. channel.
  2008. Unlike to `org-export-with-backend', this function will
  2009. recursively convert DATA using BACKEND translation table."
  2010. (when (symbolp backend) (setq backend (org-export-get-backend backend)))
  2011. (org-export-data
  2012. data
  2013. ;; Set-up a new communication channel with translations defined in
  2014. ;; BACKEND as the translate table and a new hash table for
  2015. ;; memoization.
  2016. (org-combine-plists
  2017. info
  2018. (list :back-end backend
  2019. :translate-alist (org-export-get-all-transcoders backend)
  2020. ;; Size of the hash table is reduced since this function
  2021. ;; will probably be used on small trees.
  2022. :exported-data (make-hash-table :test 'eq :size 401)))))
  2023. (defun org-export--interpret-p (blob info)
  2024. "Non-nil if element or object BLOB should be interpreted during export.
  2025. If nil, BLOB will appear as raw Org syntax. Check is done
  2026. according to export options INFO, stored as a plist."
  2027. (case (org-element-type blob)
  2028. ;; ... entities...
  2029. (entity (plist-get info :with-entities))
  2030. ;; ... emphasis...
  2031. ((bold italic strike-through underline)
  2032. (plist-get info :with-emphasize))
  2033. ;; ... fixed-width areas.
  2034. (fixed-width (plist-get info :with-fixed-width))
  2035. ;; ... LaTeX environments and fragments...
  2036. ((latex-environment latex-fragment)
  2037. (let ((with-latex-p (plist-get info :with-latex)))
  2038. (and with-latex-p (not (eq with-latex-p 'verbatim)))))
  2039. ;; ... sub/superscripts...
  2040. ((subscript superscript)
  2041. (let ((sub/super-p (plist-get info :with-sub-superscript)))
  2042. (if (eq sub/super-p '{})
  2043. (org-element-property :use-brackets-p blob)
  2044. sub/super-p)))
  2045. ;; ... tables...
  2046. (table (plist-get info :with-tables))
  2047. (otherwise t)))
  2048. (defun org-export-expand (blob contents &optional with-affiliated)
  2049. "Expand a parsed element or object to its original state.
  2050. BLOB is either an element or an object. CONTENTS is its
  2051. contents, as a string or nil.
  2052. When optional argument WITH-AFFILIATED is non-nil, add affiliated
  2053. keywords before output."
  2054. (let ((type (org-element-type blob)))
  2055. (concat (and with-affiliated (memq type org-element-all-elements)
  2056. (org-element--interpret-affiliated-keywords blob))
  2057. (funcall (intern (format "org-element-%s-interpreter" type))
  2058. blob contents))))
  2059. (defun org-export-ignore-element (element info)
  2060. "Add ELEMENT to `:ignore-list' in INFO.
  2061. Any element in `:ignore-list' will be skipped when using
  2062. `org-element-map'. INFO is modified by side effects."
  2063. (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
  2064. ;;; The Filter System
  2065. ;;
  2066. ;; Filters allow end-users to tweak easily the transcoded output.
  2067. ;; They are the functional counterpart of hooks, as every filter in
  2068. ;; a set is applied to the return value of the previous one.
  2069. ;;
  2070. ;; Every set is back-end agnostic. Although, a filter is always
  2071. ;; called, in addition to the string it applies to, with the back-end
  2072. ;; used as argument, so it's easy for the end-user to add back-end
  2073. ;; specific filters in the set. The communication channel, as
  2074. ;; a plist, is required as the third argument.
  2075. ;;
  2076. ;; From the developer side, filters sets can be installed in the
  2077. ;; process with the help of `org-export-define-backend', which
  2078. ;; internally stores filters as an alist. Each association has a key
  2079. ;; among the following symbols and a function or a list of functions
  2080. ;; as value.
  2081. ;;
  2082. ;; - `:filter-options' applies to the property list containing export
  2083. ;; options. Unlike to other filters, functions in this list accept
  2084. ;; two arguments instead of three: the property list containing
  2085. ;; export options and the back-end. Users can set its value through
  2086. ;; `org-export-filter-options-functions' variable.
  2087. ;;
  2088. ;; - `:filter-parse-tree' applies directly to the complete parsed
  2089. ;; tree. Users can set it through
  2090. ;; `org-export-filter-parse-tree-functions' variable.
  2091. ;;
  2092. ;; - `:filter-final-output' applies to the final transcoded string.
  2093. ;; Users can set it with `org-export-filter-final-output-functions'
  2094. ;; variable
  2095. ;;
  2096. ;; - `:filter-plain-text' applies to any string not recognized as Org
  2097. ;; syntax. `org-export-filter-plain-text-functions' allows users to
  2098. ;; configure it.
  2099. ;;
  2100. ;; - `:filter-TYPE' applies on the string returned after an element or
  2101. ;; object of type TYPE has been transcoded. A user can modify
  2102. ;; `org-export-filter-TYPE-functions'
  2103. ;;
  2104. ;; All filters sets are applied with
  2105. ;; `org-export-filter-apply-functions' function. Filters in a set are
  2106. ;; applied in a LIFO fashion. It allows developers to be sure that
  2107. ;; their filters will be applied first.
  2108. ;;
  2109. ;; Filters properties are installed in communication channel with
  2110. ;; `org-export-install-filters' function.
  2111. ;;
  2112. ;; Eventually, two hooks (`org-export-before-processing-hook' and
  2113. ;; `org-export-before-parsing-hook') are run at the beginning of the
  2114. ;; export process and just before parsing to allow for heavy structure
  2115. ;; modifications.
  2116. ;;;; Hooks
  2117. (defvar org-export-before-processing-hook nil
  2118. "Hook run at the beginning of the export process.
  2119. This is run before include keywords and macros are expanded and
  2120. Babel code blocks executed, on a copy of the original buffer
  2121. being exported. Visibility and narrowing are preserved. Point
  2122. is at the beginning of the buffer.
  2123. Every function in this hook will be called with one argument: the
  2124. back-end currently used, as a symbol.")
  2125. (defvar org-export-before-parsing-hook nil
  2126. "Hook run before parsing an export buffer.
  2127. This is run after include keywords and macros have been expanded
  2128. and Babel code blocks executed, on a copy of the original buffer
  2129. being exported. Visibility and narrowing are preserved. Point
  2130. is at the beginning of the buffer.
  2131. Every function in this hook will be called with one argument: the
  2132. back-end currently used, as a symbol.")
  2133. ;;;; Special Filters
  2134. (defvar org-export-filter-options-functions nil
  2135. "List of functions applied to the export options.
  2136. Each filter is called with two arguments: the export options, as
  2137. a plist, and the back-end, as a symbol. It must return
  2138. a property list containing export options.")
  2139. (defvar org-export-filter-parse-tree-functions nil
  2140. "List of functions applied to the parsed tree.
  2141. Each filter is called with three arguments: the parse tree, as
  2142. returned by `org-element-parse-buffer', the back-end, as
  2143. a symbol, and the communication channel, as a plist. It must
  2144. return the modified parse tree to transcode.")
  2145. (defvar org-export-filter-plain-text-functions nil
  2146. "List of functions applied to plain text.
  2147. Each filter is called with three arguments: a string which
  2148. contains no Org syntax, the back-end, as a symbol, and the
  2149. communication channel, as a plist. It must return a string or
  2150. nil.")
  2151. (defvar org-export-filter-final-output-functions nil
  2152. "List of functions applied to the transcoded string.
  2153. Each filter is called with three arguments: the full transcoded
  2154. string, the back-end, as a symbol, and the communication channel,
  2155. as a plist. It must return a string that will be used as the
  2156. final export output.")
  2157. ;;;; Elements Filters
  2158. (defvar org-export-filter-babel-call-functions nil
  2159. "List of functions applied to a transcoded babel-call.
  2160. Each filter is called with three arguments: the transcoded data,
  2161. as a string, the back-end, as a symbol, and the communication
  2162. channel, as a plist. It must return a string or nil.")
  2163. (defvar org-export-filter-center-block-functions nil
  2164. "List of functions applied to a transcoded center block.
  2165. Each filter is called with three arguments: the transcoded data,
  2166. as a string, the back-end, as a symbol, and the communication
  2167. channel, as a plist. It must return a string or nil.")
  2168. (defvar org-export-filter-clock-functions nil
  2169. "List of functions applied to a transcoded clock.
  2170. Each filter is called with three arguments: the transcoded data,
  2171. as a string, the back-end, as a symbol, and the communication
  2172. channel, as a plist. It must return a string or nil.")
  2173. (defvar org-export-filter-comment-functions nil
  2174. "List of functions applied to a transcoded comment.
  2175. Each filter is called with three arguments: the transcoded data,
  2176. as a string, the back-end, as a symbol, and the communication
  2177. channel, as a plist. It must return a string or nil.")
  2178. (defvar org-export-filter-comment-block-functions nil
  2179. "List of functions applied to a transcoded comment-block.
  2180. Each filter is called with three arguments: the transcoded data,
  2181. as a string, the back-end, as a symbol, and the communication
  2182. channel, as a plist. It must return a string or nil.")
  2183. (defvar org-export-filter-diary-sexp-functions nil
  2184. "List of functions applied to a transcoded diary-sexp.
  2185. Each filter is called with three arguments: the transcoded data,
  2186. as a string, the back-end, as a symbol, and the communication
  2187. channel, as a plist. It must return a string or nil.")
  2188. (defvar org-export-filter-drawer-functions nil
  2189. "List of functions applied to a transcoded drawer.
  2190. Each filter is called with three arguments: the transcoded data,
  2191. as a string, the back-end, as a symbol, and the communication
  2192. channel, as a plist. It must return a string or nil.")
  2193. (defvar org-export-filter-dynamic-block-functions nil
  2194. "List of functions applied to a transcoded dynamic-block.
  2195. Each filter is called with three arguments: the transcoded data,
  2196. as a string, the back-end, as a symbol, and the communication
  2197. channel, as a plist. It must return a string or nil.")
  2198. (defvar org-export-filter-example-block-functions nil
  2199. "List of functions applied to a transcoded example-block.
  2200. Each filter is called with three arguments: the transcoded data,
  2201. as a string, the back-end, as a symbol, and the communication
  2202. channel, as a plist. It must return a string or nil.")
  2203. (defvar org-export-filter-export-block-functions nil
  2204. "List of functions applied to a transcoded export-block.
  2205. Each filter is called with three arguments: the transcoded data,
  2206. as a string, the back-end, as a symbol, and the communication
  2207. channel, as a plist. It must return a string or nil.")
  2208. (defvar org-export-filter-fixed-width-functions nil
  2209. "List of functions applied to a transcoded fixed-width.
  2210. Each filter is called with three arguments: the transcoded data,
  2211. as a string, the back-end, as a symbol, and the communication
  2212. channel, as a plist. It must return a string or nil.")
  2213. (defvar org-export-filter-footnote-definition-functions nil
  2214. "List of functions applied to a transcoded footnote-definition.
  2215. Each filter is called with three arguments: the transcoded data,
  2216. as a string, the back-end, as a symbol, and the communication
  2217. channel, as a plist. It must return a string or nil.")
  2218. (defvar org-export-filter-headline-functions nil
  2219. "List of functions applied to a transcoded headline.
  2220. Each filter is called with three arguments: the transcoded data,
  2221. as a string, the back-end, as a symbol, and the communication
  2222. channel, as a plist. It must return a string or nil.")
  2223. (defvar org-export-filter-horizontal-rule-functions nil
  2224. "List of functions applied to a transcoded horizontal-rule.
  2225. Each filter is called with three arguments: the transcoded data,
  2226. as a string, the back-end, as a symbol, and the communication
  2227. channel, as a plist. It must return a string or nil.")
  2228. (defvar org-export-filter-inlinetask-functions nil
  2229. "List of functions applied to a transcoded inlinetask.
  2230. Each filter is called with three arguments: the transcoded data,
  2231. as a string, the back-end, as a symbol, and the communication
  2232. channel, as a plist. It must return a string or nil.")
  2233. (defvar org-export-filter-item-functions nil
  2234. "List of functions applied to a transcoded item.
  2235. Each filter is called with three arguments: the transcoded data,
  2236. as a string, the back-end, as a symbol, and the communication
  2237. channel, as a plist. It must return a string or nil.")
  2238. (defvar org-export-filter-keyword-functions nil
  2239. "List of functions applied to a transcoded keyword.
  2240. Each filter is called with three arguments: the transcoded data,
  2241. as a string, the back-end, as a symbol, and the communication
  2242. channel, as a plist. It must return a string or nil.")
  2243. (defvar org-export-filter-latex-environment-functions nil
  2244. "List of functions applied to a transcoded latex-environment.
  2245. Each filter is called with three arguments: the transcoded data,
  2246. as a string, the back-end, as a symbol, and the communication
  2247. channel, as a plist. It must return a string or nil.")
  2248. (defvar org-export-filter-node-property-functions nil
  2249. "List of functions applied to a transcoded node-property.
  2250. Each filter is called with three arguments: the transcoded data,
  2251. as a string, the back-end, as a symbol, and the communication
  2252. channel, as a plist. It must return a string or nil.")
  2253. (defvar org-export-filter-paragraph-functions nil
  2254. "List of functions applied to a transcoded paragraph.
  2255. Each filter is called with three arguments: the transcoded data,
  2256. as a string, the back-end, as a symbol, and the communication
  2257. channel, as a plist. It must return a string or nil.")
  2258. (defvar org-export-filter-plain-list-functions nil
  2259. "List of functions applied to a transcoded plain-list.
  2260. Each filter is called with three arguments: the transcoded data,
  2261. as a string, the back-end, as a symbol, and the communication
  2262. channel, as a plist. It must return a string or nil.")
  2263. (defvar org-export-filter-planning-functions nil
  2264. "List of functions applied to a transcoded planning.
  2265. Each filter is called with three arguments: the transcoded data,
  2266. as a string, the back-end, as a symbol, and the communication
  2267. channel, as a plist. It must return a string or nil.")
  2268. (defvar org-export-filter-property-drawer-functions nil
  2269. "List of functions applied to a transcoded property-drawer.
  2270. Each filter is called with three arguments: the transcoded data,
  2271. as a string, the back-end, as a symbol, and the communication
  2272. channel, as a plist. It must return a string or nil.")
  2273. (defvar org-export-filter-quote-block-functions nil
  2274. "List of functions applied to a transcoded quote block.
  2275. Each filter is called with three arguments: the transcoded quote
  2276. data, as a string, the back-end, as a symbol, and the
  2277. communication channel, as a plist. It must return a string or
  2278. nil.")
  2279. (defvar org-export-filter-quote-section-functions nil
  2280. "List of functions applied to a transcoded quote-section.
  2281. Each filter is called with three arguments: the transcoded data,
  2282. as a string, the back-end, as a symbol, and the communication
  2283. channel, as a plist. It must return a string or nil.")
  2284. (defvar org-export-filter-section-functions nil
  2285. "List of functions applied to a transcoded section.
  2286. Each filter is called with three arguments: the transcoded data,
  2287. as a string, the back-end, as a symbol, and the communication
  2288. channel, as a plist. It must return a string or nil.")
  2289. (defvar org-export-filter-special-block-functions nil
  2290. "List of functions applied to a transcoded special block.
  2291. Each filter is called with three arguments: the transcoded data,
  2292. as a string, the back-end, as a symbol, and the communication
  2293. channel, as a plist. It must return a string or nil.")
  2294. (defvar org-export-filter-src-block-functions nil
  2295. "List of functions applied to a transcoded src-block.
  2296. Each filter is called with three arguments: the transcoded data,
  2297. as a string, the back-end, as a symbol, and the communication
  2298. channel, as a plist. It must return a string or nil.")
  2299. (defvar org-export-filter-table-functions nil
  2300. "List of functions applied to a transcoded table.
  2301. Each filter is called with three arguments: the transcoded data,
  2302. as a string, the back-end, as a symbol, and the communication
  2303. channel, as a plist. It must return a string or nil.")
  2304. (defvar org-export-filter-table-cell-functions nil
  2305. "List of functions applied to a transcoded table-cell.
  2306. Each filter is called with three arguments: the transcoded data,
  2307. as a string, the back-end, as a symbol, and the communication
  2308. channel, as a plist. It must return a string or nil.")
  2309. (defvar org-export-filter-table-row-functions nil
  2310. "List of functions applied to a transcoded table-row.
  2311. Each filter is called with three arguments: the transcoded data,
  2312. as a string, the back-end, as a symbol, and the communication
  2313. channel, as a plist. It must return a string or nil.")
  2314. (defvar org-export-filter-verse-block-functions nil
  2315. "List of functions applied to a transcoded verse block.
  2316. Each filter is called with three arguments: the transcoded data,
  2317. as a string, the back-end, as a symbol, and the communication
  2318. channel, as a plist. It must return a string or nil.")
  2319. ;;;; Objects Filters
  2320. (defvar org-export-filter-bold-functions nil
  2321. "List of functions applied to transcoded bold text.
  2322. Each filter is called with three arguments: the transcoded data,
  2323. as a string, the back-end, as a symbol, and the communication
  2324. channel, as a plist. It must return a string or nil.")
  2325. (defvar org-export-filter-code-functions nil
  2326. "List of functions applied to transcoded code text.
  2327. Each filter is called with three arguments: the transcoded data,
  2328. as a string, the back-end, as a symbol, and the communication
  2329. channel, as a plist. It must return a string or nil.")
  2330. (defvar org-export-filter-entity-functions nil
  2331. "List of functions applied to a transcoded entity.
  2332. Each filter is called with three arguments: the transcoded data,
  2333. as a string, the back-end, as a symbol, and the communication
  2334. channel, as a plist. It must return a string or nil.")
  2335. (defvar org-export-filter-export-snippet-functions nil
  2336. "List of functions applied to a transcoded export-snippet.
  2337. Each filter is called with three arguments: the transcoded data,
  2338. as a string, the back-end, as a symbol, and the communication
  2339. channel, as a plist. It must return a string or nil.")
  2340. (defvar org-export-filter-footnote-reference-functions nil
  2341. "List of functions applied to a transcoded footnote-reference.
  2342. Each filter is called with three arguments: the transcoded data,
  2343. as a string, the back-end, as a symbol, and the communication
  2344. channel, as a plist. It must return a string or nil.")
  2345. (defvar org-export-filter-inline-babel-call-functions nil
  2346. "List of functions applied to a transcoded inline-babel-call.
  2347. Each filter is called with three arguments: the transcoded data,
  2348. as a string, the back-end, as a symbol, and the communication
  2349. channel, as a plist. It must return a string or nil.")
  2350. (defvar org-export-filter-inline-src-block-functions nil
  2351. "List of functions applied to a transcoded inline-src-block.
  2352. Each filter is called with three arguments: the transcoded data,
  2353. as a string, the back-end, as a symbol, and the communication
  2354. channel, as a plist. It must return a string or nil.")
  2355. (defvar org-export-filter-italic-functions nil
  2356. "List of functions applied to transcoded italic text.
  2357. Each filter is called with three arguments: the transcoded data,
  2358. as a string, the back-end, as a symbol, and the communication
  2359. channel, as a plist. It must return a string or nil.")
  2360. (defvar org-export-filter-latex-fragment-functions nil
  2361. "List of functions applied to a transcoded latex-fragment.
  2362. Each filter is called with three arguments: the transcoded data,
  2363. as a string, the back-end, as a symbol, and the communication
  2364. channel, as a plist. It must return a string or nil.")
  2365. (defvar org-export-filter-line-break-functions nil
  2366. "List of functions applied to a transcoded line-break.
  2367. Each filter is called with three arguments: the transcoded data,
  2368. as a string, the back-end, as a symbol, and the communication
  2369. channel, as a plist. It must return a string or nil.")
  2370. (defvar org-export-filter-link-functions nil
  2371. "List of functions applied to a transcoded link.
  2372. Each filter is called with three arguments: the transcoded data,
  2373. as a string, the back-end, as a symbol, and the communication
  2374. channel, as a plist. It must return a string or nil.")
  2375. (defvar org-export-filter-radio-target-functions nil
  2376. "List of functions applied to a transcoded radio-target.
  2377. Each filter is called with three arguments: the transcoded data,
  2378. as a string, the back-end, as a symbol, and the communication
  2379. channel, as a plist. It must return a string or nil.")
  2380. (defvar org-export-filter-statistics-cookie-functions nil
  2381. "List of functions applied to a transcoded statistics-cookie.
  2382. Each filter is called with three arguments: the transcoded data,
  2383. as a string, the back-end, as a symbol, and the communication
  2384. channel, as a plist. It must return a string or nil.")
  2385. (defvar org-export-filter-strike-through-functions nil
  2386. "List of functions applied to transcoded strike-through text.
  2387. Each filter is called with three arguments: the transcoded data,
  2388. as a string, the back-end, as a symbol, and the communication
  2389. channel, as a plist. It must return a string or nil.")
  2390. (defvar org-export-filter-subscript-functions nil
  2391. "List of functions applied to a transcoded subscript.
  2392. Each filter is called with three arguments: the transcoded data,
  2393. as a string, the back-end, as a symbol, and the communication
  2394. channel, as a plist. It must return a string or nil.")
  2395. (defvar org-export-filter-superscript-functions nil
  2396. "List of functions applied to a transcoded superscript.
  2397. Each filter is called with three arguments: the transcoded data,
  2398. as a string, the back-end, as a symbol, and the communication
  2399. channel, as a plist. It must return a string or nil.")
  2400. (defvar org-export-filter-target-functions nil
  2401. "List of functions applied to a transcoded target.
  2402. Each filter is called with three arguments: the transcoded data,
  2403. as a string, the back-end, as a symbol, and the communication
  2404. channel, as a plist. It must return a string or nil.")
  2405. (defvar org-export-filter-timestamp-functions nil
  2406. "List of functions applied to a transcoded timestamp.
  2407. Each filter is called with three arguments: the transcoded data,
  2408. as a string, the back-end, as a symbol, and the communication
  2409. channel, as a plist. It must return a string or nil.")
  2410. (defvar org-export-filter-underline-functions nil
  2411. "List of functions applied to transcoded underline text.
  2412. Each filter is called with three arguments: the transcoded data,
  2413. as a string, the back-end, as a symbol, and the communication
  2414. channel, as a plist. It must return a string or nil.")
  2415. (defvar org-export-filter-verbatim-functions nil
  2416. "List of functions applied to transcoded verbatim text.
  2417. Each filter is called with three arguments: the transcoded data,
  2418. as a string, the back-end, as a symbol, and the communication
  2419. channel, as a plist. It must return a string or nil.")
  2420. ;;;; Filters Tools
  2421. ;;
  2422. ;; Internal function `org-export-install-filters' installs filters
  2423. ;; hard-coded in back-ends (developer filters) and filters from global
  2424. ;; variables (user filters) in the communication channel.
  2425. ;;
  2426. ;; Internal function `org-export-filter-apply-functions' takes care
  2427. ;; about applying each filter in order to a given data. It ignores
  2428. ;; filters returning a nil value but stops whenever a filter returns
  2429. ;; an empty string.
  2430. (defun org-export-filter-apply-functions (filters value info)
  2431. "Call every function in FILTERS.
  2432. Functions are called with arguments VALUE, current export
  2433. back-end's name and INFO. A function returning a nil value will
  2434. be skipped. If it returns the empty string, the process ends and
  2435. VALUE is ignored.
  2436. Call is done in a LIFO fashion, to be sure that developer
  2437. specified filters, if any, are called first."
  2438. (catch 'exit
  2439. (let* ((backend (plist-get info :back-end))
  2440. (backend-name (and backend (org-export-backend-name backend))))
  2441. (dolist (filter filters value)
  2442. (let ((result (funcall filter value backend-name info)))
  2443. (cond ((not result) value)
  2444. ((equal value "") (throw 'exit nil))
  2445. (t (setq value result))))))))
  2446. (defun org-export-install-filters (info)
  2447. "Install filters properties in communication channel.
  2448. INFO is a plist containing the current communication channel.
  2449. Return the updated communication channel."
  2450. (let (plist)
  2451. ;; Install user-defined filters with `org-export-filters-alist'
  2452. ;; and filters already in INFO (through ext-plist mechanism).
  2453. (mapc (lambda (p)
  2454. (let* ((prop (car p))
  2455. (info-value (plist-get info prop))
  2456. (default-value (symbol-value (cdr p))))
  2457. (setq plist
  2458. (plist-put plist prop
  2459. ;; Filters in INFO will be called
  2460. ;; before those user provided.
  2461. (append (if (listp info-value) info-value
  2462. (list info-value))
  2463. default-value)))))
  2464. org-export-filters-alist)
  2465. ;; Prepend back-end specific filters to that list.
  2466. (mapc (lambda (p)
  2467. ;; Single values get consed, lists are appended.
  2468. (let ((key (car p)) (value (cdr p)))
  2469. (when value
  2470. (setq plist
  2471. (plist-put
  2472. plist key
  2473. (if (atom value) (cons value (plist-get plist key))
  2474. (append value (plist-get plist key))))))))
  2475. (org-export-get-all-filters (plist-get info :back-end)))
  2476. ;; Return new communication channel.
  2477. (org-combine-plists info plist)))
  2478. ;;; Core functions
  2479. ;;
  2480. ;; This is the room for the main function, `org-export-as', along with
  2481. ;; its derivative, `org-export-string-as'.
  2482. ;; `org-export--copy-to-kill-ring-p' determines if output of these
  2483. ;; function should be added to kill ring.
  2484. ;;
  2485. ;; Note that `org-export-as' doesn't really parse the current buffer,
  2486. ;; but a copy of it (with the same buffer-local variables and
  2487. ;; visibility), where macros and include keywords are expanded and
  2488. ;; Babel blocks are executed, if appropriate.
  2489. ;; `org-export-with-buffer-copy' macro prepares that copy.
  2490. ;;
  2491. ;; File inclusion is taken care of by
  2492. ;; `org-export-expand-include-keyword' and
  2493. ;; `org-export--prepare-file-contents'. Structure wise, including
  2494. ;; a whole Org file in a buffer often makes little sense. For
  2495. ;; example, if the file contains a headline and the include keyword
  2496. ;; was within an item, the item should contain the headline. That's
  2497. ;; why file inclusion should be done before any structure can be
  2498. ;; associated to the file, that is before parsing.
  2499. ;;
  2500. ;; `org-export-insert-default-template' is a command to insert
  2501. ;; a default template (or a back-end specific template) at point or in
  2502. ;; current subtree.
  2503. (defun org-export-copy-buffer ()
  2504. "Return a copy of the current buffer.
  2505. The copy preserves Org buffer-local variables, visibility and
  2506. narrowing."
  2507. (let ((copy-buffer-fun (org-export--generate-copy-script (current-buffer)))
  2508. (new-buf (generate-new-buffer (buffer-name))))
  2509. (with-current-buffer new-buf
  2510. (funcall copy-buffer-fun)
  2511. (set-buffer-modified-p nil))
  2512. new-buf))
  2513. (defmacro org-export-with-buffer-copy (&rest body)
  2514. "Apply BODY in a copy of the current buffer.
  2515. The copy preserves local variables, visibility and contents of
  2516. the original buffer. Point is at the beginning of the buffer
  2517. when BODY is applied."
  2518. (declare (debug t))
  2519. (org-with-gensyms (buf-copy)
  2520. `(let ((,buf-copy (org-export-copy-buffer)))
  2521. (unwind-protect
  2522. (with-current-buffer ,buf-copy
  2523. (goto-char (point-min))
  2524. (progn ,@body))
  2525. (and (buffer-live-p ,buf-copy)
  2526. ;; Kill copy without confirmation.
  2527. (progn (with-current-buffer ,buf-copy
  2528. (restore-buffer-modified-p nil))
  2529. (kill-buffer ,buf-copy)))))))
  2530. (defun org-export--generate-copy-script (buffer)
  2531. "Generate a function duplicating BUFFER.
  2532. The copy will preserve local variables, visibility, contents and
  2533. narrowing of the original buffer. If a region was active in
  2534. BUFFER, contents will be narrowed to that region instead.
  2535. The resulting function can be evaluated at a later time, from
  2536. another buffer, effectively cloning the original buffer there.
  2537. The function assumes BUFFER's major mode is `org-mode'."
  2538. (with-current-buffer buffer
  2539. `(lambda ()
  2540. (let ((inhibit-modification-hooks t))
  2541. ;; Set major mode. Ignore `org-mode-hook' as it has been run
  2542. ;; already in BUFFER.
  2543. (let ((org-mode-hook nil) (org-inhibit-startup t)) (org-mode))
  2544. ;; Copy specific buffer local variables and variables set
  2545. ;; through BIND keywords.
  2546. ,@(let ((bound-variables (org-export--list-bound-variables))
  2547. vars)
  2548. (dolist (entry (buffer-local-variables (buffer-base-buffer)) vars)
  2549. (when (consp entry)
  2550. (let ((var (car entry))
  2551. (val (cdr entry)))
  2552. (and (not (eq var 'org-font-lock-keywords))
  2553. (or (memq var
  2554. '(default-directory
  2555. buffer-file-name
  2556. buffer-file-coding-system))
  2557. (assq var bound-variables)
  2558. (string-match "^\\(org-\\|orgtbl-\\)"
  2559. (symbol-name var)))
  2560. ;; Skip unreadable values, as they cannot be
  2561. ;; sent to external process.
  2562. (or (not val) (ignore-errors (read (format "%S" val))))
  2563. (push `(set (make-local-variable (quote ,var))
  2564. (quote ,val))
  2565. vars))))))
  2566. ;; Whole buffer contents.
  2567. (insert
  2568. ,(org-with-wide-buffer
  2569. (buffer-substring-no-properties
  2570. (point-min) (point-max))))
  2571. ;; Narrowing.
  2572. ,(if (org-region-active-p)
  2573. `(narrow-to-region ,(region-beginning) ,(region-end))
  2574. `(narrow-to-region ,(point-min) ,(point-max)))
  2575. ;; Current position of point.
  2576. (goto-char ,(point))
  2577. ;; Overlays with invisible property.
  2578. ,@(let (ov-set)
  2579. (mapc
  2580. (lambda (ov)
  2581. (let ((invis-prop (overlay-get ov 'invisible)))
  2582. (when invis-prop
  2583. (push `(overlay-put
  2584. (make-overlay ,(overlay-start ov)
  2585. ,(overlay-end ov))
  2586. 'invisible (quote ,invis-prop))
  2587. ov-set))))
  2588. (overlays-in (point-min) (point-max)))
  2589. ov-set)))))
  2590. ;;;###autoload
  2591. (defun org-export-as
  2592. (backend &optional subtreep visible-only body-only ext-plist)
  2593. "Transcode current Org buffer into BACKEND code.
  2594. BACKEND is either an export back-end, as returned by, e.g.,
  2595. `org-export-create-backend', or a symbol referring to
  2596. a registered back-end.
  2597. If narrowing is active in the current buffer, only transcode its
  2598. narrowed part.
  2599. If a region is active, transcode that region.
  2600. When optional argument SUBTREEP is non-nil, transcode the
  2601. sub-tree at point, extracting information from the headline
  2602. properties first.
  2603. When optional argument VISIBLE-ONLY is non-nil, don't export
  2604. contents of hidden elements.
  2605. When optional argument BODY-ONLY is non-nil, only return body
  2606. code, without surrounding template.
  2607. Optional argument EXT-PLIST, when provided, is a property list
  2608. with external parameters overriding Org default settings, but
  2609. still inferior to file-local settings.
  2610. Return code as a string."
  2611. (when (symbolp backend) (setq backend (org-export-get-backend backend)))
  2612. (org-export-barf-if-invalid-backend backend)
  2613. (save-excursion
  2614. (save-restriction
  2615. ;; Narrow buffer to an appropriate region or subtree for
  2616. ;; parsing. If parsing subtree, be sure to remove main headline
  2617. ;; too.
  2618. (cond ((org-region-active-p)
  2619. (narrow-to-region (region-beginning) (region-end)))
  2620. (subtreep
  2621. (org-narrow-to-subtree)
  2622. (goto-char (point-min))
  2623. (forward-line)
  2624. (narrow-to-region (point) (point-max))))
  2625. ;; Initialize communication channel with original buffer
  2626. ;; attributes, unavailable in its copy.
  2627. (let* ((org-export-current-backend (org-export-backend-name backend))
  2628. (info (org-combine-plists
  2629. (list :export-options
  2630. (delq nil
  2631. (list (and subtreep 'subtree)
  2632. (and visible-only 'visible-only)
  2633. (and body-only 'body-only))))
  2634. (org-export--get-buffer-attributes)))
  2635. tree)
  2636. ;; Update communication channel and get parse tree. Buffer
  2637. ;; isn't parsed directly. Instead, a temporary copy is
  2638. ;; created, where include keywords, macros are expanded and
  2639. ;; code blocks are evaluated.
  2640. (org-export-with-buffer-copy
  2641. ;; Run first hook with current back-end's name as argument.
  2642. (run-hook-with-args 'org-export-before-processing-hook
  2643. (org-export-backend-name backend))
  2644. (org-export-expand-include-keyword)
  2645. ;; Update macro templates since #+INCLUDE keywords might have
  2646. ;; added some new ones.
  2647. (org-macro-initialize-templates)
  2648. (org-macro-replace-all org-macro-templates)
  2649. (org-export-execute-babel-code)
  2650. ;; Update radio targets since keyword inclusion might have
  2651. ;; added some more.
  2652. (org-update-radio-target-regexp)
  2653. ;; Run last hook with current back-end's name as argument.
  2654. (goto-char (point-min))
  2655. (save-excursion
  2656. (run-hook-with-args 'org-export-before-parsing-hook
  2657. (org-export-backend-name backend)))
  2658. ;; Update communication channel with environment. Also
  2659. ;; install user's and developer's filters.
  2660. (setq info
  2661. (org-export-install-filters
  2662. (org-combine-plists
  2663. info (org-export-get-environment backend subtreep ext-plist))))
  2664. ;; Expand export-specific set of macros: {{{author}}},
  2665. ;; {{{date}}}, {{{email}}} and {{{title}}}. It must be done
  2666. ;; once regular macros have been expanded, since document
  2667. ;; keywords may contain one of them.
  2668. (org-macro-replace-all
  2669. (list (cons "author"
  2670. (org-element-interpret-data (plist-get info :author)))
  2671. (cons "date"
  2672. (org-element-interpret-data (plist-get info :date)))
  2673. ;; EMAIL is not a parsed keyword: store it as-is.
  2674. (cons "email" (or (plist-get info :email) ""))
  2675. (cons "title"
  2676. (org-element-interpret-data (plist-get info :title)))))
  2677. ;; Call options filters and update export options. We do not
  2678. ;; use `org-export-filter-apply-functions' here since the
  2679. ;; arity of such filters is different.
  2680. (let ((backend-name (org-export-backend-name backend)))
  2681. (dolist (filter (plist-get info :filter-options))
  2682. (let ((result (funcall filter info backend-name)))
  2683. (when result (setq info result)))))
  2684. ;; Parse buffer and call parse-tree filter on it.
  2685. (setq tree
  2686. (org-export-filter-apply-functions
  2687. (plist-get info :filter-parse-tree)
  2688. (org-element-parse-buffer nil visible-only) info))
  2689. ;; Now tree is complete, compute its properties and add them
  2690. ;; to communication channel.
  2691. (setq info
  2692. (org-combine-plists
  2693. info (org-export-collect-tree-properties tree info)))
  2694. ;; Eventually transcode TREE. Wrap the resulting string into
  2695. ;; a template.
  2696. (let* ((body (org-element-normalize-string
  2697. (or (org-export-data tree info) "")))
  2698. (inner-template (cdr (assq 'inner-template
  2699. (plist-get info :translate-alist))))
  2700. (full-body (if (not (functionp inner-template)) body
  2701. (funcall inner-template body info)))
  2702. (template (cdr (assq 'template
  2703. (plist-get info :translate-alist)))))
  2704. ;; Remove all text properties since they cannot be
  2705. ;; retrieved from an external process. Finally call
  2706. ;; final-output filter and return result.
  2707. (org-no-properties
  2708. (org-export-filter-apply-functions
  2709. (plist-get info :filter-final-output)
  2710. (if (or (not (functionp template)) body-only) full-body
  2711. (funcall template full-body info))
  2712. info))))))))
  2713. ;;;###autoload
  2714. (defun org-export-string-as (string backend &optional body-only ext-plist)
  2715. "Transcode STRING into BACKEND code.
  2716. BACKEND is either an export back-end, as returned by, e.g.,
  2717. `org-export-create-backend', or a symbol referring to
  2718. a registered back-end.
  2719. When optional argument BODY-ONLY is non-nil, only return body
  2720. code, without preamble nor postamble.
  2721. Optional argument EXT-PLIST, when provided, is a property list
  2722. with external parameters overriding Org default settings, but
  2723. still inferior to file-local settings.
  2724. Return code as a string."
  2725. (with-temp-buffer
  2726. (insert string)
  2727. (let ((org-inhibit-startup t)) (org-mode))
  2728. (org-export-as backend nil nil body-only ext-plist)))
  2729. ;;;###autoload
  2730. (defun org-export-replace-region-by (backend)
  2731. "Replace the active region by its export to BACKEND.
  2732. BACKEND is either an export back-end, as returned by, e.g.,
  2733. `org-export-create-backend', or a symbol referring to
  2734. a registered back-end."
  2735. (if (not (org-region-active-p))
  2736. (user-error "No active region to replace")
  2737. (let* ((beg (region-beginning))
  2738. (end (region-end))
  2739. (str (buffer-substring beg end)) rpl)
  2740. (setq rpl (org-export-string-as str backend t))
  2741. (delete-region beg end)
  2742. (insert rpl))))
  2743. ;;;###autoload
  2744. (defun org-export-insert-default-template (&optional backend subtreep)
  2745. "Insert all export keywords with default values at beginning of line.
  2746. BACKEND is a symbol referring to the name of a registered export
  2747. back-end, for which specific export options should be added to
  2748. the template, or `default' for default template. When it is nil,
  2749. the user will be prompted for a category.
  2750. If SUBTREEP is non-nil, export configuration will be set up
  2751. locally for the subtree through node properties."
  2752. (interactive)
  2753. (unless (derived-mode-p 'org-mode) (user-error "Not in an Org mode buffer"))
  2754. (when (and subtreep (org-before-first-heading-p))
  2755. (user-error "No subtree to set export options for"))
  2756. (let ((node (and subtreep (save-excursion (org-back-to-heading t) (point))))
  2757. (backend
  2758. (or backend
  2759. (intern
  2760. (org-completing-read
  2761. "Options category: "
  2762. (cons "default"
  2763. (mapcar (lambda (b)
  2764. (symbol-name (org-export-backend-name b)))
  2765. org-export--registered-backends))))))
  2766. options keywords)
  2767. ;; Populate OPTIONS and KEYWORDS.
  2768. (dolist (entry (cond ((eq backend 'default) org-export-options-alist)
  2769. ((org-export-backend-p backend)
  2770. (org-export-get-all-options backend))
  2771. (t (org-export-get-all-options
  2772. (org-export-get-backend backend)))))
  2773. (let ((keyword (nth 1 entry))
  2774. (option (nth 2 entry)))
  2775. (cond
  2776. (keyword (unless (assoc keyword keywords)
  2777. (let ((value
  2778. (if (eq (nth 4 entry) 'split)
  2779. (mapconcat 'identity (eval (nth 3 entry)) " ")
  2780. (eval (nth 3 entry)))))
  2781. (push (cons keyword value) keywords))))
  2782. (option (unless (assoc option options)
  2783. (push (cons option (eval (nth 3 entry))) options))))))
  2784. ;; Move to an appropriate location in order to insert options.
  2785. (unless subtreep (beginning-of-line))
  2786. ;; First get TITLE, DATE, AUTHOR and EMAIL if they belong to the
  2787. ;; list of available keywords.
  2788. (when (assoc "TITLE" keywords)
  2789. (let ((title
  2790. (or (let ((visited-file (buffer-file-name (buffer-base-buffer))))
  2791. (and visited-file
  2792. (file-name-sans-extension
  2793. (file-name-nondirectory visited-file))))
  2794. (buffer-name (buffer-base-buffer)))))
  2795. (if (not subtreep) (insert (format "#+TITLE: %s\n" title))
  2796. (org-entry-put node "EXPORT_TITLE" title))))
  2797. (when (assoc "DATE" keywords)
  2798. (let ((date (with-temp-buffer (org-insert-time-stamp (current-time)))))
  2799. (if (not subtreep) (insert "#+DATE: " date "\n")
  2800. (org-entry-put node "EXPORT_DATE" date))))
  2801. (when (assoc "AUTHOR" keywords)
  2802. (let ((author (cdr (assoc "AUTHOR" keywords))))
  2803. (if subtreep (org-entry-put node "EXPORT_AUTHOR" author)
  2804. (insert
  2805. (format "#+AUTHOR:%s\n"
  2806. (if (not (org-string-nw-p author)) ""
  2807. (concat " " author)))))))
  2808. (when (assoc "EMAIL" keywords)
  2809. (let ((email (cdr (assoc "EMAIL" keywords))))
  2810. (if subtreep (org-entry-put node "EXPORT_EMAIL" email)
  2811. (insert
  2812. (format "#+EMAIL:%s\n"
  2813. (if (not (org-string-nw-p email)) ""
  2814. (concat " " email)))))))
  2815. ;; Then (multiple) OPTIONS lines. Never go past fill-column.
  2816. (when options
  2817. (let ((items
  2818. (mapcar
  2819. #'(lambda (opt) (format "%s:%S" (car opt) (cdr opt)))
  2820. (sort options (lambda (k1 k2) (string< (car k1) (car k2)))))))
  2821. (if subtreep
  2822. (org-entry-put
  2823. node "EXPORT_OPTIONS" (mapconcat 'identity items " "))
  2824. (while items
  2825. (insert "#+OPTIONS:")
  2826. (let ((width 10))
  2827. (while (and items
  2828. (< (+ width (length (car items)) 1) fill-column))
  2829. (let ((item (pop items)))
  2830. (insert " " item)
  2831. (incf width (1+ (length item))))))
  2832. (insert "\n")))))
  2833. ;; And the rest of keywords.
  2834. (dolist (key (sort keywords (lambda (k1 k2) (string< (car k1) (car k2)))))
  2835. (unless (member (car key) '("TITLE" "DATE" "AUTHOR" "EMAIL"))
  2836. (let ((val (cdr key)))
  2837. (if subtreep (org-entry-put node (concat "EXPORT_" (car key)) val)
  2838. (insert
  2839. (format "#+%s:%s\n"
  2840. (car key)
  2841. (if (org-string-nw-p val) (format " %s" val) "")))))))))
  2842. (defun org-export-expand-include-keyword (&optional included dir)
  2843. "Expand every include keyword in buffer.
  2844. Optional argument INCLUDED is a list of included file names along
  2845. with their line restriction, when appropriate. It is used to
  2846. avoid infinite recursion. Optional argument DIR is the current
  2847. working directory. It is used to properly resolve relative
  2848. paths."
  2849. (let ((case-fold-search t))
  2850. (goto-char (point-min))
  2851. (while (re-search-forward "^[ \t]*#\\+INCLUDE:" nil t)
  2852. (let ((element (save-match-data (org-element-at-point))))
  2853. (when (eq (org-element-type element) 'keyword)
  2854. (beginning-of-line)
  2855. ;; Extract arguments from keyword's value.
  2856. (let* ((value (org-element-property :value element))
  2857. (ind (org-get-indentation))
  2858. (file (and (string-match
  2859. "^\\(\".+?\"\\|\\S-+\\)\\(?:\\s-+\\|$\\)" value)
  2860. (prog1 (expand-file-name
  2861. (org-remove-double-quotes
  2862. (match-string 1 value))
  2863. dir)
  2864. (setq value (replace-match "" nil nil value)))))
  2865. (lines
  2866. (and (string-match
  2867. ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\""
  2868. value)
  2869. (prog1 (match-string 1 value)
  2870. (setq value (replace-match "" nil nil value)))))
  2871. (env (cond ((string-match "\\<example\\>" value) 'example)
  2872. ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
  2873. (match-string 1 value))))
  2874. ;; Minimal level of included file defaults to the child
  2875. ;; level of the current headline, if any, or one. It
  2876. ;; only applies is the file is meant to be included as
  2877. ;; an Org one.
  2878. (minlevel
  2879. (and (not env)
  2880. (if (string-match ":minlevel +\\([0-9]+\\)" value)
  2881. (prog1 (string-to-number (match-string 1 value))
  2882. (setq value (replace-match "" nil nil value)))
  2883. (let ((cur (org-current-level)))
  2884. (if cur (1+ (org-reduced-level cur)) 1))))))
  2885. ;; Remove keyword.
  2886. (delete-region (point) (progn (forward-line) (point)))
  2887. (cond
  2888. ((not file) nil)
  2889. ((not (file-readable-p file))
  2890. (error "Cannot include file %s" file))
  2891. ;; Check if files has already been parsed. Look after
  2892. ;; inclusion lines too, as different parts of the same file
  2893. ;; can be included too.
  2894. ((member (list file lines) included)
  2895. (error "Recursive file inclusion: %s" file))
  2896. (t
  2897. (cond
  2898. ((eq env 'example)
  2899. (insert
  2900. (let ((ind-str (make-string ind ? ))
  2901. (contents
  2902. (org-escape-code-in-string
  2903. (org-export--prepare-file-contents file lines))))
  2904. (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
  2905. ind-str contents ind-str))))
  2906. ((stringp env)
  2907. (insert
  2908. (let ((ind-str (make-string ind ? ))
  2909. (contents
  2910. (org-escape-code-in-string
  2911. (org-export--prepare-file-contents file lines))))
  2912. (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
  2913. ind-str env contents ind-str))))
  2914. (t
  2915. (insert
  2916. (with-temp-buffer
  2917. (let ((org-inhibit-startup t)) (org-mode))
  2918. (insert
  2919. (org-export--prepare-file-contents file lines ind minlevel))
  2920. (org-export-expand-include-keyword
  2921. (cons (list file lines) included)
  2922. (file-name-directory file))
  2923. (buffer-string)))))))))))))
  2924. (defun org-export--prepare-file-contents (file &optional lines ind minlevel)
  2925. "Prepare the contents of FILE for inclusion and return them as a string.
  2926. When optional argument LINES is a string specifying a range of
  2927. lines, include only those lines.
  2928. Optional argument IND, when non-nil, is an integer specifying the
  2929. global indentation of returned contents. Since its purpose is to
  2930. allow an included file to stay in the same environment it was
  2931. created \(i.e. a list item), it doesn't apply past the first
  2932. headline encountered.
  2933. Optional argument MINLEVEL, when non-nil, is an integer
  2934. specifying the level that any top-level headline in the included
  2935. file should have."
  2936. (with-temp-buffer
  2937. (insert-file-contents file)
  2938. (when lines
  2939. (let* ((lines (split-string lines "-"))
  2940. (lbeg (string-to-number (car lines)))
  2941. (lend (string-to-number (cadr lines)))
  2942. (beg (if (zerop lbeg) (point-min)
  2943. (goto-char (point-min))
  2944. (forward-line (1- lbeg))
  2945. (point)))
  2946. (end (if (zerop lend) (point-max)
  2947. (goto-char (point-min))
  2948. (forward-line (1- lend))
  2949. (point))))
  2950. (narrow-to-region beg end)))
  2951. ;; Remove blank lines at beginning and end of contents. The logic
  2952. ;; behind that removal is that blank lines around include keyword
  2953. ;; override blank lines in included file.
  2954. (goto-char (point-min))
  2955. (org-skip-whitespace)
  2956. (beginning-of-line)
  2957. (delete-region (point-min) (point))
  2958. (goto-char (point-max))
  2959. (skip-chars-backward " \r\t\n")
  2960. (forward-line)
  2961. (delete-region (point) (point-max))
  2962. ;; If IND is set, preserve indentation of include keyword until
  2963. ;; the first headline encountered.
  2964. (when ind
  2965. (unless (eq major-mode 'org-mode)
  2966. (let ((org-inhibit-startup t)) (org-mode)))
  2967. (goto-char (point-min))
  2968. (let ((ind-str (make-string ind ? )))
  2969. (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
  2970. ;; Do not move footnote definitions out of column 0.
  2971. (unless (and (looking-at org-footnote-definition-re)
  2972. (eq (org-element-type (org-element-at-point))
  2973. 'footnote-definition))
  2974. (insert ind-str))
  2975. (forward-line))))
  2976. ;; When MINLEVEL is specified, compute minimal level for headlines
  2977. ;; in the file (CUR-MIN), and remove stars to each headline so
  2978. ;; that headlines with minimal level have a level of MINLEVEL.
  2979. (when minlevel
  2980. (unless (eq major-mode 'org-mode)
  2981. (let ((org-inhibit-startup t)) (org-mode)))
  2982. (org-with-limited-levels
  2983. (let ((levels (org-map-entries
  2984. (lambda () (org-reduced-level (org-current-level))))))
  2985. (when levels
  2986. (let ((offset (- minlevel (apply 'min levels))))
  2987. (unless (zerop offset)
  2988. (when org-odd-levels-only (setq offset (* offset 2)))
  2989. ;; Only change stars, don't bother moving whole
  2990. ;; sections.
  2991. (org-map-entries
  2992. (lambda () (if (< offset 0) (delete-char (abs offset))
  2993. (insert (make-string offset ?*)))))))))))
  2994. (org-element-normalize-string (buffer-string))))
  2995. (defun org-export-execute-babel-code ()
  2996. "Execute every Babel code in the visible part of current buffer."
  2997. ;; Get a pristine copy of current buffer so Babel references can be
  2998. ;; properly resolved.
  2999. (let ((reference (org-export-copy-buffer)))
  3000. (unwind-protect (let ((org-current-export-file reference))
  3001. (org-babel-exp-process-buffer))
  3002. (kill-buffer reference))))
  3003. (defun org-export--copy-to-kill-ring-p ()
  3004. "Return a non-nil value when output should be added to the kill ring.
  3005. See also `org-export-copy-to-kill-ring'."
  3006. (if (eq org-export-copy-to-kill-ring 'if-interactive)
  3007. (not (or executing-kbd-macro noninteractive))
  3008. (eq org-export-copy-to-kill-ring t)))
  3009. ;;; Tools For Back-Ends
  3010. ;;
  3011. ;; A whole set of tools is available to help build new exporters. Any
  3012. ;; function general enough to have its use across many back-ends
  3013. ;; should be added here.
  3014. ;;;; For Affiliated Keywords
  3015. ;;
  3016. ;; `org-export-read-attribute' reads a property from a given element
  3017. ;; as a plist. It can be used to normalize affiliated keywords'
  3018. ;; syntax.
  3019. ;;
  3020. ;; Since captions can span over multiple lines and accept dual values,
  3021. ;; their internal representation is a bit tricky. Therefore,
  3022. ;; `org-export-get-caption' transparently returns a given element's
  3023. ;; caption as a secondary string.
  3024. (defun org-export-read-attribute (attribute element &optional property)
  3025. "Turn ATTRIBUTE property from ELEMENT into a plist.
  3026. When optional argument PROPERTY is non-nil, return the value of
  3027. that property within attributes.
  3028. This function assumes attributes are defined as \":keyword
  3029. value\" pairs. It is appropriate for `:attr_html' like
  3030. properties.
  3031. All values will become strings except the empty string and
  3032. \"nil\", which will become nil. Also, values containing only
  3033. double quotes will be read as-is, which means that \"\" value
  3034. will become the empty string."
  3035. (let* ((prepare-value
  3036. (lambda (str)
  3037. (save-match-data
  3038. (cond ((member str '(nil "" "nil")) nil)
  3039. ((string-match "^\"\\(\"+\\)?\"$" str)
  3040. (or (match-string 1 str) ""))
  3041. (t str)))))
  3042. (attributes
  3043. (let ((value (org-element-property attribute element)))
  3044. (when value
  3045. (let ((s (mapconcat 'identity value " ")) result)
  3046. (while (string-match
  3047. "\\(?:^\\|[ \t]+\\)\\(:[-a-zA-Z0-9_]+\\)\\([ \t]+\\|$\\)"
  3048. s)
  3049. (let ((value (substring s 0 (match-beginning 0))))
  3050. (push (funcall prepare-value value) result))
  3051. (push (intern (match-string 1 s)) result)
  3052. (setq s (substring s (match-end 0))))
  3053. ;; Ignore any string before first property with `cdr'.
  3054. (cdr (nreverse (cons (funcall prepare-value s) result))))))))
  3055. (if property (plist-get attributes property) attributes)))
  3056. (defun org-export-get-caption (element &optional shortp)
  3057. "Return caption from ELEMENT as a secondary string.
  3058. When optional argument SHORTP is non-nil, return short caption,
  3059. as a secondary string, instead.
  3060. Caption lines are separated by a white space."
  3061. (let ((full-caption (org-element-property :caption element)) caption)
  3062. (dolist (line full-caption (cdr caption))
  3063. (let ((cap (funcall (if shortp 'cdr 'car) line)))
  3064. (when cap
  3065. (setq caption (nconc (list " ") (copy-sequence cap) caption)))))))
  3066. ;;;; For Derived Back-ends
  3067. ;;
  3068. ;; `org-export-with-backend' is a function allowing to locally use
  3069. ;; another back-end to transcode some object or element. In a derived
  3070. ;; back-end, it may be used as a fall-back function once all specific
  3071. ;; cases have been treated.
  3072. (defun org-export-with-backend (backend data &optional contents info)
  3073. "Call a transcoder from BACKEND on DATA.
  3074. BACKEND is an export back-end, as returned by, e.g.,
  3075. `org-export-create-backend', or a symbol referring to
  3076. a registered back-end. DATA is an Org element, object, secondary
  3077. string or string. CONTENTS, when non-nil, is the transcoded
  3078. contents of DATA element, as a string. INFO, when non-nil, is
  3079. the communication channel used for export, as a plist."
  3080. (when (symbolp backend) (setq backend (org-export-get-backend backend)))
  3081. (org-export-barf-if-invalid-backend backend)
  3082. (let ((type (org-element-type data)))
  3083. (if (memq type '(nil org-data)) (error "No foreign transcoder available")
  3084. (let* ((all-transcoders (org-export-get-all-transcoders backend))
  3085. (transcoder (cdr (assq type all-transcoders))))
  3086. (if (not (functionp transcoder))
  3087. (error "No foreign transcoder available")
  3088. (funcall
  3089. transcoder data contents
  3090. (org-combine-plists
  3091. info (list :back-end backend
  3092. :translate-alist all-transcoders
  3093. :exported-data (make-hash-table :test 'eq :size 401)))))))))
  3094. ;;;; For Export Snippets
  3095. ;;
  3096. ;; Every export snippet is transmitted to the back-end. Though, the
  3097. ;; latter will only retain one type of export-snippet, ignoring
  3098. ;; others, based on the former's target back-end. The function
  3099. ;; `org-export-snippet-backend' returns that back-end for a given
  3100. ;; export-snippet.
  3101. (defun org-export-snippet-backend (export-snippet)
  3102. "Return EXPORT-SNIPPET targeted back-end as a symbol.
  3103. Translation, with `org-export-snippet-translation-alist', is
  3104. applied."
  3105. (let ((back-end (org-element-property :back-end export-snippet)))
  3106. (intern
  3107. (or (cdr (assoc back-end org-export-snippet-translation-alist))
  3108. back-end))))
  3109. ;;;; For Footnotes
  3110. ;;
  3111. ;; `org-export-collect-footnote-definitions' is a tool to list
  3112. ;; actually used footnotes definitions in the whole parse tree, or in
  3113. ;; a headline, in order to add footnote listings throughout the
  3114. ;; transcoded data.
  3115. ;;
  3116. ;; `org-export-footnote-first-reference-p' is a predicate used by some
  3117. ;; back-ends, when they need to attach the footnote definition only to
  3118. ;; the first occurrence of the corresponding label.
  3119. ;;
  3120. ;; `org-export-get-footnote-definition' and
  3121. ;; `org-export-get-footnote-number' provide easier access to
  3122. ;; additional information relative to a footnote reference.
  3123. (defun org-export-collect-footnote-definitions (data info)
  3124. "Return an alist between footnote numbers, labels and definitions.
  3125. DATA is the parse tree from which definitions are collected.
  3126. INFO is the plist used as a communication channel.
  3127. Definitions are sorted by order of references. They either
  3128. appear as Org data or as a secondary string for inlined
  3129. footnotes. Unreferenced definitions are ignored."
  3130. (let* (num-alist
  3131. collect-fn ; for byte-compiler.
  3132. (collect-fn
  3133. (function
  3134. (lambda (data)
  3135. ;; Collect footnote number, label and definition in DATA.
  3136. (org-element-map data 'footnote-reference
  3137. (lambda (fn)
  3138. (when (org-export-footnote-first-reference-p fn info)
  3139. (let ((def (org-export-get-footnote-definition fn info)))
  3140. (push
  3141. (list (org-export-get-footnote-number fn info)
  3142. (org-element-property :label fn)
  3143. def)
  3144. num-alist)
  3145. ;; Also search in definition for nested footnotes.
  3146. (when (eq (org-element-property :type fn) 'standard)
  3147. (funcall collect-fn def)))))
  3148. ;; Don't enter footnote definitions since it will happen
  3149. ;; when their first reference is found.
  3150. info nil 'footnote-definition)))))
  3151. (funcall collect-fn (plist-get info :parse-tree))
  3152. (reverse num-alist)))
  3153. (defun org-export-footnote-first-reference-p (footnote-reference info)
  3154. "Non-nil when a footnote reference is the first one for its label.
  3155. FOOTNOTE-REFERENCE is the footnote reference being considered.
  3156. INFO is the plist used as a communication channel."
  3157. (let ((label (org-element-property :label footnote-reference)))
  3158. ;; Anonymous footnotes are always a first reference.
  3159. (if (not label) t
  3160. ;; Otherwise, return the first footnote with the same LABEL and
  3161. ;; test if it is equal to FOOTNOTE-REFERENCE.
  3162. (let* (search-refs ; for byte-compiler.
  3163. (search-refs
  3164. (function
  3165. (lambda (data)
  3166. (org-element-map data 'footnote-reference
  3167. (lambda (fn)
  3168. (cond
  3169. ((string= (org-element-property :label fn) label)
  3170. (throw 'exit fn))
  3171. ;; If FN isn't inlined, be sure to traverse its
  3172. ;; definition before resuming search. See
  3173. ;; comments in `org-export-get-footnote-number'
  3174. ;; for more information.
  3175. ((eq (org-element-property :type fn) 'standard)
  3176. (funcall search-refs
  3177. (org-export-get-footnote-definition fn info)))))
  3178. ;; Don't enter footnote definitions since it will
  3179. ;; happen when their first reference is found.
  3180. info 'first-match 'footnote-definition)))))
  3181. (eq (catch 'exit (funcall search-refs (plist-get info :parse-tree)))
  3182. footnote-reference)))))
  3183. (defun org-export-get-footnote-definition (footnote-reference info)
  3184. "Return definition of FOOTNOTE-REFERENCE as parsed data.
  3185. INFO is the plist used as a communication channel. If no such
  3186. definition can be found, return the \"DEFINITION NOT FOUND\"
  3187. string."
  3188. (let ((label (org-element-property :label footnote-reference)))
  3189. (or (org-element-property :inline-definition footnote-reference)
  3190. (cdr (assoc label (plist-get info :footnote-definition-alist)))
  3191. "DEFINITION NOT FOUND.")))
  3192. (defun org-export-get-footnote-number (footnote info)
  3193. "Return number associated to a footnote.
  3194. FOOTNOTE is either a footnote reference or a footnote definition.
  3195. INFO is the plist used as a communication channel."
  3196. (let* ((label (org-element-property :label footnote))
  3197. seen-refs
  3198. search-ref ; For byte-compiler.
  3199. (search-ref
  3200. (function
  3201. (lambda (data)
  3202. ;; Search footnote references through DATA, filling
  3203. ;; SEEN-REFS along the way.
  3204. (org-element-map data 'footnote-reference
  3205. (lambda (fn)
  3206. (let ((fn-lbl (org-element-property :label fn)))
  3207. (cond
  3208. ;; Anonymous footnote match: return number.
  3209. ((and (not fn-lbl) (eq fn footnote))
  3210. (throw 'exit (1+ (length seen-refs))))
  3211. ;; Labels match: return number.
  3212. ((and label (string= label fn-lbl))
  3213. (throw 'exit (1+ (length seen-refs))))
  3214. ;; Anonymous footnote: it's always a new one.
  3215. ;; Also, be sure to return nil from the `cond' so
  3216. ;; `first-match' doesn't get us out of the loop.
  3217. ((not fn-lbl) (push 'inline seen-refs) nil)
  3218. ;; Label not seen so far: add it so SEEN-REFS.
  3219. ;;
  3220. ;; Also search for subsequent references in
  3221. ;; footnote definition so numbering follows
  3222. ;; reading logic. Note that we don't have to care
  3223. ;; about inline definitions, since
  3224. ;; `org-element-map' already traverses them at the
  3225. ;; right time.
  3226. ;;
  3227. ;; Once again, return nil to stay in the loop.
  3228. ((not (member fn-lbl seen-refs))
  3229. (push fn-lbl seen-refs)
  3230. (funcall search-ref
  3231. (org-export-get-footnote-definition fn info))
  3232. nil))))
  3233. ;; Don't enter footnote definitions since it will
  3234. ;; happen when their first reference is found.
  3235. info 'first-match 'footnote-definition)))))
  3236. (catch 'exit (funcall search-ref (plist-get info :parse-tree)))))
  3237. ;;;; For Headlines
  3238. ;;
  3239. ;; `org-export-get-relative-level' is a shortcut to get headline
  3240. ;; level, relatively to the lower headline level in the parsed tree.
  3241. ;;
  3242. ;; `org-export-get-headline-number' returns the section number of an
  3243. ;; headline, while `org-export-number-to-roman' allows to convert it
  3244. ;; to roman numbers.
  3245. ;;
  3246. ;; `org-export-low-level-p', `org-export-first-sibling-p' and
  3247. ;; `org-export-last-sibling-p' are three useful predicates when it
  3248. ;; comes to fulfill the `:headline-levels' property.
  3249. ;;
  3250. ;; `org-export-get-tags', `org-export-get-category' and
  3251. ;; `org-export-get-node-property' extract useful information from an
  3252. ;; headline or a parent headline. They all handle inheritance.
  3253. ;;
  3254. ;; `org-export-get-alt-title' tries to retrieve an alternative title,
  3255. ;; as a secondary string, suitable for table of contents. It falls
  3256. ;; back onto default title.
  3257. (defun org-export-get-relative-level (headline info)
  3258. "Return HEADLINE relative level within current parsed tree.
  3259. INFO is a plist holding contextual information."
  3260. (+ (org-element-property :level headline)
  3261. (or (plist-get info :headline-offset) 0)))
  3262. (defun org-export-low-level-p (headline info)
  3263. "Non-nil when HEADLINE is considered as low level.
  3264. INFO is a plist used as a communication channel.
  3265. A low level headlines has a relative level greater than
  3266. `:headline-levels' property value.
  3267. Return value is the difference between HEADLINE relative level
  3268. and the last level being considered as high enough, or nil."
  3269. (let ((limit (plist-get info :headline-levels)))
  3270. (when (wholenump limit)
  3271. (let ((level (org-export-get-relative-level headline info)))
  3272. (and (> level limit) (- level limit))))))
  3273. (defun org-export-get-headline-number (headline info)
  3274. "Return HEADLINE numbering as a list of numbers.
  3275. INFO is a plist holding contextual information."
  3276. (cdr (assoc headline (plist-get info :headline-numbering))))
  3277. (defun org-export-numbered-headline-p (headline info)
  3278. "Return a non-nil value if HEADLINE element should be numbered.
  3279. INFO is a plist used as a communication channel."
  3280. (let ((sec-num (plist-get info :section-numbers))
  3281. (level (org-export-get-relative-level headline info)))
  3282. (if (wholenump sec-num) (<= level sec-num) sec-num)))
  3283. (defun org-export-number-to-roman (n)
  3284. "Convert integer N into a roman numeral."
  3285. (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
  3286. ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
  3287. ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
  3288. ( 1 . "I")))
  3289. (res ""))
  3290. (if (<= n 0)
  3291. (number-to-string n)
  3292. (while roman
  3293. (if (>= n (caar roman))
  3294. (setq n (- n (caar roman))
  3295. res (concat res (cdar roman)))
  3296. (pop roman)))
  3297. res)))
  3298. (defun org-export-get-tags (element info &optional tags inherited)
  3299. "Return list of tags associated to ELEMENT.
  3300. ELEMENT has either an `headline' or an `inlinetask' type. INFO
  3301. is a plist used as a communication channel.
  3302. Select tags (see `org-export-select-tags') and exclude tags (see
  3303. `org-export-exclude-tags') are removed from the list.
  3304. When non-nil, optional argument TAGS should be a list of strings.
  3305. Any tag belonging to this list will also be removed.
  3306. When optional argument INHERITED is non-nil, tags can also be
  3307. inherited from parent headlines and FILETAGS keywords."
  3308. (org-remove-if
  3309. (lambda (tag) (or (member tag (plist-get info :select-tags))
  3310. (member tag (plist-get info :exclude-tags))
  3311. (member tag tags)))
  3312. (if (not inherited) (org-element-property :tags element)
  3313. ;; Build complete list of inherited tags.
  3314. (let ((current-tag-list (org-element-property :tags element)))
  3315. (mapc
  3316. (lambda (parent)
  3317. (mapc
  3318. (lambda (tag)
  3319. (when (and (memq (org-element-type parent) '(headline inlinetask))
  3320. (not (member tag current-tag-list)))
  3321. (push tag current-tag-list)))
  3322. (org-element-property :tags parent)))
  3323. (org-export-get-genealogy element))
  3324. ;; Add FILETAGS keywords and return results.
  3325. (org-uniquify (append (plist-get info :filetags) current-tag-list))))))
  3326. (defun org-export-get-node-property (property blob &optional inherited)
  3327. "Return node PROPERTY value for BLOB.
  3328. PROPERTY is an upcase symbol (i.e. `:COOKIE_DATA'). BLOB is an
  3329. element or object.
  3330. If optional argument INHERITED is non-nil, the value can be
  3331. inherited from a parent headline.
  3332. Return value is a string or nil."
  3333. (let ((headline (if (eq (org-element-type blob) 'headline) blob
  3334. (org-export-get-parent-headline blob))))
  3335. (if (not inherited) (org-element-property property blob)
  3336. (let ((parent headline) value)
  3337. (catch 'found
  3338. (while parent
  3339. (when (plist-member (nth 1 parent) property)
  3340. (throw 'found (org-element-property property parent)))
  3341. (setq parent (org-element-property :parent parent))))))))
  3342. (defun org-export-get-category (blob info)
  3343. "Return category for element or object BLOB.
  3344. INFO is a plist used as a communication channel.
  3345. CATEGORY is automatically inherited from a parent headline, from
  3346. #+CATEGORY: keyword or created out of original file name. If all
  3347. fail, the fall-back value is \"???\"."
  3348. (or (let ((headline (if (eq (org-element-type blob) 'headline) blob
  3349. (org-export-get-parent-headline blob))))
  3350. ;; Almost like `org-export-node-property', but we cannot trust
  3351. ;; `plist-member' as every headline has a `:CATEGORY'
  3352. ;; property, would it be nil or equal to "???" (which has the
  3353. ;; same meaning).
  3354. (let ((parent headline) value)
  3355. (catch 'found
  3356. (while parent
  3357. (let ((category (org-element-property :CATEGORY parent)))
  3358. (and category (not (equal "???" category))
  3359. (throw 'found category)))
  3360. (setq parent (org-element-property :parent parent))))))
  3361. (org-element-map (plist-get info :parse-tree) 'keyword
  3362. (lambda (kwd)
  3363. (when (equal (org-element-property :key kwd) "CATEGORY")
  3364. (org-element-property :value kwd)))
  3365. info 'first-match)
  3366. (let ((file (plist-get info :input-file)))
  3367. (and file (file-name-sans-extension (file-name-nondirectory file))))
  3368. "???"))
  3369. (defun org-export-get-alt-title (headline info)
  3370. "Return alternative title for HEADLINE, as a secondary string.
  3371. INFO is a plist used as a communication channel. If no optional
  3372. title is defined, fall-back to the regular title."
  3373. (or (org-element-property :alt-title headline)
  3374. (org-element-property :title headline)))
  3375. (defun org-export-first-sibling-p (headline info)
  3376. "Non-nil when HEADLINE is the first sibling in its sub-tree.
  3377. INFO is a plist used as a communication channel."
  3378. (not (eq (org-element-type (org-export-get-previous-element headline info))
  3379. 'headline)))
  3380. (defun org-export-last-sibling-p (headline info)
  3381. "Non-nil when HEADLINE is the last sibling in its sub-tree.
  3382. INFO is a plist used as a communication channel."
  3383. (not (org-export-get-next-element headline info)))
  3384. ;;;; For Keywords
  3385. ;;
  3386. ;; `org-export-get-date' returns a date appropriate for the document
  3387. ;; to about to be exported. In particular, it takes care of
  3388. ;; `org-export-date-timestamp-format'.
  3389. (defun org-export-get-date (info &optional fmt)
  3390. "Return date value for the current document.
  3391. INFO is a plist used as a communication channel. FMT, when
  3392. non-nil, is a time format string that will be applied on the date
  3393. if it consists in a single timestamp object. It defaults to
  3394. `org-export-date-timestamp-format' when nil.
  3395. A proper date can be a secondary string, a string or nil. It is
  3396. meant to be translated with `org-export-data' or alike."
  3397. (let ((date (plist-get info :date))
  3398. (fmt (or fmt org-export-date-timestamp-format)))
  3399. (cond ((not date) nil)
  3400. ((and fmt
  3401. (not (cdr date))
  3402. (eq (org-element-type (car date)) 'timestamp))
  3403. (org-timestamp-format (car date) fmt))
  3404. (t date))))
  3405. ;;;; For Links
  3406. ;;
  3407. ;; `org-export-solidify-link-text' turns a string into a safer version
  3408. ;; for links, replacing most non-standard characters with hyphens.
  3409. ;;
  3410. ;; `org-export-get-coderef-format' returns an appropriate format
  3411. ;; string for coderefs.
  3412. ;;
  3413. ;; `org-export-inline-image-p' returns a non-nil value when the link
  3414. ;; provided should be considered as an inline image.
  3415. ;;
  3416. ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
  3417. ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
  3418. ;; returns an appropriate unique identifier when found, or nil.
  3419. ;;
  3420. ;; `org-export-resolve-id-link' returns the first headline with
  3421. ;; specified id or custom-id in parse tree, the path to the external
  3422. ;; file with the id or nil when neither was found.
  3423. ;;
  3424. ;; `org-export-resolve-coderef' associates a reference to a line
  3425. ;; number in the element it belongs, or returns the reference itself
  3426. ;; when the element isn't numbered.
  3427. (defun org-export-solidify-link-text (s)
  3428. "Take link text S and make a safe target out of it."
  3429. (save-match-data
  3430. (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-:]+") "-")))
  3431. (defun org-export-get-coderef-format (path desc)
  3432. "Return format string for code reference link.
  3433. PATH is the link path. DESC is its description."
  3434. (save-match-data
  3435. (cond ((not desc) "%s")
  3436. ((string-match (regexp-quote (concat "(" path ")")) desc)
  3437. (replace-match "%s" t t desc))
  3438. (t desc))))
  3439. (defun org-export-inline-image-p (link &optional rules)
  3440. "Non-nil if LINK object points to an inline image.
  3441. Optional argument is a set of RULES defining inline images. It
  3442. is an alist where associations have the following shape:
  3443. \(TYPE . REGEXP)
  3444. Applying a rule means apply REGEXP against LINK's path when its
  3445. type is TYPE. The function will return a non-nil value if any of
  3446. the provided rules is non-nil. The default rule is
  3447. `org-export-default-inline-image-rule'.
  3448. This only applies to links without a description."
  3449. (and (not (org-element-contents link))
  3450. (let ((case-fold-search t)
  3451. (rules (or rules org-export-default-inline-image-rule)))
  3452. (catch 'exit
  3453. (mapc
  3454. (lambda (rule)
  3455. (and (string= (org-element-property :type link) (car rule))
  3456. (string-match (cdr rule)
  3457. (org-element-property :path link))
  3458. (throw 'exit t)))
  3459. rules)
  3460. ;; Return nil if no rule matched.
  3461. nil))))
  3462. (defun org-export-resolve-coderef (ref info)
  3463. "Resolve a code reference REF.
  3464. INFO is a plist used as a communication channel.
  3465. Return associated line number in source code, or REF itself,
  3466. depending on src-block or example element's switches."
  3467. (org-element-map (plist-get info :parse-tree) '(example-block src-block)
  3468. (lambda (el)
  3469. (with-temp-buffer
  3470. (insert (org-trim (org-element-property :value el)))
  3471. (let* ((label-fmt (regexp-quote
  3472. (or (org-element-property :label-fmt el)
  3473. org-coderef-label-format)))
  3474. (ref-re
  3475. (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
  3476. (replace-regexp-in-string "%s" ref label-fmt nil t))))
  3477. ;; Element containing REF is found. Resolve it to either
  3478. ;; a label or a line number, as needed.
  3479. (when (re-search-backward ref-re nil t)
  3480. (cond
  3481. ((org-element-property :use-labels el) ref)
  3482. ((eq (org-element-property :number-lines el) 'continued)
  3483. (+ (org-export-get-loc el info) (line-number-at-pos)))
  3484. (t (line-number-at-pos)))))))
  3485. info 'first-match))
  3486. (defun org-export-resolve-fuzzy-link (link info)
  3487. "Return LINK destination.
  3488. INFO is a plist holding contextual information.
  3489. Return value can be an object, an element, or nil:
  3490. - If LINK path matches a target object (i.e. <<path>>) return it.
  3491. - If LINK path exactly matches the name affiliated keyword
  3492. \(i.e. #+NAME: path) of an element, return that element.
  3493. - If LINK path exactly matches any headline name, return that
  3494. element. If more than one headline share that name, priority
  3495. will be given to the one with the closest common ancestor, if
  3496. any, or the first one in the parse tree otherwise.
  3497. - Otherwise, return nil.
  3498. Assume LINK type is \"fuzzy\". White spaces are not
  3499. significant."
  3500. (let* ((raw-path (org-element-property :path link))
  3501. (match-title-p (eq (aref raw-path 0) ?*))
  3502. ;; Split PATH at white spaces so matches are space
  3503. ;; insensitive.
  3504. (path (org-split-string
  3505. (if match-title-p (substring raw-path 1) raw-path)))
  3506. ;; Cache for destinations that are not position dependent.
  3507. (link-cache
  3508. (or (plist-get info :resolve-fuzzy-link-cache)
  3509. (plist-get (setq info (plist-put info :resolve-fuzzy-link-cache
  3510. (make-hash-table :test 'equal)))
  3511. :resolve-fuzzy-link-cache)))
  3512. (cached (gethash path link-cache 'not-found)))
  3513. (cond
  3514. ;; Destination is not position dependent: use cached value.
  3515. ((and (not match-title-p) (not (eq cached 'not-found))) cached)
  3516. ;; First try to find a matching "<<path>>" unless user specified
  3517. ;; he was looking for a headline (path starts with a "*"
  3518. ;; character).
  3519. ((and (not match-title-p)
  3520. (let ((match (org-element-map (plist-get info :parse-tree) 'target
  3521. (lambda (blob)
  3522. (and (equal (org-split-string
  3523. (org-element-property :value blob))
  3524. path)
  3525. blob))
  3526. info 'first-match)))
  3527. (and match (puthash path match link-cache)))))
  3528. ;; Then try to find an element with a matching "#+NAME: path"
  3529. ;; affiliated keyword.
  3530. ((and (not match-title-p)
  3531. (let ((match (org-element-map (plist-get info :parse-tree)
  3532. org-element-all-elements
  3533. (lambda (el)
  3534. (let ((name (org-element-property :name el)))
  3535. (when (and name
  3536. (equal (org-split-string name) path))
  3537. el)))
  3538. info 'first-match)))
  3539. (and match (puthash path match link-cache)))))
  3540. ;; Last case: link either points to a headline or to nothingness.
  3541. ;; Try to find the source, with priority given to headlines with
  3542. ;; the closest common ancestor. If such candidate is found,
  3543. ;; return it, otherwise return nil.
  3544. (t
  3545. (let ((find-headline
  3546. (function
  3547. ;; Return first headline whose `:raw-value' property is
  3548. ;; NAME in parse tree DATA, or nil. Statistics cookies
  3549. ;; are ignored.
  3550. (lambda (name data)
  3551. (org-element-map data 'headline
  3552. (lambda (headline)
  3553. (when (equal (org-split-string
  3554. (replace-regexp-in-string
  3555. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  3556. (org-element-property :raw-value headline)))
  3557. name)
  3558. headline))
  3559. info 'first-match)))))
  3560. ;; Search among headlines sharing an ancestor with link, from
  3561. ;; closest to farthest.
  3562. (catch 'exit
  3563. (mapc
  3564. (lambda (parent)
  3565. (let ((foundp (funcall find-headline path parent)))
  3566. (when foundp (throw 'exit foundp))))
  3567. (let ((parent-hl (org-export-get-parent-headline link)))
  3568. (if (not parent-hl) (list (plist-get info :parse-tree))
  3569. (cons parent-hl (org-export-get-genealogy parent-hl)))))
  3570. ;; No destination found: return nil.
  3571. (and (not match-title-p) (puthash path nil link-cache))))))))
  3572. (defun org-export-resolve-id-link (link info)
  3573. "Return headline referenced as LINK destination.
  3574. INFO is a plist used as a communication channel.
  3575. Return value can be the headline element matched in current parse
  3576. tree, a file name or nil. Assume LINK type is either \"id\" or
  3577. \"custom-id\"."
  3578. (let ((id (org-element-property :path link)))
  3579. ;; First check if id is within the current parse tree.
  3580. (or (org-element-map (plist-get info :parse-tree) 'headline
  3581. (lambda (headline)
  3582. (when (or (string= (org-element-property :ID headline) id)
  3583. (string= (org-element-property :CUSTOM_ID headline) id))
  3584. headline))
  3585. info 'first-match)
  3586. ;; Otherwise, look for external files.
  3587. (cdr (assoc id (plist-get info :id-alist))))))
  3588. (defun org-export-resolve-radio-link (link info)
  3589. "Return radio-target object referenced as LINK destination.
  3590. INFO is a plist used as a communication channel.
  3591. Return value can be a radio-target object or nil. Assume LINK
  3592. has type \"radio\"."
  3593. (let ((path (replace-regexp-in-string
  3594. "[ \r\t\n]+" " " (org-element-property :path link))))
  3595. (org-element-map (plist-get info :parse-tree) 'radio-target
  3596. (lambda (radio)
  3597. (and (eq (compare-strings
  3598. (replace-regexp-in-string
  3599. "[ \r\t\n]+" " " (org-element-property :value radio))
  3600. nil nil path nil nil t)
  3601. t)
  3602. radio))
  3603. info 'first-match)))
  3604. ;;;; For References
  3605. ;;
  3606. ;; `org-export-get-ordinal' associates a sequence number to any object
  3607. ;; or element.
  3608. (defun org-export-get-ordinal (element info &optional types predicate)
  3609. "Return ordinal number of an element or object.
  3610. ELEMENT is the element or object considered. INFO is the plist
  3611. used as a communication channel.
  3612. Optional argument TYPES, when non-nil, is a list of element or
  3613. object types, as symbols, that should also be counted in.
  3614. Otherwise, only provided element's type is considered.
  3615. Optional argument PREDICATE is a function returning a non-nil
  3616. value if the current element or object should be counted in. It
  3617. accepts two arguments: the element or object being considered and
  3618. the plist used as a communication channel. This allows to count
  3619. only a certain type of objects (i.e. inline images).
  3620. Return value is a list of numbers if ELEMENT is a headline or an
  3621. item. It is nil for keywords. It represents the footnote number
  3622. for footnote definitions and footnote references. If ELEMENT is
  3623. a target, return the same value as if ELEMENT was the closest
  3624. table, item or headline containing the target. In any other
  3625. case, return the sequence number of ELEMENT among elements or
  3626. objects of the same type."
  3627. ;; Ordinal of a target object refer to the ordinal of the closest
  3628. ;; table, item, or headline containing the object.
  3629. (when (eq (org-element-type element) 'target)
  3630. (setq element
  3631. (loop for parent in (org-export-get-genealogy element)
  3632. when
  3633. (memq
  3634. (org-element-type parent)
  3635. '(footnote-definition footnote-reference headline item
  3636. table))
  3637. return parent)))
  3638. (case (org-element-type element)
  3639. ;; Special case 1: A headline returns its number as a list.
  3640. (headline (org-export-get-headline-number element info))
  3641. ;; Special case 2: An item returns its number as a list.
  3642. (item (let ((struct (org-element-property :structure element)))
  3643. (org-list-get-item-number
  3644. (org-element-property :begin element)
  3645. struct
  3646. (org-list-prevs-alist struct)
  3647. (org-list-parents-alist struct))))
  3648. ((footnote-definition footnote-reference)
  3649. (org-export-get-footnote-number element info))
  3650. (otherwise
  3651. (let ((counter 0))
  3652. ;; Increment counter until ELEMENT is found again.
  3653. (org-element-map (plist-get info :parse-tree)
  3654. (or types (org-element-type element))
  3655. (lambda (el)
  3656. (cond
  3657. ((eq element el) (1+ counter))
  3658. ((not predicate) (incf counter) nil)
  3659. ((funcall predicate el info) (incf counter) nil)))
  3660. info 'first-match)))))
  3661. ;;;; For Src-Blocks
  3662. ;;
  3663. ;; `org-export-get-loc' counts number of code lines accumulated in
  3664. ;; src-block or example-block elements with a "+n" switch until
  3665. ;; a given element, excluded. Note: "-n" switches reset that count.
  3666. ;;
  3667. ;; `org-export-unravel-code' extracts source code (along with a code
  3668. ;; references alist) from an `element-block' or `src-block' type
  3669. ;; element.
  3670. ;;
  3671. ;; `org-export-format-code' applies a formatting function to each line
  3672. ;; of code, providing relative line number and code reference when
  3673. ;; appropriate. Since it doesn't access the original element from
  3674. ;; which the source code is coming, it expects from the code calling
  3675. ;; it to know if lines should be numbered and if code references
  3676. ;; should appear.
  3677. ;;
  3678. ;; Eventually, `org-export-format-code-default' is a higher-level
  3679. ;; function (it makes use of the two previous functions) which handles
  3680. ;; line numbering and code references inclusion, and returns source
  3681. ;; code in a format suitable for plain text or verbatim output.
  3682. (defun org-export-get-loc (element info)
  3683. "Return accumulated lines of code up to ELEMENT.
  3684. INFO is the plist used as a communication channel.
  3685. ELEMENT is excluded from count."
  3686. (let ((loc 0))
  3687. (org-element-map (plist-get info :parse-tree)
  3688. `(src-block example-block ,(org-element-type element))
  3689. (lambda (el)
  3690. (cond
  3691. ;; ELEMENT is reached: Quit the loop.
  3692. ((eq el element))
  3693. ;; Only count lines from src-block and example-block elements
  3694. ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
  3695. ((not (memq (org-element-type el) '(src-block example-block))) nil)
  3696. ((let ((linums (org-element-property :number-lines el)))
  3697. (when linums
  3698. ;; Accumulate locs or reset them.
  3699. (let ((lines (org-count-lines
  3700. (org-trim (org-element-property :value el)))))
  3701. (setq loc (if (eq linums 'new) lines (+ loc lines))))))
  3702. ;; Return nil to stay in the loop.
  3703. nil)))
  3704. info 'first-match)
  3705. ;; Return value.
  3706. loc))
  3707. (defun org-export-unravel-code (element)
  3708. "Clean source code and extract references out of it.
  3709. ELEMENT has either a `src-block' an `example-block' type.
  3710. Return a cons cell whose CAR is the source code, cleaned from any
  3711. reference and protective comma and CDR is an alist between
  3712. relative line number (integer) and name of code reference on that
  3713. line (string)."
  3714. (let* ((line 0) refs
  3715. ;; Get code and clean it. Remove blank lines at its
  3716. ;; beginning and end.
  3717. (code (replace-regexp-in-string
  3718. "\\`\\([ \t]*\n\\)+" ""
  3719. (replace-regexp-in-string
  3720. "\\([ \t]*\n\\)*[ \t]*\\'" "\n"
  3721. (org-element-property :value element))))
  3722. ;; Get format used for references.
  3723. (label-fmt (regexp-quote
  3724. (or (org-element-property :label-fmt element)
  3725. org-coderef-label-format)))
  3726. ;; Build a regexp matching a loc with a reference.
  3727. (with-ref-re
  3728. (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
  3729. (replace-regexp-in-string
  3730. "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
  3731. ;; Return value.
  3732. (cons
  3733. ;; Code with references removed.
  3734. (org-element-normalize-string
  3735. (mapconcat
  3736. (lambda (loc)
  3737. (incf line)
  3738. (if (not (string-match with-ref-re loc)) loc
  3739. ;; Ref line: remove ref, and signal its position in REFS.
  3740. (push (cons line (match-string 3 loc)) refs)
  3741. (replace-match "" nil nil loc 1)))
  3742. (org-split-string code "\n") "\n"))
  3743. ;; Reference alist.
  3744. refs)))
  3745. (defun org-export-format-code (code fun &optional num-lines ref-alist)
  3746. "Format CODE by applying FUN line-wise and return it.
  3747. CODE is a string representing the code to format. FUN is
  3748. a function. It must accept three arguments: a line of
  3749. code (string), the current line number (integer) or nil and the
  3750. reference associated to the current line (string) or nil.
  3751. Optional argument NUM-LINES can be an integer representing the
  3752. number of code lines accumulated until the current code. Line
  3753. numbers passed to FUN will take it into account. If it is nil,
  3754. FUN's second argument will always be nil. This number can be
  3755. obtained with `org-export-get-loc' function.
  3756. Optional argument REF-ALIST can be an alist between relative line
  3757. number (i.e. ignoring NUM-LINES) and the name of the code
  3758. reference on it. If it is nil, FUN's third argument will always
  3759. be nil. It can be obtained through the use of
  3760. `org-export-unravel-code' function."
  3761. (let ((--locs (org-split-string code "\n"))
  3762. (--line 0))
  3763. (org-element-normalize-string
  3764. (mapconcat
  3765. (lambda (--loc)
  3766. (incf --line)
  3767. (let ((--ref (cdr (assq --line ref-alist))))
  3768. (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
  3769. --locs "\n"))))
  3770. (defun org-export-format-code-default (element info)
  3771. "Return source code from ELEMENT, formatted in a standard way.
  3772. ELEMENT is either a `src-block' or `example-block' element. INFO
  3773. is a plist used as a communication channel.
  3774. This function takes care of line numbering and code references
  3775. inclusion. Line numbers, when applicable, appear at the
  3776. beginning of the line, separated from the code by two white
  3777. spaces. Code references, on the other hand, appear flushed to
  3778. the right, separated by six white spaces from the widest line of
  3779. code."
  3780. ;; Extract code and references.
  3781. (let* ((code-info (org-export-unravel-code element))
  3782. (code (car code-info))
  3783. (code-lines (org-split-string code "\n")))
  3784. (if (null code-lines) ""
  3785. (let* ((refs (and (org-element-property :retain-labels element)
  3786. (cdr code-info)))
  3787. ;; Handle line numbering.
  3788. (num-start (case (org-element-property :number-lines element)
  3789. (continued (org-export-get-loc element info))
  3790. (new 0)))
  3791. (num-fmt
  3792. (and num-start
  3793. (format "%%%ds "
  3794. (length (number-to-string
  3795. (+ (length code-lines) num-start))))))
  3796. ;; Prepare references display, if required. Any reference
  3797. ;; should start six columns after the widest line of code,
  3798. ;; wrapped with parenthesis.
  3799. (max-width
  3800. (+ (apply 'max (mapcar 'length code-lines))
  3801. (if (not num-start) 0 (length (format num-fmt num-start))))))
  3802. (org-export-format-code
  3803. code
  3804. (lambda (loc line-num ref)
  3805. (let ((number-str (and num-fmt (format num-fmt line-num))))
  3806. (concat
  3807. number-str
  3808. loc
  3809. (and ref
  3810. (concat (make-string
  3811. (- (+ 6 max-width)
  3812. (+ (length loc) (length number-str))) ? )
  3813. (format "(%s)" ref))))))
  3814. num-start refs)))))
  3815. ;;;; For Tables
  3816. ;;
  3817. ;; `org-export-table-has-special-column-p' and and
  3818. ;; `org-export-table-row-is-special-p' are predicates used to look for
  3819. ;; meta-information about the table structure.
  3820. ;;
  3821. ;; `org-table-has-header-p' tells when the rows before the first rule
  3822. ;; should be considered as table's header.
  3823. ;;
  3824. ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
  3825. ;; and `org-export-table-cell-borders' extract information from
  3826. ;; a table-cell element.
  3827. ;;
  3828. ;; `org-export-table-dimensions' gives the number on rows and columns
  3829. ;; in the table, ignoring horizontal rules and special columns.
  3830. ;; `org-export-table-cell-address', given a table-cell object, returns
  3831. ;; the absolute address of a cell. On the other hand,
  3832. ;; `org-export-get-table-cell-at' does the contrary.
  3833. ;;
  3834. ;; `org-export-table-cell-starts-colgroup-p',
  3835. ;; `org-export-table-cell-ends-colgroup-p',
  3836. ;; `org-export-table-row-starts-rowgroup-p',
  3837. ;; `org-export-table-row-ends-rowgroup-p',
  3838. ;; `org-export-table-row-starts-header-p' and
  3839. ;; `org-export-table-row-ends-header-p' indicate position of current
  3840. ;; row or cell within the table.
  3841. (defun org-export-table-has-special-column-p (table)
  3842. "Non-nil when TABLE has a special column.
  3843. All special columns will be ignored during export."
  3844. ;; The table has a special column when every first cell of every row
  3845. ;; has an empty value or contains a symbol among "/", "#", "!", "$",
  3846. ;; "*" "_" and "^". Though, do not consider a first row containing
  3847. ;; only empty cells as special.
  3848. (let ((special-column-p 'empty))
  3849. (catch 'exit
  3850. (mapc
  3851. (lambda (row)
  3852. (when (eq (org-element-property :type row) 'standard)
  3853. (let ((value (org-element-contents
  3854. (car (org-element-contents row)))))
  3855. (cond ((member value '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
  3856. (setq special-column-p 'special))
  3857. ((not value))
  3858. (t (throw 'exit nil))))))
  3859. (org-element-contents table))
  3860. (eq special-column-p 'special))))
  3861. (defun org-export-table-has-header-p (table info)
  3862. "Non-nil when TABLE has a header.
  3863. INFO is a plist used as a communication channel.
  3864. A table has a header when it contains at least two row groups."
  3865. (let ((cache (or (plist-get info :table-header-cache)
  3866. (plist-get (setq info
  3867. (plist-put info :table-header-cache
  3868. (make-hash-table :test 'eq)))
  3869. :table-header-cache))))
  3870. (or (gethash table cache)
  3871. (let ((rowgroup 1) row-flag)
  3872. (puthash
  3873. table
  3874. (org-element-map table 'table-row
  3875. (lambda (row)
  3876. (cond
  3877. ((> rowgroup 1) t)
  3878. ((and row-flag (eq (org-element-property :type row) 'rule))
  3879. (incf rowgroup) (setq row-flag nil))
  3880. ((and (not row-flag) (eq (org-element-property :type row)
  3881. 'standard))
  3882. (setq row-flag t) nil)))
  3883. info 'first-match)
  3884. cache)))))
  3885. (defun org-export-table-row-is-special-p (table-row info)
  3886. "Non-nil if TABLE-ROW is considered special.
  3887. INFO is a plist used as the communication channel.
  3888. All special rows will be ignored during export."
  3889. (when (eq (org-element-property :type table-row) 'standard)
  3890. (let ((first-cell (org-element-contents
  3891. (car (org-element-contents table-row)))))
  3892. ;; A row is special either when...
  3893. (or
  3894. ;; ... it starts with a field only containing "/",
  3895. (equal first-cell '("/"))
  3896. ;; ... the table contains a special column and the row start
  3897. ;; with a marking character among, "^", "_", "$" or "!",
  3898. (and (org-export-table-has-special-column-p
  3899. (org-export-get-parent table-row))
  3900. (member first-cell '(("^") ("_") ("$") ("!"))))
  3901. ;; ... it contains only alignment cookies and empty cells.
  3902. (let ((special-row-p 'empty))
  3903. (catch 'exit
  3904. (mapc
  3905. (lambda (cell)
  3906. (let ((value (org-element-contents cell)))
  3907. ;; Since VALUE is a secondary string, the following
  3908. ;; checks avoid expanding it with `org-export-data'.
  3909. (cond ((not value))
  3910. ((and (not (cdr value))
  3911. (stringp (car value))
  3912. (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
  3913. (car value)))
  3914. (setq special-row-p 'cookie))
  3915. (t (throw 'exit nil)))))
  3916. (org-element-contents table-row))
  3917. (eq special-row-p 'cookie)))))))
  3918. (defun org-export-table-row-group (table-row info)
  3919. "Return TABLE-ROW's group number, as an integer.
  3920. INFO is a plist used as the communication channel.
  3921. Return value is the group number, as an integer, or nil for
  3922. special rows and rows separators. First group is also table's
  3923. header."
  3924. (let ((cache (or (plist-get info :table-row-group-cache)
  3925. (plist-get (setq info
  3926. (plist-put info :table-row-group-cache
  3927. (make-hash-table :test 'eq)))
  3928. :table-row-group-cache))))
  3929. (cond ((gethash table-row cache))
  3930. ((eq (org-element-property :type table-row) 'rule) nil)
  3931. (t (let ((group 0) row-flag)
  3932. (org-element-map (org-export-get-parent table-row) 'table-row
  3933. (lambda (row)
  3934. (if (eq (org-element-property :type row) 'rule)
  3935. (setq row-flag nil)
  3936. (unless row-flag (incf group) (setq row-flag t)))
  3937. (when (eq table-row row) (puthash table-row group cache)))
  3938. info 'first-match))))))
  3939. (defun org-export-table-cell-width (table-cell info)
  3940. "Return TABLE-CELL contents width.
  3941. INFO is a plist used as the communication channel.
  3942. Return value is the width given by the last width cookie in the
  3943. same column as TABLE-CELL, or nil."
  3944. (let* ((row (org-export-get-parent table-cell))
  3945. (table (org-export-get-parent row))
  3946. (cells (org-element-contents row))
  3947. (columns (length cells))
  3948. (column (- columns (length (memq table-cell cells))))
  3949. (cache (or (plist-get info :table-cell-width-cache)
  3950. (plist-get (setq info
  3951. (plist-put info :table-cell-width-cache
  3952. (make-hash-table :test 'eq)))
  3953. :table-cell-width-cache)))
  3954. (width-vector (or (gethash table cache)
  3955. (puthash table (make-vector columns 'empty) cache)))
  3956. (value (aref width-vector column)))
  3957. (if (not (eq value 'empty)) value
  3958. (let (cookie-width)
  3959. (dolist (row (org-element-contents table)
  3960. (aset width-vector column cookie-width))
  3961. (when (org-export-table-row-is-special-p row info)
  3962. ;; In a special row, try to find a width cookie at COLUMN.
  3963. (let* ((value (org-element-contents
  3964. (elt (org-element-contents row) column)))
  3965. (cookie (car value)))
  3966. ;; The following checks avoid expanding unnecessarily
  3967. ;; the cell with `org-export-data'.
  3968. (when (and value
  3969. (not (cdr value))
  3970. (stringp cookie)
  3971. (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" cookie)
  3972. (match-string 1 cookie))
  3973. (setq cookie-width
  3974. (string-to-number (match-string 1 cookie)))))))))))
  3975. (defun org-export-table-cell-alignment (table-cell info)
  3976. "Return TABLE-CELL contents alignment.
  3977. INFO is a plist used as the communication channel.
  3978. Return alignment as specified by the last alignment cookie in the
  3979. same column as TABLE-CELL. If no such cookie is found, a default
  3980. alignment value will be deduced from fraction of numbers in the
  3981. column (see `org-table-number-fraction' for more information).
  3982. Possible values are `left', `right' and `center'."
  3983. ;; Load `org-table-number-fraction' and `org-table-number-regexp'.
  3984. (require 'org-table)
  3985. (let* ((row (org-export-get-parent table-cell))
  3986. (table (org-export-get-parent row))
  3987. (cells (org-element-contents row))
  3988. (columns (length cells))
  3989. (column (- columns (length (memq table-cell cells))))
  3990. (cache (or (plist-get info :table-cell-alignment-cache)
  3991. (plist-get (setq info
  3992. (plist-put info :table-cell-alignment-cache
  3993. (make-hash-table :test 'eq)))
  3994. :table-cell-alignment-cache)))
  3995. (align-vector (or (gethash table cache)
  3996. (puthash table (make-vector columns nil) cache))))
  3997. (or (aref align-vector column)
  3998. (let ((number-cells 0)
  3999. (total-cells 0)
  4000. cookie-align
  4001. previous-cell-number-p)
  4002. (dolist (row (org-element-contents (org-export-get-parent row)))
  4003. (cond
  4004. ;; In a special row, try to find an alignment cookie at
  4005. ;; COLUMN.
  4006. ((org-export-table-row-is-special-p row info)
  4007. (let ((value (org-element-contents
  4008. (elt (org-element-contents row) column))))
  4009. ;; Since VALUE is a secondary string, the following
  4010. ;; checks avoid useless expansion through
  4011. ;; `org-export-data'.
  4012. (when (and value
  4013. (not (cdr value))
  4014. (stringp (car value))
  4015. (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
  4016. (car value))
  4017. (match-string 1 (car value)))
  4018. (setq cookie-align (match-string 1 (car value))))))
  4019. ;; Ignore table rules.
  4020. ((eq (org-element-property :type row) 'rule))
  4021. ;; In a standard row, check if cell's contents are
  4022. ;; expressing some kind of number. Increase NUMBER-CELLS
  4023. ;; accordingly. Though, don't bother if an alignment
  4024. ;; cookie has already defined cell's alignment.
  4025. ((not cookie-align)
  4026. (let ((value (org-export-data
  4027. (org-element-contents
  4028. (elt (org-element-contents row) column))
  4029. info)))
  4030. (incf total-cells)
  4031. ;; Treat an empty cell as a number if it follows
  4032. ;; a number.
  4033. (if (not (or (string-match org-table-number-regexp value)
  4034. (and (string= value "") previous-cell-number-p)))
  4035. (setq previous-cell-number-p nil)
  4036. (setq previous-cell-number-p t)
  4037. (incf number-cells))))))
  4038. ;; Return value. Alignment specified by cookies has
  4039. ;; precedence over alignment deduced from cell's contents.
  4040. (aset align-vector
  4041. column
  4042. (cond ((equal cookie-align "l") 'left)
  4043. ((equal cookie-align "r") 'right)
  4044. ((equal cookie-align "c") 'center)
  4045. ((>= (/ (float number-cells) total-cells)
  4046. org-table-number-fraction)
  4047. 'right)
  4048. (t 'left)))))))
  4049. (defun org-export-table-cell-borders (table-cell info)
  4050. "Return TABLE-CELL borders.
  4051. INFO is a plist used as a communication channel.
  4052. Return value is a list of symbols, or nil. Possible values are:
  4053. `top', `bottom', `above', `below', `left' and `right'. Note:
  4054. `top' (resp. `bottom') only happen for a cell in the first
  4055. row (resp. last row) of the table, ignoring table rules, if any.
  4056. Returned borders ignore special rows."
  4057. (let* ((row (org-export-get-parent table-cell))
  4058. (table (org-export-get-parent-table table-cell))
  4059. borders)
  4060. ;; Top/above border? TABLE-CELL has a border above when a rule
  4061. ;; used to demarcate row groups can be found above. Hence,
  4062. ;; finding a rule isn't sufficient to push `above' in BORDERS:
  4063. ;; another regular row has to be found above that rule.
  4064. (let (rule-flag)
  4065. (catch 'exit
  4066. (mapc (lambda (row)
  4067. (cond ((eq (org-element-property :type row) 'rule)
  4068. (setq rule-flag t))
  4069. ((not (org-export-table-row-is-special-p row info))
  4070. (if rule-flag (throw 'exit (push 'above borders))
  4071. (throw 'exit nil)))))
  4072. ;; Look at every row before the current one.
  4073. (cdr (memq row (reverse (org-element-contents table)))))
  4074. ;; No rule above, or rule found starts the table (ignoring any
  4075. ;; special row): TABLE-CELL is at the top of the table.
  4076. (when rule-flag (push 'above borders))
  4077. (push 'top borders)))
  4078. ;; Bottom/below border? TABLE-CELL has a border below when next
  4079. ;; non-regular row below is a rule.
  4080. (let (rule-flag)
  4081. (catch 'exit
  4082. (mapc (lambda (row)
  4083. (cond ((eq (org-element-property :type row) 'rule)
  4084. (setq rule-flag t))
  4085. ((not (org-export-table-row-is-special-p row info))
  4086. (if rule-flag (throw 'exit (push 'below borders))
  4087. (throw 'exit nil)))))
  4088. ;; Look at every row after the current one.
  4089. (cdr (memq row (org-element-contents table))))
  4090. ;; No rule below, or rule found ends the table (modulo some
  4091. ;; special row): TABLE-CELL is at the bottom of the table.
  4092. (when rule-flag (push 'below borders))
  4093. (push 'bottom borders)))
  4094. ;; Right/left borders? They can only be specified by column
  4095. ;; groups. Column groups are defined in a row starting with "/".
  4096. ;; Also a column groups row only contains "<", "<>", ">" or blank
  4097. ;; cells.
  4098. (catch 'exit
  4099. (let ((column (let ((cells (org-element-contents row)))
  4100. (- (length cells) (length (memq table-cell cells))))))
  4101. (mapc
  4102. (lambda (row)
  4103. (unless (eq (org-element-property :type row) 'rule)
  4104. (when (equal (org-element-contents
  4105. (car (org-element-contents row)))
  4106. '("/"))
  4107. (let ((column-groups
  4108. (mapcar
  4109. (lambda (cell)
  4110. (let ((value (org-element-contents cell)))
  4111. (when (member value '(("<") ("<>") (">") nil))
  4112. (car value))))
  4113. (org-element-contents row))))
  4114. ;; There's a left border when previous cell, if
  4115. ;; any, ends a group, or current one starts one.
  4116. (when (or (and (not (zerop column))
  4117. (member (elt column-groups (1- column))
  4118. '(">" "<>")))
  4119. (member (elt column-groups column) '("<" "<>")))
  4120. (push 'left borders))
  4121. ;; There's a right border when next cell, if any,
  4122. ;; starts a group, or current one ends one.
  4123. (when (or (and (/= (1+ column) (length column-groups))
  4124. (member (elt column-groups (1+ column))
  4125. '("<" "<>")))
  4126. (member (elt column-groups column) '(">" "<>")))
  4127. (push 'right borders))
  4128. (throw 'exit nil)))))
  4129. ;; Table rows are read in reverse order so last column groups
  4130. ;; row has precedence over any previous one.
  4131. (reverse (org-element-contents table)))))
  4132. ;; Return value.
  4133. borders))
  4134. (defun org-export-table-cell-starts-colgroup-p (table-cell info)
  4135. "Non-nil when TABLE-CELL is at the beginning of a row group.
  4136. INFO is a plist used as a communication channel."
  4137. ;; A cell starts a column group either when it is at the beginning
  4138. ;; of a row (or after the special column, if any) or when it has
  4139. ;; a left border.
  4140. (or (eq (org-element-map (org-export-get-parent table-cell) 'table-cell
  4141. 'identity info 'first-match)
  4142. table-cell)
  4143. (memq 'left (org-export-table-cell-borders table-cell info))))
  4144. (defun org-export-table-cell-ends-colgroup-p (table-cell info)
  4145. "Non-nil when TABLE-CELL is at the end of a row group.
  4146. INFO is a plist used as a communication channel."
  4147. ;; A cell ends a column group either when it is at the end of a row
  4148. ;; or when it has a right border.
  4149. (or (eq (car (last (org-element-contents
  4150. (org-export-get-parent table-cell))))
  4151. table-cell)
  4152. (memq 'right (org-export-table-cell-borders table-cell info))))
  4153. (defun org-export-table-row-starts-rowgroup-p (table-row info)
  4154. "Non-nil when TABLE-ROW is at the beginning of a column group.
  4155. INFO is a plist used as a communication channel."
  4156. (unless (or (eq (org-element-property :type table-row) 'rule)
  4157. (org-export-table-row-is-special-p table-row info))
  4158. (let ((borders (org-export-table-cell-borders
  4159. (car (org-element-contents table-row)) info)))
  4160. (or (memq 'top borders) (memq 'above borders)))))
  4161. (defun org-export-table-row-ends-rowgroup-p (table-row info)
  4162. "Non-nil when TABLE-ROW is at the end of a column group.
  4163. INFO is a plist used as a communication channel."
  4164. (unless (or (eq (org-element-property :type table-row) 'rule)
  4165. (org-export-table-row-is-special-p table-row info))
  4166. (let ((borders (org-export-table-cell-borders
  4167. (car (org-element-contents table-row)) info)))
  4168. (or (memq 'bottom borders) (memq 'below borders)))))
  4169. (defun org-export-table-row-starts-header-p (table-row info)
  4170. "Non-nil when TABLE-ROW is the first table header's row.
  4171. INFO is a plist used as a communication channel."
  4172. (and (org-export-table-has-header-p
  4173. (org-export-get-parent-table table-row) info)
  4174. (org-export-table-row-starts-rowgroup-p table-row info)
  4175. (= (org-export-table-row-group table-row info) 1)))
  4176. (defun org-export-table-row-ends-header-p (table-row info)
  4177. "Non-nil when TABLE-ROW is the last table header's row.
  4178. INFO is a plist used as a communication channel."
  4179. (and (org-export-table-has-header-p
  4180. (org-export-get-parent-table table-row) info)
  4181. (org-export-table-row-ends-rowgroup-p table-row info)
  4182. (= (org-export-table-row-group table-row info) 1)))
  4183. (defun org-export-table-row-number (table-row info)
  4184. "Return TABLE-ROW number.
  4185. INFO is a plist used as a communication channel. Return value is
  4186. zero-based and ignores separators. The function returns nil for
  4187. special columns and separators."
  4188. (when (and (eq (org-element-property :type table-row) 'standard)
  4189. (not (org-export-table-row-is-special-p table-row info)))
  4190. (let ((number 0))
  4191. (org-element-map (org-export-get-parent-table table-row) 'table-row
  4192. (lambda (row)
  4193. (cond ((eq row table-row) number)
  4194. ((eq (org-element-property :type row) 'standard)
  4195. (incf number) nil)))
  4196. info 'first-match))))
  4197. (defun org-export-table-dimensions (table info)
  4198. "Return TABLE dimensions.
  4199. INFO is a plist used as a communication channel.
  4200. Return value is a CONS like (ROWS . COLUMNS) where
  4201. ROWS (resp. COLUMNS) is the number of exportable
  4202. rows (resp. columns)."
  4203. (let (first-row (columns 0) (rows 0))
  4204. ;; Set number of rows, and extract first one.
  4205. (org-element-map table 'table-row
  4206. (lambda (row)
  4207. (when (eq (org-element-property :type row) 'standard)
  4208. (incf rows)
  4209. (unless first-row (setq first-row row)))) info)
  4210. ;; Set number of columns.
  4211. (org-element-map first-row 'table-cell (lambda (cell) (incf columns)) info)
  4212. ;; Return value.
  4213. (cons rows columns)))
  4214. (defun org-export-table-cell-address (table-cell info)
  4215. "Return address of a regular TABLE-CELL object.
  4216. TABLE-CELL is the cell considered. INFO is a plist used as
  4217. a communication channel.
  4218. Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
  4219. zero-based index. Only exportable cells are considered. The
  4220. function returns nil for other cells."
  4221. (let* ((table-row (org-export-get-parent table-cell))
  4222. (row-number (org-export-table-row-number table-row info)))
  4223. (when row-number
  4224. (cons row-number
  4225. (let ((col-count 0))
  4226. (org-element-map table-row 'table-cell
  4227. (lambda (cell)
  4228. (if (eq cell table-cell) col-count (incf col-count) nil))
  4229. info 'first-match))))))
  4230. (defun org-export-get-table-cell-at (address table info)
  4231. "Return regular table-cell object at ADDRESS in TABLE.
  4232. Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
  4233. zero-based index. TABLE is a table type element. INFO is
  4234. a plist used as a communication channel.
  4235. If no table-cell, among exportable cells, is found at ADDRESS,
  4236. return nil."
  4237. (let ((column-pos (cdr address)) (column-count 0))
  4238. (org-element-map
  4239. ;; Row at (car address) or nil.
  4240. (let ((row-pos (car address)) (row-count 0))
  4241. (org-element-map table 'table-row
  4242. (lambda (row)
  4243. (cond ((eq (org-element-property :type row) 'rule) nil)
  4244. ((= row-count row-pos) row)
  4245. (t (incf row-count) nil)))
  4246. info 'first-match))
  4247. 'table-cell
  4248. (lambda (cell)
  4249. (if (= column-count column-pos) cell
  4250. (incf column-count) nil))
  4251. info 'first-match)))
  4252. ;;;; For Tables Of Contents
  4253. ;;
  4254. ;; `org-export-collect-headlines' builds a list of all exportable
  4255. ;; headline elements, maybe limited to a certain depth. One can then
  4256. ;; easily parse it and transcode it.
  4257. ;;
  4258. ;; Building lists of tables, figures or listings is quite similar.
  4259. ;; Once the generic function `org-export-collect-elements' is defined,
  4260. ;; `org-export-collect-tables', `org-export-collect-figures' and
  4261. ;; `org-export-collect-listings' can be derived from it.
  4262. (defun org-export-collect-headlines (info &optional n)
  4263. "Collect headlines in order to build a table of contents.
  4264. INFO is a plist used as a communication channel.
  4265. When optional argument N is an integer, it specifies the depth of
  4266. the table of contents. Otherwise, it is set to the value of the
  4267. last headline level. See `org-export-headline-levels' for more
  4268. information.
  4269. Return a list of all exportable headlines as parsed elements.
  4270. Footnote sections, if any, will be ignored."
  4271. (let ((limit (plist-get info :headline-levels)))
  4272. (setq n (if (wholenump n) (min n limit) limit))
  4273. (org-element-map (plist-get info :parse-tree) 'headline
  4274. #'(lambda (headline)
  4275. (unless (org-element-property :footnote-section-p headline)
  4276. (let ((level (org-export-get-relative-level headline info)))
  4277. (and (<= level n) headline))))
  4278. info)))
  4279. (defun org-export-collect-elements (type info &optional predicate)
  4280. "Collect referenceable elements of a determined type.
  4281. TYPE can be a symbol or a list of symbols specifying element
  4282. types to search. Only elements with a caption are collected.
  4283. INFO is a plist used as a communication channel.
  4284. When non-nil, optional argument PREDICATE is a function accepting
  4285. one argument, an element of type TYPE. It returns a non-nil
  4286. value when that element should be collected.
  4287. Return a list of all elements found, in order of appearance."
  4288. (org-element-map (plist-get info :parse-tree) type
  4289. (lambda (element)
  4290. (and (org-element-property :caption element)
  4291. (or (not predicate) (funcall predicate element))
  4292. element))
  4293. info))
  4294. (defun org-export-collect-tables (info)
  4295. "Build a list of tables.
  4296. INFO is a plist used as a communication channel.
  4297. Return a list of table elements with a caption."
  4298. (org-export-collect-elements 'table info))
  4299. (defun org-export-collect-figures (info predicate)
  4300. "Build a list of figures.
  4301. INFO is a plist used as a communication channel. PREDICATE is
  4302. a function which accepts one argument: a paragraph element and
  4303. whose return value is non-nil when that element should be
  4304. collected.
  4305. A figure is a paragraph type element, with a caption, verifying
  4306. PREDICATE. The latter has to be provided since a \"figure\" is
  4307. a vague concept that may depend on back-end.
  4308. Return a list of elements recognized as figures."
  4309. (org-export-collect-elements 'paragraph info predicate))
  4310. (defun org-export-collect-listings (info)
  4311. "Build a list of src blocks.
  4312. INFO is a plist used as a communication channel.
  4313. Return a list of src-block elements with a caption."
  4314. (org-export-collect-elements 'src-block info))
  4315. ;;;; Smart Quotes
  4316. ;;
  4317. ;; The main function for the smart quotes sub-system is
  4318. ;; `org-export-activate-smart-quotes', which replaces every quote in
  4319. ;; a given string from the parse tree with its "smart" counterpart.
  4320. ;;
  4321. ;; Dictionary for smart quotes is stored in
  4322. ;; `org-export-smart-quotes-alist'.
  4323. ;;
  4324. ;; Internally, regexps matching potential smart quotes (checks at
  4325. ;; string boundaries are also necessary) are defined in
  4326. ;; `org-export-smart-quotes-regexps'.
  4327. (defconst org-export-smart-quotes-alist
  4328. '(("da"
  4329. ;; one may use: »...«, "...", ›...‹, or '...'.
  4330. ;; http://sproget.dk/raad-og-regler/retskrivningsregler/retskrivningsregler/a7-40-60/a7-58-anforselstegn/
  4331. ;; LaTeX quotes require Babel!
  4332. (opening-double-quote :utf-8 "»" :html "&raquo;" :latex ">>"
  4333. :texinfo "@guillemetright{}")
  4334. (closing-double-quote :utf-8 "«" :html "&laquo;" :latex "<<"
  4335. :texinfo "@guillemetleft{}")
  4336. (opening-single-quote :utf-8 "›" :html "&rsaquo;" :latex "\\frq{}"
  4337. :texinfo "@guilsinglright{}")
  4338. (closing-single-quote :utf-8 "‹" :html "&lsaquo;" :latex "\\flq{}"
  4339. :texinfo "@guilsingleft{}")
  4340. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4341. ("de"
  4342. (opening-double-quote :utf-8 "„" :html "&bdquo;" :latex "\"`"
  4343. :texinfo "@quotedblbase{}")
  4344. (closing-double-quote :utf-8 "“" :html "&ldquo;" :latex "\"'"
  4345. :texinfo "@quotedblleft{}")
  4346. (opening-single-quote :utf-8 "‚" :html "&sbquo;" :latex "\\glq{}"
  4347. :texinfo "@quotesinglbase{}")
  4348. (closing-single-quote :utf-8 "‘" :html "&lsquo;" :latex "\\grq{}"
  4349. :texinfo "@quoteleft{}")
  4350. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4351. ("en"
  4352. (opening-double-quote :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
  4353. (closing-double-quote :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
  4354. (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
  4355. (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
  4356. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4357. ("es"
  4358. (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
  4359. :texinfo "@guillemetleft{}")
  4360. (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
  4361. :texinfo "@guillemetright{}")
  4362. (opening-single-quote :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
  4363. (closing-single-quote :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
  4364. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4365. ("fr"
  4366. (opening-double-quote :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
  4367. :texinfo "@guillemetleft{}@tie{}")
  4368. (closing-double-quote :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
  4369. :texinfo "@tie{}@guillemetright{}")
  4370. (opening-single-quote :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
  4371. :texinfo "@guillemetleft{}@tie{}")
  4372. (closing-single-quote :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
  4373. :texinfo "@tie{}@guillemetright{}")
  4374. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4375. ("no"
  4376. ;; https://nn.wikipedia.org/wiki/Sitatteikn
  4377. (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
  4378. :texinfo "@guillemetleft{}")
  4379. (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
  4380. :texinfo "@guillemetright{}")
  4381. (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
  4382. (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
  4383. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4384. ("nb"
  4385. ;; https://nn.wikipedia.org/wiki/Sitatteikn
  4386. (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
  4387. :texinfo "@guillemetleft{}")
  4388. (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
  4389. :texinfo "@guillemetright{}")
  4390. (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
  4391. (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
  4392. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4393. ("nn"
  4394. ;; https://nn.wikipedia.org/wiki/Sitatteikn
  4395. (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
  4396. :texinfo "@guillemetleft{}")
  4397. (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
  4398. :texinfo "@guillemetright{}")
  4399. (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
  4400. (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
  4401. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4402. ("sv"
  4403. ;; based on https://sv.wikipedia.org/wiki/Citattecken
  4404. (opening-double-quote :utf-8 "”" :html "&rdquo;" :latex "’’" :texinfo "’’")
  4405. (closing-double-quote :utf-8 "”" :html "&rdquo;" :latex "’’" :texinfo "’’")
  4406. (opening-single-quote :utf-8 "’" :html "&rsquo;" :latex "’" :texinfo "`")
  4407. (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "’" :texinfo "'")
  4408. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4409. )
  4410. "Smart quotes translations.
  4411. Alist whose CAR is a language string and CDR is an alist with
  4412. quote type as key and a plist associating various encodings to
  4413. their translation as value.
  4414. A quote type can be any symbol among `opening-double-quote',
  4415. `closing-double-quote', `opening-single-quote',
  4416. `closing-single-quote' and `apostrophe'.
  4417. Valid encodings include `:utf-8', `:html', `:latex' and
  4418. `:texinfo'.
  4419. If no translation is found, the quote character is left as-is.")
  4420. (defconst org-export-smart-quotes-regexps
  4421. (list
  4422. ;; Possible opening quote at beginning of string.
  4423. "\\`\\([\"']\\)\\(\\w\\|\\s.\\|\\s_\\|\\s(\\)"
  4424. ;; Possible closing quote at beginning of string.
  4425. "\\`\\([\"']\\)\\(\\s-\\|\\s)\\|\\s.\\)"
  4426. ;; Possible apostrophe at beginning of string.
  4427. "\\`\\('\\)\\S-"
  4428. ;; Opening single and double quotes.
  4429. "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\(?:\\w\\|\\s.\\|\\s_\\)"
  4430. ;; Closing single and double quotes.
  4431. "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\(?:\\s-\\|\\s)\\|\\s.\\)"
  4432. ;; Apostrophe.
  4433. "\\S-\\('\\)\\S-"
  4434. ;; Possible opening quote at end of string.
  4435. "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\'"
  4436. ;; Possible closing quote at end of string.
  4437. "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\'"
  4438. ;; Possible apostrophe at end of string.
  4439. "\\S-\\('\\)\\'")
  4440. "List of regexps matching a quote or an apostrophe.
  4441. In every regexp, quote or apostrophe matched is put in group 1.")
  4442. (defun org-export-activate-smart-quotes (s encoding info &optional original)
  4443. "Replace regular quotes with \"smart\" quotes in string S.
  4444. ENCODING is a symbol among `:html', `:latex', `:texinfo' and
  4445. `:utf-8'. INFO is a plist used as a communication channel.
  4446. The function has to retrieve information about string
  4447. surroundings in parse tree. It can only happen with an
  4448. unmodified string. Thus, if S has already been through another
  4449. process, a non-nil ORIGINAL optional argument will provide that
  4450. original string.
  4451. Return the new string."
  4452. (if (equal s "") ""
  4453. (let* ((prev (org-export-get-previous-element (or original s) info))
  4454. ;; Try to be flexible when computing number of blanks
  4455. ;; before object. The previous object may be a string
  4456. ;; introduced by the back-end and not completely parsed.
  4457. (pre-blank (and prev
  4458. (or (org-element-property :post-blank prev)
  4459. ;; A string with missing `:post-blank'
  4460. ;; property.
  4461. (and (stringp prev)
  4462. (string-match " *\\'" prev)
  4463. (length (match-string 0 prev)))
  4464. ;; Fallback value.
  4465. 0)))
  4466. (next (org-export-get-next-element (or original s) info))
  4467. (get-smart-quote
  4468. (lambda (q type)
  4469. ;; Return smart quote associated to a give quote Q, as
  4470. ;; a string. TYPE is a symbol among `open', `close' and
  4471. ;; `apostrophe'.
  4472. (let ((key (case type
  4473. (apostrophe 'apostrophe)
  4474. (open (if (equal "'" q) 'opening-single-quote
  4475. 'opening-double-quote))
  4476. (otherwise (if (equal "'" q) 'closing-single-quote
  4477. 'closing-double-quote)))))
  4478. (or (plist-get
  4479. (cdr (assq key
  4480. (cdr (assoc (plist-get info :language)
  4481. org-export-smart-quotes-alist))))
  4482. encoding)
  4483. q)))))
  4484. (if (or (equal "\"" s) (equal "'" s))
  4485. ;; Only a quote: no regexp can match. We have to check both
  4486. ;; sides and decide what to do.
  4487. (cond ((and (not prev) (not next)) s)
  4488. ((not prev) (funcall get-smart-quote s 'open))
  4489. ((and (not next) (zerop pre-blank))
  4490. (funcall get-smart-quote s 'close))
  4491. ((not next) s)
  4492. ((zerop pre-blank) (funcall get-smart-quote s 'apostrophe))
  4493. (t (funcall get-smart-quote 'open)))
  4494. ;; 1. Replace quote character at the beginning of S.
  4495. (cond
  4496. ;; Apostrophe?
  4497. ((and prev (zerop pre-blank)
  4498. (string-match (nth 2 org-export-smart-quotes-regexps) s))
  4499. (setq s (replace-match
  4500. (funcall get-smart-quote (match-string 1 s) 'apostrophe)
  4501. nil t s 1)))
  4502. ;; Closing quote?
  4503. ((and prev (zerop pre-blank)
  4504. (string-match (nth 1 org-export-smart-quotes-regexps) s))
  4505. (setq s (replace-match
  4506. (funcall get-smart-quote (match-string 1 s) 'close)
  4507. nil t s 1)))
  4508. ;; Opening quote?
  4509. ((and (or (not prev) (> pre-blank 0))
  4510. (string-match (nth 0 org-export-smart-quotes-regexps) s))
  4511. (setq s (replace-match
  4512. (funcall get-smart-quote (match-string 1 s) 'open)
  4513. nil t s 1))))
  4514. ;; 2. Replace quotes in the middle of the string.
  4515. (setq s (replace-regexp-in-string
  4516. ;; Opening quotes.
  4517. (nth 3 org-export-smart-quotes-regexps)
  4518. (lambda (text)
  4519. (funcall get-smart-quote (match-string 1 text) 'open))
  4520. s nil t 1))
  4521. (setq s (replace-regexp-in-string
  4522. ;; Closing quotes.
  4523. (nth 4 org-export-smart-quotes-regexps)
  4524. (lambda (text)
  4525. (funcall get-smart-quote (match-string 1 text) 'close))
  4526. s nil t 1))
  4527. (setq s (replace-regexp-in-string
  4528. ;; Apostrophes.
  4529. (nth 5 org-export-smart-quotes-regexps)
  4530. (lambda (text)
  4531. (funcall get-smart-quote (match-string 1 text) 'apostrophe))
  4532. s nil t 1))
  4533. ;; 3. Replace quote character at the end of S.
  4534. (cond
  4535. ;; Apostrophe?
  4536. ((and next (string-match (nth 8 org-export-smart-quotes-regexps) s))
  4537. (setq s (replace-match
  4538. (funcall get-smart-quote (match-string 1 s) 'apostrophe)
  4539. nil t s 1)))
  4540. ;; Closing quote?
  4541. ((and (not next)
  4542. (string-match (nth 7 org-export-smart-quotes-regexps) s))
  4543. (setq s (replace-match
  4544. (funcall get-smart-quote (match-string 1 s) 'close)
  4545. nil t s 1)))
  4546. ;; Opening quote?
  4547. ((and next (string-match (nth 6 org-export-smart-quotes-regexps) s))
  4548. (setq s (replace-match
  4549. (funcall get-smart-quote (match-string 1 s) 'open)
  4550. nil t s 1))))
  4551. ;; Return string with smart quotes.
  4552. s))))
  4553. ;;;; Topology
  4554. ;;
  4555. ;; Here are various functions to retrieve information about the
  4556. ;; neighbourhood of a given element or object. Neighbours of interest
  4557. ;; are direct parent (`org-export-get-parent'), parent headline
  4558. ;; (`org-export-get-parent-headline'), first element containing an
  4559. ;; object, (`org-export-get-parent-element'), parent table
  4560. ;; (`org-export-get-parent-table'), previous element or object
  4561. ;; (`org-export-get-previous-element') and next element or object
  4562. ;; (`org-export-get-next-element').
  4563. ;;
  4564. ;; `org-export-get-genealogy' returns the full genealogy of a given
  4565. ;; element or object, from closest parent to full parse tree.
  4566. (defsubst org-export-get-parent (blob)
  4567. "Return BLOB parent or nil.
  4568. BLOB is the element or object considered."
  4569. (org-element-property :parent blob))
  4570. (defun org-export-get-genealogy (blob)
  4571. "Return full genealogy relative to a given element or object.
  4572. BLOB is the element or object being considered.
  4573. Ancestors are returned from closest to farthest, the last one
  4574. being the full parse tree."
  4575. (let (genealogy (parent blob))
  4576. (while (setq parent (org-element-property :parent parent))
  4577. (push parent genealogy))
  4578. (nreverse genealogy)))
  4579. (defun org-export-get-parent-headline (blob)
  4580. "Return BLOB parent headline or nil.
  4581. BLOB is the element or object being considered."
  4582. (let ((parent blob))
  4583. (while (and (setq parent (org-element-property :parent parent))
  4584. (not (eq (org-element-type parent) 'headline))))
  4585. parent))
  4586. (defun org-export-get-parent-element (object)
  4587. "Return first element containing OBJECT or nil.
  4588. OBJECT is the object to consider."
  4589. (let ((parent object))
  4590. (while (and (setq parent (org-element-property :parent parent))
  4591. (memq (org-element-type parent) org-element-all-objects)))
  4592. parent))
  4593. (defun org-export-get-parent-table (object)
  4594. "Return OBJECT parent table or nil.
  4595. OBJECT is either a `table-cell' or `table-element' type object."
  4596. (let ((parent object))
  4597. (while (and (setq parent (org-element-property :parent parent))
  4598. (not (eq (org-element-type parent) 'table))))
  4599. parent))
  4600. (defun org-export-get-previous-element (blob info &optional n)
  4601. "Return previous element or object.
  4602. BLOB is an element or object. INFO is a plist used as
  4603. a communication channel. Return previous exportable element or
  4604. object, a string, or nil.
  4605. When optional argument N is a positive integer, return a list
  4606. containing up to N siblings before BLOB, from farthest to
  4607. closest. With any other non-nil value, return a list containing
  4608. all of them."
  4609. (let ((siblings
  4610. ;; An object can belong to the contents of its parent or
  4611. ;; to a secondary string. We check the latter option
  4612. ;; first.
  4613. (let ((parent (org-export-get-parent blob)))
  4614. (or (let ((sec-value (org-element-property
  4615. (cdr (assq (org-element-type parent)
  4616. org-element-secondary-value-alist))
  4617. parent)))
  4618. (and (memq blob sec-value) sec-value))
  4619. (org-element-contents parent))))
  4620. prev)
  4621. (catch 'exit
  4622. (mapc (lambda (obj)
  4623. (cond ((memq obj (plist-get info :ignore-list)))
  4624. ((null n) (throw 'exit obj))
  4625. ((not (wholenump n)) (push obj prev))
  4626. ((zerop n) (throw 'exit prev))
  4627. (t (decf n) (push obj prev))))
  4628. (cdr (memq blob (reverse siblings))))
  4629. prev)))
  4630. (defun org-export-get-next-element (blob info &optional n)
  4631. "Return next element or object.
  4632. BLOB is an element or object. INFO is a plist used as
  4633. a communication channel. Return next exportable element or
  4634. object, a string, or nil.
  4635. When optional argument N is a positive integer, return a list
  4636. containing up to N siblings after BLOB, from closest to farthest.
  4637. With any other non-nil value, return a list containing all of
  4638. them."
  4639. (let ((siblings
  4640. ;; An object can belong to the contents of its parent or to
  4641. ;; a secondary string. We check the latter option first.
  4642. (let ((parent (org-export-get-parent blob)))
  4643. (or (let ((sec-value (org-element-property
  4644. (cdr (assq (org-element-type parent)
  4645. org-element-secondary-value-alist))
  4646. parent)))
  4647. (cdr (memq blob sec-value)))
  4648. (cdr (memq blob (org-element-contents parent))))))
  4649. next)
  4650. (catch 'exit
  4651. (mapc (lambda (obj)
  4652. (cond ((memq obj (plist-get info :ignore-list)))
  4653. ((null n) (throw 'exit obj))
  4654. ((not (wholenump n)) (push obj next))
  4655. ((zerop n) (throw 'exit (nreverse next)))
  4656. (t (decf n) (push obj next))))
  4657. siblings)
  4658. (nreverse next))))
  4659. ;;;; Translation
  4660. ;;
  4661. ;; `org-export-translate' translates a string according to the language
  4662. ;; specified by the LANGUAGE keyword. `org-export-dictionary' contains
  4663. ;; the dictionary used for the translation.
  4664. (defconst org-export-dictionary
  4665. '(("%e %n: %c"
  4666. ("fr" :default "%e %n : %c" :html "%e&nbsp;%n&nbsp;: %c"))
  4667. ("Author"
  4668. ("ca" :default "Autor")
  4669. ("cs" :default "Autor")
  4670. ("da" :default "Forfatter")
  4671. ("de" :default "Autor")
  4672. ("eo" :html "A&#365;toro")
  4673. ("es" :default "Autor")
  4674. ("fi" :html "Tekij&auml;")
  4675. ("fr" :default "Auteur")
  4676. ("hu" :default "Szerz&otilde;")
  4677. ("is" :html "H&ouml;fundur")
  4678. ("it" :default "Autore")
  4679. ("ja" :html "&#33879;&#32773;" :utf-8 "著者")
  4680. ("nl" :default "Auteur")
  4681. ("no" :default "Forfatter")
  4682. ("nb" :default "Forfatter")
  4683. ("nn" :default "Forfattar")
  4684. ("pl" :default "Autor")
  4685. ("ru" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
  4686. ("sv" :html "F&ouml;rfattare")
  4687. ("uk" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
  4688. ("zh-CN" :html "&#20316;&#32773;" :utf-8 "作者")
  4689. ("zh-TW" :html "&#20316;&#32773;" :utf-8 "作者"))
  4690. ("Date"
  4691. ("ca" :default "Data")
  4692. ("cs" :default "Datum")
  4693. ("da" :default "Dato")
  4694. ("de" :default "Datum")
  4695. ("eo" :default "Dato")
  4696. ("es" :default "Fecha")
  4697. ("fi" :html "P&auml;iv&auml;m&auml;&auml;r&auml;")
  4698. ("hu" :html "D&aacute;tum")
  4699. ("is" :default "Dagsetning")
  4700. ("it" :default "Data")
  4701. ("ja" :html "&#26085;&#20184;" :utf-8 "日付")
  4702. ("nl" :default "Datum")
  4703. ("no" :default "Dato")
  4704. ("nb" :default "Dato")
  4705. ("nn" :default "Dato")
  4706. ("pl" :default "Data")
  4707. ("ru" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
  4708. ("sv" :default "Datum")
  4709. ("uk" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
  4710. ("zh-CN" :html "&#26085;&#26399;" :utf-8 "日期")
  4711. ("zh-TW" :html "&#26085;&#26399;" :utf-8 "日期"))
  4712. ("Equation"
  4713. ("da" :default "Ligning")
  4714. ("de" :default "Gleichung")
  4715. ("es" :html "Ecuaci&oacute;n" :default "Ecuación")
  4716. ("fr" :ascii "Equation" :default "Équation")
  4717. ("no" :default "Ligning")
  4718. ("nb" :default "Ligning")
  4719. ("nn" :default "Likning")
  4720. ("sv" :default "Ekvation")
  4721. ("zh-CN" :html "&#26041;&#31243;" :utf-8 "方程"))
  4722. ("Figure"
  4723. ("da" :default "Figur")
  4724. ("de" :default "Abbildung")
  4725. ("es" :default "Figura")
  4726. ("ja" :html "&#22259;" :utf-8 "図")
  4727. ("no" :default "Illustrasjon")
  4728. ("nb" :default "Illustrasjon")
  4729. ("nn" :default "Illustrasjon")
  4730. ("sv" :default "Illustration")
  4731. ("zh-CN" :html "&#22270;" :utf-8 "图"))
  4732. ("Figure %d:"
  4733. ("da" :default "Figur %d")
  4734. ("de" :default "Abbildung %d:")
  4735. ("es" :default "Figura %d:")
  4736. ("fr" :default "Figure %d :" :html "Figure&nbsp;%d&nbsp;:")
  4737. ("ja" :html "&#22259;%d: " :utf-8 "図%d: ")
  4738. ("no" :default "Illustrasjon %d")
  4739. ("nb" :default "Illustrasjon %d")
  4740. ("nn" :default "Illustrasjon %d")
  4741. ("sv" :default "Illustration %d")
  4742. ("zh-CN" :html "&#22270;%d&nbsp;" :utf-8 "图%d "))
  4743. ("Footnotes"
  4744. ("ca" :html "Peus de p&agrave;gina")
  4745. ("cs" :default "Pozn\xe1mky pod carou")
  4746. ("da" :default "Fodnoter")
  4747. ("de" :html "Fu&szlig;noten" :default "Fußnoten")
  4748. ("eo" :default "Piednotoj")
  4749. ("es" :html "Nota al pie de p&aacute;gina" :default "Nota al pie de página")
  4750. ("fi" :default "Alaviitteet")
  4751. ("fr" :default "Notes de bas de page")
  4752. ("hu" :html "L&aacute;bjegyzet")
  4753. ("is" :html "Aftanm&aacute;lsgreinar")
  4754. ("it" :html "Note a pi&egrave; di pagina")
  4755. ("ja" :html "&#33050;&#27880;" :utf-8 "脚注")
  4756. ("nl" :default "Voetnoten")
  4757. ("no" :default "Fotnoter")
  4758. ("nb" :default "Fotnoter")
  4759. ("nn" :default "Fotnotar")
  4760. ("pl" :default "Przypis")
  4761. ("ru" :html "&#1057;&#1085;&#1086;&#1089;&#1082;&#1080;" :utf-8 "Сноски")
  4762. ("sv" :default "Fotnoter")
  4763. ("uk" :html "&#1055;&#1088;&#1080;&#1084;&#1110;&#1090;&#1082;&#1080;"
  4764. :utf-8 "Примітки")
  4765. ("zh-CN" :html "&#33050;&#27880;" :utf-8 "脚注")
  4766. ("zh-TW" :html "&#33139;&#35387;" :utf-8 "腳註"))
  4767. ("List of Listings"
  4768. ("da" :default "Programmer")
  4769. ("de" :default "Programmauflistungsverzeichnis")
  4770. ("es" :default "Indice de Listados de programas")
  4771. ("fr" :default "Liste des programmes")
  4772. ("no" :default "Dataprogrammer")
  4773. ("nb" :default "Dataprogrammer")
  4774. ("zh-CN" :html "&#20195;&#30721;&#30446;&#24405;" :utf-8 "代码目录"))
  4775. ("List of Tables"
  4776. ("da" :default "Tabeller")
  4777. ("de" :default "Tabellenverzeichnis")
  4778. ("es" :default "Indice de tablas")
  4779. ("fr" :default "Liste des tableaux")
  4780. ("no" :default "Tabeller")
  4781. ("nb" :default "Tabeller")
  4782. ("nn" :default "Tabeller")
  4783. ("sv" :default "Tabeller")
  4784. ("zh-CN" :html "&#34920;&#26684;&#30446;&#24405;" :utf-8 "表格目录"))
  4785. ("Listing %d:"
  4786. ("da" :default "Program %d")
  4787. ("de" :default "Programmlisting %d")
  4788. ("es" :default "Listado de programa %d")
  4789. ("fr" :default "Programme %d :" :html "Programme&nbsp;%d&nbsp;:")
  4790. ("no" :default "Dataprogram")
  4791. ("nb" :default "Dataprogram")
  4792. ("zh-CN" :html "&#20195;&#30721;%d&nbsp;" :utf-8 "代码%d "))
  4793. ("See section %s"
  4794. ("da" :default "jævnfør afsnit %s")
  4795. ("de" :default "siehe Abschnitt %s")
  4796. ("es" :default "vea seccion %s")
  4797. ("fr" :default "cf. section %s")
  4798. ("zh-CN" :html "&#21442;&#35265;&#31532;%d&#33410;" :utf-8 "参见第%s节"))
  4799. ("Table"
  4800. ("de" :default "Tabelle")
  4801. ("es" :default "Tabla")
  4802. ("fr" :default "Tableau")
  4803. ("ja" :html "&#34920;" :utf-8 "表")
  4804. ("zh-CN" :html "&#34920;" :utf-8 "表"))
  4805. ("Table %d:"
  4806. ("da" :default "Tabel %d")
  4807. ("de" :default "Tabelle %d")
  4808. ("es" :default "Tabla %d")
  4809. ("fr" :default "Tableau %d :")
  4810. ("ja" :html "&#34920;%d:" :utf-8 "表%d:")
  4811. ("no" :default "Tabell %d")
  4812. ("nb" :default "Tabell %d")
  4813. ("nn" :default "Tabell %d")
  4814. ("sv" :default "Tabell %d")
  4815. ("zh-CN" :html "&#34920;%d&nbsp;" :utf-8 "表%d "))
  4816. ("Table of Contents"
  4817. ("ca" :html "&Iacute;ndex")
  4818. ("cs" :default "Obsah")
  4819. ("da" :default "Indhold")
  4820. ("de" :default "Inhaltsverzeichnis")
  4821. ("eo" :default "Enhavo")
  4822. ("es" :html "&Iacute;ndice")
  4823. ("fi" :html "Sis&auml;llysluettelo")
  4824. ("fr" :ascii "Sommaire" :default "Table des matières")
  4825. ("hu" :html "Tartalomjegyz&eacute;k")
  4826. ("is" :default "Efnisyfirlit")
  4827. ("it" :default "Indice")
  4828. ("ja" :html "&#30446;&#27425;" :utf-8 "目次")
  4829. ("nl" :default "Inhoudsopgave")
  4830. ("no" :default "Innhold")
  4831. ("nb" :default "Innhold")
  4832. ("nn" :default "Innhald")
  4833. ("pl" :html "Spis tre&#x015b;ci")
  4834. ("ru" :html "&#1057;&#1086;&#1076;&#1077;&#1088;&#1078;&#1072;&#1085;&#1080;&#1077;"
  4835. :utf-8 "Содержание")
  4836. ("sv" :html "Inneh&aring;ll")
  4837. ("uk" :html "&#1047;&#1084;&#1110;&#1089;&#1090;" :utf-8 "Зміст")
  4838. ("zh-CN" :html "&#30446;&#24405;" :utf-8 "目录")
  4839. ("zh-TW" :html "&#30446;&#37636;" :utf-8 "目錄"))
  4840. ("Unknown reference"
  4841. ("da" :default "ukendt reference")
  4842. ("de" :default "Unbekannter Verweis")
  4843. ("es" :default "referencia desconocida")
  4844. ("fr" :ascii "Destination inconnue" :default "Référence inconnue")
  4845. ("zh-CN" :html "&#26410;&#30693;&#24341;&#29992;" :utf-8 "未知引用")))
  4846. "Dictionary for export engine.
  4847. Alist whose CAR is the string to translate and CDR is an alist
  4848. whose CAR is the language string and CDR is a plist whose
  4849. properties are possible charsets and values translated terms.
  4850. It is used as a database for `org-export-translate'. Since this
  4851. function returns the string as-is if no translation was found,
  4852. the variable only needs to record values different from the
  4853. entry.")
  4854. (defun org-export-translate (s encoding info)
  4855. "Translate string S according to language specification.
  4856. ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
  4857. and `:utf-8'. INFO is a plist used as a communication channel.
  4858. Translation depends on `:language' property. Return the
  4859. translated string. If no translation is found, try to fall back
  4860. to `:default' encoding. If it fails, return S."
  4861. (let* ((lang (plist-get info :language))
  4862. (translations (cdr (assoc lang
  4863. (cdr (assoc s org-export-dictionary))))))
  4864. (or (plist-get translations encoding)
  4865. (plist-get translations :default)
  4866. s)))
  4867. ;;; Asynchronous Export
  4868. ;;
  4869. ;; `org-export-async-start' is the entry point for asynchronous
  4870. ;; export. It recreates current buffer (including visibility,
  4871. ;; narrowing and visited file) in an external Emacs process, and
  4872. ;; evaluates a command there. It then applies a function on the
  4873. ;; returned results in the current process.
  4874. ;;
  4875. ;; At a higher level, `org-export-to-buffer' and `org-export-to-file'
  4876. ;; allow to export to a buffer or a file, asynchronously or not.
  4877. ;;
  4878. ;; `org-export-output-file-name' is an auxiliary function meant to be
  4879. ;; used with `org-export-to-file'. With a given extension, it tries
  4880. ;; to provide a canonical file name to write export output to.
  4881. ;;
  4882. ;; Asynchronously generated results are never displayed directly.
  4883. ;; Instead, they are stored in `org-export-stack-contents'. They can
  4884. ;; then be retrieved by calling `org-export-stack'.
  4885. ;;
  4886. ;; Export Stack is viewed through a dedicated major mode
  4887. ;;`org-export-stack-mode' and tools: `org-export-stack-refresh',
  4888. ;;`org-export-stack-delete', `org-export-stack-view' and
  4889. ;;`org-export-stack-clear'.
  4890. ;;
  4891. ;; For back-ends, `org-export-add-to-stack' add a new source to stack.
  4892. ;; It should be used whenever `org-export-async-start' is called.
  4893. (defmacro org-export-async-start (fun &rest body)
  4894. "Call function FUN on the results returned by BODY evaluation.
  4895. BODY evaluation happens in an asynchronous process, from a buffer
  4896. which is an exact copy of the current one.
  4897. Use `org-export-add-to-stack' in FUN in order to register results
  4898. in the stack.
  4899. This is a low level function. See also `org-export-to-buffer'
  4900. and `org-export-to-file' for more specialized functions."
  4901. (declare (indent 1) (debug t))
  4902. (org-with-gensyms (process temp-file copy-fun proc-buffer coding)
  4903. ;; Write the full sexp evaluating BODY in a copy of the current
  4904. ;; buffer to a temporary file, as it may be too long for program
  4905. ;; args in `start-process'.
  4906. `(with-temp-message "Initializing asynchronous export process"
  4907. (let ((,copy-fun (org-export--generate-copy-script (current-buffer)))
  4908. (,temp-file (make-temp-file "org-export-process"))
  4909. (,coding buffer-file-coding-system))
  4910. (with-temp-file ,temp-file
  4911. (insert
  4912. ;; Null characters (from variable values) are inserted
  4913. ;; within the file. As a consequence, coding system for
  4914. ;; buffer contents will not be recognized properly. So,
  4915. ;; we make sure it is the same as the one used to display
  4916. ;; the original buffer.
  4917. (format ";; -*- coding: %s; -*-\n%S"
  4918. ,coding
  4919. `(with-temp-buffer
  4920. (when org-export-async-debug '(setq debug-on-error t))
  4921. ;; Ignore `kill-emacs-hook' and code evaluation
  4922. ;; queries from Babel as we need a truly
  4923. ;; non-interactive process.
  4924. (setq kill-emacs-hook nil
  4925. org-babel-confirm-evaluate-answer-no t)
  4926. ;; Initialize export framework.
  4927. (require 'ox)
  4928. ;; Re-create current buffer there.
  4929. (funcall ,,copy-fun)
  4930. (restore-buffer-modified-p nil)
  4931. ;; Sexp to evaluate in the buffer.
  4932. (print (progn ,,@body))))))
  4933. ;; Start external process.
  4934. (let* ((process-connection-type nil)
  4935. (,proc-buffer (generate-new-buffer-name "*Org Export Process*"))
  4936. (,process
  4937. (start-process
  4938. "org-export-process" ,proc-buffer
  4939. (expand-file-name invocation-name invocation-directory)
  4940. "-Q" "--batch"
  4941. "-l" org-export-async-init-file
  4942. "-l" ,temp-file)))
  4943. ;; Register running process in stack.
  4944. (org-export-add-to-stack (get-buffer ,proc-buffer) nil ,process)
  4945. ;; Set-up sentinel in order to catch results.
  4946. (let ((handler ,fun))
  4947. (set-process-sentinel
  4948. ,process
  4949. `(lambda (p status)
  4950. (let ((proc-buffer (process-buffer p)))
  4951. (when (eq (process-status p) 'exit)
  4952. (unwind-protect
  4953. (if (zerop (process-exit-status p))
  4954. (unwind-protect
  4955. (let ((results
  4956. (with-current-buffer proc-buffer
  4957. (goto-char (point-max))
  4958. (backward-sexp)
  4959. (read (current-buffer)))))
  4960. (funcall ,handler results))
  4961. (unless org-export-async-debug
  4962. (and (get-buffer proc-buffer)
  4963. (kill-buffer proc-buffer))))
  4964. (org-export-add-to-stack proc-buffer nil p)
  4965. (ding)
  4966. (message "Process '%s' exited abnormally" p))
  4967. (unless org-export-async-debug
  4968. (delete-file ,,temp-file)))))))))))))
  4969. ;;;###autoload
  4970. (defun org-export-to-buffer
  4971. (backend buffer
  4972. &optional async subtreep visible-only body-only ext-plist
  4973. post-process)
  4974. "Call `org-export-as' with output to a specified buffer.
  4975. BACKEND is either an export back-end, as returned by, e.g.,
  4976. `org-export-create-backend', or a symbol referring to
  4977. a registered back-end.
  4978. BUFFER is the name of the output buffer. If it already exists,
  4979. it will be erased first, otherwise, it will be created.
  4980. A non-nil optional argument ASYNC means the process should happen
  4981. asynchronously. The resulting buffer should then be accessible
  4982. through the `org-export-stack' interface. When ASYNC is nil, the
  4983. buffer is displayed if `org-export-show-temporary-export-buffer'
  4984. is non-nil.
  4985. Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
  4986. EXT-PLIST are similar to those used in `org-export-as', which
  4987. see.
  4988. Optional argument POST-PROCESS is a function which should accept
  4989. no argument. It is always called within the current process,
  4990. from BUFFER, with point at its beginning. Export back-ends can
  4991. use it to set a major mode there, e.g,
  4992. \(defun org-latex-export-as-latex
  4993. \(&optional async subtreep visible-only body-only ext-plist)
  4994. \(interactive)
  4995. \(org-export-to-buffer 'latex \"*Org LATEX Export*\"
  4996. async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
  4997. This function returns BUFFER."
  4998. (declare (indent 2))
  4999. (if async
  5000. (org-export-async-start
  5001. `(lambda (output)
  5002. (with-current-buffer (get-buffer-create ,buffer)
  5003. (erase-buffer)
  5004. (setq buffer-file-coding-system ',buffer-file-coding-system)
  5005. (insert output)
  5006. (goto-char (point-min))
  5007. (org-export-add-to-stack (current-buffer) ',backend)
  5008. (ignore-errors (funcall ,post-process))))
  5009. `(org-export-as
  5010. ',backend ,subtreep ,visible-only ,body-only ',ext-plist))
  5011. (let ((output
  5012. (org-export-as backend subtreep visible-only body-only ext-plist))
  5013. (buffer (get-buffer-create buffer))
  5014. (encoding buffer-file-coding-system))
  5015. (when (and (org-string-nw-p output) (org-export--copy-to-kill-ring-p))
  5016. (org-kill-new output))
  5017. (with-current-buffer buffer
  5018. (erase-buffer)
  5019. (setq buffer-file-coding-system encoding)
  5020. (insert output)
  5021. (goto-char (point-min))
  5022. (and (functionp post-process) (funcall post-process)))
  5023. (when org-export-show-temporary-export-buffer
  5024. (switch-to-buffer-other-window buffer))
  5025. buffer)))
  5026. ;;;###autoload
  5027. (defun org-export-to-file
  5028. (backend file &optional async subtreep visible-only body-only ext-plist
  5029. post-process)
  5030. "Call `org-export-as' with output to a specified file.
  5031. BACKEND is either an export back-end, as returned by, e.g.,
  5032. `org-export-create-backend', or a symbol referring to
  5033. a registered back-end. FILE is the name of the output file, as
  5034. a string.
  5035. A non-nil optional argument ASYNC means the process should happen
  5036. asynchronously. The resulting buffer will then be accessible
  5037. through the `org-export-stack' interface.
  5038. Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
  5039. EXT-PLIST are similar to those used in `org-export-as', which
  5040. see.
  5041. Optional argument POST-PROCESS is called with FILE as its
  5042. argument and happens asynchronously when ASYNC is non-nil. It
  5043. has to return a file name, or nil. Export back-ends can use this
  5044. to send the output file through additional processing, e.g,
  5045. \(defun org-latex-export-to-latex
  5046. \(&optional async subtreep visible-only body-only ext-plist)
  5047. \(interactive)
  5048. \(let ((outfile (org-export-output-file-name \".tex\" subtreep)))
  5049. \(org-export-to-file 'latex outfile
  5050. async subtreep visible-only body-only ext-plist
  5051. \(lambda (file) (org-latex-compile file)))
  5052. The function returns either a file name returned by POST-PROCESS,
  5053. or FILE."
  5054. (declare (indent 2))
  5055. (if (not (file-writable-p file)) (error "Output file not writable")
  5056. (let ((encoding (or org-export-coding-system buffer-file-coding-system)))
  5057. (if async
  5058. (org-export-async-start
  5059. `(lambda (file)
  5060. (org-export-add-to-stack (expand-file-name file) ',backend))
  5061. `(let ((output
  5062. (org-export-as
  5063. ',backend ,subtreep ,visible-only ,body-only
  5064. ',ext-plist)))
  5065. (with-temp-buffer
  5066. (insert output)
  5067. (let ((coding-system-for-write ',encoding))
  5068. (write-file ,file)))
  5069. (or (ignore-errors (funcall ',post-process ,file)) ,file)))
  5070. (let ((output (org-export-as
  5071. backend subtreep visible-only body-only ext-plist)))
  5072. (with-temp-buffer
  5073. (insert output)
  5074. (let ((coding-system-for-write encoding))
  5075. (write-file file)))
  5076. (when (and (org-export--copy-to-kill-ring-p) (org-string-nw-p output))
  5077. (org-kill-new output))
  5078. ;; Get proper return value.
  5079. (or (and (functionp post-process) (funcall post-process file))
  5080. file))))))
  5081. (defun org-export-output-file-name (extension &optional subtreep pub-dir)
  5082. "Return output file's name according to buffer specifications.
  5083. EXTENSION is a string representing the output file extension,
  5084. with the leading dot.
  5085. With a non-nil optional argument SUBTREEP, try to determine
  5086. output file's name by looking for \"EXPORT_FILE_NAME\" property
  5087. of subtree at point.
  5088. When optional argument PUB-DIR is set, use it as the publishing
  5089. directory.
  5090. When optional argument VISIBLE-ONLY is non-nil, don't export
  5091. contents of hidden elements.
  5092. Return file name as a string."
  5093. (let* ((visited-file (buffer-file-name (buffer-base-buffer)))
  5094. (base-name
  5095. ;; File name may come from EXPORT_FILE_NAME subtree
  5096. ;; property, assuming point is at beginning of said
  5097. ;; sub-tree.
  5098. (file-name-sans-extension
  5099. (or (and subtreep
  5100. (org-entry-get
  5101. (save-excursion
  5102. (ignore-errors (org-back-to-heading) (point)))
  5103. "EXPORT_FILE_NAME" t))
  5104. ;; File name may be extracted from buffer's associated
  5105. ;; file, if any.
  5106. (and visited-file (file-name-nondirectory visited-file))
  5107. ;; Can't determine file name on our own: Ask user.
  5108. (let ((read-file-name-function
  5109. (and org-completion-use-ido 'ido-read-file-name)))
  5110. (read-file-name
  5111. "Output file: " pub-dir nil nil nil
  5112. (lambda (name)
  5113. (string= (file-name-extension name t) extension)))))))
  5114. (output-file
  5115. ;; Build file name. Enforce EXTENSION over whatever user
  5116. ;; may have come up with. PUB-DIR, if defined, always has
  5117. ;; precedence over any provided path.
  5118. (cond
  5119. (pub-dir
  5120. (concat (file-name-as-directory pub-dir)
  5121. (file-name-nondirectory base-name)
  5122. extension))
  5123. ((file-name-absolute-p base-name) (concat base-name extension))
  5124. (t (concat (file-name-as-directory ".") base-name extension)))))
  5125. ;; If writing to OUTPUT-FILE would overwrite original file, append
  5126. ;; EXTENSION another time to final name.
  5127. (if (and visited-file (org-file-equal-p visited-file output-file))
  5128. (concat output-file extension)
  5129. output-file)))
  5130. (defun org-export-add-to-stack (source backend &optional process)
  5131. "Add a new result to export stack if not present already.
  5132. SOURCE is a buffer or a file name containing export results.
  5133. BACKEND is a symbol representing export back-end used to generate
  5134. it.
  5135. Entries already pointing to SOURCE and unavailable entries are
  5136. removed beforehand. Return the new stack."
  5137. (setq org-export-stack-contents
  5138. (cons (list source backend (or process (current-time)))
  5139. (org-export-stack-remove source))))
  5140. (defun org-export-stack ()
  5141. "Menu for asynchronous export results and running processes."
  5142. (interactive)
  5143. (let ((buffer (get-buffer-create "*Org Export Stack*")))
  5144. (set-buffer buffer)
  5145. (when (zerop (buffer-size)) (org-export-stack-mode))
  5146. (org-export-stack-refresh)
  5147. (pop-to-buffer buffer))
  5148. (message "Type \"q\" to quit, \"?\" for help"))
  5149. (defun org-export--stack-source-at-point ()
  5150. "Return source from export results at point in stack."
  5151. (let ((source (car (nth (1- (org-current-line)) org-export-stack-contents))))
  5152. (if (not source) (error "Source unavailable, please refresh buffer")
  5153. (let ((source-name (if (stringp source) source (buffer-name source))))
  5154. (if (save-excursion
  5155. (beginning-of-line)
  5156. (looking-at (concat ".* +" (regexp-quote source-name) "$")))
  5157. source
  5158. ;; SOURCE is not consistent with current line. The stack
  5159. ;; view is outdated.
  5160. (error "Source unavailable; type `g' to update buffer"))))))
  5161. (defun org-export-stack-clear ()
  5162. "Remove all entries from export stack."
  5163. (interactive)
  5164. (setq org-export-stack-contents nil))
  5165. (defun org-export-stack-refresh (&rest dummy)
  5166. "Refresh the asynchronous export stack.
  5167. DUMMY is ignored. Unavailable sources are removed from the list.
  5168. Return the new stack."
  5169. (let ((inhibit-read-only t))
  5170. (org-preserve-lc
  5171. (erase-buffer)
  5172. (insert (concat
  5173. (let ((counter 0))
  5174. (mapconcat
  5175. (lambda (entry)
  5176. (let ((proc-p (processp (nth 2 entry))))
  5177. (concat
  5178. ;; Back-end.
  5179. (format " %-12s " (or (nth 1 entry) ""))
  5180. ;; Age.
  5181. (let ((data (nth 2 entry)))
  5182. (if proc-p (format " %6s " (process-status data))
  5183. ;; Compute age of the results.
  5184. (org-format-seconds
  5185. "%4h:%.2m "
  5186. (float-time (time-since data)))))
  5187. ;; Source.
  5188. (format " %s"
  5189. (let ((source (car entry)))
  5190. (if (stringp source) source
  5191. (buffer-name source)))))))
  5192. ;; Clear stack from exited processes, dead buffers or
  5193. ;; non-existent files.
  5194. (setq org-export-stack-contents
  5195. (org-remove-if-not
  5196. (lambda (el)
  5197. (if (processp (nth 2 el))
  5198. (buffer-live-p (process-buffer (nth 2 el)))
  5199. (let ((source (car el)))
  5200. (if (bufferp source) (buffer-live-p source)
  5201. (file-exists-p source)))))
  5202. org-export-stack-contents)) "\n")))))))
  5203. (defun org-export-stack-remove (&optional source)
  5204. "Remove export results at point from stack.
  5205. If optional argument SOURCE is non-nil, remove it instead."
  5206. (interactive)
  5207. (let ((source (or source (org-export--stack-source-at-point))))
  5208. (setq org-export-stack-contents
  5209. (org-remove-if (lambda (el) (equal (car el) source))
  5210. org-export-stack-contents))))
  5211. (defun org-export-stack-view (&optional in-emacs)
  5212. "View export results at point in stack.
  5213. With an optional prefix argument IN-EMACS, force viewing files
  5214. within Emacs."
  5215. (interactive "P")
  5216. (let ((source (org-export--stack-source-at-point)))
  5217. (cond ((processp source)
  5218. (org-switch-to-buffer-other-window (process-buffer source)))
  5219. ((bufferp source) (org-switch-to-buffer-other-window source))
  5220. (t (org-open-file source in-emacs)))))
  5221. (defvar org-export-stack-mode-map
  5222. (let ((km (make-sparse-keymap)))
  5223. (define-key km " " 'next-line)
  5224. (define-key km "n" 'next-line)
  5225. (define-key km "\C-n" 'next-line)
  5226. (define-key km [down] 'next-line)
  5227. (define-key km "p" 'previous-line)
  5228. (define-key km "\C-p" 'previous-line)
  5229. (define-key km "\C-?" 'previous-line)
  5230. (define-key km [up] 'previous-line)
  5231. (define-key km "C" 'org-export-stack-clear)
  5232. (define-key km "v" 'org-export-stack-view)
  5233. (define-key km (kbd "RET") 'org-export-stack-view)
  5234. (define-key km "d" 'org-export-stack-remove)
  5235. km)
  5236. "Keymap for Org Export Stack.")
  5237. (define-derived-mode org-export-stack-mode special-mode "Org-Stack"
  5238. "Mode for displaying asynchronous export stack.
  5239. Type \\[org-export-stack] to visualize the asynchronous export
  5240. stack.
  5241. In an Org Export Stack buffer, use \\<org-export-stack-mode-map>\\[org-export-stack-view] to view export output
  5242. on current line, \\[org-export-stack-remove] to remove it from the stack and \\[org-export-stack-clear] to clear
  5243. stack completely.
  5244. Removing entries in an Org Export Stack buffer doesn't affect
  5245. files or buffers, only the display.
  5246. \\{org-export-stack-mode-map}"
  5247. (abbrev-mode 0)
  5248. (auto-fill-mode 0)
  5249. (setq buffer-read-only t
  5250. buffer-undo-list t
  5251. truncate-lines t
  5252. header-line-format
  5253. '(:eval
  5254. (format " %-12s | %6s | %s" "Back-End" "Age" "Source")))
  5255. (org-add-hook 'post-command-hook 'org-export-stack-refresh nil t)
  5256. (set (make-local-variable 'revert-buffer-function)
  5257. 'org-export-stack-refresh))
  5258. ;;; The Dispatcher
  5259. ;;
  5260. ;; `org-export-dispatch' is the standard interactive way to start an
  5261. ;; export process. It uses `org-export--dispatch-ui' as a subroutine
  5262. ;; for its interface, which, in turn, delegates response to key
  5263. ;; pressed to `org-export--dispatch-action'.
  5264. ;;;###autoload
  5265. (defun org-export-dispatch (&optional arg)
  5266. "Export dispatcher for Org mode.
  5267. It provides an access to common export related tasks in a buffer.
  5268. Its interface comes in two flavors: standard and expert.
  5269. While both share the same set of bindings, only the former
  5270. displays the valid keys associations in a dedicated buffer.
  5271. Scrolling (resp. line-wise motion) in this buffer is done with
  5272. SPC and DEL (resp. C-n and C-p) keys.
  5273. Set variable `org-export-dispatch-use-expert-ui' to switch to one
  5274. flavor or the other.
  5275. When ARG is \\[universal-argument], repeat the last export action, with the same set
  5276. of options used back then, on the current buffer.
  5277. When ARG is \\[universal-argument] \\[universal-argument], display the asynchronous export stack."
  5278. (interactive "P")
  5279. (let* ((input
  5280. (cond ((equal arg '(16)) '(stack))
  5281. ((and arg org-export-dispatch-last-action))
  5282. (t (save-window-excursion
  5283. (unwind-protect
  5284. (progn
  5285. ;; Remember where we are
  5286. (move-marker org-export-dispatch-last-position
  5287. (point)
  5288. (org-base-buffer (current-buffer)))
  5289. ;; Get and store an export command
  5290. (setq org-export-dispatch-last-action
  5291. (org-export--dispatch-ui
  5292. (list org-export-initial-scope
  5293. (and org-export-in-background 'async))
  5294. nil
  5295. org-export-dispatch-use-expert-ui)))
  5296. (and (get-buffer "*Org Export Dispatcher*")
  5297. (kill-buffer "*Org Export Dispatcher*")))))))
  5298. (action (car input))
  5299. (optns (cdr input)))
  5300. (unless (memq 'subtree optns)
  5301. (move-marker org-export-dispatch-last-position nil))
  5302. (case action
  5303. ;; First handle special hard-coded actions.
  5304. (template (org-export-insert-default-template nil optns))
  5305. (stack (org-export-stack))
  5306. (publish-current-file
  5307. (org-publish-current-file (memq 'force optns) (memq 'async optns)))
  5308. (publish-current-project
  5309. (org-publish-current-project (memq 'force optns) (memq 'async optns)))
  5310. (publish-choose-project
  5311. (org-publish (assoc (org-icompleting-read
  5312. "Publish project: "
  5313. org-publish-project-alist nil t)
  5314. org-publish-project-alist)
  5315. (memq 'force optns)
  5316. (memq 'async optns)))
  5317. (publish-all (org-publish-all (memq 'force optns) (memq 'async optns)))
  5318. (otherwise
  5319. (save-excursion
  5320. (when arg
  5321. ;; Repeating command, maybe move cursor to restore subtree
  5322. ;; context.
  5323. (if (eq (marker-buffer org-export-dispatch-last-position)
  5324. (org-base-buffer (current-buffer)))
  5325. (goto-char org-export-dispatch-last-position)
  5326. ;; We are in a different buffer, forget position.
  5327. (move-marker org-export-dispatch-last-position nil)))
  5328. (funcall action
  5329. ;; Return a symbol instead of a list to ease
  5330. ;; asynchronous export macro use.
  5331. (and (memq 'async optns) t)
  5332. (and (memq 'subtree optns) t)
  5333. (and (memq 'visible optns) t)
  5334. (and (memq 'body optns) t)))))))
  5335. (defun org-export--dispatch-ui (options first-key expertp)
  5336. "Handle interface for `org-export-dispatch'.
  5337. OPTIONS is a list containing current interactive options set for
  5338. export. It can contain any of the following symbols:
  5339. `body' toggles a body-only export
  5340. `subtree' restricts export to current subtree
  5341. `visible' restricts export to visible part of buffer.
  5342. `force' force publishing files.
  5343. `async' use asynchronous export process
  5344. FIRST-KEY is the key pressed to select the first level menu. It
  5345. is nil when this menu hasn't been selected yet.
  5346. EXPERTP, when non-nil, triggers expert UI. In that case, no help
  5347. buffer is provided, but indications about currently active
  5348. options are given in the prompt. Moreover, \[?] allows to switch
  5349. back to standard interface."
  5350. (let* ((fontify-key
  5351. (lambda (key &optional access-key)
  5352. ;; Fontify KEY string. Optional argument ACCESS-KEY, when
  5353. ;; non-nil is the required first-level key to activate
  5354. ;; KEY. When its value is t, activate KEY independently
  5355. ;; on the first key, if any. A nil value means KEY will
  5356. ;; only be activated at first level.
  5357. (if (or (eq access-key t) (eq access-key first-key))
  5358. (org-propertize key 'face 'org-warning)
  5359. key)))
  5360. (fontify-value
  5361. (lambda (value)
  5362. ;; Fontify VALUE string.
  5363. (org-propertize value 'face 'font-lock-variable-name-face)))
  5364. ;; Prepare menu entries by extracting them from registered
  5365. ;; back-ends and sorting them by access key and by ordinal,
  5366. ;; if any.
  5367. (entries
  5368. (sort (sort (delq nil
  5369. (mapcar 'org-export-backend-menu
  5370. org-export--registered-backends))
  5371. (lambda (a b)
  5372. (let ((key-a (nth 1 a))
  5373. (key-b (nth 1 b)))
  5374. (cond ((and (numberp key-a) (numberp key-b))
  5375. (< key-a key-b))
  5376. ((numberp key-b) t)))))
  5377. 'car-less-than-car))
  5378. ;; Compute a list of allowed keys based on the first key
  5379. ;; pressed, if any. Some keys
  5380. ;; (?^B, ?^V, ?^S, ?^F, ?^A, ?&, ?# and ?q) are always
  5381. ;; available.
  5382. (allowed-keys
  5383. (nconc (list 2 22 19 6 1)
  5384. (if (not first-key) (org-uniquify (mapcar 'car entries))
  5385. (let (sub-menu)
  5386. (dolist (entry entries (sort (mapcar 'car sub-menu) '<))
  5387. (when (eq (car entry) first-key)
  5388. (setq sub-menu (append (nth 2 entry) sub-menu))))))
  5389. (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
  5390. ((not first-key) (list ?P)))
  5391. (list ?& ?#)
  5392. (when expertp (list ??))
  5393. (list ?q)))
  5394. ;; Build the help menu for standard UI.
  5395. (help
  5396. (unless expertp
  5397. (concat
  5398. ;; Options are hard-coded.
  5399. (format "[%s] Body only: %s [%s] Visible only: %s
  5400. \[%s] Export scope: %s [%s] Force publishing: %s
  5401. \[%s] Async export: %s\n\n"
  5402. (funcall fontify-key "C-b" t)
  5403. (funcall fontify-value
  5404. (if (memq 'body options) "On " "Off"))
  5405. (funcall fontify-key "C-v" t)
  5406. (funcall fontify-value
  5407. (if (memq 'visible options) "On " "Off"))
  5408. (funcall fontify-key "C-s" t)
  5409. (funcall fontify-value
  5410. (if (memq 'subtree options) "Subtree" "Buffer "))
  5411. (funcall fontify-key "C-f" t)
  5412. (funcall fontify-value
  5413. (if (memq 'force options) "On " "Off"))
  5414. (funcall fontify-key "C-a" t)
  5415. (funcall fontify-value
  5416. (if (memq 'async options) "On " "Off")))
  5417. ;; Display registered back-end entries. When a key
  5418. ;; appears for the second time, do not create another
  5419. ;; entry, but append its sub-menu to existing menu.
  5420. (let (last-key)
  5421. (mapconcat
  5422. (lambda (entry)
  5423. (let ((top-key (car entry)))
  5424. (concat
  5425. (unless (eq top-key last-key)
  5426. (setq last-key top-key)
  5427. (format "\n[%s] %s\n"
  5428. (funcall fontify-key (char-to-string top-key))
  5429. (nth 1 entry)))
  5430. (let ((sub-menu (nth 2 entry)))
  5431. (unless (functionp sub-menu)
  5432. ;; Split sub-menu into two columns.
  5433. (let ((index -1))
  5434. (concat
  5435. (mapconcat
  5436. (lambda (sub-entry)
  5437. (incf index)
  5438. (format
  5439. (if (zerop (mod index 2)) " [%s] %-26s"
  5440. "[%s] %s\n")
  5441. (funcall fontify-key
  5442. (char-to-string (car sub-entry))
  5443. top-key)
  5444. (nth 1 sub-entry)))
  5445. sub-menu "")
  5446. (when (zerop (mod index 2)) "\n"))))))))
  5447. entries ""))
  5448. ;; Publishing menu is hard-coded.
  5449. (format "\n[%s] Publish
  5450. [%s] Current file [%s] Current project
  5451. [%s] Choose project [%s] All projects\n\n\n"
  5452. (funcall fontify-key "P")
  5453. (funcall fontify-key "f" ?P)
  5454. (funcall fontify-key "p" ?P)
  5455. (funcall fontify-key "x" ?P)
  5456. (funcall fontify-key "a" ?P))
  5457. (format "[%s] Export stack [%s] Insert template\n"
  5458. (funcall fontify-key "&" t)
  5459. (funcall fontify-key "#" t))
  5460. (format "[%s] %s"
  5461. (funcall fontify-key "q" t)
  5462. (if first-key "Main menu" "Exit")))))
  5463. ;; Build prompts for both standard and expert UI.
  5464. (standard-prompt (unless expertp "Export command: "))
  5465. (expert-prompt
  5466. (when expertp
  5467. (format
  5468. "Export command (C-%s%s%s%s%s) [%s]: "
  5469. (if (memq 'body options) (funcall fontify-key "b" t) "b")
  5470. (if (memq 'visible options) (funcall fontify-key "v" t) "v")
  5471. (if (memq 'subtree options) (funcall fontify-key "s" t) "s")
  5472. (if (memq 'force options) (funcall fontify-key "f" t) "f")
  5473. (if (memq 'async options) (funcall fontify-key "a" t) "a")
  5474. (mapconcat (lambda (k)
  5475. ;; Strip control characters.
  5476. (unless (< k 27) (char-to-string k)))
  5477. allowed-keys "")))))
  5478. ;; With expert UI, just read key with a fancy prompt. In standard
  5479. ;; UI, display an intrusive help buffer.
  5480. (if expertp
  5481. (org-export--dispatch-action
  5482. expert-prompt allowed-keys entries options first-key expertp)
  5483. ;; At first call, create frame layout in order to display menu.
  5484. (unless (get-buffer "*Org Export Dispatcher*")
  5485. (delete-other-windows)
  5486. (org-switch-to-buffer-other-window
  5487. (get-buffer-create "*Org Export Dispatcher*"))
  5488. (setq cursor-type nil
  5489. header-line-format "Use SPC, DEL, C-n or C-p to navigate.")
  5490. ;; Make sure that invisible cursor will not highlight square
  5491. ;; brackets.
  5492. (set-syntax-table (copy-syntax-table))
  5493. (modify-syntax-entry ?\[ "w"))
  5494. ;; At this point, the buffer containing the menu exists and is
  5495. ;; visible in the current window. So, refresh it.
  5496. (with-current-buffer "*Org Export Dispatcher*"
  5497. ;; Refresh help. Maintain display continuity by re-visiting
  5498. ;; previous window position.
  5499. (let ((pos (window-start)))
  5500. (erase-buffer)
  5501. (insert help)
  5502. (set-window-start nil pos)))
  5503. (org-fit-window-to-buffer)
  5504. (org-export--dispatch-action
  5505. standard-prompt allowed-keys entries options first-key expertp))))
  5506. (defun org-export--dispatch-action
  5507. (prompt allowed-keys entries options first-key expertp)
  5508. "Read a character from command input and act accordingly.
  5509. PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
  5510. a list of characters available at a given step in the process.
  5511. ENTRIES is a list of menu entries. OPTIONS, FIRST-KEY and
  5512. EXPERTP are the same as defined in `org-export--dispatch-ui',
  5513. which see.
  5514. Toggle export options when required. Otherwise, return value is
  5515. a list with action as CAR and a list of interactive export
  5516. options as CDR."
  5517. (let (key)
  5518. ;; Scrolling: when in non-expert mode, act on motion keys (C-n,
  5519. ;; C-p, SPC, DEL).
  5520. (while (and (setq key (read-char-exclusive prompt))
  5521. (not expertp)
  5522. (memq key '(14 16 ?\s ?\d)))
  5523. (case key
  5524. (14 (if (not (pos-visible-in-window-p (point-max)))
  5525. (ignore-errors (scroll-up 1))
  5526. (message "End of buffer")
  5527. (sit-for 1)))
  5528. (16 (if (not (pos-visible-in-window-p (point-min)))
  5529. (ignore-errors (scroll-down 1))
  5530. (message "Beginning of buffer")
  5531. (sit-for 1)))
  5532. (?\s (if (not (pos-visible-in-window-p (point-max)))
  5533. (scroll-up nil)
  5534. (message "End of buffer")
  5535. (sit-for 1)))
  5536. (?\d (if (not (pos-visible-in-window-p (point-min)))
  5537. (scroll-down nil)
  5538. (message "Beginning of buffer")
  5539. (sit-for 1)))))
  5540. (cond
  5541. ;; Ignore undefined associations.
  5542. ((not (memq key allowed-keys))
  5543. (ding)
  5544. (unless expertp (message "Invalid key") (sit-for 1))
  5545. (org-export--dispatch-ui options first-key expertp))
  5546. ;; q key at first level aborts export. At second level, cancel
  5547. ;; first key instead.
  5548. ((eq key ?q) (if (not first-key) (error "Export aborted")
  5549. (org-export--dispatch-ui options nil expertp)))
  5550. ;; Help key: Switch back to standard interface if expert UI was
  5551. ;; active.
  5552. ((eq key ??) (org-export--dispatch-ui options first-key nil))
  5553. ;; Send request for template insertion along with export scope.
  5554. ((eq key ?#) (cons 'template (memq 'subtree options)))
  5555. ;; Switch to asynchronous export stack.
  5556. ((eq key ?&) '(stack))
  5557. ;; Toggle options: C-b (2) C-v (22) C-s (19) C-f (6) C-a (1).
  5558. ((memq key '(2 22 19 6 1))
  5559. (org-export--dispatch-ui
  5560. (let ((option (case key (2 'body) (22 'visible) (19 'subtree)
  5561. (6 'force) (1 'async))))
  5562. (if (memq option options) (remq option options)
  5563. (cons option options)))
  5564. first-key expertp))
  5565. ;; Action selected: Send key and options back to
  5566. ;; `org-export-dispatch'.
  5567. ((or first-key (functionp (nth 2 (assq key entries))))
  5568. (cons (cond
  5569. ((not first-key) (nth 2 (assq key entries)))
  5570. ;; Publishing actions are hard-coded. Send a special
  5571. ;; signal to `org-export-dispatch'.
  5572. ((eq first-key ?P)
  5573. (case key
  5574. (?f 'publish-current-file)
  5575. (?p 'publish-current-project)
  5576. (?x 'publish-choose-project)
  5577. (?a 'publish-all)))
  5578. ;; Return first action associated to FIRST-KEY + KEY
  5579. ;; path. Indeed, derived backends can share the same
  5580. ;; FIRST-KEY.
  5581. (t (catch 'found
  5582. (mapc (lambda (entry)
  5583. (let ((match (assq key (nth 2 entry))))
  5584. (when match (throw 'found (nth 2 match)))))
  5585. (member (assq first-key entries) entries)))))
  5586. options))
  5587. ;; Otherwise, enter sub-menu.
  5588. (t (org-export--dispatch-ui options key expertp)))))
  5589. (provide 'ox)
  5590. ;; Local variables:
  5591. ;; generated-autoload-file: "org-loaddefs.el"
  5592. ;; End:
  5593. ;;; ox.el ends here