Permanently protected module

Module:Library of Congress Classification

From Wikisource
Jump to navigation Jump to search

require('strict')

local p = {}

local getArgs = require('Module:Arguments').getArgs

local class_data = mw.loadData('Module:Library of Congress Classification/data')

function p._classification(args)
	local class = args['class']
	local subclass1 = args['subclass1']
	local subclass2 = args['subclass2']
	
	if not class then
		return class_data['default']
	end
	
	class = string.lower(class)
	if not class_data[class] then
		return class_data['default']
	elseif type(class_data[class]) == 'string' then
		return class_data[class]
	elseif not subclass1 then
		return class_data[class]['nosubclass'] or class_data['default']
	end
	
	subclass1 = string.lower(subclass1)
	if not class_data[class][subclass1] then
		subclass1 = 'default'
	end
	if not class_data[class][subclass1] then
		return class_data['default']
	elseif type(class_data[class][subclass1]) == 'string' then
		return class_data[class][subclass1]
	end
	
	if not subclass2 then
		subclass2 = class_data[class]['subclass2_default'] or 'default'
	end
	subclass2 = string.lower(subclass2)
	
	return class_data[class][subclass1][subclass2] or class_data[class][subclass1]['default'] or class_data['default']
end

function p.classification(frame)
	return p._classification(getArgs(frame))
end

return p