ox.el 248 KB

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