Jump to content

Module:Jcttop/core/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m More refactoring.
m More refactoring.
Line 5: Line 5:
local concat = table.concat
local concat = table.concat
local insert = table.insert
local insert = table.insert

local roadDataModule = require("Module:Road data")


local needSub1Col = true
local needSub1Col = true
Line 10: Line 12:


local function location(args)
local function location(args)
local locns = roadDataModule.locations(args, true)
-- Region, for disambiguation
if locns.indep_city then
local region = args.region

-- Independent city
local indepCityText
if args.indep_city_special then
indepCityText = args.indep_city_special -- Overrides `indep_city` argument.
elseif args.indep_city then
local indepCity = args.indep_city
local cityLink -- Wikilink for independent city
if args.indep_city_nodab then
cityLink = format('[[%s]]', indepCity)
else
-- Specialize independent city to the region.
cityLink = format('[[%s, %s|%s]]', indepCity, region, indepCity)
end
indepCityText = "the [[Independent city|City]] of " .. cityLink
end
if indepCityText then
needSub1Col = false
needSub1Col = false
needSub2Col = false
needSub2Col = false
return indepCityText
return locns.indep_city
end
end
if locns.sub1 then needSub1Col = false end

if locns.sub2 then
-- First-level subdivision, e.g., county
needSub2Col = false
-- Name of the type of subdivision, e.g., "County" and "Parish"
if locns.sub1 then
local sub1name = args.sub1name
return format("%s, %s", locns.sub2, locns.sub1)
local sub1Text
if args.sub1_special then
sub1Text = args.sub1_special -- Overrides `sub1` argument.
elseif args.sub1 then
local sub1 = args.sub1
sub1 = sub1name and format("%s %s", sub1, sub1name) or sub1
if args.sub1_nodab then
-- Add type (if specified) to wikilink for first-level subdivision.
sub1Text = format('[[%s]]', sub1)
else
-- Specialize first-level subdivision, with type added, to the region.
sub1Text = format('[[%s, %s|%s]]', sub1, region, sub1)
end
end
return locns.sub2
end
end
return locns.sub1
if sub1Text then needSub1Col = false end

-- Second-level subdivision, e.g., city and town
local sub2Text
if args.sub2_special then
sub2Text = args.sub2_special -- Overrides `sub2` argument.
elseif args.sub2 then
local sub2 = args.sub2
if args.sub2_nodab then
sub2Text = format("[[%s]]", sub2)
else
local article
local sub2Link = {sub2}
local sub2Name = sub2
-- Type of area, e.g., city and village, as a form of disambiguation
local area = args.area
if area then
insert(sub2Link, format(' (%s)', area)) -- Add area to wikilink.
local areas = { -- table of different area types
village = "Village",
city = "City",
town = "Town",
community = "Community",
CDP = "Community",
hamlet = "Hamlet",
["unorganized territory"] = "Unorganized Territory"
}
-- Add area name to displayed wikitext.
article = "the "
sub2Name = format("%s of %s", areas[area], sub2Name)
end
insert(sub2Link, ", ")
-- Some second-level subdivisions are not unique in a given region.
-- `sub1dab` is the first-level subdivision to be used for disambiguation.
local sub1dab = args.sub1dab
if sub1dab then
insert(sub2Link, format('%s %s, ', sub1dab, sub1name))
end
insert(sub2Link, region) -- Add region to wikilink

sub2Text = format("%s[[%s|%s]]", article or "", concat(sub2Link), sub2Name)
end
end
if sub2Text then
needSub2Col = false
if sub1Text then return format("%s, %s", sub2Text, sub1Text) end
return sub2Text
end
return sub1Text
end
end



Revision as of 22:03, 10 May 2016

local p = {}

-- Shorthands for library functions
local format = mw.ustring.format
local concat = table.concat
local insert = table.insert

local roadDataModule = require("Module:Road data")

local needSub1Col = true
local needSub2Col = true

local function location(args)
	local locns = roadDataModule.locations(args, true)
	if locns.indep_city then
		needSub1Col = false
		needSub2Col = false
		return locns.indep_city
	end
	if locns.sub1 then needSub1Col = false end
	if locns.sub2 then
		needSub2Col = false
		if locns.sub1 then
			return format("%s, %s", locns.sub2, locns.sub1)
		end
		return locns.sub2
	end
	return locns.sub1
end

local function hatnote(args)
	local isFormer = args.former == "yes"
	local text = {}

	if args.region_note then
		insert(text, args.region_note)
	end

	local hatnoteArg = args.hatnote
	if hatnoteArg then
		if hatnoteArg ~= "off" then
			insert(text, hatnoteArg)
		end
	else
		local location = location(args)
		if location then
			local verb = isFormer and 'was' or 'is'
			insert(text, format("The entire %s %s in %s.%s%s",
				args.type or 'route', verb, location,
				not needSub1Col and args.sub1_ref or "",
				not needSub2Col and args.sub2_ref or ""))
		end
	end

	if args.unnum == 'yes' then
		local verb = isFormer and 'were' or 'are'
		insert(text, format("All exits %s unnumbered.", verb))
	end

	return concat(text, " ")
end

local function header(args)
	local row = mw.html.create('tr')
	local region_col = args.region_col
	if region_col then
		row:tag('th'):attr('scope', 'col')
			:wikitext(mw.language.getContentLanguage():ucfirst(region_col))
	end

	if needSub1Col and not (args.nosub1 and args.nosub1 == "yes") then
		row:tag('th'):attr('scope', 'col')
			:wikitext(args.sub1disp or args.sub1name, args.sub1_ref)
	end

	if needSub2Col then
		row:tag('th'):attr('scope', 'col')
			:wikitext(args.location_def or 'Location', args.sub2_ref)
	end

	local altunit = args.altunit
	if altunit then
		row:tag('th'):attr('scope', 'col')
			:wikitext(altunit, args.altunit_ref)
	else
		local unit = args.length or args.unit
		if unit ~= 'off' then
			row:tag('th'):attr('scope', 'col')
				:wikitext(unit, args.length_ref)
			row:tag('th'):attr('scope', 'col')
				:wikitext(args.unit2)
		end
	end

	local exit = args[1]
	if exit == 'old' then
		row:tag('th'):attr('scope', 'col')
			:wikitext(args.old_def or 'Old exit', args.old_ref)
		row:tag('th'):attr('scope', 'col')
			:wikitext(args.exit_def or 'New exit', args.exit_ref)
	elseif exit == 'exit' then
		row:tag('th'):attr('scope', 'col')
			:wikitext(args.exit_def or 'Exit', args.exit_ref)
	end

	if args[2] == 'name' then
		row:tag('th'):attr('scope', 'col')
			:wikitext(args.name_def or 'Name', args.name_ref)
	end

	row:tag('th'):attr('scope', 'col')
		:wikitext(args.dest_def or 'Destinations', args.dest_ref)

	row:tag('th'):attr('scope', 'col')
		:wikitext(args.notes_def or 'Notes', args.notes_ref)

	return '\n{| class="plainrowheaders wikitable hlist"\n' .. tostring(row)
end

---
-- Generate a hatnote and the header row for a junction-list table.
-- Accessible from other Lua modules.
function p._jcttop(args)
	return hatnote(args) .. header(args)
end

--- Entry function for {{jcttop/core}}
function p.jcttop(frame)
	-- Import module function to work with passed arguments
	local getArgs = require('Module:Arguments').getArgs
	-- Gather passed arguments into easy-to-use table
	local args = getArgs(frame)
	return p._jcttop(args)
end

return p