پودمان: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 | 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 | -- GET ARG | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
local function getArg(args, field) | |||
if not field then return nil end | |||
if field.args then | |||
for i = 1, #field.args do | |||
local val = args[norm(field.args[i])] | |||
if val and val ~= "" then return val end | |||
end | |||
end | |||
if field.arg then | |||
return args[norm(field.arg)] | |||
end | |||
return nil | |||
end | |||
------------------------------------------------------- | |||
-- GET ARGS | |||
------------------------------------------------------- | |||
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 return end | ||
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[norm(k)] = mw.text.trim(v) | |||
end | end | ||
end | end | ||
end | end | ||
add(frame.args) | 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 | end | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
-- | -- ADD ROW (اصلاح شده) | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
local function addRow(root, label, value) | |||
local function | if not value or value == "" then return end | ||
return | local tr = root:tag("tr") | ||
tr:tag("th"):wikitext(label) | |||
tr:tag("td"):wikitext(mw.text.encode(value)) | |||
end | end | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
-- | -- BADGE | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
local badgeMap = { | local badgeMap = { | ||
city = "شهر", | city = "شهر", | ||
| خط ۱۰۱: | خط ۷۰: | ||
district = "بخش", | district = "بخش", | ||
ruraldistrict = "دهستان", | ruraldistrict = "دهستان", | ||
mountain = "کوه", | mountain = "کوه", | ||
cave = "غار", | |||
river = "رودخانه", | river = "رودخانه", | ||
waterfall = "آبشار", | waterfall = "آبشار", | ||
park = "پارک", | park = "پارک", | ||
historic = "بنای تاریخی" | |||
historic = "بنای تاریخی | |||
} | } | ||
local function addBadge(root, rawType) | local function addBadge(root, rawType) | ||
if not rawType or rawType == "" then | 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 | ||
| خط ۱۲۳: | خط ۸۶: | ||
:tag("td") | :tag("td") | ||
:attr("colspan", "2") | :attr("colspan", "2") | ||
: | :addClass("infobox-badge") | ||
:tag("span") | :tag("span") | ||
: | :addClass("infobox-badge-label") | ||
:wikitext(label) | :wikitext(label) | ||
end | end | ||
| خط ۱۳۹: | خط ۹۵: | ||
-- IMAGE | -- IMAGE | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
local function addImage(root, args) | local function addImage(root, args) | ||
local image = | local image = args[norm("تصویر")] or args[norm("پرونده")] | ||
if not image then return end | |||
if not image then | |||
image = mw.text.trim(image) | image = mw.text.trim(image) | ||
root:tag("tr") | root:tag("tr") | ||
:tag("td") | :tag("td") | ||
:attr("colspan", "2") | :attr("colspan", "2") | ||
: | :addClass("infobox-image") | ||
:wikitext("[[پرونده:" .. image .. "|250px]]") | :wikitext("[[پرونده:" .. mw.uri.encode(image, "WIKI") .. "|250px]]") | ||
end | end | ||
| خط ۱۵۹: | خط ۱۰۹: | ||
-- CAPTION | -- CAPTION | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
local function addCaption(root, args) | local function addCaption(root, args) | ||
local caption = | local caption = args[norm("توضیح تصویر")] or args[norm("عنوان تصویر")] or args[norm("زیرنویس")] | ||
if not caption then return end | |||
if not caption then | |||
root:tag("tr") | root:tag("tr") | ||
:tag("td") | :tag("td") | ||
:attr("colspan", "2") | :attr("colspan", "2") | ||
: | :addClass("infobox-caption") | ||
:wikitext(mw.text.encode(caption)) | :wikitext(mw.text.encode(caption)) | ||
end | end | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
-- | -- TABLE | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
local function newTable(rawType) | |||
rawType = rawType or "city" | |||
return mw.html.create("table") | |||
:addClass("infobox") | |||
:addClass("alborz-infobox") | |||
:addClass("infobox-type-" .. rawType) | |||
end | |||
local function | ------------------------------------------------------- | ||
-- TITLE | |||
------------------------------------------------------- | |||
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") | |||
:addClass("infobox-title") | |||
:wikitext(mw.text.encode(title)) | |||
end | |||
------------------------------------------------------- | ------------------------------------------------------- | ||
-- ROWS | -- 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 | ||
local field = data.fields[key] | local field = data.fields[key] | ||
if field then | if field then | ||
local value = getArg(args, field) | |||
local value = | |||
if (not value or value == "") and field.default then | if (not value or value == "") and field.default then | ||
value = field.default | value = field.default | ||
end | end | ||
if value and value ~= "" then | if value and value ~= "" then | ||
addRow(root, field.label, value) | addRow(root, field.label, value) | ||
end | end | ||
end | end | ||
end | end | ||
| خط ۲۲۶: | خط ۱۶۵: | ||
-- RENDER | -- RENDER | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
local function render(args) | local function render(args) | ||
local rawType = norm(args["نوع"]) or "city" | |||
local rawType = | |||
local layout = data.layouts[rawType] or data.layouts.city | local layout = data.layouts[rawType] or data.layouts.city | ||
| خط ۲۴۶: | خط ۱۸۳: | ||
-- MAIN | -- MAIN | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
function p.main(frame) | function p.main(frame) | ||
local args = getArgs(frame) | |||
return render(args) | |||
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
-------------------------------------------------------
-- GET ARG
-------------------------------------------------------
local function getArg(args, field)
if not field then return nil end
if field.args then
for i = 1, #field.args do
local val = args[norm(field.args[i])]
if val and val ~= "" then return val end
end
end
if field.arg then
return args[norm(field.arg)]
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
args[norm(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
-------------------------------------------------------
-- 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 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 rawType
root:tag("tr")
:tag("td")
:attr("colspan", "2")
:addClass("infobox-badge")
:tag("span")
:addClass("infobox-badge-label")
:wikitext(label)
end
-------------------------------------------------------
-- IMAGE
-------------------------------------------------------
local function addImage(root, args)
local image = args[norm("تصویر")] or args[norm("پرونده")]
if not image then return end
image = mw.text.trim(image)
root:tag("tr")
:tag("td")
:attr("colspan", "2")
:addClass("infobox-image")
:wikitext("[[پرونده:" .. mw.uri.encode(image, "WIKI") .. "|250px]]")
end
-------------------------------------------------------
-- CAPTION
-------------------------------------------------------
local function addCaption(root, args)
local caption = args[norm("توضیح تصویر")] or args[norm("عنوان تصویر")] or args[norm("زیرنویس")]
if not caption then return end
root:tag("tr")
:tag("td")
:attr("colspan", "2")
:addClass("infobox-caption")
: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-" .. rawType)
end
-------------------------------------------------------
-- TITLE
-------------------------------------------------------
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")
:addClass("infobox-title")
:wikitext(mw.text.encode(title))
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)
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 = norm(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)
local args = getArgs(frame)
return render(args)
end
return p