ox.el 239 KB

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