پرش به محتوا

پودمان:AlborzInfoBox: تفاوت میان نسخه‌ها

از ویکی البرز
جزبدون خلاصۀ ویرایش
جزبدون خلاصۀ ویرایش
 
خط ۶: خط ۶:


local data = mw.loadData("Module:AlborzInfoBox/Data/Geography")
local data = mw.loadData("Module:AlborzInfoBox/Data/Geography")


------------------------------------------------------------------------
------------------------------------------------------------------------
خط ۲۳: خط ۲۲:


-- حذف نیم‌فاصله و کاراکترهای نامرئی
-- حذف نیم‌فاصله و کاراکترهای نامرئی
value = mw.ustring.gsub(
value = mw.ustring.gsub(value, "[\u200C\u200D\uFEFF]", "")
value,
"[\u200C\u200D\uFEFF]",
""
)


-- یکسان‌سازی فاصله‌ها
-- یکسان‌سازی فاصله‌ها
value = mw.ustring.gsub(
value = mw.ustring.gsub(value, "%s+", " ")
value,
"%s+",
" "
)


-- حروف عربی → فارسی
-- حروف عربی → فارسی
خط ۶۴: خط ۵۵:
if value ~= nil then
if value ~= nil then


local normalizedKey =
local normalizedKey = norm(key)
norm(key)
local normalizedValue = mw.text.trim(tostring(value))
 
local normalizedValue =
mw.text.trim(tostring(value))
 
if normalizedKey ~= ""
and normalizedValue ~= ""
then
 
args[normalizedKey] =
normalizedValue


if normalizedKey ~= "" and normalizedValue ~= "" then
args[normalizedKey] = normalizedValue
end
end


end
end


end
    end


end
end
خط ۸۹: خط ۷۲:


-- آرگومان‌های الگوی مادر
-- آرگومان‌های الگوی مادر
local parent =
local parent = frame:getParent()
frame:getParent()


if parent then
if parent then
خط ۱۳۰: خط ۱۱۲:
for _, name in ipairs(names) do
for _, name in ipairs(names) do


local key =
local key = norm(name)
norm(name)
local value = args[key]
 
local value =
args[key]


if value and value ~= "" then
if value and value ~= "" then
خط ۱۵۳: خط ۱۳۲:


------------------------------------------------------------------------
------------------------------------------------------------------------
-- SAFE TEXT
-- SAFE WIKITEXT
------------------------------------------------------------------------
------------------------------------------------------------------------


خط ۱۸۱: خط ۱۶۰:
local function addRow(root, label, value, raw)
local function addRow(root, label, value, raw)


value =
value = cleanText(value)
cleanText(value)


if not value then
if not value then
خط ۱۸۸: خط ۱۶۶:
end
end


local tr =
local tr = root:tag("tr")
root:tag("tr")


--------------------------------------------------------------------
-- Label
-- Label
--------------------------------------------------------------------
local th = tr:tag("th")
 
local th =
tr:tag("th")


th
th
خط ۲۰۳: خط ۱۷۶:
:wikitext(label)
:wikitext(label)


--------------------------------------------------------------------
-- Value
-- Value
--------------------------------------------------------------------
local td = tr:tag("td")
 
local td =
tr:tag("td")


td:addClass("infobox-value")
td:addClass("infobox-value")


if raw then
if raw then
td:wikitext(value)
td:wikitext(value)
else
else
 
td:wikitext(mw.text.encode(value))
td:wikitext(
mw.text.encode(value)
)
 
end
end


خط ۲۵۴: خط ۲۱۷:
local function addBadge(root, typeKey)
local function addBadge(root, typeKey)


--------------------------------------------------------------------
local label = badgeMap[typeKey]
-- نوع شناخته‌شده
--------------------------------------------------------------------
 
local label =
badgeMap[typeKey]
 
--------------------------------------------------------------------
-- نوع ناشناخته
--------------------------------------------------------------------


-- اگر نوع ناشناخته باشد
if not label then
if not label then
label = "نوع نامشخص"
label = "مکان"
end
end


--------------------------------------------------------------------
local tr = root:tag("tr")
-- Row
--------------------------------------------------------------------
 
local tr =
root:tag("tr")


local td =
local td = tr:tag("td")
tr:tag("td")


td
td
:attr("colspan", "2")
:attr("colspan", "2")
:addClass("alborz-infobox-type-row")
:addClass("infobox-badge-row")
 
--------------------------------------------------------------------
-- Badge
--------------------------------------------------------------------


local span =
local span = td:tag("span")
td:tag("span")


span
span
:addClass("alborz-infobox-type")
:addClass("infobox-badge")
:wikitext(label)
:wikitext(label)


خط ۳۰۷: خط ۲۵۱:
end
end


value =
value = mw.text.trim(value)
mw.text.trim(value)


-- اگر مقدار شامل ویکی‌متن یا HTML باشد
-- اگر خود مقدار شامل ویکی‌متن یا HTML باشد
if value:find("{", 1, true)
if value:find("{", 1, true)
or value:find("<", 1, true)
or value:find("<", 1, true)
خط ۳۱۶: خط ۲۵۹:
or value:find("[[", 1, true)
or value:find("[[", 1, true)
then
then
return false
return false
end
end


خط ۳۳۷: خط ۲۷۸:
or args[norm("image")]
or args[norm("image")]


if not content
if not content or content == "" then
or content == ""
then
return
return
end
end


content =
content = mw.text.trim(content)
mw.text.trim(content)


local tr =
local tr = root:tag("tr")
root:tag("tr")


local td =
local td = tr:tag("td")
tr:tag("td")


td
td
:attr("colspan", "2")
:attr("colspan", "2")
:addClass("infobox-image")
:addClass("infobox-image")
--------------------------------------------------------------------
-- Simple filename
--------------------------------------------------------------------


if isSimpleFilename(content) then
if isSimpleFilename(content) then


local filename =
local filename = content
content


filename =
filename = mw.ustring.gsub(
mw.ustring.gsub(
filename,
filename,
"^[Ff]ile:",
"^[Ff]ile:",
""
""
)
)


filename =
filename = mw.ustring.gsub(
mw.ustring.gsub(
filename,
filename,
"^پرونده:",
"^پرونده:",
""
""
)
)


td:wikitext(
td:wikitext(
"[[File:"
"[[File:" .. filename .. "|250px]]"
.. filename
.. "|250px]]"
)
)
--------------------------------------------------------------------
-- Existing wikitext
--------------------------------------------------------------------


else
else
خط ۴۱۰: خط ۳۳۳:
or args[norm("برچسب تصویر")]
or args[norm("برچسب تصویر")]


if not caption
if not caption or caption == "" then
or caption == ""
then
return
return
end
end


local tr =
local tr = root:tag("tr")
root:tag("tr")


local td =
local td = tr:tag("td")
tr:tag("td")


td
td
:attr("colspan", "2")
:attr("colspan", "2")
:addClass("infobox-caption")
:addClass("infobox-caption")
:wikitext(
:wikitext(mw.text.encode(caption))
mw.text.encode(caption)
)


end
end
خط ۴۴۹: خط ۳۶۶:
local function addTitle(root, args)
local function addTitle(root, args)


local title =
local title = getTitle(args)
getTitle(args)


if not title
if not title or title == "" then
or title == ""
then
return
return
end
end


local tr =
local tr = root:tag("tr")
root:tag("tr")


local th =
local th = tr:tag("th")
tr:tag("th")


th
th
:attr("colspan", "2")
:attr("colspan", "2")
:addClass("infobox-title")
:addClass("infobox-title")
:wikitext(
:wikitext(mw.text.encode(title))
mw.text.encode(title)
)


end
end
خط ۴۸۰: خط ۳۹۰:
local function newTable(typeKey)
local function newTable(typeKey)


local root =
local root = mw.html.create("table")
mw.html.create("table")


root
root
خط ۴۹۸: خط ۴۰۷:
-- TYPE ALIASES
-- TYPE ALIASES
------------------------------------------------------------------------
------------------------------------------------------------------------
-- نکته:
-- کلیدها در این جدول باید با خروجی norm() هماهنگ باشند.
-- چون norm نیم‌فاصله را حذف می‌کند، «منطقه‌شهری» به «منطقهشهری»
-- تبدیل می‌شود.


local typeAliases = {
local typeAliases = {


--------------------------------------------------------------------
["شهرستان"]       = "county",
-- شهرستان
["county"]         = "county",
--------------------------------------------------------------------
 
["شهرستان"] = "county",
["county"] = "county",
 
--------------------------------------------------------------------
-- شهر
--------------------------------------------------------------------
 
["شهر"] = "city",
["city"] = "city",


--------------------------------------------------------------------
["شهر"]            = "city",
-- روستا
["city"]            = "city",
--------------------------------------------------------------------


["روستا"] = "village",
["روستا"]         = "village",
["village"] = "village",
["village"]       = "village",


--------------------------------------------------------------------
["بخش"]            = "district",
-- بخش
["district"]      = "district",
--------------------------------------------------------------------


["بخش"] = "district",
["دهستان"]         = "ruraldistrict",
["district"] = "district",
["ruraldistrict"] = "ruraldistrict",


--------------------------------------------------------------------
["محله"]          = "neighborhood",
-- دهستان
["neighborhood"]  = "neighborhood",
--------------------------------------------------------------------


["دهستان"] = "ruraldistrict",
["منطقه شهری"]     = "urbanarea",
["ruraldistrict"] = "ruraldistrict",
["منطقه‌شهری"]     = "urbanarea",
 
--------------------------------------------------------------------
-- محله
--------------------------------------------------------------------
 
["محله"] = "neighborhood",
["neighborhood"] = "neighborhood",
 
--------------------------------------------------------------------
-- منطقه شهری
--------------------------------------------------------------------
 
["منطقه شهری"] = "urbanarea",
["منطقهشهری"] = "urbanarea",
["منطقه شهری کرج"] = "urbanarea",
["منطقه شهری کرج"] = "urbanarea",
["منطقهشهری کرج"] = "urbanarea",
["منطقه"]         = "urbanarea",
["منطقه"] = "urbanarea",
["urbanarea"]     = "urbanarea",
 
["urban area"]     = "urbanarea",
["urbanarea"] = "urbanarea",
["urban area"] = "urbanarea",
 
--------------------------------------------------------------------
-- کوه
--------------------------------------------------------------------
 
["کوه"] = "mountain",
["mountain"] = "mountain",
 
--------------------------------------------------------------------
-- غار
--------------------------------------------------------------------
 
["غار"] = "cave",
["cave"] = "cave",
 
--------------------------------------------------------------------
-- رودخانه
--------------------------------------------------------------------
 
["رودخانه"] = "river",
["river"] = "river",


--------------------------------------------------------------------
["کوه"]            = "mountain",
-- آبشار
["mountain"]      = "mountain",
--------------------------------------------------------------------


["آبشار"] = "waterfall",
["غار"]           = "cave",
["waterfall"] = "waterfall",
["cave"]           = "cave",


--------------------------------------------------------------------
["رودخانه"]        = "river",
-- پارک
["river"]          = "river",
--------------------------------------------------------------------


["پارک"] = "park",
["آبشار"]         = "waterfall",
["park"] = "park",
["waterfall"]     = "waterfall",


--------------------------------------------------------------------
["پارک"]          = "park",
-- بنای تاریخی
["park"]          = "park",
--------------------------------------------------------------------


["بنای تاریخی"] = "historic",
["بنای تاریخی"]   = "historic",
["بنایتاریخی"] = "historic",
["بنای‌تاریخی"]   = "historic",
["historic"] = "historic"
["historic"]       = "historic"


}
}
خط ۶۱۲: خط ۴۶۳:
local function resolveType(value)
local function resolveType(value)


--------------------------------------------------------------------
-- اگر نوع مشخص نشده باشد
-- نوع پیش‌فرض
if not value or value == "" then
--------------------------------------------------------------------
 
if not value
or value == ""
then
 
return "city"
return "city"
end
end


--------------------------------------------------------------------
value = norm(value)
-- Normalize
--------------------------------------------------------------------
 
value =
norm(value)


--------------------------------------------------------------------
-- ابتدا از aliasهای ماژول استفاده می‌کنیم
-- Alias داخلی
local resolved = typeAliases[value]
--------------------------------------------------------------------
 
local resolved =
typeAliases[value]


if resolved then
if resolved then
خط ۶۴۲: خط ۴۷۷:
end
end


--------------------------------------------------------------------
-- سپس از typeMap موجود در Data
-- Data typeMap
if data.typeMap then
--------------------------------------------------------------------


if data
resolved = data.typeMap[value]
and data.typeMap
then
 
resolved =
data.typeMap[value]


if resolved then
if resolved then
خط ۶۵۹: خط ۴۸۸:
end
end


--------------------------------------------------------------------
-- در نهایت خود مقدار
-- اگر مقدار خودش یک typeKey معتبر باشد
return value
--------------------------------------------------------------------
 
if data
and data.layouts
and data.layouts[value]
then
 
return value
 
end
 
--------------------------------------------------------------------
-- نوع ناشناخته
--------------------------------------------------------------------
 
return nil


end
end
خط ۶۸۷: خط ۵۰۰:
local function getLayout(typeKey)
local function getLayout(typeKey)


if not data
if not data or not data.layouts then
or not data.layouts
then
 
return {}
return {}
end
end


if not typeKey then
return
return {}
data.layouts[typeKey]
end
or data.layouts.city
 
--------------------------------------------------------------------
-- فقط layout همان نوع
--------------------------------------------------------------------
 
return data.layouts[typeKey]
or {}
or {}


خط ۷۲۱: خط ۵۲۴:
for _, key in ipairs(layout) do
for _, key in ipairs(layout) do


local field =
local field = data.fields[key]
data.fields[key]


if field then
if field then


local value =
local value = getValue(args, field)
getValue(args, field)


if value
if value and value ~= "" then
and value ~= ""
then


addRow(
addRow(
خط ۷۵۴: خط ۵۵۳:


local function render(args)
local function render(args)
--------------------------------------------------------------------
-- Data validation
--------------------------------------------------------------------


if not data then
if not data then
خط ۷۶۷: خط ۵۶۲:
end
end


if not data.layouts then
----------------------------------------------------------------
return "Infobox layouts not found"
-- Resolve type
end
----------------------------------------------------------------
 
--------------------------------------------------------------------
-- Requested type
--------------------------------------------------------------------


local requestedType =
local requestedType =
خط ۷۷۹: خط ۵۷۰:
or args[norm("type")]
or args[norm("type")]


--------------------------------------------------------------------
local typeKey =
-- Resolve type
--------------------------------------------------------------------
 
local internalType =
resolveType(requestedType)
resolveType(requestedType)


--------------------------------------------------------------------
----------------------------------------------------------------
-- اگر نوع مشخص نشده بود
-- Data type map
--------------------------------------------------------------------
----------------------------------------------------------------
 
if not internalType then
 
-- فقط زمانی شهر به عنوان پیش‌فرض استفاده می‌شود
-- که اصلاً نوع وارد نشده باشد.
 
if not requestedType
or requestedType == ""
then
 
internalType = "city"
 
else
 
-- نوع ناشناخته را همان‌طور نگه می‌داریم
-- تا به اشتباه شهر یا مکان نمایش داده نشود.
 
internalType =
norm(requestedType)


end
local internalType = typeKey


if data.typeMap and data.typeMap[typeKey] then
internalType = data.typeMap[typeKey]
end
end


--------------------------------------------------------------------
----------------------------------------------------------------
-- Data type map
-- اطمینان از وجود layout
--------------------------------------------------------------------
----------------------------------------------------------------


if data.typeMap
if not data.layouts[internalType] then
and data.typeMap[internalType]
then


internalType =
-- اگر نوع ناشناخته بود، شهر را پیش‌فرض نکنیم
data.typeMap[internalType]
-- بلکه از نوع مشخص‌شده استفاده کنیم.
-- در صورت نبود layout، از layout شهر استفاده می‌شود.
internalType = typeKey


end
end


--------------------------------------------------------------------
----------------------------------------------------------------
-- Layout
-- Layout
--------------------------------------------------------------------
----------------------------------------------------------------


local layout =
local layout =
getLayout(internalType)
getLayout(internalType)


--------------------------------------------------------------------
----------------------------------------------------------------
-- Table
-- Table
--------------------------------------------------------------------
----------------------------------------------------------------


local root =
local root =
newTable(internalType)
newTable(internalType)


--------------------------------------------------------------------
----------------------------------------------------------------
-- Title
-- Title
--------------------------------------------------------------------
----------------------------------------------------------------


addTitle(
addTitle(root, args)
root,
args
)


--------------------------------------------------------------------
----------------------------------------------------------------
-- Type badge
-- Type badge
--------------------------------------------------------------------
----------------------------------------------------------------


addBadge(
addBadge(root, internalType)
root,
internalType
)


--------------------------------------------------------------------
----------------------------------------------------------------
-- Image
-- Image
--------------------------------------------------------------------
----------------------------------------------------------------


addImage(
addImage(root, args)
root,
args
)


--------------------------------------------------------------------
----------------------------------------------------------------
-- Caption
-- Caption
--------------------------------------------------------------------
----------------------------------------------------------------


addCaption(
addCaption(root, args)
root,
args
)


--------------------------------------------------------------------
----------------------------------------------------------------
-- Rows
-- Rows
--------------------------------------------------------------------
----------------------------------------------------------------


renderRows(
renderRows(
خط ۸۸۵: خط ۶۴۳:
layout
layout
)
)
--------------------------------------------------------------------
-- Output
--------------------------------------------------------------------


return tostring(root)
return tostring(root)

نسخهٔ کنونی تا ‏۷ اوت ۲۰۲۶، ساعت ۲۲:۵۶

توضیحات این پودمان می‌تواند در پودمان:AlborzInfoBox/توضیحات قرار گیرد.

local p = {}

------------------------------------------------------------------------
-- DATA
------------------------------------------------------------------------

local data = mw.loadData("Module:AlborzInfoBox/Data/Geography")

------------------------------------------------------------------------
-- NORMALIZE
------------------------------------------------------------------------

local function norm(value)

	if value == nil then
		return nil
	end

	value = tostring(value)

	value = mw.text.trim(value)

	-- حذف نیم‌فاصله و کاراکترهای نامرئی
	value = mw.ustring.gsub(value, "[\u200C\u200D\uFEFF]", "")

	-- یکسان‌سازی فاصله‌ها
	value = mw.ustring.gsub(value, "%s+", " ")

	-- حروف عربی → فارسی
	value = mw.ustring.gsub(value, "ي", "ی")
	value = mw.ustring.gsub(value, "ى", "ی")
	value = mw.ustring.gsub(value, "ك", "ک")

	return mw.text.trim(value)

end


------------------------------------------------------------------------
-- ARGUMENT HANDLING
------------------------------------------------------------------------

local function collectArgs(frame)

	local args = {}

	local function push(source)

		if not source then
			return
		end

		for key, value in pairs(source) do

			if value ~= nil then

				local normalizedKey = norm(key)
				local normalizedValue = mw.text.trim(tostring(value))

				if normalizedKey ~= "" and normalizedValue ~= "" then
					args[normalizedKey] = normalizedValue
				end

			end

	    	end

	end

	-- آرگومان‌های مستقیم invoke
	push(frame.args)

	-- آرگومان‌های الگوی مادر
	local parent = frame:getParent()

	if parent then
		push(parent.args)
	end

	return args

end


------------------------------------------------------------------------
-- FIELD VALUE
------------------------------------------------------------------------

local function getValue(args, field)

	if not field then
		return nil
	end

	local names = {}

	-- نام اصلی
	if field.arg then
		table.insert(names, field.arg)
	end

	-- نام‌های جایگزین
	if field.args then

		for _, name in ipairs(field.args) do
			table.insert(names, name)
		end

	end

	-- جست‌وجوی مقدار
	for _, name in ipairs(names) do

		local key = norm(name)
		local value = args[key]

		if value and value ~= "" then
			return value
		end

	end

	-- مقدار پیش‌فرض
	if field.default ~= nil then
		return field.default
	end

	return nil

end


------------------------------------------------------------------------
-- SAFE WIKITEXT
------------------------------------------------------------------------

local function cleanText(value)

	if value == nil then
		return nil
	end

	value = tostring(value)

	value = mw.text.trim(value)

	if value == "" then
		return nil
	end

	return value

end


------------------------------------------------------------------------
-- ADD ROW
------------------------------------------------------------------------

local function addRow(root, label, value, raw)

	value = cleanText(value)

	if not value then
		return
	end

	local tr = root:tag("tr")

	-- Label
	local th = tr:tag("th")

	th
		:attr("scope", "row")
		:addClass("infobox-label")
		:wikitext(label)

	-- Value
	local td = tr:tag("td")

	td:addClass("infobox-value")

	if raw then
		td:wikitext(value)
	else
		td:wikitext(mw.text.encode(value))
	end

end


------------------------------------------------------------------------
-- TYPE BADGE
------------------------------------------------------------------------

local badgeMap = {

	city          = "شهر",
	village       = "روستا",
	county        = "شهرستان",
	district      = "بخش",
	ruraldistrict = "دهستان",

	neighborhood  = "محله",
	urbanarea     = "منطقه شهری",

	mountain      = "کوه",
	cave          = "غار",
	river         = "رودخانه",
	waterfall     = "آبشار",
	park          = "پارک",
	historic      = "بنای تاریخی"

}


local function addBadge(root, typeKey)

	local label = badgeMap[typeKey]

	-- اگر نوع ناشناخته باشد
	if not label then
		label = "مکان"
	end

	local tr = root:tag("tr")

	local td = tr:tag("td")

	td
		:attr("colspan", "2")
		:addClass("infobox-badge-row")

	local span = td:tag("span")

	span
		:addClass("infobox-badge")
		:wikitext(label)

end


------------------------------------------------------------------------
-- IMAGE
------------------------------------------------------------------------

local function isSimpleFilename(value)

	if not value or value == "" then
		return false
	end

	value = mw.text.trim(value)

	-- اگر خود مقدار شامل ویکی‌متن یا HTML باشد
	if value:find("{", 1, true)
		or value:find("<", 1, true)
		or value:find("|", 1, true)
		or value:find("[[", 1, true)
	then
		return false
	end

	return true

end


------------------------------------------------------------------------
-- IMAGE RENDERER
------------------------------------------------------------------------

local function addImage(root, args)

	local content =
		args[norm("تصویر")]
		or args[norm("پرونده")]
		or args[norm("image")]

	if not content or content == "" then
		return
	end

	content = mw.text.trim(content)

	local tr = root:tag("tr")

	local td = tr:tag("td")

	td
		:attr("colspan", "2")
		:addClass("infobox-image")

	if isSimpleFilename(content) then

		local filename = content

		filename = mw.ustring.gsub(
			filename,
			"^[Ff]ile:",
			""
		)

		filename = mw.ustring.gsub(
			filename,
			"^پرونده:",
			""
		)

		td:wikitext(
			"[[File:" .. filename .. "|250px]]"
		)

	else

		td:wikitext(content)

	end

end


------------------------------------------------------------------------
-- IMAGE CAPTION
------------------------------------------------------------------------

local function addCaption(root, args)

	local caption =
		args[norm("توضیح تصویر")]
		or args[norm("عنوان تصویر")]
		or args[norm("زیرنویس")]
		or args[norm("برچسب تصویر")]

	if not caption or caption == "" then
		return
	end

	local tr = root:tag("tr")

	local td = tr:tag("td")

	td
		:attr("colspan", "2")
		:addClass("infobox-caption")
		:wikitext(mw.text.encode(caption))

end


------------------------------------------------------------------------
-- TITLE
------------------------------------------------------------------------

local function getTitle(args)

	return
		args[norm("نام‌رسمی")]
		or args[norm("نام رسمی")]
		or args[norm("نام")]
		or mw.title.getCurrentTitle().text

end


local function addTitle(root, args)

	local title = getTitle(args)

	if not title or title == "" then
		return
	end

	local tr = root:tag("tr")

	local th = tr:tag("th")

	th
		:attr("colspan", "2")
		:addClass("infobox-title")
		:wikitext(mw.text.encode(title))

end


------------------------------------------------------------------------
-- TABLE
------------------------------------------------------------------------

local function newTable(typeKey)

	local root = mw.html.create("table")

	root
		:addClass("infobox")
		:addClass("alborz-infobox")
		:addClass("infobox-geography")
		:addClass("infobox-type-" .. typeKey)
		:attr("role", "presentation")

	return root

end


------------------------------------------------------------------------
-- TYPE ALIASES
------------------------------------------------------------------------

local typeAliases = {

	["شهرستان"]       = "county",
	["county"]         = "county",

	["شهر"]            = "city",
	["city"]            = "city",

	["روستا"]          = "village",
	["village"]        = "village",

	["بخش"]            = "district",
	["district"]       = "district",

	["دهستان"]         = "ruraldistrict",
	["ruraldistrict"]  = "ruraldistrict",

	["محله"]           = "neighborhood",
	["neighborhood"]   = "neighborhood",

	["منطقه شهری"]     = "urbanarea",
	["منطقه‌شهری"]     = "urbanarea",
	["منطقه شهری کرج"] = "urbanarea",
	["منطقه"]          = "urbanarea",
	["urbanarea"]      = "urbanarea",
	["urban area"]     = "urbanarea",

	["کوه"]            = "mountain",
	["mountain"]       = "mountain",

	["غار"]            = "cave",
	["cave"]           = "cave",

	["رودخانه"]        = "river",
	["river"]          = "river",

	["آبشار"]          = "waterfall",
	["waterfall"]      = "waterfall",

	["پارک"]           = "park",
	["park"]           = "park",

	["بنای تاریخی"]    = "historic",
	["بنای‌تاریخی"]    = "historic",
	["historic"]       = "historic"

}


------------------------------------------------------------------------
-- TYPE RESOLUTION
------------------------------------------------------------------------

local function resolveType(value)

	-- اگر نوع مشخص نشده باشد
	if not value or value == "" then
		return "city"
	end

	value = norm(value)

	-- ابتدا از aliasهای ماژول استفاده می‌کنیم
	local resolved = typeAliases[value]

	if resolved then
		return resolved
	end

	-- سپس از typeMap موجود در Data
	if data.typeMap then

		resolved = data.typeMap[value]

		if resolved then
			return resolved
		end

	end

	-- در نهایت خود مقدار
	return value

end


------------------------------------------------------------------------
-- LAYOUT
------------------------------------------------------------------------

local function getLayout(typeKey)

	if not data or not data.layouts then
		return {}
	end

	return
		data.layouts[typeKey]
		or data.layouts.city
		or {}

end


------------------------------------------------------------------------
-- RENDER ROWS
------------------------------------------------------------------------

local function renderRows(root, args, layout)

	if not layout then
		return
	end

	for _, key in ipairs(layout) do

		local field = data.fields[key]

		if field then

			local value = getValue(args, field)

			if value and value ~= "" then

				addRow(
					root,
					field.label,
					value,
					field.raw
				)

			end

		end

	end

end


------------------------------------------------------------------------
-- MAIN RENDER
------------------------------------------------------------------------

local function render(args)

	if not data then
		return "Infobox data not found"
	end

	if not data.fields then
		return "Infobox fields not found"
	end

	----------------------------------------------------------------
	-- Resolve type
	----------------------------------------------------------------

	local requestedType =
		args[norm("نوع")]
		or args[norm("type")]

	local typeKey =
		resolveType(requestedType)

	----------------------------------------------------------------
	-- Data type map
	----------------------------------------------------------------

	local internalType = typeKey

	if data.typeMap and data.typeMap[typeKey] then
		internalType = data.typeMap[typeKey]
	end

	----------------------------------------------------------------
	-- اطمینان از وجود layout
	----------------------------------------------------------------

	if not data.layouts[internalType] then

		-- اگر نوع ناشناخته بود، شهر را پیش‌فرض نکنیم
		-- بلکه از نوع مشخص‌شده استفاده کنیم.
		-- در صورت نبود layout، از layout شهر استفاده می‌شود.
		internalType = typeKey

	end

	----------------------------------------------------------------
	-- Layout
	----------------------------------------------------------------

	local layout =
		getLayout(internalType)

	----------------------------------------------------------------
	-- Table
	----------------------------------------------------------------

	local root =
		newTable(internalType)

	----------------------------------------------------------------
	-- Title
	----------------------------------------------------------------

	addTitle(root, args)

	----------------------------------------------------------------
	-- Type badge
	----------------------------------------------------------------

	addBadge(root, internalType)

	----------------------------------------------------------------
	-- Image
	----------------------------------------------------------------

	addImage(root, args)

	----------------------------------------------------------------
	-- Caption
	----------------------------------------------------------------

	addCaption(root, args)

	----------------------------------------------------------------
	-- Rows
	----------------------------------------------------------------

	renderRows(
		root,
		args,
		layout
	)

	return tostring(root)

end


------------------------------------------------------------------------
-- PUBLIC ENTRY POINT
------------------------------------------------------------------------

function p.main(frame)

	local args =
		collectArgs(frame)

	return render(args)

end


------------------------------------------------------------------------
-- RETURN
------------------------------------------------------------------------

return p