Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:FBI QR code: Difference between revisions

From Hacks Guide Wiki
m (Ihaveahax moved page Module:FBI QR codes to Module:FBI QR code without leaving a redirect: match template name)
No edit summary
(22 intermediate revisions by the same user not shown)
Line 1: Line 1:
local args = {}
local p = {}
local p = {}


Line 23: Line 25:
-- for the specified prefix. For example, if the prefix was 'data', and
-- for the specified prefix. For example, if the prefix was 'data', and
-- 'data1', 'data2', and 'data5' exist, it would return {1, 2, 5}.
-- 'data1', 'data2', and 'data5' exist, it would return {1, 2, 5}.
local function getArgNums(prefix, args)
local function getArgNums(prefix)
local nums = {}
local nums = {}
for k, v in pairs(args) do
for k, v in pairs(args) do
Line 33: Line 35:
end
end


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


function p.makeTabberData(sections)
function p.makeTabber(sections)
local finalSections = ''
local tabber = mw.html.create('tabber')
for k, v in ipairs(sections) do
for k, v in ipairs(sections) do
finalSections = finalSections..'|-|'..v[1]..'='..v[2]
tabber:wikitext('|-|'..v[1]..'=\n\n', v[2])
end
end
return finalSections
return tostring(tabber)
end
 
function p.makeTabber(frame, sections)
return frame:callParserFunction('#tag', 'tabber', p.makeTabberData(sections))
end
end


function p.main(frame)
function p.main(frame)
local parent = frame:getParent()
local parent = frame:getParent()
-- adapted from [[Module:Infobox]]
if frame == mw.getCurrentFrame() then
args = frame:getParent().args
else
args = frame
end


local sections = p.makeSections(parent, parent.args)
local sections = p.makeSections(parent)
return p.makeTabber(parent, sections)
return p.makeTabber(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
end


return p
return p

Revision as of 05:07, 4 July 2022

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

local args = {}

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)
	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)
	local sections = {}
	local nums = union(getArgNums('name'), getArgNums('url'))
	
	for k, v in ipairs(nums) do
		local name = args['name' .. tostring(num)]
		local url = args['name' .. tostring(num)]
		if name and url then
			local qr = frame:callParserFunction('#qrlite', {url, format = 'svg', margin = '3'})
			table.insert(sections, {name, qr..frame:preprocess('<br>['..url..' '..url..']')})
		end
	end
	
	return sections
end

function p.makeTabber(sections)
	local tabber = mw.html.create('tabber')
	
	for k, v in ipairs(sections) do
		tabber:wikitext('|-|'..v[1]..'=\n\n', v[2])
	end
	
	return tostring(tabber)
end

function p.main(frame)
	local parent = frame:getParent()
	
	-- adapted from [[Module:Infobox]]
	if frame == mw.getCurrentFrame() then
		args = frame:getParent().args
	else
		args = frame
	end

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

return p