ox.el 251 KB

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