پودمان:AlborzInfoBox: تفاوت میان نسخهها
ظاهر
جزبدون خلاصۀ ویرایش |
جزبدون خلاصۀ ویرایش |
||
| خط ۱: | خط ۱: | ||
local p = {} | local p = {} | ||
local data = mw.loadData('Module:AlborzInfoBox/Data/Geography') | local data = mw.loadData('Module:AlborzInfoBox/Data/Geography') | ||
| خط ۸: | خط ۹: | ||
if not s then return nil end | if not s then return nil end | ||
s = mw.text.trim(s) | s = mw.text.trim(s) | ||
s = mw.ustring.gsub(s, "[\u200C\u200D]", "") | s = mw.ustring.gsub(s, "[\u200C\u200D]", "") -- حذف ZWNJ و ZWJ | ||
s = mw.text.trim(s) | s = mw.text.trim(s) | ||
return s | return s | ||
| خط ۲۰: | خط ۲۱: | ||
local candidates = {} | local candidates = {} | ||
if field.arg then table.insert(candidates, field.arg) end | |||
-- arg تکی | |||
if field.arg then | |||
table.insert(candidates, field.arg) | |||
end | |||
-- args چندگانه | |||
if field.args then | if field.args then | ||
for _, a in ipairs(field.args) do table.insert(candidates, a) end | for _, a in ipairs(field.args) do | ||
table.insert(candidates, a) | |||
end | |||
end | end | ||
for _, name in ipairs(candidates) do | for _, name in ipairs(candidates) do | ||
local | local normalized = norm(name) | ||
if val and val ~= "" then return val end | local val = args[normalized] | ||
if val and val ~= "" then | |||
return val | |||
end | |||
end | end | ||
return nil | return nil | ||
| خط ۴۱: | خط ۵۳: | ||
for k, v in pairs(tbl) do | for k, v in pairs(tbl) do | ||
if v and v ~= "" then | if v and v ~= "" then | ||
local normKey = norm(k) | |||
args[normKey] = mw.text.trim(v) | |||
end | end | ||
end | end | ||
| خط ۵۸: | خط ۷۱: | ||
local tr = root:tag("tr") | local tr = root:tag("tr") | ||
tr:tag("th"):wikitext(label) | tr:tag("th"):wikitext(label) | ||
if value:find("%[%[") or value:find("{{") then | if value:find("%[%[") or value:find("{{") or value:find("|") then | ||
tr:tag("td"):wikitext(value) | tr:tag("td"):wikitext(value) | ||
else | else | ||
| خط ۶۶: | خط ۷۹: | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
-- BADGE | -- BADGE | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
local badgeMap = { | local badgeMap = { | ||
| خط ۸۰: | خط ۹۳: | ||
park = "پارک", | park = "پارک", | ||
historic = "بنای تاریخی", | historic = "بنای تاریخی", | ||
["بنای تاریخی | ["بنای تاریخی"] = "بنای تاریخی" | ||
} | } | ||
| خط ۸۷: | خط ۹۹: | ||
if not rawType or rawType == "" then return end | if not rawType or rawType == "" then return end | ||
rawType = norm(rawType) | rawType = norm(rawType) | ||
local label = badgeMap[rawType] or "بنای تاریخی" | local label = badgeMap[rawType] or "بنای تاریخی" | ||
root:tag("tr") | root:tag("tr") | ||
| خط ۱۰۷: | خط ۱۱۹: | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
-- | -- Image & Caption | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
local function addImage(root, args) | local function addImage(root, args) | ||
local image = args[norm("تصویر")] or args[norm("پرونده")] | local image = args[norm("تصویر")] or args[norm("پرونده")] | ||
if not image then return end | if not image or image == "" then return end | ||
image = mw.text.trim(image) | image = mw.text.trim(image) | ||
root:tag("tr") | root:tag("tr") | ||
| خط ۱۲۱: | خط ۱۳۳: | ||
local function addCaption(root, args) | local function addCaption(root, args) | ||
local caption = args[norm("توضیح تصویر")] or args[norm("عنوان تصویر")] | local caption = args[norm("توضیح تصویر")] or args[norm("عنوان تصویر")] or args[norm("زیرنویس")] | ||
if not caption then return end | if not caption or caption == "" then return end | ||
root:tag("tr") | root:tag("tr") | ||
:tag("td") | :tag("td") | ||
| خط ۱۳۲: | خط ۱۴۴: | ||
end | end | ||
------------------------------------------------------- | |||
-- Table | |||
------------------------------------------------------- | |||
local function newTable(rawType) | local function newTable(rawType) | ||
rawType = rawType or "city" | rawType = rawType or "city" | ||
| خط ۱۳۷: | خط ۱۵۲: | ||
:addClass("infobox") | :addClass("infobox") | ||
:addClass("alborz-infobox") | :addClass("alborz-infobox") | ||
:addClass("infobox-type-" .. rawType) | :addClass("infobox-type-" .. norm(rawType)) | ||
end | end | ||
local function addTitle(root, args) | local function addTitle(root, args) | ||
local title = args[norm("نام")] or args[norm("نام رسمی")] or args[norm("نامرسمی")] | local title = args[norm("نام")] | ||
or args[norm("نام رسمی")] | |||
or args[norm("نامرسمی")] | |||
or mw.title.getCurrentTitle().text | |||
root:tag("tr") | root:tag("tr") | ||
| خط ۱۵۳: | خط ۱۷۰: | ||
end | end | ||
------------------------------------------------------- | |||
-- ADD ROWS | |||
------------------------------------------------------- | |||
local function addRows(root, args, layout) | local function addRows(root, args, layout) | ||
for _, key in ipairs(layout) do | for _, key in ipairs(layout) do | ||
| خط ۱۶۹: | خط ۱۸۹: | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
-- RENDER | -- RENDER (اصلاح شده) | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
local function render(args) | local function render(args) | ||
local | local rawTypeInput = norm(args["نوع"]) or "city" | ||
local layout = data.layouts[ | |||
-- تبدیل به کلید داخلی | |||
local internalType = data.typeMap[rawTypeInput] or rawTypeInput | |||
local layout = data.layouts[internalType] or data.layouts.city | |||
local root = newTable( | local root = newTable(rawTypeInput) -- rawTypeInput برای نمایش و کلاس | ||
addTitle(root, args) | addTitle(root, args) | ||
addBadge(root, | addBadge(root, rawTypeInput) | ||
addImage(root, args) | addImage(root, args) | ||
addCaption(root, args) | addCaption(root, args) | ||
نسخهٔ ۴ ژوئیهٔ ۲۰۲۶، ساعت ۱۳:۰۸
توضیحات این پودمان میتواند در پودمان:AlborzInfoBox/توضیحات قرار گیرد.
local p = {}
local data = mw.loadData('Module:AlborzInfoBox/Data/Geography')
-------------------------------------------------------
-- NORMALIZE
-------------------------------------------------------
local function norm(s)
if not s then return nil end
s = mw.text.trim(s)
s = mw.ustring.gsub(s, "[\u200C\u200D]", "") -- حذف ZWNJ و ZWJ
s = mw.text.trim(s)
return s
end
-------------------------------------------------------
-- GET ARG (قوی)
-------------------------------------------------------
local function getArg(args, field)
if not field then return nil end
local candidates = {}
-- arg تکی
if field.arg then
table.insert(candidates, field.arg)
end
-- args چندگانه
if field.args then
for _, a in ipairs(field.args) do
table.insert(candidates, a)
end
end
for _, name in ipairs(candidates) do
local normalized = norm(name)
local val = args[normalized]
if val and val ~= "" then
return val
end
end
return nil
end
-------------------------------------------------------
-- GET ARGS
-------------------------------------------------------
local function getArgs(frame)
local args = {}
local function add(tbl)
if not tbl then return end
for k, v in pairs(tbl) do
if v and v ~= "" then
local normKey = norm(k)
args[normKey] = mw.text.trim(v)
end
end
end
add(frame.args)
local parent = frame:getParent()
if parent then add(parent.args) end
return args
end
-------------------------------------------------------
-- ADD ROW
-------------------------------------------------------
local function addRow(root, label, value)
if not value or value == "" then return end
local tr = root:tag("tr")
tr:tag("th"):wikitext(label)
if value:find("%[%[") or value:find("{{") or value:find("|") then
tr:tag("td"):wikitext(value)
else
tr:tag("td"):wikitext(mw.text.encode(value))
end
end
-------------------------------------------------------
-- BADGE
-------------------------------------------------------
local badgeMap = {
city = "شهر",
village = "روستا",
county = "شهرستان",
district = "بخش",
ruraldistrict = "دهستان",
mountain = "کوه",
cave = "غار",
river = "رودخانه",
waterfall = "آبشار",
park = "پارک",
historic = "بنای تاریخی",
["بنای تاریخی"] = "بنای تاریخی"
}
local function addBadge(root, rawType)
if not rawType or rawType == "" then return end
rawType = norm(rawType)
local label = badgeMap[rawType] or "بنای تاریخی"
root:tag("tr")
:tag("td")
:attr("colspan", "2")
:css("text-align", "center")
:css("padding", "6px 0 8px 0")
:tag("span")
:css("display", "inline-block")
:css("background", "#e8f0f8")
:css("color", "#1e4b7a")
:css("font-size", "87%")
:css("font-weight", "bold")
:css("padding", "3px 12px")
:css("border-radius", "5px")
:css("border", "1px solid #b0c8e0")
:wikitext(label)
end
-------------------------------------------------------
-- Image & Caption
-------------------------------------------------------
local function addImage(root, args)
local image = args[norm("تصویر")] or args[norm("پرونده")]
if not image or image == "" then return end
image = mw.text.trim(image)
root:tag("tr")
:tag("td")
:attr("colspan", "2")
:css("text-align", "center")
:wikitext("[[پرونده:" .. mw.uri.encode(image, "WIKI") .. "|250px]]")
end
local function addCaption(root, args)
local caption = args[norm("توضیح تصویر")] or args[norm("عنوان تصویر")] or args[norm("زیرنویس")]
if not caption or caption == "" then return end
root:tag("tr")
:tag("td")
:attr("colspan", "2")
:css("text-align", "center")
:css("font-size", "90%")
:css("padding", "3px 0 8px 0")
:wikitext(mw.text.encode(caption))
end
-------------------------------------------------------
-- Table
-------------------------------------------------------
local function newTable(rawType)
rawType = rawType or "city"
return mw.html.create("table")
:addClass("infobox")
:addClass("alborz-infobox")
:addClass("infobox-type-" .. norm(rawType))
end
local function addTitle(root, args)
local title = args[norm("نام")]
or args[norm("نام رسمی")]
or args[norm("نامرسمی")]
or mw.title.getCurrentTitle().text
root:tag("tr")
:tag("th")
:attr("colspan", "2")
:css("text-align", "center")
:css("font-size", "118%")
:css("padding", "8px 4px")
:wikitext(mw.text.encode(title))
end
-------------------------------------------------------
-- ADD ROWS
-------------------------------------------------------
local function addRows(root, args, layout)
for _, key in ipairs(layout) do
local field = data.fields[key]
if field then
local value = getArg(args, field)
if (not value or value == "") and field.default then
value = field.default
end
if value and value ~= "" then
addRow(root, field.label, value)
end
end
end
end
-------------------------------------------------------
-- RENDER (اصلاح شده)
-------------------------------------------------------
local function render(args)
local rawTypeInput = norm(args["نوع"]) or "city"
-- تبدیل به کلید داخلی
local internalType = data.typeMap[rawTypeInput] or rawTypeInput
local layout = data.layouts[internalType] or data.layouts.city
local root = newTable(rawTypeInput) -- rawTypeInput برای نمایش و کلاس
addTitle(root, args)
addBadge(root, rawTypeInput)
addImage(root, args)
addCaption(root, args)
addRows(root, args, layout)
return tostring(root)
end
function p.main(frame)
local args = getArgs(frame)
return render(args)
end
return p