ox.el 269 KB

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