Module:FBI QR codes

From Hacks Guide Wiki
Revision as of 03:59, 5 July 2022 by Ihaveahax (talk | contribs) (try adding url again)

(diff) ← Older revision | Approved revision (diff) | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:FBI QR codes/doc

local p = {}

-- from [[Module:Infobox]]
-- Returns the union of the values of two tables, as a sequence.
local function union(t1, t2)

	local vals = {}
	for k, v in pairs(t1) do
		vals[v] = true
	end
	for k, v in pairs(t2) do
		vals[v] = true
	end
	local ret = {}
	for k, v in pairs(vals) do
		table.insert(ret, k)
	end
	return ret
end

-- from [[Module:Infobox]]
-- Returns a table containing the numbers of the arguments that exist
-- for the specified prefix. For example, if the prefix was 'data', and
-- 'data1', 'data2', and 'data5' exist, it would return {1, 2, 5}.
local function getArgNums(prefix, args)
	local nums = {}
	for k, v in pairs(args) do
		local num = tostring(k):match('^' .. prefix .. '([1-9]%d*)$')
		if num then table.insert(nums, tonumber(num)) end
	end
	table.sort(nums)
	return nums
end

function p.makeSections(frame, args)
	local sections = {}
	local nums = union(getArgNums('name', args), getArgNums('url', args))
	
	for k, v in ipairs(nums) do
		local name = args['name' .. tostring(v)]
		local url = args['url' .. tostring(v)]
		if name and url then
			local qr = frame:callParserFunction('#qrlite', {url, format = 'png', margin = '3'})
			table.insert(sections, {name, '<p class="hb-qrcode-image">'..qr..'</p><p class="hb-qrcode-link">['..url..' '..url..']</p>'})
		end
	end
	
	return sections
end

function p.makeTabberData(sections)
	local finalSections = ''
	for k, v in ipairs(sections) do
		finalSections = finalSections..'|-|'..v[1]..'='..v[2]
	end
	
	return finalSections
end

function p.makeTabber(frame, sections)
	return frame:callParserFunction('#tag', 'tabber', p.makeTabberData(sections))
end

function p.main(frame)
	local parent = frame:getParent()

	local sections = p.makeSections(parent, parent.args)
	return p.makeTabber(parent, sections)
end

function p.template(frame)
	local args = {}
	for k,v in pairs(frame.args) do args[k] = mw.text.trim(v) end
	local sections = p.makeSections(frame, args)
	return p.makeTabber(frame, sections)
end

return p