پرش به محتوا

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

از ویکی البرز
جزبدون خلاصۀ ویرایش
جزبدون خلاصۀ ویرایش
خط ۱: خط ۱:
--------------------------------------------------------------------------------
-- Module:AlborzInfoBox
-- Wiki Alborz Geography Information System
-- Core Engine
--------------------------------------------------------------------------------
local p = {}
local p = {}


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


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


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


local function norm(value)
local function norm(value)
خط ۴۲: خط ۳۶:
end
end


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


local function collectArgs(frame)
local function collectArgs(frame)
خط ۸۷: خط ۸۲:
end
end


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


local function getValue(args, field)
local function getValue(args, field)
خط ۱۳۴: خط ۱۳۰:
end
end


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


local function cleanText(value)
local function cleanText(value)
خط ۱۵۶: خط ۱۵۳:
end
end


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


local function addRow(root, label, value, raw)
local function addRow(root, label, value, raw)
خط ۱۹۱: خط ۱۸۹:
end
end


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


local badgeMap = {
local badgeMap = {
خط ۲۱۴: خط ۲۱۳:


}
}


local function addBadge(root, typeKey)
local function addBadge(root, typeKey)


local label = badgeMap[typeKey] or "مکان"
local label = badgeMap[typeKey]
 
-- اگر نوع ناشناخته باشد
if not label then
label = "مکان"
end


local tr = root:tag("tr")
local tr = root:tag("tr")
خط ۲۳۵: خط ۲۴۰:
end
end


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


local function isSimpleFilename(value)
local function isSimpleFilename(value)
خط ۲۶۰: خط ۲۶۶:
end
end


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


local function addImage(root, args)
local function addImage(root, args)
خط ۳۱۳: خط ۳۲۰:
end
end


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


local function addCaption(root, args)
local function addCaption(root, args)
خط ۳۴۰: خط ۳۴۸:
end
end


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


local function getTitle(args)
local function getTitle(args)
خط ۳۵۳: خط ۳۶۲:


end
end


local function addTitle(root, args)
local function addTitle(root, args)
خط ۳۷۳: خط ۳۸۳:
end
end


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


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


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


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",
["district"]       = "district",


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


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


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


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


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


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


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


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


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


}
}


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


local function resolveType(value)
local function resolveType(value)


-- نوع پیش‌فرض
-- اگر نوع مشخص نشده باشد
if not value or value == "" then
if not value or value == "" then
return "city"
return "city"
خط ۴۵۶: خط ۴۷۰:
value = norm(value)
value = norm(value)


return typeAliases[value] or 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
end


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


local function getLayout(typeKey)
local function getLayout(typeKey)
خط ۴۷۷: خط ۵۱۱:
end
end


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


local function renderRows(root, args, layout)
local function renderRows(root, args, layout)
خط ۵۱۲: خط ۵۴۷:
end
end


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


local function render(args)
local function render(args)
خط ۵۴۵: خط ۵۸۱:
if data.typeMap and data.typeMap[typeKey] then
if data.typeMap and data.typeMap[typeKey] then
internalType = data.typeMap[typeKey]
internalType = data.typeMap[typeKey]
end
----------------------------------------------------------------
-- اطمینان از وجود layout
----------------------------------------------------------------
if not data.layouts[internalType] then
-- اگر نوع ناشناخته بود، شهر را پیش‌فرض نکنیم
-- بلکه از نوع مشخص‌شده استفاده کنیم.
-- در صورت نبود layout، از layout شهر استفاده می‌شود.
internalType = typeKey
end
end


خط ۵۹۹: خط ۶۴۸:
end
end


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


function p.main(frame)
function p.main(frame)
خط ۶۱۲: خط ۶۶۲:
end
end


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


return p
return p

نسخهٔ ‏۷ اوت ۲۰۲۶، ساعت ۲۲:۴۸

توضیحات این پودمان می‌تواند در پودمان: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