ox.el 252 KB

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