ox.el 281 KB

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