ox.el 235 KB

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