Module:inc-ash-dial-map

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Test

[edit]
Map of dialectal forms of 𑀓𑀭𑁄𑀢𑀺 (“do”)

local export = {}
local m_links = require("Module:links")
local lang = require("Module:languages").getByCode("inc-ash")
local variety_data = require("Module:inc-ash/dial/data")

local dots = {
	"d2502e", "6941c7", "9fdd42", "c74dc9", "6ccb6e", 
	"d34280", "77d6ba", "4f286c", "d1b94e", "777ad0", 
	"557433", "cf8ebf", "342a29", "c7c3a2", "7f3241", 
	"8ab8d7", "8d6234", "5b6080", "da8573", "4e7a6e"
}

function export.make_map(frame)
	local width = tonumber(frame.args["width"]) or 1200
	local word = frame.args[1] or mw.title.getCurrentTitle().text
	local syn_data = require("Module:inc-ash/dial/data/" .. word).list
	local map = [=[
		<div style="margin-left: auto; margin-right:auto; width:]=] .. width .. [=[px; max-width:]=] .. width .. [=[px;">
		<div><div style="height:]=] .. width * (1615/1500) .. [=[px;width:]=] .. width .. [=[px;overflow:auto;">
		<div style="position:relative;top:0;left:0">
		<div style="position:relative;top:0;left:0;line-height:0">[[File:India location map.svg|]=] .. width .. [=[px|link=]]</div>
		]=]
	local prelim_data, data, points, legend = {}, {}, {}, {}
	for location, synonym_set in pairs(syn_data) do
		-- check if location is in alias list and use the proper one if so
		local actual_location = location
		if variety_data['aliases'][location] ~= nil then actual_location = variety_data['aliases'][location] end
		if location ~= "note" and location ~= "meaning" and location ~= "pos" and variety_data[actual_location].lat and synonym_set[1] ~= "" then
			for _, term in ipairs(synonym_set) do
				term = mw.text.split(term, ":")[1]
				local lemma = lang:transliterate(mw.ustring.match(term, "<(.*)>") or term)
				if prelim_data[term] then
					prelim_data[term].count = prelim_data[term].count + 1
					table.insert(prelim_data[term].locations, location)
				else
					prelim_data[term] = { count = 1, locations = { location }, term = mw.ustring.gsub(term, "[<>]", ""), lemma = lemma }
				end
			end
		end
	end
	for term, term_data in pairs(prelim_data) do
		table.insert(data, { term = term_data.term, count = term_data.count, locations = term_data.locations, lemma = term_data.lemma })
	end
	table.sort(data, function(first, second) return first.count > second.count end)
	
	local prev_count = data[1].count
	local greyed, greyed_count = false, 0
	local completed_lemmas = {}
	local num_completed = 0
	for _, d in ipairs(data) do
		local num = -1
		if completed_lemmas[d.lemma] then
			num = completed_lemmas[d.lemma]
		else
			num = num_completed + 1
			completed_lemmas[d.lemma] = num
			num_completed = num_completed + 1
		end
		greyed = greyed or (num > 10 and d.count ~= prev_count) or num > 20
		local colour = greyed and "CCCCBF" or dots[num]
		for _, location in ipairs(d.locations) do
			-- check if location is in alias list and use the proper one if so
			local actual_location = location
			if variety_data['aliases'][location] ~= nil then actual_location = variety_data['aliases'][location] end
			
			local loc_info = variety_data[actual_location]
			local top_offset, left_offset = 0, 0
			if table.getn(syn_data[location]) > 1 then
				top_offset = math.random(-300, 300) / 100
				left_offset = math.random(-300, 300) / 100
			end
			local top = ((37.5 - loc_info.lat) * (width*(1615/1500))/(37.5-5)) + top_offset
			local left = ((loc_info.long - 67) * width/(99-67)) + left_offset
			local loc_name = mw.ustring.gsub(loc_info.english or actual_location, "%((.*)%)$", "- %1")
			table.insert(points,
				tostring( mw.html.create( "div" )
					:css( "position", "absolute" )
					:css( "top", top .. "px" )
					:css( "left", left .. "px" )
					:css( "margin", "auto" )
					:css( "transform", "translate(-50%,-50%)" ) -- http://stackoverflow.com/questions/33683602/transform-origin-equivalent-for-position-absolute
					:css( "padding", "5px" )
					:css( "border-radius", "100%" )
					:css( "background-color", "#" .. colour )
					:css( "cursor", "help" )
					:css( "opacity", "0.8" )
					:attr( "title", loc_name .. " (" .. loc_info.group .. ")" )))
		end
		if greyed then
			greyed_count = greyed_count + d.count
		else
			table.insert(legend, 
				tostring( mw.html.create( "div" )
					:css( "display", "inline-block" )
					:css( "width", "10px" )
					:css( "height", "10px" )
					:css( "border-radius", "100%" )
					:css( "background-color", "#" .. colour )) .. 
				
				m_links.full_link({lang = lang,
					term = mw.ustring.gsub(d.term, "(.+)_[1-9]", "%1"),
					alt = mw.ustring.gsub(d.term, "(.+)_([1-9])", "%1<sub>%2</sub>")}) .. " (" .. d.count .. ")")
		end
		
		prev_count = d.count
	end
	
	if greyed_count > 0 then
		table.insert(legend, 
			tostring( mw.html.create( "div" )
				:css( "display", "inline-block" )
				:css( "width", "10px" )
				:css( "height", "10px" )
				:css( "border-radius", "100%" )
				:css( "background-color", "#CCCCBF" )) .. 
			
			"other terms (" .. greyed_count .. ")")
	end

	map = [=[
	{| class="wikitable mw-collapsible mw-collapsed" style="margin:0; text-align:center;"
	|-
	! style="background:#FCFFFC; width:40em" colspan=3 | Map of dialectal forms of <b><span class="Brah" lang="inc-ash">]=] ..
		word .. '</span></b> (“' .. syn_data["meaning"] .. '”) ' .. "\n|-\n|\n" .. map .. table.concat(points) .. '</div></div></div>' ..
		'<div style="column-count:' .. math.ceil(width/240) .. ';-moz-column-count:' .. math.ceil(width/240) .. ';-webkit-column-count:' .. math.ceil(width/240) .. ';font-size:smaller;line-height:1.7">' ..
		table.concat(legend, "<br>") .. "</div></div>" .. [=[
		
	|}]=]
		
	return map
end

return export