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.
-- This module implements {{find sources}} and other similar templates, and -- also provides a mechanism to easily create new source-finding templates. -- Define constants localROOT_PAGE='Module:Find sources' localTEMPLATE_ROOT=ROOT_PAGE..'/templates/'-- for template config modules localLINK_ROOT=ROOT_PAGE..'/links/'-- for link config modules localCONFIG_PAGE=ROOT_PAGE..'/config'-- for global config -- Load required modules localcheckType=require('libraryUtil').checkType localcfg=mw.loadData(CONFIG_PAGE) localp={} localfunctionmaybeLoadData(page) localsuccess,data=pcall(mw.loadData,page) returnsuccessanddata end localfunctionsubstituteParams(msg,...) returnmw.message.newRawMessage(msg,...):plain() end localfunctionrenderSearchString(searchTerms,separator,transformFunc) -- This takes a table of search terms and turns it into a search string -- that can be used in a URL or in a display value. The transformFunc -- parameter can be used to transform each search term in some way (for -- example, URL-encoding them). localsearchStrings={} fori,sinipairs(searchTerms)do searchStrings[i]=s end iftransformFuncthen fori,sinipairs(searchStrings)do searchStrings[i]=transformFunc(s) end end returntable.concat(searchStrings,separator) end functionp._renderLink(code,searchTerms,display) -- Renders the external link wikicode for one link, given the link code, -- a table of search terms, and an optional display value. -- Get link config. locallinkCfg=maybeLoadData(LINK_ROOT..code) ifnotlinkCfgthen error(string.format( "invalid link code '%s'; no link config found at [[%s]]", code, LINK_ROOT..code )) end -- Make URL. localurl do localseparator=linkCfg.separatoror"+" localsearchString=renderSearchString( searchTerms, separator, mw.uri.encode ) url=substituteParams(linkCfg.url,searchString) end returnstring.format('[%s %s]',url,displayorlinkCfg.display) end functionp._main(template,args) -- The main access point from Lua. checkType('_main',1,template,'string') checkType('_main',2,args,'table',true) args=argsor{} localtitle=mw.title.getCurrentTitle() -- Get the template config. localtemplateCfgPage=TEMPLATE_ROOT..template localtemplateCfg=maybeLoadData(templateCfgPage) ifnottemplateCfgthen error(string.format( "invalid template name '%s'; no template config found at [[%s]]", template,templateCfgPage )) end -- Namespace check. ifnottemplateCfg.isUsedInMainspaceandtitle.namespace==0then localformatString='<strong class="error">%s</strong>' ifcfg['namespace-error-category']then formatString=formatString..'[[%s:%s]]' end returnstring.format( formatString, cfg['namespace-error'], mw.site.namespaces[14].name, cfg['namespace-error-category'] ) end -- Get the search terms from the arguments. localsearchTerms={} fori,sinipairs(args)do searchTerms[i]=s end ifnotsearchTerms[1]then -- Use the current subpage name as the default search term. If the page -- uses a disambiguator like "Foo (bar)", make "Foo" the first term and -- "bar" the second. localterm,dab=title.subpageText:match('^(.*) (%b())$') ifdabthen dab=dab:sub(2,-2)-- Remove parens end iftermanddabthen searchTerms[1]=term searchTerms[2]=dab else searchTerms[1]=title.subpageText end end searchTerms[1]='"'..searchTerms[1]..'"' -- Make the intro link localintroLink iftemplateCfg.introLinkthen localcode=templateCfg.introLink.code localdisplay=templateCfg.introLink.displayorrenderSearchString( searchTerms, ' ' ) introLink=p._renderLink(code,searchTerms,display) else introLink='' end -- Make the other links locallinks={} fori,tinipairs(templateCfg.links)do links[i]=p._renderLink(t.code,searchTerms,t.display) end localseparator=templateCfg.separatororcfg['default-separator'] links=table.concat(links,separator) -- Make the blurb. localblurb=substituteParams(templateCfg.blurb,introLink,links) localspan=mw.html.create('span') span :addClass('plainlinks') :addClass(templateCfg.class) :cssText(templateCfg.style) :wikitext(blurb) returntostring(span) end setmetatable(p,{__index=function(t,template) -- The main access point from #invoke. -- Invocations will look like {{#invoke:Find sources|template name}}, -- where "template name" is a subpage of [[Module:Find sources/templates]]. returnfunction(frame) localargs=require('Module:Arguments').getArgs(frame,{ wrappers=mw.site.namespaces[10].name..':'..template }) returnt._main(template,args) end end}) returnp