ox.el 272 KB

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