Jump to content

Module:Jcon/documentation: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Add pre and post
Update
 
Line 2: Line 2:


local p = {}
local p = {}

local data = mw.loadData('Module:Jcon/data')
local data = mw.loadData('Module:Jcon/data')
local roadData = require('Module:Road data/strings/CAN/ON')
local parser = require('Module:Road data/parser').parser
local TableTools = require('Module:TableTools')


-- Generates a list of supported regions
-- Generates a list of supported regions
Line 9: Line 13:
local entries = {}
local entries = {}
local pre = 'Note: All inputs are converted to lowercase by the template and "Region of", "County Road", etc. are striped from the input.'
local pre = 'Note: All inputs are converted to lowercase by the template and "Region of", "County Road", etc. are striped from the input.'
local post = 'Table produced by [[Module:Jcon/documentation]] with data from [[Module:Jcon/data]] ('
local post = 'Table produced by [[Module:Jcon/documentation]] with data from [[Module:Road data/strings/CAN/ON]] ('
.. frame:expandTemplate{ title = 'edit', args = { 'Module:Jcon/data' } } .. ').'
.. frame:expandTemplate{ title = 'edit', args = { 'Module:Road data/strings/CAN/ON' } } .. ').'
local tableEl = mw.html.create('table'):addClass('wikitable') -- Create output table element
local tableEl = mw.html.create('table'):addClass('wikitable') -- Create output table element
local headerRow = tableEl:tag('tr')
local headerRow = tableEl:tag('tr')
headerRow:tag('th'):wikitext('Name')
headerRow:tag('th'):wikitext('Type')
headerRow:tag('th'):wikitext('Aliases')
headerRow:tag('th'):wikitext('Route name')
headerRow:tag('th'):wikitext('Prefix')
for name, info in pairs(roadData) do

if info.alias then
for alias, name in pairs(data.aliases) do -- Reverse the alias table to allow lookup by name
roadData[name] = mw.loadData('Module:Road data/strings/' .. info.alias.module)[info.alias.type]
if not reverseAliases[name] then
reverseAliases[name] = {}
end
end
table.insert(reverseAliases[name], alias)
end
end
local keys = TableTools.keysToList(roadData, function (a, b)
return string.lower(a) < string.lower(b)
end)
local valueToGroup = {}
local groups = {}


for name, info in pairs(data.types) do -- Create tables for each region
for _, name in ipairs(keys) do
if string.sub(name, 1, 1) ~= ' ' then
local aliases = ''
local info = roadData[name]
if reverseAliases[name] then
if valueToGroup[info] then
for _, alias in ipairs(reverseAliases[name]) do
table.insert(groups[valueToGroup[info]], name)
aliases = aliases .. '\n* ' .. alias -- Add alias to list item
else
table.insert(groups, { name })
valueToGroup[info] = #groups
end
end
end
end
local row = tableEl:tag('tr')
row:tag('td'):wikitext(name)
row:tag('td'):wikitext(aliases)
row:tag('td'):wikitext(info.prefix .. ' ' .. info.type)
end
end
for name, group in ipairs(groups) do
local info = roadData[group[1]]
local typeOut = ''
for sign, fileName in pairs(data.signs) do
for _, type in ipairs(group) do
typeOut = typeOut .. '<code>' .. type .. '</code><br>'
local aliases = ''
if reverseAliases[sign] then
for _, alias in ipairs(reverseAliases[sign]) do
aliases = aliases .. '\n* ' .. alias -- Add alias to list item
end
end
end
local row = tableEl:tag('tr')
row:tag('td'):wikitext(typeOut)
row:tag('td'):wikitext(
'<code>' .. (type(info.name) == 'table'
and (info.name.default or info.name[0] or info.name.below or '??')
or info.name) .. '</code>'
)
end
for sign, fileName in pairs(data._signs) do
local row = tableEl:tag('tr')
local row = tableEl:tag('tr')
row:tag('td'):wikitext(sign)
row:tag('td'):wikitext(sign)
row:tag('td'):wikitext(aliases)
row:tag('td'):wikitext('[[File:' .. fileName .. '|20px]]')
row:tag('td'):wikitext('[[File:' .. fileName .. '|20px]]')
end
end

Latest revision as of 10:25, 24 August 2024

require('strict')

local p = {}

local data = mw.loadData('Module:Jcon/data')
local roadData = require('Module:Road data/strings/CAN/ON')
local parser = require('Module:Road data/parser').parser
local TableTools = require('Module:TableTools')

-- Generates a list of supported regions
function p.supported(frame)
	local reverseAliases = {}
	local entries = {}
	local pre = 'Note: All inputs are converted to lowercase by the template and "Region of", "County Road", etc. are striped from the input.'
	local post = 'Table produced by [[Module:Jcon/documentation]] with data from [[Module:Road data/strings/CAN/ON]] ('
		.. frame:expandTemplate{ title = 'edit', args = { 'Module:Road data/strings/CAN/ON' } } .. ').'
	local tableEl = mw.html.create('table'):addClass('wikitable') -- Create output table element
	
	local headerRow = tableEl:tag('tr')
	headerRow:tag('th'):wikitext('Type')
	headerRow:tag('th'):wikitext('Route name')
	
	for name, info in pairs(roadData) do
		if info.alias then
			roadData[name] = mw.loadData('Module:Road data/strings/' .. info.alias.module)[info.alias.type]
		end
	end
	
	local keys = TableTools.keysToList(roadData, function (a, b)
		return string.lower(a) < string.lower(b)
	end)
	
	local valueToGroup = {}
	local groups = {}

	for _, name in ipairs(keys) do
		if string.sub(name, 1, 1) ~= ' ' then
			local info = roadData[name]
			
			if valueToGroup[info] then
				table.insert(groups[valueToGroup[info]], name)
			else
				table.insert(groups, { name })
				valueToGroup[info] = #groups
			end
		end
	end
	
	for name, group in ipairs(groups) do
		local info = roadData[group[1]]
		local typeOut = ''
		
		for _, type in ipairs(group) do
			typeOut = typeOut .. '<code>' .. type .. '</code><br>'
		end
		
		local row = tableEl:tag('tr')
		row:tag('td'):wikitext(typeOut)
		row:tag('td'):wikitext(
			'<code>' .. (type(info.name) == 'table'
				and (info.name.default or info.name[0] or info.name.below or '??')
				or info.name) .. '</code>'
		)
	end
		
	for sign, fileName in pairs(data._signs) do
		local row = tableEl:tag('tr')
		row:tag('td'):wikitext(sign)
		row:tag('td'):wikitext('[[File:' .. fileName .. '|20px]]')
	end

	return pre .. tostring(tableEl) .. post
end

return p