ox.el 274 KB

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