پودمان: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) | ||
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]", "") | ||
s = mw.text.trim(s) | s = mw.text.trim(s) | ||
return s | return s | ||
end | end | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
-- | -- ARG HELPERS | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
local function getArgs(frame) | local function getArgs(frame) | ||
local args = {} | local args = {} | ||
local function | |||
for k, v in pairs( | local function add(tbl) | ||
if not tbl then return end | |||
for k, v in pairs(tbl) do | |||
if v and v ~= "" then | if v and v ~= "" then | ||
local | local nk = norm(k) | ||
args[ | args[nk] = mw.text.trim(v) | ||
args[k] = v | args[k] = mw.text.trim(v) | ||
end | end | ||
end | end | ||
end | end | ||
add(frame.args) | |||
local parent = frame:getParent() | local parent = frame:getParent() | ||
if parent then | if parent then | ||
add(parent.args) | |||
end | |||
return args | return args | ||
end | |||
local function getArg(args, ...) | |||
local keys = {...} | |||
for i = 1, #keys do | |||
local k = keys[i] | |||
if k then | |||
local nk = norm(k) | |||
if args[nk] and args[nk] ~= "" then | |||
return args[nk] | |||
end | |||
end | |||
end | |||
return nil | |||
end | end | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
-- | -- TABLE BASE | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
local function | |||
local function newTable(rawType) | |||
return mw.html.create("table") | |||
:addClass("infobox") | |||
:addClass("alborz-infobox") | |||
:addClass("infobox-type-" .. (rawType or "generic")) | |||
end | end | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
-- | -- TITLE | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
local function addTitle(root, args) | |||
local title = | |||
getArg(args, "نام", "نام رسمی", "نامرسمی") | |||
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(title)) | |||
end | |||
------------------------------------------------------- | |||
-- BADGE MAP (ساده و سریع) | |||
------------------------------------------------------- | |||
local badgeMap = { | |||
city = "شهر", | |||
village = "روستا", | |||
county = "شهرستان", | |||
district = "بخش", | |||
ruraldistrict = "دهستان", | |||
mountain = "کوه", | |||
river = "رودخانه", | |||
waterfall = "آبشار", | |||
cave = "غار", | |||
park = "پارک", | |||
historic = "بنای تاریخی", | |||
religious = "بنای مذهبی" | |||
} | |||
local function addBadge(root, rawType) | local function addBadge(root, rawType) | ||
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 rawType | local label = badgeMap[rawType] or rawType | ||
| خط ۷۱: | خط ۱۲۴: | ||
:attr("colspan", "2") | :attr("colspan", "2") | ||
:css("text-align", "center") | :css("text-align", "center") | ||
:css("padding", "6px | :css("padding", "6px 0") | ||
:tag("span") | :tag("span") | ||
:css("display", "inline-block") | :css("display", "inline-block") | ||
:css(" | :css("padding", "2px 10px") | ||
:css(" | :css("border", "1px solid #c0d4e8") | ||
:css("border-radius", "4px") | |||
:css("background", "#f0f6ff") | |||
:css("font-size", "87%") | :css("font-size", "87%") | ||
:css("font-weight", "bold") | :css("font-weight", "bold") | ||
:wikitext(label) | :wikitext(label) | ||
end | end | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
-- | -- IMAGE | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
local | local function addImage(root, args) | ||
local image = getArg(args, "تصویر", "پرونده", "image") | |||
if not image then | |||
return | |||
end | |||
image = mw.text.trim(image) | |||
root:tag("tr") | root:tag("tr") | ||
:tag(" | :tag("td") | ||
:attr("colspan", "2") | :attr("colspan", "2") | ||
:css("text-align", "center") | :css("text-align", "center") | ||
: | :wikitext("[[پرونده:" .. image .. "|250px]]") | ||
end | |||
------------------------------------------------------- | |||
-- CAPTION | |||
------------------------------------------------------- | |||
local function addCaption(root, args) | |||
local caption = | |||
getArg(args, | |||
"توضیح تصویر", | |||
"عنوان تصویر", | |||
"برچسب تصویر") | |||
if not caption then | |||
return | |||
if | |||
end | end | ||
--------------------------------------------------- | root:tag("tr") | ||
:tag("td") | |||
:attr("colspan", "2") | |||
:css("text-align", "center") | |||
if | :css("font-size", "90%") | ||
:css("padding", "3px 0 6px 0") | |||
:wikitext(mw.text.encode(caption)) | |||
end | |||
------------------------------------------------------- | |||
-- ROW | |||
------------------------------------------------------- | |||
local function addRow(root, label, value) | |||
if not value or value == "" then | |||
return | |||
end | end | ||
--------------------------------------------------- | local tr = root:tag("tr") | ||
tr:tag("th") | |||
:wikitext(label) | |||
tr:tag("td") | |||
:wikitext(value) | |||
end | |||
------------------------------------------------------- | |||
-- ROWS | |||
------------------------------------------------------- | |||
local function addRows(root, args, layout) | |||
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 value = args | local value = | ||
getArg(args, field.arg) | |||
if (not value or value == "") and field.default then | if (not value or value == "") and field.default then | ||
| خط ۱۴۹: | خط ۲۱۵: | ||
end | end | ||
addRow(root, field.label, value) | if value and value ~= "" then | ||
addRow(root, field.label, value) | |||
end | |||
end | end | ||
end | end | ||
end | |||
------------------------------------------------------- | |||
-- RENDER | |||
------------------------------------------------------- | |||
local function render(args) | |||
local rawType = getArg(args, "نوع") or "city" | |||
local layout = data.layouts[rawType] or data.layouts.city | |||
local root = newTable(rawType) | |||
addTitle(root, args) | |||
addBadge(root, rawType) | |||
addImage(root, args) | |||
addCaption(root, args) | |||
addRows(root, args, layout) | |||
return tostring(root) | return tostring(root) | ||
end | end | ||
------------------------------------------------------- | |||
-- MAIN | |||
------------------------------------------------------- | |||
function p.main(frame) | function p.main(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
-------------------------------------------------------
-- ARG HELPERS
-------------------------------------------------------
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 nk = norm(k)
args[nk] = mw.text.trim(v)
args[k] = mw.text.trim(v)
end
end
end
add(frame.args)
local parent = frame:getParent()
if parent then
add(parent.args)
end
return args
end
local function getArg(args, ...)
local keys = {...}
for i = 1, #keys do
local k = keys[i]
if k then
local nk = norm(k)
if args[nk] and args[nk] ~= "" then
return args[nk]
end
end
end
return nil
end
-------------------------------------------------------
-- TABLE BASE
-------------------------------------------------------
local function newTable(rawType)
return mw.html.create("table")
:addClass("infobox")
:addClass("alborz-infobox")
:addClass("infobox-type-" .. (rawType or "generic"))
end
-------------------------------------------------------
-- TITLE
-------------------------------------------------------
local function addTitle(root, args)
local title =
getArg(args, "نام", "نام رسمی", "نامرسمی")
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(title))
end
-------------------------------------------------------
-- BADGE MAP (ساده و سریع)
-------------------------------------------------------
local badgeMap = {
city = "شهر",
village = "روستا",
county = "شهرستان",
district = "بخش",
ruraldistrict = "دهستان",
mountain = "کوه",
river = "رودخانه",
waterfall = "آبشار",
cave = "غار",
park = "پارک",
historic = "بنای تاریخی",
religious = "بنای مذهبی"
}
local function addBadge(root, rawType)
if not rawType or rawType == "" then
return
end
rawType = norm(rawType)
local label = badgeMap[rawType] or rawType
root:tag("tr")
:tag("td")
:attr("colspan", "2")
:css("text-align", "center")
:css("padding", "6px 0")
:tag("span")
:css("display", "inline-block")
:css("padding", "2px 10px")
:css("border", "1px solid #c0d4e8")
:css("border-radius", "4px")
:css("background", "#f0f6ff")
:css("font-size", "87%")
:css("font-weight", "bold")
:wikitext(label)
end
-------------------------------------------------------
-- IMAGE
-------------------------------------------------------
local function addImage(root, args)
local image = getArg(args, "تصویر", "پرونده", "image")
if not image then
return
end
image = mw.text.trim(image)
root:tag("tr")
:tag("td")
:attr("colspan", "2")
:css("text-align", "center")
:wikitext("[[پرونده:" .. image .. "|250px]]")
end
-------------------------------------------------------
-- CAPTION
-------------------------------------------------------
local function addCaption(root, args)
local caption =
getArg(args,
"توضیح تصویر",
"عنوان تصویر",
"برچسب تصویر")
if not caption then
return
end
root:tag("tr")
:tag("td")
:attr("colspan", "2")
:css("text-align", "center")
:css("font-size", "90%")
:css("padding", "3px 0 6px 0")
:wikitext(mw.text.encode(caption))
end
-------------------------------------------------------
-- 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(value)
end
-------------------------------------------------------
-- 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.arg)
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 rawType = getArg(args, "نوع") or "city"
local layout = data.layouts[rawType] or data.layouts.city
local root = newTable(rawType)
addTitle(root, args)
addBadge(root, rawType)
addImage(root, args)
addCaption(root, args)
addRows(root, args, layout)
return tostring(root)
end
-------------------------------------------------------
-- MAIN
-------------------------------------------------------
function p.main(frame)
return render(getArgs(frame))
end
return p