ox.el 256 KB

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