ox.el 238 KB

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