ox.el 257 KB

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