Module:inc-ash/dial

This module needs documentation.
Please document this module by describing its purpose and usage on the documentation page.

local lang = require("Module:languages").getByCode("inc-ash")
local sub = mw.ustring.sub
local export = {}

local variety_list = {
	"Central", "East", "Northwest", "West", "South"
}

local variety_colour = {
	["Central"]		= "FAF5F0",
	["East"]		= "F0F5FA",
	["Northwest"]	= "F0FAF3",
	["West"]		= "FAF0F6",
	["South"]		= "FAF9F0",
}

local special_note = {
	-- none yet
}

function export.main(frame)
	local args = frame:getParent().args
	local pagename = mw.title.getCurrentTitle().text
	local target_page = args[1] or pagename
	local resource_page = "Module:inc-ash/dial/data/" .. target_page
	local variety_data = require("Module:inc-ash/dial/data")
	if mw.title.new(resource_page).exists then
		m_syndata = require(resource_page).list
	else
		return frame:expandTemplate{ title = "Template:inc-ash-dial/uncreated", args = { target_page } }
	end
	
	local template = {
		["Central"]		= {},
		["East"]		= {},
		["Northwest"]	= {},
		["West"]		= {},
		["South"]		= {},
	}

	main_title = mw.ustring.gsub((target_page == pagename and pagename or '[[' .. target_page .. ']]'), "[0-9%-]", "")
	text = [=[
	{| class="wikitable mw-collapsible mw-collapsed" style="margin:0; text-align:center;"
	|-
	! style="background:#FCFFFC; width:40em" colspan=3 | Dialectal forms of <b><span class="Brah" lang="inc-ash">]=] ..
		main_title .. '</span></b> (“' .. m_syndata["meaning"] .. '”) ' .. [=[
		
	|-
	! style="background:#E8ECFA" | Variety
	! style="background:#E8ECFA" | Location
	! style="background:#E8ECFA" | Forms]=] .. [=[
	<div style="float: right; clear: right; font-size:60%"><span class="plainlinks">[]=] ..
		tostring(mw.uri.fullUrl("Module:inc-ash/dial/data/" .. target_page, { ["action"] = "edit" })) ..
	' edit]</span></div>'
	
	m_syndata["meaning"] = nil
	if m_syndata["note"] then
		note = m_syndata["note"]
		m_syndata["note"] = nil
	end
	
	local categories = ""
	
	for location, synonym_set in pairs(m_syndata) do
		-- check if location is in alias list and use the proper one if so
		if variety_data['aliases'][location] ~= nil then location = variety_data['aliases'][location] end
		
		local sc = "Brah"
		if location == "Shahbazgarhi" or location == "Mansehra" then
			sc = "Khar"
		end
		if synonym_set[1] ~= "" then
			local formatted_synonyms = {}
			for i, synonym in ipairs(synonym_set) do
				local synonym_decomp = mw.text.split(synonym, ":")
				table.insert(formatted_synonyms,
					"[[" .. mw.ustring.gsub(synonym_decomp[1], "(.+)_([1-9])", "%1#Etymology %2|%1<sub>%2</sub>") .. "]]" ..
					(synonym_decomp[2] and '</span> <span style="font-size:60%"><i>' .. synonym_decomp[2] .. '</i></span>' ..
					(i < #synonym_set and '<span class="' .. sc .. '" lang="inc-ash">' or "") or ""))
			end
			local location_data = variety_data[location]
			local location_name = mw.ustring.gsub(location_data.english or location, "(%(.*%))", "<small>%1</small>")
			local location_link = location_data.link or location_name
			table.insert(template[location_data.group],
				{ location_data.order, location_name, location_link, formatted_synonyms })
		end
	end
	
	local attested = {}
	
	for _, variety in ipairs(variety_list) do
		local sc = "Brah"
		if variety == "Northwest" then
			sc = "Khar"
		end
		local colour = variety_colour[variety]
		if #template[variety] > 0 then
			table.sort(template[variety], function(first, second) return first[1] < second[1] end)
			for i, point_data in ipairs(template[variety]) do
				local result = {}
				for _, word in ipairs(point_data[4]) do
					if mw.ustring.gsub(sub(word, 3, -3), "%]%].+$", "") == pagename then
						table.insert(attested, {point_data[3], point_data[2]})
						categories = categories .. "[[Category:" .. point_data[2] .. " Ashokan Prakrit]]"
					end
					local tr = mw.ustring.gsub((lang:transliterate(sub(word, 3, -3), sc)), "%]%].+$", "")
					local add = '<span class="' .. sc .. '" lang="inc-ash">' .. word .. "</span> <small>(" .. tr
					if point_data["ts"] ~= nil then add = add .. " /" .. point_data["ts"] .. "/" end
					add = add .. ")</small>"
					table.insert(result, add)
				end
				text = text .. "\n|-"
				if i == 1 then
					text = text .. "\n!rowspan=" .. #template[variety] .. (special_note[variety] and " colspan=2" or "") .. 
					' style="background:#' .. colour .. '"| ' .. (special_note[variety] or variety)
				end
				text = text .. ((point_data[2] and not special_note[variety]) and ('\n|style="background:#' .. colour .. '"| ' .. 
					'[[w:' .. point_data[3] .. '|' .. point_data[2] .. ']]') or '') ..
					'\n|style="background:#' .. colour .. "'| " ..
					table.concat(result, ", ")
			end
		end
	end

	if note and note ~= "" then
		text = text .. '\n|-\n! style="background:#FFF7FB; padding-top:5px; padding-bottom: 5px" | ' ..
			"<small>Note</small>\n| colspan=2|<small><i>" .. note .. "</i></small>"
	end
	
	local res = "Attested at "
	table.sort(attested, function(first, second) return first[2] < second[2] end)
	for i, dialect in ipairs(attested) do
		if i == #attested and i ~= 1 then
			res = res .. " and "
		elseif i ~= 1 then
			res = res .. ", "
		end
		res = res .. '[[w:' .. dialect[1] .. '|' .. dialect[2] .. ']]'
	end
	
	return res .. '.\n' .. text .. '\n|}' .. categories
end

return export