ox.el 284 KB

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