Module:Find sources
Documentation for this module may be created at Module:Find sources/doc
-- Module:Find sources
-- Simple implementation for find sources functionality
local p = {}
-- Link configurations
local links = {
news = {
url = 'https://www.google.com/search?tbm=nws&q={{{1}}}',
display = 'news',
},
newspapers = {
url = 'https://www.google.com/search?tbm=nws&q={{{1}}}',
display = 'newspapers',
},
books = {
url = 'https://www.google.com/search?tbm=bks&q={{{1}}}',
display = 'books',
},
scholar = {
url = 'https://scholar.google.com/scholar?q={{{1}}}',
display = 'scholar',
},
jstor = {
url = 'https://www.jstor.org/action/doBasicSearch?Query={{{1}}}',
display = 'JSTOR',
},
}
function p._renderLink(code, args)
local linkCfg = links[code]
if not linkCfg then
return string.format('[%s]', code)
end
-- Build the search query
local query = table.concat(args, '+')
local url = string.gsub(linkCfg.url, '{{{1}}}', query)
url = string.gsub(url, '{{{q}}}', query)
-- Use only the display text from the config, not the search term
local display = linkCfg.display
return string.format('[%s %s]', url, display)
end
function p._main(template, args)
local templateCfgs = {
mainspace = {
blurb = 'Find sources',
introLink = nil,
links = {
{code = 'news'},
{code = 'newspapers'},
{code = 'books'},
{code = 'scholar'},
{code = 'jstor'},
},
separator = ' · ',
},
}
local templateCfg = templateCfgs[template] or templateCfgs.mainspace
local links_output = {}
-- Add the intro text "Find sources:"
local searchTerm = table.concat(args, ' ')
links_output[#links_output + 1] = string.format("'''Find sources:''' \"%s\"", searchTerm)
-- Add the links (just the display text, no search term repeated)
for i, linkCfg in ipairs(templateCfg.links) do
links_output[#links_output + 1] = p._renderLink(linkCfg.code, args)
end
return table.concat(links_output, templateCfg.separator)
end
function p.main(frame)
local args = {}
local parentArgs = frame:getParent().args
-- Collect numbered arguments
for i = 1, 20 do
if parentArgs[i] and parentArgs[i] ~= '' then
args[#args + 1] = parentArgs[i]
end
end
-- If no arguments, use the page name as default search term
if #args == 0 then
local title = mw.title.getCurrentTitle()
if title and title.namespace == 0 then
args[1] = title.text
else
args[1] = 'example'
end
end
local template = frame.args[1] or 'mainspace'
return p._main(template, args)
end
-- Create the function that the template is looking for
p['Find sources mainspace'] = p.main
return p