ox.el 250 KB

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