پودمان:AlborzEducation: تفاوت میان نسخهها
ظاهر
جزبدون خلاصۀ ویرایش |
جزبدون خلاصۀ ویرایش |
||
| خط ۵: | خط ۵: | ||
-- + اضافه شدن inherit و بهبودهای دیگر | -- + اضافه شدن inherit و بهبودهای دیگر | ||
-- + پشتیبانی از layout تخصصی آموزشگاه هنری | -- + پشتیبانی از layout تخصصی آموزشگاه هنری | ||
-- + تشخیص هوشمند و مقاوم layout | |||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local p = {} | local p = {} | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- CONFIGURATION | -- CONFIGURATION | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local INFOBOX_CLASS = "infobox education-infobox" | local INFOBOX_CLASS = "infobox education-infobox" | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- LOAD DATA | -- LOAD DATA | ||
| خط ۲۱: | خط ۲۰: | ||
local layouts = data.layouts | local layouts = data.layouts | ||
local groups = data.groups | local groups = data.groups | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- BASIC UTILITIES | -- BASIC UTILITIES | ||
| خط ۷۱: | خط ۶۹: | ||
return value:match("^{{") or value:match("^%[") or value:match("^%[%[") | return value:match("^{{") or value:match("^%[") or value:match("^%[%[") | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- IMAGE HELPER | -- IMAGE HELPER | ||
| خط ۸۲: | خط ۷۹: | ||
return string.format("[[پرونده:%s|%s|center]]", file, width) | return string.format("[[پرونده:%s|%s|center]]", file, width) | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- URL & ID HELPERS | -- URL & ID HELPERS | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local function normalizeUrl(url) | local function normalizeUrl(url) | ||
| خط ۱۱۱: | خط ۱۰۷: | ||
return value | return value | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- BOOLEAN | -- BOOLEAN | ||
| خط ۱۲۷: | خط ۱۲۲: | ||
return "ندارد" | return "ندارد" | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- LIST HELPER | -- LIST HELPER | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local function formatList(value) | local function formatList(value) | ||
| خط ۱۳۵: | خط ۱۲۹: | ||
return value | return value | ||
end | end | ||
-- اول ویرگول فارسی را به انگلیسی تبدیل کن تا gsplit درست کار کند | -- اول ویرگول فارسی را به انگلیسی تبدیل کن تا gsplit درست کار کند | ||
value = value:gsub("،", ",") | value = value:gsub("،", ",") | ||
local items = {} | local items = {} | ||
for item in mw.text.gsplit(value, ",") do | for item in mw.text.gsplit(value, ",") do | ||
| خط ۱۴۶: | خط ۱۳۸: | ||
end | end | ||
end | end | ||
if #items == 0 then | if #items == 0 then | ||
return value | return value | ||
| خط ۱۵۳: | خط ۱۴۴: | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- FORMATTERS | -- FORMATTERS | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local formatters = { | local formatters = { | ||
| خط ۱۶۳: | خط ۱۵۴: | ||
return string.format("[%s وبگاه رسمی]", url) | return string.format("[%s وبگاه رسمی]", url) | ||
end, | end, | ||
email = function(value) | email = function(value) | ||
if isAlreadyFormatted(value) then | if isAlreadyFormatted(value) then | ||
| خط ۱۷۰: | خط ۱۶۰: | ||
return string.format("[mailto:%s %s]", value, value) | return string.format("[mailto:%s %s]", value, value) | ||
end, | end, | ||
instagram = function(value) | instagram = function(value) | ||
if isAlreadyFormatted(value) then | if isAlreadyFormatted(value) then | ||
| خط ۱۷۸: | خط ۱۶۷: | ||
return string.format("[https://instagram.com/%s @%s]", id, id) | return string.format("[https://instagram.com/%s @%s]", id, id) | ||
end, | end, | ||
telegram = function(value) | telegram = function(value) | ||
if isAlreadyFormatted(value) then | if isAlreadyFormatted(value) then | ||
| خط ۱۸۶: | خط ۱۷۴: | ||
return string.format("[https://t.me/%s @%s]", id, id) | return string.format("[https://t.me/%s @%s]", id, id) | ||
end, | end, | ||
aparat = function(value) | aparat = function(value) | ||
if isAlreadyFormatted(value) then | if isAlreadyFormatted(value) then | ||
| خط ۱۹۴: | خط ۱۸۱: | ||
return string.format("[https://www.aparat.com/%s صفحهٔ آپارات]", id) | return string.format("[https://www.aparat.com/%s صفحهٔ آپارات]", id) | ||
end, | end, | ||
wikipedia = function(value) | wikipedia = function(value) | ||
if isAlreadyFormatted(value) then | if isAlreadyFormatted(value) then | ||
| خط ۲۰۱: | خط ۱۸۷: | ||
return string.format("[[%s]]", value) | return string.format("[[%s]]", value) | ||
end, | end, | ||
url = function(value) | url = function(value) | ||
if isAlreadyFormatted(value) then | if isAlreadyFormatted(value) then | ||
| خط ۲۰۹: | خط ۱۹۴: | ||
return string.format("[%s %s]", url, value) | return string.format("[%s %s]", url, value) | ||
end, | end, | ||
boolean = function(value) | boolean = function(value) | ||
return formatBoolean(value) | return formatBoolean(value) | ||
end, | end, | ||
list = function(value) | list = function(value) | ||
return formatList(value) | return formatList(value) | ||
end, | end, | ||
person = function(value) | person = function(value) | ||
if isAlreadyFormatted(value) then | if isAlreadyFormatted(value) then | ||
| خط ۲۲۴: | خط ۲۰۶: | ||
return string.format("[[%s]]", value) | return string.format("[[%s]]", value) | ||
end, | end, | ||
location = function(value) | location = function(value) | ||
if isAlreadyFormatted(value) then | if isAlreadyFormatted(value) then | ||
| خط ۲۳۱: | خط ۲۱۲: | ||
return string.format("[[%s]]", value) | return string.format("[[%s]]", value) | ||
end, | end, | ||
coordinates = function(value) | coordinates = function(value) | ||
if isAlreadyFormatted(value) then | if isAlreadyFormatted(value) then | ||
| خط ۲۴۴: | خط ۲۲۴: | ||
return nil | return nil | ||
end | end | ||
local formatterName = (fieldDef and fieldDef.formatter) or fieldName | local formatterName = (fieldDef and fieldDef.formatter) or fieldName | ||
if fieldDef and fieldDef.type and not formatters[formatterName] then | if fieldDef and fieldDef.type and not formatters[formatterName] then | ||
if fieldDef.type == "boolean" then | if fieldDef.type == "boolean" then | ||
| خط ۲۵۸: | خط ۲۳۶: | ||
end | end | ||
end | end | ||
local formatter = formatters[formatterName] | local formatter = formatters[formatterName] | ||
if formatter then | if formatter then | ||
return formatter(value) | return formatter(value) | ||
end | end | ||
return value | return value | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- FIELD RENDERING | -- FIELD RENDERING | ||
| خط ۲۷۵: | خط ۲۵۰: | ||
return nil | return nil | ||
end | end | ||
if field.type == "image" then | if field.type == "image" then | ||
local width = (fieldName == "logo") and "120px" or "250px" | local width = (fieldName == "logo") and "120px" or "250px" | ||
| خط ۲۸۶: | خط ۲۶۰: | ||
:done() | :done() | ||
end | end | ||
value = formatValue(fieldName, value, field) | value = formatValue(fieldName, value, field) | ||
local row = mw.html.create("tr") | local row = mw.html.create("tr") | ||
row:tag("th"):wikitext(field.label or fieldName) | row:tag("th"):wikitext(field.label or fieldName) | ||
| خط ۲۹۴: | خط ۲۶۶: | ||
return row | return row | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- TITLE & LOGO | -- TITLE & LOGO | ||
| خط ۳۰۱: | خط ۲۷۲: | ||
local logo = getFieldValue(args, "logo") | local logo = getFieldValue(args, "logo") | ||
local title = getFieldValue(args, "fullname") or getFieldValue(args, "name") | local title = getFieldValue(args, "fullname") or getFieldValue(args, "name") | ||
if logo then | if logo then | ||
box:tag("tr") | box:tag("tr") | ||
| خط ۳۱۱: | خط ۲۸۱: | ||
:wikitext(imageMarkup(logo, "120px")) | :wikitext(imageMarkup(logo, "120px")) | ||
end | end | ||
if title then | if title then | ||
box:tag("tr") | box:tag("tr") | ||
| خط ۳۲۴: | خط ۲۹۳: | ||
end | end | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- MAIN IMAGE + CAPTION | -- MAIN IMAGE + CAPTION | ||
| خط ۳۳۳: | خط ۳۰۱: | ||
return | return | ||
end | end | ||
box:tag("tr") | box:tag("tr") | ||
:tag("td") | :tag("td") | ||
| خط ۳۴۰: | خط ۳۰۷: | ||
:css("text-align", "center") | :css("text-align", "center") | ||
:wikitext(imageMarkup(image, "250px")) | :wikitext(imageMarkup(image, "250px")) | ||
local caption = getFieldValue(args, "caption") | local caption = getFieldValue(args, "caption") | ||
if caption then | if caption then | ||
| خط ۳۵۳: | خط ۳۱۹: | ||
end | end | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- GROUP | -- GROUP | ||
| خط ۳۶۸: | خط ۳۳۳: | ||
return | return | ||
end | end | ||
local group = groups[groupName] | local group = groups[groupName] | ||
if group then | if group then | ||
| خط ۳۸۰: | خط ۳۴۴: | ||
:wikitext((group.icon or "") .. " " .. (group.label or groupName)) | :wikitext((group.icon or "") .. " " .. (group.label or groupName)) | ||
end | end | ||
for _, field in ipairs(fieldList) do | for _, field in ipairs(fieldList) do | ||
if not rendered[field] then | if not rendered[field] then | ||
| خط ۳۹۴: | خط ۳۵۷: | ||
end | end | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- LAYOUT MAP | -- LAYOUT MAP | ||
| خط ۴۱۸: | خط ۳۸۰: | ||
["school"] = "school", | ["school"] = "school", | ||
} | } | ||
-------------------------------------------------------------------------------- | |||
-- تشخیص هوشمند layout (مقاوم در برابر فاصله و تغییرات جزئی) | |||
-------------------------------------------------------------------------------- | |||
local function detectLayout(typeValue) | |||
if not typeValue or typeValue == "" then | |||
return "school" | |||
end | |||
local t = mw.ustring.lower(typeValue) | |||
-- ۱. مطابقت دقیق | |||
if layoutMap[t] then | |||
return layoutMap[t] | |||
end | |||
if layoutMap[typeValue] then | |||
return layoutMap[typeValue] | |||
end | |||
-- ۲. بررسی کلمات کلیدی (اولویت با موارد خاصتر) | |||
if t:find("هنری") or t:find("هنر") or t:find("آکادمی هنری") or t:find("آکادمی هنر") then | |||
return "art_school" | |||
end | |||
if t:find("دانشگاه") then | |||
return "university" | |||
end | |||
if t:find("حوزه") or t:find("علمیه") then | |||
return "seminary" | |||
end | |||
if t:find("پژوهش") then | |||
return "research_center" | |||
end | |||
if t:find("کالج") then | |||
return "college" | |||
end | |||
if t:find("مهد") or t:find("کودک") then | |||
return "kindergarten" | |||
end | |||
if t:find("آموزشگاه") or t:find("مرکز آموزشی") or t:find("آموزشی") then | |||
return "training_center" | |||
end | |||
if t:find("مدرسه") then | |||
return "school" | |||
end | |||
return "school" | |||
end | |||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- RESOLVE INHERIT | -- RESOLVE INHERIT | ||
| خط ۴۲۵: | خط ۴۳۲: | ||
local seen = {} | local seen = {} | ||
local current = layoutName | local current = layoutName | ||
while current do | while current do | ||
if seen[current] then | if seen[current] then | ||
| خط ۴۳۱: | خط ۴۳۷: | ||
end | end | ||
seen[current] = true | seen[current] = true | ||
local layout = layouts[current] | local layout = layouts[current] | ||
if not layout then | if not layout then | ||
return layouts.school or {} | return layouts.school or {} | ||
end | end | ||
if layout.inherit then | if layout.inherit then | ||
current = layout.inherit | current = layout.inherit | ||
| خط ۴۴۳: | خط ۴۴۷: | ||
end | end | ||
end | end | ||
return layouts.school or {} | return layouts.school or {} | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- RENDER LAYOUT | -- RENDER LAYOUT | ||
| خط ۴۵۲: | خط ۴۵۴: | ||
local function renderLayout(layoutName, args) | local function renderLayout(layoutName, args) | ||
local layout = resolveLayout(layoutName) | local layout = resolveLayout(layoutName) | ||
local box = mw.html.create("table") | local box = mw.html.create("table") | ||
:addClass(INFOBOX_CLASS) | :addClass(INFOBOX_CLASS) | ||
:addClass("education-" .. layoutName) | :addClass("education-" .. layoutName) | ||
local rendered = {} | local rendered = {} | ||
renderTitle(box, args) | renderTitle(box, args) | ||
rendered.logo = true | rendered.logo = true | ||
renderImage(box, args) | renderImage(box, args) | ||
rendered.image = true | rendered.image = true | ||
rendered.caption = true | rendered.caption = true | ||
for _, section in ipairs(layout) do | for _, section in ipairs(layout) do | ||
renderGroup(box, section.group, section.fields, args, rendered) | renderGroup(box, section.group, section.fields, args, rendered) | ||
end | end | ||
return tostring(box) | return tostring(box) | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- MAIN | -- MAIN | ||
| خط ۴۷۸: | خط ۴۷۳: | ||
function p.main(frame) | function p.main(frame) | ||
local args = normalizeArgs(getArgs(frame)) | local args = normalizeArgs(getArgs(frame)) | ||
local layout = detectLayout(args.type) | |||
local | |||
return renderLayout(layout, args) | return renderLayout(layout, args) | ||
end | end | ||
return p | return p | ||
نسخهٔ ۲۵ اوت ۲۰۲۶، ساعت ۲۱:۴۵
توضیحات این پودمان میتواند در پودمان:AlborzEducation/توضیحات قرار گیرد.
--------------------------------------------------------------------------------
-- Module:AlborzEducation
-- Wiki Alborz Education Infobox Engine
-- پایه: نسخهای که وبگاه و تلگرام+شماره درست کار میکند
-- + اضافه شدن inherit و بهبودهای دیگر
-- + پشتیبانی از layout تخصصی آموزشگاه هنری
-- + تشخیص هوشمند و مقاوم layout
--------------------------------------------------------------------------------
local p = {}
--------------------------------------------------------------------------------
-- CONFIGURATION
--------------------------------------------------------------------------------
local INFOBOX_CLASS = "infobox education-infobox"
--------------------------------------------------------------------------------
-- LOAD DATA
--------------------------------------------------------------------------------
local data = mw.loadData("Module:AlborzEducation/Data")
local lookup = data.lookup
local fields = data.fieldDefinitions
local layouts = data.layouts
local groups = data.groups
--------------------------------------------------------------------------------
-- BASIC UTILITIES
--------------------------------------------------------------------------------
local function trim(value)
if value == nil then
return nil
end
value = mw.text.trim(tostring(value))
if value == "" then
return nil
end
return value
end
local function getArgs(frame)
local args = {}
for k, v in pairs(frame.args) do
k = trim(k)
v = trim(v)
if k and v then
args[k] = v
end
end
return args
end
local function normalizeArgs(args)
local result = {}
for key, value in pairs(args) do
local newKey = lookup[key] or key
result[newKey] = value
end
return result
end
local function getFieldValue(args, name)
if args[name] then
return args[name]
end
local def = fields[name]
if def and def.default then
return def.default
end
return nil
end
local function isAlreadyFormatted(value)
return value:match("^{{") or value:match("^%[") or value:match("^%[%[")
end
--------------------------------------------------------------------------------
-- IMAGE HELPER
--------------------------------------------------------------------------------
local function imageMarkup(file, width)
if not file then
return nil
end
width = width or "250px"
return string.format("[[پرونده:%s|%s|center]]", file, width)
end
--------------------------------------------------------------------------------
-- URL & ID HELPERS
--------------------------------------------------------------------------------
local function normalizeUrl(url)
url = trim(url)
if not url then
return nil
end
if not url:match("^https?://") then
url = "https://" .. url
end
return url
end
local function extractId(value)
value = trim(value)
if not value then
return nil
end
value = value:gsub("^https?://", "")
value = value:gsub("^www%.", "")
value = value:gsub("^instagram%.com/", "")
value = value:gsub("^t%.me/", "")
value = value:gsub("^aparat%.com/", "")
value = value:gsub("^@", "")
value = value:gsub("/$", "")
return value
end
--------------------------------------------------------------------------------
-- BOOLEAN
--------------------------------------------------------------------------------
local function formatBoolean(value)
local yes = {
["بله"] = true, ["بلی"] = true,
["yes"] = true, ["Yes"] = true, ["YES"] = true,
["true"] = true, ["True"] = true, ["TRUE"] = true,
["1"] = true
}
if yes[tostring(value)] then
return "دارد"
end
return "ندارد"
end
--------------------------------------------------------------------------------
-- LIST HELPER
--------------------------------------------------------------------------------
local function formatList(value)
if isAlreadyFormatted(value) then
return value
end
-- اول ویرگول فارسی را به انگلیسی تبدیل کن تا gsplit درست کار کند
value = value:gsub("،", ",")
local items = {}
for item in mw.text.gsplit(value, ",") do
item = trim(item)
if item and item ~= "" then
table.insert(items, "* " .. item)
end
end
if #items == 0 then
return value
end
return table.concat(items, "\n")
end
--------------------------------------------------------------------------------
-- FORMATTERS
--------------------------------------------------------------------------------
local formatters = {
website = function(value)
if isAlreadyFormatted(value) then
return value
end
local url = normalizeUrl(value)
return string.format("[%s وبگاه رسمی]", url)
end,
email = function(value)
if isAlreadyFormatted(value) then
return value
end
return string.format("[mailto:%s %s]", value, value)
end,
instagram = function(value)
if isAlreadyFormatted(value) then
return value
end
local id = extractId(value)
return string.format("[https://instagram.com/%s @%s]", id, id)
end,
telegram = function(value)
if isAlreadyFormatted(value) then
return value
end
local id = extractId(value)
return string.format("[https://t.me/%s @%s]", id, id)
end,
aparat = function(value)
if isAlreadyFormatted(value) then
return value
end
local id = extractId(value)
return string.format("[https://www.aparat.com/%s صفحهٔ آپارات]", id)
end,
wikipedia = function(value)
if isAlreadyFormatted(value) then
return value
end
return string.format("[[%s]]", value)
end,
url = function(value)
if isAlreadyFormatted(value) then
return value
end
local url = normalizeUrl(value)
return string.format("[%s %s]", url, value)
end,
boolean = function(value)
return formatBoolean(value)
end,
list = function(value)
return formatList(value)
end,
person = function(value)
if isAlreadyFormatted(value) then
return value
end
return string.format("[[%s]]", value)
end,
location = function(value)
if isAlreadyFormatted(value) then
return value
end
return string.format("[[%s]]", value)
end,
coordinates = function(value)
if isAlreadyFormatted(value) then
return value
end
return value
end,
}
local function formatValue(fieldName, value, fieldDef)
if not value then
return nil
end
local formatterName = (fieldDef and fieldDef.formatter) or fieldName
if fieldDef and fieldDef.type and not formatters[formatterName] then
if fieldDef.type == "boolean" then
formatterName = "boolean"
elseif fieldDef.type == "email" then
formatterName = "email"
elseif fieldDef.type == "url" then
formatterName = "url"
elseif fieldDef.type == "list" then
formatterName = "list"
end
end
local formatter = formatters[formatterName]
if formatter then
return formatter(value)
end
return value
end
--------------------------------------------------------------------------------
-- FIELD RENDERING
--------------------------------------------------------------------------------
local function renderField(fieldName, value)
local field = fields[fieldName]
if not field or not value then
return nil
end
if field.type == "image" then
local width = (fieldName == "logo") and "120px" or "250px"
return mw.html.create("tr")
:tag("td")
:attr("colspan", "2")
:addClass("education-infobox-image")
:css("text-align", "center")
:wikitext(imageMarkup(value, width))
:done()
end
value = formatValue(fieldName, value, field)
local row = mw.html.create("tr")
row:tag("th"):wikitext(field.label or fieldName)
row:tag("td"):wikitext(value)
return row
end
--------------------------------------------------------------------------------
-- TITLE & LOGO
--------------------------------------------------------------------------------
local function renderTitle(box, args)
local logo = getFieldValue(args, "logo")
local title = getFieldValue(args, "fullname") or getFieldValue(args, "name")
if logo then
box:tag("tr")
:tag("td")
:attr("colspan", "2")
:addClass("education-infobox-logo")
:css("text-align", "center")
:css("padding", "8px")
:wikitext(imageMarkup(logo, "120px"))
end
if title then
box:tag("tr")
:tag("th")
:attr("colspan", "2")
:addClass("education-infobox-title")
:css("text-align", "center")
:css("font-size", "120%")
:css("font-weight", "bold")
:css("padding", "8px")
:wikitext(title)
end
end
--------------------------------------------------------------------------------
-- MAIN IMAGE + CAPTION
--------------------------------------------------------------------------------
local function renderImage(box, args)
local image = getFieldValue(args, "image")
if not image then
return
end
box:tag("tr")
:tag("td")
:attr("colspan", "2")
:addClass("education-infobox-image")
:css("text-align", "center")
:wikitext(imageMarkup(image, "250px"))
local caption = getFieldValue(args, "caption")
if caption then
box:tag("tr")
:tag("td")
:attr("colspan", "2")
:addClass("education-infobox-caption")
:css("text-align", "center")
:css("font-size", "90%")
:css("padding", "4px 8px")
:wikitext(caption)
end
end
--------------------------------------------------------------------------------
-- GROUP
--------------------------------------------------------------------------------
local function renderGroup(box, groupName, fieldList, args, rendered)
local hasData = false
for _, field in ipairs(fieldList) do
if getFieldValue(args, field) and not rendered[field] then
hasData = true
break
end
end
if not hasData then
return
end
local group = groups[groupName]
if group then
box:tag("tr")
:tag("th")
:attr("colspan", "2")
:addClass("education-infobox-group")
:css("text-align", "center")
:css("font-weight", "bold")
:css("padding", "6px")
:wikitext((group.icon or "") .. " " .. (group.label or groupName))
end
for _, field in ipairs(fieldList) do
if not rendered[field] then
local value = getFieldValue(args, field)
if value then
local row = renderField(field, value)
if row then
box:node(row)
rendered[field] = true
end
end
end
end
end
--------------------------------------------------------------------------------
-- LAYOUT MAP
--------------------------------------------------------------------------------
local layoutMap = {
["دانشگاه"] = "university",
["university"] = "university",
["حوزه علمیه"] = "seminary",
["seminary"] = "seminary",
["آموزشگاه"] = "training_center",
["training_center"] = "training_center",
["آموزشگاه هنری"] = "art_school",
["آموزشگاه هنر"] = "art_school",
["هنری"] = "art_school",
["art_school"] = "art_school",
["پژوهشکده"] = "research_center",
["research_center"] = "research_center",
["کالج"] = "college",
["college"] = "college",
["مهدکودک"] = "kindergarten",
["kindergarten"] = "kindergarten",
["مدرسه"] = "school",
["school"] = "school",
}
--------------------------------------------------------------------------------
-- تشخیص هوشمند layout (مقاوم در برابر فاصله و تغییرات جزئی)
--------------------------------------------------------------------------------
local function detectLayout(typeValue)
if not typeValue or typeValue == "" then
return "school"
end
local t = mw.ustring.lower(typeValue)
-- ۱. مطابقت دقیق
if layoutMap[t] then
return layoutMap[t]
end
if layoutMap[typeValue] then
return layoutMap[typeValue]
end
-- ۲. بررسی کلمات کلیدی (اولویت با موارد خاصتر)
if t:find("هنری") or t:find("هنر") or t:find("آکادمی هنری") or t:find("آکادمی هنر") then
return "art_school"
end
if t:find("دانشگاه") then
return "university"
end
if t:find("حوزه") or t:find("علمیه") then
return "seminary"
end
if t:find("پژوهش") then
return "research_center"
end
if t:find("کالج") then
return "college"
end
if t:find("مهد") or t:find("کودک") then
return "kindergarten"
end
if t:find("آموزشگاه") or t:find("مرکز آموزشی") or t:find("آموزشی") then
return "training_center"
end
if t:find("مدرسه") then
return "school"
end
return "school"
end
--------------------------------------------------------------------------------
-- RESOLVE INHERIT
--------------------------------------------------------------------------------
local function resolveLayout(layoutName)
local seen = {}
local current = layoutName
while current do
if seen[current] then
return layouts.school or {}
end
seen[current] = true
local layout = layouts[current]
if not layout then
return layouts.school or {}
end
if layout.inherit then
current = layout.inherit
else
return layout
end
end
return layouts.school or {}
end
--------------------------------------------------------------------------------
-- RENDER LAYOUT
--------------------------------------------------------------------------------
local function renderLayout(layoutName, args)
local layout = resolveLayout(layoutName)
local box = mw.html.create("table")
:addClass(INFOBOX_CLASS)
:addClass("education-" .. layoutName)
local rendered = {}
renderTitle(box, args)
rendered.logo = true
renderImage(box, args)
rendered.image = true
rendered.caption = true
for _, section in ipairs(layout) do
renderGroup(box, section.group, section.fields, args, rendered)
end
return tostring(box)
end
--------------------------------------------------------------------------------
-- MAIN
--------------------------------------------------------------------------------
function p.main(frame)
local args = normalizeArgs(getArgs(frame))
local layout = detectLayout(args.type)
return renderLayout(layout, args)
end
return p