پودمان:AlborzInfoBox: تفاوت میان نسخهها
ظاهر
جزبدون خلاصۀ ویرایش |
جزبدون خلاصۀ ویرایش |
||
| خط ۴: | خط ۴: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- | -- SAFE NORMALIZER (حل مشکل کلیدهای مخفی) | ||
-------------------------------------------------------------------------------- | |||
local function norm(s) | |||
if not s then return nil end | |||
s = mw.text.trim(s) | |||
s = mw.ustring.gsub(s, "", "") -- حذف نیمفاصله | |||
return s | |||
end | |||
-------------------------------------------------------------------------------- | |||
-- RENDER | |||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local function render(args) | local function render(args) | ||
local layout = data.layouts[ | -- نرمالسازی کلیدها (خیلی مهم) | ||
local cleanArgs = {} | |||
for k, v in pairs(args) do | |||
cleanArgs[norm(k)] = norm(v) | |||
end | |||
local layout = data.layouts[cleanArgs["نوع"] or "city"] | |||
if not layout then | if not layout then | ||
layout = data.layouts.city | layout = data.layouts.city | ||
| خط ۱۵: | خط ۳۲: | ||
local root = mw.html.create("table") | local root = mw.html.create("table") | ||
:addClass("infobox | :addClass("infobox alborz-infobox") | ||
-------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ||
-- | -- TITLE | ||
-------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ||
local title = | local title = cleanArgs["نام"] or mw.title.getCurrentTitle().text | ||
root:tag("tr") | root:tag("tr") | ||
| خط ۳۰: | خط ۴۶: | ||
-------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ||
-- | -- IMAGE | ||
-------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ||
if | if cleanArgs["تصویر"] and cleanArgs["تصویر"] ~= "" then | ||
root:tag("tr") | root:tag("tr") | ||
:tag("td") | :tag("td") | ||
:attr("colspan","2") | :attr("colspan","2") | ||
:wikitext("[[پرونده:" .. | :wikitext("[[پرونده:" .. cleanArgs["تصویر"] .. "|250px]]") | ||
if | if cleanArgs["توضیح تصویر"] and cleanArgs["توضیح تصویر"] ~= "" then | ||
root:tag("tr") | root:tag("tr") | ||
:tag("td") | :tag("td") | ||
:attr("colspan","2") | :attr("colspan","2") | ||
:wikitext( | :wikitext(cleanArgs["توضیح تصویر"]) | ||
end | end | ||
| خط ۵۱: | خط ۶۷: | ||
-------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ||
-- | -- ROWS | ||
-------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ||
| خط ۶۳: | خط ۷۹: | ||
local value = nil | local value = nil | ||
if field.arg then | if field.arg then | ||
value = | value = cleanArgs[norm(field.arg)] | ||
end | end | ||
if (not value or value == "") and field.label then | if (not value or value == "") and field.label then | ||
value = | value = cleanArgs[norm(field.label)] | ||
end | end | ||
| خط ۹۲: | خط ۱۰۶: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- | -- MAIN | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| خط ۱۰۳: | خط ۱۱۷: | ||
for k, v in pairs(parent.args or {}) do | for k, v in pairs(parent.args or {}) do | ||
if v and v ~= "" then | if v and v ~= "" then | ||
args[k] = | args[k] = v | ||
end | end | ||
end | end | ||
| خط ۱۰۹: | خط ۱۲۳: | ||
for k, v in pairs(frame.args or {}) do | for k, v in pairs(frame.args or {}) do | ||
if v and v ~= "" then | if v and v ~= "" then | ||
args[k] = | args[k] = v | ||
end | end | ||
end | end | ||
نسخهٔ ۳ ژوئیهٔ ۲۰۲۶، ساعت ۱۶:۰۰
توضیحات این پودمان میتواند در پودمان:AlborzInfoBox/توضیحات قرار گیرد.
local p = {}
local data = mw.loadData('Module:AlborzInfoBox/Data/Geography')
--------------------------------------------------------------------------------
-- SAFE NORMALIZER (حل مشکل کلیدهای مخفی)
--------------------------------------------------------------------------------
local function norm(s)
if not s then return nil end
s = mw.text.trim(s)
s = mw.ustring.gsub(s, "", "") -- حذف نیمفاصله
return s
end
--------------------------------------------------------------------------------
-- RENDER
--------------------------------------------------------------------------------
local function render(args)
-- نرمالسازی کلیدها (خیلی مهم)
local cleanArgs = {}
for k, v in pairs(args) do
cleanArgs[norm(k)] = norm(v)
end
local layout = data.layouts[cleanArgs["نوع"] or "city"]
if not layout then
layout = data.layouts.city
end
local root = mw.html.create("table")
:addClass("infobox alborz-infobox")
--------------------------------------------------------------------------
-- TITLE
--------------------------------------------------------------------------
local title = cleanArgs["نام"] or mw.title.getCurrentTitle().text
root:tag("tr")
:tag("th")
:attr("colspan","2")
:wikitext(title)
--------------------------------------------------------------------------
-- IMAGE
--------------------------------------------------------------------------
if cleanArgs["تصویر"] and cleanArgs["تصویر"] ~= "" then
root:tag("tr")
:tag("td")
:attr("colspan","2")
:wikitext("[[پرونده:" .. cleanArgs["تصویر"] .. "|250px]]")
if cleanArgs["توضیح تصویر"] and cleanArgs["توضیح تصویر"] ~= "" then
root:tag("tr")
:tag("td")
:attr("colspan","2")
:wikitext(cleanArgs["توضیح تصویر"])
end
end
--------------------------------------------------------------------------
-- ROWS
--------------------------------------------------------------------------
for i = 1, #layout do
local key = layout[i]
local field = data.fields[key]
if field then
local value = nil
if field.arg then
value = cleanArgs[norm(field.arg)]
end
if (not value or value == "") and field.label then
value = cleanArgs[norm(field.label)]
end
if value and value ~= "" then
local tr = root:tag("tr")
tr:tag("th")
:wikitext(field.label)
tr:tag("td")
:wikitext(value)
end
end
end
return tostring(root)
end
--------------------------------------------------------------------------------
-- MAIN
--------------------------------------------------------------------------------
function p.main(frame)
local args = {}
local parent = frame:getParent() or frame
for k, v in pairs(parent.args or {}) do
if v and v ~= "" then
args[k] = v
end
end
for k, v in pairs(frame.args or {}) do
if v and v ~= "" then
args[k] = v
end
end
return render(args)
end
return p