ox.el 262 KB

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