Module:ar-dial-syn

This module page is experimental.
The details of its operation have not yet been fully decided upon. Do not deploy widely until the module page is finished.

Arabic dialectal synonyms module. See {{ar-dial}}.


local export = {}
local m_links = require("Module:links")
local m_scs = require("Module:scripts")
local m_languages = require("Module:languages")
local ar = m_languages.getByCode("ar")

--TODO separate classical, modern standard into different groups?
local variety_list = {
	"special","Algerian","Andalusian","Baharna","Central Asian","Chadian","Cypriot","Dhofari","Egyptian","Gulf","Hassaniya","Hijazi","Libyan","Maltese","Moroccan","Najdi","North Levantine","North Mesopotamian","Omani","Shihhi","South Levantine","South Mesopotamian","Sudanese","Tunisian","Yemeni","Pidgins and creoles"
}

--TODO rm, and actually make into proper lua
--local iso_codes = { 'Algerian':'arq','Baharna':'abv','Chadian':'shu','Cypriot':'acy','Dhofari':'adf','Egyptian':'arz','Gulf':'afb','Hassaniya':'mey','Hijazi':'acw','Libyan':'ayl','Maltese':'mt','Moroccan':'ary','Najdi':'ars','North Levantine':'apc','North Mesopotamian':'ayp','Omani':'acx','Shihhi':'ssh','South Levantine':'ajp','Sudanese':'apd','Tunisian':'aeb','Juba':'pga','Nubi (Kenya)':'kcn','Nubi (Uganda)':'kcn'}

--TODO add colours
local variety_colour={}
--local variety_colour = {
	--"special","Algerian","Andalusian","Baharna","Central Asian","Chadian","Cypriot","Dhofari","Egyptian","Gulf","Hassaniya","Hijazi","Libyan","Maltese","Moroccan","Najdi","North Levantine","North Mesopotamian","Omani","Shihhi","South Levantine","South Mesopotamian","Sudanese","Tunisian","Yemeni","Pidgins and creoles"
--}

--TODO needed?
local special_note={}
--local special_note = {
	--["Classical"] = "[[w:Classical Chinese|Classical Chinese]]",
	--["Formal"] = "Formal <small>([[w:Written vernacular Chinese|Written Standard Chinese]])</small>",
	--["Taxonomic"] = "Taxonomic name",
--}

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:ar/data/dial-syn/" .. target_page
	local variety_data = require("Module:ar/data/dial")
	local m_syndata
	if mw.title.new(resource_page).exists then
		m_syndata = require(resource_page).list
	else
		return frame:expandTemplate{ title = "Template:ar-dial/uncreated", args = { target_page } }
	end

	local template = {
		["special"]={},
		["Algerian"]={},
		["Andalusian"]={},
		["Baharna"]={},
		["Central Asian"]={},
		["Chadian"]={},
		["Cypriot"]={},
		["Dhofari"]={},
		["Egyptian"]={},
		["Gulf"]={},
		["Hassaniya"]={},
		["Hijazi"]={},
		["Libyan"]={},
		["Maltese"]={},
		["Moroccan"]={},
		["Najdi"]={},
		["North Levantine"]={},
		["North Mesopotamian"]={},
		["Omani"]={},
		["Shihhi"]={},
		["South Levantine"]={},
		["South Mesopotamian"]={},
		["Sudanese"]={},
		["Tunisian"]={},
		["Yemeni"]={},
		["Pidgins and creoles"]={}
	}

	local main_title = mw.ustring.gsub(m_links.full_link( { term = target_page, lang = ar } ), "[0-9%-]", "")
	local syn_table = { [=[
	<div class="NavFrame" style="border:0px; max-width: 40em; text-align:center;"><div class="NavHead" style="font-size:105%; border:1px solid #aaaaaa; margin-left:-1px; background-color:#CCCCFF; text-align:center;" cellpadding="3">Dialectal synonyms of ]=] ..
	main_title .. ' (“' .. m_syndata["meaning"] .. '”) ' ..
	"[[Template:ar-dial-map/" .. target_page .. '|<small>&#91;map&#93;</small>]]\n' .. [=[</div><div class="NavContent"> 
	{| class="wikitable" style="margin:0; text-align:center; width: 100%"
	|-
	! style="background:#E8ECFA" | Variety
	! style="background:#E8ECFA" | Location
	! style="background:#E8ECFA" | Location (in Arabic)
	! style="background:#E8ECFA" | Words]=] .. [=[
	<div style="float: right; clear: right; font-size:60%"><span class="plainlinks">[]=] ..
	tostring(mw.uri.fullUrl("Module:ar/data/dial-syn/" .. 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
	--TODO hm
	--if m_syndata["Formal"][1] == "" then
		--m_syndata["Formal"] = { target_page }
	--elseif m_syndata["Formal"][1] == "-" then
		--m_syndata["Formal"] = nil
	--end

	for location, synonym_set in pairs(m_syndata) do
		if synonym_set[1] ~= "" then
			local formatted_synonyms = {}
			local location_data = variety_data[location] or error("No data for the location " .. location .. "!")
			local location_lang = m_languages.getByCanonicalName(location_data.group)or m_languages.getByCanonicalName(location_data.group..' Arabic') or ar
			local location_sc = m_scs.getByCode(location_data.script,1>0)

			for i, synonym in ipairs(synonym_set) do
				local splat = mw.text.split(synonym, ':')
				--local synonym_note = mw.text.split(synonym, ":")
				--local synonym_etym = mw.text.split(synonym_note[1], "_")
				--local synonym_trsc = mw.text.split(synonym_etym[1], "|")
				--local syn, trsc, etym, note = synonym_trsc[1], synonym_trsc[2], synonym_etym[2], synonym_note[2]
				local syn, trsc, etym, note = splat[1], splat[2], splat[3], splat[4]
				--TODO: fancify error
				assert(m_scs.findBestScriptWithoutLang(syn):getCode() == location_data.script, "Invalid script for term "..syn.." of lang "..location)

				local synonym_link = m_links.full_link( {
					term = syn .. (etym and string.format("#Etymology %s", etym) or ""),
					alt = etym and (syn .. string.format("<sub>%s</sub>", etym)),
					tr = trsc,
					lang = location_lang,
					sc = location_sc
				} )

				note = note and ' <span style="font-size:60%"><i>' .. note .. '</i></span>' or ""
				table.insert(formatted_synonyms, synonym_link .. note)
			end

			local location_name = mw.ustring.gsub(location, "(%(.*%))", "<small>%1</small>")
			local location_name_ar = mw.wikibase.getLabelByLang(location_data.wikidata, 'ar')
			--TODO: s/link=""// in ar/data/dial script
			local location_link = location_data.link or mw.wikibase.getSitelink(location_data.wikidata, 'enwiki') or location
			--T-O-D-O: ar names
			table.insert(template[location_data.group],
			{ location_data.order, location_name, location_name_ar, location_link, formatted_synonyms, location_lang })
		end
	end

	for _, variety in ipairs(variety_list) do
		--TODO: tmp
		local colour = variety_colour[variety]or"F0F0FF"
		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
				table.insert(syn_table, "\n|-")
				if i == 1 then
					table.insert(syn_table, "\n!rowspan=" .. #template[variety] .. (special_note[variety] and " colspan=2" or "") .. 
					' style="background:#' .. colour .. '"| ' .. (special_note[variety] or '[[w:'..point_data[6]:getWikipediaArticle()..'|'..variety..']]'))
				end
				table.insert(syn_table, ((point_data[2] and not special_note[variety]) and ('\n|style="background:#' .. colour .. '"| ' .. 
				'[[w:' .. point_data[4] .. '|' .. point_data[2] .. ']]') or '') ..
				'\n|style="background:#' .. colour .. '"| ' .. point_data[3] ..
				'\n|style="background:#' .. colour .. '"| ' ..
				table.concat(point_data[5], ", "))
			end
		end
	end

	if note and note ~= "" then
		table.insert(syn_table, '\n|-\n! style="background:#FFF7FB; padding-top:5px; padding-bottom: 5px" | ' ..
		"<small>Note</small>\n| colspan=2|<small><i>" .. note .. "</i></small>")
	end

	table.insert(syn_table, '\n|}</div></div>')

	return table.concat(syn_table, "")
end

return export