پودمان:AlborzInfoBox: تفاوت میان نسخهها
ظاهر
جزبدون خلاصۀ ویرایش |
جزبدون خلاصۀ ویرایش |
||
| خط ۴: | خط ۴: | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
-- 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, "[ | -- فقط نیمفاصله (ZWNJ) را حذف میکنیم، فاصله معمولی را نگه میداریم | ||
s = mw.ustring.gsub(s, "[\u200C\u200D]", "") | |||
s = mw.text.trim(s) | |||
return s | return s | ||
end | end | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
-- ARGS | -- GET ARGS | ||
------------------------------------------------------- | ------------------------------------------------------- | ||
local function getArgs(frame) | local function getArgs(frame) | ||
local args = {} | 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 | ||
end | end | ||
addArgs(frame.args) | |||
local parent = frame:getParent() | local parent = frame:getParent() | ||
if parent then | if parent then | ||
addArgs(parent.args) | |||
end | end | ||
return args | return args | ||
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 | ||
| خط ۵۴: | خط ۴۸: | ||
:addClass("infobox alborz-infobox") | :addClass("infobox alborz-infobox") | ||
-- TITLE | -- TITLE | ||
local title = args[norm("نام")] or mw.title.getCurrentTitle().text | |||
local title = args["نام"] or mw.title.getCurrentTitle().text | |||
root:tag("tr") | root:tag("tr") | ||
:tag("th") | :tag("th") | ||
| خط ۶۵: | خط ۵۵: | ||
:wikitext(mw.text.encode(title)) | :wikitext(mw.text.encode(title)) | ||
-- IMAGE | -- IMAGE | ||
if args[norm("تصویر")] and args[norm("تصویر")] ~= "" then | |||
if args["تصویر"] and args["تصویر"] ~= "" then | |||
root:tag("tr") | root:tag("tr") | ||
:tag("td") | :tag("td") | ||
:attr("colspan", "2") | :attr("colspan", "2") | ||
:wikitext("[[پرونده:" .. args["تصویر"] .. "|250px]]") | :wikitext("[[پرونده:" .. mw.text.encode(args[norm("تصویر")]) .. "|250px]]") | ||
end | end | ||
-- CAPTION | -- CAPTION | ||
if args[norm("توضیح تصویر")] and args[norm("توضیح تصویر")] ~= "" then | |||
if args["توضیح تصویر"] and args["توضیح تصویر"] ~= "" then | |||
root:tag("tr") | root:tag("tr") | ||
:tag("td") | :tag("td") | ||
| خط ۸۶: | خط ۷۰: | ||
:css("font-size", "90%") | :css("font-size", "90%") | ||
:css("text-align", "center") | :css("text-align", "center") | ||
:wikitext(mw.text.encode(args["توضیح تصویر"])) | :wikitext(mw.text.encode(args[norm("توضیح تصویر")])) | ||
end | end | ||
-- FIELDS | |||
for _, key in ipairs(layout) do | |||
-- FIELDS | |||
for | |||
local field = data.fields[key] | local field = data.fields[key] | ||
if field then | if field then | ||
local | local argKey = norm(field.arg) | ||
local value = args[argKey] or args[field.arg] | |||
if (not value or value == "") and field.default then | if (not value or value == "") and field.default then | ||
| خط ۱۱۹: | خط ۸۶: | ||
if value and value ~= "" then | if value and value ~= "" then | ||
local tr = root:tag("tr") | local tr = root:tag("tr") | ||
tr:tag("th"):wikitext(field.label) | tr:tag("th"):wikitext(field.label) | ||
tr:tag("td"):wikitext(mw.text.encode(value)) | tr:tag("td"):wikitext(mw.text.encode(value)) | ||
| خط ۱۲۸: | خط ۹۴: | ||
return tostring(root) | return tostring(root) | ||
end | end | ||
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)
-- فقط نیمفاصله (ZWNJ) را حذف میکنیم، فاصله معمولی را نگه میداریم
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
-------------------------------------------------------
-- RENDER
-------------------------------------------------------
local function render(args)
local rawType = norm(args["نوع"]) or "city"
local layout = data.layouts[rawType] or data.layouts.city
local root = mw.html.create("table")
:addClass("infobox alborz-infobox")
-- TITLE
local title = args[norm("نام")] or mw.title.getCurrentTitle().text
root:tag("tr")
:tag("th")
:attr("colspan", "2")
:wikitext(mw.text.encode(title))
-- IMAGE
if args[norm("تصویر")] and args[norm("تصویر")] ~= "" then
root:tag("tr")
:tag("td")
:attr("colspan", "2")
:wikitext("[[پرونده:" .. mw.text.encode(args[norm("تصویر")]) .. "|250px]]")
end
-- CAPTION
if args[norm("توضیح تصویر")] and args[norm("توضیح تصویر")] ~= "" then
root:tag("tr")
:tag("td")
:attr("colspan", "2")
:css("font-size", "90%")
:css("text-align", "center")
:wikitext(mw.text.encode(args[norm("توضیح تصویر")]))
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
if value and value ~= "" then
local tr = root:tag("tr")
tr:tag("th"):wikitext(field.label)
tr:tag("td"):wikitext(mw.text.encode(value))
end
end
end
return tostring(root)
end
function p.main(frame)
return render(getArgs(frame))
end
return p