Jump to content

Module:Find sources

Permanently protected module
From Wikipedia, the free encyclopedia

-- This module implements {{find sources}} and other similar templates, and-- also provides a mechanism to easily create new source-finding templates.-- Define constantslocalROOT_PAGE='Module:Find sources'localTEMPLATE_ROOT=ROOT_PAGE..'/templates/'-- for template config moduleslocalLINK_CONFIG=ROOT_PAGE..'/links'-- for link config moduleslocalCONFIG_PAGE=ROOT_PAGE..'/config'-- for global config-- Load required moduleslocalcheckType=require('libraryUtil').checkTypelocalcfg=mw.loadData(CONFIG_PAGE)localp={}localfunctionmaybeLoadData(page)localsuccess,data=pcall(mw.loadData,page)returnsuccessanddataendlocalfunctionsubstituteParams(msg,...)returnmw.message.newRawMessage(msg,...):plain()endlocalfunctionrenderSearchString(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)dosearchStrings[i]=sendiftransformFuncthenfori,sinipairs(searchStrings)dosearchStrings[i]=transformFunc(s)endendreturntable.concat(searchStrings,separator)endfunctionp._renderLink(code,searchTerms,display,tooltip)-- Renders the external link wikicode for one link, given the link code,-- a table of search terms, and an optional display value and tooltip.-- Get link config.locallinks=maybeLoadData(LINK_CONFIG)locallinkCfg=links[code]ifnotlinkCfgthenerror(string.format("invalid link code '%s'; no link config found at [[%s]]",code,LINK_CONFIG))end-- Make URL.localurldolocalseparator=linkCfg.separatoror"+"localsearchString=renderSearchString(searchTerms,separator,mw.uri.encode)url=substituteParams(linkCfg.url,searchString)endiftooltipthenreturnstring.format('<span title="%s" style="border-bottom: 1px dotted;">[%s %s]</span>',mw.text.encode(tooltip),url,displayorlinkCfg.display)elsereturnstring.format('[%s %s]',url,displayorlinkCfg.display)endendfunctionp._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..templatelocaltemplateCfg=maybeLoadData(templateCfgPage)ifnottemplateCfgthenerror(string.format("invalid template name '%s'; no template config found at [[%s]]",template,templateCfgPage))end-- Namespace check.ifnottemplateCfg.isUsedInMainspaceandtitle.namespace==0thenlocalformatString='<strong class="error">%s</strong>'ifcfg['namespace-error-category']thenformatString=formatString..'[[%s:%s]]'endreturnstring.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)dosearchTerms[i]=sendifnotsearchTerms[1]then-- Use the current subpage name as the default search term, unless -- another title is provided. If the page uses a disambiguator like-- "Foo (bar)", make "Foo" the first term and "bar" the second.localsearchTitle=args.titleortitle.subpageTextlocalterm,dab=searchTitle:match('^(.*) (%b())$')ifdabthendab=dab:sub(2,-2)-- Remove parensendiftermanddabthensearchTerms[1]=termsearchTerms[2]=dabelsesearchTerms[1]=searchTitleendendsearchTerms[1]='"'..searchTerms[1]..'"'-- Make the intro linklocalintroLinkiftemplateCfg.introLinkthenlocalcode=templateCfg.introLink.codelocaldisplay=templateCfg.introLink.displayorrenderSearchString(searchTerms,'&nbsp;')localtooltip=templateCfg.introLink.tooltipintroLink=p._renderLink(code,searchTerms,display,tooltip)elseintroLink=''end-- Make the other linkslocallinks={}localseparator=templateCfg.separatororcfg['default-separator']localsep=''fori,tinipairs(templateCfg.links)dolinks[i]=sep..p._renderLink(t.code,searchTerms,t.display,t.tooltip)..(t.afterDisplayor'')sep=t.separatororseparatorendlinks=table.concat(links)-- 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)endsetmetatable(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]].localtname=templateiftname:sub(-8)=='/sandbox'then-- This makes {{Find sources/sandbox|Albert Einstein}} work.tname=tname:sub(1,-9)endreturnfunction(frame)localargs=require('Module:Arguments').getArgs(frame,{wrappers=mw.site.namespaces[10].name..':'..tname})returnt._main(template,args)endend})returnp
close