Permanently protected module

Module:Portal class

From Wikisource
Jump to navigation Jump to search

require('strict')

local p = {}

local getArgs = require('Module:Arguments').getArgs
local classification = require('Module:Library of Congress Classification')._classification

function p._portal_class(args)
	local stylesheet = mw.getCurrentFrame():preprocess('<templatestyles src="Template:Portal Class/styles.css" />')
	local box_div = mw.html.create('div'):addClass('wst-portal-class')
	local in_portal_ns = (mw.title.getCurrentTitle().nsText == 'Portal')
	
	-- title
	local class_title = tostring(mw.html.create('div'):addClass('wst-portal-class-title'):wikitext('Class'))
	
	-- class
	local class_text = mw.html.create('div'):addClass('wst-portal-class-text')
	
	local main_class = ''
	if args.class == 'none' then
		main_class = 'n/a'
	elseif args.class then
		main_class = '[[Portal:' .. classification({class = args.class}) .. '|' .. string.upper(args.class) .. ']]'
	elseif in_portal_ns then
		main_class = '?'
	else
		main_class = 'n/a'
	end
	
	local subclass1 = ''
	if args.subclass1 then
		subclass1 = '[[Portal:' .. classification({class = args.class, subclass1 = args.subclass1}) .. '|' .. string.upper(args.subclass1) .. ']]'
	end
	local subclass2 = ''
	if args.subclass2 then
		subclass2 = '[[Portal:' .. classification({class = args.class, subclass1 = args.subclass1, subclass2 = args.subclass2}) .. '|' .. string.upper(args.subclass2) .. ']]'
	end
	
	class_text:wikitext(main_class .. subclass1 .. subclass2)
	class_text = tostring(class_text)
	
	-- base
	local base_text = tostring(mw.html.create('div'):addClass('wst-portal-class-base'):wikitext('[[Portal:Portals|Main]]/[[Portal:Index|Index]]'))
	
	-- categorization
	local cat = ''
	if args.class ~= 'none' and in_portal_ns then
		args.class = string.lower(args.class or 'default')
		local portal_categories = {
			a = 'General',
			b = 'Ideology',
			c = 'Historiography',
			d = 'World history',
			e = 'United States',
			f = 'American history',
			g = 'Geo-anthropology',
			h = 'Social science',
			i = 'National',
			j = 'Political science',
			k = 'Law',
			l = 'Education',
			m = 'Music',
			n = 'Art',
			p = 'Language',
			q = 'Science',
			r = 'Medicine',
			s = 'Agriculture',
			t = 'Technology',
			u = 'Military',
			v = 'Naval',
			x = 'Miscellaneous',
			z = 'Library',
			default = 'Unclassified'
		}
		cat = '[[Category:' .. (portal_categories[args.class] or portal_categories['default']) .. ' portals]]'
	end
	
	box_div:wikitext(class_title .. class_text .. base_text .. cat)
	
	return stylesheet .. tostring(box_div)
end

function p.portal_class(frame)
	return p._portal_class(getArgs(frame))
end

return p