模組:FindSources

local fs = {}
local getArgs
local KW_LIMIT = 5

local function linkbuilder(args, url_model, split, url_other, firstword_show, split_show)
	local qw = {}
	for i = 1, KW_LIMIT do
		local val = args[i]
		val = (i == 1 and val == nil) and args["subpagename"] or val
		if val ~= nil then
			val = tostring(mw.uri.encode(i == 1 and '"' .. val .. '"' or val))
			if i ~= 1 then table.insert(qw, split) end
			table.insert(qw, val)
		else
			break
		end
	end

	local url = mw.ustring.format(url_model, table.concat(qw, ""))
	url = (url_other ~= nil and url .. url_other) or url

	local shower_url = nil
	if firstword_show == nil then
		local sw = {}
		for i = 1, KW_LIMIT do
			local val = args[i]
			val = (i == 1 and val == nil) and args["subpagename"] or val
			if val ~= nil then
				if i == 1 then
					table.insert(sw, '"' .. val .. '"')
				else
					table.insert(sw, split_show)
					table.insert(sw, val)
				end
			else
				break
			end
		end
		shower_url = table.concat(sw, "")
	else
		shower_url = firstword_show
	end

	return mw.ustring.format('[%s %s]', url, shower_url)
end

local function const(hans, hant, frame)
	local lan = require('Module:Lan')

	return lan._main(
		{ ['zh-hans'] = hans, ['zh-hant'] = hant },
		frame or mw.getCurrentFrame()
	)
end

function fs._main(args)
	if args["namespacenum"] == 0 then
		return '<span class="error">請勿在條目使用findsources模板!</span>'
	end

	local out = const('来源搜索', '來源搜尋') .. ':<span class="plainlinks">'
	--
	.. const('“', '「') .. linkbuilder(args, '//www.google.com/search?&as_eq=wikipedia&q=%s', '+', nil, nil, '&nbsp;') .. const('”', '」') .. '&mdash;&mdash;'
	--
	.. 'Google:'
	.. linkbuilder(args, '//www.google.com/search?q=%s', '+', nil, const('网页', '網頁')) .. '、'
	.. linkbuilder(args, '//www.google.com/search?tbm=nws&as_src=-newswire+-wire+-presswire+-PR+-press+-release+-wikipedia&q=%s', '+', args['free']=='yes' and '&as_price=p1' or nil, const('新闻', '新聞')) .. '、'
	.. linkbuilder(args, '//scholar.google.com/scholar?&q=%s', '+', nil, const('学术', '學術')) .. '、'	
	.. linkbuilder(args, '//www.google.com/search?tbo=p&tbm=bks&q=%s', '+', nil, const('图书', '圖書')) .. '、'
	.. linkbuilder(args, '//www.google.com/search?tbm=isch&safe=off&q=%s', '+', nil, const('图片', '圖片')) .. ';'
	--
	.. '百度:'
	.. linkbuilder(args, '//www.baidu.com/s?ie=utf-8&wd=%s', '+', nil, const('网页', '網頁')) .. '、'
	.. linkbuilder(args, 'https://www.baidu.com/s?rtt=1&bsst=1&cl=2&tn=news&rsv_dl=ns_pc&word=%s', '+', nil, const('新闻', '新聞')) .. '、'
	.. linkbuilder(args, 'http://xueshu.baidu.com/s?ie=utf-8&wd=%s', '+', nil, const('学术', '學術')) .. '、'
	.. linkbuilder(args, 'http://image.baidu.com/i?ie=utf-8&tn=baiduimage&word=%s', '+', nil, const('图片', '圖片')) .. ';'
	--
	.. linkbuilder(args, 'https://gongjushu.cnki.net/rbook/search/simplesearch?key=%s&t=0&c=1', '+', nil, const('知网工具书', '知網工具書')) .. ';'
	.. linkbuilder(args, 'https://www.jstor.org/action/doBasicSearch?Query=%s&acc=on&wc=on', '+', nil, 'JSTOR') .. ';'
	.. linkbuilder(args, 'https://wikipedialibrary.wmflabs.org/search/?q=%s', '+', nil, const('维基百科图书馆', '維基百科圖書館'))
	--
	.. '</span>'
	.. '<span style="display:none">'
	.. linkbuilder(args, '//abusefilter.invalid/ReportedPage?page=%s', '+', nil, 'Report')
	.. '</span>'

	return out
end

function fs.main(frame)
	if not getArgs then
		getArgs = require('Module:Arguments').getArgs
	end
	local args = getArgs(frame, {parentFirst = true})
	local curPage_obj = mw.title.getCurrentTitle()
	args["subpagename"] = curPage_obj.subpageText
	args["namespacenum"] = curPage_obj.namespace

	return fs._main(args)
end

return fs