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 07:25, 24 December 2013 (Adding distances). 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 types = mw.loadData("Module:Road data/RJL types")

local function conversion(unit, unitdef)
	local primary = tonumber(unit) or -1
	if primary == -1 then return unit 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, root)
	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 or ''
	local region = args.region
	if regionSpan ~= '' then
		local regionCell = root.tag('td').attr('rowspan', regionSpan)
		local regionSpecial = args.region_special or ''
		local regionContent = regionSpecial ~= '' and regionSpecial or ('[[' .. region .. ']]')
		regionCell.wikitext(regionContent)
	end
	
	-- Independent Cities
	local indepCity = args.indep_city or ''
	local indepCitySpecial = args.indep_city_special or ''
	local sub1note = args.sub1_note or ''
	local sub2span = args.sub2span or '1'
	if indepCity ~= '' or indepCitySpecial ~= '' then
		local indepCityCell = root.tag('td').attr('colspan', '2').attr('rowspan', sub2span)
		local align = args.indep_city_align or ''
		align = align ~= '' and 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 or ''
	local sub1Special = args.sub1_special or ''
	local sub1name = args.sub1name or ''
	if sub1 ~= '' or sub1Special ~= '' then
		local sub1span = args.sub1span or '1'
		local sub1Cell = root.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 or ''
	local sub2Special = args.sub2_special or ''
	if (sub2 == 'none') or (sub2 == '&nbsp;') then
		root.tag('td').wikitext("&nbsp;")
	elseif sub2Special ~= '' then
		root.tag('td').attr('rowspan', sub2span).wikitext(sub2Special)
	elseif sub2 ~= '' then
		local sub2Cell = root.tag('td').attr('rowspan', sub2span)
		if not notPrimaryTopic then
			sub2Cell.wikitext('[[' .. sub2 .. ']]')
		else
			local text = {'[[', sub2}
			local area = args.area or ''
			local insert = table.insert
			if area ~= '' then
				insert(text, format(' (%s)', area))
			end
			insert(text, ", ")
			local sub1dab = args.sub1dab or ''
			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

function p.locations(args)
--[[function p.locations(frame)
	local pframe = frame:getParent()
	local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
	local args = pframe.args -- the arguments passed TO the template, in the wikitext that transcludes the template]]
	
	local root = HtmlBuilder.create()
	
	local unitary = args.unitary or ''
	if unitary ~= '' then
		local tag = root.tag('td').attr('colspan', '3').wikitext(unitary)
		local align = args.unitary_align or ''
		align = align ~= '' and align or 'left'
		tag.css('text-align', align)
	else
		locations(args, root)
	end
	return tostring(root)
end

function p.units(args)
	local root = HtmlBuilder.create()
	
	local alt_unit = args.altunit
	if alt_unit then
		local tag = root.tag('th').attr('scope', 'row').css('text-align', 'right')
		local span = args.auspan or args.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 = root.tag('th').attr('scope', 'row').css('text-align', 'right')
			local span = args.uspan or args.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 = root.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
	
	return tostring(root)
end

return p