Jump to content

Module:Jctint

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Happy5214 (talk | contribs) at 12:57, 24 December 2013 (Use global row in units function). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

local format = mw.ustring.format
local HtmlBuilder = require "Module:HtmlBuilder"
local getArgs = require('Module:Arguments').getArgs

local types = mw.loadData("Module:Road data/RJL types")

local row
local jspan

local function conversion(unit, unitdef)
	local primary = unit and tonumber(unit) or -1
	if not primary then
		local page = mw.title.getCurrentTitle()
		local pagename = page.prefixedText
		local category = format("[[Category:Jctint template using non-numeric parameter values|# %s]]", pagename)
		return category .. unit
	elseif primary == -1 then
		return nil
	end
	local math = require "Module:Math"
	local precision = math._precision
	local round = math._precision_format
	local converted
	if unitdef == 'mi' then
		converted = primary * 1.609344
	else
		converted = primary / 1.609344
	end
	local prec = precision(unit)
	if prec < 0 then prec = 0 end
	return round(converted, prec)
end

local function locations(args)
	local unitary = args.unitary
	if unitary then
		local tag = row.tag('td').attr('colspan', '3').wikitext(unitary)
		local align = args.unitary_align or 'left'
		tag.css('text-align', align)
	else
		local areas = {village = "Village", city = "City", town = "Town", community = "Community", CDP = "Community", hamlet = "Hamlet", ["unorganized territory"] = "Unorganized Territory"}
		
		local notPrimaryTopic = args.primary_topic == 'no'
		
		-- Regions
		local regionSpan = args.regionspan
		local region = args.region
		if regionSpan then
			local regionCell = row.tag('td').attr('rowspan', regionSpan)
			local regionSpecial = args.region_special or ('[[' .. region .. ']]')
			regionCell.wikitext(regionSpecial)
		end
		
		-- Independent Cities
		local indepCity = args.indep_city
		local indepCitySpecial = args.indep_city_special
		local sub1note = args.sub1_note
		local sub2span = args.sub2span or '1'
		if indepCity or indepCitySpecial then
			local indepCityCell = row.tag('td').attr('colspan', '2').attr('rowspan', sub2span)
			local align = args.indep_city_align or 'left'
			indepCityCell.css('text-align', align)
			if indepCitySpecial then
				indepCityCell.wikitext(indepCitySpecial)
			elseif notPrimaryTopic then
				local text = format('[[Independent city|City]] of [[%s, %s|%s]]', indepCity, region, indepCity)
				indepCityCell.wikitext(text)
			else
				local text = format('[[Independent city|City]] of [[%s]]', indepCity)
				indepCityCell.wikitext(text)
			end
			if sub1note then
				indepCityCell.tag('br', {selfClosing = true})
				indepCityCell.tag('small').wikitext(sub1note)
			end
		end
		
		-- First-level Subdivisions
		local sub1 = args.sub1
		local sub1Special = args.sub1_special
		local sub1name = args.sub1name
		if sub1 or sub1Special then
			local sub1span = args.sub1span or '1'
			local sub1Cell = row.tag('td').attr('rowspan', sub1span)
			if sub1Special then
				sub1Cell.wikitext(sub1Special)
			elseif notPrimaryTopic then
				local text = format('[[%s %s, %s|%s]]', sub1, sub1name, region, sub1)
				sub1Cell.wikitext(text)
			else
				local text = '[[' .. sub1
				if sub1name then
					text = text .. format(' %s|%s', sub1name, sub1)
				end
				sub1Cell.wikitext(text .. ']]')
			end
			if sub1note then
				sub1Cell.tag('br', {selfClosing = true})
				sub1Cell.tag('small').wikitext(sub1note)
			end
		end
		
		-- Second-level Subdivisions
		local sub2 = args.sub2
		local sub2Special = args.sub2_special
		if (sub2 == 'none') or (sub2 == '&nbsp;') then
			row.tag('td').wikitext("&nbsp;")
		elseif sub2Special then
			row.tag('td').attr('rowspan', sub2span).wikitext(sub2Special)
		elseif sub2 then
			local sub2Cell = row.tag('td').attr('rowspan', sub2span)
			if not notPrimaryTopic then
				sub2Cell.wikitext('[[' .. sub2 .. ']]')
			else
				local text = {'[[', sub2}
				local area = args.area
				local insert = table.insert
				if area then
					insert(text, format(' (%s)', area))
				end
				insert(text, ", ")
				local sub1dab = args.sub1dab
				if sub1dab then
					insert(text, format('%s %s, ', sub1dab, sub1name))
				end
				insert(text, region .. '|')
				if area then
					local linktext = areas[area]
					insert(text, linktext .. ' of ')
				end
				insert(text, sub2 .. ']]')
				sub2Cell.wikitext(table.concat(text))
			end
		end
	end
end

function units(args)
	local alt_unit = args.altunit
	if alt_unit then
		local tag = row.tag('th').attr('scope', 'row').css('text-align', 'right')
		local span = args.auspan or jspan or '1'
		tag.attr('rowspan', span)
		local type = args.type
		if type then tag.attr('title', types[type].jctint) end
		tag.wikitext(alt_unit)
	else
		local unit = args.unit
		if unit ~= 'none' then
			local primary = row.tag('th').attr('scope', 'row').css('text-align', 'right')
			local span = args.uspan or jspan or '1'
			primary.attr('rowspan', span)
			local type = args.type
			local title
			if type then
				title = types[type].jctint
				primary.attr('title', title)
			end
			
			local secondary = row.tag('td').css('text-align', 'right').css('background-color', '#f2f2f2').attr('rowspan', span)
			if title then secondary.attr('title', title) end
			
			local unitdef = args.unitdef
			primary.wikitext(unit)
			secondary.wikitext(conversion(unit, unitdef))
			
			local unit2 = args.unit2
			if unit2 then
				local line = args.line
				if line then
					primary.tag('hr', {selfClosing = true})
					secondary.tag('hr', {selfClosing = true})
				else
					primary.wikitext('–').tag('br', {selfClosing = true})
					secondary.wikitext('–').tag('br', {selfClosing = true})
				end
				primary.wikitext(unit2)
				secondary.wikitext(conversion(unit2, unitdef))
			end
			
			local unit_ref = args.unit_ref
			if unit_ref then primary.wikitext(unit_ref) end
		end
	end
end

function place(args)
	local tag = row.tag('td').css('text-align', 'center')
	local pspan = args.pspan or jspan or '1'
	tag.attr('rowspan', pspan)
	
	local colspan
	local exit = args[1]
	local named = args[2]
	if exit == 'exit' then
		if named == 'name' then
			colspan = '4'
		else
			colspan = '3'
		end
	elseif exit == 'old' then
		if named == 'name' then
			colspan = '5'
		else
			colspan = '4'
		end
	else
		colspan = '2'
	end
	tag.attr('colspan', colspan)
	
	local type = args.type
	if type then
		local typeData = types[type]
		tag.attr('title', typeData.jctint).css('background-color', typeData.color)
	end
	
	tag.wikitext(args.place)
end

function exits(args)
	local exit = args[1]
	local named = args[2]
	
	local type = args.type
	local title, color
	if type then
		local typeData = types[type]
		title = typeData.jctint
		color = typeData.color
	end
	
	if exit == 'old' then
		local oldTag = row.tag('td').css('text-align', 'center').css('background-color', '#d3d3d3').attr('title', 'Former exit number')
		local ospan = args.ospan or jspan or '1'
		oldTag.attr('rowspan', ospan).wikitext(args.old)
	end
	
	if exit == 'exit' or exit == 'old' then
		local exitTag = row.tag('td').css('text-align', 'center')
		local espan = args.espan or jspan or '1'
		exitTag.attr('rowspan', espan)
		if type then
			exitTag.attr('title', title).css('background-color', color)
		end
		exitTag.wikitext(args.exit)
	end
	
	if named == 'name' then
		local nameTag = row.tag('td')
		local namespan = args.namespan or jspan or '1'
		nameTag.attr('rowspan', namespan)
		if type then
			nameTag.attr('title', title).css('background-color', color)
		end
		nameTag.wikitext(args.name)
	end
end

function destinations(args)	
	local destTag = row.tag('td')
	local rcspan = args.rcspan or '1'
	destTag.attr('colspan', rcspan)
	local rspan = args.rspan or jspan or '1'
	destTag.attr('rowspan', rspan)
	local type = args.type
	local title, color
	if type then
		local typeData = types[type]
		title = typeData.jctint
		color = typeData.color
		destTag.attr('title', title).css('background-color', color)
	end
	
	destTag.wikitext(args.road)
	
	local notes = args.notes
	if notes ~= 'none' then
		local notesTag = row.tag('td')
		local nspan = args.nspan or jspan or '1'
		notesTag.attr('rowspan', nspan)
		if type then
			notesTag.attr('title', title).css('background-color', color)
		end
		notesTag.wikitext(notes)
	end
end

function p.jctint(frame)
	local args = getArgs(frame)
	jspan = args.jspan
	local root = HtmlBuilder.create()
	row = root.tag('tr').css('text-align', 'left')
	
	locations(args)
	units(args)
	if args.place then
		place(args)
	else
		exits(args)
		destinations(args)
	end
	
	return tostring(root)
end

return p