ox.el 249 KB

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