A modult a Modul:ar-dial-syn/doc lapon tudod dokumentálni

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

local variety_list = {
	"Qurʾānic", "Modern Standard Arabic", 
	"Algeria", "Central Asia", "Chad", "Egypt", "Gulf", "Iraq", "Jordan", "Lebanon", "Libya", "Mauritania", "Morocco", "Oman", "Palestine/Israel", "Saudi Arabia", "Sudan", "Syria", "Tunisia", "Yemen"
}

local variety_colour = {
	["Qurʾānic"]	= "FAF0F2",
	["Modern Standard Arabic"]		= "FAF0F2",
	["Algeria"]	= "FAF5F0",
	["Central Asia"]	= "F0F5FA",
	["Chad"]			= "F0FAF3",
	["Egypt"]		= "F0FAF3",
	["Gulf"]		= "FAF9F0",
	["Iraq"]			= "F0F5FA",
	["Jordan"]		= "F7FAF0",
	["Lebanon"]	= "F7FAF0",
	["Libya"]		= "FAF5F0",
	["Mauritania"]	= "FAF5F0",
	["Morocco"]		= "FAF5F0",
	["Oman"]			= "F4F0FA",
	["Palestine/Israel"]		= "F7FAF0",
	["Saudi Arabia"]			= "F4F0FA",
	["Sudan"]			= "F0FAF3",
	["Syria"]			= "F7FAF0",
	["Tunisia"]			= "FAF5F0",
	["Yemen"]			= "F4F0FA",
}

local special = {
	["Qurʾānic"] = "[[w:Classical Arabic|Qurʾānic]]",
	["Modern Standard Arabic"] = "[[w:Modern Standard Arabic|Modern Standard Arabic]]",
}

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-dial/data/" .. target_page
	local variety_data = require("Module:ar-dial/data")
	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 = {
		["Qurʾānic"]	=	{},
		["Modern Standard Arabic"]		=	{},
		["Algeria"]		=	{},
		["Central Asia"]	= 	{},
		["Chad"]	=	{},
		["Egypt"]	=	{},
		["Gulf"]			=	{},
		["Iraq"]		=	{},
		["Jordan"]		=	{},
		["Lebanon"]			= 	{},
		["Libya"]		=	{},
		["Mauritania"]	= 	{},
		["Morocco"]		=	{},
		["Oman"]	=	{},
		["Palestine/Israel"]		=	{},
		["Saudi Arabia"]			=	{},
		["Sudan"]		=	{},
		["Syria"]		=	{},
		["Tunisia"]		=	{},
		["Yemen"]		=	{},
	}

	local main_title = mw.ustring.gsub((target_page == pagename and pagename or '[[' .. target_page .. ']]'), "[0-9%-]", "")
	local syn_table = { [=[
	{| class="wikitable mw-collapsible mw-collapsed" style="margin:0; text-align:center;"
	|-
	! style="background:#FCFFFC; width:40em" colspan=3 | Dialectal forms and  synonyms of <b><span class="Arab" lang="ar">]=] ..
		main_title .. '</span></b> (“' .. m_syndata["meaning"] .. '”) ' ..
		"[[Template:ar-dial-map/" .. target_page .. '|<small>&#91;map&#93;</small>]]\n' .. [=[
	|-
	|-
	! style="background:#E8ECFA" | Region
	! style="background:#E8ECFA" | Dialect
	! style="background:#E8ECFA" | Words]=] .. [=[
	<div style="float: right; clear: right; font-size:60%">[]=] ..
		tostring(mw.uri.fullUrl("Module:ar-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
	m_syndata["Modern Standard Arabic"] = m_syndata["Modern Standard Arabic"][1] ~= "" and m_syndata["Modern Standard Arabic"] or { target_page }
	
	for location, synonym_set in pairs(m_syndata) do
		local sc = "Latn"
		if special[location] then
			sc = "Arab"
		end
		if synonym_set[1] ~= "" then
			local formatted_synonyms = {}
			for i, synonym in ipairs(synonym_set) do
				local syn_label = mw.text.split(synonym, ":")
				local transcription_arabic = mw.text.split(syn_label[1], "|")
				local transcription, arabic = transcription_arabic[1], transcription_arabic[2]
				if special[location] then
					synonym = m_links.full_link( {
						term = syn_label[1],
						lang = lang
					} )
				elseif transcription_arabic[2] then
					synonym = m_links.full_link( {
						term = transcription_arabic[2],
						lang = lang,
						tr = transcription_arabic[1]
					} )
				else
					synonym = syn_label[1]
				end
				table.insert(formatted_synonyms, synonym ..
					(syn_label[2] and ' <span style="font-size:60%"><i>' .. syn_label[2] .. '</i></span>' or ""))
			end
			local location_data = variety_data[location] or error("No data for the location " .. 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
	
	for _, variety in ipairs(variety_list) do
		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
				table.insert(syn_table, "\n|-")
				if i == 1 then
					table.insert(syn_table, "\n!rowspan=" .. #template[variety] .. (special[variety] and " colspan=2" or "") .. 
					' style="background:#' .. colour .. '"| ' .. (special[variety] or variety))
				end
				table.insert(syn_table, ((point_data[2] and not special[variety]) and ('\n|style="background:#' .. colour .. '"| ' .. 
					'[[w:' .. point_data[3] .. '|' .. point_data[2] .. ']]') or '') ..
					'\n|style="background:#' .. colour .. '"| ' ..
					table.concat(point_data[4], ", "))
			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|}')
	
	return table.concat(syn_table, "")
end

return export