ox.el 278 KB

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