ox.el 238 KB

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