ox.el 251 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526
  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. (let ((select (plist-get info :select-tags)))
  1537. (if (cl-some (lambda (tag) (member tag select)) (plist-get info :filetags))
  1538. ;; If FILETAGS contains a select tag, every headline or
  1539. ;; inlinetask is returned.
  1540. (org-element-map data '(headline inlinetask) #'identity)
  1541. (letrec ((selected-trees nil)
  1542. (walk-data
  1543. (lambda (data genealogy)
  1544. (let ((type (org-element-type data)))
  1545. (cond
  1546. ((memq type '(headline inlinetask))
  1547. (let ((tags (org-element-property :tags data)))
  1548. (if (cl-some (lambda (tag) (member tag select)) tags)
  1549. ;; When a select tag is found, mark full
  1550. ;; genealogy and every headline within the
  1551. ;; tree as acceptable.
  1552. (setq selected-trees
  1553. (append
  1554. genealogy
  1555. (org-element-map data '(headline inlinetask)
  1556. #'identity)
  1557. selected-trees))
  1558. ;; If at a headline, continue searching in
  1559. ;; tree, recursively.
  1560. (when (eq type 'headline)
  1561. (dolist (el (org-element-contents data))
  1562. (funcall walk-data el (cons data genealogy)))))))
  1563. ((or (eq type 'org-data)
  1564. (memq type org-element-greater-elements))
  1565. (dolist (el (org-element-contents data))
  1566. (funcall walk-data el genealogy))))))))
  1567. (funcall walk-data data nil)
  1568. selected-trees))))
  1569. (defun org-export--skip-p (blob options selected)
  1570. "Non-nil when element or object BLOB should be skipped during export.
  1571. OPTIONS is the plist holding export options. SELECTED, when
  1572. non-nil, is a list of headlines or inlinetasks belonging to
  1573. a tree with a select tag."
  1574. (cl-case (org-element-type blob)
  1575. (clock (not (plist-get options :with-clocks)))
  1576. (drawer
  1577. (let ((with-drawers-p (plist-get options :with-drawers)))
  1578. (or (not with-drawers-p)
  1579. (and (consp with-drawers-p)
  1580. ;; If `:with-drawers' value starts with `not', ignore
  1581. ;; every drawer whose name belong to that list.
  1582. ;; Otherwise, ignore drawers whose name isn't in that
  1583. ;; list.
  1584. (let ((name (org-element-property :drawer-name blob)))
  1585. (if (eq (car with-drawers-p) 'not)
  1586. (member-ignore-case name (cdr with-drawers-p))
  1587. (not (member-ignore-case name with-drawers-p))))))))
  1588. (fixed-width (not (plist-get options :with-fixed-width)))
  1589. ((footnote-definition footnote-reference)
  1590. (not (plist-get options :with-footnotes)))
  1591. ((headline inlinetask)
  1592. (let ((with-tasks (plist-get options :with-tasks))
  1593. (todo (org-element-property :todo-keyword blob))
  1594. (todo-type (org-element-property :todo-type blob))
  1595. (archived (plist-get options :with-archived-trees))
  1596. (tags (org-export-get-tags blob options nil t)))
  1597. (or
  1598. (and (eq (org-element-type blob) 'inlinetask)
  1599. (not (plist-get options :with-inlinetasks)))
  1600. ;; Ignore subtrees with an exclude tag.
  1601. (cl-loop for k in (plist-get options :exclude-tags)
  1602. thereis (member k tags))
  1603. ;; When a select tag is present in the buffer, ignore any tree
  1604. ;; without it.
  1605. (and selected (not (memq blob selected)))
  1606. ;; Ignore commented sub-trees.
  1607. (org-element-property :commentedp blob)
  1608. ;; Ignore archived subtrees if `:with-archived-trees' is nil.
  1609. (and (not archived) (org-element-property :archivedp blob))
  1610. ;; Ignore tasks, if specified by `:with-tasks' property.
  1611. (and todo
  1612. (or (not with-tasks)
  1613. (and (memq with-tasks '(todo done))
  1614. (not (eq todo-type with-tasks)))
  1615. (and (consp with-tasks) (not (member todo with-tasks))))))))
  1616. ((latex-environment latex-fragment) (not (plist-get options :with-latex)))
  1617. (node-property
  1618. (let ((properties-set (plist-get options :with-properties)))
  1619. (cond ((null properties-set) t)
  1620. ((consp properties-set)
  1621. (not (member-ignore-case (org-element-property :key blob)
  1622. properties-set))))))
  1623. (planning (not (plist-get options :with-planning)))
  1624. (property-drawer (not (plist-get options :with-properties)))
  1625. (statistics-cookie (not (plist-get options :with-statistics-cookies)))
  1626. (table (not (plist-get options :with-tables)))
  1627. (table-cell
  1628. (and (org-export-table-has-special-column-p
  1629. (org-export-get-parent-table blob))
  1630. (org-export-first-sibling-p blob options)))
  1631. (table-row (org-export-table-row-is-special-p blob options))
  1632. (timestamp
  1633. ;; `:with-timestamps' only applies to isolated timestamps
  1634. ;; objects, i.e. timestamp objects in a paragraph containing only
  1635. ;; timestamps and whitespaces.
  1636. (when (let ((parent (org-export-get-parent-element blob)))
  1637. (and (memq (org-element-type parent) '(paragraph verse-block))
  1638. (not (org-element-map parent
  1639. (cons 'plain-text
  1640. (remq 'timestamp org-element-all-objects))
  1641. (lambda (obj)
  1642. (or (not (stringp obj)) (org-string-nw-p obj)))
  1643. options t))))
  1644. (cl-case (plist-get options :with-timestamps)
  1645. ((nil) t)
  1646. (active
  1647. (not (memq (org-element-property :type blob) '(active active-range))))
  1648. (inactive
  1649. (not (memq (org-element-property :type blob)
  1650. '(inactive inactive-range)))))))))
  1651. ;;; The Transcoder
  1652. ;;
  1653. ;; `org-export-data' reads a parse tree (obtained with, i.e.
  1654. ;; `org-element-parse-buffer') and transcodes it into a specified
  1655. ;; back-end output. It takes care of filtering out elements or
  1656. ;; objects according to export options and organizing the output blank
  1657. ;; lines and white space are preserved. The function memoizes its
  1658. ;; results, so it is cheap to call it within transcoders.
  1659. ;;
  1660. ;; It is possible to modify locally the back-end used by
  1661. ;; `org-export-data' or even use a temporary back-end by using
  1662. ;; `org-export-data-with-backend'.
  1663. ;;
  1664. ;; `org-export-transcoder' is an accessor returning appropriate
  1665. ;; translator function for a given element or object.
  1666. (defun org-export-transcoder (blob info)
  1667. "Return appropriate transcoder for BLOB.
  1668. INFO is a plist containing export directives."
  1669. (let ((type (org-element-type blob)))
  1670. ;; Return contents only for complete parse trees.
  1671. (if (eq type 'org-data) (lambda (_datum contents _info) contents)
  1672. (let ((transcoder (cdr (assq type (plist-get info :translate-alist)))))
  1673. (and (functionp transcoder) transcoder)))))
  1674. (defun org-export-data (data info)
  1675. "Convert DATA into current back-end format.
  1676. DATA is a parse tree, an element or an object or a secondary
  1677. string. INFO is a plist holding export options.
  1678. Return a string."
  1679. (or (gethash data (plist-get info :exported-data))
  1680. ;; Handle broken links according to
  1681. ;; `org-export-with-broken-links'.
  1682. (cl-macrolet
  1683. ((broken-link-handler
  1684. (&rest body)
  1685. `(condition-case err
  1686. (progn ,@body)
  1687. (org-link-broken
  1688. (pcase (plist-get info :with-broken-links)
  1689. (`nil (user-error "Unable to resolve link: %S" (nth 1 err)))
  1690. (`mark (org-export-data
  1691. (format "[BROKEN LINK: %s]" (nth 1 err)) info))
  1692. (_ nil))))))
  1693. (let* ((type (org-element-type data))
  1694. (results
  1695. (cond
  1696. ;; Ignored element/object.
  1697. ((memq data (plist-get info :ignore-list)) nil)
  1698. ;; Plain text.
  1699. ((eq type 'plain-text)
  1700. (org-export-filter-apply-functions
  1701. (plist-get info :filter-plain-text)
  1702. (let ((transcoder (org-export-transcoder data info)))
  1703. (if transcoder (funcall transcoder data info) data))
  1704. info))
  1705. ;; Secondary string.
  1706. ((not type)
  1707. (mapconcat (lambda (obj) (org-export-data obj info)) data ""))
  1708. ;; Element/Object without contents or, as a special
  1709. ;; case, headline with archive tag and archived trees
  1710. ;; restricted to title only.
  1711. ((or (not (org-element-contents data))
  1712. (and (eq type 'headline)
  1713. (eq (plist-get info :with-archived-trees) 'headline)
  1714. (org-element-property :archivedp data)))
  1715. (let ((transcoder (org-export-transcoder data info)))
  1716. (or (and (functionp transcoder)
  1717. (broken-link-handler
  1718. (funcall transcoder data nil info)))
  1719. ;; Export snippets never return a nil value so
  1720. ;; that white spaces following them are never
  1721. ;; ignored.
  1722. (and (eq type 'export-snippet) ""))))
  1723. ;; Element/Object with contents.
  1724. (t
  1725. (let ((transcoder (org-export-transcoder data info)))
  1726. (when transcoder
  1727. (let* ((greaterp (memq type org-element-greater-elements))
  1728. (objectp
  1729. (and (not greaterp)
  1730. (memq type org-element-recursive-objects)))
  1731. (contents
  1732. (mapconcat
  1733. (lambda (element) (org-export-data element info))
  1734. (org-element-contents
  1735. (if (or greaterp objectp) data
  1736. ;; Elements directly containing
  1737. ;; objects must have their indentation
  1738. ;; normalized first.
  1739. (org-element-normalize-contents
  1740. data
  1741. ;; When normalizing contents of the
  1742. ;; first paragraph in an item or
  1743. ;; a footnote definition, ignore
  1744. ;; first line's indentation: there is
  1745. ;; none and it might be misleading.
  1746. (when (eq type 'paragraph)
  1747. (let ((parent (org-export-get-parent data)))
  1748. (and
  1749. (eq (car (org-element-contents parent))
  1750. data)
  1751. (memq (org-element-type parent)
  1752. '(footnote-definition item))))))))
  1753. "")))
  1754. (broken-link-handler
  1755. (funcall transcoder data
  1756. (if (not greaterp) contents
  1757. (org-element-normalize-string contents))
  1758. info)))))))))
  1759. ;; Final result will be memoized before being returned.
  1760. (puthash
  1761. data
  1762. (cond
  1763. ((not results) "")
  1764. ((memq type '(org-data plain-text nil)) results)
  1765. ;; Append the same white space between elements or objects
  1766. ;; as in the original buffer, and call appropriate filters.
  1767. (t
  1768. (let ((results
  1769. (org-export-filter-apply-functions
  1770. (plist-get info (intern (format ":filter-%s" type)))
  1771. (let ((post-blank (or (org-element-property :post-blank data)
  1772. 0)))
  1773. (if (memq type org-element-all-elements)
  1774. (concat (org-element-normalize-string results)
  1775. (make-string post-blank ?\n))
  1776. (concat results (make-string post-blank ?\s))))
  1777. info)))
  1778. results)))
  1779. (plist-get info :exported-data))))))
  1780. (defun org-export-data-with-backend (data backend info)
  1781. "Convert DATA into BACKEND format.
  1782. DATA is an element, an object, a secondary string or a string.
  1783. BACKEND is a symbol. INFO is a plist used as a communication
  1784. channel.
  1785. Unlike to `org-export-with-backend', this function will
  1786. recursively convert DATA using BACKEND translation table."
  1787. (when (symbolp backend) (setq backend (org-export-get-backend backend)))
  1788. (org-export-data
  1789. data
  1790. ;; Set-up a new communication channel with translations defined in
  1791. ;; BACKEND as the translate table and a new hash table for
  1792. ;; memoization.
  1793. (org-combine-plists
  1794. info
  1795. (list :back-end backend
  1796. :translate-alist (org-export-get-all-transcoders backend)
  1797. ;; Size of the hash table is reduced since this function
  1798. ;; will probably be used on small trees.
  1799. :exported-data (make-hash-table :test 'eq :size 401)))))
  1800. (defun org-export-expand (blob contents &optional with-affiliated)
  1801. "Expand a parsed element or object to its original state.
  1802. BLOB is either an element or an object. CONTENTS is its
  1803. contents, as a string or nil.
  1804. When optional argument WITH-AFFILIATED is non-nil, add affiliated
  1805. keywords before output."
  1806. (let ((type (org-element-type blob)))
  1807. (concat (and with-affiliated (memq type org-element-all-elements)
  1808. (org-element--interpret-affiliated-keywords blob))
  1809. (funcall (intern (format "org-element-%s-interpreter" type))
  1810. blob contents))))
  1811. ;;; The Filter System
  1812. ;;
  1813. ;; Filters allow end-users to tweak easily the transcoded output.
  1814. ;; They are the functional counterpart of hooks, as every filter in
  1815. ;; a set is applied to the return value of the previous one.
  1816. ;;
  1817. ;; Every set is back-end agnostic. Although, a filter is always
  1818. ;; called, in addition to the string it applies to, with the back-end
  1819. ;; used as argument, so it's easy for the end-user to add back-end
  1820. ;; specific filters in the set. The communication channel, as
  1821. ;; a plist, is required as the third argument.
  1822. ;;
  1823. ;; From the developer side, filters sets can be installed in the
  1824. ;; process with the help of `org-export-define-backend', which
  1825. ;; internally stores filters as an alist. Each association has a key
  1826. ;; among the following symbols and a function or a list of functions
  1827. ;; as value.
  1828. ;;
  1829. ;; - `:filter-options' applies to the property list containing export
  1830. ;; options. Unlike to other filters, functions in this list accept
  1831. ;; two arguments instead of three: the property list containing
  1832. ;; export options and the back-end. Users can set its value through
  1833. ;; `org-export-filter-options-functions' variable.
  1834. ;;
  1835. ;; - `:filter-parse-tree' applies directly to the complete parsed
  1836. ;; tree. Users can set it through
  1837. ;; `org-export-filter-parse-tree-functions' variable.
  1838. ;;
  1839. ;; - `:filter-body' applies to the body of the output, before template
  1840. ;; translator chimes in. Users can set it through
  1841. ;; `org-export-filter-body-functions' variable.
  1842. ;;
  1843. ;; - `:filter-final-output' applies to the final transcoded string.
  1844. ;; Users can set it with `org-export-filter-final-output-functions'
  1845. ;; variable.
  1846. ;;
  1847. ;; - `:filter-plain-text' applies to any string not recognized as Org
  1848. ;; syntax. `org-export-filter-plain-text-functions' allows users to
  1849. ;; configure it.
  1850. ;;
  1851. ;; - `:filter-TYPE' applies on the string returned after an element or
  1852. ;; object of type TYPE has been transcoded. A user can modify
  1853. ;; `org-export-filter-TYPE-functions' to install these filters.
  1854. ;;
  1855. ;; All filters sets are applied with
  1856. ;; `org-export-filter-apply-functions' function. Filters in a set are
  1857. ;; applied in a LIFO fashion. It allows developers to be sure that
  1858. ;; their filters will be applied first.
  1859. ;;
  1860. ;; Filters properties are installed in communication channel with
  1861. ;; `org-export-install-filters' function.
  1862. ;;
  1863. ;; Eventually, two hooks (`org-export-before-processing-hook' and
  1864. ;; `org-export-before-parsing-hook') are run at the beginning of the
  1865. ;; export process and just before parsing to allow for heavy structure
  1866. ;; modifications.
  1867. ;;;; Hooks
  1868. (defvar org-export-before-processing-hook nil
  1869. "Hook run at the beginning of the export process.
  1870. This is run before include keywords and macros are expanded and
  1871. Babel code blocks executed, on a copy of the original buffer
  1872. being exported. Visibility and narrowing are preserved. Point
  1873. is at the beginning of the buffer.
  1874. Every function in this hook will be called with one argument: the
  1875. back-end currently used, as a symbol.")
  1876. (defvar org-export-before-parsing-hook nil
  1877. "Hook run before parsing an export buffer.
  1878. This is run after include keywords and macros have been expanded
  1879. and Babel code blocks executed, on a copy of the original buffer
  1880. being exported. Visibility and narrowing are preserved. Point
  1881. is at the beginning of the buffer.
  1882. Every function in this hook will be called with one argument: the
  1883. back-end currently used, as a symbol.")
  1884. ;;;; Special Filters
  1885. (defvar org-export-filter-options-functions nil
  1886. "List of functions applied to the export options.
  1887. Each filter is called with two arguments: the export options, as
  1888. a plist, and the back-end, as a symbol. It must return
  1889. a property list containing export options.")
  1890. (defvar org-export-filter-parse-tree-functions nil
  1891. "List of functions applied to the parsed tree.
  1892. Each filter is called with three arguments: the parse tree, as
  1893. returned by `org-element-parse-buffer', the back-end, as
  1894. a symbol, and the communication channel, as a plist. It must
  1895. return the modified parse tree to transcode.")
  1896. (defvar org-export-filter-plain-text-functions nil
  1897. "List of functions applied to plain text.
  1898. Each filter is called with three arguments: a string which
  1899. contains no Org syntax, the back-end, as a symbol, and the
  1900. communication channel, as a plist. It must return a string or
  1901. nil.")
  1902. (defvar org-export-filter-body-functions nil
  1903. "List of functions applied to transcoded body.
  1904. Each filter is called with three arguments: a string which
  1905. contains no Org syntax, the back-end, as a symbol, and the
  1906. communication channel, as a plist. It must return a string or
  1907. nil.")
  1908. (defvar org-export-filter-final-output-functions nil
  1909. "List of functions applied to the transcoded string.
  1910. Each filter is called with three arguments: the full transcoded
  1911. string, the back-end, as a symbol, and the communication channel,
  1912. as a plist. It must return a string that will be used as the
  1913. final export output.")
  1914. ;;;; Elements Filters
  1915. (defvar org-export-filter-babel-call-functions nil
  1916. "List of functions applied to a transcoded babel-call.
  1917. Each filter is called with three arguments: the transcoded data,
  1918. as a string, the back-end, as a symbol, and the communication
  1919. channel, as a plist. It must return a string or nil.")
  1920. (defvar org-export-filter-center-block-functions nil
  1921. "List of functions applied to a transcoded center block.
  1922. Each filter is called with three arguments: the transcoded data,
  1923. as a string, the back-end, as a symbol, and the communication
  1924. channel, as a plist. It must return a string or nil.")
  1925. (defvar org-export-filter-clock-functions nil
  1926. "List of functions applied to a transcoded clock.
  1927. Each filter is called with three arguments: the transcoded data,
  1928. as a string, the back-end, as a symbol, and the communication
  1929. channel, as a plist. It must return a string or nil.")
  1930. (defvar org-export-filter-diary-sexp-functions nil
  1931. "List of functions applied to a transcoded diary-sexp.
  1932. Each filter is called with three arguments: the transcoded data,
  1933. as a string, the back-end, as a symbol, and the communication
  1934. channel, as a plist. It must return a string or nil.")
  1935. (defvar org-export-filter-drawer-functions nil
  1936. "List of functions applied to a transcoded drawer.
  1937. Each filter is called with three arguments: the transcoded data,
  1938. as a string, the back-end, as a symbol, and the communication
  1939. channel, as a plist. It must return a string or nil.")
  1940. (defvar org-export-filter-dynamic-block-functions nil
  1941. "List of functions applied to a transcoded dynamic-block.
  1942. Each filter is called with three arguments: the transcoded data,
  1943. as a string, the back-end, as a symbol, and the communication
  1944. channel, as a plist. It must return a string or nil.")
  1945. (defvar org-export-filter-example-block-functions nil
  1946. "List of functions applied to a transcoded example-block.
  1947. Each filter is called with three arguments: the transcoded data,
  1948. as a string, the back-end, as a symbol, and the communication
  1949. channel, as a plist. It must return a string or nil.")
  1950. (defvar org-export-filter-export-block-functions nil
  1951. "List of functions applied to a transcoded export-block.
  1952. Each filter is called with three arguments: the transcoded data,
  1953. as a string, the back-end, as a symbol, and the communication
  1954. channel, as a plist. It must return a string or nil.")
  1955. (defvar org-export-filter-fixed-width-functions nil
  1956. "List of functions applied to a transcoded fixed-width.
  1957. Each filter is called with three arguments: the transcoded data,
  1958. as a string, the back-end, as a symbol, and the communication
  1959. channel, as a plist. It must return a string or nil.")
  1960. (defvar org-export-filter-footnote-definition-functions nil
  1961. "List of functions applied to a transcoded footnote-definition.
  1962. Each filter is called with three arguments: the transcoded data,
  1963. as a string, the back-end, as a symbol, and the communication
  1964. channel, as a plist. It must return a string or nil.")
  1965. (defvar org-export-filter-headline-functions nil
  1966. "List of functions applied to a transcoded headline.
  1967. Each filter is called with three arguments: the transcoded data,
  1968. as a string, the back-end, as a symbol, and the communication
  1969. channel, as a plist. It must return a string or nil.")
  1970. (defvar org-export-filter-horizontal-rule-functions nil
  1971. "List of functions applied to a transcoded horizontal-rule.
  1972. Each filter is called with three arguments: the transcoded data,
  1973. as a string, the back-end, as a symbol, and the communication
  1974. channel, as a plist. It must return a string or nil.")
  1975. (defvar org-export-filter-inlinetask-functions nil
  1976. "List of functions applied to a transcoded inlinetask.
  1977. Each filter is called with three arguments: the transcoded data,
  1978. as a string, the back-end, as a symbol, and the communication
  1979. channel, as a plist. It must return a string or nil.")
  1980. (defvar org-export-filter-item-functions nil
  1981. "List of functions applied to a transcoded item.
  1982. Each filter is called with three arguments: the transcoded data,
  1983. as a string, the back-end, as a symbol, and the communication
  1984. channel, as a plist. It must return a string or nil.")
  1985. (defvar org-export-filter-keyword-functions nil
  1986. "List of functions applied to a transcoded keyword.
  1987. Each filter is called with three arguments: the transcoded data,
  1988. as a string, the back-end, as a symbol, and the communication
  1989. channel, as a plist. It must return a string or nil.")
  1990. (defvar org-export-filter-latex-environment-functions nil
  1991. "List of functions applied to a transcoded latex-environment.
  1992. Each filter is called with three arguments: the transcoded data,
  1993. as a string, the back-end, as a symbol, and the communication
  1994. channel, as a plist. It must return a string or nil.")
  1995. (defvar org-export-filter-node-property-functions nil
  1996. "List of functions applied to a transcoded node-property.
  1997. Each filter is called with three arguments: the transcoded data,
  1998. as a string, the back-end, as a symbol, and the communication
  1999. channel, as a plist. It must return a string or nil.")
  2000. (defvar org-export-filter-paragraph-functions nil
  2001. "List of functions applied to a transcoded paragraph.
  2002. Each filter is called with three arguments: the transcoded data,
  2003. as a string, the back-end, as a symbol, and the communication
  2004. channel, as a plist. It must return a string or nil.")
  2005. (defvar org-export-filter-plain-list-functions nil
  2006. "List of functions applied to a transcoded plain-list.
  2007. Each filter is called with three arguments: the transcoded data,
  2008. as a string, the back-end, as a symbol, and the communication
  2009. channel, as a plist. It must return a string or nil.")
  2010. (defvar org-export-filter-planning-functions nil
  2011. "List of functions applied to a transcoded planning.
  2012. Each filter is called with three arguments: the transcoded data,
  2013. as a string, the back-end, as a symbol, and the communication
  2014. channel, as a plist. It must return a string or nil.")
  2015. (defvar org-export-filter-property-drawer-functions nil
  2016. "List of functions applied to a transcoded property-drawer.
  2017. Each filter is called with three arguments: the transcoded data,
  2018. as a string, the back-end, as a symbol, and the communication
  2019. channel, as a plist. It must return a string or nil.")
  2020. (defvar org-export-filter-quote-block-functions nil
  2021. "List of functions applied to a transcoded quote block.
  2022. Each filter is called with three arguments: the transcoded quote
  2023. data, as a string, the back-end, as a symbol, and the
  2024. communication channel, as a plist. It must return a string or
  2025. nil.")
  2026. (defvar org-export-filter-section-functions nil
  2027. "List of functions applied to a transcoded section.
  2028. Each filter is called with three arguments: the transcoded data,
  2029. as a string, the back-end, as a symbol, and the communication
  2030. channel, as a plist. It must return a string or nil.")
  2031. (defvar org-export-filter-special-block-functions nil
  2032. "List of functions applied to a transcoded special block.
  2033. Each filter is called with three arguments: the transcoded data,
  2034. as a string, the back-end, as a symbol, and the communication
  2035. channel, as a plist. It must return a string or nil.")
  2036. (defvar org-export-filter-src-block-functions nil
  2037. "List of functions applied to a transcoded src-block.
  2038. Each filter is called with three arguments: the transcoded data,
  2039. as a string, the back-end, as a symbol, and the communication
  2040. channel, as a plist. It must return a string or nil.")
  2041. (defvar org-export-filter-table-functions nil
  2042. "List of functions applied to a transcoded table.
  2043. Each filter is called with three arguments: the transcoded data,
  2044. as a string, the back-end, as a symbol, and the communication
  2045. channel, as a plist. It must return a string or nil.")
  2046. (defvar org-export-filter-table-cell-functions nil
  2047. "List of functions applied to a transcoded table-cell.
  2048. Each filter is called with three arguments: the transcoded data,
  2049. as a string, the back-end, as a symbol, and the communication
  2050. channel, as a plist. It must return a string or nil.")
  2051. (defvar org-export-filter-table-row-functions nil
  2052. "List of functions applied to a transcoded table-row.
  2053. Each filter is called with three arguments: the transcoded data,
  2054. as a string, the back-end, as a symbol, and the communication
  2055. channel, as a plist. It must return a string or nil.")
  2056. (defvar org-export-filter-verse-block-functions nil
  2057. "List of functions applied to a transcoded verse block.
  2058. Each filter is called with three arguments: the transcoded data,
  2059. as a string, the back-end, as a symbol, and the communication
  2060. channel, as a plist. It must return a string or nil.")
  2061. ;;;; Objects Filters
  2062. (defvar org-export-filter-bold-functions nil
  2063. "List of functions applied to transcoded bold text.
  2064. Each filter is called with three arguments: the transcoded data,
  2065. as a string, the back-end, as a symbol, and the communication
  2066. channel, as a plist. It must return a string or nil.")
  2067. (defvar org-export-filter-code-functions nil
  2068. "List of functions applied to transcoded code text.
  2069. Each filter is called with three arguments: the transcoded data,
  2070. as a string, the back-end, as a symbol, and the communication
  2071. channel, as a plist. It must return a string or nil.")
  2072. (defvar org-export-filter-entity-functions nil
  2073. "List of functions applied to a transcoded entity.
  2074. Each filter is called with three arguments: the transcoded data,
  2075. as a string, the back-end, as a symbol, and the communication
  2076. channel, as a plist. It must return a string or nil.")
  2077. (defvar org-export-filter-export-snippet-functions nil
  2078. "List of functions applied to a transcoded export-snippet.
  2079. Each filter is called with three arguments: the transcoded data,
  2080. as a string, the back-end, as a symbol, and the communication
  2081. channel, as a plist. It must return a string or nil.")
  2082. (defvar org-export-filter-footnote-reference-functions nil
  2083. "List of functions applied to a transcoded footnote-reference.
  2084. Each filter is called with three arguments: the transcoded data,
  2085. as a string, the back-end, as a symbol, and the communication
  2086. channel, as a plist. It must return a string or nil.")
  2087. (defvar org-export-filter-inline-babel-call-functions nil
  2088. "List of functions applied to a transcoded inline-babel-call.
  2089. Each filter is called with three arguments: the transcoded data,
  2090. as a string, the back-end, as a symbol, and the communication
  2091. channel, as a plist. It must return a string or nil.")
  2092. (defvar org-export-filter-inline-src-block-functions nil
  2093. "List of functions applied to a transcoded inline-src-block.
  2094. Each filter is called with three arguments: the transcoded data,
  2095. as a string, the back-end, as a symbol, and the communication
  2096. channel, as a plist. It must return a string or nil.")
  2097. (defvar org-export-filter-italic-functions nil
  2098. "List of functions applied to transcoded italic text.
  2099. Each filter is called with three arguments: the transcoded data,
  2100. as a string, the back-end, as a symbol, and the communication
  2101. channel, as a plist. It must return a string or nil.")
  2102. (defvar org-export-filter-latex-fragment-functions nil
  2103. "List of functions applied to a transcoded latex-fragment.
  2104. Each filter is called with three arguments: the transcoded data,
  2105. as a string, the back-end, as a symbol, and the communication
  2106. channel, as a plist. It must return a string or nil.")
  2107. (defvar org-export-filter-line-break-functions nil
  2108. "List of functions applied to a transcoded line-break.
  2109. Each filter is called with three arguments: the transcoded data,
  2110. as a string, the back-end, as a symbol, and the communication
  2111. channel, as a plist. It must return a string or nil.")
  2112. (defvar org-export-filter-link-functions nil
  2113. "List of functions applied to a transcoded link.
  2114. Each filter is called with three arguments: the transcoded data,
  2115. as a string, the back-end, as a symbol, and the communication
  2116. channel, as a plist. It must return a string or nil.")
  2117. (defvar org-export-filter-radio-target-functions nil
  2118. "List of functions applied to a transcoded radio-target.
  2119. Each filter is called with three arguments: the transcoded data,
  2120. as a string, the back-end, as a symbol, and the communication
  2121. channel, as a plist. It must return a string or nil.")
  2122. (defvar org-export-filter-statistics-cookie-functions nil
  2123. "List of functions applied to a transcoded statistics-cookie.
  2124. Each filter is called with three arguments: the transcoded data,
  2125. as a string, the back-end, as a symbol, and the communication
  2126. channel, as a plist. It must return a string or nil.")
  2127. (defvar org-export-filter-strike-through-functions nil
  2128. "List of functions applied to transcoded strike-through text.
  2129. Each filter is called with three arguments: the transcoded data,
  2130. as a string, the back-end, as a symbol, and the communication
  2131. channel, as a plist. It must return a string or nil.")
  2132. (defvar org-export-filter-subscript-functions nil
  2133. "List of functions applied to a transcoded subscript.
  2134. Each filter is called with three arguments: the transcoded data,
  2135. as a string, the back-end, as a symbol, and the communication
  2136. channel, as a plist. It must return a string or nil.")
  2137. (defvar org-export-filter-superscript-functions nil
  2138. "List of functions applied to a transcoded superscript.
  2139. Each filter is called with three arguments: the transcoded data,
  2140. as a string, the back-end, as a symbol, and the communication
  2141. channel, as a plist. It must return a string or nil.")
  2142. (defvar org-export-filter-target-functions nil
  2143. "List of functions applied to a transcoded target.
  2144. Each filter is called with three arguments: the transcoded data,
  2145. as a string, the back-end, as a symbol, and the communication
  2146. channel, as a plist. It must return a string or nil.")
  2147. (defvar org-export-filter-timestamp-functions nil
  2148. "List of functions applied to a transcoded timestamp.
  2149. Each filter is called with three arguments: the transcoded data,
  2150. as a string, the back-end, as a symbol, and the communication
  2151. channel, as a plist. It must return a string or nil.")
  2152. (defvar org-export-filter-underline-functions nil
  2153. "List of functions applied to transcoded underline text.
  2154. Each filter is called with three arguments: the transcoded data,
  2155. as a string, the back-end, as a symbol, and the communication
  2156. channel, as a plist. It must return a string or nil.")
  2157. (defvar org-export-filter-verbatim-functions nil
  2158. "List of functions applied to transcoded verbatim text.
  2159. Each filter is called with three arguments: the transcoded data,
  2160. as a string, the back-end, as a symbol, and the communication
  2161. channel, as a plist. It must return a string or nil.")
  2162. ;;;; Filters Tools
  2163. ;;
  2164. ;; Internal function `org-export-install-filters' installs filters
  2165. ;; hard-coded in back-ends (developer filters) and filters from global
  2166. ;; variables (user filters) in the communication channel.
  2167. ;;
  2168. ;; Internal function `org-export-filter-apply-functions' takes care
  2169. ;; about applying each filter in order to a given data. It ignores
  2170. ;; filters returning a nil value but stops whenever a filter returns
  2171. ;; an empty string.
  2172. (defun org-export-filter-apply-functions (filters value info)
  2173. "Call every function in FILTERS.
  2174. Functions are called with arguments VALUE, current export
  2175. back-end's name and INFO. A function returning a nil value will
  2176. be skipped. If it returns the empty string, the process ends and
  2177. VALUE is ignored.
  2178. Call is done in a LIFO fashion, to be sure that developer
  2179. specified filters, if any, are called first."
  2180. (catch 'exit
  2181. (let* ((backend (plist-get info :back-end))
  2182. (backend-name (and backend (org-export-backend-name backend))))
  2183. (dolist (filter filters value)
  2184. (let ((result (funcall filter value backend-name info)))
  2185. (cond ((not result) value)
  2186. ((equal value "") (throw 'exit nil))
  2187. (t (setq value result))))))))
  2188. (defun org-export-install-filters (info)
  2189. "Install filters properties in communication channel.
  2190. INFO is a plist containing the current communication channel.
  2191. Return the updated communication channel."
  2192. (let (plist)
  2193. ;; Install user-defined filters with `org-export-filters-alist'
  2194. ;; and filters already in INFO (through ext-plist mechanism).
  2195. (dolist (p org-export-filters-alist)
  2196. (let* ((prop (car p))
  2197. (info-value (plist-get info prop))
  2198. (default-value (symbol-value (cdr p))))
  2199. (setq plist
  2200. (plist-put plist prop
  2201. ;; Filters in INFO will be called
  2202. ;; before those user provided.
  2203. (append (if (listp info-value) info-value
  2204. (list info-value))
  2205. default-value)))))
  2206. ;; Prepend back-end specific filters to that list.
  2207. (dolist (p (org-export-get-all-filters (plist-get info :back-end)))
  2208. ;; Single values get consed, lists are appended.
  2209. (let ((key (car p)) (value (cdr p)))
  2210. (when value
  2211. (setq plist
  2212. (plist-put
  2213. plist key
  2214. (if (atom value) (cons value (plist-get plist key))
  2215. (append value (plist-get plist key))))))))
  2216. ;; Return new communication channel.
  2217. (org-combine-plists info plist)))
  2218. ;;; Core functions
  2219. ;;
  2220. ;; This is the room for the main function, `org-export-as', along with
  2221. ;; its derivative, `org-export-string-as'.
  2222. ;; `org-export--copy-to-kill-ring-p' determines if output of these
  2223. ;; function should be added to kill ring.
  2224. ;;
  2225. ;; Note that `org-export-as' doesn't really parse the current buffer,
  2226. ;; but a copy of it (with the same buffer-local variables and
  2227. ;; visibility), where macros and include keywords are expanded and
  2228. ;; Babel blocks are executed, if appropriate.
  2229. ;; `org-export-with-buffer-copy' macro prepares that copy.
  2230. ;;
  2231. ;; File inclusion is taken care of by
  2232. ;; `org-export-expand-include-keyword' and
  2233. ;; `org-export--prepare-file-contents'. Structure wise, including
  2234. ;; a whole Org file in a buffer often makes little sense. For
  2235. ;; example, if the file contains a headline and the include keyword
  2236. ;; was within an item, the item should contain the headline. That's
  2237. ;; why file inclusion should be done before any structure can be
  2238. ;; associated to the file, that is before parsing.
  2239. ;;
  2240. ;; `org-export-insert-default-template' is a command to insert
  2241. ;; a default template (or a back-end specific template) at point or in
  2242. ;; current subtree.
  2243. (defun org-export-copy-buffer ()
  2244. "Return a copy of the current buffer.
  2245. The copy preserves Org buffer-local variables, visibility and
  2246. narrowing."
  2247. (let ((copy-buffer-fun (org-export--generate-copy-script (current-buffer)))
  2248. (new-buf (generate-new-buffer (buffer-name))))
  2249. (with-current-buffer new-buf
  2250. (funcall copy-buffer-fun)
  2251. (set-buffer-modified-p nil))
  2252. new-buf))
  2253. (defmacro org-export-with-buffer-copy (&rest body)
  2254. "Apply BODY in a copy of the current buffer.
  2255. The copy preserves local variables, visibility and contents of
  2256. the original buffer. Point is at the beginning of the buffer
  2257. when BODY is applied."
  2258. (declare (debug t))
  2259. (org-with-gensyms (buf-copy)
  2260. `(let ((,buf-copy (org-export-copy-buffer)))
  2261. (unwind-protect
  2262. (with-current-buffer ,buf-copy
  2263. (goto-char (point-min))
  2264. (progn ,@body))
  2265. (and (buffer-live-p ,buf-copy)
  2266. ;; Kill copy without confirmation.
  2267. (progn (with-current-buffer ,buf-copy
  2268. (restore-buffer-modified-p nil))
  2269. (kill-buffer ,buf-copy)))))))
  2270. (defun org-export--generate-copy-script (buffer)
  2271. "Generate a function duplicating BUFFER.
  2272. The copy will preserve local variables, visibility, contents and
  2273. narrowing of the original buffer. If a region was active in
  2274. BUFFER, contents will be narrowed to that region instead.
  2275. The resulting function can be evaluated at a later time, from
  2276. another buffer, effectively cloning the original buffer there.
  2277. The function assumes BUFFER's major mode is `org-mode'."
  2278. (with-current-buffer buffer
  2279. `(lambda ()
  2280. (let ((inhibit-modification-hooks t))
  2281. ;; Set major mode. Ignore `org-mode-hook' as it has been run
  2282. ;; already in BUFFER.
  2283. (let ((org-mode-hook nil) (org-inhibit-startup t)) (org-mode))
  2284. ;; Copy specific buffer local variables and variables set
  2285. ;; through BIND keywords.
  2286. ,@(let ((bound-variables (org-export--list-bound-variables))
  2287. vars)
  2288. (dolist (entry (buffer-local-variables (buffer-base-buffer)) vars)
  2289. (when (consp entry)
  2290. (let ((var (car entry))
  2291. (val (cdr entry)))
  2292. (and (not (memq var org-export-ignored-local-variables))
  2293. (or (memq var
  2294. '(default-directory
  2295. buffer-file-name
  2296. buffer-file-coding-system))
  2297. (assq var bound-variables)
  2298. (string-match "^\\(org-\\|orgtbl-\\)"
  2299. (symbol-name var)))
  2300. ;; Skip unreadable values, as they cannot be
  2301. ;; sent to external process.
  2302. (or (not val) (ignore-errors (read (format "%S" val))))
  2303. (push `(set (make-local-variable (quote ,var))
  2304. (quote ,val))
  2305. vars))))))
  2306. ;; Whole buffer contents.
  2307. (insert
  2308. ,(org-with-wide-buffer
  2309. (buffer-substring-no-properties
  2310. (point-min) (point-max))))
  2311. ;; Narrowing.
  2312. ,(if (org-region-active-p)
  2313. `(narrow-to-region ,(region-beginning) ,(region-end))
  2314. `(narrow-to-region ,(point-min) ,(point-max)))
  2315. ;; Current position of point.
  2316. (goto-char ,(point))
  2317. ;; Overlays with invisible property.
  2318. ,@(let (ov-set)
  2319. (dolist (ov (overlays-in (point-min) (point-max)) ov-set)
  2320. (let ((invis-prop (overlay-get ov 'invisible)))
  2321. (when invis-prop
  2322. (push `(overlay-put
  2323. (make-overlay ,(overlay-start ov)
  2324. ,(overlay-end ov))
  2325. 'invisible (quote ,invis-prop))
  2326. ov-set)))))))))
  2327. (defun org-export--delete-comments ()
  2328. "Delete commented areas in the buffer.
  2329. Commented areas are comments, comment blocks, commented trees and
  2330. inlinetasks. Trailing blank lines after a comment or a comment
  2331. block are removed, as long as it doesn't alter the structure of
  2332. the document. Narrowing, if any, is ignored."
  2333. (org-with-wide-buffer
  2334. (goto-char (point-min))
  2335. (let* ((case-fold-search t)
  2336. (comment-re "^[ \t]*#\\(?: \\|$\\|\\+end_comment\\)")
  2337. (regexp (concat org-outline-regexp-bol ".*" org-comment-string "\\|"
  2338. comment-re)))
  2339. (while (re-search-forward regexp nil t)
  2340. (let ((element (org-element-at-point)))
  2341. (pcase (org-element-type element)
  2342. ((or `headline `inlinetask)
  2343. (when (org-element-property :commentedp element)
  2344. (delete-region (org-element-property :begin element)
  2345. (org-element-property :end element))))
  2346. ((or `comment `comment-block)
  2347. (let* ((parent (org-element-property :parent element))
  2348. (start (org-element-property :begin element))
  2349. (end (org-element-property :end element))
  2350. ;; We remove trailing blank lines. Doing so could
  2351. ;; modify the structure of the document. Therefore
  2352. ;; we ensure that any comment between elements is
  2353. ;; replaced with one empty line, so as to keep them
  2354. ;; separated.
  2355. (add-blank?
  2356. (save-excursion
  2357. (goto-char start)
  2358. (not (or (bobp)
  2359. (eq (org-element-property :contents-begin parent)
  2360. start)
  2361. (eq (org-element-property :contents-end parent)
  2362. end)
  2363. (progn
  2364. (forward-line -1)
  2365. (or (org-looking-at-p "^[ \t]*$")
  2366. (org-with-limited-levels
  2367. (org-at-heading-p)))))))))
  2368. (delete-region start end)
  2369. (when add-blank? (insert "\n"))))))))))
  2370. (defun org-export--prune-tree (data info)
  2371. "Prune non exportable elements from DATA.
  2372. DATA is the parse tree to traverse. INFO is the plist holding
  2373. export info. Also set `:ignore-list' in INFO to a list of
  2374. objects which should be ignored during export, but not removed
  2375. from tree."
  2376. (letrec ((ignore nil)
  2377. ;; First find trees containing a select tag, if any.
  2378. (selected (org-export--selected-trees data info))
  2379. (walk-data
  2380. (lambda (data)
  2381. ;; Prune non-exportable elements and objects from tree.
  2382. ;; As a special case, special rows and cells from tables
  2383. ;; are stored in IGNORE, as they still need to be
  2384. ;; accessed during export.
  2385. (when data
  2386. (let ((type (org-element-type data)))
  2387. (if (org-export--skip-p data info selected)
  2388. (if (memq type '(table-cell table-row)) (push data ignore)
  2389. (org-element-extract-element data))
  2390. (if (and (eq type 'headline)
  2391. (eq (plist-get info :with-archived-trees)
  2392. 'headline)
  2393. (org-element-property :archivedp data))
  2394. ;; If headline is archived but tree below has
  2395. ;; to be skipped, remove contents.
  2396. (org-element-set-contents data)
  2397. ;; Move into recursive objects/elements.
  2398. (mapc walk-data (org-element-contents data)))
  2399. ;; Move into secondary string, if any.
  2400. (dolist (p (cdr (assq type
  2401. org-element-secondary-value-alist)))
  2402. (mapc walk-data (org-element-property p data)))))))))
  2403. ;; If a select tag is active, also ignore the section before the
  2404. ;; first headline, if any.
  2405. (when selected
  2406. (let ((first-element (car (org-element-contents data))))
  2407. (when (eq (org-element-type first-element) 'section)
  2408. (org-element-extract-element first-element))))
  2409. ;; Prune tree and communication channel.
  2410. (funcall walk-data data)
  2411. (dolist (entry
  2412. (append
  2413. ;; Priority is given to back-end specific options.
  2414. (org-export-get-all-options (plist-get info :back-end))
  2415. org-export-options-alist))
  2416. (when (eq (nth 4 entry) 'parse)
  2417. (funcall walk-data (plist-get info (car entry)))))
  2418. ;; Eventually set `:ignore-list'.
  2419. (plist-put info :ignore-list ignore)))
  2420. (defun org-export--remove-uninterpreted-data (data info)
  2421. "Change uninterpreted elements back into Org syntax.
  2422. DATA is the parse tree. INFO is a plist containing export
  2423. options. Each uninterpreted element or object is changed back
  2424. into a string. Contents, if any, are not modified. The parse
  2425. tree is modified by side effect."
  2426. (org-export--remove-uninterpreted-data-1 data info)
  2427. (dolist (entry org-export-options-alist)
  2428. (when (eq (nth 4 entry) 'parse)
  2429. (let ((p (car entry)))
  2430. (plist-put info
  2431. p
  2432. (org-export--remove-uninterpreted-data-1
  2433. (plist-get info p)
  2434. info))))))
  2435. (defun org-export--remove-uninterpreted-data-1 (data info)
  2436. "Change uninterpreted elements back into Org syntax.
  2437. DATA is a parse tree or a secondary string. INFO is a plist
  2438. containing export options. It is modified by side effect and
  2439. returned by the function."
  2440. (org-element-map data
  2441. '(entity bold italic latex-environment latex-fragment strike-through
  2442. subscript superscript underline)
  2443. (lambda (blob)
  2444. (let ((new
  2445. (cl-case (org-element-type blob)
  2446. ;; ... entities...
  2447. (entity
  2448. (and (not (plist-get info :with-entities))
  2449. (list (concat
  2450. (org-export-expand blob nil)
  2451. (make-string
  2452. (or (org-element-property :post-blank blob) 0)
  2453. ?\s)))))
  2454. ;; ... emphasis...
  2455. ((bold italic strike-through underline)
  2456. (and (not (plist-get info :with-emphasize))
  2457. (let ((marker (cl-case (org-element-type blob)
  2458. (bold "*")
  2459. (italic "/")
  2460. (strike-through "+")
  2461. (underline "_"))))
  2462. (append
  2463. (list marker)
  2464. (org-element-contents blob)
  2465. (list (concat
  2466. marker
  2467. (make-string
  2468. (or (org-element-property :post-blank blob)
  2469. 0)
  2470. ?\s)))))))
  2471. ;; ... LaTeX environments and fragments...
  2472. ((latex-environment latex-fragment)
  2473. (and (eq (plist-get info :with-latex) 'verbatim)
  2474. (list (org-export-expand blob nil))))
  2475. ;; ... sub/superscripts...
  2476. ((subscript superscript)
  2477. (let ((sub/super-p (plist-get info :with-sub-superscript))
  2478. (bracketp (org-element-property :use-brackets-p blob)))
  2479. (and (or (not sub/super-p)
  2480. (and (eq sub/super-p '{}) (not bracketp)))
  2481. (append
  2482. (list (concat
  2483. (if (eq (org-element-type blob) 'subscript)
  2484. "_"
  2485. "^")
  2486. (and bracketp "{")))
  2487. (org-element-contents blob)
  2488. (list (concat
  2489. (and bracketp "}")
  2490. (and (org-element-property :post-blank blob)
  2491. (make-string
  2492. (org-element-property :post-blank blob)
  2493. ?\s)))))))))))
  2494. (when new
  2495. ;; Splice NEW at BLOB location in parse tree.
  2496. (dolist (e new (org-element-extract-element blob))
  2497. (unless (equal e "") (org-element-insert-before e blob))))))
  2498. info nil nil t)
  2499. ;; Return modified parse tree.
  2500. data)
  2501. (defun org-export--merge-external-footnote-definitions (tree)
  2502. "Insert footnote definitions outside parsing scope in TREE.
  2503. If there is a footnote section in TREE, definitions found are
  2504. appended to it. If `org-footnote-section' is non-nil, a new
  2505. footnote section containing all definitions is inserted in TREE.
  2506. Otherwise, definitions are appended at the end of the section
  2507. containing their first reference.
  2508. Only definitions actually referred to within TREE, directly or
  2509. not, are considered."
  2510. (let* ((collect-labels
  2511. (lambda (data)
  2512. (org-element-map data 'footnote-reference
  2513. (lambda (f)
  2514. (and (eq (org-element-property :type f) 'standard)
  2515. (org-element-property :label f))))))
  2516. (referenced-labels (funcall collect-labels tree)))
  2517. (when referenced-labels
  2518. (let* ((definitions)
  2519. (push-definition
  2520. (lambda (datum)
  2521. (cl-case (org-element-type datum)
  2522. (footnote-definition
  2523. (push (save-restriction
  2524. (narrow-to-region (org-element-property :begin datum)
  2525. (org-element-property :end datum))
  2526. (org-element-map (org-element-parse-buffer)
  2527. 'footnote-definition #'identity nil t))
  2528. definitions))
  2529. (footnote-reference
  2530. (let ((label (org-element-property :label datum))
  2531. (cbeg (org-element-property :contents-begin datum)))
  2532. (when (and label cbeg
  2533. (eq (org-element-property :type datum) 'inline))
  2534. (push
  2535. (apply #'org-element-create
  2536. 'footnote-definition
  2537. (list :label label :post-blank 1)
  2538. (org-element-parse-secondary-string
  2539. (buffer-substring
  2540. cbeg (org-element-property :contents-end datum))
  2541. (org-element-restriction 'footnote-reference)))
  2542. definitions))))))))
  2543. ;; Collect all out of scope definitions.
  2544. (save-excursion
  2545. (goto-char (point-min))
  2546. (org-with-wide-buffer
  2547. (while (re-search-backward org-footnote-re nil t)
  2548. (funcall push-definition (org-element-context))))
  2549. (goto-char (point-max))
  2550. (org-with-wide-buffer
  2551. (while (re-search-forward org-footnote-re nil t)
  2552. (funcall push-definition (org-element-context)))))
  2553. ;; Filter out definitions referenced neither in the original
  2554. ;; tree nor in the external definitions.
  2555. (let* ((directly-referenced
  2556. (cl-remove-if-not
  2557. (lambda (d)
  2558. (member (org-element-property :label d) referenced-labels))
  2559. definitions))
  2560. (all-labels
  2561. (append (funcall collect-labels directly-referenced)
  2562. referenced-labels)))
  2563. (setq definitions
  2564. (cl-remove-if-not
  2565. (lambda (d)
  2566. (member (org-element-property :label d) all-labels))
  2567. definitions)))
  2568. ;; Install definitions in subtree.
  2569. (cond
  2570. ((null definitions))
  2571. ;; If there is a footnote section, insert them here.
  2572. ((let ((footnote-section
  2573. (org-element-map tree 'headline
  2574. (lambda (h)
  2575. (and (org-element-property :footnote-section-p h) h))
  2576. nil t)))
  2577. (and footnote-section
  2578. (apply #'org-element-adopt-elements (nreverse definitions)))))
  2579. ;; If there should be a footnote section, create one containing
  2580. ;; all the definitions at the end of the tree.
  2581. (org-footnote-section
  2582. (org-element-adopt-elements
  2583. tree
  2584. (org-element-create 'headline
  2585. (list :footnote-section-p t
  2586. :level 1
  2587. :title org-footnote-section)
  2588. (apply #'org-element-create
  2589. 'section
  2590. nil
  2591. (nreverse definitions)))))
  2592. ;; Otherwise add each definition at the end of the section where
  2593. ;; it is first referenced.
  2594. (t
  2595. (letrec ((seen nil)
  2596. (insert-definitions
  2597. (lambda (data)
  2598. ;; Insert definitions in the same section as
  2599. ;; their first reference in DATA.
  2600. (org-element-map data 'footnote-reference
  2601. (lambda (f)
  2602. (when (eq (org-element-property :type f) 'standard)
  2603. (let ((label (org-element-property :label f)))
  2604. (unless (member label seen)
  2605. (push label seen)
  2606. (let ((definition
  2607. (catch 'found
  2608. (dolist (d definitions)
  2609. (when (equal
  2610. (org-element-property :label
  2611. d)
  2612. label)
  2613. (setq definitions
  2614. (delete d definitions))
  2615. (throw 'found d))))))
  2616. (when definition
  2617. (org-element-adopt-elements
  2618. (org-element-lineage f '(section))
  2619. definition)
  2620. (funcall insert-definitions
  2621. definition)))))))))))
  2622. (funcall insert-definitions tree))))))))
  2623. ;;;###autoload
  2624. (defun org-export-as
  2625. (backend &optional subtreep visible-only body-only ext-plist)
  2626. "Transcode current Org buffer into BACKEND code.
  2627. BACKEND is either an export back-end, as returned by, e.g.,
  2628. `org-export-create-backend', or a symbol referring to
  2629. a registered back-end.
  2630. If narrowing is active in the current buffer, only transcode its
  2631. narrowed part.
  2632. If a region is active, transcode that region.
  2633. When optional argument SUBTREEP is non-nil, transcode the
  2634. sub-tree at point, extracting information from the headline
  2635. properties first.
  2636. When optional argument VISIBLE-ONLY is non-nil, don't export
  2637. contents of hidden elements.
  2638. When optional argument BODY-ONLY is non-nil, only return body
  2639. code, without surrounding template.
  2640. Optional argument EXT-PLIST, when provided, is a property list
  2641. with external parameters overriding Org default settings, but
  2642. still inferior to file-local settings.
  2643. Return code as a string."
  2644. (when (symbolp backend) (setq backend (org-export-get-backend backend)))
  2645. (org-export-barf-if-invalid-backend backend)
  2646. (save-excursion
  2647. (save-restriction
  2648. ;; Narrow buffer to an appropriate region or subtree for
  2649. ;; parsing. If parsing subtree, be sure to remove main headline
  2650. ;; too.
  2651. (cond ((org-region-active-p)
  2652. (narrow-to-region (region-beginning) (region-end)))
  2653. (subtreep
  2654. (org-narrow-to-subtree)
  2655. (goto-char (point-min))
  2656. (forward-line)
  2657. (narrow-to-region (point) (point-max))))
  2658. ;; Initialize communication channel with original buffer
  2659. ;; attributes, unavailable in its copy.
  2660. (let* ((org-export-current-backend (org-export-backend-name backend))
  2661. (info (org-combine-plists
  2662. (org-export--get-export-attributes
  2663. backend subtreep visible-only body-only)
  2664. (org-export--get-buffer-attributes)))
  2665. (parsed-keywords
  2666. (delq nil
  2667. (mapcar (lambda (o) (and (eq (nth 4 o) 'parse) (nth 1 o)))
  2668. (append (org-export-get-all-options backend)
  2669. org-export-options-alist))))
  2670. tree)
  2671. ;; Update communication channel and get parse tree. Buffer
  2672. ;; isn't parsed directly. Instead, all buffer modifications
  2673. ;; and consequent parsing are undertaken in a temporary copy.
  2674. (org-export-with-buffer-copy
  2675. ;; Run first hook with current back-end's name as argument.
  2676. (run-hook-with-args 'org-export-before-processing-hook
  2677. (org-export-backend-name backend))
  2678. ;; Include files, delete comments and expand macros.
  2679. (org-export-expand-include-keyword)
  2680. (org-export--delete-comments)
  2681. (org-macro-initialize-templates)
  2682. (org-macro-replace-all org-macro-templates nil parsed-keywords)
  2683. ;; Refresh buffer properties and radio targets after
  2684. ;; potentially invasive previous changes. Likewise, do it
  2685. ;; again after executing Babel code.
  2686. (org-set-regexps-and-options)
  2687. (org-update-radio-target-regexp)
  2688. (org-export-execute-babel-code)
  2689. (org-set-regexps-and-options)
  2690. (org-update-radio-target-regexp)
  2691. ;; Run last hook with current back-end's name as argument.
  2692. ;; Update buffer properties and radio targets one last time
  2693. ;; before parsing.
  2694. (goto-char (point-min))
  2695. (save-excursion
  2696. (run-hook-with-args 'org-export-before-parsing-hook
  2697. (org-export-backend-name backend)))
  2698. (org-set-regexps-and-options)
  2699. (org-update-radio-target-regexp)
  2700. ;; Update communication channel with environment. Also
  2701. ;; install user's and developer's filters.
  2702. (setq info
  2703. (org-export-install-filters
  2704. (org-combine-plists
  2705. info (org-export-get-environment backend subtreep ext-plist))))
  2706. ;; Call options filters and update export options. We do not
  2707. ;; use `org-export-filter-apply-functions' here since the
  2708. ;; arity of such filters is different.
  2709. (let ((backend-name (org-export-backend-name backend)))
  2710. (dolist (filter (plist-get info :filter-options))
  2711. (let ((result (funcall filter info backend-name)))
  2712. (when result (setq info result)))))
  2713. ;; Expand export-specific set of macros: {{{author}}},
  2714. ;; {{{date(FORMAT)}}}, {{{email}}} and {{{title}}}. It must
  2715. ;; be done once regular macros have been expanded, since
  2716. ;; parsed keywords may contain one of them.
  2717. (org-macro-replace-all
  2718. (list
  2719. (cons "author" (org-element-interpret-data (plist-get info :author)))
  2720. (cons "date"
  2721. (let* ((date (plist-get info :date))
  2722. (value (or (org-element-interpret-data date) "")))
  2723. (if (and (consp date)
  2724. (not (cdr date))
  2725. (eq (org-element-type (car date)) 'timestamp))
  2726. (format "(eval (if (org-string-nw-p \"$1\") %s %S))"
  2727. (format "(org-timestamp-format '%S \"$1\")"
  2728. (org-element-copy (car date)))
  2729. value)
  2730. value)))
  2731. (cons "email" (org-element-interpret-data (plist-get info :email)))
  2732. (cons "title" (org-element-interpret-data (plist-get info :title)))
  2733. (cons "results" "$1"))
  2734. 'finalize
  2735. parsed-keywords)
  2736. ;; Parse buffer.
  2737. (setq tree (org-element-parse-buffer nil visible-only))
  2738. ;; Merge footnote definitions outside scope into parse tree.
  2739. (org-export--merge-external-footnote-definitions tree)
  2740. ;; Prune tree from non-exported elements and transform
  2741. ;; uninterpreted elements or objects in both parse tree and
  2742. ;; communication channel.
  2743. (org-export--prune-tree tree info)
  2744. (org-export--remove-uninterpreted-data tree info)
  2745. ;; Call parse tree filters.
  2746. (setq tree
  2747. (org-export-filter-apply-functions
  2748. (plist-get info :filter-parse-tree) tree info))
  2749. ;; Now tree is complete, compute its properties and add them
  2750. ;; to communication channel.
  2751. (setq info (org-export--collect-tree-properties tree info))
  2752. ;; Eventually transcode TREE. Wrap the resulting string into
  2753. ;; a template.
  2754. (let* ((body (org-element-normalize-string
  2755. (or (org-export-data tree info) "")))
  2756. (inner-template (cdr (assq 'inner-template
  2757. (plist-get info :translate-alist))))
  2758. (full-body (org-export-filter-apply-functions
  2759. (plist-get info :filter-body)
  2760. (if (not (functionp inner-template)) body
  2761. (funcall inner-template body info))
  2762. info))
  2763. (template (cdr (assq 'template
  2764. (plist-get info :translate-alist)))))
  2765. ;; Remove all text properties since they cannot be
  2766. ;; retrieved from an external process. Finally call
  2767. ;; final-output filter and return result.
  2768. (org-no-properties
  2769. (org-export-filter-apply-functions
  2770. (plist-get info :filter-final-output)
  2771. (if (or (not (functionp template)) body-only) full-body
  2772. (funcall template full-body info))
  2773. info))))))))
  2774. ;;;###autoload
  2775. (defun org-export-string-as (string backend &optional body-only ext-plist)
  2776. "Transcode STRING into BACKEND code.
  2777. BACKEND is either an export back-end, as returned by, e.g.,
  2778. `org-export-create-backend', or a symbol referring to
  2779. a registered back-end.
  2780. When optional argument BODY-ONLY is non-nil, only return body
  2781. code, without preamble nor postamble.
  2782. Optional argument EXT-PLIST, when provided, is a property list
  2783. with external parameters overriding Org default settings, but
  2784. still inferior to file-local settings.
  2785. Return code as a string."
  2786. (with-temp-buffer
  2787. (insert string)
  2788. (let ((org-inhibit-startup t)) (org-mode))
  2789. (org-export-as backend nil nil body-only ext-plist)))
  2790. ;;;###autoload
  2791. (defun org-export-replace-region-by (backend)
  2792. "Replace the active region by its export to BACKEND.
  2793. BACKEND is either an export back-end, as returned by, e.g.,
  2794. `org-export-create-backend', or a symbol referring to
  2795. a registered back-end."
  2796. (unless (org-region-active-p) (user-error "No active region to replace"))
  2797. (insert
  2798. (org-export-string-as
  2799. (delete-and-extract-region (region-beginning) (region-end)) backend t)))
  2800. ;;;###autoload
  2801. (defun org-export-insert-default-template (&optional backend subtreep)
  2802. "Insert all export keywords with default values at beginning of line.
  2803. BACKEND is a symbol referring to the name of a registered export
  2804. back-end, for which specific export options should be added to
  2805. the template, or `default' for default template. When it is nil,
  2806. the user will be prompted for a category.
  2807. If SUBTREEP is non-nil, export configuration will be set up
  2808. locally for the subtree through node properties."
  2809. (interactive)
  2810. (unless (derived-mode-p 'org-mode) (user-error "Not in an Org mode buffer"))
  2811. (when (and subtreep (org-before-first-heading-p))
  2812. (user-error "No subtree to set export options for"))
  2813. (let ((node (and subtreep (save-excursion (org-back-to-heading t) (point))))
  2814. (backend
  2815. (or backend
  2816. (intern
  2817. (org-completing-read
  2818. "Options category: "
  2819. (cons "default"
  2820. (mapcar (lambda (b)
  2821. (symbol-name (org-export-backend-name b)))
  2822. org-export-registered-backends))
  2823. nil t))))
  2824. options keywords)
  2825. ;; Populate OPTIONS and KEYWORDS.
  2826. (dolist (entry (cond ((eq backend 'default) org-export-options-alist)
  2827. ((org-export-backend-p backend)
  2828. (org-export-backend-options backend))
  2829. (t (org-export-backend-options
  2830. (org-export-get-backend backend)))))
  2831. (let ((keyword (nth 1 entry))
  2832. (option (nth 2 entry)))
  2833. (cond
  2834. (keyword (unless (assoc keyword keywords)
  2835. (let ((value
  2836. (if (eq (nth 4 entry) 'split)
  2837. (mapconcat #'identity (eval (nth 3 entry)) " ")
  2838. (eval (nth 3 entry)))))
  2839. (push (cons keyword value) keywords))))
  2840. (option (unless (assoc option options)
  2841. (push (cons option (eval (nth 3 entry))) options))))))
  2842. ;; Move to an appropriate location in order to insert options.
  2843. (unless subtreep (beginning-of-line))
  2844. ;; First (multiple) OPTIONS lines. Never go past fill-column.
  2845. (when options
  2846. (let ((items
  2847. (mapcar
  2848. #'(lambda (opt) (format "%s:%S" (car opt) (cdr opt)))
  2849. (sort options (lambda (k1 k2) (string< (car k1) (car k2)))))))
  2850. (if subtreep
  2851. (org-entry-put
  2852. node "EXPORT_OPTIONS" (mapconcat 'identity items " "))
  2853. (while items
  2854. (insert "#+OPTIONS:")
  2855. (let ((width 10))
  2856. (while (and items
  2857. (< (+ width (length (car items)) 1) fill-column))
  2858. (let ((item (pop items)))
  2859. (insert " " item)
  2860. (cl-incf width (1+ (length item))))))
  2861. (insert "\n")))))
  2862. ;; Then the rest of keywords, in the order specified in either
  2863. ;; `org-export-options-alist' or respective export back-ends.
  2864. (dolist (key (nreverse keywords))
  2865. (let ((val (cond ((equal (car key) "DATE")
  2866. (or (cdr key)
  2867. (with-temp-buffer
  2868. (org-insert-time-stamp (current-time)))))
  2869. ((equal (car key) "TITLE")
  2870. (or (let ((visited-file
  2871. (buffer-file-name (buffer-base-buffer))))
  2872. (and visited-file
  2873. (file-name-sans-extension
  2874. (file-name-nondirectory visited-file))))
  2875. (buffer-name (buffer-base-buffer))))
  2876. (t (cdr key)))))
  2877. (if subtreep (org-entry-put node (concat "EXPORT_" (car key)) val)
  2878. (insert
  2879. (format "#+%s:%s\n"
  2880. (car key)
  2881. (if (org-string-nw-p val) (format " %s" val) ""))))))))
  2882. (defun org-export-expand-include-keyword (&optional included dir footnotes)
  2883. "Expand every include keyword in buffer.
  2884. Optional argument INCLUDED is a list of included file names along
  2885. with their line restriction, when appropriate. It is used to
  2886. avoid infinite recursion. Optional argument DIR is the current
  2887. working directory. It is used to properly resolve relative
  2888. paths. Optional argument FOOTNOTES is a hash-table used for
  2889. storing and resolving footnotes. It is created automatically."
  2890. (let ((case-fold-search t)
  2891. (file-prefix (make-hash-table :test #'equal))
  2892. (current-prefix 0)
  2893. (footnotes (or footnotes (make-hash-table :test #'equal)))
  2894. (include-re "^[ \t]*#\\+INCLUDE:"))
  2895. ;; If :minlevel is not set the text-property
  2896. ;; `:org-include-induced-level' will be used to determine the
  2897. ;; relative level when expanding INCLUDE.
  2898. ;; Only affects included Org documents.
  2899. (goto-char (point-min))
  2900. (while (re-search-forward include-re nil t)
  2901. (put-text-property (line-beginning-position) (line-end-position)
  2902. :org-include-induced-level
  2903. (1+ (org-reduced-level (or (org-current-level) 0)))))
  2904. ;; Expand INCLUDE keywords.
  2905. (goto-char (point-min))
  2906. (while (re-search-forward include-re nil t)
  2907. (let ((element (save-match-data (org-element-at-point))))
  2908. (when (eq (org-element-type element) 'keyword)
  2909. (beginning-of-line)
  2910. ;; Extract arguments from keyword's value.
  2911. (let* ((value (org-element-property :value element))
  2912. (ind (org-get-indentation))
  2913. location
  2914. (file
  2915. (and (string-match
  2916. "^\\(\".+?\"\\|\\S-+\\)\\(?:\\s-+\\|$\\)" value)
  2917. (prog1
  2918. (save-match-data
  2919. (let ((matched (match-string 1 value)))
  2920. (when (string-match "\\(::\\(.*?\\)\\)\"?\\'"
  2921. matched)
  2922. (setq location (match-string 2 matched))
  2923. (setq matched
  2924. (replace-match "" nil nil matched 1)))
  2925. (expand-file-name
  2926. (org-remove-double-quotes
  2927. matched)
  2928. dir)))
  2929. (setq value (replace-match "" nil nil value)))))
  2930. (only-contents
  2931. (and (string-match ":only-contents *\\([^: \r\t\n]\\S-*\\)?"
  2932. value)
  2933. (prog1 (org-not-nil (match-string 1 value))
  2934. (setq value (replace-match "" nil nil value)))))
  2935. (lines
  2936. (and (string-match
  2937. ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\""
  2938. value)
  2939. (prog1 (match-string 1 value)
  2940. (setq value (replace-match "" nil nil value)))))
  2941. (env (cond ((string-match "\\<example\\>" value)
  2942. 'literal)
  2943. ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
  2944. 'literal)))
  2945. ;; Minimal level of included file defaults to the child
  2946. ;; level of the current headline, if any, or one. It
  2947. ;; only applies is the file is meant to be included as
  2948. ;; an Org one.
  2949. (minlevel
  2950. (and (not env)
  2951. (if (string-match ":minlevel +\\([0-9]+\\)" value)
  2952. (prog1 (string-to-number (match-string 1 value))
  2953. (setq value (replace-match "" nil nil value)))
  2954. (get-text-property (point)
  2955. :org-include-induced-level))))
  2956. (src-args (and (eq env 'literal)
  2957. (match-string 1 value)))
  2958. (block (and (string-match "\\<\\(\\S-+\\)\\>" value)
  2959. (match-string 1 value))))
  2960. ;; Remove keyword.
  2961. (delete-region (point) (progn (forward-line) (point)))
  2962. (cond
  2963. ((not file) nil)
  2964. ((not (file-readable-p file))
  2965. (error "Cannot include file %s" file))
  2966. ;; Check if files has already been parsed. Look after
  2967. ;; inclusion lines too, as different parts of the same file
  2968. ;; can be included too.
  2969. ((member (list file lines) included)
  2970. (error "Recursive file inclusion: %s" file))
  2971. (t
  2972. (cond
  2973. ((eq env 'literal)
  2974. (insert
  2975. (let ((ind-str (make-string ind ? ))
  2976. (arg-str (if (stringp src-args)
  2977. (format " %s" src-args)
  2978. ""))
  2979. (contents
  2980. (org-escape-code-in-string
  2981. (org-export--prepare-file-contents file lines))))
  2982. (format "%s#+BEGIN_%s%s\n%s%s#+END_%s\n"
  2983. ind-str block arg-str contents ind-str block))))
  2984. ((stringp block)
  2985. (insert
  2986. (let ((ind-str (make-string ind ? ))
  2987. (contents
  2988. (org-export--prepare-file-contents file lines)))
  2989. (format "%s#+BEGIN_%s\n%s%s#+END_%s\n"
  2990. ind-str block contents ind-str block))))
  2991. (t
  2992. (insert
  2993. (with-temp-buffer
  2994. (let ((org-inhibit-startup t)
  2995. (lines
  2996. (if location
  2997. (org-export--inclusion-absolute-lines
  2998. file location only-contents lines)
  2999. lines)))
  3000. (org-mode)
  3001. (insert
  3002. (org-export--prepare-file-contents
  3003. file lines ind minlevel
  3004. (or (gethash file file-prefix)
  3005. (puthash file (cl-incf current-prefix) file-prefix))
  3006. footnotes)))
  3007. (org-export-expand-include-keyword
  3008. (cons (list file lines) included)
  3009. (file-name-directory file)
  3010. footnotes)
  3011. (buffer-string)))))
  3012. ;; Expand footnotes after all files have been included.
  3013. ;; Footnotes are stored at end of buffer.
  3014. (unless included
  3015. (org-with-wide-buffer
  3016. (goto-char (point-max))
  3017. (maphash (lambda (k v) (insert (format "\n[%s] %s\n" k v)))
  3018. footnotes)))))))))))
  3019. (defun org-export--inclusion-absolute-lines (file location only-contents lines)
  3020. "Resolve absolute lines for an included file with file-link.
  3021. FILE is string file-name of the file to include. LOCATION is a
  3022. string name within FILE to be included (located via
  3023. `org-link-search'). If ONLY-CONTENTS is non-nil only the
  3024. contents of the named element will be included, as determined
  3025. Org-Element. If LINES is non-nil only those lines are included.
  3026. Return a string of lines to be included in the format expected by
  3027. `org-export--prepare-file-contents'."
  3028. (with-temp-buffer
  3029. (insert-file-contents file)
  3030. (unless (eq major-mode 'org-mode)
  3031. (let ((org-inhibit-startup t)) (org-mode)))
  3032. (condition-case err
  3033. ;; Enforce consistent search.
  3034. (let ((org-link-search-must-match-exact-headline nil))
  3035. (org-link-search location))
  3036. (error
  3037. (error "%s for %s::%s" (error-message-string err) file location)))
  3038. (let* ((element (org-element-at-point))
  3039. (contents-begin
  3040. (and only-contents (org-element-property :contents-begin element))))
  3041. (narrow-to-region
  3042. (or contents-begin (org-element-property :begin element))
  3043. (org-element-property (if contents-begin :contents-end :end) element))
  3044. (when (and only-contents
  3045. (memq (org-element-type element) '(headline inlinetask)))
  3046. ;; Skip planning line and property-drawer.
  3047. (goto-char (point-min))
  3048. (when (org-looking-at-p org-planning-line-re) (forward-line))
  3049. (when (looking-at org-property-drawer-re) (goto-char (match-end 0)))
  3050. (unless (bolp) (forward-line))
  3051. (narrow-to-region (point) (point-max))))
  3052. (when lines
  3053. (org-skip-whitespace)
  3054. (beginning-of-line)
  3055. (let* ((lines (split-string lines "-"))
  3056. (lbeg (string-to-number (car lines)))
  3057. (lend (string-to-number (cadr lines)))
  3058. (beg (if (zerop lbeg) (point-min)
  3059. (goto-char (point-min))
  3060. (forward-line (1- lbeg))
  3061. (point)))
  3062. (end (if (zerop lend) (point-max)
  3063. (goto-char beg)
  3064. (forward-line (1- lend))
  3065. (point))))
  3066. (narrow-to-region beg end)))
  3067. (let ((end (point-max)))
  3068. (goto-char (point-min))
  3069. (widen)
  3070. (let ((start-line (line-number-at-pos)))
  3071. (format "%d-%d"
  3072. start-line
  3073. (save-excursion
  3074. (+ start-line
  3075. (let ((counter 0))
  3076. (while (< (point) end) (cl-incf counter) (forward-line))
  3077. counter))))))))
  3078. (defun org-export--prepare-file-contents
  3079. (file &optional lines ind minlevel id footnotes)
  3080. "Prepare contents of FILE for inclusion and return it as a string.
  3081. When optional argument LINES is a string specifying a range of
  3082. lines, include only those lines.
  3083. Optional argument IND, when non-nil, is an integer specifying the
  3084. global indentation of returned contents. Since its purpose is to
  3085. allow an included file to stay in the same environment it was
  3086. created (e.g., a list item), it doesn't apply past the first
  3087. headline encountered.
  3088. Optional argument MINLEVEL, when non-nil, is an integer
  3089. specifying the level that any top-level headline in the included
  3090. file should have.
  3091. Optional argument ID is an integer that will be inserted before
  3092. each footnote definition and reference if FILE is an Org file.
  3093. This is useful to avoid conflicts when more than one Org file
  3094. with footnotes is included in a document.
  3095. Optional argument FOOTNOTES is a hash-table to store footnotes in
  3096. the included document."
  3097. (with-temp-buffer
  3098. (insert-file-contents file)
  3099. (when lines
  3100. (let* ((lines (split-string lines "-"))
  3101. (lbeg (string-to-number (car lines)))
  3102. (lend (string-to-number (cadr lines)))
  3103. (beg (if (zerop lbeg) (point-min)
  3104. (goto-char (point-min))
  3105. (forward-line (1- lbeg))
  3106. (point)))
  3107. (end (if (zerop lend) (point-max)
  3108. (goto-char (point-min))
  3109. (forward-line (1- lend))
  3110. (point))))
  3111. (narrow-to-region beg end)))
  3112. ;; Remove blank lines at beginning and end of contents. The logic
  3113. ;; behind that removal is that blank lines around include keyword
  3114. ;; override blank lines in included file.
  3115. (goto-char (point-min))
  3116. (org-skip-whitespace)
  3117. (beginning-of-line)
  3118. (delete-region (point-min) (point))
  3119. (goto-char (point-max))
  3120. (skip-chars-backward " \r\t\n")
  3121. (forward-line)
  3122. (delete-region (point) (point-max))
  3123. ;; If IND is set, preserve indentation of include keyword until
  3124. ;; the first headline encountered.
  3125. (when (and ind (> ind 0))
  3126. (unless (eq major-mode 'org-mode)
  3127. (let ((org-inhibit-startup t)) (org-mode)))
  3128. (goto-char (point-min))
  3129. (let ((ind-str (make-string ind ? )))
  3130. (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
  3131. ;; Do not move footnote definitions out of column 0.
  3132. (unless (and (looking-at org-footnote-definition-re)
  3133. (eq (org-element-type (org-element-at-point))
  3134. 'footnote-definition))
  3135. (insert ind-str))
  3136. (forward-line))))
  3137. ;; When MINLEVEL is specified, compute minimal level for headlines
  3138. ;; in the file (CUR-MIN), and remove stars to each headline so
  3139. ;; that headlines with minimal level have a level of MINLEVEL.
  3140. (when minlevel
  3141. (unless (eq major-mode 'org-mode)
  3142. (let ((org-inhibit-startup t)) (org-mode)))
  3143. (org-with-limited-levels
  3144. (let ((levels (org-map-entries
  3145. (lambda () (org-reduced-level (org-current-level))))))
  3146. (when levels
  3147. (let ((offset (- minlevel (apply #'min levels))))
  3148. (unless (zerop offset)
  3149. (when org-odd-levels-only (setq offset (* offset 2)))
  3150. ;; Only change stars, don't bother moving whole
  3151. ;; sections.
  3152. (org-map-entries
  3153. (lambda ()
  3154. (if (< offset 0) (delete-char (abs offset))
  3155. (insert (make-string offset ?*)))))))))))
  3156. ;; Append ID to all footnote references and definitions, so they
  3157. ;; become file specific and cannot collide with footnotes in other
  3158. ;; included files. Further, collect relevant footnote definitions
  3159. ;; outside of LINES, in order to reintroduce them later.
  3160. (when id
  3161. (let ((marker-min (point-min-marker))
  3162. (marker-max (point-max-marker))
  3163. (get-new-label
  3164. (lambda (label)
  3165. ;; Generate new label from LABEL. If LABEL is akin to
  3166. ;; [1] convert it to [fn:--ID-1]. Otherwise add "-ID-"
  3167. ;; after "fn:".
  3168. (if (org-string-match-p "\\`[0-9]+\\'" label)
  3169. (format "fn:--%d-%s" id label)
  3170. (format "fn:-%d-%s" id (substring label 3)))))
  3171. (set-new-label
  3172. (lambda (f old new)
  3173. ;; Replace OLD label with NEW in footnote F.
  3174. (save-excursion
  3175. (goto-char (1+ (org-element-property :begin f)))
  3176. (looking-at (regexp-quote old))
  3177. (replace-match new))))
  3178. (seen-alist))
  3179. (goto-char (point-min))
  3180. (while (re-search-forward org-footnote-re nil t)
  3181. (let ((footnote (save-excursion
  3182. (backward-char)
  3183. (org-element-context))))
  3184. (when (memq (org-element-type footnote)
  3185. '(footnote-definition footnote-reference))
  3186. (let* ((label (org-element-property :label footnote)))
  3187. ;; Update the footnote-reference at point and collect
  3188. ;; the new label, which is only used for footnotes
  3189. ;; outsides LINES.
  3190. (when label
  3191. (let ((seen (cdr (assoc label seen-alist))))
  3192. (if seen (funcall set-new-label footnote label seen)
  3193. (let ((new (funcall get-new-label label)))
  3194. (push (cons label new) seen-alist)
  3195. (org-with-wide-buffer
  3196. (let* ((def (org-footnote-get-definition label))
  3197. (beg (nth 1 def)))
  3198. (when (and def
  3199. (or (< beg marker-min)
  3200. (>= beg marker-max)))
  3201. ;; Store since footnote-definition is
  3202. ;; outside of LINES.
  3203. (puthash new
  3204. (org-element-normalize-string (nth 3 def))
  3205. footnotes))))
  3206. (funcall set-new-label footnote label new)))))))))
  3207. (set-marker marker-min nil)
  3208. (set-marker marker-max nil)))
  3209. (org-element-normalize-string (buffer-string))))
  3210. (defun org-export-execute-babel-code ()
  3211. "Execute every Babel code in the visible part of current buffer."
  3212. ;; Get a pristine copy of current buffer so Babel references can be
  3213. ;; properly resolved.
  3214. (let ((reference (org-export-copy-buffer)))
  3215. (unwind-protect (org-babel-exp-process-buffer reference)
  3216. (kill-buffer reference))))
  3217. (defun org-export--copy-to-kill-ring-p ()
  3218. "Return a non-nil value when output should be added to the kill ring.
  3219. See also `org-export-copy-to-kill-ring'."
  3220. (if (eq org-export-copy-to-kill-ring 'if-interactive)
  3221. (not (or executing-kbd-macro noninteractive))
  3222. (eq org-export-copy-to-kill-ring t)))
  3223. ;;; Tools For Back-Ends
  3224. ;;
  3225. ;; A whole set of tools is available to help build new exporters. Any
  3226. ;; function general enough to have its use across many back-ends
  3227. ;; should be added here.
  3228. ;;;; For Affiliated Keywords
  3229. ;;
  3230. ;; `org-export-read-attribute' reads a property from a given element
  3231. ;; as a plist. It can be used to normalize affiliated keywords'
  3232. ;; syntax.
  3233. ;;
  3234. ;; Since captions can span over multiple lines and accept dual values,
  3235. ;; their internal representation is a bit tricky. Therefore,
  3236. ;; `org-export-get-caption' transparently returns a given element's
  3237. ;; caption as a secondary string.
  3238. (defun org-export-read-attribute (attribute element &optional property)
  3239. "Turn ATTRIBUTE property from ELEMENT into a plist.
  3240. When optional argument PROPERTY is non-nil, return the value of
  3241. that property within attributes.
  3242. This function assumes attributes are defined as \":keyword
  3243. value\" pairs. It is appropriate for `:attr_html' like
  3244. properties.
  3245. All values will become strings except the empty string and
  3246. \"nil\", which will become nil. Also, values containing only
  3247. double quotes will be read as-is, which means that \"\" value
  3248. will become the empty string."
  3249. (let* ((prepare-value
  3250. (lambda (str)
  3251. (save-match-data
  3252. (cond ((member str '(nil "" "nil")) nil)
  3253. ((string-match "^\"\\(\"+\\)?\"$" str)
  3254. (or (match-string 1 str) ""))
  3255. (t str)))))
  3256. (attributes
  3257. (let ((value (org-element-property attribute element)))
  3258. (when value
  3259. (let ((s (mapconcat 'identity value " ")) result)
  3260. (while (string-match
  3261. "\\(?:^\\|[ \t]+\\)\\(:[-a-zA-Z0-9_]+\\)\\([ \t]+\\|$\\)"
  3262. s)
  3263. (let ((value (substring s 0 (match-beginning 0))))
  3264. (push (funcall prepare-value value) result))
  3265. (push (intern (match-string 1 s)) result)
  3266. (setq s (substring s (match-end 0))))
  3267. ;; Ignore any string before first property with `cdr'.
  3268. (cdr (nreverse (cons (funcall prepare-value s) result))))))))
  3269. (if property (plist-get attributes property) attributes)))
  3270. (defun org-export-get-caption (element &optional shortp)
  3271. "Return caption from ELEMENT as a secondary string.
  3272. When optional argument SHORTP is non-nil, return short caption,
  3273. as a secondary string, instead.
  3274. Caption lines are separated by a white space."
  3275. (let ((full-caption (org-element-property :caption element)) caption)
  3276. (dolist (line full-caption (cdr caption))
  3277. (let ((cap (funcall (if shortp 'cdr 'car) line)))
  3278. (when cap
  3279. (setq caption (nconc (list " ") (copy-sequence cap) caption)))))))
  3280. ;;;; For Derived Back-ends
  3281. ;;
  3282. ;; `org-export-with-backend' is a function allowing to locally use
  3283. ;; another back-end to transcode some object or element. In a derived
  3284. ;; back-end, it may be used as a fall-back function once all specific
  3285. ;; cases have been treated.
  3286. (defun org-export-with-backend (backend data &optional contents info)
  3287. "Call a transcoder from BACKEND on DATA.
  3288. BACKEND is an export back-end, as returned by, e.g.,
  3289. `org-export-create-backend', or a symbol referring to
  3290. a registered back-end. DATA is an Org element, object, secondary
  3291. string or string. CONTENTS, when non-nil, is the transcoded
  3292. contents of DATA element, as a string. INFO, when non-nil, is
  3293. the communication channel used for export, as a plist."
  3294. (when (symbolp backend) (setq backend (org-export-get-backend backend)))
  3295. (org-export-barf-if-invalid-backend backend)
  3296. (let ((type (org-element-type data)))
  3297. (if (memq type '(nil org-data)) (error "No foreign transcoder available")
  3298. (let* ((all-transcoders (org-export-get-all-transcoders backend))
  3299. (transcoder (cdr (assq type all-transcoders))))
  3300. (if (not (functionp transcoder))
  3301. (error "No foreign transcoder available")
  3302. (funcall
  3303. transcoder data contents
  3304. (org-combine-plists
  3305. info (list
  3306. :back-end backend
  3307. :translate-alist all-transcoders
  3308. :exported-data (make-hash-table :test #'eq :size 401)))))))))
  3309. ;;;; For Export Snippets
  3310. ;;
  3311. ;; Every export snippet is transmitted to the back-end. Though, the
  3312. ;; latter will only retain one type of export-snippet, ignoring
  3313. ;; others, based on the former's target back-end. The function
  3314. ;; `org-export-snippet-backend' returns that back-end for a given
  3315. ;; export-snippet.
  3316. (defun org-export-snippet-backend (export-snippet)
  3317. "Return EXPORT-SNIPPET targeted back-end as a symbol.
  3318. Translation, with `org-export-snippet-translation-alist', is
  3319. applied."
  3320. (let ((back-end (org-element-property :back-end export-snippet)))
  3321. (intern
  3322. (or (cdr (assoc back-end org-export-snippet-translation-alist))
  3323. back-end))))
  3324. ;;;; For Footnotes
  3325. ;;
  3326. ;; `org-export-collect-footnote-definitions' is a tool to list
  3327. ;; actually used footnotes definitions in the whole parse tree, or in
  3328. ;; a headline, in order to add footnote listings throughout the
  3329. ;; transcoded data.
  3330. ;;
  3331. ;; `org-export-footnote-first-reference-p' is a predicate used by some
  3332. ;; back-ends, when they need to attach the footnote definition only to
  3333. ;; the first occurrence of the corresponding label.
  3334. ;;
  3335. ;; `org-export-get-footnote-definition' and
  3336. ;; `org-export-get-footnote-number' provide easier access to
  3337. ;; additional information relative to a footnote reference.
  3338. (defun org-export-get-footnote-definition (footnote-reference info)
  3339. "Return definition of FOOTNOTE-REFERENCE as parsed data.
  3340. INFO is the plist used as a communication channel. If no such
  3341. definition can be found, raise an error."
  3342. (let ((label (org-element-property :label footnote-reference)))
  3343. (if (not label) (org-element-contents footnote-reference)
  3344. (let ((cache (or (plist-get info :footnote-definition-cache)
  3345. (let ((hash (make-hash-table :test #'equal)))
  3346. (plist-put info :footnote-definition-cache hash)
  3347. hash))))
  3348. (or (gethash label cache)
  3349. (puthash label
  3350. (org-element-map (plist-get info :parse-tree)
  3351. '(footnote-definition footnote-reference)
  3352. (lambda (f)
  3353. (and (equal (org-element-property :label f) label)
  3354. (org-element-contents f)))
  3355. info t)
  3356. cache)
  3357. (error "Definition not found for footnote %s" label))))))
  3358. (defun org-export--footnote-reference-map
  3359. (function data info &optional body-first)
  3360. "Apply FUNCTION on every footnote reference in DATA.
  3361. INFO is a plist containing export state. By default, as soon as
  3362. a new footnote reference is encountered, FUNCTION is called onto
  3363. its definition. However, if BODY-FIRST is non-nil, this step is
  3364. delayed until the end of the process."
  3365. (letrec ((definitions nil)
  3366. (seen-refs nil)
  3367. (search-ref
  3368. (lambda (data delayp)
  3369. ;; Search footnote references through DATA, filling
  3370. ;; SEEN-REFS along the way. When DELAYP is non-nil,
  3371. ;; store footnote definitions so they can be entered
  3372. ;; later.
  3373. (org-element-map data 'footnote-reference
  3374. (lambda (f)
  3375. (funcall function f)
  3376. (let ((--label (org-element-property :label f)))
  3377. (unless (and --label (member --label seen-refs))
  3378. (when --label (push --label seen-refs))
  3379. ;; Search for subsequent references in footnote
  3380. ;; definition so numbering follows reading
  3381. ;; logic, unless DELAYP in non-nil.
  3382. (cond
  3383. (delayp
  3384. (push (org-export-get-footnote-definition f info)
  3385. definitions))
  3386. ;; Do not force entering inline definitions,
  3387. ;; since `org-element-map' already traverses
  3388. ;; them at the right time.
  3389. ((eq (org-element-property :type f) 'inline))
  3390. (t (funcall search-ref
  3391. (org-export-get-footnote-definition f info)
  3392. nil))))))
  3393. info nil
  3394. ;; Don't enter footnote definitions since it will
  3395. ;; happen when their first reference is found.
  3396. ;; Moreover, if DELAYP is non-nil, make sure we
  3397. ;; postpone entering definitions of inline references.
  3398. (if delayp '(footnote-definition footnote-reference)
  3399. 'footnote-definition)))))
  3400. (funcall search-ref data body-first)
  3401. (funcall search-ref (nreverse definitions) nil)))
  3402. (defun org-export-collect-footnote-definitions (info &optional data body-first)
  3403. "Return an alist between footnote numbers, labels and definitions.
  3404. INFO is the current export state, as a plist.
  3405. Definitions are collected throughout the whole parse tree, or
  3406. DATA when non-nil.
  3407. Sorting is done by order of references. As soon as a new
  3408. reference is encountered, other references are searched within
  3409. its definition. However, if BODY-FIRST is non-nil, this step is
  3410. delayed after the whole tree is checked. This alters results
  3411. when references are found in footnote definitions.
  3412. Definitions either appear as Org data or as a secondary string
  3413. for inlined footnotes. Unreferenced definitions are ignored."
  3414. (let ((n 0) labels alist)
  3415. (org-export--footnote-reference-map
  3416. (lambda (f)
  3417. ;; Collect footnote number, label and definition.
  3418. (let ((l (org-element-property :label f)))
  3419. (unless (and l (member l labels))
  3420. (cl-incf n)
  3421. (push (list n l (org-export-get-footnote-definition f info)) alist))
  3422. (when l (push l labels))))
  3423. (or data (plist-get info :parse-tree)) info body-first)
  3424. (nreverse alist)))
  3425. (defun org-export-footnote-first-reference-p
  3426. (footnote-reference info &optional data body-first)
  3427. "Non-nil when a footnote reference is the first one for its label.
  3428. FOOTNOTE-REFERENCE is the footnote reference being considered.
  3429. INFO is a plist containing current export state.
  3430. Search is done throughout the whole parse tree, or DATA when
  3431. non-nil.
  3432. By default, as soon as a new footnote reference is encountered,
  3433. other references are searched within its definition. However, if
  3434. BODY-FIRST is non-nil, this step is delayed after the whole tree
  3435. is checked. This alters results when references are found in
  3436. footnote definitions."
  3437. (let ((label (org-element-property :label footnote-reference)))
  3438. ;; Anonymous footnotes are always a first reference.
  3439. (or (not label)
  3440. (catch 'exit
  3441. (org-export--footnote-reference-map
  3442. (lambda (f)
  3443. (let ((l (org-element-property :label f)))
  3444. (when (and l label (string= label l))
  3445. (throw 'exit (eq footnote-reference f)))))
  3446. (or data (plist-get info :parse-tree)) info body-first)))))
  3447. (defun org-export-get-footnote-number (footnote info &optional data body-first)
  3448. "Return number associated to a footnote.
  3449. FOOTNOTE is either a footnote reference or a footnote definition.
  3450. INFO is the plist containing export state.
  3451. Number is unique throughout the whole parse tree, or DATA, when
  3452. non-nil.
  3453. By default, as soon as a new footnote reference is encountered,
  3454. counting process moves into its definition. However, if
  3455. BODY-FIRST is non-nil, this step is delayed until the end of the
  3456. process, leading to a different order when footnotes are nested."
  3457. (let ((count 0)
  3458. (seen)
  3459. (label (org-element-property :label footnote)))
  3460. (catch 'exit
  3461. (org-export--footnote-reference-map
  3462. (lambda (f)
  3463. (let ((l (org-element-property :label f)))
  3464. (cond
  3465. ;; Anonymous footnote match: return number.
  3466. ((and (not l) (not label) (eq footnote f)) (throw 'exit (1+ count)))
  3467. ;; Labels match: return number.
  3468. ((and label l (string= label l)) (throw 'exit (1+ count)))
  3469. ;; Otherwise store label and increase counter if label
  3470. ;; wasn't encountered yet.
  3471. ((not l) (cl-incf count))
  3472. ((not (member l seen)) (push l seen) (cl-incf count)))))
  3473. (or data (plist-get info :parse-tree)) info body-first))))
  3474. ;;;; For Headlines
  3475. ;;
  3476. ;; `org-export-get-relative-level' is a shortcut to get headline
  3477. ;; level, relatively to the lower headline level in the parsed tree.
  3478. ;;
  3479. ;; `org-export-get-headline-number' returns the section number of an
  3480. ;; headline, while `org-export-number-to-roman' allows to convert it
  3481. ;; to roman numbers. With an optional argument,
  3482. ;; `org-export-get-headline-number' returns a number to unnumbered
  3483. ;; headlines (used for internal id).
  3484. ;;
  3485. ;; `org-export-low-level-p', `org-export-first-sibling-p' and
  3486. ;; `org-export-last-sibling-p' are three useful predicates when it
  3487. ;; comes to fulfill the `:headline-levels' property.
  3488. ;;
  3489. ;; `org-export-get-tags', `org-export-get-category' and
  3490. ;; `org-export-get-node-property' extract useful information from an
  3491. ;; headline or a parent headline. They all handle inheritance.
  3492. ;;
  3493. ;; `org-export-get-alt-title' tries to retrieve an alternative title,
  3494. ;; as a secondary string, suitable for table of contents. It falls
  3495. ;; back onto default title.
  3496. (defun org-export-get-relative-level (headline info)
  3497. "Return HEADLINE relative level within current parsed tree.
  3498. INFO is a plist holding contextual information."
  3499. (+ (org-element-property :level headline)
  3500. (or (plist-get info :headline-offset) 0)))
  3501. (defun org-export-low-level-p (headline info)
  3502. "Non-nil when HEADLINE is considered as low level.
  3503. INFO is a plist used as a communication channel.
  3504. A low level headlines has a relative level greater than
  3505. `:headline-levels' property value.
  3506. Return value is the difference between HEADLINE relative level
  3507. and the last level being considered as high enough, or nil."
  3508. (let ((limit (plist-get info :headline-levels)))
  3509. (when (wholenump limit)
  3510. (let ((level (org-export-get-relative-level headline info)))
  3511. (and (> level limit) (- level limit))))))
  3512. (defun org-export-get-headline-number (headline info)
  3513. "Return numbered HEADLINE numbering as a list of numbers.
  3514. INFO is a plist holding contextual information."
  3515. (and (org-export-numbered-headline-p headline info)
  3516. (cdr (assq headline (plist-get info :headline-numbering)))))
  3517. (defun org-export-numbered-headline-p (headline info)
  3518. "Return a non-nil value if HEADLINE element should be numbered.
  3519. INFO is a plist used as a communication channel."
  3520. (unless (cl-some
  3521. (lambda (head) (org-not-nil (org-element-property :UNNUMBERED head)))
  3522. (org-element-lineage headline nil t))
  3523. (let ((sec-num (plist-get info :section-numbers))
  3524. (level (org-export-get-relative-level headline info)))
  3525. (if (wholenump sec-num) (<= level sec-num) sec-num))))
  3526. (defun org-export-number-to-roman (n)
  3527. "Convert integer N into a roman numeral."
  3528. (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
  3529. ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
  3530. ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
  3531. ( 1 . "I")))
  3532. (res ""))
  3533. (if (<= n 0)
  3534. (number-to-string n)
  3535. (while roman
  3536. (if (>= n (caar roman))
  3537. (setq n (- n (caar roman))
  3538. res (concat res (cdar roman)))
  3539. (pop roman)))
  3540. res)))
  3541. (defun org-export-get-tags (element info &optional tags inherited)
  3542. "Return list of tags associated to ELEMENT.
  3543. ELEMENT has either an `headline' or an `inlinetask' type. INFO
  3544. is a plist used as a communication channel.
  3545. When non-nil, optional argument TAGS should be a list of strings.
  3546. Any tag belonging to this list will also be removed.
  3547. When optional argument INHERITED is non-nil, tags can also be
  3548. inherited from parent headlines and FILETAGS keywords."
  3549. (cl-remove-if
  3550. (lambda (tag) (member tag tags))
  3551. (if (not inherited) (org-element-property :tags element)
  3552. ;; Build complete list of inherited tags.
  3553. (let ((current-tag-list (org-element-property :tags element)))
  3554. (dolist (parent (org-element-lineage element))
  3555. (dolist (tag (org-element-property :tags parent))
  3556. (when (and (memq (org-element-type parent) '(headline inlinetask))
  3557. (not (member tag current-tag-list)))
  3558. (push tag current-tag-list))))
  3559. ;; Add FILETAGS keywords and return results.
  3560. (org-uniquify (append (plist-get info :filetags) current-tag-list))))))
  3561. (defun org-export-get-node-property (property blob &optional inherited)
  3562. "Return node PROPERTY value for BLOB.
  3563. PROPERTY is an upcase symbol (i.e. `:COOKIE_DATA'). BLOB is an
  3564. element or object.
  3565. If optional argument INHERITED is non-nil, the value can be
  3566. inherited from a parent headline.
  3567. Return value is a string or nil."
  3568. (let ((headline (if (eq (org-element-type blob) 'headline) blob
  3569. (org-export-get-parent-headline blob))))
  3570. (if (not inherited) (org-element-property property blob)
  3571. (let ((parent headline))
  3572. (catch 'found
  3573. (while parent
  3574. (when (plist-member (nth 1 parent) property)
  3575. (throw 'found (org-element-property property parent)))
  3576. (setq parent (org-element-property :parent parent))))))))
  3577. (defun org-export-get-category (blob info)
  3578. "Return category for element or object BLOB.
  3579. INFO is a plist used as a communication channel.
  3580. CATEGORY is automatically inherited from a parent headline, from
  3581. #+CATEGORY: keyword or created out of original file name. If all
  3582. fail, the fall-back value is \"???\"."
  3583. (or (org-export-get-node-property :CATEGORY blob t)
  3584. (org-element-map (plist-get info :parse-tree) 'keyword
  3585. (lambda (kwd)
  3586. (when (equal (org-element-property :key kwd) "CATEGORY")
  3587. (org-element-property :value kwd)))
  3588. info 'first-match)
  3589. (let ((file (plist-get info :input-file)))
  3590. (and file (file-name-sans-extension (file-name-nondirectory file))))
  3591. "???"))
  3592. (defun org-export-get-alt-title (headline _)
  3593. "Return alternative title for HEADLINE, as a secondary string.
  3594. If no optional title is defined, fall-back to the regular title."
  3595. (let ((alt (org-element-property :ALT_TITLE headline)))
  3596. (if alt (org-element-parse-secondary-string
  3597. alt (org-element-restriction 'headline) headline)
  3598. (org-element-property :title headline))))
  3599. (defun org-export-first-sibling-p (blob info)
  3600. "Non-nil when BLOB is the first sibling in its parent.
  3601. BLOB is an element or an object. If BLOB is a headline, non-nil
  3602. means it is the first sibling in the sub-tree. INFO is a plist
  3603. used as a communication channel."
  3604. (memq (org-element-type (org-export-get-previous-element blob info))
  3605. '(nil section)))
  3606. (defun org-export-last-sibling-p (blob info)
  3607. "Non-nil when BLOB is the last sibling in its parent.
  3608. BLOB is an element or an object. INFO is a plist used as
  3609. a communication channel."
  3610. (not (org-export-get-next-element blob info)))
  3611. ;;;; For Keywords
  3612. ;;
  3613. ;; `org-export-get-date' returns a date appropriate for the document
  3614. ;; to about to be exported. In particular, it takes care of
  3615. ;; `org-export-date-timestamp-format'.
  3616. (defun org-export-get-date (info &optional fmt)
  3617. "Return date value for the current document.
  3618. INFO is a plist used as a communication channel. FMT, when
  3619. non-nil, is a time format string that will be applied on the date
  3620. if it consists in a single timestamp object. It defaults to
  3621. `org-export-date-timestamp-format' when nil.
  3622. A proper date can be a secondary string, a string or nil. It is
  3623. meant to be translated with `org-export-data' or alike."
  3624. (let ((date (plist-get info :date))
  3625. (fmt (or fmt org-export-date-timestamp-format)))
  3626. (cond ((not date) nil)
  3627. ((and fmt
  3628. (not (cdr date))
  3629. (eq (org-element-type (car date)) 'timestamp))
  3630. (org-timestamp-format (car date) fmt))
  3631. (t date))))
  3632. ;;;; For Links
  3633. ;;
  3634. ;; `org-export-custom-protocol-maybe' handles custom protocol defined
  3635. ;; with `org-add-link-type', which see.
  3636. ;;
  3637. ;; `org-export-get-coderef-format' returns an appropriate format
  3638. ;; string for coderefs.
  3639. ;;
  3640. ;; `org-export-inline-image-p' returns a non-nil value when the link
  3641. ;; provided should be considered as an inline image.
  3642. ;;
  3643. ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
  3644. ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
  3645. ;; returns an appropriate unique identifier.
  3646. ;;
  3647. ;; `org-export-resolve-id-link' returns the first headline with
  3648. ;; specified id or custom-id in parse tree, the path to the external
  3649. ;; file with the id.
  3650. ;;
  3651. ;; `org-export-resolve-coderef' associates a reference to a line
  3652. ;; number in the element it belongs, or returns the reference itself
  3653. ;; when the element isn't numbered.
  3654. ;;
  3655. ;; `org-export-file-uri' expands a filename as stored in :path value
  3656. ;; of a "file" link into a file URI.
  3657. ;;
  3658. ;; Broken links raise a `org-link-broken' error, which is caught by
  3659. ;; `org-export-data' for further processing, depending on
  3660. ;; `org-export-with-broken-links' value.
  3661. (org-define-error 'org-link-broken "Unable to resolve link; aborting")
  3662. (defun org-export-custom-protocol-maybe (link desc backend)
  3663. "Try exporting LINK with a dedicated function.
  3664. DESC is its description, as a string, or nil. BACKEND is the
  3665. back-end used for export, as a symbol.
  3666. Return output as a string, or nil if no protocol handles LINK.
  3667. A custom protocol has precedence over regular back-end export.
  3668. The function ignores links with an implicit type (e.g.,
  3669. \"custom-id\")."
  3670. (let ((type (org-element-property :type link)))
  3671. (unless (or (member type '("coderef" "custom-id" "fuzzy" "radio"))
  3672. (not backend))
  3673. (let ((protocol (nth 2 (assoc type org-link-protocols))))
  3674. (and (functionp protocol)
  3675. (funcall protocol
  3676. (org-link-unescape (org-element-property :path link))
  3677. desc
  3678. backend))))))
  3679. (defun org-export-get-coderef-format (path desc)
  3680. "Return format string for code reference link.
  3681. PATH is the link path. DESC is its description."
  3682. (save-match-data
  3683. (cond ((not desc) "%s")
  3684. ((string-match (regexp-quote (concat "(" path ")")) desc)
  3685. (replace-match "%s" t t desc))
  3686. (t desc))))
  3687. (defun org-export-inline-image-p (link &optional rules)
  3688. "Non-nil if LINK object points to an inline image.
  3689. Optional argument is a set of RULES defining inline images. It
  3690. is an alist where associations have the following shape:
  3691. (TYPE . REGEXP)
  3692. Applying a rule means apply REGEXP against LINK's path when its
  3693. type is TYPE. The function will return a non-nil value if any of
  3694. the provided rules is non-nil. The default rule is
  3695. `org-export-default-inline-image-rule'.
  3696. This only applies to links without a description."
  3697. (and (not (org-element-contents link))
  3698. (let ((case-fold-search t))
  3699. (catch 'exit
  3700. (dolist (rule (or rules org-export-default-inline-image-rule))
  3701. (and (string= (org-element-property :type link) (car rule))
  3702. (org-string-match-p (cdr rule)
  3703. (org-element-property :path link))
  3704. (throw 'exit t)))))))
  3705. (defun org-export-resolve-coderef (ref info)
  3706. "Resolve a code reference REF.
  3707. INFO is a plist used as a communication channel.
  3708. Return associated line number in source code, or REF itself,
  3709. depending on src-block or example element's switches. Throw an
  3710. error if no block contains REF."
  3711. (or (org-element-map (plist-get info :parse-tree) '(example-block src-block)
  3712. (lambda (el)
  3713. (with-temp-buffer
  3714. (insert (org-trim (org-element-property :value el)))
  3715. (let* ((label-fmt (regexp-quote
  3716. (or (org-element-property :label-fmt el)
  3717. org-coderef-label-format)))
  3718. (ref-re
  3719. (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
  3720. (format label-fmt ref))))
  3721. ;; Element containing REF is found. Resolve it to
  3722. ;; either a label or a line number, as needed.
  3723. (when (re-search-backward ref-re nil t)
  3724. (cond
  3725. ((org-element-property :use-labels el) ref)
  3726. ((eq (org-element-property :number-lines el) 'continued)
  3727. (+ (org-export-get-loc el info) (line-number-at-pos)))
  3728. (t (line-number-at-pos)))))))
  3729. info 'first-match)
  3730. (signal 'org-link-broken (list ref))))
  3731. (defun org-export-resolve-fuzzy-link (link info)
  3732. "Return LINK destination.
  3733. INFO is a plist holding contextual information.
  3734. Return value can be an object or an element:
  3735. - If LINK path matches a target object (i.e. <<path>>) return it.
  3736. - If LINK path exactly matches the name affiliated keyword
  3737. (i.e. #+NAME: path) of an element, return that element.
  3738. - If LINK path exactly matches any headline name, return that
  3739. element.
  3740. - Otherwise, throw an error.
  3741. Assume LINK type is \"fuzzy\". White spaces are not
  3742. significant."
  3743. (let* ((raw-path (org-link-unescape (org-element-property :path link)))
  3744. (headline-only (eq (string-to-char raw-path) ?*))
  3745. ;; Split PATH at white spaces so matches are space
  3746. ;; insensitive.
  3747. (path (org-split-string
  3748. (if headline-only (substring raw-path 1) raw-path)))
  3749. (link-cache
  3750. (or (plist-get info :resolve-fuzzy-link-cache)
  3751. (plist-get (plist-put info
  3752. :resolve-fuzzy-link-cache
  3753. (make-hash-table :test #'equal))
  3754. :resolve-fuzzy-link-cache)))
  3755. (cached (gethash path link-cache 'not-found)))
  3756. (if (not (eq cached 'not-found)) cached
  3757. (let ((ast (plist-get info :parse-tree)))
  3758. (puthash
  3759. path
  3760. (cond
  3761. ;; First try to find a matching "<<path>>" unless user
  3762. ;; specified he was looking for a headline (path starts with
  3763. ;; a "*" character).
  3764. ((and (not headline-only)
  3765. (org-element-map ast 'target
  3766. (lambda (datum)
  3767. (and (equal (org-split-string
  3768. (org-element-property :value datum))
  3769. path)
  3770. datum))
  3771. info 'first-match)))
  3772. ;; Then try to find an element with a matching "#+NAME: path"
  3773. ;; affiliated keyword.
  3774. ((and (not headline-only)
  3775. (org-element-map ast org-element-all-elements
  3776. (lambda (datum)
  3777. (let ((name (org-element-property :name datum)))
  3778. (and name (equal (org-split-string name) path) datum)))
  3779. info 'first-match)))
  3780. ;; Try to find a matching headline.
  3781. ((org-element-map ast 'headline
  3782. (lambda (h)
  3783. (and (equal (org-split-string
  3784. (replace-regexp-in-string
  3785. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  3786. (org-element-property :raw-value h)))
  3787. path)
  3788. h))
  3789. info 'first-match))
  3790. (t (signal 'org-link-broken (list raw-path))))
  3791. link-cache)))))
  3792. (defun org-export-resolve-id-link (link info)
  3793. "Return headline referenced as LINK destination.
  3794. INFO is a plist used as a communication channel.
  3795. Return value can be the headline element matched in current parse
  3796. tree or a file name. Assume LINK type is either \"id\" or
  3797. \"custom-id\". Throw an error if no match is found."
  3798. (let ((id (org-element-property :path link)))
  3799. ;; First check if id is within the current parse tree.
  3800. (or (org-element-map (plist-get info :parse-tree) 'headline
  3801. (lambda (headline)
  3802. (when (or (equal (org-element-property :ID headline) id)
  3803. (equal (org-element-property :CUSTOM_ID headline) id))
  3804. headline))
  3805. info 'first-match)
  3806. ;; Otherwise, look for external files.
  3807. (cdr (assoc id (plist-get info :id-alist)))
  3808. (signal 'org-link-broken (list id)))))
  3809. (defun org-export-resolve-radio-link (link info)
  3810. "Return radio-target object referenced as LINK destination.
  3811. INFO is a plist used as a communication channel.
  3812. Return value can be a radio-target object or nil. Assume LINK
  3813. has type \"radio\"."
  3814. (let ((path (replace-regexp-in-string
  3815. "[ \r\t\n]+" " " (org-element-property :path link))))
  3816. (org-element-map (plist-get info :parse-tree) 'radio-target
  3817. (lambda (radio)
  3818. (and (eq (compare-strings
  3819. (replace-regexp-in-string
  3820. "[ \r\t\n]+" " " (org-element-property :value radio))
  3821. nil nil path nil nil t)
  3822. t)
  3823. radio))
  3824. info 'first-match)))
  3825. (defun org-export-file-uri (filename)
  3826. "Return file URI associated to FILENAME."
  3827. (cond ((org-string-match-p "\\`//" filename) (concat "file:" filename))
  3828. ((not (file-name-absolute-p filename)) filename)
  3829. ((org-file-remote-p filename) (concat "file:/" filename))
  3830. (t (concat "file://" (expand-file-name filename)))))
  3831. ;;;; For References
  3832. ;;
  3833. ;; `org-export-get-reference' associate a unique reference for any
  3834. ;; object or element.
  3835. ;;
  3836. ;; `org-export-get-ordinal' associates a sequence number to any object
  3837. ;; or element.
  3838. (defun org-export-get-reference (datum info)
  3839. "Return a unique reference for DATUM, as a string.
  3840. DATUM is either an element or an object. INFO is the current
  3841. export state, as a plist. Returned reference consists of
  3842. alphanumeric characters only."
  3843. (let ((type (org-element-type datum))
  3844. (cache (or (plist-get info :internal-references)
  3845. (let ((h (make-hash-table :test #'eq)))
  3846. (plist-put info :internal-references h)
  3847. h))))
  3848. (or (gethash datum cache)
  3849. (puthash datum
  3850. (format "org%s%d"
  3851. (if type
  3852. (replace-regexp-in-string "-" "" (symbol-name type))
  3853. "secondarystring")
  3854. (cl-incf (gethash type cache 0)))
  3855. cache))))
  3856. (defun org-export-get-ordinal (element info &optional types predicate)
  3857. "Return ordinal number of an element or object.
  3858. ELEMENT is the element or object considered. INFO is the plist
  3859. used as a communication channel.
  3860. Optional argument TYPES, when non-nil, is a list of element or
  3861. object types, as symbols, that should also be counted in.
  3862. Otherwise, only provided element's type is considered.
  3863. Optional argument PREDICATE is a function returning a non-nil
  3864. value if the current element or object should be counted in. It
  3865. accepts two arguments: the element or object being considered and
  3866. the plist used as a communication channel. This allows to count
  3867. only a certain type of objects (i.e. inline images).
  3868. Return value is a list of numbers if ELEMENT is a headline or an
  3869. item. It is nil for keywords. It represents the footnote number
  3870. for footnote definitions and footnote references. If ELEMENT is
  3871. a target, return the same value as if ELEMENT was the closest
  3872. table, item or headline containing the target. In any other
  3873. case, return the sequence number of ELEMENT among elements or
  3874. objects of the same type."
  3875. ;; Ordinal of a target object refer to the ordinal of the closest
  3876. ;; table, item, or headline containing the object.
  3877. (when (eq (org-element-type element) 'target)
  3878. (setq element
  3879. (org-element-lineage
  3880. element
  3881. '(footnote-definition footnote-reference headline item table))))
  3882. (cl-case (org-element-type element)
  3883. ;; Special case 1: A headline returns its number as a list.
  3884. (headline (org-export-get-headline-number element info))
  3885. ;; Special case 2: An item returns its number as a list.
  3886. (item (let ((struct (org-element-property :structure element)))
  3887. (org-list-get-item-number
  3888. (org-element-property :begin element)
  3889. struct
  3890. (org-list-prevs-alist struct)
  3891. (org-list-parents-alist struct))))
  3892. ((footnote-definition footnote-reference)
  3893. (org-export-get-footnote-number element info))
  3894. (otherwise
  3895. (let ((counter 0))
  3896. ;; Increment counter until ELEMENT is found again.
  3897. (org-element-map (plist-get info :parse-tree)
  3898. (or types (org-element-type element))
  3899. (lambda (el)
  3900. (cond
  3901. ((eq element el) (1+ counter))
  3902. ((not predicate) (cl-incf counter) nil)
  3903. ((funcall predicate el info) (cl-incf counter) nil)))
  3904. info 'first-match)))))
  3905. ;;;; For Src-Blocks
  3906. ;;
  3907. ;; `org-export-get-loc' counts number of code lines accumulated in
  3908. ;; src-block or example-block elements with a "+n" switch until
  3909. ;; a given element, excluded. Note: "-n" switches reset that count.
  3910. ;;
  3911. ;; `org-export-unravel-code' extracts source code (along with a code
  3912. ;; references alist) from an `element-block' or `src-block' type
  3913. ;; element.
  3914. ;;
  3915. ;; `org-export-format-code' applies a formatting function to each line
  3916. ;; of code, providing relative line number and code reference when
  3917. ;; appropriate. Since it doesn't access the original element from
  3918. ;; which the source code is coming, it expects from the code calling
  3919. ;; it to know if lines should be numbered and if code references
  3920. ;; should appear.
  3921. ;;
  3922. ;; Eventually, `org-export-format-code-default' is a higher-level
  3923. ;; function (it makes use of the two previous functions) which handles
  3924. ;; line numbering and code references inclusion, and returns source
  3925. ;; code in a format suitable for plain text or verbatim output.
  3926. (defun org-export-get-loc (element info)
  3927. "Return accumulated lines of code up to ELEMENT.
  3928. INFO is the plist used as a communication channel.
  3929. ELEMENT is excluded from count."
  3930. (let ((loc 0))
  3931. (org-element-map (plist-get info :parse-tree)
  3932. `(src-block example-block ,(org-element-type element))
  3933. (lambda (el)
  3934. (cond
  3935. ;; ELEMENT is reached: Quit the loop.
  3936. ((eq el element))
  3937. ;; Only count lines from src-block and example-block elements
  3938. ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
  3939. ((not (memq (org-element-type el) '(src-block example-block))) nil)
  3940. ((let ((linums (org-element-property :number-lines el)))
  3941. (when linums
  3942. ;; Accumulate locs or reset them.
  3943. (let ((lines (org-count-lines
  3944. (org-trim (org-element-property :value el)))))
  3945. (setq loc (if (eq linums 'new) lines (+ loc lines))))))
  3946. ;; Return nil to stay in the loop.
  3947. nil)))
  3948. info 'first-match)
  3949. ;; Return value.
  3950. loc))
  3951. (defun org-export-unravel-code (element)
  3952. "Clean source code and extract references out of it.
  3953. ELEMENT has either a `src-block' an `example-block' type.
  3954. Return a cons cell whose CAR is the source code, cleaned from any
  3955. reference, protective commas and spurious indentation, and CDR is
  3956. an alist between relative line number (integer) and name of code
  3957. reference on that line (string)."
  3958. (let* ((line 0) refs
  3959. (value (org-element-property :value element))
  3960. ;; Get code and clean it. Remove blank lines at its
  3961. ;; beginning and end.
  3962. (code (replace-regexp-in-string
  3963. "\\`\\([ \t]*\n\\)+" ""
  3964. (replace-regexp-in-string
  3965. "\\([ \t]*\n\\)*[ \t]*\\'" "\n"
  3966. (if (or org-src-preserve-indentation
  3967. (org-element-property :preserve-indent element))
  3968. value
  3969. (org-remove-indentation value)))))
  3970. ;; Get format used for references.
  3971. (label-fmt (regexp-quote
  3972. (or (org-element-property :label-fmt element)
  3973. org-coderef-label-format)))
  3974. ;; Build a regexp matching a loc with a reference.
  3975. (with-ref-re
  3976. (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
  3977. (replace-regexp-in-string
  3978. "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
  3979. ;; Return value.
  3980. (cons
  3981. ;; Code with references removed.
  3982. (org-element-normalize-string
  3983. (mapconcat
  3984. (lambda (loc)
  3985. (cl-incf line)
  3986. (if (not (string-match with-ref-re loc)) loc
  3987. ;; Ref line: remove ref, and signal its position in REFS.
  3988. (push (cons line (match-string 3 loc)) refs)
  3989. (replace-match "" nil nil loc 1)))
  3990. (org-split-string code "\n") "\n"))
  3991. ;; Reference alist.
  3992. refs)))
  3993. (defun org-export-format-code (code fun &optional num-lines ref-alist)
  3994. "Format CODE by applying FUN line-wise and return it.
  3995. CODE is a string representing the code to format. FUN is
  3996. a function. It must accept three arguments: a line of
  3997. code (string), the current line number (integer) or nil and the
  3998. reference associated to the current line (string) or nil.
  3999. Optional argument NUM-LINES can be an integer representing the
  4000. number of code lines accumulated until the current code. Line
  4001. numbers passed to FUN will take it into account. If it is nil,
  4002. FUN's second argument will always be nil. This number can be
  4003. obtained with `org-export-get-loc' function.
  4004. Optional argument REF-ALIST can be an alist between relative line
  4005. number (i.e. ignoring NUM-LINES) and the name of the code
  4006. reference on it. If it is nil, FUN's third argument will always
  4007. be nil. It can be obtained through the use of
  4008. `org-export-unravel-code' function."
  4009. (let ((--locs (org-split-string code "\n"))
  4010. (--line 0))
  4011. (org-element-normalize-string
  4012. (mapconcat
  4013. (lambda (--loc)
  4014. (cl-incf --line)
  4015. (let ((--ref (cdr (assq --line ref-alist))))
  4016. (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
  4017. --locs "\n"))))
  4018. (defun org-export-format-code-default (element info)
  4019. "Return source code from ELEMENT, formatted in a standard way.
  4020. ELEMENT is either a `src-block' or `example-block' element. INFO
  4021. is a plist used as a communication channel.
  4022. This function takes care of line numbering and code references
  4023. inclusion. Line numbers, when applicable, appear at the
  4024. beginning of the line, separated from the code by two white
  4025. spaces. Code references, on the other hand, appear flushed to
  4026. the right, separated by six white spaces from the widest line of
  4027. code."
  4028. ;; Extract code and references.
  4029. (let* ((code-info (org-export-unravel-code element))
  4030. (code (car code-info))
  4031. (code-lines (org-split-string code "\n")))
  4032. (if (null code-lines) ""
  4033. (let* ((refs (and (org-element-property :retain-labels element)
  4034. (cdr code-info)))
  4035. ;; Handle line numbering.
  4036. (num-start (cl-case (org-element-property :number-lines element)
  4037. (continued (org-export-get-loc element info))
  4038. (new 0)))
  4039. (num-fmt
  4040. (and num-start
  4041. (format "%%%ds "
  4042. (length (number-to-string
  4043. (+ (length code-lines) num-start))))))
  4044. ;; Prepare references display, if required. Any reference
  4045. ;; should start six columns after the widest line of code,
  4046. ;; wrapped with parenthesis.
  4047. (max-width
  4048. (+ (apply 'max (mapcar 'length code-lines))
  4049. (if (not num-start) 0 (length (format num-fmt num-start))))))
  4050. (org-export-format-code
  4051. code
  4052. (lambda (loc line-num ref)
  4053. (let ((number-str (and num-fmt (format num-fmt line-num))))
  4054. (concat
  4055. number-str
  4056. loc
  4057. (and ref
  4058. (concat (make-string
  4059. (- (+ 6 max-width)
  4060. (+ (length loc) (length number-str))) ? )
  4061. (format "(%s)" ref))))))
  4062. num-start refs)))))
  4063. ;;;; For Tables
  4064. ;;
  4065. ;; `org-export-table-has-special-column-p' and and
  4066. ;; `org-export-table-row-is-special-p' are predicates used to look for
  4067. ;; meta-information about the table structure.
  4068. ;;
  4069. ;; `org-table-has-header-p' tells when the rows before the first rule
  4070. ;; should be considered as table's header.
  4071. ;;
  4072. ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
  4073. ;; and `org-export-table-cell-borders' extract information from
  4074. ;; a table-cell element.
  4075. ;;
  4076. ;; `org-export-table-dimensions' gives the number on rows and columns
  4077. ;; in the table, ignoring horizontal rules and special columns.
  4078. ;; `org-export-table-cell-address', given a table-cell object, returns
  4079. ;; the absolute address of a cell. On the other hand,
  4080. ;; `org-export-get-table-cell-at' does the contrary.
  4081. ;;
  4082. ;; `org-export-table-cell-starts-colgroup-p',
  4083. ;; `org-export-table-cell-ends-colgroup-p',
  4084. ;; `org-export-table-row-starts-rowgroup-p',
  4085. ;; `org-export-table-row-ends-rowgroup-p',
  4086. ;; `org-export-table-row-starts-header-p',
  4087. ;; `org-export-table-row-ends-header-p' and
  4088. ;; `org-export-table-row-in-header-p' indicate position of current row
  4089. ;; or cell within the table.
  4090. (defun org-export-table-has-special-column-p (table)
  4091. "Non-nil when TABLE has a special column.
  4092. All special columns will be ignored during export."
  4093. ;; The table has a special column when every first cell of every row
  4094. ;; has an empty value or contains a symbol among "/", "#", "!", "$",
  4095. ;; "*" "_" and "^". Though, do not consider a first row containing
  4096. ;; only empty cells as special.
  4097. (let ((special-column-p 'empty))
  4098. (catch 'exit
  4099. (dolist (row (org-element-contents table))
  4100. (when (eq (org-element-property :type row) 'standard)
  4101. (let ((value (org-element-contents
  4102. (car (org-element-contents row)))))
  4103. (cond ((member value '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
  4104. (setq special-column-p 'special))
  4105. ((not value))
  4106. (t (throw 'exit nil))))))
  4107. (eq special-column-p 'special))))
  4108. (defun org-export-table-has-header-p (table info)
  4109. "Non-nil when TABLE has a header.
  4110. INFO is a plist used as a communication channel.
  4111. A table has a header when it contains at least two row groups."
  4112. (let ((cache (or (plist-get info :table-header-cache)
  4113. (plist-get (setq info
  4114. (plist-put info :table-header-cache
  4115. (make-hash-table :test 'eq)))
  4116. :table-header-cache))))
  4117. (or (gethash table cache)
  4118. (let ((rowgroup 1) row-flag)
  4119. (puthash
  4120. table
  4121. (org-element-map table 'table-row
  4122. (lambda (row)
  4123. (cond
  4124. ((> rowgroup 1) t)
  4125. ((and row-flag (eq (org-element-property :type row) 'rule))
  4126. (cl-incf rowgroup) (setq row-flag nil))
  4127. ((and (not row-flag) (eq (org-element-property :type row)
  4128. 'standard))
  4129. (setq row-flag t) nil)))
  4130. info 'first-match)
  4131. cache)))))
  4132. (defun org-export-table-row-is-special-p (table-row _)
  4133. "Non-nil if TABLE-ROW is considered special.
  4134. All special rows will be ignored during export."
  4135. (when (eq (org-element-property :type table-row) 'standard)
  4136. (let ((first-cell (org-element-contents
  4137. (car (org-element-contents table-row)))))
  4138. ;; A row is special either when...
  4139. (or
  4140. ;; ... it starts with a field only containing "/",
  4141. (equal first-cell '("/"))
  4142. ;; ... the table contains a special column and the row start
  4143. ;; with a marking character among, "^", "_", "$" or "!",
  4144. (and (org-export-table-has-special-column-p
  4145. (org-export-get-parent table-row))
  4146. (member first-cell '(("^") ("_") ("$") ("!"))))
  4147. ;; ... it contains only alignment cookies and empty cells.
  4148. (let ((special-row-p 'empty))
  4149. (catch 'exit
  4150. (dolist (cell (org-element-contents table-row))
  4151. (let ((value (org-element-contents cell)))
  4152. ;; Since VALUE is a secondary string, the following
  4153. ;; checks avoid expanding it with `org-export-data'.
  4154. (cond ((not value))
  4155. ((and (not (cdr value))
  4156. (stringp (car value))
  4157. (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
  4158. (car value)))
  4159. (setq special-row-p 'cookie))
  4160. (t (throw 'exit nil)))))
  4161. (eq special-row-p 'cookie)))))))
  4162. (defun org-export-table-row-group (table-row info)
  4163. "Return TABLE-ROW's group number, as an integer.
  4164. INFO is a plist used as the communication channel.
  4165. Return value is the group number, as an integer, or nil for
  4166. special rows and rows separators. First group is also table's
  4167. header."
  4168. (let ((cache (or (plist-get info :table-row-group-cache)
  4169. (plist-get (setq info
  4170. (plist-put info :table-row-group-cache
  4171. (make-hash-table :test 'eq)))
  4172. :table-row-group-cache))))
  4173. (cond ((gethash table-row cache))
  4174. ((eq (org-element-property :type table-row) 'rule) nil)
  4175. (t (let ((group 0) row-flag)
  4176. (org-element-map (org-export-get-parent table-row) 'table-row
  4177. (lambda (row)
  4178. (if (eq (org-element-property :type row) 'rule)
  4179. (setq row-flag nil)
  4180. (unless row-flag (cl-incf group) (setq row-flag t)))
  4181. (when (eq table-row row) (puthash table-row group cache)))
  4182. info 'first-match))))))
  4183. (defun org-export-table-cell-width (table-cell info)
  4184. "Return TABLE-CELL contents width.
  4185. INFO is a plist used as the communication channel.
  4186. Return value is the width given by the last width cookie in the
  4187. same column as TABLE-CELL, or nil."
  4188. (let* ((row (org-export-get-parent table-cell))
  4189. (table (org-export-get-parent row))
  4190. (cells (org-element-contents row))
  4191. (columns (length cells))
  4192. (column (- columns (length (memq table-cell cells))))
  4193. (cache (or (plist-get info :table-cell-width-cache)
  4194. (plist-get (setq info
  4195. (plist-put info :table-cell-width-cache
  4196. (make-hash-table :test 'eq)))
  4197. :table-cell-width-cache)))
  4198. (width-vector (or (gethash table cache)
  4199. (puthash table (make-vector columns 'empty) cache)))
  4200. (value (aref width-vector column)))
  4201. (if (not (eq value 'empty)) value
  4202. (let (cookie-width)
  4203. (dolist (row (org-element-contents table)
  4204. (aset width-vector column cookie-width))
  4205. (when (org-export-table-row-is-special-p row info)
  4206. ;; In a special row, try to find a width cookie at COLUMN.
  4207. (let* ((value (org-element-contents
  4208. (elt (org-element-contents row) column)))
  4209. (cookie (car value)))
  4210. ;; The following checks avoid expanding unnecessarily
  4211. ;; the cell with `org-export-data'.
  4212. (when (and value
  4213. (not (cdr value))
  4214. (stringp cookie)
  4215. (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" cookie)
  4216. (match-string 1 cookie))
  4217. (setq cookie-width
  4218. (string-to-number (match-string 1 cookie)))))))))))
  4219. (defun org-export-table-cell-alignment (table-cell info)
  4220. "Return TABLE-CELL contents alignment.
  4221. INFO is a plist used as the communication channel.
  4222. Return alignment as specified by the last alignment cookie in the
  4223. same column as TABLE-CELL. If no such cookie is found, a default
  4224. alignment value will be deduced from fraction of numbers in the
  4225. column (see `org-table-number-fraction' for more information).
  4226. Possible values are `left', `right' and `center'."
  4227. ;; Load `org-table-number-fraction' and `org-table-number-regexp'.
  4228. (require 'org-table)
  4229. (let* ((row (org-export-get-parent table-cell))
  4230. (table (org-export-get-parent row))
  4231. (cells (org-element-contents row))
  4232. (columns (length cells))
  4233. (column (- columns (length (memq table-cell cells))))
  4234. (cache (or (plist-get info :table-cell-alignment-cache)
  4235. (plist-get (setq info
  4236. (plist-put info :table-cell-alignment-cache
  4237. (make-hash-table :test 'eq)))
  4238. :table-cell-alignment-cache)))
  4239. (align-vector (or (gethash table cache)
  4240. (puthash table (make-vector columns nil) cache))))
  4241. (or (aref align-vector column)
  4242. (let ((number-cells 0)
  4243. (total-cells 0)
  4244. cookie-align
  4245. previous-cell-number-p)
  4246. (dolist (row (org-element-contents (org-export-get-parent row)))
  4247. (cond
  4248. ;; In a special row, try to find an alignment cookie at
  4249. ;; COLUMN.
  4250. ((org-export-table-row-is-special-p row info)
  4251. (let ((value (org-element-contents
  4252. (elt (org-element-contents row) column))))
  4253. ;; Since VALUE is a secondary string, the following
  4254. ;; checks avoid useless expansion through
  4255. ;; `org-export-data'.
  4256. (when (and value
  4257. (not (cdr value))
  4258. (stringp (car value))
  4259. (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
  4260. (car value))
  4261. (match-string 1 (car value)))
  4262. (setq cookie-align (match-string 1 (car value))))))
  4263. ;; Ignore table rules.
  4264. ((eq (org-element-property :type row) 'rule))
  4265. ;; In a standard row, check if cell's contents are
  4266. ;; expressing some kind of number. Increase NUMBER-CELLS
  4267. ;; accordingly. Though, don't bother if an alignment
  4268. ;; cookie has already defined cell's alignment.
  4269. ((not cookie-align)
  4270. (let ((value (org-export-data
  4271. (org-element-contents
  4272. (elt (org-element-contents row) column))
  4273. info)))
  4274. (cl-incf total-cells)
  4275. ;; Treat an empty cell as a number if it follows
  4276. ;; a number.
  4277. (if (not (or (string-match org-table-number-regexp value)
  4278. (and (string= value "") previous-cell-number-p)))
  4279. (setq previous-cell-number-p nil)
  4280. (setq previous-cell-number-p t)
  4281. (cl-incf number-cells))))))
  4282. ;; Return value. Alignment specified by cookies has
  4283. ;; precedence over alignment deduced from cell's contents.
  4284. (aset align-vector
  4285. column
  4286. (cond ((equal cookie-align "l") 'left)
  4287. ((equal cookie-align "r") 'right)
  4288. ((equal cookie-align "c") 'center)
  4289. ((>= (/ (float number-cells) total-cells)
  4290. org-table-number-fraction)
  4291. 'right)
  4292. (t 'left)))))))
  4293. (defun org-export-table-cell-borders (table-cell info)
  4294. "Return TABLE-CELL borders.
  4295. INFO is a plist used as a communication channel.
  4296. Return value is a list of symbols, or nil. Possible values are:
  4297. `top', `bottom', `above', `below', `left' and `right'. Note:
  4298. `top' (resp. `bottom') only happen for a cell in the first
  4299. row (resp. last row) of the table, ignoring table rules, if any.
  4300. Returned borders ignore special rows."
  4301. (let* ((row (org-export-get-parent table-cell))
  4302. (table (org-export-get-parent-table table-cell))
  4303. borders)
  4304. ;; Top/above border? TABLE-CELL has a border above when a rule
  4305. ;; used to demarcate row groups can be found above. Hence,
  4306. ;; finding a rule isn't sufficient to push `above' in BORDERS:
  4307. ;; another regular row has to be found above that rule.
  4308. (let (rule-flag)
  4309. (catch 'exit
  4310. ;; Look at every row before the current one.
  4311. (dolist (row (cdr (memq row (reverse (org-element-contents table)))))
  4312. (cond ((eq (org-element-property :type row) 'rule)
  4313. (setq rule-flag t))
  4314. ((not (org-export-table-row-is-special-p row info))
  4315. (if rule-flag (throw 'exit (push 'above borders))
  4316. (throw 'exit nil)))))
  4317. ;; No rule above, or rule found starts the table (ignoring any
  4318. ;; special row): TABLE-CELL is at the top of the table.
  4319. (when rule-flag (push 'above borders))
  4320. (push 'top borders)))
  4321. ;; Bottom/below border? TABLE-CELL has a border below when next
  4322. ;; non-regular row below is a rule.
  4323. (let (rule-flag)
  4324. (catch 'exit
  4325. ;; Look at every row after the current one.
  4326. (dolist (row (cdr (memq row (org-element-contents table))))
  4327. (cond ((eq (org-element-property :type row) 'rule)
  4328. (setq rule-flag t))
  4329. ((not (org-export-table-row-is-special-p row info))
  4330. (if rule-flag (throw 'exit (push 'below borders))
  4331. (throw 'exit nil)))))
  4332. ;; No rule below, or rule found ends the table (modulo some
  4333. ;; special row): TABLE-CELL is at the bottom of the table.
  4334. (when rule-flag (push 'below borders))
  4335. (push 'bottom borders)))
  4336. ;; Right/left borders? They can only be specified by column
  4337. ;; groups. Column groups are defined in a row starting with "/".
  4338. ;; Also a column groups row only contains "<", "<>", ">" or blank
  4339. ;; cells.
  4340. (catch 'exit
  4341. (let ((column (let ((cells (org-element-contents row)))
  4342. (- (length cells) (length (memq table-cell cells))))))
  4343. ;; Table rows are read in reverse order so last column groups
  4344. ;; row has precedence over any previous one.
  4345. (dolist (row (reverse (org-element-contents table)))
  4346. (unless (eq (org-element-property :type row) 'rule)
  4347. (when (equal (org-element-contents
  4348. (car (org-element-contents row)))
  4349. '("/"))
  4350. (let ((column-groups
  4351. (mapcar
  4352. (lambda (cell)
  4353. (let ((value (org-element-contents cell)))
  4354. (when (member value '(("<") ("<>") (">") nil))
  4355. (car value))))
  4356. (org-element-contents row))))
  4357. ;; There's a left border when previous cell, if
  4358. ;; any, ends a group, or current one starts one.
  4359. (when (or (and (not (zerop column))
  4360. (member (elt column-groups (1- column))
  4361. '(">" "<>")))
  4362. (member (elt column-groups column) '("<" "<>")))
  4363. (push 'left borders))
  4364. ;; There's a right border when next cell, if any,
  4365. ;; starts a group, or current one ends one.
  4366. (when (or (and (/= (1+ column) (length column-groups))
  4367. (member (elt column-groups (1+ column))
  4368. '("<" "<>")))
  4369. (member (elt column-groups column) '(">" "<>")))
  4370. (push 'right borders))
  4371. (throw 'exit nil)))))))
  4372. ;; Return value.
  4373. borders))
  4374. (defun org-export-table-cell-starts-colgroup-p (table-cell info)
  4375. "Non-nil when TABLE-CELL is at the beginning of a column group.
  4376. INFO is a plist used as a communication channel."
  4377. ;; A cell starts a column group either when it is at the beginning
  4378. ;; of a row (or after the special column, if any) or when it has
  4379. ;; a left border.
  4380. (or (eq (org-element-map (org-export-get-parent table-cell) 'table-cell
  4381. 'identity info 'first-match)
  4382. table-cell)
  4383. (memq 'left (org-export-table-cell-borders table-cell info))))
  4384. (defun org-export-table-cell-ends-colgroup-p (table-cell info)
  4385. "Non-nil when TABLE-CELL is at the end of a column group.
  4386. INFO is a plist used as a communication channel."
  4387. ;; A cell ends a column group either when it is at the end of a row
  4388. ;; or when it has a right border.
  4389. (or (eq (car (last (org-element-contents
  4390. (org-export-get-parent table-cell))))
  4391. table-cell)
  4392. (memq 'right (org-export-table-cell-borders table-cell info))))
  4393. (defun org-export-table-row-starts-rowgroup-p (table-row info)
  4394. "Non-nil when TABLE-ROW is at the beginning of a row group.
  4395. INFO is a plist used as a communication channel."
  4396. (unless (or (eq (org-element-property :type table-row) 'rule)
  4397. (org-export-table-row-is-special-p table-row info))
  4398. (let ((borders (org-export-table-cell-borders
  4399. (car (org-element-contents table-row)) info)))
  4400. (or (memq 'top borders) (memq 'above borders)))))
  4401. (defun org-export-table-row-ends-rowgroup-p (table-row info)
  4402. "Non-nil when TABLE-ROW is at the end of a row group.
  4403. INFO is a plist used as a communication channel."
  4404. (unless (or (eq (org-element-property :type table-row) 'rule)
  4405. (org-export-table-row-is-special-p table-row info))
  4406. (let ((borders (org-export-table-cell-borders
  4407. (car (org-element-contents table-row)) info)))
  4408. (or (memq 'bottom borders) (memq 'below borders)))))
  4409. (defun org-export-table-row-in-header-p (table-row info)
  4410. "Non-nil when TABLE-ROW is located within table's header.
  4411. INFO is a plist used as a communication channel. Always return
  4412. nil for special rows and rows separators."
  4413. (and (org-export-table-has-header-p
  4414. (org-export-get-parent-table table-row) info)
  4415. (eql (org-export-table-row-group table-row info) 1)))
  4416. (defun org-export-table-row-starts-header-p (table-row info)
  4417. "Non-nil when TABLE-ROW is the first table header's row.
  4418. INFO is a plist used as a communication channel."
  4419. (and (org-export-table-row-in-header-p table-row info)
  4420. (org-export-table-row-starts-rowgroup-p table-row info)))
  4421. (defun org-export-table-row-ends-header-p (table-row info)
  4422. "Non-nil when TABLE-ROW is the last table header's row.
  4423. INFO is a plist used as a communication channel."
  4424. (and (org-export-table-row-in-header-p table-row info)
  4425. (org-export-table-row-ends-rowgroup-p table-row info)))
  4426. (defun org-export-table-row-number (table-row info)
  4427. "Return TABLE-ROW number.
  4428. INFO is a plist used as a communication channel. Return value is
  4429. zero-based and ignores separators. The function returns nil for
  4430. special columns and separators."
  4431. (when (and (eq (org-element-property :type table-row) 'standard)
  4432. (not (org-export-table-row-is-special-p table-row info)))
  4433. (let ((number 0))
  4434. (org-element-map (org-export-get-parent-table table-row) 'table-row
  4435. (lambda (row)
  4436. (cond ((eq row table-row) number)
  4437. ((eq (org-element-property :type row) 'standard)
  4438. (cl-incf number) nil)))
  4439. info 'first-match))))
  4440. (defun org-export-table-dimensions (table info)
  4441. "Return TABLE dimensions.
  4442. INFO is a plist used as a communication channel.
  4443. Return value is a CONS like (ROWS . COLUMNS) where
  4444. ROWS (resp. COLUMNS) is the number of exportable
  4445. rows (resp. columns)."
  4446. (let (first-row (columns 0) (rows 0))
  4447. ;; Set number of rows, and extract first one.
  4448. (org-element-map table 'table-row
  4449. (lambda (row)
  4450. (when (eq (org-element-property :type row) 'standard)
  4451. (cl-incf rows)
  4452. (unless first-row (setq first-row row)))) info)
  4453. ;; Set number of columns.
  4454. (org-element-map first-row 'table-cell (lambda (_) (cl-incf columns)) info)
  4455. ;; Return value.
  4456. (cons rows columns)))
  4457. (defun org-export-table-cell-address (table-cell info)
  4458. "Return address of a regular TABLE-CELL object.
  4459. TABLE-CELL is the cell considered. INFO is a plist used as
  4460. a communication channel.
  4461. Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
  4462. zero-based index. Only exportable cells are considered. The
  4463. function returns nil for other cells."
  4464. (let* ((table-row (org-export-get-parent table-cell))
  4465. (row-number (org-export-table-row-number table-row info)))
  4466. (when row-number
  4467. (cons row-number
  4468. (let ((col-count 0))
  4469. (org-element-map table-row 'table-cell
  4470. (lambda (cell)
  4471. (if (eq cell table-cell) col-count (cl-incf col-count) nil))
  4472. info 'first-match))))))
  4473. (defun org-export-get-table-cell-at (address table info)
  4474. "Return regular table-cell object at ADDRESS in TABLE.
  4475. Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
  4476. zero-based index. TABLE is a table type element. INFO is
  4477. a plist used as a communication channel.
  4478. If no table-cell, among exportable cells, is found at ADDRESS,
  4479. return nil."
  4480. (let ((column-pos (cdr address)) (column-count 0))
  4481. (org-element-map
  4482. ;; Row at (car address) or nil.
  4483. (let ((row-pos (car address)) (row-count 0))
  4484. (org-element-map table 'table-row
  4485. (lambda (row)
  4486. (cond ((eq (org-element-property :type row) 'rule) nil)
  4487. ((= row-count row-pos) row)
  4488. (t (cl-incf row-count) nil)))
  4489. info 'first-match))
  4490. 'table-cell
  4491. (lambda (cell)
  4492. (if (= column-count column-pos) cell
  4493. (cl-incf column-count) nil))
  4494. info 'first-match)))
  4495. ;;;; For Tables Of Contents
  4496. ;;
  4497. ;; `org-export-collect-headlines' builds a list of all exportable
  4498. ;; headline elements, maybe limited to a certain depth. One can then
  4499. ;; easily parse it and transcode it.
  4500. ;;
  4501. ;; Building lists of tables, figures or listings is quite similar.
  4502. ;; Once the generic function `org-export-collect-elements' is defined,
  4503. ;; `org-export-collect-tables', `org-export-collect-figures' and
  4504. ;; `org-export-collect-listings' can be derived from it.
  4505. (defun org-export-collect-headlines (info &optional n scope)
  4506. "Collect headlines in order to build a table of contents.
  4507. INFO is a plist used as a communication channel.
  4508. When optional argument N is an integer, it specifies the depth of
  4509. the table of contents. Otherwise, it is set to the value of the
  4510. last headline level. See `org-export-headline-levels' for more
  4511. information.
  4512. Optional argument SCOPE, when non-nil, is an element. If it is
  4513. a headline, only children of SCOPE are collected. Otherwise,
  4514. collect children of the headline containing provided element. If
  4515. there is no such headline, collect all headlines. In any case,
  4516. argument N becomes relative to the level of that headline.
  4517. Return a list of all exportable headlines as parsed elements.
  4518. Footnote sections are ignored."
  4519. (let* ((scope (cond ((not scope) (plist-get info :parse-tree))
  4520. ((eq (org-element-type scope) 'headline) scope)
  4521. ((org-export-get-parent-headline scope))
  4522. (t (plist-get info :parse-tree))))
  4523. (limit (plist-get info :headline-levels))
  4524. (n (if (not (wholenump n)) limit
  4525. (min (if (eq (org-element-type scope) 'org-data) n
  4526. (+ (org-export-get-relative-level scope info) n))
  4527. limit))))
  4528. (org-element-map (org-element-contents scope) 'headline
  4529. (lambda (headline)
  4530. (unless (org-element-property :footnote-section-p headline)
  4531. (let ((level (org-export-get-relative-level headline info)))
  4532. (and (<= level n) headline))))
  4533. info)))
  4534. (defun org-export-collect-elements (type info &optional predicate)
  4535. "Collect referenceable elements of a determined type.
  4536. TYPE can be a symbol or a list of symbols specifying element
  4537. types to search. Only elements with a caption are collected.
  4538. INFO is a plist used as a communication channel.
  4539. When non-nil, optional argument PREDICATE is a function accepting
  4540. one argument, an element of type TYPE. It returns a non-nil
  4541. value when that element should be collected.
  4542. Return a list of all elements found, in order of appearance."
  4543. (org-element-map (plist-get info :parse-tree) type
  4544. (lambda (element)
  4545. (and (org-element-property :caption element)
  4546. (or (not predicate) (funcall predicate element))
  4547. element))
  4548. info))
  4549. (defun org-export-collect-tables (info)
  4550. "Build a list of tables.
  4551. INFO is a plist used as a communication channel.
  4552. Return a list of table elements with a caption."
  4553. (org-export-collect-elements 'table info))
  4554. (defun org-export-collect-figures (info predicate)
  4555. "Build a list of figures.
  4556. INFO is a plist used as a communication channel. PREDICATE is
  4557. a function which accepts one argument: a paragraph element and
  4558. whose return value is non-nil when that element should be
  4559. collected.
  4560. A figure is a paragraph type element, with a caption, verifying
  4561. PREDICATE. The latter has to be provided since a \"figure\" is
  4562. a vague concept that may depend on back-end.
  4563. Return a list of elements recognized as figures."
  4564. (org-export-collect-elements 'paragraph info predicate))
  4565. (defun org-export-collect-listings (info)
  4566. "Build a list of src blocks.
  4567. INFO is a plist used as a communication channel.
  4568. Return a list of src-block elements with a caption."
  4569. (org-export-collect-elements 'src-block info))
  4570. ;;;; Smart Quotes
  4571. ;;
  4572. ;; The main function for the smart quotes sub-system is
  4573. ;; `org-export-activate-smart-quotes', which replaces every quote in
  4574. ;; a given string from the parse tree with its "smart" counterpart.
  4575. ;;
  4576. ;; Dictionary for smart quotes is stored in
  4577. ;; `org-export-smart-quotes-alist'.
  4578. (defconst org-export-smart-quotes-alist
  4579. '(("da"
  4580. ;; one may use: »...«, "...", ›...‹, or '...'.
  4581. ;; http://sproget.dk/raad-og-regler/retskrivningsregler/retskrivningsregler/a7-40-60/a7-58-anforselstegn/
  4582. ;; LaTeX quotes require Babel!
  4583. (primary-opening
  4584. :utf-8 "»" :html "&raquo;" :latex ">>" :texinfo "@guillemetright{}")
  4585. (primary-closing
  4586. :utf-8 "«" :html "&laquo;" :latex "<<" :texinfo "@guillemetleft{}")
  4587. (secondary-opening
  4588. :utf-8 "›" :html "&rsaquo;" :latex "\\frq{}" :texinfo "@guilsinglright{}")
  4589. (secondary-closing
  4590. :utf-8 "‹" :html "&lsaquo;" :latex "\\flq{}" :texinfo "@guilsingleft{}")
  4591. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4592. ("de"
  4593. (primary-opening
  4594. :utf-8 "„" :html "&bdquo;" :latex "\"`" :texinfo "@quotedblbase{}")
  4595. (primary-closing
  4596. :utf-8 "“" :html "&ldquo;" :latex "\"'" :texinfo "@quotedblleft{}")
  4597. (secondary-opening
  4598. :utf-8 "‚" :html "&sbquo;" :latex "\\glq{}" :texinfo "@quotesinglbase{}")
  4599. (secondary-closing
  4600. :utf-8 "‘" :html "&lsquo;" :latex "\\grq{}" :texinfo "@quoteleft{}")
  4601. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4602. ("en"
  4603. (primary-opening :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
  4604. (primary-closing :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
  4605. (secondary-opening :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
  4606. (secondary-closing :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
  4607. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4608. ("es"
  4609. (primary-opening
  4610. :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
  4611. :texinfo "@guillemetleft{}")
  4612. (primary-closing
  4613. :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
  4614. :texinfo "@guillemetright{}")
  4615. (secondary-opening :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
  4616. (secondary-closing :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
  4617. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4618. ("fr"
  4619. (primary-opening
  4620. :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
  4621. :texinfo "@guillemetleft{}@tie{}")
  4622. (primary-closing
  4623. :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
  4624. :texinfo "@tie{}@guillemetright{}")
  4625. (secondary-opening
  4626. :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
  4627. :texinfo "@guillemetleft{}@tie{}")
  4628. (secondary-closing :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
  4629. :texinfo "@tie{}@guillemetright{}")
  4630. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4631. ("no"
  4632. ;; https://nn.wikipedia.org/wiki/Sitatteikn
  4633. (primary-opening
  4634. :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
  4635. :texinfo "@guillemetleft{}")
  4636. (primary-closing
  4637. :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
  4638. :texinfo "@guillemetright{}")
  4639. (secondary-opening :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
  4640. (secondary-closing :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
  4641. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4642. ("nb"
  4643. ;; https://nn.wikipedia.org/wiki/Sitatteikn
  4644. (primary-opening
  4645. :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
  4646. :texinfo "@guillemetleft{}")
  4647. (primary-closing
  4648. :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
  4649. :texinfo "@guillemetright{}")
  4650. (secondary-opening :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
  4651. (secondary-closing :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
  4652. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4653. ("nn"
  4654. ;; https://nn.wikipedia.org/wiki/Sitatteikn
  4655. (primary-opening
  4656. :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
  4657. :texinfo "@guillemetleft{}")
  4658. (primary-closing
  4659. :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
  4660. :texinfo "@guillemetright{}")
  4661. (secondary-opening :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
  4662. (secondary-closing :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
  4663. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4664. ("ru"
  4665. ;; 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
  4666. ;; http://www.artlebedev.ru/kovodstvo/sections/104/
  4667. (primary-opening :utf-8 "«" :html "&laquo;" :latex "{}<<"
  4668. :texinfo "@guillemetleft{}")
  4669. (primary-closing :utf-8 "»" :html "&raquo;" :latex ">>{}"
  4670. :texinfo "@guillemetright{}")
  4671. (secondary-opening
  4672. :utf-8 "„" :html "&bdquo;" :latex "\\glqq{}" :texinfo "@quotedblbase{}")
  4673. (secondary-closing
  4674. :utf-8 "“" :html "&ldquo;" :latex "\\grqq{}" :texinfo "@quotedblleft{}")
  4675. (apostrophe :utf-8 "’" :html: "&#39;"))
  4676. ("sv"
  4677. ;; based on https://sv.wikipedia.org/wiki/Citattecken
  4678. (primary-opening :utf-8 "”" :html "&rdquo;" :latex "’’" :texinfo "’’")
  4679. (primary-closing :utf-8 "”" :html "&rdquo;" :latex "’’" :texinfo "’’")
  4680. (secondary-opening :utf-8 "’" :html "&rsquo;" :latex "’" :texinfo "`")
  4681. (secondary-closing :utf-8 "’" :html "&rsquo;" :latex "’" :texinfo "'")
  4682. (apostrophe :utf-8 "’" :html "&rsquo;")))
  4683. "Smart quotes translations.
  4684. Alist whose CAR is a language string and CDR is an alist with
  4685. quote type as key and a plist associating various encodings to
  4686. their translation as value.
  4687. A quote type can be any symbol among `primary-opening',
  4688. `primary-closing', `secondary-opening', `secondary-closing' and
  4689. `apostrophe'.
  4690. Valid encodings include `:utf-8', `:html', `:latex' and
  4691. `:texinfo'.
  4692. If no translation is found, the quote character is left as-is.")
  4693. (defun org-export--smart-quote-status (s info)
  4694. "Return smart quote status at the beginning of string S.
  4695. INFO is the current export state, as a plist."
  4696. (let* ((parent (org-element-property :parent s))
  4697. (cache (or (plist-get info :smart-quote-cache)
  4698. (let ((table (make-hash-table :test #'eq)))
  4699. (plist-put info :smart-quote-cache table)
  4700. table)))
  4701. (value (gethash parent cache 'missing-data)))
  4702. (if (not (eq value 'missing-data)) (cdr (assq s value))
  4703. (let (level1-open full-status)
  4704. (org-element-map parent 'plain-text
  4705. (lambda (text)
  4706. (let ((start 0) current-status)
  4707. (while (setq start (string-match "['\"]" text start))
  4708. (push
  4709. (cond
  4710. ((equal (match-string 0 text) "\"")
  4711. (setf level1-open (not level1-open))
  4712. (if level1-open 'primary-opening 'primary-closing))
  4713. ;; Not already in a level 1 quote: this is an
  4714. ;; apostrophe.
  4715. ((not level1-open) 'apostrophe)
  4716. ;; Extract previous char and next char. As
  4717. ;; a special case, they can also be set to `blank',
  4718. ;; `no-blank' or nil. Then determine if current
  4719. ;; match is allowed as an opening quote or a closing
  4720. ;; quote.
  4721. (t
  4722. (let* ((previous
  4723. (if (> start 0) (substring text (1- start) start)
  4724. (let ((p (org-export-get-previous-element
  4725. text info)))
  4726. (cond ((not p) nil)
  4727. ((stringp p) (substring p -1))
  4728. ((memq (org-element-property :post-blank p)
  4729. '(0 nil))
  4730. 'no-blank)
  4731. (t 'blank)))))
  4732. (next
  4733. (if (< (1+ start) (length text))
  4734. (substring text (1+ start) (+ start 2))
  4735. (let ((n (org-export-get-next-element text info)))
  4736. (cond ((not n) nil)
  4737. ((stringp n) (substring n 0 1))
  4738. (t 'no-blank)))))
  4739. (allow-open
  4740. (and (if (stringp previous)
  4741. (string-match "\\s\"\\|\\s-\\|\\s("
  4742. previous)
  4743. (memq previous '(blank nil)))
  4744. (if (stringp next)
  4745. (string-match "\\w\\|\\s.\\|\\s_" next)
  4746. (eq next 'no-blank))))
  4747. (allow-close
  4748. (and (if (stringp previous)
  4749. (string-match "\\w\\|\\s.\\|\\s_" previous)
  4750. (eq previous 'no-blank))
  4751. (if (stringp next)
  4752. (string-match "\\s-\\|\\s)\\|\\s.\\|\\s\""
  4753. next)
  4754. (memq next '(blank nil))))))
  4755. (cond
  4756. ((and allow-open allow-close) (error "Should not happen"))
  4757. (allow-open 'secondary-opening)
  4758. (allow-close 'secondary-closing)
  4759. (t 'apostrophe)))))
  4760. current-status)
  4761. (cl-incf start))
  4762. (when current-status
  4763. (push (cons text (nreverse current-status)) full-status))))
  4764. info nil org-element-recursive-objects)
  4765. (puthash parent full-status cache)
  4766. (cdr (assq s full-status))))))
  4767. (defun org-export-activate-smart-quotes (s encoding info &optional original)
  4768. "Replace regular quotes with \"smart\" quotes in string S.
  4769. ENCODING is a symbol among `:html', `:latex', `:texinfo' and
  4770. `:utf-8'. INFO is a plist used as a communication channel.
  4771. The function has to retrieve information about string
  4772. surroundings in parse tree. It can only happen with an
  4773. unmodified string. Thus, if S has already been through another
  4774. process, a non-nil ORIGINAL optional argument will provide that
  4775. original string.
  4776. Return the new string."
  4777. (let ((quote-status
  4778. (copy-sequence (org-export--smart-quote-status (or original s) info))))
  4779. (replace-regexp-in-string
  4780. "['\"]"
  4781. (lambda (match)
  4782. (or (plist-get
  4783. (cdr (assq (pop quote-status)
  4784. (cdr (assoc (plist-get info :language)
  4785. org-export-smart-quotes-alist))))
  4786. encoding)
  4787. match))
  4788. s nil t)))
  4789. ;;;; Topology
  4790. ;;
  4791. ;; Here are various functions to retrieve information about the
  4792. ;; neighborhood of a given element or object. Neighbors of interest
  4793. ;; are direct parent (`org-export-get-parent'), parent headline
  4794. ;; (`org-export-get-parent-headline'), first element containing an
  4795. ;; object, (`org-export-get-parent-element'), parent table
  4796. ;; (`org-export-get-parent-table'), previous element or object
  4797. ;; (`org-export-get-previous-element') and next element or object
  4798. ;; (`org-export-get-next-element').
  4799. ;; defsubst org-export-get-parent must be defined before first use
  4800. (define-obsolete-function-alias
  4801. 'org-export-get-genealogy 'org-element-lineage "25.1")
  4802. (defun org-export-get-parent-headline (blob)
  4803. "Return BLOB parent headline or nil.
  4804. BLOB is the element or object being considered."
  4805. (org-element-lineage blob '(headline)))
  4806. (defun org-export-get-parent-element (object)
  4807. "Return first element containing OBJECT or nil.
  4808. OBJECT is the object to consider."
  4809. (org-element-lineage object org-element-all-elements))
  4810. (defun org-export-get-parent-table (object)
  4811. "Return OBJECT parent table or nil.
  4812. OBJECT is either a `table-cell' or `table-element' type object."
  4813. (org-element-lineage object '(table)))
  4814. (defun org-export-get-previous-element (blob info &optional n)
  4815. "Return previous element or object.
  4816. BLOB is an element or object. INFO is a plist used as
  4817. a communication channel. Return previous exportable element or
  4818. object, a string, or nil.
  4819. When optional argument N is a positive integer, return a list
  4820. containing up to N siblings before BLOB, from farthest to
  4821. closest. With any other non-nil value, return a list containing
  4822. all of them."
  4823. (let* ((secondary (org-element-secondary-p blob))
  4824. (parent (org-export-get-parent blob))
  4825. (siblings
  4826. (if secondary (org-element-property secondary parent)
  4827. (org-element-contents parent)))
  4828. prev)
  4829. (catch 'exit
  4830. (dolist (obj (cdr (memq blob (reverse siblings))) prev)
  4831. (cond ((memq obj (plist-get info :ignore-list)))
  4832. ((null n) (throw 'exit obj))
  4833. ((not (wholenump n)) (push obj prev))
  4834. ((zerop n) (throw 'exit prev))
  4835. (t (cl-decf n) (push obj prev)))))))
  4836. (defun org-export-get-next-element (blob info &optional n)
  4837. "Return next element or object.
  4838. BLOB is an element or object. INFO is a plist used as
  4839. a communication channel. Return next exportable element or
  4840. object, a string, or nil.
  4841. When optional argument N is a positive integer, return a list
  4842. containing up to N siblings after BLOB, from closest to farthest.
  4843. With any other non-nil value, return a list containing all of
  4844. them."
  4845. (let* ((secondary (org-element-secondary-p blob))
  4846. (parent (org-export-get-parent blob))
  4847. (siblings
  4848. (cdr (memq blob
  4849. (if secondary (org-element-property secondary parent)
  4850. (org-element-contents parent)))))
  4851. next)
  4852. (catch 'exit
  4853. (dolist (obj siblings (nreverse next))
  4854. (cond ((memq obj (plist-get info :ignore-list)))
  4855. ((null n) (throw 'exit obj))
  4856. ((not (wholenump n)) (push obj next))
  4857. ((zerop n) (throw 'exit (nreverse next)))
  4858. (t (cl-decf n) (push obj next)))))))
  4859. ;;;; Translation
  4860. ;;
  4861. ;; `org-export-translate' translates a string according to the language
  4862. ;; specified by the LANGUAGE keyword. `org-export-dictionary' contains
  4863. ;; the dictionary used for the translation.
  4864. (defconst org-export-dictionary
  4865. '(("%e %n: %c"
  4866. ("fr" :default "%e %n : %c" :html "%e&nbsp;%n&nbsp;: %c"))
  4867. ("Author"
  4868. ("ca" :default "Autor")
  4869. ("cs" :default "Autor")
  4870. ("da" :default "Forfatter")
  4871. ("de" :default "Autor")
  4872. ("eo" :html "A&#365;toro")
  4873. ("es" :default "Autor")
  4874. ("et" :default "Autor")
  4875. ("fi" :html "Tekij&auml;")
  4876. ("fr" :default "Auteur")
  4877. ("hu" :default "Szerz&otilde;")
  4878. ("is" :html "H&ouml;fundur")
  4879. ("it" :default "Autore")
  4880. ("ja" :default "著者" :html "&#33879;&#32773;")
  4881. ("nl" :default "Auteur")
  4882. ("no" :default "Forfatter")
  4883. ("nb" :default "Forfatter")
  4884. ("nn" :default "Forfattar")
  4885. ("pl" :default "Autor")
  4886. ("pt_BR" :default "Autor")
  4887. ("ru" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
  4888. ("sv" :html "F&ouml;rfattare")
  4889. ("uk" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
  4890. ("zh-CN" :html "&#20316;&#32773;" :utf-8 "作者")
  4891. ("zh-TW" :html "&#20316;&#32773;" :utf-8 "作者"))
  4892. ("Continued from previous page"
  4893. ("de" :default "Fortsetzung von vorheriger Seite")
  4894. ("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")
  4895. ("fr" :default "Suite de la page précédente")
  4896. ("it" :default "Continua da pagina precedente")
  4897. ("ja" :default "前ページからの続き")
  4898. ("nl" :default "Vervolg van vorige pagina")
  4899. ("pt" :default "Continuação da página anterior")
  4900. ("ru" :html "(&#1055;&#1088;&#1086;&#1076;&#1086;&#1083;&#1078;&#1077;&#1085;&#1080;&#1077;)"
  4901. :utf-8 "(Продолжение)"))
  4902. ("Continued on next page"
  4903. ("de" :default "Fortsetzung nächste Seite")
  4904. ("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")
  4905. ("fr" :default "Suite page suivante")
  4906. ("it" :default "Continua alla pagina successiva")
  4907. ("ja" :default "次ページに続く")
  4908. ("nl" :default "Vervolg op volgende pagina")
  4909. ("pt" :default "Continua na página seguinte")
  4910. ("ru" :html "(&#1055;&#1088;&#1086;&#1076;&#1086;&#1083;&#1078;&#1077;&#1085;&#1080;&#1077; &#1089;&#1083;&#1077;&#1076;&#1091;&#1077;&#1090;)"
  4911. :utf-8 "(Продолжение следует)"))
  4912. ("Date"
  4913. ("ca" :default "Data")
  4914. ("cs" :default "Datum")
  4915. ("da" :default "Dato")
  4916. ("de" :default "Datum")
  4917. ("eo" :default "Dato")
  4918. ("es" :default "Fecha")
  4919. ("et" :html "Kuup&#228;ev" :utf-8 "Kuupäev")
  4920. ("fi" :html "P&auml;iv&auml;m&auml;&auml;r&auml;")
  4921. ("hu" :html "D&aacute;tum")
  4922. ("is" :default "Dagsetning")
  4923. ("it" :default "Data")
  4924. ("ja" :default "日付" :html "&#26085;&#20184;")
  4925. ("nl" :default "Datum")
  4926. ("no" :default "Dato")
  4927. ("nb" :default "Dato")
  4928. ("nn" :default "Dato")
  4929. ("pl" :default "Data")
  4930. ("pt_BR" :default "Data")
  4931. ("ru" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
  4932. ("sv" :default "Datum")
  4933. ("uk" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
  4934. ("zh-CN" :html "&#26085;&#26399;" :utf-8 "日期")
  4935. ("zh-TW" :html "&#26085;&#26399;" :utf-8 "日期"))
  4936. ("Equation"
  4937. ("da" :default "Ligning")
  4938. ("de" :default "Gleichung")
  4939. ("es" :ascii "Ecuacion" :html "Ecuaci&oacute;n" :default "Ecuación")
  4940. ("et" :html "V&#245;rrand" :utf-8 "Võrrand")
  4941. ("fr" :ascii "Equation" :default "Équation")
  4942. ("ja" :default "方程式")
  4943. ("no" :default "Ligning")
  4944. ("nb" :default "Ligning")
  4945. ("nn" :default "Likning")
  4946. ("pt_BR" :html "Equa&ccedil;&atilde;o" :default "Equação" :ascii "Equacao")
  4947. ("ru" :html "&#1059;&#1088;&#1072;&#1074;&#1085;&#1077;&#1085;&#1080;&#1077;"
  4948. :utf-8 "Уравнение")
  4949. ("sv" :default "Ekvation")
  4950. ("zh-CN" :html "&#26041;&#31243;" :utf-8 "方程"))
  4951. ("Figure"
  4952. ("da" :default "Figur")
  4953. ("de" :default "Abbildung")
  4954. ("es" :default "Figura")
  4955. ("et" :default "Joonis")
  4956. ("ja" :default "図" :html "&#22259;")
  4957. ("no" :default "Illustrasjon")
  4958. ("nb" :default "Illustrasjon")
  4959. ("nn" :default "Illustrasjon")
  4960. ("pt_BR" :default "Figura")
  4961. ("ru" :html "&#1056;&#1080;&#1089;&#1091;&#1085;&#1086;&#1082;" :utf-8 "Рисунок")
  4962. ("sv" :default "Illustration")
  4963. ("zh-CN" :html "&#22270;" :utf-8 "图"))
  4964. ("Figure %d:"
  4965. ("da" :default "Figur %d")
  4966. ("de" :default "Abbildung %d:")
  4967. ("es" :default "Figura %d:")
  4968. ("et" :default "Joonis %d:")
  4969. ("fr" :default "Figure %d :" :html "Figure&nbsp;%d&nbsp;:")
  4970. ("ja" :default "図%d: " :html "&#22259;%d: ")
  4971. ("no" :default "Illustrasjon %d")
  4972. ("nb" :default "Illustrasjon %d")
  4973. ("nn" :default "Illustrasjon %d")
  4974. ("pt_BR" :default "Figura %d:")
  4975. ("ru" :html "&#1056;&#1080;&#1089;. %d.:" :utf-8 "Рис. %d.:")
  4976. ("sv" :default "Illustration %d")
  4977. ("zh-CN" :html "&#22270;%d&nbsp;" :utf-8 "图%d "))
  4978. ("Footnotes"
  4979. ("ca" :html "Peus de p&agrave;gina")
  4980. ("cs" :default "Pozn\xe1mky pod carou")
  4981. ("da" :default "Fodnoter")
  4982. ("de" :html "Fu&szlig;noten" :default "Fußnoten")
  4983. ("eo" :default "Piednotoj")
  4984. ("es" :ascii "Nota al pie de pagina" :html "Nota al pie de p&aacute;gina" :default "Nota al pie de página")
  4985. ("et" :html "Allm&#228;rkused" :utf-8 "Allmärkused")
  4986. ("fi" :default "Alaviitteet")
  4987. ("fr" :default "Notes de bas de page")
  4988. ("hu" :html "L&aacute;bjegyzet")
  4989. ("is" :html "Aftanm&aacute;lsgreinar")
  4990. ("it" :html "Note a pi&egrave; di pagina")
  4991. ("ja" :default "脚注" :html "&#33050;&#27880;")
  4992. ("nl" :default "Voetnoten")
  4993. ("no" :default "Fotnoter")
  4994. ("nb" :default "Fotnoter")
  4995. ("nn" :default "Fotnotar")
  4996. ("pl" :default "Przypis")
  4997. ("pt_BR" :html "Notas de Rodap&eacute;" :default "Notas de Rodapé" :ascii "Notas de Rodape")
  4998. ("ru" :html "&#1057;&#1085;&#1086;&#1089;&#1082;&#1080;" :utf-8 "Сноски")
  4999. ("sv" :default "Fotnoter")
  5000. ("uk" :html "&#1055;&#1088;&#1080;&#1084;&#1110;&#1090;&#1082;&#1080;"
  5001. :utf-8 "Примітки")
  5002. ("zh-CN" :html "&#33050;&#27880;" :utf-8 "脚注")
  5003. ("zh-TW" :html "&#33139;&#35387;" :utf-8 "腳註"))
  5004. ("List of Listings"
  5005. ("da" :default "Programmer")
  5006. ("de" :default "Programmauflistungsverzeichnis")
  5007. ("es" :ascii "Indice de Listados de programas" :html "&Iacute;ndice de Listados de programas" :default "Índice de Listados de programas")
  5008. ("et" :default "Loendite nimekiri")
  5009. ("fr" :default "Liste des programmes")
  5010. ("ja" :default "ソースコード目次")
  5011. ("no" :default "Dataprogrammer")
  5012. ("nb" :default "Dataprogrammer")
  5013. ("ru" :html "&#1057;&#1087;&#1080;&#1089;&#1086;&#1082; &#1088;&#1072;&#1089;&#1087;&#1077;&#1095;&#1072;&#1090;&#1086;&#1082;"
  5014. :utf-8 "Список распечаток")
  5015. ("zh-CN" :html "&#20195;&#30721;&#30446;&#24405;" :utf-8 "代码目录"))
  5016. ("List of Tables"
  5017. ("da" :default "Tabeller")
  5018. ("de" :default "Tabellenverzeichnis")
  5019. ("es" :ascii "Indice de tablas" :html "&Iacute;ndice de tablas" :default "Índice de tablas")
  5020. ("et" :default "Tabelite nimekiri")
  5021. ("fr" :default "Liste des tableaux")
  5022. ("ja" :default "表目次")
  5023. ("no" :default "Tabeller")
  5024. ("nb" :default "Tabeller")
  5025. ("nn" :default "Tabeller")
  5026. ("pt_BR" :default "Índice de Tabelas" :ascii "Indice de Tabelas")
  5027. ("ru" :html "&#1057;&#1087;&#1080;&#1089;&#1086;&#1082; &#1090;&#1072;&#1073;&#1083;&#1080;&#1094;"
  5028. :utf-8 "Список таблиц")
  5029. ("sv" :default "Tabeller")
  5030. ("zh-CN" :html "&#34920;&#26684;&#30446;&#24405;" :utf-8 "表格目录"))
  5031. ("Listing"
  5032. ("da" :default "Program")
  5033. ("de" :default "Programmlisting")
  5034. ("es" :default "Listado de programa")
  5035. ("et" :default "Loend")
  5036. ("fr" :default "Programme" :html "Programme")
  5037. ("ja" :default "ソースコード")
  5038. ("no" :default "Dataprogram")
  5039. ("nb" :default "Dataprogram")
  5040. ("pt_BR" :default "Listagem")
  5041. ("ru" :html "&#1056;&#1072;&#1089;&#1087;&#1077;&#1095;&#1072;&#1090;&#1082;&#1072;"
  5042. :utf-8 "Распечатка")
  5043. ("zh-CN" :html "&#20195;&#30721;" :utf-8 "代码"))
  5044. ("Listing %d:"
  5045. ("da" :default "Program %d")
  5046. ("de" :default "Programmlisting %d")
  5047. ("es" :default "Listado de programa %d")
  5048. ("et" :default "Loend %d")
  5049. ("fr" :default "Programme %d :" :html "Programme&nbsp;%d&nbsp;:")
  5050. ("ja" :default "ソースコード%d:")
  5051. ("no" :default "Dataprogram %d")
  5052. ("nb" :default "Dataprogram %d")
  5053. ("pt_BR" :default "Listagem %d")
  5054. ("ru" :html "&#1056;&#1072;&#1089;&#1087;&#1077;&#1095;&#1072;&#1090;&#1082;&#1072; %d.:"
  5055. :utf-8 "Распечатка %d.:")
  5056. ("zh-CN" :html "&#20195;&#30721;%d&nbsp;" :utf-8 "代码%d "))
  5057. ("References"
  5058. ("fr" :ascii "References" :default "Références")
  5059. ("de" :default "Quellen")
  5060. ("es" :default "Referencias"))
  5061. ("See section %s"
  5062. ("da" :default "jævnfør afsnit %s")
  5063. ("de" :default "siehe Abschnitt %s")
  5064. ("es" :ascii "Vea seccion %s" :html "Vea secci&oacute;n %s" :default "Vea sección %s")
  5065. ("et" :html "Vaata peat&#252;kki %s" :utf-8 "Vaata peatükki %s")
  5066. ("fr" :default "cf. section %s")
  5067. ("ja" :default "セクション %s を参照")
  5068. ("pt_BR" :html "Veja a se&ccedil;&atilde;o %s" :default "Veja a seção %s"
  5069. :ascii "Veja a secao %s")
  5070. ("ru" :html "&#1057;&#1084;. &#1088;&#1072;&#1079;&#1076;&#1077;&#1083; %s"
  5071. :utf-8 "См. раздел %s")
  5072. ("zh-CN" :html "&#21442;&#35265;&#31532;%s&#33410;" :utf-8 "参见第%s节"))
  5073. ("Table"
  5074. ("de" :default "Tabelle")
  5075. ("es" :default "Tabla")
  5076. ("et" :default "Tabel")
  5077. ("fr" :default "Tableau")
  5078. ("ja" :default "表" :html "&#34920;")
  5079. ("pt_BR" :default "Tabela")
  5080. ("ru" :html "&#1058;&#1072;&#1073;&#1083;&#1080;&#1094;&#1072;"
  5081. :utf-8 "Таблица")
  5082. ("zh-CN" :html "&#34920;" :utf-8 "表"))
  5083. ("Table %d:"
  5084. ("da" :default "Tabel %d")
  5085. ("de" :default "Tabelle %d")
  5086. ("es" :default "Tabla %d")
  5087. ("et" :default "Tabel %d")
  5088. ("fr" :default "Tableau %d :")
  5089. ("ja" :default "表%d:" :html "&#34920;%d:")
  5090. ("no" :default "Tabell %d")
  5091. ("nb" :default "Tabell %d")
  5092. ("nn" :default "Tabell %d")
  5093. ("pt_BR" :default "Tabela %d")
  5094. ("ru" :html "&#1058;&#1072;&#1073;&#1083;&#1080;&#1094;&#1072; %d.:"
  5095. :utf-8 "Таблица %d.:")
  5096. ("sv" :default "Tabell %d")
  5097. ("zh-CN" :html "&#34920;%d&nbsp;" :utf-8 "表%d "))
  5098. ("Table of Contents"
  5099. ("ca" :html "&Iacute;ndex")
  5100. ("cs" :default "Obsah")
  5101. ("da" :default "Indhold")
  5102. ("de" :default "Inhaltsverzeichnis")
  5103. ("eo" :default "Enhavo")
  5104. ("es" :ascii "Indice" :html "&Iacute;ndice" :default "Índice")
  5105. ("et" :default "Sisukord")
  5106. ("fi" :html "Sis&auml;llysluettelo")
  5107. ("fr" :ascii "Sommaire" :default "Table des matières")
  5108. ("hu" :html "Tartalomjegyz&eacute;k")
  5109. ("is" :default "Efnisyfirlit")
  5110. ("it" :default "Indice")
  5111. ("ja" :default "目次" :html "&#30446;&#27425;")
  5112. ("nl" :default "Inhoudsopgave")
  5113. ("no" :default "Innhold")
  5114. ("nb" :default "Innhold")
  5115. ("nn" :default "Innhald")
  5116. ("pl" :html "Spis tre&#x015b;ci")
  5117. ("pt_BR" :html "&Iacute;ndice" :utf8 "Índice" :ascii "Indice")
  5118. ("ru" :html "&#1057;&#1086;&#1076;&#1077;&#1088;&#1078;&#1072;&#1085;&#1080;&#1077;"
  5119. :utf-8 "Содержание")
  5120. ("sv" :html "Inneh&aring;ll")
  5121. ("uk" :html "&#1047;&#1084;&#1110;&#1089;&#1090;" :utf-8 "Зміст")
  5122. ("zh-CN" :html "&#30446;&#24405;" :utf-8 "目录")
  5123. ("zh-TW" :html "&#30446;&#37636;" :utf-8 "目錄"))
  5124. ("Unknown reference"
  5125. ("da" :default "ukendt reference")
  5126. ("de" :default "Unbekannter Verweis")
  5127. ("es" :default "Referencia desconocida")
  5128. ("et" :default "Tundmatu viide")
  5129. ("fr" :ascii "Destination inconnue" :default "Référence inconnue")
  5130. ("ja" :default "不明な参照先")
  5131. ("pt_BR" :default "Referência desconhecida"
  5132. :ascii "Referencia desconhecida")
  5133. ("ru" :html "&#1053;&#1077;&#1080;&#1079;&#1074;&#1077;&#1089;&#1090;&#1085;&#1072;&#1103; &#1089;&#1089;&#1099;&#1083;&#1082;&#1072;"
  5134. :utf-8 "Неизвестная ссылка")
  5135. ("zh-CN" :html "&#26410;&#30693;&#24341;&#29992;" :utf-8 "未知引用")))
  5136. "Dictionary for export engine.
  5137. Alist whose car is the string to translate and cdr is an alist
  5138. whose car is the language string and cdr is a plist whose
  5139. properties are possible charsets and values translated terms.
  5140. It is used as a database for `org-export-translate'. Since this
  5141. function returns the string as-is if no translation was found,
  5142. the variable only needs to record values different from the
  5143. entry.")
  5144. (defun org-export-translate (s encoding info)
  5145. "Translate string S according to language specification.
  5146. ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
  5147. and `:utf-8'. INFO is a plist used as a communication channel.
  5148. Translation depends on `:language' property. Return the
  5149. translated string. If no translation is found, try to fall back
  5150. to `:default' encoding. If it fails, return S."
  5151. (let* ((lang (plist-get info :language))
  5152. (translations (cdr (assoc lang
  5153. (cdr (assoc s org-export-dictionary))))))
  5154. (or (plist-get translations encoding)
  5155. (plist-get translations :default)
  5156. s)))
  5157. ;;; Asynchronous Export
  5158. ;;
  5159. ;; `org-export-async-start' is the entry point for asynchronous
  5160. ;; export. It recreates current buffer (including visibility,
  5161. ;; narrowing and visited file) in an external Emacs process, and
  5162. ;; evaluates a command there. It then applies a function on the
  5163. ;; returned results in the current process.
  5164. ;;
  5165. ;; At a higher level, `org-export-to-buffer' and `org-export-to-file'
  5166. ;; allow to export to a buffer or a file, asynchronously or not.
  5167. ;;
  5168. ;; `org-export-output-file-name' is an auxiliary function meant to be
  5169. ;; used with `org-export-to-file'. With a given extension, it tries
  5170. ;; to provide a canonical file name to write export output to.
  5171. ;;
  5172. ;; Asynchronously generated results are never displayed directly.
  5173. ;; Instead, they are stored in `org-export-stack-contents'. They can
  5174. ;; then be retrieved by calling `org-export-stack'.
  5175. ;;
  5176. ;; Export Stack is viewed through a dedicated major mode
  5177. ;;`org-export-stack-mode' and tools: `org-export-stack-refresh',
  5178. ;;`org-export-stack-delete', `org-export-stack-view' and
  5179. ;;`org-export-stack-clear'.
  5180. ;;
  5181. ;; For back-ends, `org-export-add-to-stack' add a new source to stack.
  5182. ;; It should be used whenever `org-export-async-start' is called.
  5183. (defmacro org-export-async-start (fun &rest body)
  5184. "Call function FUN on the results returned by BODY evaluation.
  5185. FUN is an anonymous function of one argument. BODY evaluation
  5186. happens in an asynchronous process, from a buffer which is an
  5187. exact copy of the current one.
  5188. Use `org-export-add-to-stack' in FUN in order to register results
  5189. in the stack.
  5190. This is a low level function. See also `org-export-to-buffer'
  5191. and `org-export-to-file' for more specialized functions."
  5192. (declare (indent 1) (debug t))
  5193. (org-with-gensyms (process temp-file copy-fun proc-buffer coding)
  5194. ;; Write the full sexp evaluating BODY in a copy of the current
  5195. ;; buffer to a temporary file, as it may be too long for program
  5196. ;; args in `start-process'.
  5197. `(with-temp-message "Initializing asynchronous export process"
  5198. (let ((,copy-fun (org-export--generate-copy-script (current-buffer)))
  5199. (,temp-file (make-temp-file "org-export-process"))
  5200. (,coding buffer-file-coding-system))
  5201. (with-temp-file ,temp-file
  5202. (insert
  5203. ;; Null characters (from variable values) are inserted
  5204. ;; within the file. As a consequence, coding system for
  5205. ;; buffer contents will not be recognized properly. So,
  5206. ;; we make sure it is the same as the one used to display
  5207. ;; the original buffer.
  5208. (format ";; -*- coding: %s; -*-\n%S"
  5209. ,coding
  5210. `(with-temp-buffer
  5211. (when org-export-async-debug '(setq debug-on-error t))
  5212. ;; Ignore `kill-emacs-hook' and code evaluation
  5213. ;; queries from Babel as we need a truly
  5214. ;; non-interactive process.
  5215. (setq kill-emacs-hook nil
  5216. org-babel-confirm-evaluate-answer-no t)
  5217. ;; Initialize export framework.
  5218. (require 'ox)
  5219. ;; Re-create current buffer there.
  5220. (funcall ,,copy-fun)
  5221. (restore-buffer-modified-p nil)
  5222. ;; Sexp to evaluate in the buffer.
  5223. (print (progn ,,@body))))))
  5224. ;; Start external process.
  5225. (let* ((process-connection-type nil)
  5226. (,proc-buffer (generate-new-buffer-name "*Org Export Process*"))
  5227. (,process
  5228. (apply
  5229. #'start-process
  5230. (append
  5231. (list "org-export-process"
  5232. ,proc-buffer
  5233. (expand-file-name invocation-name invocation-directory)
  5234. "--batch")
  5235. (if org-export-async-init-file
  5236. (list "-Q" "-l" org-export-async-init-file)
  5237. (list "-l" user-init-file))
  5238. (list "-l" ,temp-file)))))
  5239. ;; Register running process in stack.
  5240. (org-export-add-to-stack (get-buffer ,proc-buffer) nil ,process)
  5241. ;; Set-up sentinel in order to catch results.
  5242. (let ((handler ,fun))
  5243. (set-process-sentinel
  5244. ,process
  5245. `(lambda (p status)
  5246. (let ((proc-buffer (process-buffer p)))
  5247. (when (eq (process-status p) 'exit)
  5248. (unwind-protect
  5249. (if (zerop (process-exit-status p))
  5250. (unwind-protect
  5251. (let ((results
  5252. (with-current-buffer proc-buffer
  5253. (goto-char (point-max))
  5254. (backward-sexp)
  5255. (read (current-buffer)))))
  5256. (funcall ,handler results))
  5257. (unless org-export-async-debug
  5258. (and (get-buffer proc-buffer)
  5259. (kill-buffer proc-buffer))))
  5260. (org-export-add-to-stack proc-buffer nil p)
  5261. (ding)
  5262. (message "Process `%s' exited abnormally" p))
  5263. (unless org-export-async-debug
  5264. (delete-file ,,temp-file)))))))))))))
  5265. ;;;###autoload
  5266. (defun org-export-to-buffer
  5267. (backend buffer
  5268. &optional async subtreep visible-only body-only ext-plist
  5269. post-process)
  5270. "Call `org-export-as' with output to a specified buffer.
  5271. BACKEND is either an export back-end, as returned by, e.g.,
  5272. `org-export-create-backend', or a symbol referring to
  5273. a registered back-end.
  5274. BUFFER is the name of the output buffer. If it already exists,
  5275. it will be erased first, otherwise, it will be created.
  5276. A non-nil optional argument ASYNC means the process should happen
  5277. asynchronously. The resulting buffer should then be accessible
  5278. through the `org-export-stack' interface. When ASYNC is nil, the
  5279. buffer is displayed if `org-export-show-temporary-export-buffer'
  5280. is non-nil.
  5281. Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
  5282. EXT-PLIST are similar to those used in `org-export-as', which
  5283. see.
  5284. Optional argument POST-PROCESS is a function which should accept
  5285. no argument. It is always called within the current process,
  5286. from BUFFER, with point at its beginning. Export back-ends can
  5287. use it to set a major mode there, e.g,
  5288. (defun org-latex-export-as-latex
  5289. (&optional async subtreep visible-only body-only ext-plist)
  5290. (interactive)
  5291. (org-export-to-buffer \\='latex \"*Org LATEX Export*\"
  5292. async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
  5293. This function returns BUFFER."
  5294. (declare (indent 2))
  5295. (if async
  5296. (org-export-async-start
  5297. `(lambda (output)
  5298. (with-current-buffer (get-buffer-create ,buffer)
  5299. (erase-buffer)
  5300. (setq buffer-file-coding-system ',buffer-file-coding-system)
  5301. (insert output)
  5302. (goto-char (point-min))
  5303. (org-export-add-to-stack (current-buffer) ',backend)
  5304. (ignore-errors (funcall ,post-process))))
  5305. `(org-export-as
  5306. ',backend ,subtreep ,visible-only ,body-only ',ext-plist))
  5307. (let ((output
  5308. (org-export-as backend subtreep visible-only body-only ext-plist))
  5309. (buffer (get-buffer-create buffer))
  5310. (encoding buffer-file-coding-system))
  5311. (when (and (org-string-nw-p output) (org-export--copy-to-kill-ring-p))
  5312. (org-kill-new output))
  5313. (with-current-buffer buffer
  5314. (erase-buffer)
  5315. (setq buffer-file-coding-system encoding)
  5316. (insert output)
  5317. (goto-char (point-min))
  5318. (and (functionp post-process) (funcall post-process)))
  5319. (when org-export-show-temporary-export-buffer
  5320. (switch-to-buffer-other-window buffer))
  5321. buffer)))
  5322. ;;;###autoload
  5323. (defun org-export-to-file
  5324. (backend file &optional async subtreep visible-only body-only ext-plist
  5325. post-process)
  5326. "Call `org-export-as' with output to a specified file.
  5327. BACKEND is either an export back-end, as returned by, e.g.,
  5328. `org-export-create-backend', or a symbol referring to
  5329. a registered back-end. FILE is the name of the output file, as
  5330. a string.
  5331. A non-nil optional argument ASYNC means the process should happen
  5332. asynchronously. The resulting buffer will then be accessible
  5333. through the `org-export-stack' interface.
  5334. Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
  5335. EXT-PLIST are similar to those used in `org-export-as', which
  5336. see.
  5337. Optional argument POST-PROCESS is called with FILE as its
  5338. argument and happens asynchronously when ASYNC is non-nil. It
  5339. has to return a file name, or nil. Export back-ends can use this
  5340. to send the output file through additional processing, e.g,
  5341. (defun org-latex-export-to-latex
  5342. (&optional async subtreep visible-only body-only ext-plist)
  5343. (interactive)
  5344. (let ((outfile (org-export-output-file-name \".tex\" subtreep)))
  5345. (org-export-to-file \\='latex outfile
  5346. async subtreep visible-only body-only ext-plist
  5347. (lambda (file) (org-latex-compile file)))
  5348. The function returns either a file name returned by POST-PROCESS,
  5349. or FILE."
  5350. (declare (indent 2))
  5351. (if (not (file-writable-p file)) (error "Output file not writable")
  5352. (let ((ext-plist (org-combine-plists `(:output-file ,file) ext-plist))
  5353. (encoding (or org-export-coding-system buffer-file-coding-system)))
  5354. (if async
  5355. (org-export-async-start
  5356. `(lambda (file)
  5357. (org-export-add-to-stack (expand-file-name file) ',backend))
  5358. `(let ((output
  5359. (org-export-as
  5360. ',backend ,subtreep ,visible-only ,body-only
  5361. ',ext-plist)))
  5362. (with-temp-buffer
  5363. (insert output)
  5364. (let ((coding-system-for-write ',encoding))
  5365. (write-file ,file)))
  5366. (or (ignore-errors (funcall ',post-process ,file)) ,file)))
  5367. (let ((output (org-export-as
  5368. backend subtreep visible-only body-only ext-plist)))
  5369. (with-temp-buffer
  5370. (insert output)
  5371. (let ((coding-system-for-write encoding))
  5372. (write-file file)))
  5373. (when (and (org-export--copy-to-kill-ring-p) (org-string-nw-p output))
  5374. (org-kill-new output))
  5375. ;; Get proper return value.
  5376. (or (and (functionp post-process) (funcall post-process file))
  5377. file))))))
  5378. (defun org-export-output-file-name (extension &optional subtreep pub-dir)
  5379. "Return output file's name according to buffer specifications.
  5380. EXTENSION is a string representing the output file extension,
  5381. with the leading dot.
  5382. With a non-nil optional argument SUBTREEP, try to determine
  5383. output file's name by looking for \"EXPORT_FILE_NAME\" property
  5384. of subtree at point.
  5385. When optional argument PUB-DIR is set, use it as the publishing
  5386. directory.
  5387. Return file name as a string."
  5388. (let* ((visited-file (buffer-file-name (buffer-base-buffer)))
  5389. (base-name
  5390. ;; File name may come from EXPORT_FILE_NAME subtree
  5391. ;; property.
  5392. (file-name-sans-extension
  5393. (or (and subtreep (org-entry-get nil "EXPORT_FILE_NAME" 'selective))
  5394. ;; File name may be extracted from buffer's associated
  5395. ;; file, if any.
  5396. (and visited-file (file-name-nondirectory visited-file))
  5397. ;; Can't determine file name on our own: Ask user.
  5398. (read-file-name
  5399. "Output file: " pub-dir nil nil nil
  5400. (lambda (name)
  5401. (string= (file-name-extension name t) extension))))))
  5402. (output-file
  5403. ;; Build file name. Enforce EXTENSION over whatever user
  5404. ;; may have come up with. PUB-DIR, if defined, always has
  5405. ;; precedence over any provided path.
  5406. (cond
  5407. (pub-dir
  5408. (concat (file-name-as-directory pub-dir)
  5409. (file-name-nondirectory base-name)
  5410. extension))
  5411. ((file-name-absolute-p base-name) (concat base-name extension))
  5412. (t (concat (file-name-as-directory ".") base-name extension)))))
  5413. ;; If writing to OUTPUT-FILE would overwrite original file, append
  5414. ;; EXTENSION another time to final name.
  5415. (if (and visited-file (file-equal-p visited-file output-file))
  5416. (concat output-file extension)
  5417. output-file)))
  5418. (defun org-export-add-to-stack (source backend &optional process)
  5419. "Add a new result to export stack if not present already.
  5420. SOURCE is a buffer or a file name containing export results.
  5421. BACKEND is a symbol representing export back-end used to generate
  5422. it.
  5423. Entries already pointing to SOURCE and unavailable entries are
  5424. removed beforehand. Return the new stack."
  5425. (setq org-export-stack-contents
  5426. (cons (list source backend (or process (current-time)))
  5427. (org-export-stack-remove source))))
  5428. (defun org-export-stack ()
  5429. "Menu for asynchronous export results and running processes."
  5430. (interactive)
  5431. (let ((buffer (get-buffer-create "*Org Export Stack*")))
  5432. (set-buffer buffer)
  5433. (when (zerop (buffer-size)) (org-export-stack-mode))
  5434. (org-export-stack-refresh)
  5435. (pop-to-buffer buffer))
  5436. (message "Type \"q\" to quit, \"?\" for help"))
  5437. (defun org-export--stack-source-at-point ()
  5438. "Return source from export results at point in stack."
  5439. (let ((source (car (nth (1- (org-current-line)) org-export-stack-contents))))
  5440. (if (not source) (error "Source unavailable, please refresh buffer")
  5441. (let ((source-name (if (stringp source) source (buffer-name source))))
  5442. (if (save-excursion
  5443. (beginning-of-line)
  5444. (looking-at (concat ".* +" (regexp-quote source-name) "$")))
  5445. source
  5446. ;; SOURCE is not consistent with current line. The stack
  5447. ;; view is outdated.
  5448. (error "Source unavailable; type `g' to update buffer"))))))
  5449. (defun org-export-stack-clear ()
  5450. "Remove all entries from export stack."
  5451. (interactive)
  5452. (setq org-export-stack-contents nil))
  5453. (defun org-export-stack-refresh (&rest _)
  5454. "Refresh the asynchronous export stack.
  5455. Unavailable sources are removed from the list. Return the new
  5456. stack."
  5457. (let ((inhibit-read-only t))
  5458. (org-preserve-lc
  5459. (erase-buffer)
  5460. (insert (concat
  5461. (mapconcat
  5462. (lambda (entry)
  5463. (let ((proc-p (processp (nth 2 entry))))
  5464. (concat
  5465. ;; Back-end.
  5466. (format " %-12s " (or (nth 1 entry) ""))
  5467. ;; Age.
  5468. (let ((data (nth 2 entry)))
  5469. (if proc-p (format " %6s " (process-status data))
  5470. ;; Compute age of the results.
  5471. (org-format-seconds
  5472. "%4h:%.2m "
  5473. (float-time (time-since data)))))
  5474. ;; Source.
  5475. (format " %s"
  5476. (let ((source (car entry)))
  5477. (if (stringp source) source
  5478. (buffer-name source)))))))
  5479. ;; Clear stack from exited processes, dead buffers or
  5480. ;; non-existent files.
  5481. (setq org-export-stack-contents
  5482. (cl-remove-if-not
  5483. (lambda (el)
  5484. (if (processp (nth 2 el))
  5485. (buffer-live-p (process-buffer (nth 2 el)))
  5486. (let ((source (car el)))
  5487. (if (bufferp source) (buffer-live-p source)
  5488. (file-exists-p source)))))
  5489. org-export-stack-contents)) "\n"))))))
  5490. (defun org-export-stack-remove (&optional source)
  5491. "Remove export results at point from stack.
  5492. If optional argument SOURCE is non-nil, remove it instead."
  5493. (interactive)
  5494. (let ((source (or source (org-export--stack-source-at-point))))
  5495. (setq org-export-stack-contents
  5496. (cl-remove-if (lambda (el) (equal (car el) source))
  5497. org-export-stack-contents))))
  5498. (defun org-export-stack-view (&optional in-emacs)
  5499. "View export results at point in stack.
  5500. With an optional prefix argument IN-EMACS, force viewing files
  5501. within Emacs."
  5502. (interactive "P")
  5503. (let ((source (org-export--stack-source-at-point)))
  5504. (cond ((processp source)
  5505. (org-switch-to-buffer-other-window (process-buffer source)))
  5506. ((bufferp source) (org-switch-to-buffer-other-window source))
  5507. (t (org-open-file source in-emacs)))))
  5508. (defvar org-export-stack-mode-map
  5509. (let ((km (make-sparse-keymap)))
  5510. (define-key km " " 'next-line)
  5511. (define-key km "n" 'next-line)
  5512. (define-key km "\C-n" 'next-line)
  5513. (define-key km [down] 'next-line)
  5514. (define-key km "p" 'previous-line)
  5515. (define-key km "\C-p" 'previous-line)
  5516. (define-key km "\C-?" 'previous-line)
  5517. (define-key km [up] 'previous-line)
  5518. (define-key km "C" 'org-export-stack-clear)
  5519. (define-key km "v" 'org-export-stack-view)
  5520. (define-key km (kbd "RET") 'org-export-stack-view)
  5521. (define-key km "d" 'org-export-stack-remove)
  5522. km)
  5523. "Keymap for Org Export Stack.")
  5524. (define-derived-mode org-export-stack-mode special-mode "Org-Stack"
  5525. "Mode for displaying asynchronous export stack.
  5526. Type \\[org-export-stack] to visualize the asynchronous export
  5527. stack.
  5528. In an Org Export Stack buffer, use \\<org-export-stack-mode-map>\\[org-export-stack-view] to view export output
  5529. on current line, \\[org-export-stack-remove] to remove it from the stack and \\[org-export-stack-clear] to clear
  5530. stack completely.
  5531. Removing entries in an Org Export Stack buffer doesn't affect
  5532. files or buffers, only the display.
  5533. \\{org-export-stack-mode-map}"
  5534. (abbrev-mode 0)
  5535. (auto-fill-mode 0)
  5536. (setq buffer-read-only t
  5537. buffer-undo-list t
  5538. truncate-lines t
  5539. header-line-format
  5540. '(:eval
  5541. (format " %-12s | %6s | %s" "Back-End" "Age" "Source")))
  5542. (org-add-hook 'post-command-hook 'org-export-stack-refresh nil t)
  5543. (setq-local revert-buffer-function
  5544. 'org-export-stack-refresh))
  5545. ;;; The Dispatcher
  5546. ;;
  5547. ;; `org-export-dispatch' is the standard interactive way to start an
  5548. ;; export process. It uses `org-export--dispatch-ui' as a subroutine
  5549. ;; for its interface, which, in turn, delegates response to key
  5550. ;; pressed to `org-export--dispatch-action'.
  5551. ;;;###autoload
  5552. (defun org-export-dispatch (&optional arg)
  5553. "Export dispatcher for Org mode.
  5554. It provides an access to common export related tasks in a buffer.
  5555. Its interface comes in two flavors: standard and expert.
  5556. While both share the same set of bindings, only the former
  5557. displays the valid keys associations in a dedicated buffer.
  5558. Scrolling (resp. line-wise motion) in this buffer is done with
  5559. SPC and DEL (resp. C-n and C-p) keys.
  5560. Set variable `org-export-dispatch-use-expert-ui' to switch to one
  5561. flavor or the other.
  5562. When ARG is \\[universal-argument], repeat the last export action, with the same set
  5563. of options used back then, on the current buffer.
  5564. When ARG is \\[universal-argument] \\[universal-argument], display the asynchronous export stack."
  5565. (interactive "P")
  5566. (let* ((input
  5567. (cond ((equal arg '(16)) '(stack))
  5568. ((and arg org-export-dispatch-last-action))
  5569. (t (save-window-excursion
  5570. (unwind-protect
  5571. (progn
  5572. ;; Remember where we are
  5573. (move-marker org-export-dispatch-last-position
  5574. (point)
  5575. (org-base-buffer (current-buffer)))
  5576. ;; Get and store an export command
  5577. (setq org-export-dispatch-last-action
  5578. (org-export--dispatch-ui
  5579. (list org-export-initial-scope
  5580. (and org-export-in-background 'async))
  5581. nil
  5582. org-export-dispatch-use-expert-ui)))
  5583. (and (get-buffer "*Org Export Dispatcher*")
  5584. (kill-buffer "*Org Export Dispatcher*")))))))
  5585. (action (car input))
  5586. (optns (cdr input)))
  5587. (unless (memq 'subtree optns)
  5588. (move-marker org-export-dispatch-last-position nil))
  5589. (cl-case action
  5590. ;; First handle special hard-coded actions.
  5591. (template (org-export-insert-default-template nil optns))
  5592. (stack (org-export-stack))
  5593. (publish-current-file
  5594. (org-publish-current-file (memq 'force optns) (memq 'async optns)))
  5595. (publish-current-project
  5596. (org-publish-current-project (memq 'force optns) (memq 'async optns)))
  5597. (publish-choose-project
  5598. (org-publish (assoc (org-icompleting-read
  5599. "Publish project: "
  5600. org-publish-project-alist nil t)
  5601. org-publish-project-alist)
  5602. (memq 'force optns)
  5603. (memq 'async optns)))
  5604. (publish-all (org-publish-all (memq 'force optns) (memq 'async optns)))
  5605. (otherwise
  5606. (save-excursion
  5607. (when arg
  5608. ;; Repeating command, maybe move cursor to restore subtree
  5609. ;; context.
  5610. (if (eq (marker-buffer org-export-dispatch-last-position)
  5611. (org-base-buffer (current-buffer)))
  5612. (goto-char org-export-dispatch-last-position)
  5613. ;; We are in a different buffer, forget position.
  5614. (move-marker org-export-dispatch-last-position nil)))
  5615. (funcall action
  5616. ;; Return a symbol instead of a list to ease
  5617. ;; asynchronous export macro use.
  5618. (and (memq 'async optns) t)
  5619. (and (memq 'subtree optns) t)
  5620. (and (memq 'visible optns) t)
  5621. (and (memq 'body optns) t)))))))
  5622. (defun org-export--dispatch-ui (options first-key expertp)
  5623. "Handle interface for `org-export-dispatch'.
  5624. OPTIONS is a list containing current interactive options set for
  5625. export. It can contain any of the following symbols:
  5626. `body' toggles a body-only export
  5627. `subtree' restricts export to current subtree
  5628. `visible' restricts export to visible part of buffer.
  5629. `force' force publishing files.
  5630. `async' use asynchronous export process
  5631. FIRST-KEY is the key pressed to select the first level menu. It
  5632. is nil when this menu hasn't been selected yet.
  5633. EXPERTP, when non-nil, triggers expert UI. In that case, no help
  5634. buffer is provided, but indications about currently active
  5635. options are given in the prompt. Moreover, [?] allows to switch
  5636. back to standard interface."
  5637. (let* ((fontify-key
  5638. (lambda (key &optional access-key)
  5639. ;; Fontify KEY string. Optional argument ACCESS-KEY, when
  5640. ;; non-nil is the required first-level key to activate
  5641. ;; KEY. When its value is t, activate KEY independently
  5642. ;; on the first key, if any. A nil value means KEY will
  5643. ;; only be activated at first level.
  5644. (if (or (eq access-key t) (eq access-key first-key))
  5645. (org-propertize key 'face 'org-warning)
  5646. key)))
  5647. (fontify-value
  5648. (lambda (value)
  5649. ;; Fontify VALUE string.
  5650. (org-propertize value 'face 'font-lock-variable-name-face)))
  5651. ;; Prepare menu entries by extracting them from registered
  5652. ;; back-ends and sorting them by access key and by ordinal,
  5653. ;; if any.
  5654. (entries
  5655. (sort (sort (delq nil
  5656. (mapcar #'org-export-backend-menu
  5657. org-export-registered-backends))
  5658. (lambda (a b)
  5659. (let ((key-a (nth 1 a))
  5660. (key-b (nth 1 b)))
  5661. (cond ((and (numberp key-a) (numberp key-b))
  5662. (< key-a key-b))
  5663. ((numberp key-b) t)))))
  5664. 'car-less-than-car))
  5665. ;; Compute a list of allowed keys based on the first key
  5666. ;; pressed, if any. Some keys
  5667. ;; (?^B, ?^V, ?^S, ?^F, ?^A, ?&, ?# and ?q) are always
  5668. ;; available.
  5669. (allowed-keys
  5670. (nconc (list 2 22 19 6 1)
  5671. (if (not first-key) (org-uniquify (mapcar 'car entries))
  5672. (let (sub-menu)
  5673. (dolist (entry entries (sort (mapcar 'car sub-menu) '<))
  5674. (when (eq (car entry) first-key)
  5675. (setq sub-menu (append (nth 2 entry) sub-menu))))))
  5676. (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
  5677. ((not first-key) (list ?P)))
  5678. (list ?& ?#)
  5679. (when expertp (list ??))
  5680. (list ?q)))
  5681. ;; Build the help menu for standard UI.
  5682. (help
  5683. (unless expertp
  5684. (concat
  5685. ;; Options are hard-coded.
  5686. (format "[%s] Body only: %s [%s] Visible only: %s
  5687. \[%s] Export scope: %s [%s] Force publishing: %s
  5688. \[%s] Async export: %s\n\n"
  5689. (funcall fontify-key "C-b" t)
  5690. (funcall fontify-value
  5691. (if (memq 'body options) "On " "Off"))
  5692. (funcall fontify-key "C-v" t)
  5693. (funcall fontify-value
  5694. (if (memq 'visible options) "On " "Off"))
  5695. (funcall fontify-key "C-s" t)
  5696. (funcall fontify-value
  5697. (if (memq 'subtree options) "Subtree" "Buffer "))
  5698. (funcall fontify-key "C-f" t)
  5699. (funcall fontify-value
  5700. (if (memq 'force options) "On " "Off"))
  5701. (funcall fontify-key "C-a" t)
  5702. (funcall fontify-value
  5703. (if (memq 'async options) "On " "Off")))
  5704. ;; Display registered back-end entries. When a key
  5705. ;; appears for the second time, do not create another
  5706. ;; entry, but append its sub-menu to existing menu.
  5707. (let (last-key)
  5708. (mapconcat
  5709. (lambda (entry)
  5710. (let ((top-key (car entry)))
  5711. (concat
  5712. (unless (eq top-key last-key)
  5713. (setq last-key top-key)
  5714. (format "\n[%s] %s\n"
  5715. (funcall fontify-key (char-to-string top-key))
  5716. (nth 1 entry)))
  5717. (let ((sub-menu (nth 2 entry)))
  5718. (unless (functionp sub-menu)
  5719. ;; Split sub-menu into two columns.
  5720. (let ((index -1))
  5721. (concat
  5722. (mapconcat
  5723. (lambda (sub-entry)
  5724. (cl-incf index)
  5725. (format
  5726. (if (zerop (mod index 2)) " [%s] %-26s"
  5727. "[%s] %s\n")
  5728. (funcall fontify-key
  5729. (char-to-string (car sub-entry))
  5730. top-key)
  5731. (nth 1 sub-entry)))
  5732. sub-menu "")
  5733. (when (zerop (mod index 2)) "\n"))))))))
  5734. entries ""))
  5735. ;; Publishing menu is hard-coded.
  5736. (format "\n[%s] Publish
  5737. [%s] Current file [%s] Current project
  5738. [%s] Choose project [%s] All projects\n\n\n"
  5739. (funcall fontify-key "P")
  5740. (funcall fontify-key "f" ?P)
  5741. (funcall fontify-key "p" ?P)
  5742. (funcall fontify-key "x" ?P)
  5743. (funcall fontify-key "a" ?P))
  5744. (format "[%s] Export stack [%s] Insert template\n"
  5745. (funcall fontify-key "&" t)
  5746. (funcall fontify-key "#" t))
  5747. (format "[%s] %s"
  5748. (funcall fontify-key "q" t)
  5749. (if first-key "Main menu" "Exit")))))
  5750. ;; Build prompts for both standard and expert UI.
  5751. (standard-prompt (unless expertp "Export command: "))
  5752. (expert-prompt
  5753. (when expertp
  5754. (format
  5755. "Export command (C-%s%s%s%s%s) [%s]: "
  5756. (if (memq 'body options) (funcall fontify-key "b" t) "b")
  5757. (if (memq 'visible options) (funcall fontify-key "v" t) "v")
  5758. (if (memq 'subtree options) (funcall fontify-key "s" t) "s")
  5759. (if (memq 'force options) (funcall fontify-key "f" t) "f")
  5760. (if (memq 'async options) (funcall fontify-key "a" t) "a")
  5761. (mapconcat (lambda (k)
  5762. ;; Strip control characters.
  5763. (unless (< k 27) (char-to-string k)))
  5764. allowed-keys "")))))
  5765. ;; With expert UI, just read key with a fancy prompt. In standard
  5766. ;; UI, display an intrusive help buffer.
  5767. (if expertp
  5768. (org-export--dispatch-action
  5769. expert-prompt allowed-keys entries options first-key expertp)
  5770. ;; At first call, create frame layout in order to display menu.
  5771. (unless (get-buffer "*Org Export Dispatcher*")
  5772. (delete-other-windows)
  5773. (org-switch-to-buffer-other-window
  5774. (get-buffer-create "*Org Export Dispatcher*"))
  5775. (setq cursor-type nil
  5776. header-line-format "Use SPC, DEL, C-n or C-p to navigate.")
  5777. ;; Make sure that invisible cursor will not highlight square
  5778. ;; brackets.
  5779. (set-syntax-table (copy-syntax-table))
  5780. (modify-syntax-entry ?\[ "w"))
  5781. ;; At this point, the buffer containing the menu exists and is
  5782. ;; visible in the current window. So, refresh it.
  5783. (with-current-buffer "*Org Export Dispatcher*"
  5784. ;; Refresh help. Maintain display continuity by re-visiting
  5785. ;; previous window position.
  5786. (let ((pos (window-start)))
  5787. (erase-buffer)
  5788. (insert help)
  5789. (set-window-start nil pos)))
  5790. (org-fit-window-to-buffer)
  5791. (org-export--dispatch-action
  5792. standard-prompt allowed-keys entries options first-key expertp))))
  5793. (defun org-export--dispatch-action
  5794. (prompt allowed-keys entries options first-key expertp)
  5795. "Read a character from command input and act accordingly.
  5796. PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
  5797. a list of characters available at a given step in the process.
  5798. ENTRIES is a list of menu entries. OPTIONS, FIRST-KEY and
  5799. EXPERTP are the same as defined in `org-export--dispatch-ui',
  5800. which see.
  5801. Toggle export options when required. Otherwise, return value is
  5802. a list with action as CAR and a list of interactive export
  5803. options as CDR."
  5804. (let (key)
  5805. ;; Scrolling: when in non-expert mode, act on motion keys (C-n,
  5806. ;; C-p, SPC, DEL).
  5807. (while (and (setq key (read-char-exclusive prompt))
  5808. (not expertp)
  5809. (memq key '(14 16 ?\s ?\d)))
  5810. (cl-case key
  5811. (14 (if (not (pos-visible-in-window-p (point-max)))
  5812. (ignore-errors (scroll-up 1))
  5813. (message "End of buffer")
  5814. (sit-for 1)))
  5815. (16 (if (not (pos-visible-in-window-p (point-min)))
  5816. (ignore-errors (scroll-down 1))
  5817. (message "Beginning of buffer")
  5818. (sit-for 1)))
  5819. (?\s (if (not (pos-visible-in-window-p (point-max)))
  5820. (scroll-up nil)
  5821. (message "End of buffer")
  5822. (sit-for 1)))
  5823. (?\d (if (not (pos-visible-in-window-p (point-min)))
  5824. (scroll-down nil)
  5825. (message "Beginning of buffer")
  5826. (sit-for 1)))))
  5827. (cond
  5828. ;; Ignore undefined associations.
  5829. ((not (memq key allowed-keys))
  5830. (ding)
  5831. (unless expertp (message "Invalid key") (sit-for 1))
  5832. (org-export--dispatch-ui options first-key expertp))
  5833. ;; q key at first level aborts export. At second level, cancel
  5834. ;; first key instead.
  5835. ((eq key ?q) (if (not first-key) (error "Export aborted")
  5836. (org-export--dispatch-ui options nil expertp)))
  5837. ;; Help key: Switch back to standard interface if expert UI was
  5838. ;; active.
  5839. ((eq key ??) (org-export--dispatch-ui options first-key nil))
  5840. ;; Send request for template insertion along with export scope.
  5841. ((eq key ?#) (cons 'template (memq 'subtree options)))
  5842. ;; Switch to asynchronous export stack.
  5843. ((eq key ?&) '(stack))
  5844. ;; Toggle options: C-b (2) C-v (22) C-s (19) C-f (6) C-a (1).
  5845. ((memq key '(2 22 19 6 1))
  5846. (org-export--dispatch-ui
  5847. (let ((option (cl-case key (2 'body) (22 'visible) (19 'subtree)
  5848. (6 'force) (1 'async))))
  5849. (if (memq option options) (remq option options)
  5850. (cons option options)))
  5851. first-key expertp))
  5852. ;; Action selected: Send key and options back to
  5853. ;; `org-export-dispatch'.
  5854. ((or first-key (functionp (nth 2 (assq key entries))))
  5855. (cons (cond
  5856. ((not first-key) (nth 2 (assq key entries)))
  5857. ;; Publishing actions are hard-coded. Send a special
  5858. ;; signal to `org-export-dispatch'.
  5859. ((eq first-key ?P)
  5860. (cl-case key
  5861. (?f 'publish-current-file)
  5862. (?p 'publish-current-project)
  5863. (?x 'publish-choose-project)
  5864. (?a 'publish-all)))
  5865. ;; Return first action associated to FIRST-KEY + KEY
  5866. ;; path. Indeed, derived backends can share the same
  5867. ;; FIRST-KEY.
  5868. (t (catch 'found
  5869. (dolist (entry (member (assq first-key entries) entries))
  5870. (let ((match (assq key (nth 2 entry))))
  5871. (when match (throw 'found (nth 2 match))))))))
  5872. options))
  5873. ;; Otherwise, enter sub-menu.
  5874. (t (org-export--dispatch-ui options key expertp)))))
  5875. (provide 'ox)
  5876. ;; Local variables:
  5877. ;; generated-autoload-file: "org-loaddefs.el"
  5878. ;; End:
  5879. ;;; ox.el ends here