ox.el 270 KB

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