ox.el 270 KB

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