Anda dapat membuat halaman dokumentasi untuk buat pada modul Scribunto ini.
Penyunting dapat melakukan uji coba pada halaman bak pasir (buat | cermin) dan kasus uji (buat) modul ini.
Subhalaman modul ini.
Penyunting dapat melakukan uji coba pada halaman bak pasir (buat | cermin) dan kasus uji (buat) modul ini.
Subhalaman modul ini.
localp={} -- Change to "" upon deployment. localmoduleSuffix="/sandbox" localparserModuleName="Modul:Road data/parser"..moduleSuffix localstatenameModuleName="Modul:Jct/statename/sandbox"-- TODO transition localconcat=table.concat localinsert=table.insert localformat=mw.ustring.format localparserModule=require(parserModuleName) localparser=parserModule.parser localutil=require("Modul:Road data/util") -- Shields localdefaultShieldSize=20 localfunctionaddContextBanner(route,name,suffix,bannerSpec) localbannerModule='Modul:Road data/banners/'..string.upper(route.country) localshieldfield=name..'shield' localshield=parser(route,shieldfield) ifshield==nilthen -- This route type does not define shield. -- Find shield in the default banner table. shield=parser(route,'shield',name,bannerModule) ifshieldandshield~=''then ifsuffix==nilthen suffix=parser(route,'shield','suffix',bannerModule) end ifsuffixandsuffix~=''then shield=shield.." "..suffix end shield=shield..".svg" end end ifshieldandshield~=''then localshieldsizefield=name..'shieldsize' localshieldSize=parser(route,shieldsizefield) ifshieldSize==nilthen -- This route type does not define shield size. -- Find shield size in the default banner table. shieldSize=parser(route,'shieldsize',name,bannerModule) end ifshieldSize==nilthenshieldSize="20"end -- Add banner plate. insert(bannerSpec,{shield,shieldSize}) end end localfunctionbannerSpec(banner,bannerSize,bannerSuffix,route) localbanners={} iftype(banner)=="table"then localbannerSizeIsNotTable=type(bannerSize)~="table" fori,filenameinipairs(banner)do localbannersize=bannerSizeIsNotTableandbannerSizeorbannerSize[i]ordefaultShieldSize insert(banners,{filename,bannersize}) end elseifbanner~=''then insert(banners,{banner,bannerSize}) end ifroute.dirthen addContextBanner(route,'dir',bannerSuffix,banners) end ifroute.tothen addContextBanner(route,'to',bannerSuffix,banners) end returnbanners end localfunctionshieldSpec(route,mainShield) localshieldSpec={} localshield ifmainShieldthenshield=parser(route,"shieldmain")end ifnotshieldthenshield=parser(route,'shield')or''end ifshield==''thenreturnshieldSpecend localshieldsize ifmainShieldthenshieldsize=parser(route,"shieldmainsize")end ifnotshieldsizethenshieldsize=parser(route,"shieldsize")or""end localshieldsizeIsNotTable=type(shieldsize)~="table" localbanner=parser(route,'banner')or{} localbannersize=parser(route,'bannersize')ordefaultShieldSize localbannersuffix=parser(route,'bannersuffix') localbannerIsNotTable=type(banner)~="table" localbannersizeIsNotTable=type(bannersize)~="table" localbannersuffixIsNotTable=type(bannersuffix)~="table" iftype(shield)=="table"then fori,filenameinipairs(shield)do localsize=shieldsizeIsNotTableandshieldsizeorshieldsize[i] ifsize==""thensize=nilend -- banner.all describes banners that apply to all multiple shields. localshieldBanner=bannerIsNotTableandbanneror(banner[i]orbanner.allor{}) -- Banner size is default if the corresponding entry -- in bannerSize table is not set. localshieldBannerSize= bannersizeIsNotTableandbannersize or(bannersize[i]orbannersize.allordefaultShieldSize) localshieldBannerSuffix=bannersuffixand(bannersuffixIsNotTableandbannersuffixorbannersuffix[i]) insert(shieldSpec,{ shield={filename,size}, banners=bannerSpec(shieldBanner,shieldBannerSize,shieldBannerSuffix,route) }) end elseifshield~=''then ifshieldsize==""thenshieldsize=nilend insert(shieldSpec,{ shield={shield,shieldsize}, banners=bannerSpec(banner,bannersize,bannersuffix,route) }) end returnshieldSpec end localmissingShields localshieldExistsCache={} -- Return up to two booleans. -- The first boolean is false if `shield` does not exist, and true otherwise. -- If the first boolean is true, the second boolean is true if the shield is -- landscape (width >= height), and false otherwise. localfunctionshieldExists(shield) localresult=shieldExistsCache[shield] ifresult==nilthen localfile=mw.title.new(shield,'Media').file -- Cache result. localexists=file.exists result={exists} ifexiststhenresult[2]=file.width>=file.heightend shieldExistsCache[shield]=result end ifresult[1]thenreturntrue,result[2]end insert(missingShields,shield) returnfalse end localfunctionrender(shieldEntry,scale,showLink) localshield=shieldEntry.shield localbanners=shieldEntry.banners localexists,landscape=shieldExists(shield[1]) ifnotexiststhenreturn''end localsize ifshield[2]then localwidth,height=mw.ustring.match(shield[2],"(%d*)x?(%d*)") width=tonumber(width) height=tonumber(height) localsizeparts={} ifwidththen insert(sizeparts,format("%d",width*scale)) end ifheightthen insert(sizeparts,format("x%d",height*scale)) end size=concat(sizeparts) else size=format("%s%d",landscapeand"x"or"",defaultShieldSize*scale) end locallink=showLinkand""or"|link=" localshieldCode=format("[[File:%s|%spx%s|%s]]",shield[1],size,link,shield[1]) ifnotbanners[1]thenreturnshieldCodeend for_,bannerinipairs(banners)do ifshieldExists(banner[1])then shieldCode=format("[[File:%s|%dpx%s|%s]]<br>%s", banner[1], banner[2]*scale, link, banner[1], shieldCode) end end return'<span style="display: inline-block; vertical-align: baseline; line-height: 0; text-align: center;">'..shieldCode..'</span>' end functionp.shield(route,scale,showLink,mainShield) missingShields={} ifroute.rdtthen localshieldSize=mw.ustring.match(route.rdt,'^(%d+)$')or17 scale=shieldSize/defaultShieldSize end scale=scaleor1 localrendered={} for_,entryinipairs(shieldSpec(route,mainShield))do insert(rendered,render(entry,scale,showLink)) end returnconcat(rendered),missingShields end functionp.link(route) localabbr,errMsg=parser(route,'abbr') ifnotabbrthen route.typeerror=true returnutil.err(errMsgorformat("Invalid type: %s",route.typeor"(nil)")) end ifroute.nolinkthenreturnabbr,abbrend locallink=parser(route,'link')or'' iflink==''thenreturnabbr,abbrend returnformat("[[%s|%s]]",link,abbr),abbr end localfunctionstateName(args) -- TODO transition localdata=mw.loadData(statenameModuleName) localabbr=args.stateorargs.province localcountryData=data[args.country] returncountryDataandcountryData[abbr] end functionp.locations(args,module,group) module=moduleor"" localmodulearticle=module.."article" localmoduleprefix=module.."prefix" localmodulenameprefix=module.."nameprefix" localmodulenamesuffix=module.."namesuffix" localwarnings={} -- Region, for disambiguation localregion=parserModule.parser(args,"region"," common ") ifnotregionthen -- TODO transition ifargs.regionthen warnings.region="region parameter is deprecated" region=args.region elseifargs.countryand(args.stateorargs.province)then warnings.region="Inferring region from country and state/province" region=stateName(args) end end localregionName localregionText iftype(region)=="table"then regionName=region.name regionText=format("[[%s|%s]]",region.link,regionName) elseifregionthen regionName=region regionText=format("[[%s]]",regionName) end args.region=regionName locallocations=parserModule.parser(args,"locations"," common ")or{} -- Primary topic requires no specialization to supplied locations. localprimaryTopic=notlocationsandmodule=="jctint"andargs.primary_topic~='no' ifargs.primary_topicthen -- TODO transition warnings.primary_topic="primary_topic parameter is deprecated" end -- Independent city localindepCityText ifargs.indep_city_specialthen indepCityText=args.indep_city_special-- Overrides `indep_city` argument. elseifargs.indep_citythen localindepCity=args.indep_city localspec=locations.indep_city ifspecthen locallink=format("%s%s%s", spec.linkprefixor"",indepCity,spec.linksuffixor"") localname=format("%s%s%s", spec[modulenameprefix]orspec.nameprefixor"", indepCity, spec[modulenamesuffix]orspec.namesuffixor"") indepCityText=format("%s%s[[%s|%s]]", spec[modulearticle]orspec.articleor"", spec[moduleprefix]orspec.prefixor"", link,name) else -- TODO transition warnings.indep_city="Spec for indep_city parameter undefined in road data module" localcityLink-- Wikilink for independent city ifprimaryTopicthen cityLink=format('[[%s]]',indepCity) else -- Specialize independent city to the region. cityLink=format('[[%s, %s|%s]]',indepCity,region,indepCity) end indepCityText="[[Independent city|City]] of "..cityLink end end ifindepCityTextthen return{region=regionText,indep_city=indepCityText,warnings=warnings} end -- First-level subdivision, e.g., county -- Name of the type of subdivision, e.g., "County" and "Parish" localsub1name=args.sub1name-- TODO transition localsub1Text ifargs.sub1_specialthen sub1Text=args.sub1_special-- Overrides `sub1` argument. elseifargs.sub1then localsub1=args.sub1 localarticle locallink=sub1 localname=sub1 -- Type of first-level subdivision area, as a form of disambiguation localsub1area=args.sub1area ifsub1areathen localsub1areaSpec=locations.sub1areasandlocations.sub1areas[sub1area] ifsub1areaSpecthen article=sub1areaSpec[modulearticle]orsub1areaSpec.articleor"" link=format("%s%s%s", sub1areaSpec.linkprefixor"",link,sub1areaSpec.linksuffixor"") name=format("%s%s%s", groupand""orsub1areaSpec[modulenameprefix]orsub1areaSpec.nameprefixor"", name, groupand""orsub1areaSpec[modulenamesuffix]orsub1areaSpec.namesuffixor"") else -- TODO report error localerrMsg=util.err(format("Undefined sub1area: %s",sub1area)) name=format("%s%s",name,errMsg) end end iflocations.sub1then localspec=locations.sub1 -- Prepend and append text from spec. link=format("%s%s%s", spec.linkprefixor"",link,spec.linksuffixor"") name=format("%s%s%s", spec[modulenameprefix]orspec.nameprefixor"", name, spec[modulenamesuffix]orspec.namesuffixor"") sub1Text=format("%s[[%s|%s]]",articleor"",link,name) else -- TODO transition warnings.sub1="Spec for sub1 parameter undefined in road data module" -- Add type (if specified) to wikilink for first-level subdivision. localsub1Link=sub1nameandformat("%s %s",sub1,sub1name)orsub1 localsub1Name=module=="jcttop"andsub1Linkorsub1 ifprimaryTopicthen sub1Text=format('[[%s|%s]]',sub1Link,sub1Name) else -- Specialize first-level subdivision, with type added, to the region. sub1Text=format('[[%s, %s|%s]]',sub1Link,region,sub1Name) end end end -- Second-level subdivision, e.g., city and town localsub2Text ifargs.sub2_specialthen sub2Text=args.sub2_special-- Overrides `sub2` argument. elseifargs.sub2then localsub2=args.sub2 ifsub2=="none"then sub2Text="​"-- Zero-width space elseifsub2==" "then -- TODO transition warnings.sub2=" argument for sub2 parameter is deprecated" sub2Text="​"-- Zero-width space elseifprimaryTopicthen -- TODO transition sub2Text=format("[[%s]]",sub2) else localarticle locallink=sub2 localname=sub2 -- Type of area, e.g., city and village, as a form of disambiguation localsub2area=args.sub2area--[[TODO transition]]orargs.area ifsub2areathen localsub2areaSpec=locations.sub2areasandlocations.sub2areas[sub2area] ifnotsub2areaSpecthen -- TODO transition warnings.sub2= format("Spec for area parameter '%s' undefined in road data module",sub2area) localsub2areas={-- table of different area types city={ linksuffix=" (city)", jcttoparticle="the ", nameprefix="City of " }, town={ linksuffix=" (town)", jcttoparticle="the ", nameprefix="Town of " }, village={ linksuffix=" (village)", jcttoparticle="the ", nameprefix="Village of " }, community={ linksuffix=" (community)", jcttoparticle="the ", nameprefix="Community of " }, CDP={ linksuffix=" (CDP)", jcttoparticle="the ", nameprefix="Community of " }, hamlet={ linksuffix=" (hamlet)", jcttoparticle="the ", nameprefix="Hamlet of " }, ["unorganized territory"]={ linksuffix=" (unorganized territory)", jcttoparticle="the ", nameprefix="Unorganized Territory of " }, township={ linksuffix=" Township", namesuffix=" Township", } } sub2areaSpec=sub2areas[sub2area] end ifsub2areaSpecthen article=sub2areaSpec[modulearticle]orsub2areaSpec.articleor"" link=format("%s%s%s", sub2areaSpec.linkprefixor"",link,sub2areaSpec.linksuffixor"") name=format("%s%s%s", groupand""orsub2areaSpec[modulenameprefix]orsub2areaSpec.nameprefixor"", name, groupand""orsub2areaSpec[modulenamesuffix]orsub2areaSpec.namesuffixor"") else -- TODO report error localerrMsg=util.err(format("Undefined sub2area: %s",sub2area)) name=format("%s%s",name,errMsg) end end iflocations.sub2then localspec=locations.sub2 -- Prepend and append text from spec. link=format("%s%s%s", spec.linkprefixor"",link,spec.linksuffixor"") name=format("%s%s%s", spec[modulenameprefix]orspec.nameprefixor"", name, spec[modulenamesuffix]orspec.namesuffixor"") else -- TODO transition warnings.sub2="Spec for sub2 parameter undefined in road data module" -- Some second-level subdivisions are not unique in a given region. -- `sub1dab` is the first-level subdivision to be used for disambiguation. localsub1dab=args.sub1dab ifsub1dabthen sub1dab=sub1nameandformat("%s %s",sub1dab,sub1name)orsub1dab link=format("%s, %s",link,sub1dab) end link=format("%s, %s",link,region)-- Add region to wikilink end sub2Text=format("%s[[%s|%s]]",articleor"",link,name) end end return{region=regionText,sub1=sub1Text,sub2=sub2Text,warnings=warnings} end returnp