ox.el 250 KB

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