Mô đun:Portal

番版𠓨𣅶13:09、𣈜30𣎃12𢆥2015𧵑SaigonSarang (討論 | 㨂𢵰) (1 翻版)
() ←番版𫇰 | 番版㵋一 (恪) | 番版㵋→ (恪)

Documentation for this module may be created at Module:Mô đun:Portal/doc

--[==[ This module is a Lua implementation of the old {{Chủ đề}} template.
-- Please take care when updating it! It outputs two functions: p.portal, which generates a table of portals, and p.image, which
-- produces the image name for an individual portal.
]==]

local htmlBuilder = require('Module:HtmlBuilder')

local p = {}

local function getImageName(s)
	-- Gets the image name for a given string.
	return 'Portal-puzzle.svg'
end

function p._portal(portals, args)
	-- This function builds the portal box used by the {{chủ đề}} template.
	local root = htmlBuilder.create('div')
	root
		.addClass('noprint')
		.addClass(args.left and 'tleft' or 'tright')
		.addClass('portal')
		.css('border', 'solid #aaa 1px')
		.css('margin', args.margin or (args.left == 'yes' and '0.5em 1em 0.5em 0') or '0.5em 0 0.5em 1em')
		.newline()

	-- Start the table. This corresponds to the start of the wikitext table in the old [[Bản mẫu:Chủ đề]].
	local tableroot = root.tag('table')
		.css('background', '#f9f9f9')
		.css('font-size', '85%')
		.css('line-height', '110%')
		.css('max-width', '175px')
		.css('width', type(args.boxsize) == 'string' and (args.boxsize .. 'px'))
	
	-- If no portals have been specified, add the page to a tracking category.
	if not portals[1] then
		tableroot.wikitext('[[Thể loại:Bản mẫu chủ đề không định rõ chủ đề]]')
		table.insert(portals, mw.title.getCurrentTitle().baseText)
	end

	-- Display the portals specified in the positional arguments.
	for i, portal in ipairs(portals) do
		local image = args.image or getImageName(portal)
		
		-- Generate the html for the image and the portal name.
		tableroot
			.newline()
			.tag('tr')
				.attr('valign', 'middle')
				.tag('td')
					.css('text-align', 'center')
					.wikitext(mw.ustring.format('[[Tập tin:%s|32x28px|alt=|link=Chủ đề:%s|Chủ đề]]', image, portal))
					.done()
				.tag('td')
					.css('padding', '0 0.2em')
					.css('vertical-align', 'middle')
					.css('font-style', 'italic')
					.css('font-weight', 'bold')
					.wikitext(mw.ustring.format('[[Chủ đề:%s|Chủ đề%s%s]]', portal, args['break'] and '<br />' or ' ', portal))
	end
	return tostring(root)
end

function p._image(portals)

	-- Wrapper function to allow getImageName() to be accessed through #invoke.
	return getImageName(portals[1])
end

local function processPortalArgs(args)
	-- This function processes a table of arguments and returns two tables: an array of portal names for processing by ipairs, and a table of
	-- the named arguments that specify style options, etc. We need to use ipairs because we want to list all the portals in the order
	-- they were passed to the template, but we also want to be able to deal with positional arguments passed explicitly, for example
	-- {{chủ đề|2=Chính trị}}. The behaviour of ipairs is undefined if nil values are present, so we need to make sure they are all removed.
	args = type(args) == 'table' and args or {}
	local portals = {}
	local namedArgs = {}
	for k, v in pairs(args) do
		if type(k) == 'number' and type(v) == 'string' then -- Make sure we have no non-string portal names.
			table.insert(portals, k)
		elseif type(k) ~= 'number' then
			namedArgs[k] = v
		end
	end
	table.sort(portals)
	for i, v in ipairs(portals) do
		portals[i] = args[v]
	end
	return portals, namedArgs
end

local function makeWrapper(funcName)
	-- Processes external arguments and sends them to the other functions.
	return function (frame)
		-- If called via #invoke, use the args passed into the invoking
		-- template, or the args passed to #invoke if any exist. Otherwise
		-- assume args are being passed directly in from the debug console
		-- or from another Lua module.
		local origArgs
		if frame == mw.getCurrentFrame() then
			origArgs = frame:getParent().args
			for k, v in pairs(frame.args) do
				origArgs = frame.args
				break
			end
		else
			origArgs = frame
		end
		-- Trim whitespace and remove blank arguments.
		local args = {}
		for k, v in pairs(origArgs) do
			if type(v) == 'string' then
				v = mw.text.trim(v)
			end
			if v ~= '' then
				args[k] = v
			end
		end
		return p[funcName](processPortalArgs(args)) -- passes two tables to func: an array of portal names, and a table of named arguments.
	end
end

local funcNames = {'portal', 'image'}

for _, funcName in ipairs(funcNames) do
	p[funcName] = makeWrapper('_' .. funcName)
end

return p