Module:Find sources: Difference between revisions
Jump to navigation
Jump to search
No edit summary Tag: Manual revert |
No edit summary Tag: Reverted |
||
| Line 1: | Line 1: | ||
-- | -- Module:Find sources | ||
-- Simple implementation for find sources functionality | |||
local p = {} | local p = {} | ||
local | -- 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 = | local linkCfg = links[code] | ||
if not linkCfg then | if not linkCfg then | ||
return string.format('[%s]', code) | |||
end | end | ||
local query = table.concat(args, '+') | |||
local url = string.gsub(linkCfg.url, '{{{1}}}', query) | |||
url = string.gsub(url, '{{{q}}}', query) | |||
return string.format('[%s %s]', url, linkCfg.display) | |||
return string.format('[%s %s]', url, | |||
end | end | ||
function p._main(template, args) | function p._main(template, args) | ||
local templateCfgs = { | |||
mainspace = { | |||
blurb = 'Find sources', | |||
introLink = nil, | |||
links = { | |||
{code = 'news'}, | |||
{code = 'newspapers'}, | |||
local | {code = 'books'}, | ||
local | {code = 'scholar'}, | ||
{code = 'jstor'}, | |||
}, | |||
separator = ' ⧼Dot-separator⧽ ', | |||
}, | |||
} | |||
local templateCfg = templateCfgs[template] or templateCfgs.mainspace | |||
local links_output = {} | |||
for i, linkCfg in ipairs(templateCfg.links) do | |||
links_output[#links_output + 1] = p._renderLink(linkCfg.code, args) | |||
end | end | ||
return table.concat(links_output, templateCfg.separator) | |||
end | |||
function p.main(frame) | |||
local args = {} | |||
local parentArgs = frame:getParent().args | |||
if | |||
-- Collect numbered arguments | |||
for i = 1, 20 do | |||
if parentArgs[i] and parentArgs[i] ~= '' then | |||
args[#args + 1] = parentArgs[i] | |||
end | end | ||
end | end | ||
-- If no arguments, use the page name as default search term | |||
if #args == 0 then | |||
local title = mw.title.getCurrentTitle() | |||
local | if title and title.namespace == 0 then | ||
args[1] = title.text | |||
if | |||
else | else | ||
args[1] = 'example' | |||
end | end | ||
end | end | ||
local template = frame.args[1] or 'mainspace' | |||
return p._main(template, args) | |||
local | |||
end | end | ||
-- Create the function that the template is looking for | |||
p['Find sources mainspace'] = p.main | |||
return p | return p | ||
Revision as of 11:39, 5 May 2026
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
local query = table.concat(args, '+')
local url = string.gsub(linkCfg.url, '{{{1}}}', query)
url = string.gsub(url, '{{{q}}}', query)
return string.format('[%s %s]', url, linkCfg.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 = ' ⧼Dot-separator⧽ ',
},
}
local templateCfg = templateCfgs[template] or templateCfgs.mainspace
local links_output = {}
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