پودمان:AlborzInfoBox: تفاوت میان نسخهها
ظاهر
جزبدون خلاصۀ ویرایش برچسب: برگرداندهشده |
برچسب: خنثیسازی |
||
| خط ۱: | خط ۱: | ||
local p = {} | local p = {} | ||
local data = mw.loadData('Module:AlborzInfoBox/Data/Geography') | local data = mw.loadData('Module:AlborzInfoBox/Data/Geography') | ||
| خط ۱۳: | خط ۱۴: | ||
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) | |||
return s | return s | ||
| خط ۱۸: | خط ۲۰: | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
-- | -- ARG HELPERS | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
local function getArgs(frame) | local function getArgs(frame) | ||
local args = {} | local args = {} | ||
local function add(tbl) | local function add(tbl) | ||
if not tbl then return end | |||
if not tbl then | |||
for k, v in pairs(tbl) do | for k, v in pairs(tbl) do | ||
if v and v ~= "" then | if v and v ~= "" then | ||
args[ | local nk = norm(k) | ||
args[nk] = mw.text.trim(v) | |||
args[k] = mw.text.trim(v) | |||
end | end | ||
end | end | ||
end | end | ||
| خط ۸۴: | خط ۴۱: | ||
local parent = frame:getParent() | local parent = frame:getParent() | ||
if parent then | if parent then | ||
add(parent.args) | add(parent.args) | ||
| خط ۹۰: | خط ۴۶: | ||
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 | |||
------------------------------------------------------- | |||
-- TITLE | |||
------------------------------------------------------- | |||
local | 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 | end | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
-- BADGE | -- BADGE MAP (ساده و سریع) | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
local badgeMap = { | local badgeMap = { | ||
city = "شهر", | city = "شهر", | ||
village = "روستا", | village = "روستا", | ||
| خط ۱۲۵: | خط ۱۰۳: | ||
mountain = "کوه", | mountain = "کوه", | ||
river = "رودخانه", | river = "رودخانه", | ||
waterfall = "آبشار", | waterfall = "آبشار", | ||
cave = "غار", | |||
park = "پارک", | park = "پارک", | ||
historic = "بنای تاریخی" | historic = "بنای تاریخی", | ||
religious = "بنای مذهبی" | |||
} | } | ||
local function addBadge(root, rawType) | local function addBadge(root, rawType) | ||
if not rawType or rawType == "" then | if not rawType or rawType == "" then | ||
return | return | ||
end | end | ||
rawType = norm(rawType) | |||
local label = badgeMap[rawType] or rawType | local label = badgeMap[rawType] or rawType | ||
root:tag("tr") | root:tag("tr") | ||
:tag("td") | :tag("td") | ||
:attr("colspan","2") | :attr("colspan", "2") | ||
: | :css("text-align", "center") | ||
:css("padding", "6px 0") | |||
:tag("span") | :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) | :wikitext(label) | ||
end | end | ||
| خط ۱۵۶: | خط ۱۴۰: | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
local function addImage(root,args) | local function addImage(root, args) | ||
local image = getArg(args, "تصویر", "پرونده", "image") | |||
local image = | |||
if not image then | if not image then | ||
return | return | ||
end | end | ||
image = mw.text.trim(image) | |||
root:tag("tr") | root:tag("tr") | ||
:tag("td") | :tag("td") | ||
:attr("colspan","2") | :attr("colspan", "2") | ||
: | :css("text-align", "center") | ||
:wikitext("[[پرونده:" .. image .. "|250px]]") | :wikitext("[[پرونده:" .. image .. "|250px]]") | ||
end | end | ||
| خط ۱۷۹: | خط ۱۶۰: | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
local function addCaption(root,args) | local function addCaption(root, args) | ||
local caption = | local caption = | ||
args | getArg(args, | ||
"توضیح تصویر", | |||
"عنوان تصویر", | |||
"برچسب تصویر") | |||
if not caption then | if not caption then | ||
| خط ۱۹۲: | خط ۱۷۳: | ||
root:tag("tr") | root:tag("tr") | ||
:tag("td") | :tag("td") | ||
:attr("colspan","2") | :attr("colspan", "2") | ||
: | :css("text-align", "center") | ||
:wikitext(caption) | :css("font-size", "90%") | ||
:css("padding", "3px 0 6px 0") | |||
:wikitext(mw.text.encode(caption)) | |||
end | end | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
-- | -- ROW | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
local function | 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 | end | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
| خط ۲۳۵: | خط ۲۰۲: | ||
local function addRows(root, args, layout) | local function addRows(root, args, layout) | ||
for _, key in ipairs(layout) do | |||
local field = data.fields[key] | |||
local field = data.fields[ | |||
if field then | if field then | ||
local value = getArg(args, field) | 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 | ||
end | end | ||
end | end | ||
| خط ۲۶۴: | خط ۲۲۹: | ||
local function render(args) | local function render(args) | ||
local rawType = args | local rawType = getArg(args, "نوع") or "city" | ||
local layout = data.layouts[rawType] or data.layouts.city | |||
local layout = | |||
local root = newTable(rawType) | local root = newTable(rawType) | ||
addTitle(root, args) | addTitle(root, args) | ||
addBadge(root, rawType) | addBadge(root, rawType) | ||
addImage(root, args) | addImage(root, args) | ||
addCaption(root, args) | addCaption(root, args) | ||
addRows(root, args, layout) | addRows(root, args, layout) | ||
return tostring(root) | return tostring(root) | ||
end | end | ||
| خط ۲۹۱: | خط ۲۴۸: | ||
function p.main(frame) | function p.main(frame) | ||
return render(getArgs(frame)) | |||
end | end | ||
return p | return p | ||
نسخهٔ ۴ ژوئیهٔ ۲۰۲۶، ساعت ۰۷:۳۱
توضیحات این پودمان میتواند در پودمان: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