ox.el 264 KB

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