ox.el 279 KB

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