پودمان:AlborzInfoBox: تفاوت میان نسخهها
ظاهر
جزبدون خلاصۀ ویرایش |
جزبدون خلاصۀ ویرایش |
||
| خط ۱: | خط ۱: | ||
local p = {} | local p = {} | ||
local data = mw.loadData('Module:AlborzInfoBox/Data/Geography') | local data = mw.loadData('Module:AlborzInfoBox/Data/Geography') | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
-- NORMALIZE | -- NORMALIZE | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
local function norm(s) | local function norm(s) | ||
| خط ۱۵: | خط ۱۴: | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
-- GET ARGS | -- GET ARGS | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
local function getArgs(frame) | local function getArgs(frame) | ||
| خط ۲۸: | خط ۲۷: | ||
end | end | ||
end | end | ||
addArgs(frame.args) | addArgs(frame.args) | ||
local parent = frame:getParent() | local parent = frame:getParent() | ||
if parent then | if parent then addArgs(parent.args) end | ||
return args | return args | ||
end | end | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
-- | -- ADD ROW | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
local function addRow(root, label, value) | local function addRow(root, label, value) | ||
if not value or value == "" then return end | if not value or value == "" then return end | ||
local tr = root:tag("tr") | local tr = root:tag("tr") | ||
tr:tag("th"):wikitext(label) | tr:tag("th"):wikitext(label) | ||
tr:tag("td"):wikitext(mw.text.encode(value)) | tr:tag("td"):wikitext(mw.text.encode(value)) | ||
end | |||
------------------------------------------------------- | |||
-- BADGE (نسخهٔ حرفهایتر) | |||
------------------------------------------------------- | |||
local function addBadge(root, rawType) | |||
if not rawType or rawType == "" then return end | |||
-- نرمالایز اضافی برای badge | |||
rawType = norm(rawType) | |||
local badgeMap = { | |||
city = "شهر", | |||
village = "روستا", | |||
county = "شهرستان", | |||
district = "بخش", | |||
ruraldistrict = "دهستان", | |||
mountain = "کوه", | |||
river = "رودخانه", | |||
waterfall = "آبشار", | |||
cave = "غار", | |||
park = "پارک", | |||
} | |||
local label = badgeMap[rawType] or rawType | |||
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", "2px 10px") | |||
:css("border-radius", "4px") | |||
:css("border", "1px solid #c0d0e8") | |||
:wikitext(label) | |||
end | end | ||
| خط ۵۲: | خط ۸۸: | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
local function render(args) | local function render(args) | ||
local rawType = norm(args["نوع"]) or "" | |||
local rawType = norm(args["نوع"]) or " | |||
local layout = data.layouts[rawType] or data.layouts.city | local layout = data.layouts[rawType] or data.layouts.city | ||
local root = mw.html.create("table") | local root = mw.html.create("table") | ||
:addClass("infobox alborz-infobox") | :addClass("infobox alborz-infobox") | ||
:addClass("infobox-type-" .. (rawType ~= "" and rawType or "city")) | |||
--------------------------------------------------- | --------------------------------------------------- | ||
-- TITLE | -- TITLE | ||
--------------------------------------------------- | --------------------------------------------------- | ||
local | local name = args[norm("نام")] or mw.title.getCurrentTitle().text | ||
root:tag("tr") | root:tag("tr") | ||
:tag("th") | :tag("th") | ||
:attr("colspan", "2") | :attr("colspan", "2") | ||
:wikitext(mw.text.encode( | :css("text-align", "center") | ||
:css("font-size", "115%") | |||
:wikitext(mw.text.encode(name)) | |||
--------------------------------------------------- | |||
-- BADGE | |||
--------------------------------------------------- | |||
addBadge(root, rawType) | |||
--------------------------------------------------- | --------------------------------------------------- | ||
-- IMAGE | -- IMAGE | ||
--------------------------------------------------- | --------------------------------------------------- | ||
local img = args[norm("تصویر")] | local img = args[norm("تصویر")] | ||
if img and img ~= "" then | if img and img ~= "" then | ||
img = mw.text.trim(img) | |||
root:tag("tr") | root:tag("tr") | ||
:tag("td") | :tag("td") | ||
:attr("colspan", "2") | :attr("colspan", "2") | ||
:wikitext("[[پرونده:" .. img .. "|250px]]") | :wikitext("[[پرونده:" .. mw.uri.encode(img, "WIKI") .. "|250px]]") | ||
end | end | ||
--------------------------------------------------- | --------------------------------------------------- | ||
-- CAPTION | -- CAPTION | ||
--------------------------------------------------- | --------------------------------------------------- | ||
local caption = args[norm("توضیح تصویر")] | |||
if caption and caption ~= "" then | |||
root:tag("tr") | |||
:tag("td") | |||
:attr("colspan", "2") | |||
:css("font-size", "90%") | |||
:css("text-align", "center") | |||
:wikitext(mw.text.encode(caption)) | |||
end | |||
--------------------------------------------------- | --------------------------------------------------- | ||
-- FIELDS | -- FIELDS | ||
--------------------------------------------------- | --------------------------------------------------- | ||
for _, key in ipairs(layout) do | for _, key in ipairs(layout) do | ||
local field = data.fields[key] | local field = data.fields[key] | ||
if field then | if field then | ||
local argKey = norm(field.arg) | local argKey = norm(field.arg) | ||
| خط ۱۱۱: | خط ۱۵۶: | ||
end | end | ||
function p.main(frame) | function p.main(frame) | ||
return render(getArgs(frame)) | return render(getArgs(frame)) | ||
نسخهٔ ۳ ژوئیهٔ ۲۰۲۶، ساعت ۱۸:۵۹
توضیحات این پودمان میتواند در پودمان: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]", "")
s = mw.text.trim(s)
return s
end
-------------------------------------------------------
-- GET ARGS
-------------------------------------------------------
local function getArgs(frame)
local args = {}
local function addArgs(argTable)
for k, v in pairs(argTable or {}) do
if v and v ~= "" then
local nkey = norm(k)
args[nkey] = v
args[k] = v
end
end
end
addArgs(frame.args)
local parent = frame:getParent()
if parent then addArgs(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)
tr:tag("td"):wikitext(mw.text.encode(value))
end
-------------------------------------------------------
-- BADGE (نسخهٔ حرفهایتر)
-------------------------------------------------------
local function addBadge(root, rawType)
if not rawType or rawType == "" then return end
-- نرمالایز اضافی برای badge
rawType = norm(rawType)
local badgeMap = {
city = "شهر",
village = "روستا",
county = "شهرستان",
district = "بخش",
ruraldistrict = "دهستان",
mountain = "کوه",
river = "رودخانه",
waterfall = "آبشار",
cave = "غار",
park = "پارک",
}
local label = badgeMap[rawType] or rawType
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", "2px 10px")
:css("border-radius", "4px")
:css("border", "1px solid #c0d0e8")
:wikitext(label)
end
-------------------------------------------------------
-- RENDER
-------------------------------------------------------
local function render(args)
local rawType = norm(args["نوع"]) or ""
local layout = data.layouts[rawType] or data.layouts.city
local root = mw.html.create("table")
:addClass("infobox alborz-infobox")
:addClass("infobox-type-" .. (rawType ~= "" and rawType or "city"))
---------------------------------------------------
-- TITLE
---------------------------------------------------
local name = args[norm("نام")] or mw.title.getCurrentTitle().text
root:tag("tr")
:tag("th")
:attr("colspan", "2")
:css("text-align", "center")
:css("font-size", "115%")
:wikitext(mw.text.encode(name))
---------------------------------------------------
-- BADGE
---------------------------------------------------
addBadge(root, rawType)
---------------------------------------------------
-- IMAGE
---------------------------------------------------
local img = args[norm("تصویر")]
if img and img ~= "" then
img = mw.text.trim(img)
root:tag("tr")
:tag("td")
:attr("colspan", "2")
:wikitext("[[پرونده:" .. mw.uri.encode(img, "WIKI") .. "|250px]]")
end
---------------------------------------------------
-- CAPTION
---------------------------------------------------
local caption = args[norm("توضیح تصویر")]
if caption and caption ~= "" then
root:tag("tr")
:tag("td")
:attr("colspan", "2")
:css("font-size", "90%")
:css("text-align", "center")
:wikitext(mw.text.encode(caption))
end
---------------------------------------------------
-- FIELDS
---------------------------------------------------
for _, key in ipairs(layout) do
local field = data.fields[key]
if field then
local argKey = norm(field.arg)
local value = args[argKey] or args[field.arg]
if (not value or value == "") and field.default then
value = field.default
end
addRow(root, field.label, value)
end
end
return tostring(root)
end
function p.main(frame)
return render(getArgs(frame))
end
return p