ox.el 272 KB

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