Jump to content

Module:User:Justinrleung/dial-map-2

From Wiktionary, the free dictionary


local export = {}
local m_links = require("Module:links")
local lang = require("Module:languages").getByCode("zh")
local variety_data = require("Module:zh/data/dial")
local frame = mw.getCurrentFrame()

local dots = {
	"d2502e", "6941c7", "9fdd42", "e7ff79", "b66063",
	"30bcff", "c6ceff", "02f291", "2e1200", "a4ff46", 
	"ffcccf", "63001e", "c124de", "00ae2d", "ff4ce4", 
	"6fff8b", "b900b1", "bfff6b", "0035b6", "fffe8d", 
	"61008c", "adff9e", "d463ff", "3c8a00", "db0098", 
	"00c97f", "a20090", "01a145", "ff5ec8", "59ffc6", 
	"e50025", "01c0cc", "a60003", "02b9db", "d37200", 
	"0151c6", "949900", "00156f", "ffa938", "290062", 
	"b69700", "6d87ff", "c88100", "014592", "ff823e", 
	"000f36", "bdffc4", "1e003b", "ffce7a", "320029", 
	"d5ffea", "6a0050", "009267", "ff4c61", "019282", 
	"fd98ff", "094300", "ad96ff", "965a00", "8eb0ff", 
	"761f00", "9bd8ff", "490d00", "fbffe9", "1d000f", 
	"feecff", "00141e", "ffddad", "001b14", "ff93ce", 
	"004f23", "9d0050", "005e5e", "ffa291", "003e33", 
	"ff9cac", "00536c", "ffc594", "0079b1", "5a3600", 
}

local groupZH = {
	["Northeastern Mandarin"]	= "燕京官話",
	["Jilu Mandarin"]			= "冀魯官話",
	["Jiaoliao Mandarin"]		= "膠遼官話",
	["Lanyin Mandarin"]			= "蘭銀官話",
	["Central Plains Mandarin"]	= "中原官話",
	["Southwestern Mandarin"]	= "西南官話",
	["Jianghuai Mandarin"]		= "江淮官話",
	['Mandarin'] = '官話',
	['Cantonese'] = '粵語',
	['Hakka'] = '客家話',
	['Huizhou'] = '徽語',
	['Gan'] = '贛語',
	['Jin'] = '晉語',
	['Jiuxing Yumin'] = '九姓漁民方言',
	['Northern Min'] = '閩北語',
	['Eastern Min'] = '閩東語',
	['Southern Min'] = '閩南語',
	['Puxian Min'] = '莆仙閩語',
	['Zhongshan Min'] = '中山閩語',
	['Central Min'] = '閩中語',
	['Shaojiang Min'] = '邵將閩語',
	['Southern Pinghua'] = '桂南平話',
	['Northern Pinghua'] = '桂北平話',
	['Shehua'] = '畬話',
	['Waxiang'] = '瓦鄉話',
	['Wu'] = '吳語',
	['Xiang'] = '湘語',
	['Xiangnan Tuhua'] = '湘南土話',
	['Yuebei Tuhua'] = '粵北土話',
};

local grey = "ccccbf"

local elements = {}

elements.map_header = function(text)
	return tostring(
		mw.html.create( "h2" )
			:wikitext( text )
		:done()
	)
end

elements.map = function(points, legend)
	return tostring(
		mw.html.create( "div" )
			:addClass( "thumb" )
			:addClass( "zh-dial-map__container" )
			:wikitext( frame:preprocess('<mapframe class="zh-dial-map__map" mapstyle="osm" width="1200" height="600" longitude="104.1954" latitude="35.8617" zoom="4">' .. points .. '</mapframe>') )
			:done()

			:tag( "div" )
				:addClass( "zh-dial-map__legend" )
				:wikitext( legend )
			:done()
		:done()
	)
end

elements.dot = function(d, loc_name, loc, colour)
	
	local term = mw.ustring.gsub(d.term, "[%[%]]", "")
	local title = string.format("%s %s %s: [[%s#Chinese|%s]]", loc_name, loc.group, loc.chinese .. groupZH[loc.group], term, term)
	
	return string.format('{ "type": "Feature", "properties": { "marker-color": "%s", "marker-size": "small", "title": "%s" }, "geometry": { "type": "Point", "coordinates": [%s, %s] } }', colour, title, loc.long, loc.lat)
	-- tostring(
	-- 	mw.html.create( "span" )
	-- 		:attr( "data-word", term )
	-- 		:attr( "data-location-en", loc_name )
	-- 		:attr( "data-location-zh", loc_info.chinese )
	-- 		:attr( "data-group", loc_info.group )
	-- 		:addClass( "zh-dial-map__dot" )
	-- 		:addClass( (colour == grey and "zh-dial-map__dot-other" or nil) )

	-- 		:css( "top", top .. "em" ) -- The size of 1 em is tied to the font-size of .zh-dial-map__frame
	-- 		:css( "left", left .. "em" )
	-- 		:css( "background-color", "#" .. colour )
	-- 		:attr( "title", loc_name .. " (" .. loc_info.chinese .. ") " .. loc_info.group .. ": " .. term )

	-- 		-- without text in the <span> it seems like the wikitext render discards the whole <span>???
	-- 		-- and makes a link with no text at all??????
	-- 		:wikitext( "&nbsp;" )
	-- 	:done()
	-- )
end

elements.legend = function(d, colour, appendedText)
	return tostring(
		mw.html.create( "div" )
			:attr( "data-word", (d and d.term or "other") )
			:addClass( "zh-dial-map__legend-row" )
			:addClass( (colour == grey and "zh-dial-map__legend-row-other" or nil) )

			:tag( "span" )
				:addClass( "zh-dial-map__legend-row-dot" )
				:css( "background-color", "#" .. colour )

				:wikitext( "&nbsp;" ) -- please let me make empty spans
			:done()

			:wikitext( appendedText )
		:done()
	)
end

function export.make_map(frame)
	local syn_data = require("Module:zh/data/dial-syn/" .. frame.args[1]).list
	local prelim_data, data, points, legend = {}, {}, {}, {}
	local loc, cur = {}, {}
	for i = 1,#variety_data,1 do
		loc = variety_data[i]
		cur = syn_data[loc.key] or {""}
		if cur[1] ~= "" and loc.lat then
			for _, term in ipairs(cur) do
				term = mw.text.split(term, ":")
				if term[2] ~= "mT" and term[2] ~= "GT" then
					term = term[1]
					if prelim_data[term] then
						prelim_data[term].count = prelim_data[term].count + 1
						table.insert(prelim_data[term].locations, loc)
					else
						prelim_data[term] = { count = 1, locations = { loc } }
					end
				end
			end
		end
	end
	for term, term_data in pairs(prelim_data) do
		table.insert(data, { term = term, count = term_data.count, locations = term_data.locations })
	end
	table.sort(data, function(first, second) return first.count > second.count end)

	local prev_count, num = data[1].count, 1
	local greyed, greyed_count = false, 0
	local num_syn = table.maxn(data)
	--when need to have greyed points, grey out all synonyms with only 1 dialect point
	local have_greyed = num_syn > 80 and data[num_syn].count == 1
	for _, d in ipairs(data) do
		greyed = greyed or (have_greyed and d.count == 1) or (num > 70 and d.count ~= prev_count) or num > 80
		local colour = greyed and grey or dots[num]
		for _, loc in ipairs(d.locations) do
			local loc_name = mw.ustring.gsub(loc.english or loc.key, "%((.*)%)$", "- %1")
			table.insert(points, elements.dot(d, loc_name, loc, colour))
		end

		if greyed then
			greyed_count = greyed_count + d.count
		else
			local link = 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>"),
				tr = "-",
			})
			table.insert(legend, elements.legend(d, colour, link .. " (" .. d.count .. ")"))
		end
		
		prev_count = d.count
		num = num + 1
	end
	
	if greyed_count > 0 then
		table.insert(legend, elements.legend(false, grey, "other terms (" .. greyed_count .. ")"))
	end

	local map_header = elements.map_header(
		"Map of Chinese dialectal equivalents for " .. m_links.full_link(
			{
				lang = lang,
				term = mw.ustring.gsub(frame.args[1], "%-.*", ""),
				gloss = syn_data["meaning"],
				tr = '-'
			}
		)
	)
	
	local map = elements.map("[" .. table.concat(points, ", ") .. "]", table.concat(legend))

	return map_header .. map
end

return export