Module:Find sources

From Wikitia
Revision as of 11:36, 5 May 2026 by Wikitia (talk | contribs)
Jump to navigation Jump to search

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 or ' ')
end

function p.main(frame)
	local args = {}
	local parentArgs = frame:getParent().args
	
	for i = 1, 20 do
		if parentArgs[i] and parentArgs[i] ~= '' then
			args[#args + 1] = parentArgs[i]
		end
	end
	
	local template = frame.args[1] or 'mainspace'
	
	return p._main(template, args)
end

return p