ox.el 253 KB

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