ox.el 277 KB

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