モジュール:サンドボックス/Momijiro/debug

モジュールの解説[作成]
local p = {}

local format = mw.ustring.format
local concat = table.concat
local insert = table.insert

local roadDataModule = require("モジュール:Road data")

-- TODO transition
-- begin transition code
local subConfig = {
	county = {
		group = "county"
	},
	township = {
		sub2area = "township",
		group = "township",
		single = true,
		firstnonum = true
	}
}

local function sub_special(args, subTypeConfig, specialPrefix, subType)
	-- Find parameter.
	local subParam
	for _,p in ipairs(subTypeConfig) do
		if args[p .. 1] or args[p] then
			subParam = p
			break
		end
	end
	if not subParam then
		return args[specialPrefix .. "_special"]
	end
	local config = subConfig[subParam] or {}
	local subs = {}
	-- Retrieve wikilinks for subdivisions.
	local num = 1
	while num == 1 or args[subParam .. num] do
		-- Save parameters.
		local saved = {}
		saved[subType]= args[subType]
		args[subType] = args[subParam .. num] or num == 1 and args[subParam]
		saved.sub1dab = args.sub1dab
		args.sub1dab = args["ctdab" .. num] or args.ctdab or num == 1 and config.firstnonum and args.county
		saved.area = args.area
		args.area = not (config.nosub1dab and args.sub1dab)
			and (config.sub2area or args["area" .. num] or num == 1 and config.firstnonum and args.area)
		local locns = roadDataModule.locations(args, "jctint", true)
		-- Restore parameters.
		args[subType] = saved[subType]
		args.sub1dab = saved.sub1dab
		args.area = saved.area
		subs[num] = locns[subType]
		num = num + 1
	end
	if #subs > 1 then
		-- Construct wikitext for multiple subs.
		local textParts = {}
		insert(textParts, subs[1])
		for i = 2, #subs do
			insert(textParts, "–")
			if i % 2 ~= 0 then
				-- Odd subs after first begin a new line.
				insert(textParts, "<br>")
			end
			insert(textParts, subs[i])
		end
		local groupSuffix = config.group
		if groupSuffix then
			insert(textParts,
				format("%s%s", #subs % 2 == 0 and "<br>" or " ", groupSuffix))
		end
		if #subs == 2 then
			insert(textParts, " line")
		elseif #subs == 3 then
			insert(textParts, " tripoint")
		elseif #subs == 4 then
			insert(textParts, " quadripoint")
		else
			insert(textParts, " [[Quadripoint#Multipoints of greater numerical complexity|multipoint]]")
		end
		return concat(textParts)
	elseif #subs == 1 and config.single then
		-- TODO transition
		-- Save parameters.
		local saved = {}
		saved[subType]= args[subType]
		args[subType] = args[subParam .. 1] or args[subParam]
		saved.sub1dab = args.sub1dab
		args.sub1dab = args.ctdab1 or args.ctdab or config.firstnonum and args.county
		saved.area = args.area
		args.area = not (config.nosub1dab and args.sub1dab)
			and (config.sub2area or args.area1 or config.firstnonum and args.area)
		local locns = roadDataModule.locations(args, "jctint")
		-- Restore parameters.
		args[subType] = saved[subType]
		args.sub1dab = saved.sub1dab
		args.area = saved.area
		return locns[subType]
	end
end
-- end transition code

function p._jctint(args)
	-- Extra parameters
	args.country = "USA"
	-- TODO transition
	-- begin transition code
	args.primary_topic = "no"
	args.sub1name = args.sub1name or "County"
	args.region = mw.loadData("モジュール:Jct/statename")[args.state]
	args.sub1_special = sub_special(args, {"county"}, "county", "sub1")
	local sub2Config = {}
	if args.sub2param then
		insert(sub2Config, args.sub2param)
	end
	insert(sub2Config, "location")
	args.sub2_special = sub_special(args, sub2Config, "location", "sub2")
	-- end transition code
	args.unitdef = "mi"

	-- Tracked parameters
	local msgs = {}
	local trackedParams = {R = "length_ref", Y = "county_special", Z = "location_special"}
	for key,param  in pairs(trackedParams) do
		if args[param] then
			insert(msgs, format("[[Category:Jctint template tracking category|%s %%page%%]]", key))
		end
	end
	if args.ctdab2 and not (args.ctdab or args.ctdab1) then
		insert(msgs, format("[[Category:Jctint template tracking category|%s %%page%%]]", "D"))
	end
	if args.township2 and args.township then
		insert(msgs, format("[[Category:Jctint template tracking category|%s %%page%%]]", "T"))
	end
	local msg
	if #msgs > 0 then
		local page = mw.title.getCurrentTitle().prefixedText -- Get transcluding page's title
		msg = mw.ustring.gsub(concat(msgs), "%%page%%", page)
	end

	-- Parameters to be renamed
	local paramSubst = {
		region_special = "state_special",
		regionspan = "sspan",
		sub1 = args.sub1param or "county",
		sub1_note = "county_note",
		sub1span = "cspan",
		sub1dab = "ctdab",
		sub2 = "location",
		sub2span = "lspan",
		unit = "mile",
		unit2 = "mile2",
		unit_ref = {"mile_ref", --[[TODO transition]] "length_ref"},
		uspan = "mspan"
	}
	-- Rename parameters.
	for tgt,src in pairs(paramSubst) do
		if type(src) == "table" then
			for _,param in ipairs(src) do
				args[tgt] = args[tgt] or args[param]
				args[param] = nil
			end
		else
			args[tgt] = args[tgt] or args[src]
			args[src] = nil
		end
	end

	-- Parameters to be removed
	local nilParams = {
		"county_special", "location_special"
	}
	-- Remove parameters.
	for _,param  in ipairs(nilParams) do
		args[param] = nil
	end
	-- Parameter groups to be removed
	local nilParamPrefixes = {
		"county", "location", "township"
	}
	-- Remove parameters.
	for _,paramPrefix  in ipairs(nilParamPrefixes) do
		args[paramPrefix] = nil
		local i = 1
		while i == 1 or args[paramPrefix .. i] do
			args[paramPrefix .. i] = nil
			i = i + 1
		end
	end

	local coreModule = require("モジュール:Jctint/core")
	return coreModule._jctint(args) .. (msg or "")
end

function p.jctint(frame)
	-- Import module function to work with passed arguments
	local getArgs = require('モジュール:Arguments').getArgs
	local args = getArgs(frame)
	return p._jctint(args)
end

return p