Module:Sandbox/User:Choppe

From RuneScape Classic Wiki
Jump to: navigation, search

Documentation for this module may be created at Module:Sandbox/User:Choppe/doc

local p = {}

local paramTest = require('Module:Paramtest')
local topIcons = require('Module:Top icons')._main

local lang = mw.getContentLanguage()
local title = mw.title.getCurrentTitle()

-- Convert month names to hex for sortkeys
local HexMonthConv = {
	January = '1',
	February = '2',
	March = '3',
	April = '4',
	May = '5',
	June = '6',
	July = '7',
	August = '8',
	September = '9',
	October = 'A',
	November = 'B',
	December = 'C',
}

-- given d,m,y, return the relevant time-based categories (with sorting)
function getDateCategories(day, month, year)
	local ret = {}
	local hexMonth, dayZero
		
	if((not day) or (not month) or (not HexMonthConv[month]) or (not year)) then
		table.insert(ret, '[[Category:Missing update date]]')
	else
		dayZero = ((tonumber(day) < 10 and '0' .. day) or day)
		hexMonth = HexMonthConv[month]
		-- [[Category:28 November updates]], sorted by year (eg 2015)
		table.insert(ret, string.format('[[Category:%s %s updates|%s%s%s]]', day, month, year, hexMonth, dayZero))
		
		-- [[Category:2015 updates]], sorted by monthday, months converted to hex, eg B28 (28 November)
		table.insert(ret, string.format('[[Category:%s updates|%s%s]]', year, hexMonth, dayZero))
	
		-- [[Category:Updates by date]], sorted year month day
		table.insert(ret, string.format('[[Category:Updates by date|%s%s%s]]', year, hexMonth, dayZero))
	end

	local indicators = {}
	hexMonthNum = tonumber(HexMonthConv[month], 16)
	-- If it is prior to OSRS's intial version date
	if(tonumber(year) < 2008 ) then
		table.insert(indicators, 'os')
	end
	table.insert(indicators, 'rs')
	
	table.insert(ret, topIcons(indicators))
	
	return table.concat(ret)
end

-- [[Template:Update]]
function p.update(frame)
	local args = frame:getParent().args

	local div = mw.html.create('div')
		:css({['display'] = 'table', ['max-width'] = '650px', ['border-radius'] = '3px', ['border'] = '1px solid grey', ['margin'] = 'auto', ['margin-bottom'] = '10px', ['margin-top'] = '10px', ['background'] = '#F5F5F5', ['text-align'] = 'center', ['padding'] = '5px', ['padding-left'] = '10px', ['padding-right'] = '10px' })
		--:addClass('official update')
		:done()
	
	local day, month, year, dateLink
	if(args.date ~= nil) then
		day = lang:formatDate('j', args.date)
		month = lang:formatDate('F', args.date)
		year = lang:formatDate('Y', args.date)
		dateLink = '[[' .. day .. ' ' .. month .. ']] [[' .. year .. ']]'
	else
		dateLink = '(missing date)'
	end
	
	local message = 'This official news post is copyrighted by [[Jagex]]. It is copied verbatim from the '
	local website = '[http://www.runescape.com/community \'\'RuneScape\'\' website]'
	
	div:wikitext(message)
		:tag('span')
			:addClass('plainlinks')
			:wikitext(website)
		:done()
		:tag('br'):done()
		:wikitext('This update dates to ' .. dateLink .. '.')
	:done()

	-- Add categories only in update namespace
	local cat = (title.namespace == 112) and '[[Category:Updates]]' .. getDateCategories(day, month, year) or ''

	return '__NOTOC__ __NOEDITSECTION__' .. tostring(div) .. cat
end


-- CATEGORY FORMATTERS

-- [[Category:28 November updates]] etc
function p.dateCat(frame)
	local frame = frame:getParent()
	local date = paramTest.has_content(frame.args[1]) and frame.args[1] or string.gsub(title.text, ' ?updates?', '') .. ' 2000'

	local ret = 'This category contains updates posted on ' .. lang:formatDate('[[j F]]', date) .. ', sorted chronologically.'
	-- cats only in category namespace
	if(title.namespace == 14) then
		--updates by day, sorted by hexmonth,day: B28 (28 November)
		ret = ret .. '[[Category:Updates by day|' .. HexMonthConv[lang:formatDate('F', date)] .. lang:formatDate('d', date) .. ']]'
		--updates by month, sorted by [space]day
		ret = ret .. '[[Category:' .. lang:formatDate('F', date) .. ' updates| ' .. lang:formatDate('d', date) .. ']]'
	end
	
	return ret
end

-- [[Category:2015 updates]] etc
function p.yearCat(frame)
	local frame = frame:getParent()
	local date = paramTest.has_content(frame.args[1]) and frame.args[1] or string.gsub(title.text, ' ?updates?', '')

	local ret = 'This category contains updates posted in [[' .. date .. ']], sorted chronologically.'
	-- cats only in category namespace
	if(title.namespace == 14) then
		--updates by year, sorted by [space]year
		ret = ret .. '[[Category:Updates by year| ' .. date .. ']]'
	end
	
	return ret
end

-- DPL HANDLERS
function p.datedpl(frame)
	local args = frame:getParent().args
	local year = (args.year ~= nil) and args.year or lang:formatDate('Y', args.date)
	-- if it has category, {{Update}}, missing both probably means {{Patch Notes}}
	local cat = paramTest.has_content(args.category) and category_info[cat_switch[string.gsub(string.lower(a.category), ' ?updates?', '')]][2] or 'Patch Notes'
	local page = args['%TITLE%']

	return string.format("'''%s''' – %s: [[Update:%s|%s]]", year, cat, page, page)
end

return p