Jump to content

Module:Jcttop/core/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Chinissai (talk | contribs) at 13:23, 10 May 2016 (Replaced primary_topic with fine-grained counterparts.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

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

local needSub1Col = true
local needSub2Col = true

local function location(args)
	-- Region, for disambiguation
	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
		needSub2Col = false
		return indepCityText
	end

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

	-- 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 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.
				sub2Name = format("the %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]]", concat(sub2Link), sub2Name)
		end
	end
	if sub2Text then
		needSub2Col = false
		return format("%s, %s", sub2Text, sub1Text) 
	end
	return sub1Text
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,
				args.sub1_ref or "",
				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