Module:fi-dialects/template/map


local export = {}
local m_dial = require("Module:fi-dialects")
local m_map = require("Module:fi-dialects/map")
local m_common = require("Module:fi-dialects/template/common")

local map_size = "1200px"

local dots = {
	"FF150F", "0D8AFF", "59FF0D", "FF0FF1", "E4FF10", 
	"10C7FF", "8210FF", "FF8E0A", "07FFD1", "380DFF", 
	"874F4E", "4C7183", "62814D", "804C7A", "78814D", 
	"4D8178", "694D86", "826949", "57844F", "574D82",
	"FF4E45", "4ABBFF", "8DFF49", "FF42F9", "DAFF4C", 
	"4AFFEA", "9F48FF", "FFB14C", "68FF4C", "6549FF", 
	"783017", "164F73", "307313", "7E187D", "5D7612", 
	"167364", "411777", "734E16", "227416", "31167A", 
}

-- go through synonyms, assign colors to each term, and compile lists to syns.
local function visit(syns, visited, parish, ...)
	local terms = {...}
	if parish then syns[parish] = terms end
	for _, term_w in ipairs(terms) do
		local term = m_common.extract_text(term_w)
		if not visited[term] then
			local next_index = #visited + 1
			table.insert(visited, term)
			visited[term] = dots[next_index]
		end
	end
end

-- makes a "chip" used for the color legend.
local function make_chip(text, color, qualifier)
	if qualifier then text = text .. " (" .. qualifier .. ")" end
	return '<span class="color" style="white-space:nowrap;margin:4px;padding:4px;border:1px solid #' .. color .. ';border-left:2em solid #' .. color .. ';line-height:2.5em">' .. text .. "</span>"
end

-- same as make_chip, but tag "term".
local function make_chip_tag(text, color, qualifier)
	if qualifier then text = text .. " (" .. qualifier .. ")" end
	return '<span class="color Latn" lang="fi" style="white-space:nowrap;margin:4px;padding:4px;border:1px solid #' .. color .. ';border-left:2em solid #' .. color .. ';line-height:2.5em">' .. text .. "</span>"
end

-- same as make_chip, but link "term".
local function make_chip_link(term, color, qualifier)
	return make_chip(m_common.link(term), color, qualifier)
end

local function make_wiki_link(target, alt, html)
	if not target then return html end
	return "[[w:" .. target .. "|" .. tostring(mw.html.create("span"):attr("title", alt):wikitext(html)) .. "]]"
end

local function make_formatted_link(term, alt, html)
	return tostring(mw.html.create("span"):attr("title", alt):wikitext("[[" .. term .. "#Finnish|" .. html .. "]]"))
end

local function format_color(colors)
	if type(colors) == "string" then return colors end
	if #colors == 1 then return colors[1] end

	-- create "pie chart"
	local sector = 1 / #colors
	local result = nil

	for factor, color in ipairs(colors) do
		result = (result and result .. "," or "") .. color .. " " .. (sector * (factor - 1)) .. "turn " .. (sector * factor) .. "turn"
	end

	return "conic-gradient(" .. result .. ")"
end

local function synonym_map(frame, term, word_id, data, is_feature)
	local synonyms = data[is_feature and "data" or "syns"]
	local source = data.source and (type(data.source) == "table" and data.source or { data.source }) or { }
	
	local parishes = {}
	local syns = {}
	local colors = {}
	
	local has_custom_order = is_feature and data.label_order
	
	if has_custom_order then
		for _, label in ipairs(data.label_order) do
			visit(syns, colors, nil, label)
		end
	end
	
	-- gather parishes and their terms
	for parish, terms in pairs(synonyms) do
		if not m_common.special[parish] then
			local result = m_dial.getParish(parish, true)
			if result then table.insert(parishes, result) end
	
			-- assign each term a color
			if type(terms) == "string" then
				visit(syns, colors, parish, terms)
			else
				-- loadData breaks unpack, we must make a copy
				local terms_copy = {}
				for i, term in ipairs(terms) do terms_copy[i] = term end
				visit(syns, colors, parish, unpack(terms_copy))
			end
		end
	end
	
	if not has_custom_order then
		-- sort and make chips
		table.sort(colors)
	end
	
	local qualifiers = data.qualifiers or { }
	local chips = { }
	for _, term in ipairs(colors) do
		local chip
		if is_feature or data.semantic then
			chip = make_chip(data.labels[term] or term, colors[term], qualifiers[term])
		elseif data.nolink then
			chip = make_chip_tag(term, colors[term], qualifiers[term])
		else
			chip = make_chip_link(term, colors[term], qualifiers[term])	
		end
		table.insert(chips, chip)
	end
	chips = table.concat(chips, " ")
	
	-- complicated peg
	local function render_dot(parish, top, left)
		local terms = syns[parish:getCode()]
		local term_texts = {}
		if is_feature then
			for i, term in ipairs(terms) do term_texts[i] = data.labels[term] or term end
		else
			for i, term in ipairs(terms) do term_texts[i] = m_common.extract_text(term) end
		end
		local alt = parish:getFormattedName() .. ', ' .. parish:getArea():getFormattedName() .. ':\n' .. table.concat(term_texts, is_feature and "; " or ", ")

		local outer = mw.html.create('div')
					:attr('class', 'dot_outer')
					:css('position', 'absolute')
					:css('top', top)		-- positioning
					:css('left', left)
					:tag('div')
						:css('position', 'relative')
						:css('left', '-4px')		-- center (8px / 2 = 4px)
						:css('top', '-4px')
						:css('width', '8px')
						:css('height', '8px')
						:attr('title', alt)

		local color = '#' .. colors[is_feature and terms[1] or m_common.extract_text(terms[1])]
		if #terms > 1 then
			color = { color }
			for i = 2, #terms do
				table.insert(color, '#' .. colors[is_feature and terms[i] or m_common.extract_text(terms[i])])
			end
		end

		local dot = outer:tag('span')
				:css('width', '8px')
				:css('height', '8px')
				:css('border-radius', '50%')
				:css('user-select', 'none')
				:css('display', 'inline-block')
				:css('background', format_color(color))
				:css('border', '0.5px solid rgba(0,0,0,0.25)')
				:wikitext('&nbsp;')
		if is_feature or #terms > 1 or data.nolink or data.semantic then return tostring(dot:allDone()) end
		return make_formatted_link(terms[1], alt, tostring(dot:allDone()))
	end
	
	local heading
	if is_feature then
		heading = data.title or 'Feature map'
	elseif data.semantic then
		heading = 'Dialectal meanings for ' .. m_common.mention(term, data.gloss, data.usage)
	else
		heading = 'Dialectal synonyms for ' .. m_common.mention(term, data.gloss, data.usage)
	end
	
	local note
	if not is_feature and synonyms.common then
		note = "''" .. 'The most commonly found form in dialects is ' .. m_common.mention(synonyms.common) .. '. The map below might not show all parishes where this form is attested.' .. "''"
	end
	
	return '<div style="float:right;"><small>([[Module:fi-dialects/data/' .. (is_feature and 'feature' or 'word') .. '/' .. word_id .. '|edit data]])</small></div>' ..
			"<p>''" .. m_common.disclaimer .. "''</p>" ..
			'<div style="float:right;"><small>([[commons:File:Finnish dialect location map.svg|background image]])</small></div>' ..
			m_common.format_sources(source) .. '\n\n' ..
			'== ' .. heading .. " ==\n" .. (note and note .. "\n" or "") .. [[
{| style="width:100%;" cellspacing="3" cellpadding="5"
|-
| align="center" |
<div style="display:inline-block;">

<p>]] .. chips .. [[</p>

]] .. m_map.show{frame = frame, parishes = parishes, peg = render_dot, size = map_size} .. [[

</div>
|}]] .. "\n" .. (is_feature and "" or ("[[Category:Finnish dialect maps|" .. word_id .. "]]"))
end

local function west_east_map(frame)
	local fallback_colors = { ["west"] = "1192a6", ["east"] = "8a06a1" }

	local branch_labels = { ["west"] = "Western Finnish", ["east"] = "Eastern Finnish" }

	local parish_data = mw.loadData("Module:fi-dialects/data/parish").parishes
	
	local west_chips = {}
	local east_chips = {}
	
	local display_groups = frame.args["groups"]

	local palette

	if display_groups then
		local group_colors = {
			-- west (cold colors)
			["Southwest"] = "0080ff",
			["SouthwestTransitional"] = "00d0ff",
			["Tavastia"] = "1cff68",
			["SouthOstrobothnia"] = "2a00fc", 
			["NorthOstrobothnia"] = "5193fc",
			["Lapland"] = "b3d2ff",
			
			-- east (warm colors)
			["Savonia"] = "ffa200",
			["Southeast"] = "e81f00",
		}

		local group_keys = {
			"Southwest", "SouthwestTransitional", "Tavastia",
			"SouthOstrobothnia", "NorthOstrobothnia", "Lapland",
			"Savonia", "Southeast",
		}

		for _, group_code in ipairs(group_keys) do
			local group = m_dial.getGroup(group_code)
			local chips = group:getBranch() == "east" and east_chips or west_chips
			table.insert(chips, make_chip(group:getFormattedName(), group_colors[group_code] or fallback_colors[group:getBranch()]))
		end

		palette = group_colors
	else
		local area_colors = {
			-- west (cold colors)
			["Länsi-Pohja"] = "7682cf",
			["Peräpohjola"] = "b3d2ff",
			["Pohjanmaa/Pohjoinen"] = "448bfc", 
			["Pohjanmaa/Keski"] = "095fe8",
			["Pohjanmaa/Etelä"] = "043eb3",
			["Satakunta/Länsi"] = "6be1f2", 
			["Satakunta/Pohjoinen"] = "09bd87",
			["Satakunta/Etelä"] = "2898a8",
			["Varsinais-Suomi/Etelä"] = "448bfc",
			["Varsinais-Suomi/Itä"] = "0677bf",
			["Varsinais-Suomi/Pohjoinen"] = "40aff5", 
			["Varsinais-Suomi/Ylämaa"] = "07c5e0", 
			["Häme/Pohjoinen"] = "52eb82", 
			["Häme/Etelä"] = "21d133",
			["Häme/Kaakko"] = "089908",
			["Kymenlaakso"] = "9acf29",
			
			-- east (warm colors)
			["Keski-Suomi/Pohjoinen"] = "e6c053",
			["Keski-Suomi/Länsi"] = "d1a62a",
			["Keski-Suomi/Etelä"] = "b88c0f",
			["Kainuu"] = "e0de92",
			["Savo/Pohjoinen"] = "e8913a",
			["Savo/Etelä"] = "c26c15",
			["Karjala/Pohjoinen"] = "fc7b12",
			["Karjala/Keski"] = "f2272d",
			["Karjala/Etelä"] = "b81102",
			["Inkeri"] = "9c3b68",
			["Vermlanti"] = "fcf40d",
		}

		local area_colors_sorted = {}
	
		for k, _ in pairs(area_colors) do
			table.insert(area_colors_sorted, k)
		end
	
		table.sort(area_colors_sorted, function (a_code, b_code) 
			local a = m_dial.getArea(a_code)
			local b = m_dial.getArea(b_code)
	
			local a_key = a:getEnglishName()
			local b_key = b:getEnglishName()
			
			-- make sure larger areas like Tavastia stay together
			if a:getSuperArea() then
				a_key = a:getSuperArea():getEnglishName() .. "/" .. a_key
			end
			if b:getSuperArea() then
				b_key = b:getSuperArea():getEnglishName() .. "/" .. b_key
			end
			
			return a_key < b_key
		end)

		for _, area_code in ipairs(area_colors_sorted) do
			local area = m_dial.getArea(area_code)
			local chips = area:getBranch() == "east" and east_chips or west_chips
			table.insert(chips, make_chip(area:getFormattedName(), area_colors[area_code] or fallback_colors[area:getBranch()]))
		end

		palette = area_colors
	end

	west_chips = table.concat(west_chips, " ")
	east_chips = table.concat(east_chips, " ")
	
	local parishes = {}
	for k, v in pairs(parish_data) do
		table.insert(parishes, m_dial.getParish(k))
	end
	
	local function color_peg(parish, top, left)
		local area = parish:getArea()
		local group = area:getGroup()
		local branch = group:getBranch()
		local color = palette[display_groups and group:getCode() or area:getCode()]
		local alt = parish:getFormattedName() .. ",\n" .. area:getFormattedName() .. ",\n" .. group:getFormattedName() .. ",\n" .. branch_labels[branch]

		color = color or fallback_colors[branch]

		return make_wiki_link(parish:getWikipediaArticle(true), alt, tostring(mw.html.create('div')
					:attr('class', 'dot_outer')
					:css('position', 'absolute')
					:css('top', top)		-- positioning
					:css('left', left)
					:tag('div')
						:css('position', 'relative')
						:css('left', '-4px')		-- center (8px / 2 = 4px)
						:css('top', '-4px')
						:css('width', '8px')
						:css('height', '8px')
						:attr('title', alt)
						:tag('span')
							:css('width', '8px')
							:css('height', '8px')
							:css('border-radius', '50%')
							:css('user-select', 'none')
							:css('display', 'inline-block')
							:css('border', '0.5px solid rgba(0,0,0,0.25)')
							:css('background', '#' .. color)
							:wikitext('&nbsp;')
							:done()
						:done()))
	end
	
	return '<div style="float:right;"><small>([[Module:fi-dialects/data/parish|edit data]])</small></div>' .. 
			"<p>''" .. m_common.disclaimer .. "''</p>\n\n" ..
			'<p>Each spot on the map is a parish, with some minor exceptions; see [[Appendix:Finnish dialects]].</p>\n\n' ..
			'<small>Sources: Data of parishes and their areas is based on data from [https://kaino.kotus.fi/sms/ Suomen murteiden sanakirja] © Kotimaisten kielten keskus, under the CC BY 4.0 license. Location data is partially extracted from [https://www.openstreetmap.org/ OpenStreetMap] © OpenStreetMap contributors, under the Open Database license. See the information for the [[commons:File:Finnish dialect location map.svg#Summary|background image]] for its sources.</small>\n' ..
			'== Map of Finnish dialects ==\n' .. [[
{| style="width:100%;" cellspacing="3" cellpadding="5"
|-
| align="center" |
<div style="display:inline-block;">

<p>'''Western Finnish''': ]] .. west_chips .. [[</p>

<p>'''Eastern Finnish''': ]] .. east_chips .. [[</p>

<div style="display:inline-block;">

]] .. m_map.show{frame = frame, parishes = parishes, peg = color_peg, size = map_size} .. [[

</div></div>
|}]] .. "\n[[Category:Finnish dialect maps| ]]"
end

function export.show_map(frame)
	local word_id
	local title_text = mw.title.getCurrentTitle().text
	local is_feature = false
	if mw.title.getCurrentTitle().namespace == 10 and title_text == "fi-dial-map/groups" then
		if not frame.args["groups"] then error("groups=1 required") end
		return west_east_map(frame)
	elseif mw.title.getCurrentTitle().namespace == 10 and mw.ustring.find(title_text, "^fi%-dial%-map/") then
		word_id = mw.ustring.gsub(title_text, "^fi%-dial%-map/", "")
	elseif mw.title.getCurrentTitle().namespace == 10 and title_text == "fi-dial-map" then
		if frame.args["groups"] then error("groups not allowed") end
		return west_east_map(frame)
	else
		error("This template can only be used in subpages of [[Template:fi-dial-map]]")
	end
	
	if mw.ustring.find(word_id, "^feature/") then
		is_feature = true
		word_id = mw.ustring.gsub(word_id, "^feature/", "")
	end
	
	local title = word_id
	if mw.ustring.find(title, "%(") then
		title = mw.ustring.match(title, "^[^(]+")
	end
	local module_name = "Module:fi-dialects/data/" .. (is_feature and "feature" or "word") .. "/" .. word_id
	local data_ok, data = pcall(function() return mw.loadData(module_name) end)
	if not data_ok then
		return "<div><em>No data found. ([[" .. module_name .. "|Add some]].)</em></div>" .. require("Module:utilities").format_categories("fi-dial-map missing data")
	end
	return synonym_map(frame, title, word_id, data, is_feature)
end

return export