ox.el 255 KB

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