ox.el 280 KB

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